@opengeni/worker-bundle 0.20.3 → 0.22.0-canary.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.
Files changed (184) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +52 -0
  2. package/dist/activities/agent-turn/agent-build.d.ts +89 -0
  3. package/dist/activities/agent-turn/claim.d.ts +67 -0
  4. package/dist/activities/agent-turn/codex-capacity.d.ts +40 -0
  5. package/dist/activities/agent-turn/codex.d.ts +32 -0
  6. package/dist/activities/agent-turn/compaction-prep.d.ts +88 -0
  7. package/dist/activities/agent-turn/credential-leases.d.ts +56 -0
  8. package/dist/activities/agent-turn/errors.d.ts +253 -0
  9. package/dist/activities/agent-turn/failure-settlement.d.ts +33 -0
  10. package/dist/activities/agent-turn/file-resources.d.ts +37 -0
  11. package/dist/activities/agent-turn/finalization.d.ts +42 -0
  12. package/dist/activities/agent-turn/github-repository-bindings.d.ts +105 -0
  13. package/dist/activities/agent-turn/governance-model.d.ts +66 -0
  14. package/dist/activities/agent-turn/history-sink.d.ts +52 -0
  15. package/dist/activities/agent-turn/history.d.ts +63 -0
  16. package/dist/activities/agent-turn/media-artifacts.d.ts +145 -0
  17. package/dist/activities/agent-turn/model-usage.d.ts +218 -0
  18. package/dist/activities/agent-turn/quiescence.d.ts +135 -0
  19. package/dist/activities/agent-turn/run-credentials.d.ts +84 -0
  20. package/dist/activities/agent-turn/run.d.ts +3 -0
  21. package/dist/activities/agent-turn/sandbox-establish.d.ts +93 -0
  22. package/dist/activities/agent-turn/sandbox-provision.d.ts +89 -0
  23. package/dist/activities/agent-turn/sandbox-route.d.ts +151 -0
  24. package/dist/activities/agent-turn/sandbox-runtime.d.ts +44 -0
  25. package/dist/activities/agent-turn/stream-attempt.d.ts +107 -0
  26. package/dist/activities/agent-turn/tool-environment.d.ts +142 -0
  27. package/dist/activities/agent-turn/tool-policy.d.ts +127 -0
  28. package/dist/activities/agent-turn/tool-result-spill.d.ts +59 -0
  29. package/dist/activities/agent-turn/turn-context.d.ts +144 -0
  30. package/dist/activities/agent-turn/xai-capacity.d.ts +2 -0
  31. package/dist/activities/agent-turn.d.ts +12 -954
  32. package/dist/activities/automations.d.ts +16 -0
  33. package/dist/activities/common.d.ts +1 -1
  34. package/dist/activities/connector-attachments.d.ts +13 -0
  35. package/dist/activities/documents.d.ts +1 -0
  36. package/dist/activities/environment.d.ts +26 -15
  37. package/dist/activities/generated-images.d.ts +1 -1
  38. package/dist/activities/git-credential-renewal.d.ts +3 -1
  39. package/dist/activities/goals.d.ts +4 -1
  40. package/dist/activities/google-drive-provider.d.ts +29 -0
  41. package/dist/activities/google-drive-publication.d.ts +17 -4
  42. package/dist/activities/incident-telemetry-authority.d.ts +52 -0
  43. package/dist/activities/incident-telemetry-preflight.d.ts +71 -0
  44. package/dist/activities/knowledge-source-sync.d.ts +4 -0
  45. package/dist/activities/mcp-credentials.d.ts +9 -3
  46. package/dist/activities/open-suffix-resume.d.ts +77 -0
  47. package/dist/activities/packs.d.ts +3 -11
  48. package/dist/activities/parent-wake.d.ts +19 -0
  49. package/dist/activities/retained-screenshot-reaper.d.ts +1 -1
  50. package/dist/activities/retained-screenshots.d.ts +1 -1
  51. package/dist/activities/rig-verification.d.ts +1 -1
  52. package/dist/activities/run-input.d.ts +9 -10
  53. package/dist/activities/sandbox-images.d.ts +1 -14
  54. package/dist/activities/sandbox-lease.d.ts +21 -3
  55. package/dist/activities/scheduled-tasks.d.ts +47 -0
  56. package/dist/activities/types.d.ts +23 -2
  57. package/dist/activities/video-media-validation.d.ts +1 -1
  58. package/dist/activities/video-output-retention.d.ts +1 -2
  59. package/dist/activities/xai-auth.d.ts +3 -0
  60. package/dist/{activities-control-BJSDRSIP.js → activities-control-DKG22ONJ.js} +2646 -551
  61. package/dist/activities-control-DKG22ONJ.js.map +1 -0
  62. package/dist/activities-control.d.ts +6 -2
  63. package/dist/activities-turn-4IMC3MAA.js +21326 -0
  64. package/dist/activities-turn-4IMC3MAA.js.map +1 -0
  65. package/dist/activities.d.ts +7 -3
  66. package/dist/artifact-materializer-entry.js +41 -22
  67. package/dist/artifact-materializer-entry.js.map +1 -1
  68. package/dist/{chunk-N5TJIFCE.js → chunk-CE7TPKBS.js} +468 -1
  69. package/dist/chunk-CE7TPKBS.js.map +1 -0
  70. package/dist/chunk-CNYGVRKF.js +4698 -0
  71. package/dist/chunk-CNYGVRKF.js.map +1 -0
  72. package/dist/connection-provider-use.d.ts +18 -0
  73. package/dist/editable-artifact-materializer.d.ts +4 -3
  74. package/dist/github-rest-mcp.d.ts +26 -0
  75. package/dist/index.d.ts +10 -2
  76. package/dist/index.js +58 -14
  77. package/dist/index.js.map +1 -1
  78. package/dist/knowledge-source-sync-activity-policy.d.ts +2 -0
  79. package/dist/model-context-contributions.d.ts +8 -4
  80. package/dist/observability-metrics.d.ts +63 -4
  81. package/dist/opensandbox-kubernetes-inventory.d.ts +21 -0
  82. package/dist/personal-github-git-credentials.d.ts +9 -0
  83. package/dist/pr-review-credentials.d.ts +6 -0
  84. package/dist/sandbox-archive-storage.d.ts +45 -0
  85. package/dist/sandbox-resume.d.ts +65 -6
  86. package/dist/sandbox-routing.d.ts +38 -2
  87. package/dist/scheduled-alert-occurrence.d.ts +33 -0
  88. package/dist/workflow-bundle.js +87 -28
  89. package/dist/workflows/activities.d.ts +1 -0
  90. package/dist/workflows/automations.d.ts +6 -0
  91. package/dist/workflows/document-index.d.ts +1 -0
  92. package/dist/workflows.d.ts +1 -0
  93. package/package.json +17 -17
  94. package/src/activities/agent-turn/admission.ts +292 -0
  95. package/src/activities/agent-turn/agent-build.ts +802 -0
  96. package/src/activities/agent-turn/claim.ts +590 -0
  97. package/src/activities/agent-turn/codex-capacity.ts +720 -0
  98. package/src/activities/agent-turn/codex.ts +94 -0
  99. package/src/activities/agent-turn/compaction-prep.ts +722 -0
  100. package/src/activities/agent-turn/credential-leases.ts +240 -0
  101. package/src/activities/agent-turn/errors.ts +1068 -0
  102. package/src/activities/agent-turn/failure-settlement.ts +1436 -0
  103. package/src/activities/agent-turn/file-resources.ts +227 -0
  104. package/src/activities/agent-turn/finalization.ts +754 -0
  105. package/src/activities/agent-turn/github-repository-bindings.ts +300 -0
  106. package/src/activities/agent-turn/governance-model.ts +465 -0
  107. package/src/activities/agent-turn/history-sink.ts +139 -0
  108. package/src/activities/agent-turn/history.ts +200 -0
  109. package/src/activities/agent-turn/media-artifacts.ts +359 -0
  110. package/src/activities/agent-turn/model-usage.ts +823 -0
  111. package/src/activities/agent-turn/quiescence.ts +353 -0
  112. package/src/activities/agent-turn/run-credentials.ts +761 -0
  113. package/src/activities/agent-turn/run.ts +1530 -0
  114. package/src/activities/agent-turn/sandbox-establish.ts +1207 -0
  115. package/src/activities/agent-turn/sandbox-provision.ts +569 -0
  116. package/src/activities/agent-turn/sandbox-route.ts +358 -0
  117. package/src/activities/agent-turn/sandbox-runtime.ts +731 -0
  118. package/src/activities/agent-turn/stream-attempt.ts +1670 -0
  119. package/src/activities/agent-turn/tool-environment.ts +729 -0
  120. package/src/activities/agent-turn/tool-policy.ts +216 -0
  121. package/src/activities/agent-turn/tool-result-spill.ts +265 -0
  122. package/src/activities/agent-turn/turn-context.ts +281 -0
  123. package/src/activities/agent-turn/xai-capacity.ts +229 -0
  124. package/src/activities/agent-turn.ts +12 -12701
  125. package/src/activities/api-integrations.ts +3 -0
  126. package/src/activities/automations.ts +214 -0
  127. package/src/activities/codemode-dispatcher.ts +12 -1
  128. package/src/activities/common.ts +1 -5
  129. package/src/activities/connector-attachments.ts +168 -0
  130. package/src/activities/context-compaction.ts +18 -0
  131. package/src/activities/environment.ts +151 -55
  132. package/src/activities/generated-images.ts +26 -11
  133. package/src/activities/git-credential-renewal.ts +13 -3
  134. package/src/activities/goals.ts +85 -19
  135. package/src/activities/google-drive-provider.ts +178 -0
  136. package/src/activities/google-drive-publication.ts +147 -49
  137. package/src/activities/incident-telemetry-authority.ts +327 -0
  138. package/src/activities/incident-telemetry-preflight.ts +317 -0
  139. package/src/activities/knowledge-source-sync.ts +178 -37
  140. package/src/activities/mcp-credentials.ts +191 -17
  141. package/src/activities/open-suffix-resume.ts +422 -0
  142. package/src/activities/packs.ts +12 -60
  143. package/src/activities/parent-wake.ts +64 -14
  144. package/src/activities/retained-screenshot-reaper.ts +6 -3
  145. package/src/activities/retained-screenshots.ts +15 -47
  146. package/src/activities/rig-verification.ts +1 -1
  147. package/src/activities/run-input.ts +102 -142
  148. package/src/activities/sandbox-images.ts +7 -166
  149. package/src/activities/sandbox-lease.ts +406 -15
  150. package/src/activities/scheduled-tasks.ts +2103 -411
  151. package/src/activities/session-state.ts +10 -2
  152. package/src/activities/types.ts +32 -2
  153. package/src/activities/video-media-validation.ts +3 -3
  154. package/src/activities/video-output-retention.ts +2 -57
  155. package/src/activities/xai-auth.ts +8 -0
  156. package/src/activities-control.ts +2 -0
  157. package/src/activities.ts +3 -0
  158. package/src/activity-services.ts +13 -4
  159. package/src/connection-provider-use.ts +36 -0
  160. package/src/editable-artifact-materializer-subprocess.ts +25 -14
  161. package/src/editable-artifact-materializer.ts +29 -18
  162. package/src/github-rest-mcp.ts +713 -0
  163. package/src/http.ts +16 -6
  164. package/src/index.ts +40 -5
  165. package/src/knowledge-source-sync-activity-policy.ts +2 -0
  166. package/src/model-context-contributions.ts +33 -10
  167. package/src/observability-metrics.ts +357 -0
  168. package/src/opensandbox-kubernetes-inventory.ts +292 -0
  169. package/src/personal-github-git-credentials.ts +331 -0
  170. package/src/pr-review-credentials.ts +163 -0
  171. package/src/sandbox-archive-storage.ts +159 -0
  172. package/src/sandbox-resume.ts +449 -63
  173. package/src/sandbox-routing.ts +314 -16
  174. package/src/scheduled-alert-occurrence.ts +176 -0
  175. package/src/workflows/activities.ts +18 -2
  176. package/src/workflows/automations.ts +11 -0
  177. package/src/workflows/session.ts +13 -3
  178. package/src/workflows.ts +1 -0
  179. package/dist/activities-control-BJSDRSIP.js.map +0 -1
  180. package/dist/activities-turn-J5YO7XXX.js +0 -17243
  181. package/dist/activities-turn-J5YO7XXX.js.map +0 -1
  182. package/dist/chunk-N5TJIFCE.js.map +0 -1
  183. package/dist/chunk-P6Z2SLAA.js +0 -3092
  184. package/dist/chunk-P6Z2SLAA.js.map +0 -1
