@bitkyc08/opencodex 2.96.0 → 2.98.0-preview.20261107

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 (465) hide show
  1. package/bin/ocx.mjs +215 -20
  2. package/gui/dist/assets/App-6TFtZsUF.css +1 -0
  3. package/gui/dist/assets/App-B8_sOxoz.js +59 -0
  4. package/gui/dist/assets/Tray-DpWEp6ee.js +1 -0
  5. package/gui/dist/assets/WantedSansVariable.split.0-Dwvb1WRx.woff2 +0 -0
  6. package/gui/dist/assets/WantedSansVariable.split.1-1wKD2RxI.woff2 +0 -0
  7. package/gui/dist/assets/WantedSansVariable.split.10-CAKA6LIP.woff2 +0 -0
  8. package/gui/dist/assets/WantedSansVariable.split.11-k5H85R4b.woff2 +0 -0
  9. package/gui/dist/assets/WantedSansVariable.split.12-BHboLsUv.woff2 +0 -0
  10. package/gui/dist/assets/WantedSansVariable.split.13-9GV377ix.woff2 +0 -0
  11. package/gui/dist/assets/WantedSansVariable.split.14-DovDXnrj.woff2 +0 -0
  12. package/gui/dist/assets/WantedSansVariable.split.15-DTdykCig.woff2 +0 -0
  13. package/gui/dist/assets/WantedSansVariable.split.16-CKz2ccgK.woff2 +0 -0
  14. package/gui/dist/assets/WantedSansVariable.split.17-Z2RD_KRZ.woff2 +0 -0
  15. package/gui/dist/assets/WantedSansVariable.split.18-BcKtfonJ.woff2 +0 -0
  16. package/gui/dist/assets/WantedSansVariable.split.19-DEQPOSZn.woff2 +0 -0
  17. package/gui/dist/assets/WantedSansVariable.split.2-DryboyuA.woff2 +0 -0
  18. package/gui/dist/assets/WantedSansVariable.split.20-Bia5JMls.woff2 +0 -0
  19. package/gui/dist/assets/WantedSansVariable.split.21-Cr4jFw-E.woff2 +0 -0
  20. package/gui/dist/assets/WantedSansVariable.split.22-DjjB69TZ.woff2 +0 -0
  21. package/gui/dist/assets/WantedSansVariable.split.23-CvUzJskt.woff2 +0 -0
  22. package/gui/dist/assets/WantedSansVariable.split.24-i_eMg5ox.woff2 +0 -0
  23. package/gui/dist/assets/WantedSansVariable.split.25-2aKHwPX-.woff2 +0 -0
  24. package/gui/dist/assets/WantedSansVariable.split.26-B6wXoCEL.woff2 +0 -0
  25. package/gui/dist/assets/WantedSansVariable.split.27-D4h1WfGM.woff2 +0 -0
  26. package/gui/dist/assets/WantedSansVariable.split.28-kvRPCStd.woff2 +0 -0
  27. package/gui/dist/assets/WantedSansVariable.split.29-DHAiwleD.woff2 +0 -0
  28. package/gui/dist/assets/WantedSansVariable.split.3-7H--eyZi.woff2 +0 -0
  29. package/gui/dist/assets/WantedSansVariable.split.30-C66cfAY2.woff2 +0 -0
  30. package/gui/dist/assets/WantedSansVariable.split.31-FDmtiNim.woff2 +0 -0
  31. package/gui/dist/assets/WantedSansVariable.split.32-DouHt2Dd.woff2 +0 -0
  32. package/gui/dist/assets/WantedSansVariable.split.33-B8HVvkJX.woff2 +0 -0
  33. package/gui/dist/assets/WantedSansVariable.split.34-BtkRu1FN.woff2 +0 -0
  34. package/gui/dist/assets/WantedSansVariable.split.35-DBylQbtk.woff2 +0 -0
  35. package/gui/dist/assets/WantedSansVariable.split.36-Sg089Kgh.woff2 +0 -0
  36. package/gui/dist/assets/WantedSansVariable.split.37-CYtvbriS.woff2 +0 -0
  37. package/gui/dist/assets/WantedSansVariable.split.38-DKkSLf8Z.woff2 +0 -0
  38. package/gui/dist/assets/WantedSansVariable.split.39-CecotKZD.woff2 +0 -0
  39. package/gui/dist/assets/WantedSansVariable.split.4-Bv8O3ZMb.woff2 +0 -0
  40. package/gui/dist/assets/WantedSansVariable.split.40-DdCvkmOU.woff2 +0 -0
  41. package/gui/dist/assets/WantedSansVariable.split.41-ESn674Hc.woff2 +0 -0
  42. package/gui/dist/assets/WantedSansVariable.split.42-DL9V7NNV.woff2 +0 -0
  43. package/gui/dist/assets/WantedSansVariable.split.43-yipO0cyk.woff2 +0 -0
  44. package/gui/dist/assets/WantedSansVariable.split.44-6rTmLZhm.woff2 +0 -0
  45. package/gui/dist/assets/WantedSansVariable.split.45-BJAj8lrm.woff2 +0 -0
  46. package/gui/dist/assets/WantedSansVariable.split.46-BDTzMn2c.woff2 +0 -0
  47. package/gui/dist/assets/WantedSansVariable.split.47-Bxd4wGgi.woff2 +0 -0
  48. package/gui/dist/assets/WantedSansVariable.split.48-DxnFBLnH.woff2 +0 -0
  49. package/gui/dist/assets/WantedSansVariable.split.49-DnVD0aOD.woff2 +0 -0
  50. package/gui/dist/assets/WantedSansVariable.split.5-B1wI2pp5.woff2 +0 -0
  51. package/gui/dist/assets/WantedSansVariable.split.50-D9JlkZd6.woff2 +0 -0
  52. package/gui/dist/assets/WantedSansVariable.split.51-D9Vw6Bu3.woff2 +0 -0
  53. package/gui/dist/assets/WantedSansVariable.split.52-ClEJk4kn.woff2 +0 -0
  54. package/gui/dist/assets/WantedSansVariable.split.53-BSXU0vh-.woff2 +0 -0
  55. package/gui/dist/assets/WantedSansVariable.split.54-EcuxkR9v.woff2 +0 -0
  56. package/gui/dist/assets/WantedSansVariable.split.55-CYiFhQX8.woff2 +0 -0
  57. package/gui/dist/assets/WantedSansVariable.split.56--Gk9g8BK.woff2 +0 -0
  58. package/gui/dist/assets/WantedSansVariable.split.57-DnkOPZcy.woff2 +0 -0
  59. package/gui/dist/assets/WantedSansVariable.split.58-9MB8504z.woff2 +0 -0
  60. package/gui/dist/assets/WantedSansVariable.split.59-ByxIsN72.woff2 +0 -0
  61. package/gui/dist/assets/WantedSansVariable.split.6-Bmk0dRVw.woff2 +0 -0
  62. package/gui/dist/assets/WantedSansVariable.split.60-Bxwx_aWQ.woff2 +0 -0
  63. package/gui/dist/assets/WantedSansVariable.split.61-DA2z5sm7.woff2 +0 -0
  64. package/gui/dist/assets/WantedSansVariable.split.62-C5iJPJZn.woff2 +0 -0
  65. package/gui/dist/assets/WantedSansVariable.split.63-DAqrmaEc.woff2 +0 -0
  66. package/gui/dist/assets/WantedSansVariable.split.64-BgXFsljb.woff2 +0 -0
  67. package/gui/dist/assets/WantedSansVariable.split.65-DKiWujvu.woff2 +0 -0
  68. package/gui/dist/assets/WantedSansVariable.split.66-8po4vUZ4.woff2 +0 -0
  69. package/gui/dist/assets/WantedSansVariable.split.67-Cy5qyI9O.woff2 +0 -0
  70. package/gui/dist/assets/WantedSansVariable.split.68-BKny7Fy3.woff2 +0 -0
  71. package/gui/dist/assets/WantedSansVariable.split.69-B4VU4u_b.woff2 +0 -0
  72. package/gui/dist/assets/WantedSansVariable.split.7-BuqwNpgA.woff2 +0 -0
  73. package/gui/dist/assets/WantedSansVariable.split.70-BCd4n346.woff2 +0 -0
  74. package/gui/dist/assets/WantedSansVariable.split.71-CkQppaWn.woff2 +0 -0
  75. package/gui/dist/assets/WantedSansVariable.split.72-Db7vaoFs.woff2 +0 -0
  76. package/gui/dist/assets/WantedSansVariable.split.73-DI80TYWl.woff2 +0 -0
  77. package/gui/dist/assets/WantedSansVariable.split.74-tMCtDAHB.woff2 +0 -0
  78. package/gui/dist/assets/WantedSansVariable.split.75-Ciw0D5WG.woff2 +0 -0
  79. package/gui/dist/assets/WantedSansVariable.split.76-DoLjFgp-.woff2 +0 -0
  80. package/gui/dist/assets/WantedSansVariable.split.77-CpT2uvMy.woff2 +0 -0
  81. package/gui/dist/assets/WantedSansVariable.split.78-CkcJ4iZ1.woff2 +0 -0
  82. package/gui/dist/assets/WantedSansVariable.split.79-DsOUJjEM.woff2 +0 -0
  83. package/gui/dist/assets/WantedSansVariable.split.8-DsjypPd6.woff2 +0 -0
  84. package/gui/dist/assets/WantedSansVariable.split.80-C0VwHTte.woff2 +0 -0
  85. package/gui/dist/assets/WantedSansVariable.split.81-DTelu57i.woff2 +0 -0
  86. package/gui/dist/assets/WantedSansVariable.split.82-BiMndGN1.woff2 +0 -0
  87. package/gui/dist/assets/WantedSansVariable.split.83-CVcC6eDG.woff2 +0 -0
  88. package/gui/dist/assets/WantedSansVariable.split.84-DlhZVt_O.woff2 +0 -0
  89. package/gui/dist/assets/WantedSansVariable.split.85-S8I2D3pA.woff2 +0 -0
  90. package/gui/dist/assets/WantedSansVariable.split.86-DozXXpH5.woff2 +0 -0
  91. package/gui/dist/assets/WantedSansVariable.split.87-Dsn0Vuuo.woff2 +0 -0
  92. package/gui/dist/assets/WantedSansVariable.split.88-iNwxxDCr.woff2 +0 -0
  93. package/gui/dist/assets/WantedSansVariable.split.89-B63u_9CZ.woff2 +0 -0
  94. package/gui/dist/assets/WantedSansVariable.split.9-Bi3IzpMI.woff2 +0 -0
  95. package/gui/dist/assets/WantedSansVariable.split.90-ByeH1Pqf.woff2 +0 -0
  96. package/gui/dist/assets/WantedSansVariable.split.91-UQLlagpm.woff2 +0 -0
  97. package/gui/dist/assets/brand-mark-VPhzz-WE.js +1 -0
  98. package/gui/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
  99. package/gui/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
  100. package/gui/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
  101. package/gui/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
  102. package/gui/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
  103. package/gui/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
  104. package/gui/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
  105. package/gui/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
  106. package/gui/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
  107. package/gui/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
  108. package/gui/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
  109. package/gui/dist/assets/index-5REODpG3.css +2 -0
  110. package/gui/dist/assets/index-BWKBeK77.js +94 -0
  111. package/gui/dist/assets/segment-thumb-features-BQYzrze9.js +1 -0
  112. package/gui/dist/classic/assets/App-Bz65x1J6.js +58 -0
  113. package/gui/dist/{assets/Tray-BOPQyDbH.js → classic/assets/Tray-kk7mdO6I.js} +1 -1
  114. package/gui/dist/classic/assets/index-Dp2j8bSc.js +94 -0
  115. package/gui/dist/classic/assets/quota-summary-CvamtHjB.js +1 -0
  116. package/gui/dist/classic/index.html +47 -0
  117. package/gui/dist/favicon.png +0 -0
  118. package/gui/dist/favicon.svg +14 -0
  119. package/gui/dist/index.html +15 -2
  120. package/package.json +1 -1
  121. package/src/adapters/anthropic.ts +17 -8
  122. package/src/adapters/command-code.ts +15 -16
  123. package/src/adapters/cursor/transport-retry.ts +2 -2
  124. package/src/adapters/devin/cloud-direct/chat.ts +15 -6
  125. package/src/adapters/google.ts +7 -7
  126. package/src/adapters/kiro/adapter.ts +18 -3
  127. package/src/adapters/kiro/conversation.ts +8 -8
  128. package/src/adapters/kiro/payload.ts +45 -12
  129. package/src/adapters/kiro/stream.ts +230 -14
  130. package/src/adapters/kiro/structured-output-json.ts +191 -0
  131. package/src/adapters/kiro/structured-output-schema.ts +542 -0
  132. package/src/adapters/kiro/structured-output.ts +367 -0
  133. package/src/adapters/kiro-constants.ts +24 -1
  134. package/src/adapters/kiro-events.ts +31 -1
  135. package/src/adapters/kiro-retry.ts +3 -3
  136. package/src/adapters/kiro-tools.ts +14 -9
  137. package/src/adapters/kiro-wire.ts +9 -5
  138. package/src/adapters/ollama-native.ts +11 -1
  139. package/src/adapters/openai-chat/messages.ts +58 -5
  140. package/src/adapters/openai-chat/mistral-image-limit.ts +50 -0
  141. package/src/adapters/openai-chat/passthrough.ts +5 -3
  142. package/src/adapters/openai-chat/tool-schema.ts +83 -13
  143. package/src/adapters/openai-responses/empty-assistant.ts +15 -3
  144. package/src/adapters/openai-responses/opengateway-input.ts +18 -6
  145. package/src/adapters/openai-responses/passthrough.ts +5 -4
  146. package/src/adapters/openai-responses/reasoning.ts +9 -4
  147. package/src/adapters/openai-responses/request-strips.ts +1 -1
  148. package/src/adapters/openai-responses/tool-output-recovery.ts +79 -0
  149. package/src/adapters/physical-send.ts +3 -3
  150. package/src/adapters/qoder/live-models.ts +115 -1
  151. package/src/adapters/zed-openai-stream.ts +2 -1
  152. package/src/bridge/errors.ts +11 -2
  153. package/src/bridge/response-json.ts +2 -1
  154. package/src/bridge/sse.ts +14 -6
  155. package/src/chat/failure-verdict.ts +21 -0
  156. package/src/chat/outbound.ts +139 -21
  157. package/src/claude/failure-verdict.ts +21 -0
  158. package/src/claude/inbound-content-options.ts +15 -2
  159. package/src/claude/inbound-model-options.ts +2 -1
  160. package/src/claude/inbound-records.ts +2 -0
  161. package/src/claude/inbound.ts +17 -11
  162. package/src/claude/outbound.ts +98 -22
  163. package/src/cli/capabilities-access-remote.ts +13 -0
  164. package/src/cli/capabilities-base.ts +26 -3
  165. package/src/cli/capabilities-command.ts +12 -3
  166. package/src/cli/capabilities-provider-models.ts +3 -3
  167. package/src/cli/capability-contracts.ts +2 -0
  168. package/src/cli/codex-launch-auth.ts +30 -1
  169. package/src/cli/codex-launcher.ts +12 -5
  170. package/src/cli/codex-shim-autorestore.ts +12 -4
  171. package/src/cli/config-command.ts +11 -2
  172. package/src/cli/connect.ts +17 -2
  173. package/src/cli/dispatch.ts +14 -177
  174. package/src/cli/doctor.ts +95 -7
  175. package/src/cli/help.ts +2 -2
  176. package/src/cli/index.ts +17 -8
  177. package/src/cli/link-apply-restart.ts +9 -2
  178. package/src/cli/link.ts +1 -1
  179. package/src/cli/models.ts +124 -38
  180. package/src/cli/registry.ts +12 -4
  181. package/src/cli/restart-failure.ts +17 -0
  182. package/src/cli/root.ts +8 -0
  183. package/src/cli/stop-approval.ts +2 -2
  184. package/src/cli/sync-command.ts +243 -0
  185. package/src/cli/sync-fanout-process.ts +92 -0
  186. package/src/cli/sync-fanout.ts +161 -0
  187. package/src/cli/sync-flags.ts +10 -0
  188. package/src/cli/sync-link-status.ts +39 -0
  189. package/src/cli/sync-update-guard.ts +18 -0
  190. package/src/cli/system-restart-client.ts +5 -0
  191. package/src/cli/uninstall-client-state.ts +12 -1
  192. package/src/cli/update-restart-child.ts +13 -3
  193. package/src/cli/update-restart-home.ts +6 -3
  194. package/src/cli/update-restart.ts +36 -19
  195. package/src/client/connect.ts +35 -15
  196. package/src/client/hub-client.ts +5 -0
  197. package/src/client/link-home.ts +3 -2
  198. package/src/client/link-management-relay.ts +39 -2
  199. package/src/client/link-readiness.ts +3 -1
  200. package/src/client/machine-api.ts +2 -1
  201. package/src/client/machine-listener.ts +10 -0
  202. package/src/client/orphan-token-recovery.ts +322 -0
  203. package/src/client/restore-ownership.ts +19 -1
  204. package/src/client/runtime-restart.ts +12 -4
  205. package/src/client/runtime.ts +40 -11
  206. package/src/client/stop-routing.ts +62 -0
  207. package/src/client/sync-runtime.ts +65 -0
  208. package/src/clients/config-export.ts +32 -3
  209. package/src/codex/auth-context.ts +49 -103
  210. package/src/codex/catalog/antigravity-discovery.ts +16 -5
  211. package/src/codex/catalog/provider-models.ts +18 -12
  212. package/src/codex/forward-transport-headers.ts +35 -6
  213. package/src/codex/inject/config-toml.ts +88 -0
  214. package/src/codex/inject/multi-agent-v2.ts +30 -4
  215. package/src/codex/inject/plan.ts +34 -7
  216. package/src/codex/inject-coordination.ts +11 -0
  217. package/src/codex/inject.ts +18 -7
  218. package/src/codex/lineage.ts +13 -7
  219. package/src/codex/native-profile-api.ts +27 -1
  220. package/src/codex/quota.ts +22 -6
  221. package/src/codex/request-owned-affinity-key.ts +11 -0
  222. package/src/codex/request-owned-affinity.ts +118 -0
  223. package/src/codex/request-owned-main-pin.ts +86 -0
  224. package/src/codex/role-capability.ts +52 -41
  225. package/src/codex/routing/active-account.ts +13 -5
  226. package/src/codex/routing/selection.ts +40 -1
  227. package/src/codex/routing/thread-affinity-store.ts +916 -0
  228. package/src/codex/routing/thread-affinity.ts +90 -12
  229. package/src/codex/shim-diagnostics.ts +7 -0
  230. package/src/codex/shim-legacy-record.ts +290 -0
  231. package/src/codex/shim-windows-refresh.ts +9 -2
  232. package/src/codex/shim-wrapper-write.ts +7 -0
  233. package/src/codex/shim.ts +80 -21
  234. package/src/codex/toml-source-lines.ts +72 -13
  235. package/src/combos/failover.ts +6 -109
  236. package/src/combos/request.ts +2 -2
  237. package/src/combos/resolve.ts +29 -11
  238. package/src/config/diagnostics.ts +9 -9
  239. package/src/config/load-degrade.ts +27 -27
  240. package/src/config/process-state.ts +5 -0
  241. package/src/config/provider-validation.ts +9 -2
  242. package/src/config/removed-opencode-free-backup.ts +6 -3
  243. package/src/config/schema/leaf-validators.ts +1 -0
  244. package/src/config/tracked-runtime-inspection.ts +3 -3
  245. package/src/generated/compatibility-version.json +595 -255
  246. package/src/images/loop.ts +4 -3
  247. package/src/lib/client-error-body.ts +215 -0
  248. package/src/lib/client-retry-after.ts +66 -0
  249. package/src/lib/config-ownership-removal.ts +422 -0
  250. package/src/lib/config-ownership.ts +650 -81
  251. package/src/lib/context-overflow-verdict.ts +113 -0
  252. package/src/lib/crash-guard.ts +4 -2
  253. package/src/lib/desktop-cli-handoff.d.mts +4 -5
  254. package/src/lib/desktop-cli-handoff.mjs +18 -17
  255. package/src/lib/empty-tool-catalog.ts +25 -3
  256. package/src/lib/error-destination.ts +47 -0
  257. package/src/lib/errors.ts +1 -0
  258. package/src/lib/failure-detail.ts +66 -0
  259. package/src/lib/local-management-capability.ts +3 -1
  260. package/src/lib/messages-error-body.ts +162 -0
  261. package/src/lib/messages-sse-error-presentation.ts +93 -0
  262. package/src/lib/model-identity.ts +44 -0
  263. package/src/lib/passive-read.ts +9 -4
  264. package/src/lib/private-sqlite-path.ts +124 -0
  265. package/src/lib/response-service-tier.ts +17 -0
  266. package/src/lib/retry-after.ts +3 -0
  267. package/src/lib/socks5-fetch.ts +21 -9
  268. package/src/lib/spend-ledger-owner.ts +43 -4
  269. package/src/lib/upstream-condition.ts +142 -0
  270. package/src/lib/upstream-retry.ts +95 -12
  271. package/src/lib/windows-owner-acl.ts +22 -9
  272. package/src/lib/winsw.ts +5 -5
  273. package/src/lib/workflow-budget.ts +3 -1
  274. package/src/link/remote-platform.ts +14 -0
  275. package/src/link/remote-windows.ts +51 -9
  276. package/src/link/ssh-argv.ts +63 -10
  277. package/src/oauth/generic-account-failover.ts +13 -11
  278. package/src/oauth/index.ts +42 -73
  279. package/src/oauth/oauth-errors.ts +67 -0
  280. package/src/oauth/store.ts +21 -11
  281. package/src/protocols/codecs/messages-ir.ts +15 -9
  282. package/src/protocols/codecs/ollama.ts +197 -0
  283. package/src/protocols/encoders/adapter-events.ts +30 -7
  284. package/src/protocols/encoders/chat.ts +18 -10
  285. package/src/protocols/encoders/messages.ts +72 -28
  286. package/src/protocols/trace.ts +18 -0
  287. package/src/providers/antigravity-models.ts +45 -12
  288. package/src/providers/fastwire.ts +3 -2
  289. package/src/providers/free-directory.ts +2 -2
  290. package/src/providers/quota/anthropic-usage-windows.ts +13 -3
  291. package/src/providers/quota/vendor-probes-key.ts +60 -24
  292. package/src/providers/reasoning-metadata.ts +2 -1
  293. package/src/providers/registry/entries-extended.ts +9 -0
  294. package/src/providers/request-pacing.ts +3 -2
  295. package/src/reasoning-effort.ts +7 -2
  296. package/src/remote/hub-usage.ts +3 -0
  297. package/src/responses/apply-patch-envelope.ts +45 -19
  298. package/src/responses/compaction-history-framing.ts +5 -1
  299. package/src/responses/freeform-wrapper-scan.ts +5 -3
  300. package/src/responses/progressive-freeform-input.ts +354 -45
  301. package/src/routing/fallback-policy.ts +3 -1
  302. package/src/server/admission-model-scope.ts +17 -6
  303. package/src/server/admitted-http-turn.ts +57 -0
  304. package/src/server/anthropic-rate-limit-headers.ts +75 -3
  305. package/src/server/auth-cors.ts +5 -0
  306. package/src/server/chat-completions.ts +45 -20
  307. package/src/server/chat-native-sse.ts +52 -7
  308. package/src/server/chat-native.ts +60 -35
  309. package/src/server/claude-bridge-pool-session.ts +59 -0
  310. package/src/server/claude-messages.ts +174 -84
  311. package/src/server/context-history.ts +5 -1
  312. package/src/server/gui-static.ts +4 -1
  313. package/src/server/hub-usage.ts +7 -2
  314. package/src/server/inbound-body-admission.ts +11 -4
  315. package/src/server/index/link-management.ts +8 -0
  316. package/src/server/index/ollama-routes.ts +91 -0
  317. package/src/server/index/serve-options.ts +44 -24
  318. package/src/server/index/startup-warnings.ts +5 -12
  319. package/src/server/index/websocket-handler.ts +5 -2
  320. package/src/server/index.ts +6 -36
  321. package/src/server/inference/client-encoder-delivery.ts +9 -3
  322. package/src/server/json-serialized-bytes.ts +44 -0
  323. package/src/server/management/agent-settings-routes.ts +24 -21
  324. package/src/server/management/codex-cli-update-routes.ts +2 -1
  325. package/src/server/management/logs-usage-routes.ts +89 -4
  326. package/src/server/management/provider-routes.ts +2 -22
  327. package/src/server/management/qoder-provider-probe.ts +105 -0
  328. package/src/server/management/route-registry.ts +4 -1
  329. package/src/server/management/usage-aggregate-cache.ts +15 -5
  330. package/src/server/management/usage-source-facets.ts +38 -0
  331. package/src/server/messages-native-scope.ts +2 -1
  332. package/src/server/messages-native-tool-guard.ts +12 -4
  333. package/src/server/messages-native.ts +123 -47
  334. package/src/server/messages-response-headers.ts +25 -11
  335. package/src/server/messages-shared.ts +99 -65
  336. package/src/server/messages-sse-error-filter.ts +199 -0
  337. package/src/server/native-final-log.ts +93 -0
  338. package/src/server/ollama-inbound.ts +349 -0
  339. package/src/server/ollama-native-paths.ts +21 -0
  340. package/src/server/ollama-route-policy.ts +73 -0
  341. package/src/server/relay.ts +22 -3
  342. package/src/server/request-decompress.ts +4 -2
  343. package/src/server/request-log-failure-attribution.ts +22 -0
  344. package/src/server/request-log-failure-detail.ts +80 -0
  345. package/src/server/request-log-terminal-status.ts +4 -2
  346. package/src/server/request-log.ts +91 -42
  347. package/src/server/response-log-body.ts +6 -1
  348. package/src/server/responses/account-change-state.ts +17 -6
  349. package/src/server/responses/adapter-continuation.ts +17 -6
  350. package/src/server/responses/adapter-delivery.ts +17 -10
  351. package/src/server/responses/adapter-dispatch.ts +32 -31
  352. package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
  353. package/src/server/responses/codex-auth-error.ts +24 -19
  354. package/src/server/responses/codex-ws-exchange.ts +11 -8
  355. package/src/server/responses/codex-ws-metadata.ts +94 -21
  356. package/src/server/responses/codex-ws-request.ts +3 -1
  357. package/src/server/responses/codex-ws-wire.ts +19 -3
  358. package/src/server/responses/compact.ts +57 -24
  359. package/src/server/responses/compaction-recovery.ts +4 -2
  360. package/src/server/responses/compaction-routing.ts +9 -4
  361. package/src/server/responses/core-auth.ts +32 -14
  362. package/src/server/responses/core-codex-account.ts +17 -6
  363. package/src/server/responses/core-combo-failure.ts +7 -1
  364. package/src/server/responses/core-combo-native.ts +2 -1
  365. package/src/server/responses/core-combo.ts +43 -27
  366. package/src/server/responses/core-errors.ts +24 -21
  367. package/src/server/responses/core-lifetime.ts +6 -3
  368. package/src/server/responses/core-normalize.ts +1 -0
  369. package/src/server/responses/core-options.ts +9 -0
  370. package/src/server/responses/core-pipeline.ts +4 -3
  371. package/src/server/responses/empty-completion-guard.ts +4 -1
  372. package/src/server/responses/fetch-helpers.ts +7 -1
  373. package/src/server/responses/input-admission.ts +28 -2
  374. package/src/server/responses/native-injection.ts +1 -1
  375. package/src/server/responses/native-response-json.ts +13 -4
  376. package/src/server/responses/native-response-output.ts +1 -2
  377. package/src/server/responses/non-replayable-error.ts +2 -1
  378. package/src/server/responses/pacing-overload.ts +3 -2
  379. package/src/server/responses/passthrough-delivery.ts +55 -14
  380. package/src/server/responses/passthrough-dispatch.ts +20 -18
  381. package/src/server/responses/passthrough-error.ts +9 -9
  382. package/src/server/responses/policy-fallback.ts +2 -1
  383. package/src/server/responses/request-prepare.ts +69 -48
  384. package/src/server/responses/request-transport.ts +36 -31
  385. package/src/server/responses/response-effects.ts +3 -2
  386. package/src/server/responses/run-turn-execution.ts +6 -6
  387. package/src/server/responses/sidecar-execution.ts +16 -12
  388. package/src/server/responses/spend-storage-error.ts +2 -1
  389. package/src/server/responses/terminal-guard.ts +30 -0
  390. package/src/server/responses/ws-upstream.ts +9 -0
  391. package/src/server/responses-custom-tool-repair.ts +14 -3
  392. package/src/server/responses-terminal-repair.ts +5 -2
  393. package/src/server/responses-undeclared-tool-guard.ts +39 -16
  394. package/src/server/restart-replacement.ts +3 -1
  395. package/src/server/safe-response-headers.ts +162 -13
  396. package/src/server/sse-payload-rewrite.ts +23 -11
  397. package/src/server/turn-state-handles.ts +164 -0
  398. package/src/server/workflow-refusal.ts +2 -1
  399. package/src/server/ws-bridge.ts +4 -0
  400. package/src/service/definition.ts +36 -4
  401. package/src/service/launchd.ts +89 -32
  402. package/src/service/logging.ts +34 -7
  403. package/src/service/ownership-mutation-lease.d.mts +11 -2
  404. package/src/service/ownership-mutation-lease.mjs +53 -11
  405. package/src/service/plist-xml.ts +59 -0
  406. package/src/service/state.ts +8 -0
  407. package/src/service/systemd-env.ts +2 -0
  408. package/src/service/systemd.ts +31 -7
  409. package/src/service/windows-taskxml.ts +16 -7
  410. package/src/service-manager-probe.ts +4 -10
  411. package/src/types/config.ts +2 -0
  412. package/src/types/provider.ts +2 -0
  413. package/src/types/request.ts +13 -1
  414. package/src/update/config-preflight.ts +48 -0
  415. package/src/update/index.ts +148 -26
  416. package/src/update/npm-install.d.mts +8 -0
  417. package/src/update/npm-install.mjs +20 -1
  418. package/src/update/orchestrated-admission.d.mts +4 -0
  419. package/src/update/orchestrated-admission.mjs +12 -0
  420. package/src/update/orchestrated-arguments.d.mts +12 -0
  421. package/src/update/orchestrated-arguments.mjs +64 -0
  422. package/src/update/orchestrated-child.d.mts +12 -0
  423. package/src/update/orchestrated-child.mjs +70 -0
  424. package/src/update/orchestrated-entry.ts +234 -0
  425. package/src/update/orchestrated-identity-probe-entry.ts +20 -0
  426. package/src/update/orchestrated-identity.ts +53 -0
  427. package/src/update/orchestrated-live-identity.ts +48 -0
  428. package/src/update/orchestrated-node-driver.mjs +99 -0
  429. package/src/update/orchestrated-outcome.d.mts +21 -0
  430. package/src/update/orchestrated-outcome.mjs +40 -0
  431. package/src/update/orchestrated-plan.ts +49 -0
  432. package/src/update/orchestrated-pnpm.d.mts +8 -0
  433. package/src/update/orchestrated-pnpm.mjs +33 -0
  434. package/src/update/orchestrated-session.d.mts +37 -0
  435. package/src/update/orchestrated-session.mjs +264 -0
  436. package/src/update/orchestrated-token.d.mts +10 -0
  437. package/src/update/orchestrated-token.mjs +21 -0
  438. package/src/update/orchestrated-token.ts +3 -0
  439. package/src/update/orchestrated-tree.d.mts +1 -0
  440. package/src/update/orchestrated-tree.mjs +14 -0
  441. package/src/update/pnpm-global-install.d.mts +6 -0
  442. package/src/update/pnpm-global-install.mjs +9 -2
  443. package/src/update/posix-install-supervisor-entry.mjs +6 -0
  444. package/src/update/posix-install-supervisor.d.mts +10 -0
  445. package/src/update/posix-install-supervisor.mjs +88 -0
  446. package/src/update/sync-update-mutex.d.mts +10 -0
  447. package/src/update/sync-update-mutex.mjs +164 -0
  448. package/src/update/sync-update-mutex.ts +1 -0
  449. package/src/update/test-home-guard.mjs +33 -0
  450. package/src/update/transactional-install.mjs +2 -2
  451. package/src/usage/client.ts +43 -0
  452. package/src/usage/error-detail.ts +225 -0
  453. package/src/usage/log.ts +64 -27
  454. package/src/usage/source-facets.ts +51 -0
  455. package/src/usage/source-filter.ts +65 -0
  456. package/src/usage/summary.ts +50 -8
  457. package/src/web-search/anthropic-executor.ts +105 -24
  458. package/src/web-search/executor.ts +5 -3
  459. package/src/web-search/loop.ts +33 -6
  460. package/src/web-search/progress-stream.ts +64 -5
  461. package/gui/dist/assets/App-bwugJRKn.js +0 -58
  462. package/gui/dist/assets/index-ebuVUtB7.js +0 -94
  463. package/gui/dist/assets/quota-summary-6HsrHQAy.js +0 -1
  464. /package/gui/dist/{assets → classic/assets}/App-B4j-Txmb.css +0 -0
  465. /package/gui/dist/{assets → classic/assets}/index-CHWwDo9L.css +0 -0
