@bitkyc08/opencodex 2.95.0 → 2.97.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (276) hide show
  1. package/bin/ocx.mjs +214 -19
  2. package/gui/dist/assets/{App-8yedBkhm.js → App-DuNhggE1.js} +18 -18
  3. package/gui/dist/assets/{Tray-erSMzxPG.js → Tray-CEw0y0jR.js} +1 -1
  4. package/gui/dist/assets/index-CynE_Rue.js +94 -0
  5. package/gui/dist/assets/{quota-summary-BK4VuQNc.js → quota-summary-VHFSlUuM.js} +1 -1
  6. package/gui/dist/index.html +1 -1
  7. package/package.json +1 -1
  8. package/src/adapters/command-code.ts +15 -16
  9. package/src/adapters/cursor/transport-retry.ts +2 -2
  10. package/src/adapters/devin/cloud-direct/chat.ts +15 -6
  11. package/src/adapters/kiro/stream.ts +63 -4
  12. package/src/adapters/kiro-constants.ts +15 -1
  13. package/src/adapters/kiro-events.ts +31 -1
  14. package/src/adapters/kiro-retry.ts +3 -3
  15. package/src/adapters/ollama-native.ts +21 -7
  16. package/src/adapters/openai-responses/canonical-forward.ts +50 -13
  17. package/src/adapters/openai-responses/empty-assistant.ts +15 -3
  18. package/src/adapters/openai-responses/passthrough.ts +8 -8
  19. package/src/adapters/openai-responses/reasoning.ts +9 -4
  20. package/src/adapters/openai-responses/request-strips.ts +1 -1
  21. package/src/adapters/openai-responses/transport.ts +17 -26
  22. package/src/adapters/physical-send.ts +3 -3
  23. package/src/adapters/qoder/adapter.ts +1 -1
  24. package/src/adapters/qoder/image-input.ts +5 -2
  25. package/src/adapters/zed-openai-stream.ts +2 -1
  26. package/src/bridge/errors.ts +11 -2
  27. package/src/bridge/response-json.ts +2 -1
  28. package/src/bridge/sse.ts +14 -6
  29. package/src/chat/failure-verdict.ts +21 -0
  30. package/src/chat/outbound.ts +139 -21
  31. package/src/claude/outbound.ts +49 -2
  32. package/src/cli/agent.ts +44 -15
  33. package/src/cli/capabilities-agents-routing.ts +9 -0
  34. package/src/cli/capabilities-base.ts +25 -3
  35. package/src/cli/capabilities-command.ts +12 -3
  36. package/src/cli/capability-contracts.ts +2 -0
  37. package/src/cli/codex-launch-auth.ts +30 -1
  38. package/src/cli/codex-launcher.ts +12 -5
  39. package/src/cli/codex-shim-autorestore.ts +5 -4
  40. package/src/cli/dispatch.ts +11 -176
  41. package/src/cli/ensure-desired-integrations.ts +4 -3
  42. package/src/cli/help.ts +2 -2
  43. package/src/cli/index.ts +9 -2
  44. package/src/cli/link.ts +1 -1
  45. package/src/cli/message-command.ts +2 -1
  46. package/src/cli/message-mcp-command.ts +29 -0
  47. package/src/cli/registry.ts +12 -4
  48. package/src/cli/root.ts +8 -0
  49. package/src/cli/sync-command.ts +243 -0
  50. package/src/cli/sync-fanout-process.ts +92 -0
  51. package/src/cli/sync-fanout.ts +150 -0
  52. package/src/cli/sync-flags.ts +10 -0
  53. package/src/cli/sync-link-status.ts +39 -0
  54. package/src/cli/sync-update-guard.ts +18 -0
  55. package/src/client/connect.ts +31 -14
  56. package/src/client/link-home.ts +3 -2
  57. package/src/client/machine-listener.ts +3 -0
  58. package/src/client/restore-ownership.ts +19 -1
  59. package/src/client/runtime-restart.ts +12 -4
  60. package/src/client/runtime.ts +40 -11
  61. package/src/client/stop-routing.ts +62 -0
  62. package/src/client/sync-runtime.ts +65 -0
  63. package/src/clients/config-export.ts +32 -3
  64. package/src/codex/account-store.ts +1 -0
  65. package/src/codex/auth-context-provider.ts +15 -0
  66. package/src/codex/auth-context.ts +59 -122
  67. package/src/codex/catalog/antigravity-discovery.ts +16 -5
  68. package/src/codex/catalog/provider-models.ts +6 -8
  69. package/src/codex/forward-transport-headers.ts +65 -36
  70. package/src/codex/inject/config-toml.ts +88 -0
  71. package/src/codex/inject/multi-agent-v2.ts +30 -4
  72. package/src/codex/inject/plan.ts +34 -7
  73. package/src/codex/inject-coordination.ts +11 -0
  74. package/src/codex/inject.ts +18 -7
  75. package/src/codex/installation-id.ts +72 -0
  76. package/src/codex/lineage.ts +13 -7
  77. package/src/codex/quota.ts +22 -6
  78. package/src/codex/request-owned-affinity-key.ts +11 -0
  79. package/src/codex/request-owned-affinity.ts +118 -0
  80. package/src/codex/request-owned-main-pin.ts +86 -0
  81. package/src/codex/role-capability.ts +52 -41
  82. package/src/codex/routing/active-account.ts +13 -5
  83. package/src/codex/routing/selection.ts +40 -1
  84. package/src/codex/routing/thread-affinity-store.ts +916 -0
  85. package/src/codex/routing/thread-affinity.ts +90 -12
  86. package/src/codex/toml-source-lines.ts +72 -13
  87. package/src/combos/failover.ts +6 -109
  88. package/src/combos/request.ts +2 -2
  89. package/src/combos/resolve.ts +29 -11
  90. package/src/config/process-state.ts +5 -0
  91. package/src/config/schema/leaf-validators.ts +1 -0
  92. package/src/generated/compatibility-version.json +496 -192
  93. package/src/grok/sync.ts +10 -1
  94. package/src/images/loop.ts +2 -1
  95. package/src/lib/client-error-body.ts +184 -0
  96. package/src/lib/client-retry-after.ts +66 -0
  97. package/src/lib/config-ownership.ts +4 -0
  98. package/src/lib/context-overflow-verdict.ts +113 -0
  99. package/src/lib/desktop-cli-handoff.d.mts +1 -1
  100. package/src/lib/desktop-cli-handoff.mjs +11 -1
  101. package/src/lib/empty-tool-catalog.ts +25 -3
  102. package/src/lib/error-destination.ts +47 -0
  103. package/src/lib/failure-detail.ts +66 -0
  104. package/src/lib/model-identity.ts +44 -0
  105. package/src/lib/private-sqlite-path.ts +124 -0
  106. package/src/lib/response-service-tier.ts +17 -0
  107. package/src/lib/retry-after.ts +3 -0
  108. package/src/lib/spend-ledger-owner.ts +43 -4
  109. package/src/lib/upstream-condition.ts +142 -0
  110. package/src/lib/upstream-retry.ts +14 -11
  111. package/src/lib/windows-owner-acl.ts +13 -6
  112. package/src/lib/windows-secret-acl.ts +2 -2
  113. package/src/link/remote-platform.ts +14 -0
  114. package/src/link/remote-windows.ts +51 -9
  115. package/src/link/ssh-argv.ts +63 -10
  116. package/src/messaging/mcp-queue-rpc.ts +102 -0
  117. package/src/messaging/mcp-queue-server.ts +42 -0
  118. package/src/messaging/mcp-queue-tools.ts +39 -0
  119. package/src/oauth/anthropic-identity.ts +2 -6
  120. package/src/oauth/generic-account-failover.ts +13 -11
  121. package/src/oauth/index.ts +41 -74
  122. package/src/oauth/oauth-errors.ts +67 -0
  123. package/src/oauth/store.ts +1 -1
  124. package/src/protocols/codecs/ollama.ts +197 -0
  125. package/src/protocols/encoders/adapter-events.ts +4 -0
  126. package/src/protocols/encoders/chat.ts +18 -10
  127. package/src/protocols/trace.ts +18 -0
  128. package/src/providers/anthropic-subscription.ts +25 -0
  129. package/src/providers/claude-cli-usage.ts +15 -67
  130. package/src/providers/fastwire.ts +3 -2
  131. package/src/providers/quota/anthropic-decision-windows.ts +5 -19
  132. package/src/providers/quota/anthropic-usage-windows.ts +65 -0
  133. package/src/providers/quota/vendor-probes-key.ts +60 -24
  134. package/src/providers/request-pacing.ts +3 -2
  135. package/src/responses/apply-patch-envelope.ts +45 -19
  136. package/src/responses/compaction-history-framing.ts +5 -1
  137. package/src/responses/freeform-wrapper-scan.ts +5 -3
  138. package/src/responses/parser-tools.ts +1 -0
  139. package/src/responses/progressive-freeform-input.ts +354 -45
  140. package/src/responses/schema.ts +5 -2
  141. package/src/routing/fallback-policy.ts +3 -1
  142. package/src/server/admission-model-scope.ts +17 -6
  143. package/src/server/admitted-http-turn.ts +56 -0
  144. package/src/server/audio-dictation.ts +3 -0
  145. package/src/server/audio-live.ts +8 -2
  146. package/src/server/audio-upstream.ts +2 -0
  147. package/src/server/auth-cors.ts +5 -0
  148. package/src/server/chat-completions.ts +45 -20
  149. package/src/server/chat-native-sse.ts +52 -7
  150. package/src/server/chat-native.ts +60 -35
  151. package/src/server/claude-bridge-pool-session.ts +59 -0
  152. package/src/server/claude-messages.ts +57 -27
  153. package/src/server/context-history.ts +14 -7
  154. package/src/server/images.ts +14 -5
  155. package/src/server/inbound-body-admission.ts +11 -4
  156. package/src/server/index/live-sideband.ts +16 -2
  157. package/src/server/index/ollama-routes.ts +89 -0
  158. package/src/server/index/serve-options.ts +22 -4
  159. package/src/server/index/websocket-handler.ts +5 -3
  160. package/src/server/index.ts +6 -36
  161. package/src/server/inference/client-encoder-delivery.ts +4 -2
  162. package/src/server/json-serialized-bytes.ts +44 -0
  163. package/src/server/live.ts +30 -5
  164. package/src/server/management/agent-settings-routes.ts +24 -21
  165. package/src/server/management/codex-cli-update-routes.ts +2 -1
  166. package/src/server/management/route-registry.ts +1 -1
  167. package/src/server/messages-native-scope.ts +2 -1
  168. package/src/server/messages-native-tool-guard.ts +1 -1
  169. package/src/server/messages-native.ts +12 -7
  170. package/src/server/messages-shared.ts +6 -2
  171. package/src/server/native-final-log.ts +93 -0
  172. package/src/server/ollama-inbound.ts +349 -0
  173. package/src/server/ollama-native-paths.ts +21 -0
  174. package/src/server/ollama-route-policy.ts +73 -0
  175. package/src/server/relay.ts +22 -3
  176. package/src/server/request-decompress.ts +4 -2
  177. package/src/server/request-log-failure-attribution.ts +22 -0
  178. package/src/server/request-log-failure-detail.ts +80 -0
  179. package/src/server/request-log-terminal-status.ts +4 -2
  180. package/src/server/request-log.ts +79 -42
  181. package/src/server/response-log-body.ts +6 -1
  182. package/src/server/responses/account-change-state.ts +17 -6
  183. package/src/server/responses/adapter-continuation.ts +9 -4
  184. package/src/server/responses/adapter-delivery.ts +5 -4
  185. package/src/server/responses/adapter-dispatch.ts +36 -30
  186. package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
  187. package/src/server/responses/codex-auth-error.ts +22 -17
  188. package/src/server/responses/codex-ws-exchange.ts +11 -8
  189. package/src/server/responses/codex-ws-metadata.ts +94 -21
  190. package/src/server/responses/codex-ws-request.ts +3 -1
  191. package/src/server/responses/combo-required-tool-contract.ts +24 -12
  192. package/src/server/responses/compact.ts +89 -41
  193. package/src/server/responses/compaction-recovery.ts +4 -2
  194. package/src/server/responses/compaction-routing.ts +9 -4
  195. package/src/server/responses/core-auth.ts +32 -14
  196. package/src/server/responses/core-codex-account.ts +34 -12
  197. package/src/server/responses/core-combo-failure.ts +5 -1
  198. package/src/server/responses/core-combo-native.ts +2 -1
  199. package/src/server/responses/core-combo.ts +51 -33
  200. package/src/server/responses/core-errors.ts +22 -20
  201. package/src/server/responses/core-lifetime.ts +6 -3
  202. package/src/server/responses/core-normalize.ts +8 -1
  203. package/src/server/responses/core-options.ts +7 -0
  204. package/src/server/responses/core-pipeline.ts +4 -3
  205. package/src/server/responses/fetch-helpers.ts +7 -1
  206. package/src/server/responses/input-admission.ts +28 -2
  207. package/src/server/responses/native-injection.ts +1 -1
  208. package/src/server/responses/native-response-json.ts +13 -4
  209. package/src/server/responses/native-response-output.ts +1 -2
  210. package/src/server/responses/non-replayable-error.ts +2 -1
  211. package/src/server/responses/pacing-overload.ts +3 -2
  212. package/src/server/responses/passthrough-build-error.ts +36 -0
  213. package/src/server/responses/passthrough-delivery.ts +55 -14
  214. package/src/server/responses/passthrough-dispatch.ts +26 -39
  215. package/src/server/responses/passthrough-error.ts +9 -9
  216. package/src/server/responses/policy-fallback.ts +2 -1
  217. package/src/server/responses/request-prepare.ts +67 -48
  218. package/src/server/responses/request-sidecar-auth.ts +3 -1
  219. package/src/server/responses/request-transport.ts +35 -31
  220. package/src/server/responses/run-turn-execution.ts +6 -6
  221. package/src/server/responses/sidecar-execution.ts +16 -12
  222. package/src/server/responses/spend-storage-error.ts +2 -1
  223. package/src/server/responses/ws-upstream.ts +9 -0
  224. package/src/server/responses-custom-tool-repair.ts +14 -3
  225. package/src/server/responses-terminal-repair.ts +5 -2
  226. package/src/server/responses-undeclared-tool-guard.ts +39 -16
  227. package/src/server/safe-response-headers.ts +162 -13
  228. package/src/server/search.ts +13 -4
  229. package/src/server/turn-state-handles.ts +164 -0
  230. package/src/server/workflow-refusal.ts +2 -1
  231. package/src/server/ws-bridge.ts +5 -2
  232. package/src/service/ownership-mutation-lease.d.mts +11 -2
  233. package/src/service/ownership-mutation-lease.mjs +53 -11
  234. package/src/types/config.ts +2 -0
  235. package/src/update/index.ts +133 -25
  236. package/src/update/npm-install.d.mts +8 -0
  237. package/src/update/npm-install.mjs +20 -1
  238. package/src/update/orchestrated-admission.d.mts +4 -0
  239. package/src/update/orchestrated-admission.mjs +12 -0
  240. package/src/update/orchestrated-arguments.d.mts +12 -0
  241. package/src/update/orchestrated-arguments.mjs +64 -0
  242. package/src/update/orchestrated-child.d.mts +12 -0
  243. package/src/update/orchestrated-child.mjs +70 -0
  244. package/src/update/orchestrated-entry.ts +234 -0
  245. package/src/update/orchestrated-identity-probe-entry.ts +20 -0
  246. package/src/update/orchestrated-identity.ts +53 -0
  247. package/src/update/orchestrated-live-identity.ts +48 -0
  248. package/src/update/orchestrated-node-driver.mjs +99 -0
  249. package/src/update/orchestrated-outcome.d.mts +21 -0
  250. package/src/update/orchestrated-outcome.mjs +40 -0
  251. package/src/update/orchestrated-plan.ts +49 -0
  252. package/src/update/orchestrated-pnpm.d.mts +8 -0
  253. package/src/update/orchestrated-pnpm.mjs +33 -0
  254. package/src/update/orchestrated-session.d.mts +37 -0
  255. package/src/update/orchestrated-session.mjs +264 -0
  256. package/src/update/orchestrated-token.d.mts +10 -0
  257. package/src/update/orchestrated-token.mjs +21 -0
  258. package/src/update/orchestrated-token.ts +3 -0
  259. package/src/update/orchestrated-tree.d.mts +1 -0
  260. package/src/update/orchestrated-tree.mjs +14 -0
  261. package/src/update/pnpm-global-install.d.mts +6 -0
  262. package/src/update/pnpm-global-install.mjs +9 -2
  263. package/src/update/posix-install-supervisor-entry.mjs +6 -0
  264. package/src/update/posix-install-supervisor.d.mts +10 -0
  265. package/src/update/posix-install-supervisor.mjs +88 -0
  266. package/src/update/sync-update-mutex.d.mts +10 -0
  267. package/src/update/sync-update-mutex.mjs +164 -0
  268. package/src/update/sync-update-mutex.ts +1 -0
  269. package/src/update/test-home-guard.mjs +33 -0
  270. package/src/update/transactional-install.mjs +2 -2
  271. package/src/usage/error-detail.ts +225 -0
  272. package/src/usage/log.ts +60 -27
  273. package/src/vision/describe.ts +4 -7
  274. package/src/web-search/executor.ts +4 -7
  275. package/src/web-search/loop.ts +31 -5
  276. package/gui/dist/assets/index-D7DCm5eQ.js +0 -94
