@opengeni/worker-bundle 0.20.3 → 0.22.1-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
@@ -0,0 +1,216 @@
1
+ import {
2
+ desktopCapableBackend,
3
+ type ComputerToolMode,
4
+ type LazyToolTransport,
5
+ } from "@opengeni/runtime";
6
+ import {
7
+ isDirectOpenAiApiBaseUrl,
8
+ type ModelProviderApi,
9
+ type RegistryProviderKind,
10
+ type Settings,
11
+ } from "@opengeni/config";
12
+ import { type ModelAttachmentInputPolicy } from "../run-input";
13
+ import { imageProviderBindingHash } from "../image-generation-operation";
14
+
15
+ export function shouldStartOnTurnRecording(params: {
16
+ recordingEnabled: boolean;
17
+ desktopEnabled: boolean;
18
+ establishedBackendId: string;
19
+ effectiveBackend: Settings["sandboxBackend"] | undefined;
20
+ }): boolean {
21
+ return (
22
+ params.recordingEnabled &&
23
+ params.desktopEnabled &&
24
+ desktopCapableBackend(params.establishedBackendId) &&
25
+ params.effectiveBackend !== "selfhosted"
26
+ );
27
+ }
28
+
29
+ /**
30
+ * Decide the EXPLICIT computer-use tool transport for THIS turn.
31
+ *
32
+ * Computer-use is ordinary `computer_*` function tools bound to the live
33
+ * desktop. The worker picks the mode from the resolved provider so the runtime
34
+ * never string-sniffs the model instance:
35
+ * • catalogue `inputModalities` must include `image`, or computer-use is off
36
+ * • Responses wires (Azure/OpenAI, Gateway, Codex, legacy client) →
37
+ * "function-image": screenshot results are structured `{type:'image'}`
38
+ * • Chat Completions → "disabled": tool results on that wire are text, so a
39
+ * screenshot would become a base64 string rather than an image the model sees
40
+ *
41
+ * Pure + exported so the mapping is unit-testable without a live turn.
42
+ */
43
+ export function computerToolModeForTurn(
44
+ resolvedModel: {
45
+ provider: { kind: RegistryProviderKind; api: ModelProviderApi };
46
+ configured: { capabilities: { inputModalities: string[] } };
47
+ } | null,
48
+ ): ComputerToolMode {
49
+ if (!resolvedModel) {
50
+ return "function-image";
51
+ }
52
+ if (!modelSupportsImageInputForTurn(resolvedModel)) {
53
+ return "disabled";
54
+ }
55
+ if (resolvedModel.provider.kind === "codex-subscription") {
56
+ return "function-image";
57
+ }
58
+ if (resolvedModel.provider.api === "chat") {
59
+ return "disabled";
60
+ }
61
+ return "function-image";
62
+ }
63
+
64
+ /** Gateway models do not advertise OpenAI's hosted apply_patch/view_image tool types. */
65
+ export function structuredToolTransportForTurn(
66
+ resolvedModel: { provider: { kind: RegistryProviderKind } } | null,
67
+ ): boolean {
68
+ if (!resolvedModel) return true;
69
+ return ![
70
+ "codex-subscription",
71
+ "xai-subscription",
72
+ "vercel-gateway-managed",
73
+ "vercel-gateway-workspace",
74
+ ].includes(resolvedModel.provider.kind);
75
+ }
76
+
77
+ /**
78
+ * Progressive tool disclosure is universal for supported OpenGeni turns; only
79
+ * its contained transport differs. Codex keeps its native path, built-in direct
80
+ * OpenAI/Azure Responses use native client tool search, and every other ordinary
81
+ * function-calling provider uses OpenGeni's stable search/invoke dispatcher.
82
+ */
83
+ export function lazyToolTransportForTurn(
84
+ resolvedModel: {
85
+ provider: {
86
+ id: string;
87
+ kind: RegistryProviderKind;
88
+ api: ModelProviderApi;
89
+ wireProfile: "openai" | "azure-openai";
90
+ builtin: boolean;
91
+ baseUrl?: string | undefined;
92
+ };
93
+ } | null,
94
+ ): LazyToolTransport {
95
+ if (!resolvedModel) return "openai_native";
96
+ const provider = resolvedModel.provider;
97
+ if (provider.kind === "codex-subscription") return "codex_native";
98
+ const isNativeResponsesProvider =
99
+ provider.api === "responses" &&
100
+ (provider.wireProfile === "azure-openai" ||
101
+ (provider.builtin && provider.id === "openai" && provider.baseUrl === undefined));
102
+ if (isNativeResponsesProvider) {
103
+ return "openai_native";
104
+ }
105
+ return "generic_dispatch";
106
+ }
107
+
108
+ /** Only brand-new lazy-capable turns may overlap non-eager tool preparation. */
109
+ export function shouldDeferNonEagerToolPreparation(args: {
110
+ lazyToolTransport: LazyToolTransport | null;
111
+ progressiveDisclosureEnabled: boolean;
112
+ artifactRuntimeAvailable: boolean;
113
+ triggerKind: "next" | "approval";
114
+ triggerType: string;
115
+ }): boolean {
116
+ return Boolean(
117
+ args.lazyToolTransport &&
118
+ args.progressiveDisclosureEnabled &&
119
+ !args.artifactRuntimeAvailable &&
120
+ args.triggerKind === "next" &&
121
+ (args.triggerType === "user.message" || args.triggerType === "system.update.delivered"),
122
+ );
123
+ }
124
+
125
+ /**
126
+ * Native web search is a runtime capability, not part of the session's MCP
127
+ * allow-list. Attach it whenever the resolved provider advertises runnable
128
+ * support. The null model is the legacy built-in Responses path, whose
129
+ * deployment flag is its provider capability gate. There is deliberately no
130
+ * cross-provider or sandbox/curl fallback.
131
+ */
132
+ export function hostedWebSearchForTurn(
133
+ resolvedModel: { configured: { hostedWebSearch: boolean } } | null,
134
+ deploymentWebSearchEnabled: boolean,
135
+ ): boolean {
136
+ return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
137
+ }
138
+
139
+ /**
140
+ * Image generation is an optional connected-account capability. A delegated
141
+ * subscription may still authorize the text model without carrying the local
142
+ * credential identity required for paid image operations; that must narrow the
143
+ * tool catalog, not reject an otherwise valid turn.
144
+ */
145
+ export function connectedSubscriptionImageGenerationAuthority<T>(
146
+ credentialContext: T | null | undefined,
147
+ credentialId: string | null | undefined,
148
+ ): { credentialContext: T; credentialId: string } | null {
149
+ if (credentialContext == null || typeof credentialId !== "string" || credentialId.length === 0)
150
+ return null;
151
+ return { credentialContext, credentialId };
152
+ }
153
+
154
+ /** Direct OpenAI hosted image IDs are reusable only by the exact API credential. */
155
+ export function openAiHostedImageProviderBindingForTurn(
156
+ settings: Pick<Settings, "openaiProvider" | "openaiApiKey" | "openaiBaseUrl">,
157
+ resolvedModel: {
158
+ provider: {
159
+ id: string;
160
+ kind: RegistryProviderKind;
161
+ builtin: boolean;
162
+ baseUrl?: string | undefined;
163
+ };
164
+ configured: {
165
+ capabilities: {
166
+ hostedTools: { imageGeneration: { runnable: boolean } };
167
+ };
168
+ };
169
+ } | null,
170
+ ): { providerId: string; providerBindingHash: string } | null {
171
+ if (!resolvedModel?.configured.capabilities.hostedTools.imageGeneration.runnable) return null;
172
+ const providerId = resolvedModel.provider.id;
173
+ const isDirectOpenAi =
174
+ resolvedModel.provider.builtin &&
175
+ resolvedModel.provider.id === "openai" &&
176
+ isDirectOpenAiApiBaseUrl(resolvedModel.provider.baseUrl) &&
177
+ settings.openaiProvider === "openai" &&
178
+ isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl);
179
+ const bindingIdentity = isDirectOpenAi ? settings.openaiApiKey : null;
180
+ if (!bindingIdentity) return null;
181
+ return {
182
+ providerId,
183
+ providerBindingHash: imageProviderBindingHash(providerId, bindingIdentity),
184
+ };
185
+ }
186
+
187
+ /** Exact model-catalog modality gate; null preserves the legacy built-in path. */
188
+ export function modelSupportsImageInputForTurn(
189
+ resolvedModel: {
190
+ configured: { capabilities?: { inputModalities: string[] } };
191
+ } | null,
192
+ ): boolean {
193
+ return (
194
+ resolvedModel === null ||
195
+ resolvedModel.configured.capabilities?.inputModalities.includes("image") !== false
196
+ );
197
+ }
198
+
199
+ /** Image support is catalogue-driven; document bytes are never `input_file`. */
200
+ export function modelAttachmentInputPolicyForTurn(
201
+ resolvedModel: {
202
+ provider: { api: ModelProviderApi };
203
+ configured: {
204
+ capabilities?: {
205
+ inputModalities: string[];
206
+ inputFileMediaTypes?: string[];
207
+ };
208
+ };
209
+ } | null,
210
+ ): ModelAttachmentInputPolicy {
211
+ const typedTransport = resolvedModel === null || resolvedModel.provider.api === "responses";
212
+ return {
213
+ supportsImageInput: typedTransport && modelSupportsImageInputForTurn(resolvedModel),
214
+ inputFileMediaTypes: [],
215
+ };
216
+ }
@@ -0,0 +1,265 @@
1
+ import { createHash } from "node:crypto";
2
+ import {
3
+ TOOL_RESULT_SPILL_MEDIA_TYPE,
4
+ TOOL_RESULT_SPILL_MOUNT_PATH,
5
+ toolResultSpillFilename,
6
+ toolResultSpillSandboxPath,
7
+ type AttemptToolResult as AttemptToolResultValue,
8
+ type FileAsset,
9
+ type ToolResultSpilledReceipt,
10
+ } from "@opengeni/contracts";
11
+ import {
12
+ completeFileUpload,
13
+ getFile,
14
+ getMaterializedSandboxFileResources,
15
+ markSandboxFileResourcesMaterialized,
16
+ prepareGeneratedWorkspaceFile,
17
+ type Database,
18
+ } from "@opengeni/db";
19
+ import type { Observability } from "@opengeni/observability";
20
+ import {
21
+ materializeSandboxFileDownloads,
22
+ sandboxRunAs,
23
+ spilledModelToolResult,
24
+ type CodemodeTokenWriterSession,
25
+ type SandboxFileDownload,
26
+ type TurnToolCancellationFence,
27
+ } from "@opengeni/runtime";
28
+ import type { Settings } from "@opengeni/config";
29
+ import { type ObjectStorage } from "@opengeni/storage";
30
+ import { objectStorageForSandboxDownloads } from "./file-resources";
31
+ import type { ResumedTurnSandbox } from "../../sandbox-resume";
32
+
33
+ const UPLOAD_INTENT_TTL_MS = 60 * 60_000;
34
+
35
+ export type ToolResultSpillReceipt = ToolResultSpilledReceipt;
36
+
37
+ export type ToolResultSpillDeps = {
38
+ db: Database;
39
+ objectStorage: ObjectStorage | null;
40
+ observability: Observability;
41
+ accountId: string;
42
+ workspaceId: string;
43
+ attemptId: string;
44
+ getModelRunSettings: () => Settings;
45
+ getSandboxFileDownloadBackend: () => Settings["sandboxBackend"];
46
+ getPublish: () =>
47
+ | ((events: Array<{ type: string; payload: unknown }>, immediate?: boolean) => Promise<unknown>)
48
+ | null;
49
+ toolCancellationFenceRef: { current: TurnToolCancellationFence | null };
50
+ getResolvedSandbox: () => ResumedTurnSandbox | null;
51
+ getSetupBoxSession: () => unknown;
52
+ getSdkOwnedSandboxSession: () => unknown;
53
+ getSandboxGroupId: () => string | null;
54
+ runWorkspaceMutation: <T>(
55
+ sandbox: ResumedTurnSandbox,
56
+ operation: string,
57
+ mutation: () => Promise<T>,
58
+ ) => Promise<T>;
59
+ };
60
+
61
+ export class ToolResultSpill {
62
+ readonly receiptsCreatedThisTurn = new Map<string, ToolResultSpillReceipt>();
63
+ private materializationCache: { instanceId: string; fileIds: Set<string> } | null = null;
64
+
65
+ constructor(private readonly deps: ToolResultSpillDeps) {}
66
+
67
+ spill = async (input: {
68
+ operationId: string;
69
+ result: AttemptToolResultValue;
70
+ }): Promise<AttemptToolResultValue> => {
71
+ const { objectStorage } = this.deps;
72
+ if (!objectStorage) {
73
+ throw new Error("Oversized tool-result spill requires configured object storage");
74
+ }
75
+ const bytes = Buffer.from(JSON.stringify(input.result) ?? "null");
76
+ const sha256 = createHash("sha256").update(bytes).digest("hex");
77
+ const identity = toolResultSpillIdentity({
78
+ workspaceId: this.deps.workspaceId,
79
+ attemptId: this.deps.attemptId,
80
+ operationId: input.operationId,
81
+ });
82
+ const filename = toolResultSpillFilename(input.operationId);
83
+ const intendedSandboxPath = toolResultSpillSandboxPath(filename);
84
+ const objectKey = `workspaces/${this.deps.workspaceId}/files/${identity.fileId}/tool-results/${filename}`;
85
+ const prepared = await prepareGeneratedWorkspaceFile(this.deps.db, {
86
+ accountId: this.deps.accountId,
87
+ workspaceId: this.deps.workspaceId,
88
+ fileId: identity.fileId,
89
+ uploadId: identity.uploadId,
90
+ filename,
91
+ safeFilename: filename,
92
+ contentType: TOOL_RESULT_SPILL_MEDIA_TYPE,
93
+ sizeBytes: bytes.byteLength,
94
+ sha256,
95
+ bucket: objectStorage.bucket,
96
+ objectKey,
97
+ expiresAt: new Date(Date.now() + UPLOAD_INTENT_TTL_MS),
98
+ });
99
+ let file = prepared.file;
100
+ if (file.status !== "ready") {
101
+ if (!(await objectStorage.fileExists(file))) {
102
+ await objectStorage.putObject({
103
+ key: objectKey,
104
+ contentType: TOOL_RESULT_SPILL_MEDIA_TYPE,
105
+ body: bytes,
106
+ sha256,
107
+ });
108
+ }
109
+ file = await completeFileUpload(this.deps.db, this.deps.workspaceId, identity.uploadId);
110
+ }
111
+ const materialized = await this.materializeFile(file);
112
+ const receipt: ToolResultSpillReceipt = {
113
+ type: "tool_result_spilled",
114
+ sandboxPath: materialized ? intendedSandboxPath : null,
115
+ fileId: file.id,
116
+ byteSize: file.sizeBytes,
117
+ mediaType: TOOL_RESULT_SPILL_MEDIA_TYPE,
118
+ };
119
+ this.receiptsCreatedThisTurn.set(file.id, {
120
+ ...receipt,
121
+ sandboxPath: intendedSandboxPath,
122
+ });
123
+ return spilledModelToolResult(receipt);
124
+ };
125
+
126
+ materializeDeferred = async (): Promise<void> => {
127
+ for (const receipt of this.receiptsCreatedThisTurn.values()) {
128
+ const file = await getFile(this.deps.db, this.deps.workspaceId, receipt.fileId);
129
+ if (file) await this.materializeFile(file);
130
+ }
131
+ };
132
+
133
+ private async materializeFile(file: FileAsset): Promise<boolean> {
134
+ if (this.deps.getSandboxFileDownloadBackend() === "none") return false;
135
+ try {
136
+ const sandbox = this.deps.getResolvedSandbox();
137
+ const setupBoxSession = this.deps.getSetupBoxSession();
138
+ if (sandbox && setupBoxSession) {
139
+ return await this.materializeInSandbox(file, sandbox, setupBoxSession);
140
+ }
141
+ const sdkOwned = this.deps.getSdkOwnedSandboxSession();
142
+ if (sdkOwned) {
143
+ return await this.writeDownload(sdkOwned, await this.prepareDownload(file));
144
+ }
145
+ return false;
146
+ } catch (error) {
147
+ this.deps.observability.warn("Tool result spill sandbox materialization deferred", {
148
+ errorClass: error instanceof Error ? error.name : "UnknownError",
149
+ errorCode: "worker_operation_failed",
150
+ origin: "worker",
151
+ });
152
+ return false;
153
+ }
154
+ }
155
+
156
+ private async materializeInSandbox(
157
+ file: FileAsset,
158
+ sandbox: ResumedTurnSandbox,
159
+ session: unknown,
160
+ ): Promise<boolean> {
161
+ const download = await this.prepareDownload(file);
162
+ if (this.materializationCache?.instanceId !== sandbox.established.instanceId) {
163
+ const sandboxGroupId = this.deps.getSandboxGroupId();
164
+ const fileIds = sandboxGroupId
165
+ ? await getMaterializedSandboxFileResources(this.deps.db, {
166
+ accountId: this.deps.accountId,
167
+ workspaceId: this.deps.workspaceId,
168
+ sandboxGroupId,
169
+ expectedEpoch: sandbox.leaseEpoch,
170
+ instanceId: sandbox.established.instanceId,
171
+ })
172
+ : new Set<string>();
173
+ this.materializationCache = {
174
+ instanceId: sandbox.established.instanceId,
175
+ fileIds,
176
+ };
177
+ }
178
+ if (this.materializationCache.fileIds.has(file.id)) return true;
179
+ await this.deps.runWorkspaceMutation(sandbox, "toolResultSpillMaterialization", async () => {
180
+ const ok = await this.writeDownload(session, download);
181
+ if (!ok) throw new Error("Tool result spill sandbox copy failed");
182
+ });
183
+ this.materializationCache.fileIds.add(file.id);
184
+ const sandboxGroupId = this.deps.getSandboxGroupId();
185
+ if (sandboxGroupId) {
186
+ await markSandboxFileResourcesMaterialized(this.deps.db, {
187
+ accountId: this.deps.accountId,
188
+ workspaceId: this.deps.workspaceId,
189
+ sandboxGroupId,
190
+ expectedEpoch: sandbox.leaseEpoch,
191
+ instanceId: sandbox.established.instanceId,
192
+ fileIds: [file.id],
193
+ });
194
+ }
195
+ return true;
196
+ }
197
+
198
+ private async prepareDownload(file: FileAsset): Promise<SandboxFileDownload> {
199
+ const { objectStorage } = this.deps;
200
+ if (!objectStorage) {
201
+ throw new Error("Oversized tool-result spill requires configured object storage");
202
+ }
203
+ const settings = this.deps.getModelRunSettings();
204
+ const downloadStorage = objectStorageForSandboxDownloads(
205
+ settings,
206
+ objectStorage,
207
+ this.deps.getSandboxFileDownloadBackend(),
208
+ );
209
+ const signed = await downloadStorage.createGetUrl({ key: file.objectKey });
210
+ return {
211
+ fileId: file.id,
212
+ mountPath: TOOL_RESULT_SPILL_MOUNT_PATH,
213
+ filename: file.safeFilename,
214
+ url: signed.url,
215
+ expiresAt: signed.expiresAt,
216
+ sizeBytes: file.sizeBytes,
217
+ ...(file.sha256 ? { sha256: file.sha256 } : {}),
218
+ };
219
+ }
220
+
221
+ private async writeDownload(session: unknown, download: SandboxFileDownload): Promise<boolean> {
222
+ const runAs = sandboxRunAs(this.deps.getModelRunSettings());
223
+ const publish = this.deps.getPublish();
224
+ const fence = this.deps.toolCancellationFenceRef.current;
225
+ const materialized = await materializeSandboxFileDownloads(
226
+ session as CodemodeTokenWriterSession,
227
+ [download],
228
+ {
229
+ onRuntimeEvent: async (event) => {
230
+ await publish?.([{ type: event.type, payload: event.payload }], true);
231
+ },
232
+ ...(runAs ? { runAs } : {}),
233
+ ...(fence ? { commandRunner: fence.runSandboxCommand.bind(fence) } : {}),
234
+ },
235
+ );
236
+ return materialized.failures.length === 0;
237
+ }
238
+ }
239
+
240
+ export function toolResultSpillIdentity(input: {
241
+ workspaceId: string;
242
+ attemptId: string;
243
+ operationId: string;
244
+ }): { fileId: string; uploadId: string } {
245
+ const digest = createHash("sha256")
246
+ .update("opengeni-tool-result-spill-v1\0")
247
+ .update(input.workspaceId)
248
+ .update("\0")
249
+ .update(input.attemptId)
250
+ .update("\0")
251
+ .update(input.operationId.toLowerCase())
252
+ .digest("hex");
253
+ return {
254
+ fileId: uuidFromDigest(digest, 0),
255
+ uploadId: uuidFromDigest(digest, 16),
256
+ };
257
+ }
258
+
259
+ function uuidFromDigest(digest: string, startByte: number): string {
260
+ const bytes = Buffer.from(digest, "hex").subarray(startByte, startByte + 16);
261
+ bytes[6] = ((bytes[6] ?? 0) & 0x0f) | 0x40;
262
+ bytes[8] = ((bytes[8] ?? 0) & 0x3f) | 0x80;
263
+ const hex = bytes.toString("hex");
264
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
265
+ }