@opengeni/worker-bundle 0.22.8 → 0.23.4-canary.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 (103) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +10 -2
  2. package/dist/activities/agent-turn/agent-build.d.ts +1 -4
  3. package/dist/activities/agent-turn/claim.d.ts +3 -4
  4. package/dist/activities/agent-turn/codex.d.ts +8 -7
  5. package/dist/activities/agent-turn/compaction-prep.d.ts +2 -0
  6. package/dist/activities/agent-turn/errors.d.ts +13 -0
  7. package/dist/activities/agent-turn/finalization.d.ts +1 -3
  8. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  9. package/dist/activities/agent-turn/model-usage.d.ts +10 -1
  10. package/dist/activities/agent-turn/quiescence.d.ts +39 -0
  11. package/dist/activities/agent-turn/sandbox-route.d.ts +14 -24
  12. package/dist/activities/agent-turn/sandbox-runtime.d.ts +1 -4
  13. package/dist/activities/agent-turn/sandbox-shared-preparation.d.ts +30 -0
  14. package/dist/activities/agent-turn/stream-attempt.d.ts +7 -0
  15. package/dist/activities/agent-turn/tool-environment.d.ts +1 -0
  16. package/dist/activities/agent-turn/tool-policy.d.ts +7 -5
  17. package/dist/activities/agent-turn/turn-context.d.ts +3 -8
  18. package/dist/activities/agent-turn/turn-sandbox-access.d.ts +12 -0
  19. package/dist/activities/agent-turn/video-reference-sandbox.d.ts +7 -0
  20. package/dist/activities/automations.d.ts +1 -0
  21. package/dist/activities/capabilities.d.ts +2 -1
  22. package/dist/activities/codex-rotation.d.ts +2 -0
  23. package/dist/activities/goals.d.ts +11 -0
  24. package/dist/activities/image-generation-references.d.ts +2 -1
  25. package/dist/activities/scheduled-tasks.d.ts +1 -0
  26. package/dist/activities/session-state.d.ts +2 -1
  27. package/dist/activities/types.d.ts +8 -0
  28. package/dist/{activities-control-VZIIV7RX.js → activities-control-SGU2WEVB.js} +328 -107
  29. package/dist/activities-control-SGU2WEVB.js.map +1 -0
  30. package/dist/activities-control.d.ts +2 -0
  31. package/dist/{activities-turn-5RGQDBFV.js → activities-turn-7EAQXW6Q.js} +828 -540
  32. package/dist/activities-turn-7EAQXW6Q.js.map +1 -0
  33. package/dist/activities.d.ts +2 -0
  34. package/dist/{chunk-O4HDX5SC.js → chunk-S2QVRVJW.js} +227 -36
  35. package/dist/chunk-S2QVRVJW.js.map +1 -0
  36. package/dist/{chunk-NAVSAZ56.js → chunk-TJIPCHMP.js} +162 -2
  37. package/dist/chunk-TJIPCHMP.js.map +1 -0
  38. package/dist/index.d.ts +1 -0
  39. package/dist/index.js +69 -6
  40. package/dist/index.js.map +1 -1
  41. package/dist/observability-metrics.d.ts +29 -0
  42. package/dist/personal-github-git-credentials.d.ts +1 -0
  43. package/dist/unhandled-rejection-boundary.d.ts +29 -0
  44. package/dist/workflow-bundle.js +26 -3
  45. package/dist/workflows/activities.d.ts +5 -0
  46. package/dist/workflows/automations.d.ts +5 -0
  47. package/package.json +18 -18
  48. package/src/activities/agent-run-admission.ts +7 -1
  49. package/src/activities/agent-turn/admission.ts +28 -6
  50. package/src/activities/agent-turn/agent-build.ts +15 -31
  51. package/src/activities/agent-turn/claim.ts +53 -72
  52. package/src/activities/agent-turn/codex.ts +21 -12
  53. package/src/activities/agent-turn/compaction-prep.ts +52 -1
  54. package/src/activities/agent-turn/errors.ts +78 -0
  55. package/src/activities/agent-turn/failure-settlement.ts +30 -19
  56. package/src/activities/agent-turn/finalization.ts +24 -21
  57. package/src/activities/agent-turn/history.ts +5 -1
  58. package/src/activities/agent-turn/list-models.ts +101 -0
  59. package/src/activities/agent-turn/model-usage.ts +88 -42
  60. package/src/activities/agent-turn/quiescence.ts +86 -0
  61. package/src/activities/agent-turn/run-credentials.ts +12 -1
  62. package/src/activities/agent-turn/run.ts +20 -14
  63. package/src/activities/agent-turn/sandbox-establish.ts +46 -11
  64. package/src/activities/agent-turn/sandbox-route.ts +21 -25
  65. package/src/activities/agent-turn/sandbox-runtime.ts +1 -116
  66. package/src/activities/agent-turn/sandbox-shared-preparation.ts +206 -0
  67. package/src/activities/agent-turn/stream-attempt.ts +106 -14
  68. package/src/activities/agent-turn/tool-environment.ts +72 -14
  69. package/src/activities/agent-turn/tool-policy.ts +7 -6
  70. package/src/activities/agent-turn/turn-context.ts +12 -14
  71. package/src/activities/agent-turn/{image-reference-sandbox.ts → turn-sandbox-access.ts} +15 -13
  72. package/src/activities/agent-turn/video-reference-sandbox.ts +23 -0
  73. package/src/activities/automations.ts +122 -67
  74. package/src/activities/capabilities.ts +72 -2
  75. package/src/activities/codex-capacity.ts +13 -5
  76. package/src/activities/codex-rotation.ts +17 -0
  77. package/src/activities/goals.ts +117 -35
  78. package/src/activities/image-generation-references.ts +9 -9
  79. package/src/activities/retained-screenshots.ts +104 -21
  80. package/src/activities/sandbox-lease.ts +7 -4
  81. package/src/activities/scheduled-tasks.ts +248 -30
  82. package/src/activities/session-state.ts +8 -1
  83. package/src/activities/site-auth-maintenance.ts +12 -12
  84. package/src/activities/types.ts +6 -0
  85. package/src/activities.ts +1 -0
  86. package/src/activity-services.ts +4 -1
  87. package/src/github-rest-mcp.ts +8 -4
  88. package/src/http.ts +3 -0
  89. package/src/index.ts +38 -4
  90. package/src/observability-metrics.ts +202 -0
  91. package/src/personal-github-git-credentials.ts +45 -4
  92. package/src/sandbox-resume.ts +1 -1
  93. package/src/sandbox-routing.ts +7 -4
  94. package/src/unhandled-rejection-boundary.ts +73 -0
  95. package/src/workflows/activities.ts +15 -0
  96. package/src/workflows/automations.ts +21 -2
  97. package/dist/activities/agent-turn/image-reference-sandbox.d.ts +0 -14
  98. package/dist/activities/recording.d.ts +0 -90
  99. package/dist/activities-control-VZIIV7RX.js.map +0 -1
  100. package/dist/activities-turn-5RGQDBFV.js.map +0 -1
  101. package/dist/chunk-NAVSAZ56.js.map +0 -1
  102. package/dist/chunk-O4HDX5SC.js.map +0 -1
  103. package/src/activities/recording.ts +0 -279
