@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
@@ -0,0 +1,771 @@
1
+ import { environmentsEncryptionKeyBytes } from "@opengeni/config";
2
+ import type { MediaGenerationResult } from "@opengeni/contracts";
3
+ import { type CanonicalVideoGenerationRequest, videoGenerationRequestDigest } from "@opengeni/core";
4
+ import {
5
+ addSessionSystemUpdateWithSourceMutation,
6
+ decryptEnvironmentValue,
7
+ encryptEnvironmentValue,
8
+ getVideoGenerationOperation,
9
+ markVideoGenerationProviderStarted,
10
+ markVideoGenerationReferenceCleaned,
11
+ markVideoGenerationRetaining,
12
+ markVideoGenerationSubmissionIntent,
13
+ markVideoGenerationSubmissionUncertain,
14
+ materializeXaiCredentialForRun,
15
+ refreshXaiSubscriptionCredentialSerialized,
16
+ rotateVideoGenerationCredential,
17
+ markVideoGenerationTerminalUpdate,
18
+ markVideoGenerationTerminalUpdateInTransaction,
19
+ mediaGenerationResultForStoredOperation,
20
+ rescheduleVideoGenerationOperation,
21
+ settleVideoGenerationFailure,
22
+ settleVideoGenerationReady,
23
+ type XaiCredentialForRun,
24
+ type VideoGenerationOperationWithReferences,
25
+ } from "@opengeni/db";
26
+ import { publishDurableSessionEvents } from "@opengeni/events";
27
+ import type { ObjectStorage } from "@opengeni/storage";
28
+ import {
29
+ buildGatewayVideoStartBody,
30
+ GatewayVideoApiError,
31
+ getGatewayVideoGenerationStatus,
32
+ startGatewayVideoGenerationWithBody,
33
+ type GatewayVideoReferenceGrant,
34
+ } from "./gateway-video-generation";
35
+ import {
36
+ downloadGeneratedVideoToVerifiedTemp,
37
+ retainVerifiedGeneratedVideo,
38
+ } from "./video-output-retention";
39
+ import {
40
+ decryptVideoGenerationCredential,
41
+ encryptVideoGenerationXaiCredential,
42
+ type VideoGenerationProviderCredential,
43
+ } from "./video-generation-credential";
44
+ import {
45
+ refreshXaiToken,
46
+ XaiSubscriptionError,
47
+ xaiAccessTokenExpiry,
48
+ type XaiSubscriptionRequestContext,
49
+ } from "@opengeni/xai-subscription";
50
+ import {
51
+ buildXaiVideoStartBody,
52
+ getXaiVideoGenerationStatus,
53
+ startXaiVideoGeneration,
54
+ } from "./xai-video-generation";
55
+ import type {
56
+ TurnActivityServices,
57
+ VideoGenerationReconcileResult,
58
+ VideoGenerationTerminalStatus,
59
+ } from "./types";
60
+
61
+ const RECONCILE_ERROR_MAX = 1_000;
62
+
63
+ export function createVideoGenerationActivities(services: () => Promise<TurnActivityServices>) {
64
+ return {
65
+ reconcileVideoGenerationOperation: async (input: {
66
+ accountId: string;
67
+ workspaceId: string;
68
+ operationId: string;
69
+ }): Promise<VideoGenerationReconcileResult> => {
70
+ const service = await services();
71
+ if (!service.objectStorage) {
72
+ throw new Error("Video generation reconciliation requires object storage");
73
+ }
74
+ return await reconcileVideoGenerationOperation(service, input);
75
+ },
76
+ };
77
+ }
78
+
79
+ export async function reconcileVideoGenerationOperation(
80
+ service: TurnActivityServices,
81
+ input: { accountId: string; workspaceId: string; operationId: string },
82
+ ): Promise<VideoGenerationReconcileResult> {
83
+ const storage = requireStorage(service.objectStorage);
84
+ let operation = await requireOperation(service, input);
85
+ if (isTerminal(operation.status)) {
86
+ await deliverTerminalResult(service, operation);
87
+ return { action: "terminal", status: operation.status };
88
+ }
89
+ const key = environmentsEncryptionKeyBytes(service.settings);
90
+ if (!key) throw new Error("Video generation recovery encryption key is unavailable");
91
+ if (!operation.credentialEncrypted) {
92
+ throw new Error("Video provider credential lease was erased too early");
93
+ }
94
+ const providerCredential = decryptVideoGenerationCredential(key, operation.credentialEncrypted);
95
+ const usesSuperGrok = operation.fundingSource === "supergrok_subscription";
96
+ if (usesSuperGrok !== (providerCredential.kind === "xai-subscription")) {
97
+ throw new Error("Video provider credential does not match its funding source");
98
+ }
99
+ const apiKey = providerCredential.kind === "api-key" ? providerCredential.apiKey : null;
100
+ const xaiAuth =
101
+ providerCredential.kind === "xai-subscription"
102
+ ? await durableXaiVideoAuth(service, operation, key, providerCredential)
103
+ : null;
104
+ let preparedSubmissionThisRun = false;
105
+
106
+ if (operation.status === "accepted") {
107
+ const canonical = decryptCanonicalRequest(operation, key);
108
+ const grants: GatewayVideoReferenceGrant[] = [];
109
+ const expirations: Date[] = [];
110
+ for (const reference of operation.references) {
111
+ if (!reference.stagingObjectKey) {
112
+ throw new Error("Video generation reference staging object is missing");
113
+ }
114
+ const signed = await storage.createGetUrl({
115
+ key: reference.stagingObjectKey,
116
+ expiresInSeconds: service.settings.videoGenerationReferenceUrlTtlSeconds,
117
+ });
118
+ grants.push({
119
+ role: parseReferenceRole(reference.role),
120
+ url: signed.url,
121
+ mediaType: reference.contentType,
122
+ });
123
+ expirations.push(signed.expiresAt);
124
+ }
125
+ const providerRequestExpiresAt = expirations.length
126
+ ? new Date(Math.min(...expirations.map((value) => value.getTime())))
127
+ : operation.recoveryDeadlineAt;
128
+ const body = usesSuperGrok
129
+ ? buildXaiVideoStartBody(canonical, grants)
130
+ : buildGatewayVideoStartBody(canonical, grants);
131
+ operation = retainReferences(
132
+ operation,
133
+ await markVideoGenerationSubmissionIntent(service.db, {
134
+ accountId: input.accountId,
135
+ workspaceId: input.workspaceId,
136
+ operationId: operation.id,
137
+ requestDigest: operation.requestDigest,
138
+ encryptedProviderRequest: encryptEnvironmentValue(key, JSON.stringify(body)),
139
+ providerRequestExpiresAt,
140
+ nextReconcileAt: new Date(),
141
+ }),
142
+ );
143
+ preparedSubmissionThisRun = true;
144
+ }
145
+
146
+ if (operation.status === "submission_uncertain") {
147
+ if (usesSuperGrok && !preparedSubmissionThisRun) {
148
+ operation = retainReferences(
149
+ operation,
150
+ await settleVideoGenerationFailure(service.db, {
151
+ accountId: input.accountId,
152
+ workspaceId: input.workspaceId,
153
+ operationId: operation.id,
154
+ requestDigest: operation.requestDigest,
155
+ status: "outcome_unknown",
156
+ publicReason:
157
+ "The video provider did not confirm whether generation started. It was not replayed because xAI provides no start idempotency key.",
158
+ }),
159
+ );
160
+ await deliverTerminalResult(service, operation);
161
+ return { action: "terminal", status: terminalStatus(operation.status) };
162
+ }
163
+ const deadline = Math.min(
164
+ operation.recoveryDeadlineAt.getTime(),
165
+ operation.providerRequestExpiresAt?.getTime() ?? 0,
166
+ );
167
+ if (Date.now() >= deadline) {
168
+ operation = retainReferences(
169
+ operation,
170
+ await settleVideoGenerationFailure(service.db, {
171
+ accountId: input.accountId,
172
+ workspaceId: input.workspaceId,
173
+ operationId: operation.id,
174
+ requestDigest: operation.requestDigest,
175
+ status: "outcome_unknown",
176
+ publicReason:
177
+ "The video provider did not confirm whether generation started. It was not retried with a new request.",
178
+ }),
179
+ );
180
+ await deliverTerminalResult(service, operation);
181
+ return { action: "terminal", status: terminalStatus(operation.status) };
182
+ }
183
+ const body = decryptProviderBody(operation, key);
184
+ try {
185
+ const started = usesSuperGrok
186
+ ? await startXaiVideoGeneration({
187
+ body,
188
+ sessionId: operation.sessionId,
189
+ auth: requireXaiAuth(xaiAuth),
190
+ })
191
+ : await startGatewayVideoGenerationWithBody({
192
+ apiKey: requireApiKey(apiKey),
193
+ modelId: operation.modelId,
194
+ body,
195
+ idempotencyKey: operation.providerIdempotencyKey,
196
+ });
197
+ operation = retainReferences(
198
+ operation,
199
+ await markVideoGenerationProviderStarted(service.db, {
200
+ accountId: input.accountId,
201
+ workspaceId: input.workspaceId,
202
+ operationId: operation.id,
203
+ requestDigest: operation.requestDigest,
204
+ providerJobId: started.providerJobId,
205
+ nextReconcileAt: nextPoll(service),
206
+ }),
207
+ );
208
+ return waitResult(service);
209
+ } catch (error) {
210
+ if (isPermanentProviderFailure(error)) {
211
+ operation = retainReferences(
212
+ operation,
213
+ await settleVideoGenerationFailure(service.db, {
214
+ accountId: input.accountId,
215
+ workspaceId: input.workspaceId,
216
+ operationId: operation.id,
217
+ requestDigest: operation.requestDigest,
218
+ status: "provider_failed",
219
+ publicReason: "The video provider rejected the generation request.",
220
+ privateError: privateError(error),
221
+ }),
222
+ );
223
+ await deliverTerminalResult(service, operation);
224
+ return { action: "terminal", status: terminalStatus(operation.status) };
225
+ }
226
+ await markVideoGenerationSubmissionUncertain(service.db, {
227
+ accountId: input.accountId,
228
+ workspaceId: input.workspaceId,
229
+ operationId: operation.id,
230
+ requestDigest: operation.requestDigest,
231
+ nextReconcileAt: nextPoll(service),
232
+ error: privateError(error),
233
+ });
234
+ return waitResult(service);
235
+ }
236
+ }
237
+
238
+ if (operation.status !== "provider_started" && operation.status !== "retaining") {
239
+ throw new Error(`Video operation cannot be reconciled from ${operation.status}`);
240
+ }
241
+ if (!operation.providerJobId) throw new Error("Video provider job identity is missing");
242
+ let status;
243
+ try {
244
+ status = usesSuperGrok
245
+ ? await getXaiVideoGenerationStatus({
246
+ providerJobId: operation.providerJobId,
247
+ sessionId: operation.sessionId,
248
+ auth: requireXaiAuth(xaiAuth),
249
+ })
250
+ : await getGatewayVideoGenerationStatus({
251
+ apiKey: requireApiKey(apiKey),
252
+ modelId: operation.modelId,
253
+ providerJobId: operation.providerJobId,
254
+ });
255
+ } catch (error) {
256
+ if (Date.now() < operation.recoveryDeadlineAt.getTime() && !isPermanentProviderFailure(error)) {
257
+ await rescheduleVideoGenerationOperation(service.db, {
258
+ accountId: input.accountId,
259
+ workspaceId: input.workspaceId,
260
+ operationId: operation.id,
261
+ requestDigest: operation.requestDigest,
262
+ nextReconcileAt: nextPoll(service),
263
+ error: privateError(error),
264
+ });
265
+ return waitResult(service);
266
+ }
267
+ const failureStatus = operation.status === "retaining" ? "retention_failed" : "outcome_unknown";
268
+ operation = retainReferences(
269
+ operation,
270
+ await settleVideoGenerationFailure(service.db, {
271
+ accountId: input.accountId,
272
+ workspaceId: input.workspaceId,
273
+ operationId: operation.id,
274
+ requestDigest: operation.requestDigest,
275
+ status: failureStatus,
276
+ publicReason:
277
+ failureStatus === "retention_failed"
278
+ ? "The generated video could not be retained."
279
+ : "The video provider stopped reporting the accepted generation.",
280
+ privateError: privateError(error),
281
+ }),
282
+ );
283
+ await deliverTerminalResult(service, operation);
284
+ return { action: "terminal", status: terminalStatus(operation.status) };
285
+ }
286
+ if (status.status === "pending") {
287
+ await rescheduleVideoGenerationOperation(service.db, {
288
+ accountId: input.accountId,
289
+ workspaceId: input.workspaceId,
290
+ operationId: operation.id,
291
+ requestDigest: operation.requestDigest,
292
+ nextReconcileAt: nextPoll(service),
293
+ });
294
+ return waitResult(service);
295
+ }
296
+ if (status.status === "error") {
297
+ const failureStatus = operation.status === "retaining" ? "retention_failed" : "provider_failed";
298
+ operation = retainReferences(
299
+ operation,
300
+ await settleVideoGenerationFailure(service.db, {
301
+ accountId: input.accountId,
302
+ workspaceId: input.workspaceId,
303
+ operationId: operation.id,
304
+ requestDigest: operation.requestDigest,
305
+ status: failureStatus,
306
+ publicReason: status.publicReason,
307
+ }),
308
+ );
309
+ await deliverTerminalResult(service, operation);
310
+ return { action: "terminal", status: terminalStatus(operation.status) };
311
+ }
312
+
313
+ if (operation.status === "provider_started") {
314
+ operation = retainReferences(
315
+ operation,
316
+ await markVideoGenerationRetaining(service.db, {
317
+ accountId: input.accountId,
318
+ workspaceId: input.workspaceId,
319
+ operationId: operation.id,
320
+ requestDigest: operation.requestDigest,
321
+ }),
322
+ );
323
+ }
324
+ try {
325
+ const canonical = decryptCanonicalRequest(operation, key);
326
+ const downloaded = await downloadGeneratedVideoToVerifiedTemp({
327
+ url: status.outputUrl,
328
+ mediaType: status.mediaType,
329
+ settings: service.settings,
330
+ expectedDurationSeconds: canonical.durationSeconds,
331
+ });
332
+ try {
333
+ const retained = await retainVerifiedGeneratedVideo({
334
+ storage,
335
+ temp: downloaded.temp,
336
+ facts: downloaded.facts,
337
+ });
338
+ const settled = await settleVideoGenerationReady(service.db, {
339
+ accountId: input.accountId,
340
+ workspaceId: input.workspaceId,
341
+ operationId: operation.id,
342
+ requestDigest: operation.requestDigest,
343
+ fileId: operation.expectedFileId,
344
+ bucket: retained.bucket,
345
+ objectKey: retained.objectKey,
346
+ sizeBytes: retained.sizeBytes,
347
+ sha256: retained.sha256,
348
+ facts: retained.facts,
349
+ });
350
+ operation = { ...settled.operation, references: operation.references };
351
+ } finally {
352
+ await downloaded.temp.cleanup();
353
+ }
354
+ } catch (error) {
355
+ if (Date.now() < operation.recoveryDeadlineAt.getTime()) {
356
+ await rescheduleVideoGenerationOperation(service.db, {
357
+ accountId: input.accountId,
358
+ workspaceId: input.workspaceId,
359
+ operationId: operation.id,
360
+ requestDigest: operation.requestDigest,
361
+ nextReconcileAt: nextPoll(service),
362
+ error: privateError(error),
363
+ });
364
+ return waitResult(service);
365
+ }
366
+ operation = retainReferences(
367
+ operation,
368
+ await settleVideoGenerationFailure(service.db, {
369
+ accountId: input.accountId,
370
+ workspaceId: input.workspaceId,
371
+ operationId: operation.id,
372
+ requestDigest: operation.requestDigest,
373
+ status: "retention_failed",
374
+ publicReason: "The generated video could not be retained.",
375
+ privateError: privateError(error),
376
+ }),
377
+ );
378
+ }
379
+ await deliverTerminalResult(service, operation);
380
+ return { action: "terminal", status: terminalStatus(operation.status) };
381
+ }
382
+
383
+ async function deliverTerminalResult(
384
+ service: TurnActivityServices,
385
+ operation: VideoGenerationOperationWithReferences,
386
+ ): Promise<void> {
387
+ if (
388
+ operation.terminalUpdateState === "delivered" ||
389
+ operation.terminalUpdateState === "suppressed"
390
+ ) {
391
+ await cleanupTerminalReferences(service, operation);
392
+ return;
393
+ }
394
+ if (!operation.sessionId) {
395
+ await markVideoGenerationTerminalUpdate(service.db, {
396
+ accountId: operation.accountId,
397
+ workspaceId: operation.workspaceId,
398
+ operationId: operation.id,
399
+ state: "suppressed",
400
+ });
401
+ await cleanupTerminalReferences(service, operation);
402
+ return;
403
+ }
404
+ const payload = await terminalPayload(service, operation);
405
+ const result = await addSessionSystemUpdateWithSourceMutation(
406
+ service.db,
407
+ {
408
+ accountId: operation.accountId,
409
+ workspaceId: operation.workspaceId,
410
+ sessionId: operation.sessionId,
411
+ kind: "media_generation_result",
412
+ classification: payload.status === "ready" ? "success" : "failure",
413
+ sourceId: operation.id,
414
+ dedupeKey: `video-generation-terminal:${operation.id}`,
415
+ summary:
416
+ payload.status === "ready"
417
+ ? "The requested video is ready and available in the workspace."
418
+ : payload.boundedPublicReason,
419
+ payload,
420
+ lineage: { operationId: operation.id },
421
+ },
422
+ async (tx, _wakeEventId, updateId) => {
423
+ const marked = await markVideoGenerationTerminalUpdateInTransaction(tx, {
424
+ workspaceId: operation.workspaceId,
425
+ operationId: operation.id,
426
+ state: updateId ? "leased" : "suppressed",
427
+ ...(updateId ? { updateId } : {}),
428
+ });
429
+ if (!marked && operation.terminalUpdateState !== "leased") {
430
+ throw new Error("Video terminal result lost its disposition fence");
431
+ }
432
+ },
433
+ );
434
+ if (result.reason === "session_cancelled") {
435
+ await cleanupTerminalReferences(service, operation);
436
+ return;
437
+ }
438
+ if (result.added && result.events.length > 0) {
439
+ await publishDurableSessionEvents(
440
+ service.bus,
441
+ operation.workspaceId,
442
+ operation.sessionId,
443
+ result.events,
444
+ );
445
+ }
446
+ if (result.shouldWake && service.wakeSessionWorkflow) {
447
+ if (result.workflowWakeRevision === null) {
448
+ throw new Error("Video terminal result wake has no revision");
449
+ }
450
+ await service.wakeSessionWorkflow({
451
+ accountId: operation.accountId,
452
+ workspaceId: operation.workspaceId,
453
+ sessionId: operation.sessionId,
454
+ workflowId: result.temporalWorkflowId ?? `session-${operation.sessionId}`,
455
+ wakeRevision: result.workflowWakeRevision,
456
+ });
457
+ }
458
+ await cleanupTerminalReferences(service, operation);
459
+ }
460
+
461
+ async function cleanupTerminalReferences(
462
+ service: TurnActivityServices,
463
+ operation: VideoGenerationOperationWithReferences,
464
+ ): Promise<void> {
465
+ const storage = requireStorage(service.objectStorage);
466
+ for (const reference of operation.references) {
467
+ if (!reference.stagingObjectKey || reference.cleanedAt) continue;
468
+ // `outcome_unknown` is only terminal after the frozen provider grant and
469
+ // recovery deadline have elapsed. Other terminal outcomes have observed a
470
+ // provider terminal state, so their immutable staging inputs are no longer
471
+ // needed. Delete first, then CAS the row; a crash repeats an idempotent delete.
472
+ if (
473
+ operation.status === "outcome_unknown" &&
474
+ Date.now() <
475
+ Math.max(
476
+ operation.recoveryDeadlineAt.getTime(),
477
+ reference.grantExpiresAt?.getTime() ?? 0,
478
+ reference.cleanupAfter?.getTime() ?? 0,
479
+ )
480
+ ) {
481
+ continue;
482
+ }
483
+ const stagingObjectKey = reference.stagingObjectKey;
484
+ await storage.deleteObject(stagingObjectKey);
485
+ const marked = await markVideoGenerationReferenceCleaned(service.db, {
486
+ accountId: operation.accountId,
487
+ workspaceId: operation.workspaceId,
488
+ operationId: operation.id,
489
+ ordinal: reference.ordinal,
490
+ stagingObjectKey,
491
+ });
492
+ if (!marked) throw new Error("Video reference cleanup lost its durable identity");
493
+ }
494
+ }
495
+
496
+ async function terminalPayload(
497
+ service: TurnActivityServices,
498
+ operation: VideoGenerationOperationWithReferences,
499
+ ): Promise<MediaGenerationResult> {
500
+ return await mediaGenerationResultForStoredOperation(service.db, operation);
501
+ }
502
+
503
+ async function requireOperation(
504
+ service: TurnActivityServices,
505
+ input: { workspaceId: string; operationId: string },
506
+ ): Promise<VideoGenerationOperationWithReferences> {
507
+ const operation = await getVideoGenerationOperation(
508
+ service.db,
509
+ input.workspaceId,
510
+ input.operationId,
511
+ );
512
+ if (!operation) throw new Error("Video generation operation was not found");
513
+ return operation;
514
+ }
515
+
516
+ function decryptCanonicalRequest(
517
+ operation: VideoGenerationOperationWithReferences,
518
+ key: Uint8Array,
519
+ ): CanonicalVideoGenerationRequest {
520
+ if (!operation.requestEncrypted) throw new Error("Video generation request was erased too early");
521
+ const parsed = JSON.parse(decryptEnvironmentValue(key, operation.requestEncrypted)) as unknown;
522
+ const request = validateCanonicalRequest(parsed);
523
+ if (videoGenerationRequestDigest(request) !== operation.requestDigest) {
524
+ throw new Error("Video generation request digest is inconsistent");
525
+ }
526
+ return request;
527
+ }
528
+
529
+ function validateCanonicalRequest(value: unknown): CanonicalVideoGenerationRequest {
530
+ const row = record(value);
531
+ const references = Array.isArray(row?.references) ? row.references.map(record) : null;
532
+ if (
533
+ row?.schemaVersion !== 1 ||
534
+ typeof row.modelId !== "string" ||
535
+ typeof row.prompt !== "string" ||
536
+ ![
537
+ "text",
538
+ "first_frame",
539
+ "first_and_last_frames",
540
+ "image_reference",
541
+ "video_reference",
542
+ ].includes(String(row.sourceMode)) ||
543
+ !references ||
544
+ !Number.isInteger(row.durationSeconds) ||
545
+ !["16:9", "4:3", "1:1", "3:4", "9:16", "21:9", "adaptive"].includes(String(row.aspectRatio)) ||
546
+ !["480p", "720p"].includes(String(row.resolution)) ||
547
+ typeof row.generateAudio !== "boolean" ||
548
+ references.some(
549
+ (reference) =>
550
+ !reference ||
551
+ !["first_frame", "last_frame", "image_reference", "video_reference"].includes(
552
+ String(reference.role),
553
+ ) ||
554
+ typeof reference.contentSha256 !== "string" ||
555
+ !/^[0-9a-f]{64}$/u.test(reference.contentSha256) ||
556
+ typeof reference.contentType !== "string" ||
557
+ !Number.isSafeInteger(reference.byteSize),
558
+ )
559
+ ) {
560
+ throw new Error("Encrypted video generation request is malformed");
561
+ }
562
+ return value as CanonicalVideoGenerationRequest;
563
+ }
564
+
565
+ function decryptProviderBody(
566
+ operation: VideoGenerationOperationWithReferences,
567
+ key: Uint8Array,
568
+ ): Record<string, unknown> {
569
+ if (!operation.providerRequestEncrypted) throw new Error("Frozen provider request is missing");
570
+ const body = record(JSON.parse(decryptEnvironmentValue(key, operation.providerRequestEncrypted)));
571
+ if (!body || Buffer.byteLength(JSON.stringify(body)) > 256 * 1024) {
572
+ throw new Error("Frozen provider request is malformed");
573
+ }
574
+ return body;
575
+ }
576
+
577
+ function parseReferenceRole(value: string): GatewayVideoReferenceGrant["role"] {
578
+ if (
579
+ value !== "first_frame" &&
580
+ value !== "last_frame" &&
581
+ value !== "image_reference" &&
582
+ value !== "video_reference"
583
+ ) {
584
+ throw new Error("Video reference role is invalid");
585
+ }
586
+ return value;
587
+ }
588
+
589
+ function requireStorage(storage: ObjectStorage | null): ObjectStorage {
590
+ if (!storage) throw new Error("Video generation requires object storage");
591
+ return storage;
592
+ }
593
+
594
+ function nextPoll(service: TurnActivityServices): Date {
595
+ return new Date(Date.now() + service.settings.videoGenerationPollIntervalMs);
596
+ }
597
+
598
+ function waitResult(service: TurnActivityServices): VideoGenerationReconcileResult {
599
+ return {
600
+ action: "waiting",
601
+ delayMs: service.settings.videoGenerationPollIntervalMs,
602
+ };
603
+ }
604
+
605
+ function requireApiKey(value: string | null): string {
606
+ if (!value) throw new Error("Video provider API key is unavailable");
607
+ return value;
608
+ }
609
+
610
+ function requireXaiAuth(
611
+ value: Pick<XaiSubscriptionRequestContext, "getToken" | "refresh"> | null,
612
+ ): Pick<XaiSubscriptionRequestContext, "getToken" | "refresh"> {
613
+ if (!value) throw new Error("SuperGrok video authorization is unavailable");
614
+ return value;
615
+ }
616
+
617
+ /**
618
+ * A paid video job outlives the connected-account row. Keep its frozen OAuth
619
+ * lease usable after disconnect, and durably replace rotated refresh tokens
620
+ * before exposing them to another provider call. The shared account row is a
621
+ * best-effort mirror only; it is never the operation's recovery authority.
622
+ */
623
+ async function durableXaiVideoAuth(
624
+ service: TurnActivityServices,
625
+ operation: VideoGenerationOperationWithReferences,
626
+ key: Uint8Array,
627
+ credential: Extract<VideoGenerationProviderCredential, { kind: "xai-subscription" }>,
628
+ ): Promise<Pick<XaiSubscriptionRequestContext, "getToken" | "refresh">> {
629
+ let current = credential;
630
+ const frozenEncrypted = operation.credentialEncrypted;
631
+ if (!frozenEncrypted) throw new Error("SuperGrok video credential lease is unavailable");
632
+ let currentEncrypted: string = frozenEncrypted;
633
+
634
+ let connectedAccount: XaiCredentialForRun | null = null;
635
+ try {
636
+ connectedAccount = await materializeXaiCredentialForRun(service.db, {
637
+ workspaceId: operation.workspaceId,
638
+ subjectId: current.subjectId,
639
+ credentialId: current.credentialId,
640
+ authoritySnapshot: current.authoritySnapshot,
641
+ encryptionKey: key,
642
+ });
643
+ } catch {
644
+ // Disconnect/revocation must not cancel a previously accepted paid job.
645
+ }
646
+
647
+ return {
648
+ getToken: async () => ({
649
+ accessToken: current.accessToken,
650
+ userId: current.userId,
651
+ }),
652
+ refresh: async () => {
653
+ let next: typeof current | null = null;
654
+ if (connectedAccount) {
655
+ try {
656
+ const result = await refreshXaiSubscriptionCredentialSerialized(service.db, {
657
+ accountId: operation.accountId,
658
+ workspaceId: operation.workspaceId,
659
+ subjectId: current.subjectId,
660
+ credentialId: current.credentialId,
661
+ authoritySnapshot: current.authoritySnapshot,
662
+ encryptionKey: key,
663
+ observedAccessToken: current.accessToken,
664
+ observedRefreshToken: current.refreshToken,
665
+ refresh: async (shared) => {
666
+ const refreshToken = shared.secret.refreshToken;
667
+ if (!refreshToken) {
668
+ throw new Error("The connected SuperGrok account cannot be refreshed");
669
+ }
670
+ const tokens = await refreshXaiToken(refreshToken);
671
+ return {
672
+ secret: {
673
+ version: 1,
674
+ accessToken: tokens.accessToken,
675
+ refreshToken: tokens.refreshToken,
676
+ },
677
+ expiresAt:
678
+ xaiAccessTokenExpiry(tokens.accessToken) ??
679
+ new Date(Date.now() + tokens.expiresInSeconds * 1_000),
680
+ };
681
+ },
682
+ });
683
+ connectedAccount = result.credential;
684
+ if (
685
+ connectedAccount.secret.accessToken &&
686
+ connectedAccount.secret.refreshToken &&
687
+ connectedAccount.providerAccountId
688
+ ) {
689
+ next = Object.freeze({
690
+ ...current,
691
+ accessToken: connectedAccount.secret.accessToken,
692
+ refreshToken: connectedAccount.secret.refreshToken,
693
+ userId: connectedAccount.providerAccountId,
694
+ });
695
+ }
696
+ } catch {
697
+ // Disconnect/revocation must not cancel a previously accepted paid job.
698
+ connectedAccount = null;
699
+ }
700
+ }
701
+ if (!next) {
702
+ const tokens = await refreshXaiToken(current.refreshToken);
703
+ next = Object.freeze({
704
+ ...current,
705
+ accessToken: tokens.accessToken,
706
+ refreshToken: tokens.refreshToken,
707
+ });
708
+ }
709
+ const nextEncrypted = encryptVideoGenerationXaiCredential(key, next);
710
+ await rotateVideoGenerationCredential(service.db, {
711
+ accountId: operation.accountId,
712
+ workspaceId: operation.workspaceId,
713
+ operationId: operation.id,
714
+ requestDigest: operation.requestDigest,
715
+ expectedCredentialEncrypted: currentEncrypted,
716
+ credentialEncrypted: nextEncrypted,
717
+ });
718
+ current = next;
719
+ currentEncrypted = nextEncrypted;
720
+ return { accessToken: current.accessToken, userId: current.userId };
721
+ },
722
+ };
723
+ }
724
+
725
+ function isPermanentProviderFailure(error: unknown): boolean {
726
+ if (error instanceof GatewayVideoApiError) return !error.retryable;
727
+ if (!(error instanceof XaiSubscriptionError)) return false;
728
+ if (error.kind === "relogin_required") return true;
729
+ if (error.kind !== "provider_rejected") return false;
730
+ return (
731
+ typeof error.status === "number" &&
732
+ error.status >= 400 &&
733
+ error.status < 500 &&
734
+ error.status !== 408 &&
735
+ error.status !== 429
736
+ );
737
+ }
738
+
739
+ function privateError(error: unknown): string {
740
+ return (error instanceof Error ? `${error.name}: ${error.message}` : String(error))
741
+ .replace(/https?:\/\/\S+/gu, "[url omitted]")
742
+ .slice(0, RECONCILE_ERROR_MAX);
743
+ }
744
+
745
+ function record(value: unknown): Record<string, unknown> | null {
746
+ return value && typeof value === "object" && !Array.isArray(value)
747
+ ? (value as Record<string, unknown>)
748
+ : null;
749
+ }
750
+
751
+ function isTerminal(status: string): status is VideoGenerationTerminalStatus {
752
+ return [
753
+ "completed",
754
+ "provider_failed",
755
+ "cancelled_before_submit",
756
+ "outcome_unknown",
757
+ "retention_failed",
758
+ ].includes(status);
759
+ }
760
+
761
+ function terminalStatus(status: string): VideoGenerationTerminalStatus {
762
+ if (!isTerminal(status)) throw new Error(`Video generation status ${status} is not terminal`);
763
+ return status;
764
+ }
765
+
766
+ function retainReferences(
767
+ current: VideoGenerationOperationWithReferences,
768
+ next: Omit<VideoGenerationOperationWithReferences, "references">,
769
+ ): VideoGenerationOperationWithReferences {
770
+ return { ...next, references: current.references };
771
+ }