@opengeni/worker-bundle 0.10.8 → 0.11.1

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,296 @@
1
+ import {
2
+ DEFAULT_CODEX_FLEET_POLICY_V1,
3
+ compareCodexFleetCanonicalStringsV1,
4
+ createCodexFleetReplayRecordV1,
5
+ type CodexFleetCandidateStatus,
6
+ type CodexFleetDecisionInputV1,
7
+ type CodexFleetReplayRecordV1,
8
+ } from "@opengeni/contracts";
9
+ import type { CodexLeaseAccountStatus } from "@opengeni/db";
10
+ import { CancelledFailure } from "@temporalio/activity";
11
+ import { createHmac } from "node:crypto";
12
+ import { TurnAttemptFencedError } from "./turn-attempt-fenced";
13
+
14
+ export const CODEX_FLEET_SHADOW_MAX_PAYLOAD_BYTES = 34 * 1_024;
15
+
16
+ export type CodexFleetActualDecisionV1 = {
17
+ outcome: "selected" | "waiting" | "none";
18
+ candidateKey: string | null;
19
+ reason: "lease_reused" | "pin" | "rotation" | "active" | "all_capped" | "none";
20
+ };
21
+
22
+ export type CodexFleetShadowComparisonV1 =
23
+ | "match"
24
+ | "different_candidate"
25
+ | "different_outcome"
26
+ | "not_comparable_truncated";
27
+
28
+ export type CodexFleetShadowPayloadV1 = {
29
+ schemaVersion: 1;
30
+ mode: "shadow";
31
+ actual: CodexFleetActualDecisionV1;
32
+ comparison: CodexFleetShadowComparisonV1;
33
+ replay: CodexFleetReplayRecordV1;
34
+ };
35
+
36
+ export type CodexFleetShadowBuildInputV1 = {
37
+ accounts: readonly CodexLeaseAccountStatus[];
38
+ actualCredentialId: string | null;
39
+ actualOutcome: CodexFleetActualDecisionV1["outcome"];
40
+ actualReason: CodexFleetActualDecisionV1["reason"];
41
+ affinityCredentialId: string | null;
42
+ fencedInFlight: boolean;
43
+ nearExhaustionPct: number;
44
+ now: Date;
45
+ /** Per-event entropy used only to unlink account aliases; never persisted. */
46
+ aliasSeed: string;
47
+ };
48
+
49
+ export type CodexFleetShadowPublicationResultV1 =
50
+ | { outcome: "disabled" }
51
+ | {
52
+ outcome: "published";
53
+ payload: CodexFleetShadowPayloadV1;
54
+ payloadBytes: number;
55
+ }
56
+ | {
57
+ outcome: "failed";
58
+ stage: "build" | "serialize" | "publish";
59
+ reason: "payload_too_large" | "unexpected";
60
+ errorName: string;
61
+ payloadBytes: number | null;
62
+ };
63
+
64
+ export const CODEX_FLEET_SHADOW_DECISION_MAX_METRIC_SERIES = 3 * 3 * 4 * 4 * 2;
65
+ export const CODEX_FLEET_SHADOW_ERROR_MAX_METRIC_SERIES = 3 * 2;
66
+
67
+ /** Fixed low-cardinality labels; workspace/account/tenant identity is structurally absent. */
68
+ export function codexFleetShadowDecisionMetricLabelsV1(payload: CodexFleetShadowPayloadV1) {
69
+ return {
70
+ actual_outcome: payload.actual.outcome,
71
+ shadow_outcome: payload.replay.decision.outcome,
72
+ comparison: payload.comparison,
73
+ confidence: payload.replay.decision.confidence,
74
+ truncated: payload.replay.truncatedCandidateCount > 0 ? "true" : "false",
75
+ } as const;
76
+ }
77
+
78
+ export function codexFleetShadowErrorMetricLabelsV1(
79
+ result: Extract<CodexFleetShadowPublicationResultV1, { outcome: "failed" }>,
80
+ ) {
81
+ return { stage: result.stage, reason: result.reason } as const;
82
+ }
83
+
84
+ /**
85
+ * Default-off, bounded, fail-open runtime seam for the shadow record.
86
+ *
87
+ * The typed result intentionally carries no error message: provider, database,
88
+ * or transport errors can include private metadata. Callers may count/log only
89
+ * the bounded stage, reason, name, and size fields returned here. A failure can
90
+ * never replace or clear the credential already selected by the allocator.
91
+ */
92
+ export async function publishCodexFleetShadowDecisionV1(input: {
93
+ enabled: boolean;
94
+ decision: CodexFleetShadowBuildInputV1;
95
+ publish: (
96
+ events: Array<{
97
+ type: "codex.fleet.decision";
98
+ payload: CodexFleetShadowPayloadV1;
99
+ }>,
100
+ ) => Promise<unknown>;
101
+ }): Promise<CodexFleetShadowPublicationResultV1> {
102
+ if (!input.enabled) return { outcome: "disabled" };
103
+
104
+ let stage: "build" | "serialize" | "publish" = "build";
105
+ let payloadBytes: number | null = null;
106
+ try {
107
+ const payload = buildCodexFleetShadowPayloadV1(input.decision);
108
+ stage = "serialize";
109
+ payloadBytes = new TextEncoder().encode(JSON.stringify(payload)).byteLength;
110
+ if (payloadBytes > CODEX_FLEET_SHADOW_MAX_PAYLOAD_BYTES) {
111
+ return {
112
+ outcome: "failed",
113
+ stage,
114
+ reason: "payload_too_large",
115
+ errorName: "CodexFleetShadowPayloadTooLargeError",
116
+ payloadBytes,
117
+ };
118
+ }
119
+ stage = "publish";
120
+ await input.publish([{ type: "codex.fleet.decision", payload }]);
121
+ return { outcome: "published", payload, payloadBytes };
122
+ } catch (error) {
123
+ // These are authoritative activity lifecycle signals, not shadow failures.
124
+ // Swallowing either would allow a superseded attempt to continue mutating.
125
+ if (error instanceof TurnAttemptFencedError || error instanceof CancelledFailure) throw error;
126
+ return {
127
+ outcome: "failed",
128
+ stage,
129
+ reason: "unexpected",
130
+ errorName: safeErrorName(error),
131
+ payloadBytes,
132
+ };
133
+ }
134
+ }
135
+
136
+ export function buildCodexFleetShadowPayloadV1(
137
+ input: CodexFleetShadowBuildInputV1,
138
+ ): CodexFleetShadowPayloadV1 {
139
+ // Randomized keyed ordering makes c00/c01 unlinkable across events while the
140
+ // supplied event seed keeps this one replay/payload build deterministic.
141
+ const eventAccounts = [...input.accounts].sort(
142
+ (left, right) =>
143
+ compareCodexFleetCanonicalStringsV1(
144
+ aliasSortKey(input.aliasSeed, left.id),
145
+ aliasSortKey(input.aliasSeed, right.id),
146
+ ) || compareCodexFleetCanonicalStringsV1(left.id, right.id),
147
+ );
148
+ const aliases = new Map(
149
+ eventAccounts.map((account, index) => [account.id, candidateAlias(index)] as const),
150
+ );
151
+ const actualCandidateKey = input.actualCredentialId
152
+ ? (aliases.get(input.actualCredentialId) ?? null)
153
+ : null;
154
+ const currentCandidateKey = input.affinityCredentialId
155
+ ? (aliases.get(input.affinityCredentialId) ?? null)
156
+ : null;
157
+ const decisionInput: CodexFleetDecisionInputV1 = {
158
+ observedAtMs: input.now.getTime(),
159
+ request: {
160
+ placement: input.fencedInFlight ? "fenced_in_flight" : "new",
161
+ priority: "standard",
162
+ currentCandidateKey: input.fencedInFlight ? actualCandidateKey : currentCandidateKey,
163
+ waitAgeMs: 0,
164
+ overlayKey: null,
165
+ overlayMode: "none",
166
+ },
167
+ admission: {
168
+ // The shadow policy does not invent hard capacity from account count. A later typed
169
+ // admission snapshot supplies dynamic capacity; shadow v1 records unknown.
170
+ dynamicCapacityUnits: null,
171
+ inUseUnits: input.accounts.reduce((sum, account) => sum + account.activeLeaseCount, 0),
172
+ queuedManagerCount: 0,
173
+ emergencyFuseActive: false,
174
+ },
175
+ candidates: eventAccounts.map((account, index) => ({
176
+ key: candidateAlias(index),
177
+ status: normalizeStatus(account.status),
178
+ allocatorEnabled: account.allocatorEnabled,
179
+ cooldownRemainingMs: remainingMs(account.exhaustedUntil, input.now),
180
+ activeLeaseCount: account.activeLeaseCount,
181
+ quota: {
182
+ primary: {
183
+ usedPercent: account.primaryUsedPercent,
184
+ resetRemainingMs: remainingMs(account.primaryResetAt, input.now),
185
+ },
186
+ secondary: {
187
+ usedPercent: account.secondaryUsedPercent,
188
+ resetRemainingMs: remainingMs(account.secondaryResetAt, input.now),
189
+ },
190
+ checkedAgeMs: ageMs(account.usageCheckedAt, input.now),
191
+ confidence: quotaConfidence(account),
192
+ },
193
+ // Current production cache evidence is aggregate metric + opaque log data,
194
+ // not allocator state. Preserve that gap as unknown instead of fabricating 0.
195
+ cache: {
196
+ hitRatio: null,
197
+ sampledTokens: null,
198
+ checkedAgeMs: null,
199
+ confidence: "unknown",
200
+ state: "unknown",
201
+ thresholdObservedForMs: null,
202
+ },
203
+ // The shadow policy has no typed workspace-local burn feed at this boundary yet.
204
+ observedBurn: {
205
+ primaryPercentPerHour: null,
206
+ secondaryPercentPerHour: null,
207
+ confidence: "unknown",
208
+ },
209
+ // Provider accounting has not supplied a typed burn observation yet.
210
+ // Unknown inference is explicit and contributes no fabricated tenant truth.
211
+ inferredUnexplainedBurn: {
212
+ primaryPercentPerHour: null,
213
+ secondaryPercentPerHour: null,
214
+ confidence: "unknown",
215
+ },
216
+ overlayKeys: [],
217
+ })),
218
+ };
219
+ const replay = createCodexFleetReplayRecordV1(decisionInput, {
220
+ ...DEFAULT_CODEX_FLEET_POLICY_V1,
221
+ placementUsageCeilingPercent: input.nearExhaustionPct,
222
+ });
223
+ const actual: CodexFleetActualDecisionV1 = {
224
+ outcome: input.actualOutcome,
225
+ candidateKey: actualCandidateKey,
226
+ reason: input.actualReason,
227
+ };
228
+ return {
229
+ schemaVersion: 1,
230
+ mode: "shadow",
231
+ actual,
232
+ comparison: compareDecision(actual, replay),
233
+ replay,
234
+ };
235
+ }
236
+
237
+ function compareDecision(
238
+ actual: CodexFleetActualDecisionV1,
239
+ replay: CodexFleetReplayRecordV1,
240
+ ): CodexFleetShadowComparisonV1 {
241
+ if (
242
+ replay.truncatedCandidateCount > 0 &&
243
+ actual.candidateKey !== null &&
244
+ !replay.input.candidates.some((candidate) => candidate.key === actual.candidateKey)
245
+ ) {
246
+ return "not_comparable_truncated";
247
+ }
248
+ const shadowOutcome = replay.decision.outcome === "selected" ? "selected" : "none";
249
+ const actualComparableOutcome = actual.outcome === "selected" ? "selected" : "none";
250
+ if (shadowOutcome !== actualComparableOutcome) return "different_outcome";
251
+ if (shadowOutcome === "selected") {
252
+ return replay.decision.selectedCandidateKey === actual.candidateKey
253
+ ? "match"
254
+ : "different_candidate";
255
+ }
256
+ return "match";
257
+ }
258
+
259
+ function normalizeStatus(status: string): CodexFleetCandidateStatus {
260
+ if (status === "active" || status === "needs_relogin" || status === "error") return status;
261
+ return "unknown";
262
+ }
263
+
264
+ function candidateAlias(index: number): string {
265
+ return `c${index.toString(36).padStart(2, "0")}`;
266
+ }
267
+
268
+ function aliasSortKey(seed: string, credentialId: string): string {
269
+ return createHmac("sha256", seed).update(credentialId).digest("hex");
270
+ }
271
+
272
+ function quotaConfidence(account: CodexLeaseAccountStatus): "unknown" | "low" | "medium" | "high" {
273
+ if (!account.usageCheckedAt) return "unknown";
274
+ const windows = [
275
+ [account.primaryUsedPercent, account.primaryResetAt],
276
+ [account.secondaryUsedPercent, account.secondaryResetAt],
277
+ ] as const;
278
+ const complete = windows.filter(([used, reset]) => used !== null && reset !== null).length;
279
+ const partial = windows.some(([used, reset]) => (used === null) !== (reset === null));
280
+ if (complete === 0) return "unknown";
281
+ if (partial) return "low";
282
+ return complete === windows.length ? "high" : "medium";
283
+ }
284
+
285
+ function remainingMs(value: Date | null, now: Date): number | null {
286
+ return value ? Math.max(0, value.getTime() - now.getTime()) : null;
287
+ }
288
+
289
+ function ageMs(value: Date | null, now: Date): number | null {
290
+ return value ? Math.max(0, now.getTime() - value.getTime()) : null;
291
+ }
292
+
293
+ function safeErrorName(error: unknown): string {
294
+ if (!(error instanceof Error)) return "UnknownError";
295
+ return /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(error.name) ? error.name : "Error";
296
+ }
@@ -11,20 +11,17 @@ import {
11
11
  type ToolRef,
12
12
  } from "@opengeni/contracts";
