@opengeni/worker-bundle 0.16.29 → 0.17.2

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 (168) hide show
  1. package/dist/activities/agent-turn.d.ts +85 -19
  2. package/dist/activities/api-integrations.d.ts +21 -0
  3. package/dist/activities/codemode-dispatcher.d.ts +35 -0
  4. package/dist/activities/codemode-token-renewal.d.ts +23 -0
  5. package/dist/activities/codex-capacity.d.ts +5 -5
  6. package/dist/activities/codex-image-generation.d.ts +21 -0
  7. package/dist/activities/context-compaction.d.ts +1 -0
  8. package/dist/activities/documents.d.ts +3 -2
  9. package/dist/activities/environment.d.ts +11 -8
  10. package/dist/activities/expiring-material-renewal.d.ts +1 -1
  11. package/dist/activities/file-upload-reaper.d.ts +7 -2
  12. package/dist/activities/gateway-image-generation.d.ts +39 -0
  13. package/dist/activities/gateway-video-generation.d.ts +53 -0
  14. package/dist/activities/generated-images.d.ts +112 -0
  15. package/dist/activities/git-credential-renewal.d.ts +1 -1
  16. package/dist/activities/goals.d.ts +2 -2
  17. package/dist/activities/google-drive-changes.d.ts +99 -0
  18. package/dist/activities/image-generation-operation.d.ts +74 -0
  19. package/dist/activities/image-generation-references.d.ts +25 -0
  20. package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
  21. package/dist/activities/knowledge-source-sync.d.ts +75 -0
  22. package/dist/activities/packs.d.ts +13 -26
  23. package/dist/activities/parent-wake.d.ts +1 -1
  24. package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
  25. package/dist/activities/retained-screenshots.d.ts +36 -0
  26. package/dist/activities/rig-verification.d.ts +82 -5
  27. package/dist/activities/run-credential-renewal.d.ts +1 -1
  28. package/dist/activities/run-input.d.ts +3 -1
  29. package/dist/activities/sandbox-images.d.ts +14 -0
  30. package/dist/activities/sandbox-lease.d.ts +47 -9
  31. package/dist/activities/scheduled-tasks.d.ts +2 -2
  32. package/dist/activities/session-state.d.ts +4 -4
  33. package/dist/activities/streaming.d.ts +1 -1
  34. package/dist/activities/types.d.ts +71 -5
  35. package/dist/activities/video-generation-admission.d.ts +38 -0
  36. package/dist/activities/video-generation-credential.d.ts +7 -0
  37. package/dist/activities/video-generation-reconciliation.d.ts +13 -0
  38. package/dist/activities/video-media-validation.d.ts +34 -0
  39. package/dist/activities/video-output-retention.d.ts +36 -0
  40. package/dist/activities/video-reference-staging.d.ts +49 -0
  41. package/dist/activities/workflow-wake.d.ts +2 -2
  42. package/dist/activities/workspace-capture.d.ts +10 -1
  43. package/dist/activities-control-44KBWLSM.js +6885 -0
  44. package/dist/activities-control-44KBWLSM.js.map +1 -0
  45. package/dist/activities-control.d.ts +289 -0
  46. package/dist/activities-turn-K4CEDEGJ.js +14675 -0
  47. package/dist/activities-turn-K4CEDEGJ.js.map +1 -0
  48. package/dist/activities-turn.d.ts +17 -0
  49. package/dist/activities.d.ts +130 -150
  50. package/dist/activity-services.d.ts +10 -0
  51. package/dist/artifact-materializer-entry.d.ts +6 -0
  52. package/dist/artifact-materializer-entry.js +2662 -0
  53. package/dist/artifact-materializer-entry.js.map +1 -0
  54. package/dist/artifact-outbox-entry.d.ts +2 -0
  55. package/dist/artifact-outbox-entry.js +681 -0
  56. package/dist/artifact-outbox-entry.js.map +1 -0
  57. package/dist/chunk-E3RI3I33.js +828 -0
  58. package/dist/chunk-E3RI3I33.js.map +1 -0
  59. package/dist/chunk-GRFQTTSQ.js +448 -0
  60. package/dist/chunk-GRFQTTSQ.js.map +1 -0
  61. package/dist/chunk-KKCRGAGZ.js +3091 -0
  62. package/dist/chunk-KKCRGAGZ.js.map +1 -0
  63. package/dist/concurrency.d.ts +108 -5
  64. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  65. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  66. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  67. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  68. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  69. package/dist/editable-artifact-materializer.d.ts +262 -0
  70. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  71. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  72. package/dist/host-export.d.ts +1 -1
  73. package/dist/index.d.ts +176 -10
  74. package/dist/index.js +662 -17374
  75. package/dist/index.js.map +1 -1
  76. package/dist/memory-pressure-guard.d.ts +57 -0
  77. package/dist/model-checkpoint-memory-collector.d.ts +22 -0
  78. package/dist/observability-metrics.d.ts +33 -0
  79. package/dist/sandbox-reaper-contract.d.ts +40 -0
  80. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  81. package/dist/sandbox-resume.d.ts +34 -7
  82. package/dist/sandbox-routing.d.ts +2 -0
  83. package/dist/worker-service-lifecycle.d.ts +6 -2
  84. package/dist/workflow-bundle.js +1043 -363
  85. package/dist/workflows/activities.d.ts +7 -1
  86. package/dist/workflows/document-index.d.ts +1 -1
  87. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  88. package/dist/workflows/rig-verification.d.ts +1 -1
  89. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  90. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  91. package/dist/workflows/session.d.ts +2 -2
  92. package/dist/workflows/video-generation.d.ts +10 -0
  93. package/dist/workflows/workflow-wake.d.ts +1 -1
  94. package/dist/workflows.d.ts +9 -7
  95. package/package.json +34 -15
  96. package/src/activities/agent-turn.ts +1470 -246
  97. package/src/activities/api-integrations.ts +138 -0
  98. package/src/activities/codemode-dispatcher.ts +335 -0
  99. package/src/activities/codemode-token-renewal.ts +64 -0
  100. package/src/activities/codex-capacity.ts +6 -6
  101. package/src/activities/codex-image-generation.ts +56 -0
  102. package/src/activities/context-compaction.ts +38 -3
  103. package/src/activities/documents.ts +14 -4
  104. package/src/activities/environment.ts +45 -42
  105. package/src/activities/expiring-material-renewal.ts +1 -1
  106. package/src/activities/file-upload-reaper.ts +34 -3
  107. package/src/activities/gateway-image-generation.ts +146 -0
  108. package/src/activities/gateway-video-generation.ts +298 -0
  109. package/src/activities/generated-images.ts +860 -0
  110. package/src/activities/goals.ts +6 -2
  111. package/src/activities/google-drive-changes.ts +454 -0
  112. package/src/activities/image-generation-operation.ts +333 -0
  113. package/src/activities/image-generation-references.ts +118 -0
  114. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  115. package/src/activities/knowledge-source-sync.ts +2840 -0
  116. package/src/activities/mcp-credentials.ts +1 -0
  117. package/src/activities/packs.ts +55 -55
  118. package/src/activities/retained-screenshot-reaper.ts +2 -2
  119. package/src/activities/retained-screenshots.ts +453 -28
  120. package/src/activities/rig-verification.ts +554 -43
  121. package/src/activities/run-input.ts +22 -6
  122. package/src/activities/sandbox-images.ts +167 -0
  123. package/src/activities/sandbox-lease.ts +1065 -483
  124. package/src/activities/scheduled-tasks.ts +77 -4
  125. package/src/activities/session-state.ts +4 -4
  126. package/src/activities/streaming.ts +42 -3
  127. package/src/activities/types.ts +87 -5
  128. package/src/activities/video-generation-admission.ts +270 -0
  129. package/src/activities/video-generation-credential.ts +34 -0
  130. package/src/activities/video-generation-reconciliation.ts +578 -0
  131. package/src/activities/video-media-validation.ts +326 -0
  132. package/src/activities/video-output-retention.ts +314 -0
  133. package/src/activities/video-reference-staging.ts +263 -0
  134. package/src/activities/workflow-wake.ts +2 -2
  135. package/src/activities/workspace-capture.ts +44 -10
  136. package/src/activities-control.ts +54 -0
  137. package/src/activities-turn.ts +40 -0
  138. package/src/activities.ts +48 -124
  139. package/src/activity-services.ts +62 -0
  140. package/src/artifact-materializer-entry.ts +35 -0
  141. package/src/artifact-outbox-entry.ts +29 -0
  142. package/src/concurrency.ts +484 -6
  143. package/src/editable-artifact-hint-broker.ts +235 -0
  144. package/src/editable-artifact-materializer-launcher.ts +515 -0
  145. package/src/editable-artifact-materializer-service.ts +787 -0
  146. package/src/editable-artifact-materializer-storage.ts +413 -0
  147. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  148. package/src/editable-artifact-materializer.ts +1289 -0
  149. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  150. package/src/editable-artifact-outbox-service.ts +438 -0
  151. package/src/index.ts +277 -72
  152. package/src/memory-pressure-guard.ts +489 -0
  153. package/src/model-checkpoint-memory-collector.ts +116 -0
  154. package/src/observability-metrics.ts +199 -4
  155. package/src/sandbox-reaper-contract.ts +73 -0
  156. package/src/sandbox-reaper-timeout.ts +79 -0
  157. package/src/sandbox-resume.ts +294 -153
  158. package/src/sandbox-routing.ts +19 -24
  159. package/src/worker-service-lifecycle.ts +80 -14
  160. package/src/workflows/activities.ts +30 -0
  161. package/src/workflows/file-upload-reaper.ts +3 -1
  162. package/src/workflows/knowledge-source-sync.ts +83 -0
  163. package/src/workflows/sandbox-reaper.ts +183 -21
  164. package/src/workflows/scheduled-tasks.ts +42 -6
  165. package/src/workflows/video-generation.ts +36 -0
  166. package/src/workflows.ts +17 -1
  167. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  168. package/src/activities/toolspace-token-renewal.ts +0 -64
