@opengeni/worker-bundle 0.22.8 → 0.23.3-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 (74) hide show
  1. package/dist/activities/agent-turn/agent-build.d.ts +1 -4
  2. package/dist/activities/agent-turn/claim.d.ts +2 -4
  3. package/dist/activities/agent-turn/codex.d.ts +6 -5
  4. package/dist/activities/agent-turn/compaction-prep.d.ts +2 -0
  5. package/dist/activities/agent-turn/errors.d.ts +13 -0
  6. package/dist/activities/agent-turn/finalization.d.ts +1 -3
  7. package/dist/activities/agent-turn/quiescence.d.ts +39 -0
  8. package/dist/activities/agent-turn/sandbox-route.d.ts +14 -24
  9. package/dist/activities/agent-turn/sandbox-runtime.d.ts +1 -4
  10. package/dist/activities/agent-turn/sandbox-shared-preparation.d.ts +30 -0
  11. package/dist/activities/agent-turn/stream-attempt.d.ts +7 -0
  12. package/dist/activities/agent-turn/tool-policy.d.ts +3 -32
  13. package/dist/activities/agent-turn/turn-context.d.ts +1 -8
  14. package/dist/activities/agent-turn/turn-sandbox-access.d.ts +12 -0
  15. package/dist/activities/agent-turn/video-reference-sandbox.d.ts +7 -0
  16. package/dist/activities/codex-rotation.d.ts +2 -0
  17. package/dist/activities/session-state.d.ts +2 -1
  18. package/dist/{activities-control-VZIIV7RX.js → activities-control-52W5IDWM.js} +125 -13
  19. package/dist/activities-control-52W5IDWM.js.map +1 -0
  20. package/dist/{activities-turn-5RGQDBFV.js → activities-turn-A2G2KLSY.js} +534 -482
  21. package/dist/activities-turn-A2G2KLSY.js.map +1 -0
  22. package/dist/{chunk-NAVSAZ56.js → chunk-GFNXVZVQ.js} +160 -1
  23. package/dist/chunk-GFNXVZVQ.js.map +1 -0
  24. package/dist/{chunk-O4HDX5SC.js → chunk-IPVMCAW5.js} +144 -15
  25. package/dist/chunk-IPVMCAW5.js.map +1 -0
  26. package/dist/index.js +42 -2
  27. package/dist/index.js.map +1 -1
  28. package/dist/observability-metrics.d.ts +28 -0
  29. package/dist/personal-github-git-credentials.d.ts +1 -0
  30. package/dist/unhandled-rejection-boundary.d.ts +29 -0
  31. package/package.json +18 -18
  32. package/src/activities/agent-turn/agent-build.ts +15 -31
  33. package/src/activities/agent-turn/claim.ts +29 -68
  34. package/src/activities/agent-turn/codex.ts +19 -10
  35. package/src/activities/agent-turn/compaction-prep.ts +50 -1
  36. package/src/activities/agent-turn/errors.ts +78 -0
  37. package/src/activities/agent-turn/failure-settlement.ts +30 -19
  38. package/src/activities/agent-turn/finalization.ts +24 -21
  39. package/src/activities/agent-turn/history.ts +5 -1
  40. package/src/activities/agent-turn/quiescence.ts +86 -0
  41. package/src/activities/agent-turn/run-credentials.ts +12 -1
  42. package/src/activities/agent-turn/run.ts +14 -11
  43. package/src/activities/agent-turn/sandbox-establish.ts +46 -11
  44. package/src/activities/agent-turn/sandbox-route.ts +21 -25
  45. package/src/activities/agent-turn/sandbox-runtime.ts +1 -116
  46. package/src/activities/agent-turn/sandbox-shared-preparation.ts +206 -0
  47. package/src/activities/agent-turn/stream-attempt.ts +98 -14
  48. package/src/activities/agent-turn/tool-environment.ts +10 -13
  49. package/src/activities/agent-turn/tool-policy.ts +3 -55
  50. package/src/activities/agent-turn/turn-context.ts +8 -14
  51. package/src/activities/agent-turn/{image-reference-sandbox.ts → turn-sandbox-access.ts} +15 -13
  52. package/src/activities/agent-turn/video-reference-sandbox.ts +23 -0
  53. package/src/activities/codex-capacity.ts +13 -5
  54. package/src/activities/codex-rotation.ts +17 -0
  55. package/src/activities/goals.ts +10 -4
  56. package/src/activities/retained-screenshots.ts +104 -21
  57. package/src/activities/sandbox-lease.ts +7 -4
  58. package/src/activities/scheduled-tasks.ts +128 -4
  59. package/src/activities/session-state.ts +8 -1
  60. package/src/github-rest-mcp.ts +6 -3
  61. package/src/http.ts +3 -0
  62. package/src/index.ts +19 -0
  63. package/src/observability-metrics.ts +200 -0
  64. package/src/personal-github-git-credentials.ts +45 -4
  65. package/src/sandbox-resume.ts +1 -1
  66. package/src/sandbox-routing.ts +7 -4
  67. package/src/unhandled-rejection-boundary.ts +73 -0
  68. package/dist/activities/agent-turn/image-reference-sandbox.d.ts +0 -14
  69. package/dist/activities/recording.d.ts +0 -90
  70. package/dist/activities-control-VZIIV7RX.js.map +0 -1
  71. package/dist/activities-turn-5RGQDBFV.js.map +0 -1
  72. package/dist/chunk-NAVSAZ56.js.map +0 -1
  73. package/dist/chunk-O4HDX5SC.js.map +0 -1
  74. 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
  );