@@ -33,6 +33,7 @@ import {
33
33
  import { redactSecretString } from "../../lib/redact";
34
34
  import { outboundCredentials } from "../../lib/outbound-credential-registry";
35
35
  import { compressUpstreamRequestBody, isCanonicalCodexResponsesUrl } from "./request-compression";
36
+ import { redeemOutboundTurnState, TURN_STATE_BINDING_INIT, turnStateBinding } from "../turn-state-handles";
36
37
 
37
38
  export { withUpstreamHttpVersion };
38
39
 
@@ -382,6 +383,10 @@ export function providerFetch(
382
383
  // else keeps the provider's HTTP fetch. See ws-upstream.ts for the details.
383
384
  const unobserved = async (input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit,
384
385
  reacquirePacing?: (provider: OcxProviderConfig, modelId?: string) => Promise<void>) => {
386
+ // Turn-state handles are redeemed here, at the send, where the admitted caller, the provider and
387
+ // the outbound account are all settled; a handle bound to anything else is not forwarded.
388
+ const sendHeaders = (sent?: RequestInit) => new Headers(sent?.headers ?? (input instanceof Request ? input.headers : undefined));
389
+ init = redeemOutboundTurnState(init, turnStateBinding(providerName, sendHeaders(init)));
385
390
  const upstreamWebsocket = provider.upstreamWebsocket;
386
391
  if (!options.httpOnly && typeof input === "string" && init
387
392
  && shouldUseCodexWsUpstream(input, init, runtime, upstreamWebsocket)) {
@@ -412,7 +417,8 @@ export function providerFetch(
412
417
  };
413
418
  const fallback = ((url: Parameters<typeof globalThis.fetch>[0], fallbackInit?: RequestInit) =>
414
419
  httpFetch(url, { ...fallbackInit, [WS_FALLBACK_RECEIPT]: notifyOnce } as RequestInit, reacquirePacing, null)) as typeof globalThis.fetch;
415
- const wsSend = codexWsExecutor((url: Parameters<typeof globalThis.fetch>[0], wsInit?: RequestInit) => codexWsUpstreamFetch(String(url), wsInit ?? init,
420
+ const wsSend = codexWsExecutor((url: Parameters<typeof globalThis.fetch>[0], wsInit?: RequestInit) => codexWsUpstreamFetch(String(url),
421
+ { ...(wsInit ?? init), [TURN_STATE_BINDING_INIT]: turnStateBinding(providerName, sendHeaders(wsInit ?? init)) } as RequestInit,
416
422
  fallback, runtime, options.onCodexWsQuota, options.beforeDispatch, options.nativeControl,
417
423
  async () => { (await waitForPacing(init.signal ?? undefined))?.release(); }, notifyOnce, options.preOutputRefusalHold),
418
424
  { provider, http: markEgressTransparentExecutor(fallback) });
@@ -1,3 +1,4 @@
1
+ import { clientFacingModelText, type ClientFacingModelLabel } from "../../lib/error-destination";
1
2
  /**
2
3
  * Pre-dispatch input admission (#1412).
3
4
  *
@@ -63,6 +64,8 @@ export interface InputAdmissionResult {
63
64
  estimatedTokens: number;
64
65
  /** Resolved ceiling, or null when nothing could be resolved (=> always admitted). */
65
66
  ceiling: number | null;
67
+ /** Actual shared context window when an input-only ceiling differs. */
68
+ contextWindow?: number | null;
66
69
  /** Output space reserved by the combo preflight; absent on the loose direct gate. */
67
70
  requiredOutputHeadroom?: number;
68
71
  }
@@ -310,6 +313,7 @@ export function checkComboTargetInputAdmission(
310
313
  estimatedTokens,
311
314
  ceiling,
312
315
  requiredOutputHeadroom,
316
+ ...(window !== ceiling ? { contextWindow: window } : {}),
313
317
  };
314
318
  }
315
319
 
@@ -325,8 +329,30 @@ export function checkInputAdmission(
325
329
  modelId: string,
326
330
  nativeContextCap?: NativeContextLimitsInput,
327
331
  ): InputAdmissionResult {
328
- const ceiling = resolveInputCeiling(provider, providerName, modelId, nativeContextCap);
332
+ const { window, ceiling } = resolveContextLimits(provider, providerName, modelId, nativeContextCap);
329
333
  if (ceiling === null) return { admitted: true, estimatedTokens: 0, ceiling: null };
330
334
  const estimatedTokens = estimateInputTokens(parsed, modelId, provider);
331
- return { admitted: estimatedTokens <= ceiling * ADMISSION_TOLERANCE, estimatedTokens, ceiling };
335
+ return { admitted: estimatedTokens <= ceiling * ADMISSION_TOLERANCE, estimatedTokens, ceiling,
336
+ ...(window !== ceiling ? { contextWindow: window } : {}) };
337
+ }
338
+
339
+ export function inputAdmissionRefusalMessage(input: InputAdmissionResult, model: ClientFacingModelLabel): string {
340
+ const count = (value: number | null) => value !== null && Number.isFinite(value) && value >= 0 ? Math.trunc(value) : 0;
341
+ const label = clientFacingModelText(model);
342
+ const estimate = count(input.estimatedTokens);
343
+ const ceiling = count(input.ceiling);
344
+ if (input.contextWindow !== undefined && input.estimatedTokens > ceiling) {
345
+ const window = input.contextWindow === null ? "" : `; context window: ${count(input.contextWindow)} tokens`;
346
+ return `Estimated input (~${estimate} tokens) exceeds the input limit of ${label} (${ceiling} tokens${window}). `
347
+ + "Start a new session or choose a model with a larger input limit.";
348
+ }
349
+ const window = count(input.contextWindow ?? input.ceiling);
350
+ const inputLimit = input.contextWindow !== undefined ? `; input limit: ${ceiling} tokens` : "";
351
+ return input.requiredOutputHeadroom !== undefined
352
+ ? `Estimated input (~${estimate} tokens) plus ${count(input.requiredOutputHeadroom)} `
353
+ + `tokens of requested output headroom cannot fit the context window of ${label} `
354
+ + `(${window} tokens${inputLimit}).`
355
+ : `Estimated input (~${estimate} tokens) is far past the context window `
356
+ + `of ${label} (${window} tokens${inputLimit}). Start a new session or choose a `
357
+ + `model with a larger context window.`;
332
358
  }
@@ -120,7 +120,7 @@ export class NativeInjectionChannel implements NativeResponseControl {
120
120
  this.providerExecutedCallTypes, this.declaredBareToolNames,
121
121
  );
122
122
  if (undeclared !== undefined) {
123
- injectionError(UNDECLARED_TOOL_CALL_ERROR_CODE, undeclaredToolCallMessage(undeclared));
123
+ injectionError(UNDECLARED_TOOL_CALL_ERROR_CODE, undeclaredToolCallMessage());
124
124
  }
125
125
  }
126
126
  /** Advertise client-owned function/custom calls and approvals, never hosted execution. */
@@ -5,10 +5,19 @@ export function nativeResponseRecord(value: unknown): value is Record<string, un
5
5
  return value !== null && typeof value === "object" && !Array.isArray(value);
6
6
  }
7
7
 
8
+ const canonical = (item: unknown): string => Array.isArray(item) ? `[${item.map(canonical).join(",")}]`
9
+ : nativeResponseRecord(item) ? `{${Object.keys(item).sort().map(key => `${JSON.stringify(key)}:${canonical(item[key])}`).join(",")}}`
10
+ : JSON.stringify(item) ?? "null";
11
+ const fingerprint = (json: string): string => createHash("sha256").update(json).digest("hex");
12
+
8
13
  /** Compare JSON content by value: object-key order is irrelevant, array order is not. */
9
14
  export function nativeResponseFingerprint(value: unknown): string {
10
- const canonical = (item: unknown): string => Array.isArray(item) ? `[${item.map(canonical).join(",")}]`
11
- : nativeResponseRecord(item) ? `{${Object.keys(item).sort().map(key => `${JSON.stringify(key)}:${canonical(item[key])}`).join(",")}}`
12
- : JSON.stringify(item) ?? "null";
13
- return createHash("sha256").update(canonical(value)).digest("hex");
15
+ return fingerprint(canonical(value));
16
+ }
17
+
18
+ /** Keep transient body keys within the existing 64-byte digest payload bound. */
19
+ export function nativeResponseOutputIdentity(item: Record<string, unknown>): string {
20
+ if (typeof item.id === "string") return `id:${item.id}`;
21
+ const json = canonical(item);
22
+ return Buffer.byteLength(json, "utf8") <= 64 ? `json:${json}` : `body:${fingerprint(json)}`;
14
23
  }
@@ -1,4 +1,4 @@
1
- import { nativeResponseFingerprint as injectionFingerprint, nativeResponseRecord as record } from "./native-response-json";
1
+ import { nativeResponseFingerprint as injectionFingerprint, nativeResponseOutputIdentity as identity, nativeResponseRecord as record } from "./native-response-json";
2
2
  type Frame = Record<string, unknown>;
3
3
 
4
4
  /**
@@ -10,7 +10,6 @@ export function nativeResponseOutput(done: ReadonlyMap<number, Frame>, terminal:
10
10
  const observed = [...done.entries()].sort((a, b) => a[0] - b[0]).map(([, item]) => item);
11
11
  if (terminal == null || (Array.isArray(terminal) && !terminal.length)) return observed;
12
12
  if (!Array.isArray(terminal) || terminal.some(item => !record(item))) throw new Error("Invalid native response output.");
13
- const identity = (item: Frame) => typeof item.id === "string" ? `id:${item.id}` : `body:${injectionFingerprint(item)}`;
14
13
  const positions = new Map<string, number>();
15
14
  observed.forEach((item, index) => {
16
15
  const key = identity(item);
@@ -1,3 +1,4 @@
1
+ import { carryProxyError } from "../../lib/client-error-body";
1
2
  import { isNonReplayableResponse, isReplayRefusalResponse, markResponseNonReplayable } from "../../lib/upstream-retry";
2
3
  import { clientCancelledResponse, readDisplaySafeErrorText } from "./core-errors";
3
4
 
@@ -44,5 +45,5 @@ export async function sanitizeNonReplayableUpstreamError(
44
45
  status: response.status, headers,
45
46
  });
46
47
  markResponseNonReplayable(safe);
47
- return safe;
48
+ return carryProxyError(response, safe);
48
49
  }
@@ -1,13 +1,14 @@
1
+ import { markProxyError } from "../../lib/client-error-body";
1
2
  import { formatErrorResponse } from "../../bridge";
2
3
  import { RequestPacingQueueOverloadError } from "../../providers/request-pacing";
3
4
 
4
5
  /** Convert local request-pacing admission failures into a retryable HTTP response. */
5
6
  export function requestPacingOverloadResponse(error: unknown): Response | undefined {
6
7
  if (!(error instanceof RequestPacingQueueOverloadError)) return undefined;
7
- return formatErrorResponse(
8
+ return markProxyError(formatErrorResponse(
8
9
  429,
9
10
  "rate_limit_error",
10
11
  error.message,
11
12
  { retryAfter: String(error.retryAfterSeconds) },
12
- );
13
+ ));
13
14
  }
@@ -0,0 +1,36 @@
1
+ import { releaseCodexAuthContextProbeLease, type CodexAuthContext } from "../../codex/auth-context";
2
+ import { formatErrorResponse } from "../../bridge";
3
+ import { redactSecretString } from "../../lib/redact";
4
+ import { NamespaceToolCollisionError } from "../../responses/namespace-tool-compat";
5
+ import { RoutedCustomToolCompatError } from "../../responses/custom-tool-compat";
6
+ import { XaiToolSchemaCompatibilityError } from "../../adapters/xai-tool-schema";
7
+ import { MuseToolChoiceCompatibilityError } from "../../adapters/openai-responses/muse-tool-choice";
8
+ import { MuseToolCatalogCompatibilityError } from "../../adapters/openai-responses/muse-tool-catalog";
9
+ import { OpenGatewayInputCompatibilityError } from "../../adapters/openai-responses/opengateway-input";
10
+ import { mapCodexAuthContextErrorToResponse } from "./codex-auth-error";
11
+
12
+ /** Input-lowering errors are 400; late credential/identity failures retain their own contract. */
13
+ export function passthroughBuildErrorResponse(error: unknown, accountSelector?: string): Response | undefined {
14
+ const refusal = mapCodexAuthContextErrorToResponse(error, { now: Date.now(), accountSelector });
15
+ if (refusal) return refusal;
16
+ if (error instanceof NamespaceToolCollisionError
17
+ || error instanceof XaiToolSchemaCompatibilityError
18
+ || error instanceof RoutedCustomToolCompatError
19
+ || error instanceof MuseToolChoiceCompatibilityError
20
+ || error instanceof MuseToolCatalogCompatibilityError
21
+ || error instanceof OpenGatewayInputCompatibilityError) {
22
+ return formatErrorResponse(400, "invalid_request_error", redactSecretString(error.message));
23
+ }
24
+ return undefined;
25
+ }
26
+
27
+ /** Auth failures precede generic adapter-build 400s and preservation of an older rejection. */
28
+ export function codexAdapterBuildRefusal(error: unknown, context: CodexAuthContext | undefined,
29
+ accountSelector?: string, cleanup?: () => void): Response | undefined {
30
+ const refusal = mapCodexAuthContextErrorToResponse(error, { now: Date.now(), accountSelector });
31
+ if (refusal) {
32
+ cleanup?.();
33
+ releaseCodexAuthContextProbeLease(context);
34
+ }
35
+ return refusal;
36
+ }
@@ -1,5 +1,11 @@
1
+ import { clientFacingModelText } from "../../lib/error-destination";
2
+ import { projectLocalModelRefusalLog } from "../request-log-failure-detail";
3
+ import { markProxyError, rememberProxyErrorMessage, carryProxyError } from "../../lib/client-error-body";
4
+ import { releaseRequestOwnedMainOnOutcome, isKeyedRequestOwnedMain } from "../../codex/request-owned-affinity";
1
5
  import { wasPermanentAccountRefusal } from "../../oauth/account-auto-pause";
2
6
  import { hasResponseMediaType } from "../../lib/http-response-semantics";
7
+ import { routeKnownModels } from "../../usage/log";
8
+ import type { OcxProviderConfig } from "../../types";
3
9
  import { isNativeControlResponse } from "./native-response-control";
4
10
  import { guardBufferedComboRequiredTool } from "./combo-required-tool-contract";
5
11
  import { createComboRequiredToolStreamGuard } from "./combo-required-tool-stream";
@@ -34,6 +40,7 @@ import {
34
40
  codexDenialOutcomeMeta,
35
41
  isFixedCodexAccount,
36
42
  shouldDeferCodexResetDerivedCooldown,
43
+ shouldRetryCodexPoolAccountQuota,
37
44
  } from "./core-codex-account";
38
45
  import { isMainQuotaDispatchLive, isMainQuotaDispatchWsClaimed } from "../../codex/main-account-cache";
39
46
  import { MAIN_CODEX_ACCOUNT_ID } from "../../codex/account-id";
@@ -482,9 +489,16 @@ export async function deliverPassthroughResponse(
482
489
  });
483
490
  }
