@opengeni/worker-bundle 0.23.4 → 0.26.0-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 (115) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +17 -2
  2. package/dist/activities/agent-turn/agent-build.d.ts +3 -10
  3. package/dist/activities/agent-turn/claim.d.ts +8 -0
  4. package/dist/activities/agent-turn/codex.d.ts +2 -2
  5. package/dist/activities/agent-turn/credential-leases.d.ts +8 -0
  6. package/dist/activities/agent-turn/errors.d.ts +1 -1
  7. package/dist/activities/agent-turn/failure-settlement.d.ts +43 -1
  8. package/dist/activities/agent-turn/list-models.d.ts +16 -0
  9. package/dist/activities/agent-turn/model-usage.d.ts +15 -1
  10. package/dist/activities/agent-turn/quiescence.d.ts +11 -6
  11. package/dist/activities/agent-turn/run.d.ts +9 -0
  12. package/dist/activities/agent-turn/sandbox-runtime.d.ts +15 -0
  13. package/dist/activities/agent-turn/session-title.d.ts +21 -0
  14. package/dist/activities/agent-turn/stream-attempt.d.ts +4 -0
  15. package/dist/activities/agent-turn/tool-environment.d.ts +4 -13
  16. package/dist/activities/agent-turn/tool-policy.d.ts +40 -5
  17. package/dist/activities/agent-turn/turn-context.d.ts +13 -2
  18. package/dist/activities/agent-turn.d.ts +1 -1
  19. package/dist/activities/automations.d.ts +1 -0
  20. package/dist/activities/capabilities.d.ts +3 -1
  21. package/dist/activities/codemode-dispatcher.d.ts +10 -1
  22. package/dist/activities/codex-capacity.d.ts +32 -3
  23. package/dist/activities/codex-fleet-shadow.d.ts +1 -1
  24. package/dist/activities/codex-image-generation.d.ts +1 -1
  25. package/dist/activities/codex-rotation.d.ts +8 -67
  26. package/dist/activities/context-compaction.d.ts +2 -0
  27. package/dist/activities/goals.d.ts +12 -1
  28. package/dist/activities/google-drive-publication.d.ts +2 -13
  29. package/dist/activities/image-generation-operation.d.ts +13 -4
  30. package/dist/activities/image-generation-references.d.ts +2 -1
  31. package/dist/activities/scheduled-tasks.d.ts +1 -0
  32. package/dist/activities/session-state.d.ts +4 -2
  33. package/dist/activities/types.d.ts +19 -1
  34. package/dist/{activities-control-52W5IDWM.js → activities-control-CTEMLTHG.js} +339 -195
  35. package/dist/activities-control-CTEMLTHG.js.map +1 -0
  36. package/dist/activities-control.d.ts +4 -0
  37. package/dist/{activities-turn-A2G2KLSY.js → activities-turn-S5IU347A.js} +1846 -1136
  38. package/dist/activities-turn-S5IU347A.js.map +1 -0
  39. package/dist/activities.d.ts +4 -0
  40. package/dist/{chunk-GFNXVZVQ.js → chunk-D4ZEFOYH.js} +107 -7
  41. package/dist/chunk-D4ZEFOYH.js.map +1 -0
  42. package/dist/{chunk-GRFQTTSQ.js → chunk-GDCLDKSH.js} +7 -3
  43. package/dist/{chunk-GRFQTTSQ.js.map → chunk-GDCLDKSH.js.map} +1 -1
  44. package/dist/{chunk-IPVMCAW5.js → chunk-TAUE3GYT.js} +225 -154
  45. package/dist/chunk-TAUE3GYT.js.map +1 -0
  46. package/dist/github-rest-mcp.d.ts +0 -2
  47. package/dist/index.d.ts +2 -0
  48. package/dist/index.js +43 -7
  49. package/dist/index.js.map +1 -1
  50. package/dist/observability-metrics.d.ts +35 -8
  51. package/dist/sandbox-reaper-timeout.d.ts +2 -2
  52. package/dist/sandbox-resume.d.ts +6 -1
  53. package/dist/workflow-bundle.js +96 -10
  54. package/dist/workflows/activities.d.ts +6 -1
  55. package/dist/workflows/automations.d.ts +5 -0
  56. package/dist/workflows/session.d.ts +3 -0
  57. package/package.json +17 -17
  58. package/src/activities/agent-run-admission.ts +7 -1
  59. package/src/activities/agent-turn/admission.ts +47 -6
  60. package/src/activities/agent-turn/agent-build.ts +20 -26
  61. package/src/activities/agent-turn/claim.ts +59 -6
  62. package/src/activities/agent-turn/codex-capacity.ts +311 -234
  63. package/src/activities/agent-turn/codex.ts +2 -2
  64. package/src/activities/agent-turn/compaction-prep.ts +8 -1
  65. package/src/activities/agent-turn/credential-leases.ts +36 -0
  66. package/src/activities/agent-turn/errors.ts +29 -1
  67. package/src/activities/agent-turn/failure-settlement.ts +451 -323
  68. package/src/activities/agent-turn/finalization.ts +31 -17
  69. package/src/activities/agent-turn/list-models.ts +101 -0
  70. package/src/activities/agent-turn/model-usage.ts +120 -45
  71. package/src/activities/agent-turn/quiescence.ts +32 -17
  72. package/src/activities/agent-turn/run.ts +98 -9
  73. package/src/activities/agent-turn/sandbox-runtime.ts +78 -81
  74. package/src/activities/agent-turn/session-title.ts +55 -2
  75. package/src/activities/agent-turn/stream-attempt.ts +281 -136
  76. package/src/activities/agent-turn/tool-environment.ts +132 -39
  77. package/src/activities/agent-turn/tool-policy.ts +71 -12
  78. package/src/activities/agent-turn/turn-context.ts +21 -2
  79. package/src/activities/agent-turn.ts +5 -1
  80. package/src/activities/automations.ts +122 -67
  81. package/src/activities/capabilities.ts +129 -2
  82. package/src/activities/codemode-dispatcher.ts +235 -59
  83. package/src/activities/codex-capacity.ts +89 -10
  84. package/src/activities/codex-fleet-shadow.ts +8 -1
  85. package/src/activities/codex-image-generation.ts +19 -2
  86. package/src/activities/codex-rotation.ts +45 -142
  87. package/src/activities/context-compaction.ts +3 -0
  88. package/src/activities/goals.ts +116 -39
  89. package/src/activities/google-drive-publication.ts +4 -81
  90. package/src/activities/image-generation-operation.ts +28 -3
  91. package/src/activities/image-generation-references.ts +9 -9
  92. package/src/activities/mcp-credentials.ts +53 -0
  93. package/src/activities/rig-verification.ts +5 -3
  94. package/src/activities/sandbox-lease.ts +29 -12
  95. package/src/activities/scheduled-tasks.ts +217 -110
  96. package/src/activities/session-state.ts +17 -0
  97. package/src/activities/site-auth-maintenance.ts +12 -12
  98. package/src/activities/types.ts +22 -8
  99. package/src/activities.ts +2 -0
  100. package/src/activity-services.ts +4 -1
  101. package/src/github-rest-mcp.ts +2 -83
  102. package/src/index.ts +34 -4
  103. package/src/observability-metrics.ts +158 -14
  104. package/src/sandbox-reaper-timeout.ts +10 -4
  105. package/src/sandbox-resume.ts +58 -8
  106. package/src/sandbox-routing.ts +71 -16
  107. package/src/workflows/activities.ts +16 -0
  108. package/src/workflows/automations.ts +21 -2
  109. package/src/workflows/session.ts +85 -7
  110. package/dist/activities/api-integrations.d.ts +0 -21
  111. package/dist/activities-control-52W5IDWM.js.map +0 -1
  112. package/dist/activities-turn-A2G2KLSY.js.map +0 -1
  113. package/dist/chunk-GFNXVZVQ.js.map +0 -1
  114. package/dist/chunk-IPVMCAW5.js.map +0 -1
  115. package/src/activities/api-integrations.ts +0 -141
