@opengeni/worker-bundle 0.16.21 → 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 (180) hide show
  1. package/dist/activities/agent-turn.d.ts +145 -32
  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 +3 -0
  8. package/dist/activities/documents.d.ts +3 -2
  9. package/dist/activities/environment.d.ts +11 -7
  10. package/dist/activities/expiring-material-renewal.d.ts +5 -3
  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 +2 -2
  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 +32 -0
  25. package/dist/activities/retained-screenshots.d.ts +129 -0
  26. package/dist/activities/rig-verification.d.ts +82 -5
  27. package/dist/activities/run-credential-renewal.d.ts +2 -2
  28. package/dist/activities/run-input.d.ts +5 -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 +11 -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 -147
  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/{chunk-BJUW5LCM.js → chunk-RAMNYSAW.js} +15 -12
  64. package/dist/chunk-RAMNYSAW.js.map +1 -0
  65. package/dist/concurrency.d.ts +108 -5
  66. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  67. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  68. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  69. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  70. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  71. package/dist/editable-artifact-materializer.d.ts +262 -0
  72. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  73. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  74. package/dist/host-export.d.ts +1 -1
  75. package/dist/host-export.js +1 -1
  76. package/dist/index.d.ts +176 -10
  77. package/dist/index.js +675 -16460
  78. package/dist/index.js.map +1 -1
  79. package/dist/memory-pressure-guard.d.ts +57 -0
  80. package/dist/model-checkpoint-memory-collector.d.ts +22 -0
  81. package/dist/observability-metrics.d.ts +33 -0
  82. package/dist/sandbox-reaper-contract.d.ts +40 -0
  83. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  84. package/dist/sandbox-resume.d.ts +40 -7
  85. package/dist/sandbox-routing.d.ts +13 -6
  86. package/dist/worker-service-lifecycle.d.ts +6 -2
  87. package/dist/workflow-bundle.js +1044 -363
  88. package/dist/workflows/activities.d.ts +7 -1
  89. package/dist/workflows/document-index.d.ts +1 -1
  90. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  91. package/dist/workflows/rig-verification.d.ts +1 -1
  92. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  93. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  94. package/dist/workflows/session.d.ts +2 -2
  95. package/dist/workflows/video-generation.d.ts +10 -0
  96. package/dist/workflows/workflow-wake.d.ts +1 -1
  97. package/dist/workflows.d.ts +9 -7
  98. package/package.json +34 -15
  99. package/src/activities/agent-turn.ts +1905 -559
  100. package/src/activities/api-integrations.ts +138 -0
  101. package/src/activities/codemode-dispatcher.ts +335 -0
  102. package/src/activities/codemode-token-renewal.ts +64 -0
  103. package/src/activities/codex-capacity.ts +16 -7
  104. package/src/activities/codex-image-generation.ts +56 -0
  105. package/src/activities/codex-rotation.ts +19 -5
  106. package/src/activities/context-compaction.ts +47 -6
  107. package/src/activities/documents.ts +14 -4
  108. package/src/activities/environment.ts +52 -60
  109. package/src/activities/expiring-material-renewal.ts +16 -7
  110. package/src/activities/file-upload-reaper.ts +34 -3
  111. package/src/activities/gateway-image-generation.ts +146 -0
  112. package/src/activities/gateway-video-generation.ts +298 -0
  113. package/src/activities/generated-images.ts +860 -0
  114. package/src/activities/git-credential-renewal.ts +3 -3
  115. package/src/activities/goals.ts +6 -2
  116. package/src/activities/google-drive-changes.ts +454 -0
  117. package/src/activities/image-generation-operation.ts +333 -0
  118. package/src/activities/image-generation-references.ts +118 -0
  119. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  120. package/src/activities/knowledge-source-sync.ts +2840 -0
  121. package/src/activities/mcp-credentials.ts +1 -0
  122. package/src/activities/packs.ts +55 -55
  123. package/src/activities/recording.ts +4 -5
  124. package/src/activities/retained-screenshot-reaper.ts +202 -0
  125. package/src/activities/retained-screenshots.ts +1317 -0
  126. package/src/activities/rig-verification.ts +567 -77
  127. package/src/activities/run-credential-renewal.ts +9 -2
  128. package/src/activities/run-input.ts +40 -10
  129. package/src/activities/sandbox-images.ts +167 -0
  130. package/src/activities/sandbox-lease.ts +1065 -483
  131. package/src/activities/scheduled-tasks.ts +96 -7
  132. package/src/activities/session-state.ts +4 -4
  133. package/src/activities/streaming.ts +42 -3
  134. package/src/activities/types.ts +87 -5
  135. package/src/activities/video-generation-admission.ts +270 -0
  136. package/src/activities/video-generation-credential.ts +34 -0
  137. package/src/activities/video-generation-reconciliation.ts +578 -0
  138. package/src/activities/video-media-validation.ts +326 -0
  139. package/src/activities/video-output-retention.ts +314 -0
  140. package/src/activities/video-reference-staging.ts +263 -0
  141. package/src/activities/workflow-wake.ts +2 -2
  142. package/src/activities/workspace-capture.ts +77 -13
  143. package/src/activities-control.ts +54 -0
  144. package/src/activities-turn.ts +40 -0
  145. package/src/activities.ts +50 -118
  146. package/src/activity-services.ts +62 -0
  147. package/src/artifact-materializer-entry.ts +35 -0
  148. package/src/artifact-outbox-entry.ts +29 -0
  149. package/src/concurrency.ts +484 -6
  150. package/src/editable-artifact-hint-broker.ts +235 -0
  151. package/src/editable-artifact-materializer-launcher.ts +515 -0
  152. package/src/editable-artifact-materializer-service.ts +787 -0
  153. package/src/editable-artifact-materializer-storage.ts +413 -0
  154. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  155. package/src/editable-artifact-materializer.ts +1289 -0
  156. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  157. package/src/editable-artifact-outbox-service.ts +438 -0
  158. package/src/host-export-pump.ts +28 -11
  159. package/src/http.ts +3 -6
  160. package/src/index.ts +277 -72
  161. package/src/memory-pressure-guard.ts +489 -0
  162. package/src/model-checkpoint-memory-collector.ts +116 -0
  163. package/src/observability-metrics.ts +202 -7
  164. package/src/sandbox-reaper-contract.ts +73 -0
  165. package/src/sandbox-reaper-timeout.ts +79 -0
  166. package/src/sandbox-resume.ts +331 -157
  167. package/src/sandbox-routing.ts +49 -39
  168. package/src/worker-service-lifecycle.ts +82 -12
  169. package/src/workflows/activities.ts +30 -0
  170. package/src/workflows/file-upload-reaper.ts +6 -1
  171. package/src/workflows/knowledge-source-sync.ts +83 -0
  172. package/src/workflows/sandbox-reaper.ts +183 -21
  173. package/src/workflows/scheduled-tasks.ts +42 -6
  174. package/src/workflows/video-generation.ts +36 -0
  175. package/src/workflows.ts +17 -1
  176. package/dist/activities/redaction.d.ts +0 -1
  177. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  178. package/dist/chunk-BJUW5LCM.js.map +0 -1
  179. package/src/activities/redaction.ts +0 -9
  180. package/src/activities/toolspace-token-renewal.ts +0 -57