484
491
 
485
- const headers = sanitizePassthroughHeaders(upstreamResponse.headers, codexSafetyBufferingOptions);
492
+ // Every relayed header set below goes through this filter with the route's known models, so a
493
+ // reported model (row and header) is only ever a model id this route or a catalog already names.
494
+ const passthroughHeaderOptions = {
495
+ ...codexSafetyBufferingOptions,
496
+ knownModels: [...routeKnownModels(logCtx), ...providerKnownModels(route.provider)],
497
+ provider: logCtx.provider,
498
+ };
499
+ const headers = sanitizePassthroughHeaders(upstreamResponse.headers, passthroughHeaderOptions);
486
500
  if (!upstreamResponse.ok) noteUpstreamRequestId(logCtx, headers);
487
- const resolvedModel = headers.get("openai-model")?.trim();
501
+ const resolvedModel = headers.get("openai-model") ?? undefined;
488
502
  if (resolvedModel) {
489
503
  logCtx.servedModel = resolvedModel;
490
504
  if (!logCtx.preserveResolvedModelFromRoute) logCtx.resolvedModel = resolvedModel;
@@ -571,6 +585,18 @@ export async function deliverPassthroughResponse(
571
585
  });
572
586
  }
573
587
  } else {
588
+ if (terminalBodyWillRecord) {
589
+ options.setTerminalOutcomeRecorder?.((status, httpStatusOverride) => {
590
+ terminalRecorder?.(status, httpStatusOverride);
591
+ options.onNativePassthroughTerminal?.(status);
592
+ });
593
+ }
594
+ if (isKeyedRequestOwnedMain(admissionState.authCtx) && !terminalBodyWillRecord && !upstreamResponse.ok && !isNonReplayableResponse(upstreamResponse)
595
+ && !isReplayRefusalResponse(upstreamResponse) && !wasPermanentAccountRefusal(upstreamResponse) && isCanonicalOpenAiForwardProvider(route.provider)) {
596
+ const outcome = upstreamResponse.status >= 500 && await shouldRetryCodexPoolAccountQuota(upstreamResponse, options.abortSignal)
597
+ ? 429 : upstreamResponse.status;
598
+ releaseRequestOwnedMainOnOutcome(admissionState.authCtx, outcome, route.modelId);
599
+ }
574
600
  // The WS observer is the only plain-main publisher for a WebSocket exchange;
575
601
  // a prelude projection carries the prelude snapshot, not fresh evidence.
576
602
  // The dispatch claim is authoritative because downstream wrappers can replace the Response.
@@ -602,7 +628,7 @@ export async function deliverPassthroughResponse(
602
628
  return new Response(upstreamResponse.body, {
603
629
  status: upstreamResponse.status,
604
630
  statusText: upstreamResponse.statusText,
605
- headers: sanitizePassthroughHeaders(upstreamResponse.headers, codexSafetyBufferingOptions),
631
+ headers: sanitizePassthroughHeaders(upstreamResponse.headers, passthroughHeaderOptions),
606
632
  });
607
633
  }
608
634
  if (!upstreamResponse.ok) {
@@ -621,27 +647,28 @@ export async function deliverPassthroughResponse(
621
647
  // Unsafe partial data falls back to #452's non-empty status-only JSON.
622
648
  const errorText = await readDisplaySafeErrorText(upstreamResponse, upstream.signal, "");
623
649
  if (upstreamResponse.status === 413) {
650
+ if (options.clientModelLabel) projectLocalModelRefusalLog(logCtx, options.clientModelLabel);
624
651
  return clientRequestedStream
625
- ? streamingContextOverflowResponse(parsed._responseModelId ?? parsed.modelId, translatorBudget)
652
+ ? streamingContextOverflowResponse(clientFacingModelText(options.clientModelLabel), translatorBudget)
626
653
  : jsonContextOverflowResponse();
627
654
  }
628
655
  const policyRefusal = rewriteUpstreamPolicyRefusal({
629
656
  status: upstreamResponse.status,
630
657
  errorText,
631
658
  stream: clientRequestedStream,
632
- modelId: parsed._responseModelId ?? parsed.modelId,
659
+ modelId: clientFacingModelText(options.clientModelLabel),
633
660
  destinationIsXai: isXaiResponsesDestination(route.provider),
634
661
  translatorBudget,
635
662
  turnAdmissionLease: options.turnAdmissionLease,
636
663
  });
637
664
  if (policyRefusal) return policyRefusal;
638
- return formatPassthroughUpstreamError(upstreamResponse.status, errorText, {
665
+ return carryProxyError(upstreamResponse, formatPassthroughUpstreamError(upstreamResponse.status, errorText, {
639
666
  statusText: upstreamResponse.statusText,
640
667
  headers,
641
668
  // Provenance, not inference: `errorText` is empty when the bounded read finds nothing
642
669
  // display-safe, and an empty body is exactly what the retryable-429 default fires on.
643
670
  replayRefusal: isReplayRefusalResponse(upstreamResponse),
644
- });
671
+ }));
645
672
  }
646
673
 
647
674
  if (options.nativeControl && isNativeControlResponse(upstreamResponse) && upstreamResponse.body) {
@@ -707,7 +734,7 @@ export async function deliverPassthroughResponse(
707
734
  upstream,
708
735
  terminalRepairPolicy,
709
736
  translatorBudget,
710
- options.responsesTerminalRepairScheduler,
737
+ options.responsesTerminalRepairScheduler, logCtx,
711
738
  )
712
739
  : upstreamResponse.body;
713
740
  passthroughSseBody = webSearchBridgePlan
@@ -749,7 +776,7 @@ export async function deliverPassthroughResponse(
749
776
  upstream,
750
777
  terminalRepairPolicy,
751
778
  translatorBudget,
752
- options.responsesTerminalRepairScheduler,
779
+ options.responsesTerminalRepairScheduler, logCtx,
753
780
  ),
754
781
  continuation,
755
782
  );
@@ -912,7 +939,8 @@ export async function deliverPassthroughResponse(
912
939
  providerExecutedCallTypes,
913
940
  declaredBareWireToolNames,
914
941
  recoverableBareCustomWireToolNames,
915
- { refuseUndeclared: nativeExchange.undeclaredToolGuardActive },
942
+ { refuseUndeclared: nativeExchange.undeclaredToolGuardActive,
943
+ onRefusal: message => rememberProxyErrorMessage(logCtx, message, { status: 502, type: "upstream_error", code: UNDECLARED_TOOL_CALL_ERROR_CODE }) },
916
944
  )
917
945
  : undefined,
918
946
  grokUpstreamEchoEnabled
@@ -1147,7 +1175,7 @@ export async function deliverPassthroughResponse(
1147
1175
  rememberPassthroughResponseChecked(client.terminal.response);
1148
1176
  }
1149
1177
 
1150
- const jsonHeaders = sanitizePassthroughHeaders(headers, codexSafetyBufferingOptions);
1178
+ const jsonHeaders = sanitizePassthroughHeaders(headers, passthroughHeaderOptions);
1151
1179
  jsonHeaders.set("content-type", "application/json");
1152
1180
  return markPreinspectedJsonResponse(new Response(clientJson, {
1153
1181
  status: upstreamResponse.status,
@@ -1415,9 +1443,9 @@ export async function deliverPassthroughResponse(
1415
1443
  }
1416
1444
  })();
1417
1445
  if (undeclared !== undefined) {
1418
- return formatErrorResponse(502, "upstream_error", undeclaredToolCallMessage(undeclared), {
1446
+ return markProxyError(formatErrorResponse(502, "upstream_error", undeclaredToolCallMessage(), {
1419
1447
  code: UNDECLARED_TOOL_CALL_ERROR_CODE,
1420
- });
1448
+ }));
1421
1449
  }
1422
1450
  }
1423
1451
  // Same split as the SSE rewrite: normalization applies whenever a declared name exists,
@@ -1489,7 +1517,7 @@ export async function deliverPassthroughResponse(
1489
1517
  }
1490
1518
  throw error;
1491
1519
  }
1492
- const sseHeaders = sanitizePassthroughHeaders(headers, codexSafetyBufferingOptions);
1520
+ const sseHeaders = sanitizePassthroughHeaders(headers, passthroughHeaderOptions);
1493
1521
  sseHeaders.set("content-type", "text/event-stream");
1494
1522
  sseHeaders.set("cache-control", "no-store");
1495
1523
  return new Response(stream, {
@@ -1537,3 +1565,16 @@ export async function deliverPassthroughResponse(
1537
1565
  headers,
1538
1566
  });
1539
1567
  }
1568
+
1569
+ /**
1570
+ * Model ids the user configured for the selected provider: its models and retention lists and
1571
+ * default. A provider-owned model hint that names one of them is relayed as the proxy's copy.
1572
+ * Live-discovered ids are not trusted here, because the upstream's own /models answer chose them;
1573
+ * neither is the selection list, which a model preset fills from that same discovered catalog.
1574
+ */
1575
+ function providerKnownModels(provider: OcxProviderConfig): string[] {
1576
+ return [
1577
+ ...(provider.models ?? []), ...(provider.retainModels ?? []),
1578
+ ...(provider.defaultModel ? [provider.defaultModel] : []),
1579
+ ];
1580
+ }
@@ -1,3 +1,7 @@
1
+ import { clientFacingModelText } from "../../lib/error-destination";
2
+ import { markProxyError } from "../../lib/client-error-body";
3
+ import { CodexInstallationIdUnavailableError } from "../../codex/installation-id";
4
+ import { passthroughBuildErrorResponse } from "./passthrough-build-error";
1
5
  import { recoverAutoPausedAccount, rotatePassthroughKeyOn429 } from "./passthrough-key-rotation";
2
6
  import { adoptCodexWsStage, capturePassthroughAffinity } from "./passthrough-observations";
3
7
  import { spendLedgerStorageErrorResponse } from "./spend-storage-error";
@@ -48,14 +52,9 @@ import {
48
52
  createCodexAuthDispatchGuard,
49
53
  } from "../../codex/auth-context";
50
54
  import {
51
- NamespaceToolCollisionError,
52
55
  restoreRoutedNamespaceCalls,
53
56
  } from "../../responses/namespace-tool-compat";
54
- import { restoreRoutedCustomCalls, RoutedCustomToolCompatError } from "../../responses/custom-tool-compat";
55
- import { XaiToolSchemaCompatibilityError } from "../../adapters/xai-tool-schema";
56
- import { MuseToolChoiceCompatibilityError } from "../../adapters/openai-responses/muse-tool-choice";
57
- import { MuseToolCatalogCompatibilityError } from "../../adapters/openai-responses/muse-tool-catalog";
58
- import { OpenGatewayInputCompatibilityError } from "../../adapters/openai-responses/opengateway-input";
57
+ import { restoreRoutedCustomCalls } from "../../responses/custom-tool-compat";
59
58
  import { formatErrorResponse } from "../../bridge";
60
59
  import { redactSecretString } from "../../lib/redact";
61
60
  import {
@@ -121,7 +120,7 @@ import type { CodexAuthContext } from "../../codex/auth-context";
121
120
  import { checkOutboundBodySize, describeOutboundBodyRefusal } from "./outbound-body-guard";
122
121
  import { streamingContextOverflowResponse } from "./context-overflow";
123
122
  import {
124
- SendBudgetExhaustedError,
123
+ SendBudgetExhaustedError, errorDestinationLabel,
125
124
  fetchWithTransientRetry,
126
125
  applyUpstreamRecoveryInit,
127
126
  isNonReplayableResponse,
@@ -379,22 +378,8 @@ export async function preparePassthroughExchange(
379
378
  });
380
379
  } catch (error) {
381
380
  releaseCodexAuthContextProbeLease(admissionState.authCtx);
382
- // A tool catalog this proxy cannot lower onto one wire namespace is a client input error, and
383
- // the rotation-rebuild and bridged paths already answer 400 for the identical throw. Rethrowing
384
- // it here escaped every catch up to the Bun handler, so the same request produced an
385
- // unstructured 500 — and no request log — depending only on whether a rotation ran first.
386
- // Same shape for a tool_choice this proxy cannot honor: the destination rejects a schema the
387
- // catalog had to drop, so the selector naming it is a client input error, not a 500.
388
- if (
389
- error instanceof NamespaceToolCollisionError
390
- || error instanceof XaiToolSchemaCompatibilityError
391
- || error instanceof RoutedCustomToolCompatError
392
- || error instanceof MuseToolChoiceCompatibilityError
393
- || error instanceof MuseToolCatalogCompatibilityError
394
- || error instanceof OpenGatewayInputCompatibilityError
395
- ) {
396
- return formatErrorResponse(400, "invalid_request_error", redactSecretString(error.message));
397
- }
381
+ const refusal = passthroughBuildErrorResponse(error, route.codexAccountNamespace);
382
+ if (refusal) return refusal;
398
383
  throw error;
399
384
  }
400
385
  const functionRepairSchemas = isCanonicalOpenAiForwardProvider(route.provider)
@@ -858,7 +843,7 @@ export async function preparePassthroughExchange(
858
843
  // Charged to the SAME request counter every other send goes through. The
859
844
  // replacement is bought here rather than by a nested retry helper, so there is
860
845
  // one charge for one send and no per-layer counter to reconcile.
861
- if (report.beforeSend?.() === false) throw new SendBudgetExhaustedError(safeHostLabel(request.url));
846
+ if (report.beforeSend?.() === false) throw new SendBudgetExhaustedError(errorDestinationLabel(request.url));
862
847
  started = true;
863
848
  },
864
849
  }),
