@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
@@ -3,13 +3,13 @@ import {
3
3
  PostCompactionContinuationEmptyError,
4
4
  SandboxDeadlineRotationError,
5
5
  agentRunFailurePayload,
6
+ armAndReconcileCodexCapacityWait,
6
7
  armTurnQuiescenceWatchdog,
7
8
  assertAgentStreamNotCancelled,
8
9
  assertPhysicalToolQuiescenceForCancellation,
9
10
  assertSessionAttemptQuiescenceRecoveryDurable,
10
11
  assertSuccessfulAgentStreamCompletion,
11
12
  authoritativeCodexCapacityResetAt,
12
- chooseRotationActive,
13
13
  classifyCodexCredentialFailure,
14
14
  classifyCodexPin,
15
15
  classifyContextWindowOverflowError,
@@ -22,7 +22,6 @@ import {
22
22
  compactionFailureReason,
23
23
  compactionFailureReasonFromError,
24
24
  computeIdleDelayMs,
25
- computeReactiveRotationResume,
26
25
  createRuntimeBatcher,
27
26
  createSharedActivityServices,
28
27
  createTurnSandboxProvisioner,
@@ -30,7 +29,6 @@ import {
30
29
  deliverChildRequiresActionToParent,
31
30
  deliverFailedChildTurnToParent,
32
31
  drainAttemptOwnedSandboxWriters,
33
- earliestCodexReset,
34
32
  escapedMcpTimeoutRecoveryFailure,
35
33
  finalizeDurableTurnOpStreams,
36
34
  isCodexCredentialEligible,
@@ -41,7 +39,6 @@ import {
41
39
  mergeResourceRefs,
42
40
  nextStreamEvent,
43
41
  persistOrSignalSessionAttemptQuiescence,
44
- persistSandboxDeadlineRotationCheckpoint,
45
42
  postClaimDatabaseRecoveryFailure,
46
43
  preClaimAdmissionFailure,
47
44
  providerRecoveryCountAfterModelRequestPhase,
@@ -65,7 +62,6 @@ import {
65
62
  sandboxRouteTransitionCode,
66
63
  selectCodexCredentialLeaseForTurn,
67
64
  settingsWithPackSandboxImage,
68
- shardCredentialForSession,
69
65
  shouldRecoverCompactionProviderFailure,
70
66
  shouldRunTurnEndWorkspacePersistence,
71
67
  shouldStartPeriodicWorkspaceSnapshot,
@@ -81,7 +77,7 @@ import {
81
77
  waitForTurnOperation,
82
78
  waitForWarmSnapshot,
83
79
  withFirstPartyTools
84
- } from "./chunk-IPVMCAW5.js";
80
+ } from "./chunk-TAUE3GYT.js";
85
81
  import {
86
82
  StreamTimingMetrics,
87
83
  makeMachineOpObserver,
@@ -90,6 +86,7 @@ import {
90
86
  recordBatchFlush,
91
87
  recordCompanyBrainContributions,
92
88
  recordContextCompaction,
89
+ recordContextCompactionStarted,
93
90
  recordCreditMicros,
94
91
  recordModelCacheTokens,
95
92
  recordModelInputTokens,
@@ -106,7 +103,7 @@ import {
106
103
  recordTurnWorkerPreparationTotal,
107
104
  runtimeMetricsHooksForObservability,
108
105
  turnLifecycleMetricsFor
109
- } from "./chunk-GFNXVZVQ.js";
106
+ } from "./chunk-D4ZEFOYH.js";
110
107
 
111
108
  // src/activities-turn.ts
112
109
  import { createProductionAgentRuntime, summarizeForCompaction as summarizeForCompaction2 } from "@opengeni/runtime";
@@ -136,7 +133,9 @@ import {
136
133
  codexRequestStorage,
137
134
  withCodexRequestOverrides
138
135
  } from "@opengeni/codex";
139
- import { xaiSubscriptionRequestStorage } from "@opengeni/xai-subscription";
136
+ import {
137
+ xaiSubscriptionRequestStorage
138
+ } from "@opengeni/xai-subscription";
140
139
 
141
140
  // src/activities/xai-auth.ts
142
141
  import { environmentsEncryptionKeyBytes } from "@opengeni/config";
@@ -1257,12 +1256,21 @@ function codexCredentialLeaseDeadlineExpired(confirmedUntilMs, nowMs = performan
1257
1256
  }
1258
1257
 
1259
1258
  // src/activities/agent-turn/credential-leases.ts
1259
+ var CodexCredentialLeaseLostError = class extends Error {
1260
+ constructor(reason) {
1261
+ super("Codex credential lease is not usable for provider dispatch");
1262
+ this.reason = reason;
1263
+ this.name = "CodexCredentialLeaseLostError";
1264
+ }
1265
+ code = "codex_credential_lease_lost";
1266
+ };
1260
1267
  var CodexTurnLease = class {
1261
1268
  constructor(deps) {
1262
1269
  this.deps = deps;
1263
1270
  }
1264
1271
  held = false;
1265
1272
  lost = false;
1273
+ lossReason = null;
1266
1274
  holderId = null;
1267
1275
  generation = null;
1268
1276
  // Monotonic worker deadline, not a comparison between the Postgres and
@@ -1275,6 +1283,7 @@ var CodexTurnLease = class {
1275
1283
  markLost = (reason) => {
1276
1284
  if (this.lost) return;
1277
1285
  this.lost = true;
1286
+ this.lossReason = reason;
1278
1287
  this.deps.observability.incrementCounter({
1279
1288
  name: "opengeni_codex_lease_renewals_total",
1280
1289
  help: "Codex lease renewal checkpoints by outcome and reason.",
@@ -1286,6 +1295,16 @@ var CodexTurnLease = class {
1286
1295
  reason
1287
1296
  });
1288
1297
  };
1298
+ /** Fail closed at the last worker-confirmed lease deadline before dispatch. */
1299
+ assertUsable = () => {
1300
+ if (this.lost) {
1301
+ throw new CodexCredentialLeaseLostError(this.lossReason ?? "not_found");
1302
+ }
1303
+ if (!this.held || this.holderId === null || this.generation === null || codexCredentialLeaseDeadlineExpired(this.confirmedUntilMs)) {
1304
+ this.markLost("deadline");
1305
+ throw new CodexCredentialLeaseLostError("deadline");
1306
+ }
1307
+ };
1289
1308
  renew = async (reason) => {
1290
1309
  const turnId = this.deps.getTurnId();
1291
1310
  if (!turnId || !this.held || !this.holderId || this.generation === null || this.lost) {
@@ -1297,6 +1316,7 @@ var CodexTurnLease = class {
1297
1316
  }
1298
1317
  if (this.heartbeatInFlight) return;
1299
1318
  this.heartbeatInFlight = true;
1319
+ const priorConfirmedUntilMs = this.confirmedUntilMs;
1300
1320
  const renewalStartedAtMs = performance.now();
1301
1321
  try {
1302
1322
  const renewedUntil = await heartbeatCodexCredentialLeaseUntil(
@@ -1310,6 +1330,8 @@ var CodexTurnLease = class {
1310
1330
  );
1311
1331
  if (!renewedUntil) {
1312
1332
  this.markLost("not_found");
1333
+ } else if (codexCredentialLeaseDeadlineExpired(priorConfirmedUntilMs) || codexCredentialLeaseDeadlineExpired(renewalStartedAtMs + CODEX_CREDENTIAL_LEASE_TTL_MS)) {
1334
+ this.markLost("deadline");
1313
1335
  } else {
1314
1336
  this.confirmedUntilMs = renewalStartedAtMs + CODEX_CREDENTIAL_LEASE_TTL_MS;
1315
1337
  this.deps.observability.incrementCounter({
@@ -3456,6 +3478,7 @@ function createTurnContext(input) {
3456
3478
  },
3457
3479
  attempt: {
3458
3480
  turnId: void 0,
3481
+ dispatchId: "",
3459
3482
  triggerEventId: void 0,
3460
3483
  executionGeneration: 0,
3461
3484
  providerRecoveryCount: 0,
@@ -3466,7 +3489,9 @@ function createTurnContext(input) {
3466
3489
  billingState: {
3467
3490
  isCodexTurn: false,
3468
3491
  isXaiTurn: false,
3469
- isExternallyBilledTurn: false
3492
+ isExternallyBilledTurn: false,
3493
+ chargesOpenGeniCredits: true,
3494
+ countsTowardTokenCap: true
3470
3495
  },
3471
3496
  sandboxState: {
3472
3497
  resolvedSandbox: null,
@@ -3484,7 +3509,8 @@ function createTurnContext(input) {
3484
3509
  sandboxHolderId: null,
3485
3510
  sandboxGroupId: null,
3486
3511
  leaseHeartbeatTimer: void 0,
3487
- rotationInFlight: null,
3512
+ rotationPreemptionInFlight: null,
3513
+ deadlineRotationRequested: false,
3488
3514
  snapshotInFlight: null,
3489
3515
  firstProviderRequestStarted: false,
3490
3516
  turnEndCaptureInProgress: false,
@@ -3525,6 +3551,9 @@ function createTurnContext(input) {
3525
3551
  },
3526
3552
  providerTurn: {
3527
3553
  effectiveCodexCredentialId: null,
3554
+ effectiveCodexCredentialVersion: null,
3555
+ codexCredentialFailoverLimit: 1,
3556
+ codexPolicySnapshot: null,
3528
3557
  effectiveXaiCredentialId: null,
3529
3558
  xaiRotationEnabled: false,
3530
3559
  xaiAuthoritySnapshot: null,
@@ -4430,6 +4459,19 @@ async function finalizeTurnAttempt(deps) {
4430
4459
  cancellationSignal,
4431
4460
  control.activityStatus
4432
4461
  );
4462
+ const drainInFlightWarmSnapshot = async () => {
4463
+ const snapshot = sandboxState.snapshotInFlight;
4464
+ if (!snapshot) return;
4465
+ await snapshot.catch((error) => {
4466
+ console.error(
4467
+ "in-flight workspace snapshot settlement failed (turn outcome unaffected)",
4468
+ safeErrorDiagnostic(error)
4469
+ );
4470
+ });
4471
+ if (sandboxState.snapshotInFlight === snapshot) {
4472
+ sandboxState.snapshotInFlight = null;
4473
+ }
4474
+ };
4433
4475
  try {
4434
4476
  const toolCancellationFence = eventing.toolCancellationFenceRef.current;
4435
4477
  renewals.gitCredentialRenewalClosed = true;
@@ -4666,7 +4708,8 @@ async function finalizeTurnAttempt(deps) {
4666
4708
  }
4667
4709
  const runTurnEndPersistence = shouldRunTurnEndWorkspacePersistence({
4668
4710
  activityStatus: control.activityStatus,
4669
- cancellationRequested: finalizerSignal?.aborted === true
4711
+ cancellationRequested: finalizerSignal?.aborted === true,
4712
+ deadlineRotationRequested: sandboxState.deadlineRotationRequested
4670
4713
  });
4671
4714
  if (runTurnEndPersistence && attempt.turnId && sandboxState.resolvedSandbox && sandboxState.setupBoxSession && sandboxState.sandboxGroupId) {
4672
4715
  sandboxState.turnEndCaptureInProgress = true;
@@ -4745,22 +4788,14 @@ async function finalizeTurnAttempt(deps) {
4745
4788
  if (sandboxState.leaseHeartbeatTimer) {
4746
4789
  stopLeaseHeartbeat();
4747
4790
  }
4748
- if (sandboxState.rotationInFlight) {
4749
- await waitForTurnFinalizerStep(sandboxState.rotationInFlight, finalizerSignal).catch(
4750
- () => void 0
4751
- );
4791
+ if (sandboxState.rotationPreemptionInFlight) {
4792
+ await sandboxState.rotationPreemptionInFlight.catch(() => void 0);
4752
4793
  }
4794
+ await drainInFlightWarmSnapshot();
4753
4795
  if (sandboxState.resolvedSandbox) {
4754
4796
  const settledTurnId = attempt.turnId;
4755
4797
  if (runTurnEndPersistence && sandboxState.setupBoxSession && sandboxState.sandboxGroupId && settledTurnId) {
4756
- if (sandboxState.snapshotInFlight) {
4757
- await waitForWarmSnapshot(
4758
- sandboxState.snapshotInFlight,
4759
- settings.sandboxSnapshotTimeoutMs,
4760
- finalizerSignal
4761
- );
4762
- }
4763
- const persisted = await maybePersistWarmWorkspaceSnapshot(
4798
+ const snapshot = maybePersistWarmWorkspaceSnapshot(
4764
4799
  { db, settings, objectStorage },
4765
4800
  {
4766
4801
  accountId: input.accountId,
@@ -4774,6 +4809,8 @@ async function finalizeTurnAttempt(deps) {
4774
4809
  sandboxState.resolvedSandbox.leaseEpoch,
4775
4810
  finalizerSignal
4776
4811
  );
4812
+ sandboxState.snapshotInFlight = snapshot.settled;
4813
+ const persisted = await snapshot;
4777
4814
  if (persisted && eventing.publish) {
4778
4815
  await eventing.publish([
4779
4816
  {
@@ -4792,6 +4829,10 @@ async function finalizeTurnAttempt(deps) {
4792
4829
  );
4793
4830
  } finally {
4794
4831
  stopLeaseHeartbeat();
4832
+ if (sandboxState.rotationPreemptionInFlight) {
4833
+ await sandboxState.rotationPreemptionInFlight.catch(() => void 0);
4834
+ }
4835
+ await drainInFlightWarmSnapshot();
4795
4836
  const sandboxReleaseTargets = new Set(sandboxState.lateSandboxesAwaitingWriterDrain);
4796
4837
  sandboxState.lateSandboxesAwaitingWriterDrain.clear();
4797
4838
  if (sandboxState.resolvedSandbox) {
@@ -4896,17 +4937,10 @@ async function finalizeTurnAttempt(deps) {
4896
4937
  import {
4897
4938
  requestSessionTurnRecovery,
4898
4939
  getSessionGoal,
4899
- armCodexCapacityWait,
4900
4940
  armXaiCapacityWait,
4901
4941
  reconcileXaiCapacityWait,
4902
- getCodexRotationSettings,
4903
4942
  listCodexAccountStatuses as listCodexAccountStatuses2,
4904
- getSessionCodexState,
4905
- setSessionCodexPinInTransaction,
4906
4943
  quarantineCodexCredentialForLease,
4907
- setCodexCredentialExhaustedWithWakeTargets,
4908
- withSessionCodexCapacityMutation,
4909
- countConsecutiveReactiveRotations,
4910
4944
  recordUsageEvent as recordUsageEvent2,
4911
4945
  getActiveSessionHistoryItemsPaged,
4912
4946
  settleCodexCredentialLeaseLoss,
@@ -4946,11 +4980,13 @@ import {
4946
4980
  } from "@opengeni/runtime";
4947
4981
  import {
4948
4982
  calculateGatewayReportedCostBreakdown,
4983
+ calculateGatewayReportedProviderCostMicros,
4949
4984
  calculateModelUsageCostBreakdown,
4950
4985
  configuredModelPricingSchedules,
4986
+ resolveModelProvider,
4951
4987
  responseSatisfiesLatencyMode,
4952
- OPENGENI_GATEWAY_MODELS,
4953
- OPENGENI_GATEWAY_PROVIDER_ID
4988
+ OPENGENI_GATEWAY_PROVIDER_ID,
4989
+ WORKSPACE_GATEWAY_PROVIDER_ID
4954
4990
  } from "@opengeni/config";
4955
4991
  import { CODEX_PROVIDER_ID } from "@opengeni/codex";
4956
4992
  function modelUsageSourceKey(input) {
@@ -4977,6 +5013,7 @@ function createModelResponseEventState(claimedSourceKeys = /* @__PURE__ */ new S
4977
5013
  function createCompactionModelUsageEventState(claimedSourceKeys = /* @__PURE__ */ new Set()) {
4978
5014
  return { usageCount: 0, claimedSourceKeys };
4979
5015
  }
5016
+ var createSessionTitleModelUsageEventState = createCompactionModelUsageEventState;
4980
5017
  function modelResponseContextSignal(state) {
4981
5018
  return state.contextSignal;
4982
5019
  }
@@ -5035,9 +5072,10 @@ async function processModelResponseTerminalEvent(input) {
5035
5072
  turnAttemptId: input.turnAttemptId,
5036
5073
  model: input.model,
5037
5074
  externallyBilled: input.externallyBilled,
5075
+ ...input.chargesOpenGeniCredits !== void 0 ? { chargesOpenGeniCredits: input.chargesOpenGeniCredits } : {},
5076
+ ...input.countsTowardTokenCap !== void 0 ? { countsTowardTokenCap: input.countsTowardTokenCap } : {},
5038
5077
  usage: responseUsage.usage,
5039
5078
  normalizedUsage,
5040
- gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
5041
5079
  gatewayBilling: responseUsage.gatewayBilling,
5042
5080
  sourceKey,
5043
5081
  ...input.latencyMode ? { latencyMode: input.latencyMode } : {},
@@ -5056,6 +5094,8 @@ async function processModelResponseTerminalEvent(input) {
5056
5094
  sourceKey,
5057
5095
  usage: responseUsage,
5058
5096
  normalizedUsage,
5097
+ ...billing ? { billingPath: billing.billingPath } : {},
5098
+ ...billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {},
5059
5099
  servingAccountHash: accountContext.servingAccountHash,
5060
5100
  accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
5061
5101
  emittedSourceKeys: input.emittedSourceKeys
@@ -5104,7 +5144,7 @@ async function processCompactionModelUsageEvent(input) {
5104
5144
  const sourceKey = modelUsageSourceKey({
5105
5145
  responseId: input.usage.responseId,
5106
5146
  dispatchId: input.dispatchId,
5107
- positionalKey: `compaction-${usageOrdinal}`
5147
+ positionalKey: `${input.sourceKind ?? "compaction"}-${usageOrdinal}`
5108
5148
  });
5109
5149
  if (input.state.claimedSourceKeys.has(sourceKey)) {
5110
5150
  return { status: "duplicate", sourceKey };
@@ -5131,9 +5171,10 @@ async function processCompactionModelUsageEvent(input) {
5131
5171
  turnAttemptId: input.turnAttemptId,
5132
5172
  model: input.model,
5133
5173
  externallyBilled: input.externallyBilled,
5174
+ ...input.chargesOpenGeniCredits !== void 0 ? { chargesOpenGeniCredits: input.chargesOpenGeniCredits } : {},
5175
+ ...input.countsTowardTokenCap !== void 0 ? { countsTowardTokenCap: input.countsTowardTokenCap } : {},
5134
5176
  usage: input.usage.usage,
5135
5177
  normalizedUsage,
5136
- gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
5137
5178
  gatewayBilling: input.usage.gatewayBilling,
5138
5179
  sourceKey,
5139
5180
  observability: input.observability
@@ -5151,6 +5192,8 @@ async function processCompactionModelUsageEvent(input) {
5151
5192
  sourceKey,
5152
5193
  usage: input.usage,
5153
5194
  normalizedUsage,
5195
+ ...billing ? { billingPath: billing.billingPath } : {},
5196
+ ...billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {},
5154
5197
  servingAccountHash: accountContext.servingAccountHash,
5155
5198
  accountChangedFromPrevCall: accountContext.accountChangedFromPrevCall,
5156
5199
  emittedSourceKeys: input.emittedSourceKeys
@@ -5176,6 +5219,12 @@ async function processCompactionModelUsageEvent(input) {
5176
5219
  });
5177
5220
  return { status: "processed", sourceKey, authoritative };
5178
5221
  }
5222
+ async function processSessionTitleModelUsageEvent(input) {
5223
+ return await processCompactionModelUsageEvent({
5224
+ ...input,
5225
+ sourceKind: "session-title"
5226
+ });
5227
+ }
5179
5228
  async function emitModelCallUsage(input) {
5180
5229
  const usage = input.usage && typeof input.usage === "object" && "usage" in input.usage ? input.usage.usage : null;
5181
5230
  if (!usage || typeof usage !== "object") {
@@ -5197,6 +5246,8 @@ async function emitModelCallUsage(input) {
5197
5246
  providerApi: input.providerApi,
5198
5247
  model: input.model,
5199
5248
  sourceKey: input.sourceKey,
5249
+ ...input.billingPath ? { billingPath: input.billingPath } : {},
5250
+ ...input.upstreamProvider ? { upstreamProvider: input.upstreamProvider } : {},
5200
5251
  ...telemetry
5201
5252
  }
5202
5253
  }
@@ -5253,22 +5304,35 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
5253
5304
  const inputTokens = sanitizedUsage.inputTokens ?? 0;
5254
5305
  const outputTokens = sanitizedUsage.outputTokens ?? 0;
5255
5306
  const totalTokens = sanitizedUsage.totalTokens ?? 0;
5256
- const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : void 0;
5307
+ const chargesOpenGeniCredits = input.chargesOpenGeniCredits ?? !input.externallyBilled;
5308
+ const countsTowardTokenCap = input.countsTowardTokenCap ?? !input.externallyBilled;
5309
+ const resolvedGatewayModel = input.gatewayBilling ? resolveModelProvider(settings, input.model) : void 0;
5310
+ const gatewayProviderId = resolvedGatewayModel?.provider.id;
5311
+ const gatewayBilling = gatewayProviderId === OPENGENI_GATEWAY_PROVIDER_ID || gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID ? input.gatewayBilling : void 0;
5312
+ const allowedProviders = resolvedGatewayModel?.model.requestPolicy?.gateway.only;
5313
+ const unpinnedWorkspaceGatewayModel = gatewayProviderId === WORKSPACE_GATEWAY_PROVIDER_ID && allowedProviders === void 0;
5257
5314
  if (gatewayBilling) {
5258
- const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
5259
- (candidate) => candidate.productId === input.model
5260
- );
5261
- if (!gatewayModel || !gatewayModel.providers.includes(gatewayBilling.finalProvider)) {
5315
+ if (!unpinnedWorkspaceGatewayModel && (!allowedProviders || !allowedProviders.includes(gatewayBilling.finalProvider))) {
5262
5316
  throw new Error(
5263
5317
  `AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`
5264
5318
  );
5265
5319
  }
5320
+ if (unpinnedWorkspaceGatewayModel && chargesOpenGeniCredits) {
5321
+ throw new Error(
5322
+ `Workspace Gateway custom model ${input.model} cannot charge OpenGeni credits without pinned pricing`
5323
+ );
5324
+ }
5266
5325
  }
5267
5326
  const pricingSchedules = configuredModelPricingSchedules(settings);
5268
5327
  const configuredPricingModel = pricingSchedules[input.model] ? input.model : input.model.startsWith("codex/") && pricingSchedules[input.model.slice("codex/".length)] ? input.model.slice("codex/".length) : null;
5269
- const pricingBreakdown = gatewayBilling ? calculateGatewayReportedCostBreakdown(
5328
+ const pricingBreakdown = gatewayBilling ? unpinnedWorkspaceGatewayModel ? {
5329
+ providerCostMicros: calculateGatewayReportedProviderCostMicros(
5330
+ gatewayBilling.inferenceCostUsd
5331
+ ),
5332
+ creditCostMicros: 0
5333
+ } : calculateGatewayReportedCostBreakdown(
5270
5334
  settings,
5271
- input.model,
5335
+ configuredPricingModel ?? input.model,
5272
5336
  gatewayBilling.inferenceCostUsd,
5273
5337
  { inputTokens }
5274
5338
  ) : configuredPricingModel ? calculateModelUsageCostBreakdown(settings, configuredPricingModel, sanitizedUsage, {
@@ -5276,43 +5340,46 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
5276
5340
  }) : null;
5277
5341
  const hasCompleteCoreTokenTelemetry = normalizedUsage.telemetry.inputTokens !== null && normalizedUsage.telemetry.outputTokens !== null;
5278
5342
  const estimatedProviderCostMicros = gatewayBilling ? pricingBreakdown?.providerCostMicros ?? null : hasCompleteCoreTokenTelemetry ? pricingBreakdown?.providerCostMicros ?? null : null;
5343
+ const equivalentCreditCostMicros = pricingBreakdown && !unpinnedWorkspaceGatewayModel ? gatewayBilling || hasCompleteCoreTokenTelemetry ? pricingBreakdown.creditCostMicros : null : null;
5279
5344
  const pricingSource = gatewayBilling ? "gateway_reported" : estimatedProviderCostMicros !== null ? "configured_list_price" : null;
5280
- if (input.externallyBilled) {
5345
+ if (countsTowardTokenCap && totalTokens > 0) {
5281
5346
  await recordUsageEvent(db, {
5282
5347
  accountId: input.accountId,
5283
5348
  workspaceId: input.workspaceId,
5284
- eventType: "model.cost",
5285
- quantity: 0,
5286
- unit: "usd_micros",
5349
+ eventType: "model.tokens",
5350
+ quantity: totalTokens,
5351
+ unit: "tokens",
5287
5352
  sourceResourceType: "model_response",
5288
5353
  sourceResourceId: `${input.turnId}:${input.sourceKey}`,
5289
5354
  sessionId: input.sessionId,
5290
5355
  turnId: input.turnId,
5291
5356
  turnAttemptId: input.turnAttemptId,
5292
- idempotencyKey: `usage:model.cost:${input.turnId}:${input.sourceKey}`
5357
+ idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`
5293
5358
  });
5294
- return {
5295
- billingPath: "external",
5296
- pricedCostMicros: 0,
5297
- estimatedProviderCostMicros,
5298
- pricingSource,
5299
- normalizedUsage
5300
- };
5301
5359
  }
5302
- if (totalTokens > 0) {
5360
+ if (!chargesOpenGeniCredits) {
5303
5361
  await recordUsageEvent(db, {
5304
5362
  accountId: input.accountId,
5305
5363
  workspaceId: input.workspaceId,
5306
- eventType: "model.tokens",
5307
- quantity: totalTokens,
5308
- unit: "tokens",
5364
+ eventType: "model.cost",
5365
+ quantity: 0,
5366
+ unit: "usd_micros",
5309
5367
  sourceResourceType: "model_response",
5310
5368
  sourceResourceId: `${input.turnId}:${input.sourceKey}`,
5311
5369
  sessionId: input.sessionId,
5312
5370
  turnId: input.turnId,
5313
5371
  turnAttemptId: input.turnAttemptId,
5314
- idempotencyKey: `usage:model.tokens:${input.turnId}:${input.sourceKey}`
5372
+ idempotencyKey: `usage:model.cost:${input.turnId}:${input.sourceKey}`
5315
5373
  });
5374
+ return {
5375
+ billingPath: "external",
5376
+ pricedCostMicros: 0,
5377
+ estimatedProviderCostMicros,
5378
+ equivalentCreditCostMicros,
5379
+ pricingSource,
5380
+ normalizedUsage,
5381
+ ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
5382
+ };
5316
5383
  }
5317
5384
  const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
5318
5385
  if (!shouldDebit || totalTokens === 0 && !gatewayBilling) {
@@ -5320,6 +5387,7 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
5320
5387
  billingPath: "opengeni_credits",
5321
5388
  pricedCostMicros: 0,
5322
5389
  estimatedProviderCostMicros,
5390
+ equivalentCreditCostMicros,
5323
5391
  pricingSource,
5324
5392
  normalizedUsage,
5325
5393
  ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
@@ -5373,6 +5441,7 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
5373
5441
  billingPath: "opengeni_credits",
5374
5442
  pricedCostMicros: costMicros,
5375
5443
  estimatedProviderCostMicros,
5444
+ equivalentCreditCostMicros,
5376
5445
  pricingSource,
5377
5446
  normalizedUsage,
5378
5447
  ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
@@ -5394,6 +5463,7 @@ async function recordAuthoritativeModelCallFact(input) {
5394
5463
  billingPath: input.billing.billingPath,
5395
5464
  pricedCostMicros: input.billing.pricedCostMicros,
5396
5465
  estimatedProviderCostMicros: input.billing.estimatedProviderCostMicros,
5466
+ equivalentCreditCostMicros: input.billing.equivalentCreditCostMicros,
5397
5467
  pricingSource: input.billing.pricingSource,
5398
5468
  inputTokens: telemetry.inputTokens,
5399
5469
  outputTokens: telemetry.outputTokens,
@@ -5414,9 +5484,10 @@ function sanitizedModelUsageInput(normalized) {
5414
5484
  ...normalized.telemetry.inputTokens !== null ? { inputTokens: normalized.telemetry.inputTokens } : {},
5415
5485
  ...normalized.telemetry.outputTokens !== null ? { outputTokens: normalized.telemetry.outputTokens } : {},
5416
5486
  ...normalized.totalTokens !== null ? { totalTokens: normalized.totalTokens } : {},
5417
- ...normalized.telemetry.cachedTokens !== null ? {
5487
+ ...normalized.telemetry.cachedTokens !== null || normalized.telemetry.cacheWriteTokens !== null ? {
5418
5488
  inputTokensDetails: {
5419
- cached_tokens: normalized.telemetry.cachedTokens
5489
+ ...normalized.telemetry.cachedTokens === null ? {} : { cached_tokens: normalized.telemetry.cachedTokens },
5490
+ ...normalized.telemetry.cacheWriteTokens === null ? {} : { cache_write_tokens: normalized.telemetry.cacheWriteTokens }
5420
5491
  }
5421
5492
  } : {},
5422
5493
  ...normalized.requestUsageEntries ? { requestUsageEntries: normalized.requestUsageEntries } : {}
@@ -5440,6 +5511,10 @@ function assertWorkspaceHumanInputAllowed(enabled, kind, attempted) {
5440
5511
  function credentialSubjectIdForTurnInitiator(turn) {
5441
5512
  return directPersonalConnectionSubjectId(turn);
5442
5513
  }
5514
+ function xaiCatalogReadinessAuthority(turn, directCredentialSubjectId) {
5515
+ const subjectId = turn.initiatingHumanSubjectId ?? directCredentialSubjectId;
5516
+ return subjectId ? { subjectId, authoritySnapshot: turn.xaiProviderAccountAuthoritySnapshot } : null;
5517
+ }
5443
5518
  function isDirectHumanTurnInitiation(turn) {
5444
5519
  if (turn.source !== "user" && turn.source !== "api" || turn.initiator.kind !== "subject") {
5445
5520
  return false;
@@ -5463,9 +5538,20 @@ function shouldPublishToolAuthNeededForTurn(payload, trigger, turn) {
5463
5538
  const text = trigger.payload.text;
5464
5539
  return typeof text === "string" && /\bslack\b/i.test(text);
5465
5540
  }
5541
+ function rollingSafeToolAuthNeededPayload(payload) {
5542
+ if (payload.authoritySource !== "host") {
5543
+ return payload;
5544
+ }
5545
+ return {
5546
+ ...payload,
5547
+ reason: "unsupported_auth",
5548
+ hostReason: payload.hostReason ?? payload.reason
5549
+ };
5550
+ }
5466
5551
  function turnExecutionPolicyBillingIdentity(policy) {
5467
5552
  return {
5468
5553
  externallyBilled: policy.billing.metering === "external",
5554
+ countsTowardTokenCap: policy.billing.upstreamPayer === "deployment",
5469
5555
  codexSubscription: policy.providerId === "codex-subscription" && policy.credentialSource.kind === "connected_subscription" && policy.credentialSource.provider === "codex",
5470
5556
  xaiSubscription: policy.providerId === "supergrok-subscription" && policy.credentialSource.kind === "connected_subscription" && policy.credentialSource.provider === "xai"
5471
5557
  };
@@ -5519,8 +5605,8 @@ var BudgetExhaustedError = class extends Error {
5519
5605
  this.name = "BudgetExhaustedError";
5520
5606
  }
5521
5607
  };
5522
- async function ensureRunAllowed(settings, db, accountId, workspaceId, isExternallyBilledTurn, entitlements) {
5523
- if (!isExternallyBilledTurn && entitlements && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
5608
+ async function ensureRunAllowed(settings, db, accountId, workspaceId, isExternallyBilledTurn, entitlements, chargesOpenGeniCredits = !isExternallyBilledTurn, countsTowardTokenCap = !isExternallyBilledTurn) {
5609
+ if (chargesOpenGeniCredits && entitlements && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
5524
5610
  const decision = await entitlements.admitRun({
5525
5611
  accountId,
5526
5612
  workspaceId,
@@ -5530,7 +5616,7 @@ async function ensureRunAllowed(settings, db, accountId, workspaceId, isExternal
5530
5616
  if (!decision.allowed) {
5531
5617
  throw new Error(decision.reason || "insufficient OpenGeni credits");
5532
5618
  }
5533
- } else if (!isExternallyBilledTurn && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
5619
+ } else if (chargesOpenGeniCredits && (settings.billingMode === "stripe" || settings.usageLimitsMode === "managed")) {
5534
5620
  const balance = await getBillingBalance(db, accountId);
5535
5621
  if (balance.balanceMicros <= 0) {
5536
5622
  throw new Error("insufficient OpenGeni credits");
@@ -5550,7 +5636,7 @@ async function ensureRunAllowed(settings, db, accountId, workspaceId, isExternal
5550
5636
  );
5551
5637
  }
5552
5638
  }
5553
- if (!isExternallyBilledTurn && limits.maxMonthlyTokensPerWorkspace) {
5639
+ if (countsTowardTokenCap && limits.maxMonthlyTokensPerWorkspace) {
5554
5640
  const used = await sumUsageQuantity(db, {
5555
5641
  workspaceId,
5556
5642
  eventType: "model.tokens",
@@ -5564,6 +5650,59 @@ async function ensureRunAllowed(settings, db, accountId, workspaceId, isExternal
5564
5650
  }
5565
5651
 
5566
5652
  // src/activities/agent-turn/failure-settlement.ts
5653
+ function codexDefinitiveFailureDisposition(input) {
5654
+ const alternateAvailable = input.rotationEnabled && input.pinDisposition !== "manual" && input.decisionKind === "active" && input.decisionCredentialId !== null && input.decisionCredentialId !== input.servingCredentialId;
5655
+ if (alternateAvailable) return "failover";
5656
+ if (input.failureKind === "quota" || input.failureKind === "rate_limit" || input.decisionKind === "allCapped" || !input.rotationEnabled || input.pinDisposition === "manual") {
5657
+ return "wait";
5658
+ }
5659
+ return "terminal";
5660
+ }
5661
+ function codexCapacityWaitFailurePayload(input) {
5662
+ if (input.failureKind === "quota") {
5663
+ return codexUsageLimitFailurePayload(
5664
+ input.usageLimit ?? { resetsInSeconds: input.cooldownSeconds },
5665
+ input.detail,
5666
+ input.allAccounts ? { allAccounts: true } : void 0
5667
+ );
5668
+ }
5669
+ if (input.failureKind === "rate_limit") {
5670
+ return {
5671
+ error: "The serving Codex subscription is temporarily rate limited.",
5672
+ code: "codex_account_rate_limited",
5673
+ detail: input.detail,
5674
+ retryable: false
5675
+ };
5676
+ }
5677
+ if (input.failureKind === "auth") {
5678
+ return {
5679
+ error: "The serving Codex account requires reconnection.",
5680
+ code: "codex_relogin_required",
5681
+ detail: "the same accepted turn is waiting for the selected account to recover",
5682
+ retryable: false
5683
+ };
5684
+ }
5685
+ return {
5686
+ error: "The serving Codex account is not authorized for this request.",
5687
+ code: "codex_account_forbidden",
5688
+ detail: "the same accepted turn is waiting for the selected account to recover",
5689
+ retryable: false
5690
+ };
5691
+ }
5692
+ function codexLeaseAccountsForSelection(accounts) {
5693
+ return accounts.map((account) => ({
5694
+ ...account,
5695
+ activeLeaseCount: 0,
5696
+ selectionCount: 0,
5697
+ lastSelectedAt: null
5698
+ }));
5699
+ }
5700
+ function acceptedCodexPolicySnapshot(providerTurn) {
5701
+ if (!providerTurn.codexPolicySnapshot) {
5702
+ throw new Error("Codex accepted policy snapshot is missing after durable lease acquisition");
5703
+ }
5704
+ return providerTurn.codexPolicySnapshot;
5705
+ }
5567
5706
  async function settleTurnFailure(deps) {
5568
5707
  const {
5569
5708
  error,
@@ -5573,7 +5712,6 @@ async function settleTurnFailure(deps) {
5573
5712
  bus,
5574
5713
  observability,
5575
5714
  wakeSessionWorkflow,
5576
- signalCodexCapacityWorkflow,
5577
5715
  cancellationSignal,
5578
5716
  sandboxRotationController,
5579
5717
  noteCancellationRequested,
@@ -5867,7 +6005,7 @@ async function settleTurnFailure(deps) {
5867
6005
  control.turnMetricOutcome = "recovering";
5868
6006
  return claimedResult({ status: "recovering" });
5869
6007
  };
5870
- if (leases.codex.lost && settings.codexCredentialLeasingEnabled && billingState.isCodexTurn && eventing.publish && attempt.turnId && eventing.turnStartedPublished && leases.codex.holderId && leases.codex.generation !== null) {
6008
+ if ((leases.codex.lost || error instanceof CodexCredentialLeaseLostError) && billingState.isCodexTurn && eventing.publish && attempt.turnId && eventing.turnStartedPublished && leases.codex.holderId && leases.codex.generation !== null) {
5871
6009
  return await settleLostCodexAttempt(
5872
6010
  attempt.turnId,
5873
6011
  leases.codex.holderId,
@@ -5897,7 +6035,8 @@ async function settleTurnFailure(deps) {
5897
6035
  control.turnMetricOutcome = "recovering";
5898
6036
  return claimedResult({ status: "recovering" });
5899
6037
  }
5900
- const codexCredentialFailure = settings.codexCredentialLeasingEnabled && billingState.isCodexTurn && providerTurn.effectiveCodexCredentialId ? classifyCodexCredentialFailure(error) : null;
6038
+ const usageLimit = isCodexTransportError(error) ? classifyCodexUsageLimitError(error) : null;
6039
+ const codexCredentialFailure = billingState.isCodexTurn && providerTurn.effectiveCodexCredentialId ? classifyCodexCredentialFailure(error) : null;
5901
6040
  if (codexCredentialFailure && providerTurn.effectiveCodexCredentialId && eventing.publish && attempt.turnId && eventing.turnStartedPublished) {
5902
6041
  observability.incrementCounter({
5903
6042
  name: "opengeni_codex_credential_failures_total",
@@ -5948,13 +6087,22 @@ async function settleTurnFailure(deps) {
5948
6087
  } : {}
5949
6088
  } : null;
5950
6089
  const cooldownUntil = codexCredentialCooldownUntil(codexCredentialFailure, serving, now);
5951
- const statePersisted = leases.codex.holderId && leases.codex.generation !== null ? await quarantineCodexCredentialForLease(db, {
6090
+ const quarantineResult = leases.codex.holderId && leases.codex.generation !== null && providerTurn.effectiveCodexCredentialVersion !== null ? await quarantineCodexCredentialForLease(db, {
5952
6091
  accountId: input.accountId,
5953
6092
  workspaceId: input.workspaceId,
6093
+ sessionId: input.sessionId,
5954
6094
  turnId: attempt.turnId,
6095
+ attemptId: input.attemptId,
6096
+ executionGeneration: attempt.executionGeneration,
6097
+ workflowId: input.workflowId,
6098
+ workflowRunId: input.workflowRunId,
6099
+ dispatchId: attempt.dispatchId,
6100
+ expectedRedispatches: attempt.redispatchesAtDispatch,
5955
6101
  credentialId: providerTurn.effectiveCodexCredentialId,
6102
+ credentialVersion: providerTurn.effectiveCodexCredentialVersion,
5956
6103
  holderId: leases.codex.holderId,
5957
6104
  generation: leases.codex.generation,
6105
+ maxFailovers: providerTurn.codexCredentialFailoverLimit,
5958
6106
  quarantine: codexCredentialFailure.kind === "auth" ? {
5959
6107
  kind: "status",
5960
6108
  status: "needs_relogin",
@@ -5968,7 +6116,42 @@ async function settleTurnFailure(deps) {
5968
6116
  until: cooldownUntil,
5969
6117
  cooldownKind: codexCredentialFailure.kind
5970
6118
  }
5971
- }) : false;
6119
+ }) : null;
6120
+ if (quarantineResult?.action === "credential_changed" && leases.codex.holderId && leases.codex.generation !== null) {
6121
+ const recovery = await settleCodexCredentialLeaseLoss(db, {
6122
+ accountId: input.accountId,
6123
+ workspaceId: input.workspaceId,
6124
+ sessionId: input.sessionId,
6125
+ turnId: attempt.turnId,
6126
+ attemptId: input.attemptId,
6127
+ holderId: leases.codex.holderId,
6128
+ generation: leases.codex.generation,
6129
+ expectedRedispatches: attempt.redispatchesAtDispatch,
6130
+ checkpointDurable: true,
6131
+ recoveryPayload: {
6132
+ triggerEventId: attempt.triggerEventId,
6133
+ reason: "codex_credential_version_changed"
6134
+ },
6135
+ failedPayload: {}
6136
+ });
6137
+ if (recovery.action === "recovering") {
6138
+ leases.codex.held = false;
6139
+ await publishDurableSessionEvents2(
6140
+ bus,
6141
+ input.workspaceId,
6142
+ input.sessionId,
6143
+ recovery.events
6144
+ );
6145
+ control.activityStatus = "recovering";
6146
+ control.turnMetricOutcome = "recovering";
6147
+ return claimedResult({ status: "recovering" });
6148
+ }
6149
+ acknowledgeLostAttemptOwnership();
6150
+ control.activityStatus = "cancelled";
6151
+ control.turnMetricOutcome = "cancelled";
6152
+ return claimedResult({ status: "cancelled" });
6153
+ }
6154
+ const statePersisted = quarantineResult?.action === "recorded";
5972
6155
  if (!statePersisted && leases.codex.holderId && leases.codex.generation !== null) {
5973
6156
  leases.codex.lost = true;
5974
6157
  return await settleLostCodexAttempt(
@@ -5978,19 +6161,112 @@ async function settleTurnFailure(deps) {
5978
6161
  true
5979
6162
  );
5980
6163
  }
5981
- const [rotation, accounts] = await Promise.all([
5982
- getCodexRotationSettings(db, input.workspaceId).catch(() => null),
5983
- listCodexAccountStatuses2(db, input.workspaceId).catch(() => [])
5984
- ]);
5985
- const decision = rotation ? chooseRotationActive({
5986
- rotationStrategy: rotation.rotationStrategy,
5987
- activeCredentialId: rotation.activeCredentialId,
5988
- priorCredentialId: providerTurn.effectiveCodexCredentialId,
5989
- accounts,
5990
- now: /* @__PURE__ */ new Date()
5991
- }) : { kind: "none" };
5992
- const candidateAvailable = statePersisted && Boolean(rotation?.rotationEnabled && rotation?.leaseRotationEnabled) && decision.kind === "active" && decision.credentialId !== providerTurn.effectiveCodexCredentialId;
5993
- if (candidateAvailable && leases.codex.holderId && leases.codex.generation !== null) {
6164
+ if (quarantineResult?.action === "recorded" && quarantineResult.exhausted && leases.codex.holderId && leases.codex.generation !== null) {
6165
+ const settlement = await settleCodexCredentialFailover(db, {
6166
+ accountId: input.accountId,
6167
+ workspaceId: input.workspaceId,
6168
+ sessionId: input.sessionId,
6169
+ turnId: attempt.turnId,
6170
+ attemptId: input.attemptId,
6171
+ holderId: leases.codex.holderId,
6172
+ generation: leases.codex.generation,
6173
+ expectedRedispatches: attempt.redispatchesAtDispatch,
6174
+ maxFailovers: quarantineResult.maxFailovers,
6175
+ recoveryPayload: {
6176
+ triggerEventId: attempt.triggerEventId,
6177
+ reason: "codex_credential_failover",
6178
+ credentialId: providerTurn.effectiveCodexCredentialId,
6179
+ failureKind: codexCredentialFailure.kind
6180
+ },
6181
+ failedPayload: {
6182
+ error: "Automatic Codex credential failover stopped after every bounded account attempt was consumed. Send a new message after checking account health or capacity.",
6183
+ code: "codex_credential_failover_exhausted",
6184
+ retryable: false,
6185
+ recovery: "user_message",
6186
+ failoverCount: quarantineResult.failoverCount,
6187
+ maxFailovers: quarantineResult.maxFailovers
6188
+ }
6189
+ });
6190
+ if (settlement.action === "limit_exceeded") {
6191
+ leases.codex.held = false;
6192
+ await publishDurableSessionEvents2(
6193
+ bus,
6194
+ input.workspaceId,
6195
+ input.sessionId,
6196
+ settlement.events
6197
+ );
6198
+ control.activityError = error;
6199
+ control.activityStatus = "idle";
6200
+ control.turnMetricOutcome = "failed";
6201
+ await deliverFailedChildTurnToParent(
6202
+ { db, bus, settings, observability, wakeSessionWorkflow },
6203
+ input.workspaceId,
6204
+ input.sessionId,
6205
+ attempt.turnId
6206
+ );
6207
+ return claimedResult({ status: "idle" });
6208
+ }
6209
+ if (settlement.action === "stale") {
6210
+ acknowledgeLostAttemptOwnership();
6211
+ control.activityStatus = "cancelled";
6212
+ control.turnMetricOutcome = "cancelled";
6213
+ return claimedResult({ status: "cancelled" });
6214
+ }
6215
+ throw new Error("Exhausted Codex failover receipt unexpectedly recovered");
6216
+ }
6217
+ let accounts;
6218
+ try {
6219
+ accounts = await listCodexAccountStatuses2(db, input.workspaceId);
6220
+ } catch (metadataError) {
6221
+ const recoveryFailure = postClaimDatabaseRecoveryFailure({
6222
+ error: metadataError,
6223
+ turnId: attempt.turnId,
6224
+ triggerEventId: attempt.triggerEventId,
6225
+ executionGeneration: attempt.executionGeneration
6226
+ });
6227
+ if (recoveryFailure) {
6228
+ control.activityStatus = "recovering";
6229
+ control.turnMetricOutcome = "recovering";
6230
+ control.activityError = metadataError;
6231
+ throw recoveryFailure;
6232
+ }
6233
+ throw metadataError;
6234
+ }
6235
+ const acceptedPolicy = acceptedCodexPolicySnapshot(providerTurn);
6236
+ const selected = selectCodexCredentialLeaseForTurn({
6237
+ context: {
6238
+ accounts: codexLeaseAccountsForSelection(accounts),
6239
+ activeCredentialId: acceptedPolicy.activeCredentialId,
6240
+ rotationEnabled: acceptedPolicy.rotationEnabled,
6241
+ rotationStrategy: acceptedPolicy.rotationStrategy,
6242
+ existingCredentialId: null,
6243
+ failedCredentialIds: [providerTurn.effectiveCodexCredentialId],
6244
+ policyScope: null,
6245
+ unavailableDiagnostics: []
6246
+ },
6247
+ sessionId: input.sessionId,
6248
+ sessionPinnedCredentialId: acceptedPolicy.pinnedCredentialId,
6249
+ sessionPinSource: acceptedPolicy.pinSource,
6250
+ sessionLastCredentialId: acceptedPolicy.lastCredentialId,
6251
+ now
6252
+ });
6253
+ const decisionKind = selected.decision.kind === "allocatorDisabled" ? "allCapped" : selected.decision.kind;
6254
+ const pinDisposition = classifyCodexPin({
6255
+ pinnedCredentialId: acceptedPolicy.pinnedCredentialId,
6256
+ pinSource: acceptedPolicy.pinSource,
6257
+ strategy: acceptedPolicy.rotationStrategy,
6258
+ rotationEnabled: acceptedPolicy.rotationEnabled
6259
+ });
6260
+ const failureDisposition = codexDefinitiveFailureDisposition({
6261
+ failureKind: codexCredentialFailure.kind,
6262
+ rotationEnabled: acceptedPolicy.rotationEnabled,
6263
+ pinDisposition,
6264
+ decisionKind,
6265
+ decisionCredentialId: selected.decision.kind === "active" ? selected.credentialId : null,
6266
+ servingCredentialId: providerTurn.effectiveCodexCredentialId
6267
+ });
6268
+ const maxFailovers = providerTurn.codexCredentialFailoverLimit;
6269
+ if (statePersisted && failureDisposition === "failover" && leases.codex.holderId && leases.codex.generation !== null) {
5994
6270
  const settlement = await settleCodexCredentialFailover(db, {
5995
6271
  accountId: input.accountId,
5996
6272
  workspaceId: input.workspaceId,
@@ -6000,13 +6276,21 @@ async function settleTurnFailure(deps) {
6000
6276
  holderId: leases.codex.holderId,
6001
6277
  generation: leases.codex.generation,
6002
6278
  expectedRedispatches: attempt.redispatchesAtDispatch,
6003
- maxFailovers: Math.max(1, accounts.length),
6279
+ maxFailovers,
6004
6280
  recoveryPayload: {
6005
6281
  triggerEventId: attempt.triggerEventId,
6006
6282
  reason: "codex_credential_failover",
6007
6283
  credentialId: providerTurn.effectiveCodexCredentialId,
6008
6284
  failureKind: codexCredentialFailure.kind,
6009
6285
  ...cooldownUntil ? { cooldownUntil: cooldownUntil.toISOString() } : {}
6286
+ },
6287
+ failedPayload: {
6288
+ error: "Automatic Codex credential failover stopped after every bounded account attempt was consumed. Send a new message after checking account health or capacity.",
6289
+ code: "codex_credential_failover_exhausted",
6290
+ retryable: false,
6291
+ recovery: "user_message",
6292
+ failoverCount: quarantineResult.failoverCount,
6293
+ maxFailovers: quarantineResult.maxFailovers
6010
6294
  }
6011
6295
  });
6012
6296
  observability.incrementCounter({
@@ -6042,8 +6326,106 @@ async function settleTurnFailure(deps) {
6042
6326
  acknowledgeLostAttemptOwnership();
6043
6327
  control.activityStatus = "cancelled";
6044
6328
  control.turnMetricOutcome = "cancelled";
6329
+ return claimedResult({ status: "cancelled" });
6330
+ }
6331
+ if (settlement.action === "limit_exceeded") {
6332
+ leases.codex.held = false;
6333
+ await publishDurableSessionEvents2(
6334
+ bus,
6335
+ input.workspaceId,
6336
+ input.sessionId,
6337
+ settlement.events
6338
+ );
6339
+ control.activityError = error;
6340
+ control.activityStatus = "idle";
6341
+ control.turnMetricOutcome = "failed";
6342
+ await deliverFailedChildTurnToParent(
6343
+ { db, bus, settings, observability, wakeSessionWorkflow },
6344
+ input.workspaceId,
6345
+ input.sessionId,
6346
+ attempt.turnId
6347
+ );
6348
+ return claimedResult({ status: "idle" });
6349
+ }
6350
+ }
6351
+ if (statePersisted && failureDisposition === "wait" && leases.codex.holderId && leases.codex.generation !== null) {
6352
+ let goal;
6353
+ try {
6354
+ goal = await getSessionGoal(db, input.workspaceId, input.sessionId);
6355
+ } catch (metadataError) {
6356
+ const recoveryFailure = postClaimDatabaseRecoveryFailure({
6357
+ error: metadataError,
6358
+ turnId: attempt.turnId,
6359
+ triggerEventId: attempt.triggerEventId,
6360
+ executionGeneration: attempt.executionGeneration
6361
+ });
6362
+ if (recoveryFailure) {
6363
+ control.activityStatus = "recovering";
6364
+ control.turnMetricOutcome = "recovering";
6365
+ control.activityError = metadataError;
6366
+ throw recoveryFailure;
6367
+ }
6368
+ throw metadataError;
6369
+ }
6370
+ const activeGoal = goal?.status === "active" ? goal : null;
6371
+ const exactProviderReset = codexCredentialFailure.cooldownSeconds !== null && Number.isFinite(codexCredentialFailure.cooldownSeconds) && codexCredentialFailure.cooldownSeconds > 0;
6372
+ const policyCredentialId = pinDisposition === "manual" && acceptedPolicy.pinnedCredentialId ? acceptedPolicy.pinnedCredentialId : !acceptedPolicy.rotationEnabled ? acceptedPolicy.activeCredentialId : null;
6373
+ const capacityAccounts = policyCredentialId ? accounts.filter((account) => account.id === policyCredentialId) : accounts;
6374
+ const authoritativeResetAt = exactProviderReset ? authoritativeCodexCapacityResetAt(capacityAccounts, now) ?? cooldownUntil : null;
6375
+ const allAccounts = acceptedPolicy.rotationEnabled && pinDisposition !== "manual" && decisionKind === "allCapped";
6376
+ const failurePayload = codexCapacityWaitFailurePayload({
6377
+ failureKind: codexCredentialFailure.kind,
6378
+ usageLimit,
6379
+ cooldownSeconds: codexCredentialFailure.cooldownSeconds,
6380
+ detail: codexCredentialFailure.kind === "quota" ? error instanceof Error ? error.message : String(error) : "the same accepted turn is waiting for eligible credential capacity",
6381
+ allAccounts
6382
+ });
6383
+ const evaluated = await armAndReconcileCodexCapacityWait(
6384
+ { db, bus },
6385
+ {
6386
+ accountId: input.accountId,
6387
+ workspaceId: input.workspaceId,
6388
+ sessionId: input.sessionId,
6389
+ turnId: attempt.turnId,
6390
+ attemptId: input.attemptId,
6391
+ workflowId: input.workflowId,
6392
+ goalId: activeGoal?.id ?? null,
6393
+ goalVersion: activeGoal?.version ?? null,
6394
+ earliestResetAt: authoritativeResetAt,
6395
+ resetKind: authoritativeResetAt ? "authoritative" : codexCredentialFailure.kind === "auth" || codexCredentialFailure.kind === "forbidden" ? "mutation_only" : "bounded_refresh",
6396
+ failurePayload,
6397
+ leaseFence: {
6398
+ holderId: leases.codex.holderId,
6399
+ generation: leases.codex.generation
6400
+ },
6401
+ expectedRedispatches: attempt.redispatchesAtDispatch
6402
+ },
6403
+ { onArmed: () => leases.codex.held = false }
6404
+ );
6405
+ control.activityError = error;
6406
+ if (evaluated.action === "resumed") {
6407
+ control.activityStatus = "recovering";
6408
+ control.turnMetricOutcome = "recovering";
6045
6409
  return claimedResult({ status: "recovering" });
6046
6410
  }
6411
+ if (evaluated.action === "waiting") {
6412
+ control.activityError = error;
6413
+ control.activityStatus = "waiting_capacity";
6414
+ control.turnMetricOutcome = "recovering";
6415
+ return claimedResult({
6416
+ status: "waiting_capacity",
6417
+ capacityWait: {
6418
+ waiterId: evaluated.waiter.id,
6419
+ generation: evaluated.waiter.generation,
6420
+ nextCheckAt: evaluated.waiter.nextCheckAt.toISOString(),
6421
+ wakeRevision: evaluated.waiter.wakeRevision
6422
+ }
6423
+ });
6424
+ }
6425
+ acknowledgeLostAttemptOwnership();
6426
+ control.activityStatus = "cancelled";
6427
+ control.turnMetricOutcome = "cancelled";
6428
+ return claimedResult({ status: "cancelled" });
6047
6429
  }
6048
6430
  }
6049
6431
  }
@@ -6154,201 +6536,20 @@ async function settleTurnFailure(deps) {
6154
6536
  control.activityError = error;
6155
6537
  return claimedResult({ status: "recovering" });
6156
6538
  }
6157
- const usageLimit = isCodexTransportError(error) ? classifyCodexUsageLimitError(error) : null;
6158
6539
  if (usageLimit && eventing.publish && attempt.turnId && eventing.turnStartedPublished) {
6159
- const goal = await getSessionGoal(db, input.workspaceId, input.sessionId).catch(() => null);
6160
- const goalActive = Boolean(goal && goal.status === "active");
6161
6540
  await flushRuntimeBatcher();
6162
6541
  await historySink.reconcileConversationTruth();
6163
- let rotated = false;
6164
- let rotationResumeMs = null;
6165
- let rotationResumeIdleUntilReset = false;
6166
- let allCappedResetAt = null;
6167
- let capacityAuthoritativeResetAt = null;
6168
- let capacityNeedsBoundedRefresh = false;
6169
- if (providerTurn.effectiveCodexCredentialId) {
6170
- const [rotation, sessionCodex] = await Promise.all([
6171
- getCodexRotationSettings(db, input.workspaceId).catch(() => null),
6172
- getSessionCodexState(db, input.workspaceId, input.sessionId).catch(() => null)
6173
- ]);
6174
- const reactiveStrategy = rotation?.rotationStrategy ?? "most_remaining";
6175
- const reactiveDisposition = classifyCodexPin({
6176
- pinnedCredentialId: sessionCodex?.pinnedCredentialId ?? null,
6177
- pinSource: sessionCodex?.pinSource ?? null,
6178
- strategy: reactiveStrategy,
6179
- rotationEnabled: Boolean(rotation?.rotationEnabled)
6180
- });
6181
- const reactiveSharded = reactiveDisposition === "sharded";
6182
- const rotating = Boolean(rotation?.rotationEnabled || rotation?.leaseRotationEnabled) && reactiveDisposition !== "manual";
6183
- if (rotating && rotation) {
6184
- const accounts = await listCodexAccountStatuses2(db, input.workspaceId).catch(() => []);
6185
- const serving = accounts.find((a) => a.id === providerTurn.effectiveCodexCredentialId) ?? null;
6186
- const until = codexCredentialCooldownUntil(
6187
- { kind: "quota", cooldownSeconds: usageLimit.resetsInSeconds },
6188
- serving,
6189
- /* @__PURE__ */ new Date()
6190
- );
6191
- const cooldownMutation = await setCodexCredentialExhaustedWithWakeTargets(
6192
- db,
6193
- input.workspaceId,
6194
- providerTurn.effectiveCodexCredentialId,
6195
- until,
6196
- "quota"
6197
- ).catch(() => null);
6198
- const cooldownPersisted = cooldownMutation?.result ?? false;
6199
- if (cooldownMutation) {
6200
- await signalCodexCapacityWakeTargets(
6201
- { signalCodexCapacityWorkflow, wakeSessionWorkflow },
6202
- cooldownMutation.wakeTargets
6203
- );
6204
- }
6205
- const fresh = accounts.map(
6206
- (a) => a.id === providerTurn.effectiveCodexCredentialId ? { ...a, exhaustedUntil: until, exhaustedKind: "quota" } : a
6207
- );
6208
- if (reactiveSharded) {
6209
- const newHome = shardCredentialForSession({
6210
- sessionId: input.sessionId,
6211
- accounts: fresh,
6212
- now: /* @__PURE__ */ new Date()
6213
- });
6214
- if (newHome) {
6215
- rotated = true;
6216
- const pinMutation = await withSessionCodexCapacityMutation(
6217
- db,
6218
- {
6219
- workspaceId: input.workspaceId,
6220
- reason: "codex_policy_pin_resharded"
6221
- },
6222
- async (tx) => {
6223
- const changed = await setSessionCodexPinInTransaction(
6224
- tx,
6225
- input.workspaceId,
6226
- input.sessionId,
6227
- newHome,
6228
- "policy",
6229
- {
6230
- expected: {
6231
- pinnedCredentialId: sessionCodex?.pinnedCredentialId ?? null,
6232
- pinSource: sessionCodex?.pinSource ?? null
6233
- }
6234
- }
6235
- );
6236
- return { result: changed, changed };
6237
- }
6238
- ).catch(() => null);
6239
- if (pinMutation) {
6240
- await signalCodexCapacityWakeTargets(
6241
- { signalCodexCapacityWorkflow, wakeSessionWorkflow },
6242
- pinMutation.wakeTargets
6243
- );
6244
- }
6245
- const priorConsecutiveRotations = await countConsecutiveReactiveRotations(
6246
- db,
6247
- input.workspaceId,
6248
- input.sessionId
6249
- ).catch(() => 0);
6250
- const resume = computeReactiveRotationResume({
6251
- cooldownPersisted,
6252
- priorConsecutiveRotations,
6253
- connectedAccountCount: accounts.length
6254
- });
6255
- rotationResumeMs = resume.continueDelayMs;
6256
- rotationResumeIdleUntilReset = resume.idleUntilReset;
6257
- } else {
6258
- rotated = true;
6259
- allCappedResetAt = earliestCodexReset(fresh, /* @__PURE__ */ new Date());
6260
- capacityAuthoritativeResetAt = authoritativeCodexCapacityResetAt(fresh, /* @__PURE__ */ new Date());
6261
- capacityNeedsBoundedRefresh = true;
6262
- }
6263
- } else {
6264
- const decision = chooseRotationActive({
6265
- rotationStrategy: reactiveStrategy,
6266
- activeCredentialId: rotation.activeCredentialId,
6267
- priorCredentialId: providerTurn.effectiveCodexCredentialId,
6268
- accounts: fresh,
6269
- now: /* @__PURE__ */ new Date()
6270
- });
6271
- if (decision.kind === "active") {
6272
- rotated = true;
6273
- const priorConsecutiveRotations = await countConsecutiveReactiveRotations(
6274
- db,
6275
- input.workspaceId,
6276
- input.sessionId
6277
- ).catch(() => 0);
6278
- const resume = computeReactiveRotationResume({
6279
- cooldownPersisted,
6280
- priorConsecutiveRotations,
6281
- connectedAccountCount: accounts.length
6282
- });
6283
- rotationResumeMs = resume.continueDelayMs;
6284
- rotationResumeIdleUntilReset = resume.idleUntilReset;
6285
- } else if (decision.kind === "allCapped") {
6286
- rotated = true;
6287
- allCappedResetAt = decision.earliestResetAt;
6288
- capacityAuthoritativeResetAt = authoritativeCodexCapacityResetAt(fresh, /* @__PURE__ */ new Date());
6289
- capacityNeedsBoundedRefresh = true;
6290
- }
6291
- }
6292
- }
6293
- }
6294
- const failurePayload = allCappedResetAt ? codexUsageLimitFailurePayload(
6295
- {
6296
- resetsInSeconds: Math.ceil(Math.max(0, allCappedResetAt.getTime() - Date.now()) / 1e3)
6297
- },
6298
- error instanceof Error ? error.message : String(error),
6299
- { allAccounts: true }
6300
- ) : codexUsageLimitFailurePayload(
6542
+ const failurePayload = codexUsageLimitFailurePayload(
6301
6543
  usageLimit,
6302
6544
  error instanceof Error ? error.message : String(error)
6303
6545
  );
6304
- if (rotationResumeMs === null) {
6305
- const providerResetAt = capacityAuthoritativeResetAt ?? (usageLimit.resetsInSeconds !== null && Number.isFinite(usageLimit.resetsInSeconds) && usageLimit.resetsInSeconds > 0 ? new Date(Date.now() + Math.ceil(usageLimit.resetsInSeconds) * 1e3) : null);
6306
- const armed = await armCodexCapacityWait(db, {
6307
- accountId: input.accountId,
6308
- workspaceId: input.workspaceId,
6309
- sessionId: input.sessionId,
6310
- turnId: attempt.turnId,
6311
- attemptId: input.attemptId,
6312
- workflowId: input.workflowId,
6313
- goalId: goalActive && goal ? goal.id : null,
6314
- goalVersion: goalActive && goal ? goal.version : null,
6315
- earliestResetAt: providerResetAt,
6316
- resetKind: providerResetAt && !capacityNeedsBoundedRefresh ? "authoritative" : "bounded_refresh",
6317
- failurePayload,
6318
- ...leases.codex.holderId && leases.codex.generation !== null ? {
6319
- leaseFence: {
6320
- holderId: leases.codex.holderId,
6321
- generation: leases.codex.generation
6322
- },
6323
- expectedRedispatches: attempt.redispatchesAtDispatch
6324
- } : {}
6325
- });
6326
- if (armed.action === "waiting") {
6327
- await publishDurableSessionEvents2(bus, input.workspaceId, input.sessionId, armed.events);
6328
- control.turnMetricOutcome = "recovering";
6329
- control.activityStatus = "waiting_capacity";
6330
- control.activityError = error;
6331
- return claimedResult({
6332
- status: "waiting_capacity",
6333
- capacityWait: {
6334
- waiterId: armed.waiter.id,
6335
- generation: armed.waiter.generation,
6336
- nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
6337
- wakeRevision: armed.waiter.wakeRevision
6338
- }
6339
- });
6340
- }
6341
- }
6342
6546
  if (!await eventing.settle({
6343
6547
  events: [
6344
- // `rotated:true` ONLY on the reactive rotation path tells evaluateGoalContinuation to
6345
- // freeze autoContinuations (a rotation walk must not burn the goal's continuation budget).
6346
6548
  {
6347
6549
  type: "turn.failed",
6348
6550
  payload: {
6349
6551
  ...failurePayload,
6350
- recovery: goalActive ? "goal_continuation" : "user_message",
6351
- ...rotated ? { rotated: true } : {}
6552
+ recovery: "user_message"
6352
6553
  }
6353
6554
  },
6354
6555
  { type: "session.status.changed", payload: { status: "idle" } }
@@ -6362,21 +6563,6 @@ async function settleTurnFailure(deps) {
6362
6563
  control.turnMetricOutcome = "failed";
6363
6564
  control.activityStatus = "idle";
6364
6565
  control.activityError = error;
6365
- if (goalActive) {
6366
- if (rotationResumeMs !== null) {
6367
- return claimedResult({
6368
- status: "idle",
6369
- continueDelayMs: rotationResumeMs,
6370
- ...rotationResumeIdleUntilReset ? { idleUntilReset: true } : {}
6371
- });
6372
- }
6373
- const resumeMs = allCappedResetAt ? computeIdleDelayMs(allCappedResetAt, /* @__PURE__ */ new Date(), CODEX_USAGE_LIMIT_MAX_RESUME_MS) : usageLimit.resetsInSeconds !== null && Number.isFinite(usageLimit.resetsInSeconds) && usageLimit.resetsInSeconds > 0 ? Math.min(Math.ceil(usageLimit.resetsInSeconds) * 1e3, CODEX_USAGE_LIMIT_MAX_RESUME_MS) : CODEX_USAGE_LIMIT_MAX_RESUME_MS;
6374
- return claimedResult({
6375
- status: "idle",
6376
- continueDelayMs: resumeMs,
6377
- ...allCappedResetAt ? { idleUntilReset: true } : {}
6378
- });
6379
- }
6380
6566
  return claimedResult({ status: "idle" });
6381
6567
  }
6382
6568
  if (error instanceof BudgetExhaustedError && eventing.publish && attempt.turnId && eventing.turnStartedPublished) {
@@ -6620,8 +6806,11 @@ import {
6620
6806
  attachOpenSuffixToPendingToolCalls,
6621
6807
  clearDurablePendingSessionToolCalls,
6622
6808
  isSessionCompactionRequested,
6623
- nextSessionHistoryPosition as nextSessionHistoryPosition2
6809
+ nextSessionHistoryPosition as nextSessionHistoryPosition2,
6810
+ persistModelContextSnapshot,
6811
+ updateSessionTitleWithEvent
6624
6812
  } from "@opengeni/db";
6813
+ import { publishDurableSessionEvents as publishDurableSessionEvents3 } from "@opengeni/events";
6625
6814
  import {
6626
6815
  normalizeModelCallUsage as normalizeModelCallUsage2,
6627
6816
  normalizeSdkEvent,
@@ -6633,6 +6822,7 @@ import {
6633
6822
  withRunCredentialsSession,
6634
6823
  runOwnedSandboxSetup
6635
6824
  } from "@opengeni/runtime";
6825
+ import { serviceTierForLatencyMode } from "@opengeni/config";
6636
6826
 
6637
6827
  // src/activities/context-compaction.ts
6638
6828
  import {
@@ -7211,6 +7401,7 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
7211
7401
  `turn attempt was fenced while recording context compaction start: ${started.reason}`
7212
7402
  );
7213
7403
  }
7404
+ options.onCompactionStarted?.(trigger);
7214
7405
  await options.publishLiveEvents?.(started.events);
7215
7406
  if (useRemoteV2) {
7216
7407
  const outcome2 = await compactContextRemoteV2(
@@ -7752,6 +7943,111 @@ import {
7752
7943
  resolveWorkspaceAgentHumanInputEnabled
7753
7944
  } from "@opengeni/contracts";
7754
7945
 
7946
+ // src/activities/agent-turn/session-title.ts
7947
+ import { hasPermission } from "@opengeni/core";
7948
+ import {
7949
+ AUTOMATIC_SESSION_TITLE_FALLBACK,
7950
+ DEFAULT_FIRST_PARTY_MCP_PERMISSIONS
7951
+ } from "@opengeni/contracts";
7952
+ var SESSION_TITLE_MODEL_TOOL_NAME = "opengeni__set_session_title";
7953
+ var SESSION_TITLE_DESCRIPTION = "Set this session's display title to a concise 3-7 word topic label. Use a stable noun phrase about the actual task or subject, never a quote/prefix of a prompt, greeting, request boilerplate, URL, identifier, credential, token, or other sensitive value. Call once on a new session, then only when the topic materially changes. Never call it as routine setup after a continuation, resume, or interruption, or merely to reassert the same title. A human-set title cannot be replaced.";
7954
+ function shouldRequestMissingSessionTitle(input) {
7955
+ const title = input.title?.trim() ?? "";
7956
+ const needsSemanticTitle = input.titleSource !== "user" && (!title || title === AUTOMATIC_SESSION_TITLE_FALLBACK);
7957
+ if (!needsSemanticTitle) return false;
7958
+ if (!input.firstPartyMcpTools.includes("set_session_title")) return false;
7959
+ const permissions = input.firstPartyMcpPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS;
7960
+ return hasPermission([...permissions], "sessions:control");
7961
+ }
7962
+ function sessionTitleToolPlan(input) {
7963
+ const titleToolAvailable = input.shouldRequestTitle && input.tools.some((tool) => tool.kind === "mcp" && tool.id === "opengeni");
7964
+ const generateTitleInParallel = titleToolAvailable && input.parallelGenerationAvailable;
7965
+ const promoteTitleTool = titleToolAvailable && !generateTitleInParallel;
7966
+ return {
7967
+ promoteTitleTool,
7968
+ generateTitleInParallel,
7969
+ remoteFirstPartyMcpTools: titleToolAvailable ? input.selectedFirstPartyMcpTools.filter((tool) => tool !== "set_session_title") : [...input.selectedFirstPartyMcpTools],
7970
+ preparationIndependentToolNames: promoteTitleTool ? [SESSION_TITLE_MODEL_TOOL_NAME] : []
7971
+ };
7972
+ }
7973
+ var PARALLEL_SESSION_TITLE_TIMEOUT_MS = 15e3;
7974
+ function startParallelSessionTitleGeneration(input) {
7975
+ const cancellationController = new AbortController();
7976
+ const timeoutSignal = AbortSignal.timeout(input.timeoutMs ?? PARALLEL_SESSION_TITLE_TIMEOUT_MS);
7977
+ const signals = [cancellationController.signal, timeoutSignal];
7978
+ if (input.signal) signals.push(input.signal);
7979
+ const signal = AbortSignal.any(signals);
7980
+ const generation = input.generate(signal).then((result) => signal.aborted ? null : result).catch((error) => {
7981
+ if (!signal.aborted) input.onError?.(error);
7982
+ return null;
7983
+ });
7984
+ let finished = null;
7985
+ return {
7986
+ finish: () => {
7987
+ if (finished) return finished;
7988
+ finished = generation;
7989
+ return finished;
7990
+ },
7991
+ cancel: async () => {
7992
+ cancellationController.abort();
7993
+ await generation;
7994
+ }
7995
+ };
7996
+ }
7997
+ function createSessionTitleAttemptToolDefinition(input) {
7998
+ return {
7999
+ identity: { serverId: "opengeni", toolName: "set_session_title" },
8000
+ modelName: SESSION_TITLE_MODEL_TOOL_NAME,
8001
+ codemodePath: ["opengeni", "set_session_title"],
8002
+ title: "Set session title",
8003
+ description: SESSION_TITLE_DESCRIPTION,
8004
+ inputSchema: {
8005
+ type: "object",
8006
+ properties: {
8007
+ title: { type: "string", minLength: 1, maxLength: 200 }
8008
+ },
8009
+ required: ["title"],
8010
+ additionalProperties: false
8011
+ },
8012
+ outputSchema: {
8013
+ type: "object",
8014
+ properties: {
8015
+ ok: { type: "boolean", const: true },
8016
+ updated: { type: "boolean" },
8017
+ title: { type: "string" }
8018
+ },
8019
+ required: ["ok", "updated", "title"],
8020
+ additionalProperties: false
8021
+ },
8022
+ annotations: {
8023
+ title: "Set session title",
8024
+ readOnlyHint: false,
8025
+ destructiveHint: false,
8026
+ idempotentHint: true,
8027
+ openWorldHint: false
8028
+ },
8029
+ source: "opengeni",
8030
+ approval: "none",
8031
+ execute: async (args) => {
8032
+ const title = args.title;
8033
+ if (typeof title !== "string") {
8034
+ throw new Error("set_session_title requires a title string");
8035
+ }
8036
+ const result = await input.updateTitle(title);
8037
+ const output = {
8038
+ ok: true,
8039
+ updated: result.updated,
8040
+ title: result.title ?? title
8041
+ };
8042
+ return {
8043
+ isError: false,
8044
+ content: [{ type: "text", text: JSON.stringify(output) }],
8045
+ structuredContent: output
8046
+ };
8047
+ }
8048
+ };
8049
+ }
8050
+
7755
8051
  // src/activities/agent-turn/sandbox-shared-preparation.ts
7756
8052
  import { randomUUID } from "crypto";
7757
8053
  import {
@@ -7918,6 +8214,7 @@ async function runTurnStreamAttempt(deps) {
7918
8214
  input,
7919
8215
  settings,
7920
8216
  db,
8217
+ bus,
7921
8218
  runtime,
7922
8219
  objectStorage,
7923
8220
  observability,
@@ -7948,6 +8245,7 @@ async function runTurnStreamAttempt(deps) {
7948
8245
  attachCodemodeTokenRenewal,
7949
8246
  attachRunCredentialRenewal,
7950
8247
  withProviderRequestContext,
8248
+ withSessionTitleProviderRequestContext,
7951
8249
  publishCompactionLiveEvents,
7952
8250
  publishCompactionOutcomeEvents,
7953
8251
  recordCompanyBrainContributionReceiptOnce,
@@ -7973,6 +8271,8 @@ async function runTurnStreamAttempt(deps) {
7973
8271
  providerApi,
7974
8272
  runSettings,
7975
8273
  turnTools,
8274
+ generateSessionTitleInParallel,
8275
+ sessionTitlePrompt,
7976
8276
  compactSummarizer,
7977
8277
  settleDeferredSteerAfterCompaction,
7978
8278
  compactionModelHistoryProjector,
@@ -7992,6 +8292,69 @@ async function runTurnStreamAttempt(deps) {
7992
8292
  if (!activeTurnId) {
7993
8293
  throw new Error("Turn id was not initialized");
7994
8294
  }
8295
+ const sessionTitleUsageState = createSessionTitleModelUsageEventState(
8296
+ claimedModelUsageSourceKeys
8297
+ );
8298
+ let parallelSessionTitle = null;
8299
+ let parallelSessionTitleFinished = false;
8300
+ const finishParallelSessionTitle = async () => {
8301
+ if (parallelSessionTitleFinished) return;
8302
+ parallelSessionTitleFinished = true;
8303
+ const generated = await parallelSessionTitle?.finish();
8304
+ if (!generated) return;
8305
+ if (generated.usage) {
8306
+ await processSessionTitleModelUsageEvent({
8307
+ usage: generated.usage,
8308
+ state: sessionTitleUsageState,
8309
+ dispatchId: modelUsageDispatchId,
8310
+ settings,
8311
+ db,
8312
+ observability,
8313
+ publish: eventing.publish,
8314
+ accountId: input.accountId,
8315
+ workspaceId: input.workspaceId,
8316
+ sessionId: input.sessionId,
8317
+ turnId: activeTurnId,
8318
+ turnAttemptId: input.attemptId,
8319
+ provider: resolvedModel?.provider.id ?? settings.openaiProvider,
8320
+ providerApi: resolvedModel?.provider.api ?? "responses",
8321
+ model: resolvedModel?.configured.id ?? turn.model,
8322
+ externallyBilled: billingState.isExternallyBilledTurn,
8323
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
8324
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
8325
+ servingCredentialId: providerTurn.effectiveCodexCredentialId,
8326
+ priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
8327
+ emittedSourceKeys: emittedModelUsageSourceKeys,
8328
+ renewLease: () => leases.renewServing("model_usage"),
8329
+ leaseLost: leases.servingLost,
8330
+ leaseLostMessage: "Provider credential lease expired during session title generation"
8331
+ });
8332
+ }
8333
+ if (!generated.title) return;
8334
+ try {
8335
+ const result = await updateSessionTitleWithEvent(db, {
8336
+ workspaceId: input.workspaceId,
8337
+ sessionId: input.sessionId,
8338
+ title: generated.title,
8339
+ source: "agent"
8340
+ });
8341
+ if (result.events.length > 0) {
8342
+ await publishDurableSessionEvents3(bus, input.workspaceId, input.sessionId, result.events);
8343
+ }
8344
+ } catch (error) {
8345
+ observability.warn("parallel session title persistence failed", {
8346
+ ...safeErrorDiagnostic(error),
8347
+ errorClass: "ParallelSessionTitlePersistenceError",
8348
+ errorCode: "parallel_session_title_persistence_failed",
8349
+ origin: "worker"
8350
+ });
8351
+ }
8352
+ };
8353
+ const cancelParallelSessionTitle = async () => {
8354
+ if (parallelSessionTitleFinished) return;
8355
+ parallelSessionTitleFinished = true;
8356
+ await parallelSessionTitle?.cancel();
8357
+ };
7995
8358
  let runInput = null;
7996
8359
  const prepareRunAttemptInput = async () => {
7997
8360
  const historyPreparationStartedAt = performance.now();
@@ -8275,6 +8638,17 @@ async function runTurnStreamAttempt(deps) {
8275
8638
  return await runtime.runStream(agent, runInput, eventing.modelRunSettings, {
8276
8639
  signal: runtimeCancellationSignal,
8277
8640
  sandboxEnvironment,
8641
+ onModelVisibleContext: async (snapshot) => {
8642
+ await persistModelContextSnapshot(db, {
8643
+ accountId: input.accountId,
8644
+ workspaceId: input.workspaceId,
8645
+ sessionId: input.sessionId,
8646
+ turnId: activeTurnId,
8647
+ attemptId: input.attemptId,
8648
+ executionGeneration: attempt.executionGeneration,
8649
+ snapshot
8650
+ });
8651
+ },
8278
8652
  onRuntimeEvent: async (event) => {
8279
8653
  await leases.renewServing("runtime_event");
8280
8654
  if (leases.servingLost()) {
@@ -8360,7 +8734,9 @@ async function runTurnStreamAttempt(deps) {
8360
8734
  });
8361
8735
  }
8362
8736
  };
8363
- if (leases.codex.lost) {
8737
+ if (billingState.isCodexTurn) {
8738
+ leases.codex.assertUsable();
8739
+ } else if (leases.codex.lost) {
8364
8740
  throw new Error("Codex credential lease expired before the model run");
8365
8741
  }
8366
8742
  if (leases.xai.lost) {
@@ -8452,6 +8828,8 @@ async function runTurnStreamAttempt(deps) {
8452
8828
  latencyMode: turnExecutionPolicy.latencyMode,
8453
8829
  metricProvider: streamProvider,
8454
8830
  externallyBilled: billingState.isExternallyBilledTurn,
8831
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
8832
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
8455
8833
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
8456
8834
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
8457
8835
  emittedSourceKeys: emittedModelUsageSourceKeys,
@@ -8509,7 +8887,9 @@ async function runTurnStreamAttempt(deps) {
8509
8887
  input.accountId,
8510
8888
  input.workspaceId,
8511
8889
  billingState.isExternallyBilledTurn,
8512
- entitlements
8890
+ entitlements,
8891
+ billingState.chargesOpenGeniCredits,
8892
+ billingState.countsTowardTokenCap
8513
8893
  );
8514
8894
  } catch (limitError) {
8515
8895
  let serializedRunState = null;
@@ -8802,6 +9182,8 @@ async function runTurnStreamAttempt(deps) {
8802
9182
  turnAttemptId: input.attemptId,
8803
9183
  model: turn.model,
8804
9184
  externallyBilled: billingState.isExternallyBilledTurn,
9185
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
9186
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
8805
9187
  usage: aggregateUsage,
8806
9188
  normalizedUsage: normalizedAggregateUsage,
8807
9189
  sourceKey: aggregateSourceKey,
@@ -8823,6 +9205,8 @@ async function runTurnStreamAttempt(deps) {
8823
9205
  sourceKey: aggregateSourceKey,
8824
9206
  usage: { usage: aggregateUsage },
8825
9207
  normalizedUsage: normalizedAggregateUsage,
9208
+ ...billing ? { billingPath: billing.billingPath } : {},
9209
+ ...billing?.upstreamProvider ? { upstreamProvider: billing.upstreamProvider } : {},
8826
9210
  servingAccountHash: aggregateAccountCtx.servingAccountHash,
8827
9211
  accountChangedFromPrevCall: aggregateAccountCtx.accountChangedFromPrevCall,
8828
9212
  emittedSourceKeys: emittedModelUsageSourceKeys
@@ -8955,6 +9339,7 @@ async function runTurnStreamAttempt(deps) {
8955
9339
  if (!attached.accepted) {
8956
9340
  return claimedResult({ status: "cancelled" });
8957
9341
  }
9342
+ await finishParallelSessionTitle();
8958
9343
  if (!await eventing.settle({
8959
9344
  events: [
8960
9345
  ...requestEvents,
@@ -8988,6 +9373,7 @@ async function runTurnStreamAttempt(deps) {
8988
9373
  const finalOutput = String(requireAgentStreamFinalOutput(eventing.stream.finalOutput));
8989
9374
  await historySink.reconcileConversationTruth({ requireDurable: true });
8990
9375
  await finalizeTurnOpStreamOps();
9376
+ await finishParallelSessionTitle();
8991
9377
  if (!await eventing.settle({
8992
9378
  events: [
8993
9379
  {
@@ -9049,125 +9435,161 @@ async function runTurnStreamAttempt(deps) {
9049
9435
  })) {
9050
9436
  return claimedResult({ status: "cancelled" });
9051
9437
  }
9052
- let retriedAfterCompaction = false;
9053
- while (true) {
9054
- try {
9055
- const result = await runStreamAttempt({
9056
- requireTerminalModelResponse: retriedAfterCompaction
9057
- });
9058
- if (retriedAfterCompaction) {
9059
- observability.info("context compaction recovery succeeded after in-activity retry", {
9060
- sessionId: input.sessionId,
9061
- turnId: activeTurnId
9438
+ if (generateSessionTitleInParallel && runtime.generateSessionTitle && !runtimeCancellationSignal.aborted) {
9439
+ const serviceTier = serviceTierForLatencyMode(
9440
+ turnExecutionPolicy.providerId,
9441
+ turnExecutionPolicy.latencyMode
9442
+ );
9443
+ parallelSessionTitle = startParallelSessionTitleGeneration({
9444
+ signal: runtimeCancellationSignal,
9445
+ generate: async (signal) => await withSessionTitleProviderRequestContext(
9446
+ () => runtime.generateSessionTitle(runSettings, sessionTitlePrompt, {
9447
+ ...resolvedModel ? {
9448
+ client: resolvedModel.client,
9449
+ provider: resolvedModel.provider,
9450
+ model: resolvedModel.model
9451
+ } : {},
9452
+ modelName: turnExecutionPolicy.upstreamModelId,
9453
+ ...serviceTier ? { serviceTier } : {},
9454
+ signal
9455
+ })
9456
+ ),
9457
+ onError: (error) => {
9458
+ observability.warn("parallel session title generation failed", {
9459
+ ...safeErrorDiagnostic(error),
9460
+ errorClass: "ParallelSessionTitleGenerationError",
9461
+ errorCode: "parallel_session_title_generation_failed",
9462
+ origin: "worker"
9062
9463
  });
9063
9464
  }
9064
- return result;
9065
- } catch (attemptError) {
9066
- const overflow = classifyContextWindowOverflowError(attemptError);
9067
- const compactionNeeded = findCompactionNeededError(attemptError);
9068
- const recoveryKind = compactionNeeded ? compactionNeeded.trigger === "operator" ? "operator" : "proactive" : overflow ? "overflow" : null;
9069
- if (!recoveryKind || !eventing.publish || !eventing.turnStartedPublished) {
9070
- throw attemptError;
9071
- }
9072
- await flushRuntimeBatcher();
9073
- await historySink.reconcileConversationTruth({ skipInputOnlyRows: true });
9074
- observability.warn("context compaction recovery attempted", {
9075
- sessionId: input.sessionId,
9076
- turnId: activeTurnId,
9077
- reason: recoveryKind,
9078
- ...safeErrorDiagnostic(attemptError),
9079
- signalTokens: compactionNeeded?.signalTokens,
9080
- thresholdTokens: compactionNeeded?.thresholdTokens
9081
- });
9082
- let compacted = false;
9083
- let compactionHandled = false;
9084
- let compactionFailureMessage = null;
9085
- let compactionRequestCleared = false;
9465
+ });
9466
+ }
9467
+ try {
9468
+ let retriedAfterCompaction = false;
9469
+ while (true) {
9086
9470
  try {
9087
- const outcome = await forceContextCompaction(
9088
- recoveryKind,
9089
- compactionNeeded?.signalTokens ?? null
9090
- );
9091
- compacted = outcome.compacted;
9092
- if (outcome.compacted) {
9093
- compactionHandled = true;
9094
- } else {
9095
- compactionHandled = recoveryKind === "operator" && outcome.requestConsumed;
9096
- if (!compactionHandled) {
9097
- compactionFailureMessage = compactionFailureReason(outcome.reason);
9098
- }
9099
- }
9100
- } catch (compactError) {
9101
- if (shouldRecoverCompactionProviderFailure(compactError)) throw compactError;
9102
- if (compactError instanceof TurnAttemptFencedError) throw compactError;
9103
- const landmark = await settleFailedContextCompactionLandmark(
9104
- db,
9105
- {
9106
- accountId: input.accountId,
9107
- workspaceId: input.workspaceId,
9471
+ const result = await runStreamAttempt({
9472
+ requireTerminalModelResponse: retriedAfterCompaction
9473
+ });
9474
+ if (retriedAfterCompaction) {
9475
+ observability.info("context compaction recovery succeeded after in-activity retry", {
9108
9476
  sessionId: input.sessionId,
9109
- turnId: activeTurnId,
9110
- executionGeneration: attempt.executionGeneration,
9111
- attemptId: input.attemptId
9112
- },
9113
- {
9114
- clearRequestedCompaction: recoveryKind === "operator",
9115
- publishLiveEvents: publishCompactionLiveEvents
9116
- }
9117
- );
9118
- compactionRequestCleared = landmark.requestConsumed;
9119
- if (!isCompactionSummaryFailure(compactError)) throw compactError;
9120
- const deferredSteer2 = await settleDeferredSteerAfterCompaction();
9121
- if (deferredSteer2) return deferredSteer2;
9122
- compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
9123
- observability.warn("context compaction recovery compaction failed", {
9477
+ turnId: activeTurnId
9478
+ });
9479
+ }
9480
+ return result;
9481
+ } catch (attemptError) {
9482
+ const overflow = classifyContextWindowOverflowError(attemptError);
9483
+ const compactionNeeded = findCompactionNeededError(attemptError);
9484
+ const recoveryKind = compactionNeeded ? compactionNeeded.trigger === "operator" ? "operator" : "proactive" : overflow ? "overflow" : null;
9485
+ if (!recoveryKind || !eventing.publish || !eventing.turnStartedPublished) {
9486
+ throw attemptError;
9487
+ }
9488
+ await flushRuntimeBatcher();
9489
+ await historySink.reconcileConversationTruth({ skipInputOnlyRows: true });
9490
+ observability.warn("context compaction recovery attempted", {
9124
9491
  sessionId: input.sessionId,
9125
9492
  turnId: activeTurnId,
9126
- ...safeErrorDiagnostic(compactError)
9493
+ reason: recoveryKind,
9494
+ ...safeErrorDiagnostic(attemptError),
9495
+ signalTokens: compactionNeeded?.signalTokens,
9496
+ thresholdTokens: compactionNeeded?.thresholdTokens
9127
9497
  });
9128
- }
9129
- if (!compactionHandled) {
9130
- const errorMessage = compactionFailureMessage ?? "compaction summarization failed: compaction produced no replacement history";
9131
- if (!await eventing.settle({
9132
- events: [
9498
+ let compacted = false;
9499
+ let compactionHandled = false;
9500
+ let compactionFailureMessage = null;
9501
+ let compactionRequestCleared = false;
9502
+ try {
9503
+ const outcome = await forceContextCompaction(
9504
+ recoveryKind,
9505
+ compactionNeeded?.signalTokens ?? null
9506
+ );
9507
+ compacted = outcome.compacted;
9508
+ if (outcome.compacted) {
9509
+ compactionHandled = true;
9510
+ } else {
9511
+ compactionHandled = recoveryKind === "operator" && outcome.requestConsumed;
9512
+ if (!compactionHandled) {
9513
+ compactionFailureMessage = compactionFailureReason(outcome.reason);
9514
+ }
9515
+ }
9516
+ } catch (compactError) {
9517
+ if (shouldRecoverCompactionProviderFailure(compactError)) throw compactError;
9518
+ if (compactError instanceof TurnAttemptFencedError) throw compactError;
9519
+ const landmark = await settleFailedContextCompactionLandmark(
9520
+ db,
9133
9521
  {
9134
- type: "turn.failed",
9135
- payload: {
9136
- error: errorMessage,
9137
- code: "context_compaction_failed",
9138
- retryable: false,
9139
- recovery: "user_message",
9140
- compacted: false
9141
- }
9522
+ accountId: input.accountId,
9523
+ workspaceId: input.workspaceId,
9524
+ sessionId: input.sessionId,
9525
+ turnId: activeTurnId,
9526
+ executionGeneration: attempt.executionGeneration,
9527
+ attemptId: input.attemptId
9142
9528
  },
9143
9529
  {
9144
- type: "session.status.changed",
9145
- payload: { status: "idle" }
9530
+ clearRequestedCompaction: recoveryKind === "operator",
9531
+ publishLiveEvents: publishCompactionLiveEvents
9146
9532
  }
9147
- ],
9148
- turnStatus: "failed",
9149
- sessionStatus: "idle",
9150
- activeTurnId: null,
9151
- ...recoveryKind === "operator" && !compactionRequestCleared ? { consumeRequestedCompactionFailure: true } : {}
9152
- })) {
9153
- return claimedResult({ status: "cancelled" });
9533
+ );
9534
+ compactionRequestCleared = landmark.requestConsumed;
9535
+ if (!isCompactionSummaryFailure(compactError)) throw compactError;
9536
+ await finishParallelSessionTitle();
9537
+ const deferredSteer2 = await settleDeferredSteerAfterCompaction();
9538
+ if (deferredSteer2) return deferredSteer2;
9539
+ compactionFailureMessage = String(compactionFailureReasonFromError(compactError));
9540
+ observability.warn("context compaction recovery compaction failed", {
9541
+ sessionId: input.sessionId,
9542
+ turnId: activeTurnId,
9543
+ ...safeErrorDiagnostic(compactError)
9544
+ });
9154
9545
  }
9155
- control.turnMetricOutcome = "failed";
9156
- control.activityStatus = "idle";
9157
- control.activityError = attemptError;
9158
- return claimedResult({ status: "idle", deferredUntilWake: true });
9546
+ if (!compactionHandled) {
9547
+ const errorMessage = compactionFailureMessage ?? "compaction summarization failed: compaction produced no replacement history";
9548
+ await finishParallelSessionTitle();
9549
+ if (!await eventing.settle({
9550
+ events: [
9551
+ {
9552
+ type: "turn.failed",
9553
+ payload: {
9554
+ error: errorMessage,
9555
+ code: "context_compaction_failed",
9556
+ retryable: false,
9557
+ recovery: "user_message",
9558
+ compacted: false
9559
+ }
9560
+ },
9561
+ {
9562
+ type: "session.status.changed",
9563
+ payload: { status: "idle" }
9564
+ }
9565
+ ],
9566
+ turnStatus: "failed",
9567
+ sessionStatus: "idle",
9568
+ activeTurnId: null,
9569
+ ...recoveryKind === "operator" && !compactionRequestCleared ? { consumeRequestedCompactionFailure: true } : {}
9570
+ })) {
9571
+ return claimedResult({ status: "cancelled" });
9572
+ }
9573
+ control.turnMetricOutcome = "failed";
9574
+ control.activityStatus = "idle";
9575
+ control.activityError = attemptError;
9576
+ return claimedResult({ status: "idle", deferredUntilWake: true });
9577
+ }
9578
+ await finishParallelSessionTitle();
9579
+ const deferredSteer = await settleDeferredSteerAfterCompaction();
9580
+ if (deferredSteer) return deferredSteer;
9581
+ retriedAfterCompaction = true;
9582
+ observability.info("context compaction recovery retrying turn after compaction", {
9583
+ sessionId: input.sessionId,
9584
+ turnId: activeTurnId,
9585
+ reason: recoveryKind,
9586
+ compacted
9587
+ });
9588
+ await prepareRunAttemptInput();
9159
9589
  }
9160
- const deferredSteer = await settleDeferredSteerAfterCompaction();
9161
- if (deferredSteer) return deferredSteer;
9162
- retriedAfterCompaction = true;
9163
- observability.info("context compaction recovery retrying turn after compaction", {
9164
- sessionId: input.sessionId,
9165
- turnId: activeTurnId,
9166
- reason: recoveryKind,
9167
- compacted
9168
- });
9169
- await prepareRunAttemptInput();
9170
9590
  }
9591
+ } finally {
9592
+ await cancelParallelSessionTitle();
9171
9593
  }
9172
9594
  }
9173
9595
 
@@ -9182,7 +9604,7 @@ import {
9182
9604
  workspaceCodexSubscriptionActive as workspaceCodexSubscriptionActive2,
9183
9605
  requireSession
9184
9606
  } from "@opengeni/db";
9185
- import { appendAndPublishTurnEventsFenced as appendAndPublishTurnEventsFenced2, publishDurableSessionEvents as publishDurableSessionEvents3 } from "@opengeni/events";
9607
+ import { appendAndPublishTurnEventsFenced as appendAndPublishTurnEventsFenced2, publishDurableSessionEvents as publishDurableSessionEvents4 } from "@opengeni/events";
9186
9608
  import {
9187
9609
  assertTurnExecutionPolicyMatchesConfigV1,
9188
9610
  resolveTurnExecutionPolicyV1 as resolveTurnExecutionPolicyV12
@@ -9191,17 +9613,35 @@ import {
9191
9613
  // src/activities/capabilities.ts
9192
9614
  import {
9193
9615
  environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes2,
9616
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
9617
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
9194
9618
  withCodexCatalogProvider,
9195
9619
  withWorkspaceGatewayCatalogProvider,
9196
9620
  withWorkspaceGatewayCredential,
9621
+ withWorkspaceOpenRouterCatalogProvider,
9622
+ withWorkspaceOpenRouterCredential,
9623
+ withOrganizationGatewayCatalogProvider,
9624
+ withOrganizationGatewayCredential,
9625
+ withOrganizationOpenRouterCatalogProvider,
9626
+ withOrganizationOpenRouterCredential,
9627
+ ORGANIZATION_GATEWAY_MODEL_ID_PREFIX,
9628
+ ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX,
9197
9629
  withXaiSubscriptionCatalogProvider
9198
9630
  } from "@opengeni/config";
9199
9631
  import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
9200
9632
  import {
9633
+ getWorkspaceGatewayCustomModelForExecution,
9634
+ getWorkspaceOpenRouterCustomModelForExecution,
9201
9635
  listSessionMcpServerMetadata,
9202
9636
  listSessionMcpServersForRun,
9637
+ listWorkspaceGatewayCustomModels,
9638
+ listWorkspaceOpenRouterCustomModels,
9203
9639
  workspaceCodexSubscriptionActive,
9204
- loadWorkspaceVercelAiGatewayApiKey
9640
+ loadWorkspaceVercelAiGatewayApiKey,
9641
+ loadWorkspaceOpenRouterApiKey,
9642
+ listOrganizationModelProviderCustomModelsForWorkspace,
9643
+ getOrganizationModelProviderCustomModelForExecution,
9644
+ loadOrganizationModelProviderApiKey
9205
9645
  } from "@opengeni/db";
9206
9646
  async function settingsWithSessionMcpServersForRun(db, workspaceId, sessionId, attemptId, settings, options) {
9207
9647
  const encryptionKey = environmentsEncryptionKeyBytes2(settings);
@@ -9263,15 +9703,79 @@ function withCodexProvider(settings) {
9263
9703
  function withXaiSubscriptionProvider(settings) {
9264
9704
  return withXaiSubscriptionCatalogProvider(settings);
9265
9705
  }
9266
- async function settingsWithWorkspaceGatewayCredential(db, workspaceId, settings) {
9267
- const catalogSettings = withWorkspaceGatewayCatalogProvider(settings);
9706
+ async function settingsWithWorkspaceGatewayCredential(db, accountId, workspaceId, settings, retainedProductModelId) {
9707
+ const activeCustomModels = await listWorkspaceGatewayCustomModels(db, { accountId, workspaceId });
9708
+ const retainedUpstreamModelId = retainedProductModelId?.startsWith(
9709
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX
9710
+ ) ? retainedProductModelId.slice(WORKSPACE_GATEWAY_MODEL_ID_PREFIX.length) : null;
9711
+ const retainedCustomModel = retainedUpstreamModelId ? await getWorkspaceGatewayCustomModelForExecution(db, {
9712
+ accountId,
9713
+ workspaceId,
9714
+ upstreamModelId: retainedUpstreamModelId
9715
+ }) : null;
9716
+ const customModels = retainedCustomModel && !activeCustomModels.some(
9717
+ (model) => model.upstreamModelId === retainedCustomModel.upstreamModelId
9718
+ ) ? [...activeCustomModels, retainedCustomModel] : activeCustomModels;
9719
+ const catalogSettings = withWorkspaceGatewayCatalogProvider(settings, customModels);
9268
9720
  const apiKey = await loadWorkspaceVercelAiGatewayApiKey(db, settings, workspaceId);
9269
- return apiKey ? withWorkspaceGatewayCredential(catalogSettings, apiKey) : catalogSettings;
9721
+ return apiKey ? withWorkspaceGatewayCredential(catalogSettings, apiKey, customModels) : catalogSettings;
9722
+ }
9723
+ async function settingsWithWorkspaceOpenRouterCredential(db, accountId, workspaceId, settings, retainedProductModelId) {
9724
+ const activeCustomModels = await listWorkspaceOpenRouterCustomModels(db, {
9725
+ accountId,
9726
+ workspaceId
9727
+ });
9728
+ const retainedUpstreamModelId = retainedProductModelId?.startsWith(
9729
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX
9730
+ ) ? retainedProductModelId.slice(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX.length) : null;
9731
+ const retainedCustomModel = retainedUpstreamModelId ? await getWorkspaceOpenRouterCustomModelForExecution(db, {
9732
+ accountId,
9733
+ workspaceId,
9734
+ upstreamModelId: retainedUpstreamModelId
9735
+ }) : null;
9736
+ const customModels = retainedCustomModel && !activeCustomModels.some(
9737
+ (model) => model.upstreamModelId === retainedCustomModel.upstreamModelId
9738
+ ) ? [...activeCustomModels, retainedCustomModel] : activeCustomModels;
9739
+ const catalogSettings = withWorkspaceOpenRouterCatalogProvider(settings, customModels);
9740
+ const apiKey = await loadWorkspaceOpenRouterApiKey(db, settings, workspaceId);
9741
+ return apiKey ? withWorkspaceOpenRouterCredential(catalogSettings, apiKey, customModels) : catalogSettings;
9742
+ }
9743
+ async function settingsWithOrganizationProviderCredentials(db, accountId, workspaceId, settings, retainedProductModelId) {
9744
+ const buildModels = async (providerKind, prefix) => {
9745
+ const active = await listOrganizationModelProviderCustomModelsForWorkspace(db, {
9746
+ accountId,
9747
+ workspaceId,
9748
+ providerKind
9749
+ });
9750
+ const upstreamModelId = retainedProductModelId?.startsWith(prefix) ? retainedProductModelId.slice(prefix.length) : null;
9751
+ const retained = upstreamModelId ? await getOrganizationModelProviderCustomModelForExecution(db, {
9752
+ accountId,
9753
+ workspaceId,
9754
+ providerKind,
9755
+ upstreamModelId
9756
+ }) : null;
9757
+ return retained && !active.some((model) => model.id === retained.id) ? [...active, retained] : active;
9758
+ };
9759
+ const gatewayModels = await buildModels("vercel_gateway", ORGANIZATION_GATEWAY_MODEL_ID_PREFIX);
9760
+ const openRouterModels = await buildModels("openrouter", ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX);
9761
+ const gatewayKey = await loadOrganizationModelProviderApiKey(db, settings, {
9762
+ accountId,
9763
+ workspaceId,
9764
+ providerKind: "vercel_gateway"
9765
+ });
9766
+ const gatewaySettings = gatewayKey ? withOrganizationGatewayCredential(settings, gatewayKey, gatewayModels) : withOrganizationGatewayCatalogProvider(settings, gatewayModels);
9767
+ const openRouterKey = await loadOrganizationModelProviderApiKey(db, settings, {
9768
+ accountId,
9769
+ workspaceId,
9770
+ providerKind: "openrouter"
9771
+ });
9772
+ return openRouterKey ? withOrganizationOpenRouterCredential(gatewaySettings, openRouterKey, openRouterModels) : withOrganizationOpenRouterCatalogProvider(gatewaySettings, openRouterModels);
9270
9773
  }
9271
9774
 
9272
9775
  // src/activities/agent-turn/claim.ts
9273
9776
  import {
9274
9777
  assertSessionAllowsProductModel,
9778
+ resolveCatalogSettings,
9275
9779
  resolveCodexAppsCredentialIdForRun
9276
9780
  } from "@opengeni/core";
9277
9781
 
@@ -9291,10 +9795,18 @@ import { readTurnExecutionPolicyV1 } from "@opengeni/contracts";
9291
9795
  function turnAttemptProducerId(input, turnId) {
9292
9796
  return `${input.workflowId}:${turnId}:${input.attemptId}`;
9293
9797
  }
9798
+ function codexCredentialLeaseHolderId(input, turnId) {
9799
+ const parts = [input.workflowId, turnId, input.attemptId];
9800
+ if (parts.some((part) => part.trim().length === 0)) {
9801
+ throw new Error("Codex credential lease holder requires a complete turn attempt identity");
9802
+ }
9803
+ return `codex-turn:${parts.map((part) => encodeURIComponent(part)).join(":")}`;
9804
+ }
9294
9805
  async function claimTurnAttempt(deps) {
9295
9806
  const {
9296
9807
  input,
9297
9808
  settings,
9809
+ catalogSourceSettings,
9298
9810
  db,
9299
9811
  bus,
9300
9812
  runtime,
@@ -9315,9 +9827,10 @@ async function claimTurnAttempt(deps) {
9315
9827
  claimedResult,
9316
9828
  acknowledgeLostAttemptOwnership
9317
9829
  } = deps;
9830
+ const deploymentCatalogSettings = (await resolveCatalogSettings(db, catalogSourceSettings)).settings;
9318
9831
  const validatePendingSystemUpdateAuthority = async (tx, update) => await validateIncidentTelemetrySystemUpdateAuthority({
9319
9832
  db: tx,
9320
- settings,
9833
+ settings: deploymentCatalogSettings,
9321
9834
  workspaceId: input.workspaceId,
9322
9835
  sessionId: input.sessionId,
9323
9836
  update
@@ -9337,12 +9850,15 @@ async function claimTurnAttempt(deps) {
9337
9850
  }
9338
9851
  const turn = claim.turn;
9339
9852
  attempt.turnId = turn.id;
9853
+ attempt.dispatchId = dispatchId;
9340
9854
  attempt.executionGeneration = turn.executionGeneration;
9341
9855
  attempt.providerRecoveryCount = providerRecoveryCountFromMetadata(turn.metadata);
9342
9856
  attempt.triggerEventId = turn.triggerEventId;
9857
+ leases.codex.holderId = codexCredentialLeaseHolderId(input, turn.id);
9343
9858
  attempt.redispatchesAtDispatch = Number(
9344
9859
  turn.metadata?.workerDeathRedispatches ?? 0
9345
9860
  );
9861
+ const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
9346
9862
  const session = await requireSession(db, input.workspaceId, input.sessionId);
9347
9863
  let installedApiIntegrations = [];
9348
9864
  const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
@@ -9350,7 +9866,7 @@ async function claimTurnAttempt(deps) {
9350
9866
  const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
9351
9867
  db,
9352
9868
  input.workspaceId,
9353
- settings,
9869
+ deploymentCatalogSettings,
9354
9870
  {
9355
9871
  ...credentialSubjectId ? { subjectId: credentialSubjectId } : {
9356
9872
  personalConnectionDelegations: turn.personalConnectionDelegations
@@ -9372,14 +9888,29 @@ async function claimTurnAttempt(deps) {
9372
9888
  codexSubscriptionActive
9373
9889
  );
9374
9890
  const xaiSettings = codexSettings.supergrokSubscriptionEnabled ? withXaiSubscriptionProvider(codexSettings) : codexSettings;
9375
- const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
9891
+ const gatewaySettings = await settingsWithWorkspaceGatewayCredential(
9892
+ db,
9893
+ input.accountId,
9894
+ input.workspaceId,
9895
+ xaiSettings,
9896
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model
9897
+ );
9898
+ const workspaceProviderSettings = await settingsWithWorkspaceOpenRouterCredential(
9899
+ db,
9900
+ input.accountId,
9901
+ input.workspaceId,
9902
+ gatewaySettings,
9903
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model
9904
+ );
9905
+ const capabilitySettings = await settingsWithOrganizationProviderCredentials(
9376
9906
  db,
9907
+ input.accountId,
9377
9908
  input.workspaceId,
9378
- xaiSettings
9909
+ workspaceProviderSettings,
9910
+ claimedPolicy.kind === "valid" ? claimedPolicy.policy.productModelId : turn.model
9379
9911
  );
9380
9912
  const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId) : null;
9381
9913
  runtime.configure(capabilitySettings);
9382
- const claimedPolicy = readTurnExecutionPolicyV1(turn.metadata);
9383
9914
  const policyForAbsent = claimedPolicy.kind === "valid" ? claimedPolicy.policy : resolveTurnExecutionPolicyV12(capabilitySettings, legacyTurnExecutionPolicyInput(turn));
9384
9915
  const installedPolicy = await installOrReadTurnExecutionPolicyForAttempt(db, {
9385
9916
  accountId: input.accountId,
@@ -9406,6 +9937,8 @@ async function claimTurnAttempt(deps) {
9406
9937
  assertSessionAllowsProductModel(session, turnExecutionPolicy.productModelId);
9407
9938
  const billingIdentity = turnExecutionPolicyBillingIdentity(turnExecutionPolicy);
9408
9939
  billingState.isExternallyBilledTurn = billingIdentity.externallyBilled;
9940
+ billingState.chargesOpenGeniCredits = verifiedExecutionPolicy.model.cost === "credits";
9941
+ billingState.countsTowardTokenCap = billingIdentity.countsTowardTokenCap;
9409
9942
  billingState.isCodexTurn = billingIdentity.codexSubscription;
9410
9943
  billingState.isXaiTurn = billingIdentity.xaiSubscription;
9411
9944
  const trigger = await getSessionEvent3(db, input.workspaceId, attempt.triggerEventId);
@@ -9441,12 +9974,14 @@ async function claimTurnAttempt(deps) {
9441
9974
  turnLifecycleMetricsFor(observability).start(attempt.turnId);
9442
9975
  await waitForTurnOperation(
9443
9976
  ensureRunAllowed(
9444
- settings,
9977
+ capabilitySettings,
9445
9978
  db,
9446
9979
  input.accountId,
9447
9980
  input.workspaceId,
9448
9981
  billingState.isExternallyBilledTurn,
9449
- entitlements
9982
+ entitlements,
9983
+ billingState.chargesOpenGeniCredits,
9984
+ billingState.countsTowardTokenCap
9450
9985
  ),
9451
9986
  cancellationSignal,
9452
9987
  void 0
@@ -9468,7 +10003,6 @@ async function claimTurnAttempt(deps) {
9468
10003
  eventing.heartbeatTimer = startActivityHeartbeat(activityContext, heartbeatDetails);
9469
10004
  let producerSeq = 0;
9470
10005
  const producerId = turnAttemptProducerId(input, attempt.turnId);
9471
- leases.codex.holderId = dispatchId;
9472
10006
  const modelUsageDispatchId = input.attemptId;
9473
10007
  const claimedModelUsageSourceKeys = /* @__PURE__ */ new Set();
9474
10008
  const emittedModelUsageSourceKeys = /* @__PURE__ */ new Set();
@@ -9554,6 +10088,7 @@ async function claimTurnAttempt(deps) {
9554
10088
  turnStatus: inputSettlement.turnStatus,
9555
10089
  sessionStatus: inputSettlement.sessionStatus,
9556
10090
  activeTurnId: inputSettlement.activeTurnId,
10091
+ ...inputSettlement.suppressGoalContinuation !== void 0 ? { suppressGoalContinuation: inputSettlement.suppressGoalContinuation } : {},
9557
10092
  events: inputs,
9558
10093
  ...runState ? { runState } : {},
9559
10094
  ...compactionRequestFailure ? { compactionRequestFailure } : {}
@@ -9566,7 +10101,7 @@ async function claimTurnAttempt(deps) {
9566
10101
  }
9567
10102
  recordCanonicalStartupMilestones(result.canonicalStartupMilestones);
9568
10103
  turnLifecycleMetricsFor(observability).progress(attempt.turnId);
9569
- await publishDurableSessionEvents3(bus, input.workspaceId, input.sessionId, result.events);
10104
+ await publishDurableSessionEvents4(bus, input.workspaceId, input.sessionId, result.events);
9570
10105
  if (inputSettlement.turnStatus === "requires_action") {
9571
10106
  await deliverChildRequiresActionToParent(
9572
10107
  { db, bus, settings, observability, wakeSessionWorkflow },
@@ -9647,17 +10182,16 @@ async function claimTurnAttempt(deps) {
9647
10182
  import {
9648
10183
  getSessionGoal as getSessionGoal2,
9649
10184
  acquireCodexCredentialLease,
9650
- armCodexCapacityWait as armCodexCapacityWait2,
10185
+ CodexCredentialLeaseAttemptFencedError,
10186
+ CodexCredentialFailoverExhaustedError,
9651
10187
  CODEX_CREDENTIAL_LEASE_TTL_MS as CODEX_CREDENTIAL_LEASE_TTL_MS2,
9652
- getCodexRotationSettings as getCodexRotationSettings2,
9653
- listCodexAccountStatuses as listCodexAccountStatuses3,
9654
- getSessionCodexState as getSessionCodexState2,
9655
10188
  recordSessionActiveCodexCredential,
9656
- setSessionCodexPinInTransaction as setSessionCodexPinInTransaction2,
9657
- setActiveCodexCredential,
9658
- withSessionCodexCapacityMutation as withSessionCodexCapacityMutation2
10189
+ setSessionCodexPinInTransaction,
10190
+ settleCodexCredentialFailover as settleCodexCredentialFailover2,
10191
+ withSessionCodexCapacityMutation
9659
10192
  } from "@opengeni/db";
9660
- import { publishDurableSessionEvents as publishDurableSessionEvents4 } from "@opengeni/events";
10193
+ import { CodexReloginRequired } from "@opengeni/codex";
10194
+ import { publishDurableSessionEvents as publishDurableSessionEvents5 } from "@opengeni/events";
9661
10195
 
9662
10196
  // src/activities/codex-fleet-shadow.ts
9663
10197
  import {
@@ -9860,6 +10394,7 @@ async function selectCodexTurnCapacity(deps) {
9860
10394
  observability,
9861
10395
  wakeSessionWorkflow,
9862
10396
  signalCodexCapacityWorkflow,
10397
+ dispatchId,
9863
10398
  control,
9864
10399
  attempt,
9865
10400
  billingState,
@@ -9867,6 +10402,7 @@ async function selectCodexTurnCapacity(deps) {
9867
10402
  providerTurn,
9868
10403
  leases,
9869
10404
  claimedResult,
10405
+ acknowledgeLostAttemptOwnership,
9870
10406
  turn,
9871
10407
  codexWorkspaceKey
9872
10408
  } = deps;
@@ -9882,123 +10418,62 @@ async function selectCodexTurnCapacity(deps) {
9882
10418
  const credentialSelectionStartedAt = performance.now();
9883
10419
  let credentialSelectionOutcome = "completed";
9884
10420
  try {
9885
- const sessionCodex = await getSessionCodexState2(db, input.workspaceId, input.sessionId);
9886
- const sessionPin = sessionCodex?.pinnedCredentialId ?? null;
9887
- const sessionPinSource = sessionCodex?.pinSource ?? null;
9888
- const selectForTurn = (context) => selectCodexCredentialLeaseForTurn({
10421
+ const selectForTurn = (context, lockedSessionCodexState2) => selectCodexCredentialLeaseForTurn({
9889
10422
  context,
9890
- leasingEnabled: settings.codexCredentialLeasingEnabled,
9891
10423
  sessionId: input.sessionId,
9892
- sessionPinnedCredentialId: sessionPin,
9893
- sessionPinSource,
9894
- sessionLastCredentialId: sessionCodex?.lastCredentialId ?? null,
10424
+ sessionPinnedCredentialId: lockedSessionCodexState2.pinnedCredentialId,
10425
+ sessionPinSource: lockedSessionCodexState2.pinSource,
10426
+ sessionLastCredentialId: lockedSessionCodexState2.lastCredentialId,
9895
10427
  now: /* @__PURE__ */ new Date()
9896
10428
  });
9897
- let leased;
9898
- let leaseAcquisitionStartedAtMs = null;
9899
- if (settings.codexCredentialLeasingEnabled) {
10429
+ let leaseAcquisitionStartedAtMs = performance.now();
10430
+ let leased = await acquireCodexCredentialLease(
10431
+ db,
10432
+ {
10433
+ accountId: input.accountId,
10434
+ workspaceId: input.workspaceId,
10435
+ sessionId: input.sessionId,
10436
+ turnId,
10437
+ attemptId: input.attemptId,
10438
+ executionGeneration: attempt.executionGeneration,
10439
+ workflowId: input.workflowId,
10440
+ workflowRunId: input.workflowRunId,
10441
+ dispatchId,
10442
+ expectedRedispatches: attempt.redispatchesAtDispatch,
10443
+ holderId,
10444
+ advanceActivePointer: true
10445
+ },
10446
+ selectForTurn
10447
+ );
10448
+ if (leased.decision.kind === "allCapped") {
10449
+ await refreshCappedCodexUsageRows(db, settings, input.workspaceId, leased.accounts, {
10450
+ signalCodexCapacityWorkflow,
10451
+ wakeSessionWorkflow
10452
+ });
9900
10453
  leaseAcquisitionStartedAtMs = performance.now();
9901
10454
  leased = await acquireCodexCredentialLease(
9902
10455
  db,
9903
10456
  {
9904
10457
  accountId: input.accountId,
9905
10458
  workspaceId: input.workspaceId,
10459
+ sessionId: input.sessionId,
9906
10460
  turnId,
10461
+ attemptId: input.attemptId,
10462
+ executionGeneration: attempt.executionGeneration,
10463
+ workflowId: input.workflowId,
10464
+ workflowRunId: input.workflowRunId,
10465
+ dispatchId,
10466
+ expectedRedispatches: attempt.redispatchesAtDispatch,
9907
10467
  holderId,
9908
- advanceActivePointer: sessionPin === null
10468
+ advanceActivePointer: true
9909
10469
  },
9910
10470
  selectForTurn
9911
10471
  );
9912
- } else {
9913
- const [rotation, accounts] = await Promise.all([
9914
- getCodexRotationSettings2(db, input.workspaceId),
9915
- listCodexAccountStatuses3(db, input.workspaceId)
9916
- ]);
9917
- const leaseAccounts = accounts.map((account) => ({
9918
- ...account,
9919
- activeLeaseCount: 0,
9920
- selectionCount: 0,
9921
- lastSelectedAt: null
9922
- }));
9923
- const activeCredentialId = rotation?.activeCredentialId ?? null;
9924
- const selected = selectForTurn({
9925
- accounts: leaseAccounts,
9926
- activeCredentialId,
9927
- rotationEnabled: rotation?.rotationEnabled ?? false,
9928
- leaseRotationEnabled: false,
9929
- rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
9930
- existingCredentialId: null,
9931
- policyScope: null,
9932
- unavailableDiagnostics: []
9933
- });
9934
- leased = {
9935
- ...selected,
9936
- accounts: leaseAccounts,
9937
- activeCredentialId,
9938
- rotationEnabled: rotation?.rotationEnabled ?? false,
9939
- rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
9940
- reused: false,
9941
- holderId: null,
9942
- generation: null,
9943
- leasedUntil: null,
9944
- unavailableDiagnostics: [],
9945
- advanceActivePointer: selected.advanceActivePointer !== false
9946
- };
9947
- }
9948
- if (leased.decision.kind === "allCapped") {
9949
- await refreshCappedCodexUsageRows(db, settings, input.workspaceId, leased.accounts, {
9950
- signalCodexCapacityWorkflow,
9951
- wakeSessionWorkflow
9952
- });
9953
- if (settings.codexCredentialLeasingEnabled) {
9954
- leaseAcquisitionStartedAtMs = performance.now();
9955
- leased = await acquireCodexCredentialLease(
9956
- db,
9957
- {
9958
- accountId: input.accountId,
9959
- workspaceId: input.workspaceId,
9960
- turnId,
9961
- holderId,
9962
- advanceActivePointer: sessionPin === null
9963
- },
9964
- selectForTurn
9965
- );
9966
- } else {
9967
- const [rotation, accounts] = await Promise.all([
9968
- getCodexRotationSettings2(db, input.workspaceId),
9969
- listCodexAccountStatuses3(db, input.workspaceId)
9970
- ]);
9971
- const leaseAccounts = accounts.map((account) => ({
9972
- ...account,
9973
- activeLeaseCount: 0,
9974
- selectionCount: 0,
9975
- lastSelectedAt: null
9976
- }));
9977
- const selected = selectForTurn({
9978
- accounts: leaseAccounts,
9979
- activeCredentialId: rotation?.activeCredentialId ?? null,
9980
- rotationEnabled: rotation?.rotationEnabled ?? false,
9981
- leaseRotationEnabled: false,
9982
- rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
9983
- existingCredentialId: null,
9984
- policyScope: null,
9985
- unavailableDiagnostics: []
9986
- });
9987
- leased = {
9988
- ...selected,
9989
- accounts: leaseAccounts,
9990
- activeCredentialId: rotation?.activeCredentialId ?? null,
9991
- rotationEnabled: rotation?.rotationEnabled ?? false,
9992
- rotationStrategy: rotation?.rotationStrategy ?? "most_remaining",
9993
- reused: false,
9994
- holderId: null,
9995
- generation: null,
9996
- leasedUntil: null,
9997
- unavailableDiagnostics: [],
9998
- advanceActivePointer: selected.advanceActivePointer !== false
9999
- };
10000
- }
10001
10472
  }
10473
+ const lockedSessionCodexState = leased.sessionCodexState;
10474
+ providerTurn.codexPolicySnapshot = leased.codexPolicySnapshot;
10475
+ const sessionPin = lockedSessionCodexState.pinnedCredentialId;
10476
+ const sessionPinSource = lockedSessionCodexState.pinSource;
10002
10477
  const rotationDecision = leased.decision;
10003
10478
  const selectedPinDisposition = classifyCodexPin({
10004
10479
  pinnedCredentialId: sessionPin,
@@ -10006,15 +10481,15 @@ async function selectCodexTurnCapacity(deps) {
10006
10481
  strategy: leased.rotationStrategy,
10007
10482
  rotationEnabled: leased.rotationEnabled
10008
10483
  });
10009
- if (selectedPinDisposition === "sharded" && leased.credentialId !== null && (sessionPinSource !== "policy" || sessionPin !== leased.credentialId)) {
10010
- const pinMutation = await withSessionCodexCapacityMutation2(
10484
+ if (!leased.codexPolicySnapshotReused && selectedPinDisposition === "sharded" && leased.credentialId !== null && (sessionPinSource !== "policy" || sessionPin !== leased.credentialId)) {
10485
+ const pinMutation = await withSessionCodexCapacityMutation(
10011
10486
  db,
10012
10487
  {
10013
10488
  workspaceId: input.workspaceId,
10014
10489
  reason: "codex_policy_pin_changed"
10015
10490
  },
10016
10491
  async (tx) => {
10017
- const changed = await setSessionCodexPinInTransaction2(
10492
+ const changed = await setSessionCodexPinInTransaction(
10018
10493
  tx,
10019
10494
  input.workspaceId,
10020
10495
  input.sessionId,
@@ -10035,14 +10510,14 @@ async function selectCodexTurnCapacity(deps) {
10035
10510
  pinMutation.wakeTargets
10036
10511
  );
10037
10512
  } else if (selectedPinDisposition === "clearStale") {
10038
- const pinMutation = await withSessionCodexCapacityMutation2(
10513
+ const pinMutation = await withSessionCodexCapacityMutation(
10039
10514
  db,
10040
10515
  {
10041
10516
  workspaceId: input.workspaceId,
10042
10517
  reason: "codex_stale_policy_pin_cleared"
10043
10518
  },
10044
10519
  async (tx) => {
10045
- const changed = await setSessionCodexPinInTransaction2(
10520
+ const changed = await setSessionCodexPinInTransaction(
10046
10521
  tx,
10047
10522
  input.workspaceId,
10048
10523
  input.sessionId,
@@ -10063,16 +10538,14 @@ async function selectCodexTurnCapacity(deps) {
10063
10538
  pinMutation.wakeTargets
10064
10539
  );
10065
10540
  }
10066
- if (!settings.codexCredentialLeasingEnabled && leased.advanceActivePointer && sessionPin === null && rotationDecision.kind === "active" && rotationDecision.moved) {
10067
- await setActiveCodexCredential(db, input.workspaceId, rotationDecision.credentialId);
10068
- }
10069
10541
  providerTurn.effectiveCodexCredentialId = leased.credentialId;
10542
+ providerTurn.codexCredentialFailoverLimit = leased.failoverLimit;
10070
10543
  leases.codex.generation = leased.generation;
10071
10544
  leases.codex.confirmedUntilMs = leased.leasedUntil && leaseAcquisitionStartedAtMs !== null ? leaseAcquisitionStartedAtMs + CODEX_CREDENTIAL_LEASE_TTL_MS2 : null;
10072
10545
  leases.codex.held = providerTurn.effectiveCodexCredentialId !== null && leased.holderId !== null && leased.generation !== null && leases.codex.confirmedUntilMs !== null;
10073
10546
  if (leases.codex.held) leases.codex.startHeartbeat();
10074
- const actualOutcome = providerTurn.effectiveCodexCredentialId ? "selected" : rotationDecision.kind === "allCapped" ? "waiting" : "none";
10075
- const actualReason = providerTurn.effectiveCodexCredentialId ? leased.reused ? "lease_reused" : sessionPin === providerTurn.effectiveCodexCredentialId ? "pin" : rotationDecision.kind === "active" && rotationDecision.moved ? "rotation" : "active" : rotationDecision.kind === "allCapped" ? "all_capped" : "none";
10547
+ const actualOutcome = providerTurn.effectiveCodexCredentialId ? "selected" : rotationDecision.kind === "allCapped" || rotationDecision.kind === "allocatorDisabled" ? "waiting" : "none";
10548
+ const actualReason = providerTurn.effectiveCodexCredentialId ? leased.reused ? "lease_reused" : sessionPin === providerTurn.effectiveCodexCredentialId ? "pin" : rotationDecision.kind === "active" && rotationDecision.moved ? "rotation" : "active" : rotationDecision.kind === "allCapped" ? "all_capped" : rotationDecision.kind === "allocatorDisabled" ? "allocator_disabled" : "none";
10076
10549
  const fencedInFlight = leased.reused;
10077
10550
  const shadowResult = await publishCodexFleetShadowDecisionV1({
10078
10551
  enabled: settings.codexFleetPolicyShadowEnabled,
@@ -10081,7 +10554,7 @@ async function selectCodexTurnCapacity(deps) {
10081
10554
  actualCredentialId: providerTurn.effectiveCodexCredentialId,
10082
10555
  actualOutcome,
10083
10556
  actualReason,
10084
- affinityCredentialId: fencedInFlight ? providerTurn.effectiveCodexCredentialId : sessionPin ?? sessionCodex?.lastCredentialId ?? null,
10557
+ affinityCredentialId: fencedInFlight ? providerTurn.effectiveCodexCredentialId : sessionPin ?? lockedSessionCodexState.lastCredentialId ?? null,
10085
10558
  fencedInFlight,
10086
10559
  nearExhaustionPct: settings.codexRotationNearExhaustionPct,
10087
10560
  now: /* @__PURE__ */ new Date(),
@@ -10145,7 +10618,7 @@ async function selectCodexTurnCapacity(deps) {
10145
10618
  depth: poolDepth
10146
10619
  });
10147
10620
  }
10148
- if (providerTurn.effectiveCodexCredentialId === null && leased.accounts.length > 0 && leased.accounts.every((account) => !account.allocatorEnabled) && turnId) {
10621
+ if (providerTurn.effectiveCodexCredentialId === null && leased.accounts.length > 0 && (rotationDecision.kind === "allocatorDisabled" || leased.accounts.every((account) => !account.allocatorEnabled)) && turnId) {
10149
10622
  if (turn.source === "compaction") {
10150
10623
  if (!await eventing.settle({
10151
10624
  events: [
@@ -10172,66 +10645,146 @@ async function selectCodexTurnCapacity(deps) {
10172
10645
  control.activityStatus = "idle";
10173
10646
  return { exit: claimedResult({ status: "idle", deferredUntilWake: true }) };
10174
10647
  }
10175
- const goal = await getSessionGoal2(db, input.workspaceId, input.sessionId).catch(() => null);
10648
+ const goal = await getSessionGoal2(db, input.workspaceId, input.sessionId);
10176
10649
  const activeGoal = goal?.status === "active" ? goal : null;
10177
- const armed = await armCodexCapacityWait2(db, {
10178
- accountId: input.accountId,
10179
- workspaceId: input.workspaceId,
10180
- sessionId: input.sessionId,
10181
- turnId,
10182
- attemptId: input.attemptId,
10183
- workflowId: input.workflowId,
10184
- goalId: activeGoal?.id ?? null,
10185
- goalVersion: activeGoal?.version ?? null,
10186
- earliestResetAt: null,
10187
- resetKind: "bounded_refresh",
10188
- failurePayload: {
10189
- error: "All connected Codex subscriptions are disabled for new allocations.",
10190
- code: "codex_allocator_disabled",
10191
- detail: "waiting for a credential to be re-enabled, reconnected, or added"
10650
+ const evaluated = await armAndReconcileCodexCapacityWait(
10651
+ { db, bus },
10652
+ {
10653
+ accountId: input.accountId,
10654
+ workspaceId: input.workspaceId,
10655
+ sessionId: input.sessionId,
10656
+ turnId,
10657
+ attemptId: input.attemptId,
10658
+ workflowId: input.workflowId,
10659
+ goalId: activeGoal?.id ?? null,
10660
+ goalVersion: activeGoal?.version ?? null,
10661
+ earliestResetAt: null,
10662
+ resetKind: "mutation_only",
10663
+ failurePayload: {
10664
+ error: rotationDecision.kind === "allocatorDisabled" ? "The policy-selected Codex subscription is disabled for new allocations." : "All connected Codex subscriptions are disabled for new allocations.",
10665
+ code: "codex_allocator_disabled",
10666
+ detail: rotationDecision.kind === "allocatorDisabled" ? "waiting for the selected credential to be re-enabled under the accepted source policy" : "waiting for a credential in the accepted source pool to be re-enabled, reconnected, or added"
10667
+ }
10192
10668
  }
10193
- });
10194
- if (armed.action === "waiting") {
10195
- await publishDurableSessionEvents4(bus, input.workspaceId, input.sessionId, armed.events);
10669
+ );
10670
+ if (evaluated.action === "resumed") {
10671
+ control.turnMetricOutcome = "recovering";
10672
+ control.activityStatus = "recovering";
10673
+ return { exit: claimedResult({ status: "recovering" }) };
10674
+ }
10675
+ if (evaluated.action === "waiting") {
10196
10676
  control.turnMetricOutcome = "recovering";
10197
10677
  control.activityStatus = "waiting_capacity";
10198
10678
  return {
10199
10679
  exit: claimedResult({
10200
10680
  status: "waiting_capacity",
10201
10681
  capacityWait: {
10202
- waiterId: armed.waiter.id,
10203
- generation: armed.waiter.generation,
10204
- nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
10205
- wakeRevision: armed.waiter.wakeRevision
10682
+ waiterId: evaluated.waiter.id,
10683
+ generation: evaluated.waiter.generation,
10684
+ nextCheckAt: evaluated.waiter.nextCheckAt.toISOString(),
10685
+ wakeRevision: evaluated.waiter.wakeRevision
10206
10686
  }
10207
10687
  })
10208
10688
  };
10209
10689
  }
10210
- if (!await eventing.settle({
10211
- events: [
10212
- {
10213
- type: "turn.failed",
10214
- payload: {
10215
- error: "All connected Codex subscriptions are disabled for new allocations.",
10216
- code: "codex_allocator_disabled",
10217
- retryable: false,
10218
- recovery: "user_message"
10690
+ acknowledgeLostAttemptOwnership();
10691
+ control.turnMetricOutcome = "cancelled";
10692
+ control.activityStatus = "cancelled";
10693
+ return { exit: claimedResult({ status: "cancelled" }) };
10694
+ }
10695
+ if (rotationDecision.kind === "none") {
10696
+ if (leased.poolAccountCount === 0 && !leased.codexPolicySnapshotReused && leased.codexPolicySnapshot?.source !== "disabled") {
10697
+ throw new CodexReloginRequired("No Codex subscription is connected for this workspace.");
10698
+ }
10699
+ const noneReason = sessionPinSource === "manual" && sessionPin !== null ? {
10700
+ code: "codex_manual_pin_unavailable",
10701
+ error: "The pinned Codex subscription is unavailable for this turn.",
10702
+ detail: "waiting for the pinned account to reconnect or become allocatable under the accepted source policy"
10703
+ } : !leased.rotationEnabled && leased.activeCredentialId === null ? {
10704
+ code: "codex_active_pointer_unavailable",
10705
+ error: "The Codex active subscription pointer is unavailable for this turn.",
10706
+ detail: "waiting for the accepted active account to be restored or become allocatable"
10707
+ } : leased.poolAccountCount > leased.accounts.length ? {
10708
+ code: "codex_policy_pool_unavailable",
10709
+ error: "No Codex subscription matches the accepted account-selection policy.",
10710
+ detail: "waiting for a matching credential to reconnect or become allocatable under the accepted source policy"
10711
+ } : {
10712
+ code: "codex_credential_unavailable",
10713
+ error: "No Codex subscription is currently available under the accepted policy.",
10714
+ detail: "waiting for a matching credential to reconnect or become allocatable under the accepted source policy"
10715
+ };
10716
+ if (turn.source === "compaction") {
10717
+ if (!await eventing.settle({
10718
+ events: [
10719
+ {
10720
+ type: "turn.cancelled",
10721
+ payload: {
10722
+ maintenance: "context_compaction",
10723
+ reason: noneReason.code,
10724
+ requestPreserved: true
10725
+ }
10726
+ },
10727
+ {
10728
+ type: "session.status.changed",
10729
+ payload: { status: "idle" }
10730
+ }
10731
+ ],
10732
+ turnStatus: "cancelled",
10733
+ sessionStatus: "idle",
10734
+ activeTurnId: null
10735
+ })) {
10736
+ return { exit: claimedResult({ status: "cancelled" }) };
10737
+ }
10738
+ control.turnMetricOutcome = "cancelled";
10739
+ control.activityStatus = "idle";
10740
+ return { exit: claimedResult({ status: "idle", deferredUntilWake: true }) };
10741
+ }
10742
+ const goal = await getSessionGoal2(db, input.workspaceId, input.sessionId);
10743
+ const activeGoal = goal?.status === "active" ? goal : null;
10744
+ const evaluated = await armAndReconcileCodexCapacityWait(
10745
+ { db, bus },
10746
+ {
10747
+ accountId: input.accountId,
10748
+ workspaceId: input.workspaceId,
10749
+ sessionId: input.sessionId,
10750
+ turnId,
10751
+ attemptId: input.attemptId,
10752
+ workflowId: input.workflowId,
10753
+ goalId: activeGoal?.id ?? null,
10754
+ goalVersion: activeGoal?.version ?? null,
10755
+ earliestResetAt: null,
10756
+ resetKind: "mutation_only",
10757
+ failurePayload: {
10758
+ error: noneReason.error,
10759
+ code: noneReason.code,
10760
+ detail: noneReason.detail
10761
+ }
10762
+ }
10763
+ );
10764
+ if (evaluated.action === "resumed") {
10765
+ control.turnMetricOutcome = "recovering";
10766
+ control.activityStatus = "recovering";
10767
+ return { exit: claimedResult({ status: "recovering" }) };
10768
+ }
10769
+ if (evaluated.action === "waiting") {
10770
+ control.turnMetricOutcome = "recovering";
10771
+ control.activityStatus = "waiting_capacity";
10772
+ return {
10773
+ exit: claimedResult({
10774
+ status: "waiting_capacity",
10775
+ capacityWait: {
10776
+ waiterId: evaluated.waiter.id,
10777
+ generation: evaluated.waiter.generation,
10778
+ nextCheckAt: evaluated.waiter.nextCheckAt.toISOString(),
10779
+ wakeRevision: evaluated.waiter.wakeRevision
10219
10780
  }
10220
- },
10221
- {
10222
- type: "session.status.changed",
10223
- payload: { status: "idle" }
10224
- }
10225
- ],
10226
- turnStatus: "failed",
10227
- sessionStatus: "idle",
10228
- activeTurnId: null
10229
- })) {
10230
- return { exit: claimedResult({ status: "cancelled" }) };
10781
+ })
10782
+ };
10231
10783
  }
10232
- control.turnMetricOutcome = "failed";
10233
- control.activityStatus = "idle";
10234
- return { exit: claimedResult({ status: "idle" }) };
10784
+ acknowledgeLostAttemptOwnership();
10785
+ control.turnMetricOutcome = "cancelled";
10786
+ control.activityStatus = "cancelled";
10787
+ return { exit: claimedResult({ status: "cancelled" }) };
10235
10788
  }
10236
10789
  if (rotationDecision.kind === "allCapped" && turnId) {
10237
10790
  if (turn.source === "compaction") {
@@ -10260,88 +10813,64 @@ async function selectCodexTurnCapacity(deps) {
10260
10813
  control.activityStatus = "idle";
10261
10814
  return { exit: claimedResult({ status: "idle", deferredUntilWake: true }) };
10262
10815
  }
10263
- const goal = await getSessionGoal2(db, input.workspaceId, input.sessionId).catch(() => null);
10816
+ const goal = await getSessionGoal2(db, input.workspaceId, input.sessionId);
10264
10817
  const goalActive = Boolean(goal && goal.status === "active");
10265
10818
  const resumeMs = computeIdleDelayMs(
10266
10819
  rotationDecision.earliestResetAt,
10267
10820
  /* @__PURE__ */ new Date(),
10268
10821
  CODEX_USAGE_LIMIT_MAX_RESUME_MS
10269
10822
  );
10823
+ const allConnectedAccountsWereConsidered = leased.rotationEnabled && selectedPinDisposition !== "manual";
10824
+ const capacityDetail = allConnectedAccountsWereConsidered ? "all connected Codex subscriptions are rate-limited" : selectedPinDisposition === "manual" ? "the pinned Codex subscription is rate-limited" : "the active Codex subscription is rate-limited";
10270
10825
  const failurePayload = codexUsageLimitFailurePayload(
10271
10826
  { resetsInSeconds: Math.ceil(resumeMs / 1e3) },
10272
- "all connected Codex subscriptions are rate-limited",
10273
- { allAccounts: true }
10827
+ capacityDetail,
10828
+ { allAccounts: allConnectedAccountsWereConsidered }
10274
10829
  );
10275
10830
  const authoritativeResetAt = authoritativeCodexCapacityResetAt(leased.accounts, /* @__PURE__ */ new Date());
10276
- const armed = await armCodexCapacityWait2(db, {
10277
- accountId: input.accountId,
10278
- workspaceId: input.workspaceId,
10279
- sessionId: input.sessionId,
10280
- turnId,
10281
- attemptId: input.attemptId,
10282
- workflowId: input.workflowId,
10283
- goalId: goalActive && goal ? goal.id : null,
10284
- goalVersion: goalActive && goal ? goal.version : null,
10285
- earliestResetAt: authoritativeResetAt,
10286
- resetKind: authoritativeResetAt ? "authoritative" : "bounded_refresh",
10287
- failurePayload
10288
- });
10289
- if (armed.action === "waiting") {
10290
- await publishDurableSessionEvents4(bus, input.workspaceId, input.sessionId, armed.events);
10831
+ const evaluated = await armAndReconcileCodexCapacityWait(
10832
+ { db, bus },
10833
+ {
10834
+ accountId: input.accountId,
10835
+ workspaceId: input.workspaceId,
10836
+ sessionId: input.sessionId,
10837
+ turnId,
10838
+ attemptId: input.attemptId,
10839
+ workflowId: input.workflowId,
10840
+ goalId: goalActive && goal ? goal.id : null,
10841
+ goalVersion: goalActive && goal ? goal.version : null,
10842
+ earliestResetAt: authoritativeResetAt,
10843
+ resetKind: authoritativeResetAt ? "authoritative" : "bounded_refresh",
10844
+ failurePayload
10845
+ }
10846
+ );
10847
+ if (evaluated.action === "resumed") {
10848
+ control.turnMetricOutcome = "recovering";
10849
+ control.activityStatus = "recovering";
10850
+ return { exit: claimedResult({ status: "recovering" }) };
10851
+ }
10852
+ if (evaluated.action === "waiting") {
10291
10853
  control.turnMetricOutcome = "recovering";
10292
10854
  control.activityStatus = "waiting_capacity";
10293
10855
  return {
10294
10856
  exit: claimedResult({
10295
10857
  status: "waiting_capacity",
10296
10858
  capacityWait: {
10297
- waiterId: armed.waiter.id,
10298
- generation: armed.waiter.generation,
10299
- nextCheckAt: armed.waiter.nextCheckAt.toISOString(),
10300
- wakeRevision: armed.waiter.wakeRevision
10859
+ waiterId: evaluated.waiter.id,
10860
+ generation: evaluated.waiter.generation,
10861
+ nextCheckAt: evaluated.waiter.nextCheckAt.toISOString(),
10862
+ wakeRevision: evaluated.waiter.wakeRevision
10301
10863
  }
10302
10864
  })
10303
10865
  };
10304
10866
  }
10305
- if (!await eventing.settle({
10306
- events: [
10307
- // `rotated:true` (Finding 2): the proactive all-capped wait is the SAME
10308
- // rotation-wait state as the reactive all-capped path, so it must freeze
10309
- // autoContinuations identically (evaluateGoalContinuation reads this marker)
10310
- // — a goal waiting out a long reset must not burn its continuation budget on
10311
- // the proactive path while the reactive path spares it.
10312
- {
10313
- type: "turn.failed",
10314
- payload: {
10315
- ...failurePayload,
10316
- recovery: goalActive ? "goal_continuation" : "user_message",
10317
- rotated: true
10318
- }
10319
- },
10320
- {
10321
- type: "session.status.changed",
10322
- payload: { status: "idle" }
10323
- }
10324
- ],
10325
- turnStatus: "failed",
10326
- sessionStatus: "idle",
10327
- activeTurnId: null
10328
- })) {
10329
- return { exit: claimedResult({ status: "cancelled" }) };
10330
- }
10331
- control.turnMetricOutcome = "failed";
10332
- control.activityStatus = "idle";
10333
- return {
10334
- exit: claimedResult(
10335
- goalActive ? {
10336
- status: "idle",
10337
- continueDelayMs: resumeMs,
10338
- idleUntilReset: true
10339
- } : { status: "idle" }
10340
- )
10341
- };
10867
+ acknowledgeLostAttemptOwnership();
10868
+ control.turnMetricOutcome = "cancelled";
10869
+ control.activityStatus = "cancelled";
10870
+ return { exit: claimedResult({ status: "cancelled" }) };
10342
10871
  }
10343
10872
  if (providerTurn.effectiveCodexCredentialId) {
10344
- const priorAccountId = sessionCodex?.lastCredentialId ?? null;
10873
+ const priorAccountId = lockedSessionCodexState.lastCredentialId;
10345
10874
  if (priorAccountId !== providerTurn.effectiveCodexCredentialId) {
10346
10875
  await recordSessionActiveCodexCredential(
10347
10876
  db,
@@ -10387,6 +10916,58 @@ async function selectCodexTurnCapacity(deps) {
10387
10916
  }
10388
10917
  } catch (error) {
10389
10918
  credentialSelectionOutcome = "failed";
10919
+ if (error instanceof CodexCredentialLeaseAttemptFencedError) {
10920
+ acknowledgeLostAttemptOwnership();
10921
+ control.activityStatus = "cancelled";
10922
+ control.turnMetricOutcome = "cancelled";
10923
+ return { exit: claimedResult({ status: "cancelled" }) };
10924
+ }
10925
+ if (error instanceof CodexCredentialFailoverExhaustedError) {
10926
+ const settlement = await settleCodexCredentialFailover2(db, {
10927
+ accountId: input.accountId,
10928
+ workspaceId: input.workspaceId,
10929
+ sessionId: input.sessionId,
10930
+ turnId,
10931
+ attemptId: input.attemptId,
10932
+ holderId: null,
10933
+ generation: null,
10934
+ expectedRedispatches: attempt.redispatchesAtDispatch,
10935
+ maxFailovers: error.maxFailovers,
10936
+ recoveryPayload: {
10937
+ triggerEventId: attempt.triggerEventId,
10938
+ reason: "codex_credential_failover_exhausted"
10939
+ },
10940
+ failedPayload: {
10941
+ error: "Automatic Codex credential failover stopped after every bounded account attempt was consumed. Send a new message after checking account health or capacity.",
10942
+ code: "codex_credential_failover_exhausted",
10943
+ retryable: false,
10944
+ recovery: "user_message",
10945
+ failoverCount: error.failoverCount,
10946
+ maxFailovers: error.maxFailovers
10947
+ }
10948
+ });
10949
+ if (settlement.action === "limit_exceeded") {
10950
+ await publishDurableSessionEvents5(
10951
+ bus,
10952
+ input.workspaceId,
10953
+ input.sessionId,
10954
+ settlement.events
10955
+ );
10956
+ control.activityStatus = "idle";
10957
+ control.turnMetricOutcome = "failed";
10958
+ await deliverFailedChildTurnToParent(
10959
+ { db, bus, settings, observability, wakeSessionWorkflow },
10960
+ input.workspaceId,
10961
+ input.sessionId,
10962
+ turnId
10963
+ );
10964
+ return { exit: claimedResult({ status: "idle" }) };
10965
+ }
10966
+ acknowledgeLostAttemptOwnership();
10967
+ control.activityStatus = "cancelled";
10968
+ control.turnMetricOutcome = "cancelled";
10969
+ return { exit: claimedResult({ status: "cancelled" }) };
10970
+ }
10390
10971
  throw error;
10391
10972
  } finally {
10392
10973
  recordTurnStartupPhase(observability, {
@@ -10432,6 +11013,9 @@ import {
10432
11013
  } from "@opengeni/contracts";
10433
11014
 
10434
11015
  // src/activities/agent-turn/tool-policy.ts
11016
+ import {
11017
+ desktopCapableBackend
11018
+ } from "@opengeni/runtime";
10435
11019
  import {
10436
11020
  isDirectOpenAiApiBaseUrl
10437
11021
  } from "@opengeni/config";
@@ -10443,7 +11027,8 @@ import {
10443
11027
  getGeneratedImageArtifact,
10444
11028
  markImageGenerationOperationOutcomeUnknown,
10445
11029
  markImageGenerationOperationRetentionFailed,
10446
- prepareImageGenerationOperation
11030
+ prepareImageGenerationOperation,
11031
+ resetImageGenerationOperationBeforeProviderDispatch
10447
11032
  } from "@opengeni/db";
10448
11033
  import { createHash as createHash8 } from "crypto";
10449
11034
  var ImageGenerationOutcomeUnknownError = class extends Error {
@@ -10468,6 +11053,7 @@ var ImageGenerationRetentionFailedError = class extends Error {
10468
11053
  var imageGenerationOperationPorts = {
10469
11054
  prepare: prepareImageGenerationOperation,
10470
11055
  begin: beginImageGenerationOperation,
11056
+ resetBeforeProviderDispatch: resetImageGenerationOperationBeforeProviderDispatch,
10471
11057
  retain: retainGeneratedImage,
10472
11058
  complete: completeImageGenerationOperation,
10473
11059
  markOutcomeUnknown: markImageGenerationOperationOutcomeUnknown,
@@ -10514,7 +11100,9 @@ async function executeImageGenerationOperation(input, ports = imageGenerationOpe
10514
11100
  accountId: input.accountId,
10515
11101
  workspaceId: input.workspaceId,
10516
11102
  operationId: identity.operationId,
10517
- operationKey: identity.operationKey
11103
+ operationKey: identity.operationKey,
11104
+ providerBindingHash: input.providerBindingHash,
11105
+ expectedArtifactId: identity.artifactId
10518
11106
  });
10519
11107
  if (!begun.started || begun.operation.status !== "provider_started") {
10520
11108
  const recovered = await ports.recover(input, {
@@ -10529,6 +11117,16 @@ async function executeImageGenerationOperation(input, ports = imageGenerationOpe
10529
11117
  try {
10530
11118
  output = await input.generate();
10531
11119
  } catch (error) {
11120
+ if (input.isProviderDispatchRejected?.(error) === true) {
11121
+ await ports.resetBeforeProviderDispatch(input.db, {
11122
+ accountId: input.accountId,
11123
+ workspaceId: input.workspaceId,
11124
+ operationId: identity.operationId,
11125
+ operationKey: identity.operationKey,
11126
+ error: error instanceof Error ? error.message : String(error)
11127
+ }).catch(() => void 0);
11128
+ throw error;
11129
+ }
10532
11130
  await ports.markOutcomeUnknown(input.db, {
10533
11131
  accountId: input.accountId,
10534
11132
  workspaceId: input.workspaceId,
@@ -10660,13 +11258,9 @@ function uuidFromDigest3(digestValue) {
10660
11258
  // src/activities/agent-turn/tool-policy.ts
10661
11259
  function structuredToolTransportForTurn(resolvedModel) {
10662
11260
  if (!resolvedModel) return true;
10663
- if (resolvedModel.provider.api === "chat") return false;
10664
- return ![
10665
- "codex-subscription",
10666
- "xai-subscription",
10667
- "vercel-gateway-managed",
10668
- "vercel-gateway-workspace"
10669
- ].includes(resolvedModel.provider.kind);
11261
+ const provider = resolvedModel.provider;
11262
+ if (provider.api === "chat" || provider.kind === "codex-subscription") return false;
11263
+ return provider.wireProfile === "azure-openai" || provider.builtin && provider.id === "openai" && provider.baseUrl === void 0;
10670
11264
  }
10671
11265
  function lazyToolTransportForTurn(resolvedModel) {
10672
11266
  if (!resolvedModel) return "openai_native";
@@ -10966,7 +11560,7 @@ async function prepareGovernanceAndModel(deps) {
10966
11560
 
10967
11561
  // src/activities/agent-turn/compaction-prep.ts
10968
11562
  import { hasPendingSteerAfterContextCompaction, isSessionCompactionRequested as isSessionCompactionRequested2 } from "@opengeni/db";
10969
- import { publishDurableSessionEvents as publishDurableSessionEvents5 } from "@opengeni/events";
11563
+ import { publishDurableSessionEvents as publishDurableSessionEvents6 } from "@opengeni/events";
10970
11564
  import {
10971
11565
  appendSessionInstructions,
10972
11566
  appendWorkspaceGovernance,
@@ -11036,6 +11630,8 @@ async function prepareCompaction(deps) {
11036
11630
  providerApi: resolvedModel?.provider.api ?? "responses",
11037
11631
  model: resolvedModel?.configured.id ?? turn.model,
11038
11632
  externallyBilled: billingState.isExternallyBilledTurn,
11633
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
11634
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
11039
11635
  turnAttemptId: input.attemptId,
11040
11636
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
11041
11637
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
@@ -11100,10 +11696,10 @@ async function prepareCompaction(deps) {
11100
11696
  });
11101
11697
  }) : void 0;
11102
11698
  const publishCompactionLiveEvents = async (events) => {
11103
- await publishDurableSessionEvents5(bus, input.workspaceId, input.sessionId, events);
11699
+ await publishDurableSessionEvents6(bus, input.workspaceId, input.sessionId, events);
11104
11700
  };
11105
11701
  const publishCompactionOutcomeEvents = async (events) => {
11106
- await publishDurableSessionEvents5(
11702
+ await publishDurableSessionEvents6(
11107
11703
  bus,
11108
11704
  input.workspaceId,
11109
11705
  input.sessionId,
@@ -11113,6 +11709,7 @@ async function prepareCompaction(deps) {
11113
11709
  const compactionModeOptions = {
11114
11710
  codexCompactionMode: session.codexCompactionMode,
11115
11711
  isCodexSubscriptionTurn: billingState.isCodexTurn,
11712
+ onCompactionStarted: (trigger) => recordContextCompactionStarted(observability, trigger),
11116
11713
  publishLiveEvents: publishCompactionLiveEvents,
11117
11714
  ...remoteCompactionRequester ? { requestRemoteCompactionV2: remoteCompactionRequester } : {}
11118
11715
  };
@@ -11549,6 +12146,12 @@ import {
11549
12146
  sandboxWarmRateMicrosPerSecond
11550
12147
  } from "@opengeni/config";
11551
12148
  import { ChannelAPartialMutationError } from "@opengeni/runtime/sandbox";
12149
+ function preemptSandboxTurnForDeadlineRotation(input) {
12150
+ if (input.controller.signal.aborted || input.cancellationSignal?.aborted) return false;
12151
+ input.sandboxState.deadlineRotationRequested = true;
12152
+ input.controller.abort(new SandboxDeadlineRotationError(input.sandboxGroupId, input.leaseEpoch));
12153
+ return true;
12154
+ }
11552
12155
  function createSandboxTurnRuntime(deps) {
11553
12156
  const {
11554
12157
  input,
@@ -11671,7 +12274,7 @@ function createSandboxTurnRuntime(deps) {
11671
12274
  admissionOutcome = "completed";
11672
12275
  } catch (admissionError) {
11673
12276
  if (admissionError instanceof SandboxWorkspaceMutationFencedError && admissionError.code === "rotation_in_progress" && sandboxState.sandboxGroupId) {
11674
- void beginRotationCheckpoint(
12277
+ void beginRotationPreemption(
11675
12278
  sandbox,
11676
12279
  sandbox.leaseEpoch,
11677
12280
  sandboxState.sandboxGroupId
@@ -11782,51 +12385,30 @@ function createSandboxTurnRuntime(deps) {
11782
12385
  console.error("sandbox box lost event publish failed", safeErrorDiagnostic(publishError));
11783
12386
  });
11784
12387
  };
11785
- const beginRotationCheckpoint = async (sandbox, rotationEpoch, rotationGroupId) => {
11786
- if (sandboxState.rotationInFlight || sandboxRotationController.signal.aborted || cancellationSignal?.aborted) {
12388
+ const beginRotationPreemption = async (sandbox, rotationEpoch, rotationGroupId) => {
12389
+ if (sandboxState.rotationPreemptionInFlight || sandboxRotationController.signal.aborted || cancellationSignal?.aborted) {
11787
12390
  return "busy";
11788
12391
  }
11789
- const rotatingLease = await readLease2(db, input.workspaceId, rotationGroupId).catch(() => null);
11790
- if (!rotatingLease || rotatingLease.leaseEpoch !== rotationEpoch || rotatingLease.instanceId !== sandbox.established.instanceId || rotatingLease.rotationRequestedAt === null) {
11791
- return "not_rotating";
12392
+ if (sandboxState.rotationPreemptionInFlight || sandboxRotationController.signal.aborted) {
12393
+ return "busy";
11792
12394
  }
11793
- if (sandboxState.rotationInFlight || sandboxRotationController.signal.aborted) return "busy";
11794
- sandboxState.rotationInFlight = (async () => {
11795
- if (sandboxState.snapshotInFlight) {
11796
- await waitForWarmSnapshot(
11797
- sandboxState.snapshotInFlight,
11798
- settings.sandboxSnapshotTimeoutMs,
11799
- cancellationSignal
11800
- );
11801
- }
11802
- const snapshotSession = sandboxState.setupBoxSession;
11803
- const snapshotTurnId = attempt.turnId;
11804
- if (!snapshotSession || !snapshotTurnId) return;
11805
- const checkpoint = await persistSandboxDeadlineRotationCheckpoint(
11806
- { db, settings, objectStorage, observability },
11807
- {
11808
- accountId: input.accountId,
11809
- workspaceId: input.workspaceId,
11810
- sessionId: input.sessionId,
11811
- turnId: snapshotTurnId,
11812
- attemptId: input.attemptId,
11813
- sandboxGroupId: rotationGroupId
11814
- },
11815
- snapshotSession,
11816
- { leaseEpoch: rotationEpoch, instanceId: sandbox.established.instanceId },
11817
- cancellationSignal
11818
- );
11819
- if (checkpoint.checkpointed) {
11820
- sandboxRotationController.abort(
11821
- new SandboxDeadlineRotationError(rotationGroupId, rotationEpoch)
11822
- );
12395
+ let disposition = "started";
12396
+ sandboxState.rotationPreemptionInFlight = (async () => {
12397
+ const rotatingLease = await readLease2(db, input.workspaceId, rotationGroupId);
12398
+ if (rotatingLease?.leaseEpoch !== rotationEpoch || rotatingLease.instanceId !== sandbox.established.instanceId || rotatingLease.rotationRequestedAt === null) {
12399
+ disposition = "not_rotating";
11823
12400
  return;
11824
12401
  }
11825
- if (checkpoint.holder === "attempt_fenced" || checkpoint.holder === "lease_fenced") {
11826
- stopLeaseHeartbeat();
11827
- }
12402
+ stopLeaseHeartbeat();
12403
+ preemptSandboxTurnForDeadlineRotation({
12404
+ controller: sandboxRotationController,
12405
+ sandboxState,
12406
+ sandboxGroupId: rotationGroupId,
12407
+ leaseEpoch: rotationEpoch,
12408
+ ...cancellationSignal ? { cancellationSignal } : {}
12409
+ });
11828
12410
  })().catch((error) => {
11829
- observability.warn("sandbox deadline rotation checkpoint failed; retrying", {
12411
+ observability.warn("sandbox deadline rotation preemption check failed; retrying", {
11830
12412
  workspaceId: input.workspaceId,
11831
12413
  sessionId: input.sessionId,
11832
12414
  sandboxGroupId: rotationGroupId,
@@ -11834,10 +12416,10 @@ function createSandboxTurnRuntime(deps) {
11834
12416
  ...safeErrorDiagnostic(error)
11835
12417
  });
11836
12418
  }).finally(() => {
11837
- sandboxState.rotationInFlight = null;
12419
+ sandboxState.rotationPreemptionInFlight = null;
11838
12420
  });
11839
- await sandboxState.rotationInFlight;
11840
- return "started";
12421
+ await sandboxState.rotationPreemptionInFlight;
12422
+ return disposition;
11841
12423
  };
11842
12424
  const startLeaseHeartbeat = (sandbox, warmBackend) => {
11843
12425
  if (sandboxState.leaseHeartbeatTimer) return;
@@ -11862,7 +12444,7 @@ function createSandboxTurnRuntime(deps) {
11862
12444
  expectedEpoch: heartbeatEpoch
11863
12445
  }).then(async (alive) => {
11864
12446
  if (alive) return;
11865
- const rotation = await beginRotationCheckpoint(sandbox, heartbeatEpoch, heartbeatGroupId);
12447
+ const rotation = await beginRotationPreemption(sandbox, heartbeatEpoch, heartbeatGroupId);
11866
12448
  if (rotation === "not_rotating") {
11867
12449
  stopLeaseHeartbeat();
11868
12450
  }
@@ -11880,9 +12462,10 @@ function createSandboxTurnRuntime(deps) {
11880
12462
  if (snapshotSession && snapshotTurnId && shouldStartPeriodicWorkspaceSnapshot({
11881
12463
  firstProviderRequestStarted: sandboxState.firstProviderRequestStarted,
11882
12464
  snapshotInFlight: Boolean(sandboxState.snapshotInFlight),
11883
- turnEndCaptureInProgress: sandboxState.turnEndCaptureInProgress
12465
+ turnEndCaptureInProgress: sandboxState.turnEndCaptureInProgress,
12466
+ deadlineRotationRequested: sandboxState.deadlineRotationRequested
11884
12467
  })) {
11885
- sandboxState.snapshotInFlight = maybePersistWarmWorkspaceSnapshot(
12468
+ const snapshot = maybePersistWarmWorkspaceSnapshot(
11886
12469
  { db, settings, objectStorage },
11887
12470
  {
11888
12471
  accountId: input.accountId,
@@ -11895,7 +12478,9 @@ function createSandboxTurnRuntime(deps) {
11895
12478
  snapshotSession,
11896
12479
  heartbeatEpoch,
11897
12480
  activityContext?.cancellationSignal
11898
- ).then(async (persisted) => {
12481
+ );
12482
+ sandboxState.snapshotInFlight = snapshot.settled;
12483
+ void snapshot.then(async (persisted) => {
11899
12484
  if (persisted && eventing.publish) {
11900
12485
  await eventing.publish([
11901
12486
  {
@@ -11904,8 +12489,11 @@ function createSandboxTurnRuntime(deps) {
11904
12489
  }
11905
12490
  ]);
11906
12491
  }
11907
- }).catch(() => void 0).finally(() => {
11908
- sandboxState.snapshotInFlight = null;
12492
+ }).catch(() => void 0);
12493
+ void snapshot.settled.then(() => {
12494
+ if (sandboxState.snapshotInFlight === snapshot.settled) {
12495
+ sandboxState.snapshotInFlight = null;
12496
+ }
11909
12497
  });
11910
12498
  }
11911
12499
  }, 1e4);
@@ -11949,11 +12537,13 @@ import { managedSessionGroupBackend, managedSessionGroupOs } from "@opengeni/cor
11949
12537
  import { sandboxLifecycleTransitionWaitMs as sandboxLifecycleTransitionWaitMs2 } from "@opengeni/config";
11950
12538
  import {
11951
12539
  adoptConnectedMachineSessionBackgroundCommand,
12540
+ adoptManagedSessionBackgroundCommand,
11952
12541
  advanceWorkspaceGenerationForRetainedProcess,
11953
12542
  advanceWorkspaceGeneration as advanceWorkspaceGeneration2,
11954
12543
  getRetainedProcess,
11955
12544
  retainWorkspaceMutationProcess,
11956
12545
  retainedProcessSettlementIdentity,
12546
+ settleConnectedMachineSessionBackgroundCommand,
11957
12547
  settleRetainedProcess,
11958
12548
  verifyRetainedProcessMutationSettlement,
11959
12549
  verifyWorkspaceMutationSettlement as verifyWorkspaceMutationSettlement2,
@@ -11965,10 +12555,6 @@ import {
11965
12555
  resolvePersonalMachineConnectionForAttempt,
11966
12556
  SandboxRetainedProcessPromotionFencedError
11967
12557
  } from "@opengeni/db";
11968
- import {
11969
- settleConnectedMachineSessionBackgroundCommand,
11970
- settleSessionBackgroundCommandForRetainedProcess
11971
- } from "@opengeni/db/session-background-commands";
11972
12558
  import {
11973
12559
  buildSelfhostedBackendSession,
11974
12560
  ActiveBackendUnresolvableError,
@@ -12166,10 +12752,6 @@ function afterPersistableHomeMutation(services, ids, home) {
12166
12752
  admittedWorkspaceGeneration: exactAdmission.workspaceGeneration,
12167
12753
  operation: op,
12168
12754
  providerBinding: boundAdmission.providerBinding ?? null,
12169
- backgroundCommand: {
12170
- commandId: retainedProcess.id,
12171
- command: op
12172
- },
12173
12755
  owner: {
12174
12756
  kind: "turn",
12175
12757
  turnId: fence.turnId,
@@ -12276,14 +12858,33 @@ function settleRetainedProcessForTurn(services, ids) {
12276
12858
  if (settlement.process.state === "active") {
12277
12859
  throw new Error("Retained-process settlement returned an active durable process");
12278
12860
  }
12279
- await settleSessionBackgroundCommandForRetainedProcess(services.db, {
12861
+ if (settlement.backgroundCommandEvents.length > 0 && services.bus) {
12862
+ await services.bus.publish(ids.workspaceId, ids.sessionId, settlement.backgroundCommandEvents).catch(() => void 0);
12863
+ }
12864
+ };
12865
+ }
12866
+ function adoptRetainedProcessAsBackgroundCommandForTurn(services, ids) {
12867
+ const fence = ids.workspaceMutationFence;
12868
+ if (!fence) return void 0;
12869
+ return async ({ backend, process: process2 }) => {
12870
+ const durable = await getRetainedProcess(services.db, {
12871
+ workspaceId: ids.workspaceId,
12872
+ sessionId: ids.sessionId,
12873
+ processId: process2.id
12874
+ });
12875
+ if (!durable || durable.providerSessionId !== process2.providerSessionId || durable.providerBackend !== backend.kind || durable.providerInstanceId !== backend.providerInstanceId || durable.leaseEpoch !== backend.leaseEpoch || durable.routeKind !== (backend.sandboxId === null ? "home" : "active") || durable.routeTargetId !== backend.sandboxId || durable.routeEpoch !== backend.activeEpoch) {
12876
+ throw new Error("Background command adoption lost its exact retained-process identity");
12877
+ }
12878
+ await adoptManagedSessionBackgroundCommand(services.db, {
12280
12879
  accountId: fence.accountId,
12281
12880
  workspaceId: ids.workspaceId,
12282
12881
  sessionId: ids.sessionId,
12283
- retainedProcessId: process2.id,
12284
- outcome: settlement.process.state,
12285
- exitCode: settlement.process.exitCode,
12286
- reason: settlement.process.settlementReason ?? proof.reason
12882
+ turnId: fence.turnId,
12883
+ executionGeneration: fence.executionGeneration,
12884
+ attemptId: fence.attemptId,
12885
+ processId: process2.id,
12886
+ expected: retainedProcessSettlementIdentity(durable),
12887
+ command: "execCommand"
12287
12888
  });
12288
12889
  };
12289
12890
  }
@@ -12304,6 +12905,10 @@ function wrapTurnBoxWithRouting(services, ids, established) {
12304
12905
  const beforeProcessMutation = beforeRetainedProcessMutation(services, ids);
12305
12906
  const afterProcessMutation = afterRetainedProcessMutation(services, ids);
12306
12907
  const settleProcess = settleRetainedProcessForTurn(services, ids);
12908
+ const adoptProcessAsBackgroundCommand = adoptRetainedProcessAsBackgroundCommandForTurn(
12909
+ services,
12910
+ ids
12911
+ );
12307
12912
  const resolver = makeActiveBackendResolver({
12308
12913
  workspaceId: ids.workspaceId,
12309
12914
  defaultBackend: established.session,
@@ -12357,7 +12962,7 @@ function wrapTurnBoxWithRouting(services, ids, established) {
12357
12962
  return { commandId: adopted.id };
12358
12963
  },
12359
12964
  selfhostedSettleBackgroundCommand: async (command) => {
12360
- await settleConnectedMachineSessionBackgroundCommand(db, {
12965
+ const settlement = await settleConnectedMachineSessionBackgroundCommand(db, {
12361
12966
  accountId: backgroundCommandAccountId,
12362
12967
  workspaceId: ids.workspaceId,
12363
12968
  sessionId: ids.sessionId,
@@ -12370,6 +12975,9 @@ function wrapTurnBoxWithRouting(services, ids, established) {
12370
12975
  exitCode: command.exitCode,
12371
12976
  reason: command.reason
12372
12977
  });
12978
+ if (settlement && bus) {
12979
+ await bus.publish(ids.workspaceId, ids.sessionId, settlement.events).catch(() => void 0);
12980
+ }
12373
12981
  }
12374
12982
  } : {},
12375
12983
  // The turn's declared environment → a selfhosted swap target's manifest, so the
@@ -12444,6 +13052,7 @@ function wrapTurnBoxWithRouting(services, ids, established) {
12444
13052
  ...beforeProcessMutation ? { beforeProcessMutation } : {},
12445
13053
  ...afterProcessMutation ? { afterProcessMutation } : {},
12446
13054
  ...settleProcess ? { settleProcess } : {},
13055
+ ...adoptProcessAsBackgroundCommand ? { adoptProcessAsBackgroundCommand } : {},
12447
13056
  ...ids.homeLease ? {
12448
13057
  onDefaultBackendError: async ({
12449
13058
  error,
@@ -12488,6 +13097,10 @@ function wrapLazyTurnBoxWithRouting(services, ids, args) {
12488
13097
  const beforeProcessMutation = beforeRetainedProcessMutation(services, ids);
12489
13098
  const afterProcessMutation = afterRetainedProcessMutation(services, ids);
12490
13099
  const settleProcess = settleRetainedProcessForTurn(services, ids);
13100
+ const adoptProcessAsBackgroundCommand = adoptRetainedProcessAsBackgroundCommandForTurn(
13101
+ services,
13102
+ ids
13103
+ );
12491
13104
  const defaultBackend = sandboxBackendForSdkBackendId(args.backendId);
12492
13105
  const defaultSupportsPty = defaultBackend ? selectBackend(defaultBackend).capabilities.Terminal.pty : false;
12493
13106
  const syntheticSession = {
@@ -12549,7 +13162,7 @@ function wrapLazyTurnBoxWithRouting(services, ids, args) {
12549
13162
  return { commandId: adopted.id };
12550
13163
  },
12551
13164
  selfhostedSettleBackgroundCommand: async (command) => {
12552
- await settleConnectedMachineSessionBackgroundCommand(db, {
13165
+ const settlement = await settleConnectedMachineSessionBackgroundCommand(db, {
12553
13166
  accountId: backgroundCommandAccountId,
12554
13167
  workspaceId: ids.workspaceId,
12555
13168
  sessionId: ids.sessionId,
@@ -12562,6 +13175,9 @@ function wrapLazyTurnBoxWithRouting(services, ids, args) {
12562
13175
  exitCode: command.exitCode,
12563
13176
  reason: command.reason
12564
13177
  });
13178
+ if (settlement && bus) {
13179
+ await bus.publish(ids.workspaceId, ids.sessionId, settlement.events).catch(() => void 0);
13180
+ }
12565
13181
  }
12566
13182
  } : {},
12567
13183
  ...ids.environment !== void 0 ? { environment: ids.environment } : {},
@@ -12612,6 +13228,7 @@ function wrapLazyTurnBoxWithRouting(services, ids, args) {
12612
13228
  ...beforeProcessMutation ? { beforeProcessMutation } : {},
12613
13229
  ...afterProcessMutation ? { afterProcessMutation } : {},
12614
13230
  ...settleProcess ? { settleProcess } : {},
13231
+ ...adoptProcessAsBackgroundCommand ? { adoptProcessAsBackgroundCommand } : {},
12615
13232
  ...args.homeLeaseIdentity ? {
12616
13233
  onDefaultBackendError: async ({
12617
13234
  error,
@@ -12775,7 +13392,7 @@ async function establishSelfhostedTurnSession(services, args) {
12775
13392
  }
12776
13393
  } : {},
12777
13394
  settleBackgroundCommand: async (command) => {
12778
- await settleConnectedMachineSessionBackgroundCommand(db, {
13395
+ const settlement = await settleConnectedMachineSessionBackgroundCommand(db, {
12779
13396
  accountId: args.accountId,
12780
13397
  workspaceId: args.workspaceId,
12781
13398
  sessionId: args.sessionId,
@@ -12788,6 +13405,9 @@ async function establishSelfhostedTurnSession(services, args) {
12788
13405
  exitCode: command.exitCode,
12789
13406
  reason: command.reason
12790
13407
  });
13408
+ if (settlement && bus) {
13409
+ await bus.publish(args.workspaceId, args.sessionId, settlement.events).catch(() => void 0);
13410
+ }
12791
13411
  },
12792
13412
  // Meter every control op (out-of-band telemetry) — no-op when unwired.
12793
13413
  ...onOp !== void 0 ? { onOp } : {},
@@ -13860,7 +14480,7 @@ import {
13860
14480
  XAI_CREDENTIAL_LEASE_TTL_MS as XAI_CREDENTIAL_LEASE_TTL_MS2,
13861
14481
  armXaiCapacityWait as armXaiCapacityWait2
13862
14482
  } from "@opengeni/db";
13863
- import { publishDurableSessionEvents as publishDurableSessionEvents6 } from "@opengeni/events";
14483
+ import { publishDurableSessionEvents as publishDurableSessionEvents7 } from "@opengeni/events";
13864
14484
  async function selectXaiTurnCapacity(deps) {
13865
14485
  const {
13866
14486
  input,
@@ -13967,7 +14587,7 @@ async function selectXaiTurnCapacity(deps) {
13967
14587
  }
13968
14588
  });
13969
14589
  if (armed.action === "waiting") {
13970
- await publishDurableSessionEvents6(bus, input.workspaceId, input.sessionId, armed.events);
14590
+ await publishDurableSessionEvents7(bus, input.workspaceId, input.sessionId, armed.events);
13971
14591
  control.turnMetricOutcome = "recovering";
13972
14592
  control.activityStatus = "waiting_capacity";
13973
14593
  return {
@@ -14962,20 +15582,32 @@ async function prepareRunCredentials(deps) {
14962
15582
 
14963
15583
  // src/activities/agent-turn/tool-environment.ts
14964
15584
  import {
15585
+ beginConnectorActionExecution,
15586
+ completeConnectorActionExecution,
14965
15587
  getScheduledVariableSetExpectedGenerationForAttempt,
15588
+ getWorkspaceModelPolicy as getWorkspaceModelPolicy2,
15589
+ listWorkspaceGatewayCustomModels as listWorkspaceGatewayCustomModels2,
15590
+ listWorkspaceOpenRouterCustomModels as listWorkspaceOpenRouterCustomModels2,
15591
+ listOrganizationModelProviderCustomModelsForWorkspace as listOrganizationModelProviderCustomModelsForWorkspace2,
15592
+ organizationModelProviderConnectionActiveForWorkspace,
14966
15593
  persistAttemptToolCatalog,
15594
+ prepareConnectorActionApproval,
15595
+ previewConnectorActionApproval,
14967
15596
  namedSubjectHasLiveWorkspaceAuthority as namedSubjectHasLiveWorkspaceAuthority2,
14968
- updateSessionTitleWithEvent,
14969
- withCodexAppsRequestAuthorization
15597
+ updateSessionTitleWithEvent as updateSessionTitleWithEvent2,
15598
+ withCodexAppsRequestAuthorization,
15599
+ workspaceCodexSubscriptionActive as workspaceCodexSubscriptionActive3,
15600
+ workspaceVercelAiGatewayConnectionActive,
15601
+ workspaceOpenRouterConnectionActive,
15602
+ workspaceXaiSubscriptionActiveForAuthority
14970
15603
  } from "@opengeni/db";
14971
- import { publishDurableSessionEvents as publishDurableSessionEvents8 } from "@opengeni/events";
15604
+ import { publishDurableSessionEvents as publishDurableSessionEvents9 } from "@opengeni/events";
14972
15605
  import {
14973
15606
  createFirstPartyInteractionAttemptToolDefinitions
14974
15607
  } from "@opengeni/runtime";
14975
15608
 
14976
15609
  // src/activities/google-drive-publication.ts
14977
15610
  import { createHash as createHash9 } from "crypto";
14978
- import { AttemptToolApprovalRequiredError } from "@opengeni/codemode";
14979
15611
  import {
14980
15612
  GOOGLE_DRIVE_FILE_SCOPE,
14981
15613
  GOOGLE_DRIVE_FULL_SCOPE,
@@ -14989,10 +15621,7 @@ import {
14989
15621
  googleDriveScopesAllowCapability
14990
15622
  } from "@opengeni/contracts/google-drive";
14991
15623
  import {
14992
- beginConnectorActionExecution,
14993
- completeConnectorActionExecution,
14994
15624
  getConnectionMetadata,
14995
- prepareConnectorActionApproval,
14996
15625
  requireFile as requireFile2,
14997
15626
  namedSubjectHasLiveWorkspaceAuthority
14998
15627
  } from "@opengeni/db";
@@ -15000,6 +15629,7 @@ import {
15000
15629
  readAuthorizedEditableArtifactMaterialization
15001
15630
  } from "@opengeni/db/editable-artifact-durable-export";
15002
15631
  import { readResponseJsonBounded, undiciFetch } from "@opengeni/network";
15632
+ import { ConnectorActionExecutionError } from "@opengeni/runtime";
15003
15633
  import { retryWhileMissing as retryWhileMissing4 } from "@opengeni/storage";
15004
15634
  var DRIVE_API = "https://www.googleapis.com/drive/v3";
15005
15635
  var DRIVE_UPLOAD_API = "https://www.googleapis.com/upload/drive/v3";
@@ -15015,9 +15645,6 @@ var defaultPorts = {
15015
15645
  getMembership: namedSubjectHasLiveWorkspaceAuthority,
15016
15646
  readMaterialization: readAuthorizedEditableArtifactMaterialization,
15017
15647
  requireFile: requireFile2,
15018
- prepare: prepareConnectorActionApproval,
15019
- begin: beginConnectorActionExecution,
15020
- complete: completeConnectorActionExecution,
15021
15648
  fetch: undiciFetch
15022
15649
  };
15023
15650
  async function resolveGoogleDrivePublicationTarget(db, workspace, delegations, ports = defaultPorts) {
@@ -15188,24 +15815,6 @@ function createGoogleDrivePublicationAttemptTool(input) {
15188
15815
  approval: "policy",
15189
15816
  execute: async (raw, context) => {
15190
15817
  const request = GoogleDrivePublicationToolInput.parse(raw);
15191
- const connectorCall = googleDrivePublicationConnectorCall(
15192
- input.target,
15193
- request,
15194
- context.operationId
15195
- );
15196
- let requestId = null;
15197
- if (context.caller.kind === "codemode") {
15198
- const admission = await ports.begin(input.db, input.identity, connectorCall);
15199
- if (!admission.allowed) {
15200
- throw new Error(
15201
- admission.reason === "uncertain_retry" || admission.reason === "already_executed" ? "Google Drive publication outcome is unknown: a previous attempt may have already sent this publication to Google Drive. Verify in Drive before retrying with a new idempotency key." : admission.reason === "not_executed" ? "Google Drive publication was not executed: the previous attempt failed before any request reached Google Drive. Retrying with a new call is safe." : `Google Drive publication was not executed: ${admission.reason}. No request reached Google Drive.`
15202
- );
15203
- }
15204
- if (!admission.managed) {
15205
- throw new Error("Google Drive publication has no explicit connector action policy");
15206
- }
15207
- requestId = admission.requestId;
15208
- }
15209
15818
  let providerRequestStarted = false;
15210
15819
  try {
15211
15820
  const receipt = await executeGoogleDrivePublication(
@@ -15224,53 +15833,21 @@ function createGoogleDrivePublicationAttemptTool(input) {
15224
15833
  },
15225
15834
  ports
15226
15835
  );
15227
- if (requestId) {
15228
- await ports.complete(input.db, {
15229
- accountId: input.identity.accountId,
15230
- workspaceId: input.identity.workspaceId,
15231
- requestId,
15232
- attemptId: input.identity.attemptId,
15233
- outcome: "completed"
15234
- });
15235
- }
15236
15836
  return {
15237
15837
  content: [{ type: "text", text: JSON.stringify(receipt) }],
15238
15838
  structuredContent: receipt
15239
15839
  };
15240
15840
  } catch (error) {
15241
- if (requestId) {
15242
- await ports.complete(input.db, {
15243
- accountId: input.identity.accountId,
15244
- workspaceId: input.identity.workspaceId,
15245
- requestId,
15246
- attemptId: input.identity.attemptId,
15247
- outcome: providerRequestStarted ? "uncertain" : "not_executed"
15248
- });
15249
- }
15250
15841
  const detail = error instanceof Error ? error.message : String(error);
15251
- throw new Error(
15842
+ throw new ConnectorActionExecutionError(
15252
15843
  providerRequestStarted ? `Google Drive publication outcome is unknown: a provider request may have been sent before the failure. Verify in Drive before retrying. Cause: ${detail}` : `Google Drive publication was not executed: no request reached Google Drive. Cause: ${detail}`,
15844
+ providerRequestStarted ? "uncertain" : "not_executed",
15253
15845
  { cause: error }
15254
15846
  );
15255
15847
  }
15256
15848
  }
15257
15849
  };
15258
15850
  }
15259
- async function authorizeGoogleDrivePublicationAttempt(input) {
15260
- const request = GoogleDrivePublicationToolInput.parse(input.arguments);
15261
- const preparation = await (input.ports ?? defaultPorts).prepare(
15262
- input.db,
15263
- input.identity,
15264
- googleDrivePublicationConnectorCall(input.target, request, input.approvalId)
15265
- );
15266
- if (!preparation.managed) {
15267
- throw new Error("Google Drive publication has no explicit connector action policy");
15268
- }
15269
- if (preparation.decision === "ask") throw new AttemptToolApprovalRequiredError();
15270
- if (preparation.decision === "block") {
15271
- throw new Error("Google Drive publication is blocked by connector action policy");
15272
- }
15273
- }
15274
15851
  async function executeGoogleDrivePublication(input, ports = defaultPorts) {
15275
15852
  const request = GoogleDrivePublicationToolInput.parse(input.request);
15276
15853
  const connection = await ports.getConnection(
@@ -15643,6 +16220,7 @@ import {
15643
16220
  sessionTenancyProductActivated
15644
16221
  } from "@opengeni/db";
15645
16222
  import { recordTenancyCompatibilityLaneUse } from "@opengeni/observability";
16223
+ var OPENGENI_CONNECTION_ID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
15646
16224
  function connectionTokenResolverForTurn(input) {
15647
16225
  const hostResolver = input.connectionCredentials?.mcpCredentials;
15648
16226
  const baseResolver = hostResolver ? buildHostConnectionTokenResolver(hostResolver, {
@@ -15658,6 +16236,37 @@ function connectionTokenResolverForTurn(input) {
15658
16236
  surface: "model"
15659
16237
  }) : buildConnectionTokenResolver(input.db, input.settings);
15660
16238
  return async (request) => {
16239
+ if (request.connectionRef.authoritySource === "host") {
16240
+ if (hostResolver) return await baseResolver(request);
16241
+ return {
16242
+ status: "auth_needed",
16243
+ reason: "unsupported_auth",
16244
+ providerDomain: request.connectionRef.providerDomain,
16245
+ authoritySource: "host",
16246
+ ...request.connectionRef.provider ? { provider: request.connectionRef.provider } : {},
16247
+ ...request.connectionRef.connectionId ? { connectionId: request.connectionRef.connectionId } : {},
16248
+ ...request.connectionRef.scopes ? { scopes: request.connectionRef.scopes } : {},
16249
+ ...request.connectionRef.resource ? { resource: request.connectionRef.resource } : {},
16250
+ ...request.connectionRef.selectedResources ? { selectedResources: request.connectionRef.selectedResources } : {}
16251
+ };
16252
+ }
16253
+ if (hostResolver && request.connectionRef.connectionId && !OPENGENI_CONNECTION_ID_PATTERN.test(request.connectionRef.connectionId)) {
16254
+ try {
16255
+ return await baseResolver(request);
16256
+ } catch {
16257
+ return {
16258
+ status: "auth_needed",
16259
+ reason: "refresh_failed",
16260
+ providerDomain: request.connectionRef.providerDomain,
16261
+ authoritySource: "host",
16262
+ ...request.connectionRef.provider ? { provider: request.connectionRef.provider } : {},
16263
+ connectionId: request.connectionRef.connectionId,
16264
+ ...request.connectionRef.scopes ? { scopes: request.connectionRef.scopes } : {},
16265
+ ...request.connectionRef.resource ? { resource: request.connectionRef.resource } : {},
16266
+ ...request.connectionRef.selectedResources ? { selectedResources: request.connectionRef.selectedResources } : {}
16267
+ };
16268
+ }
16269
+ }
15661
16270
  const acceptedDelegation = input.turn.personalConnectionDelegations.find(
15662
16271
  (delegation) => (delegation.connectionType === void 0 || delegation.connectionType === "mcp" || delegation.connectionType === "github_personal" && delegation.serverId === "github:personal" && request.serverId === "github:personal" && request.connectionRef.provider === "github" && request.connectionRef.kind === "oauth2" && request.connectionRef.providerDomain === "github.com" && request.credentialTarget === "http_api" && isGitHubApiDestination(request.destinationUrl)) && delegation.serverId === request.serverId && delegation.connectionId === request.connectionRef.connectionId
15663
16272
  );
@@ -15769,93 +16378,6 @@ function isGitHubApiDestination(value) {
15769
16378
  }
15770
16379
  }
15771
16380
 
15772
- // src/activities/api-integrations.ts
15773
- import {
15774
- createGraphqlMcpServer,
15775
- createOpenApiMcpServer,
15776
- createPinnedIntegrationTransport
15777
- } from "@opengeni/capabilities";
15778
- function buildApiIntegrationServersForTurn(input) {
15779
- const transport = createPinnedIntegrationTransport({
15780
- network: input.settings,
15781
- ...input.fetchImpl ? { fetchImpl: input.fetchImpl } : {}
15782
- });
15783
- return input.integrations.map((integration) => {
15784
- const authority = {
15785
- ...input.authority,
15786
- ...integration.connectionRef?.connectionId ? { connectionRef: integration.connectionRef.connectionId } : {}
15787
- };
15788
- const credentialResolver = integration.connectionRef ? integrationCredentialResolver(input, integration) : void 0;
15789
- const server = integration.revision.protocol === "openapi" ? createOpenApiMcpServer({
15790
- revision: integration.revision,
15791
- transport,
15792
- authority,
15793
- ...credentialResolver ? { credentialResolver } : {}
15794
- }) : createGraphqlMcpServer({
15795
- revision: integration.revision,
15796
- endpoint: integration.baseUrl,
15797
- transport,
15798
- authority,
15799
- ...credentialResolver ? { credentialResolver } : {}
15800
- });
15801
- return {
15802
- id: integration.serverId,
15803
- server,
15804
- ...integration.connectionRef?.connectionId ? { resolvedConnectionId: integration.connectionRef.connectionId } : {}
15805
- };
15806
- });
15807
- }
15808
- function integrationCredentialResolver(input, integration) {
15809
- const connectionRef = integration.connectionRef;
15810
- if (!connectionRef) throw new Error("Integration credential resolver requires a connection");
15811
- return {
15812
- resolve: async (request) => {
15813
- const result = await input.resolveCredential({
15814
- workspaceId: input.authority.workspaceId,
15815
- serverId: integration.serverId,
15816
- toolName: request.operationKey,
15817
- connectionRef,
15818
- destinationUrl: request.destinationUrl,
15819
- credentialTarget: "http_api",
15820
- forceRefresh: request.forceRefresh === true
15821
- });
15822
- if (result.status === "auth_needed") {
15823
- await publishAuthNeeded(input, integration.serverId, request.operationKey, result);
15824
- return null;
15825
- }
15826
- const destination = new URL(request.destinationUrl);
15827
- return {
15828
- audience: { origin: destination.origin, pathPrefix: "/" },
15829
- placements: result.placements ?? Object.entries(result.headers).map(([name, value]) => ({
15830
- carrier: "header",
15831
- name,
15832
- value
15833
- })),
15834
- ...result.authorizeProviderRequest ? { authorizeProviderRequest: result.authorizeProviderRequest } : {},
15835
- ...result.expiresAt ? { expiresAt: result.expiresAt.toISOString() } : {},
15836
- ...connectionRef.scopes ? { scope: [...connectionRef.scopes] } : {}
15837
- };
15838
- }
15839
- };
15840
- }
15841
- async function publishAuthNeeded(input, serverId, toolName, result) {
15842
- try {
15843
- await input.onAuthNeeded?.({
15844
- serverId,
15845
- toolName,
15846
- providerDomain: result.providerDomain,
15847
- ...result.provider ? { provider: result.provider } : {},
15848
- reason: result.reason,
15849
- ...result.connectionId ? { connectionId: result.connectionId } : {},
15850
- ...result.scopes ? { scopes: result.scopes } : {},
15851
- ...result.resource ? { resource: result.resource } : {},
15852
- ...result.selectedResources ? { selectedResources: result.selectedResources } : {},
15853
- ...result.authorizationUrl ? { authorizationUrl: result.authorizationUrl } : {}
15854
- });
15855
- } catch {
15856
- }
15857
- }
15858
-
15859
16381
  // src/github-rest-mcp.ts
15860
16382
  import {
15861
16383
  mergeToolRefs
@@ -15866,20 +16388,15 @@ import {
15866
16388
  } from "@opengeni/contracts/personal-github";
15867
16389
  import { personalGitHubRepositoryResources as personalGitHubRepositoryResources2 } from "@opengeni/core";
15868
16390
  import {
15869
- beginConnectorActionExecution as beginConnectorActionExecution2,
15870
- completeConnectorActionExecution as completeConnectorActionExecution2,
15871
16391
  getConnectionMetadata as getConnectionMetadata2,
15872
16392
  getPersonalGitHubRepositorySelectionState,
15873
16393
  hasAuditableGitHubInstallationAuthority,
15874
16394
  listGitHubInstallationAccessForWorkspace,
15875
- prepareConnectorActionApproval as prepareConnectorActionApproval2,
15876
16395
  resolveAcceptedConnectionUse as resolveAcceptedConnectionUse2
15877
16396
  } from "@opengeni/db";
15878
- import {
15879
- AttemptToolApprovalRequiredError as AttemptToolApprovalRequiredError2
15880
- } from "@opengeni/codemode";
15881
16397
  import { createGitHubAppInstallationTokenWithExpiry as createGitHubAppInstallationTokenWithExpiry2, githubAppBotIdentity as githubAppBotIdentity2 } from "@opengeni/github";
15882
16398
  import {
16399
+ ConnectorActionBindingRejectedError,
15883
16400
  prefixedMcpToolName
15884
16401
  } from "@opengeni/runtime";
15885
16402
  import {
@@ -15890,8 +16407,7 @@ import {
15890
16407
  GITHUB_REST_TOOL_NAMES,
15891
16408
  GitHubRestAuthorityError,
15892
16409
  githubRestConnectorActionOutcome,
15893
- GitHubRestMcpServer,
15894
- GitHubRestMutationNotExecutedError
16410
+ GitHubRestMcpServer
15895
16411
  } from "@opengeni/runtime/github-rest-mcp";
15896
16412
  var PERSONAL_GITHUB_DELEGATION_SERVER_ID = "github:personal";
15897
16413
  var INTERNAL_GITHUB_MCP_TIMEOUT_MS = 2e4;
@@ -15901,8 +16417,7 @@ async function buildGitHubRestMcpForTurn(input) {
15901
16417
  settings: input.settings,
15902
16418
  tools: input.tools,
15903
16419
  localMcpServers: [],
15904
- connectorBindings: [],
15905
- authorizeCodemodeCall: null
16420
+ connectorBindings: []
15906
16421
  };
15907
16422
  }
15908
16423
  const appRepositories = appRepositoryAuthority(input.resources);
@@ -15915,8 +16430,7 @@ async function buildGitHubRestMcpForTurn(input) {
15915
16430
  settings: input.settings,
15916
16431
  tools: input.tools,
15917
16432
  localMcpServers: [],
15918
- connectorBindings: [],
15919
- authorizeCodemodeCall: null
16433
+ connectorBindings: []
15920
16434
  };
15921
16435
  }
15922
16436
  const configuredIds = new Set(input.settings.mcpServers.map((server) => server.id));
@@ -15931,7 +16445,6 @@ async function buildGitHubRestMcpForTurn(input) {
15931
16445
  const localMcpServers = [];
15932
16446
  const toolRefs = [];
15933
16447
  const connectorBindings = [];
15934
- const codemodeActions = createCodemodeActionLifecycle(input, connectorBindings);
15935
16448
  if (appRepositories.length > 0) {
15936
16449
  const appResolver = workspaceAppAuthorityResolver(input, appRepositories);
15937
16450
  localMcpServers.push({
@@ -15940,8 +16453,7 @@ async function buildGitHubRestMcpForTurn(input) {
15940
16453
  serverId: GITHUB_REST_MCP_APP_SERVER_ID,
15941
16454
  authorityKind: "workspace_app",
15942
16455
  repositories: appRepositories,
15943
- resolveAuthority: appResolver,
15944
- completeCodemodeAction: codemodeActions.complete
16456
+ resolveAuthority: appResolver
15945
16457
  })
15946
16458
  });
15947
16459
  configs.push(internalMcpConfig(GITHUB_REST_MCP_APP_SERVER_ID, "GitHub \u2014 OpenGeni bot"));
@@ -15957,8 +16469,7 @@ async function buildGitHubRestMcpForTurn(input) {
15957
16469
  serverId: GITHUB_REST_MCP_PERSONAL_SERVER_ID,
15958
16470
  authorityKind: "personal_oauth",
15959
16471
  repositories: personalRepositories,
15960
- resolveAuthority: personalAuthorityResolver(input),
15961
- completeCodemodeAction: codemodeActions.complete
16472
+ resolveAuthority: personalAuthorityResolver(input)
15962
16473
  })
15963
16474
  });
15964
16475
  configs.push(internalMcpConfig(GITHUB_REST_MCP_PERSONAL_SERVER_ID, "GitHub \u2014 My account"));
@@ -15971,62 +16482,10 @@ async function buildGitHubRestMcpForTurn(input) {
15971
16482
  settings: {
15972
16483
  ...input.settings,
15973
16484
  mcpServers: [...input.settings.mcpServers, ...configs]
15974
- },
15975
- tools: mergeToolRefs(input.tools, toolRefs),
15976
- localMcpServers,
15977
- connectorBindings,
15978
- authorizeCodemodeCall: codemodeActions.authorize
15979
- };
15980
- }
15981
- function createCodemodeActionLifecycle(input, bindings) {
15982
- const identity = {
15983
- accountId: input.accountId,
15984
- workspaceId: input.workspaceId,
15985
- sessionId: input.sessionId,
15986
- turnId: input.turn.id,
15987
- attemptId: input.attemptId,
15988
- executionGeneration: input.turn.executionGeneration,
15989
- initiator: input.turn.initiator
15990
- };
15991
- const executing = /* @__PURE__ */ new Map();
15992
- return {
15993
- authorize: async ({ call, entry }) => {
15994
- if (call.caller.kind !== "codemode") return;
15995
- const binding = bindings.find((candidate) => candidate.modelName === entry.modelName);
15996
- if (!binding) return;
15997
- const invocation = binding.call(call.operationId, call.arguments);
15998
- const policy = await prepareConnectorActionApproval2(input.db, identity, invocation);
15999
- if (policy.managed && policy.decision === "ask") {
16000
- throw new AttemptToolApprovalRequiredError2();
16001
- }
16002
- if (policy.managed && policy.decision === "block") {
16003
- throw new GitHubRestMutationNotExecutedError(
16004
- "GitHub connector action is blocked by policy"
16005
- );
16006
- }
16007
- const admission = await beginConnectorActionExecution2(input.db, identity, invocation);
16008
- if (!admission.allowed) {
16009
- if (admission.reason === "approval_required") {
16010
- throw new AttemptToolApprovalRequiredError2();
16011
- }
16012
- throw new GitHubRestMutationNotExecutedError(
16013
- `GitHub connector action was not executed: ${admission.reason}`
16014
- );
16015
- }
16016
- if (admission.managed) executing.set(call.operationId, admission.requestId);
16017
- },
16018
- complete: async ({ operationId, outcome }) => {
16019
- const requestId = executing.get(operationId);
16020
- if (!requestId) return;
16021
- executing.delete(operationId);
16022
- await completeConnectorActionExecution2(input.db, {
16023
- accountId: input.accountId,
16024
- workspaceId: input.workspaceId,
16025
- requestId,
16026
- attemptId: input.attemptId,
16027
- outcome
16028
- });
16029
- }
16485
+ },
16486
+ tools: mergeToolRefs(input.tools, toolRefs),
16487
+ localMcpServers,
16488
+ connectorBindings
16030
16489
  };
16031
16490
  }
16032
16491
  function internalMcpConfig(id, name) {
@@ -16314,7 +16773,7 @@ function githubConnectorBindings(serverId, repositories) {
16314
16773
  const args = objectRecord(arguments_);
16315
16774
  const repository = typeof args.repository === "string" ? byName.get(args.repository.toLowerCase()) : null;
16316
16775
  if (!repository) {
16317
- throw new GitHubRestAuthorityError(
16776
+ throw new ConnectorActionBindingRejectedError(
16318
16777
  "GitHub action repository does not match an accepted resource"
16319
16778
  );
16320
16779
  }
@@ -16502,14 +16961,16 @@ import { allowedFirstPartyMcpToolsForSession } from "@opengeni/config";
16502
16961
  // src/activities/codemode-dispatcher.ts
16503
16962
  import { randomUUID as randomUUID3 } from "crypto";
16504
16963
  import {
16505
- AttemptToolApprovalRequiredError as AttemptToolApprovalRequiredError3,
16964
+ AttemptToolApprovalRequiredError,
16506
16965
  AttemptToolCatalogStaleError,
16966
+ AttemptToolInputValidationError,
16507
16967
  AttemptToolNotFoundError,
16508
16968
  codemodeDispatchSubject,
16509
16969
  decodeCodemodeDispatchRequest,
16510
16970
  encodeCodemodeDispatchAck
16511
16971
  } from "@opengeni/codemode";
16512
16972
  import {
16973
+ CODEMODE_CLAIM_LEASE_MS,
16513
16974
  CODEMODE_CLAIM_HEARTBEAT_MS,
16514
16975
  CODEMODE_MAX_CONCURRENT_CALLS_PER_ATTEMPT
16515
16976
  } from "@opengeni/contracts";
@@ -16517,16 +16978,18 @@ import {
16517
16978
  cancelQueuedCodemodeOperationsForAttempt,
16518
16979
  claimCodemodeOperation,
16519
16980
  failCodemodeOperation,
16981
+ getSessionEventByClientEventId,
16982
+ getSessionToolCallCreatedEventByOperationId,
16520
16983
  markCodemodeOperationExecutionStarted,
16521
16984
  renewCodemodeOperationClaim,
16522
16985
  settleCodemodeOperationWithOutput
16523
16986
  } from "@opengeni/db";
16524
16987
  import {
16525
16988
  appendAndPublishTurnEventsFenced as appendAndPublishTurnEventsFenced3,
16526
- publishDurableSessionEvents as publishDurableSessionEvents7
16989
+ publishDurableSessionEvents as publishDurableSessionEvents8
16527
16990
  } from "@opengeni/events";
16528
16991
  var CodemodeAttemptDispatcher = class {
16529
- constructor(db, bus, environment, scope, turnSignal, maxConcurrentCalls = CODEMODE_MAX_CONCURRENT_CALLS_PER_ATTEMPT) {
16992
+ constructor(db, bus, environment, scope, turnSignal, maxConcurrentCalls = CODEMODE_MAX_CONCURRENT_CALLS_PER_ATTEMPT, timings = {}) {
16530
16993
  this.db = db;
16531
16994
  this.bus = bus;
16532
16995
  this.environment = environment;
@@ -16539,12 +17002,22 @@ var CodemodeAttemptDispatcher = class {
16539
17002
  if (!Number.isSafeInteger(maxConcurrentCalls) || maxConcurrentCalls < 1) {
16540
17003
  throw new Error("Codemode dispatcher concurrency must be a positive safe integer");
16541
17004
  }
17005
+ this.claimLeaseMs = timings.claimLeaseMs ?? CODEMODE_CLAIM_LEASE_MS;
17006
+ this.claimHeartbeatMs = timings.claimHeartbeatMs ?? CODEMODE_CLAIM_HEARTBEAT_MS;
17007
+ if (!Number.isSafeInteger(this.claimLeaseMs) || this.claimLeaseMs < 1e3 || this.claimLeaseMs > 10 * 6e4) {
17008
+ throw new Error("Codemode dispatcher claim lease must be between 1 second and 10 minutes");
17009
+ }
17010
+ if (!Number.isSafeInteger(this.claimHeartbeatMs) || this.claimHeartbeatMs < 1 || this.claimHeartbeatMs >= this.claimLeaseMs) {
17011
+ throw new Error("Codemode dispatcher heartbeat must be positive and shorter than its lease");
17012
+ }
16542
17013
  }
16543
17014
  stopController = new AbortController();
16544
17015
  inFlight = /* @__PURE__ */ new Map();
16545
17016
  pendingClaims = 0;
16546
17017
  unsubscribe = null;
16547
17018
  closing = false;
17019
+ claimLeaseMs;
17020
+ claimHeartbeatMs;
16548
17021
  start() {
16549
17022
  if (this.unsubscribe || this.closing)
16550
17023
  throw new Error("Codemode dispatcher cannot be restarted");
@@ -16572,12 +17045,15 @@ var CodemodeAttemptDispatcher = class {
16572
17045
  ...this.scope,
16573
17046
  catalogDigest: request.catalogDigest,
16574
17047
  operationId: request.operationId,
16575
- claimId: randomUUID3()
17048
+ claimId: randomUUID3(),
17049
+ claimLeaseMs: this.claimLeaseMs
16576
17050
  });
16577
17051
  if (claim.status === "claimed") {
16578
- const execution = this.execute(claim.operation, claim.claimId).finally(() => {
16579
- this.inFlight.delete(request.operationId);
16580
- });
17052
+ const execution = this.execute(claim.operation, claim.claimId, claim.reclaimed).finally(
17053
+ () => {
17054
+ this.inFlight.delete(request.operationId);
17055
+ }
17056
+ );
16581
17057
  this.inFlight.set(request.operationId, execution);
16582
17058
  return encodeCodemodeDispatchAck({
16583
17059
  version: 1,
@@ -16585,6 +17061,18 @@ var CodemodeAttemptDispatcher = class {
16585
17061
  status: "accepted"
16586
17062
  });
16587
17063
  }
17064
+ if (claim.status === "execution_owner_lost") {
17065
+ await this.settleWithOutput(claim.operation, claim.claimId, {
17066
+ state: "outcome_unknown",
17067
+ errorCode: "worker_lost_during_execution",
17068
+ errorMessage: "The execution owner disappeared after the tool call began. Inspect actual state before retrying."
17069
+ });
17070
+ return encodeCodemodeDispatchAck({
17071
+ version: 1,
17072
+ operationId: request.operationId,
17073
+ status: "terminal"
17074
+ });
17075
+ }
16588
17076
  return encodeCodemodeDispatchAck({
16589
17077
  version: 1,
16590
17078
  operationId: request.operationId,
@@ -16613,7 +17101,26 @@ var CodemodeAttemptDispatcher = class {
16613
17101
  });
16614
17102
  await Promise.allSettled([...this.inFlight.values()]);
16615
17103
  }
16616
- async execute(operation, claimId) {
17104
+ async execute(operation, claimId, reclaimed) {
17105
+ const claimAuthority = {
17106
+ accountId: this.scope.accountId,
17107
+ workspaceId: this.scope.workspaceId,
17108
+ attemptId: this.scope.attemptId,
17109
+ operationId: operation.operationId,
17110
+ claimId,
17111
+ claimLeaseMs: this.claimLeaseMs
17112
+ };
17113
+ const heartbeat = setInterval(() => {
17114
+ void renewCodemodeOperationClaim(this.db, claimAuthority).catch(() => void 0);
17115
+ }, this.claimHeartbeatMs);
17116
+ heartbeat.unref?.();
17117
+ try {
17118
+ await this.executeClaimed(operation, claimId, reclaimed, claimAuthority);
17119
+ } finally {
17120
+ clearInterval(heartbeat);
17121
+ }
17122
+ }
17123
+ async executeClaimed(operation, claimId, reclaimed, claimAuthority) {
16617
17124
  const entry = this.environment.catalog.entries.find(
16618
17125
  (candidate) => candidate.identity.serverId === operation.identity.serverId && candidate.identity.toolName === operation.identity.toolName
16619
17126
  );
@@ -16636,38 +17143,7 @@ var CodemodeAttemptDispatcher = class {
16636
17143
  );
16637
17144
  return;
16638
17145
  }
16639
- const created = await appendAndPublishTurnEventsFenced3(
16640
- this.db,
16641
- this.bus,
16642
- this.scope.workspaceId,
16643
- this.scope.sessionId,
16644
- this.scope.turnId,
16645
- this.scope.executionGeneration,
16646
- this.scope.attemptId,
16647
- [
16648
- {
16649
- type: "agent.toolCall.created",
16650
- turnId: this.scope.turnId,
16651
- turnGeneration: this.scope.executionGeneration,
16652
- turnAttemptId: this.scope.attemptId,
16653
- producerId: operation.caller.subjectId,
16654
- payload: {
16655
- id: operation.operationId,
16656
- name: entry.modelName,
16657
- arguments: operation.arguments,
16658
- origin: "codemode",
16659
- subjectId: operation.caller.subjectId,
16660
- raw: {
16661
- type: "codemode_call",
16662
- serverId: operation.identity.serverId,
16663
- toolName: operation.identity.toolName,
16664
- catalogDigest: operation.catalogDigest
16665
- }
16666
- }
16667
- }
16668
- ]
16669
- ).catch(() => ({ accepted: false }));
16670
- if (!created.accepted) {
17146
+ if (!await this.ensureToolCallCreated(operation, entry, reclaimed)) {
16671
17147
  await this.failBeforeExecution(
16672
17148
  operation.operationId,
16673
17149
  claimId,
@@ -16684,24 +17160,9 @@ var CodemodeAttemptDispatcher = class {
16684
17160
  });
16685
17161
  return;
16686
17162
  }
16687
- const claimAuthority = {
16688
- accountId: this.scope.accountId,
16689
- workspaceId: this.scope.workspaceId,
16690
- attemptId: this.scope.attemptId,
16691
- operationId: operation.operationId,
16692
- claimId
16693
- };
16694
- const crossedExecutionBoundary = await markCodemodeOperationExecutionStarted(
16695
- this.db,
16696
- claimAuthority
16697
- );
16698
- if (!crossedExecutionBoundary) return;
16699
- const heartbeat = setInterval(() => {
16700
- void renewCodemodeOperationClaim(this.db, claimAuthority).catch(() => void 0);
16701
- }, CODEMODE_CLAIM_HEARTBEAT_MS);
16702
- heartbeat.unref?.();
17163
+ let preparedCall;
16703
17164
  try {
16704
- const result = await this.environment.call(
17165
+ preparedCall = await this.environment.prepareCall(
16705
17166
  {
16706
17167
  operationId: operation.operationId,
16707
17168
  catalogDigest: operation.catalogDigest,
@@ -16711,16 +17172,111 @@ var CodemodeAttemptDispatcher = class {
16711
17172
  },
16712
17173
  { signal }
16713
17174
  );
16714
- await this.settleWithOutput(operation, claimId, { state: "completed", result });
16715
17175
  } catch (error) {
16716
- const knownPreExecution = error instanceof AttemptToolApprovalRequiredError3 || error instanceof AttemptToolCatalogStaleError || error instanceof AttemptToolNotFoundError || connectorActionWasNotExecuted(error);
17176
+ await this.settleWithOutput(operation, claimId, {
17177
+ state: "failed",
17178
+ errorCode: errorCode(error, signal, false),
17179
+ errorMessage: safeErrorMessage(error)
17180
+ });
17181
+ return;
17182
+ }
17183
+ if (signal.aborted) {
17184
+ await this.settleWithOutput(operation, claimId, {
17185
+ state: "failed",
17186
+ errorCode: "attempt_cancelled",
17187
+ errorMessage: "Execution attempt ended before the Codemode call started"
17188
+ });
17189
+ return;
17190
+ }
17191
+ const crossedExecutionBoundary = await markCodemodeOperationExecutionStarted(
17192
+ this.db,
17193
+ claimAuthority
17194
+ );
17195
+ if (!crossedExecutionBoundary) return;
17196
+ try {
17197
+ const result = await preparedCall.execute();
17198
+ await this.settleWithOutput(operation, claimId, {
17199
+ state: "completed",
17200
+ result
17201
+ });
17202
+ } catch (error) {
17203
+ const knownPreExecution = error instanceof AttemptToolApprovalRequiredError || error instanceof AttemptToolCatalogStaleError || error instanceof AttemptToolNotFoundError || connectorActionWasNotExecuted(error);
16717
17204
  await this.settleWithOutput(operation, claimId, {
16718
17205
  state: knownPreExecution ? "failed" : "outcome_unknown",
16719
- errorCode: errorCode(error, signal),
17206
+ errorCode: errorCode(error, signal, true),
16720
17207
  errorMessage: !knownPreExecution ? "Tool execution ended without a durable result. Its side-effect outcome is unknown; inspect actual state before retrying." : safeErrorMessage(error)
16721
17208
  });
16722
- } finally {
16723
- clearInterval(heartbeat);
17209
+ }
17210
+ }
17211
+ async ensureToolCallCreated(operation, entry, reclaimed) {
17212
+ const clientEventId = codemodeToolCallCreatedClientEventId(operation.operationId);
17213
+ if (reclaimed) {
17214
+ const existing = await getSessionEventByClientEventId(
17215
+ this.db,
17216
+ this.scope.workspaceId,
17217
+ this.scope.sessionId,
17218
+ clientEventId
17219
+ ).catch(() => null);
17220
+ if (existing) {
17221
+ assertMatchingCodemodeToolCallCreated(existing, operation, entry, this.scope);
17222
+ return true;
17223
+ }
17224
+ const legacyCreated = await getSessionToolCallCreatedEventByOperationId(this.db, {
17225
+ workspaceId: this.scope.workspaceId,
17226
+ sessionId: this.scope.sessionId,
17227
+ turnId: this.scope.turnId,
17228
+ attemptId: this.scope.attemptId,
17229
+ operationId: operation.operationId
17230
+ }).catch(() => null);
17231
+ if (legacyCreated) {
17232
+ assertMatchingCodemodeToolCallCreated(legacyCreated, operation, entry, this.scope);
17233
+ return true;
17234
+ }
17235
+ }
17236
+ try {
17237
+ const created = await appendAndPublishTurnEventsFenced3(
17238
+ this.db,
17239
+ this.bus,
17240
+ this.scope.workspaceId,
17241
+ this.scope.sessionId,
17242
+ this.scope.turnId,
17243
+ this.scope.executionGeneration,
17244
+ this.scope.attemptId,
17245
+ [
17246
+ {
17247
+ type: "agent.toolCall.created",
17248
+ clientEventId,
17249
+ turnId: this.scope.turnId,
17250
+ turnGeneration: this.scope.executionGeneration,
17251
+ turnAttemptId: this.scope.attemptId,
17252
+ producerId: operation.caller.subjectId,
17253
+ payload: {
17254
+ id: operation.operationId,
17255
+ name: entry.modelName,
17256
+ arguments: operation.arguments,
17257
+ origin: "codemode",
17258
+ subjectId: operation.caller.subjectId,
17259
+ raw: {
17260
+ type: "codemode_call",
17261
+ serverId: operation.identity.serverId,
17262
+ toolName: operation.identity.toolName,
17263
+ catalogDigest: operation.catalogDigest
17264
+ }
17265
+ }
17266
+ }
17267
+ ]
17268
+ );
17269
+ return created.accepted;
17270
+ } catch {
17271
+ const raced = await getSessionEventByClientEventId(
17272
+ this.db,
17273
+ this.scope.workspaceId,
17274
+ this.scope.sessionId,
17275
+ clientEventId
17276
+ ).catch(() => null);
17277
+ if (!raced) return false;
17278
+ assertMatchingCodemodeToolCallCreated(raced, operation, entry, this.scope);
17279
+ return true;
16724
17280
  }
16725
17281
  }
16726
17282
  async settleWithOutput(operation, claimId, settlement) {
@@ -16732,7 +17288,7 @@ var CodemodeAttemptDispatcher = class {
16732
17288
  settlement
16733
17289
  });
16734
17290
  if (!result.committed || result.events.length === 0) return;
16735
- await publishDurableSessionEvents7(
17291
+ await publishDurableSessionEvents8(
16736
17292
  this.bus,
16737
17293
  this.scope.workspaceId,
16738
17294
  this.scope.sessionId,
@@ -16752,6 +17308,19 @@ var CodemodeAttemptDispatcher = class {
16752
17308
  });
16753
17309
  }
16754
17310
  };
17311
+ function codemodeToolCallCreatedClientEventId(operationId) {
17312
+ return `opengeni:codemode-tool-call-created:${operationId}`;
17313
+ }
17314
+ function assertMatchingCodemodeToolCallCreated(event, operation, entry, scope) {
17315
+ const payload = recordValue(event.payload);
17316
+ const raw = recordValue(payload?.raw);
17317
+ if (event.type !== "agent.toolCall.created" || event.turnId !== scope.turnId || event.turnGeneration !== scope.executionGeneration || event.turnAttemptId !== scope.attemptId || event.turnAssociation !== "current" || payload?.id !== operation.operationId || payload?.name !== entry.modelName || payload?.origin !== "codemode" || payload?.subjectId !== operation.caller.subjectId || raw?.type !== "codemode_call" || raw?.serverId !== operation.identity.serverId || raw?.toolName !== operation.identity.toolName || raw?.catalogDigest !== operation.catalogDigest) {
17318
+ throw new Error("Codemode tool-call creation id is bound to incompatible durable evidence");
17319
+ }
17320
+ }
17321
+ function recordValue(value) {
17322
+ return value !== null && typeof value === "object" && !Array.isArray(value) ? value : null;
17323
+ }
16755
17324
  function connectorActionWasNotExecuted(error) {
16756
17325
  return Boolean(
16757
17326
  error && typeof error === "object" && "connectorActionOutcome" in error && error.connectorActionOutcome === "not_executed"
@@ -16760,8 +17329,10 @@ function connectorActionWasNotExecuted(error) {
16760
17329
  function combinedSignal(primary, secondary) {
16761
17330
  return secondary ? AbortSignal.any([primary, secondary]) : primary;
16762
17331
  }
16763
- function errorCode(error, signal) {
16764
- if (signal.aborted) return "attempt_cancelled_during_execution";
17332
+ function errorCode(error, signal, executionStarted) {
17333
+ if (signal.aborted) {
17334
+ return executionStarted ? "attempt_cancelled_during_execution" : "attempt_cancelled";
17335
+ }
16765
17336
  if (connectorActionWasNotExecuted(error)) return "connector_action_not_executed";
16766
17337
  if (typeof error === "object" && error !== null && "code" in error) {
16767
17338
  const code = error.code;
@@ -16770,7 +17341,7 @@ function errorCode(error, signal) {
16770
17341
  return "tool_outcome_unknown";
16771
17342
  }
16772
17343
  function safeErrorMessage(error) {
16773
- if (error instanceof AttemptToolApprovalRequiredError3 || error instanceof AttemptToolCatalogStaleError || error instanceof AttemptToolNotFoundError) {
17344
+ if (error instanceof AttemptToolApprovalRequiredError || error instanceof AttemptToolCatalogStaleError || error instanceof AttemptToolInputValidationError || error instanceof AttemptToolNotFoundError) {
16774
17345
  return error.message;
16775
17346
  }
16776
17347
  return "Codemode call failed before tool execution";
@@ -16782,64 +17353,79 @@ import {
16782
17353
  defaultSessionMcpServerIds,
16783
17354
  loadRigDefaultVariableSetEnvironment,
16784
17355
  mergeRigDefaultVariableSetEnvironment,
17356
+ buildApiIntegrationMcpServers,
17357
+ resolveCatalogSettings as resolveCatalogSettings2,
17358
+ resolveWorkspaceModelSelection,
16785
17359
  withFrozenPersonalConnectionDelegations,
16786
17360
  resolveSessionToolPolicy
16787
17361
  } from "@opengeni/core";
16788
17362
  import { SandboxChannelAService as SandboxChannelAService3 } from "@opengeni/runtime/sandbox";
16789
17363
  import { sandboxRunAs as sandboxRunAs4 } from "@opengeni/runtime";
16790
17364
 
16791
- // src/activities/agent-turn/session-title.ts
16792
- import { hasPermission } from "@opengeni/core";
16793
- import {
16794
- AUTOMATIC_SESSION_TITLE_FALLBACK,
16795
- DEFAULT_FIRST_PARTY_MCP_PERMISSIONS
16796
- } from "@opengeni/contracts";
16797
- var SESSION_TITLE_MODEL_TOOL_NAME = "opengeni__set_session_title";
16798
- var SESSION_TITLE_DESCRIPTION = "Set this session's display title to a concise 3-7 word topic label. Use a stable noun phrase about the actual task or subject, never a quote/prefix of a prompt, greeting, request boilerplate, URL, identifier, credential, token, or other sensitive value. Call once on a new session, then only when the topic materially changes. Never call it as routine setup after a continuation, resume, or interruption, or merely to reassert the same title. A human-set title cannot be replaced.";
16799
- function shouldRequestMissingSessionTitle(input) {
16800
- const title = input.title?.trim() ?? "";
16801
- const needsSemanticTitle = input.titleSource !== "user" && (!title || title === AUTOMATIC_SESSION_TITLE_FALLBACK);
16802
- if (!needsSemanticTitle) return false;
16803
- if (!input.firstPartyMcpTools.includes("set_session_title")) return false;
16804
- const permissions = input.firstPartyMcpPermissions ?? DEFAULT_FIRST_PARTY_MCP_PERMISSIONS;
16805
- return hasPermission([...permissions], "sessions:control");
16806
- }
16807
- function sessionTitleToolPlan(input) {
16808
- const promoteTitleTool = input.shouldRequestTitle && input.tools.some((tool) => tool.kind === "mcp" && tool.id === "opengeni");
17365
+ // src/activities/agent-turn/turn-sandbox-access.ts
17366
+ async function resolveTurnSandboxAccess(sandboxState, sdkOwnedSandboxSession, unavailableMessage) {
17367
+ let sandbox = sandboxState.resolvedSandbox;
17368
+ if (!sandbox && sandboxState.turnSandboxProvisioner) {
17369
+ sandbox = await sandboxState.turnSandboxProvisioner.get();
17370
+ }
17371
+ const session = sandboxState.lazyOwnedSandbox?.session ?? sandbox?.established.session ?? sdkOwnedSandboxSession;
17372
+ if (!session) throw new Error(unavailableMessage);
16809
17373
  return {
16810
- promoteTitleTool,
16811
- remoteFirstPartyMcpTools: promoteTitleTool ? input.selectedFirstPartyMcpTools.filter((tool) => tool !== "set_session_title") : [...input.selectedFirstPartyMcpTools],
16812
- preparationIndependentToolNames: promoteTitleTool ? [SESSION_TITLE_MODEL_TOOL_NAME] : []
17374
+ sandbox,
17375
+ session,
17376
+ leaseEpoch: sandbox?.leaseEpoch ?? 0
16813
17377
  };
16814
17378
  }
16815
- function createSessionTitleAttemptToolDefinition(input) {
17379
+
17380
+ // src/activities/agent-turn/list-models.ts
17381
+ import {
17382
+ compareModelPickerOrder,
17383
+ modelPickerBillingClassFor
17384
+ } from "@opengeni/contracts/model-picker-order";
17385
+ var LIST_MODELS_TOOL_NAME = "list_models";
17386
+ var LIST_MODELS_TOOL_DESCRIPTION = "List the models this workspace can select right now, in picker order, with deployment-defined cost and optional guidance. This does not switch the current session model; use an ID with session_create or the human model picker.";
17387
+ function safeLineField(value) {
17388
+ return value.replace(/[\r\n|]/gu, " ").replace(/\s+/gu, " ").trim();
17389
+ }
17390
+ function renderListModels(input) {
17391
+ const lines = [];
17392
+ if (input.currentModelId) {
17393
+ lines.push(`Current: ${safeLineField(input.currentModelId)}`);
17394
+ }
17395
+ const selectable = input.selections.filter((entry) => entry.availability.selectable).map((selection) => ({
17396
+ selection,
17397
+ billingClass: modelPickerBillingClassFor(selection.model),
17398
+ selectable: selection.availability.selectable,
17399
+ label: selection.model.label
17400
+ })).sort(compareModelPickerOrder);
17401
+ if (selectable.length === 0) {
17402
+ lines.push("No models are available in this workspace.");
17403
+ return lines.join("\n");
17404
+ }
17405
+ for (const { selection } of selectable) {
17406
+ const { model } = selection;
17407
+ const fields = [safeLineField(model.id), safeLineField(model.label), model.cost];
17408
+ const note = input.modelNotes[model.id];
17409
+ if (note) fields.push(safeLineField(note));
17410
+ lines.push(`- ${fields.join(" | ")}`);
17411
+ }
17412
+ return lines.join("\n");
17413
+ }
17414
+ function createListModelsAttemptToolDefinition(input) {
16816
17415
  return {
16817
- identity: { serverId: "opengeni", toolName: "set_session_title" },
16818
- modelName: SESSION_TITLE_MODEL_TOOL_NAME,
16819
- codemodePath: ["opengeni", "set_session_title"],
16820
- title: "Set session title",
16821
- description: SESSION_TITLE_DESCRIPTION,
17416
+ identity: { serverId: "opengeni", toolName: LIST_MODELS_TOOL_NAME },
17417
+ modelName: LIST_MODELS_TOOL_NAME,
17418
+ codemodePath: ["opengeni", LIST_MODELS_TOOL_NAME],
17419
+ title: "List selectable models",
17420
+ description: LIST_MODELS_TOOL_DESCRIPTION,
16822
17421
  inputSchema: {
16823
17422
  type: "object",
16824
- properties: {
16825
- title: { type: "string", minLength: 1, maxLength: 200 }
16826
- },
16827
- required: ["title"],
16828
- additionalProperties: false
16829
- },
16830
- outputSchema: {
16831
- type: "object",
16832
- properties: {
16833
- ok: { type: "boolean", const: true },
16834
- updated: { type: "boolean" },
16835
- title: { type: "string" }
16836
- },
16837
- required: ["ok", "updated", "title"],
17423
+ properties: {},
16838
17424
  additionalProperties: false
16839
17425
  },
16840
17426
  annotations: {
16841
- title: "Set session title",
16842
- readOnlyHint: false,
17427
+ title: "List selectable models",
17428
+ readOnlyHint: true,
16843
17429
  destructiveHint: false,
16844
17430
  idempotentHint: true,
16845
17431
  openWorldHint: false
@@ -16847,40 +17433,27 @@ function createSessionTitleAttemptToolDefinition(input) {
16847
17433
  source: "opengeni",
16848
17434
  approval: "none",
16849
17435
  execute: async (args) => {
16850
- const title = args.title;
16851
- if (typeof title !== "string") {
16852
- throw new Error("set_session_title requires a title string");
17436
+ if (Object.keys(args).length !== 0) {
17437
+ throw new Error("list_models accepts no arguments");
16853
17438
  }
16854
- const result = await input.updateTitle(title);
16855
- const output = {
16856
- ok: true,
16857
- updated: result.updated,
16858
- title: result.title ?? title
16859
- };
17439
+ const catalog = await input.load();
16860
17440
  return {
16861
17441
  isError: false,
16862
- content: [{ type: "text", text: JSON.stringify(output) }],
16863
- structuredContent: output
17442
+ content: [
17443
+ {
17444
+ type: "text",
17445
+ text: renderListModels({
17446
+ currentModelId: input.currentModelId,
17447
+ selections: catalog.selections,
17448
+ modelNotes: catalog.modelNotes
17449
+ })
17450
+ }
17451
+ ]
16864
17452
  };
16865
17453
  }
16866
17454
  };
16867
17455
  }
16868
17456
 
16869
- // src/activities/agent-turn/turn-sandbox-access.ts
16870
- async function resolveTurnSandboxAccess(sandboxState, sdkOwnedSandboxSession, unavailableMessage) {
16871
- let sandbox = sandboxState.resolvedSandbox;
16872
- if (!sandbox && sandboxState.turnSandboxProvisioner) {
16873
- sandbox = await sandboxState.turnSandboxProvisioner.get();
16874
- }
16875
- const session = sandboxState.lazyOwnedSandbox?.session ?? sandbox?.established.session ?? sdkOwnedSandboxSession;
16876
- if (!session) throw new Error(unavailableMessage);
16877
- return {
16878
- sandbox,
16879
- session,
16880
- leaseEpoch: sandbox?.leaseEpoch ?? 0
16881
- };
16882
- }
16883
-
16884
17457
  // src/activities/agent-turn/tool-environment.ts
16885
17458
  async function prepareTurnToolPolicy(deps) {
16886
17459
  const {
@@ -17002,6 +17575,7 @@ async function prepareTurnToolPolicy(deps) {
17002
17575
  async function prepareTurnToolRuntime(deps) {
17003
17576
  const {
17004
17577
  input,
17578
+ catalogSourceSettings,
17005
17579
  db,
17006
17580
  bus,
17007
17581
  runtime,
@@ -17106,10 +17680,13 @@ async function prepareTurnToolRuntime(deps) {
17106
17680
  if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
17107
17681
  return;
17108
17682
  }
17109
- await eventing.publish([{ type: "tool.auth_needed", payload }], true);
17683
+ await eventing.publish(
17684
+ [{ type: "tool.auth_needed", payload: rollingSafeToolAuthNeededPayload(payload) }],
17685
+ true
17686
+ );
17110
17687
  };
17111
17688
  const selectedApiIntegrationServerIds = new Set(turnTools.map((tool) => tool.id));
17112
- const apiIntegrationMcpServers = buildApiIntegrationServersForTurn({
17689
+ const apiIntegrationMcpServers = buildApiIntegrationMcpServers({
17113
17690
  settings: runSettings,
17114
17691
  integrations: installedApiIntegrations.filter(
17115
17692
  (integration) => selectedApiIntegrationServerIds.has(integration.serverId)
@@ -17173,7 +17750,8 @@ async function prepareTurnToolRuntime(deps) {
17173
17750
  titleSource: session.titleSource,
17174
17751
  firstPartyMcpTools: selectedFirstPartyMcpTools,
17175
17752
  firstPartyMcpPermissions: session.firstPartyMcpPermissions
17176
- })
17753
+ }),
17754
+ parallelGenerationAvailable: typeof runtime.generateSessionTitle === "function"
17177
17755
  });
17178
17756
  const googleDrivePublicationAllowed = selectedFirstPartyMcpTools.includes("editable_artifact_export") && selectedFirstPartyMcpTools.includes("editable_artifact_export_status") && (!session.firstPartyMcpPermissions?.length || session.firstPartyMcpPermissions.includes("artifacts:read") && session.firstPartyMcpPermissions.includes("artifacts:publish"));
17179
17757
  const googleDriveConnectorBindings = googleDrivePublicationTool && googleDrivePublicationTarget && googleDrivePublicationAllowed ? [
@@ -17186,17 +17764,108 @@ async function prepareTurnToolRuntime(deps) {
17186
17764
  )
17187
17765
  }
17188
17766
  ] : [];
17767
+ const attemptConnectorActionBindings = [
17768
+ ...googleDriveConnectorBindings,
17769
+ ...githubRestMcp.connectorBindings
17770
+ ];
17771
+ const connectorActionPolicy = {
17772
+ preview: async (call) => await previewConnectorActionApproval(db, connectorActionIdentity, call),
17773
+ prepare: async (call) => await prepareConnectorActionApproval(db, connectorActionIdentity, call),
17774
+ begin: async (call) => await beginConnectorActionExecution(db, connectorActionIdentity, call),
17775
+ complete: async ({ requestId, outcome }) => await completeConnectorActionExecution(db, {
17776
+ accountId: input.accountId,
17777
+ workspaceId: input.workspaceId,
17778
+ requestId,
17779
+ attemptId: input.attemptId,
17780
+ outcome
17781
+ })
17782
+ };
17189
17783
  const attemptToolDefinitions = [
17784
+ createListModelsAttemptToolDefinition({
17785
+ currentModelId: turnExecutionPolicy.productModelId,
17786
+ load: async () => {
17787
+ const currentCatalog = await resolveCatalogSettings2(db, catalogSourceSettings);
17788
+ const currentSettings = currentCatalog.settings;
17789
+ const xaiReadinessAuthority = xaiCatalogReadinessAuthority(turn, credentialSubjectId);
17790
+ const [
17791
+ policy,
17792
+ codexSubscriptionActive,
17793
+ xaiSubscriptionActive,
17794
+ workspaceGatewayConnectionActive,
17795
+ workspaceGatewayCustomModels,
17796
+ openRouterConnectionActive,
17797
+ workspaceOpenRouterCustomModels,
17798
+ organizationGatewayConnectionActive,
17799
+ organizationGatewayCustomModels,
17800
+ organizationOpenRouterConnectionActive,
17801
+ organizationOpenRouterCustomModels
17802
+ ] = await Promise.all([
17803
+ getWorkspaceModelPolicy2(db, input.workspaceId),
17804
+ workspaceCodexSubscriptionActive3(db, currentSettings, input.workspaceId),
17805
+ xaiReadinessAuthority && currentSettings.supergrokSubscriptionEnabled ? workspaceXaiSubscriptionActiveForAuthority(db, currentSettings, {
17806
+ workspaceId: input.workspaceId,
17807
+ ...xaiReadinessAuthority
17808
+ }) : false,
17809
+ workspaceVercelAiGatewayConnectionActive(db, input.workspaceId),
17810
+ listWorkspaceGatewayCustomModels2(db, {
17811
+ accountId: input.accountId,
17812
+ workspaceId: input.workspaceId
17813
+ }),
17814
+ workspaceOpenRouterConnectionActive(db, input.workspaceId),
17815
+ listWorkspaceOpenRouterCustomModels2(db, {
17816
+ accountId: input.accountId,
17817
+ workspaceId: input.workspaceId
17818
+ }),
17819
+ organizationModelProviderConnectionActiveForWorkspace(db, {
17820
+ accountId: input.accountId,
17821
+ workspaceId: input.workspaceId,
17822
+ providerKind: "vercel_gateway"
17823
+ }),
17824
+ listOrganizationModelProviderCustomModelsForWorkspace2(db, {
17825
+ accountId: input.accountId,
17826
+ workspaceId: input.workspaceId,
17827
+ providerKind: "vercel_gateway"
17828
+ }),
17829
+ organizationModelProviderConnectionActiveForWorkspace(db, {
17830
+ accountId: input.accountId,
17831
+ workspaceId: input.workspaceId,
17832
+ providerKind: "openrouter"
17833
+ }),
17834
+ listOrganizationModelProviderCustomModelsForWorkspace2(db, {
17835
+ accountId: input.accountId,
17836
+ workspaceId: input.workspaceId,
17837
+ providerKind: "openrouter"
17838
+ })
17839
+ ]);
17840
+ return {
17841
+ selections: resolveWorkspaceModelSelection({
17842
+ settings: currentSettings,
17843
+ policy,
17844
+ codexSubscriptionActive,
17845
+ xaiSubscriptionActive,
17846
+ workspaceGatewayConnectionActive,
17847
+ workspaceGatewayCustomModels,
17848
+ workspaceOpenRouterConnectionActive: openRouterConnectionActive,
17849
+ workspaceOpenRouterCustomModels,
17850
+ organizationGatewayConnectionActive,
17851
+ organizationGatewayCustomModels,
17852
+ organizationOpenRouterConnectionActive,
17853
+ organizationOpenRouterCustomModels
17854
+ }),
17855
+ modelNotes: currentCatalog.modelNotes
17856
+ };
17857
+ }
17858
+ }),
17190
17859
  ...titleToolPlan.promoteTitleTool ? [
17191
17860
  createSessionTitleAttemptToolDefinition({
17192
17861
  updateTitle: async (title) => {
17193
- const result = await updateSessionTitleWithEvent(db, {
17862
+ const result = await updateSessionTitleWithEvent2(db, {
17194
17863
  workspaceId: input.workspaceId,
17195
17864
  sessionId: input.sessionId,
17196
17865
  title,
17197
17866
  source: "agent"
17198
17867
  });
17199
- await publishDurableSessionEvents8(
17868
+ await publishDurableSessionEvents9(
17200
17869
  bus,
17201
17870
  input.workspaceId,
17202
17871
  input.sessionId,
@@ -17322,21 +17991,8 @@ async function prepareTurnToolRuntime(deps) {
17322
17991
  nestedAgentDepth: session.nestedAgentDepth,
17323
17992
  effectiveMaxNestedAgentDepth: session.effectiveMaxNestedAgentDepth,
17324
17993
  attemptToolDefinitions,
17325
- ...googleDrivePublicationTarget && googleDrivePublicationAllowed || githubRestMcp.authorizeCodemodeCall ? {
17326
- attemptToolAuthorize: async (authorization) => {
17327
- const { call } = authorization;
17328
- if (googleDrivePublicationTarget && googleDrivePublicationAllowed && call.caller.kind === "codemode" && call.identity.serverId === "google-drive-publishing" && call.identity.toolName === "google_drive_publish_file") {
17329
- await authorizeGoogleDrivePublicationAttempt({
17330
- db,
17331
- identity: connectorActionIdentity,
17332
- target: googleDrivePublicationTarget,
17333
- approvalId: call.operationId,
17334
- arguments: call.arguments
17335
- });
17336
- }
17337
- await githubRestMcp.authorizeCodemodeCall?.(authorization);
17338
- }
17339
- } : {}
17994
+ connectorActionPolicy,
17995
+ attemptConnectorActionBindings
17340
17996
  }),
17341
17997
  cancellationSignal,
17342
17998
  async (latePreparedTools) => await latePreparedTools.close().catch(() => void 0)
@@ -17395,11 +18051,9 @@ async function prepareTurnToolRuntime(deps) {
17395
18051
  activatePreparedToolEnvironment(eventing.preparedTools);
17396
18052
  }
17397
18053
  return {
17398
- attemptConnectorActionBindings: [
17399
- ...googleDriveConnectorBindings,
17400
- ...githubRestMcp.connectorBindings
17401
- ],
17402
- connectorActionIdentity,
18054
+ attemptConnectorActionBindings,
18055
+ connectorActionPolicy,
18056
+ generateSessionTitleInParallel: titleToolPlan.generateTitleInParallel,
17403
18057
  postToolPreparationStartedAt,
17404
18058
  preparationIndependentToolNames: titleToolPlan.preparationIndependentToolNames
17405
18059
  };
@@ -17596,16 +18250,13 @@ import {
17596
18250
  getXaiSessionAccountPin as getXaiSessionAccountPin2,
17597
18251
  setXaiSessionAccountPin as setXaiSessionAccountPin2,
17598
18252
  getWorkspaceVideoGenerationPolicy,
17599
- loadWorkspaceVercelAiGatewayCredentialLease,
17600
- beginConnectorActionExecution as beginConnectorActionExecution3,
17601
- completeConnectorActionExecution as completeConnectorActionExecution3,
17602
- prepareConnectorActionApproval as prepareConnectorActionApproval3
18253
+ loadWorkspaceVercelAiGatewayCredentialLease
17603
18254
  } from "@opengeni/db";
17604
18255
  import {
17605
- serviceTierForLatencyMode,
18256
+ serviceTierForLatencyMode as serviceTierForLatencyMode2,
17606
18257
  environmentsEncryptionKeyBytes as environmentsEncryptionKeyBytes4,
17607
- WORKSPACE_GATEWAY_PROVIDER_ID,
17608
- resolveModelProvider
18258
+ WORKSPACE_GATEWAY_PROVIDER_ID as WORKSPACE_GATEWAY_PROVIDER_ID2,
18259
+ resolveModelProvider as resolveModelProvider2
17609
18260
  } from "@opengeni/config";
17610
18261
 
17611
18262
  // src/activities/xai-image-generation.ts
@@ -18536,18 +19187,27 @@ import {
18536
19187
  var CODEX_IMAGE_MODEL = "gpt-image-2";
18537
19188
  async function executeCodexImageGeneration(input) {
18538
19189
  const providerBindingHash = imageProviderBindingHash(CODEX_PROVIDER_ID2, input.credentialId);
19190
+ let providerDispatchAdmitted = false;
19191
+ const codexContext = {
19192
+ ...input.codexContext,
19193
+ beforeProviderDispatch: async () => {
19194
+ await input.codexContext.beforeProviderDispatch?.();
19195
+ providerDispatchAdmitted = true;
19196
+ }
19197
+ };
18539
19198
  return await executeImageGenerationOperation({
18540
19199
  ...input,
18541
19200
  providerId: CODEX_PROVIDER_ID2,
18542
19201
  providerBindingHash,
18543
19202
  modelId: CODEX_IMAGE_MODEL,
18544
19203
  ...input.references ? { referenceDigests: input.references } : {},
19204
+ isProviderDispatchRejected: (error) => !providerDispatchAdmitted && error instanceof CodexCredentialLeaseLostError,
18545
19205
  generate: async () => {
18546
19206
  const generated = await generateCodexSubscriptionImage({
18547
19207
  prompt: input.prompt,
18548
19208
  ...input.references ? { references: input.references } : {},
18549
19209
  turnId: input.turnId,
18550
- context: input.codexContext,
19210
+ context: codexContext,
18551
19211
  ...input.abortSignal ? { abortSignal: input.abortSignal } : {}
18552
19212
  });
18553
19213
  return {
@@ -18679,19 +19339,16 @@ async function referenceBytes(input, reference2) {
18679
19339
  );
18680
19340
  }
18681
19341
  return {
18682
- bytes: await readStoredFile(input.objectStorage, file),
19342
+ bytes: await readStoredImageReferenceFile(input.objectStorage, file),
18683
19343
  file
18684
19344
  };
18685
19345
  }
18686
- async function readStoredFile(objectStorage, file) {
18687
- const bytes = await objectStorage.getFileRange(file, {
18688
- start: 0,
18689
- end: file.sizeBytes - 1
18690
- });
18691
- if (!bytes) {
19346
+ async function readStoredImageReferenceFile(objectStorage, file) {
19347
+ const bytes = await objectStorage.getFileBytes(file);
19348
+ if (bytes.byteLength !== file.sizeBytes) {
18692
19349
  throw new ImageGenerationReferenceError(
18693
- "reference_unavailable",
18694
- "The durable image reference bytes are unavailable."
19350
+ "reference_integrity_mismatch",
19351
+ "The durable image reference bytes do not match their file metadata."
18695
19352
  );
18696
19353
  }
18697
19354
  return bytes;
@@ -18806,7 +19463,8 @@ async function buildTurnAgent(deps) {
18806
19463
  sandboxCodemodeToken,
18807
19464
  fileResourceDownloads,
18808
19465
  attemptConnectorActionBindings,
18809
- connectorActionIdentity,
19466
+ connectorActionPolicy,
19467
+ trigger,
18810
19468
  preparationIndependentToolNames,
18811
19469
  videoGenerationAcceptancesByCallId,
18812
19470
  activeSandboxBackend,
@@ -18940,9 +19598,9 @@ async function buildTurnAgent(deps) {
18940
19598
  }
18941
19599
  };
18942
19600
  }
18943
- const gatewayResolution = resolveModelProvider(
19601
+ const gatewayResolution = resolveModelProvider2(
18944
19602
  capabilitySettings,
18945
- WORKSPACE_GATEWAY_PROVIDER_ID
19603
+ WORKSPACE_GATEWAY_PROVIDER_ID2
18946
19604
  );
18947
19605
  const gateway = gatewayResolution?.provider;
18948
19606
  if (gateway?.kind !== "vercel-gateway-workspace" || !gateway.apiKey) return {};
@@ -19121,21 +19779,11 @@ async function buildTurnAgent(deps) {
19121
19779
  }
19122
19780
  };
19123
19781
  })();
19124
- const serviceTier = serviceTierForLatencyMode(
19782
+ const serviceTier = serviceTierForLatencyMode2(
19125
19783
  turnExecutionPolicy.providerId,
19126
19784
  turnExecutionPolicy.latencyMode
19127
19785
  );
19128
- const connectorActionPolicy = {
19129
- prepare: async (call) => await prepareConnectorActionApproval3(db, connectorActionIdentity, call),
19130
- begin: async (call) => await beginConnectorActionExecution3(db, connectorActionIdentity, call),
19131
- complete: async ({ requestId, outcome }) => await completeConnectorActionExecution3(db, {
19132
- accountId: input.accountId,
19133
- workspaceId: input.workspaceId,
19134
- requestId,
19135
- attemptId: input.attemptId,
19136
- outcome
19137
- })
19138
- };
19786
+ const approvedToolCallId = approvedConnectorActionCallId(trigger);
19139
19787
  const runtimeSkillActivations = [
19140
19788
  ...installedSkillRuntime.activations,
19141
19789
  ...packRuntime.skillActivations,
@@ -19212,6 +19860,7 @@ async function buildTurnAgent(deps) {
19212
19860
  resolvedMcpConnectionIds: preparedTools.resolvedMcpConnectionIds,
19213
19861
  connectorActionPolicy,
19214
19862
  attemptConnectorActionBindings,
19863
+ ...approvedToolCallId ? { approvedToolCallId } : {},
19215
19864
  // LIVE by-reference connector namespaces (fills during this turn's
19216
19865
  // codex_apps tools/list): the codex tool_search description reads it per
19217
19866
  // model call so the model sees the account's real connected sources.
@@ -19318,14 +19967,49 @@ async function buildTurnAgent(deps) {
19318
19967
  postAgentPreparationStartedAt
19319
19968
  };
19320
19969
  }
19970
+ function approvedConnectorActionCallId(trigger) {
19971
+ if (trigger.type !== "user.approvalDecision") {
19972
+ return null;
19973
+ }
19974
+ const payload = trigger.payload;
19975
+ return payload.decision === "approve" && typeof payload.approvalId === "string" ? payload.approvalId : null;
19976
+ }
19321
19977
 
19322
19978
  // src/activities/agent-turn/run.ts
19979
+ function sessionTitleCodexRequestContext(context, nextRequestId) {
19980
+ return {
19981
+ clientVersion: context.clientVersion,
19982
+ ...context.sessionId !== void 0 ? { sessionId: context.sessionId } : {},
19983
+ getToken: context.getToken,
19984
+ refresh: context.refresh,
19985
+ resolveModel: context.resolveModel,
19986
+ ...context.onUsageHeaders ? { onUsageHeaders: context.onUsageHeaders } : {},
19987
+ ...context.beforeProviderDispatch ? { beforeProviderDispatch: context.beforeProviderDispatch } : {},
19988
+ ...context.responseTimeoutPolicy ? { responseTimeoutPolicy: context.responseTimeoutPolicy } : {},
19989
+ nextRequestId,
19990
+ turnMetadata: { request_kind: "session_title" }
19991
+ };
19992
+ }
19993
+ function sessionTitleXaiRequestContext(context, nextRequestId) {
19994
+ return {
19995
+ clientVersion: context.clientVersion,
19996
+ sessionId: context.sessionId,
19997
+ turnId: context.turnId,
19998
+ getToken: context.getToken,
19999
+ refresh: context.refresh,
20000
+ resolveModel: context.resolveModel,
20001
+ ...context.streamIdleTimeoutMs !== void 0 ? { streamIdleTimeoutMs: context.streamIdleTimeoutMs } : {},
20002
+ ...context.hostedToolContinuationTimeoutMs !== void 0 ? { hostedToolContinuationTimeoutMs: context.hostedToolContinuationTimeoutMs } : {},
20003
+ nextRequestId
20004
+ };
20005
+ }
19323
20006
  function createRunAgentTurnActivity(services) {
19324
20007
  const modelCheckpointMemoryCollector = createModelCheckpointMemoryCollector();
19325
20008
  const turnCompletionMemoryCollector = createModelCheckpointMemoryCollector();
19326
20009
  return async function runAgentTurn(input) {
19327
20010
  const {
19328
20011
  settings,
20012
+ catalogSourceSettings = settings,
19329
20013
  db,
19330
20014
  bus,
19331
20015
  runtime,
@@ -19493,6 +20177,7 @@ function createRunAgentTurnActivity(services) {
19493
20177
  const claimed = await claimTurnAttempt({
19494
20178
  input,
19495
20179
  settings,
20180
+ catalogSourceSettings,
19496
20181
  db,
19497
20182
  bus,
19498
20183
  runtime,
@@ -19629,6 +20314,11 @@ function createRunAgentTurnActivity(services) {
19629
20314
  input.workspaceId,
19630
20315
  providerTurn.effectiveCodexCredentialId ?? ""
19631
20316
  );
20317
+ const resolveTrackedToken = async (resolve) => {
20318
+ const token = await resolve();
20319
+ providerTurn.effectiveCodexCredentialVersion = token.credentialVersion;
20320
+ return token;
20321
+ };
19632
20322
  return {
19633
20323
  clientVersion: CODEX_CLIENT_VERSION2,
19634
20324
  // Backend sticky cache-routing key — the SAME id as the body's
@@ -19638,8 +20328,8 @@ function createRunAgentTurnActivity(services) {
19638
20328
  // (per-request shard lottery = prod's measured 48.6% on sol);
19639
20329
  // with it, resends pin to the warm shard (Codex CLI parity).
19640
20330
  sessionId: input.sessionId,
19641
- getToken: () => resolver.getToken(),
19642
- refresh: () => resolver.refresh(),
20331
+ getToken: () => resolveTrackedToken(resolver.getToken),
20332
+ refresh: () => resolveTrackedToken(resolver.refresh),
19643
20333
  resolveModel: buildModelResolver(
19644
20334
  CODEX_FALLBACK_MODEL_SLUGS,
19645
20335
  CODEX_FALLBACK_MODEL_SLUGS[0]
@@ -19648,6 +20338,9 @@ function createRunAgentTurnActivity(services) {
19648
20338
  providerTurn.latestCodexUsage = snapshot;
19649
20339
  },
19650
20340
  // latest wins; flushed once in finally
20341
+ beforeProviderDispatch: () => {
20342
+ leases.codex.assertUsable();
20343
+ },
19651
20344
  onRequestPreparationDiagnostic: (phase) => {
19652
20345
  if (eventing.firstModelRequestCheckpointAt === null || firstModelRequestCheckpoints.has(phase)) {
19653
20346
  return;
@@ -19880,6 +20573,17 @@ function createRunAgentTurnActivity(services) {
19880
20573
  }
19881
20574
  const withCodex = (fn) => codexContext ? codexRequestStorage.run(codexContext, fn) : fn();
19882
20575
  const withProviderRequestContext = (fn) => providerTurn.xaiRequestContext ? xaiSubscriptionRequestStorage.run(providerTurn.xaiRequestContext, fn) : withCodex(fn);
20576
+ let codexSessionTitleRequestSequence = 0;
20577
+ let xaiSessionTitleRequestSequence = 0;
20578
+ const codexSessionTitleContext = codexContext ? sessionTitleCodexRequestContext(
20579
+ codexContext,
20580
+ () => `${dispatchId}:title:${++codexSessionTitleRequestSequence}`
20581
+ ) : null;
20582
+ const xaiSessionTitleContext = providerTurn.xaiRequestContext ? sessionTitleXaiRequestContext(
20583
+ providerTurn.xaiRequestContext,
20584
+ () => `${dispatchId}:xai:title:${++xaiSessionTitleRequestSequence}`
20585
+ ) : null;
20586
+ const withSessionTitleProviderRequestContext = (fn) => xaiSessionTitleContext ? xaiSubscriptionRequestStorage.run(xaiSessionTitleContext, fn) : codexSessionTitleContext ? codexRequestStorage.run(codexSessionTitleContext, fn) : fn();
19883
20587
  const withCodexRemoteCompaction = (fn) => withCodex(
19884
20588
  () => withCodexRequestOverrides(
19885
20589
  {
@@ -19897,7 +20601,7 @@ function createRunAgentTurnActivity(services) {
19897
20601
  );
19898
20602
  const compactionPrep = await prepareCompaction({
19899
20603
  input,
19900
- settings,
20604
+ settings: capabilitySettings,
19901
20605
  db,
19902
20606
  bus,
19903
20607
  observability,
@@ -20150,6 +20854,7 @@ function createRunAgentTurnActivity(services) {
20150
20854
  ];
20151
20855
  const toolRuntime = await prepareTurnToolRuntime({
20152
20856
  input,
20857
+ catalogSourceSettings,
20153
20858
  db,
20154
20859
  bus,
20155
20860
  runtime,
@@ -20188,7 +20893,8 @@ function createRunAgentTurnActivity(services) {
20188
20893
  });
20189
20894
  const {
20190
20895
  attemptConnectorActionBindings,
20191
- connectorActionIdentity,
20896
+ connectorActionPolicy,
20897
+ generateSessionTitleInParallel,
20192
20898
  postToolPreparationStartedAt,
20193
20899
  preparationIndependentToolNames
20194
20900
  } = toolRuntime;
@@ -20241,7 +20947,8 @@ function createRunAgentTurnActivity(services) {
20241
20947
  sandboxCodemodeToken,
20242
20948
  fileResourceDownloads,
20243
20949
  attemptConnectorActionBindings,
20244
- connectorActionIdentity,
20950
+ connectorActionPolicy,
20951
+ trigger,
20245
20952
  preparationIndependentToolNames,
20246
20953
  videoGenerationAcceptancesByCallId,
20247
20954
  activeSandboxBackend,
@@ -20325,7 +21032,7 @@ function createRunAgentTurnActivity(services) {
20325
21032
  };
20326
21033
  const postAgentCompaction = await runPostAgentCompaction({
20327
21034
  input,
20328
- settings,
21035
+ settings: capabilitySettings,
20329
21036
  db,
20330
21037
  bus,
20331
21038
  observability,
@@ -20473,8 +21180,9 @@ function createRunAgentTurnActivity(services) {
20473
21180
  );
20474
21181
  return await runTurnStreamAttempt({
20475
21182
  input,
20476
- settings,
21183
+ settings: capabilitySettings,
20477
21184
  db,
21185
+ bus,
20478
21186
  runtime,
20479
21187
  objectStorage,
20480
21188
  observability,
@@ -20505,6 +21213,7 @@ function createRunAgentTurnActivity(services) {
20505
21213
  attachCodemodeTokenRenewal,
20506
21214
  attachRunCredentialRenewal,
20507
21215
  withProviderRequestContext,
21216
+ withSessionTitleProviderRequestContext,
20508
21217
  publishCompactionLiveEvents,
20509
21218
  publishCompactionOutcomeEvents,
20510
21219
  recordCompanyBrainContributionReceiptOnce,
@@ -20530,6 +21239,8 @@ function createRunAgentTurnActivity(services) {
20530
21239
  providerApi,
20531
21240
  runSettings,
20532
21241
  turnTools,
21242
+ generateSessionTitleInParallel,
21243
+ sessionTitlePrompt: session.initialMessage.trim() ? session.initialMessage : turn.prompt,
20533
21244
  compactSummarizer,
20534
21245
  settleDeferredSteerAfterCompaction,
20535
21246
  compactionModelHistoryProjector,
@@ -20554,7 +21265,6 @@ function createRunAgentTurnActivity(services) {
20554
21265
  bus,
20555
21266
  observability,
20556
21267
  wakeSessionWorkflow,
20557
- signalCodexCapacityWorkflow,
20558
21268
  cancellationSignal,
20559
21269
  sandboxRotationController,
20560
21270
  noteCancellationRequested,
@@ -20630,7 +21340,7 @@ import {
20630
21340
  settleVideoGenerationFailure,
20631
21341
  settleVideoGenerationReady
20632
21342
  } from "@opengeni/db";
20633
- import { publishDurableSessionEvents as publishDurableSessionEvents9 } from "@opengeni/events";
21343
+ import { publishDurableSessionEvents as publishDurableSessionEvents10 } from "@opengeni/events";
20634
21344
 
20635
21345
  // src/activities/gateway-video-generation.ts
20636
21346
  import { readResponseTextBounded as readResponseTextBounded2 } from "@opengeni/network";
@@ -21432,7 +22142,7 @@ async function deliverTerminalResult(service, operation) {
21432
22142
  return;
21433
22143
  }
21434
22144
  if (result.added && result.events.length > 0) {
21435
- await publishDurableSessionEvents9(
22145
+ await publishDurableSessionEvents10(
21436
22146
  service.bus,
21437
22147
  operation.workspaceId,
21438
22148
  operation.sessionId,
@@ -21697,4 +22407,4 @@ export {
21697
22407
  createTurnActivities,
21698
22408
  createTurnActivitiesFromServices
21699
22409
  };
21700
- //# sourceMappingURL=activities-turn-A2G2KLSY.js.map
22410
+ //# sourceMappingURL=activities-turn-S5IU347A.js.map