@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,802 @@
1
+ import {
2
+ selectXaiCredentialForUse,
3
+ materializeXaiCredentialForRun,
4
+ resolveXaiProviderAccountAuthoritySnapshotForAcceptance,
5
+ getXaiSessionAccountPin,
6
+ setXaiSessionAccountPin,
7
+ countSessionHistoryItems,
8
+ getWorkspaceVideoGenerationPolicy,
9
+ loadWorkspaceVercelAiGatewayCredentialLease,
10
+ beginConnectorActionExecution,
11
+ completeConnectorActionExecution,
12
+ prepareConnectorActionApproval,
13
+ } from "@opengeni/db";
14
+ import {
15
+ type AttemptConnectorActionBinding,
16
+ type BuildAgentOptions,
17
+ type ConnectorActionPolicyHooks,
18
+ type SandboxFileDownload,
19
+ type TurnSandboxCommandSession,
20
+ } from "@opengeni/runtime";
21
+ import {
22
+ serviceTierForLatencyMode,
23
+ environmentsEncryptionKeyBytes,
24
+ WORKSPACE_GATEWAY_PROVIDER_ID,
25
+ resolveModelProvider,
26
+ type Settings,
27
+ } from "@opengeni/config";
28
+ import { type CodexRequestContext } from "@opengeni/codex";
29
+ import { executeXaiSubscriptionImageGeneration } from "../xai-image-generation";
30
+ import { rigProviderImageContentHash, videoGenerationCapabilitiesForPolicy } from "@opengeni/core";
31
+ import {
32
+ admitVideoGenerationRequest,
33
+ managedVideoGenerationCredentialLease,
34
+ xaiVideoGenerationCredentialLease,
35
+ type VideoGenerationCredentialLease,
36
+ } from "../video-generation-admission";
37
+ import { VideoReferenceInputError } from "../video-reference-staging";
38
+ import { rigProviderImageSourceImage } from "../packs";
39
+ import type { TurnActivityServices as ActivityServices, RunAgentTurnInput } from "../types";
40
+ import { recordTurnStartupPhase } from "../../observability-metrics";
41
+ import {
42
+ modelVisibleCompanyBrainSkillActivations,
43
+ summarizeCompanyBrainContributions,
44
+ } from "../../model-context-contributions";
45
+ import { createTurnCredentialLeases } from "./credential-leases";
46
+ import { createTurnMediaArtifacts } from "./media-artifacts";
47
+ import { executeGatewayImageGeneration } from "../gateway-image-generation";
48
+ import { executeCodexImageGeneration } from "../codex-image-generation";
49
+ import { resolveImageGenerationReferences } from "../image-generation-references";
50
+ import { SandboxChannelAService, type ChannelASession } from "@opengeni/runtime/sandbox";
51
+ import { sandboxRunAs } from "@opengeni/runtime";
52
+ import { VideoGenerationRejectedResult } from "@opengeni/contracts";
53
+
54
+ import {
55
+ computerToolModeForTurn,
56
+ structuredToolTransportForTurn,
57
+ hostedWebSearchForTurn,
58
+ connectedSubscriptionImageGenerationAuthority,
59
+ } from "./tool-policy";
60
+ import type { ClaimTurnOk } from "./claim";
61
+ import type { GovernanceModelOk } from "./governance-model";
62
+ import type { CompactionPrepOk } from "./compaction-prep";
63
+ import type { SandboxTurnRuntime } from "./sandbox-runtime";
64
+ import type { runtimeResourcesForTurn } from "./file-resources";
65
+ import type { sandboxArtifactRuntimeAdmission } from "./sandbox-route";
66
+ import type {
67
+ loadWorkspaceEnvironmentForRunWithCredentials,
68
+ sandboxEnvironmentForRun,
69
+ } from "../environment";
70
+ import type {
71
+ AttemptIdentityState,
72
+ EventingState,
73
+ ProviderTurnState,
74
+ RecordingState,
75
+ SandboxRuntimeState,
76
+ } from "./turn-context";
77
+
78
+ export type BuildTurnAgentDeps = {
79
+ input: RunAgentTurnInput;
80
+ db: ActivityServices["db"];
81
+ runtime: ActivityServices["runtime"];
82
+ objectStorage: ActivityServices["objectStorage"];
83
+ observability: ActivityServices["observability"];
84
+ cancellationSignal: AbortSignal | undefined;
85
+ runtimeCancellationSignal: AbortSignal | undefined;
86
+ eventing: EventingState;
87
+ attempt: AttemptIdentityState;
88
+ sandboxState: SandboxRuntimeState;
89
+ recordingState: RecordingState;
90
+ maybeStartOnTurnRecording: SandboxTurnRuntime["maybeStartOnTurnRecording"];
91
+ providerTurn: ProviderTurnState;
92
+ media: ReturnType<typeof createTurnMediaArtifacts>;
93
+ leases: ReturnType<typeof createTurnCredentialLeases>;
94
+ turn: ClaimTurnOk["turn"];
95
+ session: ClaimTurnOk["session"];
96
+ fileAuthoritySubjectId: ClaimTurnOk["fileAuthoritySubjectId"];
97
+ capabilitySettings: ClaimTurnOk["capabilitySettings"];
98
+ humanInputResume: ClaimTurnOk["humanInputResume"];
99
+ turnExecutionPolicy: ClaimTurnOk["turnExecutionPolicy"];
100
+ runSettings: GovernanceModelOk["runSettings"];
101
+ logicalSandboxSettings: GovernanceModelOk["logicalSandboxSettings"];
102
+ verifiedRigProviderImageId: GovernanceModelOk["verifiedRigProviderImageId"];
103
+ resolvedModel: GovernanceModelOk["resolvedModel"];
104
+ nativeImageProviderBinding: GovernanceModelOk["nativeImageProviderBinding"];
105
+ lazyToolTransport: GovernanceModelOk["lazyToolTransport"];
106
+ modelInputPolicy: GovernanceModelOk["modelInputPolicy"];
107
+ supportsImageInput: GovernanceModelOk["supportsImageInput"];
108
+ agentHumanInputEnabled: GovernanceModelOk["agentHumanInputEnabled"];
109
+ workspaceAgentInstructions: GovernanceModelOk["workspaceAgentInstructions"];
110
+ workspaceGovernance: GovernanceModelOk["workspaceGovernance"];
111
+ structuredWorkspacePolicyActive: GovernanceModelOk["structuredWorkspacePolicyActive"];
112
+ workspaceMemory: GovernanceModelOk["workspaceMemory"];
113
+ rigVersion: GovernanceModelOk["rigVersion"];
114
+ rigName: GovernanceModelOk["rigName"];
115
+ packRuntime: GovernanceModelOk["packRuntime"];
116
+ installedSkillRuntime: GovernanceModelOk["installedSkillRuntime"];
117
+ buildCompanyBrainContributionReceiptFor: GovernanceModelOk["buildCompanyBrainContributionReceiptFor"];
118
+ promptCacheKey: CompactionPrepOk["promptCacheKey"];
119
+ workspaceVariableSet: Awaited<ReturnType<typeof loadWorkspaceEnvironmentForRunWithCredentials>>;
120
+ runtimeResources: ReturnType<typeof runtimeResourcesForTurn>;
121
+ sandboxEnvironment: Record<string, string>;
122
+ sandboxArtifactRuntime: ReturnType<typeof sandboxArtifactRuntimeAdmission>;
123
+ sandboxGitToken: string | undefined;
124
+ sandboxGitTokens: Record<string, string> | undefined;
125
+ sandboxGitCredentialBindings: Awaited<
126
+ ReturnType<typeof sandboxEnvironmentForRun>
127
+ >["gitCredentialBindings"];
128
+ sandboxCodemodeToken: string | undefined;
129
+ fileResourceDownloads: SandboxFileDownload[];
130
+ attemptConnectorActionBindings: readonly AttemptConnectorActionBinding[];
131
+ connectorActionIdentity: {
132
+ accountId: string;
133
+ workspaceId: string;
134
+ sessionId: string;
135
+ turnId: string;
136
+ attemptId: string;
137
+ executionGeneration: number;
138
+ initiator: Pick<ClaimTurnOk["turn"]["initiator"], "kind" | "subjectId">;
139
+ };
140
+ videoGenerationAcceptancesByCallId: Map<string, { operationId: string; requestDigest: string }>;
141
+ activeSandboxBackend: Settings["sandboxBackend"] | undefined;
142
+ groupBoxBackend: Settings["sandboxBackend"];
143
+ postToolPreparationStartedAt: number;
144
+ codexContext: CodexRequestContext | null;
145
+ };
146
+
147
+ export async function buildTurnAgent(deps: BuildTurnAgentDeps) {
148
+ const {
149
+ input,
150
+ db,
151
+ runtime,
152
+ objectStorage,
153
+ observability,
154
+ cancellationSignal,
155
+ runtimeCancellationSignal,
156
+ eventing,
157
+ attempt,
158
+ sandboxState,
159
+ recordingState,
160
+ maybeStartOnTurnRecording,
161
+ providerTurn,
162
+ media,
163
+ leases,
164
+ turn,
165
+ session,
166
+ fileAuthoritySubjectId,
167
+ capabilitySettings,
168
+ humanInputResume,
169
+ turnExecutionPolicy,
170
+ runSettings,
171
+ logicalSandboxSettings,
172
+ verifiedRigProviderImageId,
173
+ resolvedModel,
174
+ nativeImageProviderBinding,
175
+ lazyToolTransport,
176
+ modelInputPolicy,
177
+ supportsImageInput,
178
+ agentHumanInputEnabled,
179
+ workspaceAgentInstructions,
180
+ workspaceGovernance,
181
+ structuredWorkspacePolicyActive,
182
+ workspaceMemory,
183
+ rigVersion,
184
+ rigName,
185
+ packRuntime,
186
+ installedSkillRuntime,
187
+ buildCompanyBrainContributionReceiptFor,
188
+ promptCacheKey,
189
+ workspaceVariableSet,
190
+ runtimeResources,
191
+ sandboxEnvironment,
192
+ sandboxArtifactRuntime,
193
+ sandboxGitToken,
194
+ sandboxGitTokens,
195
+ sandboxGitCredentialBindings,
196
+ sandboxCodemodeToken,
197
+ fileResourceDownloads,
198
+ attemptConnectorActionBindings,
199
+ connectorActionIdentity,
200
+ videoGenerationAcceptancesByCallId,
201
+ activeSandboxBackend,
202
+ groupBoxBackend,
203
+ postToolPreparationStartedAt,
204
+ codexContext,
205
+ } = deps;
206
+ const preparedTools = eventing.preparedTools!;
207
+
208
+ const isGenesisTurn =
209
+ attempt.triggerType === "user.message" &&
210
+ (await countSessionHistoryItems(db, input.workspaceId, input.sessionId)) === 0;
211
+ // Clone-onto-real-disk hazard (Case B). A session keeps its CLOUD HOME
212
+ // backend (runSettings.sandboxBackend, e.g. "modal") but its ACTIVE sandbox
213
+ // may have been swapped to a connected machine (active_sandbox_id → a
214
+ // selfhosted lease). buildAgent's repository-clone lifecycle hook keys off
215
+ // the EFFECTIVE backend; if we let it default to the home backend it would
216
+ // `git clone` a private GitHub-App repo onto the user's REAL disk. So pass
217
+ // "selfhosted" through when the active sandbox is a connected machine;
218
+ // otherwise leave it undefined so buildAgent defaults to the home backend
219
+ // (byte-for-byte unchanged cloud behavior). `activeSandboxBackend` was
220
+ // resolved ONCE at turn start (above) via resolveActiveSandboxBackend (the
221
+ // tested gate) and is reused here — resolving once is correct because the
222
+ // clone hook runs at beforeAgentStart, so a mid-turn swap can't affect it.
223
+ // buildAgent's option key is `workspaceEnvironment` (internal runtime
224
+ // symbol; the product concept is a variable set). Built as a TYPED const —
225
+ // a direct literal assignment to Pick<BuildAgentOptions,...> IS excess-
226
+ // property-checked, so a wrong key fails tsc. A bare conditional spread
227
+ // inside the options literal is NOT checked, which is exactly how the M1
228
+ // key regression (workspaceVariableSet vs workspaceEnvironment) slipped
229
+ // through and silently dropped the variable-set instructions block.
230
+ const workspaceEnvironmentOption: Pick<BuildAgentOptions, "workspaceEnvironment"> =
231
+ workspaceVariableSet
232
+ ? {
233
+ workspaceEnvironment: {
234
+ name: workspaceVariableSet.name,
235
+ description: workspaceVariableSet.description,
236
+ variableNames: Object.keys(workspaceVariableSet.values),
237
+ },
238
+ }
239
+ : {};
240
+ const hostedWebSearch = hostedWebSearchForTurn(resolvedModel, runSettings.webSearchEnabled);
241
+ const resolveImageReferences = async (
242
+ references: Parameters<typeof resolveImageGenerationReferences>[0]["references"],
243
+ ) =>
244
+ await resolveImageGenerationReferences({
245
+ db,
246
+ objectStorage: objectStorage!,
247
+ accountId: input.accountId,
248
+ workspaceId: input.workspaceId,
249
+ subjectId: fileAuthoritySubjectId,
250
+ references,
251
+ readSandboxFile: async (path, maxBytes) => {
252
+ const imageReferenceSession = (sandboxState.setupBoxSession ??
253
+ media.sdkOwnedSandboxSession) as ChannelASession | null;
254
+ if (!imageReferenceSession) {
255
+ throw new Error("Sandbox image reference is unavailable");
256
+ }
257
+ const relativePath = path.slice("/workspace/".length);
258
+ const referenceRunAs = sandboxRunAs(eventing.modelRunSettings);
259
+ const channel = new SandboxChannelAService({
260
+ session: imageReferenceSession,
261
+ workspaceRoot: "/workspace",
262
+ leaseEpoch: sandboxState.resolvedSandbox?.leaseEpoch ?? 0,
263
+ ...(referenceRunAs ? { runAs: referenceRunAs } : {}),
264
+ });
265
+ const read = await channel.fsRead({
266
+ path: relativePath,
267
+ encoding: "base64",
268
+ maxBytes,
269
+ });
270
+ if (read.truncated) throw new Error("Sandbox image reference exceeds the byte limit");
271
+ return Uint8Array.from(Buffer.from(read.content, "base64"));
272
+ },
273
+ });
274
+ const imageGenerationOption: Pick<BuildAgentOptions, "imageGeneration"> = (() => {
275
+ // Never expose a paid image operation unless its permanent artifact can
276
+ // be committed. Failing after provider execution would leave an
277
+ // unrecoverable outcome-unknown operation with no user-visible image.
278
+ if (!objectStorage) return {};
279
+ if (nativeImageProviderBinding) {
280
+ media.nativeImageGenerationRetention = {
281
+ ...nativeImageProviderBinding,
282
+ sessionId: input.sessionId,
283
+ turnId: turn.id,
284
+ attemptId: input.attemptId,
285
+ };
286
+ return { imageGeneration: { kind: "native_hosted" } };
287
+ }
288
+
289
+ if (resolvedModel?.provider.kind === "codex-subscription") {
290
+ const imageAuthority = connectedSubscriptionImageGenerationAuthority(
291
+ codexContext,
292
+ providerTurn.effectiveCodexCredentialId,
293
+ );
294
+ if (!imageAuthority) return {};
295
+ return {
296
+ imageGeneration: {
297
+ kind: "provider_adapter",
298
+ execute: async ({ prompt, references }, { toolCallId }) => {
299
+ const resolvedReferences = await resolveImageReferences(references);
300
+ const receipt = await executeCodexImageGeneration({
301
+ db,
302
+ objectStorage,
303
+ accountId: input.accountId,
304
+ workspaceId: input.workspaceId,
305
+ sessionId: input.sessionId,
306
+ turnId: turn.id,
307
+ attemptId: input.attemptId,
308
+ toolCallId,
309
+ prompt,
310
+ references: resolvedReferences,
311
+ credentialId: imageAuthority.credentialId,
312
+ codexContext: imageAuthority.credentialContext,
313
+ ...(runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}),
314
+ });
315
+ media.rememberGeneratedImageCreatedThisTurn(receipt);
316
+ await media.materializeGeneratedImage(receipt);
317
+ return receipt;
318
+ },
319
+ },
320
+ };
321
+ }
322
+
323
+ if (resolvedModel?.provider.kind === "xai-subscription") {
324
+ const imageAuthority = connectedSubscriptionImageGenerationAuthority(
325
+ providerTurn.xaiRequestContext,
326
+ providerTurn.effectiveXaiCredentialId,
327
+ );
328
+ if (!imageAuthority) return {};
329
+ return {
330
+ imageGeneration: {
331
+ kind: "provider_adapter",
332
+ execute: async ({ prompt, references }, { toolCallId }) => {
333
+ const resolvedReferences = await resolveImageReferences(references);
334
+ const receipt = await executeXaiSubscriptionImageGeneration({
335
+ db,
336
+ objectStorage,
337
+ accountId: input.accountId,
338
+ workspaceId: input.workspaceId,
339
+ sessionId: input.sessionId,
340
+ turnId: turn.id,
341
+ attemptId: input.attemptId,
342
+ toolCallId,
343
+ prompt,
344
+ references: resolvedReferences,
345
+ credentialId: imageAuthority.credentialId,
346
+ xaiContext: imageAuthority.credentialContext,
347
+ ...(runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}),
348
+ });
349
+ media.rememberGeneratedImageCreatedThisTurn(receipt);
350
+ await media.materializeGeneratedImage(receipt);
351
+ return receipt;
352
+ },
353
+ },
354
+ };
355
+ }
356
+
357
+ const gatewayResolution = resolveModelProvider(
358
+ capabilitySettings,
359
+ WORKSPACE_GATEWAY_PROVIDER_ID,
360
+ );
361
+ const gateway = gatewayResolution?.provider;
362
+ if (gateway?.kind !== "vercel-gateway-workspace" || !gateway.apiKey) return {};
363
+ const gatewayApiKey = gateway.apiKey;
364
+ return {
365
+ imageGeneration: {
366
+ kind: "provider_adapter",
367
+ execute: async ({ prompt, references }, { toolCallId }) => {
368
+ const resolvedReferences = await resolveImageReferences(references);
369
+ const receipt = await executeGatewayImageGeneration({
370
+ db,
371
+ objectStorage,
372
+ accountId: input.accountId,
373
+ workspaceId: input.workspaceId,
374
+ sessionId: input.sessionId,
375
+ turnId: turn.id,
376
+ attemptId: input.attemptId,
377
+ apiKey: gatewayApiKey,
378
+ modelId: capabilitySettings.imageGenerationModel,
379
+ prompt,
380
+ references: resolvedReferences,
381
+ toolCallId,
382
+ ...(runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}),
383
+ });
384
+ media.rememberGeneratedImageCreatedThisTurn(receipt);
385
+ await media.materializeGeneratedImage(receipt);
386
+ return receipt;
387
+ },
388
+ },
389
+ };
390
+ })();
391
+ const videoGenerationPolicy = await getWorkspaceVideoGenerationPolicy(db, input.workspaceId);
392
+ const videoGenerationEnabled =
393
+ videoGenerationPolicy.defaultModelId !== null &&
394
+ videoGenerationPolicy.enabledModelIds.length > 0;
395
+ let videoGenerationCredential: VideoGenerationCredentialLease | null = null;
396
+ if (objectStorage && videoGenerationEnabled) {
397
+ if (videoGenerationPolicy.fundingSource === "opengeni_credits") {
398
+ videoGenerationCredential = managedVideoGenerationCredentialLease(eventing.modelRunSettings);
399
+ } else if (videoGenerationPolicy.fundingSource === "workspace_gateway") {
400
+ const workspaceCredential = await loadWorkspaceVercelAiGatewayCredentialLease(
401
+ db,
402
+ eventing.modelRunSettings,
403
+ input.workspaceId,
404
+ );
405
+ if (workspaceCredential) {
406
+ videoGenerationCredential = {
407
+ fundingSource: "workspace_gateway",
408
+ ...workspaceCredential,
409
+ };
410
+ }
411
+ } else if (videoGenerationPolicy.fundingSource === "supergrok_subscription") {
412
+ const encryptionKey = environmentsEncryptionKeyBytes(eventing.modelRunSettings);
413
+ const subjectId = leases.xai.subjectId ?? turn.initiatingHumanSubjectId;
414
+ if (encryptionKey && subjectId) {
415
+ const authoritySnapshot =
416
+ providerTurn.xaiAuthoritySnapshot ??
417
+ (await resolveXaiProviderAccountAuthoritySnapshotForAcceptance(db, {
418
+ workspaceId: input.workspaceId,
419
+ subjectId,
420
+ }));
421
+ const pin = await getXaiSessionAccountPin(db, {
422
+ workspaceId: input.workspaceId,
423
+ subjectId,
424
+ sessionId: input.sessionId,
425
+ authoritySnapshot,
426
+ });
427
+ const selected = providerTurn.effectiveXaiCredentialId
428
+ ? {
429
+ credentialId: providerTurn.effectiveXaiCredentialId,
430
+ rotationEnabled: providerTurn.xaiRotationEnabled,
431
+ }
432
+ : await selectXaiCredentialForUse(db, {
433
+ accountId: input.accountId,
434
+ workspaceId: input.workspaceId,
435
+ subjectId,
436
+ authoritySnapshot,
437
+ shardKey: input.sessionId,
438
+ pinnedCredentialId: pin?.pinnedCredentialId ?? null,
439
+ pinSource:
440
+ pin?.pinSource === "manual" || pin?.pinSource === "policy" ? pin.pinSource : null,
441
+ });
442
+ if (selected.credentialId) {
443
+ if (
444
+ selected.rotationEnabled &&
445
+ pin?.pinSource !== "manual" &&
446
+ pin?.pinnedCredentialId !== selected.credentialId
447
+ ) {
448
+ await setXaiSessionAccountPin(db, {
449
+ accountId: input.accountId,
450
+ workspaceId: input.workspaceId,
451
+ subjectId,
452
+ sessionId: input.sessionId,
453
+ authoritySnapshot,
454
+ credentialId: selected.credentialId,
455
+ pinSource: "policy",
456
+ expectedVersion: pin?.version ?? null,
457
+ }).catch((error: unknown) => {
458
+ if (error instanceof Error && error.message === "xAI session pin changed") return;
459
+ throw error;
460
+ });
461
+ }
462
+ const credential = await materializeXaiCredentialForRun(db, {
463
+ workspaceId: input.workspaceId,
464
+ subjectId,
465
+ credentialId: selected.credentialId,
466
+ authoritySnapshot,
467
+ encryptionKey,
468
+ });
469
+ videoGenerationCredential = xaiVideoGenerationCredentialLease({
470
+ settings: eventing.modelRunSettings,
471
+ credential,
472
+ subjectId,
473
+ authoritySnapshot,
474
+ });
475
+ }
476
+ }
477
+ }
478
+ }
479
+ const videoGenerationOption: Pick<BuildAgentOptions, "videoGeneration"> = (() => {
480
+ if (
481
+ !objectStorage ||
482
+ eventing.modelRunSettings.sandboxBackend === "none" ||
483
+ !videoGenerationCredential ||
484
+ !videoGenerationEnabled
485
+ ) {
486
+ return {};
487
+ }
488
+ // Parse the frozen capability snapshot before advertising either tool.
489
+ // Invalid or unsupported workspace policy therefore fails closed before
490
+ // it can perturb the model's tool list.
491
+ const capabilities = videoGenerationCapabilitiesForPolicy({
492
+ policy: videoGenerationPolicy,
493
+ credentialVersion: videoGenerationCredential.version,
494
+ });
495
+ return {
496
+ videoGeneration: {
497
+ capabilities: async () => capabilities,
498
+ execute: async (toolInput, { toolCallId }) => {
499
+ const sessionForReference =
500
+ sandboxState.resolvedSandbox?.established.session ?? media.sdkOwnedSandboxSession;
501
+ const fence = eventing.toolCancellationFenceRef.current;
502
+ const runAs = sandboxRunAs(eventing.modelRunSettings);
503
+ let accepted: Awaited<ReturnType<typeof admitVideoGenerationRequest>>;
504
+ try {
505
+ accepted = await admitVideoGenerationRequest({
506
+ db,
507
+ storage: objectStorage,
508
+ settings: eventing.modelRunSettings,
509
+ accountId: input.accountId,
510
+ workspaceId: input.workspaceId,
511
+ sessionId: input.sessionId,
512
+ turnId: turn.id,
513
+ attemptId: input.attemptId,
514
+ toolCallId,
515
+ toolInput,
516
+ policy: videoGenerationPolicy,
517
+ credential: videoGenerationCredential,
518
+ ...(sessionForReference && fence
519
+ ? {
520
+ runCommand: async (command) =>
521
+ await fence.runSandboxCommandStructured(
522
+ sessionForReference as TurnSandboxCommandSession,
523
+ {
524
+ ...command,
525
+ ...(runAs ? { runAs } : {}),
526
+ },
527
+ ),
528
+ }
529
+ : {}),
530
+ ...(runtimeCancellationSignal ? { signal: runtimeCancellationSignal } : {}),
531
+ });
532
+ } catch (error) {
533
+ if (error instanceof VideoReferenceInputError) {
534
+ return VideoGenerationRejectedResult.parse({
535
+ schemaVersion: 1,
536
+ status: "rejected",
537
+ code: error.code,
538
+ message: error.message,
539
+ operationCreated: false,
540
+ });
541
+ }
542
+ throw error;
543
+ }
544
+ videoGenerationAcceptancesByCallId.set(toolCallId, {
545
+ operationId: accepted.operationId,
546
+ requestDigest: accepted.requestDigest,
547
+ });
548
+ return accepted.receipt;
549
+ },
550
+ },
551
+ };
552
+ })();
553
+ const serviceTier = serviceTierForLatencyMode(
554
+ turnExecutionPolicy.providerId,
555
+ turnExecutionPolicy.latencyMode,
556
+ );
557
+ const connectorActionPolicy: ConnectorActionPolicyHooks = {
558
+ prepare: async (call) =>
559
+ await prepareConnectorActionApproval(db, connectorActionIdentity, call),
560
+ begin: async (call) => await beginConnectorActionExecution(db, connectorActionIdentity, call),
561
+ complete: async ({ requestId, outcome }) =>
562
+ await completeConnectorActionExecution(db, {
563
+ accountId: input.accountId,
564
+ workspaceId: input.workspaceId,
565
+ requestId,
566
+ attemptId: input.attemptId,
567
+ outcome,
568
+ }),
569
+ };
570
+ const runtimeSkillActivations = [
571
+ ...installedSkillRuntime.activations,
572
+ ...packRuntime.skillActivations,
573
+ ...session.skills.map((skill) => ({
574
+ source: "session" as const,
575
+ id: `session:${session.id}:${skill.name}`,
576
+ artifact: {
577
+ name: skill.name,
578
+ description: skill.description ?? null,
579
+ files: skill.files.map((file) => ({
580
+ path: file.path,
581
+ content: file.content,
582
+ })),
583
+ },
584
+ reason: "attached to session",
585
+ })),
586
+ ];
587
+ const modelVisibleRuntimeSkillActivations = modelVisibleCompanyBrainSkillActivations(
588
+ eventing.modelRunSettings.sandboxBackend,
589
+ runtimeSkillActivations,
590
+ );
591
+ try {
592
+ eventing.companyBrainContextContributions = summarizeCompanyBrainContributions(
593
+ buildCompanyBrainContributionReceiptFor(modelVisibleRuntimeSkillActivations),
594
+ );
595
+ } catch {
596
+ // Contribution telemetry must never change model execution semantics.
597
+ }
598
+ recordTurnStartupPhase(observability, {
599
+ phase: "post_tool_preparation",
600
+ provider: turnExecutionPolicy.providerId,
601
+ backend: activeSandboxBackend ?? groupBoxBackend,
602
+ outcome: "completed",
603
+ durationSeconds: (performance.now() - postToolPreparationStartedAt) / 1_000,
604
+ });
605
+ const agent = (() => {
606
+ const agentConstructionStartedAt = performance.now();
607
+ let agentConstructionOutcome: "completed" | "failed" = "completed";
608
+ try {
609
+ return runtime.buildAgent(eventing.modelRunSettings, runtimeResources, {
610
+ reasoningEffort: turn.reasoningEffort,
611
+ latencyMode: turnExecutionPolicy.latencyMode,
612
+ ...(serviceTier ? { serviceTier } : {}),
613
+ ...(humanInputResume ? { humanInputResponse: humanInputResume } : {}),
614
+ humanInputEnabled: agentHumanInputEnabled,
615
+ genesisTitleHint: isGenesisTurn,
616
+ sandboxEnvironment,
617
+ ...(preparedTools.attemptToolCatalog
618
+ ? { attemptToolCatalog: preparedTools.attemptToolCatalog }
619
+ : {}),
620
+ ...(sandboxArtifactRuntime.available ? { artifactRuntimeAvailable: true } : {}),
621
+ ...(cancellationSignal ? { turnCancellationSignal: cancellationSignal } : {}),
622
+ onToolCancellationFence: (fence) => {
623
+ eventing.toolCancellationFenceRef.current = fence;
624
+ },
625
+ // TOKEN-BROKER (B1): forward the per-turn git token OFF-MANIFEST as the clone
626
+ // seed. ONLY when the effective backend is NOT selfhosted (the connected
627
+ // machine uses its own git creds — mirrors the skipGitHubToken gate above)
628
+ // AND the mint actually produced a token (repo resources present). The runtime
629
+ // seeds it to the box's token file before the repository-clone runs; it never
630
+ // touches the box/agent manifest env.
631
+ ...(activeSandboxBackend !== "selfhosted" && sandboxGitTokens
632
+ ? { gitTokenSeeds: sandboxGitTokens }
633
+ : {}),
634
+ ...(activeSandboxBackend !== "selfhosted" && sandboxGitCredentialBindings
635
+ ? { gitCredentialBindings: sandboxGitCredentialBindings }
636
+ : {}),
637
+ ...(activeSandboxBackend !== "selfhosted" && !sandboxGitTokens && sandboxGitToken
638
+ ? { gitTokenSeed: sandboxGitToken }
639
+ : {}),
640
+ ...(sandboxCodemodeToken ? { codemodeAvailable: true } : {}),
641
+ // Managed boxes receive the bearer through their protected per-session
642
+ // token file. Connected Machines use transient per-exec delivery above,
643
+ // so they must not run the file-seeding lifecycle hook.
644
+ ...(activeSandboxBackend !== "selfhosted" && sandboxCodemodeToken
645
+ ? {
646
+ codemodeTokenSeed: sandboxCodemodeToken,
647
+ codemodeTokenSessionId: input.sessionId,
648
+ }
649
+ : {}),
650
+ ...(activeSandboxBackend ? { activeSandboxBackend } : {}),
651
+ ...(activeSandboxBackend === "selfhosted" && sandboxState.machinePrimarySession
652
+ ? { sandboxWorkspaceRoot: sandboxState.machinePrimarySession.workspaceRoot }
653
+ : {}),
654
+ fileResourceDownloads,
655
+ mcpServers: preparedTools.mcpServers,
656
+ resolvedMcpConnectionIds: preparedTools.resolvedMcpConnectionIds,
657
+ connectorActionPolicy,
658
+ attemptConnectorActionBindings,
659
+ // LIVE by-reference connector namespaces (fills during this turn's
660
+ // codex_apps tools/list): the codex tool_search description reads it per
661
+ // model call so the model sees the account's real connected sources.
662
+ codexConnectorNamespaces: preparedTools.codexConnectorNamespaces,
663
+ // Resolved-model routing + gating (legacy defaults when null). The model
664
+ // is passed as the model *string* (agent.model = runSettings.openaiModel),
665
+ // NOT a Model instance: an instance only survives the in-process
666
+ // ("none") run, whereas the SandboxAgent/Modal path drops it and
667
+ // re-resolves the model *name* through the global MultiProviderModelProvider
668
+ // configureOpenAI installed — so registry models (Fireworks GLM) route to
669
+ // their own client instead of 404ing against the built-in Azure/OpenAI
670
+ // client. The gating still comes from the resolved provider: server-side
671
+ // store/compaction follow the provider's compaction mode (registry
672
+ // providers resolve to "client"); encrypted reasoning is only
673
+ // round-tripped on the Responses wire API; hosted web search is attached
674
+ // whenever the provider declares it runnable and is independent of the
675
+ // session's MCP allow-list; the effective context window drives the
676
+ // compaction threshold.
677
+ hostedWebSearch,
678
+ ...imageGenerationOption,
679
+ ...videoGenerationOption,
680
+ lazyToolTransport,
681
+ ...(eventing.toolPreparationReady
682
+ ? { toolPreparationReady: eventing.toolPreparationReady }
683
+ : {}),
684
+ supportsImageInput,
685
+ inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
686
+ ...(resolvedModel
687
+ ? {
688
+ encryptedReasoning:
689
+ resolvedModel.provider.api === "responses" &&
690
+ runSettings.openaiReasoningEncryptedContent,
691
+ contextWindowTokens:
692
+ resolvedModel.configured.contextWindowTokens ?? runSettings.contextWindowTokens,
693
+ // The ChatGPT/Codex backend rejects the SDK's HOSTED apply_patch
694
+ // tool. Gateway Responses routes likewise expose ordinary function
695
+ // tools, not OpenAI-hosted sandbox tools. Tell buildAgent to use
696
+ // function apply_patch and wrap successful view_image results as
697
+ // typed input_image content. Chat wires have no proven typed image
698
+ // result transport and therefore receive no view_image tool.
699
+ structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
700
+ // EXPLICIT computer-use tool transport. See {@link computerToolModeForTurn}.
701
+ computerToolMode: computerToolModeForTurn(resolvedModel),
702
+ ...(promptCacheKey ? { promptCacheKey } : {}),
703
+ }
704
+ : // LEGACY global-client fallback (resolveTurnModel returned null → the model
705
+ // is not in the registry, served by the built-in OpenAI/Azure Responses
706
+ // client). Pin computerToolMode to function-image EXPLICITLY rather than
707
+ // leaving the runtime to sniff the instance.
708
+ {
709
+ computerToolMode: computerToolModeForTurn(null),
710
+ promptCacheKey: input.sessionId,
711
+ }),
712
+ // Lazy computer-use seam: runtime first brings up :0 only after the model
713
+ // selects a computer tool, then this hook begins the optional proof
714
+ // recording. Shell/filesystem turns never invoke either operation.
715
+ onComputerUseReady: async () => {
716
+ if (!sandboxState.resolvedSandbox) {
717
+ throw new Error("Computer-use display became ready without a resolved sandbox");
718
+ }
719
+ // This callback is the authoritative execution boundary. Record the
720
+ // action before async ffmpeg startup so transport-event ordering cannot
721
+ // make settlement misclassify a real computer turn as unused.
722
+ recordingState.didComputerUse = true;
723
+ await maybeStartOnTurnRecording(sandboxState.resolvedSandbox, activeSandboxBackend);
724
+ },
725
+ onRetainableSessionImageOutput: media.retainSessionImageAtToolBoundary,
726
+ ...(runtimeSkillActivations.length > 0
727
+ ? { skillActivations: runtimeSkillActivations }
728
+ : {}),
729
+ ...(!structuredWorkspacePolicyActive && workspaceAgentInstructions
730
+ ? { instructionsTemplate: workspaceAgentInstructions }
731
+ : {}),
732
+ ...(workspaceGovernance ? { workspaceGovernance } : {}),
733
+ ...(workspaceMemory ? { workspaceMemory } : {}),
734
+ // Per-session persona tier (session > workspace > deployment default).
735
+ // Composed system-level AFTER the workspace persona so it refines it for
736
+ // this one session; absent ⇒ byte-identical to today's composition.
737
+ ...(session.instructions ? { sessionInstructions: session.instructions } : {}),
738
+ ...workspaceEnvironmentOption,
739
+ // RIG RUNTIME (M3): the doctrine block, the setup-script hook (only when
740
+ // the frozen version carries a non-empty script), and the rig credential
741
+ // hooks. All absent for a rig-less turn (byte-for-byte today).
742
+ ...(rigVersion && rigName
743
+ ? {
744
+ rig: { name: rigName, version: rigVersion.version },
745
+ ...(rigVersion.setupScript && rigVersion.setupScript.trim().length > 0
746
+ ? {
747
+ rigSetup: {
748
+ rigId: session.rigId!,
749
+ versionId: rigVersion.id,
750
+ rigName,
751
+ script: rigVersion.setupScript,
752
+ timeoutMs: runSettings.rigSetupTimeoutMs,
753
+ contentHash: rigProviderImageContentHash({
754
+ backend: turn.sandboxBackend,
755
+ sourceImage: rigProviderImageSourceImage(
756
+ logicalSandboxSettings,
757
+ turn.sandboxBackend,
758
+ ),
759
+ definition: rigVersion,
760
+ }),
761
+ ...(verifiedRigProviderImageId
762
+ ? { verifiedProviderImageId: verifiedRigProviderImageId }
763
+ : {}),
764
+ },
765
+ }
766
+ : {}),
767
+ ...(rigVersion.credentialHooks.length > 0
768
+ ? { rigCredentialHookIds: rigVersion.credentialHooks }
769
+ : {}),
770
+ }
771
+ : {}),
772
+ });
773
+ } catch (error) {
774
+ agentConstructionOutcome = "failed";
775
+ throw error;
776
+ } finally {
777
+ recordTurnStartupPhase(observability, {
778
+ phase: "agent_construction",
779
+ provider: turnExecutionPolicy.providerId,
780
+ backend: activeSandboxBackend ?? groupBoxBackend,
781
+ outcome: agentConstructionOutcome,
782
+ durationSeconds: (performance.now() - agentConstructionStartedAt) / 1_000,
783
+ });
784
+ }
785
+ })();
786
+ const postAgentPreparationStartedAt = performance.now();
787
+ if (
788
+ eventing.modelRunSettings.sandboxBackend !== "none" &&
789
+ eventing.toolCancellationFenceRef.current === null
790
+ ) {
791
+ throw new Error(
792
+ "Sandbox agent construction did not install the mandatory turn tool cancellation fence",
793
+ );
794
+ }
795
+ return {
796
+ agent,
797
+ modelVisibleRuntimeSkillActivations,
798
+ postAgentPreparationStartedAt,
799
+ };
800
+ }
801
+
802
+ export type BuildTurnAgentOk = Awaited<ReturnType<typeof buildTurnAgent>>;