@@ -22,7 +22,10 @@ import {
22
22
  withCodexRequestOverrides,
23
23
  type CodexRequestContext,
24
24
  } from "@opengeni/codex";
25
- import { xaiSubscriptionRequestStorage } from "@opengeni/xai-subscription";
25
+ import {
26
+ xaiSubscriptionRequestStorage,
27
+ type XaiSubscriptionRequestContext,
28
+ } from "@opengeni/xai-subscription";
26
29
  import { buildXaiTurnRequestAuthorization } from "../xai-auth";
27
30
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
28
31
  import { currentActivityContext } from "../streaming";
@@ -79,6 +82,54 @@ import { prepareTurnToolPolicy, prepareTurnToolRuntime } from "./tool-environmen
79
82
  import { applyTurnGitHubRepositoryBindings } from "./github-repository-bindings";
80
83
  import { buildTurnAgent } from "./agent-build";
81
84
 
85
+ /**
86
+ * Retain subscription credential/account authority for the title sidecar
87
+ * without sharing main-stream recovery, startup, audit, or opaque-artifact
88
+ * callbacks. Title usage is returned by the runtime and metered separately.
89
+ */
90
+ export function sessionTitleCodexRequestContext(
91
+ context: CodexRequestContext,
92
+ nextRequestId: () => string,
93
+ ): CodexRequestContext {
94
+ return {
95
+ clientVersion: context.clientVersion,
96
+ ...(context.sessionId !== undefined ? { sessionId: context.sessionId } : {}),
97
+ getToken: context.getToken,
98
+ refresh: context.refresh,
99
+ resolveModel: context.resolveModel,
100
+ ...(context.onUsageHeaders ? { onUsageHeaders: context.onUsageHeaders } : {}),
101
+ ...(context.beforeProviderDispatch
102
+ ? { beforeProviderDispatch: context.beforeProviderDispatch }
103
+ : {}),
104
+ ...(context.responseTimeoutPolicy
105
+ ? { responseTimeoutPolicy: context.responseTimeoutPolicy }
106
+ : {}),
107
+ nextRequestId,
108
+ turnMetadata: { request_kind: "session_title" },
109
+ };
110
+ }
111
+
112
+ export function sessionTitleXaiRequestContext(
113
+ context: XaiSubscriptionRequestContext,
114
+ nextRequestId: () => string,
115
+ ): XaiSubscriptionRequestContext {
116
+ return {
117
+ clientVersion: context.clientVersion,
118
+ sessionId: context.sessionId,
119
+ turnId: context.turnId,
120
+ getToken: context.getToken,
121
+ refresh: context.refresh,
122
+ resolveModel: context.resolveModel,
123
+ ...(context.streamIdleTimeoutMs !== undefined
124
+ ? { streamIdleTimeoutMs: context.streamIdleTimeoutMs }
125
+ : {}),
126
+ ...(context.hostedToolContinuationTimeoutMs !== undefined
127
+ ? { hostedToolContinuationTimeoutMs: context.hostedToolContinuationTimeoutMs }
128
+ : {}),
129
+ nextRequestId,
130
+ };
131
+ }
132
+
82
133
  /** Lifecycle orchestrator: claim → capacity → governance → sandbox → tools → stream. */
