@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.
Files changed (44) hide show
  1. package/dist/index.d.ts +184 -68
  2. package/dist/index.js +7099 -1643
  3. package/dist/index.js.map +1 -1
  4. package/package.json +18 -18
  5. package/src/activities/agent-turn.ts +4332 -1068
  6. package/src/activities/capabilities.ts +43 -8
  7. package/src/activities/codex-capacity.ts +223 -0
  8. package/src/activities/codex-rotation.ts +550 -54
  9. package/src/activities/common.ts +14 -2
  10. package/src/activities/context-compaction.ts +152 -118
  11. package/src/activities/documents.ts +32 -27
  12. package/src/activities/environment.ts +318 -75
  13. package/src/activities/file-upload-reaper.ts +79 -0
  14. package/src/activities/git-credential-renewal.ts +183 -0
  15. package/src/activities/goals.ts +142 -123
  16. package/src/activities/packs.ts +55 -5
  17. package/src/activities/parent-wake.ts +216 -42
  18. package/src/activities/recording.ts +154 -122
  19. package/src/activities/redaction.ts +3 -1
  20. package/src/activities/rig-verification.ts +441 -0
  21. package/src/activities/run-input.ts +110 -122
  22. package/src/activities/sandbox-lease.ts +186 -51
  23. package/src/activities/scheduled-tasks.ts +287 -171
  24. package/src/activities/session-state.ts +165 -157
  25. package/src/activities/streaming.ts +143 -19
  26. package/src/activities/turn-attempt-fenced.ts +14 -0
  27. package/src/activities/types.ts +106 -36
  28. package/src/activities/workflow-wake.ts +44 -0
  29. package/src/activities/workspace-capture.ts +964 -0
  30. package/src/activities.ts +75 -35
  31. package/src/concurrency.ts +11 -0
  32. package/src/http.ts +16 -6
  33. package/src/index.ts +322 -49
  34. package/src/observability-metrics.ts +441 -8
  35. package/src/op-journal.ts +46 -0
  36. package/src/sandbox-resume.ts +431 -201
  37. package/src/sandbox-routing.ts +209 -22
  38. package/src/workflows/activities.ts +65 -8
  39. package/src/workflows/file-upload-reaper.ts +12 -0
  40. package/src/workflows/rig-verification.ts +28 -0
  41. package/src/workflows/scheduled-tasks.ts +12 -26
  42. package/src/workflows/session.ts +330 -192
  43. package/src/workflows/workflow-wake.ts +13 -0
  44. package/src/workflows.ts +9 -7
@@ -1,15 +1,19 @@
1
- import type { Settings } from "@opengeni/config";
2
- import { contextInputBudgetTokens, resolveContextCompactionMode } from "@opengeni/config";
3
- import type { FileAsset, ResourceRef } from "@opengeni/contracts";
1
+ import type { FileAsset, ResourceRef, SessionSystemUpdate } from "@opengeni/contracts";
4
2
  import {
5
3
  getActiveSessionHistoryItems,
6
4
  getLatestRunState,
7
5
  getSandboxSessionEnvelope,
8
6
  getSessionEvent,
7
+ listSessionSystemUpdatesForTurn,
9
8
  requireFile,
10
9
  type Database,
11
10
  } from "@opengeni/db";
12
- import { stripReasoningEncryptedContent, stripReasoningIdentityFromSerializedRunState, neutralizeToolSearchItemsInSerializedRunState, type OpenGeniRuntime } from "@opengeni/runtime";
11
+ import {
12
+ stripReasoningEncryptedContent,
13
+ stripReasoningIdentityFromSerializedRunState,
14
+ neutralizeToolSearchItemsInSerializedRunState,
15
+ type OpenGeniRuntime,
16
+ } from "@opengeni/runtime";
13
17
 
