@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.
@@ -15,7 +15,6 @@ import {
15
15
  getSessionEvent,
16
16
  getSessionRootId,
17
17
  getSessionGoal,
18
- getLatestRunState,
19
18
  getHumanInputResumeForEvent,
20
19
  getSessionHumanInputRequest,
21
20
  installOrReadTurnExecutionPolicyForAttempt,
@@ -34,7 +33,6 @@ import {
34
33
  recordSessionActiveCodexCredential,
35
34
  setSessionCodexPin,
36
35
  recordCodexAccountUsageWithWakeTargets,
37
- recordCodexAccountConnectors,
38
36
  quarantineCodexCredentialForLease,
39
37
  setActiveCodexCredential,
40
38
  resolveWorkspaceMemoryBlock,
@@ -76,6 +74,7 @@ import {
76
74
  beginConnectorActionExecution,
77
75
  completeConnectorActionExecution,
78
76
  prepareConnectorActionApproval,
77
+ withCodexAppsRequestAuthorization,
79
78
  type AppendEventInput,
80
79
  type ActiveSandboxPointer,
81
80
  type SandboxRecord,
@@ -90,9 +89,10 @@ import {
90
89
  sandboxStateEntryFromRunState,
91
90
  maxTurnsExceededRunState,
92
91
  modelResponseServiceTierFromSdkEvent,
93
- modelResponseUsageFromSdkEvent,
92
+ modelTerminalResponseFromSdkEvent,
94
93
  normalizeModelCallUsage,
95
94
  normalizeSdkEvent,
95
+ projectHistoryForProvider,
96
96
  sanitizeHistoryItemsForModel,
97
97
  projectModelInputForCapabilities,
98
98
  appendPersistentSessionSettings,
@@ -122,9 +122,11 @@ import {
122
122
  sandboxFileDownloadFailureNote,
123
123
  SUMMARY_BUFFER_TOKENS,
124
124
  isMcpRequestTimeoutError,
125
+ isMcpTransportConnectivityError,
125
126
  runOwnedSandboxSetup,
126
127
  RoutingMutationOutcomeUnknownError,
127
128
  WorkspaceArchiveIntegrityError,
129
+ sdkBackendIdForSandboxBackend,
128
130
  swapTargetEstablishability,
129
131
  type SandboxFileDownload,
130
132
  type SandboxFileDownloadFailure,
@@ -210,6 +212,7 @@ import {
210
212
  classifyCodexUsageLimitError,
211
213
  codexRequestStorage,
212
214
  isCodexTransportError,
215
+ opaqueProviderArtifactFingerprint,
213
216
  withCodexRequestOverrides,
214
217
  selectCodexCredentialId,
215
218
  type CodexRequestContext,
@@ -220,6 +223,7 @@ import {
220
223
  assertSessionAllowsProductModel,
221
224
  defaultSessionMcpServerIds,
222
225
  directPersonalConnectionSubjectId,
226
+ resolveCodexAppsCredentialIdForRun,
223
227
  withFrozenPersonalConnectionDelegations,
224
228
  resolveSessionToolPolicy,
225
229
  } from "@opengeni/core";
@@ -273,11 +277,9 @@ import {
273
277
  type SecretForRedaction,
274
278
  } from "./redaction";
275
279
  import {
276
- applyCodexHistoryStrip,
277
280
  createModelHistoryAttachmentProjector,
278
281
  turnInput,
279
282
  type ModelAttachmentInputPolicy,
280
- type TurnCodexAccount,
281
283
  } from "./run-input";
282
284
  import {
283
285
  createRuntimeBatcher,
@@ -463,7 +465,8 @@ export function providerRecoveryResult(input: {
463
465
  input.failureCode === "provider_rate_limited"
464
466
  ? (providerDelay ?? PROVIDER_BACKPRESSURE_DELAY_MS)
465
467
  : input.failureCode === "provider_unavailable" ||
466
- input.failureCode === "upstream_connectivity_unavailable"
468
+ input.failureCode === "upstream_connectivity_unavailable" ||
469
+ input.failureCode === "mcp_transport_unavailable"
467
470
  ? Math.max(
468
471
  providerDelay ?? 0,
469
472
  PROVIDER_CONNECTIVITY_BACKOFF_MS[
@@ -1121,10 +1124,9 @@ type TurnEventPublisher = (
1121
1124
  immediate?: boolean,
1122
1125
  ) => Promise<{ events: SessionEvent[]; accepted: boolean }>;
1123
1126
 
1124
- export type ModelResponseUsageEventState = {
1125
- responseUsageCount: number;
1126
- providerContextRevision: number;
1127
- lastProviderContextTokensObserved: number | null;
1127
+ export type ModelResponseEventState = {
1128
+ responseCount: number;
1129
+ contextSignal: { revision: number; totalTokens: number } | null;
1128
1130
  claimedSourceKeys: Set<string>;
1129
1131
  };
1130
1132
 
@@ -1133,13 +1135,12 @@ export type CompactionModelUsageEventState = {
1133
1135
  claimedSourceKeys: Set<string>;
1134
1136
  };
1135
1137
 
1136
- export function createModelResponseUsageEventState(
1138
+ export function createModelResponseEventState(
1137
1139
  claimedSourceKeys: Set<string> = new Set<string>(),
1138
- ): ModelResponseUsageEventState {
1140
+ ): ModelResponseEventState {
1139
1141
  return {
1140
- responseUsageCount: 0,
1141
- providerContextRevision: 0,
1142
- lastProviderContextTokensObserved: null,
1142
+ responseCount: 0,
1143
+ contextSignal: null,
1143
1144
  claimedSourceKeys,
1144
1145
  };
1145
1146
  }
@@ -1150,15 +1151,10 @@ export function createCompactionModelUsageEventState(
1150
1151
  return { usageCount: 0, claimedSourceKeys };
1151
1152
  }
1152
1153
 
1153
- export function modelResponseUsageContextSignal(
1154
- state: ModelResponseUsageEventState,
1154
+ export function modelResponseContextSignal(
1155
+ state: ModelResponseEventState,
1155
1156
  ): { revision: number; totalTokens: number } | null {
1156
- return state.lastProviderContextTokensObserved === null
1157
- ? null
1158
- : {
1159
- revision: state.providerContextRevision,
1160
- totalTokens: state.lastProviderContextTokensObserved,
1161
- };
1157
+ return state.contextSignal;
1162
1158
  }
1163
1159
 
1164
1160
  export function assertModelResponseLatencyMode(input: {
@@ -1193,19 +1189,20 @@ export function assertModelResponseLatencyMode(input: {
1193
1189
  }
1194
1190
 
1195
1191
  /**
1196
- * Process one SDK stream event through the exact production usage path.
1192
+ * Process one SDK terminal-response event through the production authority path.
1197
1193
  *
1198
1194
  * The pinned Responses SDK mirrors one provider terminal response as both a
1199
1195
  * normalized `response_done` and a raw `model/response.completed` event. Claim
1200
- * the stable response/source key before lease renewal or any usage side effect,
1201
- * and advance the positional ordinal only for a newly claimed response. The
1202
- * durable `agent.model.usage` source-key fence remains the cross-restart
1203
- * authority: a replay may retry the idempotent billing write, but it cannot
1204
- * advance process-local metrics, provider context, or attempt-owned signals.
1196
+ * the stable response/source key before lease renewal or any side effect, and
1197
+ * use that one positional ordinal for both response identity and same-run
1198
+ * context binding. A response without usage still clears attempt-owned token
1199
+ * state. When usage exists, the durable `agent.model.usage` source-key fence
1200
+ * remains the cross-restart authority: a replay may retry the idempotent billing
1201
+ * write, but it cannot advance metrics, context, or attempt-owned signals.
1205
1202
  */
1206
- export async function processModelResponseUsageEvent(input: {
1207
- event: Parameters<typeof modelResponseUsageFromSdkEvent>[0];
1208
- state: ModelResponseUsageEventState;
1203
+ export async function processModelResponseTerminalEvent(input: {
1204
+ event: Parameters<typeof modelTerminalResponseFromSdkEvent>[0];
1205
+ state: ModelResponseEventState;
1209
1206
  dispatchId: string | null;
1210
1207
  settings: Settings;
1211
1208
  db: ActivityServices["db"];
@@ -1228,20 +1225,25 @@ export async function processModelResponseUsageEvent(input: {
1228
1225
  renewLease: () => Promise<void>;
1229
1226
  leaseLost: () => boolean;
1230
1227
  leaseLostMessage: string;
1231
- setLastInputTokens: (tokens: number) => Promise<void>;
1228
+ setLastInputTokens: (tokens: number | null) => Promise<void>;
1232
1229
  }): Promise<
1233
- | { status: "not_usage" }
1230
+ | { status: "not_response" }
1234
1231
  | { status: "duplicate"; sourceKey: string }
1235
- | { status: "processed"; sourceKey: string; authoritative: boolean }
1232
+ | {
1233
+ status: "processed";
1234
+ sourceKey: string;
1235
+ authoritative: boolean;
1236
+ usageReported: boolean;
1237
+ }
1236
1238
  > {
1237
- const responseUsage = modelResponseUsageFromSdkEvent(input.event);
1238
- if (!responseUsage) {
1239
- return { status: "not_usage" };
1239
+ const terminal = modelTerminalResponseFromSdkEvent(input.event);
1240
+ if (!terminal) {
1241
+ return { status: "not_response" };
1240
1242
  }
1241
1243
 
1242
- const responseOrdinal = input.state.responseUsageCount + 1;
1244
+ const responseOrdinal = input.state.responseCount + 1;
1243
1245
  const sourceKey = modelUsageSourceKey({
1244
- responseId: responseUsage.responseId,
1246
+ responseId: terminal.responseId,
1245
1247
  dispatchId: input.dispatchId,
1246
1248
  positionalKey: `response-${responseOrdinal}`,
1247
1249
  });
@@ -1249,20 +1251,25 @@ export async function processModelResponseUsageEvent(input: {
1249
1251
  return { status: "duplicate", sourceKey };
1250
1252
  }
1251
1253
  input.state.claimedSourceKeys.add(sourceKey);
1252
- input.state.responseUsageCount = responseOrdinal;
1254
+ input.state.responseCount = responseOrdinal;
1255
+
1256
+ const responseUsage = terminal.usage;
1253
1257
 
1254
- const normalizedUsage = normalizeModelCallUsage(responseUsage.usage);
1258
+ const normalizedUsage = normalizeModelCallUsage(responseUsage?.usage);
1255
1259
  const accountContext = modelCallAccountContext({
1256
1260
  servingCredentialId: input.servingCredentialId,
1257
1261
  priorSessionCredentialId: input.priorSessionCredentialId,
1258
1262
  isFirstCallOfTurn: responseOrdinal === 1,
1259
1263
  });
1260
- let authoritative = false;
1264
+ // A terminal response without usage still authoritatively replaces the
1265
+ // attempt-owned context signal. There is simply no billing event to claim.
1266
+ let authoritative = responseUsage === null;
1261
1267
  await recordCompletedModelCallBeforeOwnershipFences({
1262
1268
  renewLease: input.renewLease,
1263
1269
  leaseLost: input.leaseLost,
1264
1270
  leaseLostMessage: input.leaseLostMessage,
1265
1271
  recordUsage: async () => {
1272
+ if (!responseUsage) return;
1266
1273
  const billing = await recordModelUsageAndDebitCredits(input.settings, input.db, {
1267
1274
  accountId: input.accountId,
1268
1275
  workspaceId: input.workspaceId,
@@ -1320,17 +1327,22 @@ export async function processModelResponseUsageEvent(input: {
1320
1327
  recordAttemptSignals: async () => {
1321
1328
  if (!authoritative) return;
1322
1329
  const observedTotal = normalizedUsage.totalTokens;
1323
- if (observedTotal !== null) {
1324
- input.state.lastProviderContextTokensObserved = observedTotal;
1325
- input.state.providerContextRevision += 1;
1326
- }
1330
+ input.state.contextSignal =
1331
+ observedTotal !== null && observedTotal > 0
1332
+ ? { revision: responseOrdinal, totalTokens: observedTotal }
1333
+ : null;
1327
1334
  const observedInput = normalizedUsage.telemetry.inputTokens;
1328
- if (observedInput !== null && observedInput > 0) {
1329
- await input.setLastInputTokens(observedInput);
1330
- }
1335
+ await input.setLastInputTokens(
1336
+ observedInput !== null && observedInput > 0 ? observedInput : null,
1337
+ );
1331
1338
  },
1332
1339
  });
1333
- return { status: "processed", sourceKey, authoritative };
1340
+ return {
1341
+ status: "processed",
1342
+ sourceKey,
1343
+ authoritative,
1344
+ usageReported: responseUsage !== null,
1345
+ };
1334
1346
  }
1335
1347
 
1336
1348
  /**
@@ -1611,6 +1623,42 @@ export function historyRowsToAppend(
1611
1623
  };
1612
1624
  }
1613
1625
 
1626
+ /** Select only opaque rows that participated in the provider's rejected wire request. */
1627
+ export function selectRejectedProviderArtifactHistoryIds(
1628
+ activeHistory: ReadonlyArray<{
1629
+ id: string;
1630
+ item: Record<string, unknown>;
1631
+ providerArtifactInvalidatedAt: Date | null;
1632
+ }>,
1633
+ candidates: {
1634
+ knownHistoryItemIds: readonly string[];
1635
+ historyItemIds: readonly string[];
1636
+ },
1637
+ wireFingerprints: readonly string[],
1638
+ ): string[] {
1639
+ const remainingWireArtifacts = new Map<string, number>();
1640
+ for (const fingerprint of wireFingerprints) {
1641
+ remainingWireArtifacts.set(fingerprint, (remainingWireArtifacts.get(fingerprint) ?? 0) + 1);
1642
+ }
1643
+ const knownHistoryItemIds = new Set(candidates.knownHistoryItemIds);
1644
+ const initialCandidateIds = new Set(candidates.historyItemIds);
1645
+ const selected: string[] = [];
1646
+ for (const row of activeHistory) {
1647
+ if (row.providerArtifactInvalidatedAt !== null) continue;
1648
+ // A row present before this attempt is eligible only when the exact
1649
+ // model-facing projection retained it. Rows appended during this run are
1650
+ // eligible after durable reconciliation.
1651
+ if (knownHistoryItemIds.has(row.id) && !initialCandidateIds.has(row.id)) continue;
1652
+ const fingerprint = opaqueProviderArtifactFingerprint(row.item);
1653
+ if (!fingerprint) continue;
1654
+ const remaining = remainingWireArtifacts.get(fingerprint) ?? 0;
1655
+ if (remaining === 0) continue;
1656
+ selected.push(row.id);
1657
+ remainingWireArtifacts.set(fingerprint, remaining - 1);
1658
+ }
1659
+ return selected;
1660
+ }
1661
+
1614
1662
  /** Literal final per-call boundary for replayed and current-turn model input. */
1615
1663
  export function secretRedactionModelInputFilter(
1616
1664
  redactValue: (value: unknown) => unknown,
@@ -1634,49 +1682,6 @@ function isModelOrToolProgressHistoryItem(item: Record<string, unknown>): boolea
1634
1682
  return false;
1635
1683
  }
1636
1684
 
1637
- /**
1638
- * Seed the turn-end reconcile watermark (`persistedHistoryCount`) from EXACTLY the
1639
- * view `state.history` was seeded from, so the model-input length and the watermark
1640
- * can NEVER disagree. The watermark is the slice index the reconcile cuts the
1641
- * (re-sanitized) `state.history` at to find this turn's genuinely-new items, so it
1642
- * must equal the length of `state.history`'s already-persisted leading prefix.
1643
- *
1644
- * The cross-account reasoning strip drops foreign reasoning items, so the prefix
1645
- * length is PATH-DEPENDENT, captured by `modelHistoryFromItems`:
1646
- *
1647
- * - items read path (`modelHistoryFromItems === true`) — `state.history` was seeded
1648
- * from the cross-account-STRIPPED active items (foreign reasoning DROPPED), so it
1649
- * starts K shorter than the raw active-row count. Seed from the SAME strip
1650
- * (HOLE D); seeding from the un-stripped count would slice K genuinely-new items
1651
- * off the reconcile and silently lose them (incl. the user's switch-turn message).
1652
- *
1653
- * - approval RunState path (`modelHistoryFromItems === false`) — `state.history` was seeded
1654
- * from the blob, where foreign reasoning is NEUTRALIZED-IN-PLACE (the item is
1655
- * KEPT, only its id/encrypted_content go — see resumeRunStateForCodexAccount), so
1656
- * the blob's history length still COUNTS those items. Applying the strip here
1657
- * under-counts by K and the reconcile re-appends K already-persisted items at
1658
- * fresh positions — HOLE E. So the blob path must NOT strip: count the raw
1659
- * sanitized active length, which mirrors the blob's completed prefix.
1660
- *
1661
- * On a same-account / non-codex turn the strip is a no-op, so both branches reduce
1662
- * to the same raw sanitized count (byte-identical to the pre-strip behaviour).
1663
- * Pure; exported for unit testing the D/E seed invariant.
1664
- */
1665
- export function reconcileSeedCount(
1666
- activeSeedRows: ReadonlyArray<{
1667
- item: Record<string, unknown>;
1668
- producerCodexCredentialId: string | null;
1669
- }>,
1670
- modelHistoryFromItems: boolean,
1671
- current: TurnCodexAccount,
1672
- ): number {
1673
- return sanitizeHistoryItemsForModel(
1674
- modelHistoryFromItems
1675
- ? applyCodexHistoryStrip(activeSeedRows, current)
1676
- : activeSeedRows.map((row) => row.item),
1677
- ).length;
1678
- }
1679
-
1680
1685
  /**
1681
1686
  * Resolve the EFFECTIVE/active compute backend a turn should gate
1682
1687
  * filesystem-touching agent lifecycle hooks on (today: the repository clone).
@@ -2277,10 +2282,6 @@ export function createTurnSandboxProvisioner<T>(
2277
2282
  };
2278
2283
  }
2279
2284
 
2280
- function sdkBackendIdForSandboxBackend(backend: Settings["sandboxBackend"]): string {
2281
- return backend === "local" ? "unix_local" : backend;
2282
- }
2283
-
2284
2285
  /**
2285
2286
  * Decide whether THIS turn may send OpenAI's `prompt_cache_key` request field.
2286
2287
  *
@@ -2459,7 +2460,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
2459
2460
  // Still required by credential-loss/capacity settlements, whose own
2460
2461
  // recovery transactions fence against worker-death redispatches.
2461
2462
  let redispatchesAtDispatch = 0;
2462
- const setLastInputTokensFenced = async (lastInputTokens: number): Promise<void> => {
2463
+ const setLastInputTokensFenced = async (lastInputTokens: number | null): Promise<void> => {
2463
2464
  if (!turnId || executionGeneration <= 0) {
2464
2465
  throw new Error("Turn attempt was not initialized before token accounting");
2465
2466
  }
@@ -3103,6 +3104,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3103
3104
  // stable and this scalar append watermark is valid again. The sanitizer
3104
3105
  // remains the final call/result pairing guard for every other reconcile.
3105
3106
  let persistedHistoryCount = 0;
3107
+ let providerArtifactCandidates: Awaited<
3108
+ ReturnType<typeof turnInput>
3109
+ >["providerArtifactCandidates"] = {
3110
+ knownHistoryItemIds: [],
3111
+ historyItemIds: [],
3112
+ };
3106
3113
  // Next free WHOLE-NUMBER absolute position to append at. Tracked separately
3107
3114
  // from persistedHistoryCount (the in-memory slice index) because a compaction
3108
3115
  // inserts a fractional summary position, so total rows no longer equal
@@ -3137,11 +3144,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3137
3144
  turnId,
3138
3145
  expectedExecutionGeneration: executionGeneration,
3139
3146
  expectedAttemptId: input.attemptId,
3140
- // Tag each row with the codex account that produced it (null on the
3141
- // non-codex path). Resolved at line ~504 before any reconcile pass
3142
- // runs, so this is the turn's effective account. The read path uses
3143
- // it to strip cross-account reasoning.encrypted_content next turn.
3144
- producerCodexCredentialId: effectiveCodexCredentialId,
3145
3147
  modelToolOutputTruncationTokens: modelRunSettings.modelToolOutputTruncationTokens,
3146
3148
  items: rows,
3147
3149
  });
@@ -3202,7 +3204,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3202
3204
  // a per-call usage log can report whether the serving account CHANGED since the
3203
3205
  // session's previous call (the prompt-cache account-switch hypothesis).
3204
3206
  let priorSessionCodexCredentialId: string | null = null;
3205
- // Multi-account P4 (Part A): the latest usage-header snapshot scraped FOR FREE
3207
+ // The latest usage-header snapshot scraped for free
3206
3208
  // off this turn's `/codex/responses` responses (a turn issues many model calls;
3207
3209
  // latest wins). Flushed ONCE into the P2 usage cache for the serving account in
3208
3210
  // the `finally` — cheaper than a /wham/usage poll AND it self-heals P3 rotation
@@ -3210,6 +3212,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3210
3212
  // Hoisted to activity scope so the finally flush (below) sees it. The sink is
3211
3213
  // wired into codexContext.onUsageHeaders inside the try.
3212
3214
  let latestCodexUsage: CodexUsageHeaderSnapshot | null = null;
3215
+ let lastCodexRequestOpaqueArtifacts: readonly string[] = [];
3213
3216
  // Hoisted for same-turn recovery: an approval-decision rerun must
3214
3217
  // re-enter through the approval resume path (its frozen mid-flight state
3215
3218
  // only exists in the RunState blob), never through a swapped trigger.
@@ -3239,6 +3242,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3239
3242
  input.workspaceId,
3240
3243
  codexSettings,
3241
3244
  );
3245
+ const codexAppsCredentialId = capabilitySettings.codexConnectedAppsEnabled
3246
+ ? await resolveCodexAppsCredentialIdForRun(db, input.workspaceId)
3247
+ : null;
3242
3248
  runtime.configure(capabilitySettings);
3243
3249
  const session = await requireSession(db, input.workspaceId, input.sessionId);
3244
3250
  const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
@@ -3302,9 +3308,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3302
3308
  trigger,
3303
3309
  );
3304
3310
  triggerType = trigger.type;
3305
- const latestTurnState = await getLatestRunState(db, input.workspaceId, input.sessionId);
3306
- const continuationCodexCredentialId =
3307
- latestTurnState?.turnId === turnId ? latestTurnState.frozenCodexCredentialId : null;
3308
3311
  redispatchesAtDispatch = Number(
3309
3312
  (turn.metadata as { workerDeathRedispatches?: number } | null)?.workerDeathRedispatches ??
3310
3313
  0,
@@ -3548,7 +3551,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3548
3551
  sessionPinnedCredentialId: sessionPin,
3549
3552
  sessionPinSource,
3550
3553
  sessionLastCredentialId: sessionCodex?.lastCredentialId ?? null,
3551
- continuationCredentialId: continuationCodexCredentialId,
3552
3554
  now: new Date(),
3553
3555
  });
3554
3556
 
@@ -3567,7 +3569,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3567
3569
  turnId,
3568
3570
  holderId: codexLeaseHolderId,
3569
3571
  advanceActivePointer: sessionPin === null,
3570
- continuationCredentialId: continuationCodexCredentialId,
3571
3572
  },
3572
3573
  selectForTurn,
3573
3574
  );
@@ -3623,7 +3624,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3623
3624
  turnId,
3624
3625
  holderId: codexLeaseHolderId,
3625
3626
  advanceActivePointer: sessionPin === null,
3626
- continuationCredentialId: continuationCodexCredentialId,
3627
3627
  },
3628
3628
  selectForTurn,
3629
3629
  );
@@ -3773,10 +3773,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
3773
3773
  : rotationDecision.kind === "allCapped"
3774
3774
  ? "all_capped"
3775
3775
  : "none";
3776
- const fencedInFlight =
3777
- leased.reused ||
3778
- (continuationCodexCredentialId !== null &&
3779
- continuationCodexCredentialId === effectiveCodexCredentialId);
3776
+ const fencedInFlight = leased.reused;
3780
3777
  const shadowResult = await publishCodexFleetShadowDecisionV1({
3781
3778
  enabled: settings.codexFleetPolicyShadowEnabled,
3782
3779
  decision: {
@@ -4090,11 +4087,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4090
4087
  );
4091
4088
  if (priorAccountId !== effectiveCodexCredentialId) {
4092
4089
  const rotated = rotationDecision.kind === "active" && rotationDecision.moved;
4093
- // P4: surface the dropped-connector note when this rotation pick couldn't
4094
- // cover the session's used connectors (a Tier-2/unknown failover); the pill
4095
- // renders the badge. Omitted when the switch covered everything (the norm).
4096
- const droppedConnectors =
4097
- rotationDecision.kind === "active" ? rotationDecision.droppedConnectors : undefined;
4098
4090
  await publish([
4099
4091
  {
4100
4092
  type: "codex.account.switched",
@@ -4102,9 +4094,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4102
4094
  fromAccountId: priorAccountId,
4103
4095
  toAccountId: effectiveCodexCredentialId,
4104
4096
  reason: rotated ? "rotation" : "manual",
4105
- ...(droppedConnectors && droppedConnectors.length > 0
4106
- ? { droppedConnectors }
4107
- : {}),
4108
4097
  },
4109
4098
  },
4110
4099
  ]);
@@ -4262,6 +4251,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4262
4251
  turnExecutionPolicy.productModelId,
4263
4252
  );
4264
4253
  const supportsImageInput = modelSupportsImageInputForTurn(resolvedModel);
4254
+ const providerApi = resolvedModel?.provider.api ?? "responses";
4265
4255
  const modelInputPolicy = modelAttachmentInputPolicyForTurn(resolvedModel);
4266
4256
  const attachmentProjector = createModelHistoryAttachmentProjector(
4267
4257
  db,
@@ -4271,6 +4261,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4271
4261
  );
4272
4262
  const modelHistoryProjector = async (items: Array<Record<string, unknown>>) =>
4273
4263
  projectModelInputForCapabilities(await attachmentProjector(items), modelInputPolicy);
4264
+ const compactionModelHistoryProjector = async (items: Array<Record<string, unknown>>) =>
4265
+ await modelHistoryProjector(projectHistoryForProvider(items, providerApi));
4274
4266
  const selectedProvider = resolvedModel?.provider;
4275
4267
  const publicProviderHeaders = new Set(
4276
4268
  selectedProvider?.publicDefaultHeaderNames?.map((name) => name.toLowerCase()) ?? [],
@@ -4395,6 +4387,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4395
4387
  onUsageHeaders: (snapshot) => {
4396
4388
  latestCodexUsage = snapshot;
4397
4389
  }, // latest wins; flushed once in finally
4390
+ onRequestOpaqueArtifacts: ({ fingerprints }) => {
4391
+ lastCodexRequestOpaqueArtifacts = fingerprints;
4392
+ },
4398
4393
  nextRequestId: () => `${dispatchId}:${++codexModelRequestSequence}`,
4399
4394
  onModelRequestEvent: async (event) => {
4400
4395
  if (!publish || !turnId) {
@@ -4564,9 +4559,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4564
4559
  ...(remoteCompactionRequester
4565
4560
  ? { requestRemoteCompactionV2: remoteCompactionRequester }
4566
4561
  : {}),
4567
- ...(effectiveCodexCredentialId
4568
- ? { producerCodexCredentialId: effectiveCodexCredentialId }
4569
- : {}),
4570
4562
  } as const;
4571
4563
 
4572
4564
  // Operator /compact:
@@ -4630,10 +4622,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
4630
4622
  force: true,
4631
4623
  clearRequestedCompaction: true,
4632
4624
  trigger: "operator",
4633
- codexAccount: {
4634
- currentCodexCredentialId: effectiveCodexCredentialId,
4635
- },
4636
- projectModelInput: modelHistoryProjector,
4625
+ projectModelInput: compactionModelHistoryProjector,
4637
4626
  ...compactionModeOptions,
4638
4627
  },
4639
4628
  ),
@@ -5608,10 +5597,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5608
5597
  connectionCredentials?.mcpCredentials && hostCredentialRootSessionId
5609
5598
  ? hostCredentialRootSessionId
5610
5599
  : input.sessionId;
5611
- // Wrap MCP prep in the codex ALS so the codex_apps connect handshake
5612
- // (initialize + tools/list) can resolve the per-workspace bearer from
5613
- // codexRequestStorage (runtime/codexAppsMcpRequestInit). withCodex is the
5614
- // identity on every non-codex turn, so this is a no-op for existing paths.
5600
+ // Connection credentials and the optional Apps credential are resolved
5601
+ // independently. Inference auth is never an Apps fallback.
5615
5602
  const rawResolveCredential = connectionTokenResolverForTurn({
5616
5603
  db,
5617
5604
  settings: runSettings,
@@ -5648,36 +5635,64 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
5648
5635
  return result;
5649
5636
  };
5650
5637
  const credentialSubjectId = credentialSubjectIdForTurnInitiator(turn);
5638
+ const codexAppsAuth = codexAppsCredentialId
5639
+ ? (() => {
5640
+ const resolver = buildCodexTokenResolver(
5641
+ db,
5642
+ runSettings,
5643
+ input.workspaceId,
5644
+ codexAppsCredentialId,
5645
+ );
5646
+ return {
5647
+ clientVersion: CODEX_CLIENT_VERSION,
5648
+ withAuthorization: async <T>(
5649
+ use: (token: {
5650
+ accessToken: string;
5651
+ chatgptAccountId: string | null;
5652
+ }) => Promise<T>,
5653
+ ): Promise<T> => {
5654
+ const snapshot = await resolver.getToken();
5655
+ registerSecretRedactions([
5656
+ { name: "CODEX_APPS_ACCESS_TOKEN", value: snapshot.accessToken },
5657
+ ]);
5658
+ return await withCodexAppsRequestAuthorization(
5659
+ db,
5660
+ { workspaceId: input.workspaceId, credentialId: codexAppsCredentialId },
5661
+ async () => await use(snapshot),
5662
+ );
5663
+ },
5664
+ };
5665
+ })()
5666
+ : undefined;
5651
5667
  preparedTools = await waitForTurnOperation(
5652
- withCodex(() =>
5653
- runtime.prepareTools(runSettings, turnTools, {
5654
- accountId: input.accountId,
5655
- workspaceId: input.workspaceId,
5656
- sessionId: input.sessionId,
5657
- // Sign the calling turn into the first-party token so tools classify
5658
- // the caller by its own identity (sacred-pause guard), not the racy
5659
- // live active pointer.
5660
- ...(turnId ? { turnId } : {}),
5661
- attemptId: input.attemptId,
5662
- executionGeneration,
5663
- subjectId: "worker:first-party-mcp",
5664
- subjectLabel: "OpenGeni worker",
5665
- ...(credentialSubjectId ? { credentialSubjectId } : {}),
5666
- resolveCredential,
5667
- onAuthNeeded: async (payload) => {
5668
- if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
5669
- return;
5670
- }
5671
- await publish!([{ type: "tool.auth_needed", payload }], true);
5672
- },
5673
- // Manager-style sessions carry a creation-validated permission set
5674
- // for their first-party MCP token; null keeps the fixed default.
5675
- ...(session.firstPartyMcpPermissions?.length
5676
- ? { firstPartyPermissions: session.firstPartyMcpPermissions }
5677
- : {}),
5678
- firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
5679
- }),
5680
- ),
5668
+ runtime.prepareTools(runSettings, turnTools, {
5669
+ accountId: input.accountId,
5670
+ workspaceId: input.workspaceId,
5671
+ sessionId: input.sessionId,
5672
+ // Sign the calling turn into the first-party token so tools classify
5673
+ // the caller by its own identity (sacred-pause guard), not the racy
5674
+ // live active pointer.
5675
+ ...(turnId ? { turnId } : {}),
5676
+ attemptId: input.attemptId,
5677
+ executionGeneration,
5678
+ subjectId: "worker:first-party-mcp",
5679
+ subjectLabel: "OpenGeni worker",
5680
+ ...(credentialSubjectId ? { credentialSubjectId } : {}),
5681
+ ...(codexAppsAuth ? { codexAppsAuth } : {}),
5682
+ resolveCredential,
5683
+ onAuthNeeded: async (payload) => {
5684
+ if (!shouldPublishToolAuthNeededForTurn(payload, trigger, turn)) {
5685
+ return;
5686
+ }
5687
+ await publish!([{ type: "tool.auth_needed", payload }], true);
5688
+ },
5689
+ // Manager-style sessions carry a creation-validated permission set
5690
+ // for their first-party MCP token; null keeps the fixed default.
5691
+ ...(session.firstPartyMcpPermissions?.length
5692
+ ? { firstPartyPermissions: session.firstPartyMcpPermissions }
5693
+ : {}),
5694
+ firstPartyTools: session.firstPartyMcpTools ?? [...DEFAULT_FIRST_PARTY_MCP_TOOLS],
5695
+ }),
5681
5696
  cancellationSignal,
5682
5697
  async (latePreparedTools) => await latePreparedTools.close().catch(() => undefined),
5683
5698
  );
@@ -6161,10 +6176,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6161
6176
  force: true,
6162
6177
  clearRequestedCompaction: true,
6163
6178
  trigger: "operator",
6164
- codexAccount: {
6165
- currentCodexCredentialId: effectiveCodexCredentialId,
6166
- },
6167
- projectModelInput: modelHistoryProjector,
6179
+ projectModelInput: compactionModelHistoryProjector,
6168
6180
  ...compactionModeOptions,
6169
6181
  },
6170
6182
  ),
@@ -6296,18 +6308,12 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6296
6308
  force: true,
6297
6309
  clearRequestedCompaction: true,
6298
6310
  trigger: "operator",
6299
- codexAccount: {
6300
- currentCodexCredentialId: effectiveCodexCredentialId,
6301
- },
6302
- projectModelInput: modelHistoryProjector,
6311
+ projectModelInput: compactionModelHistoryProjector,
6303
6312
  ...compactionModeOptions,
6304
6313
  }
6305
6314
  : {
6306
6315
  trigger: "auto",
6307
- codexAccount: {
6308
- currentCodexCredentialId: effectiveCodexCredentialId,
6309
- },
6310
- projectModelInput: modelHistoryProjector,
6316
+ projectModelInput: compactionModelHistoryProjector,
6311
6317
  ...compactionModeOptions,
6312
6318
  },
6313
6319
  ),
@@ -6444,35 +6450,25 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6444
6450
  const unavailableSandboxFilesNote = sandboxFileDownloadFailureNote(
6445
6451
  fileMaterializationFailures,
6446
6452
  );
6447
- // Cross-account reasoning strip: pass THIS turn's codex account so every
6448
- // history read path (items + run-state replay) drops reasoning produced by
6449
- // a DIFFERENT codex account. effectiveCodexCredentialId is the resolved
6450
- // codex credential on a codex turn (pin > workspace-active) and null on a
6451
- // non-codex turn OR a codex turn with no usable account — exactly the
6452
- // "current account" the single strip rule compares against (null is the
6453
- // built-in/Azure account, so a non-codex turn still drops codex-produced
6454
- // reasoning, and a no-codex-history session is a byte-for-byte no-op).
6453
+ // Build one attempt from canonical history. The provider adapter creates
6454
+ // only the temporary wire view required by this turn's selected API;
6455
+ // subscription identity never edits or filters durable history.
6455
6456
  const activeTurnId = turnId;
6456
6457
  if (!activeTurnId) {
6457
6458
  throw new Error("Turn id was not initialized");
6458
6459
  }
6459
6460
  let runInput: Awaited<ReturnType<typeof turnInput>>["input"] | null = null;
6460
6461
  const prepareRunAttemptInput = async () => {
6461
- const prepared = await turnInput(
6462
- db,
6463
- runtime,
6464
- agent,
6465
- trigger,
6466
- { currentCodexCredentialId: effectiveCodexCredentialId },
6467
- {
6468
- turnId: activeTurnId,
6469
- recovering: turn.executionGeneration > 1,
6470
- ...(unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {}),
6471
- ...(runCredentialsNote ? { runCredentialsNote } : {}),
6472
- projectModelHistory: modelHistoryProjector,
6473
- },
6474
- );
6462
+ const prepared = await turnInput(db, runtime, agent, trigger, {
6463
+ turnId: activeTurnId,
6464
+ recovering: turn.executionGeneration > 1,
6465
+ ...(unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {}),
6466
+ ...(runCredentialsNote ? { runCredentialsNote } : {}),
6467
+ providerApi,
6468
+ projectModelHistory: modelHistoryProjector,
6469
+ });
6475
6470
  runInput = prepared.input;
6471
+ providerArtifactCandidates = prepared.providerArtifactCandidates;
6476
6472
  // Slice index = the length of the model-facing (active) history this turn
6477
6473
  // is seeded from; new items beyond it (the trigger message + this turn's
6478
6474
  // generated items) are the ones to persist. After a compaction this is the
@@ -6494,18 +6490,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6494
6490
  // and bricks the session (issue-61). The sanitized seed is already
6495
6491
  // orphan-free, so it is a stable prefix of the re-sanitized history and the
6496
6492
  // slice begins exactly at the first genuinely-new item.
6497
- const activeSeedRows = await getActiveSessionHistoryItems(
6498
- db,
6499
- input.workspaceId,
6500
- input.sessionId,
6501
- );
6502
- // Seed the reconcile watermark from EXACTLY the view the model's
6503
- // `state.history` was seeded from (items strip on the items path = HOLE D; NO
6504
- // strip on the run-state blob path, where foreign reasoning is neutralized but
6505
- // KEPT = HOLE E), so the model-input length and the watermark never disagree.
6506
- persistedHistoryCount = reconcileSeedCount(activeSeedRows, prepared.modelHistoryFromItems, {
6507
- currentCodexCredentialId: effectiveCodexCredentialId,
6508
- });
6493
+ persistedHistoryCount = prepared.persistedHistoryCount;
6509
6494
  nextHistoryPosition = await nextSessionHistoryPosition(
6510
6495
  db,
6511
6496
  input.workspaceId,
@@ -6530,20 +6515,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6530
6515
  attemptId: input.attemptId,
6531
6516
  },
6532
6517
  // Never reuse the persisted prior-turn signal for recovery. Proactive
6533
- // guards provide their exact current signal; provider overflows do not,
6534
- // so null derives decision metadata from active history. Forced
6535
- // recovery proves progress separately by comparing the replacement
6536
- // with the current active-history estimate in maybeCompactContext.
6518
+ // guards provide their exact current provider-based signal; provider
6519
+ // overflows do not, so their diagnostic signal stays unknown/zero.
6520
+ // Forced recovery proves progress separately by comparing the
6521
+ // replacement with the current active-history estimate.
6537
6522
  recoverySignalTokens,
6538
6523
  compactSummarizer,
6539
6524
  {
6540
6525
  force: true,
6541
6526
  ...(triggerLabel === "operator" ? { clearRequestedCompaction: true } : {}),
6542
6527
  trigger: triggerLabel,
6543
- codexAccount: {
6544
- currentCodexCredentialId: effectiveCodexCredentialId,
6545
- },
6546
- projectModelInput: modelHistoryProjector,
6528
+ projectModelInput: compactionModelHistoryProjector,
6547
6529
  ...compactionModeOptions,
6548
6530
  },
6549
6531
  ),
@@ -6559,13 +6541,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6559
6541
  return outcome;
6560
6542
  };
6561
6543
 
6562
- // Keep response usage identity across every stream attempt in this
6544
+ // Keep response identity across every stream attempt in this
6563
6545
  // activity. Context compaction retries the same logical dispatch by
6564
6546
  // calling runStreamAttempt again; resetting this state there would reuse
6565
6547
  // the first no-response-ID fallback key and suppress a real model call.
6566
- const modelResponseUsageState = createModelResponseUsageEventState(
6567
- claimedModelUsageSourceKeys,
6568
- );
6548
+ const modelResponseState = createModelResponseEventState(claimedModelUsageSourceKeys);
6569
6549
  const runStreamAttempt = async (): Promise<RunAgentTurnResult> => {
6570
6550
  if (!runInput) {
6571
6551
  throw new Error("Run input was not prepared");
@@ -6579,6 +6559,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6579
6559
  // schema or compatibility state.
6580
6560
  let currentToolBatchCallIds = new Set<string>();
6581
6561
  let currentToolBatchCompletedCallIds = new Set<string>();
6562
+ let streamSawPerResponseUsage = false;
6582
6563
  // Actual input tokens of the most recent model response this turn; the
6583
6564
  // pre-read trigger for the NEXT turn. Persisted at every turn-end path.
6584
6565
  throwIfWorkerShuttingDown();
@@ -6695,7 +6676,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6695
6676
  : {}),
6696
6677
  }
6697
6678
  : {}),
6698
- contextCompactionSignal: () => modelResponseUsageContextSignal(modelResponseUsageState),
6679
+ contextCompactionSignal: () => modelResponseContextSignal(modelResponseState),
6699
6680
  contextCompactionRequested: () =>
6700
6681
  isSessionCompactionRequested(db, input.workspaceId, input.sessionId),
6701
6682
  callModelInputFilter: secretRedactionModelInputFilter((value) => redact(value)),
@@ -6736,9 +6717,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6736
6717
  }
6737
6718
  let stableToolCallIdsToClear: string[] | null = null;
6738
6719
  let completedCurrentToolBatch = false;
6739
- const responseUsageResult = await processModelResponseUsageEvent({
6720
+ const responseResult = await processModelResponseTerminalEvent({
6740
6721
  event: next.value,
6741
- state: modelResponseUsageState,
6722
+ state: modelResponseState,
6742
6723
  dispatchId: modelUsageDispatchId,
6743
6724
  settings,
6744
6725
  db,
@@ -6769,7 +6750,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6769
6750
  model: turn.model,
6770
6751
  ...(resolvedModel?.provider.id ? { providerId: resolvedModel.provider.id } : {}),
6771
6752
  });
6772
- if (responseUsageResult.status === "processed") {
6753
+ if (responseResult.status === "processed") {
6754
+ streamSawPerResponseUsage ||= responseResult.usageReported;
6773
6755
  currentToolBatchCallIds = new Set<string>();
6774
6756
  currentToolBatchCompletedCallIds = new Set<string>();
6775
6757
  await reconcileConversationTruth();
@@ -6908,7 +6890,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6908
6890
  stream.completed.catch(() => undefined),
6909
6891
  cancellationSignal,
6910
6892
  );
6911
- if (modelResponseUsageState.responseUsageCount === 0) {
6893
+ if (!streamSawPerResponseUsage) {
6912
6894
  const aggregateUsage = stream.state.usage;
6913
6895
  const normalizedAggregateUsage = normalizeModelCallUsage(aggregateUsage);
6914
6896
  const aggregateInput = normalizedAggregateUsage.telemetry.inputTokens;
@@ -6919,8 +6901,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6919
6901
  });
6920
6902
  if (!claimedModelUsageSourceKeys.has(aggregateSourceKey)) {
6921
6903
  claimedModelUsageSourceKeys.add(aggregateSourceKey);
6922
- // The single aggregate frame is this turn's only model-usage record, so
6923
- // it is the first (account-switch surfaces here just like a first response).
6904
+ // The aggregate frame is only a billing fallback when no terminal
6905
+ // response exposed usage. It is not final-request context authority.
6924
6906
  const aggregateAccountCtx = modelCallAccountContext({
6925
6907
  servingCredentialId: effectiveCodexCredentialId,
6926
6908
  priorSessionCredentialId: priorSessionCodexCredentialId,
@@ -6987,14 +6969,11 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
6987
6969
  },
6988
6970
  recordAttemptSignals: async () => {
6989
6971
  if (!aggregateAuthoritative) return;
6990
- if (normalizedAggregateUsage.totalTokens !== null) {
6991
- modelResponseUsageState.lastProviderContextTokensObserved =
6992
- normalizedAggregateUsage.totalTokens;
6993
- modelResponseUsageState.providerContextRevision += 1;
6994
- }
6995
- if (aggregateInput !== null && aggregateInput > 0) {
6996
- await setLastInputTokensFenced(aggregateInput);
6997
- }
6972
+ // Stream aggregates can cover multiple requests and therefore
6973
+ // cannot identify the final request that compaction must bind.
6974
+ // They remain billing/telemetry fallback data only.
6975
+ modelResponseState.contextSignal = null;
6976
+ await setLastInputTokensFenced(null);
6998
6977
  },
6999
6978
  });
7000
6979
  }
@@ -7071,7 +7050,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7071
7050
  runState: {
7072
7051
  serializedRunState: stream.state.toString(),
7073
7052
  pendingApprovals: approvals,
7074
- frozenCodexCredentialId: effectiveCodexCredentialId,
7075
7053
  humanInputRequests: humanInputRequests.map(
7076
7054
  ({ isNew: _isNew, ...request }) => request,
7077
7055
  ),
@@ -7735,7 +7713,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7735
7713
  priorCredentialId: effectiveCodexCredentialId,
7736
7714
  accounts,
7737
7715
  now: new Date(),
7738
- usedConnectors: serving?.connectorNamespaces ?? [],
7739
7716
  })
7740
7717
  : ({ kind: "none" } as const);
7741
7718
  const candidateAvailable =
@@ -7951,9 +7928,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
7951
7928
  priorCredentialId: effectiveCodexCredentialId,
7952
7929
  accounts: fresh,
7953
7930
  now: new Date(),
7954
- // P4: the just-capped serving account's connector set is the proxy for
7955
- // "what this session has access to" — prefer a covering failover target.
7956
- usedConnectors: serving?.connectorNamespaces ?? [],
7957
7931
  });
7958
7932
  if (decision.kind === "active") {
7959
7933
  rotated = true;
@@ -8164,8 +8138,9 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8164
8138
  // credential-row UUID is unchanged. HTTP 400 + the exact provider
8165
8139
  // semantic proves this request never entered inference. Atomically mark
8166
8140
  // the active opaque artifacts rejected and recover the SAME logical turn
8167
- // from durable history; messages, tool calls/results, summaries, and the
8168
- // original audit rows remain intact. If no artifact can be invalidated,
8141
+ // from durable history; messages, tool calls/results, readable reasoning,
8142
+ // and the original audit rows remain intact. Opaque remote compaction has
8143
+ // no portable plaintext representation. If no artifact can be invalidated,
8169
8144
  // fall through to the terminal path rather than resend an equivalent
8170
8145
  // request forever.
8171
8146
  const encryptedArtifactRejection =
@@ -8181,6 +8156,20 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8181
8156
  ) {
8182
8157
  await flushRuntimeBatcher();
8183
8158
  await reconcileConversationTruth({ requireDurable: true });
8159
+ const activeHistory = await getActiveSessionHistoryItems(
8160
+ db,
8161
+ input.workspaceId,
8162
+ input.sessionId,
8163
+ );
8164
+ const rejectedHistoryItemIds = selectRejectedProviderArtifactHistoryIds(
8165
+ activeHistory,
8166
+ providerArtifactCandidates,
8167
+ lastCodexRequestOpaqueArtifacts,
8168
+ );
8169
+ const rejectedRunStateId =
8170
+ providerArtifactCandidates.runStateId && lastCodexRequestOpaqueArtifacts.length > 0
8171
+ ? providerArtifactCandidates.runStateId
8172
+ : undefined;
8184
8173
  const recovery = await requestSessionTurnRecovery(db, input.workspaceId, {
8185
8174
  sessionId: input.sessionId,
8186
8175
  turnId,
@@ -8192,7 +8181,8 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8192
8181
  retryable: true,
8193
8182
  },
8194
8183
  providerArtifactInvalidation: {
8195
- codexCredentialId: effectiveCodexCredentialId,
8184
+ historyItemIds: rejectedHistoryItemIds,
8185
+ ...(rejectedRunStateId ? { runStateId: rejectedRunStateId } : {}),
8196
8186
  reason: encryptedArtifactRejection.kind,
8197
8187
  },
8198
8188
  });
@@ -8543,19 +8533,6 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
8543
8533
  );
8544
8534
  }
8545
8535
  }
8546
- // Part B.1: the connector namespaces codex_apps listed this turn → the
8547
- // connector-set cache. NON-EMPTY-only: a flaky/empty tools/list must never
8548
- // overwrite a known set with [] (false coverage drop). Read by reference
8549
- // AFTER the run, so every tools/list this turn has accumulated.
8550
- const connectorNamespaces = preparedTools?.codexConnectorNamespaces;
8551
- if (connectorNamespaces && connectorNamespaces.size > 0) {
8552
- await waitForTurnFinalizerStep(
8553
- recordCodexAccountConnectors(db, input.workspaceId, effectiveCodexCredentialId, [
8554
- ...connectorNamespaces,
8555
- ]).catch(() => undefined),
8556
- finalizerSignal,
8557
- );
8558
- }
8559
8536
  }
8560
8537
  if (codexLeaseHeartbeatTimer) {
8561
8538
  clearInterval(codexLeaseHeartbeatTimer);
@@ -9032,6 +9009,14 @@ export function agentRunFailurePayload(
9032
9009
  : {}),
9033
9010
  };
9034
9011
  }
9012
+ if (isMcpTransportConnectivityError(error)) {
9013
+ return {
9014
+ error:
9015
+ "A required MCP server was temporarily unreachable. The same turn will retry after a short delay.",
9016
+ code: "mcp_transport_unavailable",
9017
+ retryable: true,
9018
+ };
9019
+ }
9035
9020
  if (
9036
9021
  status === 429 ||
9037
9022
  code === "rate_limit_exceeded" ||