@@ -893,14 +878,14 @@ export async function preparePassthroughExchange(
893
878
  console.warn(
894
879
  `[responses] refused an oversized outbound body: bytes=${result.bytes} limit=${result.limit} `
895
880
  + `input_images=${result.imageCount} image_bytes=${result.imageBytes} `
896
- + `model=${JSON.stringify(parsed.modelId)}`,
881
+ + `model=${JSON.stringify(clientFacingModelText(options.clientModelLabel))}`,
897
882
  );
898
883
  // A streaming client treats HTTP 413 as a retryable transport error and resends the same
899
884
  // oversized body — the reconnect loop #3177 exists to stop. Terminal overflow is the
900
885
  // honest shape, and it is what the upstream-413 path already returns.
901
886
  if (clientRequestedStream) {
902
887
  return streamingContextOverflowResponse(
903
- parsed._responseModelId ?? parsed.modelId,
888
+ clientFacingModelText(options.clientModelLabel),
904
889
  translatorBudget,
905
890
  );
906
891
  }
@@ -928,7 +913,7 @@ export async function preparePassthroughExchange(
928
913
  nativeHostState.lease = null;
929
914
  releaseCodexAuthContextProbeLease(admissionState.authCtx);
930
915
  return unboundPoolSpendRefusalResponse(logCtx)
931
- ?? formatErrorResponse(429, "request_send_budget_exhausted", err.message);
916
+ ?? markProxyError(formatErrorResponse(429, "request_send_budget_exhausted", err.message));
932
917
  }
933
918
  const refusal = unwrapUpstreamRetryEvidenceError(err);
934
919
  if (refusal instanceof CopilotAutoAdapterHandoff) {
@@ -949,12 +934,12 @@ export async function preparePassthroughExchange(
949
934
  nativeHostState.lease = null;
950
935
  releaseCodexAuthContextProbeLease(admissionState.authCtx);
951
936
  if (refusal instanceof OAuthLoginRequiredError) {
952
- return formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(refusal));
937
+ return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(refusal)));
953
938
  }