@@ -886,6 +927,31 @@ export async function runTurnStreamAttempt(
886
927
  ...(resolvedModel?.provider.id ? { providerId: resolvedModel.provider.id } : {}),
887
928
  });
888
929
  if (responseResult.status === "processed") {
930
+ if (
931
+ !providerPublishesNativeRequestEvents &&
932
+ fallbackProviderRequestLifecycleStartedAt !== null
933
+ ) {
934
+ const durationMs = Math.max(
935
+ 0,
936
+ performance.now() - fallbackProviderRequestLifecycleStartedAt,
937
+ );
938
+ fallbackProviderRequestLifecycleStartedAt = null;
939
+ await eventing.publish!([
940
+ {
941
+ type: "agent.model.request",
942
+ payload: {
943
+ phase: "completed",
944
+ provider: streamProvider,
945
+ durationMs: Math.round(durationMs),
946
+ turnId: activeTurnId,
947
+ attemptId: input.attemptId,
948
+ dispatchId,
949
+ executionGeneration: attempt.executionGeneration,
950
+ },
951
+ },
952
+ ]);
953
+ attempt.providerRecoveryCount = 0;
954
+ }
889
955
  const rawStreamHistory = (eventing.stream.state as { history?: unknown[] }).history;
890
956
  if (Array.isArray(rawStreamHistory)) {
891
957
  // The completed image item is normally retained from its own
@@ -1168,9 +1234,13 @@ export async function runTurnStreamAttempt(
1168
1234
  }
1169
1235
  }
1170
1236
  } catch (error) {
1171
- if (fallbackProviderRequestStartedAt !== null) {
1172
- const durationMs = Math.max(0, performance.now() - fallbackProviderRequestStartedAt);
1237
+ if (fallbackProviderRequestLifecycleStartedAt !== null) {
1238
+ const durationMs = Math.max(
1239
+ 0,
1240
+ performance.now() - fallbackProviderRequestLifecycleStartedAt,
1241
+ );
1173
1242
  fallbackProviderRequestStartedAt = null;
1243
+ fallbackProviderRequestLifecycleStartedAt = null;
1174
1244
  await eventing.publish!([
1175
1245
  {
1176
1246
  type: "agent.model.request",
@@ -1227,11 +1297,12 @@ export async function runTurnStreamAttempt(
1227
1297
  void iterator.return?.().catch(() => undefined);
1228
1298
  }
1229
1299
  }
1230
- await waitForTurnStreamCleanup(
1231
- eventing.batcher.flush(),
1232
- eventing.stream.completed.catch(() => undefined),
1233
- cancellationSignal,
1234
- );
1300
+ await assertSuccessfulAgentStreamCompletion({
1301
+ batcherFlush: eventing.batcher.flush(),
1302
+ stream: eventing.stream,
1303
+ temporalCancellationSignal: cancellationSignal,
1304
+ runtimeCancellationSignal,
1305
+ });
1235
1306
  if (
1236
1307
  options.requireTerminalModelResponse &&
1237
1308
  eventing.stream.interruptions.length === 0 &&
@@ -1247,6 +1318,7 @@ export async function runTurnStreamAttempt(
1247
1318
  throwIfTurnCancelled();
1248
1319
  throw new PostCompactionContinuationEmptyError();
1249
1320
  }
1321
+ assertAgentStreamNotCancelled(eventing.stream.cancelled);
1250
1322
  if (!streamSawPerResponseUsage) {
1251
1323
  const aggregateUsage = eventing.stream.state.usage;
1252
1324
  const normalizedAggregateUsage = normalizeModelCallUsage(aggregateUsage);
@@ -1479,7 +1551,7 @@ export async function runTurnStreamAttempt(
1479
1551
  return claimedResult({ status: "requires_action" });
1480
1552
  }
1481
1553
 
1482
- const finalOutput = String(eventing.stream.finalOutput ?? "");
1554
+ const finalOutput = String(requireAgentStreamFinalOutput(eventing.stream.finalOutput));
1483
1555
  await historySink.reconcileConversationTruth({ requireDurable: true });
1484
1556
  // Op-stream durability fence: the tool outputs are now durably in the
1485
1557
  // history store (a redispatch would NOT re-execute them), so this
@@ -1545,8 +1617,16 @@ export async function runTurnStreamAttempt(
1545
1617
  control.activityStatus = "requires_action";
1546
1618
  return claimedResult({ status: "requires_action" });
1547
1619
  }
1548
-
1549
- await prepareRunAttemptInput();
1620
+ if (
1621
+ !(await attachPendingUpdatesBeforePreparingModelInput({
1622
+ shouldAttach:
1623
+ trigger.type === "user.approvalDecision" || trigger.type === "user.humanInputResponse",
1624
+ attachPendingUpdates: attachPendingUpdatesAfterOpenSuffix,
1625
+ prepareModelInput: prepareRunAttemptInput,
1626
+ }))
1627
+ ) {
1628
+ return claimedResult({ status: "cancelled" });
1629
+ }
1550
1630
  let retriedAfterCompaction = false;
1551
1631
  while (true) {
1552
1632
  try {
@@ -1624,6 +1704,8 @@ export async function runTurnStreamAttempt(
1624
1704
  );
1625
1705
  compactionRequestCleared = landmark.requestConsumed;
1626
1706
  if (!isCompactionSummaryFailure(compactError)) throw compactError;
1707
+ const deferredSteer = await settleDeferredSteerAfterCompaction();
1708
+ if (deferredSteer) return deferredSteer;
1627
1709
  compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
1628
1710
  observability.warn("context compaction recovery compaction failed", {
1629
1711
  sessionId: input.sessionId,
@@ -1676,6 +1758,8 @@ export async function runTurnStreamAttempt(
1676
1758
  // machine updates stay pending for that actionable wake.
1677
1759
  return claimedResult({ status: "idle", deferredUntilWake: true });
1678
1760
  }
1761
+ const deferredSteer = await settleDeferredSteerAfterCompaction();
1762
+ if (deferredSteer) return deferredSteer;
1679
1763
  // Codex parity: compaction remains inside the same logical turn and
1680
1764
  // the same activity. Rebuild the model-visible history from the
1681
1765
  // durable replacement and continue the sampling loop; do not create
@@ -40,7 +40,7 @@ import type { TurnActivityServices as ActivityServices, RunAgentTurnInput } from
40
40
  import { recordTurnStartupPhase } from "../../observability-metrics";
41
41
  import { ToolResultSpill } from "./tool-result-spill";
42
42
  import { createTurnMediaArtifacts } from "./media-artifacts";
43
- import { SandboxChannelAService, type ChannelASession } from "@opengeni/runtime/sandbox";
43
+ import { SandboxChannelAService } from "@opengeni/runtime/sandbox";
44
44
  import { sandboxRunAs } from "@opengeni/runtime";
45
45
  import { type ToolAuthNeededPayload } from "@opengeni/contracts";
46
46
 
@@ -64,6 +64,7 @@ import {
64
64
  sessionTitleToolPlan,
65
65
  shouldRequestMissingSessionTitle,
66
66
  } from "./session-title";
67
+ import { resolveTurnSandboxAccess } from "./turn-sandbox-access";
67
68
 
68
69
  export type PrepareTurnToolPolicyDeps = {
69
70
  input: RunAgentTurnInput;
@@ -586,21 +587,17 @@ export async function prepareTurnToolRuntime(deps: PrepareTurnToolRuntimeDeps) {
586
587
  ) => {
587
588
  throwIfWorkerShuttingDown();
588
589
  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
- }
590
+ const sandboxAccess = await resolveTurnSandboxAccess(
591
+ sandboxState,
592
+ media.sdkOwnedSandboxSession,
593
+ "Connector attachment sandbox is unavailable",
594
+ );
595
+ const sandbox = sandboxAccess.sandbox;
599
596
  const runAs = sandboxRunAs(runSettings);
600
597
  const channel = new SandboxChannelAService({
601
- session: sessionForImport,
598
+ session: sandboxAccess.session,
602
599
  workspaceRoot: "/workspace",
603
- leaseEpoch: sandbox?.leaseEpoch ?? 0,
600
+ leaseEpoch: sandboxAccess.leaseEpoch,
604
601
  emit: async (events) => {
605
602
  await eventing.publish?.(events, true);
606
603
  },
@@ -1,8 +1,4 @@
1
- import {
2
- desktopCapableBackend,
3
- type ComputerToolMode,
4
- type LazyToolTransport,
5
- } from "@opengeni/runtime";
1
+ import { type LazyToolTransport } from "@opengeni/runtime";
6
2
  import {
7
3
  isDirectOpenAiApiBaseUrl,
8
4
  type ModelProviderApi,
@@ -12,55 +8,6 @@ import {
12
8
  import { type ModelAttachmentInputPolicy } from "../run-input";
13
9
  import { imageProviderBindingHash } from "../image-generation-operation";
14
10
 
15
- export function shouldStartOnTurnRecording(params: {
16
- recordingEnabled: boolean;
17
- desktopEnabled: boolean;
18
- establishedBackendId: string;
19
- effectiveBackend: Settings["sandboxBackend"] | undefined;
20
- }): boolean {
21
- return (
22
- params.recordingEnabled &&
23
- params.desktopEnabled &&
24
- desktopCapableBackend(params.establishedBackendId) &&
25
- params.effectiveBackend !== "selfhosted"
26
- );
27
- }
28
-
29
- /**
30
- * Decide the EXPLICIT computer-use tool transport for THIS turn.
31
- *
32
- * Computer-use is ordinary `computer_*` function tools bound to the live
33
- * desktop. The worker picks the mode from the resolved provider so the runtime
34
- * never string-sniffs the model instance:
35
- * • catalogue `inputModalities` must include `image`, or computer-use is off
36
- * • Responses wires (Azure/OpenAI, Gateway, Codex, legacy client) →
37
- * "function-image": screenshot results are structured `{type:'image'}`
38
- * • Chat Completions → "disabled": tool results on that wire are text, so a
39
- * screenshot would become a base64 string rather than an image the model sees
40
- *
41
- * Pure + exported so the mapping is unit-testable without a live turn.
42
- */
43
- export function computerToolModeForTurn(
44
- resolvedModel: {
45
- provider: { kind: RegistryProviderKind; api: ModelProviderApi };
46
- configured: { capabilities: { inputModalities: string[] } };
47
- } | null,
48
- ): ComputerToolMode {
49
- if (!resolvedModel) {
50
- return "function-image";
51
- }
52
- if (!modelSupportsImageInputForTurn(resolvedModel)) {
53
- return "disabled";
54
- }
55
- if (resolvedModel.provider.kind === "codex-subscription") {
56
- return "function-image";
57
- }
58
- if (resolvedModel.provider.api === "chat") {
59
- return "disabled";
60
- }
61
- return "function-image";
62
- }
63
-
64
11
  /** Chat wires and Gateway models do not advertise OpenAI's hosted sandbox tool types. */
65
12
  export function structuredToolTransportForTurn(
66
13
  resolvedModel: {
@@ -112,6 +59,8 @@ export function lazyToolTransportForTurn(
112
59
  export function shouldDeferNonEagerToolPreparation(args: {
113
60
  lazyToolTransport: LazyToolTransport | null;
114
61
  progressiveDisclosureEnabled: boolean;
62
+ /** Retained for call-site compatibility; the lazy runtime now gates artifact
63
+ * and ordinary function calls on the same exact preparation promise. */
115
64
  artifactRuntimeAvailable: boolean;
116
65
  triggerKind: "next" | "approval";
117
66
  triggerType: string;
@@ -119,7 +68,6 @@ export function shouldDeferNonEagerToolPreparation(args: {
119
68
  return Boolean(
120
69
  args.lazyToolTransport &&
121
70
  args.progressiveDisclosureEnabled &&
122
- !args.artifactRuntimeAvailable &&
123
71
  args.triggerKind === "next" &&
124
72
  (args.triggerType === "user.message" || args.triggerType === "system.update.delivered"),
125
73
  );
@@ -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;
@@ -110,12 +116,6 @@ export type RenewalState = {
110
116
  publishedRunCredentialNotices: Set<string>;
111
117
  };
112
118
 
113
- export type RecordingState = {
114
- activeRecording: ActiveRecording | null;
115
- computerUseRecordingStart: Promise<void> | null;
116
- didComputerUse: boolean;
117
- };
118
-
119
119
  export type EventingState = {
120
120
  heartbeatTimer: ReturnType<typeof startActivityHeartbeat> | undefined;
121
121
  batcher: ReturnType<typeof createRuntimeBatcher> | null;
@@ -174,7 +174,6 @@ export type TurnContext = {
174
174
  billingState: BillingState;
175
175
  sandboxState: SandboxRuntimeState;
176
176
  renewals: RenewalState;
177
- recordingState: RecordingState;
178
177
  eventing: EventingState;
179
178
  workspaceRefs: WorkspaceRefState;
180
179
  providerTurn: ProviderTurnState;
@@ -238,11 +237,6 @@ export function createTurnContext(input: {
238
237
  codemodeTokenRenewalClosed: false,
239
238
  publishedRunCredentialNotices: new Set(),
240
239
  },
241
- recordingState: {
242
- activeRecording: null,
243
- computerUseRecordingStart: null,
244
- didComputerUse: false,
245
- },
246
240
  eventing: {
247
241
  heartbeatTimer: undefined,
248
242
  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
+ }
@@ -11,8 +11,8 @@ import {
11
11
  } from "@opengeni/db";
12
12
  import type { Settings } from "@opengeni/config";
13
13
  import {
14
- CODEX_USAGE_EXHAUSTED_PCT,
15
14
  authoritativeCodexCapacityResetAt,
15
+ codexAccountNeedsLiveCapacityRefresh,
16
16
  isCodexCredentialEligible,
17
17
  isCodexCredentialHealthy,
18
18
  selectCodexCredentialLeaseForTurn,
@@ -119,10 +119,19 @@ export function codexCapacityDecision(
119
119
  };
120
120
  }
121
121
  const authoritativeReset = authoritativeCodexCapacityResetAt(context.accounts, now);
122
+ const hasReconcilableQuotaCooldown = context.accounts.some(
123
+ (account) =>
124
+ account.status === "active" &&
125
+ account.allocatorEnabled &&
126
+ account.exhaustedKind === "quota" &&
127
+ account.exhaustedUntil !== null &&
128
+ account.exhaustedUntil > now,
129
+ );
122
130
  return {
123
131
  kind: "unavailable",
124
132
  earliestResetAt: authoritativeReset,
125
- resetKind: authoritativeReset ? "authoritative" : "bounded_refresh",
133
+ resetKind:
134
+ authoritativeReset && !hasReconcilableQuotaCooldown ? "authoritative" : "bounded_refresh",
126
135
  diagnostic: {
127
136
  connectedCount: context.accounts.length,
128
137
  allocatorEnabledCount: context.accounts.filter((account) => account.allocatorEnabled).length,
@@ -136,13 +145,12 @@ async function refreshCapacityMetadata(
136
145
  workspaceId: string,
137
146
  ): Promise<void> {
138
147
  const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
148
+ const now = new Date();
139
149
  const stale = accounts.filter(
140
150
  (account) =>
141
151
  account.allocatorEnabled &&
142
152
  account.status === "active" &&
143
- ((account.primaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
144
- (account.secondaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
145
- account.usageCheckedAt === null),
153
+ (codexAccountNeedsLiveCapacityRefresh(account, now) || account.usageCheckedAt === null),
146
154
  );
147
155
  await refreshCodexUsageAndRepairCapacityWaiters(
148
156
  stale.map(
@@ -15,6 +15,23 @@ import type {
15
15
 
16
16
  export type CodexRotationAccount = CodexAccountStatus | CodexLeaseAccountStatus;
17
17
 
18
+ /** Capacity metadata worth an authoritative live read before/all through an idle. */
19
+ export function codexAccountNeedsLiveCapacityRefresh(
20
+ account: Pick<
21
+ CodexAccountStatus,
22
+ "primaryUsedPercent" | "secondaryUsedPercent" | "exhaustedUntil" | "exhaustedKind"
23
+ >,
24
+ now: Date,
25
+ ): boolean {
26
+ return (
27
+ (account.primaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
28
+ (account.secondaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
29
+ (account.exhaustedKind === "quota" &&
30
+ account.exhaustedUntil !== null &&
31
+ account.exhaustedUntil > now)
32
+ );
33
+ }
34
+
18
35
  export type CodexRotationStrategy =
19
36
  | "most_remaining"
20
37
  | "round_robin"
@@ -200,15 +200,21 @@ export function goalContinuationPrompt(
200
200
  "",
201
201
  ];
202
202
  return [
203
- "Continue working toward the active session goal.",
203
+ "Reconcile the active session goal against authoritative current state, then carry the work through to the full requested end state and verify it.",
204
+ "",
205
+ "Goal recovery:",
206
+ "- Treat this continuation as re-entry into the full objective, not as a request to perform one step and stop.",
207
+ "- Do not rely on previous assistant claims of progress or completion; use them only to locate authoritative evidence.",
208
+ "- If authoritative evidence already proves the full objective, call opengeni__goal_complete instead of manufacturing more work.",
209
+ "- Before repeating a state-setting action, verify whether its desired state already holds. If it does, do not repeat it; continue reconciling the overall goal.",
204
210
  "",
205
211
  "Continuation behavior:",
206
- "- This goal persists across turns. Ending this turn does not require shrinking the objective to what fits now.",
207
- "- Keep the full objective intact. If it cannot be finished now, make concrete progress toward the real requested end state, leave the goal active, and do not redefine success around a smaller or easier task.",
212
+ "- This goal persists across turns. A runtime boundary can end one turn without shrinking the objective; the next continuation resumes the same full objective.",
213
+ "- Keep working until the requested end state is true and verified. Do not end the turn merely because one useful action completed, and do not redefine success around a smaller or easier task.",
208
214
  "- Temporary rough edges are acceptable while the work is moving in the right direction. Completion still requires the requested end state to be true and verified.",
209
215
  "",
210
216
  "Work from evidence:",
211
- "Use the current workspace and external state as authoritative. Previous conversation context can help locate relevant work, but inspect the current state before relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
217
+ "Use the current workspace and external state as authoritative. Conversation context can help locate relevant work, but it is not proof of the current state. Inspect the current state before relying on it. Improve, replace, or remove existing work as needed to satisfy the actual objective.",
212
218
  "",
213
219
  "Progress visibility:",
214
220
  "If a planning tool is available and the next work is meaningfully multi-step, use it to show a concise plan tied to the real objective. Keep the plan current as steps complete or the next best action changes. Skip planning overhead for trivial one-step progress, and do not treat a plan update as a substitute for doing the work.",