@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
@@ -1,9 +1,11 @@
1
1
  import {
2
2
  fetchCodexUsageForAccount,
3
3
  getCodexCapacityWaitForSession,
4
+ getXaiCapacityWaitForSession,
4
5
  listCodexAccountStatuses,
5
6
  listPendingCodexCapacityWakeTargets,
6
7
  reconcileCodexCapacityWait as reconcileCodexCapacityWaitDb,
8
+ reconcileXaiCapacityWait as reconcileXaiCapacityWaitDb,
7
9
  type CodexCapacityWakeTarget,
8
10
  type CodexCapacitySelectionContext,
9
11
  } from "@opengeni/db";
@@ -16,7 +18,7 @@ import {
16
18
  selectCodexCredentialLeaseForTurn,
17
19
  } from "./codex-rotation";
18
20
  import type {
19
- ActivityServices,
21
+ ControlActivityServices,
20
22
  GetCodexCapacityWaitInput,
21
23
  ReconcileCodexCapacityWaitInput,
22
24
  ReconcileCodexCapacityWaitResult,
@@ -24,10 +26,10 @@ import type {
24
26
 
25
27
  type CodexCapacitySignalServices = {
26
28
  signalCodexCapacityWorkflow?:
27
- | NonNullable<ActivityServices["signalCodexCapacityWorkflow"]>
29
+ | NonNullable<ControlActivityServices["signalCodexCapacityWorkflow"]>
28
30
  | null
29
31
  | undefined;
30
- wakeSessionWorkflow: ActivityServices["wakeSessionWorkflow"];
32
+ wakeSessionWorkflow: ControlActivityServices["wakeSessionWorkflow"];
31
33
  };
32
34
 
33
35
  /**
@@ -74,7 +76,7 @@ export async function signalCodexCapacityWakeTargets(
74
76
 
75
77
  /** Repair a commit/signal crash edge by redelivering every pending revision. */
76
78
  export async function signalPendingCodexCapacityWakeTargets(
77
- services: CodexCapacitySignalServices & { db: ActivityServices["db"] },
79
+ services: CodexCapacitySignalServices & { db: ControlActivityServices["db"] },
78
80
  workspaceId: string,
79
81
  ): Promise<void> {
80
82
  const targets = await listPendingCodexCapacityWakeTargets(services.db, workspaceId).catch(
@@ -130,7 +132,7 @@ export function codexCapacityDecision(
130
132
  }
131
133
 
132
134
  async function refreshCapacityMetadata(
133
- services: ActivityServices,
135
+ services: ControlActivityServices,
134
136
  workspaceId: string,
135
137
  ): Promise<void> {
136
138
  const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
@@ -151,12 +153,21 @@ async function refreshCapacityMetadata(
151
153
  );
152
154
  }
153
155
 
154
- export function createCodexCapacityActivities(services: () => Promise<ActivityServices>) {
156
+ export function createCodexCapacityActivities(services: () => Promise<ControlActivityServices>) {
155
157
  async function getCodexCapacityWait(input: GetCodexCapacityWaitInput) {
156
158
  const { db } = await services();
157
- const waiter = await getCodexCapacityWaitForSession(db, input.workspaceId, input.sessionId);
159
+ const codexWaiter = await getCodexCapacityWaitForSession(
160
+ db,
161
+ input.workspaceId,
162
+ input.sessionId,
163
+ );
164
+ const xaiWaiter = codexWaiter
165
+ ? null
166
+ : await getXaiCapacityWaitForSession(db, input.workspaceId, input.sessionId);
167
+ const waiter = codexWaiter ?? xaiWaiter;
158
168
  return waiter
159
169
  ? {
170
+ ...(xaiWaiter ? { provider: "xai" as const } : {}),
160
171
  waiterId: waiter.id,
161
172
  generation: waiter.generation,
162
173
  // A capacity mutation may have committed while its Temporal signal
@@ -176,6 +187,42 @@ export function createCodexCapacityActivities(services: () => Promise<ActivitySe
176
187
  input: ReconcileCodexCapacityWaitInput,
177
188
  ): Promise<ReconcileCodexCapacityWaitResult> {
178
189
  const resolved = await services();
190
+ if (input.provider === "xai") {
191
+ const current = await getXaiCapacityWaitForSession(
192
+ resolved.db,
193
+ input.workspaceId,
194
+ input.sessionId,
195
+ );
196
+ if (!current || current.id !== input.waiterId || current.generation !== input.generation) {
197
+ return { action: "stale" };
198
+ }
199
+ const result = await reconcileXaiCapacityWaitDb(resolved.db, {
200
+ accountId: input.accountId,
201
+ workspaceId: input.workspaceId,
202
+ sessionId: input.sessionId,
203
+ waiterId: input.waiterId,
204
+ generation: input.generation,
205
+ });
206
+ if (result.events.length > 0) {
207
+ try {
208
+ await resolved.bus.publish(input.workspaceId, input.sessionId, result.events);
209
+ } catch {
210
+ // Postgres is authoritative; SSE replay/gap fill repairs missed fanout.
211
+ }
212
+ }
213
+ if (result.action === "resumed") return { action: "resumed" };
214
+ if (result.action === "waiting") {
215
+ return {
216
+ action: "waiting",
217
+ provider: "xai",
218
+ waiterId: result.waiter.id,
219
+ generation: result.waiter.generation,
220
+ nextCheckAt: result.waiter.nextCheckAt.toISOString(),
221
+ wakeRevision: result.waiter.wakeRevision,
222
+ };
223
+ }
224
+ return { action: result.action };
225
+ }
179
226
  const current = await getCodexCapacityWaitForSession(
180
227
  resolved.db,
181
228
  input.workspaceId,
@@ -0,0 +1,56 @@
1
+ import {
2
+ CODEX_PROVIDER_ID,
3
+ generateCodexSubscriptionImage,
4
+ type CodexRequestContext,
5
+ } from "@opengeni/codex";
6
+ import type { Database } from "@opengeni/db";
7
+ import type { ObjectStorage } from "@opengeni/storage";
8
+ import type { GeneratedImageReceipt } from "./generated-images";
9
+ import {
10
+ executeImageGenerationOperation,
11
+ imageProviderBindingHash,
12
+ } from "./image-generation-operation";
13
+ import type { ResolvedImageGenerationReference } from "./image-generation-references";
14
+
15
+ const CODEX_IMAGE_MODEL = "gpt-image-2";
16
+
17
+ /** Execute the same standalone subscription image path used by Codex clients. */
18
+ export async function executeCodexImageGeneration(input: {
19
+ db: Database;
20
+ objectStorage: ObjectStorage | null;
21
+ accountId: string;
22
+ workspaceId: string;
23
+ sessionId: string;
24
+ turnId: string;
25
+ attemptId: string;
26
+ toolCallId: string;
27
+ prompt: string;
28
+ references?: readonly ResolvedImageGenerationReference[];
29
+ credentialId: string;
30
+ codexContext: Pick<CodexRequestContext, "clientVersion" | "getToken" | "refresh">;
31
+ abortSignal?: AbortSignal;
32
+ }): Promise<GeneratedImageReceipt> {
33
+ const providerBindingHash = imageProviderBindingHash(CODEX_PROVIDER_ID, input.credentialId);
34
+ return await executeImageGenerationOperation({
35
+ ...input,
36
+ providerId: CODEX_PROVIDER_ID,
37
+ providerBindingHash,
38
+ modelId: CODEX_IMAGE_MODEL,
39
+ ...(input.references ? { referenceDigests: input.references } : {}),
40
+ generate: async () => {
41
+ const generated = await generateCodexSubscriptionImage({
42
+ prompt: input.prompt,
43
+ ...(input.references ? { references: input.references } : {}),
44
+ turnId: input.turnId,
45
+ context: input.codexContext,
46
+ ...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
47
+ });
48
+ return {
49
+ toolCallId: input.toolCallId,
50
+ providerItemId: null,
51
+ bytes: generated.bytes,
52
+ declaredMediaType: generated.declaredMediaType,
53
+ };
54
+ },
55
+ });
56
+ }
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  applyContextCompaction,
3
- getActiveSessionHistoryItems,
3
+ getActiveSessionHistoryItemsPaged,
4
4
  recordSkippedContextCompaction,
5
5
  recordStartedContextCompaction,
6
6
  type Database,
@@ -16,6 +16,7 @@ import {
16
16
  estimateTokens,
17
17
  latestCompactionReplacementFingerprint,
18
18
  prepareCompactionPromptInput,
19
+ projectRemoteCompactionOverflowRetryInput,
19
20
  sanitizeHistoryItemsForModel,
20
21
  summarizeForCompaction,
21
22
  type CompactionItem,
@@ -112,7 +113,9 @@ export async function maybeCompactContext(
112
113
  }
113
114
  const useRemoteV2 = options.codexCompactionMode === "remote_v2";
114
115
 
115
- const active = await getActiveSessionHistoryItems(db, scope.workspaceId, scope.sessionId);
116
+ // Preserve the complete ordered transcript while bounding each Postgres
117
+ // driver result frame beside the decoded history already held by this turn.
118
+ const active = await getActiveSessionHistoryItemsPaged(db, scope.workspaceId, scope.sessionId);
116
119
  if (active.length === 0) {
117
120
  let requestConsumed = false;
118
121
  if (options.clearRequestedCompaction) {
@@ -325,7 +328,19 @@ async function compactContextRemoteV2(
325
328
  // Codex remote_v2: on a valid compaction item, install and recompute usage.
326
329
  // No local "must shrink / must differ" gate — that is portable-only.
327
330
  // Fail closed on provider/extract failure — no portable fallback.
328
- const compactionItem = await options.requestRemoteCompactionV2(settings, items);
331
+ let providerCalls = 1;
332
+ let rewrittenToolOutputs = 0;
333
+ let compactionItem: CompactionItem;
334
+ try {
335
+ compactionItem = await options.requestRemoteCompactionV2(settings, items);
336
+ } catch (error) {
337
+ if (!isExactContextLengthExceeded(error)) throw error;
338
+ const retry = projectRemoteCompactionOverflowRetryInput(items);
339
+ if (retry.rewrittenToolOutputs === 0) throw error;
340
+ providerCalls = 2;
341
+ rewrittenToolOutputs = retry.rewrittenToolOutputs;
342
+ compactionItem = await options.requestRemoteCompactionV2(settings, retry.input);
343
+ }
329
344
  const replacementHistory = buildRemoteV2ReplacementHistory(canonicalItems, compactionItem);
330
345
  const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
331
346
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
@@ -351,6 +366,8 @@ async function compactContextRemoteV2(
351
366
  implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION,
352
367
  estimatedTokensBefore,
353
368
  estimatedTokensAfter,
369
+ compactionInputToolOutputsRewritten: rewrittenToolOutputs,
370
+ compactionInputProviderCalls: providerCalls,
354
371
  },
355
372
  });
356
373
  if (!applied.applied) {
@@ -523,3 +540,21 @@ export function isContextWindowExceeded(error: unknown, seen = new WeakSet<objec
523
540
  isContextWindowExceeded(record.diagnostics, seen)
524
541
  );
525
542
  }
543
+
544
+ export function isExactContextLengthExceeded(
545
+ error: unknown,
546
+ seen = new WeakSet<object>(),
547
+ ): boolean {
548
+ if (!error || typeof error !== "object") return false;
549
+ if (seen.has(error)) return false;
550
+ seen.add(error);
551
+ const record = error as Record<string, unknown>;
552
+ if (record.code === "context_length_exceeded") {
553
+ return true;
554
+ }
555
+ return (
556
+ isExactContextLengthExceeded(record.cause, seen) ||
557
+ isExactContextLengthExceeded(record.error, seen) ||
558
+ isExactContextLengthExceeded(record.diagnostics, seen)
559
+ );
560
+ }
@@ -1,4 +1,4 @@
1
- import { getDocument, indexDocumentNow } from "@opengeni/documents";
1
+ import type { DocumentServices } from "@opengeni/documents";
2
2
  import { configuredStaticUsageLimits } from "@opengeni/config";
3
3
  import {
4
4
  getBillingBalance,
@@ -8,15 +8,25 @@ import {
8
8
  sumUsageQuantity,
9
9
  withWorkspaceUsageLock,
10
10
  } from "@opengeni/db";
11
- import type { ActivityServices, IndexDocumentInput } from "./types";
11
+ import type { ControlActivityServices, IndexDocumentInput } from "./types";
12
12
 
13
- export function createDocumentActivities(services: () => Promise<ActivityServices>) {
13
+ export function createDocumentActivities(
14
+ services: () => Promise<ControlActivityServices>,
15
+ resolveDocumentServices?: () => Promise<DocumentServices>,
16
+ ) {
14
17
  return {
15
18
  indexDocument: async (input: IndexDocumentInput) => {
16
- const { settings, db, objectStorage, documentServices } = await services();
19
+ const { settings, db, objectStorage } = await services();
17
20
  if (!objectStorage) {
18
21
  throw new Error("object storage is not configured");
19
22
  }
23
+ if (!resolveDocumentServices) {
24
+ throw new Error("document services are not configured");
25
+ }
26
+ const [{ getDocument, indexDocumentNow }, documentServices] = await Promise.all([
27
+ import("@opengeni/documents"),
28
+ resolveDocumentServices(),
29
+ ]);
20
30
  const context = await rlsContextForWorkspace(db, input.workspaceId);
21
31
  if (context.accountId !== input.accountId) {
22
32
  throw new Error("document account/workspace authority mismatch");
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  applyGitAuthPointerEnvironment,
3
- firstPartyMcpWorkspaceUrl,
3
+ codemodeWorkspaceUrl,
4
+ resolveFirstPartyDelegationSecret,
4
5
  stableSandboxEnvironmentForRun,
5
6
  type Settings,
6
7
  } from "@opengeni/config";
@@ -67,35 +68,40 @@ export type GitHubTokenMintAuthorization = (selection: {
67
68
  repositoryIds: number[];
68
69
  }) => Promise<void>;
69
70
 
70
- export const TOOLSPACE_TOKEN_TTL_SECONDS = 60 * 60;
71
+ export const CODEMODE_TOKEN_TTL_SECONDS = 60 * 60;
71
72
 
72
- export type MintedSandboxToolspaceToken = {
73
+ export type MintedSandboxCodemodeToken = {
73
74
  token: string;
74
75
  expiresAt: Date;
75
76
  };
76
77
 
77
- export type SandboxToolspaceAuthority = {
78
+ export type SandboxCodemodeAuthority = {
78
79
  sessionId: string;
80
+ turnId: string;
81
+ attemptId: string;
82
+ executionGeneration: number;
79
83
  };
80
84
 
81
- export async function mintSandboxToolspaceToken(
85
+ export async function mintSandboxCodemodeToken(
82
86
  settings: Settings,
83
87
  scope: ConnectionScope,
84
- authority: SandboxToolspaceAuthority,
88
+ authority: SandboxCodemodeAuthority,
85
89
  nowMs = Date.now(),
86
- ): Promise<MintedSandboxToolspaceToken | undefined> {
87
- if (!settings.toolspaceEnabled || !settings.delegationSecret) {
88
- return undefined;
89
- }
90
- const expiresAtSeconds = Math.floor(nowMs / 1000) + TOOLSPACE_TOKEN_TTL_SECONDS;
91
- const token = await signDelegatedAccessToken(settings.delegationSecret, {
90
+ ): Promise<MintedSandboxCodemodeToken | undefined> {
91
+ const delegationSecret = resolveFirstPartyDelegationSecret(settings);
92
+ if (!delegationSecret) return undefined;
93
+ const expiresAtSeconds = Math.floor(nowMs / 1000) + CODEMODE_TOKEN_TTL_SECONDS;
94
+ const token = await signDelegatedAccessToken(delegationSecret, {
92
95
  accountId: scope.accountId,
93
96
  workspaceId: scope.workspaceId,
94
- subjectId: `sandbox:${authority.sessionId}`,
95
- subjectLabel: "sandbox toolspace",
96
- permissions: ["toolspace:call"],
97
+ subjectId: `sandbox:${authority.attemptId}`,
98
+ subjectLabel: "sandbox Codemode",
99
+ permissions: ["codemode:call"],
97
100
  sessionId: authority.sessionId,
98
- principalKind: "service",
101
+ turnId: authority.turnId,
102
+ attemptId: authority.attemptId,
103
+ executionGeneration: authority.executionGeneration,
104
+ principalKind: "agent_attempt",
99
105
  exp: expiresAtSeconds,
100
106
  });
101
107
  return { token, expiresAt: new Date(expiresAtSeconds * 1000) };
@@ -203,9 +209,15 @@ export async function sandboxEnvironmentForRun(
203
209
  // value later. `= {}` default so the non-optional reads below are safe.
204
210
  options: RunGitCredentialOptions & {
205
211
  skipGitHubToken?: boolean;
206
- skipToolspace?: boolean;
212
+ /**
213
+ * Codemode credential delivery for this attempt. Managed sandboxes receive
214
+ * stable URL/file pointers in their manifest and the bearer through the
215
+ * lifecycle file writer. Connected Machines receive no manifest pointers;
216
+ * the worker injects the minted bearer only into exact exec requests.
217
+ */
218
+ codemodeDelivery?: "managed_file" | "transient_exec" | "none";
207
219
  deferGitHubToken?: boolean;
208
- toolspaceAuthority?: SandboxToolspaceAuthority;
220
+ codemodeAuthority?: SandboxCodemodeAuthority;
209
221
  } = {},
210
222
  ): Promise<{
211
223
  environment: Record<string, string>;
@@ -213,8 +225,8 @@ export async function sandboxEnvironmentForRun(
213
225
  gitTokens?: GitTokenSeeds;
214
226
  gitTokenExpiresAt?: GitTokenExpiries;
215
227
  gitCredentialBindings?: GitCredentialBindingSeed[];
216
- toolspaceToken?: string;
217
- toolspaceTokenExpiresAt?: Date;
228
+ codemodeToken?: string;
229
+ codemodeTokenExpiresAt?: Date;
218
230
  }> {
219
231
  // Precedence: deployment allowlist < git identity < workspace environment
220
232
  // < backend-aware HOME (the STABLE base, shared with the API-direct attach
@@ -235,25 +247,22 @@ export async function sandboxEnvironmentForRun(
235
247
  // proactively renews every selected provider behind these stable pointers.
236
248
  const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
237
249
  const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
238
- // Connected Machines own their environment and credentials. They do not
239
- // receive OpenGeni Toolspace pointers, package hints, or delegated tokens.
240
- // Scrub the stable pointers as well as skipping the mint because a session
241
- // whose home backend is managed may be actively routed to selfhosted.
242
- if (options.skipToolspace) {
243
- delete environment.OPENGENI_TOOLSPACE_URL;
244
- delete environment.OPENGENI_TOOLSPACE_TOKEN_FILE;
250
+ const codemodeDelivery = options.codemodeDelivery ?? "managed_file";
251
+ // Connected Machines keep their machine-wide environment untouched. Their
252
+ // URL, package hint, and direct bearer are projected only onto an exact child
253
+ // exec request by SelfhostedSession, never into the manifest or filesystem.
254
+ if (codemodeDelivery !== "managed_file") {
255
+ delete environment.OPENGENI_CODEMODE_URL;
256
+ delete environment.OPENGENI_CODEMODE_TOKEN_FILE;
245
257
  delete environment.OPENGENI_OGTOOL_PACKAGE_SPEC;
246
258
  }
247
- const toolspaceScope = options.scope;
248
- const toolspaceToken =
249
- !options.skipToolspace && toolspaceScope && options.toolspaceAuthority
250
- ? await mintSandboxToolspaceToken(settings, toolspaceScope, options.toolspaceAuthority)
259
+ const codemodeScope = options.scope;
260
+ const codemodeToken =
261
+ codemodeDelivery !== "none" && codemodeScope && options.codemodeAuthority
262
+ ? await mintSandboxCodemodeToken(settings, codemodeScope, options.codemodeAuthority)
251
263
  : undefined;
252
- if (toolspaceToken && toolspaceScope) {
253
- environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(
254
- settings,
255
- toolspaceScope.workspaceId,
256
- );
264
+ if (codemodeDelivery === "managed_file" && codemodeToken && codemodeScope) {
265
+ environment.OPENGENI_CODEMODE_URL ??= codemodeWorkspaceUrl(settings, codemodeScope.workspaceId);
257
266
  }
258
267
  const selections = gitCredentialSelections(resources);
259
268
  // NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
@@ -268,10 +277,10 @@ export async function sandboxEnvironmentForRun(
268
277
  if (selections.length === 0 || options.skipGitHubToken) {
269
278
  return {
270
279
  environment,
271
- ...(toolspaceToken
280
+ ...(codemodeToken
272
281
  ? {
273
- toolspaceToken: toolspaceToken.token,
274
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
282
+ codemodeToken: codemodeToken.token,
283
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
275
284
  }
276
285
  : {}),
277
286
  };
@@ -283,10 +292,10 @@ export async function sandboxEnvironmentForRun(
283
292
  );
284
293
  return {
285
294
  environment,
286
- ...(toolspaceToken
295
+ ...(codemodeToken
287
296
  ? {
288
- toolspaceToken: toolspaceToken.token,
289
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
297
+ codemodeToken: codemodeToken.token,
298
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
290
299
  }
291
300
  : {}),
292
301
  };
@@ -313,10 +322,10 @@ export async function sandboxEnvironmentForRun(
313
322
  ...(Object.keys(minted.gitTokens).length > 0 ? { gitTokens: minted.gitTokens } : {}),
314
323
  ...(Object.keys(minted.expiresAt).length > 0 ? { gitTokenExpiresAt: minted.expiresAt } : {}),
315
324
  ...(minted.bindings.length > 0 ? { gitCredentialBindings: minted.bindings } : {}),
316
- ...(toolspaceToken
325
+ ...(codemodeToken
317
326
  ? {
318
- toolspaceToken: toolspaceToken.token,
319
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
327
+ codemodeToken: codemodeToken.token,
328
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
320
329
  }
321
330
  : {}),
322
331
  };
@@ -14,7 +14,7 @@ export type ExpiringMaterialRenewalController = {
14
14
 
15
15
  export type ExpiringMaterialRenewalPublicErrorClass =
16
16
  | "RunCredentialRenewalOperationError"
17
- | "ToolspaceTokenRenewalOperationError";
17
+ | "CodemodeTokenRenewalOperationError";
18
18
 
19
19
  export type ExpiringMaterialRenewalOptions<
20
20
  T,
@@ -3,10 +3,12 @@ import {
3
3
  claimExpiredFileUploadCleanup,
4
4
  completeDueTranscriptionRecordingObjectCleanup,
5
5
  completeExpiredFileUploadCleanup,
6
+ claimVideoGenerationOperations,
6
7
  purgeExpiredTranscriptionRecordings,
7
8
  } from "@opengeni/db";
8
9
  import type { ObjectStorage } from "@opengeni/storage";
9
- import type { ActivityServices } from "./types";
10
+ import type { ControlActivityServices } from "./types";
11
+ import { randomUUID } from "node:crypto";
10
12
 
11
13
  export const FILE_UPLOAD_CLEANUP_GRACE_MS = 60 * 60 * 1_000;
12
14
  export const FILE_UPLOAD_CLEANUP_CLAIM_TIMEOUT_MS = 10 * 60 * 1_000;
@@ -72,7 +74,7 @@ export type FileUploadReaperActivityOptions = {
72
74
  * settled terminally. One provider failure never aborts the rest of the batch.
73
75
  */
74
76
  export function createFileUploadReaperActivities(
75
- services: () => Promise<ActivityServices>,
77
+ services: () => Promise<ControlActivityServices>,
76
78
  options: FileUploadReaperActivityOptions = {},
77
79
  ) {
78
80
  const graceMs = options.graceMs ?? FILE_UPLOAD_CLEANUP_GRACE_MS;
@@ -176,5 +178,34 @@ export function createFileUploadReaperActivities(
176
178
  return { claimed, deleted, failed };
177
179
  }
178
180
 
179
- return { reapExpiredFileUploads };
181
+ async function recoverVideoGenerationWorkflows(): Promise<{
182
+ claimed: number;
183
+ started: number;
184
+ failed: number;
185
+ }> {
186
+ const { db, startVideoGenerationWorkflow, observability } = await services();
187
+ if (!startVideoGenerationWorkflow) return { claimed: 0, started: 0, failed: 0 };
188
+ const claims = await claimVideoGenerationOperations(db, {
189
+ owner: `video-workflow-recovery:${randomUUID()}`,
190
+ leaseSeconds: 120,
191
+ limit: 100,
192
+ });
193
+ let started = 0;
194
+ let failed = 0;
195
+ for (const claim of claims) {
196
+ try {
197
+ await startVideoGenerationWorkflow(claim);
198
+ started += 1;
199
+ } catch (error) {
200
+ failed += 1;
201
+ observability.warn("Video generation workflow recovery start failed", {
202
+ operationId: claim.operationId,
203
+ errorClass: error instanceof Error ? error.name : "UnknownError",
204
+ });
205
+ }
206
+ }
207
+ return { claimed: claims.length, started, failed };
208
+ }
209
+
210
+ return { reapExpiredFileUploads, recoverVideoGenerationWorkflows };
180
211
  }
@@ -0,0 +1,146 @@
1
+ import type { Database } from "@opengeni/db";
2
+ import { readJsonBase64Field, readResponseTextBounded, type FetchLike } from "@opengeni/network";
3
+ import type { ObjectStorage } from "@opengeni/storage";
4
+ import { type GeneratedImageOutput, type GeneratedImageReceipt } from "./generated-images";
5
+ import {
6
+ executeImageGenerationOperation,
7
+ imageProviderBindingHash,
8
+ } from "./image-generation-operation";
9
+ import type { ResolvedImageGenerationReference } from "./image-generation-references";
10
+
11
+ const GATEWAY_IMAGE_PROVIDER_ID = "vercel-ai-gateway";
12
+ const GATEWAY_IMAGE_URL = "https://ai-gateway.vercel.sh/v3/ai/image-model";
13
+ const GATEWAY_PROTOCOL_VERSION = "0.0.1";
14
+ const GATEWAY_IMAGE_SPECIFICATION_VERSION = "3";
15
+ const GATEWAY_IMAGE_RESPONSE_MAX_BYTES = 90 * 1024 * 1024;
16
+ const GATEWAY_IMAGE_MAX_BYTES = 64 * 1024 * 1024;
17
+ const GATEWAY_IMAGE_ERROR_MAX_BYTES = 64 * 1024;
18
+
19
+ export class GatewayImageApiError extends Error {
20
+ constructor(
21
+ readonly status: number,
22
+ message: string,
23
+ ) {
24
+ super(message);
25
+ this.name = "GatewayImageApiError";
26
+ }
27
+ }
28
+
29
+ export async function executeGatewayImageGeneration(input: {
30
+ db: Database;
31
+ objectStorage: ObjectStorage | null;
32
+ accountId: string;
33
+ workspaceId: string;
34
+ sessionId: string;
35
+ turnId: string;
36
+ attemptId: string;
37
+ apiKey: string;
38
+ modelId: string;
39
+ prompt: string;
40
+ references?: readonly ResolvedImageGenerationReference[];
41
+ toolCallId: string;
42
+ abortSignal?: AbortSignal;
43
+ }): Promise<GeneratedImageReceipt> {
44
+ const providerBindingHash = imageProviderBindingHash(GATEWAY_IMAGE_PROVIDER_ID, input.apiKey);
45
+ return await executeImageGenerationOperation({
46
+ ...input,
47
+ providerId: GATEWAY_IMAGE_PROVIDER_ID,
48
+ providerBindingHash,
49
+ ...(input.references ? { referenceDigests: input.references } : {}),
50
+ generate: async () =>
51
+ await generateGatewayImage({
52
+ apiKey: input.apiKey,
53
+ modelId: input.modelId,
54
+ prompt: input.prompt,
55
+ ...(input.references ? { references: input.references } : {}),
56
+ toolCallId: input.toolCallId,
57
+ ...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
58
+ }),
59
+ });
60
+ }
61
+
62
+ /**
63
+ * One explicit Vercel AI Gateway image-protocol adapter. The caller owns durable
64
+ * operation admission; this function never retries an outcome-ambiguous paid
65
+ * request. The response is decoded directly from its JSON stream so neither
66
+ * the complete envelope nor its multi-megabyte base64 string enters memory.
67
+ */
68
+ export async function generateGatewayImage(input: {
69
+ apiKey: string;
70
+ modelId: string;
71
+ prompt: string;
72
+ references?: readonly ResolvedImageGenerationReference[];
73
+ toolCallId: string;
74
+ abortSignal?: AbortSignal;
75
+ fetch?: FetchLike;
76
+ }): Promise<GeneratedImageOutput> {
77
+ if (!input.apiKey.trim()) throw new Error("AI Gateway image generation credential is empty");
78
+ if (!input.modelId.trim()) throw new Error("AI Gateway image generation model is empty");
79
+ const response = await (input.fetch ?? globalThis.fetch)(GATEWAY_IMAGE_URL, {
80
+ method: "POST",
81
+ redirect: "error",
82
+ headers: {
83
+ Authorization: `Bearer ${input.apiKey}`,
84
+ accept: "application/json",
85
+ "content-type": "application/json",
86
+ "ai-gateway-auth-method": "api-key",
87
+ "ai-gateway-protocol-version": GATEWAY_PROTOCOL_VERSION,
88
+ "ai-image-model-specification-version": GATEWAY_IMAGE_SPECIFICATION_VERSION,
89
+ "ai-model-id": input.modelId,
90
+ "user-agent": "opengeni/image-generation",
91
+ },
92
+ body: JSON.stringify({
93
+ prompt: input.prompt,
94
+ n: 1,
95
+ ...(input.references?.length
96
+ ? {
97
+ files: input.references.map((reference) => ({
98
+ type: "file",
99
+ mediaType: reference.mediaType,
100
+ data: Buffer.from(reference.bytes).toString("base64"),
101
+ })),
102
+ }
103
+ : {}),
104
+ }),
105
+ ...(input.abortSignal ? { signal: input.abortSignal } : {}),
106
+ });
107
+ if (!response.ok) {
108
+ const detail = await readResponseTextBounded(
109
+ response,
110
+ GATEWAY_IMAGE_ERROR_MAX_BYTES,
111
+ "AI Gateway image error",
112
+ input.abortSignal ? { signal: input.abortSignal } : {},
113
+ ).catch(() => "");
114
+ throw new GatewayImageApiError(
115
+ response.status,
116
+ detail
117
+ ? `AI Gateway image generation failed (${response.status}): ${boundedErrorMessage(detail)}`
118
+ : `AI Gateway image generation failed (${response.status})`,
119
+ );
120
+ }
121
+ const bytes = await readJsonBase64Field(response, {
122
+ fieldName: "images",
123
+ shape: "first_array_string",
124
+ maxResponseBytes: GATEWAY_IMAGE_RESPONSE_MAX_BYTES,
125
+ maxDecodedBytes: GATEWAY_IMAGE_MAX_BYTES,
126
+ label: "AI Gateway image generation",
127
+ ...(input.abortSignal ? { signal: input.abortSignal } : {}),
128
+ });
129
+ return {
130
+ toolCallId: input.toolCallId,
131
+ providerItemId: null,
132
+ bytes,
133
+ };
134
+ }
135
+
136
+ function boundedErrorMessage(body: string): string {
137
+ let message = body;
138
+ try {
139
+ const value = JSON.parse(body) as { error?: { message?: unknown }; message?: unknown };
140
+ const candidate = value.error?.message ?? value.message;
141
+ if (typeof candidate === "string") message = candidate;
142
+ } catch {
143
+ // Preserve a bounded non-JSON provider diagnostic.
144
+ }
145
+ return message.replace(/\s+/g, " ").trim().slice(0, 1_000);
146
+ }