@opengeni/worker-bundle 0.16.14 → 0.16.21

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.
package/dist/index.js CHANGED
@@ -60,7 +60,6 @@ import {
60
60
  getSessionEvent as getSessionEvent2,
61
61
  getSessionRootId as getSessionRootId2,
62
62
  getSessionGoal as getSessionGoal3,
63
- getLatestRunState as getLatestRunState2,
64
63
  getHumanInputResumeForEvent as getHumanInputResumeForEvent2,
65
64
  getSessionHumanInputRequest,
66
65
  installOrReadTurnExecutionPolicyForAttempt,
@@ -79,7 +78,6 @@ import {
79
78
  recordSessionActiveCodexCredential,
80
79
  setSessionCodexPin,
81
80
  recordCodexAccountUsageWithWakeTargets,
82
- recordCodexAccountConnectors,
83
81
  quarantineCodexCredentialForLease,
84
82
  setActiveCodexCredential,
85
83
  resolveWorkspaceMemoryBlock,
@@ -120,7 +118,8 @@ import {
120
118
  PreferenceRegistryInitiatorError,
121
119
  beginConnectorActionExecution,
122
120
  completeConnectorActionExecution,
123
- prepareConnectorActionApproval
121
+ prepareConnectorActionApproval,
122
+ withCodexAppsRequestAuthorization
124
123
  } from "@opengeni/db";
125
124
  import { appendAndPublishTurnEventsFenced, publishDurableSessionEvents } from "@opengeni/events";
126
125
  import { sandboxOperationMetricObserver } from "@opengeni/observability";
@@ -128,9 +127,10 @@ import {
128
127
  sandboxStateEntryFromRunState,
129
128
  maxTurnsExceededRunState,
130
129
  modelResponseServiceTierFromSdkEvent,
131
- modelResponseUsageFromSdkEvent,
130
+ modelTerminalResponseFromSdkEvent,
132
131
  normalizeModelCallUsage,
133
132
  normalizeSdkEvent,
133
+ projectHistoryForProvider as projectHistoryForProvider2,
134
134
  sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2,
135
135
  projectModelInputForCapabilities,
136
136
  appendPersistentSessionSettings,
@@ -160,9 +160,11 @@ import {
160
160
  sandboxFileDownloadFailureNote,
161
161
  SUMMARY_BUFFER_TOKENS as SUMMARY_BUFFER_TOKENS2,
162
162
  isMcpRequestTimeoutError,
163
+ isMcpTransportConnectivityError,
163
164
  runOwnedSandboxSetup,
164
165
  RoutingMutationOutcomeUnknownError,
165
166
  WorkspaceArchiveIntegrityError as WorkspaceArchiveIntegrityError2,
167
+ sdkBackendIdForSandboxBackend,
166
168
  swapTargetEstablishability,
167
169
  deleteRecordingArtifacts as deleteRecordingArtifacts2,
168
170
  stopRecording as stopRecordingOnBox2
@@ -326,28 +328,6 @@ function bindingRemaining(acct, now) {
326
328
  function cooling(acct, now) {
327
329
  return acct.exhaustedUntil != null && acct.exhaustedUntil.getTime() > now.getTime();
328
330
  }
329
- function covers(acct, usedConnectors) {
330
- if (usedConnectors.length === 0) {
331
- return true;
332
- }
333
- const owned = acct.connectorNamespaces;
334
- if (owned === null) {
335
- return false;
336
- }
337
- const ownedSet = new Set(owned);
338
- return usedConnectors.every((connector) => ownedSet.has(connector));
339
- }
340
- function droppedConnectorsFor(acct, usedConnectors) {
341
- if (usedConnectors.length === 0) {
342
- return [];
343
- }
344
- const owned = acct.connectorNamespaces;
345
- if (owned === null) {
346
- return [...usedConnectors];
347
- }
348
- const ownedSet = new Set(owned);
349
- return usedConnectors.filter((connector) => !ownedSet.has(connector));
350
- }
351
331
  function isCodexCredentialHealthy(acct, now) {
352
332
  return acct.status === "active" && !cooling(acct, now) && bindingUsedPct(acct, now) < CODEX_USAGE_EXHAUSTED_PCT;
353
333
  }
@@ -454,7 +434,6 @@ function chooseRotationActive(args) {
454
434
  const { activeCredentialId, priorCredentialId, accounts, now } = args;
455
435
  const rotationStrategy = effectiveRotationStrategy(args.rotationStrategy);
456
436
  const allocatableAccounts = accounts.filter((account) => account.allocatorEnabled);
457
- const usedConnectors = args.usedConnectors ?? [];
458
437
  if (allocatableAccounts.length === 0) {
459
438
  return { kind: "none" };
460
439
  }
@@ -466,12 +445,10 @@ function chooseRotationActive(args) {
466
445
  earliestResetAt: earliestReset(allocatableAccounts, now)
467
446
  };
468
447
  }
469
- const dropped = droppedConnectorsFor(chosen2, usedConnectors);
470
448
  return {
471
449
  kind: "active",
472
450
  credentialId: chosen2.id,
473
- moved: chosen2.id !== activeCredentialId,
474
- ...dropped.length > 0 ? { droppedConnectors: dropped } : {}
451
+ moved: chosen2.id !== activeCredentialId
475
452
  };
476
453
  };
477
454
  if (rotationStrategy === "round_robin") {
@@ -496,12 +473,10 @@ function chooseRotationActive(args) {
496
473
  }
497
474
  return decide(eligibles[0]);
498
475
  }
499
- const covering = eligibles.filter((acct) => covers(acct, usedConnectors));
500
- const pool = covering.length > 0 ? covering : eligibles;
501
476
  const activeLeases = (acct) => "activeLeaseCount" in acct ? acct.activeLeaseCount : 0;
502
477
  const selections = (acct) => "selectionCount" in acct ? acct.selectionCount : 0;
503
478
  const lastSelected = (acct) => "lastSelectedAt" in acct && acct.lastSelectedAt ? acct.lastSelectedAt.getTime() : 0;
504
- const chosen = pool.reduce((best, acct) => {
479
+ const chosen = eligibles.reduce((best, acct) => {
505
480
  if (!best) {
506
481
  return acct;
507
482
  }
@@ -558,18 +533,6 @@ function selectCodexCredentialLeaseForTurn(args) {
558
533
  }
559
534
  };
560
535
  }
561
- const continuation = args.continuationCredentialId ? accounts.find((account) => account.id === args.continuationCredentialId) : void 0;
562
- if (continuation && isCodexCredentialHealthy(continuation, args.now)) {
563
- return {
564
- credentialId: continuation.id,
565
- advanceActivePointer: false,
566
- decision: {
567
- kind: "active",
568
- credentialId: continuation.id,
569
- moved: continuation.id !== activeCredentialId
570
- }
571
- };
572
- }
573
536
  const connectedIds = new Set(
574
537
  accounts.filter((account) => account.allocatorEnabled).map((account) => account.id)
575
538
  );
@@ -650,8 +613,7 @@ function selectCodexCredentialLeaseForTurn(args) {
650
613
  // the workspace pointer is only correct when this session has never run.
651
614
  priorCredentialId: priorId,
652
615
  accounts,
653
- now: args.now,
654
- usedConnectors: accounts.find((account) => account.id === priorId)?.connectorNamespaces ?? []
616
+ now: args.now
655
617
  });
656
618
  return {
657
619
  credentialId: decision.kind === "active" ? decision.credentialId : null,
@@ -908,7 +870,6 @@ function codexCapacityDecision(context, settings) {
908
870
  sessionPinnedCredentialId: context.sessionPinnedCredentialId,
909
871
  sessionPinSource: context.sessionPinSource,
910
872
  sessionLastCredentialId: context.sessionLastCredentialId,
911
- continuationCredentialId: null,
912
873
  now
913
874
  });
914
875
  if (selected.credentialId) {
@@ -1019,6 +980,7 @@ import {
1019
980
  classifyCodexUsageLimitError,
1020
981
  codexRequestStorage,
1021
982
  isCodexTransportError,
983
+ opaqueProviderArtifactFingerprint,
1022
984
  withCodexRequestOverrides,
1023
985
  selectCodexCredentialId
1024
986
  } from "@opengeni/codex";
@@ -1053,6 +1015,7 @@ import {
1053
1015
  assertSessionAllowsProductModel,
1054
1016
  defaultSessionMcpServerIds,
1055
1017
  directPersonalConnectionSubjectId,
1018
+ resolveCodexAppsCredentialIdForRun,
1056
1019
  withFrozenPersonalConnectionDelegations,
1057
1020
  resolveSessionToolPolicy
1058
1021
  } from "@opengeni/core";
@@ -1097,40 +1060,35 @@ import {
1097
1060
  requireFile
1098
1061
  } from "@opengeni/db";
1099
1062
  import {
1100
- stripReasoningEncryptedContent,
1101
- stripReasoningIdentityFromSerializedRunState,
1102
- neutralizeToolSearchItemsInSerializedRunState
1063
+ projectHistoryForProvider,
1064
+ projectRejectedProviderArtifactsFromSerializedRunState,
1065
+ projectRejectedReasoningArtifact,
1066
+ hasOpaqueProviderArtifact
1103
1067
  } from "@opengeni/runtime";
1104
- var NON_CODEX_TURN = { currentCodexCredentialId: null };
1105
- function applyCodexHistoryStrip(rows, current) {
1068
+ function projectRejectedProviderArtifacts(rows) {
1106
1069
  const out = [];
1107
1070
  for (const row of rows) {
1108
- if (row.producerCodexCredentialId === current.currentCodexCredentialId && !row.providerArtifactInvalidatedAt) {
1071
+ if (!row.providerArtifactInvalidatedAt) {
1109
1072
  out.push(row.item);
1110
1073
  continue;
1111
1074
  }
1112
1075
  const type = typeof row.item.type === "string" ? row.item.type : void 0;
1113
1076
  if (type === "reasoning") {
1114
- continue;
1115
- }
1116
- if (type === "tool_search_call" || type === "tool_search_output") {
1077
+ out.push(projectRejectedReasoningArtifact(row.item));
1117
1078
  continue;
1118
1079
  }
1119
1080
  if (type === "compaction") {
1120
- out.push(stripReasoningEncryptedContent(row.item));
1121
1081
  continue;
1122
1082
  }
1123
1083
  out.push(row.item);
1124
1084
  }
1125
1085
  return out;
1126
1086
  }
1127
- function resumeRunStateForCodexAccount(state, current) {
1128
- if (state.frozenCodexCredentialId === current.currentCodexCredentialId && !state.providerArtifactInvalidatedAt) {
1087
+ function resumeRunState(state) {
1088
+ if (!state.providerArtifactInvalidatedAt) {
1129
1089
  return state.serializedRunState;
1130
1090
  }
1131
- return neutralizeToolSearchItemsInSerializedRunState(
1132
- stripReasoningIdentityFromSerializedRunState(state.serializedRunState)
1133
- );
1091
+ return projectRejectedProviderArtifactsFromSerializedRunState(state.serializedRunState);
1134
1092
  }
1135
1093
  var MAX_INLINE_MODEL_ATTACHMENT_BYTES = 20 * 1024 * 1024;
1136
1094
  var MODEL_IMAGE_CONTENT_TYPES = /* @__PURE__ */ new Set(["image/gif", "image/jpeg", "image/png", "image/webp"]);
@@ -1322,7 +1280,7 @@ function withCurrentUserAttachmentRefs(historyItems, refs) {
1322
1280
  }
1323
1281
  return historyItems;
1324
1282
  }
1325
- async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN, options) {
1283
+ async function turnInput(db, runtime, agent, trigger, options) {
1326
1284
  if (!trigger) {
1327
1285
  throw new Error("Missing trigger event");
1328
1286
  }
@@ -1367,8 +1325,8 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
1367
1325
  trigger,
1368
1326
  void 0,
1369
1327
  joinInternalContext(internalContext, attachmentContext),
1370
- current,
1371
1328
  fileAttachments.map((attachment) => attachment.resource),
1329
+ options.providerApi,
1372
1330
  options.projectModelHistory
1373
1331
  );
1374
1332
  }
@@ -1376,7 +1334,17 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
1376
1334
  if (updates.length === 0) {
1377
1335
  throw new Error("Internal update inference has no delivered updates");
1378
1336
  }
1379
- return await messageInput(db, runtime, agent, trigger, void 0, internalContext, current);
1337
+ return await messageInput(
1338
+ db,
1339
+ runtime,
1340
+ agent,
1341
+ trigger,
1342
+ void 0,
1343
+ internalContext,
1344
+ [],
1345
+ options.providerApi,
1346
+ options.projectModelHistory
1347
+ );
1380
1348
  }
1381
1349
  if (trigger.type === "user.approvalDecision") {
1382
1350
  const payload = trigger.payload;
@@ -1384,22 +1352,21 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
1384
1352
  if (!state) {
1385
1353
  throw new Error("No saved run state is available for approval decision");
1386
1354
  }
1355
+ const prepared = await runtime.prepareInput(agent, {
1356
+ kind: "approval",
1357
+ serializedRunState: resumeRunState(state),
1358
+ approvalId: String(payload.approvalId ?? ""),
1359
+ decision: payload.decision === "approve" ? "approve" : "reject",
1360
+ ...typeof payload.message === "string" ? { message: payload.message } : {}
1361
+ });
1387
1362
  return {
1388
- input: await runtime.prepareInput(agent, {
1389
- kind: "approval",
1390
- // Cross-account run-state strip (HOLE C): if the account resuming this
1391
- // frozen approval differs from the one that froze it, neutralize the
1392
- // blob's account-bound reasoning before replay (else byte-for-byte).
1393
- serializedRunState: resumeRunStateForCodexAccount(state, current),
1394
- approvalId: String(payload.approvalId ?? ""),
1395
- decision: payload.decision === "approve" ? "approve" : "reject",
1396
- ...typeof payload.message === "string" ? { message: payload.message } : {}
1397
- }),
1398
- // Model seeded from the run-state BLOB (neutralize-in-place), NOT stripped
1399
- // items: the reconcile watermark must NOT apply the cross-account strip
1400
- // (HOLE E) — else a cross-account approval resume re-appends K
1401
- // already-persisted items at fresh positions.
1402
- modelHistoryFromItems: false
1363
+ input: prepared,
1364
+ persistedHistoryCount: prepared.persistedHistoryCount,
1365
+ providerArtifactCandidates: {
1366
+ knownHistoryItemIds: [],
1367
+ historyItemIds: [],
1368
+ runStateId: state.id
1369
+ }
1403
1370
  };
1404
1371
  }
1405
1372
  if (trigger.type === "user.humanInputResponse") {
@@ -1413,13 +1380,19 @@ async function turnInput(db, runtime, agent, trigger, current = NON_CODEX_TURN,
1413
1380
  if (!resume) {
1414
1381
  throw new Error("Human-input response does not resolve to a durable request");
1415
1382
  }
1383
+ const prepared = await runtime.prepareInput(agent, {
1384
+ kind: "human_input",
1385
+ serializedRunState: resumeRunState(state),
1386
+ toolCallId: resume.toolCallId
1387
+ });
1416
1388
  return {
1417
- input: await runtime.prepareInput(agent, {
1418
- kind: "human_input",
1419
- serializedRunState: resumeRunStateForCodexAccount(state, current),
1420
- toolCallId: resume.toolCallId
1421
- }),
1422
- modelHistoryFromItems: false
1389
+ input: prepared,
1390
+ persistedHistoryCount: prepared.persistedHistoryCount,
1391
+ providerArtifactCandidates: {
1392
+ knownHistoryItemIds: [],
1393
+ historyItemIds: [],
1394
+ runStateId: state.id
1395
+ }
1423
1396
  };
1424
1397
  }
1425
1398
  throw new Error(`Unsupported trigger event type: ${trigger.type}`);
@@ -1428,24 +1401,31 @@ function joinInternalContext(...parts) {
1428
1401
  const content = parts.map((part) => part?.trim()).filter((part) => Boolean(part));
1429
1402
  return content.length > 0 ? content.join("\n\n") : void 0;
1430
1403
  }
1431
- async function messageInput(db, runtime, agent, trigger, text, internalContext, current = NON_CODEX_TURN, currentAttachmentRefs = [], projectModelHistory) {
1404
+ async function messageInput(db, runtime, agent, trigger, text, internalContext, currentAttachmentRefs = [], providerApi = "responses", projectModelHistory) {
1432
1405
  const stored = await getActiveSessionHistoryItems(db, trigger.workspaceId, trigger.sessionId);
1433
1406
  const envelope = await getSandboxSessionEnvelope(db, trigger.workspaceId, trigger.sessionId);
1434
- const referencedHistory = withCurrentUserAttachmentRefs(
1435
- applyCodexHistoryStrip(stored, current),
1436
- currentAttachmentRefs
1437
- );
1407
+ const canonicalView = projectRejectedProviderArtifacts(stored);
1408
+ const providerView = projectHistoryForProvider(canonicalView, providerApi);
1409
+ const referencedHistory = withCurrentUserAttachmentRefs(providerView, currentAttachmentRefs);
1438
1410
  const historyItems = projectModelHistory ? await projectModelHistory(referencedHistory) : referencedHistory;
1411
+ const prepared = await runtime.prepareInput(agent, {
1412
+ kind: "message",
1413
+ ...text ? { text } : {},
1414
+ ...internalContext ? { internalContext } : {},
1415
+ historyItems,
1416
+ sandboxEnvelope: envelope,
1417
+ ...projectModelHistory ? { modelInputAlreadyProjected: true } : {}
1418
+ });
1419
+ const preparedItems = Array.isArray(prepared.input) ? new Set(prepared.input) : /* @__PURE__ */ new Set();
1439
1420
  return {
1440
- input: await runtime.prepareInput(agent, {
1441
- kind: "message",
1442
- ...text ? { text } : {},
1443
- ...internalContext ? { internalContext } : {},
1444
- historyItems,
1445
- sandboxEnvelope: envelope,
1446
- ...projectModelHistory ? { modelInputAlreadyProjected: true } : {}
1447
- }),
1448
- modelHistoryFromItems: true
1421
+ input: prepared,
1422
+ persistedHistoryCount: prepared.persistedHistoryCount,
1423
+ providerArtifactCandidates: {
1424
+ knownHistoryItemIds: stored.map((row) => row.id),
1425
+ historyItemIds: stored.filter(
1426
+ (row) => row.providerArtifactInvalidatedAt === null && hasOpaqueProviderArtifact(row.item) && preparedItems.has(row.item)
1427
+ ).map((row) => row.id)
1428
+ }
1449
1429
  };
1450
1430
  }
1451
1431
  async function resolveUserMessageFileAttachments(db, workspaceId, resources) {
@@ -1510,10 +1490,7 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
1510
1490
  requestConsumed
1511
1491
  };
1512
1492
  }
1513
- const canonicalItems = applyCodexHistoryStrip(
1514
- active,
1515
- options.codexAccount ?? { currentCodexCredentialId: null }
1516
- );
1493
+ const canonicalItems = projectRejectedProviderArtifacts(active);
1517
1494
  const projectForWire = async (input) => sanitizeHistoryItemsForModel(
1518
1495
  options.projectModelInput ? await options.projectModelInput(input) : input,
1519
1496
  settings.modelToolOutputTruncationTokens
@@ -1639,9 +1616,7 @@ async function compactContextRemoteV2(db, settings, scope, canonicalItems, items
1639
1616
  expectedAttemptId: scope.attemptId,
1640
1617
  replacementItems: replacementHistory.slice(0, -1),
1641
1618
  summaryItem: tailItem,
1642
- replacementInputTokens: estimatedTokensAfter,
1643
1619
  ...options.clearRequestedCompaction ? { clearRequestedCompaction: true } : {},
1644
- ...options.producerCodexCredentialId ? { producerCodexCredentialId: options.producerCodexCredentialId } : {},
1645
1620
  eventPayload: {
1646
1621
  trigger: options.trigger ?? "auto",
1647
1622
  implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION,
@@ -1693,7 +1668,6 @@ async function compactContextPortable(db, settings, scope, canonicalItems, items
1693
1668
  expectedAttemptId: scope.attemptId,
1694
1669
  replacementItems: replacementHistory.slice(0, -1),
1695
1670
  summaryItem,
1696
- replacementInputTokens: estimatedTokensAfter,
1697
1671
  ...options.clearRequestedCompaction ? { clearRequestedCompaction: true } : {},
1698
1672
  eventPayload: {
1699
1673
  trigger: options.trigger ?? "auto",
@@ -4240,6 +4214,7 @@ import {
4240
4214
  RoutingBackendRecoveryRequiredError,
4241
4215
  RoutingSandboxSession,
4242
4216
  resolveModalCheckpointProviderBindingForSession as resolveModalCheckpointProviderBindingForSession2,
4217
+ sandboxBackendForSdkBackendId,
4243
4218
  verifySandboxExecReadiness as verifySandboxExecReadiness2
4244
4219
  } from "@opengeni/runtime";
4245
4220
  function relayConfigFromSettings(settings) {
@@ -4325,7 +4300,8 @@ async function resolveCurrentHomeBackend(services, ids, established) {
4325
4300
  };
4326
4301
  }
4327
4302
  const resumeState = lease.resumeState;
4328
- const resumeBackend = lease.resumeBackendId ?? lease.backend ?? ids.backend;
4303
+ const durableResumeBackend = lease.resumeBackendId ?? lease.backend ?? ids.backend;
4304
+ const resumeBackend = sandboxBackendForSdkBackendId(durableResumeBackend) ?? durableResumeBackend;
4329
4305
  if (!resumeState || resumeBackend !== ids.backend || providerIdentityFromResumeState(resumeState) !== lease.instanceId) {
4330
4306
  throw homeRouteRecoveryError(lease, lease.leaseEpoch);
4331
4307
  }
@@ -4366,7 +4342,8 @@ async function resolveCurrentHomeBackend(services, ids, established) {
4366
4342
  marked.status === "stale" ? "superseded" : homeRouteRecoveryDisposition(marked.lease)
4367
4343
  );
4368
4344
  }
4369
- if (rebound.instanceId !== lease.instanceId || rebound.backendId !== resumeBackend) {
4345
+ const reboundBackend = sandboxBackendForSdkBackendId(rebound.backendId) ?? rebound.backendId;
4346
+ if (rebound.instanceId !== lease.instanceId || reboundBackend !== resumeBackend) {
4370
4347
  throw homeRouteRecoveryError(lease, lease.leaseEpoch);
4371
4348
  }
4372
4349
  services.onHomeSandboxRebound?.({
@@ -6491,7 +6468,7 @@ function legacyTurnExecutionPolicyInput(turn) {
6491
6468
  }
6492
6469
  function providerRecoveryResult(input) {
6493
6470
  const providerDelay = input.retryAfterMs !== null && input.retryAfterMs !== void 0 && Number.isFinite(input.retryAfterMs) && input.retryAfterMs > 0 ? Math.ceil(input.retryAfterMs) : null;
6494
- const continueDelayMs = input.failureCode === "provider_rate_limited" ? providerDelay ?? PROVIDER_BACKPRESSURE_DELAY_MS : input.failureCode === "provider_unavailable" || input.failureCode === "upstream_connectivity_unavailable" ? Math.max(
6471
+ const continueDelayMs = input.failureCode === "provider_rate_limited" ? providerDelay ?? PROVIDER_BACKPRESSURE_DELAY_MS : input.failureCode === "provider_unavailable" || input.failureCode === "upstream_connectivity_unavailable" || input.failureCode === "mcp_transport_unavailable" ? Math.max(
6495
6472
  providerDelay ?? 0,
6496
6473
  PROVIDER_CONNECTIVITY_BACKOFF_MS[Math.min(
6497
6474
  Math.max(Math.trunc(input.attemptNumber) - 1, 0),
@@ -6816,22 +6793,18 @@ async function recordCompletedModelCallBeforeOwnershipFences(input) {
6816
6793
  }
6817
6794
  await input.recordAttemptSignals?.();
6818
6795
  }
6819
- function createModelResponseUsageEventState(claimedSourceKeys = /* @__PURE__ */ new Set()) {
6796
+ function createModelResponseEventState(claimedSourceKeys = /* @__PURE__ */ new Set()) {
6820
6797
  return {
6821
- responseUsageCount: 0,
6822
- providerContextRevision: 0,
6823
- lastProviderContextTokensObserved: null,
6798
+ responseCount: 0,
6799
+ contextSignal: null,
6824
6800
  claimedSourceKeys
6825
6801
  };
6826
6802
  }
6827
6803
  function createCompactionModelUsageEventState(claimedSourceKeys = /* @__PURE__ */ new Set()) {
6828
6804
  return { usageCount: 0, claimedSourceKeys };
6829
6805
  }
6830
- function modelResponseUsageContextSignal(state) {
6831
- return state.lastProviderContextTokensObserved === null ? null : {
6832
- revision: state.providerContextRevision,
6833
- totalTokens: state.lastProviderContextTokensObserved
6834
- };
6806
+ function modelResponseContextSignal(state) {
6807
+ return state.contextSignal;
6835
6808
  }
6836
6809
  function assertModelResponseLatencyMode(input) {
6837
6810
  if (input.requested === "standard") {
@@ -6850,14 +6823,14 @@ function assertModelResponseLatencyMode(input) {
6850
6823
  );
6851
6824
  }
6852
6825
  }
6853
- async function processModelResponseUsageEvent(input) {
6854
- const responseUsage = modelResponseUsageFromSdkEvent(input.event);
6855
- if (!responseUsage) {
6856
- return { status: "not_usage" };
6826
+ async function processModelResponseTerminalEvent(input) {
6827
+ const terminal = modelTerminalResponseFromSdkEvent(input.event);
6828
+ if (!terminal) {
6829
+ return { status: "not_response" };
6857
6830
  }
6858
- const responseOrdinal = input.state.responseUsageCount + 1;
6831
+ const responseOrdinal = input.state.responseCount + 1;
6859
6832
  const sourceKey = modelUsageSourceKey({
6860
- responseId: responseUsage.responseId,
6833
+ responseId: terminal.responseId,
6861
6834
  dispatchId: input.dispatchId,
6862
6835
  positionalKey: `response-${responseOrdinal}`
6863
6836
  });
@@ -6865,19 +6838,21 @@ async function processModelResponseUsageEvent(input) {
6865
6838
  return { status: "duplicate", sourceKey };
6866
6839
  }
6867
6840
  input.state.claimedSourceKeys.add(sourceKey);
6868
- input.state.responseUsageCount = responseOrdinal;
6869
- const normalizedUsage = normalizeModelCallUsage(responseUsage.usage);
6841
+ input.state.responseCount = responseOrdinal;
6842
+ const responseUsage = terminal.usage;
6843
+ const normalizedUsage = normalizeModelCallUsage(responseUsage?.usage);
6870
6844
  const accountContext = modelCallAccountContext({
6871
6845
  servingCredentialId: input.servingCredentialId,
6872
6846
  priorSessionCredentialId: input.priorSessionCredentialId,
6873
6847
  isFirstCallOfTurn: responseOrdinal === 1
6874
6848
  });
6875
- let authoritative = false;
6849
+ let authoritative = responseUsage === null;
6876
6850
  await recordCompletedModelCallBeforeOwnershipFences({
6877
6851
  renewLease: input.renewLease,
6878
6852
  leaseLost: input.leaseLost,
6879
6853
  leaseLostMessage: input.leaseLostMessage,
6880
6854
  recordUsage: async () => {
6855
+ if (!responseUsage) return;
6881
6856
  const billing = await recordModelUsageAndDebitCredits(input.settings, input.db, {
6882
6857
  accountId: input.accountId,
6883
6858
  workspaceId: input.workspaceId,
@@ -6935,17 +6910,19 @@ async function processModelResponseUsageEvent(input) {
6935
6910
  recordAttemptSignals: async () => {
6936
6911
  if (!authoritative) return;
6937
6912
  const observedTotal = normalizedUsage.totalTokens;
6938
- if (observedTotal !== null) {
6939
- input.state.lastProviderContextTokensObserved = observedTotal;
6940
- input.state.providerContextRevision += 1;
6941
- }
6913
+ input.state.contextSignal = observedTotal !== null && observedTotal > 0 ? { revision: responseOrdinal, totalTokens: observedTotal } : null;
6942
6914
  const observedInput = normalizedUsage.telemetry.inputTokens;
6943
- if (observedInput !== null && observedInput > 0) {
6944
- await input.setLastInputTokens(observedInput);
6945
- }
6915
+ await input.setLastInputTokens(
6916
+ observedInput !== null && observedInput > 0 ? observedInput : null
6917
+ );
6946
6918
  }
6947
6919
  });
6948
- return { status: "processed", sourceKey, authoritative };
6920
+ return {
6921
+ status: "processed",
6922
+ sourceKey,
6923
+ authoritative,
6924
+ usageReported: responseUsage !== null
6925
+ };
6949
6926
  }
6950
6927
  async function processCompactionModelUsageEvent(input) {
6951
6928
  const usageOrdinal = input.state.usageCount + 1;
@@ -7114,6 +7091,26 @@ function historyRowsToAppend(rawHistory, persistedHistoryCount, nextPosition = p
7114
7091
  nextPosition: nextPosition + rows.length
7115
7092
  };
7116
7093
  }
7094
+ function selectRejectedProviderArtifactHistoryIds(activeHistory, candidates, wireFingerprints) {
7095
+ const remainingWireArtifacts = /* @__PURE__ */ new Map();
7096
+ for (const fingerprint of wireFingerprints) {
7097
+ remainingWireArtifacts.set(fingerprint, (remainingWireArtifacts.get(fingerprint) ?? 0) + 1);
7098
+ }
7099
+ const knownHistoryItemIds = new Set(candidates.knownHistoryItemIds);
7100
+ const initialCandidateIds = new Set(candidates.historyItemIds);
7101
+ const selected = [];
7102
+ for (const row of activeHistory) {
7103
+ if (row.providerArtifactInvalidatedAt !== null) continue;
7104
+ if (knownHistoryItemIds.has(row.id) && !initialCandidateIds.has(row.id)) continue;
7105
+ const fingerprint = opaqueProviderArtifactFingerprint(row.item);
7106
+ if (!fingerprint) continue;
7107
+ const remaining = remainingWireArtifacts.get(fingerprint) ?? 0;
7108
+ if (remaining === 0) continue;
7109
+ selected.push(row.id);
7110
+ remainingWireArtifacts.set(fingerprint, remaining - 1);
7111
+ }
7112
+ return selected;
7113
+ }
7117
7114
  function secretRedactionModelInputFilter(redactValue) {
7118
7115
  return ({ modelData }) => ({
7119
7116
  ...modelData,
@@ -7132,11 +7129,6 @@ function isModelOrToolProgressHistoryItem(item) {
7132
7129
  }
7133
7130
  return false;
7134
7131
  }
7135
- function reconcileSeedCount(activeSeedRows, modelHistoryFromItems, current) {
7136
- return sanitizeHistoryItemsForModel2(
7137
- modelHistoryFromItems ? applyCodexHistoryStrip(activeSeedRows, current) : activeSeedRows.map((row) => row.item)
7138
- ).length;
7139
- }
7140
7132
  async function resolveActiveSandboxBackend(routingOn, loadPointer, loadSandboxKind) {
7141
7133
  if (!routingOn) {
7142
7134
  return void 0;
@@ -7442,9 +7434,6 @@ function createTurnSandboxProvisioner(establish, options = {}) {
7442
7434
  }
7443
7435
  };
7444
7436
  }
7445
- function sdkBackendIdForSandboxBackend(backend) {
7446
- return backend === "local" ? "unix_local" : backend;
7447
- }
7448
7437
  function acceptsPromptCacheKeyForTurn(resolvedModel) {
7449
7438
  if (!resolvedModel) {
7450
7439
  return true;
@@ -7980,6 +7969,10 @@ function createRunAgentTurnActivity(services) {
7980
7969
  }
7981
7970
  };
7982
7971
  let persistedHistoryCount = 0;
7972
+ let providerArtifactCandidates = {
7973
+ knownHistoryItemIds: [],
7974
+ historyItemIds: []
7975
+ };
7983
7976
  let nextHistoryPosition = 0;
7984
7977
  const reconcileConversationTruth = async (options = {}) => {
7985
7978
  if (!stream || !turnId) {
@@ -8007,11 +8000,6 @@ function createRunAgentTurnActivity(services) {
8007
8000
  turnId,
8008
8001
  expectedExecutionGeneration: executionGeneration,
8009
8002
  expectedAttemptId: input.attemptId,
8010
- // Tag each row with the codex account that produced it (null on the
8011
- // non-codex path). Resolved at line ~504 before any reconcile pass
8012
- // runs, so this is the turn's effective account. The read path uses
8013
- // it to strip cross-account reasoning.encrypted_content next turn.
8014
- producerCodexCredentialId: effectiveCodexCredentialId,
8015
8003
  modelToolOutputTruncationTokens: modelRunSettings.modelToolOutputTruncationTokens,
8016
8004
  items: rows
8017
8005
  });
@@ -8060,6 +8048,7 @@ function createRunAgentTurnActivity(services) {
8060
8048
  let effectiveCodexCredentialId = null;
8061
8049
  let priorSessionCodexCredentialId = null;
8062
8050
  let latestCodexUsage = null;
8051
+ let lastCodexRequestOpaqueArtifacts = [];
8063
8052
  let triggerType = null;
8064
8053
  try {
8065
8054
  const mcpSettings = await settingsWithEnabledCapabilityMcpServers(
@@ -8083,6 +8072,7 @@ function createRunAgentTurnActivity(services) {
8083
8072
  input.workspaceId,
8084
8073
  codexSettings
8085
8074
  );
8075
+ const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId) : null;
8086
8076
  runtime.configure(capabilitySettings);
8087
8077
  const session = await requireSession2(db, input.workspaceId, input.sessionId);
8088
8078
  const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
@@ -8143,8 +8133,6 @@ function createRunAgentTurnActivity(services) {
8143
8133
  trigger
8144
8134
  );
8145
8135
  triggerType = trigger.type;
8146
- const latestTurnState = await getLatestRunState2(db, input.workspaceId, input.sessionId);
8147
- const continuationCodexCredentialId = latestTurnState?.turnId === turnId ? latestTurnState.frozenCodexCredentialId : null;
8148
8136
  redispatchesAtDispatch = Number(
8149
8137
  turn.metadata?.workerDeathRedispatches ?? 0
8150
8138
  );
@@ -8327,7 +8315,6 @@ function createRunAgentTurnActivity(services) {
8327
8315
  sessionPinnedCredentialId: sessionPin,
8328
8316
  sessionPinSource,
8329
8317
  sessionLastCredentialId: sessionCodex?.lastCredentialId ?? null,
8330
- continuationCredentialId: continuationCodexCredentialId,
8331
8318
  now: /* @__PURE__ */ new Date()
8332
8319
  });
8333
8320
  let leased;
@@ -8341,8 +8328,7 @@ function createRunAgentTurnActivity(services) {
8341
8328
  workspaceId: input.workspaceId,
8342
8329
  turnId,
8343
8330
  holderId: codexLeaseHolderId,
8344
- advanceActivePointer: sessionPin === null,
8345
- continuationCredentialId: continuationCodexCredentialId
8331
+ advanceActivePointer: sessionPin === null
8346
8332
  },
8347
8333
  selectForTurn
8348
8334
  );
@@ -8396,8 +8382,7 @@ function createRunAgentTurnActivity(services) {
8396
8382
  workspaceId: input.workspaceId,
8397
8383
  turnId,
8398
8384
  holderId: codexLeaseHolderId,
8399
- advanceActivePointer: sessionPin === null,
8400
- continuationCredentialId: continuationCodexCredentialId
8385
+ advanceActivePointer: sessionPin === null
8401
8386
  },
8402
8387
  selectForTurn
8403
8388
  );
@@ -8511,7 +8496,7 @@ function createRunAgentTurnActivity(services) {
8511
8496
  if (codexLeaseHeld) startCodexLeaseHeartbeat();
8512
8497
  const actualOutcome = effectiveCodexCredentialId ? "selected" : rotationDecision.kind === "allCapped" ? "waiting" : "none";
8513
8498
  const actualReason = effectiveCodexCredentialId ? leased.reused ? "lease_reused" : sessionPin === effectiveCodexCredentialId ? "pin" : rotationDecision.kind === "active" && rotationDecision.moved ? "rotation" : "active" : rotationDecision.kind === "allCapped" ? "all_capped" : "none";
8514
- const fencedInFlight = leased.reused || continuationCodexCredentialId !== null && continuationCodexCredentialId === effectiveCodexCredentialId;
8499
+ const fencedInFlight = leased.reused;
8515
8500
  const shadowResult = await publishCodexFleetShadowDecisionV1({
8516
8501
  enabled: settings.codexFleetPolicyShadowEnabled,
8517
8502
  decision: {
@@ -8792,15 +8777,13 @@ function createRunAgentTurnActivity(services) {
8792
8777
  );
8793
8778
  if (priorAccountId !== effectiveCodexCredentialId) {
8794
8779
  const rotated = rotationDecision.kind === "active" && rotationDecision.moved;
8795
- const droppedConnectors = rotationDecision.kind === "active" ? rotationDecision.droppedConnectors : void 0;
8796
8780
  await publish([
8797
8781
  {
8798
8782
  type: "codex.account.switched",
8799
8783
  payload: {
8800
8784
  fromAccountId: priorAccountId,
8801
8785
  toAccountId: effectiveCodexCredentialId,
8802
- reason: rotated ? "rotation" : "manual",
8803
- ...droppedConnectors && droppedConnectors.length > 0 ? { droppedConnectors } : {}
8786
+ reason: rotated ? "rotation" : "manual"
8804
8787
  }
8805
8788
  }
8806
8789
  ]);
@@ -8907,6 +8890,7 @@ function createRunAgentTurnActivity(services) {
8907
8890
  turnExecutionPolicy.productModelId
8908
8891
  );
8909
8892
  const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
8893
+ const providerApi = resolvedModel?.provider.api ?? "responses";
8910
8894
  const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
8911
8895
  const attachmentProjector = createModelHistoryAttachmentProjector(
8912
8896
  db,
@@ -8915,6 +8899,7 @@ function createRunAgentTurnActivity(services) {
8915
8899
  objectStorage ? (file) => objectStorage.getFileBytes(file) : void 0
8916
8900
  );
8917
8901
  const modelHistoryProjector = async (items) => projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
8902
+ const compactionModelHistoryProjector = async (items) => await modelHistoryProjector(projectHistoryForProvider2(items, providerApi));
8918
8903
  const selectedProvider = resolvedModel?.provider;
8919
8904
  const publicProviderHeaders = new Set(
8920
8905
  selectedProvider?.publicDefaultHeaderNames?.map((name) => name.toLowerCase()) ?? []
@@ -8998,6 +8983,9 @@ function createRunAgentTurnActivity(services) {
8998
8983
  latestCodexUsage = snapshot;
8999
8984
  },
9000
8985
  // latest wins; flushed once in finally
8986
+ onRequestOpaqueArtifacts: ({ fingerprints }) => {
8987
+ lastCodexRequestOpaqueArtifacts = fingerprints;
8988
+ },
9001
8989
  nextRequestId: () => `${dispatchId}:${++codexModelRequestSequence}`,
9002
8990
  onModelRequestEvent: async (event) => {
9003
8991
  if (!publish || !turnId) {
@@ -9135,8 +9123,7 @@ function createRunAgentTurnActivity(services) {
9135
9123
  codexCompactionMode: session.codexCompactionMode,
9136
9124
  isCodexSubscriptionTurn: isCodexTurn,
9137
9125
  publishLiveEvents: publishCompactionLiveEvents,
9138
- ...remoteCompactionRequester ? { requestRemoteCompactionV2: remoteCompactionRequester } : {},
9139
- ...effectiveCodexCredentialId ? { producerCodexCredentialId: effectiveCodexCredentialId } : {}
9126
+ ...remoteCompactionRequester ? { requestRemoteCompactionV2: remoteCompactionRequester } : {}
9140
9127
  };
9141
9128
  const compactionOnlyTurn = turn.source === "compaction";
9142
9129
  const remoteV2CompactionNeedsAgentPrefix = Boolean(remoteCompactionRequester) && session.codexCompactionMode === "remote_v2";
@@ -9187,10 +9174,7 @@ function createRunAgentTurnActivity(services) {
9187
9174
  force: true,
9188
9175
  clearRequestedCompaction: true,
9189
9176
  trigger: "operator",
9190
- codexAccount: {
9191
- currentCodexCredentialId: effectiveCodexCredentialId
9192
- },
9193
- projectModelInput: modelHistoryProjector,
9177
+ projectModelInput: compactionModelHistoryProjector,
9194
9178
  ...compactionModeOptions
9195
9179
  }
9196
9180
  ),
@@ -9965,34 +9949,55 @@ function createRunAgentTurnActivity(services) {
9965
9949
  return result;
9966
9950
  };
9967
9951
  const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
9952
+ const codexAppsAuth = codexAppsCredentialId ? (() => {
9953
+ const resolver = buildCodexTokenResolver(
9954
+ db,
9955
+ runSettings,
9956
+ input.workspaceId,
9957
+ codexAppsCredentialId
9958
+ );
9959
+ return {
9960
+ clientVersion: CODEX_CLIENT_VERSION,
9961
+ withAuthorization: async (use) => {
9962
+ const snapshot = await resolver.getToken();
9963
+ registerSecretRedactions([
9964
+ { name: "CODEX_APPS_ACCESS_TOKEN", value: snapshot.accessToken }
9965
+ ]);
9966
+ return await withCodexAppsRequestAuthorization(
9967
+ db,
9968
+ { workspaceId: input.workspaceId, credentialId: codexAppsCredentialId },
9969
+ async () => await use(snapshot)
9970
+ );
9971
+ }
9972
+ };
9973
+ })() : void 0;
9968
9974
  preparedTools = await waitForTurnOperation(
9969
- withCodex(
9970
- () => runtime.prepareTools(runSettings, turnTools, {
9971
- accountId: input.accountId,
9972
- workspaceId: input.workspaceId,
9973
- sessionId: input.sessionId,
9974
- // Sign the calling turn into the first-party token so tools classify
9975
- // the caller by its own identity (sacred-pause guard), not the racy
9976
- // live active pointer.
9977
- ...turnId ? { turnId } : {},
9978
- attemptId: input.attemptId,
9979
- executionGeneration,
9980
- subjectId: "worker:first-party-mcp",
9981
- subjectLabel: "OpenGeni worker",
9982
- ...credentialSubjectId ? { credentialSubjectId } : {},
9983
- resolveCredential,
9984
- onAuthNeeded: async (payload) => {
9985
- if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
9986
- return;
9987
- }
9988
- await publish([{ type: "tool.auth_needed", payload }], true);
9989
- },
9990
- // Manager-style sessions carry a creation-validated permission set
9991
- // for their first-party MCP token; null keeps the fixed default.
9992
- ...session.firstPartyMcpPermissions?.length ? { firstPartyPermissions: session.firstPartyMcpPermissions } : {},
9993
- firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS]
9994
- })
9995
- ),
9975
+ runtime.prepareTools(runSettings, turnTools, {
9976
+ accountId: input.accountId,
9977
+ workspaceId: input.workspaceId,
9978
+ sessionId: input.sessionId,
9979
+ // Sign the calling turn into the first-party token so tools classify
9980
+ // the caller by its own identity (sacred-pause guard), not the racy
9981
+ // live active pointer.
9982
+ ...turnId ? { turnId } : {},
9983
+ attemptId: input.attemptId,
9984
+ executionGeneration,
9985
+ subjectId: "worker:first-party-mcp",
9986
+ subjectLabel: "OpenGeni worker",
9987
+ ...credentialSubjectId ? { credentialSubjectId } : {},
9988
+ ...codexAppsAuth ? { codexAppsAuth } : {},
9989
+ resolveCredential,
9990
+ onAuthNeeded: async (payload) => {
9991
+ if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
9992
+ return;
9993
+ }
9994
+ await publish([{ type: "tool.auth_needed", payload }], true);
9995
+ },
9996
+ // Manager-style sessions carry a creation-validated permission set
9997
+ // for their first-party MCP token; null keeps the fixed default.
9998
+ ...session.firstPartyMcpPermissions?.length ? { firstPartyPermissions: session.firstPartyMcpPermissions } : {},
9999
+ firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS]
10000
+ }),
9996
10001
  cancellationSignal,
9997
10002
  async (latePreparedTools) => await latePreparedTools.close().catch(() => void 0)
9998
10003
  );
@@ -10386,10 +10391,7 @@ function createRunAgentTurnActivity(services) {
10386
10391
  force: true,
10387
10392
  clearRequestedCompaction: true,
10388
10393
  trigger: "operator",
10389
- codexAccount: {
10390
- currentCodexCredentialId: effectiveCodexCredentialId
10391
- },
10392
- projectModelInput: modelHistoryProjector,
10394
+ projectModelInput: compactionModelHistoryProjector,
10393
10395
  ...compactionModeOptions
10394
10396
  }
10395
10397
  ),
@@ -10500,17 +10502,11 @@ function createRunAgentTurnActivity(services) {
10500
10502
  force: true,
10501
10503
  clearRequestedCompaction: true,
10502
10504
  trigger: "operator",
10503
- codexAccount: {
10504
- currentCodexCredentialId: effectiveCodexCredentialId
10505
- },
10506
- projectModelInput: modelHistoryProjector,
10505
+ projectModelInput: compactionModelHistoryProjector,
10507
10506
  ...compactionModeOptions
10508
10507
  } : {
10509
10508
  trigger: "auto",
10510
- codexAccount: {
10511
- currentCodexCredentialId: effectiveCodexCredentialId
10512
- },
10513
- projectModelInput: modelHistoryProjector,
10509
+ projectModelInput: compactionModelHistoryProjector,
10514
10510
  ...compactionModeOptions
10515
10511
  }
10516
10512
  ),
@@ -10638,29 +10634,17 @@ function createRunAgentTurnActivity(services) {
10638
10634
  }
10639
10635
  let runInput = null;
10640
10636
  const prepareRunAttemptInput = async () => {
10641
- const prepared = await turnInput(
10642
- db,
10643
- runtime,
10644
- agent,
10645
- trigger,
10646
- { currentCodexCredentialId: effectiveCodexCredentialId },
10647
- {
10648
- turnId: activeTurnId,
10649
- recovering: turn.executionGeneration > 1,
10650
- ...unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {},
10651
- ...runCredentialsNote ? { runCredentialsNote } : {},
10652
- projectModelHistory: modelHistoryProjector
10653
- }
10654
- );
10655
- runInput = prepared.input;
10656
- const activeSeedRows = await getActiveSessionHistoryItems3(
10657
- db,
10658
- input.workspaceId,
10659
- input.sessionId
10660
- );
10661
- persistedHistoryCount = reconcileSeedCount(activeSeedRows, prepared.modelHistoryFromItems, {
10662
- currentCodexCredentialId: effectiveCodexCredentialId
10637
+ const prepared = await turnInput(db, runtime, agent, trigger, {
10638
+ turnId: activeTurnId,
10639
+ recovering: turn.executionGeneration > 1,
10640
+ ...unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {},
10641
+ ...runCredentialsNote ? { runCredentialsNote } : {},
10642
+ providerApi,
10643
+ projectModelHistory: modelHistoryProjector
10663
10644
  });
10645
+ runInput = prepared.input;
10646
+ providerArtifactCandidates = prepared.providerArtifactCandidates;
10647
+ persistedHistoryCount = prepared.persistedHistoryCount;
10664
10648
  nextHistoryPosition = await nextSessionHistoryPosition(
10665
10649
  db,
10666
10650
  input.workspaceId,
@@ -10681,20 +10665,17 @@ function createRunAgentTurnActivity(services) {
10681
10665
  attemptId: input.attemptId
10682
10666
  },
10683
10667
  // Never reuse the persisted prior-turn signal for recovery. Proactive
10684
- // guards provide their exact current signal; provider overflows do not,
10685
- // so null derives decision metadata from active history. Forced
10686
- // recovery proves progress separately by comparing the replacement
10687
- // with the current active-history estimate in maybeCompactContext.
10668
+ // guards provide their exact current provider-based signal; provider
10669
+ // overflows do not, so their diagnostic signal stays unknown/zero.
10670
+ // Forced recovery proves progress separately by comparing the
10671
+ // replacement with the current active-history estimate.
10688
10672
  recoverySignalTokens,
10689
10673
  compactSummarizer,
10690
10674
  {
10691
10675
  force: true,
10692
10676
  ...triggerLabel === "operator" ? { clearRequestedCompaction: true } : {},
10693
10677
  trigger: triggerLabel,
10694
- codexAccount: {
10695
- currentCodexCredentialId: effectiveCodexCredentialId
10696
- },
10697
- projectModelInput: modelHistoryProjector,
10678
+ projectModelInput: compactionModelHistoryProjector,
10698
10679
  ...compactionModeOptions
10699
10680
  }
10700
10681
  ),
@@ -10709,9 +10690,7 @@ function createRunAgentTurnActivity(services) {
10709
10690
  }
10710
10691
  return outcome;
10711
10692
  };
10712
- const modelResponseUsageState = createModelResponseUsageEventState(
10713
- claimedModelUsageSourceKeys
10714
- );
10693
+ const modelResponseState = createModelResponseEventState(claimedModelUsageSourceKeys);
10715
10694
  const runStreamAttempt = async () => {
10716
10695
  if (!runInput) {
10717
10696
  throw new Error("Run input was not prepared");
@@ -10720,6 +10699,7 @@ function createRunAgentTurnActivity(services) {
10720
10699
  batcher = null;
10721
10700
  let currentToolBatchCallIds = /* @__PURE__ */ new Set();
10722
10701
  let currentToolBatchCompletedCallIds = /* @__PURE__ */ new Set();
10702
+ let streamSawPerResponseUsage = false;
10723
10703
  throwIfWorkerShuttingDown();
10724
10704
  throwIfTurnCancelled();
10725
10705
  const ownedEstablished = resolvedSandbox?.established ?? lazyOwnedSandbox;
@@ -10803,7 +10783,7 @@ function createRunAgentTurnActivity(services) {
10803
10783
  }
10804
10784
  } : {}
10805
10785
  } : {},
10806
- contextCompactionSignal: () => modelResponseUsageContextSignal(modelResponseUsageState),
10786
+ contextCompactionSignal: () => modelResponseContextSignal(modelResponseState),
10807
10787
  contextCompactionRequested: () => isSessionCompactionRequested(db, input.workspaceId, input.sessionId),
10808
10788
  callModelInputFilter: secretRedactionModelInputFilter((value) => redact(value)),
10809
10789
  ...toolCancellationFenceRef.current ? { turnToolCancellationFence: toolCancellationFenceRef.current } : {}
@@ -10836,9 +10816,9 @@ function createRunAgentTurnActivity(services) {
10836
10816
  }
10837
10817
  let stableToolCallIdsToClear = null;
10838
10818
  let completedCurrentToolBatch = false;
10839
- const responseUsageResult = await processModelResponseUsageEvent({
10819
+ const responseResult = await processModelResponseTerminalEvent({
10840
10820
  event: next.value,
10841
- state: modelResponseUsageState,
10821
+ state: modelResponseState,
10842
10822
  dispatchId: modelUsageDispatchId,
10843
10823
  settings,
10844
10824
  db,
@@ -10869,7 +10849,8 @@ function createRunAgentTurnActivity(services) {
10869
10849
  model: turn.model,
10870
10850
  ...resolvedModel?.provider.id ? { providerId: resolvedModel.provider.id } : {}
10871
10851
  });
10872
- if (responseUsageResult.status === "processed") {
10852
+ if (responseResult.status === "processed") {
10853
+ streamSawPerResponseUsage ||= responseResult.usageReported;
10873
10854
  currentToolBatchCallIds = /* @__PURE__ */ new Set();
10874
10855
  currentToolBatchCompletedCallIds = /* @__PURE__ */ new Set();
10875
10856
  await reconcileConversationTruth();
@@ -10982,7 +10963,7 @@ function createRunAgentTurnActivity(services) {
10982
10963
  stream.completed.catch(() => void 0),
10983
10964
  cancellationSignal
10984
10965
  );
10985
- if (modelResponseUsageState.responseUsageCount === 0) {
10966
+ if (!streamSawPerResponseUsage) {
10986
10967
  const aggregateUsage = stream.state.usage;
10987
10968
  const normalizedAggregateUsage = normalizeModelCallUsage(aggregateUsage);
10988
10969
  const aggregateInput = normalizedAggregateUsage.telemetry.inputTokens;
@@ -11059,13 +11040,8 @@ function createRunAgentTurnActivity(services) {
11059
11040
  },
11060
11041
  recordAttemptSignals: async () => {
11061
11042
  if (!aggregateAuthoritative) return;
11062
- if (normalizedAggregateUsage.totalTokens !== null) {
11063
- modelResponseUsageState.lastProviderContextTokensObserved = normalizedAggregateUsage.totalTokens;
11064
- modelResponseUsageState.providerContextRevision += 1;
11065
- }
11066
- if (aggregateInput !== null && aggregateInput > 0) {
11067
- await setLastInputTokensFenced(aggregateInput);
11068
- }
11043
+ modelResponseState.contextSignal = null;
11044
+ await setLastInputTokensFenced(null);
11069
11045
  }
11070
11046
  });
11071
11047
  }
@@ -11132,7 +11108,6 @@ function createRunAgentTurnActivity(services) {
11132
11108
  runState: {
11133
11109
  serializedRunState: stream.state.toString(),
11134
11110
  pendingApprovals: approvals,
11135
- frozenCodexCredentialId: effectiveCodexCredentialId,
11136
11111
  humanInputRequests: humanInputRequests.map(
11137
11112
  ({ isNew: _isNew, ...request }) => request
11138
11113
  )
@@ -11642,8 +11617,7 @@ function createRunAgentTurnActivity(services) {
11642
11617
  activeCredentialId: rotation.activeCredentialId,
11643
11618
  priorCredentialId: effectiveCodexCredentialId,
11644
11619
  accounts,
11645
- now: /* @__PURE__ */ new Date(),
11646
- usedConnectors: serving?.connectorNamespaces ?? []
11620
+ now: /* @__PURE__ */ new Date()
11647
11621
  }) : { kind: "none" };
11648
11622
  const candidateAvailable = statePersisted && Boolean(rotation?.rotationEnabled && rotation?.leaseRotationEnabled) && decision.kind === "active" && decision.credentialId !== effectiveCodexCredentialId;
11649
11623
  if (candidateAvailable && codexLeaseHolderId && codexLeaseGeneration !== null) {
@@ -11812,10 +11786,7 @@ function createRunAgentTurnActivity(services) {
11812
11786
  activeCredentialId: rotation.activeCredentialId,
11813
11787
  priorCredentialId: effectiveCodexCredentialId,
11814
11788
  accounts: fresh,
11815
- now: /* @__PURE__ */ new Date(),
11816
- // P4: the just-capped serving account's connector set is the proxy for
11817
- // "what this session has access to" — prefer a covering failover target.
11818
- usedConnectors: serving?.connectorNamespaces ?? []
11789
+ now: /* @__PURE__ */ new Date()
11819
11790
  });
11820
11791
  if (decision.kind === "active") {
11821
11792
  rotated = true;
@@ -11979,6 +11950,17 @@ function createRunAgentTurnActivity(services) {
11979
11950
  if (encryptedArtifactRejection && effectiveCodexCredentialId && publish && turnId && turnStartedPublished) {
11980
11951
  await flushRuntimeBatcher();
11981
11952
  await reconcileConversationTruth({ requireDurable: true });
11953
+ const activeHistory = await getActiveSessionHistoryItems3(
11954
+ db,
11955
+ input.workspaceId,
11956
+ input.sessionId
11957
+ );
11958
+ const rejectedHistoryItemIds = selectRejectedProviderArtifactHistoryIds(
11959
+ activeHistory,
11960
+ providerArtifactCandidates,
11961
+ lastCodexRequestOpaqueArtifacts
11962
+ );
11963
+ const rejectedRunStateId = providerArtifactCandidates.runStateId && lastCodexRequestOpaqueArtifacts.length > 0 ? providerArtifactCandidates.runStateId : void 0;
11982
11964
  const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
11983
11965
  sessionId: input.sessionId,
11984
11966
  turnId,
@@ -11990,7 +11972,8 @@ function createRunAgentTurnActivity(services) {
11990
11972
  retryable: true
11991
11973
  },
11992
11974
  providerArtifactInvalidation: {
11993
- codexCredentialId: effectiveCodexCredentialId,
11975
+ historyItemIds: rejectedHistoryItemIds,
11976
+ ...rejectedRunStateId ? { runStateId: rejectedRunStateId } : {},
11994
11977
  reason: encryptedArtifactRejection.kind
11995
11978
  }
11996
11979
  });
@@ -12281,15 +12264,6 @@ function createRunAgentTurnActivity(services) {
12281
12264
  );
12282
12265
  }
12283
12266
  }
12284
- const connectorNamespaces = preparedTools?.codexConnectorNamespaces;
12285
- if (connectorNamespaces && connectorNamespaces.size > 0) {
12286
- await waitForTurnFinalizerStep(
12287
- recordCodexAccountConnectors(db, input.workspaceId, effectiveCodexCredentialId, [
12288
- ...connectorNamespaces
12289
- ]).catch(() => void 0),
12290
- finalizerSignal
12291
- );
12292
- }
12293
12267
  }
12294
12268
  if (codexLeaseHeartbeatTimer) {
12295
12269
  clearInterval(codexLeaseHeartbeatTimer);
@@ -12606,6 +12580,13 @@ function agentRunFailurePayload(error, options = {}) {
12606
12580
  ...mcpTimeout.detail || mcpTimeout.message ? { detail: mcpTimeout.detail ?? mcpTimeout.message } : {}
12607
12581
  };
12608
12582
  }
12583
+ if (isMcpTransportConnectivityError(error)) {
12584
+ return {
12585
+ error: "A required MCP server was temporarily unreachable. The same turn will retry after a short delay.",
12586
+ code: "mcp_transport_unavailable",
12587
+ retryable: true
12588
+ };
12589
+ }
12609
12590
  if (status === 429 || code === "rate_limit_exceeded" || /(?:too many requests|rate.?limit|\b429\b)/i.test(message)) {
12610
12591
  return {
12611
12592
  error: "Model provider rate limit hit. Try again in a minute or lower the reasoning effort.",
@@ -13121,21 +13102,69 @@ function startOfUtcMonth3(date = /* @__PURE__ */ new Date()) {
13121
13102
  }
13122
13103
 
13123
13104
  // src/activities/file-upload-reaper.ts
13124
- import { claimExpiredFileUploadCleanup, completeExpiredFileUploadCleanup } from "@opengeni/db";
13105
+ import {
13106
+ claimDueTranscriptionRecordingObjectCleanup,
13107
+ claimExpiredFileUploadCleanup,
13108
+ completeDueTranscriptionRecordingObjectCleanup,
13109
+ completeExpiredFileUploadCleanup,
13110
+ purgeExpiredTranscriptionRecordings
13111
+ } from "@opengeni/db";
13125
13112
  var FILE_UPLOAD_CLEANUP_GRACE_MS = 60 * 60 * 1e3;
13126
13113
  var FILE_UPLOAD_CLEANUP_CLAIM_TIMEOUT_MS = 10 * 60 * 1e3;
13127
13114
  var FILE_UPLOAD_CLEANUP_BATCH_SIZE = 100;
13115
+ var SAFE_CLEANUP_ERROR_CATEGORIES = /* @__PURE__ */ new Set([
13116
+ "aborted",
13117
+ "conflict",
13118
+ "invalid_response",
13119
+ "network",
13120
+ "not_found",
13121
+ "permission_denied",
13122
+ "provider",
13123
+ "timeout",
13124
+ "unavailable"
13125
+ ]);
13126
+ function cleanupErrorAttributes(error) {
13127
+ let errorCategory = "provider_error";
13128
+ let errorStatus;
13129
+ if (error && typeof error === "object") {
13130
+ const rawCode = error.code;
13131
+ if (typeof rawCode === "string" && SAFE_CLEANUP_ERROR_CATEGORIES.has(rawCode)) {
13132
+ errorCategory = rawCode;
13133
+ } else if (rawCode !== void 0) {
13134
+ errorCategory = "unknown";
13135
+ }
13136
+ const rawStatus = Number(
13137
+ error.status ?? error.statusCode
13138
+ );
13139
+ if (Number.isInteger(rawStatus) && rawStatus >= 100 && rawStatus <= 599) {
13140
+ errorStatus = rawStatus;
13141
+ }
13142
+ }
13143
+ return { errorCategory, ...errorStatus === void 0 ? {} : { errorStatus } };
13144
+ }
13128
13145
  function createFileUploadReaperActivities(services, options = {}) {
13129
13146
  const graceMs = options.graceMs ?? FILE_UPLOAD_CLEANUP_GRACE_MS;
13130
13147
  const claimTimeoutMs = options.claimTimeoutMs ?? FILE_UPLOAD_CLEANUP_CLAIM_TIMEOUT_MS;
13131
13148
  const batchSize = options.batchSize ?? FILE_UPLOAD_CLEANUP_BATCH_SIZE;
13132
13149
  const deleteObject = options.deleteObject ?? (async (storage, key) => storage.deleteObject(key));
13150
+ const claimFileUploads = options.claimFileUploads ?? claimExpiredFileUploadCleanup;
13151
+ const completeFileUpload = options.completeFileUpload ?? completeExpiredFileUploadCleanup;
13152
+ const claimTranscriptionObjects = options.claimTranscriptionObjects ?? claimDueTranscriptionRecordingObjectCleanup;
13153
+ const completeTranscriptionObject = options.completeTranscriptionObject ?? completeDueTranscriptionRecordingObjectCleanup;
13154
+ const purgeTranscriptionRecordings = options.purgeTranscriptionRecordings ?? purgeExpiredTranscriptionRecordings;
13133
13155
  async function reapExpiredFileUploads2() {
13134
13156
  const { db, objectStorage, observability } = await services();
13135
13157
  if (!objectStorage) {
13158
+ try {
13159
+ await purgeTranscriptionRecordings(db, { graceMs, limit: batchSize });
13160
+ } catch (error) {
13161
+ observability.warn("expired transcription recording metadata purge failed", {
13162
+ ...cleanupErrorAttributes(error)
13163
+ });
13164
+ }
13136
13165
  return { claimed: 0, deleted: 0, failed: 0 };
13137
13166
  }
13138
- const claims = await claimExpiredFileUploadCleanup(db, {
13167
+ const claims = await claimFileUploads(db, {
13139
13168
  graceMs,
13140
13169
  claimTimeoutMs,
13141
13170
  limit: batchSize
@@ -13145,7 +13174,7 @@ function createFileUploadReaperActivities(services, options = {}) {
13145
13174
  for (const claim of claims) {
13146
13175
  try {
13147
13176
  await deleteObject(objectStorage, claim.objectKey);
13148
- const settled = await completeExpiredFileUploadCleanup(db, claim);
13177
+ const settled = await completeFileUpload(db, claim);
13149
13178
  if (!settled) {
13150
13179
  throw new Error("cleanup claim no longer owns a reclaimable upload");
13151
13180
  }
@@ -13156,18 +13185,58 @@ function createFileUploadReaperActivities(services, options = {}) {
13156
13185
  workspaceId: claim.workspaceId,
13157
13186
  uploadId: claim.uploadId,
13158
13187
  fileId: claim.fileId,
13159
- error: error instanceof Error ? error.message : String(error)
13188
+ ...cleanupErrorAttributes(error)
13160
13189
  });
13161
13190
  }
13162
13191
  }
13163
- if (claims.length > 0) {
13192
+ const recordingClaims = await claimTranscriptionObjects(db, {
13193
+ graceMs,
13194
+ claimTimeoutMs,
13195
+ limit: batchSize
13196
+ });
13197
+ for (const claim of recordingClaims) {
13198
+ try {
13199
+ await deleteObject(objectStorage, claim.objectKey);
13200
+ const settled = await completeTranscriptionObject(db, claim);
13201
+ if (!settled) {
13202
+ throw new Error("cleanup claim no longer owns a transcription recording object");
13203
+ }
13204
+ deleted += 1;
13205
+ } catch (error) {
13206
+ failed += 1;
13207
+ observability.warn(
13208
+ "expired transcription recording object cleanup failed; claim remains reclaimable",
13209
+ {
13210
+ workspaceId: claim.workspaceId,
13211
+ recordingId: claim.recordingId,
13212
+ ...cleanupErrorAttributes(error)
13213
+ }
13214
+ );
13215
+ }
13216
+ }
13217
+ let transcriptionRecordingsPurged = 0;
13218
+ try {
13219
+ transcriptionRecordingsPurged = await purgeTranscriptionRecordings(db, {
13220
+ graceMs,
13221
+ limit: batchSize
13222
+ });
13223
+ } catch (error) {
13224
+ observability.warn("expired transcription recording metadata purge failed", {
13225
+ ...cleanupErrorAttributes(error)
13226
+ });
13227
+ }
13228
+ const claimed = claims.length + recordingClaims.length;
13229
+ if (claimed > 0 || transcriptionRecordingsPurged > 0) {
13164
13230
  observability.info("expired file upload cleanup swept", {
13165
- claimed: claims.length,
13231
+ claimed,
13166
13232
  deleted,
13167
- failed
13233
+ failed,
13234
+ fileUploads: claims.length,
13235
+ transcriptionRecordingObjects: recordingClaims.length,
13236
+ transcriptionRecordingsPurged
13168
13237
  });
13169
13238
  }
13170
- return { claimed: claims.length, deleted, failed };
13239
+ return { claimed, deleted, failed };
13171
13240
  }
13172
13241
  return { reapExpiredFileUploads: reapExpiredFileUploads2 };
13173
13242
  }
@@ -13228,6 +13297,7 @@ import {
13228
13297
  resolveModalCheckpointProviderBindingForLiveSandbox,
13229
13298
  resolveModalCheckpointProviderBinding,
13230
13299
  resolveModalCheckpointProviderBindingForSession as resolveModalCheckpointProviderBindingForSession3,
13300
+ sandboxBackendForSdkBackendId as sandboxBackendForSdkBackendId2,
13231
13301
  sandboxCommandExitCode,
13232
13302
  sandboxCommandStillRunning,
13233
13303
  sweepModalOrphanSandboxes,
@@ -14145,7 +14215,8 @@ async function probeDrainableProviderReadiness(settings, lease) {
14145
14215
  if (!lease.instanceId || !lease.resumeState) {
14146
14216
  throw new Error("Expired workspace capture has no resumable provider identity");
14147
14217
  }
14148
- const backend = lease.resumeBackendId ?? lease.backend;
14218
+ const durableBackendId = lease.resumeBackendId ?? lease.backend;
14219
+ const backend = sandboxBackendForSdkBackendId2(durableBackendId) ?? durableBackendId;
14149
14220
  let established;
14150
14221
  try {
14151
14222
  established = await establishSandboxSessionFromEnvelope3(settings, lease.resumeState, {
@@ -14375,7 +14446,8 @@ async function terminateDrainableBox(db, settings, row, observability, terminate
14375
14446
  }
14376
14447
  }
14377
14448
  async function terminateProviderBox(settings, lease, observability, persistArchive, createClientForBackend = createSandboxClientForBackend) {
14378
- const backend = lease.resumeBackendId ?? lease.backend;
14449
+ const durableBackendId = lease.resumeBackendId ?? lease.backend;
14450
+ const backend = sandboxBackendForSdkBackendId2(durableBackendId) ?? durableBackendId;
14379
14451
  if (!backend || backend === "none") {
14380
14452
  return { terminated: true, providerMissingBeforeCapture: false };
14381
14453
  }