83
134
  export function createRunAgentTurnActivity(services: () => Promise<ActivityServices>) {
84
135
  const modelCheckpointMemoryCollector = createModelCheckpointMemoryCollector();
@@ -89,6 +140,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
89
140
  return async function runAgentTurn(input: RunAgentTurnInput): Promise<RunAgentTurnResult> {
90
141
  const {
91
142
  settings,
143
+ catalogSourceSettings = settings,
92
144
  db,
93
145
  bus,
94
146
  runtime,
@@ -302,6 +354,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
302
354
  const claimed = await claimTurnAttempt({
303
355
  input,
304
356
  settings,
357
+ catalogSourceSettings,
305
358
  db,
306
359
  bus,
307
360
  runtime,
@@ -464,6 +517,13 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
464
517
  input.workspaceId,
465
518
  providerTurn.effectiveCodexCredentialId ?? "",
466
519
  );
520
+ const resolveTrackedToken = async (
521
+ resolve: () => ReturnType<typeof resolver.getToken>,
522
+ ) => {
523
+ const token = await resolve();
524
+ providerTurn.effectiveCodexCredentialVersion = token.credentialVersion;
525
+ return token;
526
+ };
467
527
  return {
468
528
  clientVersion: CODEX_CLIENT_VERSION,
469
529
  // Backend sticky cache-routing key — the SAME id as the body's
@@ -473,8 +533,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
473
533
  // (per-request shard lottery = prod's measured 48.6% on sol);
474
534
  // with it, resends pin to the warm shard (Codex CLI parity).
475
535
  sessionId: input.sessionId,
476
- getToken: () => resolver.getToken(),
477
- refresh: () => resolver.refresh(),
536
+ getToken: () => resolveTrackedToken(resolver.getToken),
537
+ refresh: () => resolveTrackedToken(resolver.refresh),
478
538
  resolveModel: buildModelResolver(
479
539
  CODEX_FALLBACK_MODEL_SLUGS,
480
540
  CODEX_FALLBACK_MODEL_SLUGS[0],
@@ -482,6 +542,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
482
542
  onUsageHeaders: (snapshot) => {
483
543
  providerTurn.latestCodexUsage = snapshot;
484
544
  }, // latest wins; flushed once in finally
545
+ beforeProviderDispatch: () => {
546
+ leases.codex.assertUsable();
547
+ },
485
548
  onRequestPreparationDiagnostic: (phase) => {
486
549
  if (
487
550
  eventing.firstModelRequestCheckpointAt === null ||
@@ -778,6 +841,26 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
778
841
  providerTurn.xaiRequestContext
779
842
  ? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn)
780
843
  : withCodex(fn);
844
+ let codexSessionTitleRequestSequence = 0;
845
+ let xaiSessionTitleRequestSequence = 0;
846
+ const codexSessionTitleContext = codexContext
847
+ ? sessionTitleCodexRequestContext(
848
+ codexContext,
849
+ () => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`,
850
+ )
851
+ : null;
852
+ const xaiSessionTitleContext = providerTurn.xaiRequestContext
853
+ ? sessionTitleXaiRequestContext(
854
+ providerTurn.xaiRequestContext,
855
+ () => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`,
856
+ )
857
+ : null;
858
+ const withSessionTitleProviderRequestContext = <T>(fn: () => Promise<T>): Promise<T> =>
859
+ xaiSessionTitleContext
860
+ ? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn)
861
+ : codexSessionTitleContext
862
+ ? codexRequestStorage.run(codexSessionTitleContext, fn)
863
+ : fn();
781
864
  const withCodexRemoteCompaction = <T>(fn: () => Promise<T>): Promise<T> =>
782
865
  withCodex(() =>
783
866
  withCodexRequestOverrides(
@@ -796,7 +879,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
796
879
  );
797
880
  const compactionPrep = await prepareCompaction({
798
881
  input,
799
- settings,
882
+ settings: capabilitySettings,
800
883
  db,
801
884
  bus,
802
885
  observability,
@@ -1062,6 +1145,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1062
1145
  ];
1063
1146
  const toolRuntime = await prepareTurnToolRuntime({
1064
1147
  input,
1148
+ catalogSourceSettings,
1065
1149
  db,
1066
1150
  bus,
1067
1151
  runtime,
@@ -1100,7 +1184,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1100
1184
  });
1101
1185
  const {
1102
1186
  attemptConnectorActionBindings,
1103
- connectorActionIdentity,
1187
+ connectorActionPolicy,
1188
+ generateSessionTitleInParallel,
1104
1189
  postToolPreparationStartedAt,
1105
1190
  preparationIndependentToolNames,
1106
1191
  } = toolRuntime;
@@ -1154,7 +1239,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1154
1239
  sandboxCodemodeToken,
1155
1240
  fileResourceDownloads,
1156
1241
  attemptConnectorActionBindings,
1157
- connectorActionIdentity,
1242
+ connectorActionPolicy,
1243
+ trigger,
1158
1244
  preparationIndependentToolNames,
1159
1245
  videoGenerationAcceptancesByCallId,
1160
1246
  activeSandboxBackend,
@@ -1243,7 +1329,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1243
1329
  };
1244
1330
  const postAgentCompaction = await runPostAgentCompaction({
1245
1331
  input,
1246
- settings,
1332
+ settings: capabilitySettings,
1247
1333
  db,
1248
1334
  bus,
1249
1335
  observability,
@@ -1408,8 +1494,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1408
1494
  );
1409
1495
  return await runTurnStreamAttempt({
1410
1496
  input,
1411
- settings,
1497
+ settings: capabilitySettings,
1412
1498
  db,
1499
+ bus,
1413
1500
  runtime,
1414
1501
  objectStorage,
1415
1502
  observability,
@@ -1440,6 +1527,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1440
1527
  attachCodemodeTokenRenewal,
1441
1528
  attachRunCredentialRenewal,
1442
1529
  withProviderRequestContext,
1530
+ withSessionTitleProviderRequestContext,
1443
1531
  publishCompactionLiveEvents,
1444
1532
  publishCompactionOutcomeEvents,
1445
1533
  recordCompanyBrainContributionReceiptOnce,
@@ -1465,6 +1553,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1465
1553
  providerApi,
1466
1554
  runSettings,
1467
1555
  turnTools,
1556
+ generateSessionTitleInParallel,
1557
+ sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
1468
1558
  compactSummarizer,
1469
1559
  settleDeferredSteerAfterCompaction,
1470
1560
  compactionModelHistoryProjector,
@@ -1489,7 +1579,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
1489
1579
  bus,
1490
1580
  observability,
1491
1581
  wakeSessionWorkflow,
1492
- signalCodexCapacityWorkflow,
1493
1582
  cancellationSignal,
1494
1583
  sandboxRotationController,
1495
1584
  noteCancellationRequested,
@@ -17,12 +17,7 @@ import {
17
17
  } from "@opengeni/config";
18
18
  import { createRuntimeBatcher, currentActivityContext } from "../streaming";
19
19
  import type { TurnActivityServices as ActivityServices, RunAgentTurnInput } from "../types";
20
- import {
21
- maybePersistWarmWorkspaceSnapshot,
22
- persistSandboxDeadlineRotationCheckpoint,
23
- waitForWarmSnapshot,
24
- type ResumedTurnSandbox,
25
- } from "../../sandbox-resume";
20
+ import { maybePersistWarmWorkspaceSnapshot, type ResumedTurnSandbox } from "../../sandbox-resume";
26
21
  import { recordCreditMicros } from "../../observability-metrics";
27
22
  import { ChannelAPartialMutationError } from "@opengeni/runtime/sandbox";
28
23
 
@@ -51,6 +46,27 @@ export type SandboxTurnRuntimeDeps = {
51
46
 
52
47
  export type SandboxTurnRuntime = ReturnType<typeof createSandboxTurnRuntime>;
53
48
 
49
+ /**
50
+ * Provider-deadline rotation is a preemption boundary, not a cooperative
51
+ * snapshot boundary. Snapshotting while this attempt still owns active tools
52
+ * can deadlock on its own unsettled mutation admission or on an earlier native
53
+ * capture. Abort first; the ordinary attempt finalizer then drains every writer
54
+ * and releases the holder, after which the zero-holder reaper owns the exact
55
+ * generation capture and same-request takeover protocol.
56
+ */
57
+ export function preemptSandboxTurnForDeadlineRotation(input: {
58
+ controller: AbortController;
59
+ sandboxState: Pick<SandboxRuntimeState, "deadlineRotationRequested">;
60
+ sandboxGroupId: string;
61
+ leaseEpoch: number;
62
+ cancellationSignal?: AbortSignal;
63
+ }): boolean {
64
+ if (input.controller.signal.aborted || input.cancellationSignal?.aborted) return false;
65
+ input.sandboxState.deadlineRotationRequested = true;
66
+ input.controller.abort(new SandboxDeadlineRotationError(input.sandboxGroupId, input.leaseEpoch));
67
+ return true;
68
+ }
69
+
54
70
  export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
55
71
  const {
56
72
  input,
@@ -235,11 +251,12 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
235
251
  sandboxState.sandboxGroupId
236
252
  ) {
237
253
  // The lease still matches this turn's exact epoch/instance but a
238
- // rotation was requested. Start the same orderly checkpoint the
239
- // heartbeat would start on its next tick, so the model is not fed
240
- // retry errors until then. Best-effort and single-flight; the
241
- // admission failure itself is still surfaced to the caller.
242
- void beginRotationCheckpoint(
254
+ // rotation was requested. Start the same immediate preemption the
255
+ // heartbeat would start on its next tick, so this attempt cannot keep
256
+ // the provider generation pinned behind its own blocked mutation.
257
+ // Best-effort and single-flight; the admission failure itself is still
258
+ // surfaced to the caller.
259
+ void beginRotationPreemption(
243
260
  sandbox,
244
261
  sandbox.leaseEpoch,
245
262
  sandboxState.sandboxGroupId,
@@ -387,85 +404,60 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
387
404
  });
388
405
  };
389
406
  /**
390
- * Start (single-flight) the orderly provider-deadline rotation checkpoint for
391
- * the established box. Entered from the lease heartbeat once the lease is
392
- * rotation-fenced, and from a workspace-mutation admission that observed
393
- * `rotation_in_progress` at the same epoch/instance, so the model is not fed
394
- * retry errors for a whole heartbeat interval before the checkpoint runs.
407
+ * Start (single-flight) provider-deadline preemption for the established box.
408
+ * Entered from the lease heartbeat once the lease is rotation-fenced, and
409
+ * from a workspace-mutation admission that observed `rotation_in_progress` at
410
+ * the same epoch/instance. The active attempt is aborted immediately instead
411
+ * of waiting behind a turn-side snapshot: finalization first drains every
412
+ * writer and then releases the holder, licensing the zero-holder reaper's
413
+ * authoritative capture/takeover/termination protocol.
414
+ *
395
415
  * `not_rotating` means the exact epoch/instance no longer carries a requested
396
416
  * rotation (holder reaped, attempt closed, epoch superseded, or draining).
397
417
  */
398
- const beginRotationCheckpoint = async (
418
+ const beginRotationPreemption = async (
399
419
  sandbox: ResumedTurnSandbox,
400
420
  rotationEpoch: number,
401
421
  rotationGroupId: string,
402
422
  ): Promise<"started" | "busy" | "not_rotating"> => {
403
- // A cancelled attempt never starts (or reinstates a holder for) a
404
- // checkpoint; its cancellation settlement owns the holder from here.
423
+ // A cancelled attempt never starts another preemption check; its
424
+ // cancellation settlement owns the holder from here.
405
425
  if (
406
- sandboxState.rotationInFlight ||
426
+ sandboxState.rotationPreemptionInFlight ||
407
427
  sandboxRotationController.signal.aborted ||
408
428
  cancellationSignal?.aborted
409
429
  ) {
410
430
  return "busy";
411
431
  }
412
- const rotatingLease = await readLease(db, input.workspaceId, rotationGroupId).catch(() => null);
413
- if (
414
- !rotatingLease ||
415
- rotatingLease.leaseEpoch !== rotationEpoch ||
416
- rotatingLease.instanceId !== sandbox.established.instanceId ||
417
- rotatingLease.rotationRequestedAt === null
418
- ) {
419
- return "not_rotating";
432
+ if (sandboxState.rotationPreemptionInFlight || sandboxRotationController.signal.aborted) {
433
+ return "busy";
420
434
  }
421
- if (sandboxState.rotationInFlight || sandboxRotationController.signal.aborted) return "busy";
422
- sandboxState.rotationInFlight = (async () => {
423
- // Rotation admission already fenced all new workspace mutations.
424
- // Wait for an earlier periodic capture, then produce the exact
425
- // generation-complete checkpoint that licenses aborting this run.
426
- if (sandboxState.snapshotInFlight) {
427
- await waitForWarmSnapshot(
428
- sandboxState.snapshotInFlight,
429
- settings.sandboxSnapshotTimeoutMs,
430
- cancellationSignal,
431
- );
432
- }
433
- const snapshotSession = sandboxState.setupBoxSession;
434
- const snapshotTurnId = attempt.turnId;
435
- if (!snapshotSession || !snapshotTurnId) return;
436
- // The checkpoint helper first reinstates this turn's exact holder
437
- // when it was lost at the same epoch/instance (defense in depth:
438
- // the warm capture requires it), then forces the capture and
439
- // reports whether the established epoch now carries a complete
440
- // archive under its requested rotation.
441
- const checkpoint = await persistSandboxDeadlineRotationCheckpoint(
442
- { db, settings, objectStorage, observability },
443
- {
444
- accountId: input.accountId,
445
- workspaceId: input.workspaceId,
446
- sessionId: input.sessionId,
447
- turnId: snapshotTurnId,
448
- attemptId: input.attemptId,
449
- sandboxGroupId: rotationGroupId,
450
- },
451
- snapshotSession,
452
- { leaseEpoch: rotationEpoch, instanceId: sandbox.established.instanceId },
453
- cancellationSignal,
454
- );
455
- if (checkpoint.checkpointed) {
456
- sandboxRotationController.abort(
457
- new SandboxDeadlineRotationError(rotationGroupId, rotationEpoch),
458
- );
435
+ let disposition: "started" | "not_rotating" = "started";
436
+ sandboxState.rotationPreemptionInFlight = (async () => {
437
+ const rotatingLease = await readLease(db, input.workspaceId, rotationGroupId);
438
+ if (
439
+ rotatingLease?.leaseEpoch !== rotationEpoch ||
440
+ rotatingLease.instanceId !== sandbox.established.instanceId ||
441
+ rotatingLease.rotationRequestedAt === null
442
+ ) {
443
+ disposition = "not_rotating";
459
444
  return;
460
445
  }
461
- if (checkpoint.holder === "attempt_fenced" || checkpoint.holder === "lease_fenced") {
462
- // This attempt is no longer the active writer, or the exact
463
- // epoch/instance is gone. Nothing left to checkpoint here.
464
- stopLeaseHeartbeat();
465
- }
446
+
447
+ // Close the periodic producer before marking the turn for preemption.
448
+ // A tick that already launched a capture is joined by finalization via
449
+ // the capture promise's physical-settlement handle.
450
+ stopLeaseHeartbeat();
451
+ preemptSandboxTurnForDeadlineRotation({
452
+ controller: sandboxRotationController,
453
+ sandboxState,
454
+ sandboxGroupId: rotationGroupId,
455
+ leaseEpoch: rotationEpoch,
456
+ ...(cancellationSignal ? { cancellationSignal } : {}),
457
+ });
466
458
  })()
467
459
  .catch((error) => {
468
- observability.warn("sandbox deadline rotation checkpoint failed; retrying", {
460
+ observability.warn("sandbox deadline rotation preemption check failed; retrying", {
469
461
  workspaceId: input.workspaceId,
470
462
  sessionId: input.sessionId,
471
463
  sandboxGroupId: rotationGroupId,
@@ -474,10 +466,10 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
474
466
  });
475
467
  })
476
468
  .finally(() => {
477
- sandboxState.rotationInFlight = null;
469
+ sandboxState.rotationPreemptionInFlight = null;
478
470
  });
479
- await sandboxState.rotationInFlight;
480
- return "started";
471
+ await sandboxState.rotationPreemptionInFlight;
472
+ return disposition;
481
473
  };
482
474
  const startLeaseHeartbeat = (
483
475
  sandbox: ResumedTurnSandbox,
@@ -525,7 +517,7 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
525
517
  })
526
518
  .then(async (alive) => {
527
519
  if (alive) return;
528
- const rotation = await beginRotationCheckpoint(sandbox, heartbeatEpoch, heartbeatGroupId);
520
+ const rotation = await beginRotationPreemption(sandbox, heartbeatEpoch, heartbeatGroupId);
529
521
  if (rotation === "not_rotating") {
530
522
  // The holder was reaped, the exact attempt closed, the epoch was
531
523
  // superseded, or the lease began draining. Do not leave a dead
@@ -562,9 +554,10 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
562
554
  firstProviderRequestStarted: sandboxState.firstProviderRequestStarted,
563
555
  snapshotInFlight: Boolean(sandboxState.snapshotInFlight),
564
556
  turnEndCaptureInProgress: sandboxState.turnEndCaptureInProgress,
557
+ deadlineRotationRequested: sandboxState.deadlineRotationRequested,
565
558
  })
566
559
  ) {
567
- sandboxState.snapshotInFlight = maybePersistWarmWorkspaceSnapshot(
560
+ const snapshot = maybePersistWarmWorkspaceSnapshot(
568
561
  { db, settings, objectStorage },
569
562
  {
570
563
  accountId: input.accountId,
@@ -577,7 +570,9 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
577
570
  snapshotSession,
578
571
  heartbeatEpoch,
579
572
  activityContext?.cancellationSignal,
580
- )
573
+ );
574
+ sandboxState.snapshotInFlight = snapshot.settled;
575
+ void snapshot
581
576
  .then(async (persisted) => {
582
577
  if (persisted && eventing.publish) {
583
578
  await eventing.publish([
@@ -588,10 +583,12 @@ export function createSandboxTurnRuntime(deps: SandboxTurnRuntimeDeps) {
588
583
  ]);
589
584
  }
590
585
  })
591
- .catch(() => undefined)
592
- .finally(() => {
586
+ .catch(() => undefined);
587
+ void snapshot.settled.then(() => {
588
+ if (sandboxState.snapshotInFlight === snapshot.settled) {
593
589
  sandboxState.snapshotInFlight = null;
594
- });
590
+ }
591
+ });
595
592
  }
596
593
  }, 10_000);
597
594
  if (
@@ -1,5 +1,6 @@
1
1
  import { hasPermission } from "@opengeni/core";
2
2
  import type { AttemptToolDefinition } from "@opengeni/codemode";
3
+ import type { GeneratedSessionTitle } from "@opengeni/runtime";
3
4
  import {
4
5
  AUTOMATIC_SESSION_TITLE_FALLBACK,
5
6
  DEFAULT_FIRST_PARTY_MCP_PERMISSIONS,
@@ -32,23 +33,75 @@ export function sessionTitleToolPlan(input: {
32
33
  tools: readonly ToolRef[];
33
34
  selectedFirstPartyMcpTools: readonly FirstPartyMcpToolName[];
34
35
  shouldRequestTitle: boolean;
36
+ parallelGenerationAvailable: boolean;
35
37
  }): {
36
38
  promoteTitleTool: boolean;
39
+ generateTitleInParallel: boolean;
37
40
  remoteFirstPartyMcpTools: FirstPartyMcpToolName[];
38
41
  preparationIndependentToolNames: string[];
39
42
  } {
40
- const promoteTitleTool =
43
+ const titleToolAvailable =
41
44
  input.shouldRequestTitle &&
42
45
  input.tools.some((tool) => tool.kind === "mcp" && tool.id === "opengeni");
46
+ const generateTitleInParallel = titleToolAvailable && input.parallelGenerationAvailable;
47
+ const promoteTitleTool = titleToolAvailable && !generateTitleInParallel;
43
48
  return {
44
49
  promoteTitleTool,
45
- remoteFirstPartyMcpTools: promoteTitleTool
50
+ generateTitleInParallel,
51
+ remoteFirstPartyMcpTools: titleToolAvailable
46
52
  ? input.selectedFirstPartyMcpTools.filter((tool) => tool !== "set_session_title")
47
53
  : [...input.selectedFirstPartyMcpTools],
48
54
  preparationIndependentToolNames: promoteTitleTool ? [SESSION_TITLE_MODEL_TOOL_NAME] : [],
49
55
  };
50
56
  }
51
57
 
58
+ export const PARALLEL_SESSION_TITLE_TIMEOUT_MS = 15_000;
59
+
60
+ export type ParallelSessionTitleGeneration = {
61
+ finish: () => Promise<GeneratedSessionTitle | null>;
62
+ cancel: () => Promise<void>;
63
+ };
64
+
65
+ /**
66
+ * Start title inference immediately and keep it independent of the main agent
67
+ * stream. finish() waits for the already-running bounded request, so a quick
68
+ * main response does not discard a valid title merely because it completed
69
+ * first. cancel() aborts and joins exceptional/cancelled exits so no provider
70
+ * work escapes the owning runAgentTurn activity.
71
+ */
72
+ export function startParallelSessionTitleGeneration(input: {
73
+ generate: (signal: AbortSignal) => Promise<GeneratedSessionTitle>;
74
+ signal?: AbortSignal;
75
+ timeoutMs?: number;
76
+ onError?: (error: unknown) => void;
77
+ }): ParallelSessionTitleGeneration {
78
+ const cancellationController = new AbortController();
79
+ const timeoutSignal = AbortSignal.timeout(input.timeoutMs ?? PARALLEL_SESSION_TITLE_TIMEOUT_MS);
80
+ const signals = [cancellationController.signal, timeoutSignal];
81
+ if (input.signal) signals.push(input.signal);
82
+ const signal = AbortSignal.any(signals);
83
+ const generation = input
84
+ .generate(signal)
85
+ .then((result) => (signal.aborted ? null : result))
86
+ .catch((error: unknown) => {
87
+ if (!signal.aborted) input.onError?.(error);
88
+ return null;
89
+ });
90
+ let finished: Promise<GeneratedSessionTitle | null> | null = null;
91
+
92
+ return {
93
+ finish: () => {
94
+ if (finished) return finished;
95
+ finished = generation;
96
+ return finished;
97
+ },
98
+ cancel: async () => {
99
+ cancellationController.abort();
100
+ await generation;
101
+ },
102
+ };
103
+ }
104
+
52
105
  export function createSessionTitleAttemptToolDefinition(input: {
53
106
  updateTitle: (title: string) => Promise<{ updated: boolean; title: string | null }>;
54
107
  }): AttemptToolDefinition {