@opengeni/worker-bundle 0.16.0 → 0.16.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.js CHANGED
@@ -131,6 +131,7 @@ import {
131
131
  normalizeModelCallUsage,
132
132
  normalizeSdkEvent,
133
133
  sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2,
134
+ projectModelInputForCapabilities,
134
135
  appendPersistentSessionSettings,
135
136
  appendSessionInstructions,
136
137
  appendWorkspaceGovernance,
@@ -1079,9 +1080,15 @@ import {
1079
1080
  import { contextInputBudgetTokens } from "@opengeni/config";
1080
1081
 
1081
1082
  // src/activities/run-input.ts
1083
+ import {
1084
+ MODEL_ATTACHMENT_REFS_FIELD,
1085
+ FileResourceRef,
1086
+ resourceMountPath
1087
+ } from "@opengeni/contracts";
1082
1088
  import { createHash } from "crypto";
1083
1089
  import {
1084
1090
  getActiveSessionHistoryItems,
1091
+ getFiles,
1085
1092
  getLatestRunState,
1086
1093
  getHumanInputResumeForEvent,
1087
1094
  getSandboxSessionEnvelope,
@@ -1154,7 +1161,7 @@ function modelAttachmentDescriptor(contentType) {
1154
1161
  return null;
1155
1162
  }
1156
1163
  async function modelAttachmentContentForFiles(files, readFileBytes) {
1157
- const attachments = [];
1164
+ const selected = [];
1158
1165
  let remainingBytes = MAX_INLINE_MODEL_ATTACHMENT_BYTES;
1159
1166
  for (const file of files) {
1160
1167
  const descriptor = modelAttachmentDescriptor(file.contentType);
@@ -1162,65 +1169,157 @@ async function modelAttachmentContentForFiles(files, readFileBytes) {
1162
1169
  if (file.status !== "ready" || !descriptor || file.sizeBytes > remainingBytes || !/^[a-f0-9]{64}$/.test(checksum)) {
1163
1170
  continue;
1164
1171
  }
1165
- try {
1166
- const bytes = await readFileBytes(file);
1167
- if (bytes.byteLength !== file.sizeBytes || bytes.byteLength > remainingBytes) {
1168
- console.error("model attachment bytes did not match finalized metadata", {
1172
+ selected.push({ file, descriptor, checksum });
1173
+ remainingBytes -= file.sizeBytes;
1174
+ }
1175
+ const attachments = new Array(selected.length);
1176
+ let cursor = 0;
1177
+ const worker = async () => {
1178
+ while (cursor < selected.length) {
1179
+ const index = cursor++;
1180
+ const { file, descriptor, checksum } = selected[index];
1181
+ try {
1182
+ const bytes = await readFileBytes(file);
1183
+ if (bytes.byteLength !== file.sizeBytes) {
1184
+ console.error("model attachment bytes did not match finalized metadata", {
1185
+ fileId: file.id,
1186
+ expectedSizeBytes: file.sizeBytes,
1187
+ actualSizeBytes: bytes.byteLength
1188
+ });
1189
+ continue;
1190
+ }
1191
+ if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
1192
+ console.error("model attachment checksum did not match finalized metadata", {
1193
+ fileId: file.id
1194
+ });
1195
+ continue;
1196
+ }
1197
+ attachments[index] = {
1198
+ kind: descriptor.kind,
1169
1199
  fileId: file.id,
1170
- expectedSizeBytes: file.sizeBytes,
1171
- actualSizeBytes: bytes.byteLength
1200
+ filename: file.safeFilename,
1201
+ contentType: descriptor.contentType,
1202
+ dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`
1203
+ };
1204
+ } catch (error) {
1205
+ console.error("model attachment content read failed; retaining sandbox path fallback", {
1206
+ fileId: file.id,
1207
+ errorType: safeErrorType(error)
1172
1208
  });
1173
- continue;
1174
1209
  }
1175
- if (createHash("sha256").update(bytes).digest("hex") !== checksum) {
1176
- console.error("model attachment checksum did not match finalized metadata", {
1177
- fileId: file.id
1178
- });
1179
- continue;
1210
+ }
1211
+ };
1212
+ await Promise.all(
1213
+ Array.from({ length: Math.min(8, selected.length) }, async () => await worker())
1214
+ );
1215
+ return attachments.filter(
1216
+ (attachment) => attachment !== void 0
1217
+ );
1218
+ }
1219
+ function modelAcceptsFileMediaType(policy, contentType) {
1220
+ const normalized = contentType.toLowerCase().split(";", 1)[0]?.trim() ?? "";
1221
+ return policy.inputFileMediaTypes.some(
1222
+ (accepted) => accepted === normalized || accepted.endsWith("/*") && normalized.startsWith(accepted.slice(0, -1))
1223
+ );
1224
+ }
1225
+ function attachmentRefsFromItem(item) {
1226
+ const raw = item[MODEL_ATTACHMENT_REFS_FIELD];
1227
+ if (!Array.isArray(raw)) return [];
1228
+ const refs = [];
1229
+ for (const candidate of raw) {
1230
+ const parsed = FileResourceRef.safeParse(candidate);
1231
+ if (parsed.success) refs.push(parsed.data);
1232
+ }
1233
+ return refs;
1234
+ }
1235
+ function attachmentUnavailableText(ref, file) {
1236
+ const filename = file?.safeFilename ?? ref.fileId;
1237
+ const mediaType = file?.contentType ?? "unknown type";
1238
+ const path = file ? sandboxFilePath(ref, file) : `/workspace/${resourceMountPath(ref)}`;
1239
+ return `[Attachment not included directly because the selected model does not accept this input or it exceeded the safe inline limit: ${filename} (${mediaType}). It remains available to tools in the sandbox at ${path}.]`;
1240
+ }
1241
+ function createModelHistoryAttachmentProjector(db, workspaceId, policy, readFileBytes) {
1242
+ const fileById = /* @__PURE__ */ new Map();
1243
+ const missingFileIds = /* @__PURE__ */ new Set();
1244
+ const contentById = /* @__PURE__ */ new Map();
1245
+ const attemptedContentIds = /* @__PURE__ */ new Set();
1246
+ return async (items) => {
1247
+ const refsByIndex = /* @__PURE__ */ new Map();
1248
+ const orderedFileIds = [];
1249
+ const seenFileIds = /* @__PURE__ */ new Set();
1250
+ for (let index = 0; index < items.length; index += 1) {
1251
+ const refs = attachmentRefsFromItem(items[index]);
1252
+ if (refs.length === 0) continue;
1253
+ refsByIndex.set(index, refs);
1254
+ for (const ref of refs) {
1255
+ if (seenFileIds.has(ref.fileId)) continue;
1256
+ seenFileIds.add(ref.fileId);
1257
+ orderedFileIds.push(ref.fileId);
1180
1258
  }
1181
- attachments.push({
1182
- kind: descriptor.kind,
1183
- fileId: file.id,
1184
- filename: file.safeFilename,
1185
- contentType: descriptor.contentType,
1186
- dataUrl: `data:${descriptor.contentType};base64,${Buffer.from(bytes).toString("base64")}`
1259
+ }
1260
+ if (refsByIndex.size === 0) return items;
1261
+ const unknownIds = orderedFileIds.filter((id) => !fileById.has(id) && !missingFileIds.has(id));
1262
+ if (unknownIds.length > 0) {
1263
+ const files = await getFiles(db, workspaceId, unknownIds);
1264
+ for (const file of files) fileById.set(file.id, file);
1265
+ for (const id of unknownIds) {
1266
+ if (!fileById.has(id)) missingFileIds.add(id);
1267
+ }
1268
+ }
1269
+ if (readFileBytes) {
1270
+ const readable = [...orderedFileIds].reverse().map((id) => fileById.get(id)).filter((file) => {
1271
+ if (!file || attemptedContentIds.has(file.id)) return false;
1272
+ const descriptor = modelAttachmentDescriptor(file.contentType);
1273
+ return Boolean(
1274
+ descriptor && (descriptor.kind === "image" && policy.supportsImageInput || descriptor.kind === "file" && modelAcceptsFileMediaType(policy, file.contentType))
1275
+ );
1187
1276
  });
1188
- remainingBytes -= bytes.byteLength;
1189
- } catch (error) {
1190
- console.error("model attachment content read failed; retaining sandbox path fallback", {
1191
- fileId: file.id,
1192
- errorType: safeErrorType(error)
1277
+ for (const file of readable) attemptedContentIds.add(file.id);
1278
+ const content = await modelAttachmentContentForFiles(readable, readFileBytes);
1279
+ for (const attachment of content) contentById.set(attachment.fileId, attachment);
1280
+ }
1281
+ const projected = [...items];
1282
+ for (const [index, refs] of refsByIndex) {
1283
+ const original = items[index];
1284
+ const existingContent = Array.isArray(original.content) ? [...original.content] : [{ type: "input_text", text: String(original.content ?? "") }];
1285
+ const attachmentParts = refs.map((ref) => {
1286
+ const attachment = contentById.get(ref.fileId);
1287
+ if (!attachment) {
1288
+ return {
1289
+ type: "input_text",
1290
+ text: attachmentUnavailableText(ref, fileById.get(ref.fileId))
1291
+ };
1292
+ }
1293
+ return attachment.kind === "image" ? { type: "input_image", image: attachment.dataUrl } : {
1294
+ type: "input_file",
1295
+ file: attachment.dataUrl,
1296
+ filename: attachment.filename
1297
+ };
1193
1298
  });
1299
+ const clone = {
1300
+ ...original,
1301
+ content: [...existingContent, ...attachmentParts]
1302
+ };
1303
+ delete clone[MODEL_ATTACHMENT_REFS_FIELD];
1304
+ projected[index] = clone;
1194
1305
  }
1195
- }
1196
- return attachments;
1306
+ return projected;
1307
+ };
1197
1308
  }
1198
- function withCurrentUserAttachmentContent(historyItems, attachments) {
1199
- if (attachments.length === 0) return historyItems;
1200
- let currentUserIndex = -1;
1309
+ function withCurrentUserAttachmentRefs(historyItems, refs) {
1310
+ if (refs.length === 0) return historyItems;
1201
1311
  for (let index = historyItems.length - 1; index >= 0; index -= 1) {
1202
1312
  const item = historyItems[index];
1203
- if (item?.type === "message" && item.role === "user") {
1204
- currentUserIndex = index;
1205
- break;
1206
- }
1313
+ if (item.type !== "message" || item.role !== "user") continue;
1314
+ const existing = attachmentRefsFromItem(item);
1315
+ const existingIds = new Set(existing.map((ref) => ref.fileId));
1316
+ const additions = refs.filter((ref) => !existingIds.has(ref.fileId));
1317
+ if (additions.length === 0) return historyItems;
1318
+ const projected = [...historyItems];
1319
+ projected[index] = { ...item, [MODEL_ATTACHMENT_REFS_FIELD]: [...existing, ...additions] };
1320
+ return projected;
1207
1321
  }
1208
- if (currentUserIndex < 0) return historyItems;
1209
- const currentUser = historyItems[currentUserIndex];
1210
- const existingContent = Array.isArray(currentUser.content) ? [...currentUser.content] : [{ type: "input_text", text: String(currentUser.content ?? "") }];
1211
- const attachmentContent = attachments.map(
1212
- (attachment) => attachment.kind === "image" ? { type: "input_image", image: attachment.dataUrl } : {
1213
- type: "input_file",
1214
- file: attachment.dataUrl,
1215
- filename: attachment.filename
1216
- }
1217
- );
1218
- const projected = [...historyItems];
1219
- projected[currentUserIndex] = {
1220
- ...currentUser,
1221
- content: [...existingContent, ...attachmentContent]
1222
- };
1223
- return projected;
1322
+ return historyItems;
1224
1323
  }
1225
1324
  async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN, options) {
1226
1325
  if (!trigger) {
@@ -1260,10 +1359,6 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
1260
1359
  resources
1261
1360
  );
1262
1361
  const attachmentContext = userMessageAttachmentsContext(fileAttachments);
1263
- const modelAttachments = options.readFileBytesForModel ? await modelAttachmentContentForFiles(
1264
- fileAttachments.map((attachment) => attachment.file),
1265
- options.readFileBytesForModel
1266
- ) : [];
1267
1362
  return await messageInput(
1268
1363
  db,
1269
1364
  runtime,
@@ -1272,7 +1367,8 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
1272
1367
  void 0,
1273
1368
  joinInternalContext(internalContext, attachmentContext),
1274
1369
  current,
1275
- modelAttachments
1370
+ fileAttachments.map((attachment) => attachment.resource),
1371
+ options.projectModelHistory
1276
1372
  );
1277
1373
  }
1278
1374
  if (trigger.type === "system.update.delivered") {
@@ -1331,20 +1427,22 @@ function joinInternalContext(...parts) {
1331
1427
  const content = parts.map((part) => part?.trim()).filter((part) => Boolean(part));
1332
1428
  return content.length > 0 ? content.join("\n\n") : void 0;
1333
1429
  }
1334
- async function messageInput(db, runtime, agent, trigger, text, internalContext, current = NON_CODEX_TURN, modelAttachments = []) {
1430
+ async function messageInput(db, runtime, agent, trigger, text, internalContext, current = NON_CODEX_TURN, currentAttachmentRefs = [], projectModelHistory) {
1335
1431
  const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
1336
1432
  const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
1337
- const historyItems = withCurrentUserAttachmentContent(
1433
+ const referencedHistory = withCurrentUserAttachmentRefs(
1338
1434
  applyCodexHistoryStrip(stored, current),
1339
- modelAttachments
1435
+ currentAttachmentRefs
1340
1436
  );
1437
+ const historyItems = projectModelHistory ? await projectModelHistory(referencedHistory) : referencedHistory;
1341
1438
  return {
1342
1439
  input: await runtime.prepareInput(agent, {
1343
1440
  kind: "message",
1344
1441
  ...text ? { text } : {},
1345
1442
  ...internalContext ? { internalContext } : {},
1346
1443
  historyItems,
1347
- sandboxEnvelope: envelope
1444
+ sandboxEnvelope: envelope,
1445
+ ...projectModelHistory ? { modelInputAlreadyProjected: true } : {}
1348
1446
  }),
1349
1447
  modelHistoryFromItems: true
1350
1448
  };
@@ -1368,7 +1466,7 @@ function userMessageAttachmentsContext(attachments) {
1368
1466
  return ["Attached files are available in the sandbox:", ...attachedFiles].join("\n");
1369
1467
  }
1370
1468
  function sandboxFilePath(resource, file) {
1371
- return `/workspace/${resource.mountPath ?? `files/${file.id}`}/${file.safeFilename}`;
1469
+ return `/workspace/${resourceMountPath(resource)}/${file.safeFilename}`;
1372
1470
  }
1373
1471
 
1374
1472
  // src/activities/context-compaction.ts
@@ -1411,9 +1509,15 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
1411
1509
  requestConsumed
1412
1510
  };
1413
1511
  }
1414
- const items = sanitizeHistoryItemsForModel(
1415
- applyCodexHistoryStrip(active, options.codexAccount ?? { currentCodexCredentialId: null })
1512
+ const canonicalItems = applyCodexHistoryStrip(
1513
+ active,
1514
+ options.codexAccount ?? { currentCodexCredentialId: null }
1416
1515
  );
1516
+ const projectForWire = async (input) => sanitizeHistoryItemsForModel(
1517
+ options.projectModelInput ? await options.projectModelInput(input) : input,
1518
+ settings.modelToolOutputTruncationTokens
1519
+ );
1520
+ const items = await projectForWire(canonicalItems);
1417
1521
  const decision = decideCompaction({
1418
1522
  items,
1419
1523
  lastInputTokens,
@@ -1448,17 +1552,28 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
1448
1552
  }
1449
1553
  await options.publishLiveEvents?.(started.events);
1450
1554
  if (useRemoteV2) {
1451
- const outcome2 = await compactContextRemoteV2(db, settings, scope, items, decision, options);
1555
+ const outcome2 = await compactContextRemoteV2(
1556
+ db,
1557
+ settings,
1558
+ scope,
1559
+ canonicalItems,
1560
+ items,
1561
+ decision,
1562
+ options,
1563
+ projectForWire
1564
+ );
1452
1565
  return prependCompactionEvents(started.events, outcome2);
1453
1566
  }
1454
1567
  const outcome = await compactContextPortable(
1455
1568
  db,
1456
1569
  settings,
1457
1570
  scope,
1571
+ canonicalItems,
1458
1572
  items,
1459
1573
  decision,
1460
1574
  summarize,
1461
- options
1575
+ options,
1576
+ projectForWire
1462
1577
  );
1463
1578
  return prependCompactionEvents(started.events, outcome);
1464
1579
  }
@@ -1495,7 +1610,7 @@ async function settleSkippedAfterStart(db, scope, options, reason) {
1495
1610
  requestConsumed: clearRequestedCompaction
1496
1611
  };
1497
1612
  }
1498
- async function compactContextRemoteV2(db, settings, scope, items, decision, options) {
1613
+ async function compactContextRemoteV2(db, settings, scope, canonicalItems, items, decision, options, projectForWire) {
1499
1614
  if (!options.requestRemoteCompactionV2) {
1500
1615
  throw new EmptyCompactionSummaryError({
1501
1616
  stage: "remote_v2_requester",
@@ -1504,8 +1619,8 @@ async function compactContextRemoteV2(db, settings, scope, items, decision, opti
1504
1619
  }
1505
1620
  const estimatedTokensBefore = estimateTokens(items);
1506
1621
  const compactionItem = await options.requestRemoteCompactionV2(settings, items);
1507
- const replacementHistory = buildRemoteV2ReplacementHistory(items, compactionItem);
1508
- const estimatedTokensAfter = estimateTokens(replacementHistory);
1622
+ const replacementHistory = buildRemoteV2ReplacementHistory(canonicalItems, compactionItem);
1623
+ const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
1509
1624
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
1510
1625
  const tailItem = replacementHistory.at(-1);
1511
1626
  if (!tailItem) {
@@ -1550,14 +1665,14 @@ async function compactContextRemoteV2(db, settings, scope, items, decision, opti
1550
1665
  events: applied.events
1551
1666
  };
1552
1667
  }
1553
- async function compactContextPortable(db, settings, scope, items, decision, summarize, options) {
1668
+ async function compactContextPortable(db, settings, scope, canonicalItems, items, decision, summarize, options, projectForWire) {
1554
1669
  const estimatedTokensBefore = estimateTokens(items);
1555
1670
  const summarized = await summarizeWithCodexOverflowTrimming(summarize, settings, items);
1556
1671
  const summaryBody = summarized.summaryBody;
1557
- const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
1558
- const estimatedTokensAfter = estimateTokens(replacementHistory);
1672
+ const replacementHistory = buildCompactionReplacementHistory(canonicalItems, summaryBody);
1673
+ const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
1559
1674
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
1560
- const previousReplacementFingerprint = latestCompactionReplacementFingerprint(items);
1675
+ const previousReplacementFingerprint = latestCompactionReplacementFingerprint(canonicalItems);
1561
1676
  const summaryItem = replacementHistory.at(-1);
1562
1677
  if (!summaryItem) {
1563
1678
  return await settleSkippedAfterStart(db, scope, options, "summarization_failed");
@@ -6323,7 +6438,8 @@ import {
6323
6438
  CAPABILITY_DESCRIPTORS,
6324
6439
  DEFAULT_FIRST_PARTY_MCP_TOOLS,
6325
6440
  evaluateWorkspaceModelPolicy as evaluateWorkspaceModelPolicy2,
6326
- readTurnExecutionPolicyV1
6441
+ readTurnExecutionPolicyV1,
6442
+ resourceMountPath as resourceMountPath2
6327
6443
  } from "@opengeni/contracts";
6328
6444
  import { createHash as createHash4, randomUUID as randomUUID2 } from "crypto";
6329
6445
  var PROVIDER_BACKPRESSURE_DELAY_MS = 6e4;
@@ -7087,6 +7203,9 @@ function computerToolModeForTurn(resolvedModel) {
7087
7203
  if (!resolvedModel) {
7088
7204
  return "hosted";
7089
7205
  }
7206
+ if (!modelSupportsImageInputForTurn(resolvedModel)) {
7207
+ return "disabled";
7208
+ }
7090
7209
  if (resolvedModel.provider.kind === "codex-subscription") {
7091
7210
  return "function-image";
7092
7211
  }
@@ -7107,8 +7226,22 @@ function structuredToolTransportForTurn(resolvedModel) {
7107
7226
  function hostedWebSearchForTurn(resolvedModel, deploymentWebSearchEnabled) {
7108
7227
  return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
7109
7228
  }
7110
- function modelAcceptsTypedAttachmentContentForTurn(resolvedModel) {
7111
- return resolvedModel === null || resolvedModel.provider.api === "responses";
7229
+ function modelSupportsImageInputForTurn(resolvedModel) {
7230
+ return resolvedModel === null || resolvedModel.configured.capabilities?.inputModalities.includes("image") !== false;
7231
+ }
7232
+ var LEGACY_INPUT_FILE_MEDIA_TYPES = [
7233
+ "application/json",
7234
+ "application/pdf",
7235
+ "application/x-yaml",
7236
+ "application/yaml",
7237
+ "text/*"
7238
+ ];
7239
+ function modelAttachmentInputPolicyForTurn(resolvedModel) {
7240
+ const typedTransport = resolvedModel === null || resolvedModel.provider.api === "responses";
7241
+ return {
7242
+ supportsImageInput: typedTransport && modelSupportsImageInputForTurn(resolvedModel),
7243
+ inputFileMediaTypes: typedTransport ? resolvedModel?.configured.capabilities?.inputFileMediaTypes ?? LEGACY_INPUT_FILE_MEDIA_TYPES : []
7244
+ };
7112
7245
  }
7113
7246
  var TurnOperationCancelledError = class extends Error {
7114
7247
  constructor(reason) {
@@ -8727,6 +8860,15 @@ function createRunAgentTurnActivity(services) {
8727
8860
  capabilitySettings,
8728
8861
  turnExecutionPolicy.productModelId
8729
8862
  );
8863
+ const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
8864
+ const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
8865
+ const attachmentProjector = createModelHistoryAttachmentProjector(
8866
+ db,
8867
+ input.workspaceId,
8868
+ modelInputPolicy,
8869
+ objectStorage ? (file) => objectStorage.getFileBytes(file) : void 0
8870
+ );
8871
+ const modelHistoryProjector = async (items) => projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
8730
8872
  const selectedProvider = resolvedModel?.provider;
8731
8873
  const publicProviderHeaders = new Set(
8732
8874
  selectedProvider?.publicDefaultHeaderNames?.map((name) => name.toLowerCase()) ?? []
@@ -9002,6 +9144,7 @@ function createRunAgentTurnActivity(services) {
9002
9144
  codexAccount: {
9003
9145
  currentCodexCredentialId: effectiveCodexCredentialId
9004
9146
  },
9147
+ projectModelInput: modelHistoryProjector,
9005
9148
  ...compactionModeOptions
9006
9149
  }
9007
9150
  ),
@@ -9731,7 +9874,8 @@ function createRunAgentTurnActivity(services) {
9731
9874
  db,
9732
9875
  objectStorage,
9733
9876
  input.workspaceId,
9734
- turnResources
9877
+ turnResources,
9878
+ activeSandboxBackend ?? groupBoxBackend
9735
9879
  ),
9736
9880
  cancellationSignal,
9737
9881
  void 0
@@ -9901,6 +10045,8 @@ function createRunAgentTurnActivity(services) {
9901
10045
  // session's MCP allow-list; the effective context window drives the
9902
10046
  // compaction threshold.
9903
10047
  hostedWebSearch,
10048
+ supportsImageInput,
10049
+ inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
9904
10050
  ...resolvedModel ? {
9905
10051
  encryptedReasoning: resolvedModel.provider.api === "responses" && runSettings.openaiReasoningEncryptedContent,
9906
10052
  contextWindowTokens: resolvedModel.configured.contextWindowTokens ?? runSettings.contextWindowTokens,
@@ -10197,6 +10343,7 @@ function createRunAgentTurnActivity(services) {
10197
10343
  codexAccount: {
10198
10344
  currentCodexCredentialId: effectiveCodexCredentialId
10199
10345
  },
10346
+ projectModelInput: modelHistoryProjector,
10200
10347
  ...compactionModeOptions
10201
10348
  }
10202
10349
  ),
@@ -10310,12 +10457,14 @@ function createRunAgentTurnActivity(services) {
10310
10457
  codexAccount: {
10311
10458
  currentCodexCredentialId: effectiveCodexCredentialId
10312
10459
  },
10460
+ projectModelInput: modelHistoryProjector,
10313
10461
  ...compactionModeOptions
10314
10462
  } : {
10315
10463
  trigger: "auto",
10316
10464
  codexAccount: {
10317
10465
  currentCodexCredentialId: effectiveCodexCredentialId
10318
10466
  },
10467
+ projectModelInput: modelHistoryProjector,
10319
10468
  ...compactionModeOptions
10320
10469
  }
10321
10470
  ),
@@ -10383,15 +10532,16 @@ function createRunAgentTurnActivity(services) {
10383
10532
  }
10384
10533
  let fileMaterializationFailures = [];
10385
10534
  let fileDownloadsMaterializedForRun = false;
10386
- if (resolvedSandbox && setupBoxSession && activeSandboxBackend !== "selfhosted" && fileResourceDownloads.length > 0) {
10535
+ if (resolvedSandbox && setupBoxSession && fileResourceDownloads.length > 0) {
10387
10536
  const boxInstanceId = resolvedSandbox.established.instanceId;
10388
- const alreadyMaterialized = await getMaterializedSandboxFileResources(db, {
10537
+ const cacheMaterialization = resolvedSandbox.established.backendId !== "selfhosted";
10538
+ const alreadyMaterialized = cacheMaterialization ? await getMaterializedSandboxFileResources(db, {
10389
10539
  accountId: input.accountId,
10390
10540
  workspaceId: input.workspaceId,
10391
10541
  sandboxGroupId: session.sandboxGroupId,
10392
10542
  expectedEpoch: resolvedSandbox.leaseEpoch,
10393
10543
  instanceId: boxInstanceId
10394
- });
10544
+ }) : /* @__PURE__ */ new Set();
10395
10545
  const downloadsToMaterialize = filterUnmaterializedSandboxFileDownloads(
10396
10546
  fileResourceDownloads,
10397
10547
  alreadyMaterialized
@@ -10420,7 +10570,7 @@ function createRunAgentTurnActivity(services) {
10420
10570
  fileMaterializationFailures = materialized.failures;
10421
10571
  const failedFileIds = new Set(materialized.failures.map((failure) => failure.fileId));
10422
10572
  const succeededFileIds = downloadsToMaterialize.map((download) => download.fileId).filter((fileId) => !failedFileIds.has(fileId));
10423
- if (succeededFileIds.length > 0) {
10573
+ if (cacheMaterialization && succeededFileIds.length > 0) {
10424
10574
  await markSandboxFileResourcesMaterialized(db, {
10425
10575
  accountId: input.accountId,
10426
10576
  workspaceId: input.workspaceId,
@@ -10453,9 +10603,7 @@ function createRunAgentTurnActivity(services) {
10453
10603
  recovering: turn.executionGeneration > 1,
10454
10604
  ...unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {},
10455
10605
  ...runCredentialsNote ? { runCredentialsNote } : {},
10456
- ...modelAcceptsTypedAttachmentContentForTurn(resolvedModel) && objectStorage ? {
10457
- readFileBytesForModel: (file) => objectStorage.getFileBytes(file)
10458
- } : {}
10606
+ projectModelHistory: modelHistoryProjector
10459
10607
  }
10460
10608
  );
10461
10609
  runInput = prepared.input;
@@ -10500,6 +10648,7 @@ function createRunAgentTurnActivity(services) {
10500
10648
  codexAccount: {
10501
10649
  currentCodexCredentialId: effectiveCodexCredentialId
10502
10650
  },
10651
+ projectModelInput: modelHistoryProjector,
10503
10652
  ...compactionModeOptions
10504
10653
  }
10505
10654
  ),
@@ -12758,8 +12907,8 @@ function startOfUtcMonth2() {
12758
12907
  const now = /* @__PURE__ */ new Date();
12759
12908
  return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), 1));
12760
12909
  }
12761
- async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspaceId, resources) {
12762
- if (settings.sandboxBackend === "none" || !requiresSignedFileResourceDownloads(settings)) {
12910
+ async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspaceId, resources, activeSandboxBackend = settings.sandboxBackend) {
12911
+ if (!requiresSignedFileResourceDownloads(settings, activeSandboxBackend)) {
12763
12912
  return [];
12764
12913
  }
12765
12914
  const fileResources = resources.filter(
@@ -12780,21 +12929,25 @@ async function sandboxFileDownloadsForRun(settings, db, objectStorage, workspace
12780
12929
  const url = await downloadStorage.createGetUrl({ key: file.objectKey });
12781
12930
  downloads.push({
12782
12931
  fileId: file.id,
12783
- mountPath: resource.mountPath ?? `files/${file.id}`,
12932
+ mountPath: resourceMountPath2(resource),
12784
12933
  filename: file.safeFilename,
12785
12934
  url: url.url,
12786
12935
  expiresAt: url.expiresAt,
12787
- sizeBytes: file.sizeBytes
12936
+ sizeBytes: file.sizeBytes,
12937
+ ...file.sha256 ? { sha256: file.sha256 } : {}
12788
12938
  });
12789
12939
  }
12790
12940
  return downloads;
12791
12941
  }
12792
- function requiresSignedFileResourceDownloads(settings) {
12793
- if (settings.sandboxBackend === "selfhosted") {
12942
+ function requiresSignedFileResourceDownloads(settings, activeSandboxBackend = settings.sandboxBackend) {
12943
+ if (activeSandboxBackend === "none") {
12944
+ return false;
12945
+ }
12946
+ if (activeSandboxBackend === "selfhosted") {
12794
12947
  return true;
12795
12948
  }
12796
- const nativeBucketMount = CAPABILITY_DESCRIPTORS[settings.sandboxBackend].nativeBucketMount;
12797
- return settings.sandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible" || settings.objectStorageBackend === "aws-s3" || settings.objectStorageBackend === "gcs" || nativeBucketMount && settings.objectStorageBackend === "azure-blob";
12949
+ const nativeBucketMount = CAPABILITY_DESCRIPTORS[activeSandboxBackend].nativeBucketMount;
12950
+ return activeSandboxBackend === "docker" && settings.objectStorageBackend === "s3-compatible" || settings.objectStorageBackend === "aws-s3" || settings.objectStorageBackend === "gcs" || nativeBucketMount && settings.objectStorageBackend === "azure-blob";
12798
12951
  }
12799
12952
  function objectStorageForSandboxDownloads(settings, objectStorage) {
12800
12953
  if (settings.objectStorageBackend !== "s3-compatible" || !settings.objectStorageSandboxEndpoint) {
@@ -12807,11 +12960,13 @@ function objectStorageForSandboxDownloads(settings, objectStorage) {
12807
12960
  }
12808
12961
 
12809
12962
  // src/activities/documents.ts
12810
- import { indexDocumentNow } from "@opengeni/documents";
12963
+ import { getDocument, indexDocumentNow } from "@opengeni/documents";
12811
12964
  import { configuredStaticUsageLimits as configuredStaticUsageLimits3 } from "@opengeni/config";
12812
12965
  import {
12813
12966
  getBillingBalance as getBillingBalance3,
12814
12967
  recordUsageEvent as recordUsageEvent2,
12968
+ resolveDocumentIndexAuthority,
12969
+ rlsContextForWorkspace,
12815
12970
  sumUsageQuantity as sumUsageQuantity3,
12816
12971
  withWorkspaceUsageLock
12817
12972
  } from "@opengeni/db";
@@ -12822,7 +12977,36 @@ function createDocumentActivities(services) {
12822
12977
  if (!objectStorage) {
12823
12978
  throw new Error("object storage is not configured");
12824
12979
  }
12980
+ const context = await rlsContextForWorkspace(db, input.workspaceId);
12981
+ if (context.accountId !== input.accountId) {
12982
+ throw new Error("document account/workspace authority mismatch");
12983
+ }
12825
12984
  return await withWorkspaceUsageLock(db, input.workspaceId, async (lockedDb) => {
12985
+ const storedAuthority = await resolveDocumentIndexAuthority(lockedDb, input);
12986
+ if (!storedAuthority) {
12987
+ throw new Error("document authority was not found before indexing");
12988
+ }
12989
+ const suppliedAuthorityFields = [
12990
+ "authorityKind",
12991
+ "authorityWorkspaceId",
12992
+ "authoritySubjectId"
12993
+ ];
12994
+ const suppliedCount = suppliedAuthorityFields.filter(
12995
+ (field) => Object.prototype.hasOwnProperty.call(input, field)
12996
+ ).length;
12997
+ const suppliedMismatch = Object.prototype.hasOwnProperty.call(input, "authorityKind") && input.authorityKind !== storedAuthority.authorityKind || Object.prototype.hasOwnProperty.call(input, "authorityWorkspaceId") && input.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId || Object.prototype.hasOwnProperty.call(input, "authoritySubjectId") && input.authoritySubjectId !== storedAuthority.authoritySubjectId;
12998
+ if (suppliedMismatch) {
12999
+ throw new Error("document authority changed before indexing");
13000
+ }
13001
+ if (suppliedCount !== 0 && suppliedCount !== suppliedAuthorityFields.length) {
13002
+ throw new Error("document authority tuple is partial");
13003
+ }
13004
+ const claimedDocument = await getDocument(lockedDb, input.workspaceId, input.documentId, {
13005
+ viewerSubjectId: storedAuthority.authoritySubjectId
13006
+ });
13007
+ if (!claimedDocument || claimedDocument.authorityKind !== storedAuthority.authorityKind || claimedDocument.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId || claimedDocument.authoritySubjectId !== storedAuthority.authoritySubjectId) {
13008
+ throw new Error("document authority changed before indexing");
13009
+ }
12826
13010
  const document = await indexDocumentNow(
12827
13011
  lockedDb,
12828
13012
  objectStorage,
@@ -12853,8 +13037,12 @@ function createDocumentActivities(services) {
12853
13037
  throw new Error(`monthly document indexing limit reached (${limit} chunks)`);
12854
13038
  }
12855
13039
  }
12856
- }
13040
+ },
13041
+ { viewerSubjectId: storedAuthority.authoritySubjectId }
12857
13042
  );
13043
+ if (document.authorityKind !== storedAuthority.authorityKind || document.authorityWorkspaceId !== storedAuthority.authorityWorkspaceId || document.authoritySubjectId !== storedAuthority.authoritySubjectId) {
13044
+ throw new Error("document authority changed before indexing");
13045
+ }
12858
13046
  if (document.status === "ready") {
12859
13047
  await recordUsageEvent2(lockedDb, {
12860
13048
  accountId: input.accountId,
@@ -12967,7 +13155,7 @@ import {
12967
13155
  retainedProcessReconciliationProof,
12968
13156
  retainedProcessSettlementIdentity as retainedProcessSettlementIdentity2,
12969
13157
  settleSandboxCheckpointArtifactGc,
12970
- rlsContextForWorkspace,
13158
+ rlsContextForWorkspace as rlsContextForWorkspace2,
12971
13159
  settleRetainedProcess as settleRetainedProcess2
12972
13160
  } from "@opengeni/db";
12973
13161
  import { sandboxArchiveCaptureTimeoutMs as sandboxArchiveCaptureTimeoutMs4, sandboxWarmRateMicrosPerSecond as sandboxWarmRateMicrosPerSecond2 } from "@opengeni/config";
@@ -13818,7 +14006,7 @@ async function forceDrainOverLimitWorkspaces(db, settings, workspaceIds, observa
13818
14006
  let forceDrained = 0;
13819
14007
  for (const workspaceId of workspaceIds) {
13820
14008
  try {
13821
- const { accountId } = await rlsContextForWorkspace(db, workspaceId);
14009
+ const { accountId } = await rlsContextForWorkspace2(db, workspaceId);
13822
14010
  const balance = enforceBalance ? await getBillingBalance4(db, accountId) : { balanceMicros: 1 };
13823
14011
  const result = await forceDrainOverLimitViewerOnlyBoxes(db, {
13824
14012
  workspaceId,
@@ -13946,7 +14134,7 @@ async function probeDrainableProviderReadiness(settings, lease) {
13946
14134
  }
13947
14135
  }
13948
14136
  async function terminateDrainableBox(db, settings, row, observability, terminateBox, probeDrainableProvider) {
13949
- const { accountId } = await rlsContextForWorkspace(db, row.workspaceId);
14137
+ const { accountId } = await rlsContextForWorkspace2(db, row.workspaceId);
13950
14138
  const lease = await readLease4(db, row.workspaceId, row.sandboxGroupId);
13951
14139
  if (!lease) {
13952
14140
  return false;