@bitkyc08/opencodex 2.91.0 → 2.93.0-preview.20261027

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 (360) hide show
  1. package/bin/ocx.mjs +47 -8
  2. package/gui/dist/assets/App-CJZoq-uF.js +58 -0
  3. package/gui/dist/assets/{Tray-BuLy7hL1.js → Tray-BWQPcgZn.js} +1 -1
  4. package/gui/dist/assets/index-DRe8Hhfz.css +1 -0
  5. package/gui/dist/assets/index-Xfct8owH.js +94 -0
  6. package/gui/dist/assets/quota-summary-DxToX1rV.js +1 -0
  7. package/gui/dist/index.html +2 -2
  8. package/gui/dist/provider-icons/workbuddy-color.svg +29 -0
  9. package/package.json +8 -7
  10. package/src/adapters/anthropic.ts +28 -5
  11. package/src/adapters/coding-agent/batch-dispatcher.ts +122 -0
  12. package/src/adapters/coding-agent/profile.ts +54 -16
  13. package/src/adapters/coding-agent/turn.ts +30 -2
  14. package/src/adapters/command-code.ts +114 -7
  15. package/src/adapters/cursor/native-exec-fs.ts +11 -9
  16. package/src/adapters/devin/live-models.ts +4 -0
  17. package/src/adapters/google-vertex-endpoint.ts +9 -1
  18. package/src/adapters/google-vertex-key-project.ts +223 -0
  19. package/src/adapters/google-wire-compiler.ts +1 -2
  20. package/src/adapters/google.ts +13 -3
  21. package/src/adapters/kiro/reasoning.ts +2 -0
  22. package/src/adapters/kiro-refusal.ts +6 -1
  23. package/src/adapters/kiro-wire.ts +1 -1
  24. package/src/adapters/openai-chat/messages.ts +8 -2
  25. package/src/adapters/openai-chat/passthrough.ts +35 -14
  26. package/src/adapters/openai-chat/reasoning-replay.ts +27 -16
  27. package/src/adapters/openai-chat/reasoning-wire.ts +117 -8
  28. package/src/adapters/openai-chat/required-tool-result.ts +64 -0
  29. package/src/adapters/openai-chat/tool-name-registry.ts +3 -1
  30. package/src/adapters/openai-chat.ts +9 -10
  31. package/src/adapters/openai-responses/passthrough.ts +7 -4
  32. package/src/adapters/openai-responses/reasoning.ts +13 -0
  33. package/src/adapters/openai-responses/request-strips.ts +60 -0
  34. package/src/adapters/qoder/adapter.ts +129 -8
  35. package/src/adapters/qoder/scaffold-guard.ts +5 -0
  36. package/src/adapters/reasoning-controls.ts +16 -0
  37. package/src/adapters/registry.ts +5 -0
  38. package/src/adapters/tool-call-id.ts +1 -3
  39. package/src/adapters/workbuddy-schema.ts +61 -0
  40. package/src/adapters/workbuddy.ts +68 -0
  41. package/src/bridge/errors.ts +27 -0
  42. package/src/bridge/response-json.ts +1 -1
  43. package/src/bridge/sse.ts +1 -1
  44. package/src/bridge.ts +1 -1
  45. package/src/chat/inbound.ts +4 -1
  46. package/src/chat/outbound.ts +20 -6
  47. package/src/chat/reasoning-details.ts +28 -5
  48. package/src/claude/desktop-first-party.ts +54 -4
  49. package/src/claude/desktop-picker.ts +11 -8
  50. package/src/claude/first-party-settings.ts +3 -1
  51. package/src/claude/inbound.ts +2 -1
  52. package/src/claude/intercept/local-ca.ts +2 -1
  53. package/src/claude/intercept/picker-ca-cleanup.ts +4 -2
  54. package/src/claude/intercept/picker-ca-startup.ts +3 -1
  55. package/src/claude/intercept/picker-models.ts +6 -3
  56. package/src/claude/intercept/picker-runtime.ts +3 -2
  57. package/src/claude/intercept/runtime.ts +6 -3
  58. package/src/claude/model-info.ts +31 -8
  59. package/src/cli/account-api.ts +11 -0
  60. package/src/cli/capabilities-agents-routing.ts +3 -3
  61. package/src/cli/capabilities-base.ts +7 -1
  62. package/src/cli/capabilities-observe-system.ts +10 -0
  63. package/src/cli/claude-desktop.ts +9 -8
  64. package/src/cli/claude.ts +3 -2
  65. package/src/cli/codex-cli-update.ts +8 -0
  66. package/src/cli/codex-shim-autorestore.ts +9 -0
  67. package/src/cli/combo-result.ts +13 -0
  68. package/src/cli/combo-stats.ts +3 -0
  69. package/src/cli/combo.ts +83 -41
  70. package/src/cli/cross-home-owner.ts +13 -1
  71. package/src/cli/dispatch.ts +25 -6
  72. package/src/cli/ensure-desired-integrations.ts +8 -6
  73. package/src/cli/export-command.ts +4 -1
  74. package/src/cli/help.ts +2 -1
  75. package/src/cli/index.ts +45 -45
  76. package/src/cli/integration-plan-dto.ts +4 -0
  77. package/src/cli/launcher-context.ts +49 -2
  78. package/src/cli/link.ts +45 -32
  79. package/src/cli/opencode.ts +20 -9
  80. package/src/cli/provider.ts +2 -1
  81. package/src/cli/registry.ts +6 -4
  82. package/src/cli/root.ts +9 -0
  83. package/src/cli/status.ts +59 -1
  84. package/src/cli/stop-restore.ts +113 -0
  85. package/src/cli/sync-expectation.ts +60 -0
  86. package/src/cli/uninstall-integrations.ts +41 -7
  87. package/src/cli/usage-compare.ts +68 -12
  88. package/src/client/connect.ts +40 -3
  89. package/src/client/link-home.ts +10 -1
  90. package/src/client/link-ingress.ts +1 -1
  91. package/src/client/link-readiness.ts +97 -0
  92. package/src/client/link-relay-transport.ts +79 -11
  93. package/src/client/link-relay.ts +74 -15
  94. package/src/client/link-tunnel.ts +45 -18
  95. package/src/client/machine-api.ts +3 -1
  96. package/src/client/machine-listener.ts +20 -2
  97. package/src/client/runtime.ts +20 -2
  98. package/src/client/sync-expectation.ts +118 -0
  99. package/src/clients/config-export/agent-clients.ts +96 -0
  100. package/src/clients/config-export/contracts.ts +25 -7
  101. package/src/clients/config-export/qoder.ts +38 -23
  102. package/src/clients/config-export/reasoning-metadata.ts +55 -8
  103. package/src/clients/config-export.ts +12 -0
  104. package/src/clients/lazycodex.ts +7 -2
  105. package/src/clients/reasoning-controls.ts +49 -0
  106. package/src/codex/account-store.ts +17 -3
  107. package/src/codex/app-server-restart-service.ts +6 -5
  108. package/src/codex/auth-api/account-identity.ts +14 -0
  109. package/src/codex/auth-api/account-list.ts +29 -2
  110. package/src/codex/auth-api/http.ts +13 -0
  111. package/src/codex/auth-api/main-account-probe.ts +2 -2
  112. package/src/codex/auth-api/pool-mode-gate.ts +16 -3
  113. package/src/codex/auth-api/pool-quota-probe.ts +15 -0
  114. package/src/codex/auth-context.ts +15 -12
  115. package/src/codex/catalog/build-entries.ts +22 -0
  116. package/src/codex/catalog/configured-tiers.ts +70 -0
  117. package/src/codex/catalog/effort.ts +52 -11
  118. package/src/codex/catalog/gather-capture.ts +5 -2
  119. package/src/codex/catalog/heal-observation.ts +155 -13
  120. package/src/codex/catalog/metadata.ts +6 -0
  121. package/src/codex/catalog/model-hints.ts +60 -11
  122. package/src/codex/catalog/model-visibility.ts +3 -2
  123. package/src/codex/catalog/native-models.ts +18 -3
  124. package/src/codex/catalog/parsing.ts +10 -8
  125. package/src/codex/catalog/pinned-models.ts +2 -1
  126. package/src/codex/catalog/provider-models.ts +18 -13
  127. package/src/codex/catalog/retained-sync.ts +2 -1
  128. package/src/codex/catalog/routed-gather.ts +19 -20
  129. package/src/codex/catalog/write-audit.ts +3 -2
  130. package/src/codex/catalog-self-heal.ts +43 -23
  131. package/src/codex/chatgpt-refresh-failure.ts +6 -2
  132. package/src/codex/cli-install-provenance.ts +418 -0
  133. package/src/codex/cli-installation-targets.ts +5 -1
  134. package/src/codex/cli-update-plan.ts +43 -16
  135. package/src/codex/collision-identity.ts +30 -5
  136. package/src/codex/convergence.ts +4 -3
  137. package/src/codex/data/roster-pinned-models.json +177 -1
  138. package/src/codex/desired-state.ts +6 -1
  139. package/src/codex/desktop-app/picker-freshness.ts +10 -8
  140. package/src/codex/desktop-compatibility/installed-build.ts +88 -12
  141. package/src/codex/desktop-compatibility/runtime.ts +29 -5
  142. package/src/codex/desktop-compatibility/usage-activation.ts +2 -1
  143. package/src/codex/desktop-compatibility/usage-controller.ts +9 -1
  144. package/src/codex/desktop-compatibility/windows-certificate-trust.ts +46 -12
  145. package/src/codex/inject/config-toml.ts +0 -18
  146. package/src/codex/inject/plan.ts +18 -3
  147. package/src/codex/inject/remove.ts +6 -2
  148. package/src/codex/inject.ts +1 -1
  149. package/src/codex/journal.ts +61 -1
  150. package/src/codex/model-entitlements.ts +1 -1
  151. package/src/codex/native-main-owner.ts +27 -1
  152. package/src/codex/native-profile-store.ts +2 -1
  153. package/src/codex/pool-credit-policy.ts +35 -1
  154. package/src/codex/pool-refresh-backoff.ts +3 -1
  155. package/src/codex/quota-types.ts +3 -0
  156. package/src/codex/quota.ts +24 -4
  157. package/src/codex/role-auto-assign.ts +6 -2
  158. package/src/codex/subagent-defaults.ts +49 -4
  159. package/src/codex/user-identity.ts +35 -6
  160. package/src/codex/windows-current-sid.ts +162 -0
  161. package/src/combos/failover.ts +17 -2
  162. package/src/combos/jev-decision-contract.ts +22 -1
  163. package/src/combos/jev-quota-activation.ts +5 -1
  164. package/src/combos/jev-quota-route.ts +1 -1
  165. package/src/combos/jev-quota.ts +9 -1
  166. package/src/combos/jev-service-exchange.ts +3 -1
  167. package/src/combos/jev-session-pin-config.ts +51 -0
  168. package/src/combos/jev.ts +101 -9
  169. package/src/combos/resolve.ts +45 -1
  170. package/src/combos/types.ts +43 -0
  171. package/src/config/load-degrade.ts +5 -0
  172. package/src/config/paths.ts +19 -3
  173. package/src/config/pending-teardown.ts +22 -1
  174. package/src/config/provider-validation.ts +22 -4
  175. package/src/config/removed-opencode-free.ts +11 -0
  176. package/src/config/schema/leaf-validators.ts +6 -0
  177. package/src/generated/compatibility-version.json +467 -311
  178. package/src/generated/model-metadata.ts +5 -5
  179. package/src/images/loop.ts +16 -2
  180. package/src/integrations/catalog-refresh.ts +1 -1
  181. package/src/integrations/hermes-profiles.ts +251 -0
  182. package/src/integrations/merge.ts +8 -0
  183. package/src/integrations/mutation-plan.ts +19 -4
  184. package/src/integrations/registry.ts +5 -0
  185. package/src/integrations/state.ts +36 -1
  186. package/src/integrations/writer.ts +18 -4
  187. package/src/lib/bun-path-runtime.mjs +2 -1
  188. package/src/lib/decision-quota-sink.ts +10 -0
  189. package/src/lib/errors.ts +62 -0
  190. package/src/lib/eventstream-decoder.ts +81 -37
  191. package/src/lib/opaque-proxy-tunnel.ts +32 -8
  192. package/src/lib/pinned-http.ts +35 -2
  193. package/src/lib/process-control.ts +13 -1
  194. package/src/lib/socks5-fetch.ts +59 -64
  195. package/src/lib/socks5-upload-waiter.ts +50 -0
  196. package/src/lib/sse-decoder.ts +7 -2
  197. package/src/lib/state-store-sweeper.ts +40 -14
  198. package/src/lib/translator-budget.ts +4 -4
  199. package/src/lib/upstream-retry.ts +4 -1
  200. package/src/lib/windows-native-principal-worker.ts +14 -0
  201. package/src/lib/windows-native-principal.ts +188 -0
  202. package/src/lib/windows-secret-acl.ts +39 -3
  203. package/src/lib/windows-user-principal.ts +62 -18
  204. package/src/link/diagnostics.ts +193 -0
  205. package/src/link/relay-auth.ts +6 -0
  206. package/src/link/ssh-config.ts +35 -3
  207. package/src/link/status-projection.ts +39 -0
  208. package/src/link/supervisor.ts +45 -12
  209. package/src/link/tunnel-history.ts +109 -0
  210. package/src/link/tunnel-state.ts +80 -17
  211. package/src/messaging/remote-files.ts +24 -1
  212. package/src/oauth/generic-account-failover.ts +91 -7
  213. package/src/oauth/index.ts +15 -0
  214. package/src/oauth/model-discovery-auth.ts +5 -0
  215. package/src/oauth/store.ts +4 -1
  216. package/src/oauth/token-guardian.ts +7 -0
  217. package/src/oauth/types.ts +9 -0
  218. package/src/oauth/workbuddy.ts +139 -0
  219. package/src/providers/account-quota-disk.ts +12 -4
  220. package/src/providers/antigravity-quota-identity.ts +37 -0
  221. package/src/providers/base-url-choices.ts +8 -5
  222. package/src/providers/codebuddy-models.ts +6 -0
  223. package/src/providers/codex-capacity.ts +3 -1
  224. package/src/providers/derive.ts +51 -3
  225. package/src/providers/kiro-models.ts +3 -0
  226. package/src/providers/mimo-thinking.ts +12 -0
  227. package/src/providers/model-discovery.ts +2 -0
  228. package/src/providers/model-rename-fields.ts +2 -0
  229. package/src/providers/model-rename-migration.ts +10 -0
  230. package/src/providers/openai-sidecar-credit.ts +2 -2
  231. package/src/providers/quota/account-cache.ts +3 -2
  232. package/src/providers/quota/antigravity.ts +25 -14
  233. package/src/providers/quota/report-cache.ts +13 -4
  234. package/src/providers/quota/vendor-probes-key.ts +60 -0
  235. package/src/providers/quota/workbuddy.ts +83 -0
  236. package/src/providers/quota-decision-antigravity.ts +64 -0
  237. package/src/providers/quota-decision-publication.ts +11 -0
  238. package/src/providers/quota-decision-snapshot.ts +21 -4
  239. package/src/providers/quota-types.ts +2 -0
  240. package/src/providers/quota.ts +3 -1
  241. package/src/providers/reasoning-capabilities.ts +76 -0
  242. package/src/providers/registry/entries-core.ts +23 -12
  243. package/src/providers/registry/entries-extended.ts +83 -27
  244. package/src/providers/registry/model-ids.ts +5 -0
  245. package/src/providers/registry/model-seeds.ts +120 -56
  246. package/src/providers/registry/types.ts +29 -2
  247. package/src/providers/resolved-model-policy.ts +30 -10
  248. package/src/providers/routed-config.ts +24 -1
  249. package/src/providers/stale-model-roster-migration.ts +123 -18
  250. package/src/providers/workbuddy-credit-balance.ts +81 -0
  251. package/src/providers/workbuddy-models.ts +37 -0
  252. package/src/providers/workbuddy.ts +60 -0
  253. package/src/remote-control/workspace-pi-runtime.ts +11 -6
  254. package/src/responses/compaction-summary.ts +45 -0
  255. package/src/responses/muse-tool-name-alias.ts +1 -2
  256. package/src/responses/parser.ts +3 -2
  257. package/src/responses/reasoning-replay-cache.ts +18 -14
  258. package/src/responses/schema.ts +1 -0
  259. package/src/responses/thought-signature-replay.ts +10 -2
  260. package/src/router.ts +2 -0
  261. package/src/routing/analytics.ts +2 -3
  262. package/src/routing/capability.ts +8 -2
  263. package/src/routing/fallback-policy.ts +3 -3
  264. package/src/routing/history/ingest.ts +28 -3
  265. package/src/server/auth-cors.ts +4 -2
  266. package/src/server/chat-completions.ts +21 -1
  267. package/src/server/chat-native-eligibility.ts +7 -0
  268. package/src/server/chat-native-reasoning.ts +24 -7
  269. package/src/server/chat-native.ts +13 -2
  270. package/src/server/claude-messages.ts +3 -0
  271. package/src/server/direct-local-http.ts +5 -2
  272. package/src/server/effort-policy.ts +5 -1
  273. package/src/server/index/link-relay-sessions.ts +9 -5
  274. package/src/server/index/live-sideband.ts +18 -15
  275. package/src/server/index/optional-listeners.ts +4 -0
  276. package/src/server/index/serve-options.ts +5 -4
  277. package/src/server/local-management-read-client.ts +3 -1
  278. package/src/server/management/agent-settings-routes.ts +27 -15
  279. package/src/server/management/combo-routes.ts +14 -4
  280. package/src/server/management/config-routes.ts +2 -2
  281. package/src/server/management/decision-discovery.ts +12 -2
  282. package/src/server/management/hermes-profile-routes.ts +156 -0
  283. package/src/server/management/integration-routes.ts +85 -24
  284. package/src/server/management/link-routes.ts +5 -2
  285. package/src/server/management/model-routes.ts +2 -2
  286. package/src/server/management/model-row-export-metadata.ts +25 -6
  287. package/src/server/management/model-rows.ts +15 -20
  288. package/src/server/management/native-integration-routes.ts +11 -8
  289. package/src/server/management/provider-routes.ts +1 -1
  290. package/src/server/management/route-registry.ts +4 -0
  291. package/src/server/management-api.ts +5 -0
  292. package/src/server/models-capabilities.ts +21 -3
  293. package/src/server/port-reclaim.ts +51 -9
  294. package/src/server/request-log-conversation.ts +49 -0
  295. package/src/server/request-log.ts +2 -0
  296. package/src/server/responses/adapter-delivery.ts +11 -1
  297. package/src/server/responses/adapter-dispatch.ts +2 -2
  298. package/src/server/responses/anthropic-thinking-recovery.ts +1 -0
  299. package/src/server/responses/codex-ws-exchange.ts +39 -9
  300. package/src/server/responses/codex-ws-session.ts +38 -4
  301. package/src/server/responses/codex-ws-wire.ts +6 -1
  302. package/src/server/responses/combo-required-tool-contract.ts +103 -0
  303. package/src/server/responses/combo-stream-preflight.ts +8 -1
  304. package/src/server/responses/compact.ts +14 -5
  305. package/src/server/responses/context-overflow.ts +8 -1
  306. package/src/server/responses/core-combo-failure.ts +10 -0
  307. package/src/server/responses/core-combo.ts +74 -44
  308. package/src/server/responses/core-lifetime.ts +5 -2
  309. package/src/server/responses/core-normalize.ts +17 -4
  310. package/src/server/responses/core-opaque-recovery.ts +1 -0
  311. package/src/server/responses/core-options.ts +7 -0
  312. package/src/server/responses/core-replay.ts +2 -1
  313. package/src/server/responses/jev-combo-choice.ts +39 -0
  314. package/src/server/responses/jev-session-pin.ts +203 -0
  315. package/src/server/responses/main-hard-lock-routing.ts +60 -44
  316. package/src/server/responses/passthrough-delivery.ts +44 -2
  317. package/src/server/responses/policy-fallback.ts +6 -1
  318. package/src/server/responses/request-compression.ts +12 -7
  319. package/src/server/responses/request-prepare.ts +32 -7
  320. package/src/server/responses/request-sidecar-auth.ts +2 -0
  321. package/src/server/responses/request-transport.ts +25 -6
  322. package/src/server/responses/response-effects.ts +22 -10
  323. package/src/server/responses/run-turn-execution.ts +2 -4
  324. package/src/server/responses/sidecar-execution.ts +13 -7
  325. package/src/server/responses-phase-inference.ts +267 -0
  326. package/src/server/sse-payload-rewrite.ts +14 -1
  327. package/src/server/stop-teardown.ts +36 -1
  328. package/src/server/ws-bridge.ts +1 -1
  329. package/src/service/definition.ts +20 -8
  330. package/src/service/desktop-supervision.d.mts +7 -4
  331. package/src/service/desktop-supervision.mjs +90 -13
  332. package/src/service/ownership-mutation-lease.mjs +21 -7
  333. package/src/types/config.ts +14 -2
  334. package/src/types/provider.ts +29 -7
  335. package/src/types/request.ts +11 -0
  336. package/src/types/wire.ts +1 -0
  337. package/src/types.ts +1 -0
  338. package/src/update/bind-probe.ts +29 -0
  339. package/src/update/codex-cli-update-launch-policy.d.mts +9 -0
  340. package/src/update/codex-cli-update-launch-policy.mjs +30 -0
  341. package/src/update/job.ts +10 -29
  342. package/src/update/npm-install-job.ps1 +173 -0
  343. package/src/update/npm-install.mjs +70 -0
  344. package/src/update/stop-contract.d.mts +1 -0
  345. package/src/update/stop-contract.mjs +14 -0
  346. package/src/update/stop-decision.d.mts +1 -1
  347. package/src/update/stop-decision.mjs +13 -3
  348. package/src/update/teardown-handover.ts +166 -0
  349. package/src/update/transactional-install.d.mts +14 -3
  350. package/src/update/transactional-install.mjs +33 -7
  351. package/src/usage/expected-prices.ts +64 -31
  352. package/src/usage/jev-stats.ts +54 -14
  353. package/src/usage/timeline.ts +19 -9
  354. package/src/vision/eligibility.ts +15 -5
  355. package/src/vision/plan.ts +2 -2
  356. package/src/web-search/loop.ts +16 -2
  357. package/gui/dist/assets/App-nZ34cR5G.js +0 -58
  358. package/gui/dist/assets/index-DGXS51sp.js +0 -94
  359. package/gui/dist/assets/index-DPeB9HpV.css +0 -1
  360. package/gui/dist/assets/quota-summary-DO1Sx5TM.js +0 -1
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Opt-in JEV session pin (#7525).
3
+ *
4
+ * A JEV Combo decides a target per request, so consecutive turns of one conversation can land on
5
+ * different providers and each switch starts with a cold provider prompt cache. With
6
+ * `sessionPin` set, a session keeps the target JEV chose for it while the session stays active.
7
+ *
8
+ * Identity. A pin is keyed by the admitted caller (the salted context principal of the matched
9
+ * data-plane key, or `loopback`), the Combo id and the explicit session lane from
10
+ * `sessionLaneIdFromRequest` (thread, session and parent-thread headers). A request without an
11
+ * explicit session identity never pins, and a body field such as `prompt_cache_key` is never an
12
+ * identity on its own.
13
+ *
14
+ * Lifetime. A pin is written only after the target JEV applied for this session completes a
15
+ * response, and it is refreshed by every later completion on that same target, including tool-loop
16
+ * continuations. It is released when the session stays idle past `ttlMs`, when any config
17
+ * generation change happens after it was written, when its target is not among the request's
18
+ * current JEV choices (disabled, removed, cooling down, quota exhausted, metered or otherwise
19
+ * ineligible), when its effort is no longer offered, when opted-in quota evidence reads
20
+ * `nearly_exhausted`, and when any target of a pinned session fails; a request that was already in
21
+ * flight when that failure was recorded cannot write the pin back. A cancelled send writes and
22
+ * releases nothing. Storage is process-local and bounded by `JEV_SESSION_PIN_CAPACITY`.
23
+ */
24
+ import type { JevCandidate, JevDecision, JevDecisionBackend } from "../../combos/jev";
25
+ import { jevQuotaSignalForTarget } from "../../combos/jev-quota";
26
+ import { JEV_QUOTA_DEFAULT_TIERS, normalizeJevQuotaTiers, type JevQuotaTiers } from "../../combos/jev-quota-config";
27
+ import type { JevSessionPinRelease } from "../../combos/jev-session-pin-config";
28
+ import type { NormalizedComboConfig } from "../../combos/types";
29
+ import { captureConfigGeneration } from "../../lib/state-store-sweeper";
30
+ import type { OcxComboDefaultEffort, OcxConfig } from "../../types";
31
+ import type { DataPlaneAdmission } from "../auth-cors";
32
+
33
+ export const JEV_SESSION_PIN_ROUTE_REASON = "jev-session-pin";
34
+ export const JEV_SESSION_PIN_CAPACITY = 512;
35
+
36
+ interface PinEntry {
37
+ targetKey: string;
38
+ effort: OcxComboDefaultEffort | null;
39
+ backend: JevDecisionBackend;
40
+ at: number;
41
+ generation: number;
42
+ }
43
+
44
+ const pins = new Map<string, PinEntry>();
45
+ /**
46
+ * Failure fence per session key. `forget()` stamps the key with a fresh sequence number; a request
47
+ * that started before that stamp may no longer write the pin back, so a concurrent completion on
48
+ * the same session cannot restore a target another request just saw fail. Bounded like the pins;
49
+ * because an evicted stamp is unknowable, a writer whose key has no stamp is also refused once any
50
+ * stamp was evicted after it started.
51
+ */
52
+ const failureFences = new Map<string, number>();
53
+ let failureSequence = 0;
54
+ /** `failureSequence` at the most recent fence eviction. */
55
+ let fenceEvictedAt = 0;
56
+
57
+ function callerNamespace(admission: DataPlaneAdmission | undefined): string {
58
+ if (!admission) return "unadmitted";
59
+ if (admission.kind === "loopback") return "loopback";
60
+ if (admission.contextPrincipalId) return `principal:${admission.contextPrincipalId}`;
61
+ return admission.kind === "configured" ? `key:${admission.keyId}` : admission.kind;
62
+ }
63
+
64
+ function write(key: string, entry: PinEntry): void {
65
+ // Insertion order is recency order; evicting from the front drops the least recent session.
66
+ pins.delete(key);
67
+ pins.set(key, entry);
68
+ while (pins.size > JEV_SESSION_PIN_CAPACITY) {
69
+ const oldest = pins.keys().next().value;
70
+ if (oldest === undefined) break;
71
+ pins.delete(oldest);
72
+ }
73
+ }
74
+
75
+ function fenceFailure(key: string): void {
76
+ failureFences.delete(key);
77
+ failureFences.set(key, ++failureSequence);
78
+ while (failureFences.size > JEV_SESSION_PIN_CAPACITY) {
79
+ const oldest = failureFences.keys().next().value;
80
+ if (oldest === undefined) break;
81
+ failureFences.delete(oldest);
82
+ fenceEvictedAt = failureSequence;
83
+ }
84
+ }
85
+
86
+ export interface JevSessionPinReuse {
87
+ /** The pinned decision to apply instead of asking the decision backend. */
88
+ decision?: JevDecision;
89
+ /** Set when a pin existed and was released instead of reused. */
90
+ released?: JevSessionPinRelease;
91
+ }
92
+
93
+ export interface JevSessionPinScope {
94
+ /**
95
+ * Reuse the session's pin against this request's current JEV choices, or report its release.
96
+ * `quota` is passed only when the Combo opted into advisory quota evidence; it is read from
97
+ * loaded snapshots exactly as the decision payload would read it.
98
+ */
99
+ reuse(
100
+ choices: readonly JevCandidate[],
101
+ quota?: { config: OcxConfig; tiers?: JevQuotaTiers },
102
+ ): JevSessionPinReuse;
103
+ /**
104
+ * Record a completed response. With the request's applied JEV decision it (re)writes the pin
105
+ * when the completed target is the decided one; without one it only refreshes a pin that
106
+ * already names the completed target.
107
+ */
108
+ complete(completedTargetKey: string, decision: JevDecision | undefined, writerGeneration: number): void;
109
+ /** Drop the session's pin after a real target failure. */
110
+ forget(): void;
111
+ }
112
+
113
+ /** The pin scope for one Combo request, or undefined when this request can never pin. */
114
+ export function jevSessionPinScope(args: {
115
+ combo: Pick<NormalizedComboConfig, "strategy" | "sessionPin">;
116
+ comboId: string;
117
+ admission: DataPlaneAdmission | undefined;
118
+ lane: string | undefined;
119
+ now?: () => number;
120
+ }): JevSessionPinScope | undefined {
121
+ const ttlMs = args.combo.sessionPin?.ttlMs;
122
+ if (args.combo.strategy !== "jev" || ttlMs === undefined || !args.lane) return undefined;
123
+ const key = `${callerNamespace(args.admission)}\u0000${args.comboId}\u0000${args.lane}`;
124
+ const now = args.now ?? Date.now;
125
+ const fence = failureFences.get(key) ?? 0;
126
+ const startedAtSequence = failureSequence;
127
+ const release = (reason: JevSessionPinRelease): JevSessionPinReuse => {
128
+ pins.delete(key);
129
+ return { released: reason };
130
+ };
131
+ return {
132
+ reuse(choices, quota) {
133
+ const entry = pins.get(key);
134
+ if (!entry) return {};
135
+ if (entry.generation !== captureConfigGeneration()) return release("config_changed");
136
+ const at = now();
137
+ if (at < entry.at || at - entry.at >= ttlMs) return release("expired");
138
+ const candidate = choices.find(choice => choice.key === entry.targetKey);
139
+ if (!candidate) return release("target_unavailable");
140
+ if (entry.effort !== null && !candidate.reasoningEfforts.includes(entry.effort)) {
141
+ return release("effort_unavailable");
142
+ }
143
+ if (quota && candidate.quotaIdentity !== null) {
144
+ const identity = candidate.quotaIdentity ?? candidate;
145
+ const tiers = normalizeJevQuotaTiers(quota.tiers) ?? JEV_QUOTA_DEFAULT_TIERS;
146
+ const signal = jevQuotaSignalForTarget(quota.config, identity.provider, identity.model, at, tiers);
147
+ if (signal.tier === "nearly_exhausted") return release("quota_nearly_exhausted");
148
+ }
149
+ return {
150
+ decision: {
151
+ backend: entry.backend,
152
+ targetKey: entry.targetKey,
153
+ effort: entry.effort,
154
+ gate: "apply",
155
+ latencyMs: 0,
156
+ pinned: true,
157
+ pinReason: "session",
158
+ },
159
+ };
160
+ },
161
+ complete(completedTargetKey, decision, writerGeneration) {
162
+ // A config change while the response was in flight makes this completion obsolete.
163
+ if (writerGeneration !== captureConfigGeneration()) return;
164
+ // So does a failure on this session reported after this request started.
165
+ const currentFence = failureFences.get(key);
166
+ if ((currentFence ?? 0) !== fence) return;
167
+ if (currentFence === undefined && fenceEvictedAt > startedAtSequence) return;
168
+ if (!decision) {
169
+ const entry = pins.get(key);
170
+ if (entry && entry.targetKey === completedTargetKey && entry.generation === writerGeneration) {
171
+ write(key, { ...entry, at: now() });
172
+ }
173
+ return;
174
+ }
175
+ // Only an applied decision pins; a fail-open fallback leaves the next turn to decide again.
176
+ if (decision.gate !== "apply" || decision.targetKey !== completedTargetKey) return;
177
+ write(key, {
178
+ targetKey: decision.targetKey,
179
+ effort: decision.effort,
180
+ backend: decision.backend,
181
+ at: now(),
182
+ generation: writerGeneration,
183
+ });
184
+ },
185
+ forget() {
186
+ pins.delete(key);
187
+ fenceFailure(key);
188
+ },
189
+ };
190
+ }
191
+
192
+ /** Test-only reset, alongside the Combo recall and rotation resets. */
193
+ export function clearJevSessionPinsForTests(): void {
194
+ pins.clear();
195
+ failureFences.clear();
196
+ failureSequence = 0;
197
+ fenceEvictedAt = 0;
198
+ }
199
+
200
+ /** Test-only view of the retained pin count. */
201
+ export function jevSessionPinCountForTests(): number {
202
+ return pins.size;
203
+ }
@@ -1,8 +1,8 @@
1
1
  import type { OcxConfig } from "../../types";
2
- import { resolveComboId } from "../../combos";
2
+ import { getCombo, resolveComboId } from "../../combos";
3
3
  import { isBareOpenAiFamilyModel, previewRouteModel, routeConcreteModel } from "../../router";
4
4
  import { resolveChosenTarget } from "./shadow-target-availability";
5
- import { getRoutingProfile, resolvePolicyProfileId, type NormalizedRoutingProfile } from "../../routing/profile";
5
+ import { getRoutingProfile, resolvePolicyProfileId } from "../../routing/profile";
6
6
  import { NATIVE_RESERVE_MODEL } from "../../codex/catalog/native-models";
7
7
  import {
8
8
  getMainAccountHardLockStatus,
@@ -12,7 +12,10 @@ import { codexQuotaScopeForModel } from "../../codex/routing/health-store";
12
12
  import { getEligiblePoolAccounts } from "../../codex/routing/selection";
13
13
  import { OPENAI_CODEX_PROVIDER_ID, isCanonicalOpenAiForwardProvider } from "../../providers/openai-tiers";
14
14
  import { providerCodexAccountMode } from "../../providers/registry";
15
- import { FallbackRefusedError, guardFallbackHop, type FallbackAdmission, type FallbackTarget } from "../../routing/fallback-policy";
15
+ import {
16
+ FallbackRefusedError, fallbackDeviceIds, fallbackHopKind, guardFallbackHop, resolveFallbackPolicy,
17
+ type FallbackAdmission, type FallbackHop, type FallbackTarget,
18
+ } from "../../routing/fallback-policy";
16
19
  /** Resolves to a synchronous ownership proof, or null when the caller does not own the locked main. */
17
20
  export type MainHardLockCallerCheck = (sourceModel: string) => Promise<(() => boolean) | null>;
18
21
 
@@ -111,8 +114,26 @@ export async function applyMainHardLockFallbackOverride(
111
114
  // Combos and policies keep their own dispatch admission and declared failover/evidence.
112
115
  // Other chosen targets must resolve now; a removed qualifier cannot become a default send.
113
116
  const policyId = resolvePolicyProfileId(config, fallback.model);
114
- if (!isConfiguredComboSelector(config, fallback.model)
115
- && !(policyId && getRoutingProfile(config, policyId))) {
117
+ const profile = policyId ? getRoutingProfile(config, policyId) : undefined;
118
+ const comboId = resolveComboId(config, fallback.model);
119
+ const combo = comboId ? getCombo(config, comboId) : undefined;
120
+ if (profile || combo) {
121
+ // Match the actual first guard's selector order. This neither picks nor filters a target.
122
+ const denied = allDeclaredTargetsDenied(config, profile?.candidates ?? combo!.targets, {
123
+ site: "hard-lock",
124
+ route: profile ? parsed.model : `combo/${comboId}`,
125
+ aliases: profile ? [`policy/${policyId}`] : [parsed.model],
126
+ from: { provider: OPENAI_CODEX_PROVIDER_ID, model: parsed.model },
127
+ trigger: "hard_lock", admission, ...(now !== undefined ? { now } : {}),
128
+ });
129
+ if (denied) {
130
+ // One refusal witness for an all-denied declaration, never a speculative taken event.
131
+ const hop = guardFallbackHop(config, denied);
132
+ if (!hop.ok) throw new FallbackRefusedError(hop.response);
133
+ }
134
+ // Mixed or unresolved declarations grant nothing: actual first/retry guards still decide.
135
+ // A selector rewrite is not a selected hop and reserves no send or taken event here.
136
+ } else {
116
137
  try {
117
138
  const target = resolveChosenTarget(fallback.model, model => previewRouteModel(config, model));
118
139
  if ("unavailable" in target) return null;
@@ -120,23 +141,18 @@ export async function applyMainHardLockFallbackOverride(
120
141
  // An unavailable target keeps the source lock's refusal without touching the body.
121
142
  return null;
122
143
  }
144
+ // Concrete targets keep their existing proactive admission at body rewrite.
145
+ const hop = guardFallbackHop(config, {
146
+ site: "hard-lock",
147
+ route: parsed.model,
148
+ from: { provider: OPENAI_CODEX_PROVIDER_ID, model: parsed.model },
149
+ to: hardLockTargetOf(config, fallback.model),
150
+ trigger: "hard_lock", admission, ...(now !== undefined ? { now } : {}),
151
+ });
152
+ if (!hop.ok) throw new FallbackRefusedError(hop.response);
153
+ hop.commit();
123
154
  }
124
155
 
125
- // OCX-11: the reroute is a proactive model hop; refused, the request is answered with the
126
- // fallback refusal and the body is left untouched.
127
- const hop = guardFallbackHop(config, {
128
- site: "hard-lock",
129
- route: parsed.model,
130
- from: { provider: OPENAI_CODEX_PROVIDER_ID, model: parsed.model },
131
- to: hardLockTargetOf(config, parsed.model, fallback.model),
132
- trigger: "hard_lock",
133
- admission,
134
- ...(now !== undefined ? { now } : {}),
135
- });
136
- if (!hop.ok) throw new FallbackRefusedError(hop.response);
137
- // The rewrite is this site's admission: the body now names the fallback model.
138
- hop.commit();
139
-
140
156
  parsed.raw.model = fallback.model;
141
157
  if (fallback.reasoningEffort !== undefined) {
142
158
  const reasoning = parsed.raw.reasoning;
@@ -151,17 +167,31 @@ export async function applyMainHardLockFallbackOverride(
151
167
  }
152
168
 
153
169
  /**
154
- * The hop's destination for the fallback policy, read from config only. A routing profile picks
155
- * its candidate from request-history evidence, and evaluating it here would open that index under
156
- * OPENCODEX_HOME just to classify the hop (on Windows the open handle then outlives the request's
157
- * home); the profile's own dispatch still evaluates it. Its declared candidates are classified
158
- * instead, as the hop the profile could take: a candidate on another provider makes it a provider
159
- * hop. Combos, account namespaces and concrete targets resolve from config alone.
170
+ * Pure policy feasibility over declared concrete destinations. Unknown resolution is not denial.
171
+ * No picker, history, cooldown, event or send-budget operation runs for individual candidates.
172
+ * The first denied hop is only a refusal witness once every declaration is known denied.
160
173
  */
161
- function hardLockTargetOf(config: OcxConfig, sourceModel: string, model: string): FallbackTarget {
162
- const policyId = resolvePolicyProfileId(config, model);
163
- const profile = policyId ? getRoutingProfile(config, policyId) : undefined;
164
- if (profile) return profileHopTarget(config, sourceModel, profile);
174
+ function allDeclaredTargetsDenied(
175
+ config: OcxConfig, targets: readonly FallbackTarget[], hop: Omit<FallbackHop, "to">,
176
+ ): FallbackHop | undefined {
177
+ const deviceIds = fallbackDeviceIds(config, hop.admission, hop.now);
178
+ const routes = [hop.route, ...(hop.aliases ?? [])];
179
+ let witness: FallbackHop | undefined;
180
+ for (const target of targets) {
181
+ let to: FallbackTarget;
182
+ try {
183
+ const route = routeConcreteModel(config, `${target.provider}/${target.model}`);
184
+ to = { provider: route.providerName, model: route.modelId };
185
+ } catch { return undefined; }
186
+ const kind = fallbackHopKind(hop.from, to);
187
+ if (kind === null || resolveFallbackPolicy(config, { kind, routes, provider: hop.from.provider, deviceIds }).allowed) return undefined;
188
+ witness ??= { ...hop, to };
189
+ }
190
+ return witness;
191
+ }
192
+
193
+ /** Concrete and account-qualified targets retain the existing config-only resolution. */
194
+ function hardLockTargetOf(config: OcxConfig, model: string): FallbackTarget {
165
195
  try {
166
196
  const route = previewRouteModel(config, model);
167
197
  return { provider: route.providerName, model: route.modelId };
@@ -170,17 +200,3 @@ function hardLockTargetOf(config: OcxConfig, sourceModel: string, model: string)
170
200
  return slash > 0 ? { provider: model.slice(0, slash), model: model.slice(slash + 1) } : { provider: "", model };
171
201
  }
172
202
  }
173
-
174
- function profileHopTarget(config: OcxConfig, sourceModel: string, profile: NormalizedRoutingProfile): FallbackTarget {
175
- const targets = profile.candidates.map((candidate): FallbackTarget => {
176
- try {
177
- const route = routeConcreteModel(config, candidate.provider + "/" + candidate.model);
178
- return { provider: route.providerName, model: route.modelId };
179
- } catch {
180
- return { provider: candidate.provider, model: candidate.model };
181
- }
182
- });
183
- return targets.find(target => target.provider !== OPENAI_CODEX_PROVIDER_ID)
184
- ?? targets.find(target => target.model !== sourceModel)
185
- ?? { provider: OPENAI_CODEX_PROVIDER_ID, model: sourceModel };
186
- }
@@ -1,5 +1,7 @@
1
1
  import { wasPermanentAccountRefusal } from "../../oauth/account-auto-pause";
2
2
  import { isNativeControlResponse } from "./native-response-control";
3
+ import { guardBufferedComboRequiredTool } from "./combo-required-tool-contract";
4
+ import { attachResponseToolOwnership } from "./response-tool-ownership";
3
5
  import { createOutboundCredentialMask, createTerminalErrorRedactionBlockRewrite } from "./terminal-error-redaction";
4
6
  import type { ResponsesRequestContext, ResponsesAdmissionState } from "./core-options";
5
7
  import type { PreparedResponsesRequest } from "./request-prepare";
@@ -80,6 +82,7 @@ import {
80
82
  createResponsesItemIdPayloadRewrite,
81
83
  repairResponsesJsonItemIds,
82
84
  } from "../responses-item-id-repair";
85
+ import { createResponsesPhaseInferenceBlockRewrite } from "../responses-phase-inference";
83
86
  import {
84
87
  payloadRewriteAsBlockRewrite,
85
88
  composeSsePayloadRewrites,
@@ -147,6 +150,8 @@ import {
147
150
  inspectResponseLogJson,
148
151
  inspectResponseLogSsePayloadParsed,
149
152
  noteUpstreamRequestId,
153
+ recordKeyWireAttemptUsage,
154
+ usageFromResponsesPayload,
150
155
  } from "../request-log";
151
156
  import { restoreRoutedCustomCallsInJson } from "../../responses/custom-tool-compat";
152
157
  import { restoreRoutedToolSearchCallsInJson } from "../../responses/tool-search-compat";
@@ -446,6 +451,15 @@ export async function deliverPassthroughResponse(
446
451
  const { requestBindings } = transportState;
447
452
 
448
453
  let upstreamResponse = nativeExchange.upstreamResponse;
454
+ // Judge the restored native payload before display projection can erase hosted-call evidence.
455
+ const requiredToolFailure = async (text: string): Promise<Response | undefined> => {
456
+ if (!options.comboAttempt || clientRequestedStream === true || isNonReplayableResponse(upstreamResponse)) return;
457
+ const candidate = attachResponseToolOwnership(new Response(text, {
458
+ status: upstreamResponse.status, headers: { "content-type": "application/json" },
459
+ }), providerExecutedCallTypes);
460
+ const verdict = await guardBufferedComboRequiredTool(candidate, parsed._rawBody, route.provider.adapter);
461
+ return verdict.ok ? undefined : verdict;
462
+ };
449
463
  const canonicalBufferedJson = clientRequestedStream !== true
450
464
  && isCanonicalOpenAiForwardProvider(route.provider);
451
465
  const originalContentType = upstreamResponse.headers.get("content-type");
@@ -851,6 +865,9 @@ export async function deliverPassthroughResponse(
851
865
  snapshotRepairEnabled
852
866
  ? createResponsesSnapshotBlockRewrite(nativeExchange.outboundRequestBody, translatorBudget)
853
867
  : undefined,
868
+ route.provider.messagePhaseInference === true
869
+ ? createResponsesPhaseInferenceBlockRewrite(translatorBudget)
870
+ : undefined,
854
871
  responseEffects.plaintextV2AgentMessageToolNames.size > 0
855
872
  ? payloadRewriteAsBlockRewrite(createPlaintextV2AgentMessageCallRestoreRewrite(
856
873
  responseEffects.plaintextV2AgentMessageToolNames, responseEffects.plaintextV2AgentMessageAliasedToolNames,
@@ -883,7 +900,7 @@ export async function deliverPassthroughResponse(
883
900
  createTerminalErrorRedactionBlockRewrite(nativeExchange.request.headers, maskCredential),
884
901
  rememberPlaintextBlock,
885
902
  // Display-only projection must follow every continuation-cache observer.
886
- isLocalCodexImageClient(req.headers, options.admission?.kind, options.inboundWire)
903
+ (!canonicalBufferedJson || !options.comboAttempt) && isLocalCodexImageClient(req.headers, options.admission?.kind, options.inboundWire)
887
904
  ? createHostedImageDisplayRewrite()
888
905
  : undefined,
889
906
  ].filter((rewrite): rewrite is NonNullable<typeof rewrite> => rewrite !== undefined);
@@ -1026,6 +1043,29 @@ export async function deliverPassthroughResponse(
1026
1043
  };
1027
1044
  if (signal.aborted) return cancelAfterValidation();
1028
1045
 
1046
+ const requiredFailure = await requiredToolFailure(JSON.stringify(client.terminal.response));
1047
+ if (requiredFailure) {
1048
+ const usage = usageFromResponsesPayload(client.terminal.response.usage);
1049
+ if (usage && !logCtx.usageFromBridge) {
1050
+ if (!recordKeyWireAttemptUsage(logCtx, usage)) {
1051
+ logCtx.usage = usage;
1052
+ if (logCtx.activeAttempt) logCtx.activeAttempt.usage = usage;
1053
+ }
1054
+ logCtx.usageWireParsed = true;
1055
+ }
1056
+ return requiredFailure;
1057
+ }
1058
+ if (signal.aborted) return cancelAfterValidation();
1059
+ let clientJson = JSON.stringify(client.terminal.response);
1060
+ if (options.comboAttempt && isLocalCodexImageClient(req.headers, options.admission?.kind, options.inboundWire)) {
1061
+ const display = createHostedImageDisplayRewrite();
1062
+ try { clientJson = display.json(clientJson); }
1063
+ catch (error) {
1064
+ if (error instanceof RangeError) return formatErrorResponse(502, "upstream_error", "hosted image result exceeds local display limits");
1065
+ throw error;
1066
+ } finally { display.dispose?.(); }
1067
+ }
1068
+
1029
1069
  // Effects intentionally run only after both bounded validations. They inspect the same
1030
1070
  // upstream-facing transcript as the streaming relay and fire once; continuation storage
1031
1071
  // receives the final client-visible response once after all restorations succeeded.
@@ -1082,7 +1122,7 @@ export async function deliverPassthroughResponse(
1082
1122
 
1083
1123
  const jsonHeaders = sanitizePassthroughHeaders(headers, codexSafetyBufferingOptions);
1084
1124
  jsonHeaders.set("content-type", "application/json");
1085
- return markPreinspectedJsonResponse(new Response(JSON.stringify(client.terminal.response), {
1125
+ return markPreinspectedJsonResponse(new Response(clientJson, {
1086
1126
  status: upstreamResponse.status,
1087
1127
  statusText: upstreamResponse.statusText,
1088
1128
  headers: jsonHeaders,
@@ -1361,6 +1401,8 @@ export async function deliverPassthroughResponse(
1361
1401
  declaredBareWireToolNames,
1362
1402
  );
1363
1403
  }
1404
+ const requiredFailure = await requiredToolFailure(clientJson);
1405
+ if (requiredFailure) return requiredFailure;
1364
1406
  commitReasoningReplayServingRoute(nativeExchange.request.headers);
1365
1407
  try {
1366
1408
  rememberPassthroughResponseChecked(
@@ -1,5 +1,6 @@
1
1
  import { comboFailureDecision } from "../../combos/failover";
2
2
  import { readBoundedResponseBody } from "../../lib/bounded-body";
3
+ import { isVerifiedModelNotInPlanResponse } from "../../lib/errors";
3
4
  import { isNonReplayableResponse } from "../../lib/upstream-retry";
4
5
  import { finishRequestAttempt, type RequestLogContext } from "../request-log";
5
6
  import { linkRequestSessionLane } from "../request-log-conversation";
@@ -130,7 +131,11 @@ async function shouldHopPolicyCandidate(response: Response, signal?: AbortSignal
130
131
  try {
131
132
  const inspected = await readBoundedResponseBody(response.clone(), { signal });
132
133
  const text = inspected.displaySafe && !inspected.truncated ? inspected.text : "";
133
- return comboFailureDecision(response.status, text, { code: errorCodeFromText(text) }) === "hop";
134
+ return comboFailureDecision(response.status, text, {
135
+ code: errorCodeFromText(text),
136
+ // A passthrough candidate returns its upstream body unchanged, so the code is not provenance.
137
+ verifiedPlanRefusal: isVerifiedModelNotInPlanResponse(response),
138
+ }) === "hop";
134
139
  } catch {
135
140
  return false;
136
141
  }
@@ -1,5 +1,5 @@
1
1
  import { Buffer } from "node:buffer";
2
- import { constants, gzipSync, zstdCompressSync } from "node:zlib";
2
+ import { constants, zstdCompressSync } from "node:zlib";
3
3
  import type { OcxConfig } from "../../types";
4
4
 
5
5
  /**
@@ -49,13 +49,18 @@ export function currentUpstreamRequestCompression(): UpstreamRequestCompression
49
49
  return resolveUpstreamRequestCompression(boundConfig?.upstreamRequestCompression);
50
50
  }
51
51
 
52
- // ArrayBuffer-backed, which is what node:zlib returns and what a fetch BodyInit accepts.
52
+ // ArrayBuffer-backed bytes, accepted by fetch without another input conversion.
53
53
  type CodedBytes = Buffer<ArrayBuffer>;
54
- type Compressor = (mode: Exclude<UpstreamRequestCompression, "off">, bytes: Buffer) => CodedBytes;
54
+ type Compressor = (mode: Exclude<UpstreamRequestCompression, "off">, body: string) => CodedBytes;
55
55
 
56
- const defaultCompressor: Compressor = (mode, bytes) => mode === "gzip"
57
- ? gzipSync(bytes, { level: 1 }) as CodedBytes
58
- : zstdCompressSync(bytes, { params: { [constants.ZSTD_c_compressionLevel]: 3 } }) as CodedBytes;
56
+ const defaultCompressor: Compressor = (mode, body) => {
57
+ if (mode === "gzip") {
58
+ // Native string input avoids a full-body UTF-8 Buffer; only view the coded output as a Buffer.
59
+ const bytes = Bun.gzipSync(body, { level: 1 });
60
+ return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
61
+ }
62
+ return zstdCompressSync(Buffer.from(body, "utf8"), { params: { [constants.ZSTD_c_compressionLevel]: 3 } }) as CodedBytes;
63
+ };
59
64
 
60
65
  let compressor: Compressor = defaultCompressor;
61
66
  let maxBodyBytes = MAX_COMPRESSIBLE_BODY_BYTES;
@@ -90,7 +95,7 @@ export function compressUpstreamRequestBody(
90
95
  const size = Buffer.byteLength(body, "utf8");
91
96
  if (size < MIN_COMPRESSIBLE_BODY_BYTES || size > maxBodyBytes) return null;
92
97
  try {
93
- return { body: compressor(mode, Buffer.from(body, "utf8")), encoding: mode };
98
+ return { body: compressor(mode, body), encoding: mode };
94
99
  } catch {
95
100
  // Deliberately silent: the error could only describe the body, and identity is still correct.
96
101
  return null;
@@ -30,6 +30,9 @@ import { recallComboForLane } from "./combo-session-recall";
30
30
  import {
31
31
  sessionLaneIdFromRequest,
32
32
  conversationIdFromResponsesRequest,
33
+ assignNativeLogConversationId,
34
+ fillHermesLogConversationFallback,
35
+ nativeLogConversationIdFromHeaders,
33
36
  sessionIdHeaderFromRequest,
34
37
  reasoningReplayConversationIdFromResponsesRequest,
35
38
  } from "../request-log-conversation";
@@ -160,7 +163,7 @@ import {
160
163
  import { acquireUpstreamHostAdmission } from "../../codex/upstream-host-health";
161
164
  import { applyCompactionRoutingOverride, compactionRoutingKeepsProviderIdentity } from "./compaction-routing";
162
165
  import { applyMainHardLockFallbackOverride } from "./main-hard-lock-routing";
163
- import { FallbackRefusedError } from "../../routing/fallback-policy";
166
+ import { FallbackRefusedError, guardFallbackHop } from "../../routing/fallback-policy";
164
167
  import { codexAuthContextLogLabel } from "../../codex/account-label";
165
168
  import {
166
169
  conversationStateBindingFromAuth,
@@ -603,14 +606,16 @@ export async function prepareResponsesRequest(
603
606
  // would become enough to read another caller's retained search result. Without a principal
604
607
  // bridgeSearchReplayScope yields no scope, so nothing is recorded or restored for it. The
605
608
  // field is always rewritten so an absent principal also clears one a reused holder carried.
606
- const clientPrincipalId = resolveContextPrincipal(req, config, options.admission);
609
+ const clientPrincipalId = options.replayPrincipal
610
+ ? options.replayPrincipal.clientPrincipalId
611
+ : resolveContextPrincipal(req, config, options.admission);
607
612
  parsed._reasoningReplayScope = { ...parsed._reasoningReplayScope, clientPrincipalId };
608
613
  }
609
614
  // Prefer a pre-populated id (routed Claude) over Responses headers that may be
610
- // absent or synthetically injected (session_id from prompt_cache_key).
611
- if (!logCtx.conversationId) {
612
- logCtx.conversationId = resolvedConversationId;
613
- }
615
+ // absent or synthetically injected (session_id from prompt_cache_key). A Hermes-only id
616
+ // is a log fallback: native resolution replaces it, and it only fills an empty slot.
617
+ assignNativeLogConversationId(logCtx, resolvedConversationId ?? nativeLogConversationIdFromHeaders(req.headers));
618
+ fillHermesLogConversationFallback(logCtx, req.headers);
614
619
  // A rewritten turn logs the selector the client asked for, so the usage log never
615
620
  // claims the operator typed the fallback model themselves.
616
621
  logCtx.requestedModel = options.mainHardLockFallbackOverride?.sourceModel
@@ -660,7 +665,10 @@ export async function prepareResponsesRequest(
660
665
  if (candidate.combo?.target.metered && !admissionMayUseMeteredComboTargets(
661
666
  config, options.admission, config.combos?.[candidate.combo.comboId],
662
667
  )) {
663
- const pick = pickComboTarget(config, candidate.combo.comboId, { eligible: target => !target.metered });
668
+ const pick = pickComboTarget(config, candidate.combo.comboId, {
669
+ eligible: target => !target.metered,
670
+ allowCooldownReprobe: false,
671
+ });
664
672
  if (!pick) {
665
673
  throw new NoAvailableComboTargetsError(candidate.combo.comboId,
666
674
  comboExclusionReasons(config, candidate.combo.comboId, target => !target.metered));
@@ -773,6 +781,23 @@ export async function prepareResponsesRequest(
773
781
  if (parsed._compactionRequest === true || options.compactionRoutingOverride) parsed._cursorIsolateConversation = true;
774
782
  route = memoryRoute ?? shadowRoute ?? resolveRoute(options.policyFallbackCandidate
775
783
  ? `${options.policyFallbackCandidate.provider}/${options.policyFallbackCandidate.model}` : parsed.modelId);
784
+ // Selection-free hard-lock preflight grants no hop. Enforce the actual ranked destination
785
+ // against the locked source before admitting this first selection.
786
+ const hardLockProfileTrace = options.mainHardLockFallbackOverride ? route.routeDecision?.profile : undefined;
787
+ if (hardLockProfileTrace && options.mainHardLockFallbackOverride) {
788
+ const override = options.mainHardLockFallbackOverride;
789
+ const hop = guardFallbackHop(config, {
790
+ site: "routing-profile",
791
+ route: override.sourceModel,
792
+ aliases: [`policy/${hardLockProfileTrace.id}`],
793
+ from: { provider: OPENAI_CODEX_PROVIDER_ID, model: override.sourceModel },
794
+ to: { provider: route.providerName, model: route.modelId },
795
+ trigger: "hard_lock",
796
+ admission: options.admission,
797
+ });
798
+ if (!hop.ok) return hop.response;
799
+ hop.commit();
800
+ }
776
801
  leaveDirectLaneIfRerouted(options, route, parsed, logCtx);
777
802
  // Name the phase in the persisted route decision, so the request log says why this turn went to
778
803
  // the memory destination instead of leaving it looking like a plain user selection. Set here, on
@@ -52,6 +52,7 @@ export async function prepareResponsesSidecarAuth(
52
52
  const visionDescribeTerminal = options.visionDescribeTerminal === true;
53
53
  const routedCompaction = parsed._compactionRequest === true
54
54
  && (!supportsNativeResponsesCompactionTrigger(route.provider) || parsed._portableCompaction === true);
55
+ const preserveCompactionPrefix = routedCompaction && transportState.adapter.name === "anthropic";
55
56
  if (routedCompaction) parsed.context.messages = omitEarlierCompactionImages(parsed.context.messages);
56
57
  const needsOpenAiVision = !visionDescribeTerminal
57
58
  && shouldResolveOpenAiVisionSidecar(config, route.provider, route.modelId, parsed, route.providerName);
@@ -158,6 +159,7 @@ export async function prepareResponsesSidecarAuth(
158
159
  return {
159
160
  openAiSidecar,
160
161
  routedCompaction,
162
+ preserveCompactionPrefix,
161
163
  };
162
164
  }
163
165