@@ -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,9 @@ 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
+ skipCodemode?: boolean;
207
213
  deferGitHubToken?: boolean;
208
- toolspaceAuthority?: SandboxToolspaceAuthority;
214
+ codemodeAuthority?: SandboxCodemodeAuthority;
209
215
  } = {},
210
216
  ): Promise<{
211
217
  environment: Record<string, string>;
@@ -213,8 +219,8 @@ export async function sandboxEnvironmentForRun(
213
219
  gitTokens?: GitTokenSeeds;
214
220
  gitTokenExpiresAt?: GitTokenExpiries;
215
221
  gitCredentialBindings?: GitCredentialBindingSeed[];
216
- toolspaceToken?: string;
217
- toolspaceTokenExpiresAt?: Date;
222
+ codemodeToken?: string;
223
+ codemodeTokenExpiresAt?: Date;
218
224
  }> {
219
225
  // Precedence: deployment allowlist < git identity < workspace environment
220
226
  // < backend-aware HOME (the STABLE base, shared with the API-direct attach
@@ -236,24 +242,21 @@ export async function sandboxEnvironmentForRun(
236
242
  const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
237
243
  const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
238
244
  // Connected Machines own their environment and credentials. They do not
239
- // receive OpenGeni Toolspace pointers, package hints, or delegated tokens.
245
+ // receive OpenGeni Codemode pointers, package hints, or delegated tokens.
240
246
  // Scrub the stable pointers as well as skipping the mint because a session
241
247
  // 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;
248
+ if (options.skipCodemode) {
249
+ delete environment.OPENGENI_CODEMODE_URL;
250
+ delete environment.OPENGENI_CODEMODE_TOKEN_FILE;
245
251
  delete environment.OPENGENI_OGTOOL_PACKAGE_SPEC;
246
252
  }
247
- const toolspaceScope = options.scope;
248
- const toolspaceToken =
249
- !options.skipToolspace && toolspaceScope && options.toolspaceAuthority
250
- ? await mintSandboxToolspaceToken(settings, toolspaceScope, options.toolspaceAuthority)
253
+ const codemodeScope = options.scope;
254
+ const codemodeToken =
255
+ !options.skipCodemode && codemodeScope && options.codemodeAuthority
256
+ ? await mintSandboxCodemodeToken(settings, codemodeScope, options.codemodeAuthority)
251
257
  : undefined;
252
- if (toolspaceToken && toolspaceScope) {
253
- environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(
254
- settings,
255
- toolspaceScope.workspaceId,
256
- );
258
+ if (codemodeToken && codemodeScope) {
259
+ environment.OPENGENI_CODEMODE_URL ??= codemodeWorkspaceUrl(settings, codemodeScope.workspaceId);
257
260
  }
258
261
  const selections = gitCredentialSelections(resources);
259
262
  // NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
@@ -268,10 +271,10 @@ export async function sandboxEnvironmentForRun(
268
271
  if (selections.length === 0 || options.skipGitHubToken) {
269
272
  return {
270
273
  environment,
271
- ...(toolspaceToken
274
+ ...(codemodeToken
272
275
  ? {
273
- toolspaceToken: toolspaceToken.token,
274
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
276
+ codemodeToken: codemodeToken.token,
277
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
275
278
  }
276
279
  : {}),
277
280
  };
@@ -283,10 +286,10 @@ export async function sandboxEnvironmentForRun(
283
286
  );
284
287
  return {
285
288
  environment,
286
- ...(toolspaceToken
289
+ ...(codemodeToken
287
290
  ? {
288
- toolspaceToken: toolspaceToken.token,
289
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
291
+ codemodeToken: codemodeToken.token,
292
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
290
293
  }
291
294
  : {}),
292
295
  };
@@ -313,10 +316,10 @@ export async function sandboxEnvironmentForRun(
313
316
  ...(Object.keys(minted.gitTokens).length > 0 ? { gitTokens: minted.gitTokens } : {}),
314
317
  ...(Object.keys(minted.expiresAt).length > 0 ? { gitTokenExpiresAt: minted.expiresAt } : {}),
315
318
  ...(minted.bindings.length > 0 ? { gitCredentialBindings: minted.bindings } : {}),
316
- ...(toolspaceToken
319
+ ...(codemodeToken
317
320
  ? {
318
- toolspaceToken: toolspaceToken.token,
319
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
321
+ codemodeToken: codemodeToken.token,
322
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
320
323
  }
321
324
  : {}),
322
325
  };
@@ -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
+ }