@@ -48,6 +48,7 @@ import { vercelGatewayRoutingConfigError } from "../providers/vercel-gateway-rou
48
48
  import { googleVertexLocationConfigError } from "../providers/google-vertex-location";
49
49
  import { xaiResponsesOptInState } from "../providers/xai-responses-opt-in";
50
50
  import { authorizationCredential } from "../lib/authorization-scheme";
51
+ import { noteTurnStateCaller } from "./turn-state-handles";
51
52
 
52
53
  let _corsOrigin = "http://localhost:10100";
53
54
  // Listener identity is process-local. Never infer this trust from forwarded headers: the
@@ -549,11 +550,15 @@ export function isDataPlaneAdmissionSecret(token: string, config: OcxConfig): bo
549
550
  * configured entry to name, and folding them into the id as sentinel strings
550
551
  * would collide with a hand-edited entry that happens to be called `loopback` —
551
552
  * ids are only validated as non-empty strings.
553
+ *
554
+ * Every admitted data-plane path builds its log context here, so this is also where the request's
555
+ * caller is recorded for turn-state handle binding (src/server/turn-state-handles.ts).
552
556
  */
553
557
  export function admissionFields(admission: DataPlaneAdmission): {
554
558
  admissionKind: DataPlaneAdmission["kind"];
555
559
  apiKeyId?: string;
556
560
  } {
561
+ noteTurnStateCaller(admission);
557
562
  return admission.kind === "configured"
558
563
  ? { admissionKind: "configured", apiKeyId: admission.keyId }
559
564
  : { admissionKind: admission.kind };
@@ -1,3 +1,9 @@
1
+ import { projectLocalModelRefusalLog } from "./request-log-failure-detail";
2
+ import { clientFacingModelLabel, clientFacingModelText, type ClientFacingModelLabel } from "../lib/error-destination";
3
+ import { wasAccountAutoPaused } from "../lib/account-auto-pause-response";
4
+ import { ownedChatErrorPayload, isProxyError, LOCAL_CHAT_ERROR_MESSAGES } from "../lib/client-error-body";
5
+ import { ownedChatErrorResponse } from "../chat/outbound";
6
+ import type { FailureVerdictSink } from "../chat/failure-verdict";
1
7
  /**
2
8
  * OpenAI Chat Completions inbound (/v1/chat/completions) for GitHub Copilot App
3
9
  * and other OpenAI-compatible clients.
@@ -30,7 +36,7 @@ import {
30
36
  } from "./capture-admission-policy";
31
37
  import { classifyError, cyberPolicyErrorType, CYBER_POLICY_ERROR_CODE, isCyberPolicyCode, UNDECLARED_TOOL_CALL_ERROR_CODE } from "../lib/errors";
32
38
  import { redactSecretString } from "../lib/redact";
33
- import { resolveClientRetryAfter } from "../lib/retry-after";
39
+ import { formatClientRetryAfter, resolveClientRetryAfter } from "../lib/retry-after";
34
40
  import {
35
41
  applyReplayRefusalClientHeaders,
36
42
  isReplayRefusalCode,
@@ -53,7 +59,7 @@ import {
53
59
  linkRequestSessionLane,
54
60
  } from "./request-log-conversation";
55
61
  import type { OcxConfig } from "../types";
56
- import { readJsonRequestBody, resolveInboundBodyLimitBytes } from "./request-decompress";
62
+ import { DecompressedBodyTooLargeError, UnsupportedContentEncodingError, readJsonRequestBody, resolveInboundBodyLimitBytes } from "./request-decompress";
57
63
  import {
58
64
  addFinalRequestLog,
59
65
  cacheDiagnosticsObserving,
@@ -73,7 +79,7 @@ import { captureExplicitOpenAiCallerAuth } from "../providers/openai-sidecar";
73
79
  import { captureCallerDirectAuth } from "../providers/caller-authorization";
74
80
  import type { AdmissionLease } from "../lib/admission";
75
81
  import {
76
- admissionModelDeniedResponse,
82
+ admissionModelDeniedResponse, projectAdmissionModelDeniedLog,
77
83
  AdmissionModelDeniedError,
78
84
  MODEL_NOT_ALLOWED_FOR_KEY,
79
85
  assertRouteAllowedByScope,
@@ -95,7 +101,7 @@ import { credentialDomainFor } from "../protocols/opaque-state";
95
101
  import { checkRepresentable, unrepresentableMessage } from "../protocols/guard";
96
102
  import { requestPathForLane } from "../protocols/path";
97
103
  import { resolveProtocolSettings } from "../protocols/settings";
98
- import { markProtocolBlocked, markProtocolEntry } from "../protocols/trace";
104
+ import { markProtocolBlocked, markProtocolEntry, suppressProtocolTrace } from "../protocols/trace";
99
105
  import { recordProtocolShadowPlan } from "../protocols/shadow-plan";
100
106
  import { jsonCompletionSse } from "./chat-native-sse";
101
107
  import { parseRequestEffortRowId } from "./effort-row";
@@ -109,12 +115,16 @@ type Rec = Record<string, unknown>;
109
115
  function isRec(v: unknown): v is Rec {
110
116
  return !!v && typeof v === "object" && !Array.isArray(v);
111
117
  }
118
+ function routingPolicyRefusalMessage(kind: "unknown" | "unavailable", model: ClientFacingModelLabel): string {
119
+ return kind === "unknown" ? `Unknown routing policy: ${clientFacingModelText(model)}`
120
+ : `No eligible candidates for policy profile: ${clientFacingModelText(model)}`;
121
+ }
112
122
  async function readChatBody(req: Request, budget: TranslatorBudget, maxBytes: number): Promise<unknown> {
113
123
  try {
114
124
  return await readJsonRequestBody(req, budget, maxBytes);
115
125
  } catch (err) {
116
126
  if (isTranslatorBudgetExceededError(err)) throw err;
117
- throw new ChatCompletionsRequestError(err instanceof Error && err.message ? err.message : "Invalid JSON body");
127
+ throw new ChatCompletionsRequestError(err instanceof DecompressedBodyTooLargeError || err instanceof UnsupportedContentEncodingError ? err.message : "Invalid JSON body");
118
128
  }
119
129
  }
120
130
 
@@ -125,8 +135,10 @@ export async function handleChatCompletions(
125
135
  logIds?: {
126
136
  requestId: string; start: number; turnAdmissionLease?: AdmissionLease; admission?: DataPlaneAdmission;
127
137
  advisorAdmissionPolicy?: CapturedAdmissionPolicy;
138
+ suppressProtocolTrace?: boolean;
128
139
  },
129
140
  ): Promise<Response> {
141
+ if (logIds?.suppressProtocolTrace) suppressProtocolTrace(logCtx);
130
142
  // Set before routing so pre-dispatch refusals stay correlated. A Hermes-only id is marked as a
131
143
  // fallback, so a native id resolved later on the Responses bridge still replaces it.
132
144
  assignNativeLogConversationId(logCtx, nativeLogConversationIdFromHeaders(req.headers));
@@ -148,7 +160,7 @@ export async function handleChatCompletions(
148
160
  }
149
161
  if (isChatCompletionsStreamError(error)) {
150
162
  if (logIds) addFinalRequestLog(logIds.requestId, logIds.start, logCtx, error.status, { closeReason: "non_stream" });
151
- return chatCompletionsErrorResponse(error.status, error.message, error.type, error.code);
163
+ return ownedChatErrorResponse(error.status, error.message, error.type, error.code);
152
164
  }
153
165
  throw error;
154
166
  }
@@ -245,7 +257,7 @@ async function handleChatCompletionsWithBudget(
245
257
  );
246
258
  // Native Chat must check its own destination; translated xAI OAuth turns
247
259
  // preview the billed Fast lane before their final Responses scope check.
248
- assertRouteAllowedByScope(inheritedAdmissionScope ?? resolveAdmissionModelScope(config, logIds?.admission), requestedModel, {
260
+ assertRouteAllowedByScope(inheritedAdmissionScope ?? resolveAdmissionModelScope(config, logIds?.admission), clientFacingModelLabel(requestedModel), {
249
261
  providerName: route.providerName,
250
262
  modelId: previewXaiOauthWireModel({ options: {
251
263
  serviceTier: typeof chatBody.service_tier === "string" ? chatBody.service_tier : undefined,
@@ -304,18 +316,23 @@ async function handleChatCompletionsWithBudget(
304
316
  }
305
317
  if (err instanceof AdmissionModelDeniedError) {
306
318
  logCtx.requestedModel = requestedModel;
319
+ projectAdmissionModelDeniedLog(err, logCtx);
307
320
  if (logIds) addFinalRequestLog(logIds.requestId, logIds.start, logCtx, 403, { closeReason: "non_stream" });
308
321
  return admissionModelDeniedResponse(err);
309
322
  }
310
323
  if (err instanceof UnknownRoutingPolicyError) {
311
324
  logCtx.requestedModel = requestedModel;
325
+ const clientModel = clientFacingModelLabel(requestedModel);
326
+ projectLocalModelRefusalLog(logCtx, clientModel);
312
327
  if (logIds) addFinalRequestLog(logIds.requestId, logIds.start, logCtx, 404, { closeReason: "non_stream" });
313
- return chatCompletionsErrorResponse(404, err.message, "invalid_request_error");
328
+ return chatCompletionsErrorResponse(404, routingPolicyRefusalMessage("unknown", clientModel), "invalid_request_error");
314
329
  }
315
330
  if (err instanceof NoEligiblePolicyCandidateError) {
316
331
  logCtx.routeDecision = err.trace;
332
+ const clientModel = clientFacingModelLabel(requestedModel);
333
+ projectLocalModelRefusalLog(logCtx, clientModel);
317
334
  if (logIds) addFinalRequestLog(logIds.requestId, logIds.start, logCtx, 404, { closeReason: "non_stream" });
318
- return chatCompletionsErrorResponse(404, err.message, "invalid_request_error");
335
+ return chatCompletionsErrorResponse(404, routingPolicyRefusalMessage("unavailable", clientModel), "invalid_request_error");
319
336
  }
320
337
  /* unknown model: let handleResponses shape the 404 */
321
338
  }
