@opengeni/worker-bundle 0.7.1 → 0.7.3
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 +184 -68
- package/dist/index.js +7099 -1643
- package/dist/index.js.map +1 -1
- package/package.json +18 -18
- package/src/activities/agent-turn.ts +4332 -1068
- package/src/activities/capabilities.ts +43 -8
- package/src/activities/codex-capacity.ts +223 -0
- package/src/activities/codex-rotation.ts +550 -54
- package/src/activities/common.ts +14 -2
- package/src/activities/context-compaction.ts +152 -118
- package/src/activities/documents.ts +32 -27
- package/src/activities/environment.ts +318 -75
- package/src/activities/file-upload-reaper.ts +79 -0
- package/src/activities/git-credential-renewal.ts +183 -0
- package/src/activities/goals.ts +142 -123
- package/src/activities/packs.ts +55 -5
- package/src/activities/parent-wake.ts +216 -42
- package/src/activities/recording.ts +154 -122
- package/src/activities/redaction.ts +3 -1
- package/src/activities/rig-verification.ts +441 -0
- package/src/activities/run-input.ts +110 -122
- package/src/activities/sandbox-lease.ts +186 -51
- package/src/activities/scheduled-tasks.ts +287 -171
- package/src/activities/session-state.ts +165 -157
- package/src/activities/streaming.ts +143 -19
- package/src/activities/turn-attempt-fenced.ts +14 -0
- package/src/activities/types.ts +106 -36
- package/src/activities/workflow-wake.ts +44 -0
- package/src/activities/workspace-capture.ts +964 -0
- package/src/activities.ts +75 -35
- package/src/concurrency.ts +11 -0
- package/src/http.ts +16 -6
- package/src/index.ts +322 -49
- package/src/observability-metrics.ts +441 -8
- package/src/op-journal.ts +46 -0
- package/src/sandbox-resume.ts +431 -201
- package/src/sandbox-routing.ts +209 -22
- package/src/workflows/activities.ts +65 -8
- package/src/workflows/file-upload-reaper.ts +12 -0
- package/src/workflows/rig-verification.ts +28 -0
- package/src/workflows/scheduled-tasks.ts +12 -26
- package/src/workflows/session.ts +330 -192
- package/src/workflows/workflow-wake.ts +13 -0
- package/src/workflows.ts +9 -7
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
environmentsEncryptionKeyBytes,
|
|
3
|
+
parseModelProvidersJson,
|
|
4
|
+
type RegistryProvider,
|
|
5
|
+
type Settings,
|
|
6
|
+
} from "@opengeni/config";
|
|
2
7
|
import { settingsWithMcpCapabilityServers } from "@opengeni/core";
|
|
3
8
|
import {
|
|
4
9
|
CODEX_APPS_MCP_SERVER_ID,
|
|
@@ -6,6 +11,9 @@ import {
|
|
|
6
11
|
CODEX_APPS_MCP_URL,
|
|
7
12
|
CODEX_APPS_STARTUP_TIMEOUT_MS,
|
|
8
13
|
CODEX_FALLBACK_MODEL_SLUGS,
|
|
14
|
+
CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
|
|
15
|
+
CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
|
|
16
|
+
CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
|
|
9
17
|
CODEX_MODEL_ID_PREFIX,
|
|
10
18
|
CODEX_PROVIDER_BASE_URL,
|
|
11
19
|
CODEX_PROVIDER_ID,
|
|
@@ -19,7 +27,11 @@ import {
|
|
|
19
27
|
type SessionMcpServerForRun,
|
|
20
28
|
} from "@opengeni/db";
|
|
21
29
|
|
|
22
|
-
export async function settingsWithEnabledCapabilityMcpServers(
|
|
30
|
+
export async function settingsWithEnabledCapabilityMcpServers(
|
|
31
|
+
db: Database,
|
|
32
|
+
workspaceId: string,
|
|
33
|
+
settings: Settings,
|
|
34
|
+
): Promise<Settings> {
|
|
23
35
|
const enabled = await listEnabledMcpCapabilityServers(db, workspaceId);
|
|
24
36
|
return settingsWithMcpCapabilityServers(settings, enabled);
|
|
25
37
|
}
|
|
@@ -38,10 +50,16 @@ export async function settingsWithSessionMcpServersForRun(
|
|
|
38
50
|
}
|
|
39
51
|
throw new Error("session MCP server credentials require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
|
|
40
52
|
}
|
|
41
|
-
return settingsWithSessionMcpServers(
|
|
53
|
+
return settingsWithSessionMcpServers(
|
|
54
|
+
settings,
|
|
55
|
+
await listSessionMcpServersForRun(db, workspaceId, sessionId, encryptionKey),
|
|
56
|
+
);
|
|
42
57
|
}
|
|
43
58
|
|
|
44
|
-
export function settingsWithSessionMcpServers(
|
|
59
|
+
export function settingsWithSessionMcpServers(
|
|
60
|
+
settings: Settings,
|
|
61
|
+
servers: SessionMcpServerForRun[],
|
|
62
|
+
): Settings {
|
|
45
63
|
if (servers.length === 0) {
|
|
46
64
|
return settings;
|
|
47
65
|
}
|
|
@@ -57,7 +75,9 @@ export function settingsWithSessionMcpServers(settings: Settings, servers: Sessi
|
|
|
57
75
|
...(server.allowedTools ? { allowedTools: server.allowedTools } : {}),
|
|
58
76
|
...(server.timeoutMs ? { timeoutMs: server.timeoutMs } : {}),
|
|
59
77
|
cacheToolsList: server.cacheToolsList ?? false,
|
|
60
|
-
...(server.requireApproval !== undefined
|
|
78
|
+
...(server.requireApproval !== undefined
|
|
79
|
+
? { requireApproval: server.requireApproval }
|
|
80
|
+
: {}),
|
|
61
81
|
headers: server.headers,
|
|
62
82
|
})),
|
|
63
83
|
],
|
|
@@ -71,13 +91,19 @@ export function settingsWithSessionMcpServers(settings: Settings, servers: Sessi
|
|
|
71
91
|
* this overlay (metadata-only read); the per-request bearer is resolved later via
|
|
72
92
|
* codexRequestStorage. Idempotent and a no-op when not applicable.
|
|
73
93
|
*/
|
|
74
|
-
export async function settingsWithCodexCredential(
|
|
94
|
+
export async function settingsWithCodexCredential(
|
|
95
|
+
db: Database,
|
|
96
|
+
workspaceId: string,
|
|
97
|
+
settings: Settings,
|
|
98
|
+
activeOverride?: boolean,
|
|
99
|
+
): Promise<Settings> {
|
|
75
100
|
// Same active-credential predicate the billing bypass uses, so provider
|
|
76
101
|
// injection and billing can never disagree on what an "active codex" turn is.
|
|
77
102
|
// The caller may pass `activeOverride` (a single, shared read; P2-b) so routing
|
|
78
103
|
// and billing decide from the exact same observation, immune to a concurrent
|
|
79
104
|
// disconnect/reconnect landing between two independent reads.
|
|
80
|
-
const active =
|
|
105
|
+
const active =
|
|
106
|
+
activeOverride ?? (await workspaceCodexSubscriptionActive(db, settings, workspaceId));
|
|
81
107
|
if (!active) {
|
|
82
108
|
return settings; // disabled / not connected / needs_relogin / error -> leave settings unchanged
|
|
83
109
|
}
|
|
@@ -139,7 +165,16 @@ export function withCodexProvider(settings: Settings): Settings {
|
|
|
139
165
|
label: "Codex (ChatGPT subscription)",
|
|
140
166
|
api: "responses",
|
|
141
167
|
baseUrl: CODEX_PROVIDER_BASE_URL,
|
|
142
|
-
models: CODEX_FALLBACK_MODEL_SLUGS.map((slug) => ({
|
|
168
|
+
models: CODEX_FALLBACK_MODEL_SLUGS.map((slug) => ({
|
|
169
|
+
id: `${CODEX_MODEL_ID_PREFIX}${slug}`,
|
|
170
|
+
label: slug,
|
|
171
|
+
reasoningEffort: true,
|
|
172
|
+
// These three values are the live Codex CLI catalog policy, kept distinct
|
|
173
|
+
// so proactive compaction and the effective input guard match Codex core.
|
|
174
|
+
contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
|
|
175
|
+
effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
|
|
176
|
+
autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
|
|
177
|
+
})),
|
|
143
178
|
};
|
|
144
179
|
return { ...settings, modelProvidersJson: JSON.stringify([...providers, codexProvider]) };
|
|
145
180
|
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fetchCodexUsageForAccount,
|
|
3
|
+
getCodexCapacityWaitForSession,
|
|
4
|
+
listCodexAccountStatuses,
|
|
5
|
+
listPendingCodexCapacityWakeTargets,
|
|
6
|
+
reconcileCodexCapacityWait as reconcileCodexCapacityWaitDb,
|
|
7
|
+
type CodexCapacityWakeTarget,
|
|
8
|
+
type CodexCapacitySelectionContext,
|
|
9
|
+
} from "@opengeni/db";
|
|
10
|
+
import type { Settings } from "@opengeni/config";
|
|
11
|
+
import {
|
|
12
|
+
authoritativeCodexCapacityResetAt,
|
|
13
|
+
isCodexCredentialEligible,
|
|
14
|
+
selectCodexCredentialLeaseForTurn,
|
|
15
|
+
} from "./codex-rotation";
|
|
16
|
+
import type {
|
|
17
|
+
ActivityServices,
|
|
18
|
+
GetCodexCapacityWaitInput,
|
|
19
|
+
ReconcileCodexCapacityWaitInput,
|
|
20
|
+
ReconcileCodexCapacityWaitResult,
|
|
21
|
+
} from "./types";
|
|
22
|
+
|
|
23
|
+
type CodexCapacitySignalServices = {
|
|
24
|
+
signalCodexCapacityWorkflow?:
|
|
25
|
+
| NonNullable<ActivityServices["signalCodexCapacityWorkflow"]>
|
|
26
|
+
| null
|
|
27
|
+
| undefined;
|
|
28
|
+
wakeSessionWorkflow: ActivityServices["wakeSessionWorkflow"];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Run a bounded set of usage refreshes, then repair every committed waiter
|
|
33
|
+
* revision even when an individual provider refresh failed. The database
|
|
34
|
+
* outbox remains authoritative; this helper only guarantees that every worker
|
|
35
|
+
* refresh path reaches the same post-commit delivery seam.
|
|
36
|
+
*/
|
|
37
|
+
export async function refreshCodexUsageAndRepairCapacityWaiters(
|
|
38
|
+
refreshes: readonly (() => Promise<unknown>)[],
|
|
39
|
+
repairPendingWakes: () => Promise<void>,
|
|
40
|
+
): Promise<void> {
|
|
41
|
+
await Promise.all(refreshes.map((refresh) => refresh().catch(() => undefined)));
|
|
42
|
+
await repairPendingWakes();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Deliver committed waiter revisions; Postgres remains the repairable outbox. */
|
|
46
|
+
export async function signalCodexCapacityWakeTargets(
|
|
47
|
+
services: CodexCapacitySignalServices,
|
|
48
|
+
targets: readonly CodexCapacityWakeTarget[],
|
|
49
|
+
): Promise<void> {
|
|
50
|
+
await Promise.allSettled(
|
|
51
|
+
targets.map((target) =>
|
|
52
|
+
services.signalCodexCapacityWorkflow
|
|
53
|
+
? services.signalCodexCapacityWorkflow({
|
|
54
|
+
accountId: target.accountId,
|
|
55
|
+
workspaceId: target.workspaceId,
|
|
56
|
+
sessionId: target.sessionId,
|
|
57
|
+
workflowId: target.workflowId,
|
|
58
|
+
wakeRevision: target.wakeRevision,
|
|
59
|
+
})
|
|
60
|
+
: services.wakeSessionWorkflow
|
|
61
|
+
? services.wakeSessionWorkflow({
|
|
62
|
+
accountId: target.accountId,
|
|
63
|
+
workspaceId: target.workspaceId,
|
|
64
|
+
sessionId: target.sessionId,
|
|
65
|
+
workflowId: target.workflowId,
|
|
66
|
+
wakeRevision: target.workflowWakeRevision,
|
|
67
|
+
})
|
|
68
|
+
: Promise.resolve(),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Repair a commit/signal crash edge by redelivering every pending revision. */
|
|
74
|
+
export async function signalPendingCodexCapacityWakeTargets(
|
|
75
|
+
services: CodexCapacitySignalServices & { db: ActivityServices["db"] },
|
|
76
|
+
workspaceId: string,
|
|
77
|
+
): Promise<void> {
|
|
78
|
+
const targets = await listPendingCodexCapacityWakeTargets(services.db, workspaceId).catch(
|
|
79
|
+
() => [],
|
|
80
|
+
);
|
|
81
|
+
await signalCodexCapacityWakeTargets(services, targets);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function codexCapacityDecision(
|
|
85
|
+
context: CodexCapacitySelectionContext,
|
|
86
|
+
settings: Settings,
|
|
87
|
+
): ReturnType<Parameters<typeof reconcileCodexCapacityWaitDb>[2]> {
|
|
88
|
+
const now = new Date();
|
|
89
|
+
const selected = selectCodexCredentialLeaseForTurn({
|
|
90
|
+
context,
|
|
91
|
+
leasingEnabled: settings.codexCredentialLeasingEnabled,
|
|
92
|
+
sessionId: context.sessionId,
|
|
93
|
+
sessionPinnedCredentialId: context.sessionPinnedCredentialId,
|
|
94
|
+
sessionPinSource: context.sessionPinSource,
|
|
95
|
+
sessionLastCredentialId: context.sessionLastCredentialId,
|
|
96
|
+
continuationCredentialId: null,
|
|
97
|
+
nearExhaustionPct: settings.codexRotationNearExhaustionPct,
|
|
98
|
+
now,
|
|
99
|
+
});
|
|
100
|
+
if (selected.credentialId) {
|
|
101
|
+
return {
|
|
102
|
+
kind: "available",
|
|
103
|
+
credentialId: selected.credentialId,
|
|
104
|
+
diagnostic: {
|
|
105
|
+
connectedCount: context.accounts.length,
|
|
106
|
+
eligibleCount: context.accounts.filter((account) =>
|
|
107
|
+
isCodexCredentialEligible(account, settings.codexRotationNearExhaustionPct, now),
|
|
108
|
+
).length,
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
const authoritativeReset = authoritativeCodexCapacityResetAt(
|
|
113
|
+
context.accounts,
|
|
114
|
+
settings.codexRotationNearExhaustionPct,
|
|
115
|
+
now,
|
|
116
|
+
);
|
|
117
|
+
return {
|
|
118
|
+
kind: "unavailable",
|
|
119
|
+
earliestResetAt: authoritativeReset,
|
|
120
|
+
resetKind: authoritativeReset ? "authoritative" : "bounded_refresh",
|
|
121
|
+
diagnostic: {
|
|
122
|
+
connectedCount: context.accounts.length,
|
|
123
|
+
allocatorEnabledCount: context.accounts.filter((account) => account.allocatorEnabled).length,
|
|
124
|
+
policyHash: context.policyHash,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async function refreshCapacityMetadata(
|
|
130
|
+
services: ActivityServices,
|
|
131
|
+
workspaceId: string,
|
|
132
|
+
): Promise<void> {
|
|
133
|
+
const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
|
|
134
|
+
const stale = accounts.filter(
|
|
135
|
+
(account) =>
|
|
136
|
+
account.allocatorEnabled &&
|
|
137
|
+
account.status === "active" &&
|
|
138
|
+
((account.primaryUsedPercent ?? 0) >= services.settings.codexRotationNearExhaustionPct ||
|
|
139
|
+
(account.secondaryUsedPercent ?? 0) >= services.settings.codexRotationNearExhaustionPct ||
|
|
140
|
+
account.usageCheckedAt === null),
|
|
141
|
+
);
|
|
142
|
+
await refreshCodexUsageAndRepairCapacityWaiters(
|
|
143
|
+
stale.map(
|
|
144
|
+
(account) => () =>
|
|
145
|
+
fetchCodexUsageForAccount(services.db, services.settings, workspaceId, account.id),
|
|
146
|
+
),
|
|
147
|
+
() => signalPendingCodexCapacityWakeTargets(services, workspaceId),
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function createCodexCapacityActivities(services: () => Promise<ActivityServices>) {
|
|
152
|
+
async function getCodexCapacityWait(input: GetCodexCapacityWaitInput) {
|
|
153
|
+
const { db } = await services();
|
|
154
|
+
const waiter = await getCodexCapacityWaitForSession(db, input.workspaceId, input.sessionId);
|
|
155
|
+
return waiter
|
|
156
|
+
? {
|
|
157
|
+
waiterId: waiter.id,
|
|
158
|
+
generation: waiter.generation,
|
|
159
|
+
// A capacity mutation may have committed while its Temporal signal
|
|
160
|
+
// was lost or while the workflow continued-as-new. Reconstruct that
|
|
161
|
+
// outbox edge as an immediate re-evaluation rather than waiting for
|
|
162
|
+
// the older timer.
|
|
163
|
+
nextCheckAt:
|
|
164
|
+
waiter.wakeRevision > waiter.observedWakeRevision
|
|
165
|
+
? new Date(0).toISOString()
|
|
166
|
+
: waiter.nextCheckAt.toISOString(),
|
|
167
|
+
wakeRevision: waiter.wakeRevision,
|
|
168
|
+
}
|
|
169
|
+
: null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function reconcileCodexCapacityWait(
|
|
173
|
+
input: ReconcileCodexCapacityWaitInput,
|
|
174
|
+
): Promise<ReconcileCodexCapacityWaitResult> {
|
|
175
|
+
const resolved = await services();
|
|
176
|
+
const current = await getCodexCapacityWaitForSession(
|
|
177
|
+
resolved.db,
|
|
178
|
+
input.workspaceId,
|
|
179
|
+
input.sessionId,
|
|
180
|
+
);
|
|
181
|
+
if (!current || current.id !== input.waiterId || current.generation !== input.generation) {
|
|
182
|
+
return { action: "stale" };
|
|
183
|
+
}
|
|
184
|
+
if (input.cause === "timer" && current.nextCheckAt.getTime() <= Date.now()) {
|
|
185
|
+
// This is a bounded secret-safe control-plane quota refresh. It creates no
|
|
186
|
+
// turn, model call, user message, schedule, or entitlement action.
|
|
187
|
+
await refreshCapacityMetadata(resolved, input.workspaceId);
|
|
188
|
+
}
|
|
189
|
+
const result = await reconcileCodexCapacityWaitDb(
|
|
190
|
+
resolved.db,
|
|
191
|
+
{
|
|
192
|
+
accountId: input.accountId,
|
|
193
|
+
workspaceId: input.workspaceId,
|
|
194
|
+
sessionId: input.sessionId,
|
|
195
|
+
waiterId: input.waiterId,
|
|
196
|
+
generation: input.generation,
|
|
197
|
+
},
|
|
198
|
+
(context) => codexCapacityDecision(context, resolved.settings),
|
|
199
|
+
);
|
|
200
|
+
if (result.events.length > 0) {
|
|
201
|
+
try {
|
|
202
|
+
await resolved.bus.publish(input.workspaceId, input.sessionId, result.events);
|
|
203
|
+
} catch {
|
|
204
|
+
// Postgres is authoritative; SSE replay/gap fill repairs missed fanout.
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (result.action === "resumed") {
|
|
208
|
+
return { action: "resumed", updateId: result.update.id };
|
|
209
|
+
}
|
|
210
|
+
if (result.action === "waiting") {
|
|
211
|
+
return {
|
|
212
|
+
action: "waiting",
|
|
213
|
+
waiterId: result.waiter.id,
|
|
214
|
+
generation: result.waiter.generation,
|
|
215
|
+
nextCheckAt: result.waiter.nextCheckAt.toISOString(),
|
|
216
|
+
wakeRevision: result.waiter.wakeRevision,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
return { action: result.action };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return { getCodexCapacityWait, reconcileCodexCapacityWait };
|
|
223
|
+
}
|