13
13
  import {
14
- addSessionSystemUpdate,
15
14
  enqueueSessionWorkflowWakeIfRunnable,
16
- evaluateGoalContinuation,
17
15
  getBillingBalance,
18
16
  getLatestStartedSessionTurn,
19
17
  getWorkspaceModelPolicy,
20
18
  getSessionGoal,
21
19
  isCodexBilledTurn,
22
- recordUsageEvent,
20
+ materializeGoalContinuation,
23
21
  requireSession,
24
22
  sumUsageQuantity,
25
23
  type Database,
26
24
  } from "@opengeni/db";
27
- import { appendAndPublishEvents } from "@opengeni/events";
28
25
  import type { ActivityServices, MaybeContinueGoalInput, MaybeContinueGoalResult } from "./types";
29
26
 
30
27
  export function createGoalActivities(services: () => Promise<ActivityServices>) {
@@ -111,92 +108,29 @@ export function createGoalActivities(services: () => Promise<ActivityServices>)
111
108
  input.workspaceId,
112
109
  isCodexRun,
113
110
  );
114
- const decision = await evaluateGoalContinuation(db, {
111
+ const decision = await materializeGoalContinuation(db, {
112
+ accountId: input.accountId,
115
113
  workspaceId: input.workspaceId,
116
114
  sessionId: input.sessionId,
115
+ workflowId: input.workflowId,
117
116
  defaultMaxAutoContinuations: settings.goalMaxAutoContinuations ?? null,
118
117
  noProgressLimit: settings.goalNoProgressLimit,
119
118
  // A model-policy block takes precedence: it is deterministic (a budget
120
119
  // pause can clear on its own; a policy pause needs a model/policy change)
121
120
  // and rides the same visible-pause channel.
122
121
  budgetBlocked: modelPolicyBlocked ?? budgetBlocked,
123
- });
124
- if (decision.decision === "none" || decision.decision === "queue") {
125
- return { action: decision.decision };
126
- }
127
- if (decision.decision === "paused") {
128
- await appendAndPublishEvents(db, bus, input.workspaceId, input.sessionId, [
129
- {
130
- type: "goal.paused",
131
- payload: {
132
- goalId: decision.goal.id,
133
- actor: "system",
134
- reason: decision.reason,
135
- ...(decision.goal.rationale ? { rationale: decision.goal.rationale } : {}),
136
- autoContinuations: decision.goal.autoContinuations,
137
- noProgressStreak: decision.goal.noProgressStreak,
138
- },
139
- },
140
- ]);
141
- return { action: "paused" };
142
- }
143
- // Stop/continue race guard: a concurrent goal_complete/goal_pause/operator
144
- // PATCH between the locked decision and this synthesis must win. The
145
- // version check also catches a replace. A pause landing after this point
146
- // results in at most one already-admitted continuation turn; the next
147
- // pass sees the non-active goal and stops, and interrupt-driven pauses
148
- // additionally cancel the claimed turn via the workflow interrupt path.
149
- const recheck = await getSessionGoal(db, input.workspaceId, input.sessionId);
150
- if (!recheck || recheck.status !== "active" || recheck.version !== decision.goal.version) {
151
- return { action: "none" };
152
- }
153
- const prompt = goalContinuationPrompt(decision.goal, decision.autoContinuation, decision.cap);
154
- const update = await addSessionSystemUpdate(db, {
155
- accountId: input.accountId,
156
- workspaceId: input.workspaceId,
157
- sessionId: input.sessionId,
158
- kind: "goal_continuation",
159
- classification: "info",
160
- sourceId: decision.goal.id,
161
- dedupeKey: `goal-continuation:${decision.goal.id}:${decision.goal.version}:${decision.autoContinuation}`,
162
- summary: prompt,
163
- payload: {
164
- type: "goal_continuation",
165
- goalId: decision.goal.id,
166
- goalVersion: decision.goal.version,
167
- autoContinuation: decision.autoContinuation,
168
- maxAutoContinuations: decision.cap,
169
- prompt,
170
- policy: {
171
- model: continuationModel,
172
- reasoningEffort: continuationReasoningEffort,
173
- tools: withFirstPartyTools(settings, session.tools),
174
- sandboxBackend: session.sandboxBackend,
175
- },
122
+ policy: {
123
+ model: continuationModel,
124
+ reasoningEffort: continuationReasoningEffort,
125
+ tools: withFirstPartyTools(settings, session.tools),
126
+ sandboxBackend: session.sandboxBackend,
176
127
  },
177
- lineage: { goalId: decision.goal.id },
128
+ prompt: goalContinuationPrompt,
178
129
  });
179
- if (update.reason === "session_cancelled") return { action: "none" };
180
- if (update.added && update.events.length > 0) {
181
- await bus.publish(input.workspaceId, input.sessionId, update.events);
130
+ if (decision.events.length > 0) {
131
+ await bus.publish(input.workspaceId, input.sessionId, decision.events);
182
132
  }
183
- // Continuations count as agent runs for limits/metering parity with
184
- // user-initiated and scheduled turns.
185
- await recordUsageEvent(db, {
186
- accountId: input.accountId,
187
- workspaceId: input.workspaceId,
188
- eventType: "agent_run.created",
189
- quantity: 1,
190
- unit: "run",
191
- sourceResourceType: "session_system_update",
192
- sourceResourceId: update.update.id,
193
- sessionId: input.sessionId,
194
- initiator: { kind: "service", subjectId: "goal-continuation" },
195
- initiatorContext: { goalId: decision.goal.id },
196
- origin: "goal",
197
- idempotencyKey: `agent_run.created:goal:${input.workspaceId}:${update.update.id}`,
198
- });
199
- return { action: "continue" };
133
+ return { action: decision.action };
200
134
  }
201
135
 
202
136
  return {
@@ -342,7 +342,7 @@ export class RigVerificationLeaseUnavailableError extends Error {
342
342
 
343
343
  constructor(
344
344
  readonly sandboxGroupId: string,
345
- readonly role: "attached" | "rearmed" | "fenced",
345
+ readonly role: "attached" | "blocked" | "rearmed" | "fenced",
346
346
  ) {
347
347
  super(`Rig verification requires a new clean sandbox, but lease ${sandboxGroupId} was ${role}`);
348
348
  }
@@ -1,4 +1,5 @@
1
1
  import type { FileAsset, ResourceRef, SessionSystemUpdate } from "@opengeni/contracts";
2
+ import { createHash } from "node:crypto";
2
3
  import {
3
4
  getActiveSessionHistoryItems,
4
5
  getLatestRunState,
@@ -146,8 +147,155 @@ export type TurnInputOptions = {
146
147
  recovering?: boolean;
147
148
  unavailableSandboxFilesNote?: string;
148
149
  runCredentialsNote?: string;
150
+ readFileBytesForModel?: (file: FileAsset) => Promise<Uint8Array>;
149
151
  };
150
152
 
153
+ export const MAX_INLINE_MODEL_ATTACHMENT_BYTES = 20 * 1024 * 1024;
154
+
155
+ export type ModelAttachmentContent = {
156
+ kind: "image" | "file";
157
+ fileId: string;
158
+ filename: string;
159
+ contentType: string;
160
+ dataUrl: string;
161
+ };
162
+
163
+ const MODEL_IMAGE_CONTENT_TYPES = new Set(["image/gif", "image/jpeg", "image/png", "image/webp"]);
164
+
165
+ const MODEL_FILE_CONTENT_TYPES = new Set([
166
+ "application/json",
167
+ "application/pdf",
168
+ "application/x-yaml",
169
+ "application/yaml",
170
+ ]);
171
+
172
+ // Generic XML has equivalent application/* and text/* registrations. Keep both
173
+ // aliases on the sandbox-path fallback until a provider parser boundary is
174
+ // explicitly supported and verified; MIME spelling must not bypass the fence.
175
+ const BLOCKED_TEXT_CONTENT_TYPES = new Set([
176
+ "text/css",
177
+ "text/html",
178
+ "text/javascript",
179
+ "text/xml",
180
+ ]);
181
+
182
+ function safeErrorType(error: unknown): string {
183
+ if (!(error instanceof Error)) return typeof error;
184
+ const name = error.name.trim();
185
+ return /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(name) ? name : "Error";
186
+ }
187
+
188
+ function modelAttachmentDescriptor(
189
+ contentType: string,
190
+ ): Pick<ModelAttachmentContent, "kind" | "contentType"> | null {
191
+ const normalized = contentType.toLowerCase().split(";", 1)[0]?.trim() ?? "";
192
+ if (MODEL_IMAGE_CONTENT_TYPES.has(normalized)) {
193
+ return { kind: "image", contentType: normalized };
194
+ }
195
+ if (
196
+ MODEL_FILE_CONTENT_TYPES.has(normalized) ||
197
+ (normalized.startsWith("text/") && !BLOCKED_TEXT_CONTENT_TYPES.has(normalized))
198
+ ) {
199
+ return { kind: "file", contentType: normalized };
200
+ }
201
+ return null;
202
+ }
203
+
204
+ export async function modelAttachmentContentForFiles(
205
+ files: FileAsset[],
206
+ readFileBytes: (file: FileAsset) => Promise<Uint8Array>,
207
+ ): Promise<ModelAttachmentContent[]> {
208
+ const attachments: ModelAttachmentContent[] = [];
209
+ let remainingBytes = MAX_INLINE_MODEL_ATTACHMENT_BYTES;
210
+ for (const file of files) {
211
+ const descriptor = modelAttachmentDescriptor(file.contentType);
212
+ const checksum = file.sha256?.trim().toLowerCase() ?? "";
213
+ if (
214
+ file.status !== "ready" ||
215
+ !descriptor ||
216
+ file.sizeBytes > remainingBytes ||
217
+ !/^[a-f0-9]{64}$/.test(checksum)
218
+ ) {
219
+ continue;
220
+ }
221
+ try {
222
+ const bytes = await readFileBytes(file);
223
+ if (bytes.byteLength !== file.sizeBytes || bytes.byteLength > remainingBytes) {
224
+ console.error("model attachment bytes did not match finalized metadata", {
225
+ fileId: file.id,
226
+ expectedSizeBytes: file.sizeBytes,
227
+ actualSizeBytes: bytes.byteLength,
228
+ });
229
+ continue;
230
+ }
231
+ if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
232
+ console.error("model attachment checksum did not match finalized metadata", {
233
+ fileId: file.id,
234
+ });
235
+ continue;
236
+ }
237
+ attachments.push({
238
+ kind: descriptor.kind,
239
+ fileId: file.id,
240
+ filename: file.safeFilename,
241
+ contentType: descriptor.contentType,
242
+ dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`,
243
+ });
244
+ remainingBytes -= bytes.byteLength;
245
+ } catch (error) {
246
+ // The sandbox-path projection remains available for every file. A direct
247
+ // provider-content read is an additive fast path and must not turn a
248
+ // transient storage read into loss of the accepted prompt.
249
+ console.error("model attachment content read failed; retaining sandbox path fallback", {
250
+ fileId: file.id,
251
+ errorType: safeErrorType(error),
252
+ });
253
+ }
254
+ }
255
+ return attachments;
256
+ }
257
+
258
+ /**
259
+ * Enrich the current turn's durable user boundary for this model attempt only.
260
+ * The item count stays unchanged, so the turn reconciler still treats the
261
+ * enriched row as the already-persisted prefix and never writes inline bytes to
262
+ * session_history_items. Recovery rebuilds the same projection from the trigger.
263
+ */
264
+ export function withCurrentUserAttachmentContent(
265
+ historyItems: Array<Record<string, unknown>>,
266
+ attachments: ModelAttachmentContent[],
267
+ ): Array<Record<string, unknown>> {
268
+ if (attachments.length === 0) return historyItems;
269
+ let currentUserIndex = -1;
270
+ for (let index = historyItems.length - 1; index >= 0; index -= 1) {
271
+ const item = historyItems[index];
272
+ if (item?.type === "message" && item.role === "user") {
273
+ currentUserIndex = index;
274
+ break;
275
+ }
276
+ }
277
+ if (currentUserIndex < 0) return historyItems;
278
+ const currentUser = historyItems[currentUserIndex]!;
279
+ const existingContent = Array.isArray(currentUser.content)
280
+ ? [...currentUser.content]
281
+ : [{ type: "input_text", text: String(currentUser.content ?? "") }];
282
+ const attachmentContent = attachments.map((attachment) =>
283
+ attachment.kind === "image"
284
+ ? { type: "input_image", image: attachment.dataUrl }
285
+ : {
286
+ type: "input_file",
287
+ file: attachment.dataUrl,
288
+ filename: attachment.filename,
289
+ },
290
+ );
291
+ const projected = [...historyItems];
292
+ projected[currentUserIndex] = {
293
+ ...currentUser,
294
+ content: [...existingContent, ...attachmentContent],
295
+ };
296
+ return projected;
297
+ }
298
+
151
299
  export async function turnInput(
152
300
  db: Database,
153
301
  runtime: OpenGeniRuntime,
@@ -182,11 +330,19 @@ export async function turnInput(
182
330
  if (typeof payload.text !== "string" || payload.text.trim().length === 0) {
183
331
  throw new Error("user.message payload is missing text");
184
332
  }
185
- const attachmentContext = await userMessageAttachmentsContext(
333
+ const resources = Array.isArray(payload.resources) ? (payload.resources as ResourceRef[]) : [];
334
+ const fileAttachments = await resolveUserMessageFileAttachments(
186
335
  db,
187
336
  trigger.workspaceId,
188
- Array.isArray(payload.resources) ? (payload.resources as ResourceRef[]) : [],
337
+ resources,
189
338
  );
339
+ const attachmentContext = userMessageAttachmentsContext(fileAttachments);
340
+ const modelAttachments = options.readFileBytesForModel
341
+ ? await modelAttachmentContentForFiles(
342
+ fileAttachments.map((attachment) => attachment.file),
343
+ options.readFileBytesForModel,
344
+ )
345
+ : [];
190
346
  return await messageInput(
191
347
  db,
192
348
  runtime,
@@ -195,6 +351,7 @@ export async function turnInput(
195
351
  undefined,
196
352
  joinInternalContext(internalContext, attachmentContext),
197
353
  current,
354
+ modelAttachments,
198
355
  );
199
356
  }
200
357
  if (trigger.type === "system.update.delivered") {
@@ -287,10 +444,14 @@ async function messageInput(
287
444
  text: string | undefined,
288
445
  internalContext: string | undefined,
289
446
  current: TurnCodexAccount = NON_CODEX_TURN,
447
+ modelAttachments: ModelAttachmentContent[] = [],
290
448
  ): Promise<PreparedTurnInput> {
291
449
  const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
292
450
  const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
293
- const historyItems = applyCodexHistoryStrip(stored, current);
451
+ const historyItems = withCurrentUserAttachmentContent(
452
+ applyCodexHistoryStrip(stored, current),
453
+ modelAttachments,
454
+ );
294
455
  return {
295
456
  input: await runtime.prepareInput(agent, {
296
457
  kind: "message",
@@ -309,25 +470,37 @@ export async function userMessageTextWithAttachments(
309
470
  text: string,
310
471
  resources: ResourceRef[],
311
472
  ): Promise<string> {
312
- const attachmentContext = await userMessageAttachmentsContext(db, workspaceId, resources);
473
+ const fileAttachments = await resolveUserMessageFileAttachments(db, workspaceId, resources);
474
+ const attachmentContext = userMessageAttachmentsContext(fileAttachments);
313
475
  return attachmentContext ? [text, "", attachmentContext].join("\n") : text;
314
476
  }
315
477
 
316
- async function userMessageAttachmentsContext(
478
+ type UserMessageFileAttachment = {
479
+ resource: Extract<ResourceRef, { kind: "file" }>;
480
+ file: FileAsset;
481
+ };
482
+
483
+ async function resolveUserMessageFileAttachments(
317
484
  db: Database,
318
485
  workspaceId: string,
319
486
  resources: ResourceRef[],
320
- ): Promise<string | undefined> {
321
- const attachedFiles: string[] = [];
487
+ ): Promise<UserMessageFileAttachment[]> {
488
+ const attachments: UserMessageFileAttachment[] = [];
322
489
  for (const resource of resources) {
323
- if (resource.kind !== "file") {
324
- continue;
325
- }
490
+ if (resource.kind !== "file") continue;
326
491
  const file = await requireFile(db, workspaceId, resource.fileId);
327
- attachedFiles.push(
328
- `- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`,
329
- );
492
+ attachments.push({ resource, file });
330
493
  }
494
+ return attachments;
495
+ }
496
+
497
+ function userMessageAttachmentsContext(
498
+ attachments: UserMessageFileAttachment[],
499
+ ): string | undefined {
500
+ const attachedFiles = attachments.map(
501
+ ({ resource, file }) =>
502
+ `- ${file.filename} (${file.contentType}, ${file.sizeBytes} bytes): ${sandboxFilePath(resource, file)}`,
503
+ );
331
504
  if (attachedFiles.length === 0) {
332
505
  return undefined;
333
506
  }