@@ -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,
@@ -97,6 +98,8 @@ export async function maybeCompactContext(
97
98
  * append again — the event is already durable.
98
99
  */
99
100
  publishLiveEvents?: (events: SessionEvent[]) => Promise<void>;
101
+ /** Materialize retained screenshot receipts only in the attempt-local model view. */
102
+ materializeHistory?: (items: CompactionItem[]) => Promise<CompactionItem[]>;
100
103
  /** Turn-scoped attachment/modality view; canonical persisted rows stay untouched. */
101
104
  projectModelInput?: (items: CompactionItem[]) => Promise<CompactionItem[]>;
102
105
  } = {},
@@ -110,7 +113,9 @@ export async function maybeCompactContext(
110
113
  }
111
114
  const useRemoteV2 = options.codexCompactionMode === "remote_v2";
112
115
 
113
- 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);
114
119
  if (active.length === 0) {
115
120
  let requestConsumed = false;
116
121
  if (options.clearRequestedCompaction) {
@@ -142,11 +147,15 @@ export async function maybeCompactContext(
142
147
  }
143
148
 
144
149
  const canonicalItems = projectRejectedProviderArtifacts(active) as CompactionItem[];
145
- const projectForWire = async (input: CompactionItem[]): Promise<CompactionItem[]> =>
146
- sanitizeHistoryItemsForModel(
147
- options.projectModelInput ? await options.projectModelInput(input) : input,
150
+ const projectForWire = async (input: CompactionItem[]): Promise<CompactionItem[]> => {
151
+ const materialized = options.materializeHistory
152
+ ? await options.materializeHistory(input)
153
+ : input;
154
+ return sanitizeHistoryItemsForModel(
155
+ options.projectModelInput ? await options.projectModelInput(materialized) : materialized,
148
156
  settings.modelToolOutputTruncationTokens,
149
157
  ) as CompactionItem[];
158
+ };
150
159
  const items = await projectForWire(canonicalItems);
151
160
  const decision = decideCompaction({
152
161
  items,
@@ -319,7 +328,19 @@ async function compactContextRemoteV2(
319
328
  // Codex remote_v2: on a valid compaction item, install and recompute usage.
320
329
  // No local "must shrink / must differ" gate — that is portable-only.
321
330
  // Fail closed on provider/extract failure — no portable fallback.
322
- 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
+ }
323
344
  const replacementHistory = buildRemoteV2ReplacementHistory(canonicalItems, compactionItem);
324
345
  const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
325
346
  const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
@@ -345,6 +366,8 @@ async function compactContextRemoteV2(
345
366
  implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION,
346
367
  estimatedTokensBefore,
347
368
  estimatedTokensAfter,
369
+ compactionInputToolOutputsRewritten: rewrittenToolOutputs,
370
+ compactionInputProviderCalls: providerCalls,
348
371
  },
349
372
  });
350
373
  if (!applied.applied) {
@@ -517,3 +540,21 @@ export function isContextWindowExceeded(error: unknown, seen = new WeakSet<objec
517
540
  isContextWindowExceeded(record.diagnostics, seen)
518
541
  );
519
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,12 +1,14 @@
1
1
  import {
2
2
  applyGitAuthPointerEnvironment,
3
- firstPartyMcpWorkspaceUrl,
3
+ codemodeWorkspaceUrl,
4
+ resolveFirstPartyDelegationSecret,
4
5
  stableSandboxEnvironmentForRun,
5
6
  type Settings,
6
7
  } from "@opengeni/config";
7
8
  import {
8
9
  gitCredentialBindingIdForRepository,
9
10
  gitCredentialProviderForRepository,
11
+ gitRemoteIdentity,
10
12
  signDelegatedAccessToken,
11
13
  type ConnectionCredentialsPort,
12
14
  type GitCredentialProvider,
@@ -66,35 +68,40 @@ export type GitHubTokenMintAuthorization = (selection: {
66
68
  repositoryIds: number[];
67
69
  }) => Promise<void>;
68
70
 
69
- export const TOOLSPACE_TOKEN_TTL_SECONDS = 60 * 60;
71
+ export const CODEMODE_TOKEN_TTL_SECONDS = 60 * 60;
70
72
 
71
- export type MintedSandboxToolspaceToken = {
73
+ export type MintedSandboxCodemodeToken = {
72
74
  token: string;
73
75
  expiresAt: Date;
74
76
  };
75
77
 
76
- export type SandboxToolspaceAuthority = {
78
+ export type SandboxCodemodeAuthority = {
77
79
  sessionId: string;
80
+ turnId: string;
81
+ attemptId: string;
82
+ executionGeneration: number;
78
83
  };
79
84
 
80
- export async function mintSandboxToolspaceToken(
85
+ export async function mintSandboxCodemodeToken(
81
86
  settings: Settings,
82
87
  scope: ConnectionScope,
83
- authority: SandboxToolspaceAuthority,
88
+ authority: SandboxCodemodeAuthority,
84
89
  nowMs = Date.now(),
85
- ): Promise<MintedSandboxToolspaceToken | undefined> {
86
- if (!settings.toolspaceEnabled || !settings.delegationSecret) {
87
- return undefined;
88
- }
89
- const expiresAtSeconds = Math.floor(nowMs / 1000) + TOOLSPACE_TOKEN_TTL_SECONDS;
90
- 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, {
91
95
  accountId: scope.accountId,
92
96
  workspaceId: scope.workspaceId,
93
- subjectId: `sandbox:${authority.sessionId}`,
94
- subjectLabel: "sandbox toolspace",
95
- permissions: ["toolspace:call"],
97
+ subjectId: `sandbox:${authority.attemptId}`,
98
+ subjectLabel: "sandbox Codemode",
99
+ permissions: ["codemode:call"],
96
100
  sessionId: authority.sessionId,
97
- principalKind: "service",
101
+ turnId: authority.turnId,
102
+ attemptId: authority.attemptId,
103
+ executionGeneration: authority.executionGeneration,
104
+ principalKind: "agent_attempt",
98
105
  exp: expiresAtSeconds,
99
106
  });
100
107
  return { token, expiresAt: new Date(expiresAtSeconds * 1000) };
@@ -202,8 +209,9 @@ export async function sandboxEnvironmentForRun(
202
209
  // value later. `= {}` default so the non-optional reads below are safe.
203
210
  options: RunGitCredentialOptions & {
204
211
  skipGitHubToken?: boolean;
212
+ skipCodemode?: boolean;
205
213
  deferGitHubToken?: boolean;
206
- toolspaceAuthority?: SandboxToolspaceAuthority;
214
+ codemodeAuthority?: SandboxCodemodeAuthority;
207
215
  } = {},
208
216
  ): Promise<{
209
217
  environment: Record<string, string>;
@@ -211,8 +219,8 @@ export async function sandboxEnvironmentForRun(
211
219
  gitTokens?: GitTokenSeeds;
212
220
  gitTokenExpiresAt?: GitTokenExpiries;
213
221
  gitCredentialBindings?: GitCredentialBindingSeed[];
214
- toolspaceToken?: string;
215
- toolspaceTokenExpiresAt?: Date;
222
+ codemodeToken?: string;
223
+ codemodeTokenExpiresAt?: Date;
216
224
  }> {
217
225
  // Precedence: deployment allowlist < git identity < workspace environment
218
226
  // < backend-aware HOME (the STABLE base, shared with the API-direct attach
@@ -233,25 +241,22 @@ export async function sandboxEnvironmentForRun(
233
241
  // proactively renews every selected provider behind these stable pointers.
234
242
  const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
235
243
  const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
236
- // TOOLSPACE (selfhosted parity): the toolspace token is minted for EVERY
237
- // backend, including a connected machine. Unlike platform git provider tokens
238
- // (inert on selfhosted → skipped above), the toolspace token is the machine's
239
- // only path to programmatic tool calling, and it grants no more than the
240
- // machine owner's own authority (toolspace:call, own-session-bound,
241
- // short-lived and renewable, per-active-turn budgeted, approval-tools
242
- // excluded). Delivery mirrors the docker path: the caller threads it
243
- // OFF-MANIFEST as the seed the runtime writes to
244
- // $OPENGENI_TOOLSPACE_TOKEN_FILE over the box's exec channel.
245
- const toolspaceScope = options.scope;
246
- const toolspaceToken =
247
- toolspaceScope && options.toolspaceAuthority
248
- ? await mintSandboxToolspaceToken(settings, toolspaceScope, options.toolspaceAuthority)
244
+ // Connected Machines own their environment and credentials. They do not
245
+ // receive OpenGeni Codemode pointers, package hints, or delegated tokens.
246
+ // Scrub the stable pointers as well as skipping the mint because a session
247
+ // whose home backend is managed may be actively routed to selfhosted.
248
+ if (options.skipCodemode) {
249
+ delete environment.OPENGENI_CODEMODE_URL;
250
+ delete environment.OPENGENI_CODEMODE_TOKEN_FILE;
251
+ delete environment.OPENGENI_OGTOOL_PACKAGE_SPEC;
252
+ }
253
+ const codemodeScope = options.scope;
254
+ const codemodeToken =
255
+ !options.skipCodemode && codemodeScope && options.codemodeAuthority
256
+ ? await mintSandboxCodemodeToken(settings, codemodeScope, options.codemodeAuthority)
249
257
  : undefined;
250
- if (toolspaceToken && toolspaceScope) {
251
- environment.OPENGENI_TOOLSPACE_URL ??= firstPartyMcpWorkspaceUrl(
252
- settings,
253
- toolspaceScope.workspaceId,
254
- );
258
+ if (codemodeToken && codemodeScope) {
259
+ environment.OPENGENI_CODEMODE_URL ??= codemodeWorkspaceUrl(settings, codemodeScope.workspaceId);
255
260
  }
256
261
  const selections = gitCredentialSelections(resources);
257
262
  // NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
@@ -266,10 +271,10 @@ export async function sandboxEnvironmentForRun(
266
271
  if (selections.length === 0 || options.skipGitHubToken) {
267
272
  return {
268
273
  environment,
269
- ...(toolspaceToken
274
+ ...(codemodeToken
270
275
  ? {
271
- toolspaceToken: toolspaceToken.token,
272
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
276
+ codemodeToken: codemodeToken.token,
277
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
273
278
  }
274
279
  : {}),
275
280
  };
@@ -281,10 +286,10 @@ export async function sandboxEnvironmentForRun(
281
286
  );
282
287
  return {
283
288
  environment,
284
- ...(toolspaceToken
289
+ ...(codemodeToken
285
290
  ? {
286
- toolspaceToken: toolspaceToken.token,
287
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
291
+ codemodeToken: codemodeToken.token,
292
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
288
293
  }
289
294
  : {}),
290
295
  };
@@ -311,10 +316,10 @@ export async function sandboxEnvironmentForRun(
311
316
  ...(Object.keys(minted.gitTokens).length > 0 ? { gitTokens: minted.gitTokens } : {}),
312
317
  ...(Object.keys(minted.expiresAt).length > 0 ? { gitTokenExpiresAt: minted.expiresAt } : {}),
313
318
  ...(minted.bindings.length > 0 ? { gitCredentialBindings: minted.bindings } : {}),
314
- ...(toolspaceToken
319
+ ...(codemodeToken
315
320
  ? {
316
- toolspaceToken: toolspaceToken.token,
317
- toolspaceTokenExpiresAt: toolspaceToken.expiresAt,
321
+ codemodeToken: codemodeToken.token,
322
+ codemodeTokenExpiresAt: codemodeToken.expiresAt,
318
323
  }
319
324
  : {}),
320
325
  };
@@ -777,7 +782,7 @@ function gitCredentialSelections(resources: ResourceRef[]): GitCredentialSelecti
777
782
  );
778
783
  }
779
784
  bindingProviders.set(credentialBindingId, provider);
780
- const normalizedRemote = normalizedGitRemote(resource.uri);
785
+ const normalizedRemote = gitRemoteIdentity(resource.uri, provider);
781
786
  const claimedBinding = remoteBindings.get(normalizedRemote);
782
787
  if (claimedBinding && claimedBinding !== bindingKey) {
783
788
  throw new Error(
@@ -836,19 +841,6 @@ function normalizedGitHost(uri: string): string {
836
841
  }
837
842
  }
838
843
 
839
- function normalizedGitRemote(uri: string): string {
840
- try {
841
- const url = new URL(uri);
842
- return `${url.protocol.toLowerCase()}//${url.host.toLowerCase()}${url.pathname.replace(/\/+$/, "").replace(/\.git$/, "")}`;
843
- } catch {
844
- return uri
845
- .trim()
846
- .replace(/\/+$/, "")
847
- .replace(/\.git$/, "")
848
- .toLowerCase();
849
- }
850
- }
851
-
852
844
  function repositoryCredentialProvider(
853
845
  resource: Extract<ResourceRef, { kind: "repository" }>,
854
846
  ): GitCredentialProvider | null {
@@ -12,17 +12,25 @@ export type ExpiringMaterialRenewalController = {
12
12
  stop(): Promise<void>;
13
13
  };
14
14
 
15
- export type ExpiringMaterialRenewalOptions<T> = {
15
+ export type ExpiringMaterialRenewalPublicErrorClass =
16
+ | "RunCredentialRenewalOperationError"
17
+ | "CodemodeTokenRenewalOperationError";
18
+
19
+ export type ExpiringMaterialRenewalOptions<
20
+ T,
21
+ ErrorClass extends ExpiringMaterialRenewalPublicErrorClass,
22
+ > = {
16
23
  initialExpiresAt: Date | null;
17
24
  resolve: () => Promise<T>;
18
25
  write: (material: T) => Promise<void>;
19
26
  expiresAt: (material: T) => Date | null;
27
+ publicErrorClass: ErrorClass;
20
28
  policy: ExpiringMaterialRenewalPolicy;
21
29
  now?: () => number;
22
30
  schedule?: (callback: () => void, delayMs: number) => RenewalTimer;
23
31
  clearSchedule?: (timer: RenewalTimer) => void;
24
32
  onSuccess?: (result: { material: T; nextDelayMs: number }) => void;
25
- onFailure?: (failure: { retryDelayMs: number; errorClass: string }) => void;
33
+ onFailure?: (failure: { retryDelayMs: number; errorClass: ErrorClass }) => void;
26
34
  };
27
35
 
28
36
  export function nextExpiringMaterialRenewalDelay(
@@ -45,9 +53,10 @@ export function nextExpiringMaterialRenewalDelay(
45
53
  * A late resolver cannot write after stop. A physical sandbox write that already
46
54
  * started is drained so turn settlement cannot race a stale mutation.
47
55
  */
48
- export function startExpiringMaterialRenewalLoop<T>(
49
- options: ExpiringMaterialRenewalOptions<T>,
50
- ): ExpiringMaterialRenewalController {
56
+ export function startExpiringMaterialRenewalLoop<
57
+ T,
58
+ ErrorClass extends ExpiringMaterialRenewalPublicErrorClass,
59
+ >(options: ExpiringMaterialRenewalOptions<T, ErrorClass>): ExpiringMaterialRenewalController {
51
60
  const now = options.now ?? Date.now;
52
61
  const schedule =
53
62
  options.schedule ??
@@ -102,14 +111,14 @@ export function startExpiringMaterialRenewalLoop<T>(
102
111
  // Observability never owns credential liveness.
103
112
  }
104
113
  scheduleRefresh(nextDelayMs);
105
- } catch (error) {
114
+ } catch {
106
115
  if (stopped) return;
107
116
  const scheduledRetryMs = retryDelayMs;
108
117
  retryDelayMs = Math.min(retryDelayMs * 2, options.policy.maxRetryMs);
109
118
  try {
110
119
  options.onFailure?.({
111
120
  retryDelayMs: scheduledRetryMs,
112
- errorClass: error instanceof Error ? error.name : "UnknownError",
121
+ errorClass: options.publicErrorClass,
113
122
  });
114
123
  } catch {
115
124
  // Observability never owns credential liveness.
@@ -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
+ }