@@ -13,13 +13,15 @@ import {
13
13
  import {
14
14
  RetainedScreenshotQuotaExceededError,
15
15
  getRetainedScreenshotArtifact,
16
+ isDatabasePersistenceFailure,
17
+ isSessionEventPersistenceError,
16
18
  prepareRetainedScreenshotArtifact,
17
19
  recordRetainedScreenshotArtifactError,
18
20
  settleRetainedScreenshotArtifactReady,
19
21
  type Database,
20
22
  type RetainedScreenshotArtifact,
21
23
  } from "@opengeni/db";
22
- import type { ObjectHead, ObjectStorage } from "@opengeni/storage";
24
+ import { retryWhileMissing, type ObjectStorage } from "@opengeni/storage";
23
25
  import { createHash } from "node:crypto";
24
26
 
25
27
  const PNG_SIGNATURE = Uint8Array.of(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a);
@@ -588,13 +590,14 @@ export async function retainComputerScreenshot(input: {
588
590
  if (error instanceof RetainedScreenshotQuotaExceededError) {
589
591
  return unavailable(identity.artifactId, "quota_exceeded");
590
592
  }
593
+ if (isSessionEventPersistenceError(error) || isDatabasePersistenceFailure(error)) {
594
+ return unavailable(identity.artifactId, "pending");
595
+ }
591
596
  throw error;
592
597
  }
593
598
 
594
599
  if (prepared.status === "ready") {
595
- return (await verifyReadyArtifact(input.objectStorage, prepared))
596
- ? reference(prepared)
597
- : unavailable(identity.artifactId, "missing_storage");
600
+ return reference(prepared);
598
601
  }
599
602
  if (prepared.status !== "pending" && prepared.status !== "reconciling") {
600
603
  return unavailable(identity.artifactId, unavailableReasonForStatus(prepared.status));
@@ -607,8 +610,6 @@ export async function retainComputerScreenshot(input: {
607
610
  body: screenshot.bytes,
608
611
  sha256: screenshot.sha256,
609
612
  });
610
- const head = await input.objectStorage.headFile(prepared.file);
611
- assertStoredScreenshotHead(head, screenshot);
612
613
  } catch (error) {
613
614
  await recordRetainedScreenshotArtifactError(input.db, {
614
615
  accountId: input.accountId,
@@ -639,9 +640,7 @@ export async function retainComputerScreenshot(input: {
639
640
  identity.artifactId,
640
641
  ).catch(() => undefined);
641
642
  if (stillReferenced?.status === "ready") {
642
- return (await verifyReadyArtifact(input.objectStorage, stillReferenced))
643
- ? reference(stillReferenced)
644
- : unavailable(identity.artifactId, "missing_storage");
643
+ return reference(stillReferenced);
645
644
  }
646
645
  if (
647
646
  stillReferenced === null ||
@@ -810,9 +809,6 @@ async function materializeRetainedScreenshotHistoryWithCache(
810
809
  ): Promise<Array<Record<string, unknown>>> {
811
810
  const now = input.now ?? new Date();
812
811
  const dataUrlForReceipt = async (receipt: RetainedArtifactMetadata): Promise<string> => {
813
- if (!receipt.available) {
814
- throw new RetainedScreenshotUnavailableError(receipt.artifactId, receipt.reason);
815
- }
816
812
  let dataUrl = cache.get(receipt.artifactId);
817
813
  if (!dataUrl) {
818
814
  if (!input.objectStorage) {
@@ -834,9 +830,12 @@ async function materializeRetainedScreenshotHistoryWithCache(
834
830
  if (artifact.retentionExpiresAt.getTime() <= now.getTime()) {
835
831
  throw new RetainedScreenshotUnavailableError(receipt.artifactId, "expired");
836
832
  }
837
- const bytes = await input.objectStorage.getFileBytes(artifact.file).catch(() => null);
838
- if (!bytes)
833
+ const object = await retryWhileMissing(async () =>
834
+ input.objectStorage!.getObjectBytes(artifact.file.objectKey),
835
+ );
836
+ if (!object)
839
837
  throw new RetainedScreenshotUnavailableError(receipt.artifactId, "missing_storage");
838
+ const bytes = object.bytes;
840
839
  const validated = validateRetainableSessionImage({
841
840
  bytes,
842
841
  declaredMediaType: artifact.mediaType,
@@ -846,8 +845,8 @@ async function materializeRetainedScreenshotHistoryWithCache(
846
845
  validated.sha256 !== artifact.sha256 ||
847
846
  validated.width !== artifact.width ||
848
847
  validated.height !== artifact.height ||
849
- receipt.sha256 !== artifact.sha256 ||
850
- receipt.originalBytes !== artifact.sizeBytes
848
+ (receipt.available &&
849
+ (receipt.sha256 !== artifact.sha256 || receipt.originalBytes !== artifact.sizeBytes))
851
850
  ) {
852
851
  throw new RetainedScreenshotUnavailableError(receipt.artifactId, "invalid_content");
853
852
  }
@@ -1020,37 +1019,6 @@ function unavailableReasonForStatus(
1020
1019
  }
1021
1020
  }
1022
1021
 
1023
- async function verifyReadyArtifact(
1024
- storage: ObjectStorage,
1025
- artifact: RetainedScreenshotArtifact,
1026
- ): Promise<boolean> {
1027
- try {
1028
- assertStoredScreenshotHead(await storage.headFile(artifact.file), {
1029
- sizeBytes: artifact.sizeBytes,
1030
- mediaType: artifact.mediaType as RetainableSessionImageMediaType,
1031
- sha256: artifact.sha256,
1032
- });
1033
- return true;
1034
- } catch {
1035
- return false;
1036
- }
1037
- }
1038
-
1039
- function assertStoredScreenshotHead(
1040
- head: ObjectHead,
1041
- expected: Pick<ValidatedSessionImage, "sizeBytes" | "mediaType" | "sha256">,
1042
- ): void {
1043
- if (head.ContentLength !== expected.sizeBytes) {
1044
- throw new Error("retained screenshot object size mismatch");
1045
- }
1046
- if (head.ContentType !== expected.mediaType) {
1047
- throw new Error("retained screenshot object MIME mismatch");
1048
- }
1049
- if (head.Metadata?.sha256 !== expected.sha256) {
1050
- throw new Error("retained screenshot object SHA-256 metadata mismatch");
1051
- }
1052
- }
1053
-
1054
1022
  function retainedReceiptFromImageContent(entry: unknown): RetainedArtifactMetadata | null {
1055
1023
  if (!entry || typeof entry !== "object" || Array.isArray(entry)) return null;
1056
1024
  const image = (entry as Record<string, unknown>).image;
@@ -501,7 +501,7 @@ export async function runWithOwnedRigVerificationSandbox<T>(
501
501
  holderId,
502
502
  subjectId: null,
503
503
  backend: input.settings.sandboxBackend,
504
- image: input.settings.modalImageRef ?? input.settings.dockerImage,
504
+ image: rigProviderImageSourceImage(input.settings, input.settings.sandboxBackend),
505
505
  rigVersionId: input.rigVersionId,
506
506
  leaseTtlMs: RIG_VERIFICATION_OWNER_TTL_MS,
507
507
  warmingLeaseTtlMs: RIG_VERIFICATION_OWNER_TTL_MS,
@@ -9,12 +9,10 @@ import { createHash } from "node:crypto";
9
9
  import {
10
10
  getActiveSessionHistoryItemsPaged,
11
11
  getFilesForSubject,
12
- getLatestRunState,
13
- getHumanInputResumeForEvent,
14
12
  getSandboxSessionEnvelope,
15
13
  getSessionEvent,
16
14
  listSessionSystemUpdatesForTurn,
17
- requireFileForSubject,
15
+ listTurnOpenSuffixToolCalls,
18
16
  type Database,
19
17
  } from "@opengeni/db";
20
18
  import {
@@ -85,7 +83,6 @@ export type TurnInputOptions = {
85
83
  providerApi: HistoryProviderApi;
86
84
  projectCanonicalHistory?: ModelHistoryAttachmentProjector;
87
85
  materializeModelHistory?: ModelHistoryAttachmentProjector;
88
- materializeSerializedRunState?: (serialized: string) => Promise<string>;
89
86
  projectModelHistory?: ModelHistoryAttachmentProjector;
90
87
  loadActiveHistory?: typeof getActiveSessionHistoryItemsPaged;
91
88
  /** Bounded critical-path timings; telemetry failures never affect preparation. */
@@ -148,8 +145,14 @@ export type ModelAttachmentContent = {
148
145
 
149
146
  export type ModelHistoryAttachmentProjector = (
150
147
  items: Array<Record<string, unknown>>,
148
+ options?: ModelHistoryAttachmentProjectionOptions,
151
149
  ) => Promise<Array<Record<string, unknown>>>;
152
150
 
151
+ export type ModelHistoryAttachmentProjectionOptions = {
152
+ /** Exact, already-authorized files attached to the triggering message. */
153
+ inlineFiles?: readonly FileAsset[];
154
+ };
155
+
153
156
  export type ModelAttachmentInputPolicy = {
154
157
  supportsImageInput: boolean;
155
158
  inputFileMediaTypes: readonly string[];
@@ -212,6 +215,7 @@ export async function modelAttachmentContentForFiles(
212
215
  if (
213
216
  file.status !== "ready" ||
214
217
  !descriptor ||
218
+ descriptor.kind !== "image" ||
215
219
  file.sizeBytes > remainingBytes ||
216
220
  !/^[a-f0-9]{64}$/.test(checksum)
217
221
  ) {
@@ -269,18 +273,6 @@ export async function modelAttachmentContentForFiles(
269
273
  );
270
274
  }
271
275
 
272
- function modelAcceptsFileMediaType(
273
- policy: ModelAttachmentInputPolicy,
274
- contentType: string,
275
- ): boolean {
276
- const normalized = contentType.toLowerCase().split(";", 1)[0]?.trim() ?? "";
277
- return policy.inputFileMediaTypes.some(
278
- (accepted) =>
279
- accepted === normalized ||
280
- (accepted.endsWith("/*") && normalized.startsWith(accepted.slice(0, -1))),
281
- );
282
- }
283
-
284
276
  function attachmentRefsFromItem(item: Record<string, unknown>): FileResourceRef[] {
285
277
  const raw = item[MODEL_ATTACHMENT_REFS_FIELD];
286
278
  if (!Array.isArray(raw)) return [];
@@ -292,37 +284,36 @@ function attachmentRefsFromItem(item: Record<string, unknown>): FileResourceRef[
292
284
  return refs;
293
285
  }
294
286
 
295
- function attachmentUnavailableText(ref: FileResourceRef, file: FileAsset | undefined): string {
287
+ function attachmentReceiptText(ref: FileResourceRef, file: FileAsset | undefined): string {
296
288
  if (!file) {
297
- return `[Attachment unavailable or no longer authorized: ${ref.fileId}.]`;
289
+ return (
290
+ `[Earlier attachment: fileId=${ref.fileId}; mountDirectory=${resourceMountPath(ref)}. ` +
291
+ `Use the existing file there, or call files__files_get_download_url with this fileId and ` +
292
+ `download it with the shell.]`
293
+ );
298
294
  }
299
- const filename = file?.safeFilename ?? ref.fileId;
300
- const mediaType = file?.contentType ?? "unknown type";
301
- const path = file ? sandboxFilePath(ref, file) : `/workspace/${resourceMountPath(ref)}`;
295
+ const path = sandboxFilePath(ref, file);
302
296
  return (
303
- `[Attachment not included directly because the selected model does not accept this input ` +
304
- `or it exceeded the safe inline limit: ${filename} (${mediaType}). ` +
305
- `It remains available to tools in the sandbox at ${path}.]`
297
+ `[Attachment: ${file.safeFilename}; fileId=${file.id}; type=${file.contentType}; ` +
298
+ `bytes=${file.sizeBytes}; path=${path}. If the local path is absent, call ` +
299
+ `files__files_get_download_url with this fileId and download it with the shell.]`
306
300
  );
307
301
  }
308
302
 
309
303
  /**
310
- * Build one turn-scoped durable-attachment projector. Metadata is batch-loaded
311
- * and file bytes are memoized, so compaction/retry can reuse the same work and
312
- * the SDK's repeated tool loop never touches storage or rescans old history.
304
+ * Build one turn-scoped durable-attachment projector. Current attachment
305
+ * metadata arrives already authorized; bytes are memoized for same-turn retry.
306
+ * Historical projection has no database or object-storage path.
313
307
  */
314
308
  export function createModelHistoryAttachmentProjector(
315
- db: Database,
316
- authority: { accountId: string; workspaceId: string; subjectId: string | null },
317
309
  policy: ModelAttachmentInputPolicy,
318
310
  readFileBytes?: (file: FileAsset) => Promise<Uint8Array>,
319
311
  ): ModelHistoryAttachmentProjector {
320
- const fileById = new Map<string, FileAsset>();
321
- const missingFileIds = new Set<string>();
322
312
  const contentById = new Map<string, ModelAttachmentContent>();
323
313
  const attemptedContentIds = new Set<string>();
324
314
 
325
- return async (items) => {
315
+ return async (items, options = {}) => {
316
+ const currentFileById = new Map((options.inlineFiles ?? []).map((file) => [file.id, file]));
326
317
  const refsByIndex = new Map<number, FileResourceRef[]>();
327
318
  const orderedFileIds: string[] = [];
328
319
  const seenFileIds = new Set<string>();
@@ -338,32 +329,17 @@ export function createModelHistoryAttachmentProjector(
338
329
  }
339
330
  if (refsByIndex.size === 0) return items;
340
331
 
341
- const unknownIds = orderedFileIds.filter((id) => !fileById.has(id) && !missingFileIds.has(id));
342
- if (unknownIds.length > 0) {
343
- const files = await getFilesForSubject(db, {
344
- ...authority,
345
- fileIds: unknownIds,
346
- });
347
- for (const file of files) fileById.set(file.id, file);
348
- for (const id of unknownIds) {
349
- if (!fileById.has(id)) missingFileIds.add(id);
350
- }
351
- }
352
-
353
332
  if (readFileBytes) {
354
- // Prefer the newest attachments if the aggregate request safety limit is
355
- // reached; an old image becomes a marker instead of hiding the new prompt.
333
+ // Only the triggering message's attachments cross the provider byte
334
+ // boundary. Historical messages retain compact durable receipts and can
335
+ // recover bytes explicitly through the existing Files MCP + shell path.
356
336
  const readable = [...orderedFileIds]
357
337
  .reverse()
358
- .map((id) => fileById.get(id))
338
+ .map((id) => currentFileById.get(id))
359
339
  .filter((file): file is FileAsset => {
360
340
  if (!file || attemptedContentIds.has(file.id)) return false;
361
341
  const descriptor = modelAttachmentDescriptor(file.contentType);
362
- return Boolean(
363
- descriptor &&
364
- ((descriptor.kind === "image" && policy.supportsImageInput) ||
365
- (descriptor.kind === "file" && modelAcceptsFileMediaType(policy, file.contentType))),
366
- );
342
+ return Boolean(descriptor && descriptor.kind === "image" && policy.supportsImageInput);
367
343
  });
368
344
  for (const file of readable) attemptedContentIds.add(file.id);
369
345
  const content = await modelAttachmentContentForFiles(readable, readFileBytes);
@@ -376,21 +352,17 @@ export function createModelHistoryAttachmentProjector(
376
352
  const existingContent = Array.isArray(original.content)
377
353
  ? [...original.content]
378
354
  : [{ type: "input_text", text: String(original.content ?? "") }];
379
- const attachmentParts = refs.map((ref) => {
380
- const attachment = contentById.get(ref.fileId);
381
- if (!attachment) {
382
- return {
383
- type: "input_text",
384
- text: attachmentUnavailableText(ref, fileById.get(ref.fileId)),
385
- };
355
+ const attachmentParts = refs.flatMap((ref) => {
356
+ const currentFile = currentFileById.get(ref.fileId);
357
+ const attachment = currentFile ? contentById.get(ref.fileId) : undefined;
358
+ const receipt = {
359
+ type: "input_text",
360
+ text: attachmentReceiptText(ref, currentFile),
361
+ };
362
+ if (!attachment || attachment.kind !== "image") {
363
+ return [receipt];
386
364
  }
387
- return attachment.kind === "image"
388
- ? { type: "input_image", image: attachment.dataUrl }
389
- : {
390
- type: "input_file",
391
- file: attachment.dataUrl,
392
- filename: attachment.filename,
393
- };
365
+ return [receipt, { type: "input_image", image: attachment.dataUrl }];
394
366
  });
395
367
  const clone: Record<string, unknown> = {
396
368
  ...original,
@@ -494,7 +466,7 @@ export async function turnInput(
494
466
  trigger,
495
467
  undefined,
496
468
  joinInternalContext(internalContext, attachmentContext),
497
- fileAttachments.map((attachment) => attachment.resource),
469
+ fileAttachments,
498
470
  options.providerApi,
499
471
  options.projectCanonicalHistory,
500
472
  options.materializeModelHistory,
@@ -523,66 +495,8 @@ export async function turnInput(
523
495
  options,
524
496
  );
525
497
  }
526
- if (trigger.type === "user.approvalDecision") {
527
- const payload = trigger.payload as {
528
- approvalId?: unknown;
529
- decision?: unknown;
530
- message?: unknown;
531
- };
532
- // Approvals are the one path that legitimately requires the RunState blob:
533
- // a turn frozen mid-flight cannot be represented as plain history items.
534
- const state = await getLatestRunState(db, trigger.workspaceId, trigger.sessionId);
535
- if (!state) {
536
- throw new Error("No saved run state is available for approval decision");
537
- }
538
- const serializedRunState = resumeRunState(state);
539
- const prepared = await runtime.prepareInput(agent, {
540
- kind: "approval",
541
- serializedRunState: options.materializeSerializedRunState
542
- ? await options.materializeSerializedRunState(serializedRunState)
543
- : serializedRunState,
544
- approvalId: String(payload.approvalId ?? ""),
545
- decision: payload.decision === "approve" ? "approve" : "reject",
546
- ...(typeof payload.message === "string" ? { message: payload.message } : {}),
547
- });
548
- return {
549
- input: prepared,
550
- persistedHistoryCount: prepared.persistedHistoryCount,
551
- providerArtifactCandidates: {
552
- knownHistoryItemIds: [],
553
- historyItemIds: [],
554
- runStateId: state.id,
555
- },
556
- };
557
- }
558
- if (trigger.type === "user.humanInputResponse") {
559
- const [state, resume] = await Promise.all([
560
- getLatestRunState(db, trigger.workspaceId, trigger.sessionId),
561
- getHumanInputResumeForEvent(db, trigger.workspaceId, trigger.sessionId, trigger),
562
- ]);
563
- if (!state) {
564
- throw new Error("No saved run state is available for human-input response");
565
- }
566
- if (!resume) {
567
- throw new Error("Human-input response does not resolve to a durable request");
568
- }
569
- const serializedRunState = resumeRunState(state);
570
- const prepared = await runtime.prepareInput(agent, {
571
- kind: "human_input",
572
- serializedRunState: options.materializeSerializedRunState
573
- ? await options.materializeSerializedRunState(serializedRunState)
574
- : serializedRunState,
575
- toolCallId: resume.toolCallId,
576
- });
577
- return {
578
- input: prepared,
579
- persistedHistoryCount: prepared.persistedHistoryCount,
580
- providerArtifactCandidates: {
581
- knownHistoryItemIds: [],
582
- historyItemIds: [],
583
- runStateId: state.id,
584
- },
585
- };
498
+ if (trigger.type === "user.approvalDecision" || trigger.type === "user.humanInputResponse") {
499
+ return await openSuffixMessageInput(db, runtime, agent, trigger, internalContext, options);
586
500
  }
587
501
  throw new Error(`Unsupported trigger event type: ${trigger.type}`);
588
502
  }
@@ -592,6 +506,43 @@ function joinInternalContext(...parts: Array<string | undefined>): string | unde
592
506
  return content.length > 0 ? content.join("\n\n") : undefined;
593
507
  }
594
508
 
509
+ async function openSuffixMessageInput(
510
+ db: Database,
511
+ runtime: OpenGeniRuntime,
512
+ agent: any,
513
+ trigger: NonNullable<Awaited<ReturnType<typeof getSessionEvent>>>,
514
+ internalContext: string | undefined,
515
+ options: TurnInputOptions,
516
+ ): Promise<PreparedTurnInput> {
517
+ const suffixRows = await listTurnOpenSuffixToolCalls(
518
+ db,
519
+ trigger.workspaceId,
520
+ trigger.sessionId,
521
+ options.turnId,
522
+ );
523
+ if (suffixRows.length === 0) {
524
+ throw new Error("Open suffix resume has no interruption rows");
525
+ }
526
+ if (suffixRows.some((row) => row.resultItem == null)) {
527
+ throw new Error("Open suffix resume still has unresolved members");
528
+ }
529
+ return await messageInput(
530
+ db,
531
+ runtime,
532
+ agent,
533
+ trigger,
534
+ undefined,
535
+ internalContext,
536
+ [],
537
+ options.providerApi,
538
+ options.projectCanonicalHistory,
539
+ options.materializeModelHistory,
540
+ options.projectModelHistory,
541
+ options.loadActiveHistory,
542
+ options,
543
+ );
544
+ }
545
+
595
546
  /** Build one inference from canonical history plus attempt-local operational context. */
596
547
  async function messageInput(
597
548
  db: Database,
@@ -600,7 +551,7 @@ async function messageInput(
600
551
  trigger: NonNullable<Awaited<ReturnType<typeof getSessionEvent>>>,
601
552
  text: string | undefined,
602
553
  internalContext: string | undefined,
603
- currentAttachmentRefs: FileResourceRef[] = [],
554
+ currentAttachments: UserMessageFileAttachment[] = [],
604
555
  providerApi: HistoryProviderApi = "responses",
605
556
  projectCanonicalHistory?: ModelHistoryAttachmentProjector,
606
557
  materializeModelHistory?: ModelHistoryAttachmentProjector,
@@ -608,6 +559,7 @@ async function messageInput(
608
559
  loadActiveHistory: typeof getActiveSessionHistoryItemsPaged = getActiveSessionHistoryItemsPaged,
609
560
  preparationOptions: Pick<TurnInputOptions, "onPreparationPhase"> = {},
610
561
  ): Promise<PreparedTurnInput> {
562
+ const currentAttachmentRefs = currentAttachments.map((attachment) => attachment.resource);
611
563
  const [stored, envelope] = await Promise.all([
612
564
  measureHistoryPreparationPhase(preparationOptions, "durable_history_load", async () =>
613
565
  loadActiveHistory(db, trigger.workspaceId, trigger.sessionId),
@@ -646,7 +598,11 @@ async function messageInput(
646
598
  preparationOptions,
647
599
  "model_attachment_projection",
648
600
  async () =>
649
- projectModelHistory ? await projectModelHistory(materializedHistory) : materializedHistory,
601
+ projectModelHistory
602
+ ? await projectModelHistory(materializedHistory, {
603
+ inlineFiles: currentAttachments.map((attachment) => attachment.file),
604
+ })
605
+ : materializedHistory,
650
606
  );
651
607
  const prepared = await measureHistoryPreparationPhase(
652
608
  preparationOptions,
@@ -719,18 +675,22 @@ async function resolveUserMessageFileAttachments(
719
675
  subjectId: string | null,
720
676
  resources: ResourceRef[],
721
677
  ): Promise<UserMessageFileAttachment[]> {
722
- const attachments: UserMessageFileAttachment[] = [];
723
- for (const resource of resources) {
724
- if (resource.kind !== "file") continue;
725
- const file = await requireFileForSubject(db, {
726
- accountId,
727
- workspaceId,
728
- subjectId,
729
- fileId: resource.fileId,
730
- });
731
- attachments.push({ resource, file });
732
- }
733
- return attachments;
678
+ const fileResources = resources.filter(
679
+ (resource): resource is FileResourceRef => resource.kind === "file",
680
+ );
681
+ if (fileResources.length === 0) return [];
682
+ const files = await getFilesForSubject(db, {
683
+ accountId,
684
+ workspaceId,
685
+ subjectId,
686
+ fileIds: fileResources.map((resource) => resource.fileId),
687
+ });
688
+ const fileById = new Map(files.map((file) => [file.id, file]));
689
+ return fileResources.map((resource) => {
690
+ const file = fileById.get(resource.fileId);
691
+ if (!file) throw new Error(`File not found: ${resource.fileId}`);
692
+ return { resource, file };
693
+ });
734
694
  }
735
695
 
736
696
  function userMessageAttachmentsContext(
@@ -750,5 +710,5 @@ function sandboxFilePath(
750
710
  resource: Extract<ResourceRef, { kind: "file" }>,
751
711
  file: FileAsset,
752
712
  ): string {
753
- return `/workspace/${resourceMountPath(resource)}/${file.safeFilename}`;
713
+ return `${resourceMountPath(resource)}/${file.safeFilename}`;
754
714
  }
@@ -1,167 +1,8 @@
1
- import type { Settings } from "@opengeni/config";
2
- import type { CapabilityPack, RigVersion, SandboxBackend } from "@opengeni/contracts";
3
- import {
4
- rigProviderImageContentHash,
5
- rigProviderImageMatchesDefinition,
6
- rigProviderImageProviderBindingKeyHash,
1
+ export {
2
+ resolveRigProviderImageSelection,
3
+ rigProviderImageSourceImage,
4
+ settingsWithPackSandboxImage,
5
+ settingsWithRigImage,
6
+ settingsWithRigProviderImage,
7
+ type RigProviderImageSelectionReason,
7
8
  } from "@opengeni/core";
8
- import { resolveModalCheckpointProviderBinding } from "@opengeni/runtime/sandbox";
9
-
10
- export function settingsWithPackSandboxImage(
11
- settings: Settings,
12
- sandboxImage: string | null,
13
- sandboxProviderImages: CapabilityPack["sandboxProviderImages"] | null = null,
14
- ): Settings {
15
- if (!sandboxImage) return settings;
16
- return {
17
- ...settings,
18
- dockerImage: sandboxImage,
19
- modalImageRef: sandboxImage,
20
- modalImageId: sandboxProviderImages?.modal?.imageId,
21
- };
22
- }
23
-
24
- export function settingsWithRigImage(settings: Settings, rigImage: string | null): Settings {
25
- if (!rigImage) return settings;
26
- return {
27
- ...settings,
28
- dockerImage: rigImage,
29
- modalImageRef: rigImage,
30
- // A logical rig image invalidates a lower-precedence pack/deployment
31
- // provider ID. A verified version-bound provider ID is applied separately
32
- // after this precedence step.
33
- modalImageId: undefined,
34
- };
35
- }
36
-
37
- export function rigProviderImageSourceImage(
38
- settings: Settings,
39
- backend: SandboxBackend,
40
- ): string | null {
41
- if (backend === "modal") return settings.modalImageId ?? settings.modalImageRef ?? null;
42
- if (backend === "docker") return settings.dockerImage ?? null;
43
- return null;
44
- }
45
-
46
- export type RigProviderImageSelectionReason =
47
- | "selected"
48
- | "missing"
49
- | "provider_unsupported"
50
- | "not_ready"
51
- | "not_cold_boot_validated"
52
- | "content_mismatch"
53
- | "provider_binding_unavailable"
54
- | "provider_binding_mismatch";
55
-
56
- export function resolveRigProviderImageSelection(
57
- settings: Settings,
58
- version: RigVersion | null,
59
- backend: SandboxBackend,
60
- currentProviderBindingKeyHash: string | null,
61
- ): {
62
- settings: Settings;
63
- reason: RigProviderImageSelectionReason;
64
- contentHash: string | null;
65
- imageId: string | null;
66
- } {
67
- if (!version) {
68
- return { settings, reason: "missing", contentHash: null, imageId: null };
69
- }
70
- if (backend !== "modal") {
71
- return { settings, reason: "provider_unsupported", contentHash: null, imageId: null };
72
- }
73
- const image = version.providerImages[backend];
74
- if (!image) {
75
- return { settings, reason: "missing", contentHash: null, imageId: null };
76
- }
77
- if (image.status !== "ready" || !image.imageId) {
78
- return {
79
- settings,
80
- reason: "not_ready",
81
- contentHash: image.contentHash,
82
- imageId: image.imageId,
83
- };
84
- }
85
- if (image.coldBootValidation?.version !== 1) {
86
- return {
87
- settings,
88
- reason: "not_cold_boot_validated",
89
- contentHash: image.contentHash,
90
- imageId: null,
91
- };
92
- }
93
- const sourceImage = rigProviderImageSourceImage(settings, backend);
94
- const expectedContentHash = rigProviderImageContentHash({
95
- backend,
96
- sourceImage,
97
- definition: version,
98
- });
99
- if (
100
- image.sourceImage !== sourceImage ||
101
- image.contentHash !== expectedContentHash ||
102
- !rigProviderImageMatchesDefinition(image, version)
103
- ) {
104
- return {
105
- settings,
106
- reason: "content_mismatch",
107
- contentHash: expectedContentHash,
108
- imageId: null,
109
- };
110
- }
111
- if (!image.providerBindingKeyHash || !currentProviderBindingKeyHash) {
112
- return {
113
- settings,
114
- reason: "provider_binding_unavailable",
115
- contentHash: expectedContentHash,
116
- imageId: null,
117
- };
118
- }
119
- if (image.providerBindingKeyHash !== currentProviderBindingKeyHash) {
120
- return {
121
- settings,
122
- reason: "provider_binding_mismatch",
123
- contentHash: expectedContentHash,
124
- imageId: null,
125
- };
126
- }
127
- return {
128
- // Keep modalImageRef unchanged: the lease continues to fence the logical
129
- // rig/pack/deployment image, so a newly available build optimization never
130
- // rotates an already-warm or archive-backed box. modalImageId affects only
131
- // genuinely fresh provider creation.
132
- settings: { ...settings, modalImageId: image.imageId },
133
- reason: "selected",
134
- contentHash: expectedContentHash,
135
- imageId: image.imageId,
136
- };
137
- }
138
-
139
- export async function settingsWithRigProviderImage(
140
- settings: Settings,
141
- version: RigVersion | null,
142
- backend: SandboxBackend,
143
- resolveBinding: typeof resolveModalCheckpointProviderBinding = resolveModalCheckpointProviderBinding,
144
- ): Promise<Settings> {
145
- const image = backend === "modal" ? version?.providerImages.modal : null;
146
- if (image?.status !== "ready" || !image.providerBindingKeyHash) {
147
- return resolveRigProviderImageSelection(settings, version, backend, null).settings;
148
- }
149
- const structural = resolveRigProviderImageSelection(
150
- settings,
151
- version,
152
- backend,
153
- image.providerBindingKeyHash,
154
- );
155
- if (structural.reason !== "selected") return structural.settings;
156
- let currentProviderBindingKeyHash: string | null = null;
157
- try {
158
- const binding = await resolveBinding(settings);
159
- currentProviderBindingKeyHash = rigProviderImageProviderBindingKeyHash(binding.key);
160
- } catch {
161
- // Provider identity is an authority check, not a reason to fail the turn.
162
- // Preserve the logical image so fresh creation and runtime setup remain the
163
- // truthful fallback when the identity lookup is unavailable.
164
- }
165
- return resolveRigProviderImageSelection(settings, version, backend, currentProviderBindingKeyHash)
166
- .settings;
167
- }