954
939
  if (refusal instanceof AnthropicAccountCooldownError) {
955
940
  return anthropicPoolCooldownResponse(refusal, { comboAttempt: options.comboAttempt, logCtx });
956
941
  }
957
- return formatErrorResponse(403, "permission_error", publicOAuthAuthenticationErrorMessage(refusal));
942
+ return markProxyError(formatErrorResponse(403, "permission_error", publicOAuthAuthenticationErrorMessage(refusal)));
958
943
  }
959
944
  const localRefusal = spendLedgerStorageErrorResponse(err, logCtx) ?? mapCodexAuthContextErrorToResponse(refusal, {
960
945
  now: Date.now(), accountSelector: route.codexAccountNamespace,
@@ -1049,7 +1034,7 @@ export async function preparePassthroughExchange(
1049
1034
  // retry wrapper replaces — proves the host was reached (#914 review).
1050
1035
  .then(adoptObservedResponse).then(preOutputRefusals.observe);
1051
1036
  },
1052
- { abortSignal: upstream.signal, label: safeHostLabel(request.url),
1037
+ { abortSignal: upstream.signal, label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url),
1053
1038
  attempts: sendBudgetState.initialSendAllowance(transientSendAttempts()),
1054
1039
  // Reporter path: direct requests and enforced spend. Receipt-mode sends are settled
1055
1040
  // by `onPhysicalDispatch`; a reporter as well would count them twice.
@@ -1124,6 +1109,7 @@ export async function preparePassthroughExchange(
1124
1109
  recordAdapterReasoning(logCtx, request);
1125
1110
  recordAdapterTier(logCtx, request);
1126
1111
  } catch (err) {
1112
+ if (err instanceof CodexInstallationIdUnavailableError) return { failed: transportFailureResponse(err) };
1127
1113
  upstream.abort();
1128
1114
  if (options.abortSignal?.aborted) return { failed: clientCancelledResponse() };
1129
1115
  const msg = err instanceof Error ? err.message : String(err);
@@ -1161,7 +1147,7 @@ export async function preparePassthroughExchange(
1161
1147
  // Gated on the return, not fire-and-forget: a consumed permit means this leg
1162
1148
  // already sent once, and letting the second call through would be a free send.
1163
1149
  if (allowance.permit && !allowance.permit.use()) {
1164
- throw new SendBudgetExhaustedError(safeHostLabel(request.url));
1150
+ throw new SendBudgetExhaustedError(errorDestinationLabel(request.url));
1165
1151
  }
1166
1152
  transportState.noteRoutedAttemptSend(passthroughEstimate, innerRecovery ?? recovery);
1167
1153
  return fetchWithHeaderTimeout(request.url, applyUpstreamRecoveryInit({
@@ -1185,7 +1171,7 @@ export async function preparePassthroughExchange(
1185
1171
  route.provider.authMode === "forward")
1186
1172
  .then(adoptObservedResponse);
1187
1173
  },
1188
- { abortSignal: upstream.signal, label: safeHostLabel(request.url), attempts: allowance.attempts,
1174
+ { abortSignal: upstream.signal, label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url), attempts: allowance.attempts,
1189
1175
  onSendsConsumed: transientSendReporter(allowance.permit),
1190
1176
  claimAmbiguousResend: recovery === "empty-completion" ? undefined : claimPreHeaderResend },
1191
1177
  );
@@ -1327,7 +1313,7 @@ export async function preparePassthroughExchange(
1327
1313
  route.provider.authMode === "forward",
1328
1314
  ).then(adoptObservedResponse);
1329
1315
  },
1330
- { abortSignal: upstream.signal, label: safeHostLabel(request.url),
1316
+ { abortSignal: upstream.signal, label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url),
1331
1317
  attempts: remainingTransientSendBudget(transientSendAttempts()),
1332
1318
  onSendsConsumed: transientSendReporter(), claimAmbiguousResend: claimPreHeaderResend },
1333
1319
  );
@@ -1364,13 +1350,13 @@ export async function preparePassthroughExchange(
1364
1350
  upstream.abort();
1365
1351
  releaseCodexAuthContextProbeLease(admissionState.authCtx);
1366
1352
  if (options.abortSignal?.aborted || req.signal.aborted) return clientCancelledResponse();
1367
- if (err instanceof OAuthTokenRefreshBusyError) return formatErrorResponse(503, "server_error", publicOAuthAuthenticationErrorMessage(err), { retryAfter: "1" });
1368
- return formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(err));
1353
+ if (err instanceof OAuthTokenRefreshBusyError) return markProxyError(formatErrorResponse(503, "server_error", publicOAuthAuthenticationErrorMessage(err), { retryAfter: "1" }));
1354
+ return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(err)));
1369
1355
  }
1370
1356
  if (route.provider.googleMode === "cloud-code-assist" && !refreshed.projectId) {
1371
1357
  upstream.abort();
1372
1358
  releaseCodexAuthContextProbeLease(admissionState.authCtx);
1373
- return formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(new Error("Cloud Code Assist project is required")));
1359
+ return markProxyError(formatErrorResponse(401, "authentication_error", publicOAuthAuthenticationErrorMessage(new Error("Cloud Code Assist project is required"))));
1374
1360
  }