14
18
  /**
15
19
  * The codex account THIS turn runs on, threaded into every history read path so a
@@ -88,9 +92,9 @@ export function applyCodexHistoryStrip(
88
92
  }
89
93
 
90
94
  /**
91
- * Resolve the serialized RunState to replay on a run-state path (approval resume
92
- * or the items-mode run-state fallback), applying the SAME cross-account rule the
93
- * history-items path uses. The blob carries no per-item producer tag, so we
95
+ * Resolve the serialized RunState used only for an approval resume, applying
96
+ * the same cross-account rule as the canonical history-items path. The blob
97
+ * carries no per-item producer tag, so we
94
98
  * compare the codex account that FROZE the state to the resuming turn's account:
95
99
  * when they differ, neutralize every reasoning item's account-bound identity
96
100
  * (encrypted_content + provider id) in the blob; when they match (including
@@ -112,7 +116,9 @@ export function resumeRunStateForCodexAccount(
112
116
  // client-executor rehydration (which would THROW when the resuming account's
113
117
  // connector pool differs from the freezing account's); the flipped shape is
114
118
  // live-verified wire-safe. The model can still re-search on this account.
115
- return neutralizeToolSearchItemsInSerializedRunState(stripReasoningIdentityFromSerializedRunState(state.serializedRunState));
119
+ return neutralizeToolSearchItemsInSerializedRunState(
120
+ stripReasoningIdentityFromSerializedRunState(state.serializedRunState),
121
+ );
116
122
  }
117
123
 
118
124
  /**
@@ -120,8 +126,8 @@ export function resumeRunStateForCodexAccount(
120
126
  * needs (HOLE E). `modelHistoryFromItems` is TRUE iff `state.history` was seeded
121
127
  * from the cross-account-STRIPPED active history items (the items read path) — so
122
128
  * the turn-end reconcile must seed `persistedHistoryCount` from the SAME strip
123
- * (HOLE D). It is FALSE when `state.history` was seeded from the run-state BLOB
124
- * (approval resume, the items-mode run-state fallback, or run_state mode): there
129
+ * (HOLE D). It is FALSE only when `state.history` was seeded from the approval
130
+ * RunState: there
125
131
  * foreign reasoning is NEUTRALIZED-IN-PLACE by {@link resumeRunStateForCodexAccount}
126
132
  * (the item is KEPT, only its id/encrypted_content go), so the blob's history
127
133
  * length still COUNTS those items. Seeding the watermark with the strip on that
@@ -134,48 +140,63 @@ export type PreparedTurnInput = {
134
140
  modelHistoryFromItems: boolean;
135
141
  };
136
142
 
143
+ export type TurnInputOptions = {
144
+ turnId: string;
145
+ recovering?: boolean;
146
+ unavailableSandboxFilesNote?: string;
147
+ };
148
+
137
149
  export async function turnInput(
138
150
  db: Database,
139
151
  runtime: OpenGeniRuntime,
140
152
  agent: any,
141
153
  trigger: Awaited<ReturnType<typeof getSessionEvent>>,
142
- settings?: Settings,
143
154
  current: TurnCodexAccount = NON_CODEX_TURN,
155
+ options: TurnInputOptions,
144
156
  ): Promise<PreparedTurnInput> {
145
157
  if (!trigger) {
146
158
  throw new Error("Missing trigger event");
147
159
  }
160
+ const updates = await listSessionSystemUpdatesForTurn(
161
+ db,
162
+ trigger.workspaceId,
163
+ trigger.sessionId,
164
+ options.turnId,
165
+ );
166
+ const updateContext = systemUpdateContext(updates);
167
+ const internalContext = joinInternalContext(
168
+ options.recovering
169
+ ? [
170
+ "[OpenGeni inference recovery]",
171
+ "Continue the same inference from durable conversation and sandbox state. A previous execution stopped before it could finish. Do not repeat completed side effects; inspect actual state when uncertain.",
172
+ ].join("\n")
173
+ : undefined,
174
+ updateContext,
175
+ options.unavailableSandboxFilesNote,
176
+ );
148
177
  if (trigger.type === "user.message") {
149
178
  const payload = trigger.payload as { text?: unknown; resources?: unknown };
150
179
  if (typeof payload.text !== "string" || payload.text.trim().length === 0) {
151
180
  throw new Error("user.message payload is missing text");
152
181
  }
153
- const text = await userMessageTextWithAttachments(
182
+ const attachmentContext = await userMessageAttachmentsContext(
154
183
  db,
155
184
  trigger.workspaceId,
156
- payload.text,
157
- Array.isArray(payload.resources) ? payload.resources as ResourceRef[] : [],
185
+ Array.isArray(payload.resources) ? (payload.resources as ResourceRef[]) : [],
186
+ );
187
+ return await messageInput(
188
+ db,
189
+ runtime,
190
+ agent,
191
+ trigger,
192
+ undefined,
193
+ joinInternalContext(internalContext, attachmentContext),
194
+ current,
158
195
  );
159
- return await messageInput(db, runtime, agent, trigger, text, settings, current);
160
- }
161
- if (trigger.type === "goal.continuation") {
162
- const payload = trigger.payload as { text?: unknown };
163
- if (typeof payload.text !== "string" || payload.text.trim().length === 0) {
164
- throw new Error("goal.continuation payload is missing text");
165
- }
166
- // Threading the stored conversation keeps the agent's full context across
167
- // continuations — this is what makes "keep working" coherent.
168
- return await messageInput(db, runtime, agent, trigger, payload.text, settings, current);
169
196
  }
170
- if (trigger.type === "turn.preempted") {
171
- const payload = trigger.payload as { text?: unknown };
172
- if (typeof payload.text !== "string" || payload.text.trim().length === 0) {
173
- throw new Error("turn.preempted payload is missing text");
174
- }
175
- // A turn re-entering after a graceful worker shutdown checkpointed it
176
- // mid-flight: thread the stored conversation (which includes the turn's
177
- // original input and its progress so far) behind a resume notice.
178
- return await messageInput(db, runtime, agent, trigger, payload.text, settings, current);
197
+ if (trigger.type === "system.update.delivered") {
198
+ if (!internalContext) throw new Error("Internal update inference has no delivered updates");
199
+ return await messageInput(db, runtime, agent, trigger, undefined, internalContext, current);
179
200
  }
180
201
  if (trigger.type === "user.approvalDecision") {
181
202
  const payload = trigger.payload as {
@@ -210,122 +231,89 @@ export async function turnInput(
210
231
  throw new Error(`Unsupported trigger event type: ${trigger.type}`);
211
232
  }
212
233
 
213
- /**
214
- * Build a message/continuation turn input from the configured history source.
215
- * Items mode reads conversation truth from session_history_items and the
216
- * sandbox envelope from its own store; a session with no stored items yet
217
- * (created before dual-write, or its first turn) falls back to the RunState
218
- * blob for this turn — the turn-end reconciliation then backfills its items,
219
- * so the fallback is self-eliminating (issue #35).
220
- */
234
+ function joinInternalContext(...parts: Array<string | undefined>): string | undefined {
235
+ const content = parts.map((part) => part?.trim()).filter((part): part is string => Boolean(part));
236
+ return content.length > 0 ? content.join("\n\n") : undefined;
237
+ }
238
+
239
+ function systemUpdateContext(updates: SessionSystemUpdate[]): string | undefined {
240
+ if (updates.length === 0) return undefined;
241
+ return [
242
+ "[OpenGeni internal updates]",
243
+ "These platform updates were delivered together for this inference. They are not human prompts.",
244
+ JSON.stringify({
245
+ updates: updates.map((update) => ({
246
+ id: update.id,
247
+ kind: update.kind,
248
+ classification: update.classification,
249
+ sourceId: update.sourceId,
250
+ summary: update.summary,
251
+ payload: update.payload,
252
+ lineage: update.lineage,
253
+ })),
254
+ }),
255
+ ].join("\n");
256
+ }
257
+
258
+ /** Build one inference from canonical history plus optional ephemeral system context. */
221
259
  async function messageInput(
222
260
  db: Database,
223
261
  runtime: OpenGeniRuntime,
224
262
  agent: any,
225
263
  trigger: NonNullable<Awaited<ReturnType<typeof getSessionEvent>>>,
226
- text: string,
227
- settings?: Settings,
264
+ text: string | undefined,
265
+ internalContext: string | undefined,
228
266
  current: TurnCodexAccount = NON_CODEX_TURN,
229
267
  ): Promise<PreparedTurnInput> {
230
- // Read-path budget guard (the last-resort backstop behind best-effort pre-turn
231
- // compaction): supply B only when the client-side compaction path is active
232
- // (Azure). On the OpenAI server path the SDK manages the window, so we leave
233
- // the guard off and never crudely trim. Undefined = guard disabled.
234
- const inputBudgetTokens = readPathBudgetTokens(settings);
235
- if (settings?.sessionHistorySource === "items") {
236
- // Active rows only: after a client-side context compaction this is
237
- // [retained user messages..., active summary]; superseded rows stay in the
238
- // table as an audit trail but never reach the model.
239
- const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
240
- if (stored.length > 0) {
241
- const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
242
- // Cross-account reasoning strip: drop any carried reasoning item NOT
243
- // produced by THIS turn's codex account (foreign reasoning is dropped
244
- // whole — id + blob; a foreign blob 400s the codex backend and a foreign
245
- // rs_ id is rejected by the Responses backend). No-op for single-account
246
- // workspaces, unchanged-account turns, and non-codex turns over a history
247
- // with no codex reasoning (every producer == current) — those replay
248
- // byte-for-byte. Message and tool content is never touched.
249
- const historyItems = applyCodexHistoryStrip(stored, current);
250
- return {
251
- input: await runtime.prepareInput(
252
- agent,
253
- {
254
- kind: "message",
255
- text,
256
- historyItems: historyItems as any,
257
- sandboxEnvelope: envelope,
258
- },
259
- inputBudgetTokens ? { inputBudgetTokens } : {},
260
- ),
261
- // state.history seeded from the cross-account-STRIPPED active items: the
262
- // reconcile watermark must apply the SAME strip (HOLE D).
263
- modelHistoryFromItems: true,
264
- };
265
- }
266
- }
267
- const latestState = await getLatestRunState(db, trigger.workspaceId, trigger.sessionId);
268
+ const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
269
+ const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
270
+ const historyItems = applyCodexHistoryStrip(stored, current);
268
271
  return {
269
- input: await runtime.prepareInput(
270
- agent,
271
- {
272
- kind: "message",
273
- text,
274
- // Cross-account run-state strip (HOLE C): the items-mode fallback replays
275
- // the RunState blob when no history rows exist yet. If the resuming turn's
276
- // codex account differs from the one that froze the blob, neutralize its
277
- // account-bound reasoning before replay (else byte-for-byte).
278
- serializedRunState: latestState ? resumeRunStateForCodexAccount(latestState, current) : null,
279
- },
280
- inputBudgetTokens ? { inputBudgetTokens } : {},
281
- ),
282
- // state.history seeded from the run-state BLOB (or empty): NOT the stripped
283
- // items, so the reconcile watermark must NOT apply the cross-account strip
284
- // (HOLE E). On this fallback the active rows are empty anyway (the read above
285
- // took the items branch when stored.length > 0), so strip-or-not both yield 0.
286
- modelHistoryFromItems: false,
272
+ input: await runtime.prepareInput(agent, {
273
+ kind: "message",
274
+ ...(text ? { text } : {}),
275
+ ...(internalContext ? { internalContext } : {}),
276
+ historyItems: historyItems as any,
277
+ sandboxEnvelope: envelope,
278
+ }),
279
+ modelHistoryFromItems: true,
287
280
  };
288
281
  }
