@opengeni/worker-bundle 0.16.29 → 0.20.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 (188) hide show
  1. package/dist/activities/agent-run-admission.d.ts +12 -0
  2. package/dist/activities/agent-turn.d.ts +110 -19
  3. package/dist/activities/api-integrations.d.ts +21 -0
  4. package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
  5. package/dist/activities/capabilities.d.ts +6 -0
  6. package/dist/activities/codemode-dispatcher.d.ts +35 -0
  7. package/dist/activities/codemode-token-renewal.d.ts +23 -0
  8. package/dist/activities/codex-capacity.d.ts +6 -5
  9. package/dist/activities/codex-image-generation.d.ts +21 -0
  10. package/dist/activities/context-compaction.d.ts +1 -0
  11. package/dist/activities/documents.d.ts +3 -2
  12. package/dist/activities/environment.d.ts +17 -8
  13. package/dist/activities/expiring-material-renewal.d.ts +1 -1
  14. package/dist/activities/file-upload-reaper.d.ts +7 -2
  15. package/dist/activities/gateway-image-generation.d.ts +39 -0
  16. package/dist/activities/gateway-video-generation.d.ts +53 -0
  17. package/dist/activities/generated-images.d.ts +112 -0
  18. package/dist/activities/git-credential-renewal.d.ts +1 -1
  19. package/dist/activities/goals.d.ts +2 -2
  20. package/dist/activities/google-drive-changes.d.ts +99 -0
  21. package/dist/activities/image-generation-operation.d.ts +74 -0
  22. package/dist/activities/image-generation-references.d.ts +25 -0
  23. package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
  24. package/dist/activities/knowledge-source-sync.d.ts +75 -0
  25. package/dist/activities/packs.d.ts +21 -36
  26. package/dist/activities/parent-wake.d.ts +1 -1
  27. package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
  28. package/dist/activities/retained-screenshots.d.ts +36 -0
  29. package/dist/activities/rig-verification.d.ts +82 -5
  30. package/dist/activities/run-credential-renewal.d.ts +1 -1
  31. package/dist/activities/run-input.d.ts +3 -1
  32. package/dist/activities/sandbox-images.d.ts +14 -0
  33. package/dist/activities/sandbox-lease.d.ts +47 -9
  34. package/dist/activities/scheduled-tasks.d.ts +2 -2
  35. package/dist/activities/session-state.d.ts +7 -5
  36. package/dist/activities/site-auth-maintenance.d.ts +27 -0
  37. package/dist/activities/streaming.d.ts +1 -1
  38. package/dist/activities/types.d.ts +87 -5
  39. package/dist/activities/video-generation-admission.d.ts +43 -0
  40. package/dist/activities/video-generation-credential.d.ts +24 -0
  41. package/dist/activities/video-generation-reconciliation.d.ts +13 -0
  42. package/dist/activities/video-media-validation.d.ts +34 -0
  43. package/dist/activities/video-output-retention.d.ts +36 -0
  44. package/dist/activities/video-reference-staging.d.ts +55 -0
  45. package/dist/activities/workflow-wake.d.ts +2 -2
  46. package/dist/activities/workspace-capture.d.ts +10 -1
  47. package/dist/activities/xai-auth.d.ts +23 -0
  48. package/dist/activities/xai-image-generation.d.ts +26 -0
  49. package/dist/activities/xai-video-generation.d.ts +18 -0
  50. package/dist/activities-control-XURE4VKN.js +7221 -0
  51. package/dist/activities-control-XURE4VKN.js.map +1 -0
  52. package/dist/activities-control.d.ts +297 -0
  53. package/dist/activities-turn-6EBXGGLS.js +16036 -0
  54. package/dist/activities-turn-6EBXGGLS.js.map +1 -0
  55. package/dist/activities-turn.d.ts +17 -0
  56. package/dist/activities.d.ts +138 -150
  57. package/dist/activity-services.d.ts +10 -0
  58. package/dist/artifact-materializer-entry.d.ts +6 -0
  59. package/dist/artifact-materializer-entry.js +2662 -0
  60. package/dist/artifact-materializer-entry.js.map +1 -0
  61. package/dist/artifact-outbox-entry.d.ts +2 -0
  62. package/dist/artifact-outbox-entry.js +681 -0
  63. package/dist/artifact-outbox-entry.js.map +1 -0
  64. package/dist/chunk-5GRJIP5H.js +3092 -0
  65. package/dist/chunk-5GRJIP5H.js.map +1 -0
  66. package/dist/chunk-GRFQTTSQ.js +448 -0
  67. package/dist/chunk-GRFQTTSQ.js.map +1 -0
  68. package/dist/chunk-TYNPCG4U.js +906 -0
  69. package/dist/chunk-TYNPCG4U.js.map +1 -0
  70. package/dist/concurrency.d.ts +108 -5
  71. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  72. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  73. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  74. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  75. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  76. package/dist/editable-artifact-materializer.d.ts +262 -0
  77. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  78. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  79. package/dist/host-export.d.ts +1 -1
  80. package/dist/index.d.ts +189 -10
  81. package/dist/index.js +715 -17380
  82. package/dist/index.js.map +1 -1
  83. package/dist/memory-pressure-guard.d.ts +57 -0
  84. package/dist/model-checkpoint-memory-collector.d.ts +23 -0
  85. package/dist/model-context-contributions.d.ts +40 -0
  86. package/dist/observability-metrics.d.ts +53 -1
  87. package/dist/sandbox-reaper-contract.d.ts +40 -0
  88. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  89. package/dist/sandbox-resume.d.ts +34 -7
  90. package/dist/sandbox-routing.d.ts +6 -0
  91. package/dist/worker-service-lifecycle.d.ts +6 -2
  92. package/dist/workflow-bundle.js +1094 -370
  93. package/dist/workflows/activities.d.ts +8 -2
  94. package/dist/workflows/document-index.d.ts +1 -1
  95. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  96. package/dist/workflows/rig-verification.d.ts +1 -1
  97. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  98. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  99. package/dist/workflows/session.d.ts +2 -2
  100. package/dist/workflows/site-auth-maintenance.d.ts +2 -0
  101. package/dist/workflows/video-generation.d.ts +10 -0
  102. package/dist/workflows/workflow-wake.d.ts +1 -1
  103. package/dist/workflows.d.ts +10 -7
  104. package/package.json +35 -15
  105. package/src/activities/agent-run-admission.ts +76 -0
  106. package/src/activities/agent-turn.ts +2600 -340
  107. package/src/activities/api-integrations.ts +138 -0
  108. package/src/activities/browser-state-artifact-reaper.ts +120 -0
  109. package/src/activities/capabilities.ts +10 -0
  110. package/src/activities/codemode-dispatcher.ts +335 -0
  111. package/src/activities/codemode-token-renewal.ts +64 -0
  112. package/src/activities/codex-capacity.ts +54 -7
  113. package/src/activities/codex-image-generation.ts +56 -0
  114. package/src/activities/context-compaction.ts +38 -3
  115. package/src/activities/documents.ts +14 -4
  116. package/src/activities/environment.ts +54 -45
  117. package/src/activities/expiring-material-renewal.ts +1 -1
  118. package/src/activities/file-upload-reaper.ts +34 -3
  119. package/src/activities/gateway-image-generation.ts +146 -0
  120. package/src/activities/gateway-video-generation.ts +298 -0
  121. package/src/activities/generated-images.ts +860 -0
  122. package/src/activities/goals.ts +8 -3
  123. package/src/activities/google-drive-changes.ts +454 -0
  124. package/src/activities/image-generation-operation.ts +333 -0
  125. package/src/activities/image-generation-references.ts +118 -0
  126. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  127. package/src/activities/knowledge-source-sync.ts +2840 -0
  128. package/src/activities/mcp-credentials.ts +1 -0
  129. package/src/activities/packs.ts +75 -152
  130. package/src/activities/parent-wake.ts +13 -1
  131. package/src/activities/retained-screenshot-reaper.ts +2 -2
  132. package/src/activities/retained-screenshots.ts +453 -28
  133. package/src/activities/rig-verification.ts +554 -43
  134. package/src/activities/run-input.ts +22 -6
  135. package/src/activities/sandbox-images.ts +167 -0
  136. package/src/activities/sandbox-lease.ts +1065 -483
  137. package/src/activities/scheduled-tasks.ts +104 -74
  138. package/src/activities/session-state.ts +22 -4
  139. package/src/activities/site-auth-maintenance.ts +269 -0
  140. package/src/activities/streaming.ts +42 -3
  141. package/src/activities/types.ts +104 -5
  142. package/src/activities/video-generation-admission.ts +303 -0
  143. package/src/activities/video-generation-credential.ts +112 -0
  144. package/src/activities/video-generation-reconciliation.ts +771 -0
  145. package/src/activities/video-media-validation.ts +332 -0
  146. package/src/activities/video-output-retention.ts +314 -0
  147. package/src/activities/video-reference-staging.ts +350 -0
  148. package/src/activities/workflow-wake.ts +2 -2
  149. package/src/activities/workspace-capture.ts +44 -10
  150. package/src/activities/xai-auth.ts +122 -0
  151. package/src/activities/xai-image-generation.ts +86 -0
  152. package/src/activities/xai-video-generation.ts +69 -0
  153. package/src/activities-control.ts +58 -0
  154. package/src/activities-turn.ts +40 -0
  155. package/src/activities.ts +53 -124
  156. package/src/activity-services.ts +62 -0
  157. package/src/artifact-materializer-entry.ts +35 -0
  158. package/src/artifact-outbox-entry.ts +29 -0
  159. package/src/concurrency.ts +484 -6
  160. package/src/editable-artifact-hint-broker.ts +235 -0
  161. package/src/editable-artifact-materializer-launcher.ts +515 -0
  162. package/src/editable-artifact-materializer-service.ts +787 -0
  163. package/src/editable-artifact-materializer-storage.ts +413 -0
  164. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  165. package/src/editable-artifact-materializer.ts +1289 -0
  166. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  167. package/src/editable-artifact-outbox-service.ts +438 -0
  168. package/src/index.ts +328 -72
  169. package/src/memory-pressure-guard.ts +489 -0
  170. package/src/model-checkpoint-memory-collector.ts +123 -0
  171. package/src/model-context-contributions.ts +159 -0
  172. package/src/observability-metrics.ts +297 -13
  173. package/src/sandbox-reaper-contract.ts +73 -0
  174. package/src/sandbox-reaper-timeout.ts +79 -0
  175. package/src/sandbox-resume.ts +294 -153
  176. package/src/sandbox-routing.ts +32 -24
  177. package/src/worker-service-lifecycle.ts +80 -14
  178. package/src/workflows/activities.ts +31 -0
  179. package/src/workflows/file-upload-reaper.ts +9 -1
  180. package/src/workflows/knowledge-source-sync.ts +83 -0
  181. package/src/workflows/sandbox-reaper.ts +183 -21
  182. package/src/workflows/scheduled-tasks.ts +42 -6
  183. package/src/workflows/session.ts +20 -6
  184. package/src/workflows/site-auth-maintenance.ts +14 -0
  185. package/src/workflows/video-generation.ts +36 -0
  186. package/src/workflows.ts +18 -1
  187. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  188. package/src/activities/toolspace-token-renewal.ts +0 -64