1375
1361
  transportState.sentOAuthSnapshot = refreshed;
1376
1362
  transportState.replayOAuthCredentialSnapshot = {
@@ -1418,6 +1404,7 @@ export async function preparePassthroughExchange(
1418
1404
  recordAdapterReasoning(logCtx, request);
1419
1405
  recordAdapterTier(logCtx, request);
1420
1406
  } catch (err) {
1407
+ if (err instanceof CodexInstallationIdUnavailableError) return transportFailureResponse(err);
1421
1408
  upstream.abort();
1422
1409
  if (options.abortSignal?.aborted) return clientCancelledResponse();
1423
1410
  const msg = err instanceof Error ? err.message : String(err);
@@ -1451,7 +1438,7 @@ export async function preparePassthroughExchange(
1451
1438
  route.provider.authMode === "forward")
1452
1439
  .then(adoptObservedResponse);
1453
1440
  },
1454
- { abortSignal: upstream.signal, label: safeHostLabel(request.url), attempts: remainingTransientSendBudget(transientSendAttempts()),
1441
+ { abortSignal: upstream.signal, label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url), attempts: remainingTransientSendBudget(transientSendAttempts()),
1455
1442
  onSendsConsumed: transientSendReporter(), claimAmbiguousResend: claimPreHeaderResend },
1456
1443
  );