289
282
 
290
- /**
291
- * The usable input-token budget B to hand the read-path guard, or undefined
292
- * when the guard should stay off. Active only when the resolved compaction mode
293
- * is "client" (the Azure path that runs our own compaction); on the server path
294
- * the SDK enforces the window, and with no settings we can't compute B.
295
- */
296
- function readPathBudgetTokens(settings?: Settings): number | undefined {
297
- if (!settings || resolveContextCompactionMode(settings) !== "client") {
298
- return undefined;
299
- }
300
- const budget = contextInputBudgetTokens(settings);
301
- return budget > 0 ? budget : undefined;
302
- }
303
-
304
283
  export async function userMessageTextWithAttachments(
305
284
  db: Database,
306
285
  workspaceId: string,
307
286
  text: string,
308
287
  resources: ResourceRef[],
309
288
  ): Promise<string> {
289
+ const attachmentContext = await userMessageAttachmentsContext(db, workspaceId, resources);
290
+ return attachmentContext ? [text, "", attachmentContext].join("\n") : text;
291
+ }
292
+
293
+ async function userMessageAttachmentsContext(
294
+ db: Database,
295
+ workspaceId: string,
296
+ resources: ResourceRef[],
297
+ ): Promise<string | undefined> {
310
298
  const attachedFiles: string[] = [];
311
299
  for (const resource of resources) {
312
300
  if (resource.kind !== "file") {
313
301
  continue;
314
302
  }
315
303
  const file = await requireFile(db, workspaceId, resource.fileId);
316
- attachedFiles.push(`- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`);
304
+ attachedFiles.push(
305
+ `- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`,
306
+ );
317
307
  }
318
308
  if (attachedFiles.length === 0) {
319
- return text;
309
+ return undefined;
320
310
  }
321
- return [
322
- text,
323
- "",
324
- "Attached files are available in the sandbox:",
325
- ...attachedFiles,
326
- ].join("\n");
311
+ return ["Attached files are available in the sandbox:", ...attachedFiles].join("\n");
327
312
  }
328
313
 
329
- function sandboxFilePath(resource: Extract<ResourceRef, { kind: "file" }>, file: FileAsset): string {
314
+ function sandboxFilePath(
315
+ resource: Extract<ResourceRef, { kind: "file" }>,
316
+ file: FileAsset,
317
+ ): string {
330
318
  return `/workspace/${resource.mountPath ?? `files/${file.id}`}/${file.safeFilename}`;
331
319
  }