@@ -542,6 +559,7 @@ async function handleChatCompletionsWithBudget(
542
559
 
543
560
  const finalizeNativeLog = createFinalRequestLog(logIds, logCtx).finish;
544
561
  const upstream = await handleResponses(internalReq, config, logCtx, {
562
+ clientModelLabel: clientFacingModelLabel(requestedModel),
545
563
  openAiSidecarAuth,
546
564
  allowStoredOpenAiSidecarAuth: !!(callerAuthorizationRoute && settledRoute
547
565
  && !isCanonicalOpenAiForwardProvider(settledRoute.provider)),
@@ -696,25 +714,31 @@ async function handleChatCompletionsWithBudget(
696
714
  message,
697
715
  upstreamRetryAfter: upstream.headers.get("retry-after"),
698
716
  });
699
- const rewritten = new Response(JSON.stringify({
717
+ const legacyEnvelope = JSON.stringify({
700
718
  error: {
701
719
  message: classified.message,
702
720
  type: classified.type,
703
721
  param: null,
704
722
  code: classified.code,
705
723
  },
706
- }), {
724
+ });
725
+ const localReason = logCtx.errorCode ?? (wasAccountAutoPaused(upstream) ? "credentials_paused" : undefined);
726
+ const proxyAuthored = isProxyError(upstream) || LOCAL_CHAT_ERROR_MESSAGES.has(logCtx.errorCode ?? "");
727
+ const rewritten = new Response(proxyAuthored ? legacyEnvelope : JSON.stringify(ownedChatErrorPayload({ status, type: classified.type, code: classified.code,
728
+ evidence: { status: upstream.status, type: upstreamType, code: upstreamCode ?? undefined, message },
729
+ localReason,
730
+ })), {
707
731
  status,
708
732
  headers: {
709
733
  "Content-Type": "application/json",
710
- ...(retryAfter ? { "Retry-After": retryAfter } : {}),
734
+ ...(formatClientRetryAfter(retryAfter) ? { "Retry-After": formatClientRetryAfter(retryAfter)! } : {}),
711
735
  ...(replayRefusal ? REPLAY_REFUSAL_CLIENT_HEADERS : {}),
712
736
  },
713
737
  });
714
738
  if (replayRefusal) retainReplayRefusal(rewritten);
715
739
  return logIds
716
740
  // Deferred logging re-wraps this response and carries the verdict with it.
717
- ? responseWithDeferredRequestLog(rewritten, logIds.requestId, logIds.start, logCtx)
741
+ ? responseWithDeferredRequestLog(rewritten, logIds.requestId, logIds.start, logCtx, undefined, legacyEnvelope)
718
742
  : rewritten;
719
743
  }
720
744
 
@@ -727,7 +751,8 @@ async function handleChatCompletionsWithBudget(
727
751
  : upstream;
728
752
 
729
753
  if (contentType.includes("text/event-stream") && response.body) {
730
- const chatSse = responsesSseToChatCompletionsSse(response.body, requestedModel, { translatorBudget });
754
+ const failureVerdictSink: FailureVerdictSink = {};
755
+ const chatSse = responsesSseToChatCompletionsSse(response.body, requestedModel, { translatorBudget, failureVerdictSink, proxyErrorContext: logCtx });
731
756
  if (stream) {
732
757
  // Stream failures surface as an error SSE frame then abort the body — never a
733
758
  // success completion that embeds `[error] ...` + clean [DONE].
@@ -741,16 +766,16 @@ async function handleChatCompletionsWithBudget(
741
766
  });
742
767
  }
743
768
  try {
744
- const completion = await collectChatCompletion(chatSse, requestedModel, translatorBudget);
769
+ const completion = await collectChatCompletion(chatSse, requestedModel, translatorBudget, failureVerdictSink);
745
770
  return new Response(JSON.stringify(completion), {
746
771
  status: 200,
747
772
  headers: { "Content-Type": "application/json" },
748
773
  });
749
774
  } catch (err) {
750
775
  if (isChatCompletionsStreamError(err)) {
751
- return chatCompletionsErrorResponse(err.status, err.message, err.type, err.code);
776
+ return ownedChatErrorResponse(err.status, err.message, err.type, err.code, failureVerdictSink.contract, failureVerdictSink.proxyAuthored);
752
777
  }
753
- return chatCompletionsErrorResponse(
778
+ return ownedChatErrorResponse(
754
779
  502,
755
780
  err instanceof Error ? err.message : String(err),
756
781
  "server_error",
@@ -767,7 +792,7 @@ async function handleChatCompletionsWithBudget(
767
792
  try {
768
793
  json = await response.json();
769
794
  } catch {
770
- return finishJson(chatCompletionsErrorResponse(502, "internal replay returned a non-JSON response", "server_error"));
795
+ return finishJson(ownedChatErrorResponse(502, "internal replay returned a non-JSON response", "server_error"));
771
796
  }
772
797
  const status = (json as Rec)?.status;
773
798
  if (status === "failed") {
@@ -790,7 +815,7 @@ async function handleChatCompletionsWithBudget(
790
815
  classified.type = "invalid_request_error";
791
816
  }
792
817
  if (isReplayRefusalCode(classified.code)) {
793
- const refusal = chatCompletionsErrorResponse(
818
+ const refusal = ownedChatErrorResponse(
794
819
  REPLAY_REFUSED_STATUS, message, classified.type, classified.code,
795
820
  );
796
821
  const headers = new Headers(refusal.headers);
@@ -799,7 +824,7 @@ async function handleChatCompletionsWithBudget(
799
824
  new Response(refusal.body, { status: refusal.status, headers }),
800
825
  ));
801
826
  }
802
- return finishJson(chatCompletionsErrorResponse(
827
+ return finishJson(ownedChatErrorResponse(
803
828
  classified.code === "translation_buffer_limit"
804
829
  ? 502
805
830
  : isCyberPolicyCode(classified.code) ? 400 : 502,
@@ -1,4 +1,6 @@
1
- import { chatCompletionsErrorBody } from "../chat/outbound";
1
+ import { ownedChatErrorPayload, canonicalSseErrorBlock } from "../lib/client-error-body";
2
+ import { legacyFailureVerdict, type FailureVerdictSink } from "../chat/failure-verdict";
3
+ import { chatCompletionsErrorBody, type ChatCompletionsCollectorSource } from "../chat/outbound";
2
4
  import { classifyError, cyberPolicyErrorType, CYBER_POLICY_ERROR_CODE, isCyberPolicyCode, UNDECLARED_TOOL_CALL_ERROR_CODE } from "../lib/errors";
3
5
  import { redactSecretString } from "../lib/redact";
4
6
  import {
@@ -66,6 +68,10 @@ export function usageFromChat(value: unknown): OcxUsage | undefined {
66
68
  };
67
69
  }
68
70
 
71
+ function namedErrorControlBlock(data: string | null): string {
72
+ return "event: error\n" + (data === null ? "" : data.split("\n").map(line => `data: ${line}\n`).join("")) + "\n";
73
+ }
74
+
69
75
  export function structuredError(value: unknown): {
70
76
  message: string;
71
77
  type?: string;
@@ -85,6 +91,19 @@ export function structuredError(value: unknown): {
85
91
  };
86
92
  }
87
93
 
94
+ /** Preserve the ordinary relay representation, replacing only choice error objects. */
95
+ export function normalizeChatChoiceErrors(value: Rec): Rec {
96
+ if (!Array.isArray(value.choices)) return value;
97
+ return { ...value, choices: value.choices.map(choice => {
98
+ if (!isRec(choice) || choice.error === undefined || choice.error === null) return choice;
99
+ const original = structuredError({ error: choice.error }) ?? { message: "upstream error" };
100
+ const status = original.status ?? 502;
101
+ const { message, type, code } = ownedChatErrorPayload({ status, type: original.type, code: original.code,
102
+ evidence: { ...original, status, code: original.code ?? undefined } }).error;
103
+ return { ...choice, error: { message, type, code } };
104
+ }) };
105
+ }
106
+
88
107
  function normalizedChunk(value: Rec, requestedModel: string): Rec {
89
108
  return {
90
109
  ...value,
@@ -159,6 +178,8 @@ export function jsonCompletionSse(value: Rec, requestedModel: string, budget?: T
159
178
 
160
179
  interface NativeChatSseOptions {
161
180
  requestedModel: string;
181
+ failureVerdictSink?: FailureVerdictSink;
182
+ collectorSource?: ChatCompletionsCollectorSource;
162
183
  translatorBudget: TranslatorBudget;
163
184
  signal: AbortSignal;
164
185
  stallTimeoutSec?: number;
@@ -291,9 +312,13 @@ export function nativeChatSse(
291
312
  const safeMessage = redactSecretString(message);
292
313
  releaseBuffer();
293
314
  try {
294
- const frame = `data: ${JSON.stringify(chatCompletionsErrorBody(status, safeMessage, type, code))}\n\n`;
315
+ const legacy = chatCompletionsErrorBody(status, safeMessage, type, code);
316
+ const error = legacy.error as { type: string; code: string | null };
317
+ const frame = canonicalSseErrorBlock(ownedChatErrorPayload({ status, type: error.type, code: error.code,
318
+ evidence: { status, type, code, message: safeMessage } }));
295
319
  if (code === "translation_buffer_limit") controller.enqueue(encoder.encode(frame));
296
320
  else enqueue(controller, frame);
321
+ if (options.failureVerdictSink) options.failureVerdictSink.verdict = legacyFailureVerdict(legacy);
297
322
  } catch { /* controller may already be closed */ }
298
323
  settle(status, safeMessage);
299
324
  try { void reader.cancel(new Error(safeMessage)).catch(() => {}); } catch { /* already closed */ }
@@ -304,16 +329,21 @@ export function nativeChatSse(
304
329
  block: string,
305
330
  delimiter: string,
306
331
  ): void => {
332
+ let eventName = "";
333
+ for (const line of block.split(/\r\n|\r|\n/)) {
334
+ if (line === "event") eventName = "";
335
+ else if (line.startsWith("event:")) eventName = line.slice(6).replace(/^ /, "");
336
+ }
307
337
  const payload = sseDataPayload(block);
308
338
  if (payload === null) {
309
339
  if (performance.now() >= pullDeadline) throw stalled;
310
- relay(controller, block + delimiter);
340
+ relay(controller, eventName === "error" ? namedErrorControlBlock(payload) : block + delimiter);
311
341
  return;
312
342
  }
313
343
  const trimmed = payload.trim();
314
344
  if (trimmed === "") {
315
345
  if (performance.now() >= pullDeadline) throw stalled;
316
- relay(controller, block + delimiter);
346
+ relay(controller, eventName === "error" ? namedErrorControlBlock(payload) : block + delimiter);
317
347
  return;
318
348
  }
319
349
  if (trimmed === "[DONE]") {
@@ -322,7 +352,7 @@ export function nativeChatSse(
322
352
  return;
323
353
  }
324
354
  sawDone = true;
325
- enqueue(controller, replaceSseDataPayload(block, "[DONE]") + delimiter);
355
+ enqueue(controller, eventName === "error" ? namedErrorControlBlock("[DONE]") : replaceSseDataPayload(block, "[DONE]") + delimiter);
326
356
  releaseBuffer();
327
357
  settle(200);
328
358
  try { void reader.cancel().catch(() => {}); } catch { /* already closed */ }
@@ -351,7 +381,9 @@ export function nativeChatSse(
351
381
  classified.code = error.code;
352
382
  }
353
383
  const safe = chatCompletionsErrorBody(status, classified.message, classified.type, classified.code);
354
- enqueue(controller, replaceSseDataPayload(block, JSON.stringify(safe)) + delimiter);
384
+ enqueue(controller, canonicalSseErrorBlock(ownedChatErrorPayload({ status, type: classified.type, code: classified.code,
385
+ evidence: { status, type: error.type, code: error.code ?? undefined, message: error.message } })));
386
+ if (options.failureVerdictSink) options.failureVerdictSink.verdict = legacyFailureVerdict(safe);
355
387
  releaseBuffer();
356
388
  settle(isCyberPolicyCode(classified.code) ? 400 : status, classified.message);
357
389
  try { void reader.cancel(new Error(classified.message)).catch(() => {}); } catch { /* already closed */ }
@@ -404,7 +436,20 @@ export function nativeChatSse(
404
436
  firstOutput = true;
405
437
  options.onFirstOutput?.();
406
438
  }
407
- relay(controller, replaceSseDataPayload(block, JSON.stringify(normalizedChunk(parsed, options.requestedModel))) + delimiter);
439
+ if (eventName === "error" || parsed.type === "error") {
440
+ // These frames keep the ordinary relay's control flow; only client bytes change.
441
+ const flat = structuredError({ error: parsed })!;
442
+ const status = flat.status ?? 502;
443
+ const payload = ownedChatErrorPayload({ status, type: flat.type, code: flat.code,
444
+ evidence: { ...flat, status, code: flat.code ?? undefined } }).error;
445
+ options.collectorSource?.retain(JSON.stringify(payload), parsed);
446
+ relay(controller, (eventName === "error" ? "event: error\n" : "") + canonicalSseErrorBlock(payload));
447
+ } else {
448
+ const normalized = normalizedChunk(parsed, options.requestedModel);
449
+ const choiceError = choices.some(choice => isRec(choice) && choice.error !== undefined && choice.error !== null);
450
+ relay(controller, choiceError ? canonicalSseErrorBlock(normalizeChatChoiceErrors(normalized))
451
+ : replaceSseDataPayload(block, JSON.stringify(normalized)) + delimiter);
452
+ }
408
453
  };
409
454
 
410
455
  function onAbort() {
@@ -1,3 +1,6 @@
1
+ import { attachComboFailureEvidence, ownedChatErrorPayload, type ClientErrorContract } from "../lib/client-error-body";
2
+ import type { FailureVerdictSink } from "../chat/failure-verdict";
3
+ import { normalizeChatChoiceErrors } from "./chat-native-sse";
1
4
  import { captureAccountAutoPauseAttempt, wasAccountAutoPaused } from "../oauth/account-auto-pause";
2
5
  import { createPhysicalSendReporter } from "../lib/request-execution-budget";
3
6
  import { createInferenceSendBudget } from "./inference/context";
@@ -7,6 +10,7 @@ import type { AdapterRequest, ProviderAdapter } from "../adapters/base";
7
10
  import { isNativeChatRouteEligible } from "./chat-native-eligibility";
8
11
  import {
9
12
  chatCompletionsErrorBody,
13
+ ChatCompletionsCollectorSource,
10
14
  chatCompletionsErrorResponse,
11
15
  collectChatCompletion,
12
16
  isChatCompletionsStreamError,
@@ -27,7 +31,7 @@ import type { AdmissionLease } from "../lib/admission";
27
31
  import { resolveContextPrincipal, type DataPlaneAdmission } from "./auth-cors";
28
32
  import { readBoundedResponseBody } from "../lib/bounded-body";
29
33
  import { redactSecretString } from "../lib/redact";
30
- import { resolveClientRetryAfter } from "../lib/retry-after";
34
+ import { formatClientRetryAfter, resolveClientRetryAfter } from "../lib/retry-after";
31
35
  import {
32
36
  applyUpstreamRecoveryInit,
33
37
  fetchWithResetRetry,
@@ -39,7 +43,7 @@ import {
39
43
  REPLAY_REFUSAL_CLIENT_HEADERS,
40
44
  REPLAY_REFUSED_STATUS,
41
45
  retainReplayRefusal,
42
- SendBudgetExhaustedError,
46
+ SendBudgetExhaustedError, errorDestinationLabel,
43
47
  TRANSIENT_RETRY_MAX_ATTEMPTS,
44
48
  UpstreamRetryEvidenceError,
45
49
  type UpstreamSendRecovery,
@@ -86,6 +90,7 @@ import { attachRequestSpendTracker } from "./responses/request-spend";
86
90
  import { workflowRefusalResponse } from "./workflow-refusal";
87
91
  import type { ComboProtocolSource } from "./responses/core-combo-native";
88
92
  import type { ProtocolEnvelope } from "../protocols/envelope";
93
+ import { protocolTraceSuppressed } from "../protocols/trace";
89
94
  import { nativeChatReplayScope, bindNativeChatReplayScope, nativeChatReasoningCapture } from "./chat-native-reasoning";
90
95
  import { resolveMistralTransport } from "../providers/mistral-transport";
91
96
 
@@ -225,7 +230,7 @@ export interface NativeChatExecution extends HandleNativeChatOptions {
225
230
  */
226
231
  export async function handleNativeChatCompletions(options: HandleNativeChatOptions): Promise<Response> {
227
232
  const { logCtx, logIds, route } = options;
228
- logCtx.inboundProtocol = "chat";
233
+ if (!protocolTraceSuppressed(logCtx)) logCtx.inboundProtocol = "chat";
229
234
  const attemptHandle = beginInferenceAttempt(logCtx, {
230
235
  provider: route.providerName,
231
236
  model: route.modelId,
@@ -299,12 +304,22 @@ export async function runNativeChatAttempt(
299
304
  const notifyFirstOutput = execution.onFirstOutput
300
305
  ?? (logIds ? () => recordFirstOutput(logCtx, logIds.start) : undefined);
301
306
  const onFirstOutput = () => { outputDelivered = true; notifyFirstOutput?.(); };
302
- const fail = (status: number, message: string, type?: string, code?: string | null): Response => {
307
+ const failLocal = (status: number, message: string, type?: string, code?: string | null): Response => {
303
308
  const safeMessage = redactSecretString(message);
304
309
  finishLog(status, safeMessage);
305
310
  return chatCompletionsErrorResponse(status, safeMessage, type, code);
306
311
  };
307
312
 
313
+ const failUpstream = (status: number, message: string, type?: string, code?: string | null, contract?: ClientErrorContract): Response => {
314
+ const legacy = chatCompletionsErrorBody(status, redactSecretString(message), type, code);
315
+ finishLog(status, redactSecretString(message));
316
+ const error = legacy.error as { type: string; code: string | null };
317
+ const finalStatus = error.code === CYBER_POLICY_ERROR_CODE ? 400 : status;
318
+ return attachComboFailureEvidence(new Response(JSON.stringify(ownedChatErrorPayload({ status: finalStatus, type: error.type, code: error.code, contract,
319
+ evidence: { status, type, code: code ?? undefined, message } })),
320
+ { status: finalStatus, headers: { "Content-Type": "application/json" } }), JSON.stringify(legacy));
321
+ };
322
+
308
323
  normalizePinnedChatEffort(execution);
309
324
  logCtx.requestedServiceTier = typeof execution.chatBody.service_tier === "string"
310
325
  ? execution.chatBody.service_tier
@@ -341,8 +356,9 @@ export async function runNativeChatAttempt(
341
356
  } catch (error) {
342
357
  cleanupAbort(); upstream.abort();
343
358
  if (!(error instanceof RequestPacingQueueOverloadError)) throw error;
344
- const refusal = fail(429, error.message, "rate_limit_error");
345
- refusal.headers.set("Retry-After", String(error.retryAfterSeconds));
359
+ const refusal = failLocal(429, error.message, "rate_limit_error");
360
+ const advice = formatClientRetryAfter(error.retryAfterSeconds);
361
+ if (advice !== undefined) refusal.headers.set("Retry-After", advice);
346
362
  return refusal;
347
363
  }
348
364
  // Key selection re-resolves the transport; a single-key route still needs canonical Mistral pacing.
@@ -391,9 +407,9 @@ export async function runNativeChatAttempt(
391
407
  cleanupAbort();
392
408
  upstream.abort();
393
409
  if (isTranslatorBudgetExceededError(error)) {
394
- return fail(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
410
+ return failLocal(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
395
411
  }
396
- return fail(400, error instanceof Error ? error.message : String(error), "invalid_request_error");
412
+ return failLocal(400, error instanceof Error ? error.message : String(error), "invalid_request_error");
397
413
  }
398
414
 
399
415
  // One inbound request owns one transient send allowance. Capture the policy before any
@@ -452,7 +468,7 @@ export async function runNativeChatAttempt(
452
468
  // Backstop for sends the helper cannot see coming (a reset replay). The first
453
469
  // report settles the combo's booking; each later one is charged and booked.
454
470
  if (physicalSends > 0 && sendBudget.remainingBaseSends(sharedSendCap) <= 0) {
455
- throw new SendBudgetExhaustedError(safeHostLabel(url));
471
+ throw new SendBudgetExhaustedError(errorDestinationLabel(url));
456
472
  }
457
473
  physicalSends += 1;
458
474
  reportDispatchSends(sendBudget, 1, execution.comboDispatchPermit);
@@ -470,7 +486,7 @@ export async function runNativeChatAttempt(
470
486
  // #2643: opted-in key-auth openai-chat providers retry pre-stream transient statuses on
471
487
  // the native chat lane too; everyone else keeps reset-only semantics.
472
488
  const remaining = remainingTransientSends();
473
- if (sendBudget && remaining <= 0) throw new SendBudgetExhaustedError(safeHostLabel(request.url));
489
+ if (sendBudget && remaining <= 0) throw new SendBudgetExhaustedError(errorDestinationLabel(request.url));
474
490
  if (requestTransientPolicy && remaining <= 0) {
475
491
  throw new Error("native Chat transient send budget exhausted before recovery dispatch");
476
492
  }
@@ -584,7 +600,7 @@ export async function runNativeChatAttempt(
584
600
  dispatch,
585
601
  {
586
602
  abortSignal: upstream.signal,
587
- label: safeHostLabel(request.url),
603
+ label: safeHostLabel(request.url), destination: errorDestinationLabel(request.url),
588
604
  ...(requestTransientPolicy
589
605
  ? {
590
606
  attempts: remaining,
@@ -675,16 +691,17 @@ export async function runNativeChatAttempt(
675
691
  releaseRetainedRequest();
676
692
  cleanupAbort();
677
693
  upstream.abort();
678
- if (req.signal.aborted) return fail(499, "Client cancelled request", "client_cancelled");
694
+ if (req.signal.aborted) return failLocal(499, "Client cancelled request", "client_cancelled");
679
695
  const sendError = error instanceof UpstreamRetryEvidenceError ? error.cause : error;
680
696
  if (sendError instanceof RequestPacingQueueOverloadError) {
681
- const refusal = fail(429, sendError.message, "rate_limit_error");
682
- refusal.headers.set("Retry-After", String(sendError.retryAfterSeconds));
697
+ const refusal = failLocal(429, sendError.message, "rate_limit_error");
698
+ const advice = formatClientRetryAfter(sendError.retryAfterSeconds);
699
+ if (advice !== undefined) refusal.headers.set("Retry-After", advice);
683
700
  return refusal;
684
701
  }
685
702
  if (sendError instanceof SendBudgetExhaustedError) {
686
703
  // A decision this process made, answered as the Responses path answers it: 429, not 502.
687
- return fail(429, sendError.message, SEND_BUDGET_EXHAUSTED_CODE, SEND_BUDGET_EXHAUSTED_CODE);
704
+ return failLocal(429, sendError.message, SEND_BUDGET_EXHAUSTED_CODE, SEND_BUDGET_EXHAUSTED_CODE);
688
705
  }
689
706
  if (sendError instanceof NativeChatSpendRefusal) {
690
707
  const refusal = workflowRefusalResponse("workflow-spend-exhausted", logCtx);
@@ -692,9 +709,9 @@ export async function runNativeChatAttempt(
692
709
  return refusal;
693
710
  }
694
711
  if (isTranslatorBudgetExceededError(error)) {
695
- return fail(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
712
+ return failLocal(413, "request translation buffer exceeded the safe limit", "request_too_large", "translation_buffer_limit");
696
713
  }
697
- return fail(502, error instanceof Error ? error.message : String(error), "server_error");
714
+ return failUpstream(502, error instanceof Error ? error.message : String(error), "server_error", undefined, "transport_failed");
698
715
  }
699
716
  releaseRetainedRequest();
700
717
 
@@ -710,7 +727,7 @@ export async function runNativeChatAttempt(
710
727
  cleanupAbort();
711
728
  if (req.signal.aborted) {
712
729
  upstream.abort();
713
- return fail(499, "Client cancelled request", "client_cancelled");
730
+ return failLocal(499, "Client cancelled request", "client_cancelled");
714
731
  }
715
732
  const detail = activeAdapter.formatErrorBody?.(response.status, response.headers, bodyText) ?? "";
716
733
  let upstreamType: string | undefined;
@@ -772,14 +789,17 @@ export async function runNativeChatAttempt(
772
789
  upstreamRetryAfter: response.headers.get("retry-after"),
773
790
  });
774
791
  finishLog(status, classified.message);
775
- const rewritten = new Response(JSON.stringify(chatCompletionsErrorBody(status, classified.message, classified.type, classified.code)), {
792
+ const rewritten = new Response(JSON.stringify(ownedChatErrorPayload({ status, type: classified.type, code: classified.code,
793
+ evidence: { status: response.status, type: upstreamType, code: upstreamCode ?? undefined, message: upstreamMessage ?? detail },
794
+ localReason: wasAccountAutoPaused(response) ? "credentials_paused" : undefined })), {
776
795
  status,
777
796
  headers: {
778
797
  "Content-Type": "application/json",
779
- ...(retryAfter ? { "Retry-After": retryAfter } : {}),
798
+ ...(formatClientRetryAfter(retryAfter) ? { "Retry-After": formatClientRetryAfter(retryAfter)! } : {}),
780
799
  ...(replayRefusal ? REPLAY_REFUSAL_CLIENT_HEADERS : {}),
781
800
  },
782
801
  });
802
+ attachComboFailureEvidence(rewritten, JSON.stringify(chatCompletionsErrorBody(status, classified.message, classified.type, classified.code)));
783
803
  return replayRefusal ? retainReplayRefusal(rewritten) : rewritten;
784
804
  }
785
805
 
@@ -802,7 +822,7 @@ export async function runNativeChatAttempt(
802
822
  },
803
823
  {
804
824
  abortSignal: upstream.signal,
805
- label: safeHostLabel(activeRequest.url),
825
+ label: safeHostLabel(activeRequest.url), destination: errorDestinationLabel(activeRequest.url),
806
826
  attempts: remainingTransientSends(),
807
827
  // The origin returned a head and may already be running the turn, so this is the
808
828
  // ambiguous row of the stage table. Only the operator allowance the Responses stream
@@ -815,7 +835,11 @@ export async function runNativeChatAttempt(
815
835
  (replacement.headers.get("content-type") ?? "").toLowerCase().includes("text/event-stream"),
816
836
  },
817
837
  );
838
+ const failureVerdictSink: FailureVerdictSink = {};
839
+ const collectorSource = requestedStream ? undefined : new ChatCompletionsCollectorSource(translatorBudget);
818
840
  const stream = nativeChatSse(resilientBody, {
841
+ failureVerdictSink,
842
+ collectorSource,
819
843
  requestedModel,
820
844
  translatorBudget,
821
845
  signal: upstream.signal,
@@ -867,13 +891,13 @@ export async function runNativeChatAttempt(
867
891
  });
868
892
  }
869
893
  try {
870
- const completion = await collectChatCompletion(stream, requestedModel, translatorBudget);
894
+ const completion = await collectChatCompletion(stream, requestedModel, translatorBudget, failureVerdictSink, collectorSource);
871
895
  cleanupAbort();
872
896
  // A cancelled native relay closes its downstream body. EOF alone must not
873
897
  // promote the buffered prefix to a successful Chat completion. A terminal
874
898
  // already accepted by the relay retains precedence over a later abort.
875
899
  if (req.signal.aborted && terminalStatus === undefined) {
876
- return fail(499, "Client cancelled request", "client_cancelled");
900
+ return failLocal(499, "Client cancelled request", "client_cancelled");
877
901
  }
878
902
  finishLog(200);
879
903
  return Response.json(completion);
@@ -881,12 +905,12 @@ export async function runNativeChatAttempt(
881
905
  cleanupAbort();
882
906
  upstream.abort();
883
907
  if (req.signal.aborted && terminalStatus === undefined) {
884
- return fail(499, "Client cancelled request", "client_cancelled");
908
+ return failLocal(499, "Client cancelled request", "client_cancelled");
885
909
  }
886
910
  if (isChatCompletionsStreamError(error)) {
887
- return fail(error.status, error.message, error.type, error.code);
911
+ return failUpstream(error.status, error.message, error.type, error.code);
888
912
  }
889
- return fail(502, error instanceof Error ? error.message : String(error), "upstream_error");
913
+ return failUpstream(502, error instanceof Error ? error.message : String(error), "upstream_error", undefined, "transport_failed");
890
914
  }
891
915
  }
892
916
 
@@ -902,24 +926,25 @@ export async function runNativeChatAttempt(
902
926
  } catch (error) {
903
927
  cleanupAbort();
904
928
  upstream.abort();
905
- if (req.signal.aborted) return fail(499, "Client cancelled request", "client_cancelled");
906
- return fail(502, error instanceof Error ? error.message : String(error), "upstream_error");
929
+ if (req.signal.aborted) return failLocal(499, "Client cancelled request", "client_cancelled");
930
+ return failUpstream(502, error instanceof Error ? error.message : String(error), "upstream_error", undefined, "transport_failed");
907
931
  }
908
932
  cleanupAbort();
909
933
  if (body.oversized) {
910
934
  upstream.abort();
911
- return fail(502, "upstream response exceeded the safe limit", "upstream_error", "translation_buffer_limit");
935
+ return failUpstream(502, "upstream response exceeded the safe limit", "upstream_error", "translation_buffer_limit");
912
936
  }
913
937
  let parsedJson: unknown;
914
938
  try {
915
939
  parsedJson = JSON.parse(body.text);
916
940
  } catch {
917
- return fail(502, "upstream returned malformed Chat Completions JSON", "upstream_error");
941
+ return failUpstream(502, "upstream returned malformed Chat Completions JSON", "upstream_error");
918
942
  }
919
943
  const error = structuredError(parsedJson);
920
- if (error) return fail(error.status ?? 502, error.message, error.type, error.code);
921
- const completion = chatCompletionJson(parsedJson);
922
- if (!completion) return fail(502, "upstream response contained no choices", "upstream_error");
944
+ if (error) return failUpstream(error.status ?? 502, error.message, error.type, error.code);
945
+ const originalCompletion = chatCompletionJson(parsedJson);
946
+ const completion = originalCompletion ? normalizeChatChoiceErrors(originalCompletion) : null;
947
+ if (!completion) return failUpstream(502, "upstream response contained no choices", "upstream_error");
923
948
  reasoningCapture.json(completion);
924
949
  const usage = usageFromChat(completion.usage);
925
950
  if (usage) {
@@ -929,7 +954,7 @@ export async function runNativeChatAttempt(
929
954
  }
930
955
  }
931
956
  if (clientDeclaredEmptyToolCatalog && chatResponseHasUndeclaredToolCalls(completion, declaredLegacyFunctionNames)) {
932
- return fail(502, UNDECLARED_CHAT_TOOL_MESSAGE, "upstream_error", UNDECLARED_TOOL_CALL_ERROR_CODE);
957
+ return failUpstream(502, UNDECLARED_CHAT_TOOL_MESSAGE, "upstream_error", UNDECLARED_TOOL_CALL_ERROR_CODE);
933
958
  }
934
959
  onFirstOutput?.();
935
960
  try {
@@ -946,7 +971,7 @@ export async function runNativeChatAttempt(
946
971
  });
947
972
  } catch (error) {
948
973
  if (isTranslatorBudgetExceededError(error)) {
949
- return fail(502, "upstream translation buffer exceeded the safe limit", "upstream_error", "translation_buffer_limit");
974
+ return failUpstream(502, "upstream translation buffer exceeded the safe limit", "upstream_error", "translation_buffer_limit");
950
975
  }
951
976
  throw error;
952
977
  }