1457
1444
  } catch (err) {
@@ -1598,7 +1585,7 @@ export async function preparePassthroughExchange(
1598
1585
  route.provider.authMode === "forward")
1599
1586
  .then(adoptObservedResponse);
1600
1587
  },
1601
- { abortSignal: upstream.signal, label: safeHostLabel(request.url), attempts: remainingTransientSendBudget(transientSendAttempts()),
1588
+ { abortSignal: upstream.signal, label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url), attempts: remainingTransientSendBudget(transientSendAttempts()),
1602
1589
  onSendsConsumed: transientSendReporter(), claimAmbiguousResend: claimPreHeaderResend },
1603
1590
  );
1604
1591
  } catch (err) {
@@ -1923,7 +1910,7 @@ export async function preparePassthroughExchange(
1923
1910
  error,
1924
1911
  {
1925
1912
  abortSignal: upstream.signal,
1926
- label: safeHostLabel(request.url),
1913
+ label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url),
1927
1914
  attempts: remainingTransientSendBudget(transientSendAttempts()),
1928
1915
  // The whole decision, including the stage the preflight observed and the grant the
1929
1916
  // pre-header helper shares. A committed stage refuses here without touching the
@@ -7,8 +7,8 @@ import {
7
7
  UPSTREAM_RESET_REPLAY_REFUSED_CODE,
8
8
  } from "../../lib/upstream-retry";
9
9
  import {
10
+ formatClientRetryAfter,
10
11
  resolveClientRetryAfter,
11
- validateClientRetryAfterHeader,
12
12
  } from "../../lib/retry-after";
13
13
 
14
14
  function isCyberPolicyBody(body: string): boolean {
@@ -93,7 +93,6 @@ export function formatPassthroughUpstreamError(
93
93
  const trimmed = bodyText.trim();
94
94
  const now = options?.now ?? Date.now();
95
95
  const upstreamRetryAfter = options?.headers?.get("retry-after")?.trim() || undefined;
96
- const originalValid = validateClientRetryAfterHeader(upstreamRetryAfter, now);
97
96
  const cyberPolicyFailure = isCyberPolicyBody(trimmed);
98
97
  const replayRefusal = options?.replayRefusal === true || isReplayRefusalBody(trimmed);
99
98
  // Two different reasons to answer with no wait at all, handled the same way: a hard policy
@@ -108,12 +107,12 @@ export function formatPassthroughUpstreamError(
108
107
  now,
109
108
  });
110
109
 
110
+ // The header leaves in this proxy's own format: an integer of seconds, never the upstream bytes.
111
+ const formatted = resolved === undefined ? undefined : formatClientRetryAfter(resolved, now);
112
+
111
113
  if (trimmed) {
112
- const needsSet = resolved !== undefined && upstreamRetryAfter !== resolved;
113
- const needsDelete = (suppressRetryAfter && upstreamRetryAfter !== undefined)
114
- || (resolved === undefined
115
- && upstreamRetryAfter !== undefined
116
- && originalValid === undefined);
114
+ const needsSet = formatted !== undefined && upstreamRetryAfter !== formatted;
115
+ const needsDelete = formatted === undefined && upstreamRetryAfter !== undefined;
117
116
 
118
117
  // A refusal always takes the rewriting path: it has a header to add even when the
119
118
  // upstream named no wait for it to remove.
@@ -128,7 +127,7 @@ export function formatPassthroughUpstreamError(
128
127
  const headers = options?.headers
129
128
  ? new Headers(options.headers)
130
129
  : new Headers({ "Content-Type": "application/json" });
131
- if (needsSet) headers.set("Retry-After", resolved!);
130
+ if (needsSet) headers.set("Retry-After", formatted!);
132
131
  else headers.delete("Retry-After");
133
132
  if (replayRefusal) applyReplayRefusalClientHeaders(headers);
134
133
  const rewritten = new Response(bodyText, {
@@ -152,7 +151,8 @@ export function formatPassthroughUpstreamError(
152
151
  );
153
152
  const headers = new Headers(response.headers);
154
153
  headers.set("Content-Type", "application/json");
155
- if (resolved !== undefined) headers.set("Retry-After", resolved);
154
+ if (formatted !== undefined) headers.set("Retry-After", formatted);
155
+ else headers.delete("Retry-After");
156
156
  if (replayRefusal) applyReplayRefusalClientHeaders(headers);
157
157
  const wrapped = new Response(response.body, { status: response.status, headers });
158
158
  return replayRefusal ? retainReplayRefusal(wrapped) : wrapped;