@@ -49,6 +49,7 @@ import {
49
49
  recordModelInputTokens,
50
50
  recordTurnStartupPhase,
51
51
  recordTurnWorkerPreparationTotal,
52
+ recordSandboxSharedPreparation,
52
53
  StreamTimingMetrics,
53
54
  turnLifecycleMetricsFor,
54
55
  } from "../../observability-metrics";
@@ -115,8 +116,13 @@ import {
115
116
  recordModelUsageAndDebitCredits,
116
117
  recordAuthoritativeModelCallFact,
117
118
  } from "./model-usage";
118
- import { waitForTurnStreamCleanup } from "./quiescence";
119
+ import {
120
+ assertAgentStreamNotCancelled,
121
+ assertSuccessfulAgentStreamCompletion,
122
+ requireAgentStreamFinalOutput,
123
+ } from "./quiescence";
119
124
  import { waitForTurnOperation } from "./sandbox-provision";
125
+ import { createSharedRigSetupCoordinator } from "./sandbox-shared-preparation";
120
126
 
121
127
  import type { CompactionSummarizer } from "../context-compaction";
122
128
  import type { TurnExecutionPolicyV1 } from "@opengeni/contracts";
@@ -209,12 +215,14 @@ export type TurnStreamAttemptDeps = {
209
215
  turn: { executionGeneration: number; model: string };
210
216
  trigger: NonNullable<Awaited<ReturnType<typeof getSessionEvent>>>;
211
217
  humanInputResume: Awaited<ReturnType<typeof getHumanInputResumeForEvent>>;
218
+ attachPendingUpdatesAfterOpenSuffix: () => Promise<boolean>;
212
219
  agent: ReturnType<ActivityServices["runtime"]["buildAgent"]>;
213
220
  resolvedModel: ReturnType<ActivityServices["runtime"]["resolveTurnModel"]>;
214
221
  providerApi: HistoryProviderApi;
215
222
  runSettings: Settings;
216
223
  turnTools: ReturnType<typeof withFirstPartyTools>;
217
224
  compactSummarizer: CompactionSummarizer;
225
+ settleDeferredSteerAfterCompaction: () => Promise<RunAgentTurnResult | null>;
218
226
  compactionModelHistoryProjector: (
219
227
  items: Array<Record<string, unknown>>,
220
228
  ) => Promise<Array<Record<string, unknown>>>;
@@ -233,6 +241,16 @@ export type TurnStreamAttemptDeps = {
233
241
  videoGenerationAcceptancesByCallId: Map<string, { operationId: string; requestDigest: string }>;
234
242
  };
235
243
 
244
+ export async function attachPendingUpdatesBeforePreparingModelInput(input: {
245
+ shouldAttach: boolean;
246
+ attachPendingUpdates: () => Promise<boolean>;
247
+ prepareModelInput: () => Promise<void>;
248
+ }): Promise<boolean> {
249
+ if (input.shouldAttach && !(await input.attachPendingUpdates())) return false;
250
+ await input.prepareModelInput();
251
+ return true;
252
+ }
253
+
236
254
  export async function runTurnStreamAttempt(
237
255
  deps: TurnStreamAttemptDeps,
238
256
  ): Promise<RunAgentTurnResult> {
@@ -289,12 +307,14 @@ export async function runTurnStreamAttempt(
289
307
  turn,
290
308
  trigger,
291
309
  humanInputResume,
310
+ attachPendingUpdatesAfterOpenSuffix,
292
311
  agent,
293
312
  resolvedModel,
294
313
  providerApi,
295
314
  runSettings,
296
315
  turnTools,
297
316
  compactSummarizer,
317
+ settleDeferredSteerAfterCompaction,
298
318
  compactionModelHistoryProjector,
299
319
  generatedImageHistoryProjector,
300
320
  modelHistoryProjector,
@@ -485,6 +505,7 @@ export async function runTurnStreamAttempt(
485
505
  resolvedModel?.provider.kind === "codex-subscription" ||
486
506
  resolvedModel?.provider.kind === "xai-subscription";
487
507
  let fallbackProviderRequestStartedAt: number | null = null;
508
+ let fallbackProviderRequestLifecycleStartedAt: number | null = null;
488
509
  const recordFallbackProviderDispatchAtWire = async (): Promise<void> => {
489
510
  await checkpointHistoryBeforeProviderDispatch(historySink);
490
511
  if (
@@ -532,6 +553,7 @@ export async function runTurnStreamAttempt(
532
553
  },
533
554
  ]);
534
555
  fallbackProviderRequestStartedAt = performance.now();
556
+ fallbackProviderRequestLifecycleStartedAt = fallbackProviderRequestStartedAt;
535
557
  } catch (error) {
536
558
  auditOutcome = "failed";
537
559
  throw error;
@@ -549,12 +571,13 @@ export async function runTurnStreamAttempt(
549
571
  const ownedEstablished =
550
572
  sandboxState.resolvedSandbox?.established ?? sandboxState.lazyOwnedSandbox;
551
573
  const runStreamOnce = async (): ReturnType<OpenGeniRuntime["runStream"]> => {
574
+ const eagerResolvedSandbox = sandboxState.resolvedSandbox;
552
575
  // Eager owned sessions must settle the exact platform-setup provider
553
576
  // promise before the long model stream starts; otherwise one admission
554
577
  // would remain in flight for the entire turn and suppress every
555
578
  // heartbeat capture. Lazy setup already runs under the same wrapper in
556
579
  // its first-operation provisioner above.
557
- if (sandboxState.resolvedSandbox && !sandboxState.lazyOwnedSandbox && ownedEstablished) {
580
+ if (eagerResolvedSandbox && !sandboxState.lazyOwnedSandbox && ownedEstablished) {
558
581
  const ownedSandboxSetupStartedAt = performance.now();
559
582
  let ownedSandboxSetupOutcome: "completed" | "failed" = "completed";
560
583
  try {
@@ -567,13 +590,13 @@ export async function runTurnStreamAttempt(
567
590
  await attachRunCredentialRenewal(
568
591
  eagerSetupSession as RunCredentialCommandSession,
569
592
  initialRunCredentialMaterial,
570
- sandboxState.resolvedSandbox,
593
+ eagerResolvedSandbox,
571
594
  );
572
595
  const eagerCredentialSetupSession = initialRunCredentialMaterial
573
596
  ? withRunCredentialsSession(eagerSetupSession as object, input.sessionId)
574
597
  : eagerSetupSession;
575
598
  await runWorkspaceMutationForSandbox(
576
- sandboxState.resolvedSandbox,
599
+ eagerResolvedSandbox,
577
600
  "eagerOwnedSandboxSetup",
578
601
  async () =>
579
602
  await runOwnedSandboxSetup(
@@ -600,6 +623,24 @@ export async function runTurnStreamAttempt(
600
623
  ),
601
624
  }
602
625
  : {}),
626
+ ...(sandboxState.sandboxGroupId && sandboxState.sandboxHolderId
627
+ ? {
628
+ coordinateSharedRigSetup: createSharedRigSetupCoordinator({
629
+ db,
630
+ accountId: input.accountId,
631
+ workspaceId: input.workspaceId,
632
+ sandboxGroupId: sandboxState.sandboxGroupId,
633
+ attemptId: input.attemptId,
634
+ holderId: sandboxState.sandboxHolderId,
635
+ sandbox: eagerResolvedSandbox,
636
+ ...(runtimeCancellationSignal
637
+ ? { signal: runtimeCancellationSignal }
638
+ : {}),
639
+ observe: (measurement) =>
640
+ recordSandboxSharedPreparation(observability, measurement),
641
+ }),
642
+ }
643
+ : {}),
603
644
  },
604
645
  ),
605
646
  );
@@ -870,6 +911,8 @@ export async function runTurnStreamAttempt(
870
911
  latencyMode: turnExecutionPolicy.latencyMode,
871
912
  metricProvider: streamProvider,
872
913
  externallyBilled: billingState.isExternallyBilledTurn,
914
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
915
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
873
916
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
874
917
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
875
918
  emittedSourceKeys: emittedModelUsageSourceKeys,
@@ -886,6 +929,31 @@ export async function runTurnStreamAttempt(
886
929
  ...(resolvedModel?.provider.id ? { providerId: resolvedModel.provider.id } : {}),
887
930
  });
888
931
  if (responseResult.status === "processed") {
932
+ if (
933
+ !providerPublishesNativeRequestEvents &&
934
+ fallbackProviderRequestLifecycleStartedAt !== null
935
+ ) {
936
+ const durationMs = Math.max(
937
+ 0,
938
+ performance.now() - fallbackProviderRequestLifecycleStartedAt,
939
+ );
940
+ fallbackProviderRequestLifecycleStartedAt = null;
941
+ await eventing.publish!([
942
+ {
943
+ type: "agent.model.request",
944
+ payload: {
945
+ phase: "completed",
946
+ provider: streamProvider,
947
+ durationMs: Math.round(durationMs),
948
+ turnId: activeTurnId,
949
+ attemptId: input.attemptId,
950
+ dispatchId,
951
+ executionGeneration: attempt.executionGeneration,
952
+ },
953
+ },
954
+ ]);
955
+ attempt.providerRecoveryCount = 0;
956
+ }
889
957
  const rawStreamHistory = (eventing.stream.state as { history?: unknown[] }).history;
890
958
  if (Array.isArray(rawStreamHistory)) {
891
959
  // The completed image item is normally retained from its own
@@ -910,6 +978,8 @@ export async function runTurnStreamAttempt(
910
978
  input.workspaceId,
911
979
  billingState.isExternallyBilledTurn,
912
980
  entitlements,
981
+ billingState.chargesOpenGeniCredits,
982
+ billingState.countsTowardTokenCap,
913
983
  );
914
984
  } catch (limitError) {
915
985
  // Capture the run state at the boundary so the budget valve in
@@ -1168,9 +1238,13 @@ export async function runTurnStreamAttempt(
1168
1238
  }
1169
1239
  }
1170
1240
  } catch (error) {
1171
- if (fallbackProviderRequestStartedAt !== null) {
1172
- const durationMs = Math.max(0, performance.now() - fallbackProviderRequestStartedAt);
1241
+ if (fallbackProviderRequestLifecycleStartedAt !== null) {
1242
+ const durationMs = Math.max(
1243
+ 0,
1244
+ performance.now() - fallbackProviderRequestLifecycleStartedAt,
1245
+ );
1173
1246
  fallbackProviderRequestStartedAt = null;
1247
+ fallbackProviderRequestLifecycleStartedAt = null;
1174
1248
  await eventing.publish!([
1175
1249
  {
1176
1250
  type: "agent.model.request",
@@ -1227,11 +1301,12 @@ export async function runTurnStreamAttempt(
1227
1301
  void iterator.return?.().catch(() => undefined);
1228
1302
  }
1229
1303
  }
1230
- await waitForTurnStreamCleanup(
1231
- eventing.batcher.flush(),
1232
- eventing.stream.completed.catch(() => undefined),
1233
- cancellationSignal,
1234
- );
1304
+ await assertSuccessfulAgentStreamCompletion({
1305
+ batcherFlush: eventing.batcher.flush(),
1306
+ stream: eventing.stream,
1307
+ temporalCancellationSignal: cancellationSignal,
1308
+ runtimeCancellationSignal,
1309
+ });
1235
1310
  if (
1236
1311
  options.requireTerminalModelResponse &&
1237
1312
  eventing.stream.interruptions.length === 0 &&
@@ -1247,6 +1322,7 @@ export async function runTurnStreamAttempt(
1247
1322
  throwIfTurnCancelled();
1248
1323
  throw new PostCompactionContinuationEmptyError();
1249
1324
  }
1325
+ assertAgentStreamNotCancelled(eventing.stream.cancelled);
1250
1326
  if (!streamSawPerResponseUsage) {
1251
1327
  const aggregateUsage = eventing.stream.state.usage;
1252
1328
  const normalizedAggregateUsage = normalizeModelCallUsage(aggregateUsage);
@@ -1279,6 +1355,8 @@ export async function runTurnStreamAttempt(
1279
1355
  turnAttemptId: input.attemptId,
1280
1356
  model: turn.model,
1281
1357
  externallyBilled: billingState.isExternallyBilledTurn,
1358
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
1359
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
1282
1360
  usage: aggregateUsage,
1283
1361
  normalizedUsage: normalizedAggregateUsage,
1284
1362
  sourceKey: aggregateSourceKey,
@@ -1300,6 +1378,8 @@ export async function runTurnStreamAttempt(
1300
1378
  sourceKey: aggregateSourceKey,
1301
1379
  usage: { usage: aggregateUsage },
1302
1380
  normalizedUsage: normalizedAggregateUsage,
1381
+ ...(billing ? { billingPath: billing.billingPath } : {}),
1382
+ ...(billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {}),
1303
1383
  servingAccountHash: aggregateAccountCtx.servingAccountHash,
1304
1384
  accountChangedFromPrevCall: aggregateAccountCtx.accountChangedFromPrevCall,
1305
1385
  emittedSourceKeys: emittedModelUsageSourceKeys,
@@ -1479,7 +1559,7 @@ export async function runTurnStreamAttempt(
1479
1559
  return claimedResult({ status: "requires_action" });
1480
1560
  }
1481
1561
 
1482
- const finalOutput = String(eventing.stream.finalOutput ?? "");
1562
+ const finalOutput = String(requireAgentStreamFinalOutput(eventing.stream.finalOutput));
1483
1563
  await historySink.reconcileConversationTruth({ requireDurable: true });
1484
1564
  // Op-stream durability fence: the tool outputs are now durably in the
1485
1565
  // history store (a redispatch would NOT re-execute them), so this
@@ -1545,8 +1625,16 @@ export async function runTurnStreamAttempt(
1545
1625
  control.activityStatus = "requires_action";
1546
1626
  return claimedResult({ status: "requires_action" });
1547
1627
  }
1548
-
1549
- await prepareRunAttemptInput();
1628
+ if (
1629
+ !(await attachPendingUpdatesBeforePreparingModelInput({
1630
+ shouldAttach:
1631
+ trigger.type === "user.approvalDecision" || trigger.type === "user.humanInputResponse",
1632
+ attachPendingUpdates: attachPendingUpdatesAfterOpenSuffix,
1633
+ prepareModelInput: prepareRunAttemptInput,
1634
+ }))
1635
+ ) {
1636
+ return claimedResult({ status: "cancelled" });
1637
+ }
1550
1638
  let retriedAfterCompaction = false;
1551
1639
  while (true) {
1552
1640
  try {
@@ -1624,6 +1712,8 @@ export async function runTurnStreamAttempt(
1624
1712
  );
1625
1713
  compactionRequestCleared = landmark.requestConsumed;
1626
1714
  if (!isCompactionSummaryFailure(compactError)) throw compactError;
1715
+ const deferredSteer = await settleDeferredSteerAfterCompaction();
1716
+ if (deferredSteer) return deferredSteer;
1627
1717
  compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
1628
1718
  observability.warn("context compaction recovery compaction failed", {
1629
1719
  sessionId: input.sessionId,
@@ -1676,6 +1766,8 @@ export async function runTurnStreamAttempt(
1676
1766
  // machine updates stay pending for that actionable wake.
1677
1767
  return claimedResult({ status: "idle", deferredUntilWake: true });
1678
1768
  }
1769
+ const deferredSteer = await settleDeferredSteerAfterCompaction();
1770
+ if (deferredSteer) return deferredSteer;
1679
1771
  // Codex parity: compaction remains inside the same logical turn and
1680
1772
  // the same activity. Rebuild the model-visible history from the
1681
1773
  // durable replacement and continue the sampling loop; do not create
@@ -1,9 +1,16 @@
1
1
  import {
2
2
  getScheduledVariableSetExpectedGenerationForAttempt,
3
+ getWorkspaceModelPolicy,
4
+ listWorkspaceGatewayCustomModels,
5
+ listWorkspaceOpenRouterCustomModels,
3
6
  persistAttemptToolCatalog,
4
7
  namedSubjectHasLiveWorkspaceAuthority,
5
8
  updateSessionTitleWithEvent,
6
9
  withCodexAppsRequestAuthorization,
10
+ workspaceCodexSubscriptionActive,
11
+ workspaceVercelAiGatewayConnectionActive,
12
+ workspaceOpenRouterConnectionActive,
13
+ workspaceXaiSubscriptionActiveForAuthority,
7
14
  } from "@opengeni/db";
8
15
  import { publishDurableSessionEvents } from "@opengeni/events";
9
16
  import {
@@ -31,6 +38,8 @@ import {
31
38
  defaultSessionMcpServerIds,
32
39
  loadRigDefaultVariableSetEnvironment,
33
40
  mergeRigDefaultVariableSetEnvironment,
41
+ resolveCatalogSettings,
42
+ resolveWorkspaceModelSelection,
34
43
  withFrozenPersonalConnectionDelegations,
35
44
  resolveSessionToolPolicy,
36
45
  } from "@opengeni/core";
@@ -40,11 +49,11 @@ import type { TurnActivityServices as ActivityServices, RunAgentTurnInput } from
40
49
  import { recordTurnStartupPhase } from "../../observability-metrics";
41
50
  import { ToolResultSpill } from "./tool-result-spill";
42
51
  import { createTurnMediaArtifacts } from "./media-artifacts";
43
- import { SandboxChannelAService, type ChannelASession } from "@opengeni/runtime/sandbox";
52
+ import { SandboxChannelAService } from "@opengeni/runtime/sandbox";
44
53
  import { sandboxRunAs } from "@opengeni/runtime";
45
54
  import { type ToolAuthNeededPayload } from "@opengeni/contracts";
46
55
 
47
- import { shouldPublishToolAuthNeededForTurn } from "./admission";
56
+ import { shouldPublishToolAuthNeededForTurn, xaiCatalogReadinessAuthority } from "./admission";
48
57
  import { unavailableMcpOperationalContext } from "./errors";
49
58
  import { runtimeResourcesForTurn } from "./file-resources";
50
59
  import { waitForTurnOperation } from "./sandbox-provision";
@@ -64,6 +73,8 @@ import {
64
73
  sessionTitleToolPlan,
65
74
  shouldRequestMissingSessionTitle,
66
75
  } from "./session-title";
76
+ import { resolveTurnSandboxAccess } from "./turn-sandbox-access";
77
+ import { createListModelsAttemptToolDefinition } from "./list-models";
67
78
 
68
79
  export type PrepareTurnToolPolicyDeps = {
69
80
  input: RunAgentTurnInput;
@@ -81,6 +92,7 @@ export type PrepareTurnToolPolicyDeps = {
81
92
 
82
93
  export type PrepareTurnToolRuntimeDeps = {
83
94
  input: RunAgentTurnInput;
95
+ catalogSourceSettings: Settings;
84
96
  db: ActivityServices["db"];
85
97
  bus: ActivityServices["bus"];
86
98
  runtime: ActivityServices["runtime"];
@@ -295,6 +307,7 @@ export async function prepareTurnToolPolicy(deps: PrepareTurnToolPolicyDeps) {
295
307
  export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
296
308
  const {
297
309
  input,
310
+ catalogSourceSettings,
298
311
  db,
299
312
  bus,
300
313
  runtime,
@@ -509,6 +522,55 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
509
522
  ]
510
523
  : [];
511
524
  const attemptToolDefinitions = [
525
+ createListModelsAttemptToolDefinition({
526
+ currentModelId: turnExecutionPolicy.productModelId,
527
+ load: async () => {
528
+ const currentCatalog = await resolveCatalogSettings(db, catalogSourceSettings);
529
+ const currentSettings = currentCatalog.settings;
530
+ const xaiReadinessAuthority = xaiCatalogReadinessAuthority(turn, credentialSubjectId);
531
+ const [
532
+ policy,
533
+ codexSubscriptionActive,
534
+ xaiSubscriptionActive,
535
+ workspaceGatewayConnectionActive,
536
+ workspaceGatewayCustomModels,
537
+ openRouterConnectionActive,
538
+ workspaceOpenRouterCustomModels,
539
+ ] = await Promise.all([
540
+ getWorkspaceModelPolicy(db, input.workspaceId),
541
+ workspaceCodexSubscriptionActive(db, currentSettings, input.workspaceId),
542
+ xaiReadinessAuthority && currentSettings.supergrokSubscriptionEnabled
543
+ ? workspaceXaiSubscriptionActiveForAuthority(db, currentSettings, {
544
+ workspaceId: input.workspaceId,
545
+ ...xaiReadinessAuthority,
546
+ })
547
+ : false,
548
+ workspaceVercelAiGatewayConnectionActive(db, input.workspaceId),
549
+ listWorkspaceGatewayCustomModels(db, {
550
+ accountId: input.accountId,
551
+ workspaceId: input.workspaceId,
552
+ }),
553
+ workspaceOpenRouterConnectionActive(db, input.workspaceId),
554
+ listWorkspaceOpenRouterCustomModels(db, {
555
+ accountId: input.accountId,
556
+ workspaceId: input.workspaceId,
557
+ }),
558
+ ]);
559
+ return {
560
+ selections: resolveWorkspaceModelSelection({
561
+ settings: currentSettings,
562
+ policy,
563
+ codexSubscriptionActive,
564
+ xaiSubscriptionActive,
565
+ workspaceGatewayConnectionActive,
566
+ workspaceGatewayCustomModels,
567
+ workspaceOpenRouterConnectionActive: openRouterConnectionActive,
568
+ workspaceOpenRouterCustomModels,
569
+ }),
570
+ modelNotes: currentCatalog.modelNotes,
571
+ };
572
+ },
573
+ }),
512
574
  ...(titleToolPlan.promoteTitleTool
513
575
  ? [
514
576
  createSessionTitleAttemptToolDefinition({
@@ -586,21 +648,17 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
586
648
  ) => {
587
649
  throwIfWorkerShuttingDown();
588
650
  throwIfTurnCancelled();
589
- let sandbox = sandboxState.resolvedSandbox;
590
- if (!sandbox && sandboxState.turnSandboxProvisioner) {
591
- sandbox = await sandboxState.turnSandboxProvisioner.get();
592
- }
593
- const sessionForImport = (sandboxState.lazyOwnedSandbox?.session ??
594
- sandbox?.established.session ??
595
- media.sdkOwnedSandboxSession) as ChannelASession | null;
596
- if (!sessionForImport) {
597
- throw new Error("Connector attachment sandbox is unavailable");
598
- }
651
+ const sandboxAccess = await resolveTurnSandboxAccess(
652
+ sandboxState,
653
+ media.sdkOwnedSandboxSession,
654
+ "Connector attachment sandbox is unavailable",
655
+ );
656
+ const sandbox = sandboxAccess.sandbox;
599
657
  const runAs = sandboxRunAs(runSettings);
600
658
  const channel = new SandboxChannelAService({
601
- session: sessionForImport,
659
+ session: sandboxAccess.session,
602
660
  workspaceRoot: "/workspace",
603
- leaseEpoch: sandbox?.leaseEpoch ?? 0,
661
+ leaseEpoch: sandboxAccess.leaseEpoch,
604
662
  emit: async (events) => {
605
663
  await eventing.publish?.(events, true);
606
664
  },
@@ -6,7 +6,7 @@ import {
6
6
  import {
7
7
  isDirectOpenAiApiBaseUrl,
8
8
  type ModelProviderApi,
9
- type RegistryProviderKind,
9
+ type ResolvedModelProvider,
10
10
  type Settings,
11
11
  } from "@opengeni/config";
12
12
  import { type ModelAttachmentInputPolicy } from "../run-input";
@@ -42,7 +42,7 @@ export function shouldStartOnTurnRecording(params: {
42
42
  */
43
43
  export function computerToolModeForTurn(
44
44
  resolvedModel: {
45
- provider: { kind: RegistryProviderKind; api: ModelProviderApi };
45
+ provider: { kind: ResolvedModelProvider["kind"]; api: ModelProviderApi };
46
46
  configured: { capabilities: { inputModalities: string[] } };
47
47
  } | null,
48
48
  ): ComputerToolMode {
@@ -64,7 +64,7 @@ export function computerToolModeForTurn(
64
64
  /** Chat wires and Gateway models do not advertise OpenAI's hosted sandbox tool types. */
65
65
  export function structuredToolTransportForTurn(
66
66
  resolvedModel: {
67
- provider: { kind: RegistryProviderKind; api: ModelProviderApi };
67
+ provider: { kind: ResolvedModelProvider["kind"]; api: ModelProviderApi };
68
68
  } | null,
69
69
  ): boolean {
70
70
  if (!resolvedModel) return true;
@@ -87,7 +87,7 @@ export function lazyToolTransportForTurn(
87
87
  resolvedModel: {
88
88
  provider: {
89
89
  id: string;
90
- kind: RegistryProviderKind;
90
+ kind: ResolvedModelProvider["kind"];
91
91
  api: ModelProviderApi;
92
92
  wireProfile: "openai" | "azure-openai";
93
93
  builtin: boolean;
@@ -112,6 +112,8 @@ export function lazyToolTransportForTurn(
112
112
  export function shouldDeferNonEagerToolPreparation(args: {
113
113
  lazyToolTransport: LazyToolTransport | null;
114
114
  progressiveDisclosureEnabled: boolean;
115
+ /** Retained for call-site compatibility; the lazy runtime now gates artifact
116
+ * and ordinary function calls on the same exact preparation promise. */
115
117
  artifactRuntimeAvailable: boolean;
116
118
  triggerKind: "next" | "approval";
117
119
  triggerType: string;
@@ -119,7 +121,6 @@ export function shouldDeferNonEagerToolPreparation(args: {
119
121
  return Boolean(
120
122
  args.lazyToolTransport &&
121
123
  args.progressiveDisclosureEnabled &&
122
- !args.artifactRuntimeAvailable &&
123
124
  args.triggerKind === "next" &&
124
125
  (args.triggerType === "user.message" || args.triggerType === "system.update.delivered"),
125
126
  );
@@ -160,7 +161,7 @@ export function openAiHostedImageProviderBindingForTurn(
160
161
  resolvedModel: {
161
162
  provider: {
162
163
  id: string;
163
- kind: RegistryProviderKind;
164
+ kind: ResolvedModelProvider["kind"];
164
165
  builtin: boolean;
165
166
  baseUrl?: string | undefined;
166
167
  };
@@ -18,7 +18,6 @@ import type { CodemodeAttemptDispatcher } from "../codemode-dispatcher";
18
18
  import type { CodemodeTokenRenewalController } from "../codemode-token-renewal";
19
19
  import type { GitCredentialRenewalController } from "../git-credential-renewal";
20
20
  import type { RunCredentialRenewalController } from "../run-credential-renewal";
21
- import type { ActiveRecording } from "../recording";
22
21
  import type { createRuntimeBatcher, startActivityHeartbeat } from "../streaming";
23
22
  import type { RunAgentTurnResult } from "../types";
24
23
  import type { TurnOutcome } from "../../observability-metrics";
@@ -36,7 +35,14 @@ export type ClaimedResult = (
36
35
 
37
36
  export type TurnSettleFn = (input: {
38
37
  events: Array<Omit<AppendEventInput, "producerId" | "producerSeq" | "turnId">>;
39
- turnStatus: "queued" | "running" | "completed" | "failed" | "cancelled" | "requires_action";
38
+ turnStatus:
39
+ | "queued"
40
+ | "running"
41
+ | "completed"
42
+ | "failed"
43
+ | "cancelled"
44
+ | "superseded"
45
+ | "requires_action";
40
46
  sessionStatus: SessionStatus;
41
47
  activeTurnId: string | null;
42
48
  consumeRequestedCompactionFailure?: boolean;
@@ -67,6 +73,8 @@ export type BillingState = {
67
73
  isCodexTurn: boolean;
68
74
  isXaiTurn: boolean;
69
75
  isExternallyBilledTurn: boolean;
76
+ chargesOpenGeniCredits: boolean;
77
+ countsTowardTokenCap: boolean;
70
78
  };
71
79
 
72
80
  export type SandboxRuntimeState = {
@@ -110,12 +118,6 @@ export type RenewalState = {
110
118
  publishedRunCredentialNotices: Set<string>;
111
119
  };
112
120
 
113
- export type RecordingState = {
114
- activeRecording: ActiveRecording | null;
115
- computerUseRecordingStart: Promise<void> | null;
116
- didComputerUse: boolean;
117
- };
118
-
119
121
  export type EventingState = {
120
122
  heartbeatTimer: ReturnType<typeof startActivityHeartbeat> | undefined;
121
123
  batcher: ReturnType<typeof createRuntimeBatcher> | null;
@@ -174,7 +176,6 @@ export type TurnContext = {
174
176
  billingState: BillingState;
175
177
  sandboxState: SandboxRuntimeState;
176
178
  renewals: RenewalState;
177
- recordingState: RecordingState;
178
179
  eventing: EventingState;
179
180
  workspaceRefs: WorkspaceRefState;
180
181
  providerTurn: ProviderTurnState;
@@ -205,6 +206,8 @@ export function createTurnContext(input: {
205
206
  isCodexTurn: false,
206
207
  isXaiTurn: false,
207
208
  isExternallyBilledTurn: false,
209
+ chargesOpenGeniCredits: true,
210
+ countsTowardTokenCap: true,
208
211
  },
209
212
  sandboxState: {
210
213
  resolvedSandbox: null,
@@ -238,11 +241,6 @@ export function createTurnContext(input: {
238
241
  codemodeTokenRenewalClosed: false,
239
242
  publishedRunCredentialNotices: new Set(),
240
243
  },
241
- recordingState: {
242
- activeRecording: null,
243
- computerUseRecordingStart: null,
244
- didComputerUse: false,
245
- },
246
244
  eventing: {
247
245
  heartbeatTimer: undefined,
248
246
  batcher: null,
@@ -1,21 +1,24 @@
1
1
  import type { ChannelASession } from "@opengeni/runtime/sandbox";
2
+ import type { ResumedTurnSandbox } from "../../sandbox-resume";
2
3
  import type { SandboxRuntimeState } from "./turn-context";
3
4
 
4
- type ImageReferenceSandboxState = Pick<
5
+ type TurnSandboxAccessState = Pick<
5
6
  SandboxRuntimeState,
6
7
  "resolvedSandbox" | "lazyOwnedSandbox" | "turnSandboxProvisioner"
7
8
  >;
8
9
 
9
- /**
10
- * Join the turn's canonical lazy sandbox boundary before reading a
11
- * model-supplied /workspace image reference. Durable File and artifact
12
- * references never call this helper because their bytes come from object
13
- * storage instead.
14
- */
15
- export async function resolveImageReferenceSandboxSession(
16
- sandboxState: ImageReferenceSandboxState,
10
+ export type ResolvedTurnSandboxAccess = Readonly<{
11
+ sandbox: ResumedTurnSandbox | null;
12
+ session: ChannelASession;
13
+ leaseEpoch: number;
14
+ }>;
15
+
16
+ /** Join the turn's canonical sandbox boundary and return its active routed session. */
17
+ export async function resolveTurnSandboxAccess(
18
+ sandboxState: TurnSandboxAccessState,
17
19
  sdkOwnedSandboxSession: unknown,
18
- ): Promise<{ session: ChannelASession; leaseEpoch: number }> {
20
+ unavailableMessage: string,
21
+ ): Promise<ResolvedTurnSandboxAccess> {
19
22
  let sandbox = sandboxState.resolvedSandbox;
20
23
  if (!sandbox && sandboxState.turnSandboxProvisioner) {
21
24
  sandbox = await sandboxState.turnSandboxProvisioner.get();
@@ -24,11 +27,10 @@ export async function resolveImageReferenceSandboxSession(
24
27
  const session = (sandboxState.lazyOwnedSandbox?.session ??
25
28
  sandbox?.established.session ??
26
29
  sdkOwnedSandboxSession) as ChannelASession | null;
27
- if (!session) {
28
- throw new Error("Sandbox image reference is unavailable");
29
- }
30
+ if (!session) throw new Error(unavailableMessage);
30
31
 
31
32
  return {
33
+ sandbox,
32
34
  session,
33
35
  leaseEpoch: sandbox?.leaseEpoch ?? 0,
34
36
  };
@@ -0,0 +1,23 @@
1
+ import type { GenerateVideoToolInput } from "@opengeni/contracts";
2
+ import { videoReferencePaths } from "../video-reference-staging";
3
+ import type { SandboxRuntimeState } from "./turn-context";
4
+ import { resolveTurnSandboxAccess, type ResolvedTurnSandboxAccess } from "./turn-sandbox-access";
5
+
6
+ type VideoReferenceSandboxState = Pick<
7
+ SandboxRuntimeState,
8
+ "resolvedSandbox" | "lazyOwnedSandbox" | "turnSandboxProvisioner"
9
+ >;
10
+
11
+ /** Text-to-video stays sandbox-free; source-bearing requests join the turn sandbox before I/O. */
12
+ export async function resolveVideoReferenceSandboxAccess(
13
+ request: GenerateVideoToolInput,
14
+ sandboxState: VideoReferenceSandboxState,
15
+ sdkOwnedSandboxSession: unknown,
16
+ ): Promise<ResolvedTurnSandboxAccess | null> {
17
+ if (videoReferencePaths(request).length === 0) return null;
18
+ return await resolveTurnSandboxAccess(
19
+ sandboxState,
20
+ sdkOwnedSandboxSession,
21
+ "Video reference sandbox is unavailable",
22
+ );
23
+ }