@opengeni/worker-bundle 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +222 -0
- package/dist/index.js +3960 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
- package/src/activities/agent-turn.ts +2290 -0
- package/src/activities/capabilities.ts +131 -0
- package/src/activities/codex-auth.ts +9 -0
- package/src/activities/codex-rotation.ts +326 -0
- package/src/activities/common.ts +37 -0
- package/src/activities/context-compaction.ts +125 -0
- package/src/activities/documents.ts +67 -0
- package/src/activities/environment.ts +209 -0
- package/src/activities/goals.ts +281 -0
- package/src/activities/packs.ts +116 -0
- package/src/activities/parent-wake.ts +129 -0
- package/src/activities/recording.ts +248 -0
- package/src/activities/redaction.ts +54 -0
- package/src/activities/run-input.ts +331 -0
- package/src/activities/sandbox-lease.ts +612 -0
- package/src/activities/scheduled-tasks.ts +346 -0
- package/src/activities/session-state.ts +201 -0
- package/src/activities/streaming.ts +74 -0
- package/src/activities/types.ts +165 -0
- package/src/activities.ts +124 -0
- package/src/index.ts +184 -0
- package/src/sandbox-resume.ts +558 -0
- package/src/sandbox-routing.ts +232 -0
- package/src/workflows/activities.ts +23 -0
- package/src/workflows/document-index.ts +11 -0
- package/src/workflows/sandbox-reaper.ts +28 -0
- package/src/workflows/scheduled-tasks.ts +38 -0
- package/src/workflows/session.ts +376 -0
- package/src/workflows.ts +19 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { indexDocumentNow } from "@opengeni/documents";
|
|
2
|
+
import { configuredStaticUsageLimits } from "@opengeni/config";
|
|
3
|
+
import {
|
|
4
|
+
getBillingBalance,
|
|
5
|
+
recordUsageEvent,
|
|
6
|
+
sumUsageQuantity,
|
|
7
|
+
withWorkspaceUsageLock,
|
|
8
|
+
} from "@opengeni/db";
|
|
9
|
+
import type {
|
|
10
|
+
ActivityServices,
|
|
11
|
+
IndexDocumentInput,
|
|
12
|
+
} from "./types";
|
|
13
|
+
|
|
14
|
+
export function createDocumentActivities(services: () => Promise<ActivityServices>) {
|
|
15
|
+
return {
|
|
16
|
+
indexDocument: async (input: IndexDocumentInput) => {
|
|
17
|
+
const { settings, db, objectStorage, documentServices } = await services();
|
|
18
|
+
if (!objectStorage) {
|
|
19
|
+
throw new Error("object storage is not configured");
|
|
20
|
+
}
|
|
21
|
+
return await withWorkspaceUsageLock(db, input.workspaceId, async (lockedDb) => {
|
|
22
|
+
const document = await indexDocumentNow(lockedDb, objectStorage, input.workspaceId, input.documentId, documentServices, {
|
|
23
|
+
beforeEmbed: async ({ chunkCount }) => {
|
|
24
|
+
if (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed") {
|
|
25
|
+
const balance = await getBillingBalance(lockedDb, input.accountId);
|
|
26
|
+
if (balance.balanceMicros <= 0) {
|
|
27
|
+
throw new Error("insufficient OpenGeni credits");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (settings.usageLimitsMode !== "static" && settings.usageLimitsMode !== "managed") {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const limit = configuredStaticUsageLimits(settings).maxDocumentIndexedChunksPerWorkspace;
|
|
34
|
+
if (!limit) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const used = await sumUsageQuantity(lockedDb, {
|
|
38
|
+
workspaceId: input.workspaceId,
|
|
39
|
+
eventType: "document.indexed",
|
|
40
|
+
since: startOfUtcMonth(),
|
|
41
|
+
});
|
|
42
|
+
if (used + chunkCount > limit) {
|
|
43
|
+
throw new Error(`monthly document indexing limit reached (${limit} chunks)`);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
if (document.status === "ready") {
|
|
48
|
+
await recordUsageEvent(lockedDb, {
|
|
49
|
+
accountId: input.accountId,
|
|
50
|
+
workspaceId: input.workspaceId,
|
|
51
|
+
eventType: "document.indexed",
|
|
52
|
+
quantity: document.chunkCount,
|
|
53
|
+
unit: "chunk",
|
|
54
|
+
sourceResourceType: "document",
|
|
55
|
+
sourceResourceId: document.id,
|
|
56
|
+
idempotencyKey: `document.indexed:${input.workspaceId}:${document.id}:${document.updatedAt}`,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return document;
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function startOfUtcMonth(date = new Date()): Date {
|
|
66
|
+
return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1));
|
|
67
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import {
|
|
2
|
+
applyGitAuthPointerEnvironment,
|
|
3
|
+
stableSandboxEnvironmentForRun,
|
|
4
|
+
type Settings,
|
|
5
|
+
} from "@opengeni/config";
|
|
6
|
+
import {
|
|
7
|
+
type ConnectionCredentialsPort,
|
|
8
|
+
type GitCredentials,
|
|
9
|
+
type ResourceRef,
|
|
10
|
+
type SandboxSecrets,
|
|
11
|
+
} from "@opengeni/contracts";
|
|
12
|
+
import {
|
|
13
|
+
loadWorkspaceEnvironmentForRun as loadWorkspaceEnvironmentForRunFromDb,
|
|
14
|
+
type Database,
|
|
15
|
+
type WorkspaceEnvironmentForRun,
|
|
16
|
+
} from "@opengeni/db";
|
|
17
|
+
import {
|
|
18
|
+
createGitHubAppInstallationToken,
|
|
19
|
+
githubAppBotIdentity,
|
|
20
|
+
} from "@opengeni/github";
|
|
21
|
+
|
|
22
|
+
// Re-exported from the shared @opengeni/db leaf (moved there so the API-direct
|
|
23
|
+
// attach paths can load the SAME decrypted workspace environment the turn
|
|
24
|
+
// declares — keeping the box-manifest env and agent-manifest env identical).
|
|
25
|
+
// Existing worker import sites (agent-turn) continue importing from here.
|
|
26
|
+
export {
|
|
27
|
+
loadWorkspaceEnvironmentForRunFromDb as loadWorkspaceEnvironmentForRun,
|
|
28
|
+
type WorkspaceEnvironmentForRun,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// §7.6 P4a — the run's workspace identity, threaded so the connection-credential
|
|
32
|
+
// provider can be called with the run's tenant context AND so the FORK-7
|
|
33
|
+
// cross-check has the run's workspace to assert the provider's echo against.
|
|
34
|
+
export type ConnectionScope = {
|
|
35
|
+
accountId: string;
|
|
36
|
+
workspaceId: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// §7.6 P4a — load the run's workspace environment, delegating the DECRYPT to a
|
|
40
|
+
// host `sandboxSecrets` provider when one is bound (the host owns the secret
|
|
41
|
+
// vault + encryption key in embedded/separate topologies) and otherwise running
|
|
42
|
+
// today's local `environmentsEncryptionKeyBytes`-keyed decrypt byte-for-byte.
|
|
43
|
+
//
|
|
44
|
+
// Unattached runs (environmentId === null) short-circuit identically in BOTH
|
|
45
|
+
// modes: zero DB/provider work, returns null. When a provider IS bound it owns
|
|
46
|
+
// the decrypt end-to-end (it reads the host's own store), so the local DB read
|
|
47
|
+
// is skipped — the provider is the sole source of truth for that leg.
|
|
48
|
+
export async function loadWorkspaceEnvironmentForRunWithCredentials(
|
|
49
|
+
db: Database,
|
|
50
|
+
settings: Settings,
|
|
51
|
+
scope: ConnectionScope,
|
|
52
|
+
environmentId: string | null,
|
|
53
|
+
sandboxSecrets?: ConnectionCredentialsPort["sandboxSecrets"],
|
|
54
|
+
): Promise<WorkspaceEnvironmentForRun | null> {
|
|
55
|
+
if (!sandboxSecrets) {
|
|
56
|
+
// Standalone default: today's local decrypt, unchanged.
|
|
57
|
+
return loadWorkspaceEnvironmentForRunFromDb(db, settings, scope.workspaceId, environmentId);
|
|
58
|
+
}
|
|
59
|
+
if (!environmentId) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const secrets: SandboxSecrets = await sandboxSecrets({
|
|
63
|
+
accountId: scope.accountId,
|
|
64
|
+
workspaceId: scope.workspaceId,
|
|
65
|
+
environmentId,
|
|
66
|
+
});
|
|
67
|
+
// FORK-7: the provider must echo THIS run's workspace before we apply its
|
|
68
|
+
// decrypted values into the sandbox.
|
|
69
|
+
assertWorkspaceEcho("sandboxSecrets", scope, secrets.workspaceId);
|
|
70
|
+
return {
|
|
71
|
+
id: secrets.id ?? environmentId,
|
|
72
|
+
name: secrets.name ?? environmentId,
|
|
73
|
+
description: secrets.description ?? null,
|
|
74
|
+
values: secrets.values,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// §7.6 FORK-7 cross-check. A credential provider echoes the workspace it scoped
|
|
79
|
+
// the credential to; we ASSERT it equals the run's workspace BEFORE the caller
|
|
80
|
+
// injects the credential. A host mapping bug returning tenant B's creds for a
|
|
81
|
+
// tenant-A run hard-throws here instead of landing tenant B's token in tenant
|
|
82
|
+
// A's sandbox. Account/workspace ids only in the message (never the credential).
|
|
83
|
+
function assertWorkspaceEcho(
|
|
84
|
+
kind: string,
|
|
85
|
+
scope: ConnectionScope,
|
|
86
|
+
echoedWorkspaceId: string,
|
|
87
|
+
): void {
|
|
88
|
+
if (echoedWorkspaceId !== scope.workspaceId) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
`connection-credential provider (${kind}) scoped to workspace ${echoedWorkspaceId} but the run is workspace ${scope.workspaceId}`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export async function sandboxEnvironmentForRun(
|
|
96
|
+
settings: Settings,
|
|
97
|
+
resources: ResourceRef[],
|
|
98
|
+
workspaceEnvironment: Record<string, string> = {},
|
|
99
|
+
// §7.6 P4a — optional host git-credential provider + the run scope it needs
|
|
100
|
+
// (unset, the standalone default → self-mint from `settings` byte-for-byte).
|
|
101
|
+
// `skipGitHubToken` (Stage D): a connected-machine turn skips the inert platform
|
|
102
|
+
// token mint entirely. `= {}` default so the non-optional reads below are safe.
|
|
103
|
+
options: {
|
|
104
|
+
skipGitHubToken?: boolean;
|
|
105
|
+
scope?: ConnectionScope;
|
|
106
|
+
gitCredentials?: ConnectionCredentialsPort["gitCredentials"];
|
|
107
|
+
} = {},
|
|
108
|
+
): Promise<{ environment: Record<string, string>; gitToken?: string }> {
|
|
109
|
+
// Precedence: deployment allowlist < git identity < workspace environment
|
|
110
|
+
// < backend-aware HOME (the STABLE base, shared with the API-direct attach
|
|
111
|
+
// paths via stableSandboxEnvironmentForRun) < platform run-scoped GitHub auth
|
|
112
|
+
// (applied below, always last). Reserved name validation at write time prevents
|
|
113
|
+
// workspace values from colliding with the platform-managed entries.
|
|
114
|
+
//
|
|
115
|
+
// TOKEN-BROKER (B1): the run-scoped GitHub App installation token is NO LONGER
|
|
116
|
+
// layered into the box/agent MANIFEST env (no GH_TOKEN/GITHUB_TOKEN/GIT_CONFIG_*
|
|
117
|
+
// extraheader). It is minted ONCE per turn and returned as `gitToken`; the caller
|
|
118
|
+
// threads it OFF-MANIFEST as a clone-seed exec env (OPENGENI_GIT_TOKEN_SEED) so the
|
|
119
|
+
// clone hook writes it to a stable FILE ($OPENGENI_GIT_TOKEN_FILE), and git auth
|
|
120
|
+
// flows through GIT_ASKPASS -> that file. The manifest carries only the stable
|
|
121
|
+
// pointers (GIT_ASKPASS, GIT_TERMINAL_PROMPT, identity, and — via the shared base —
|
|
122
|
+
// OPENGENI_GIT_TOKEN_FILE), so the token VALUE never rides the manifest and the
|
|
123
|
+
// SDK's per-turn provided-session env delta stays empty even though the token
|
|
124
|
+
// rotates. The agent can refresh the token mid-turn via the `github_token` MCP tool.
|
|
125
|
+
const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment);
|
|
126
|
+
const selection = githubRepositorySelection(resources);
|
|
127
|
+
// NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
|
|
128
|
+
// a connected machine (selfhosted), the platform GitHub App installation token is
|
|
129
|
+
// INERT — exec routes over NATS to the user's machine, which uses ITS OWN git
|
|
130
|
+
// credentials, and the box that the token would auth is never created. So skip the
|
|
131
|
+
// (network) token mint entirely and return the STABLE base env (no gitToken). Env-
|
|
132
|
+
// parity holds: the SAME base object still feeds buildManifest + the SelfhostedSession
|
|
133
|
+
// manifest, so the SDK's per-turn provided-session env delta stays empty
|
|
134
|
+
// (validateNoEnvironmentDelta). The API-direct viewer attach path already drops the
|
|
135
|
+
// token under this exact contract — proof a box runs fine without it.
|
|
136
|
+
if (!selection || options.skipGitHubToken) {
|
|
137
|
+
return { environment };
|
|
138
|
+
}
|
|
139
|
+
// Run-scoped sandbox preparation for GitHub App repository resources. When a
|
|
140
|
+
// host binds `gitCredentials`, the HOST mints the installation token (BYO
|
|
141
|
+
// GitHub App); otherwise OpenGeni self-mints from `settings` (today's path). A
|
|
142
|
+
// SINGLE token is minted per turn and returned as `gitToken` (seeded to the box's
|
|
143
|
+
// token file by the caller's clone hook) — NOT layered into the manifest env.
|
|
144
|
+
let token: string;
|
|
145
|
+
let identity: { name: string; email: string } | null;
|
|
146
|
+
if (options?.gitCredentials && options.scope) {
|
|
147
|
+
const minted: GitCredentials = await options.gitCredentials({
|
|
148
|
+
accountId: options.scope.accountId,
|
|
149
|
+
workspaceId: options.scope.workspaceId,
|
|
150
|
+
installationId: selection.installationId,
|
|
151
|
+
repositoryIds: selection.repositoryIds,
|
|
152
|
+
});
|
|
153
|
+
// FORK-7: assert the provider scoped the token to THIS run's workspace
|
|
154
|
+
// before accepting the token for clone seeding.
|
|
155
|
+
assertWorkspaceEcho("gitCredentials", options.scope, minted.workspaceId);
|
|
156
|
+
token = minted.token;
|
|
157
|
+
identity = minted.identity ?? githubAppBotIdentity(settings);
|
|
158
|
+
} else {
|
|
159
|
+
token = await createGitHubAppInstallationToken(settings, {
|
|
160
|
+
installationId: selection.installationId,
|
|
161
|
+
repositoryIds: selection.repositoryIds,
|
|
162
|
+
});
|
|
163
|
+
identity = githubAppBotIdentity(settings);
|
|
164
|
+
}
|
|
165
|
+
// TOKEN-BROKER (B2): the askpass helper is PROVISIONED AT SETUP (runtime) into a
|
|
166
|
+
// per-box, user-writable path in the SAME dir as the token file, instead of a
|
|
167
|
+
// baked image script at /usr/local/bin/opengeni-git-askpass. The clone-hook seed
|
|
168
|
+
// block writes both the token file AND this askpass script before the fetch, so
|
|
169
|
+
// git auth becomes correct on ANY box image (including pre-existing warm boxes on
|
|
170
|
+
// their next turn's clone hook) — no product image needs to carry the askpass.
|
|
171
|
+
// The pointer layer is the SHARED config helper so every API-direct attach
|
|
172
|
+
// surface (viewer attach, channel-A) declares the IDENTICAL env when it
|
|
173
|
+
// cold-creates the box for a repo-attached session — an attach-warmed box
|
|
174
|
+
// missing these keys kills the next repo turn on the SDK's manifest-env guard.
|
|
175
|
+
applyGitAuthPointerEnvironment(environment, identity);
|
|
176
|
+
return { environment, gitToken: token };
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function githubRepositorySelection(resources: ResourceRef[]): { installationId: number; repositoryIds: number[] } | null {
|
|
180
|
+
const selected = resources.flatMap((resource) => {
|
|
181
|
+
if (resource.kind !== "repository") {
|
|
182
|
+
return [];
|
|
183
|
+
}
|
|
184
|
+
const installationId = positiveInteger(resource.githubInstallationId);
|
|
185
|
+
const repositoryId = positiveInteger(resource.githubRepositoryId);
|
|
186
|
+
return installationId && repositoryId ? [{ installationId, repositoryId }] : [];
|
|
187
|
+
});
|
|
188
|
+
if (selected.length === 0) {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
const installationId = selected[0]!.installationId;
|
|
192
|
+
if (selected.some((item) => item.installationId !== installationId)) {
|
|
193
|
+
throw new Error("GitHub App repository resources must belong to one installation");
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
installationId,
|
|
197
|
+
repositoryIds: selected.map((item) => item.repositoryId),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function positiveInteger(value: unknown): number | null {
|
|
202
|
+
if (typeof value === "number" && Number.isInteger(value) && value > 0) {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
if (typeof value === "string" && /^\d+$/.test(value) && Number(value) > 0) {
|
|
206
|
+
return Number(value);
|
|
207
|
+
}
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { configuredStaticUsageLimits, type Settings } from "@opengeni/config";
|
|
2
|
+
import {
|
|
3
|
+
mergeToolRefs,
|
|
4
|
+
reasoningEffortForMetadata,
|
|
5
|
+
type SessionGoal,
|
|
6
|
+
type ToolRef,
|
|
7
|
+
} from "@opengeni/contracts";
|
|
8
|
+
import {
|
|
9
|
+
enqueueSessionTurn,
|
|
10
|
+
evaluateGoalContinuation,
|
|
11
|
+
getBillingBalance,
|
|
12
|
+
getSessionEvent,
|
|
13
|
+
getSessionGoal,
|
|
14
|
+
isCodexBilledTurn,
|
|
15
|
+
recordUsageEvent,
|
|
16
|
+
requireSession,
|
|
17
|
+
setSessionGoalLastContinuationTurn,
|
|
18
|
+
setSessionGoalStatus,
|
|
19
|
+
sumUsageQuantity,
|
|
20
|
+
type Database,
|
|
21
|
+
} from "@opengeni/db";
|
|
22
|
+
import { appendAndPublishEvents, type EventBus } from "@opengeni/events";
|
|
23
|
+
import type {
|
|
24
|
+
ActivityServices,
|
|
25
|
+
MaybeContinueGoalInput,
|
|
26
|
+
MaybeContinueGoalResult,
|
|
27
|
+
PauseGoalForInterruptInput,
|
|
28
|
+
} from "./types";
|
|
29
|
+
|
|
30
|
+
export function createGoalActivities(services: () => Promise<ActivityServices>) {
|
|
31
|
+
async function maybeContinueGoal(input: MaybeContinueGoalInput): Promise<MaybeContinueGoalResult> {
|
|
32
|
+
const { settings, db, bus } = await services();
|
|
33
|
+
// Cheap pre-read: the common goal-less session skips the budget queries.
|
|
34
|
+
const existingGoal = await getSessionGoal(db, input.workspaceId, input.sessionId);
|
|
35
|
+
if (!existingGoal || existingGoal.status !== "active") {
|
|
36
|
+
return { action: "none" };
|
|
37
|
+
}
|
|
38
|
+
// Loaded before the budget check so the codex-billed predicate can read the
|
|
39
|
+
// continuation turn's model (session.model). Kept below the goal-less fast
|
|
40
|
+
// path above so a non-goal session still skips this read entirely.
|
|
41
|
+
const session = await requireSession(db, input.workspaceId, input.sessionId);
|
|
42
|
+
// A codex-model goal continuation is paid by the user's ChatGPT/Codex plan,
|
|
43
|
+
// so it must not be budget-paused for zero OpenGeni credits. This file uses
|
|
44
|
+
// BASE settings (no codex overlay); the predicate does its own credential read.
|
|
45
|
+
const isCodexRun = await isCodexBilledTurn({ db, settings, workspaceId: input.workspaceId, model: session.model });
|
|
46
|
+
// Budget exhaustion pauses the goal visibly instead of failing the
|
|
47
|
+
// session. Computed up front and applied inside the locked decision so a
|
|
48
|
+
// limits pause never consumes continuation budget.
|
|
49
|
+
const budgetBlocked = await goalRunBudgetBlocked(settings, db, input.accountId, input.workspaceId, isCodexRun);
|
|
50
|
+
const decision = await evaluateGoalContinuation(db, {
|
|
51
|
+
workspaceId: input.workspaceId,
|
|
52
|
+
sessionId: input.sessionId,
|
|
53
|
+
defaultMaxAutoContinuations: settings.goalMaxAutoContinuations ?? null,
|
|
54
|
+
noProgressLimit: settings.goalNoProgressLimit,
|
|
55
|
+
budgetBlocked,
|
|
56
|
+
});
|
|
57
|
+
if (decision.decision === "none" || decision.decision === "queue") {
|
|
58
|
+
return { action: decision.decision };
|
|
59
|
+
}
|
|
60
|
+
if (decision.decision === "paused") {
|
|
61
|
+
await appendAndPublishEvents(db, bus, input.workspaceId, input.sessionId, [{
|
|
62
|
+
type: "goal.paused",
|
|
63
|
+
payload: {
|
|
64
|
+
goalId: decision.goal.id,
|
|
65
|
+
actor: "system",
|
|
66
|
+
reason: decision.reason,
|
|
67
|
+
...(decision.goal.rationale ? { rationale: decision.goal.rationale } : {}),
|
|
68
|
+
autoContinuations: decision.goal.autoContinuations,
|
|
69
|
+
noProgressStreak: decision.goal.noProgressStreak,
|
|
70
|
+
},
|
|
71
|
+
}]);
|
|
72
|
+
return { action: "paused" };
|
|
73
|
+
}
|
|
74
|
+
// Stop/continue race guard: a concurrent goal_complete/goal_pause/operator
|
|
75
|
+
// PATCH between the locked decision and this synthesis must win. The
|
|
76
|
+
// version check also catches a replace. A pause landing after this point
|
|
77
|
+
// results in at most one already-admitted continuation turn; the next
|
|
78
|
+
// pass sees the non-active goal and stops, and interrupt-driven pauses
|
|
79
|
+
// additionally cancel the claimed turn via the workflow interrupt path.
|
|
80
|
+
const recheck = await getSessionGoal(db, input.workspaceId, input.sessionId);
|
|
81
|
+
if (!recheck || recheck.status !== "active" || recheck.version !== decision.goal.version) {
|
|
82
|
+
return { action: "none" };
|
|
83
|
+
}
|
|
84
|
+
const prompt = goalContinuationPrompt(decision.goal, decision.autoContinuation, decision.cap);
|
|
85
|
+
const [continuationEvent] = await appendAndPublishEvents(db, bus, input.workspaceId, input.sessionId, [{
|
|
86
|
+
type: "goal.continuation",
|
|
87
|
+
payload: {
|
|
88
|
+
goalId: decision.goal.id,
|
|
89
|
+
text: prompt,
|
|
90
|
+
autoContinuation: decision.autoContinuation,
|
|
91
|
+
maxAutoContinuations: decision.cap,
|
|
92
|
+
goalVersion: decision.goal.version,
|
|
93
|
+
},
|
|
94
|
+
}]);
|
|
95
|
+
if (!continuationEvent) {
|
|
96
|
+
throw new Error("failed to append goal continuation trigger event");
|
|
97
|
+
}
|
|
98
|
+
const turn = await enqueueSessionTurn(db, {
|
|
99
|
+
accountId: input.accountId,
|
|
100
|
+
workspaceId: input.workspaceId,
|
|
101
|
+
sessionId: input.sessionId,
|
|
102
|
+
triggerEventId: continuationEvent.id,
|
|
103
|
+
temporalWorkflowId: input.workflowId,
|
|
104
|
+
source: "goal",
|
|
105
|
+
prompt,
|
|
106
|
+
resources: [],
|
|
107
|
+
// Continuations keep the session tool surface and force the first-party
|
|
108
|
+
// server so the goal_complete/goal_pause escape hatches stay reachable.
|
|
109
|
+
tools: withFirstPartyTools(settings, session.tools),
|
|
110
|
+
model: session.model,
|
|
111
|
+
reasoningEffort: reasoningEffortForMetadata(session.metadata, settings.openaiReasoningEffort),
|
|
112
|
+
sandboxBackend: session.sandboxBackend,
|
|
113
|
+
metadata: { goalId: decision.goal.id, autoContinuation: decision.autoContinuation },
|
|
114
|
+
});
|
|
115
|
+
await setSessionGoalLastContinuationTurn(db, input.workspaceId, input.sessionId, turn.id);
|
|
116
|
+
await appendAndPublishEvents(db, bus, input.workspaceId, input.sessionId, [{
|
|
117
|
+
type: "turn.queued",
|
|
118
|
+
turnId: turn.id,
|
|
119
|
+
payload: { turnId: turn.id, triggerEventId: continuationEvent.id, source: turn.source },
|
|
120
|
+
}]);
|
|
121
|
+
// Continuations count as agent runs for limits/metering parity with
|
|
122
|
+
// user-initiated and scheduled turns.
|
|
123
|
+
await recordUsageEvent(db, {
|
|
124
|
+
accountId: input.accountId,
|
|
125
|
+
workspaceId: input.workspaceId,
|
|
126
|
+
eventType: "agent_run.created",
|
|
127
|
+
quantity: 1,
|
|
128
|
+
unit: "run",
|
|
129
|
+
sourceResourceType: "session_turn",
|
|
130
|
+
sourceResourceId: turn.id,
|
|
131
|
+
idempotencyKey: `agent_run.created:goal:${input.workspaceId}:${turn.id}`,
|
|
132
|
+
});
|
|
133
|
+
return { action: "continue" };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async function pauseGoalForInterrupt(input: PauseGoalForInterruptInput): Promise<void> {
|
|
137
|
+
const { db, bus } = await services();
|
|
138
|
+
if (input.triggerEventId) {
|
|
139
|
+
const trigger = await getSessionEvent(db, input.workspaceId, input.triggerEventId);
|
|
140
|
+
if (isSteerInterrupt(trigger)) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
await pauseActiveGoalOnInterrupt(db, bus, input.workspaceId, input.sessionId);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
maybeContinueGoal,
|
|
149
|
+
pauseGoalForInterrupt,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* True when an interrupt's trigger event is a STEER: `user.interrupt` tagged
|
|
155
|
+
* `reason: "steer"`, sent by `OpenGeniClient.steerMessage`. Steering cancels
|
|
156
|
+
* the running turn only to deliver the steered message next — it redirects
|
|
157
|
+
* the work rather than stopping it, so an active goal keeps going. Every
|
|
158
|
+
* other user interrupt (the stop button, a plain `interrupt()` call) is the
|
|
159
|
+
* explicit act of stopping and pauses the goal.
|
|
160
|
+
*/
|
|
161
|
+
export function isSteerInterrupt(trigger: { type: string; payload: unknown } | null | undefined): boolean {
|
|
162
|
+
if (!trigger || trigger.type !== "user.interrupt") {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
const payload = trigger.payload;
|
|
166
|
+
return typeof payload === "object" && payload !== null && (payload as { reason?: unknown }).reason === "steer";
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A user interrupt is the explicit act of stopping, so it pauses an active
|
|
171
|
+
* goal. Shared by the idle-interrupt activity and `interruptActiveTurn` so the
|
|
172
|
+
* loop never auto-continues a goal the user just stopped. Callers gate this
|
|
173
|
+
* on `isSteerInterrupt` first: steer interrupts must NOT pause the goal.
|
|
174
|
+
* No-op when the session has no goal or it is not active.
|
|
175
|
+
*/
|
|
176
|
+
export async function pauseActiveGoalOnInterrupt(db: Database, bus: EventBus, workspaceId: string, sessionId: string): Promise<void> {
|
|
177
|
+
const goal = await getSessionGoal(db, workspaceId, sessionId);
|
|
178
|
+
if (!goal || goal.status !== "active") {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const { goal: paused, changed } = await setSessionGoalStatus(db, workspaceId, sessionId, {
|
|
182
|
+
status: "paused",
|
|
183
|
+
pausedReason: "user_interrupt",
|
|
184
|
+
});
|
|
185
|
+
if (changed) {
|
|
186
|
+
await appendAndPublishEvents(db, bus, workspaceId, sessionId, [{
|
|
187
|
+
type: "goal.paused",
|
|
188
|
+
payload: {
|
|
189
|
+
goalId: paused.id,
|
|
190
|
+
actor: "user",
|
|
191
|
+
reason: "user_interrupt",
|
|
192
|
+
autoContinuations: paused.autoContinuations,
|
|
193
|
+
noProgressStreak: paused.noProgressStreak,
|
|
194
|
+
},
|
|
195
|
+
}]);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function goalContinuationPrompt(goal: SessionGoal, autoContinuation: number, cap: number | null): string {
|
|
200
|
+
const counter = cap === null ? `${autoContinuation}` : `${autoContinuation}/${cap}`;
|
|
201
|
+
return [
|
|
202
|
+
`[GOAL CONTINUATION ${counter}] The session goal is not done. Goal: ${goal.text}.`,
|
|
203
|
+
`Success criteria: ${goal.successCriteria ?? "none specified"}.`,
|
|
204
|
+
"Continue working toward the goal now. If it is actually complete, call opengeni__goal_complete with concrete evidence.",
|
|
205
|
+
"If you are blocked or continuing is not productive, call opengeni__goal_pause with your rationale.",
|
|
206
|
+
"You may revise the goal with opengeni__goal_update. Do not stop without one of these explicit actions.",
|
|
207
|
+
].join("\n");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Ensures a session/turn carries the first-party "opengeni" MCP server, which
|
|
212
|
+
* hosts set_session_title, the goal tools, and the permission-gated
|
|
213
|
+
* orchestration/environment/github tools. Attached to EVERY session/turn (not
|
|
214
|
+
* just goal-bearing ones); built-in tool refs are not auto-added to empty tool
|
|
215
|
+
* lists anywhere else in the pipeline. No-op when the server is not configured.
|
|
216
|
+
*/
|
|
217
|
+
export function withFirstPartyTools(settings: Settings, tools: ToolRef[]): ToolRef[] {
|
|
218
|
+
if (!settings.mcpServers.some((server) => server.id === "opengeni")) {
|
|
219
|
+
return tools;
|
|
220
|
+
}
|
|
221
|
+
return mergeToolRefs(tools, [{ kind: "mcp", id: "opengeni" }]);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Ensures a turn references the synthetic codex_apps connectors MCP server when
|
|
226
|
+
* the codex overlay injected it (active subscription + connector scopes). A
|
|
227
|
+
* registry entry is inert until a ToolRef references its id, so this wires the
|
|
228
|
+
* server into the run. No-op when the server is not configured (every non-codex
|
|
229
|
+
* turn), and idempotent via mergeToolRefs.
|
|
230
|
+
*/
|
|
231
|
+
export function withCodexAppsTool(settings: Settings, tools: ToolRef[]): ToolRef[] {
|
|
232
|
+
if (!settings.mcpServers.some((server) => server.id === "codex_apps")) {
|
|
233
|
+
return tools;
|
|
234
|
+
}
|
|
235
|
+
return mergeToolRefs(tools, [{ kind: "mcp", id: "codex_apps" }]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Non-throwing variant of the scheduled-run admission check: returns a human
|
|
240
|
+
* readable reason when balance or monthly caps block another agent run.
|
|
241
|
+
*/
|
|
242
|
+
async function goalRunBudgetBlocked(settings: Settings, db: Database, accountId: string, workspaceId: string, isCodexRun: boolean): Promise<string | null> {
|
|
243
|
+
// Codex-billed continuations are paid by the user's ChatGPT/Codex plan: skip
|
|
244
|
+
// the credit-balance gate and the monthly model-cost cap. The agent-run COUNT
|
|
245
|
+
// cap below is a volume quota (not a credit/cost gate) and is intentionally kept.
|
|
246
|
+
if (!isCodexRun && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
|
|
247
|
+
const balance = await getBillingBalance(db, accountId);
|
|
248
|
+
if (balance.balanceMicros <= 0) {
|
|
249
|
+
return "insufficient OpenGeni credits";
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (settings.usageLimitsMode === "static" || settings.usageLimitsMode === "managed") {
|
|
253
|
+
const limits = configuredStaticUsageLimits(settings);
|
|
254
|
+
if (!isCodexRun && limits.maxMonthlyCostMicrosPerAccount) {
|
|
255
|
+
const used = await sumUsageQuantity(db, {
|
|
256
|
+
accountId,
|
|
257
|
+
eventType: "model.cost",
|
|
258
|
+
since: startOfUtcMonth(),
|
|
259
|
+
});
|
|
260
|
+
if (used >= limits.maxMonthlyCostMicrosPerAccount) {
|
|
261
|
+
return `monthly model cost limit reached (${limits.maxMonthlyCostMicrosPerAccount} micros)`;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (limits.maxMonthlyAgentRunsPerWorkspace) {
|
|
265
|
+
const used = await sumUsageQuantity(db, {
|
|
266
|
+
workspaceId,
|
|
267
|
+
eventType: "agent_run.created",
|
|
268
|
+
since: startOfUtcMonth(),
|
|
269
|
+
});
|
|
270
|
+
if (used + 1 > limits.maxMonthlyAgentRunsPerWorkspace) {
|
|
271
|
+
return `monthly agent run limit reached (${limits.maxMonthlyAgentRunsPerWorkspace})`;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function startOfUtcMonth(): Date {
|
|
279
|
+
const now = new Date();
|
|
280
|
+
return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
|
|
281
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Settings } from "@opengeni/config";
|
|
2
|
+
import { CapabilityPack } from "@opengeni/contracts";
|
|
3
|
+
import { getWorkspace, getWorkspacePack, listPackInstallations, type Database } from "@opengeni/db";
|
|
4
|
+
import type { PackSkill } from "@opengeni/runtime";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The pack-scoped runtime for a workspace: the sandbox image its sessions run
|
|
8
|
+
* in (when an enabled pack declares one) and the pack skills that join the
|
|
9
|
+
* sandbox skill index.
|
|
10
|
+
*/
|
|
11
|
+
export type WorkspacePackRuntime = {
|
|
12
|
+
sandboxImage: string | null;
|
|
13
|
+
skills: PackSkill[];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const emptyPackRuntime: WorkspacePackRuntime = { sandboxImage: null, skills: [] };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the pack-scoped runtime from the workspace's active pack
|
|
20
|
+
* installations. Only registered (manifest-backed) packs can contribute a
|
|
21
|
+
* sandbox image or skills; built-in packs never declare either (enforced by a
|
|
22
|
+
* test on the built-in catalog), so their installations are skipped here
|
|
23
|
+
* without consulting the API's built-in pack list.
|
|
24
|
+
*/
|
|
25
|
+
export async function resolveWorkspacePackRuntime(db: Database, workspaceId: string): Promise<WorkspacePackRuntime> {
|
|
26
|
+
const installations = await listPackInstallations(db, workspaceId);
|
|
27
|
+
const active = installations.filter((installation) => installation.status === "active");
|
|
28
|
+
if (active.length === 0) {
|
|
29
|
+
return emptyPackRuntime;
|
|
30
|
+
}
|
|
31
|
+
const packs: CapabilityPack[] = [];
|
|
32
|
+
for (const installation of active) {
|
|
33
|
+
const registration = await getWorkspacePack(db, workspaceId, installation.packId);
|
|
34
|
+
if (!registration) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const parsed = CapabilityPack.safeParse(registration.pack);
|
|
38
|
+
if (parsed.success) {
|
|
39
|
+
packs.push(parsed.data);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return workspacePackRuntimeFromPacks(packs);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Pure composition rule for enabled pack manifests. v1 keeps this small by
|
|
47
|
+
* design: at most one enabled pack may declare a sandbox image (no image
|
|
48
|
+
* layering or composition), and skill names must be unique across enabled
|
|
49
|
+
* packs. Violations fail the turn with a plain error instead of guessing.
|
|
50
|
+
*/
|
|
51
|
+
export function workspacePackRuntimeFromPacks(packs: CapabilityPack[]): WorkspacePackRuntime {
|
|
52
|
+
const imagePacks = packs.filter((pack) => typeof pack.sandboxImage === "string" && pack.sandboxImage.trim().length > 0);
|
|
53
|
+
if (imagePacks.length > 1) {
|
|
54
|
+
const ids = imagePacks.map((pack) => pack.id).sort().join(", ");
|
|
55
|
+
throw new Error(
|
|
56
|
+
`Multiple enabled packs declare a sandbox image (${ids}). Only one enabled pack per workspace may declare sandboxImage; disable the others and retry.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const skills: PackSkill[] = [];
|
|
60
|
+
// Keyed case-insensitively to match the per-pack uniqueness rule in the
|
|
61
|
+
// CapabilityPack contract (and case-insensitive filesystems).
|
|
62
|
+
const skillOwners = new Map<string, string>();
|
|
63
|
+
for (const pack of [...packs].sort((a, b) => a.id.localeCompare(b.id))) {
|
|
64
|
+
for (const skill of pack.skills) {
|
|
65
|
+
const key = skill.name.toLowerCase();
|
|
66
|
+
const existingOwner = skillOwners.get(key);
|
|
67
|
+
if (existingOwner !== undefined && existingOwner !== pack.id) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Enabled packs ${existingOwner} and ${pack.id} both declare a skill named "${skill.name}". Pack skill names must be unique across enabled packs; disable one of the packs and retry.`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
skillOwners.set(key, pack.id);
|
|
73
|
+
skills.push({
|
|
74
|
+
name: skill.name,
|
|
75
|
+
description: skill.description ?? null,
|
|
76
|
+
files: skill.files.map((file) => ({ path: file.path, content: file.content })),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
sandboxImage: imagePacks[0]?.sandboxImage?.trim() ?? null,
|
|
82
|
+
skills,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Resolves the per-workspace agent persona override (the white-label surface).
|
|
88
|
+
* Returns the workspace's stored template when set, else null to mean "use the
|
|
89
|
+
* deployment default" (settings.agentInstructionsTemplate). The runtime always
|
|
90
|
+
* injects the non-bypassable CORE regardless, so a null here keeps the
|
|
91
|
+
* byte-identical default and a non-null value only restyles the persona.
|
|
92
|
+
*
|
|
93
|
+
* This is the workspace tier of the session > workspace > deployment-default
|
|
94
|
+
* resolution; per-session overrides do not exist in this slice, so the worker
|
|
95
|
+
* resolves workspace > default and passes the result as instructionsTemplate.
|
|
96
|
+
*/
|
|
97
|
+
export async function resolveWorkspaceAgentInstructions(db: Database, workspaceId: string): Promise<string | null> {
|
|
98
|
+
const workspace = await getWorkspace(db, workspaceId);
|
|
99
|
+
return workspace?.agentInstructions ?? null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Applies a pack-declared sandbox image to run settings. With no pack image
|
|
104
|
+
* the settings pass through untouched, so deployments without packs keep the
|
|
105
|
+
* global OPENGENI_DOCKER_IMAGE / OPENGENI_MODAL_IMAGE_REF behavior exactly.
|
|
106
|
+
*/
|
|
107
|
+
export function settingsWithPackSandboxImage(settings: Settings, sandboxImage: string | null): Settings {
|
|
108
|
+
if (!sandboxImage) {
|
|
109
|
+
return settings;
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
...settings,
|
|
113
|
+
dockerImage: sandboxImage,
|
|
114
|
+
modalImageRef: sandboxImage,
|
|
115
|
+
};
|
|
116
|
+
}
|