@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.
@@ -0,0 +1,331 @@
1
+ import type { Settings } from "@opengeni/config";
2
+ import { contextInputBudgetTokens, resolveContextCompactionMode } from "@opengeni/config";
3
+ import type { FileAsset, ResourceRef } from "@opengeni/contracts";
4
+ import {
5
+ getActiveSessionHistoryItems,
6
+ getLatestRunState,
7
+ getSandboxSessionEnvelope,
8
+ getSessionEvent,
9
+ requireFile,
10
+ type Database,
11
+ } from "@opengeni/db";
12
+ import { stripReasoningEncryptedContent, stripReasoningIdentityFromSerializedRunState, neutralizeToolSearchItemsInSerializedRunState, type OpenGeniRuntime } from "@opengeni/runtime";
13
+
14
+ /**
15
+ * The codex account THIS turn runs on, threaded into every history read path so a
16
+ * cross-account turn never replays another account's encrypted reasoning. The
17
+ * single rule across all paths: DROP any reasoning item whose producing codex
18
+ * account differs from `currentCodexCredentialId`.
19
+ *
20
+ * `currentCodexCredentialId` is the resolved codex credential id on a codex turn,
21
+ * or NULL on a non-codex turn (the "account" of the built-in Azure/OpenAI path).
22
+ * NULL is a real value in the comparison, not a "skip" sentinel: a non-codex turn
23
+ * (current = null) still drops codex-produced reasoning (producer != null) so a
24
+ * foreign encrypted blob never reaches the Azure/built-in Responses call. A
25
+ * session with no codex history (every producer == null == current) is a no-op.
26
+ */
27
+ export type TurnCodexAccount = { currentCodexCredentialId: string | null };
28
+
29
+ /** A non-codex turn's account (current = null): no codex credential resolved. */
30
+ const NON_CODEX_TURN: TurnCodexAccount = { currentCodexCredentialId: null };
31
+
32
+ /**
33
+ * Apply the cross-account reasoning strip to a set of stored history rows. Pure +
34
+ * non-mutating. The single rule: a row whose producing codex account EQUALS the
35
+ * turn's current account replays verbatim (by reference); a row produced by a
36
+ * DIFFERENT account is treated by item type —
37
+ *
38
+ * - `reasoning` → DROPPED WHOLE (id + blob filtered out of the history). The
39
+ * foreign `rs_…` id is validated by the Responses backend, which rejects a
40
+ * reasoning item that has a foreign id and no encrypted_content (store:false),
41
+ * so blanking only the blob is not enough — the whole item must go.
42
+ * - `compaction` → kept, with only its account-bound `encrypted_content` blob
43
+ * stripped (its summary is real conversation content that must survive).
44
+ * - everything else (messages, tool calls, tool outputs) → kept verbatim by
45
+ * reference; message and tool content are never account-bound, never touched.
46
+ *
47
+ * Mismatch covers a foreign codex account, the non-codex/Azure producer (null on
48
+ * a codex turn), and legacy untagged rows (null): all are stripped, which is
49
+ * defensive and harmless (at most one turn of lost chain-of-thought continuity,
50
+ * never any content). No-op (rows by reference) when every producer equals the
51
+ * current account — a single-account workspace, an unchanged-account turn, or a
52
+ * non-codex turn over a history with no codex-produced reasoning.
53
+ */
54
+ export function applyCodexHistoryStrip(
55
+ rows: ReadonlyArray<{ item: Record<string, unknown>; producerCodexCredentialId: string | null }>,
56
+ current: TurnCodexAccount,
57
+ ): Array<Record<string, unknown>> {
58
+ const out: Array<Record<string, unknown>> = [];
59
+ for (const row of rows) {
60
+ if (row.producerCodexCredentialId === current.currentCodexCredentialId) {
61
+ out.push(row.item);
62
+ continue;
63
+ }
64
+ const type = typeof row.item.type === "string" ? row.item.type : undefined;
65
+ if (type === "reasoning") {
66
+ // Foreign reasoning: drop the WHOLE item (id + blob) — see rule above.
67
+ continue;
68
+ }
69
+ if (type === "tool_search_call" || type === "tool_search_output") {
70
+ // Foreign tool_search items (progressive connector disclosure): drop WHOLE,
71
+ // like reasoning. The `tsc_…` id is minted by the producing account's
72
+ // backend, and the output's disclosure set reflects THAT account's
73
+ // connectors — replaying either into a different account risks a 400 /
74
+ // wrong-connector disclosure. Dropping both sides keeps the pair invariant
75
+ // (the history sanitizer would drop a stranded half anyway); the model
76
+ // simply re-searches on the new account, re-disclosing from the NEW
77
+ // account's own connector pool. Never any content loss — search items
78
+ // carry tool metadata, not conversation content.
79
+ continue;
80
+ }
81
+ if (type === "compaction") {
82
+ out.push(stripReasoningEncryptedContent(row.item));
83
+ continue;
84
+ }
85
+ out.push(row.item);
86
+ }
87
+ return out;
88
+ }
89
+
90
+ /**
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
94
+ * compare the codex account that FROZE the state to the resuming turn's account:
95
+ * when they differ, neutralize every reasoning item's account-bound identity
96
+ * (encrypted_content + provider id) in the blob; when they match (including
97
+ * null == null for non-codex / single-account) the blob replays byte-for-byte
98
+ * (same string reference). This closes the gap where a frozen A-minted RunState
99
+ * was replayed verbatim into a turn that switched to account B (or to a non-codex
100
+ * turn), 400ing the resume.
101
+ */
102
+ export function resumeRunStateForCodexAccount(
103
+ state: { serializedRunState: string; frozenCodexCredentialId: string | null },
104
+ current: TurnCodexAccount,
105
+ ): string {
106
+ if (state.frozenCodexCredentialId === current.currentCodexCredentialId) {
107
+ return state.serializedRunState;
108
+ }
109
+ // Cross-account: neutralize reasoning identity in place AND flip frozen
110
+ // tool_search pairs to execution:"server" in place (count-preserving — HOLE E
111
+ // forbids removing blob items). The server flip makes the SDK skip its
112
+ // client-executor rehydration (which would THROW when the resuming account's
113
+ // connector pool differs from the freezing account's); the flipped shape is
114
+ // live-verified wire-safe. The model can still re-search on this account.
115
+ return neutralizeToolSearchItemsInSerializedRunState(stripReasoningIdentityFromSerializedRunState(state.serializedRunState));
116
+ }
117
+
118
+ /**
119
+ * A prepared turn input plus the watermark-seed discriminator the reconcile pass
120
+ * needs (HOLE E). `modelHistoryFromItems` is TRUE iff `state.history` was seeded
121
+ * from the cross-account-STRIPPED active history items (the items read path) — so
122
+ * 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
125
+ * foreign reasoning is NEUTRALIZED-IN-PLACE by {@link resumeRunStateForCodexAccount}
126
+ * (the item is KEPT, only its id/encrypted_content go), so the blob's history
127
+ * length still COUNTS those items. Seeding the watermark with the strip on that
128
+ * path under-counts by K and the reconcile re-appends K already-persisted items at
129
+ * fresh positions — that is HOLE E. The watermark must therefore NOT strip on the
130
+ * blob path (count the raw sanitized active length, matching the blob).
131
+ */
132
+ export type PreparedTurnInput = {
133
+ input: Awaited<ReturnType<OpenGeniRuntime["prepareInput"]>>;
134
+ modelHistoryFromItems: boolean;
135
+ };
136
+
137
+ export async function turnInput(
138
+ db: Database,
139
+ runtime: OpenGeniRuntime,
140
+ agent: any,
141
+ trigger: Awaited<ReturnType<typeof getSessionEvent>>,
142
+ settings?: Settings,
143
+ current: TurnCodexAccount = NON_CODEX_TURN,
144
+ ): Promise<PreparedTurnInput> {
145
+ if (!trigger) {
146
+ throw new Error("Missing trigger event");
147
+ }
148
+ if (trigger.type === "user.message") {
149
+ const payload = trigger.payload as { text?: unknown; resources?: unknown };
150
+ if (typeof payload.text !== "string" || payload.text.trim().length === 0) {
151
+ throw new Error("user.message payload is missing text");
152
+ }
153
+ const text = await userMessageTextWithAttachments(
154
+ db,
155
+ trigger.workspaceId,
156
+ payload.text,
157
+ Array.isArray(payload.resources) ? payload.resources as ResourceRef[] : [],
158
+ );
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
+ }
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);
179
+ }
180
+ if (trigger.type === "user.approvalDecision") {
181
+ const payload = trigger.payload as {
182
+ approvalId?: unknown;
183
+ decision?: unknown;
184
+ message?: unknown;
185
+ };
186
+ // Approvals are the one path that legitimately requires the RunState blob:
187
+ // a turn frozen mid-flight cannot be represented as plain history items.
188
+ const state = await getLatestRunState(db, trigger.workspaceId, trigger.sessionId);
189
+ if (!state) {
190
+ throw new Error("No saved run state is available for approval decision");
191
+ }
192
+ return {
193
+ input: await runtime.prepareInput(agent, {
194
+ kind: "approval",
195
+ // Cross-account run-state strip (HOLE C): if the account resuming this
196
+ // frozen approval differs from the one that froze it, neutralize the
197
+ // blob's account-bound reasoning before replay (else byte-for-byte).
198
+ serializedRunState: resumeRunStateForCodexAccount(state, current),
199
+ approvalId: String(payload.approvalId ?? ""),
200
+ decision: payload.decision === "approve" ? "approve" : "reject",
201
+ ...(typeof payload.message === "string" ? { message: payload.message } : {}),
202
+ }),
203
+ // Model seeded from the run-state BLOB (neutralize-in-place), NOT stripped
204
+ // items: the reconcile watermark must NOT apply the cross-account strip
205
+ // (HOLE E) — else a cross-account approval resume re-appends K
206
+ // already-persisted items at fresh positions.
207
+ modelHistoryFromItems: false,
208
+ };
209
+ }
210
+ throw new Error(`Unsupported trigger event type: ${trigger.type}`);
211
+ }
212
+
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
+ */
221
+ async function messageInput(
222
+ db: Database,
223
+ runtime: OpenGeniRuntime,
224
+ agent: any,
225
+ trigger: NonNullable<Awaited<ReturnType<typeof getSessionEvent>>>,
226
+ text: string,
227
+ settings?: Settings,
228
+ current: TurnCodexAccount = NON_CODEX_TURN,
229
+ ): 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
+ // [active summary, ...active recent tail]; superseded (summarized-away)
238
+ // prefix rows stay in the 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
+ 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,
287
+ };
288
+ }
289
+
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
+ export async function userMessageTextWithAttachments(
305
+ db: Database,
306
+ workspaceId: string,
307
+ text: string,
308
+ resources: ResourceRef[],
309
+ ): Promise<string> {
310
+ const attachedFiles: string[] = [];
311
+ for (const resource of resources) {
312
+ if (resource.kind !== "file") {
313
+ continue;
314
+ }
315
+ const file = await requireFile(db, workspaceId, resource.fileId);
316
+ attachedFiles.push(`- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`);
317
+ }
318
+ if (attachedFiles.length === 0) {
319
+ return text;
320
+ }
321
+ return [
322
+ text,
323
+ "",
324
+ "Attached files are available in the sandbox:",
325
+ ...attachedFiles,
326
+ ].join("\n");
327
+ }
328
+
329
+ function sandboxFilePath(resource: Extract<ResourceRef, { kind: "file" }>, file: FileAsset): string {
330
+ return `/workspace/${resource.mountPath ?? `files/${file.id}`}/${file.safeFilename}`;
331
+ }