@@ -16,11 +16,26 @@ import {
16
16
  getSessionRootId,
17
17
  getSessionGoal,
18
18
  getHumanInputResumeForEvent,
19
+ getInteractionInterventionResumeForEvent,
19
20
  getSessionHumanInputRequest,
20
21
  installOrReadTurnExecutionPolicyForAttempt,
22
+ persistAttemptToolCatalog,
21
23
  workspaceCodexSubscriptionActive,
24
+ acquireXaiCredentialLease,
25
+ selectXaiCredentialForUse,
26
+ materializeXaiCredentialForRun,
27
+ resolveXaiProviderAccountAuthoritySnapshotForAcceptance,
28
+ heartbeatXaiCredentialLeaseUntil,
29
+ releaseXaiCredentialLease,
30
+ getXaiSessionAccountPin,
31
+ setXaiSessionAccountPin,
32
+ recordXaiSessionLastAccount,
33
+ updateXaiQuotaMetadata,
34
+ XAI_CREDENTIAL_LEASE_TTL_MS,
22
35
  acquireCodexCredentialLease,
23
36
  armCodexCapacityWait,
37
+ armXaiCapacityWait,
38
+ reconcileXaiCapacityWait,
24
39
  heartbeatCodexCredentialLeaseUntil,
25
40
  releaseCodexCredentialLease,
26
41
  CODEX_CREDENTIAL_LEASE_TTL_MS,
@@ -32,13 +47,13 @@ import {
32
47
  fetchCodexUsageForAccount,
33
48
  getSessionCodexState,
34
49
  recordSessionActiveCodexCredential,
35
- setSessionCodexPin,
50
+ setSessionCodexPinInTransaction,
36
51
  recordCodexAccountUsageWithWakeTargets,
37
52
  quarantineCodexCredentialForLease,
38
53
  setActiveCodexCredential,
39
54
  resolveWorkspaceMemoryBlock,
40
55
  setCodexCredentialExhaustedWithWakeTargets,
41
- withCodexCapacityMutation,
56
+ withSessionCodexCapacityMutation,
42
57
  countConsecutiveReactiveRotations,
43
58
  requireSession,
44
59
  recordUsageEvent,
@@ -48,8 +63,8 @@ import {
48
63
  clearDurablePendingSessionToolCalls,
49
64
  appendSessionHistoryItems,
50
65
  isSessionCompactionRequested,
66
+ getActiveSessionHistoryItemsPaged,
51
67
  countSessionHistoryItems,
52
- getActiveSessionHistoryItems,
53
68
  nextSessionHistoryPosition,
54
69
  settleCodexCredentialLeaseLoss,
55
70
  settleCodexCredentialFailover,
@@ -61,14 +76,22 @@ import {
61
76
  accrueWarmSeconds,
62
77
  getMaterializedSandboxFileResources,
63
78
  markSandboxFileResourcesMaterialized,
79
+ getGeneratedVideoArtifact,
80
+ listSessionSystemUpdatesForTurn,
81
+ getWorkspaceVideoGenerationPolicy,
82
+ loadWorkspaceVercelAiGatewayCredentialLease,
64
83
  areGitHubRepositoriesAllowedForWorkspace,
65
84
  SandboxLeaseRecoveryBlockedError,
66
85
  SandboxLeaseSupersededError,
86
+ SandboxLeaseTransitionError,
67
87
  SandboxImageConflictError,
88
+ ActiveSessionHistoryLimitExceededError,
89
+ ApprovalRunStateLimitExceededError,
68
90
  isSessionEventPersistenceError,
69
91
  getEnrollment,
70
92
  abandonRecordingForTurnAttempt,
71
- markSessionAttemptQuiesced,
93
+ commitSessionAttemptQuiescence,
94
+ getOrCreateCompanyProfileSnapshot,
72
95
  getOrCreatePreferenceRegistrySnapshot,
73
96
  getOrCreateWorkspaceInstructionPolicySnapshot,
74
97
  PreferenceRegistryInitiatorError,
@@ -82,10 +105,12 @@ import {
82
105
  type CodexCredentialLeaseResult,
83
106
  type CodexCredentialLeaseSelectionContext,
84
107
  type ApplySessionTurnSettlementInput,
108
+ type ApiIntegrationRuntime,
109
+ type SessionAttemptQuiescenceCommit,
85
110
  type SessionTurnRecordingSettlement,
86
111
  } from "@opengeni/db";
87
112
  import { appendAndPublishTurnEventsFenced, publishDurableSessionEvents } from "@opengeni/events";
88
- import { sandboxOperationMetricObserver } from "@opengeni/observability";
113
+ import { sandboxLeaseTelemetryKey, sandboxOperationMetricObserver } from "@opengeni/observability";
89
114
  import {
90
115
  sandboxStateEntryFromRunState,
91
116
  maxTurnsExceededRunState,
@@ -94,17 +119,20 @@ import {
94
119
  normalizeModelCallUsage,
95
120
  normalizeSdkEvent,
96
121
  normalizeProtocolJsonValue,
122
+ compactMcpResultCustomDataRunState,
123
+ releaseMcpResultCustomDataFromSdkEvent,
97
124
  projectHistoryForProvider,
125
+ restoreGenericDispatchHistoryItems,
98
126
  sanitizeHistoryItemsForModel,
99
127
  projectModelInputForCapabilities,
100
128
  appendPersistentSessionSettings,
129
+ appendSessionGoal,
101
130
  appendSessionInstructions,
102
131
  appendWorkspaceGovernance,
103
132
  appendWorkspaceMemory,
104
133
  composeAgentInstructions,
105
134
  hasActiveWorkspaceInstructionPolicy,
106
135
  renderWorkspaceGovernanceContext,
107
- summarizeForCompaction,
108
136
  requestRemoteCompactionV2,
109
137
  serializedToolsForRemoteCompaction,
110
138
  REMOTE_COMPACTION_V2_BETA_FEATURE,
@@ -119,8 +147,8 @@ import {
119
147
  clearRunCredentialsForAttempt,
120
148
  withRunCredentialsSession,
121
149
  refreshGitCredentialBindingTokenFiles,
122
- refreshToolspaceTokenFile,
123
- toolspaceTokenFileFromEnvironment,
150
+ refreshCodemodeTokenFile,
151
+ codemodeTokenFileFromEnvironment,
124
152
  sandboxFileDownloadFailureNote,
125
153
  SUMMARY_BUFFER_TOKENS,
126
154
  isMcpRequestTimeoutError,
@@ -134,6 +162,7 @@ import {
134
162
  type SandboxFileDownloadFailure,
135
163
  type OpenGeniRuntime,
136
164
  type ComputerToolMode,
165
+ type RetainableSessionImageOutputHook,
137
166
  type ModelResponseUsage,
138
167
  type ModelCallUsageInput,
139
168
  type ModelCallUsageNormalization,
@@ -143,27 +172,36 @@ import {
143
172
  type BackendUnresolvableCode,
144
173
  type EstablishedSandboxSession,
145
174
  type GitCredentialTokenWriterSession,
175
+ type LazyToolTransport,
146
176
  type NormalizedRunCredentialMaterial,
147
177
  type RunCredentialCommandSession,
148
- type ToolspaceTokenWriterSession,
178
+ type CodemodeTokenWriterSession,
179
+ createFirstPartyInteractionAttemptToolDefinitions,
149
180
  deleteRecordingArtifacts,
150
181
  stopRecording as stopRecordingOnBox,
151
182
  } from "@opengeni/runtime";
152
183
  import { connectionTokenResolverForTurn } from "./mcp-credentials";
184
+ import { buildApiIntegrationServersForTurn } from "./api-integrations";
153
185
  import {
186
+ allowedFirstPartyMcpToolsForSession,
154
187
  assertTurnExecutionPolicyMatchesConfigV1,
155
- calculateGatewayReportedCostMicros,
156
- calculateModelUsageCostMicros,
157
- configuredModelPricing,
188
+ calculateGatewayReportedCostBreakdown,
189
+ calculateModelUsageCostBreakdown,
190
+ configuredModelPricingSchedules,
158
191
  configuredStaticUsageLimits,
159
192
  responseSatisfiesLatencyMode,
160
- sandboxArchiveCaptureTimeoutMs,
193
+ sandboxLifecycleTransitionWaitMs,
161
194
  sandboxWarmRateMicrosPerSecond,
162
195
  serviceTierForLatencyMode,
196
+ environmentsEncryptionKeyBytes,
163
197
  settingsWithResolvedModelContext,
164
198
  resolveTurnExecutionPolicyV1,
165
199
  OPENGENI_GATEWAY_MODELS,
166
200
  OPENGENI_GATEWAY_PROVIDER_ID,
201
+ WORKSPACE_GATEWAY_PROVIDER_ID,
202
+ codemodeWorkspaceUrl,
203
+ isDirectOpenAiApiBaseUrl,
204
+ resolveModelProvider,
167
205
  type ModelUsageInput,
168
206
  type ModelProviderApi,
169
207
  type RegistryProviderKind,
@@ -175,6 +213,7 @@ import {
175
213
  settingsWithEnabledCapabilityMcpServers,
176
214
  settingsWithSessionMcpServersForRun,
177
215
  settingsWithWorkspaceGatewayCredential,
216
+ withXaiSubscriptionProvider,
178
217
  } from "./capabilities";
179
218
  import {
180
219
  CODEX_USAGE_EXHAUSTED_PCT,
@@ -196,6 +235,7 @@ import {
196
235
  publishCodexFleetShadowDecisionV1,
197
236
  } from "./codex-fleet-shadow";
198
237
  import type { CodexAccountStatus } from "@opengeni/db";
238
+ import { CodemodeAttemptDispatcher } from "./codemode-dispatcher";
199
239
  import { buildCodexTokenResolver } from "./codex-auth";
200
240
  import {
201
241
  refreshCodexUsageAndRepairCapacityWaiters,
@@ -220,22 +260,40 @@ import {
220
260
  type CodexUsageHeaderSnapshot,
221
261
  } from "@opengeni/codex";
222
262
  import { mergeResourceRefs } from "./common";
263
+ import {
264
+ fetchXaiSubscriptionQuota,
265
+ isXaiSubscriptionTransportError,
266
+ XaiSubscriptionReloginRequired,
267
+ xaiSubscriptionRequestStorage,
268
+ type XaiSubscriptionRequestContext,
269
+ } from "@opengeni/xai-subscription";
270
+ import { buildXaiTurnRequestAuthorization } from "./xai-auth";
271
+ import { executeXaiSubscriptionImageGeneration } from "./xai-image-generation";
223
272
  import {
224
273
  assertSessionAllowsProductModel,
225
274
  defaultSessionMcpServerIds,
226
275
  directPersonalConnectionSubjectId,
276
+ rigProviderImageContentHash,
227
277
  resolveCodexAppsCredentialIdForRun,
228
278
  withFrozenPersonalConnectionDelegations,
229
279
  resolveSessionToolPolicy,
280
+ videoGenerationCapabilitiesForPolicy,
230
281
  } from "@opengeni/core";
231
282
  import { maybeCompactContext, settleFailedContextCompactionLandmark } from "./context-compaction";
232
283
  import { TurnAttemptFencedError } from "./turn-attempt-fenced";
284
+ import {
285
+ admitVideoGenerationRequest,
286
+ managedVideoGenerationCredentialLease,
287
+ xaiVideoGenerationCredentialLease,
288
+ type VideoGenerationCredentialLease,
289
+ } from "./video-generation-admission";
290
+ import { VideoReferenceInputError } from "./video-reference-staging";
233
291
  import {
234
292
  assertGitCredentialRenewalTransportUnchanged,
235
293
  gitCredentialAuthorityForTurn,
236
294
  gitHubTokenMintSelections,
237
295
  loadWorkspaceEnvironmentForRunWithCredentials,
238
- mintSandboxToolspaceToken,
296
+ mintSandboxCodemodeToken,
239
297
  mintRunGitCredentials,
240
298
  mintRunGitCredentialBinding,
241
299
  sandboxEnvironmentForRun,
@@ -252,10 +310,10 @@ import {
252
310
  type RunCredentialRenewalController,
253
311
  } from "./run-credential-renewal";
254
312
  import {
255
- TOOLSPACE_TOKEN_EXPIRY_LEAD_MS,
256
- startToolspaceTokenRenewalLoop,
257
- type ToolspaceTokenRenewalController,
258
- } from "./toolspace-token-renewal";
313
+ CODEMODE_TOKEN_EXPIRY_LEAD_MS,
314
+ startCodemodeTokenRenewalLoop,
315
+ type CodemodeTokenRenewalController,
316
+ } from "./codemode-token-renewal";
259
317
  import {
260
318
  bindRunCredentialResolver,
261
319
  runCredentialAuthNeededPayloads,
@@ -264,10 +322,12 @@ import {
264
322
  import { withFirstPartyTools } from "./goals";
265
323
  import {
266
324
  mergeRigDefaultVariableSetEnvironment,
325
+ rigProviderImageSourceImage,
267
326
  resolveWorkspacePackRuntime,
268
- resolveWorkspaceSkillLibraryRuntime,
327
+ resolveWorkspaceInstalledSkillRuntime,
269
328
  settingsWithPackSandboxImage,
270
329
  settingsWithRigImage,
330
+ settingsWithRigProviderImage,
271
331
  } from "./packs";
272
332
  import { deliverFailedChildTurnToParent } from "./parent-wake";
273
333
  import {
@@ -282,7 +342,7 @@ import {
282
342
  startActivityHeartbeat,
283
343
  } from "./streaming";
284
344
  import type {
285
- ActivityServices,
345
+ TurnActivityServices as ActivityServices,
286
346
  EscapedMcpTimeoutRecoveryDetail,
287
347
  RunAgentTurnInput,
288
348
  RunAgentTurnResult,
@@ -317,6 +377,8 @@ import {
317
377
  recordCreditMicros,
318
378
  recordModelCacheTokens,
319
379
  recordModelInputTokens,
380
+ recordModelRequestPhase,
381
+ recordCompanyBrainContributions,
320
382
  recordSessionEventAppendLatency,
321
383
  recordSessionEventPublishLatency,
322
384
  runtimeMetricsHooksForObservability,
@@ -324,6 +386,7 @@ import {
324
386
  turnLifecycleMetricsFor,
325
387
  type TurnOutcome,
326
388
  } from "../observability-metrics";
389
+ import { buildCompanyBrainContributionReceipt } from "../model-context-contributions";
327
390
  import {
328
391
  beginRecording,
329
392
  discardUnpublishedRecording,
@@ -337,11 +400,36 @@ import {
337
400
  compactRetainedScreenshotRunState,
338
401
  materializeRetainedScreenshotHistory,
339
402
  materializeRetainedScreenshotRunState,
403
+ sdkEventContainsInlineImage,
340
404
  retainComputerScreenshot,
405
+ toolOutputContainsInlineImage,
341
406
  typedScreenshotFromSdkEvent,
407
+ typedScreenshotFromToolOutput,
408
+ unavailableRetainedSessionImage,
342
409
  } from "./retained-screenshots";
410
+ import {
411
+ assertGeneratedImageHistoryRetained,
412
+ collectGeneratedImageReceipts,
413
+ collectGeneratedImageArtifactReceipts,
414
+ collectGeneratedImageRunStateArtifactReceipts,
415
+ compactGeneratedImageHistory,
416
+ compactGeneratedImageRunState,
417
+ compactGeneratedImageSdkEvent,
418
+ generatedImageFromSdkEvent,
419
+ generatedImagesFromHistory,
420
+ isCompletedGeneratedImageSdkEvent,
421
+ projectGeneratedImageHistoryForModel,
422
+ projectGeneratedImageRunStateForModel,
423
+ retainGeneratedImage,
424
+ type GeneratedImageReceipt,
425
+ type GeneratedImageOutput,
426
+ } from "./generated-images";
427
+ import { executeGatewayImageGeneration } from "./gateway-image-generation";
428
+ import { executeCodexImageGeneration } from "./codex-image-generation";
429
+ import { imageProviderBindingHash } from "./image-generation-operation";
430
+ import { resolveImageGenerationReferences } from "./image-generation-references";
343
431
  import { captureWorkspaceRevision, openFreshWorkspaceCaptureSession } from "./workspace-capture";
344
- import type { ChannelASession } from "@opengeni/runtime/sandbox";
432
+ import { SandboxChannelAService, type ChannelASession } from "@opengeni/runtime/sandbox";
345
433
  import { createObjectStorage, type ObjectStorage } from "@opengeni/storage";
346
434
  import {
347
435
  desktopCapableBackend,
@@ -350,22 +438,26 @@ import {
350
438
  } from "@opengeni/runtime";
351
439
  import {
352
440
  CAPABILITY_DESCRIPTORS,
353
- DEFAULT_FIRST_PARTY_MCP_TOOLS,
354
441
  evaluateWorkspaceModelPolicy,
355
442
  readTurnExecutionPolicyV1,
356
443
  resolveWorkspaceAgentHumanInputEnabled,
444
+ resolveWorkspaceMemoryPromptMode,
357
445
  resourceMountPath,
446
+ VideoGenerationRejectedResult,
358
447
  type LatencyMode,
359
448
  type ResourceRef,
360
449
  type RetainedArtifactMetadata,
450
+ type MediaGenerationResult,
361
451
  type SessionEvent,
362
452
  type SessionEventType,
363
453
  type SessionStatus,
364
454
  type SessionTurn,
365
455
  type ToolAuthNeededPayload,
366
456
  type TurnExecutionPolicyV1,
457
+ type XaiProviderAccountAuthoritySnapshotV1,
367
458
  } from "@opengeni/contracts";
368
459
  import { createHash, randomUUID } from "node:crypto";
460
+ import { createModelCheckpointMemoryCollector } from "../model-checkpoint-memory-collector";
369
461
 
370
462
  // Retryable provider connectivity/5xx failures start quickly and back off to
371
463
  // this ceiling. Explicit rate limits retain the minute-granular fallback.
@@ -441,6 +533,7 @@ export function shouldPublishToolAuthNeededForTurn(
441
533
  export function turnExecutionPolicyBillingIdentity(policy: TurnExecutionPolicyV1): {
442
534
  externallyBilled: boolean;
443
535
  codexSubscription: boolean;
536
+ xaiSubscription: boolean;
444
537
  } {
445
538
  return {
446
539
  externallyBilled: policy.billing.metering === "external",
@@ -448,6 +541,10 @@ export function turnExecutionPolicyBillingIdentity(policy: TurnExecutionPolicyV1
448
541
  policy.providerId === "codex-subscription" &&
449
542
  policy.credentialSource.kind === "connected_subscription" &&
450
543
  policy.credentialSource.provider === "codex",
544
+ xaiSubscription:
545
+ policy.providerId === "supergrok-subscription" &&
546
+ policy.credentialSource.kind === "connected_subscription" &&
547
+ policy.credentialSource.provider === "xai",
451
548
  };
452
549
  }
453
550
 
@@ -659,6 +756,55 @@ export function stableHumanInputRequestId(
659
756
  return `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`;
660
757
  }
661
758
 
759
+ export function stableInteractionInterventionId(
760
+ sessionId: string,
761
+ turnId: string,
762
+ toolCallId: string,
763
+ ): string {
764
+ return stableInteractionInterventionUuid(
765
+ "opengeni-interaction-intervention-v1",
766
+ sessionId,
767
+ turnId,
768
+ toolCallId,
769
+ );
770
+ }
771
+
772
+ export function stableInteractionInterventionOperationId(
773
+ sessionId: string,
774
+ turnId: string,
775
+ toolCallId: string,
776
+ ): string {
777
+ return stableInteractionInterventionUuid(
778
+ "opengeni-interaction-intervention-operation-v1",
779
+ sessionId,
780
+ turnId,
781
+ toolCallId,
782
+ );
783
+ }
784
+
785
+ function stableInteractionInterventionUuid(
786
+ namespace: string,
787
+ sessionId: string,
788
+ turnId: string,
789
+ toolCallId: string,
790
+ ): string {
791
+ const hex = createHash("sha256")
792
+ .update(namespace)
793
+ .update("\0")
794
+ .update(sessionId)
795
+ .update("\0")
796
+ .update(turnId)
797
+ .update("\0")
798
+ .update(toolCallId)
799
+ .digest("hex")
800
+ .slice(0, 32)
801
+ .split("");
802
+ hex[12] = "5";
803
+ hex[16] = ["8", "9", "a", "b"][Number.parseInt(hex[16] ?? "0", 16) % 4] ?? "8";
804
+ const value = hex.join("");
805
+ return `${value.slice(0, 8)}-${value.slice(8, 12)}-${value.slice(12, 16)}-${value.slice(16, 20)}-${value.slice(20)}`;
806
+ }
807
+
662
808
  /**
663
809
  * True when this activity attempt was cancelled because its hosting worker is
664
810
  * shutting down gracefully (SIGTERM during a deploy), as opposed to a
@@ -730,18 +876,22 @@ const QUIESCENCE_PROOF_SIGNAL_MAX_RETRY_MS = 5_000;
730
876
  * state. The proof object never changes between attempts. */
731
877
  export async function persistOrSignalSessionAttemptQuiescence(input: {
732
878
  proof: SessionAttemptQuiescenceProof;
733
- persistReceipt: () => Promise<SessionEvent[]>;
879
+ persistReceipt: () => Promise<SessionAttemptQuiescenceCommit>;
880
+ deliverWorkflowWake?: (
881
+ wake: NonNullable<SessionAttemptQuiescenceCommit["workflowWake"]>,
882
+ ) => Promise<unknown>;
734
883
  publishEvents: (events: SessionEvent[]) => Promise<unknown>;
735
884
  signalProof: ActivityServices["signalSessionAttemptQuiesced"];
736
885
  sleep?: (ms: number) => Promise<void>;
737
886
  heartbeat?: (attempt: number, delayMs: number) => void;
738
887
  onReceiptFailure?: (error: unknown) => void;
888
+ onWakeFailure?: (error: unknown) => void;
739
889
  onPublishFailure?: (error: unknown) => void;
740
890
  onSignalFailure?: (error: unknown, attempt: number, delayMs: number) => void;
741
891
  }): Promise<"receipt" | "signal"> {
742
- let events: SessionEvent[];
892
+ let receipt: SessionAttemptQuiescenceCommit;
743
893
  try {
744
- events = await input.persistReceipt();
894
+ receipt = await input.persistReceipt();
745
895
  } catch (receiptError) {
746
896
  input.onReceiptFailure?.(receiptError);
747
897
  if (!input.signalProof) {
@@ -771,8 +921,17 @@ export async function persistOrSignalSessionAttemptQuiescence(input: {
771
921
  }
772
922
  }
773
923
 
924
+ if (receipt.workflowWake && input.deliverWorkflowWake) {
925
+ try {
926
+ await input.deliverWorkflowWake(receipt.workflowWake);
927
+ } catch (wakeError) {
928
+ // The exact revision is already durable. Immediate delivery is latency
929
+ // optimization only; the outbox dispatcher remains the repair path.
930
+ input.onWakeFailure?.(wakeError);
931
+ }
932
+ }
774
933
  try {
775
- await input.publishEvents(events);
934
+ await input.publishEvents(receipt.events);
776
935
  } catch (publishError) {
777
936
  // Postgres already committed quiesced_at, the queue event, and the wake.
778
937
  // NATS is live fanout only; never misclassify its failure as receipt loss.
@@ -792,7 +951,7 @@ export async function drainAttemptOwnedSandboxWriters(input: {
792
951
  toolCancellationFence: Pick<TurnToolCancellationFence, "cancel" | "waitForQuiescence"> | null;
793
952
  cancellationReason?: unknown;
794
953
  gitCredentialRenewals: readonly Pick<GitCredentialRenewalController, "stop">[];
795
- toolspaceTokenRenewal: Pick<ToolspaceTokenRenewalController, "stop"> | null;
954
+ codemodeTokenRenewal: Pick<CodemodeTokenRenewalController, "stop"> | null;
796
955
  runCredentialRenewal: Pick<RunCredentialRenewalController, "stop"> | null;
797
956
  }): Promise<void> {
798
957
  if (input.toolCancellationFence) {
@@ -802,10 +961,41 @@ export async function drainAttemptOwnedSandboxWriters(input: {
802
961
  await input.toolCancellationFence.waitForQuiescence();
803
962
  }
804
963
  await Promise.all(input.gitCredentialRenewals.map(async (renewal) => await renewal.stop()));
805
- await input.toolspaceTokenRenewal?.stop();
964
+ await input.codemodeTokenRenewal?.stop();
806
965
  await input.runCredentialRenewal?.stop();
807
966
  }
808
967
 
968
+ /** Persist the exact turn's second-stage release only after the caller has
969
+ * crossed the physical writer boundary. This wait is intentionally independent
970
+ * of Temporal cancellation: it is a short idempotent DB transaction that closes
971
+ * the durable archive-capture fence. Bounded retries cover a rollout connection
972
+ * reset without turning ordinary finalizer housekeeping into lifecycle state. */
973
+ export async function releaseTurnSandboxAfterWriterDrain(
974
+ sandbox: Pick<ResumedTurnSandbox, "release">,
975
+ options: {
976
+ maxAttempts?: number;
977
+ retryDelayMs?: number;
978
+ wait?: (delayMs: number) => Promise<void>;
979
+ } = {},
980
+ ): Promise<void> {
981
+ const maxAttempts = Math.max(1, options.maxAttempts ?? 3);
982
+ const retryDelayMs = Math.max(0, options.retryDelayMs ?? 50);
983
+ const wait = options.wait ?? sleep;
984
+ let lastError: unknown;
985
+ for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
986
+ try {
987
+ await sandbox.release({ workspaceWritersQuiesced: true });
988
+ return;
989
+ } catch (error) {
990
+ lastError = error;
991
+ if (attempt < maxAttempts) {
992
+ await wait(retryDelayMs * attempt);
993
+ }
994
+ }
995
+ }
996
+ throw lastError;
997
+ }
998
+
809
999
  /**
810
1000
  * Await a finalizer operation only while this Temporal activity still owns its
811
1001
  * execution window. Once Pause/Steer cancellation arrives, the operation keeps
@@ -1799,6 +1989,15 @@ export function managedSandboxOwnershipForTurn(
1799
1989
  };
1800
1990
  }
1801
1991
 
1992
+ /** A sandboxless home still establishes an explicitly attached Connected Machine. */
1993
+ export function shouldEstablishSandboxForTurn(
1994
+ sandboxOwnershipEnabled: boolean,
1995
+ homeBackend: Settings["sandboxBackend"],
1996
+ machinePrimary: boolean,
1997
+ ): boolean {
1998
+ return sandboxOwnershipEnabled && (homeBackend !== "none" || machinePrimary);
1999
+ }
2000
+
1802
2001
  /**
1803
2002
  * Classify a persisted active-sandbox pointer for TURN-START RECONCILE (issue #341
1804
2003
  * invariant B). Returns the typed reason to RESET the pointer to the session HOME,
@@ -1942,6 +2141,65 @@ export async function ensureTurnModalRegistryImage(
1942
2141
  await ensureRegistryImage(runSettings);
1943
2142
  }
1944
2143
 
2144
+ const SANDBOX_ARTIFACT_RUNTIME_MANIFEST = "/opt/opengeni/artifact-runtime/installation.json";
2145
+ const SANDBOX_ARTIFACT_TOOL_ENTRY = "/opt/opengeni/artifact-runtime/skill-facade-entry.mjs";
2146
+
2147
+ export type SandboxArtifactRuntimeAdmission = Readonly<{
2148
+ available: boolean;
2149
+ environment: Readonly<Record<string, string>>;
2150
+ }>;
2151
+
2152
+ /**
2153
+ * Admit the optional native standalone-file runtime only for the deployment's
2154
+ * exact base sandbox image contract. A pack/rig image override is an
2155
+ * independent filesystem and therefore fails closed even when the deployment
2156
+ * base image is capable. Collaborative artifact skills are admitted separately
2157
+ * from the frozen canonical tool catalog.
2158
+ *
2159
+ * This keeps lazy provisioning intact: CI/release proves the image closure,
2160
+ * while a before-agent-start doctor verifies the actual box before any model
2161
+ * call. No speculative sandbox is created merely to populate the skill index.
2162
+ */
2163
+ export function sandboxArtifactRuntimeAdmission(
2164
+ deploymentSettings: Settings,
2165
+ runSettings: Settings,
2166
+ backend: Settings["sandboxBackend"] | undefined,
2167
+ options: Readonly<{ production?: boolean }> = {},
2168
+ ): SandboxArtifactRuntimeAdmission {
2169
+ if (!deploymentSettings.sandboxArtifactRuntimeEnabled) {
2170
+ return { available: false, environment: {} };
2171
+ }
2172
+ const production = options.production ?? process.env.NODE_ENV === "production";
2173
+ if (backend === "docker") {
2174
+ const image = deploymentSettings.dockerImage;
2175
+ const localDevelopmentImage = /^opengeni-sandbox:local-[0-9a-f]{12}$/u.test(image);
2176
+ if (
2177
+ runSettings.dockerImage !== image ||
2178
+ (!/@sha256:[0-9a-f]{64}$/u.test(image) && (production || !localDevelopmentImage))
2179
+ ) {
2180
+ return { available: false, environment: {} };
2181
+ }
2182
+ } else if (backend === "modal") {
2183
+ if (
2184
+ !deploymentSettings.modalImageRef ||
2185
+ !/@sha256:[0-9a-f]{64}$/u.test(deploymentSettings.modalImageRef) ||
2186
+ runSettings.modalImageRef !== deploymentSettings.modalImageRef ||
2187
+ runSettings.modalImageId !== deploymentSettings.modalImageId
2188
+ ) {
2189
+ return { available: false, environment: {} };
2190
+ }
2191
+ } else {
2192
+ return { available: false, environment: {} };
2193
+ }
2194
+ return {
2195
+ available: true,
2196
+ environment: {
2197
+ OPENGENI_ARTIFACT_RUNTIME_MANIFEST: SANDBOX_ARTIFACT_RUNTIME_MANIFEST,
2198
+ OPENGENI_ARTIFACT_TOOL_ENTRY: SANDBOX_ARTIFACT_TOOL_ENTRY,
2199
+ },
2200
+ };
2201
+ }
2202
+
1945
2203
  /**
1946
2204
  * Decide whether the first actual computer action may start a proof recording.
1947
2205
  *
@@ -2036,9 +2294,42 @@ export function structuredToolTransportForTurn(
2036
2294
  resolvedModel: { provider: { kind: RegistryProviderKind } } | null,
2037
2295
  ): boolean {
2038
2296
  if (!resolvedModel) return true;
2039
- return !["codex-subscription", "vercel-gateway-managed", "vercel-gateway-workspace"].includes(
2040
- resolvedModel.provider.kind,
2041
- );
2297
+ return ![
2298
+ "codex-subscription",
2299
+ "xai-subscription",
2300
+ "vercel-gateway-managed",
2301
+ "vercel-gateway-workspace",
2302
+ ].includes(resolvedModel.provider.kind);
2303
+ }
2304
+
2305
+ /**
2306
+ * Progressive tool disclosure is universal for supported OpenGeni turns; only
2307
+ * its contained transport differs. Codex keeps its native path, built-in direct
2308
+ * OpenAI/Azure Responses use native client tool search, and every other ordinary
2309
+ * function-calling provider uses OpenGeni's stable search/invoke dispatcher.
2310
+ */
2311
+ export function lazyToolTransportForTurn(
2312
+ resolvedModel: {
2313
+ provider: {
2314
+ id: string;
2315
+ kind: RegistryProviderKind;
2316
+ api: ModelProviderApi;
2317
+ builtin: boolean;
2318
+ baseUrl?: string | undefined;
2319
+ };
2320
+ } | null,
2321
+ ): LazyToolTransport {
2322
+ if (!resolvedModel) return "openai_native";
2323
+ const provider = resolvedModel.provider;
2324
+ if (provider.kind === "codex-subscription") return "codex_native";
2325
+ const isDirectBuiltinResponses =
2326
+ provider.builtin &&
2327
+ provider.api === "responses" &&
2328
+ (provider.id === "azure" || (provider.id === "openai" && provider.baseUrl === undefined));
2329
+ if (isDirectBuiltinResponses) {
2330
+ return "openai_native";
2331
+ }
2332
+ return "generic_dispatch";
2042
2333
  }
2043
2334
 
2044
2335
  /**
@@ -2055,6 +2346,54 @@ export function hostedWebSearchForTurn(
2055
2346
  return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
2056
2347
  }
2057
2348
 
2349
+ /**
2350
+ * Image generation is an optional connected-account capability. A delegated
2351
+ * subscription may still authorize the text model without carrying the local
2352
+ * credential identity required for paid image operations; that must narrow the
2353
+ * tool catalog, not reject an otherwise valid turn.
2354
+ */
2355
+ export function connectedSubscriptionImageGenerationAuthority<T>(
2356
+ credentialContext: T | null | undefined,
2357
+ credentialId: string | null | undefined,
2358
+ ): { credentialContext: T; credentialId: string } | null {
2359
+ if (credentialContext == null || typeof credentialId !== "string" || credentialId.length === 0)
2360
+ return null;
2361
+ return { credentialContext, credentialId };
2362
+ }
2363
+
2364
+ /** Direct OpenAI hosted image IDs are reusable only by the exact API credential. */
2365
+ export function openAiHostedImageProviderBindingForTurn(
2366
+ settings: Pick<Settings, "openaiProvider" | "openaiApiKey" | "openaiBaseUrl">,
2367
+ resolvedModel: {
2368
+ provider: {
2369
+ id: string;
2370
+ kind: RegistryProviderKind;
2371
+ builtin: boolean;
2372
+ baseUrl?: string | undefined;
2373
+ };
2374
+ configured: {
2375
+ capabilities: {
2376
+ hostedTools: { imageGeneration: { runnable: boolean } };
2377
+ };
2378
+ };
2379
+ } | null,
2380
+ ): { providerId: string; providerBindingHash: string } | null {
2381
+ if (!resolvedModel?.configured.capabilities.hostedTools.imageGeneration.runnable) return null;
2382
+ const providerId = resolvedModel.provider.id;
2383
+ const isDirectOpenAi =
2384
+ resolvedModel.provider.builtin &&
2385
+ resolvedModel.provider.id === "openai" &&
2386
+ isDirectOpenAiApiBaseUrl(resolvedModel.provider.baseUrl) &&
2387
+ settings.openaiProvider === "openai" &&
2388
+ isDirectOpenAiApiBaseUrl(settings.openaiBaseUrl);
2389
+ const bindingIdentity = isDirectOpenAi ? settings.openaiApiKey : null;
2390
+ if (!bindingIdentity) return null;
2391
+ return {
2392
+ providerId,
2393
+ providerBindingHash: imageProviderBindingHash(providerId, bindingIdentity),
2394
+ };
2395
+ }
2396
+
2058
2397
  /** Exact model-catalog modality gate; null preserves the legacy built-in path. */
2059
2398
  export function modelSupportsImageInputForTurn(
2060
2399
  resolvedModel: {
@@ -2080,7 +2419,10 @@ export function modelAttachmentInputPolicyForTurn(
2080
2419
  resolvedModel: {
2081
2420
  provider: { api: ModelProviderApi };
2082
2421
  configured: {
2083
- capabilities?: { inputModalities: string[]; inputFileMediaTypes?: string[] };
2422
+ capabilities?: {
2423
+ inputModalities: string[];
2424
+ inputFileMediaTypes?: string[];
2425
+ };
2084
2426
  };
2085
2427
  } | null,
2086
2428
  ): ModelAttachmentInputPolicy {
@@ -2207,27 +2549,30 @@ export function isLazySandboxProvisionRetryable(error: unknown): boolean {
2207
2549
  if (error instanceof WorkspaceArchiveIntegrityError) {
2208
2550
  return error.retryable;
2209
2551
  }
2210
- if (error instanceof SandboxLeaseSupersededError || error instanceof SandboxWarmingTimeoutError) {
2552
+ if (
2553
+ error instanceof SandboxLeaseSupersededError ||
2554
+ error instanceof SandboxLeaseTransitionError
2555
+ ) {
2211
2556
  return true;
2212
2557
  }
2213
- const message = error instanceof Error ? error.message : String(error);
2214
- return /(?:capacity|create|creation|provider|sandbox).*(?:timeout|timed out)|(?:timeout|timed out).*(?:capacity|create|creation|provider|sandbox)|ECONNRESET|ETIMEDOUT|EAI_AGAIN|temporar/i.test(
2215
- message,
2216
- );
2558
+ if (error instanceof SandboxWarmingTimeoutError) {
2559
+ return false;
2560
+ }
2561
+ // Provider/transport text is not durable evidence that creation never
2562
+ // happened. Retrying an ambiguous unknown here can create a second box; only
2563
+ // typed, ownership-fenced lifecycle outcomes above are safe to replay.
2564
+ return false;
2217
2565
  }
2218
2566
 
2219
- /**
2220
- * Deadline recovery cannot re-admit the same turn until the global reaper has
2221
- * observed the released holder, captured the final archive, terminated the old
2222
- * provider box, and committed the lease cold. Give that full sequence one
2223
- * snapshot budget plus two schedule periods. This is workflow-visible pacing,
2224
- * not an in-memory sleep, so Pause/Steer/new user intent can still wake the
2225
- * durable session immediately.
2226
- */
2567
+ /** Short workflow-visible anti-churn pacing after a lifecycle transition. The
2568
+ * next acquire waits on the exact durable claim itself, so this is deliberately
2569
+ * not a guessed snapshot-plus-schedules completion time. */
2227
2570
  export function sandboxDeadlineRotationRecoveryDelayMs(
2228
- settings: Pick<Settings, "sandboxLeaseReaperPeriodMs" | "sandboxSnapshotTimeoutMs">,
2571
+ settings: Pick<Settings, "sandboxLeaseReaperPeriodMs">,
2229
2572
  ): number {
2230
- return settings.sandboxSnapshotTimeoutMs + settings.sandboxLeaseReaperPeriodMs * 2;
2573
+ // The next attempt waits on the durable lifecycle claim itself. This delay is
2574
+ // only anti-churn pacing, not an estimate of snapshot + future schedules.
2575
+ return Math.min(5_000, settings.sandboxLeaseReaperPeriodMs);
2231
2576
  }
2232
2577
 
2233
2578
  export function createTurnSandboxProvisioner<T>(
@@ -2448,12 +2793,18 @@ export async function finalizeDurableTurnOpStreams(
2448
2793
  }
2449
2794
 
2450
2795
  export function createRunAgentTurnActivity(services: () => Promise<ActivityServices>) {
2796
+ const modelCheckpointMemoryCollector = createModelCheckpointMemoryCollector();
2797
+ // Keep a distinct cooldown for terminal collection. A collection at the
2798
+ // final model checkpoint happens while the activity still owns its complete
2799
+ // turn graph and must not suppress collection after that graph is released.
2800
+ const turnCompletionMemoryCollector = createModelCheckpointMemoryCollector();
2451
2801
  return async function runAgentTurn(input: RunAgentTurnInput): Promise<RunAgentTurnResult> {
2452
2802
  const {
2453
2803
  settings,
2454
2804
  db,
2455
2805
  bus,
2456
2806
  runtime,
2807
+ summarizeContextForCompaction,
2457
2808
  objectStorage,
2458
2809
  observability,
2459
2810
  wakeSessionWorkflow,
@@ -2461,6 +2812,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2461
2812
  signalCodexCapacityWorkflow,
2462
2813
  entitlements,
2463
2814
  connectionCredentials,
2815
+ startVideoGenerationWorkflow,
2464
2816
  } = await services();
2465
2817
  const activityContext = currentActivityContext();
2466
2818
  const cancellationSignal = activityContext?.cancellationSignal;
@@ -2537,6 +2889,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2537
2889
  );
2538
2890
  const sandboxOperationObserver = sandboxOperationMetricObserver(observability);
2539
2891
  let isCodexTurn = false;
2892
+ let isXaiTurn = false;
2540
2893
  let isExternallyBilledTurn = false;
2541
2894
  let executionGeneration = 0;
2542
2895
  let providerRecoveryCount = 0;
@@ -2668,11 +3021,107 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2668
3021
  }, 60_000);
2669
3022
  codexLeaseHeartbeatTimer.unref?.();
2670
3023
  };
3024
+ let xaiLeaseHeld = false;
3025
+ let xaiLeaseLost = false;
3026
+ let xaiLeaseSubjectId: string | null = null;
3027
+ let xaiLeaseHolderId: string | null = null;
3028
+ let xaiLeaseGeneration: number | null = null;
3029
+ let xaiLeaseConfirmedUntilMs: number | null = null;
3030
+ let xaiLeaseHeartbeatInFlight = false;
3031
+ let xaiLeaseHeartbeatTimer: ReturnType<typeof setInterval> | undefined;
3032
+ const markXaiLeaseLost = (reason: "deadline" | "not_found"): void => {
3033
+ if (xaiLeaseLost) return;
3034
+ xaiLeaseLost = true;
3035
+ observability.warn("xAI credential lease was lost during an active turn", {
3036
+ workspaceId: input.workspaceId,
3037
+ turnId,
3038
+ reason,
3039
+ });
3040
+ };
3041
+ const renewXaiLease = async (): Promise<void> => {
3042
+ if (
3043
+ !turnId ||
3044
+ !xaiLeaseHeld ||
3045
+ !xaiLeaseSubjectId ||
3046
+ !xaiLeaseHolderId ||
3047
+ xaiLeaseGeneration === null ||
3048
+ xaiLeaseLost
3049
+ ) {
3050
+ return;
3051
+ }
3052
+ if (codexCredentialLeaseDeadlineExpired(xaiLeaseConfirmedUntilMs)) {
3053
+ markXaiLeaseLost("deadline");
3054
+ return;
3055
+ }
3056
+ if (xaiLeaseHeartbeatInFlight) return;
3057
+ xaiLeaseHeartbeatInFlight = true;
3058
+ const renewalStartedAtMs = performance.now();
3059
+ try {
3060
+ const renewedUntil = await heartbeatXaiCredentialLeaseUntil(db, {
3061
+ workspaceId: input.workspaceId,
3062
+ subjectId: xaiLeaseSubjectId,
3063
+ turnId,
3064
+ holderId: xaiLeaseHolderId,
3065
+ generation: xaiLeaseGeneration,
3066
+ leaseTtlMs: XAI_CREDENTIAL_LEASE_TTL_MS,
3067
+ });
3068
+ if (!renewedUntil) {
3069
+ markXaiLeaseLost("not_found");
3070
+ } else {
3071
+ xaiLeaseConfirmedUntilMs = renewalStartedAtMs + XAI_CREDENTIAL_LEASE_TTL_MS;
3072
+ }
3073
+ } catch (error) {
3074
+ if (codexCredentialLeaseDeadlineExpired(xaiLeaseConfirmedUntilMs)) {
3075
+ markXaiLeaseLost("deadline");
3076
+ return;
3077
+ }
3078
+ observability.warn("xAI credential lease heartbeat failed", {
3079
+ workspaceId: input.workspaceId,
3080
+ turnId,
3081
+ ...safeErrorDiagnostic(error),
3082
+ });
3083
+ } finally {
3084
+ xaiLeaseHeartbeatInFlight = false;
3085
+ }
3086
+ };
3087
+ const startXaiLeaseHeartbeat = (): void => {
3088
+ if (!turnId || xaiLeaseHeartbeatTimer) return;
3089
+ xaiLeaseHeartbeatTimer = setInterval(() => {
3090
+ void renewXaiLease();
3091
+ }, 60_000);
3092
+ xaiLeaseHeartbeatTimer.unref?.();
3093
+ };
3094
+ const renewServingCredentialLease = async (
3095
+ reason: "timer" | "runtime_event" | "model_usage",
3096
+ ): Promise<void> => {
3097
+ await renewCodexLease(reason);
3098
+ await renewXaiLease();
3099
+ };
3100
+ const servingCredentialLeaseLost = (): boolean => codexLeaseLost || xaiLeaseLost;
2671
3101
  // P1.2 ownership inversion: when sandboxOwnershipEnabled, the turn resolves
2672
3102
  // the one box by id from the group lease and injects it NON-OWNED into the
2673
3103
  // run. null when the flag is off (byte-for-byte the legacy build-and-discard
2674
3104
  // path) OR when the backend is "none". Released + dropped in `finally`.
2675
3105
  let resolvedSandbox: ResumedTurnSandbox | null = null;
3106
+ let attemptWritersDrained = false;
3107
+ const lateSandboxesAwaitingWriterDrain = new Set<ResumedTurnSandbox>();
3108
+ const releaseLateSandbox = async (sandbox: ResumedTurnSandbox): Promise<void> => {
3109
+ lateSandboxesAwaitingWriterDrain.add(sandbox);
3110
+ // Drop the holder/timer immediately, but keep its null-outcome admissions
3111
+ // fenced until the shared attempt writer drain completes. The staged
3112
+ // release serializes a later proof-bearing call behind this one.
3113
+ await sandbox.release().catch(() => undefined);
3114
+ if (!attemptWritersDrained) return;
3115
+ try {
3116
+ await releaseTurnSandboxAfterWriterDrain(sandbox);
3117
+ lateSandboxesAwaitingWriterDrain.delete(sandbox);
3118
+ } catch (error) {
3119
+ console.error(
3120
+ "late sandbox quiesced release failed (turn outcome unaffected)",
3121
+ safeErrorDiagnostic(error),
3122
+ );
3123
+ }
3124
+ };
2676
3125
  const requireResolvedSandboxForMutation = (message: string): ResumedTurnSandbox => {
2677
3126
  if (!resolvedSandbox) throw new Error(message);
2678
3127
  return resolvedSandbox;
@@ -2754,7 +3203,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2754
3203
  expectedEpoch: sandbox.leaseEpoch,
2755
3204
  expectedInstanceId: sandbox.established.instanceId,
2756
3205
  operation,
2757
- captureWaitMs: sandboxArchiveCaptureTimeoutMs(settings),
3206
+ captureWaitMs: sandboxLifecycleTransitionWaitMs(settings),
3207
+ ...(cancellationSignal ? { waitSignal: cancellationSignal } : {}),
2758
3208
  };
2759
3209
  const admission = await advanceWorkspaceGeneration(db, identity);
2760
3210
  let result: T;
@@ -2811,11 +3261,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2811
3261
  let runCredentialRenewal: RunCredentialRenewalController | null = null;
2812
3262
  let runCredentialRenewalClosed = false;
2813
3263
  let runCredentialSession: RunCredentialCommandSession | null = null;
2814
- // The delegated Toolspace bearer has a one-hour TTL. Renewal is attempt-
3264
+ // The delegated Codemode bearer has a one-hour TTL. Renewal is attempt-
2815
3265
  // owned and attaches only after the initial token file reached a real
2816
3266
  // sandbox session; finalization drains an in-flight replacement.
2817
- let toolspaceTokenRenewal: ToolspaceTokenRenewalController | null = null;
2818
- let toolspaceTokenRenewalClosed = false;
3267
+ let codemodeTokenRenewal: CodemodeTokenRenewalController | null = null;
3268
+ let codemodeTokenRenewalClosed = false;
2819
3269
  // MID-SESSION snapshot single-flight guard: the heartbeat tick fires every
2820
3270
  // 10s but a Modal filesystem snapshot can take longer — never overlap two
2821
3271
  // captures on one box. The in-flight capture's promise is held so the
@@ -2869,6 +3319,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2869
3319
  await current?.flush().catch(() => undefined);
2870
3320
  };
2871
3321
  let preparedTools: Awaited<ReturnType<OpenGeniRuntime["prepareTools"]>> | null = null;
3322
+ let codemodeDispatcher: CodemodeAttemptDispatcher | null = null;
2872
3323
  const toolCancellationFenceRef: {
2873
3324
  current: TurnToolCancellationFence | null;
2874
3325
  } = {
@@ -3195,9 +3646,294 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3195
3646
  // remains the final call/result pairing guard for every other reconcile.
3196
3647
  let persistedHistoryCount = 0;
3197
3648
  const retainedScreenshotReceiptsByCallId = new Map<string, RetainedArtifactMetadata>();
3198
- const computerScreenshotCallIds = new Set<string>();
3649
+ // Set from the resolved provider wire before any model history is prepared.
3650
+ // Text-only/chat wires keep compact receipts and must never allocate image
3651
+ // bytes merely for the following request-local projection to remove them.
3652
+ let modelCanReceiveRetainedSessionImages = true;
3653
+ const generatedImageReceiptsByProviderItemId = new Map<string, GeneratedImageReceipt>();
3654
+ const generatedImageReceiptsByArtifactId = new Map<string, GeneratedImageReceipt>();
3655
+ const videoGenerationAcceptancesByCallId = new Map<
3656
+ string,
3657
+ { operationId: string; requestDigest: string }
3658
+ >();
3659
+ const requiredGeneratedVideoFiles: Array<{
3660
+ operationId: string;
3661
+ artifactId: string;
3662
+ fileId: string;
3663
+ objectKey: string;
3664
+ sizeBytes: number;
3665
+ sha256: string;
3666
+ filename: string;
3667
+ }> = [];
3668
+ let generatedImageMaterializationCache: {
3669
+ instanceId: string;
3670
+ fileIds: Set<string>;
3671
+ } | null = null;
3672
+ // Legacy ownership mode lets the Agents SDK create the sandbox inside
3673
+ // run(). Keep that exact session once the runtime exposes it so an image
3674
+ // generated later in the same model/tool loop can be copied immediately.
3675
+ let sdkOwnedSandboxSession: CodemodeTokenWriterSession | null = null;
3676
+ const prepareGeneratedImageDownload = async (receipt: GeneratedImageReceipt) => {
3677
+ if (!objectStorage) {
3678
+ throw new Error("Generated image sandbox materialization requires object storage");
3679
+ }
3680
+ const file = await requireFile(db, input.workspaceId, receipt.artifact.artifactId);
3681
+ const downloadStorage = objectStorageForSandboxDownloads(modelRunSettings, objectStorage);
3682
+ const signed = await downloadStorage.createGetUrl({
3683
+ key: file.objectKey,
3684
+ });
3685
+ return {
3686
+ file,
3687
+ download: {
3688
+ fileId: file.id,
3689
+ mountPath: "generated-images",
3690
+ filename: file.safeFilename,
3691
+ url: signed.url,
3692
+ expiresAt: signed.expiresAt,
3693
+ sizeBytes: file.sizeBytes,
3694
+ ...(file.sha256 ? { sha256: file.sha256 } : {}),
3695
+ } satisfies SandboxFileDownload,
3696
+ };
3697
+ };
3698
+ const writeGeneratedImageDownload = async (
3699
+ sessionForDownload: CodemodeTokenWriterSession,
3700
+ download: SandboxFileDownload,
3701
+ ): Promise<void> => {
3702
+ const runAs = sandboxRunAs(modelRunSettings);
3703
+ const materialized = await materializeSandboxFileDownloads(sessionForDownload, [download], {
3704
+ onRuntimeEvent: async (event) => {
3705
+ await publish?.([{ type: event.type, payload: event.payload }], true);
3706
+ },
3707
+ ...(runAs ? { runAs } : {}),
3708
+ ...(toolCancellationFenceRef.current
3709
+ ? {
3710
+ commandRunner: toolCancellationFenceRef.current.runSandboxCommand.bind(
3711
+ toolCancellationFenceRef.current,
3712
+ ),
3713
+ }
3714
+ : {}),
3715
+ });
3716
+ if (materialized.failures.length > 0) {
3717
+ throw new Error(materialized.failures[0]!.reason);
3718
+ }
3719
+ };
3720
+ const warnGeneratedImageMaterializationDeferred = (error: unknown): void => {
3721
+ // Generation and permanent retention are already durable. Never turn a
3722
+ // transient sandbox copy failure into a replay of paid provider work;
3723
+ // an unmaterialized resource is retried on the next real sandbox.
3724
+ observability.warn("Generated image sandbox materialization deferred", {
3725
+ errorClass: error instanceof Error ? error.name : "UnknownError",
3726
+ errorCode: "generated_image_materialization_deferred",
3727
+ origin: "worker",
3728
+ });
3729
+ };
3730
+ const materializeGeneratedImageInSandbox = async (
3731
+ receipt: GeneratedImageReceipt,
3732
+ sandbox: ResumedTurnSandbox,
3733
+ sessionForDownload: unknown,
3734
+ ): Promise<boolean> => {
3735
+ try {
3736
+ const { file, download } = await prepareGeneratedImageDownload(receipt);
3737
+ const cacheable = sandbox.established.backendId !== "selfhosted";
3738
+ if (generatedImageMaterializationCache?.instanceId !== sandbox.established.instanceId) {
3739
+ const fileIds =
3740
+ cacheable && sandboxGroupId
3741
+ ? await getMaterializedSandboxFileResources(db, {
3742
+ accountId: input.accountId,
3743
+ workspaceId: input.workspaceId,
3744
+ sandboxGroupId,
3745
+ expectedEpoch: sandbox.leaseEpoch,
3746
+ instanceId: sandbox.established.instanceId,
3747
+ })
3748
+ : new Set<string>();
3749
+ generatedImageMaterializationCache = {
3750
+ instanceId: sandbox.established.instanceId,
3751
+ fileIds,
3752
+ };
3753
+ }
3754
+ if (generatedImageMaterializationCache.fileIds.has(file.id)) return true;
3755
+ await runWorkspaceMutationForSandbox(
3756
+ sandbox,
3757
+ "generatedImageMaterialization",
3758
+ async () =>
3759
+ await writeGeneratedImageDownload(
3760
+ sessionForDownload as CodemodeTokenWriterSession,
3761
+ download,
3762
+ ),
3763
+ );
3764
+ generatedImageMaterializationCache.fileIds.add(file.id);
3765
+ if (cacheable && sandboxGroupId) {
3766
+ await markSandboxFileResourcesMaterialized(db, {
3767
+ accountId: input.accountId,
3768
+ workspaceId: input.workspaceId,
3769
+ sandboxGroupId,
3770
+ expectedEpoch: sandbox.leaseEpoch,
3771
+ instanceId: sandbox.established.instanceId,
3772
+ fileIds: [file.id],
3773
+ });
3774
+ }
3775
+ return true;
3776
+ } catch (error) {
3777
+ warnGeneratedImageMaterializationDeferred(error);
3778
+ return false;
3779
+ }
3780
+ };
3781
+ const materializeGeneratedImageInOwnedSdkSession = async (
3782
+ receipt: GeneratedImageReceipt,
3783
+ sessionForDownload: unknown,
3784
+ ): Promise<boolean> => {
3785
+ try {
3786
+ const { download } = await prepareGeneratedImageDownload(receipt);
3787
+ await writeGeneratedImageDownload(
3788
+ sessionForDownload as CodemodeTokenWriterSession,
3789
+ download,
3790
+ );
3791
+ return true;
3792
+ } catch (error) {
3793
+ warnGeneratedImageMaterializationDeferred(error);
3794
+ return false;
3795
+ }
3796
+ };
3797
+ const materializeGeneratedImage = async (receipt: GeneratedImageReceipt): Promise<boolean> => {
3798
+ if (modelRunSettings.sandboxBackend === "none") return false;
3799
+ // Never allocate a cloud sandbox merely because durable history contains
3800
+ // an image. If this turn later needs one, its ready hook copies every
3801
+ // known receipt before releasing the first operation.
3802
+ if (resolvedSandbox && setupBoxSession) {
3803
+ return await materializeGeneratedImageInSandbox(receipt, resolvedSandbox, setupBoxSession);
3804
+ }
3805
+ if (sdkOwnedSandboxSession) {
3806
+ return await materializeGeneratedImageInOwnedSdkSession(receipt, sdkOwnedSandboxSession);
3807
+ }
3808
+ return false;
3809
+ };
3810
+ let nativeImageGenerationRetention: {
3811
+ providerId: string;
3812
+ providerBindingHash: string;
3813
+ sessionId: string;
3814
+ turnId: string;
3815
+ attemptId: string;
3816
+ } | null = null;
3817
+ const retainNativeGeneratedImage = async (
3818
+ output: GeneratedImageOutput,
3819
+ ): Promise<GeneratedImageReceipt> => {
3820
+ if (output.providerItemId) {
3821
+ const existing = generatedImageReceiptsByProviderItemId.get(output.providerItemId);
3822
+ if (existing) return existing;
3823
+ }
3824
+ const context = nativeImageGenerationRetention;
3825
+ if (!context || !output.providerItemId) {
3826
+ throw new Error("Native generated image arrived without provider retention identity");
3827
+ }
3828
+ const retained = await retainGeneratedImage({
3829
+ db,
3830
+ objectStorage,
3831
+ accountId: input.accountId,
3832
+ workspaceId: input.workspaceId,
3833
+ sessionId: context.sessionId,
3834
+ turnId: context.turnId,
3835
+ attemptId: context.attemptId,
3836
+ sourceStrategy: "native_hosted",
3837
+ providerId: context.providerId,
3838
+ providerBindingHash: context.providerBindingHash,
3839
+ output,
3840
+ });
3841
+ generatedImageReceiptsByProviderItemId.set(output.providerItemId, retained.receipt);
3842
+ generatedImageReceiptsByArtifactId.set(
3843
+ retained.receipt.artifact.artifactId,
3844
+ retained.receipt,
3845
+ );
3846
+ await materializeGeneratedImage(retained.receipt);
3847
+ return retained.receipt;
3848
+ };
3849
+ const retainNativeGeneratedImagesFromHistory = async (
3850
+ history: Array<Record<string, unknown>>,
3851
+ ): Promise<void> => {
3852
+ collectGeneratedImageReceipts(history, generatedImageReceiptsByProviderItemId);
3853
+ collectGeneratedImageArtifactReceipts(history, generatedImageReceiptsByArtifactId);
3854
+ for (const output of generatedImagesFromHistory(
3855
+ history,
3856
+ generatedImageReceiptsByProviderItemId,
3857
+ )) {
3858
+ if (
3859
+ !output.providerItemId ||
3860
+ generatedImageReceiptsByProviderItemId.has(output.providerItemId)
3861
+ ) {
3862
+ continue;
3863
+ }
3864
+ await retainNativeGeneratedImage(output);
3865
+ }
3866
+ assertGeneratedImageHistoryRetained(history, generatedImageReceiptsByProviderItemId);
3867
+ };
3868
+ const compactMediaRunState = (serialized: string): string =>
3869
+ compactGeneratedImageRunState(
3870
+ compactRetainedScreenshotRunState(serialized, retainedScreenshotReceiptsByCallId),
3871
+ generatedImageReceiptsByProviderItemId,
3872
+ );
3873
+ const compactApprovalRunState = (serialized: string): string =>
3874
+ compactMcpResultCustomDataRunState(compactMediaRunState(serialized));
3875
+ // Explicit image-producing tools cross the durable session-media boundary.
3876
+ // Incidental frames returned by click/scroll actions remain unretained; an
3877
+ // intentional computer_screenshot or view_image result must never be lost
3878
+ // when the event/history sanitizer removes its inline bytes.
3879
+ const retainedSessionImageCallIds = new Set<string>();
3880
+ const retainSessionImageAtToolBoundary: RetainableSessionImageOutputHook = async ({
3881
+ toolCallId,
3882
+ output,
3883
+ }) => {
3884
+ const activeTurnId = turnId;
3885
+ if (!activeTurnId) {
3886
+ throw new Error("Session image tool completed before turn initialization");
3887
+ }
3888
+ const typedScreenshot = typedScreenshotFromToolOutput({
3889
+ callId: toolCallId,
3890
+ output,
3891
+ });
3892
+ retainedSessionImageCallIds.add(toolCallId);
3893
+ if (!typedScreenshot) {
3894
+ if (toolOutputContainsInlineImage(output)) {
3895
+ retainedScreenshotReceiptsByCallId.set(
3896
+ toolCallId,
3897
+ unavailableRetainedSessionImage({
3898
+ sessionId: input.sessionId,
3899
+ turnId: activeTurnId,
3900
+ attemptId: input.attemptId,
3901
+ toolCallId,
3902
+ toolOutputId: toolCallId,
3903
+ reason: "unsupported",
3904
+ }),
3905
+ );
3906
+ }
3907
+ return;
3908
+ }
3909
+ retainedScreenshotReceiptsByCallId.set(
3910
+ toolCallId,
3911
+ unavailableRetainedSessionImage({
3912
+ sessionId: input.sessionId,
3913
+ turnId: activeTurnId,
3914
+ attemptId: input.attemptId,
3915
+ toolCallId,
3916
+ toolOutputId: toolCallId,
3917
+ reason: "pending",
3918
+ }),
3919
+ );
3920
+ retainedScreenshotReceiptsByCallId.set(
3921
+ toolCallId,
3922
+ await retainComputerScreenshot({
3923
+ db,
3924
+ objectStorage,
3925
+ accountId: input.accountId,
3926
+ workspaceId: input.workspaceId,
3927
+ sessionId: input.sessionId,
3928
+ turnId: activeTurnId,
3929
+ attemptId: input.attemptId,
3930
+ output: typedScreenshot,
3931
+ }),
3932
+ );
3933
+ };
3199
3934
  const materializeScreenshotHistory = async (history: Array<Record<string, unknown>>) => {
3200
3935
  collectRetainedScreenshotReceipts(history, retainedScreenshotReceiptsByCallId);
3936
+ if (!modelCanReceiveRetainedSessionImages) return history;
3201
3937
  return await materializeRetainedScreenshotHistory({
3202
3938
  db,
3203
3939
  objectStorage,
@@ -3208,13 +3944,19 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3208
3944
  };
3209
3945
  const materializeScreenshotRunState = async (serialized: string) => {
3210
3946
  collectRetainedScreenshotRunStateReceipts(serialized, retainedScreenshotReceiptsByCallId);
3211
- return await materializeRetainedScreenshotRunState({
3212
- db,
3213
- objectStorage,
3214
- workspaceId: input.workspaceId,
3215
- sessionId: input.sessionId,
3216
- serialized,
3217
- });
3947
+ collectGeneratedImageRunStateArtifactReceipts(serialized, generatedImageReceiptsByArtifactId);
3948
+ const withScreenshots = modelCanReceiveRetainedSessionImages
3949
+ ? await materializeRetainedScreenshotRunState({
3950
+ db,
3951
+ objectStorage,
3952
+ workspaceId: input.workspaceId,
3953
+ sessionId: input.sessionId,
3954
+ serialized,
3955
+ })
3956
+ : serialized;
3957
+ return projectGeneratedImageRunStateForModel(
3958
+ compactGeneratedImageRunState(withScreenshots, generatedImageReceiptsByProviderItemId),
3959
+ );
3218
3960
  };
3219
3961
  let providerArtifactCandidates: Awaited<
3220
3962
  ReturnType<typeof turnInput>
@@ -3237,9 +3979,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3237
3979
  try {
3238
3980
  const rawHistory = (stream.state as { history?: unknown[] }).history;
3239
3981
  if (Array.isArray(rawHistory)) {
3240
- const durableHistory = compactRetainedScreenshotHistory(
3241
- rawHistory as Array<Record<string, unknown>>,
3242
- retainedScreenshotReceiptsByCallId,
3982
+ const typedHistory = rawHistory as Array<Record<string, unknown>>;
3983
+ await retainNativeGeneratedImagesFromHistory(typedHistory);
3984
+ const durableHistory = compactGeneratedImageHistory(
3985
+ compactRetainedScreenshotHistory(typedHistory, retainedScreenshotReceiptsByCallId),
3986
+ generatedImageReceiptsByProviderItemId,
3243
3987
  );
3244
3988
  const { rows, nextWatermark, nextPosition } = historyRowsToAppend(
3245
3989
  durableHistory,
@@ -3305,6 +4049,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3305
4049
  // The Codex account this turn runs on (pin > workspace active), resolved once
3306
4050
  // a codex-billed turn is confirmed and threaded into the token resolver below.
3307
4051
  let effectiveCodexCredentialId: string | null = null;
4052
+ let effectiveXaiCredentialId: string | null = null;
4053
+ let xaiRotationEnabled = false;
4054
+ let xaiAuthoritySnapshot: XaiProviderAccountAuthoritySnapshotV1 | null = null;
4055
+ let xaiRequestContext: XaiSubscriptionRequestContext | null = null;
4056
+ let xaiCredentialQuarantined = false;
3308
4057
  // The session's Codex credential BEFORE this turn resolved its own — captured
3309
4058
  // before recordSessionActiveCodexCredential overwrites the durable pointer, so
3310
4059
  // a per-call usage log can report whether the serving account CHANGED since the
@@ -3324,14 +4073,43 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3324
4073
  // only exists in the RunState blob), never through a swapped trigger.
3325
4074
  let triggerType: string | null = null;
3326
4075
  try {
3327
- const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
3328
- db,
3329
- input.workspaceId,
3330
- settings,
3331
- );
3332
- // Read the active-credential flag once for the runtime capability overlay.
3333
- // Accepted billing/provider identity comes from the turn policy below,
3334
- // never from this mutable health snapshot.
4076
+ const session = await requireSession(db, input.workspaceId, input.sessionId);
4077
+ const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
4078
+ sessionId: input.sessionId,
4079
+ workflowId: input.workflowId,
4080
+ workflowRunId: input.workflowRunId,
4081
+ attemptId: input.attemptId,
4082
+ dispatchId,
4083
+ trigger: input.trigger,
4084
+ });
4085
+ if (claim.action === "unclaimed") {
4086
+ activityStatus = "unclaimed";
4087
+ return { status: "unclaimed", reason: claim.reason };
4088
+ }
4089
+ const turn = claim.turn;
4090
+ turnId = turn.id;
4091
+ executionGeneration = turn.executionGeneration;
4092
+ providerRecoveryCount = providerRecoveryCountFromMetadata(turn.metadata);
4093
+ let installedApiIntegrations: readonly ApiIntegrationRuntime[] = [];
4094
+ const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
4095
+ const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
4096
+ db,
4097
+ input.workspaceId,
4098
+ settings,
4099
+ {
4100
+ ...(credentialSubjectId
4101
+ ? { subjectId: credentialSubjectId }
4102
+ : {
4103
+ personalConnectionDelegations: turn.personalConnectionDelegations,
4104
+ }),
4105
+ onResolvedApiIntegrations: (integrations) => {
4106
+ installedApiIntegrations = integrations;
4107
+ },
4108
+ },
4109
+ );
4110
+ // Read the active-credential flag once for the runtime capability overlay.
4111
+ // Accepted billing/provider identity comes from the turn policy below,
4112
+ // never from this mutable health snapshot.
3335
4113
  const codexSubscriptionActive = await workspaceCodexSubscriptionActive(
3336
4114
  db,
3337
4115
  mcpSettings,
@@ -3343,32 +4121,18 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3343
4121
  mcpSettings,
3344
4122
  codexSubscriptionActive,
3345
4123
  );
4124
+ const xaiSettings = codexSettings.supergrokSubscriptionEnabled
4125
+ ? withXaiSubscriptionProvider(codexSettings)
4126
+ : codexSettings;
3346
4127
  const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
3347
4128
  db,
3348
4129
  input.workspaceId,
3349
- codexSettings,
4130
+ xaiSettings,
3350
4131
  );
3351
4132
  const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled
3352
4133
  ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId)
3353
4134
  : null;
3354
4135
  runtime.configure(capabilitySettings);
3355
- const session = await requireSession(db, input.workspaceId, input.sessionId);
3356
- const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
3357
- sessionId: input.sessionId,
3358
- workflowId: input.workflowId,
3359
- workflowRunId: input.workflowRunId,
3360
- attemptId: input.attemptId,
3361
- dispatchId,
3362
- trigger: input.trigger,
3363
- });
3364
- if (claim.action === "unclaimed") {
3365
- activityStatus = "unclaimed";
3366
- return { status: "unclaimed", reason: claim.reason };
3367
- }
3368
- const turn = claim.turn;
3369
- turnId = turn.id;
3370
- executionGeneration = turn.executionGeneration;
3371
- providerRecoveryCount = providerRecoveryCountFromMetadata(turn.metadata);
3372
4136
  const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
3373
4137
  const policyForAbsent =
3374
4138
  claimedPolicy.kind === "valid"
@@ -3402,6 +4166,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3402
4166
  const billingIdentity = turnExecutionPolicyBillingIdentity(turnExecutionPolicy);
3403
4167
  isExternallyBilledTurn = billingIdentity.externallyBilled;
3404
4168
  isCodexTurn = billingIdentity.codexSubscription;
4169
+ isXaiTurn = billingIdentity.xaiSubscription;
3405
4170
  triggerEventId = turn.triggerEventId;
3406
4171
  const trigger = await getSessionEvent(db, input.workspaceId, triggerEventId);
3407
4172
  if (!trigger) {
@@ -3413,6 +4178,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3413
4178
  input.sessionId,
3414
4179
  trigger,
3415
4180
  );
4181
+ const interactionInterventionResume = await getInteractionInterventionResumeForEvent(
4182
+ db,
4183
+ input.workspaceId,
4184
+ input.sessionId,
4185
+ trigger,
4186
+ );
3416
4187
  triggerType = trigger.type;
3417
4188
  redispatchesAtDispatch = Number(
3418
4189
  (turn.metadata as { workerDeathRedispatches?: number } | null)?.workerDeathRedispatches ??
@@ -3499,14 +4270,21 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3499
4270
  inputs,
3500
4271
  {
3501
4272
  onAppend: ({ durationSeconds }) =>
3502
- recordSessionEventAppendLatency(observability, { durationSeconds }),
4273
+ recordSessionEventAppendLatency(observability, {
4274
+ durationSeconds,
4275
+ }),
3503
4276
  onPublish: ({ durationSeconds }) =>
3504
- recordSessionEventPublishLatency(observability, { durationSeconds }),
4277
+ recordSessionEventPublishLatency(observability, {
4278
+ durationSeconds,
4279
+ }),
3505
4280
  },
3506
4281
  );
3507
4282
  if (inputs.length > 0 && !appended.accepted) {
3508
4283
  throw new TurnAttemptFencedError("turn execution generation was fenced");
3509
4284
  }
4285
+ if (inputs.length > 0) {
4286
+ turnLifecycleMetricsFor(observability).progress(turnId!);
4287
+ }
3510
4288
  activityContext?.heartbeat({
3511
4289
  ...heartbeatDetails,
3512
4290
  phase: "events_published",
@@ -3565,10 +4343,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3565
4343
  const runState = inputSettlement.runState
3566
4344
  ? {
3567
4345
  ...inputSettlement.runState,
3568
- serializedRunState: compactRetainedScreenshotRunState(
3569
- inputSettlement.runState.serializedRunState,
3570
- retainedScreenshotReceiptsByCallId,
3571
- ),
4346
+ serializedRunState: compactMediaRunState(inputSettlement.runState.serializedRunState),
3572
4347
  }
3573
4348
  : undefined;
3574
4349
  const result = await applySessionTurnSettlement(db, input.workspaceId, {
@@ -3603,6 +4378,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3603
4378
  turnMetricOutcome = "cancelled";
3604
4379
  return false;
3605
4380
  }
4381
+ turnLifecycleMetricsFor(observability).progress(turnId!);
3606
4382
  if (recordingForSettlement && preparedRecording) {
3607
4383
  if (result.recordingMutationApplied) {
3608
4384
  activeRecording = null;
@@ -3797,14 +4573,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3797
4573
  leased.credentialId !== null &&
3798
4574
  (sessionPinSource !== "policy" || sessionPin !== leased.credentialId)
3799
4575
  ) {
3800
- const pinMutation = await withCodexCapacityMutation(
4576
+ const pinMutation = await withSessionCodexCapacityMutation(
3801
4577
  db,
3802
4578
  {
3803
4579
  workspaceId: input.workspaceId,
3804
4580
  reason: "codex_policy_pin_changed",
3805
4581
  },
3806
4582
  async (tx) => {
3807
- const changed = await setSessionCodexPin(
4583
+ const changed = await setSessionCodexPinInTransaction(
3808
4584
  tx,
3809
4585
  input.workspaceId,
3810
4586
  input.sessionId,
@@ -3825,14 +4601,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3825
4601
  pinMutation.wakeTargets,
3826
4602
  );
3827
4603
  } else if (selectedPinDisposition === "clearStale") {
3828
- const pinMutation = await withCodexCapacityMutation(
4604
+ const pinMutation = await withSessionCodexCapacityMutation(
3829
4605
  db,
3830
4606
  {
3831
4607
  workspaceId: input.workspaceId,
3832
4608
  reason: "codex_stale_policy_pin_cleared",
3833
4609
  },
3834
4610
  async (tx) => {
3835
- const changed = await setSessionCodexPin(
4611
+ const changed = await setSessionCodexPinInTransaction(
3836
4612
  tx,
3837
4613
  input.workspaceId,
3838
4614
  input.sessionId,
@@ -4250,14 +5026,220 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4250
5026
  }
4251
5027
  }
4252
5028
 
5029
+ if (isXaiTurn) {
5030
+ const authoritySnapshot = turn.xaiProviderAccountAuthoritySnapshot;
5031
+ xaiAuthoritySnapshot = authoritySnapshot;
5032
+ const subjectId =
5033
+ authoritySnapshot.scope === "user"
5034
+ ? turn.initiatingHumanSubjectId
5035
+ : "worker:xai-workspace";
5036
+ if (!subjectId) {
5037
+ throw new Error("User-scoped SuperGrok work has no frozen initiating human");
5038
+ }
5039
+ const sessionPin = await getXaiSessionAccountPin(db, {
5040
+ workspaceId: input.workspaceId,
5041
+ subjectId,
5042
+ sessionId: input.sessionId,
5043
+ authoritySnapshot,
5044
+ });
5045
+ const leaseStartedAtMs = performance.now();
5046
+ const leased = await acquireXaiCredentialLease(db, {
5047
+ accountId: input.accountId,
5048
+ workspaceId: input.workspaceId,
5049
+ subjectId,
5050
+ sessionId: input.sessionId,
5051
+ turnId: turn.id,
5052
+ holderId: dispatchId,
5053
+ authoritySnapshot,
5054
+ pinnedCredentialId: sessionPin?.pinnedCredentialId ?? null,
5055
+ pinSource:
5056
+ sessionPin?.pinSource === "manual" || sessionPin?.pinSource === "policy"
5057
+ ? sessionPin.pinSource
5058
+ : null,
5059
+ });
5060
+ effectiveXaiCredentialId = leased.credentialId;
5061
+ xaiRotationEnabled = leased.rotationEnabled;
5062
+ xaiLeaseSubjectId = subjectId;
5063
+ xaiLeaseHolderId = leased.holderId;
5064
+ xaiLeaseGeneration = leased.generation;
5065
+ xaiLeaseConfirmedUntilMs = leased.leasedUntil
5066
+ ? leaseStartedAtMs + XAI_CREDENTIAL_LEASE_TTL_MS
5067
+ : null;
5068
+ xaiLeaseHeld =
5069
+ effectiveXaiCredentialId !== null &&
5070
+ leased.holderId !== null &&
5071
+ leased.generation !== null &&
5072
+ xaiLeaseConfirmedUntilMs !== null;
5073
+ if (!effectiveXaiCredentialId) {
5074
+ const connected = leased.accounts.length;
5075
+ const allocatorEnabled = leased.accounts.filter(
5076
+ (account) => account.allocatorEnabled,
5077
+ ).length;
5078
+ if (connected === 0) {
5079
+ throw Object.assign(
5080
+ new Error("No SuperGrok subscription account is connected for this authority scope"),
5081
+ { code: "xai_not_connected" },
5082
+ );
5083
+ }
5084
+ if (turn.source === "compaction") {
5085
+ if (
5086
+ !(await settle!({
5087
+ events: [
5088
+ {
5089
+ type: "turn.cancelled",
5090
+ payload: {
5091
+ maintenance: "context_compaction",
5092
+ reason: "xai_capacity_unavailable",
5093
+ requestPreserved: true,
5094
+ },
5095
+ },
5096
+ {
5097
+ type: "session.status.changed",
5098
+ payload: { status: "idle" },
5099
+ },
5100
+ ],
5101
+ turnStatus: "cancelled",
5102
+ sessionStatus: "idle",
5103
+ activeTurnId: null,
5104
+ }))
5105
+ ) {
5106
+ return claimedResult({ status: "cancelled" });
5107
+ }
5108
+ turnMetricOutcome = "cancelled";
5109
+ activityStatus = "idle";
5110
+ return claimedResult({ status: "idle", deferredUntilWake: true });
5111
+ }
5112
+ const goal = await getSessionGoal(db, input.workspaceId, input.sessionId).catch(
5113
+ () => null,
5114
+ );
5115
+ const activeGoal = goal?.status === "active" ? goal : null;
5116
+ const now = new Date();
5117
+ const futureResets = leased.accounts
5118
+ .map((account) => account.exhaustedUntil)
5119
+ .filter((date): date is Date => date !== null && date > now);
5120
+ const earliestResetAt = futureResets.length
5121
+ ? new Date(Math.min(...futureResets.map((date) => date.getTime())))
5122
+ : null;
5123
+ const error =
5124
+ allocatorEnabled === 0
5125
+ ? "All connected SuperGrok subscription accounts are disabled for allocation"
5126
+ : "All connected SuperGrok subscription accounts are temporarily unavailable";
5127
+ const armed = await armXaiCapacityWait(db, {
5128
+ accountId: input.accountId,
5129
+ workspaceId: input.workspaceId,
5130
+ subjectId,
5131
+ sessionId: input.sessionId,
5132
+ turnId: turn.id,
5133
+ attemptId: input.attemptId,
5134
+ workflowId: input.workflowId,
5135
+ authoritySnapshot,
5136
+ goalId: activeGoal?.id ?? null,
5137
+ goalVersion: activeGoal?.version ?? null,
5138
+ earliestResetAt,
5139
+ failurePayload: {
5140
+ error,
5141
+ code: allocatorEnabled === 0 ? "xai_allocator_disabled" : "xai_capacity_unavailable",
5142
+ detail: "waiting for an eligible account, reconnect, pin change, or quota reset",
5143
+ },
5144
+ });
5145
+ if (armed.action === "waiting") {
5146
+ await publishDurableSessionEvents(
5147
+ bus,
5148
+ input.workspaceId,
5149
+ input.sessionId,
5150
+ armed.events,
5151
+ );
5152
+ turnMetricOutcome = "recovering";
5153
+ activityStatus = "waiting_capacity";
5154
+ return claimedResult({
5155
+ status: "waiting_capacity",
5156
+ capacityWait: {
5157
+ provider: "xai",
5158
+ waiterId: armed.waiter.id,
5159
+ generation: armed.waiter.generation,
5160
+ nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
5161
+ wakeRevision: armed.waiter.wakeRevision,
5162
+ },
5163
+ });
5164
+ }
5165
+ if (
5166
+ !(await settle!({
5167
+ events: [
5168
+ {
5169
+ type: "turn.failed",
5170
+ payload: {
5171
+ error,
5172
+ code: "xai_capacity_wait_stale",
5173
+ retryable: false,
5174
+ recovery: "user_message",
5175
+ },
5176
+ },
5177
+ { type: "session.status.changed", payload: { status: "idle" } },
5178
+ ],
5179
+ turnStatus: "failed",
5180
+ sessionStatus: "idle",
5181
+ activeTurnId: null,
5182
+ }))
5183
+ ) {
5184
+ return claimedResult({ status: "cancelled" });
5185
+ }
5186
+ turnMetricOutcome = "failed";
5187
+ activityStatus = "idle";
5188
+ return claimedResult({ status: "idle" });
5189
+ }
5190
+ if (xaiLeaseHeld) startXaiLeaseHeartbeat();
5191
+ if (
5192
+ leased.rotationEnabled &&
5193
+ sessionPin?.pinSource !== "manual" &&
5194
+ (sessionPin?.pinnedCredentialId !== effectiveXaiCredentialId ||
5195
+ sessionPin?.pinSource !== "policy")
5196
+ ) {
5197
+ await setXaiSessionAccountPin(db, {
5198
+ accountId: input.accountId,
5199
+ workspaceId: input.workspaceId,
5200
+ subjectId,
5201
+ sessionId: input.sessionId,
5202
+ authoritySnapshot,
5203
+ credentialId: effectiveXaiCredentialId,
5204
+ pinSource: "policy",
5205
+ expectedVersion: sessionPin?.version ?? null,
5206
+ }).catch((error: unknown) => {
5207
+ if (error instanceof Error && error.message === "xAI session pin changed") return;
5208
+ throw error;
5209
+ });
5210
+ } else if (!leased.rotationEnabled && sessionPin?.pinSource === "policy") {
5211
+ await setXaiSessionAccountPin(db, {
5212
+ accountId: input.accountId,
5213
+ workspaceId: input.workspaceId,
5214
+ subjectId,
5215
+ sessionId: input.sessionId,
5216
+ authoritySnapshot,
5217
+ credentialId: null,
5218
+ pinSource: null,
5219
+ expectedVersion: sessionPin.version,
5220
+ }).catch((error: unknown) => {
5221
+ if (error instanceof Error && error.message === "xAI session pin changed") return;
5222
+ throw error;
5223
+ });
5224
+ }
5225
+ await recordXaiSessionLastAccount(db, {
5226
+ accountId: input.accountId,
5227
+ workspaceId: input.workspaceId,
5228
+ subjectId,
5229
+ sessionId: input.sessionId,
5230
+ authoritySnapshot,
5231
+ credentialId: effectiveXaiCredentialId,
5232
+ });
5233
+ }
5234
+
4253
5235
  // Pack-scoped runtime: enabled packs may declare the sandbox image this
4254
5236
  // workspace's sessions run in and skills for the sandbox skill index.
4255
5237
  // Resolved after turn.started so a composition conflict (two enabled
4256
5238
  // packs declaring images) fails the turn with its plain error instead
4257
5239
  // of failing the activity opaquely.
4258
- const [packRuntime, skillLibraryRuntime] = await Promise.all([
5240
+ const [packRuntime, installedSkillRuntime] = await Promise.all([
4259
5241
  resolveWorkspacePackRuntime(db, input.workspaceId),
4260
- resolveWorkspaceSkillLibraryRuntime(db, input.workspaceId),
5242
+ resolveWorkspaceInstalledSkillRuntime(db, input.workspaceId),
4261
5243
  ]);
4262
5244
  // RIG BINDING (M3): load the session's FROZEN rig version (resolved+frozen
4263
5245
  // at create). Everything rig-derived below (image precedence, env default
@@ -4292,38 +5274,57 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4292
5274
  attemptId: input.attemptId,
4293
5275
  executionGeneration: turn.executionGeneration,
4294
5276
  };
4295
- const [workspace, instructionPolicySnapshot, preferenceSnapshot] = await Promise.all([
4296
- getWorkspace(db, input.workspaceId),
4297
- getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
4298
- getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
4299
- if (error instanceof PreferenceRegistryInitiatorError) return null;
4300
- throw error;
4301
- }),
4302
- ]);
5277
+ const [workspace, companyProfileSnapshot, instructionPolicySnapshot, preferenceSnapshot] =
5278
+ await Promise.all([
5279
+ getWorkspace(db, input.workspaceId),
5280
+ getOrCreateCompanyProfileSnapshot(db, governanceClaims),
5281
+ getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
5282
+ getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
5283
+ if (error instanceof PreferenceRegistryInitiatorError) return null;
5284
+ throw error;
5285
+ }),
5286
+ ]);
4303
5287
  if (!workspace) throw new Error(`Workspace not found: ${input.workspaceId}`);
4304
5288
  const workspaceAgentInstructions = workspace.agentInstructions;
4305
5289
  const agentHumanInputEnabled = resolveWorkspaceAgentHumanInputEnabled(workspace.settings);
5290
+ const memoryPromptMode = resolveWorkspaceMemoryPromptMode(workspace.settings);
4306
5291
  assertWorkspaceHumanInputAllowed(agentHumanInputEnabled, "resume", humanInputResume !== null);
4307
- const workspaceGovernance = renderWorkspaceGovernanceContext({
4308
- instructionPolicy: instructionPolicySnapshot,
4309
- preferences: preferenceSnapshot,
4310
- });
5292
+ const companyProfileIncluded =
5293
+ memoryPromptMode === "legacy_standing" || session.nestedAgentDepth === 0;
5294
+ const workspaceGovernance = renderWorkspaceGovernanceContext(
5295
+ {
5296
+ companyProfile: companyProfileSnapshot,
5297
+ instructionPolicy: instructionPolicySnapshot,
5298
+ preferences: preferenceSnapshot,
5299
+ },
5300
+ {
5301
+ includeCompanyProfile: companyProfileIncluded,
5302
+ },
5303
+ );
4311
5304
  const structuredWorkspacePolicyActive =
4312
5305
  hasActiveWorkspaceInstructionPolicy(instructionPolicySnapshot);
4313
5306
  const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
4314
- const baseRunSettings = {
4315
- // IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
4316
- // OUTERMOST so a rig-pinned image overrides both the pack image and the
4317
- // deployment default; a rig with no image (or a rig-less turn) is a
4318
- // pass-through, leaving the pack/deployment chain exactly as today.
4319
- ...settingsWithRigImage(
4320
- settingsWithPackSandboxImage(
4321
- capabilitySettings,
4322
- packRuntime.sandboxImage,
4323
- packRuntime.sandboxProviderImages,
4324
- ),
4325
- rigVersion?.image ?? null,
5307
+ const logicalSandboxSettings = settingsWithRigImage(
5308
+ settingsWithPackSandboxImage(
5309
+ capabilitySettings,
5310
+ packRuntime.sandboxImage,
5311
+ packRuntime.sandboxProviderImages,
4326
5312
  ),
5313
+ rigVersion?.image ?? null,
5314
+ );
5315
+ const providerImageSettings = await settingsWithRigProviderImage(
5316
+ logicalSandboxSettings,
5317
+ rigVersion,
5318
+ turn.sandboxBackend,
5319
+ );
5320
+ const baseRunSettings = {
5321
+ // IMAGE PRECEDENCE: rig > pre-V2 Pack compatibility > deployment.
5322
+ // resolveWorkspacePackRuntime returns no image for V2 Pack rows, so
5323
+ // settingsWithRigImage runs outermost over only the intentionally
5324
+ // retained legacy fallback. A matching verified provider-native ID is
5325
+ // then applied only to fresh creation without changing the logical
5326
+ // lease image.
5327
+ ...providerImageSettings,
4327
5328
  openaiModel: turn.model,
4328
5329
  openaiReasoningEffort: turn.reasoningEffort,
4329
5330
  sandboxBackend: turn.sandboxBackend,
@@ -4353,9 +5354,19 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4353
5354
  capabilitySettings,
4354
5355
  turnExecutionPolicy.productModelId,
4355
5356
  );
4356
- const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
4357
5357
  const providerApi = resolvedModel?.provider.api ?? "responses";
5358
+ const nativeImageProviderBinding =
5359
+ providerApi === "responses"
5360
+ ? openAiHostedImageProviderBindingForTurn(capabilitySettings, resolvedModel)
5361
+ : null;
5362
+ const lazyToolTransport = lazyToolTransportForTurn(resolvedModel);
4358
5363
  const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
5364
+ // Use the proven wire capability, not the catalogue modality alone. Chat
5365
+ // providers may advertise vision, but OpenGeni intentionally has no typed
5366
+ // image transport for that wire yet; exposing view_image there would turn
5367
+ // pixels into a multi-megabyte text/base64 function result.
5368
+ const supportsImageInput = modelInputPolicy.supportsImageInput;
5369
+ modelCanReceiveRetainedSessionImages = supportsImageInput;
4359
5370
  const attachmentProjector = createModelHistoryAttachmentProjector(
4360
5371
  db,
4361
5372
  input.workspaceId,
@@ -4364,8 +5375,20 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4364
5375
  );
4365
5376
  const modelHistoryProjector = async (items: Array<Record<string, unknown>>) =>
4366
5377
  projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
5378
+ const generatedImageHistoryProjector = async (
5379
+ items: Array<Record<string, unknown>>,
5380
+ ): Promise<Array<Record<string, unknown>>> => {
5381
+ collectGeneratedImageReceipts(items, generatedImageReceiptsByProviderItemId);
5382
+ collectGeneratedImageArtifactReceipts(items, generatedImageReceiptsByArtifactId);
5383
+ return projectGeneratedImageHistoryForModel(items);
5384
+ };
4367
5385
  const compactionModelHistoryProjector = async (items: Array<Record<string, unknown>>) =>
4368
- await modelHistoryProjector(projectHistoryForProvider(items, providerApi));
5386
+ await modelHistoryProjector(
5387
+ projectHistoryForProvider(
5388
+ await generatedImageHistoryProjector(restoreGenericDispatchHistoryItems(items)),
5389
+ providerApi,
5390
+ ),
5391
+ );
4369
5392
  // Bind the provider/model catalog's context policy to every model-facing
4370
5393
  // path for this turn. In particular, Codex subscription turns must not
4371
5394
  // inherit the deployment's OpenAI/Azure mode or 1.05M context defaults:
@@ -4399,6 +5422,44 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4399
5422
  }
4400
5423
  }
4401
5424
  }
5425
+ // A recovered asynchronous video completion is model-visible only after
5426
+ // its durable File is present at the exact sandbox path carried by the
5427
+ // receipt. Resolve and verify the claimed update before sandbox policy is
5428
+ // chosen so this turn cannot remain lazy and expose an absent path.
5429
+ for (const update of await listSessionSystemUpdatesForTurn(
5430
+ db,
5431
+ input.workspaceId,
5432
+ input.sessionId,
5433
+ turn.id,
5434
+ )) {
5435
+ const payload = update.payload as MediaGenerationResult;
5436
+ if (payload.type !== "media_generation_result" || payload.status !== "ready") continue;
5437
+ const retained = await getGeneratedVideoArtifact(
5438
+ db,
5439
+ input.workspaceId,
5440
+ payload.receipt.artifact.artifactId,
5441
+ );
5442
+ if (
5443
+ !retained ||
5444
+ retained.artifact.deletedAt ||
5445
+ retained.file.status !== "ready" ||
5446
+ retained.file.contentType !== "video/mp4" ||
5447
+ retained.file.sizeBytes !== payload.receipt.artifact.originalBytes ||
5448
+ retained.file.sha256 !== payload.receipt.artifact.sha256 ||
5449
+ retained.artifact.sandboxFilename !== `generated-video-${retained.artifact.id}.mp4`
5450
+ ) {
5451
+ throw new Error("Generated video completion does not match its retained File");
5452
+ }
5453
+ requiredGeneratedVideoFiles.push({
5454
+ operationId: payload.operationId,
5455
+ artifactId: retained.artifact.id,
5456
+ fileId: retained.file.id,
5457
+ objectKey: retained.file.objectKey,
5458
+ sizeBytes: retained.file.sizeBytes,
5459
+ sha256: retained.file.sha256,
5460
+ filename: retained.artifact.sandboxFilename,
5461
+ });
5462
+ }
4402
5463
  // A codex-subscription turn resolves the bearer for THIS turn's effective
4403
5464
  // codex account (effectiveCodexCredentialId; pin > workspace-active) at
4404
5465
  // model-call time — multi-account P1 means a workspace can hold N accounts,
@@ -4441,6 +5502,31 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4441
5502
  onRequestOpaqueArtifacts: ({ fingerprints }) => {
4442
5503
  lastCodexRequestOpaqueArtifacts = fingerprints;
4443
5504
  },
5505
+ onModelRequestDiagnostic: (event) => {
5506
+ const phase =
5507
+ event.phase === "headers"
5508
+ ? "headers"
5509
+ : event.phase === "first_byte"
5510
+ ? "first_byte"
5511
+ : event.phase === "completed" ||
5512
+ event.phase === "failed" ||
5513
+ event.phase === "timed_out"
5514
+ ? "terminal"
5515
+ : null;
5516
+ if (!phase) return;
5517
+ recordModelRequestPhase(observability, {
5518
+ provider: "codex-subscription",
5519
+ phase,
5520
+ ...(event.phase === "completed"
5521
+ ? { outcome: "completed" as const }
5522
+ : event.phase === "failed"
5523
+ ? { outcome: "failed" as const }
5524
+ : event.phase === "timed_out"
5525
+ ? { outcome: "timed_out" as const }
5526
+ : {}),
5527
+ durationSeconds: event.durationMs / 1000,
5528
+ });
5529
+ },
4444
5530
  nextRequestId: () => `${dispatchId}:${++codexModelRequestSequence}`,
4445
5531
  onModelRequestEvent: async (event) => {
4446
5532
  if (!publish || !turnId) {
@@ -4463,8 +5549,35 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4463
5549
  };
4464
5550
  })()
4465
5551
  : null;
5552
+ if (resolvedModel?.provider.kind === "xai-subscription") {
5553
+ if (!effectiveXaiCredentialId || !xaiLeaseSubjectId) {
5554
+ throw new Error("SuperGrok subscription execution has no leased credential");
5555
+ }
5556
+ let xaiModelRequestSequence = 0;
5557
+ const authorization = await buildXaiTurnRequestAuthorization({
5558
+ db,
5559
+ settings: runSettings,
5560
+ accountId: input.accountId,
5561
+ workspaceId: input.workspaceId,
5562
+ subjectId: xaiLeaseSubjectId,
5563
+ sessionId: input.sessionId,
5564
+ turnId: turn.id,
5565
+ credentialId: effectiveXaiCredentialId,
5566
+ authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot,
5567
+ hostedSearch: {
5568
+ webSearch: runSettings.webSearchEnabled,
5569
+ xSearch: runSettings.webSearchEnabled,
5570
+ },
5571
+ nextRequestId: () => `${dispatchId}:xai:${++xaiModelRequestSequence}`,
5572
+ });
5573
+ xaiRequestContext = authorization.context;
5574
+ }
4466
5575
  const withCodex = <T>(fn: () => Promise<T>): Promise<T> =>
4467
5576
  codexContext ? codexRequestStorage.run(codexContext, fn) : fn();
5577
+ const withProviderRequestContext = <T>(fn: () => Promise<T>): Promise<T> =>
5578
+ xaiRequestContext
5579
+ ? xaiSubscriptionRequestStorage.run(xaiRequestContext, fn)
5580
+ : withCodex(fn);
4468
5581
  const withCodexRemoteCompaction = <T>(fn: () => Promise<T>): Promise<T> =>
4469
5582
  withCodex(() =>
4470
5583
  withCodexRequestOverrides(
@@ -4508,17 +5621,18 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4508
5621
  servingCredentialId: effectiveCodexCredentialId,
4509
5622
  priorSessionCredentialId: priorSessionCodexCredentialId,
4510
5623
  emittedSourceKeys: emittedModelUsageSourceKeys,
4511
- renewLease: () => renewCodexLease("model_usage"),
4512
- leaseLost: () => codexLeaseLost,
4513
- leaseLostMessage: "Codex credential lease expired during context compaction",
5624
+ renewLease: () => renewServingCredentialLease("model_usage"),
5625
+ leaseLost: servingCredentialLeaseLost,
5626
+ leaseLostMessage: "Provider credential lease expired during context compaction",
4514
5627
  });
4515
5628
  };
4516
5629
  const compactionSummarizerFor = (systemInstructions?: string) =>
4517
5630
  resolvedModel
4518
5631
  ? (s: Settings, m: Array<Record<string, unknown>>) =>
4519
- withCodex(() =>
4520
- summarizeForCompaction(s, m, {
5632
+ withProviderRequestContext(() =>
5633
+ summarizeContextForCompaction(s, m, {
4521
5634
  client: resolvedModel.client,
5635
+ provider: resolvedModel.provider,
4522
5636
  api: resolvedModel.provider.api,
4523
5637
  model: turnExecutionPolicy.upstreamModelId,
4524
5638
  maxOutputTokens: SUMMARY_BUFFER_TOKENS,
@@ -4528,7 +5642,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4528
5642
  }),
4529
5643
  )
4530
5644
  : (s: Settings, m: Array<Record<string, unknown>>) =>
4531
- summarizeForCompaction(s, m, {
5645
+ summarizeContextForCompaction(s, m, {
4532
5646
  model: turnExecutionPolicy.upstreamModelId,
4533
5647
  maxOutputTokens: SUMMARY_BUFFER_TOKENS,
4534
5648
  onUsage: recordCompactionUsage,
@@ -4583,6 +5697,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4583
5697
  }
4584
5698
  return requestRemoteCompactionV2(s, m, {
4585
5699
  client: resolvedModel.client,
5700
+ provider: resolvedModel.provider,
4586
5701
  model: turnExecutionPolicy.upstreamModelId,
4587
5702
  systemInstructions: remoteCompactionInstructions,
4588
5703
  onUsage: recordCompactionUsage,
@@ -4628,20 +5743,23 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4628
5743
  };
4629
5744
  const compactionInstructions = appendWorkspaceMemory(
4630
5745
  appendPersistentSessionSettings(
4631
- appendSessionInstructions(
4632
- appendWorkspaceGovernance(
4633
- composeAgentInstructions(
4634
- structuredWorkspacePolicyActive
4635
- ? modelRunSettings.agentInstructionsTemplate
4636
- : (workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate),
4637
- undefined,
4638
- rigVersion && rigName
4639
- ? { name: rigName, version: rigVersion.version }
4640
- : undefined,
5746
+ appendSessionGoal(
5747
+ appendSessionInstructions(
5748
+ appendWorkspaceGovernance(
5749
+ composeAgentInstructions(
5750
+ structuredWorkspacePolicyActive
5751
+ ? modelRunSettings.agentInstructionsTemplate
5752
+ : (workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate),
5753
+ undefined,
5754
+ rigVersion && rigName
5755
+ ? { name: rigName, version: rigVersion.version }
5756
+ : undefined,
5757
+ ),
5758
+ workspaceGovernance ?? undefined,
4641
5759
  ),
4642
- workspaceGovernance ?? undefined,
5760
+ session.instructions ?? undefined,
4643
5761
  ),
4644
- session.instructions ?? undefined,
5762
+ turn.goalSnapshot,
4645
5763
  ),
4646
5764
  persistentSessionSettings,
4647
5765
  ),
@@ -4897,6 +6015,15 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4897
6015
  activeSandboxBackend === "selfhosted" &&
4898
6016
  Boolean(activeSandboxPointer?.activeSandboxId) &&
4899
6017
  Boolean(activeSandboxRecord?.enrollmentId);
6018
+ // `none` describes the durable home, not an explicit per-turn route. Give
6019
+ // the runtime the effective backend so it builds a SandboxAgent for the
6020
+ // attached Connected Machine without mutating the session or turn record.
6021
+ if (machinePrimary && modelRunSettings.sandboxBackend === "none") {
6022
+ modelRunSettings = {
6023
+ ...modelRunSettings,
6024
+ sandboxBackend: "selfhosted",
6025
+ };
6026
+ }
4900
6027
  // The backend that can actually create a sandbox for this turn. In the
4901
6028
  // common path this is runSettings.sandboxBackend. A selfhosted home turn
4902
6029
  // that is NOT machine-primary falls back to the deployment cloud backend
@@ -4965,7 +6092,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4965
6092
  // Resolved run credentials must be written to one exact leased sandbox
4966
6093
  // before agent execution. A warm active pointer can bypass the lazy
4967
6094
  // provisioner entirely, which previously skipped materialization.
4968
- !initialRunCredentialMaterial
6095
+ !initialRunCredentialMaterial &&
6096
+ requiredGeneratedVideoFiles.length === 0
4969
6097
  ? "on-demand"
4970
6098
  : "eager";
4971
6099
  // Computed exactly ONCE per turn and reused for BOTH the box manifest
@@ -5019,17 +6147,25 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5019
6147
  turn,
5020
6148
  })
5021
6149
  : undefined;
5022
- const toolspaceAuthority = {
6150
+ const codemodeAuthority = {
5023
6151
  sessionId: input.sessionId,
6152
+ turnId: turn.id,
6153
+ attemptId: input.attemptId,
6154
+ executionGeneration: turn.executionGeneration,
5024
6155
  };
6156
+ const sandboxArtifactRuntime = sandboxArtifactRuntimeAdmission(
6157
+ settings,
6158
+ runSettings,
6159
+ activeSandboxBackend ?? groupBoxBackend,
6160
+ );
5025
6161
  const {
5026
- environment: sandboxEnvironment,
6162
+ environment: baseSandboxEnvironment,
5027
6163
  gitToken: sandboxGitToken,
5028
6164
  gitTokens: sandboxGitTokens,
5029
6165
  gitTokenExpiresAt: sandboxGitTokenExpiresAt,
5030
6166
  gitCredentialBindings: sandboxGitCredentialBindings,
5031
- toolspaceToken: sandboxToolspaceToken,
5032
- toolspaceTokenExpiresAt: sandboxToolspaceTokenExpiresAt,
6167
+ codemodeToken: sandboxCodemodeToken,
6168
+ codemodeTokenExpiresAt: sandboxCodemodeTokenExpiresAt,
5033
6169
  } = await waitForTurnOperation(
5034
6170
  sandboxEnvironmentForRun(
5035
6171
  runSettings,
@@ -5039,22 +6175,45 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5039
6175
  sandboxWorkspaceEnvironmentValues,
5040
6176
  {
5041
6177
  skipGitHubToken: activeSandboxBackend === "selfhosted",
5042
- skipToolspace: activeSandboxBackend === "selfhosted",
6178
+ codemodeDelivery:
6179
+ activeSandboxBackend === "selfhosted" ? "transient_exec" : "managed_file",
5043
6180
  deferGitHubToken:
5044
6181
  activeSandboxBackend !== "selfhosted" && establishPolicy === "on-demand",
5045
6182
  scope: connectionScope,
5046
6183
  ...(gitCredentialAuthority ? { authority: gitCredentialAuthority } : {}),
5047
6184
  gitCredentials: connectionCredentials?.gitCredentials,
5048
6185
  authorizeGitHubTokenMint,
5049
- toolspaceAuthority,
6186
+ codemodeAuthority,
5050
6187
  },
5051
6188
  ),
5052
6189
  cancellationSignal,
5053
6190
  undefined,
5054
6191
  );
6192
+ // Reserved, image-owned paths win over workspace/session variables. The
6193
+ // exact same merged object feeds both box manifest and agent declaration,
6194
+ // preserving the no-environment-delta invariant.
6195
+ const sandboxEnvironment = sandboxArtifactRuntime.available
6196
+ ? { ...baseSandboxEnvironment, ...sandboxArtifactRuntime.environment }
6197
+ : baseSandboxEnvironment;
6198
+
6199
+ // One mutable in-memory bearer cell serves every Connected Machine route
6200
+ // in this attempt. SelfhostedSession snapshots it into each exact exec
6201
+ // request; it never enters the manifest, argv, filesystem, RunState, or
6202
+ // serialized session state. Managed renewal updates the same cell so a
6203
+ // later mid-turn swap sees the fresh bearer too.
6204
+ const codemodeTokenState = sandboxCodemodeToken ? { token: sandboxCodemodeToken } : undefined;
6205
+ const transientCodemodeEnvironment = codemodeTokenState
6206
+ ? (): Readonly<Record<string, string>> => ({
6207
+ OPENGENI_CODEMODE_URL: codemodeWorkspaceUrl(runSettings, input.workspaceId),
6208
+ OPENGENI_CODEMODE_TOKEN: codemodeTokenState.token,
6209
+ ...(runSettings.ogtoolPackageSpec
6210
+ ? { OPENGENI_OGTOOL_PACKAGE_SPEC: runSettings.ogtoolPackageSpec }
6211
+ : {}),
6212
+ })
6213
+ : undefined;
5055
6214
 
5056
- const sandboxToolspaceTokenFile = sandboxToolspaceToken
5057
- ? toolspaceTokenFileFromEnvironment(sandboxEnvironment, input.sessionId)
6215
+ const sandboxCodemodeTokenFile = sandboxCodemodeToken
6216
+ ? codemodeTokenFileFromEnvironment(sandboxEnvironment, input.sessionId)
5058
6217
  : undefined;
5059
6218
 
5060
6219
  const initialGitCredentials: MintedRunGitCredentials | undefined =
@@ -5167,82 +6326,83 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5167
6326
  gitCredentialRenewals = controllers;
5168
6327
  };
5169
6328
 
5170
- const attachToolspaceTokenRenewal = async (
5171
- tokenSession: ToolspaceTokenWriterSession,
5172
- initialExpiresAt = sandboxToolspaceTokenExpiresAt,
6329
+ const attachCodemodeTokenRenewal = async (
6330
+ tokenSession?: CodemodeTokenWriterSession,
6331
+ initialExpiresAt = sandboxCodemodeTokenExpiresAt,
5173
6332
  initialSandbox?: ResumedTurnSandbox,
5174
6333
  ): Promise<void> => {
5175
- if (!sandboxToolspaceToken || !initialExpiresAt) return;
5176
- const previous = toolspaceTokenRenewal;
5177
- toolspaceTokenRenewal = null;
6334
+ if (!codemodeTokenState || !initialExpiresAt) return;
6335
+ const previous = codemodeTokenRenewal;
6336
+ codemodeTokenRenewal = null;
5178
6337
  await previous?.stop();
5179
- if (toolspaceTokenRenewalClosed) return;
6338
+ if (codemodeTokenRenewalClosed) return;
5180
6339
 
5181
6340
  const mint = async () => {
5182
- const material = await mintSandboxToolspaceToken(
6341
+ const material = await mintSandboxCodemodeToken(
5183
6342
  runSettings,
5184
6343
  connectionScope,
5185
- toolspaceAuthority,
6344
+ codemodeAuthority,
5186
6345
  );
5187
- if (material) {
5188
- }
5189
6346
  return material;
5190
6347
  };
5191
6348
  const write = async (material: NonNullable<Awaited<ReturnType<typeof mint>>>) => {
5192
- const runAs = sandboxRunAs(runSettings);
5193
- const targetSandbox = resolvedSandbox ?? initialSandbox;
5194
- if (!targetSandbox) {
5195
- throw new Error("Toolspace token renewal has no exact sandbox lease target");
6349
+ if (tokenSession) {
6350
+ const runAs = sandboxRunAs(runSettings);
6351
+ const targetSandbox = resolvedSandbox ?? initialSandbox;
6352
+ if (!targetSandbox) {
6353
+ throw new Error("Codemode token renewal has no exact sandbox lease target");
6354
+ }
6355
+ await runWorkspaceMutationForSandbox(
6356
+ targetSandbox,
6357
+ "codemodeTokenRenewal",
6358
+ async () =>
6359
+ await refreshCodemodeTokenFile(tokenSession, material.token, {
6360
+ ...(runAs ? { runAs } : {}),
6361
+ ...(sandboxCodemodeTokenFile
6362
+ ? {
6363
+ tokenFile: sandboxCodemodeTokenFile,
6364
+ legacyTokenFile: sandboxEnvironment.OPENGENI_CODEMODE_TOKEN_FILE!,
6365
+ }
6366
+ : {}),
6367
+ ...(toolCancellationFenceRef.current
6368
+ ? {
6369
+ commandRunner: toolCancellationFenceRef.current.runSandboxCommand.bind(
6370
+ toolCancellationFenceRef.current,
6371
+ ),
6372
+ }
6373
+ : {}),
6374
+ }),
6375
+ );
5196
6376
  }
5197
- await runWorkspaceMutationForSandbox(
5198
- targetSandbox,
5199
- "toolspaceTokenRenewal",
5200
- async () =>
5201
- await refreshToolspaceTokenFile(tokenSession, material.token, {
5202
- ...(runAs ? { runAs } : {}),
5203
- ...(sandboxToolspaceTokenFile
5204
- ? {
5205
- tokenFile: sandboxToolspaceTokenFile,
5206
- legacyTokenFile: sandboxEnvironment.OPENGENI_TOOLSPACE_TOKEN_FILE!,
5207
- }
5208
- : {}),
5209
- ...(toolCancellationFenceRef.current
5210
- ? {
5211
- commandRunner: toolCancellationFenceRef.current.runSandboxCommand.bind(
5212
- toolCancellationFenceRef.current,
5213
- ),
5214
- }
5215
- : {}),
5216
- }),
5217
- );
6377
+ codemodeTokenState.token = material.token;
5218
6378
  };
5219
6379
  let renewalExpiresAt = initialExpiresAt;
5220
- if (renewalExpiresAt.getTime() <= Date.now() + TOOLSPACE_TOKEN_EXPIRY_LEAD_MS) {
6380
+ if (renewalExpiresAt.getTime() <= Date.now() + CODEMODE_TOKEN_EXPIRY_LEAD_MS) {
5221
6381
  const fresh = await mint();
5222
6382
  if (!fresh) {
5223
- throw new Error("Toolspace token mint became unavailable during sandbox setup");
6383
+ throw new Error("Codemode token mint became unavailable during sandbox setup");
5224
6384
  }
5225
6385
  await write(fresh);
5226
6386
  renewalExpiresAt = fresh.expiresAt;
5227
6387
  }
5228
- const controller = startToolspaceTokenRenewalLoop({
6388
+ const controller = startCodemodeTokenRenewalLoop({
5229
6389
  initialExpiresAt: renewalExpiresAt,
5230
6390
  mint,
5231
6391
  write,
5232
6392
  onSuccess: () => {
5233
6393
  observability.incrementCounter({
5234
- name: "opengeni_toolspace_token_renewals_total",
5235
- help: "Sandbox Toolspace token renewal attempts by outcome.",
6394
+ name: "opengeni_codemode_token_renewals_total",
6395
+ help: "Sandbox Codemode token renewal attempts by outcome.",
5236
6396
  labels: { outcome: "completed" },
5237
6397
  });
5238
6398
  },
5239
6399
  onFailure: ({ retryDelayMs, errorClass }) => {
5240
6400
  observability.incrementCounter({
5241
- name: "opengeni_toolspace_token_renewals_total",
5242
- help: "Sandbox Toolspace token renewal attempts by outcome.",
6401
+ name: "opengeni_codemode_token_renewals_total",
6402
+ help: "Sandbox Codemode token renewal attempts by outcome.",
5243
6403
  labels: { outcome: "error" },
5244
6404
  });
5245
- observability.warn("Sandbox Toolspace token renewal failed; retry scheduled", {
6405
+ observability.warn("Sandbox Codemode token renewal failed; retry scheduled", {
5246
6406
  sessionId: input.sessionId,
5247
6407
  turnId,
5248
6408
  errorClass,
@@ -5250,13 +6410,19 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5250
6410
  });
5251
6411
  },
5252
6412
  });
5253
- if (toolspaceTokenRenewalClosed) {
6413
+ if (codemodeTokenRenewalClosed) {
5254
6414
  await controller.stop();
5255
6415
  return;
5256
6416
  }
5257
- toolspaceTokenRenewal = controller;
6417
+ codemodeTokenRenewal = controller;
5258
6418
  };
5259
6419
 
6420
+ // A Connected Machine needs renewal, but renewal is purely worker-local:
6421
+ // starting this loop performs no control-plane or machine operation.
6422
+ if (activeSandboxBackend === "selfhosted") {
6423
+ await attachCodemodeTokenRenewal();
6424
+ }
6425
+
5260
6426
  const attachRunCredentialRenewal = async (
5261
6427
  credentialSession: RunCredentialCommandSession,
5262
6428
  initialSandbox?: ResumedTurnSandbox,
@@ -5402,7 +6568,13 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5402
6568
  // applies the agent's manifest to this provided session and throws on ANY
5403
6569
  // variableSet delta (validateNoEnvironmentDelta). Passing sandboxEnvironment
5404
6570
  // here makes current==target so the delta is empty.
5405
- if (settings.sandboxOwnershipEnabled && turn.sandboxBackend !== "none") {
6571
+ if (
6572
+ shouldEstablishSandboxForTurn(
6573
+ settings.sandboxOwnershipEnabled,
6574
+ turn.sandboxBackend,
6575
+ machinePrimary,
6576
+ )
6577
+ ) {
5406
6578
  const managedOwnership = managedSandboxOwnershipForTurn(
5407
6579
  machinePrimary,
5408
6580
  input.attemptId,
@@ -5450,6 +6622,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5450
6622
  opStream: machineOpStream,
5451
6623
  epoch: activeSandboxPointer!.activeEpoch,
5452
6624
  environment: sandboxEnvironment,
6625
+ ...(transientCodemodeEnvironment
6626
+ ? { transientExecEnvironment: transientCodemodeEnvironment }
6627
+ : {}),
5453
6628
  workingDir: activeSandboxPointer!.workingDir,
5454
6629
  },
5455
6630
  );
@@ -5473,11 +6648,15 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5473
6648
  onOp: machineOpObserver.observer,
5474
6649
  onSandboxOperation: sandboxOperationObserver,
5475
6650
  onHomeSandboxRebound,
6651
+ ...(runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}),
5476
6652
  },
5477
6653
  {
5478
6654
  workspaceId: input.workspaceId,
5479
6655
  sessionId: input.sessionId,
5480
6656
  environment: sandboxEnvironment,
6657
+ ...(transientCodemodeEnvironment
6658
+ ? { transientExecEnvironment: transientCodemodeEnvironment }
6659
+ : {}),
5481
6660
  workspaceMutationFence: {
5482
6661
  accountId: input.accountId,
5483
6662
  turnId: turn.id,
@@ -5517,6 +6696,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5517
6696
  {
5518
6697
  db,
5519
6698
  settings: runSettings,
6699
+ logicalFallbackSettings: logicalSandboxSettings,
5520
6700
  cancellationSignal: sandboxResumeSignal,
5521
6701
  sandboxMetrics: runtimeMetricsHooksForObservability(observability),
5522
6702
  onSandboxLost: publishSandboxLost,
@@ -5555,7 +6735,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5555
6735
  managedOwnership!.holderId,
5556
6736
  ),
5557
6737
  sandboxResumeSignal,
5558
- async (lateSandbox) => await lateSandbox.release(),
6738
+ releaseLateSandbox,
5559
6739
  );
5560
6740
  setupBoxSession = resolvedSandbox.established.session;
5561
6741
  // Durable box-lifecycle events (sandbox-file-persistence observability):
@@ -5583,6 +6763,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5583
6763
  onSandboxOperation: sandboxOperationObserver,
5584
6764
  onHomeSandboxLost: publishSandboxLost,
5585
6765
  onHomeSandboxRebound,
6766
+ ...(runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}),
5586
6767
  },
5587
6768
  // Thread the SAME declared environment the group box was created with
5588
6769
  // (resumeBoxForTurn, above) so a selfhosted swap target's manifest
@@ -5592,6 +6773,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5592
6773
  workspaceId: input.workspaceId,
5593
6774
  sessionId: input.sessionId,
5594
6775
  environment: sandboxEnvironment,
6776
+ ...(transientCodemodeEnvironment
6777
+ ? { transientExecEnvironment: transientCodemodeEnvironment }
6778
+ : {}),
5595
6779
  workspaceMutationFence: {
5596
6780
  accountId: input.accountId,
5597
6781
  turnId: turn.id,
@@ -5615,7 +6799,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5615
6799
  }
5616
6800
  }
5617
6801
 
5618
- const fileResourceDownloads = await waitForTurnOperation(
6802
+ const ordinaryFileResourceDownloads = await waitForTurnOperation(
5619
6803
  sandboxFileDownloadsForRun(
5620
6804
  runSettings,
5621
6805
  db,
@@ -5627,6 +6811,35 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5627
6811
  cancellationSignal,
5628
6812
  undefined,
5629
6813
  );
6814
+ if (requiredGeneratedVideoFiles.length > 0 && !objectStorage) {
6815
+ throw new Error("Generated video materialization requires object storage");
6816
+ }
6817
+ const generatedVideoDownloads: SandboxFileDownload[] = [];
6818
+ if (objectStorage && requiredGeneratedVideoFiles.length > 0) {
6819
+ const downloadStorage = objectStorageForSandboxDownloads(modelRunSettings, objectStorage);
6820
+ for (const file of requiredGeneratedVideoFiles) {
6821
+ const signed = await downloadStorage.createGetUrl({
6822
+ key: file.objectKey,
6823
+ });
6824
+ generatedVideoDownloads.push({
6825
+ fileId: file.fileId,
6826
+ mountPath: "generated-videos",
6827
+ filename: file.filename,
6828
+ url: signed.url,
6829
+ expiresAt: signed.expiresAt,
6830
+ sizeBytes: file.sizeBytes,
6831
+ sha256: file.sha256,
6832
+ });
6833
+ }
6834
+ }
6835
+ const fileResourceDownloads = [
6836
+ ...new Map(
6837
+ [...ordinaryFileResourceDownloads, ...generatedVideoDownloads].map((download) => [
6838
+ download.fileId,
6839
+ download,
6840
+ ]),
6841
+ ).values(),
6842
+ ];
5630
6843
  throwIfWorkerShuttingDown();
5631
6844
  throwIfTurnCancelled();
5632
6845
  const mcpCredentialRootSessionId =
@@ -5667,7 +6880,30 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5667
6880
  }
5668
6881
  return result;
5669
6882
  };
5670
- const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
6883
+ const publishToolAuthNeeded = async (payload: ToolAuthNeededPayload): Promise<void> => {
6884
+ if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
6885
+ return;
6886
+ }
6887
+ await publish!([{ type: "tool.auth_needed", payload }], true);
6888
+ };
6889
+ const selectedApiIntegrationServerIds = new Set(turnTools.map((tool) => tool.id));
6890
+ const localMcpServers = buildApiIntegrationServersForTurn({
6891
+ settings: runSettings,
6892
+ integrations: installedApiIntegrations.filter((integration) =>
6893
+ selectedApiIntegrationServerIds.has(integration.serverId),
6894
+ ),
6895
+ authority: {
6896
+ accountId: input.accountId,
6897
+ workspaceId: input.workspaceId,
6898
+ sessionId: input.sessionId,
6899
+ rootSessionId: session.rootSessionId,
6900
+ turnId: turn.id,
6901
+ attemptId: input.attemptId,
6902
+ ...(credentialSubjectId ? { initiatingSubjectId: credentialSubjectId } : {}),
6903
+ },
6904
+ resolveCredential,
6905
+ onAuthNeeded: publishToolAuthNeeded,
6906
+ });
5671
6907
  const codexAppsAuth = codexAppsCredentialId
5672
6908
  ? (() => {
5673
6909
  const resolver = buildCodexTokenResolver(
@@ -5688,13 +6924,20 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5688
6924
 
5689
6925
  return await withCodexAppsRequestAuthorization(
5690
6926
  db,
5691
- { workspaceId: input.workspaceId, credentialId: codexAppsCredentialId },
6927
+ {
6928
+ workspaceId: input.workspaceId,
6929
+ credentialId: codexAppsCredentialId,
6930
+ },
5692
6931
  async () => await use(snapshot),
5693
6932
  );
5694
6933
  },
5695
6934
  };
5696
6935
  })()
5697
6936
  : undefined;
6937
+ const selectedFirstPartyMcpTools = allowedFirstPartyMcpToolsForSession(
6938
+ runSettings,
6939
+ session.firstPartyMcpTools,
6940
+ );
5698
6941
  preparedTools = await waitForTurnOperation(
5699
6942
  runtime.prepareTools(runSettings, turnTools, {
5700
6943
  accountId: input.accountId,
@@ -5711,22 +6954,60 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5711
6954
  ...(credentialSubjectId ? { credentialSubjectId } : {}),
5712
6955
  ...(codexAppsAuth ? { codexAppsAuth } : {}),
5713
6956
  resolveCredential,
5714
- onAuthNeeded: async (payload) => {
5715
- if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
5716
- return;
5717
- }
5718
- await publish!([{ type: "tool.auth_needed", payload }], true);
6957
+ onAuthNeeded: publishToolAuthNeeded,
6958
+ localMcpServers,
6959
+ onAttemptToolCatalog: async (catalog) => {
6960
+ await persistAttemptToolCatalog(db, catalog);
5719
6961
  },
5720
6962
  // Manager-style sessions carry a creation-validated permission set
5721
6963
  // for their first-party MCP token; null keeps the fixed default.
5722
6964
  ...(session.firstPartyMcpPermissions?.length
5723
6965
  ? { firstPartyPermissions: session.firstPartyMcpPermissions }
5724
6966
  : {}),
5725
- firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
6967
+ firstPartyTools: selectedFirstPartyMcpTools,
6968
+ nestedAgentDepth: session.nestedAgentDepth,
6969
+ effectiveMaxNestedAgentDepth: session.effectiveMaxNestedAgentDepth,
6970
+ attemptToolDefinitions: createFirstPartyInteractionAttemptToolDefinitions({
6971
+ settings: runSettings,
6972
+ scope: {
6973
+ accountId: input.accountId,
6974
+ workspaceId: input.workspaceId,
6975
+ sessionId: input.sessionId,
6976
+ turnId: turn.id,
6977
+ attemptId: input.attemptId,
6978
+ executionGeneration,
6979
+ },
6980
+ ...(session.firstPartyMcpPermissions?.length
6981
+ ? { permissions: session.firstPartyMcpPermissions }
6982
+ : {}),
6983
+ selectedTools: selectedFirstPartyMcpTools,
6984
+ subjectId: "worker:first-party-mcp",
6985
+ subjectLabel: "OpenGeni worker",
6986
+ ...(interactionInterventionResume
6987
+ ? { interventionResume: interactionInterventionResume }
6988
+ : {}),
6989
+ }),
5726
6990
  }),
5727
6991
  cancellationSignal,
5728
6992
  async (latePreparedTools) => await latePreparedTools.close().catch(() => undefined),
5729
6993
  );
6994
+ if (turnId && preparedTools.attemptToolEnvironment) {
6995
+ codemodeDispatcher = new CodemodeAttemptDispatcher(
6996
+ db,
6997
+ bus,
6998
+ preparedTools.attemptToolEnvironment,
6999
+ {
7000
+ accountId: input.accountId,
7001
+ workspaceId: input.workspaceId,
7002
+ sessionId: input.sessionId,
7003
+ turnId,
7004
+ attemptId: input.attemptId,
7005
+ executionGeneration,
7006
+ },
7007
+ cancellationSignal,
7008
+ );
7009
+ codemodeDispatcher.start();
7010
+ }
5730
7011
  // Genesis turn = the first user turn (no assistant history reconciled
5731
7012
  // yet). Durable Postgres state (countSessionHistoryItems includes
5732
7013
  // superseded rows after compaction), NOT a workflow counter (turnsThisRun
@@ -5765,6 +7046,318 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5765
7046
  }
5766
7047
  : {};
5767
7048
  const hostedWebSearch = hostedWebSearchForTurn(resolvedModel, runSettings.webSearchEnabled);
7049
+ const resolveImageReferences = async (
7050
+ references: Parameters<typeof resolveImageGenerationReferences>[0]["references"],
7051
+ ) =>
7052
+ await resolveImageGenerationReferences({
7053
+ db,
7054
+ objectStorage: objectStorage!,
7055
+ workspaceId: input.workspaceId,
7056
+ references,
7057
+ readSandboxFile: async (path, maxBytes) => {
7058
+ const imageReferenceSession = (setupBoxSession ??
7059
+ sdkOwnedSandboxSession) as ChannelASession | null;
7060
+ if (!imageReferenceSession) {
7061
+ throw new Error("Sandbox image reference is unavailable");
7062
+ }
7063
+ const relativePath = path.slice("/workspace/".length);
7064
+ const referenceRunAs = sandboxRunAs(modelRunSettings);
7065
+ const channel = new SandboxChannelAService({
7066
+ session: imageReferenceSession,
7067
+ workspaceRoot: "/workspace",
7068
+ leaseEpoch: resolvedSandbox?.leaseEpoch ?? 0,
7069
+ ...(referenceRunAs ? { runAs: referenceRunAs } : {}),
7070
+ });
7071
+ const read = await channel.fsRead({
7072
+ path: relativePath,
7073
+ encoding: "base64",
7074
+ maxBytes,
7075
+ });
7076
+ if (read.truncated) throw new Error("Sandbox image reference exceeds the byte limit");
7077
+ return Uint8Array.from(Buffer.from(read.content, "base64"));
7078
+ },
7079
+ });
7080
+ const imageGenerationOption: Pick<BuildAgentOptions, "imageGeneration"> = (() => {
7081
+ // Never expose a paid image operation unless its permanent artifact can
7082
+ // be committed. Failing after provider execution would leave an
7083
+ // unrecoverable outcome-unknown operation with no user-visible image.
7084
+ if (!objectStorage) return {};
7085
+ if (nativeImageProviderBinding) {
7086
+ nativeImageGenerationRetention = {
7087
+ ...nativeImageProviderBinding,
7088
+ sessionId: input.sessionId,
7089
+ turnId: turn.id,
7090
+ attemptId: input.attemptId,
7091
+ };
7092
+ return { imageGeneration: { kind: "native_hosted" } };
7093
+ }
7094
+
7095
+ if (resolvedModel?.provider.kind === "codex-subscription") {
7096
+ const imageAuthority = connectedSubscriptionImageGenerationAuthority(
7097
+ codexContext,
7098
+ effectiveCodexCredentialId,
7099
+ );
7100
+ if (!imageAuthority) return {};
7101
+ return {
7102
+ imageGeneration: {
7103
+ kind: "provider_adapter",
7104
+ execute: async ({ prompt, references }, { toolCallId }) => {
7105
+ const resolvedReferences = await resolveImageReferences(references);
7106
+ const receipt = await executeCodexImageGeneration({
7107
+ db,
7108
+ objectStorage,
7109
+ accountId: input.accountId,
7110
+ workspaceId: input.workspaceId,
7111
+ sessionId: input.sessionId,
7112
+ turnId: turn.id,
7113
+ attemptId: input.attemptId,
7114
+ toolCallId,
7115
+ prompt,
7116
+ references: resolvedReferences,
7117
+ credentialId: imageAuthority.credentialId,
7118
+ codexContext: imageAuthority.credentialContext,
7119
+ ...(runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}),
7120
+ });
7121
+ generatedImageReceiptsByArtifactId.set(receipt.artifact.artifactId, receipt);
7122
+ await materializeGeneratedImage(receipt);
7123
+ return receipt;
7124
+ },
7125
+ },
7126
+ };
7127
+ }
7128
+
7129
+ if (resolvedModel?.provider.kind === "xai-subscription") {
7130
+ const imageAuthority = connectedSubscriptionImageGenerationAuthority(
7131
+ xaiRequestContext,
7132
+ effectiveXaiCredentialId,
7133
+ );
7134
+ if (!imageAuthority) return {};
7135
+ return {
7136
+ imageGeneration: {
7137
+ kind: "provider_adapter",
7138
+ execute: async ({ prompt, references }, { toolCallId }) => {
7139
+ const resolvedReferences = await resolveImageReferences(references);
7140
+ const receipt = await executeXaiSubscriptionImageGeneration({
7141
+ db,
7142
+ objectStorage,
7143
+ accountId: input.accountId,
7144
+ workspaceId: input.workspaceId,
7145
+ sessionId: input.sessionId,
7146
+ turnId: turn.id,
7147
+ attemptId: input.attemptId,
7148
+ toolCallId,
7149
+ prompt,
7150
+ references: resolvedReferences,
7151
+ credentialId: imageAuthority.credentialId,
7152
+ xaiContext: imageAuthority.credentialContext,
7153
+ ...(runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}),
7154
+ });
7155
+ generatedImageReceiptsByArtifactId.set(receipt.artifact.artifactId, receipt);
7156
+ await materializeGeneratedImage(receipt);
7157
+ return receipt;
7158
+ },
7159
+ },
7160
+ };
7161
+ }
7162
+
7163
+ const gatewayResolution = resolveModelProvider(
7164
+ capabilitySettings,
7165
+ WORKSPACE_GATEWAY_PROVIDER_ID,
7166
+ );
7167
+ const gateway = gatewayResolution?.provider;
7168
+ if (gateway?.kind !== "vercel-gateway-workspace" || !gateway.apiKey) return {};
7169
+ const gatewayApiKey = gateway.apiKey;
7170
+ return {
7171
+ imageGeneration: {
7172
+ kind: "provider_adapter",
7173
+ execute: async ({ prompt, references }, { toolCallId }) => {
7174
+ const resolvedReferences = await resolveImageReferences(references);
7175
+ const receipt = await executeGatewayImageGeneration({
7176
+ db,
7177
+ objectStorage,
7178
+ accountId: input.accountId,
7179
+ workspaceId: input.workspaceId,
7180
+ sessionId: input.sessionId,
7181
+ turnId: turn.id,
7182
+ attemptId: input.attemptId,
7183
+ apiKey: gatewayApiKey,
7184
+ modelId: capabilitySettings.imageGenerationModel,
7185
+ prompt,
7186
+ references: resolvedReferences,
7187
+ toolCallId,
7188
+ ...(runtimeCancellationSignal ? { abortSignal: runtimeCancellationSignal } : {}),
7189
+ });
7190
+ generatedImageReceiptsByArtifactId.set(receipt.artifact.artifactId, receipt);
7191
+ await materializeGeneratedImage(receipt);
7192
+ return receipt;
7193
+ },
7194
+ },
7195
+ };
7196
+ })();
7197
+ const videoGenerationPolicy = await getWorkspaceVideoGenerationPolicy(db, input.workspaceId);
7198
+ const videoGenerationEnabled =
7199
+ videoGenerationPolicy.defaultModelId !== null &&
7200
+ videoGenerationPolicy.enabledModelIds.length > 0;
7201
+ let videoGenerationCredential: VideoGenerationCredentialLease | null = null;
7202
+ if (objectStorage && videoGenerationEnabled) {
7203
+ if (videoGenerationPolicy.fundingSource === "opengeni_credits") {
7204
+ videoGenerationCredential = managedVideoGenerationCredentialLease(modelRunSettings);
7205
+ } else if (videoGenerationPolicy.fundingSource === "workspace_gateway") {
7206
+ const workspaceCredential = await loadWorkspaceVercelAiGatewayCredentialLease(
7207
+ db,
7208
+ modelRunSettings,
7209
+ input.workspaceId,
7210
+ );
7211
+ if (workspaceCredential) {
7212
+ videoGenerationCredential = {
7213
+ fundingSource: "workspace_gateway",
7214
+ ...workspaceCredential,
7215
+ };
7216
+ }
7217
+ } else if (videoGenerationPolicy.fundingSource === "supergrok_subscription") {
7218
+ const encryptionKey = environmentsEncryptionKeyBytes(modelRunSettings);
7219
+ const subjectId = xaiLeaseSubjectId ?? turn.initiatingHumanSubjectId;
7220
+ if (encryptionKey && subjectId) {
7221
+ const authoritySnapshot =
7222
+ xaiAuthoritySnapshot ??
7223
+ (await resolveXaiProviderAccountAuthoritySnapshotForAcceptance(db, {
7224
+ workspaceId: input.workspaceId,
7225
+ subjectId,
7226
+ }));
7227
+ const pin = await getXaiSessionAccountPin(db, {
7228
+ workspaceId: input.workspaceId,
7229
+ subjectId,
7230
+ sessionId: input.sessionId,
7231
+ authoritySnapshot,
7232
+ });
7233
+ const selected = effectiveXaiCredentialId
7234
+ ? {
7235
+ credentialId: effectiveXaiCredentialId,
7236
+ rotationEnabled: xaiRotationEnabled,
7237
+ }
7238
+ : await selectXaiCredentialForUse(db, {
7239
+ accountId: input.accountId,
7240
+ workspaceId: input.workspaceId,
7241
+ subjectId,
7242
+ authoritySnapshot,
7243
+ shardKey: input.sessionId,
7244
+ pinnedCredentialId: pin?.pinnedCredentialId ?? null,
7245
+ pinSource:
7246
+ pin?.pinSource === "manual" || pin?.pinSource === "policy"
7247
+ ? pin.pinSource
7248
+ : null,
7249
+ });
7250
+ if (selected.credentialId) {
7251
+ if (
7252
+ selected.rotationEnabled &&
7253
+ pin?.pinSource !== "manual" &&
7254
+ pin?.pinnedCredentialId !== selected.credentialId
7255
+ ) {
7256
+ await setXaiSessionAccountPin(db, {
7257
+ accountId: input.accountId,
7258
+ workspaceId: input.workspaceId,
7259
+ subjectId,
7260
+ sessionId: input.sessionId,
7261
+ authoritySnapshot,
7262
+ credentialId: selected.credentialId,
7263
+ pinSource: "policy",
7264
+ expectedVersion: pin?.version ?? null,
7265
+ }).catch((error: unknown) => {
7266
+ if (error instanceof Error && error.message === "xAI session pin changed") return;
7267
+ throw error;
7268
+ });
7269
+ }
7270
+ const credential = await materializeXaiCredentialForRun(db, {
7271
+ workspaceId: input.workspaceId,
7272
+ subjectId,
7273
+ credentialId: selected.credentialId,
7274
+ authoritySnapshot,
7275
+ encryptionKey,
7276
+ });
7277
+ videoGenerationCredential = xaiVideoGenerationCredentialLease({
7278
+ settings: modelRunSettings,
7279
+ credential,
7280
+ subjectId,
7281
+ authoritySnapshot,
7282
+ });
7283
+ }
7284
+ }
7285
+ }
7286
+ }
7287
+ const videoGenerationOption: Pick<BuildAgentOptions, "videoGeneration"> = (() => {
7288
+ if (
7289
+ !objectStorage ||
7290
+ modelRunSettings.sandboxBackend === "none" ||
7291
+ !videoGenerationCredential ||
7292
+ !videoGenerationEnabled
7293
+ ) {
7294
+ return {};
7295
+ }
7296
+ // Parse the frozen capability snapshot before advertising either tool.
7297
+ // Invalid or unsupported workspace policy therefore fails closed before
7298
+ // it can perturb the model's tool list.
7299
+ const capabilities = videoGenerationCapabilitiesForPolicy({
7300
+ policy: videoGenerationPolicy,
7301
+ credentialVersion: videoGenerationCredential.version,
7302
+ });
7303
+ return {
7304
+ videoGeneration: {
7305
+ capabilities: async () => capabilities,
7306
+ execute: async (toolInput, { toolCallId }) => {
7307
+ const sessionForReference =
7308
+ resolvedSandbox?.established.session ?? sdkOwnedSandboxSession;
7309
+ const fence = toolCancellationFenceRef.current;
7310
+ const runAs = sandboxRunAs(modelRunSettings);
7311
+ let accepted: Awaited<ReturnType<typeof admitVideoGenerationRequest>>;
7312
+ try {
7313
+ accepted = await admitVideoGenerationRequest({
7314
+ db,
7315
+ storage: objectStorage,
7316
+ settings: modelRunSettings,
7317
+ accountId: input.accountId,
7318
+ workspaceId: input.workspaceId,
7319
+ sessionId: input.sessionId,
7320
+ turnId: turn.id,
7321
+ attemptId: input.attemptId,
7322
+ toolCallId,
7323
+ toolInput,
7324
+ policy: videoGenerationPolicy,
7325
+ credential: videoGenerationCredential,
7326
+ ...(sessionForReference && fence
7327
+ ? {
7328
+ runCommand: async (command) =>
7329
+ await fence.runSandboxCommandStructured(
7330
+ sessionForReference as import("@opengeni/runtime").TurnSandboxCommandSession,
7331
+ {
7332
+ ...command,
7333
+ ...(runAs ? { runAs } : {}),
7334
+ },
7335
+ ),
7336
+ }
7337
+ : {}),
7338
+ ...(runtimeCancellationSignal ? { signal: runtimeCancellationSignal } : {}),
7339
+ });
7340
+ } catch (error) {
7341
+ if (error instanceof VideoReferenceInputError) {
7342
+ return VideoGenerationRejectedResult.parse({
7343
+ schemaVersion: 1,
7344
+ status: "rejected",
7345
+ code: error.code,
7346
+ message: error.message,
7347
+ operationCreated: false,
7348
+ });
7349
+ }
7350
+ throw error;
7351
+ }
7352
+ videoGenerationAcceptancesByCallId.set(toolCallId, {
7353
+ operationId: accepted.operationId,
7354
+ requestDigest: accepted.requestDigest,
7355
+ });
7356
+ return accepted.receipt;
7357
+ },
7358
+ },
7359
+ };
7360
+ })();
5768
7361
  const serviceTier = serviceTierForLatencyMode(
5769
7362
  turnExecutionPolicy.providerId,
5770
7363
  turnExecutionPolicy.latencyMode,
@@ -5795,6 +7388,23 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5795
7388
  outcome,
5796
7389
  }),
5797
7390
  };
7391
+ const runtimeSkillActivations = [
7392
+ ...installedSkillRuntime.activations,
7393
+ ...packRuntime.skillActivations,
7394
+ ...session.skills.map((skill) => ({
7395
+ source: "session" as const,
7396
+ id: `session:${session.id}:${skill.name}`,
7397
+ artifact: {
7398
+ name: skill.name,
7399
+ description: skill.description ?? null,
7400
+ files: skill.files.map((file) => ({
7401
+ path: file.path,
7402
+ content: file.content,
7403
+ })),
7404
+ },
7405
+ reason: "attached to session",
7406
+ })),
7407
+ ];
5798
7408
  const agent = runtime.buildAgent(modelRunSettings, turnResources, {
5799
7409
  reasoningEffort: turn.reasoningEffort,
5800
7410
  latencyMode: turnExecutionPolicy.latencyMode,
@@ -5806,6 +7416,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5806
7416
  titleIsSet: Boolean(session.title?.trim()),
5807
7417
  },
5808
7418
  sandboxEnvironment,
7419
+ ...(preparedTools.attemptToolCatalog
7420
+ ? { attemptToolCatalog: preparedTools.attemptToolCatalog }
7421
+ : {}),
7422
+ ...(sandboxArtifactRuntime.available ? { artifactRuntimeAvailable: true } : {}),
5809
7423
  ...(cancellationSignal ? { turnCancellationSignal: cancellationSignal } : {}),
5810
7424
  onToolCancellationFence: (fence) => {
5811
7425
  toolCancellationFenceRef.current = fence;
@@ -5825,13 +7439,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5825
7439
  ...(activeSandboxBackend !== "selfhosted" && !sandboxGitTokens && sandboxGitToken
5826
7440
  ? { gitTokenSeed: sandboxGitToken }
5827
7441
  : {}),
5828
- // Toolspace delivery is managed-sandbox-only. Connected Machines own any
5829
- // manually configured API credentials and must never be contacted during
5830
- // turn admission merely to seed OpenGeni tooling.
5831
- ...(sandboxToolspaceToken
7442
+ ...(sandboxCodemodeToken ? { codemodeAvailable: true } : {}),
7443
+ // Managed boxes receive the bearer through their protected per-session
7444
+ // token file. Connected Machines use transient per-exec delivery above,
7445
+ // so they must not run the file-seeding lifecycle hook.
7446
+ ...(activeSandboxBackend !== "selfhosted" && sandboxCodemodeToken
5832
7447
  ? {
5833
- toolspaceTokenSeed: sandboxToolspaceToken,
5834
- toolspaceTokenSessionId: input.sessionId,
7448
+ codemodeTokenSeed: sandboxCodemodeToken,
7449
+ codemodeTokenSessionId: input.sessionId,
5835
7450
  }
5836
7451
  : {}),
5837
7452
  ...(activeSandboxBackend ? { activeSandboxBackend } : {}),
@@ -5858,6 +7473,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5858
7473
  // session's MCP allow-list; the effective context window drives the
5859
7474
  // compaction threshold.
5860
7475
  hostedWebSearch,
7476
+ ...imageGenerationOption,
7477
+ ...videoGenerationOption,
7478
+ lazyToolTransport,
5861
7479
  supportsImageInput,
5862
7480
  inputFileMediaTypes: modelInputPolicy.inputFileMediaTypes,
5863
7481
  ...(resolvedModel
@@ -5867,15 +7485,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5867
7485
  runSettings.openaiReasoningEncryptedContent,
5868
7486
  contextWindowTokens:
5869
7487
  resolvedModel.configured.contextWindowTokens ?? runSettings.contextWindowTokens,
5870
- // The ChatGPT/Codex backend rejects the SDK's HOSTED sandbox tools —
5871
- // the `apply_patch` tool type ("Unsupported tool type: apply_patch")
5872
- // and structured tool output — which the OpenAIResponsesModel the SDK
5873
- // binds would otherwise select. Tell buildAgent to emit the function
5874
- // `apply_patch` + text `view_image` variants the backend accepts. Only
5875
- // the codex-subscription provider needs this; every other backend
5876
- // (built-in OpenAI/Azure = real hosted support; registry "chat"
5877
- // providers = the SDK's own ChatCompletions detection) keeps the SDK
5878
- // default.
7488
+ // The ChatGPT/Codex backend rejects the SDK's HOSTED apply_patch
7489
+ // tool. Gateway Responses routes likewise expose ordinary function
7490
+ // tools, not OpenAI-hosted sandbox tools. Tell buildAgent to use
7491
+ // function apply_patch and wrap successful view_image results as
7492
+ // typed input_image content. Chat wires have no proven typed image
7493
+ // result transport and therefore receive no view_image tool.
5879
7494
  structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
5880
7495
  // EXPLICIT computer-use tool transport, derived from the resolved provider's
5881
7496
  // authoritative wire identity (codex → function-image, chat → disabled,
@@ -5905,19 +7520,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5905
7520
  didComputerUse = true;
5906
7521
  await maybeStartOnTurnRecording(resolvedSandbox, activeSandboxBackend);
5907
7522
  },
5908
- ...(packRuntime.skills.length > 0 ? { packSkills: packRuntime.skills } : {}),
5909
- ...(session.skills.length > 0 ? { sessionSkills: session.skills } : {}),
5910
- ...(skillLibraryRuntime.skillLibrarySkills.length > 0
5911
- ? {
5912
- skillLibrarySkills: skillLibraryRuntime.skillLibrarySkills,
5913
- skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections,
5914
- }
7523
+ onRetainableSessionImageOutput: retainSessionImageAtToolBoundary,
7524
+ ...(runtimeSkillActivations.length > 0
7525
+ ? { skillActivations: runtimeSkillActivations }
5915
7526
  : {}),
5916
7527
  ...(!structuredWorkspacePolicyActive && workspaceAgentInstructions
5917
7528
  ? { instructionsTemplate: workspaceAgentInstructions }
5918
7529
  : {}),
5919
7530
  ...(workspaceGovernance ? { workspaceGovernance } : {}),
5920
7531
  ...(workspaceMemory ? { workspaceMemory } : {}),
7532
+ goalSnapshot: turn.goalSnapshot,
5921
7533
  // Per-session persona tier (session > workspace > deployment default).
5922
7534
  // Composed system-level AFTER the workspace persona so it refines it for
5923
7535
  // this one session; absent ⇒ byte-identical to today's composition.
@@ -5946,6 +7558,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5946
7558
  rigName,
5947
7559
  script: rigVersion.setupScript,
5948
7560
  timeoutMs: runSettings.rigSetupTimeoutMs,
7561
+ contentHash: rigProviderImageContentHash({
7562
+ backend: turn.sandboxBackend,
7563
+ sourceImage: rigProviderImageSourceImage(
7564
+ logicalSandboxSettings,
7565
+ turn.sandboxBackend,
7566
+ ),
7567
+ definition: rigVersion,
7568
+ }),
5949
7569
  },
5950
7570
  }
5951
7571
  : {}),
@@ -5984,13 +7604,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5984
7604
  authorizeGitHubTokenMint,
5985
7605
  });
5986
7606
  const lazyGitTokens = lazyGitCredentials?.gitTokens;
5987
- const lazyToolspaceToken = sandboxToolspaceToken
5988
- ? await mintSandboxToolspaceToken(runSettings, connectionScope, toolspaceAuthority)
7607
+ const lazyCodemodeToken = sandboxCodemodeToken
7608
+ ? await mintSandboxCodemodeToken(runSettings, connectionScope, codemodeAuthority)
5989
7609
  : undefined;
5990
7610
  const provisioned = await resumeBoxForTurn(
5991
7611
  {
5992
7612
  db,
5993
7613
  settings: runSettings,
7614
+ logicalFallbackSettings: logicalSandboxSettings,
5994
7615
  cancellationSignal: sandboxResumeSignal,
5995
7616
  sandboxMetrics: runtimeMetricsHooksForObservability(observability),
5996
7617
  onSandboxLost: publishSandboxLost,
@@ -6043,9 +7664,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6043
7664
  gitCredentialBindingsOverride: lazyGitCredentials.bindings,
6044
7665
  }
6045
7666
  : {}),
6046
- ...(lazyToolspaceToken
7667
+ ...(lazyCodemodeToken
6047
7668
  ? {
6048
- toolspaceTokenSeedOverride: lazyToolspaceToken.token,
7669
+ codemodeTokenSeedOverride: lazyCodemodeToken.token,
6049
7670
  }
6050
7671
  : {}),
6051
7672
  ...(toolCancellationFenceRef.current
@@ -6058,9 +7679,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6058
7679
  },
6059
7680
  ),
6060
7681
  );
6061
- await attachToolspaceTokenRenewal(
6062
- provisioned.established.session as ToolspaceTokenWriterSession,
6063
- lazyToolspaceToken?.expiresAt,
7682
+ await attachCodemodeTokenRenewal(
7683
+ provisioned.established.session as CodemodeTokenWriterSession,
7684
+ lazyCodemodeToken?.expiresAt,
6064
7685
  provisioned,
6065
7686
  );
6066
7687
  await attachGitCredentialRenewal(
@@ -6110,6 +7731,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6110
7731
  startLeaseHeartbeat(provisioned, activeSandboxBackend ?? groupBoxBackend);
6111
7732
  setupBoxSession = provisioned.established.session;
6112
7733
  resolvedSandbox = provisioned;
7734
+ // `get()` does not release the first routed sandbox operation
7735
+ // until this hook returns. Materialize durable generated images
7736
+ // here so that operation can use their advertised paths without
7737
+ // racing a best-effort background copy.
7738
+ for (const receipt of generatedImageReceiptsByArtifactId.values()) {
7739
+ await materializeGeneratedImageInSandbox(
7740
+ receipt,
7741
+ provisioned,
7742
+ provisioned.established.session,
7743
+ );
7744
+ }
6113
7745
  },
6114
7746
  onFailed: async (error) => {
6115
7747
  await publish?.(
@@ -6126,7 +7758,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6126
7758
  );
6127
7759
  },
6128
7760
  disposeResult: async (provisioned) => {
6129
- await provisioned.release().catch(() => undefined);
7761
+ await releaseLateSandbox(provisioned);
6130
7762
  },
6131
7763
  },
6132
7764
  );
@@ -6140,11 +7772,15 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6140
7772
  onSandboxOperation: sandboxOperationObserver,
6141
7773
  onHomeSandboxLost: publishSandboxLost,
6142
7774
  onHomeSandboxRebound,
7775
+ ...(runtimeCancellationSignal ? { waitSignal: runtimeCancellationSignal } : {}),
6143
7776
  },
6144
7777
  {
6145
7778
  workspaceId: input.workspaceId,
6146
7779
  sessionId: input.sessionId,
6147
7780
  environment: sandboxEnvironment,
7781
+ ...(transientCodemodeEnvironment
7782
+ ? { transientExecEnvironment: transientCodemodeEnvironment }
7783
+ : {}),
6148
7784
  workspaceMutationFence: {
6149
7785
  accountId: input.accountId,
6150
7786
  turnId: turn.id,
@@ -6165,6 +7801,40 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6165
7801
  },
6166
7802
  );
6167
7803
  }
7804
+ let companyBrainContributionReceiptRecorded = false;
7805
+ const recordCompanyBrainContributionReceiptOnce = (): void => {
7806
+ if (companyBrainContributionReceiptRecorded) return;
7807
+ companyBrainContributionReceiptRecorded = true;
7808
+ try {
7809
+ const companyBrainContributionReceipt = buildCompanyBrainContributionReceipt({
7810
+ attemptId: input.attemptId,
7811
+ turnId: turn.id,
7812
+ nestedAgentDepth: session.nestedAgentDepth,
7813
+ memoryPromptMode,
7814
+ instructionPolicy: instructionPolicySnapshot,
7815
+ workspaceAgentInstructions,
7816
+ preferences: preferenceSnapshot,
7817
+ companyProfile: companyProfileSnapshot,
7818
+ companyProfileIncluded,
7819
+ workspaceMemory,
7820
+ skillActivations: runtimeSkillActivations,
7821
+ });
7822
+ recordCompanyBrainContributions(observability, companyBrainContributionReceipt);
7823
+ observability.info("model context contribution receipt", {
7824
+ attemptId: companyBrainContributionReceipt.attemptId,
7825
+ turnId: companyBrainContributionReceipt.turnId,
7826
+ sessionRole: companyBrainContributionReceipt.sessionRole,
7827
+ memoryPromptMode: companyBrainContributionReceipt.memoryPromptMode,
7828
+ instructionPolicySnapshotId:
7829
+ companyBrainContributionReceipt.instructionPolicySnapshotId,
7830
+ preferenceSnapshotId: companyBrainContributionReceipt.preferenceSnapshotId,
7831
+ companyProfileSnapshotId: companyBrainContributionReceipt.companyProfileSnapshotId,
7832
+ contributions: JSON.stringify(companyBrainContributionReceipt.contributions),
7833
+ });
7834
+ } catch {
7835
+ // Observability must never change model execution semantics.
7836
+ }
7837
+ };
6168
7838
  const agentInstructions = typeof agent.instructions === "string" ? agent.instructions : "";
6169
7839
  const compactSummarizer = compactionSummarizerFor(
6170
7840
  agentInstructions.trim() ? agentInstructions : undefined,
@@ -6442,7 +8112,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6442
8112
  "fileMaterialization",
6443
8113
  async () =>
6444
8114
  await materializeSandboxFileDownloads(
6445
- setupBoxSession as any,
8115
+ setupBoxSession as CodemodeTokenWriterSession,
6446
8116
  downloadsToMaterialize,
6447
8117
  {
6448
8118
  onRuntimeEvent: async (event) => {
@@ -6477,6 +8147,37 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6477
8147
  }
6478
8148
  fileDownloadsMaterializedForRun = true;
6479
8149
  }
8150
+ const requiredVideoFileIds = new Set(requiredGeneratedVideoFiles.map((file) => file.fileId));
8151
+ const failedRequiredVideo = fileMaterializationFailures.find((failure) =>
8152
+ requiredVideoFileIds.has(failure.fileId),
8153
+ );
8154
+ if (failedRequiredVideo) {
8155
+ const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
8156
+ sessionId: input.sessionId,
8157
+ turnId: turn.id,
8158
+ triggerEventId: triggerEventId!,
8159
+ attemptId: input.attemptId,
8160
+ reason: "generated_video_materialization_failed",
8161
+ detail: {
8162
+ retryable: true,
8163
+ operationIds: requiredGeneratedVideoFiles.map((file) => file.operationId),
8164
+ },
8165
+ });
8166
+ if (recovery.action === "stale") {
8167
+ acknowledgeLostAttemptOwnership();
8168
+ activityStatus = "cancelled";
8169
+ turnMetricOutcome = "cancelled";
8170
+ return claimedResult({ status: "cancelled" });
8171
+ }
8172
+ if (recovery.action !== "recovering") {
8173
+ throw new Error("Generated video materialization could not enter recovery");
8174
+ }
8175
+ acknowledgeRecoveryQuiescence();
8176
+ await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, recovery.events);
8177
+ activityStatus = "recovering";
8178
+ turnMetricOutcome = "recovering";
8179
+ return claimedResult({ status: "recovering", continueDelayMs: 1_000 });
8180
+ }
6480
8181
  const unavailableSandboxFilesNote = sandboxFileDownloadFailureNote(
6481
8182
  fileMaterializationFailures,
6482
8183
  );
@@ -6495,10 +8196,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6495
8196
  ...(unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {}),
6496
8197
  ...(runCredentialsNote ? { runCredentialsNote } : {}),
6497
8198
  providerApi,
8199
+ projectCanonicalHistory: generatedImageHistoryProjector,
6498
8200
  materializeModelHistory: materializeScreenshotHistory,
6499
8201
  materializeSerializedRunState: materializeScreenshotRunState,
6500
8202
  projectModelHistory: modelHistoryProjector,
6501
8203
  });
8204
+ for (const receipt of generatedImageReceiptsByArtifactId.values()) {
8205
+ await materializeGeneratedImage(receipt);
8206
+ }
6502
8207
  runInput = prepared.input;
6503
8208
  providerArtifactCandidates = prepared.providerArtifactCandidates;
6504
8209
  // Slice index = the length of the model-facing (active) history this turn
@@ -6522,6 +8227,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6522
8227
  // and bricks the session (issue-61). The repaired seed is already
6523
8228
  // orphan-free, so it is a stable prefix of the re-sanitized history and the
6524
8229
  // slice begins exactly at the first genuinely-new item.
8230
+ // prepareInput already sanitized the exact durable prefix represented
8231
+ // by state.history. Carry its count forward instead of loading and
8232
+ // retaining the full active transcript a second time beside runInput.
6525
8233
  persistedHistoryCount = prepared.persistedHistoryCount;
6526
8234
  nextHistoryPosition = await nextSessionHistoryPosition(
6527
8235
  db,
@@ -6632,9 +8340,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6632
8340
  preparedInput: runInput!,
6633
8341
  ...(fileDownloadsMaterializedForRun ? { fileDownloadsMaterialized: true } : {}),
6634
8342
  onRuntimeEvent: async (event) => {
6635
- await renewCodexLease("runtime_event");
6636
- if (codexLeaseLost) {
6637
- throw new Error("Codex credential lease expired during sandbox setup");
8343
+ await renewServingCredentialLease("runtime_event");
8344
+ if (servingCredentialLeaseLost()) {
8345
+ throw new Error("Provider credential lease expired during sandbox setup");
6638
8346
  }
6639
8347
  await publish!([{ type: event.type, payload: event.payload }], true);
6640
8348
  },
@@ -6657,14 +8365,15 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6657
8365
  // provider may have accepted work even if no response/event follows.
6658
8366
  // Escaped MCP setup timeouts are automatically recoverable only
6659
8367
  // before this line.
8368
+ recordCompanyBrainContributionReceiptOnce();
6660
8369
  modelRequestStarted = true;
6661
8370
  return await runtime.runStream(agent, runInput!, modelRunSettings, {
6662
8371
  signal: runtimeCancellationSignal,
6663
8372
  sandboxEnvironment,
6664
8373
  onRuntimeEvent: async (event) => {
6665
- await renewCodexLease("runtime_event");
6666
- if (codexLeaseLost) {
6667
- throw new Error("Codex credential lease expired during the active turn");
8374
+ await renewServingCredentialLease("runtime_event");
8375
+ if (servingCredentialLeaseLost()) {
8376
+ throw new Error("Provider credential lease expired during the active turn");
6668
8377
  }
6669
8378
  await publish!([{ type: event.type, payload: event.payload }], true);
6670
8379
  },
@@ -6691,14 +8400,15 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6691
8400
  },
6692
8401
  }
6693
8402
  : {}),
6694
- ...(sandboxToolspaceToken && sandboxToolspaceTokenExpiresAt && !lazyOwnedSandbox
8403
+ ...(activeSandboxBackend !== "selfhosted" &&
8404
+ sandboxCodemodeToken &&
8405
+ sandboxCodemodeTokenExpiresAt &&
8406
+ !lazyOwnedSandbox
6695
8407
  ? {
6696
- onToolspaceTokenSessionReady: async (
6697
- tokenSession: ToolspaceTokenWriterSession,
6698
- ) => {
8408
+ onCodemodeTokenSessionReady: async (tokenSession: CodemodeTokenWriterSession) => {
6699
8409
  const renewalSession =
6700
- (setupBoxSession as ToolspaceTokenWriterSession | null) ?? tokenSession;
6701
- await attachToolspaceTokenRenewal(renewalSession);
8410
+ (setupBoxSession as CodemodeTokenWriterSession | null) ?? tokenSession;
8411
+ await attachCodemodeTokenRenewal(renewalSession);
6702
8412
  },
6703
8413
  }
6704
8414
  : {}),
@@ -6719,6 +8429,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6719
8429
  : {}),
6720
8430
  }
6721
8431
  : {}),
8432
+ ...(modelRunSettings.sandboxBackend !== "none"
8433
+ ? {
8434
+ onSandboxSessionReady: async (sandboxSession: CodemodeTokenWriterSession) => {
8435
+ sdkOwnedSandboxSession = sandboxSession;
8436
+ for (const receipt of generatedImageReceiptsByArtifactId.values()) {
8437
+ await materializeGeneratedImageInOwnedSdkSession(receipt, sandboxSession);
8438
+ }
8439
+ },
8440
+ }
8441
+ : {}),
6722
8442
  contextCompactionSignal: () => modelResponseContextSignal(modelResponseState),
6723
8443
  contextCompactionRequested: () =>
6724
8444
  isSessionCompactionRequested(db, input.workspaceId, input.sessionId),
@@ -6730,7 +8450,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6730
8450
  if (codexLeaseLost) {
6731
8451
  throw new Error("Codex credential lease expired before the model run");
6732
8452
  }
6733
- stream = await withCodex(runStreamOnce);
8453
+ if (xaiLeaseLost) {
8454
+ throw new Error("xAI credential lease expired before the model run");
8455
+ }
8456
+ stream = await withProviderRequestContext(runStreamOnce);
6734
8457
  // Bounded provider label for the streaming SLIs — the resolved registry
6735
8458
  // provider id (or the built-in OpenAI/Azure provider), never a raw
6736
8459
  // user-supplied model string.
@@ -6760,6 +8483,16 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6760
8483
  let stableToolCallIdsToClear: string[] | null = null;
6761
8484
  let completedCurrentToolBatch = false;
6762
8485
  let retainedScreenshotMetadata: RetainedArtifactMetadata | null = null;
8486
+ let normalizedSdkEvents: ReturnType<typeof normalizeSdkEvent> | null = null;
8487
+ const generatedImage = generatedImageFromSdkEvent(next.value);
8488
+ if (isCompletedGeneratedImageSdkEvent(next.value) && !generatedImage) {
8489
+ throw new Error(
8490
+ "Completed native image item could not cross the retained-artifact boundary",
8491
+ );
8492
+ }
8493
+ const generatedImageReceipt = generatedImage
8494
+ ? await retainNativeGeneratedImage(generatedImage)
8495
+ : null;
6763
8496
  const responseResult = await processModelResponseTerminalEvent({
6764
8497
  event: next.value,
6765
8498
  state: modelResponseState,
@@ -6782,9 +8515,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6782
8515
  servingCredentialId: effectiveCodexCredentialId,
6783
8516
  priorSessionCredentialId: priorSessionCodexCredentialId,
6784
8517
  emittedSourceKeys: emittedModelUsageSourceKeys,
6785
- renewLease: () => renewCodexLease("model_usage"),
6786
- leaseLost: () => codexLeaseLost,
6787
- leaseLostMessage: "Codex credential lease expired during the active turn",
8518
+ renewLease: () => renewServingCredentialLease("model_usage"),
8519
+ leaseLost: servingCredentialLeaseLost,
8520
+ leaseLostMessage: "Provider credential lease expired during the active turn",
6788
8521
  setLastInputTokens: setLastInputTokensFenced,
6789
8522
  });
6790
8523
  assertModelResponseLatencyMode({
@@ -6794,10 +8527,22 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6794
8527
  ...(resolvedModel?.provider.id ? { providerId: resolvedModel.provider.id } : {}),
6795
8528
  });
6796
8529
  if (responseResult.status === "processed") {
8530
+ const rawStreamHistory = (stream.state as { history?: unknown[] }).history;
8531
+ if (Array.isArray(rawStreamHistory)) {
8532
+ // The completed image item is normally retained from its own
8533
+ // run-item event above. Scan once at the terminal response as
8534
+ // a compatibility backstop before reconciliation can persist
8535
+ // provider bytes. Never rescan history for every token delta.
8536
+ await retainNativeGeneratedImagesFromHistory(
8537
+ rawStreamHistory as Array<Record<string, unknown>>,
8538
+ );
8539
+ }
6797
8540
  streamSawPerResponseUsage ||= responseResult.usageReported;
6798
8541
  currentToolBatchCallIds = new Set<string>();
6799
8542
  currentToolBatchCompletedCallIds = new Set<string>();
6800
8543
  await reconcileConversationTruth();
8544
+ turnLifecycleMetricsFor(observability).progress(turnId!);
8545
+ modelCheckpointMemoryCollector.schedule(observability);
6801
8546
  try {
6802
8547
  await ensureRunAllowed(
6803
8548
  settings,
@@ -6813,7 +8558,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6813
8558
  // conversation context preserved for the post-top-up resume.
6814
8559
  let serializedRunState: string | null = null;
6815
8560
  try {
6816
- serializedRunState = String(stream.state.toString());
8561
+ serializedRunState = compactMediaRunState(String(stream.state.toString()));
6817
8562
  } catch {
6818
8563
  serializedRunState = null;
6819
8564
  }
@@ -6823,7 +8568,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6823
8568
  );
6824
8569
  }
6825
8570
  }
6826
- const pendingToolCall = pendingToolCallFromSdkEvent(next.value);
8571
+ const durableSdkEvent = generatedImageReceipt
8572
+ ? compactGeneratedImageSdkEvent(next.value, generatedImageReceipt)
8573
+ : next.value;
8574
+ const pendingToolCall = pendingToolCallFromSdkEvent(durableSdkEvent);
6827
8575
  if (pendingToolCall) {
6828
8576
  const registered = await registerPendingSessionToolCall(db, {
6829
8577
  accountId: input.accountId,
@@ -6843,18 +8591,57 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6843
8591
  );
6844
8592
  }
6845
8593
  currentToolBatchCallIds.add(pendingToolCall.callId);
6846
- if (pendingToolCall.callName === "computer_screenshot") {
6847
- computerScreenshotCallIds.add(pendingToolCall.callId);
8594
+ if (toolCallProducesRetainableSessionImage(pendingToolCall.callName)) {
8595
+ retainedSessionImageCallIds.add(pendingToolCall.callId);
6848
8596
  }
6849
8597
  }
6850
- const completedToolCall = completedToolCallFromSdkEvent(next.value);
8598
+ const completedToolCall = completedToolCallFromSdkEvent(durableSdkEvent);
6851
8599
  if (completedToolCall) {
6852
- const typedScreenshot = typedScreenshotFromSdkEvent(next.value);
8600
+ retainedScreenshotMetadata =
8601
+ retainedScreenshotReceiptsByCallId.get(completedToolCall.callId) ?? null;
8602
+ const typedScreenshot = retainedScreenshotMetadata
8603
+ ? null
8604
+ : typedScreenshotFromSdkEvent(durableSdkEvent);
8605
+ if (
8606
+ !retainedScreenshotMetadata &&
8607
+ retainedSessionImageCallIds.has(completedToolCall.callId) &&
8608
+ sdkEventContainsInlineImage(durableSdkEvent) &&
8609
+ !typedScreenshot
8610
+ ) {
8611
+ retainedScreenshotMetadata = unavailableRetainedSessionImage({
8612
+ sessionId: input.sessionId,
8613
+ turnId: activeTurnId,
8614
+ attemptId: input.attemptId,
8615
+ toolCallId: completedToolCall.callId,
8616
+ toolOutputId: completedToolCall.callId,
8617
+ reason: "unsupported",
8618
+ });
8619
+ retainedScreenshotReceiptsByCallId.set(
8620
+ completedToolCall.callId,
8621
+ retainedScreenshotMetadata,
8622
+ );
8623
+ }
6853
8624
  if (
8625
+ !retainedScreenshotMetadata &&
6854
8626
  typedScreenshot &&
6855
8627
  typedScreenshot.callId === completedToolCall.callId &&
6856
- computerScreenshotCallIds.has(completedToolCall.callId)
8628
+ retainedSessionImageCallIds.has(completedToolCall.callId)
6857
8629
  ) {
8630
+ // Install a compact deterministic fallback before database or
8631
+ // object-storage I/O. Unexpected retention failure can then
8632
+ // reconcile/serialize without leaking inline bytes.
8633
+ retainedScreenshotMetadata = unavailableRetainedSessionImage({
8634
+ sessionId: input.sessionId,
8635
+ turnId: activeTurnId,
8636
+ attemptId: input.attemptId,
8637
+ toolCallId: typedScreenshot.callId,
8638
+ toolOutputId: typedScreenshot.toolOutputId,
8639
+ reason: "pending",
8640
+ });
8641
+ retainedScreenshotReceiptsByCallId.set(
8642
+ completedToolCall.callId,
8643
+ retainedScreenshotMetadata,
8644
+ );
6858
8645
  retainedScreenshotMetadata = await retainComputerScreenshot({
6859
8646
  db,
6860
8647
  objectStorage,
@@ -6870,6 +8657,30 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6870
8657
  retainedScreenshotMetadata,
6871
8658
  );
6872
8659
  }
8660
+ normalizedSdkEvents = normalizeSdkEvent(
8661
+ durableSdkEvent as typeof next.value,
8662
+ retainedScreenshotMetadata
8663
+ ? {
8664
+ toolOutputOverride: retainedScreenshotMetadata,
8665
+ retainedOutputEvidence: retainedScreenshotMetadata.available
8666
+ ? retainedScreenshotMetadata
8667
+ : {
8668
+ available: false,
8669
+ reason: retainedScreenshotMetadata.reason,
8670
+ },
8671
+ }
8672
+ : {},
8673
+ );
8674
+ const normalizedToolOutput = normalizedSdkEvents.find(
8675
+ (event) =>
8676
+ event.type === "agent.toolCall.output" &&
8677
+ (event.payload as { id?: unknown }).id === completedToolCall.callId,
8678
+ )?.payload as { output?: unknown } | undefined;
8679
+ if (!normalizedToolOutput || !Object.hasOwn(normalizedToolOutput, "output")) {
8680
+ throw new Error(
8681
+ `Completed SDK tool call ${completedToolCall.callId} produced no durable output projection`,
8682
+ );
8683
+ }
6873
8684
  const durableResultItem = retainedScreenshotMetadata
6874
8685
  ? (compactRetainedScreenshotHistory(
6875
8686
  [completedToolCall.resultItem],
@@ -6891,12 +8702,39 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6891
8702
  callId: completedToolCall.callId,
6892
8703
  modelToolOutputTruncationTokens: modelRunSettings.modelToolOutputTruncationTokens,
6893
8704
  resultItem: durableResultItem as Record<string, unknown>,
8705
+ eventOutput: normalizedToolOutput.output,
8706
+ ...(videoGenerationAcceptancesByCallId.has(completedToolCall.callId)
8707
+ ? {
8708
+ videoGenerationAcceptance: videoGenerationAcceptancesByCallId.get(
8709
+ completedToolCall.callId,
8710
+ )!,
8711
+ }
8712
+ : {}),
6894
8713
  });
6895
8714
  if (!recorded.accepted) {
6896
8715
  throw new TurnAttemptFencedError(
6897
8716
  "turn attempt ended while recording a tool-call result",
6898
8717
  );
6899
8718
  }
8719
+ const videoAcceptance = videoGenerationAcceptancesByCallId.get(
8720
+ completedToolCall.callId,
8721
+ );
8722
+ if (videoAcceptance && startVideoGenerationWorkflow) {
8723
+ try {
8724
+ await startVideoGenerationWorkflow({
8725
+ accountId: input.accountId,
8726
+ workspaceId: input.workspaceId,
8727
+ operationId: videoAcceptance.operationId,
8728
+ });
8729
+ } catch (error) {
8730
+ // The accepted operation is durable. The recovery sweep starts
8731
+ // the same deterministic workflow ID if this nudge fails.
8732
+ observability.warn("Video generation workflow start deferred", {
8733
+ operationId: videoAcceptance.operationId,
8734
+ errorClass: error instanceof Error ? error.name : "UnknownError",
8735
+ });
8736
+ }
8737
+ }
6900
8738
  const belongsToCurrentBatch = currentToolBatchCallIds.has(completedToolCall.callId);
6901
8739
  if (belongsToCurrentBatch) {
6902
8740
  currentToolBatchCompletedCallIds.add(completedToolCall.callId);
@@ -6919,24 +8757,31 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6919
8757
  completedCurrentToolBatch = currentBatchIsStable;
6920
8758
  }
6921
8759
  }
6922
- const normalized = normalizeSdkEvent(
6923
- next.value,
6924
- retainedScreenshotMetadata
6925
- ? {
6926
- toolOutputOverride: retainedScreenshotMetadata,
6927
- retainedOutputEvidence: retainedScreenshotMetadata.available
6928
- ? retainedScreenshotMetadata
6929
- : {
6930
- available: false,
6931
- reason: retainedScreenshotMetadata.reason,
6932
- },
6933
- }
6934
- : {},
6935
- );
8760
+ const normalized =
8761
+ normalizedSdkEvents ??
8762
+ normalizeSdkEvent(
8763
+ durableSdkEvent as typeof next.value,
8764
+ retainedScreenshotMetadata
8765
+ ? {
8766
+ toolOutputOverride: retainedScreenshotMetadata,
8767
+ retainedOutputEvidence: retainedScreenshotMetadata.available
8768
+ ? retainedScreenshotMetadata
8769
+ : {
8770
+ available: false,
8771
+ reason: retainedScreenshotMetadata.reason,
8772
+ },
8773
+ }
8774
+ : {},
8775
+ );
6936
8776
  for (const event of normalized) {
6937
8777
  streamTiming.onEvent(event.type);
6938
8778
  await batcher.push(event);
6939
8779
  }
8780
+ // Structural tool-output events await their durable append before
8781
+ // push returns. The complete result is now retained in the event
8782
+ // row and pending-call recovery receipt, so release only our
8783
+ // duplicate live SDK marker before a long turn accumulates it.
8784
+ releaseMcpResultCustomDataFromSdkEvent(durableSdkEvent);
6940
8785
  if (stableToolCallIdsToClear) {
6941
8786
  const cleared = await clearDurablePendingSessionToolCalls(db, {
6942
8787
  accountId: input.accountId,
@@ -6957,7 +8802,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6957
8802
  currentToolBatchCompletedCallIds = new Set<string>();
6958
8803
  }
6959
8804
  for (const callId of stableToolCallIdsToClear) {
6960
- computerScreenshotCallIds.delete(callId);
8805
+ retainedSessionImageCallIds.delete(callId);
6961
8806
  }
6962
8807
  }
6963
8808
  }
@@ -6999,9 +8844,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6999
8844
  });
7000
8845
  let aggregateAuthoritative = false;
7001
8846
  await recordCompletedModelCallBeforeOwnershipFences({
7002
- renewLease: () => renewCodexLease("model_usage"),
7003
- leaseLost: () => codexLeaseLost,
7004
- leaseLostMessage: "Codex credential lease expired during the active turn",
8847
+ renewLease: () => renewServingCredentialLease("model_usage"),
8848
+ leaseLost: servingCredentialLeaseLost,
8849
+ leaseLostMessage: "Provider credential lease expired during the active turn",
7005
8850
  recordUsage: async () => {
7006
8851
  const billing = await recordModelUsageAndDebitCredits(settings, db, {
7007
8852
  accountId: input.accountId,
@@ -7072,6 +8917,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7072
8917
  const approvals = runtime.serializeApprovals(stream.interruptions);
7073
8918
  const humanInputInterruptions =
7074
8919
  runtime.serializeHumanInputRequests?.(stream.interruptions) ?? [];
8920
+ const interactionInterventionInterruptions =
8921
+ runtime.serializeInteractionInterventionRequests?.(stream.interruptions) ?? [];
7075
8922
  const latestWorkspace = await getWorkspace(db, input.workspaceId);
7076
8923
  if (!latestWorkspace) throw new Error(`Workspace not found: ${input.workspaceId}`);
7077
8924
  assertWorkspaceHumanInputAllowed(
@@ -7123,6 +8970,26 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7123
8970
  },
7124
8971
  },
7125
8972
  }));
8973
+ const interactionInterventionRequests = interactionInterventionInterruptions.map(
8974
+ (interruption) => ({
8975
+ id: stableInteractionInterventionId(
8976
+ input.sessionId,
8977
+ activeTurnId,
8978
+ interruption.toolCallId,
8979
+ ),
8980
+ operationId: stableInteractionInterventionOperationId(
8981
+ input.sessionId,
8982
+ activeTurnId,
8983
+ interruption.toolCallId,
8984
+ ),
8985
+ toolCallId: interruption.toolCallId,
8986
+ input: interruption.input,
8987
+ }),
8988
+ );
8989
+ const pendingApprovals = [
8990
+ ...approvals,
8991
+ ...interactionInterventionInterruptions.map((interruption) => interruption.approval),
8992
+ ];
7126
8993
  if (
7127
8994
  !(await settle!({
7128
8995
  events: [
@@ -7144,11 +9011,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7144
9011
  sessionStatus: "requires_action",
7145
9012
  activeTurnId,
7146
9013
  runState: {
7147
- serializedRunState: stream.state.toString(),
7148
- pendingApprovals: approvals,
9014
+ serializedRunState: compactApprovalRunState(stream.state.toString()),
9015
+ pendingApprovals,
7149
9016
  humanInputRequests: humanInputRequests.map(
7150
9017
  ({ isNew: _isNew, ...request }) => request,
7151
9018
  ),
9019
+ interactionInterventionRequests,
7152
9020
  },
7153
9021
  }))
7154
9022
  ) {
@@ -7360,15 +9228,24 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7360
9228
  // back to the workflow-claimed turn when the local lookup had not
7361
9229
  // finished yet.
7362
9230
  const recoveryTurnId = turnId;
7363
- // P1.2: a lease supersession during resume (a newer epoch re-established
7364
- // the box concurrently) is NOT a session failure. Recover the same turn
7365
- // so its next attempt reattaches under the current epoch.
7366
- if (error instanceof SandboxLeaseSupersededError && recoveryTurnId) {
9231
+ // A true epoch supersession and a provider lifecycle transition are both
9232
+ // recoverable control-plane states, never session failures. The latter is
9233
+ // paced briefly; the next acquire waits on the durable claim and resumes
9234
+ // immediately when it clears.
9235
+ if (
9236
+ (error instanceof SandboxLeaseSupersededError ||
9237
+ error instanceof SandboxLeaseTransitionError) &&
9238
+ recoveryTurnId
9239
+ ) {
7367
9240
  try {
7368
9241
  const fencedLease = await readLease(db, input.workspaceId, error.sandboxGroupId).catch(
7369
9242
  () => null,
7370
9243
  );
7371
- const rotationPending = fencedLease?.rotationRequestedAt != null;
9244
+ const lifecycleTransition = error instanceof SandboxLeaseTransitionError;
9245
+ const rotationPending =
9246
+ fencedLease?.rotationRequestedAt != null ||
9247
+ (lifecycleTransition && error.reason === "rotation_in_progress");
9248
+ const transitionPending = lifecycleTransition || rotationPending;
7372
9249
  const deadlineRotationPending =
7373
9250
  rotationPending && fencedLease?.rotationReason === "provider_deadline";
7374
9251
  const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
@@ -7378,13 +9255,20 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7378
9255
  attemptId: input.attemptId,
7379
9256
  reason: deadlineRotationPending
7380
9257
  ? "sandbox_deadline_rotation"
7381
- : "sandbox_lease_superseded",
7382
- ...(rotationPending
9258
+ : lifecycleTransition
9259
+ ? "sandbox_lifecycle_transition"
9260
+ : "sandbox_lease_superseded",
9261
+ ...(transitionPending
7383
9262
  ? {
7384
9263
  detail: {
7385
9264
  sandboxGroupId: error.sandboxGroupId,
7386
9265
  leaseEpoch: error.leaseEpoch,
7387
- rotationReason: fencedLease?.rotationReason ?? "operator",
9266
+ ...(rotationPending
9267
+ ? {
9268
+ rotationReason: fencedLease?.rotationReason ?? "operator",
9269
+ }
9270
+ : {}),
9271
+ ...(lifecycleTransition ? { transitionReason: error.reason } : {}),
7388
9272
  },
7389
9273
  }
7390
9274
  : {}),
@@ -7406,17 +9290,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7406
9290
  turnMetricOutcome = "recovering";
7407
9291
  return claimedResult({
7408
9292
  status: "recovering",
7409
- ...(rotationPending
9293
+ ...(transitionPending
7410
9294
  ? {
7411
9295
  continueDelayMs: sandboxDeadlineRotationRecoveryDelayMs(settings),
7412
9296
  }
7413
9297
  : {}),
7414
9298
  });
7415
9299
  } catch (recoveryError) {
7416
- console.error(
7417
- "sandbox lease supersession recovery failed",
7418
- safeErrorDiagnostic(recoveryError),
7419
- );
9300
+ console.error("sandbox lifecycle recovery failed", safeErrorDiagnostic(recoveryError));
7420
9301
  throw recoveryError;
7421
9302
  }
7422
9303
  }
@@ -7482,8 +9363,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7482
9363
  // the frozen RunState blob (the only representation of a turn
7483
9364
  // paused mid-flight), so swapping the trigger for a resume notice
7484
9365
  // could drop the user's decision. Re-applying an already-consumed
7485
- // approval re-executes at most the single approved step — the same
7486
- // bound every recovery already accepts.
9366
+ // approval re-enters at most the single approved step. Every
9367
+ // approval-gated MCP action crosses the durable execution admission
9368
+ // fence before its provider invocation, so a consumed step resumes
9369
+ // as already-executed or outcome-unknown rather than calling MCP again.
7487
9370
  const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
7488
9371
  sessionId: input.sessionId,
7489
9372
  turnId: recoveryTurnId,
@@ -7691,6 +9574,29 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7691
9574
  ) {
7692
9575
  return await settleLostCodexAttempt(turnId, codexLeaseHolderId, codexLeaseGeneration);
7693
9576
  }
9577
+ if (xaiLeaseLost && isXaiTurn && publish && turnId && turnStartedPublished) {
9578
+ await flushRuntimeBatcher();
9579
+ await reconcileConversationTruth({ requireDurable: true });
9580
+ const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
9581
+ sessionId: input.sessionId,
9582
+ turnId,
9583
+ triggerEventId: triggerEventId!,
9584
+ attemptId: input.attemptId,
9585
+ reason: "xai_lease_lost",
9586
+ detail: { provider: "supergrok-subscription" },
9587
+ });
9588
+ if (recovery.action === "stale") {
9589
+ acknowledgeLostAttemptOwnership();
9590
+ activityStatus = "cancelled";
9591
+ turnMetricOutcome = "cancelled";
9592
+ return claimedResult({ status: "cancelled" });
9593
+ }
9594
+ acknowledgeRecoveryQuiescence();
9595
+ await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, recovery.events);
9596
+ activityStatus = "recovering";
9597
+ turnMetricOutcome = "recovering";
9598
+ return claimedResult({ status: "recovering" });
9599
+ }
7694
9600
  // Definitive Codex credential/account refusals are the only provider
7695
9601
  // errors that may walk the pool. This is an explicit checkpoint + SAME
7696
9602
  // turn recovery, never an SDK/Temporal blind retry. A network break,
@@ -7874,6 +9780,143 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7874
9780
  }
7875
9781
  }
7876
9782
  }
9783
+ const xaiCredentialFailure =
9784
+ isXaiTurn && effectiveXaiCredentialId ? classifyXaiCredentialFailure(error) : null;
9785
+ if (
9786
+ xaiCredentialFailure &&
9787
+ effectiveXaiCredentialId &&
9788
+ xaiAuthoritySnapshot &&
9789
+ xaiLeaseSubjectId &&
9790
+ xaiLeaseHolderId &&
9791
+ xaiLeaseGeneration !== null &&
9792
+ publish &&
9793
+ turnId &&
9794
+ turnStartedPublished
9795
+ ) {
9796
+ await flushRuntimeBatcher();
9797
+ await reconcileConversationTruth({ requireDurable: true });
9798
+ const goal = await getSessionGoal(db, input.workspaceId, input.sessionId).catch(() => null);
9799
+ const activeGoal = goal?.status === "active" ? goal : null;
9800
+ const now = new Date();
9801
+ const cooldownUntil =
9802
+ xaiCredentialFailure.kind === "rate_limit"
9803
+ ? new Date(now.getTime() + Math.max(1, xaiCredentialFailure.cooldownMs ?? 60_000))
9804
+ : null;
9805
+ const failurePayload = {
9806
+ error:
9807
+ xaiCredentialFailure.kind === "auth"
9808
+ ? "The serving SuperGrok account requires reconnection"
9809
+ : xaiCredentialFailure.kind === "forbidden"
9810
+ ? "The serving SuperGrok account is not authorized for this request"
9811
+ : "The serving SuperGrok account is temporarily rate limited",
9812
+ code:
9813
+ xaiCredentialFailure.kind === "auth"
9814
+ ? "xai_relogin_required"
9815
+ : xaiCredentialFailure.kind === "forbidden"
9816
+ ? "xai_account_forbidden"
9817
+ : "xai_account_rate_limited",
9818
+ detail: "the same accepted turn is waiting for another eligible account",
9819
+ };
9820
+ const armed = await armXaiCapacityWait(db, {
9821
+ accountId: input.accountId,
9822
+ workspaceId: input.workspaceId,
9823
+ subjectId: xaiLeaseSubjectId,
9824
+ sessionId: input.sessionId,
9825
+ turnId,
9826
+ attemptId: input.attemptId,
9827
+ workflowId: input.workflowId,
9828
+ authoritySnapshot: xaiAuthoritySnapshot,
9829
+ goalId: activeGoal?.id ?? null,
9830
+ goalVersion: activeGoal?.version ?? null,
9831
+ earliestResetAt: cooldownUntil,
9832
+ failurePayload,
9833
+ leaseFence: {
9834
+ holderId: xaiLeaseHolderId,
9835
+ generation: xaiLeaseGeneration,
9836
+ },
9837
+ credentialQuarantine:
9838
+ xaiCredentialFailure.kind === "auth"
9839
+ ? {
9840
+ kind: "status",
9841
+ status: "needs_relogin",
9842
+ lastError: "model request remained unauthorized after refresh",
9843
+ }
9844
+ : xaiCredentialFailure.kind === "forbidden"
9845
+ ? {
9846
+ kind: "status",
9847
+ status: "error",
9848
+ lastError: "model request was forbidden for this credential",
9849
+ }
9850
+ : { kind: "cooldown", until: cooldownUntil! },
9851
+ now,
9852
+ });
9853
+ if (armed.action === "waiting") {
9854
+ xaiLeaseHeld = false;
9855
+ xaiCredentialQuarantined = true;
9856
+ await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, armed.events);
9857
+ const evaluated = await reconcileXaiCapacityWait(db, {
9858
+ accountId: input.accountId,
9859
+ workspaceId: input.workspaceId,
9860
+ sessionId: input.sessionId,
9861
+ waiterId: armed.waiter.id,
9862
+ generation: armed.waiter.generation,
9863
+ now,
9864
+ });
9865
+ if (evaluated.events.length > 0) {
9866
+ await publishDurableSessionEvents(
9867
+ bus,
9868
+ input.workspaceId,
9869
+ input.sessionId,
9870
+ evaluated.events,
9871
+ );
9872
+ }
9873
+ activityError = error;
9874
+ if (evaluated.action === "resumed") {
9875
+ activityStatus = "recovering";
9876
+ turnMetricOutcome = "recovering";
9877
+ return claimedResult({ status: "recovering" });
9878
+ }
9879
+ if (evaluated.action === "waiting") {
9880
+ activityStatus = "waiting_capacity";
9881
+ turnMetricOutcome = "recovering";
9882
+ return claimedResult({
9883
+ status: "waiting_capacity",
9884
+ capacityWait: {
9885
+ provider: "xai",
9886
+ waiterId: evaluated.waiter.id,
9887
+ generation: evaluated.waiter.generation,
9888
+ nextCheckAt: evaluated.waiter.nextCheckAt.toISOString(),
9889
+ wakeRevision: evaluated.waiter.wakeRevision,
9890
+ },
9891
+ });
9892
+ }
9893
+ acknowledgeLostAttemptOwnership();
9894
+ activityStatus = "cancelled";
9895
+ turnMetricOutcome = "cancelled";
9896
+ return claimedResult({ status: "cancelled" });
9897
+ }
9898
+
9899
+ const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
9900
+ sessionId: input.sessionId,
9901
+ turnId,
9902
+ triggerEventId: triggerEventId!,
9903
+ attemptId: input.attemptId,
9904
+ reason: "xai_credential_recheck",
9905
+ detail: failurePayload,
9906
+ });
9907
+ if (recovery.action === "stale") {
9908
+ acknowledgeLostAttemptOwnership();
9909
+ activityStatus = "cancelled";
9910
+ turnMetricOutcome = "cancelled";
9911
+ return claimedResult({ status: "cancelled" });
9912
+ }
9913
+ acknowledgeRecoveryQuiescence();
9914
+ await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, recovery.events);
9915
+ activityStatus = "recovering";
9916
+ turnMetricOutcome = "recovering";
9917
+ activityError = error;
9918
+ return claimedResult({ status: "recovering" });
9919
+ }
7877
9920
  // A ChatGPT/Codex usage cap (429 usage_limit_reached) is account state,
7878
9921
  // NOT an agent failure: surface the precise, actionable message (so the
7879
9922
  // user sees the reset window) but idle the session — never go terminal,
@@ -7966,14 +10009,14 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7966
10009
  });
7967
10010
  if (newHome) {
7968
10011
  rotated = true;
7969
- const pinMutation = await withCodexCapacityMutation(
10012
+ const pinMutation = await withSessionCodexCapacityMutation(
7970
10013
  db,
7971
10014
  {
7972
10015
  workspaceId: input.workspaceId,
7973
10016
  reason: "codex_policy_pin_resharded",
7974
10017
  },
7975
10018
  async (tx) => {
7976
- const changed = await setSessionCodexPin(
10019
+ const changed = await setSessionCodexPinInTransaction(
7977
10020
  tx,
7978
10021
  input.workspaceId,
7979
10022
  input.sessionId,
@@ -8248,7 +10291,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8248
10291
  ) {
8249
10292
  await flushRuntimeBatcher();
8250
10293
  await reconcileConversationTruth({ requireDurable: true });
8251
- const activeHistory = await getActiveSessionHistoryItems(
10294
+ const activeHistory = await getActiveSessionHistoryItemsPaged(
8252
10295
  db,
8253
10296
  input.workspaceId,
8254
10297
  input.sessionId,
@@ -8304,9 +10347,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8304
10347
  // truth, recover this SAME accepted turn, then let the workflow re-claim
8305
10348
  // it after a pacing delay. This is independent of goal state and never
8306
10349
  // relies on a synthetic continuation prompt.
8307
- const failure = agentRunFailurePayload(error, { isCodexTurn }) as ReturnType<
8308
- typeof agentRunFailurePayload
8309
- >;
10350
+ const failure = agentRunFailurePayload(error, {
10351
+ isCodexTurn,
10352
+ }) as ReturnType<typeof agentRunFailurePayload>;
8310
10353
  if (isSessionEventPersistenceError(error)) {
8311
10354
  // Preserve the exact source message in the internal runtime diagnostic;
8312
10355
  // SQLSTATE/catalog facts remain separate classification attributes.
@@ -8454,10 +10497,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8454
10497
  gitCredentialRenewalClosed = true;
8455
10498
  const gitRenewalsToStop = gitCredentialRenewals;
8456
10499
  gitCredentialRenewals = [];
8457
- toolspaceTokenRenewalClosed = true;
8458
- const toolspaceRenewalToStop =
8459
- toolspaceTokenRenewal as ToolspaceTokenRenewalController | null;
8460
- toolspaceTokenRenewal = null;
10500
+ codemodeTokenRenewalClosed = true;
10501
+ const codemodeRenewalToStop = codemodeTokenRenewal as CodemodeTokenRenewalController | null;
10502
+ codemodeTokenRenewal = null;
8461
10503
  runCredentialRenewalClosed = true;
8462
10504
  const runRenewalToStop = runCredentialRenewal as RunCredentialRenewalController | null;
8463
10505
  runCredentialRenewal = null;
@@ -8509,9 +10551,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8509
10551
  toolCancellationFence,
8510
10552
  cancellationReason: cancellationSignal?.reason ?? new Error("TURN_ATTEMPT_FINALIZED"),
8511
10553
  gitCredentialRenewals: gitRenewalsToStop,
8512
- toolspaceTokenRenewal: toolspaceRenewalToStop,
10554
+ codemodeTokenRenewal: codemodeRenewalToStop,
8513
10555
  runCredentialRenewal: runRenewalToStop,
8514
10556
  });
10557
+ attemptWritersDrained = true;
8515
10558
  if (acknowledgeQuiescence) {
8516
10559
  // A cancellation before sandbox-backed capabilities exist still has
8517
10560
  // no tool controller to drain. Sandbox agent construction fails closed
@@ -8537,7 +10580,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8537
10580
  const recoveryMode = await persistOrSignalSessionAttemptQuiescence({
8538
10581
  proof,
8539
10582
  persistReceipt: async () =>
8540
- await markSessionAttemptQuiesced(db, {
10583
+ await commitSessionAttemptQuiescence(db, {
8541
10584
  accountId: input.accountId,
8542
10585
  workspaceId: input.workspaceId,
8543
10586
  sessionId: input.sessionId,
@@ -8547,6 +10590,21 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8547
10590
  temporalActivityId: dispatchId,
8548
10591
  allowUninterrupted: true,
8549
10592
  }),
10593
+ ...(wakeSessionWorkflow
10594
+ ? {
10595
+ deliverWorkflowWake: async (
10596
+ wake: NonNullable<SessionAttemptQuiescenceCommit["workflowWake"]>,
10597
+ ) =>
10598
+ await wakeSessionWorkflow({
10599
+ accountId: wake.accountId,
10600
+ workspaceId: wake.workspaceId,
10601
+ sessionId: wake.sessionId,
10602
+ workflowId: wake.temporalWorkflowId,
10603
+ wakeRevision: wake.wakeRevision,
10604
+ interruptionRequested: wake.interruptionRequested,
10605
+ }),
10606
+ }
10607
+ : {}),
8550
10608
  publishEvents: async (events) => {
8551
10609
  await waitForTurnFinalizerStep(
8552
10610
  publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, events),
@@ -8570,6 +10628,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8570
10628
  safeErrorDiagnostic(error),
8571
10629
  );
8572
10630
  },
10631
+ onWakeFailure: (error) => {
10632
+ console.error(
10633
+ "agent turn quiescence immediate workflow wake failed; outbox repair retained",
10634
+ safeErrorDiagnostic(error),
10635
+ );
10636
+ },
8573
10637
  onPublishFailure: (error) => {
8574
10638
  console.error(
8575
10639
  "agent turn quiescence event fanout failed",
@@ -8664,6 +10728,57 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8664
10728
  );
8665
10729
  codexLeaseHeld = false;
8666
10730
  }
10731
+ if (
10732
+ effectiveXaiCredentialId &&
10733
+ xaiLeaseSubjectId &&
10734
+ xaiRequestContext &&
10735
+ !xaiCredentialQuarantined
10736
+ ) {
10737
+ const quota = await waitForTurnFinalizerStep(
10738
+ fetchXaiSubscriptionQuota({ context: xaiRequestContext }).catch(() => null),
10739
+ finalizerSignal,
10740
+ );
10741
+ if (quota) {
10742
+ await waitForTurnFinalizerStep(
10743
+ updateXaiQuotaMetadata(db, {
10744
+ workspaceId: input.workspaceId,
10745
+ subjectId: xaiLeaseSubjectId,
10746
+ credentialId: effectiveXaiCredentialId,
10747
+ quotaUsedPercent: quota.usedPercent,
10748
+ quotaResetAt: quota.period?.end ?? null,
10749
+ quotaCheckedAt: quota.checkedAt,
10750
+ exhaustedUntil:
10751
+ quota.usedPercent !== null && quota.usedPercent >= 100
10752
+ ? (quota.period?.end ?? null)
10753
+ : null,
10754
+ }).catch(() => undefined),
10755
+ finalizerSignal,
10756
+ );
10757
+ }
10758
+ }
10759
+ if (xaiLeaseHeartbeatTimer) {
10760
+ clearInterval(xaiLeaseHeartbeatTimer);
10761
+ xaiLeaseHeartbeatTimer = undefined;
10762
+ }
10763
+ if (
10764
+ xaiLeaseHeld &&
10765
+ turnId &&
10766
+ xaiLeaseSubjectId &&
10767
+ xaiLeaseHolderId &&
10768
+ xaiLeaseGeneration !== null
10769
+ ) {
10770
+ await waitForTurnFinalizerStep(
10771
+ releaseXaiCredentialLease(db, {
10772
+ workspaceId: input.workspaceId,
10773
+ subjectId: xaiLeaseSubjectId,
10774
+ turnId,
10775
+ holderId: xaiLeaseHolderId,
10776
+ generation: xaiLeaseGeneration,
10777
+ }).catch(() => undefined),
10778
+ finalizerSignal,
10779
+ );
10780
+ xaiLeaseHeld = false;
10781
+ }
8667
10782
  // Workbench v2 turn-end workspace capture — runs FIRST in
8668
10783
  // the turn-end finally, while the box is MAXIMALLY ALIVE. The agent's last
8669
10784
  // tool ran before this finally, so /workspace is already final; capture is
@@ -8733,6 +10848,13 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8733
10848
  ...(finalizerSignal ? { signal: finalizerSignal } : {}),
8734
10849
  });
8735
10850
  }
10851
+ if (codemodeDispatcher) {
10852
+ await waitForTurnFinalizerStep(
10853
+ codemodeDispatcher.close().catch(() => undefined),
10854
+ finalizerSignal,
10855
+ );
10856
+ codemodeDispatcher = null;
10857
+ }
8736
10858
  if (preparedTools) {
8737
10859
  await waitForTurnFinalizerStep(
8738
10860
  preparedTools.close().catch(() => undefined),
@@ -8820,17 +10942,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8820
10942
  // while the box is still solidly alive, instead of racing the turn-end
8821
10943
  // teardown that was killing 100% of captures on real Modal desktop boxes.
8822
10944
  }
8823
- const sandboxToRelease = resolvedSandbox;
8824
- resolvedSandbox = null; // drop ownership now; the exact-holder release may finish later
8825
- await waitForTurnFinalizerStep(
8826
- sandboxToRelease.release().catch((releaseError) => {
8827
- console.error(
8828
- "sandbox lease release failed (turn outcome unaffected)",
8829
- safeErrorDiagnostic(releaseError),
8830
- );
8831
- }),
8832
- finalizerSignal,
8833
- );
8834
10945
  }
8835
10946
  } catch (error) {
8836
10947
  finalizationError ??= error;
@@ -8839,9 +10950,42 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8839
10950
  safeErrorDiagnostic(error),
8840
10951
  );
8841
10952
  } finally {
8842
- // If fallible finalization exited before the ordinary exact-holder
8843
- // release, the resume signal is the unconditional last line of defense.
8844
- // Normal finalization already removed its listener in release().
10953
+ // The writer drain is the only authority that licenses settling an
10954
+ // abandoned turn admission. Keep this second-stage release outside all
10955
+ // later housekeeping so an event/cache/capture/recording failure cannot
10956
+ // strand a null-outcome admission forever. Do not race it against
10957
+ // Temporal cancellation: the eager listener may already have dropped
10958
+ // the holder, and this idempotent proof-bearing pass still must run.
10959
+ stopLeaseHeartbeat();
10960
+ const sandboxReleaseTargets = new Set(lateSandboxesAwaitingWriterDrain);
10961
+ lateSandboxesAwaitingWriterDrain.clear();
10962
+ if (resolvedSandbox) {
10963
+ sandboxReleaseTargets.add(resolvedSandbox);
10964
+ resolvedSandbox = null;
10965
+ }
10966
+ if (attemptWritersDrained) {
10967
+ for (const sandboxToRelease of sandboxReleaseTargets) {
10968
+ try {
10969
+ await releaseTurnSandboxAfterWriterDrain(sandboxToRelease);
10970
+ } catch (releaseError) {
10971
+ finalizationError ??= releaseError;
10972
+ console.error(
10973
+ "sandbox lease quiesced release failed (turn outcome unaffected)",
10974
+ safeErrorDiagnostic(releaseError),
10975
+ );
10976
+ }
10977
+ }
10978
+ } else {
10979
+ // No proof exists. Ensure every late result still receives the eager
10980
+ // leak-prevention release while preserving its admissions as a
10981
+ // fail-closed archive-capture fence.
10982
+ for (const sandboxToRelease of sandboxReleaseTargets) {
10983
+ await sandboxToRelease.release().catch(() => undefined);
10984
+ }
10985
+ }
10986
+ // Close provisioning after consuming the currently known targets. A
10987
+ // provider result that races in later is routed through releaseLateSandbox;
10988
+ // targets already released above synchronously removed their listener.
8845
10989
  if (!sandboxResumeController.signal.aborted) {
8846
10990
  sandboxResumeController.abort(
8847
10991
  cancellationSignal?.reason ?? new Error("TURN_ATTEMPT_FINALIZED"),
@@ -8868,12 +11012,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8868
11012
  value: physicalCancellationDurationSeconds,
8869
11013
  });
8870
11014
  observability.info("agent turn physical cancellation completed", {
8871
- "opengeni.session_id": input.sessionId,
8872
- "opengeni.turn_id": turnId ?? "",
8873
- "opengeni.attempt_id": input.attemptId,
8874
- "opengeni.physical_cancellation_duration_ms": Math.round(
8875
- physicalCancellationDurationSeconds * 1000,
8876
- ),
11015
+ durationMs: Math.round(physicalCancellationDurationSeconds * 1000),
11016
+ ...(sandboxGroupId
11017
+ ? {
11018
+ sandboxLeaseKey: sandboxLeaseTelemetryKey(input.workspaceId, sandboxGroupId),
11019
+ }
11020
+ : {}),
8877
11021
  });
8878
11022
  }
8879
11023
  observability.recordWorkerActivity({
@@ -8900,6 +11044,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8900
11044
  ? safeErrorForTelemetry(finalizationError ?? activityError)
8901
11045
  : undefined,
8902
11046
  });
11047
+ // This timer runs only after the activity promise and its full turn
11048
+ // stack unwind. Checkpoint collection alone cannot reclaim objects that
11049
+ // remain strongly reachable until this terminal boundary.
11050
+ turnCompletionMemoryCollector.schedule(observability);
8903
11051
  assertPhysicalToolQuiescenceForCancellation({
8904
11052
  acknowledgeQuiescence,
8905
11053
  physicalToolQuiescenceConfirmed,
@@ -9012,6 +11160,51 @@ export function isTransientProviderError(error: unknown): boolean {
9012
11160
  );
9013
11161
  }
9014
11162
 
11163
+ export type XaiCredentialFailure = {
11164
+ kind: "auth" | "forbidden" | "rate_limit";
11165
+ cooldownMs: number | null;
11166
+ };
11167
+
11168
+ /**
11169
+ * Only definitive SuperGrok account refusals may move the same logical turn to
11170
+ * another credential. The marked transport response proves inference was
11171
+ * rejected before a stream began; refresh relogin is equally definitive.
11172
+ */
11173
+ export function classifyXaiCredentialFailure(error: unknown): XaiCredentialFailure | null {
11174
+ let relogin: unknown = error;
11175
+ for (let depth = 0; depth < 6 && relogin && typeof relogin === "object"; depth += 1) {
11176
+ if (relogin instanceof XaiSubscriptionReloginRequired) {
11177
+ return { kind: "auth", cooldownMs: null };
11178
+ }
11179
+ relogin = (relogin as Record<string, unknown>).cause;
11180
+ }
11181
+ if (!isXaiSubscriptionTransportError(error)) return null;
11182
+ let current: unknown = error;
11183
+ for (let depth = 0; depth < 6 && current && typeof current === "object"; depth += 1) {
11184
+ const value = current as Record<string, unknown>;
11185
+ const body =
11186
+ value.error && typeof value.error === "object"
11187
+ ? (value.error as Record<string, unknown>)
11188
+ : null;
11189
+ const status = Number(value.status ?? value.statusCode ?? body?.status ?? body?.statusCode);
11190
+ const code = String(value.code ?? body?.code ?? "").toLowerCase();
11191
+ if (status === 401 || code === "unauthorized" || code === "invalid_token") {
11192
+ return { kind: "auth", cooldownMs: null };
11193
+ }
11194
+ if (status === 403) {
11195
+ return { kind: "forbidden", cooldownMs: null };
11196
+ }
11197
+ if (status === 429 || code === "rate_limit_exceeded" || code === "too_many_requests") {
11198
+ return {
11199
+ kind: "rate_limit",
11200
+ cooldownMs: providerRetryAfterMs(error) ?? PROVIDER_BACKPRESSURE_DELAY_MS,
11201
+ };
11202
+ }
11203
+ current = value.cause;
11204
+ }
11205
+ return null;
11206
+ }
11207
+
9015
11208
  export function agentRunFailurePayload(
9016
11209
  error: unknown,
9017
11210
  options: { isCodexTurn?: boolean } = {},
@@ -9054,6 +11247,24 @@ export function agentRunFailurePayload(
9054
11247
  typeof error === "object" && error !== null && "code" in error
9055
11248
  ? String((error as { code?: unknown }).code)
9056
11249
  : undefined;
11250
+ if (error instanceof ActiveSessionHistoryLimitExceededError) {
11251
+ return {
11252
+ error:
11253
+ "The session's active conversation history exceeds the worker's safe materialization envelope. Clear the session context before retrying; an oversized history cannot be compacted safely in a serving worker.",
11254
+ code: error.code,
11255
+ retryable: false,
11256
+ detail: error.message,
11257
+ };
11258
+ }
11259
+ if (error instanceof ApprovalRunStateLimitExceededError) {
11260
+ return {
11261
+ error:
11262
+ "The saved approval state exceeds the worker's safe materialization envelope. Clear the pending approval context before retrying.",
11263
+ code: error.code,
11264
+ retryable: false,
11265
+ detail: error.message,
11266
+ };
11267
+ }
9057
11268
  // An accepted Codex stream with no terminal response is malformed/partial,
9058
11269
  // not provider backpressure. Replaying the same accepted turn could repeat
9059
11270
  // model or tool effects, so this marked transport failure must outrank the
@@ -9367,6 +11578,10 @@ export function pendingToolCallFromSdkEvent(event: unknown): {
9367
11578
  return null;
9368
11579
  }
9369
11580
  const raw = item.rawItem as Record<string, unknown>;
11581
+ // The hosted image call is a complete provider fact carried by one item; it
11582
+ // never receives a separate function result and therefore must not enter the
11583
+ // pending function-call ledger.
11584
+ if (raw.type === "hosted_tool_call" && raw.name === "image_generation_call") return null;
9370
11585
  const callId = raw.callId ?? raw.call_id ?? raw.id;
9371
11586
  const callType = raw.type;
9372
11587
  if (typeof callId !== "string" || callId.length === 0 || typeof callType !== "string") {
@@ -9381,6 +11596,11 @@ export function pendingToolCallFromSdkEvent(event: unknown): {
9381
11596
  };
9382
11597
  }
9383
11598
 
11599
+ /** Tools whose intentional visual result must survive inline-media sanitization. */
11600
+ export function toolCallProducesRetainableSessionImage(name: string | null): boolean {
11601
+ return name === "computer_screenshot" || name === "view_image";
11602
+ }
11603
+
9384
11604
  export function completedToolCallFromSdkEvent(event: unknown): {
9385
11605
  callId: string;
9386
11606
  resultItem: Record<string, unknown>;
@@ -9505,6 +11725,9 @@ export type ModelUsageBillingRecord = {
9505
11725
  billingPath: "opengeni_credits" | "external";
9506
11726
  /** Same quantity written to usage_events.model.cost when present; else 0. */
9507
11727
  pricedCostMicros: number;
11728
+ /** Hypothetical provider-rate USD micros; never an OpenGeni charge. */
11729
+ estimatedProviderCostMicros: number | null;
11730
+ pricingSource: "configured_list_price" | "gateway_reported" | null;
9508
11731
  normalizedUsage: ModelCallUsageNormalization;
9509
11732
  upstreamProvider?: string;
9510
11733
  };
@@ -9552,6 +11775,37 @@ export async function recordModelUsageAndDebitCredits(
9552
11775
  );
9553
11776
  }
9554
11777
  }
11778
+ const pricingSchedules = configuredModelPricingSchedules(settings);
11779
+ const configuredPricingModel = pricingSchedules[input.model]
11780
+ ? input.model
11781
+ : input.model.startsWith("codex/") && pricingSchedules[input.model.slice("codex/".length)]
11782
+ ? input.model.slice("codex/".length)
11783
+ : null;
11784
+ const pricingBreakdown = gatewayBilling
11785
+ ? calculateGatewayReportedCostBreakdown(
11786
+ settings,
11787
+ input.model,
11788
+ gatewayBilling.inferenceCostUsd,
11789
+ { inputTokens },
11790
+ )
11791
+ : configuredPricingModel
11792
+ ? calculateModelUsageCostBreakdown(settings, configuredPricingModel, sanitizedUsage, {
11793
+ latencyMode: input.latencyMode ?? "standard",
11794
+ })
11795
+ : null;
11796
+ const hasCompleteCoreTokenTelemetry =
11797
+ normalizedUsage.telemetry.inputTokens !== null &&
11798
+ normalizedUsage.telemetry.outputTokens !== null;
11799
+ const estimatedProviderCostMicros = gatewayBilling
11800
+ ? (pricingBreakdown?.providerCostMicros ?? null)
11801
+ : hasCompleteCoreTokenTelemetry
11802
+ ? (pricingBreakdown?.providerCostMicros ?? null)
11803
+ : null;
11804
+ const pricingSource = gatewayBilling
11805
+ ? ("gateway_reported" as const)
11806
+ : estimatedProviderCostMicros !== null
11807
+ ? ("configured_list_price" as const)
11808
+ : null;
9555
11809
  // An externally billed turn is paid outside OpenGeni, so it
9556
11810
  // consumes ZERO OpenGeni credits and must never feed an OpenGeni cap. A
9557
11811
  // codex/<slug> model has no entry in configuredModelPricing, so the normal path
@@ -9560,7 +11814,8 @@ export async function recordModelUsageAndDebitCredits(
9560
11814
  // the API tokens:consume cap sum `model.tokens` with NO cost dimension, so
9561
11815
  // any row would count against maxMonthlyTokensPerWorkspace);
9562
11816
  // - record a `model.cost = 0` audit marker (harmless to the monthly cost cap);
9563
- // - never look up pricing and never debit credits.
11817
+ // - optionally calculate a non-charging provider estimate for Insights when
11818
+ // core token telemetry is complete, but never debit credits.
9564
11819
  if (input.externallyBilled) {
9565
11820
  await recordUsageEvent(db, {
9566
11821
  accountId: input.accountId,
@@ -9578,6 +11833,8 @@ export async function recordModelUsageAndDebitCredits(
9578
11833
  return {
9579
11834
  billingPath: "external",
9580
11835
  pricedCostMicros: 0,
11836
+ estimatedProviderCostMicros,
11837
+ pricingSource,
9581
11838
  normalizedUsage,
9582
11839
  };
9583
11840
  }
@@ -9601,20 +11858,16 @@ export async function recordModelUsageAndDebitCredits(
9601
11858
  return {
9602
11859
  billingPath: "opengeni_credits",
9603
11860
  pricedCostMicros: 0,
11861
+ estimatedProviderCostMicros,
11862
+ pricingSource,
9604
11863
  normalizedUsage,
9605
11864
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
9606
11865
  };
9607
11866
  }
9608
- if (!configuredModelPricing(settings)[input.model]) {
11867
+ if (!pricingBreakdown) {
9609
11868
  throw new Error(`Missing model pricing for ${input.model}`);
9610
11869
  }
9611
- const costMicros = gatewayBilling
9612
- ? calculateGatewayReportedCostMicros(settings, input.model, gatewayBilling.inferenceCostUsd, {
9613
- inputTokens,
9614
- })
9615
- : calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
9616
- latencyMode: input.latencyMode ?? "standard",
9617
- });
11870
+ const costMicros = pricingBreakdown.creditCostMicros;
9618
11871
  await recordUsageEvent(db, {
9619
11872
  accountId: input.accountId,
9620
11873
  workspaceId: input.workspaceId,
@@ -9658,6 +11911,8 @@ export async function recordModelUsageAndDebitCredits(
9658
11911
  return {
9659
11912
  billingPath: "opengeni_credits",
9660
11913
  pricedCostMicros: costMicros,
11914
+ estimatedProviderCostMicros,
11915
+ pricingSource,
9661
11916
  normalizedUsage,
9662
11917
  ...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
9663
11918
  };
@@ -9692,6 +11947,8 @@ export async function recordAuthoritativeModelCallFact(input: {
9692
11947
  model: input.model,
9693
11948
  billingPath: input.billing.billingPath,
9694
11949
  pricedCostMicros: input.billing.pricedCostMicros,
11950
+ estimatedProviderCostMicros: input.billing.estimatedProviderCostMicros,
11951
+ pricingSource: input.billing.pricingSource,
9695
11952
  inputTokens: telemetry.inputTokens,
9696
11953
  outputTokens: telemetry.outputTokens,
9697
11954
  cachedTokens: telemetry.cachedTokens,
@@ -9722,6 +11979,9 @@ function sanitizedModelUsageInput(normalized: ModelCallUsageNormalization): Mode
9722
11979
  },
9723
11980
  }
9724
11981
  : {}),
11982
+ ...(normalized.requestUsageEntries
11983
+ ? { requestUsageEntries: normalized.requestUsageEntries }
11984
+ : {}),
9725
11985
  };
9726
11986
  }
9727
11987