@bitkyc08/opencodex 2.97.0 → 2.98.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. package/bin/ocx.mjs +1 -1
  2. package/gui/dist/assets/App-6TFtZsUF.css +1 -0
  3. package/gui/dist/assets/App-FQ0OtiMJ.js +59 -0
  4. package/gui/dist/assets/Tray-D2d5wqLq.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-A03mw_9y.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-BjTIN4to.js +94 -0
  111. package/gui/dist/assets/segment-thumb-features-Bf6vpwf8.js +1 -0
  112. package/gui/dist/classic/assets/App-C9Eg3nUo.js +58 -0
  113. package/gui/dist/{assets/Tray-CEw0y0jR.js → classic/assets/Tray-B8P8cokE.js} +1 -1
  114. package/gui/dist/classic/assets/index-DFAFnven.js +94 -0
  115. package/gui/dist/classic/assets/quota-summary-C-4HP4_5.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/google.ts +7 -7
  123. package/src/adapters/kiro/adapter.ts +18 -3
  124. package/src/adapters/kiro/conversation.ts +8 -8
  125. package/src/adapters/kiro/payload.ts +45 -12
  126. package/src/adapters/kiro/stream.ts +169 -12
  127. package/src/adapters/kiro/structured-output-json.ts +191 -0
  128. package/src/adapters/kiro/structured-output-schema.ts +542 -0
  129. package/src/adapters/kiro/structured-output.ts +367 -0
  130. package/src/adapters/kiro-constants.ts +9 -0
  131. package/src/adapters/kiro-tools.ts +14 -9
  132. package/src/adapters/kiro-wire.ts +9 -5
  133. package/src/adapters/openai-chat/messages.ts +58 -5
  134. package/src/adapters/openai-chat/mistral-image-limit.ts +50 -0
  135. package/src/adapters/openai-chat/passthrough.ts +5 -3
  136. package/src/adapters/openai-chat/tool-schema.ts +83 -13
  137. package/src/adapters/openai-responses/opengateway-input.ts +18 -6
  138. package/src/adapters/openai-responses/tool-output-recovery.ts +79 -0
  139. package/src/adapters/qoder/live-models.ts +115 -1
  140. package/src/claude/failure-verdict.ts +21 -0
  141. package/src/claude/inbound-content-options.ts +15 -2
  142. package/src/claude/inbound-model-options.ts +2 -1
  143. package/src/claude/inbound-records.ts +2 -0
  144. package/src/claude/inbound.ts +17 -11
  145. package/src/claude/outbound.ts +52 -23
  146. package/src/cli/capabilities-access-remote.ts +13 -0
  147. package/src/cli/capabilities-base.ts +1 -0
  148. package/src/cli/capabilities-provider-models.ts +3 -3
  149. package/src/cli/codex-shim-autorestore.ts +7 -0
  150. package/src/cli/config-command.ts +11 -2
  151. package/src/cli/connect.ts +17 -2
  152. package/src/cli/dispatch.ts +3 -1
  153. package/src/cli/doctor.ts +95 -7
  154. package/src/cli/index.ts +12 -7
  155. package/src/cli/link-apply-restart.ts +9 -2
  156. package/src/cli/models.ts +124 -38
  157. package/src/cli/registry.ts +1 -1
  158. package/src/cli/restart-failure.ts +17 -0
  159. package/src/cli/stop-approval.ts +2 -2
  160. package/src/cli/sync-command.ts +1 -1
  161. package/src/cli/sync-fanout.ts +15 -4
  162. package/src/cli/system-restart-client.ts +5 -0
  163. package/src/cli/uninstall-client-state.ts +12 -1
  164. package/src/cli/update-restart-child.ts +13 -3
  165. package/src/cli/update-restart-home.ts +6 -3
  166. package/src/cli/update-restart.ts +36 -19
  167. package/src/client/connect.ts +4 -1
  168. package/src/client/hub-client.ts +5 -0
  169. package/src/client/link-management-relay.ts +39 -2
  170. package/src/client/link-readiness.ts +3 -1
  171. package/src/client/machine-api.ts +2 -1
  172. package/src/client/machine-listener.ts +7 -0
  173. package/src/client/orphan-token-recovery.ts +322 -0
  174. package/src/codex/catalog/provider-models.ts +12 -4
  175. package/src/codex/native-profile-api.ts +27 -1
  176. package/src/codex/shim-diagnostics.ts +7 -0
  177. package/src/codex/shim-legacy-record.ts +290 -0
  178. package/src/codex/shim-windows-refresh.ts +9 -2
  179. package/src/codex/shim-wrapper-write.ts +7 -0
  180. package/src/codex/shim.ts +80 -21
  181. package/src/config/diagnostics.ts +9 -9
  182. package/src/config/load-degrade.ts +27 -27
  183. package/src/config/provider-validation.ts +9 -2
  184. package/src/config/removed-opencode-free-backup.ts +6 -3
  185. package/src/config/tracked-runtime-inspection.ts +3 -3
  186. package/src/generated/compatibility-version.json +219 -147
  187. package/src/images/loop.ts +2 -2
  188. package/src/lib/client-error-body.ts +37 -6
  189. package/src/lib/config-ownership-removal.ts +422 -0
  190. package/src/lib/config-ownership.ts +646 -81
  191. package/src/lib/context-overflow-verdict.ts +3 -3
  192. package/src/lib/crash-guard.ts +4 -2
  193. package/src/lib/desktop-cli-handoff.d.mts +3 -4
  194. package/src/lib/desktop-cli-handoff.mjs +7 -16
  195. package/src/lib/errors.ts +1 -0
  196. package/src/lib/local-management-capability.ts +3 -1
  197. package/src/lib/messages-error-body.ts +162 -0
  198. package/src/lib/messages-sse-error-presentation.ts +93 -0
  199. package/src/lib/passive-read.ts +9 -4
  200. package/src/lib/socks5-fetch.ts +21 -9
  201. package/src/lib/upstream-retry.ts +81 -1
  202. package/src/lib/windows-owner-acl.ts +9 -3
  203. package/src/lib/winsw.ts +5 -5
  204. package/src/lib/workflow-budget.ts +3 -1
  205. package/src/oauth/index.ts +7 -0
  206. package/src/oauth/store.ts +20 -10
  207. package/src/protocols/codecs/messages-ir.ts +15 -9
  208. package/src/protocols/encoders/adapter-events.ts +26 -7
  209. package/src/protocols/encoders/messages.ts +72 -28
  210. package/src/providers/antigravity-models.ts +45 -12
  211. package/src/providers/free-directory.ts +2 -2
  212. package/src/providers/reasoning-metadata.ts +2 -1
  213. package/src/providers/registry/entries-extended.ts +9 -0
  214. package/src/reasoning-effort.ts +7 -2
  215. package/src/remote/hub-usage.ts +3 -0
  216. package/src/server/admission-model-scope.ts +1 -1
  217. package/src/server/admitted-http-turn.ts +2 -1
  218. package/src/server/anthropic-rate-limit-headers.ts +75 -3
  219. package/src/server/claude-messages.ts +130 -67
  220. package/src/server/gui-static.ts +4 -1
  221. package/src/server/hub-usage.ts +7 -2
  222. package/src/server/index/link-management.ts +8 -0
  223. package/src/server/index/ollama-routes.ts +2 -0
  224. package/src/server/index/serve-options.ts +23 -20
  225. package/src/server/index/startup-warnings.ts +5 -12
  226. package/src/server/index/websocket-handler.ts +1 -0
  227. package/src/server/inference/client-encoder-delivery.ts +5 -1
  228. package/src/server/management/logs-usage-routes.ts +89 -4
  229. package/src/server/management/provider-routes.ts +2 -22
  230. package/src/server/management/qoder-provider-probe.ts +105 -0
  231. package/src/server/management/route-registry.ts +3 -0
  232. package/src/server/management/usage-aggregate-cache.ts +15 -5
  233. package/src/server/management/usage-source-facets.ts +38 -0
  234. package/src/server/messages-native-tool-guard.ts +11 -3
  235. package/src/server/messages-native.ts +111 -40
  236. package/src/server/messages-response-headers.ts +25 -11
  237. package/src/server/messages-shared.ts +94 -64
  238. package/src/server/messages-sse-error-filter.ts +199 -0
  239. package/src/server/request-log.ts +12 -0
  240. package/src/server/responses/adapter-continuation.ts +11 -5
  241. package/src/server/responses/adapter-delivery.ts +14 -8
  242. package/src/server/responses/adapter-dispatch.ts +8 -8
  243. package/src/server/responses/anthropic-thinking-recovery.ts +2 -2
  244. package/src/server/responses/codex-auth-error.ts +8 -4
  245. package/src/server/responses/codex-ws-wire.ts +19 -3
  246. package/src/server/responses/compact.ts +4 -1
  247. package/src/server/responses/core-auth.ts +1 -1
  248. package/src/server/responses/core-combo-failure.ts +2 -0
  249. package/src/server/responses/core-combo.ts +2 -0
  250. package/src/server/responses/core-errors.ts +5 -4
  251. package/src/server/responses/core-options.ts +2 -0
  252. package/src/server/responses/empty-completion-guard.ts +4 -1
  253. package/src/server/responses/pacing-overload.ts +1 -1
  254. package/src/server/responses/passthrough-dispatch.ts +5 -5
  255. package/src/server/responses/request-prepare.ts +3 -1
  256. package/src/server/responses/request-transport.ts +9 -8
  257. package/src/server/responses/response-effects.ts +3 -2
  258. package/src/server/responses/terminal-guard.ts +30 -0
  259. package/src/server/restart-replacement.ts +3 -1
  260. package/src/server/sse-payload-rewrite.ts +23 -11
  261. package/src/server/workflow-refusal.ts +1 -1
  262. package/src/server/ws-bridge.ts +4 -0
  263. package/src/service/definition.ts +36 -4
  264. package/src/service/launchd.ts +89 -32
  265. package/src/service/logging.ts +34 -7
  266. package/src/service/plist-xml.ts +59 -0
  267. package/src/service/state.ts +8 -0
  268. package/src/service/systemd-env.ts +2 -0
  269. package/src/service/systemd.ts +31 -7
  270. package/src/service/windows-taskxml.ts +16 -7
  271. package/src/service-manager-probe.ts +4 -10
  272. package/src/types/provider.ts +2 -0
  273. package/src/types/request.ts +13 -1
  274. package/src/update/config-preflight.ts +48 -0
  275. package/src/update/index.ts +15 -1
  276. package/src/usage/client.ts +43 -0
  277. package/src/usage/error-detail.ts +1 -1
  278. package/src/usage/log.ts +4 -0
  279. package/src/usage/source-facets.ts +51 -0
  280. package/src/usage/source-filter.ts +65 -0
  281. package/src/usage/summary.ts +50 -8
  282. package/src/web-search/anthropic-executor.ts +105 -24
  283. package/src/web-search/executor.ts +5 -3
  284. package/src/web-search/loop.ts +2 -1
  285. package/src/web-search/progress-stream.ts +64 -5
  286. package/gui/dist/assets/App-DuNhggE1.js +0 -58
  287. package/gui/dist/assets/index-CynE_Rue.js +0 -94
  288. package/gui/dist/assets/quota-summary-VHFSlUuM.js +0 -1
  289. /package/gui/dist/{assets → classic/assets}/App-B4j-Txmb.css +0 -0
  290. /package/gui/dist/{assets → classic/assets}/index-CHWwDo9L.css +0 -0
@@ -1,6 +1,7 @@
1
1
  import { baseProviderLabel } from "../providers/label";
2
2
  import { canonicalAntigravityUsageModel } from "../providers/antigravity-models";
3
3
  import { usageDisplayTotalTokens } from "./totals";
4
+ import { isLegacyCodexUsage, matchesUsageSource, snapshotConfiguredKeyIds, type UsageSourceFilter } from "./source-filter";
4
5
  import type { UsageTimeWindow } from "./time-range";
5
6
  import { isUnresolvedRequestedModel, usageModelPriceOptions } from "./model-identity";
6
7
  import {
@@ -210,6 +211,17 @@ export interface UsageSummary {
210
211
  models: UsageModel[];
211
212
  providers: UsageProvider[];
212
213
  accounts: UsageAccount[];
214
+ sourceFilter?: UsageSourceFilter;
215
+ legacyUnclassifiedRequests?: number;
216
+ }
217
+
218
+ export interface UsageSummaryFilterInput {
219
+ provider?: string | null;
220
+ model?: string | null;
221
+ apiKeyId?: string | null;
222
+ sourceFilter?: UsageSourceFilter;
223
+ /** Internal snapshot, never a client-supplied list or part of the response echo. */
224
+ configuredApiKeyIds?: readonly string[];
213
225
  }
214
226
 
215
227
  /**
@@ -765,6 +777,7 @@ interface UsagePartition {
765
777
  dayStart: number;
766
778
  surface: UsagePartitionSurface;
767
779
  oldestTimestamp: number | null;
780
+ legacyUnclassifiedRequests: number;
768
781
  totals: UsageSummaryTotals;
769
782
  models: Map<string, UsageModelAccumulator>;
770
783
  providers?: Map<string, UsageModelAccumulator>;
@@ -782,6 +795,9 @@ interface NormalizedUsageFilter {
782
795
  provider: string | null;
783
796
  model: string | null;
784
797
  apiKeyId: string | null;
798
+ sourceFilter?: UsageSourceFilter;
799
+ configuredApiKeyIds?: readonly string[];
800
+ configuredApiKeyIdSet?: ReadonlySet<string>;
785
801
  }
786
802
 
787
803
  export interface UsageSummaryAccumulator {
@@ -1058,6 +1074,7 @@ function projectedEntryForFilter(
1058
1074
  filter: NormalizedUsageFilter,
1059
1075
  ): { entry: PersistedUsageEntry; comboOverlap: boolean } | null {
1060
1076
  if (filter.apiKeyId !== null && entry.apiKeyId !== filter.apiKeyId) return null;
1077
+ if (filter.sourceFilter && !matchesUsageSource(entry, filter.sourceFilter, filter.configuredApiKeyIdSet)) return null;
1061
1078
  if (!entry.attempts?.length) {
1062
1079
  const identity = usageModelIdentity(entry.provider, entry.model, entry.resolvedModel);
1063
1080
  return filterMatchesAttribution(filter, entry.provider, identity.model)
@@ -1285,19 +1302,31 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1285
1302
  private estimatedRetainedBytes = ESTIMATED_ACCUMULATOR_BASE_BYTES;
1286
1303
 
1287
1304
  constructor(options?: {
1288
- filter?: { provider?: string | null; model?: string | null; apiKeyId?: string | null };
1305
+ filter?: UsageSummaryFilterInput;
1289
1306
  mode?: UsageAccumulatorMode;
1290
1307
  window?: UsageTimeWindow;
1291
1308
  }) {
1292
1309
  const provider = normalizeFilterValue(options?.filter?.provider);
1293
1310
  const model = normalizeFilterValue(options?.filter?.model);
1294
1311
  const apiKeyId = normalizeExactFilterValue(options?.filter?.apiKeyId);
1295
- this.filter = provider === null && model === null && apiKeyId === null
1312
+ const inputSource = options?.filter?.sourceFilter;
1313
+ const sourceFilter = inputSource ? { client: inputSource.client, device: { ...inputSource.device } } : undefined;
1314
+ const configuredApiKeyIds = options?.filter?.configuredApiKeyIds
1315
+ ? snapshotConfiguredKeyIds(options.filter.configuredApiKeyIds) : undefined;
1316
+ if (sourceFilter?.device.kind === "unconfigured" && !configuredApiKeyIds) {
1317
+ throw new Error("configured key snapshot required");
1318
+ }
1319
+ this.filter = provider === null && model === null && apiKeyId === null && !sourceFilter
1296
1320
  ? null
1297
- : { provider, model, apiKeyId };
1321
+ : { provider, model, apiKeyId, ...(sourceFilter ? { sourceFilter } : {}),
1322
+ ...(configuredApiKeyIds ? { configuredApiKeyIds, configuredApiKeyIdSet: new Set(configuredApiKeyIds) } : {}) };
1298
1323
  this.mode = options?.mode ?? "exact";
1299
1324
  this.window = options?.window ? Object.freeze({ ...options.window }) : undefined;
1300
1325
  this.requestIds = this.mode === "exact" ? new Map() : null;
1326
+ if (configuredApiKeyIds) {
1327
+ // Account conservatively for the array, set, and serialized retained cache identity.
1328
+ this.estimatedRetainedBytes += configuredApiKeyIds.reduce((bytes, id) => bytes + 128 + id.length * 8, 0);
1329
+ }
1301
1330
  }
1302
1331
 
1303
1332
  get snapshotWindow(): { start: number | null; end: number | null } {
@@ -1371,6 +1400,7 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1371
1400
  dayStart,
1372
1401
  surface,
1373
1402
  oldestTimestamp: null,
1403
+ legacyUnclassifiedRequests: 0,
1374
1404
  totals: blankTotals(),
1375
1405
  models: new Map(),
1376
1406
  ...(this.mode === "row-unique" ? { providers: new Map() } : {}),
@@ -1546,7 +1576,7 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1546
1576
  return label;
1547
1577
  }
1548
1578
 
1549
- add(sourceEntry: PersistedUsageEntry): void {
1579
+ private noteSnapshot(sourceEntry: PersistedUsageEntry): void {
1550
1580
  if (Number.isFinite(sourceEntry.timestamp)) {
1551
1581
  this.snapshotStart = this.snapshotStart === null
1552
1582
  ? sourceEntry.timestamp
@@ -1555,13 +1585,19 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1555
1585
  ? sourceEntry.timestamp
1556
1586
  : Math.max(this.snapshotEnd, sourceEntry.timestamp);
1557
1587
  }
1588
+ }
1589
+
1590
+ add(sourceEntry: PersistedUsageEntry): void {
1591
+ if (!this.filter?.sourceFilter) this.noteSnapshot(sourceEntry);
1558
1592
  if (this.window && (!Number.isFinite(sourceEntry.timestamp)
1559
1593
  || sourceEntry.timestamp < this.window.since || sourceEntry.timestamp > this.window.until)) return;
1560
1594
  const projected = this.filter ? projectedEntryForFilter(sourceEntry, this.filter) : { entry: sourceEntry, comboOverlap: false };
1561
1595
  if (!projected) return;
1596
+ if (this.filter?.sourceFilter) this.noteSnapshot(sourceEntry);
1562
1597
  this.comboOverlap ||= projected.comboOverlap;
1563
1598
  const entry = projected.entry;
1564
1599
  const partition = this.partitionFor(entry);
1600
+ if (this.filter?.sourceFilter && isLegacyCodexUsage(entry)) partition.legacyUnclassifiedRequests += 1;
1565
1601
  const costInfo = computeEntryCost(entry);
1566
1602
  bumpStatus(partition.totals, entry.usageStatus);
1567
1603
  partition.totals.attemptCount += entry.attempts?.length ?? 1;
@@ -1634,10 +1670,12 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1634
1670
  const dayAccumulators = new Map<string, UsageDayAccumulator>();
1635
1671
  const modelOverlaps: UsageModelOverlap[] = [];
1636
1672
  let oldestTimestamp: number | null = null;
1673
+ let legacyUnclassifiedRequests = 0;
1637
1674
 
1638
1675
  for (const partition of this.partitions.values()) {
1639
1676
  if (!usageSurfaceMatches(partition.surface, surface)) continue;
1640
1677
  if (!this.window && since !== null && partition.dayStart < since) continue;
1678
+ legacyUnclassifiedRequests += partition.legacyUnclassifiedRequests;
1641
1679
  mergeTotals(totals, partition.totals);
1642
1680
  mergeModelMaps(models, partition.models);
1643
1681
  if (partition.providers) mergeModelMaps(providers, partition.providers);
@@ -1713,6 +1751,8 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1713
1751
  models: buildUsageModels(models, totals.totalTokens, modelOverlaps),
1714
1752
  providers: buildUsageProviders(this.mode === "row-unique" ? providers : models, totals.totalTokens),
1715
1753
  accounts: buildUsageAccounts(accounts),
1754
+ ...(this.filter?.sourceFilter ? { sourceFilter: { client: this.filter.sourceFilter.client,
1755
+ device: { ...this.filter.sourceFilter.device } }, legacyUnclassifiedRequests } : {}),
1716
1756
  };
1717
1757
  if (!this.filter) return summary;
1718
1758
  const matches = (provider: string, model: string): boolean =>
@@ -1742,7 +1782,7 @@ class StreamingUsageSummaryAccumulator implements UsageSummaryAccumulator {
1742
1782
  }
1743
1783
 
1744
1784
  export function createUsageSummaryAccumulator(options?: {
1745
- filter?: { provider?: string | null; model?: string | null; apiKeyId?: string | null };
1785
+ filter?: UsageSummaryFilterInput;
1746
1786
  mode?: UsageAccumulatorMode;
1747
1787
  window?: UsageTimeWindow;
1748
1788
  }): UsageSummaryAccumulator {
@@ -1790,15 +1830,16 @@ function normalizeExactFilterValue(input: string | null | undefined): string | n
1790
1830
  */
1791
1831
  export function projectUsageSummary<T extends UsageSummary>(
1792
1832
  summary: T,
1793
- filter: { provider?: string | null; model?: string | null; apiKeyId?: string | null },
1833
+ filter: UsageSummaryFilterInput,
1794
1834
  entries?: PersistedUsageEntry[],
1795
1835
  ): T & { filter?: UsageFilterEcho } {
1796
1836
  const provider = normalizeFilterValue(filter.provider);
1797
1837
  const model = normalizeFilterValue(filter.model);
1798
1838
  const apiKeyId = normalizeExactFilterValue(filter.apiKeyId);
1799
- if (provider === null && model === null && apiKeyId === null) return summary;
1839
+ if (provider === null && model === null && apiKeyId === null && !filter.sourceFilter) return summary;
1800
1840
  const accumulator = createUsageSummaryAccumulator({
1801
- filter: { provider, model, apiKeyId },
1841
+ filter: { provider, model, apiKeyId, ...(filter.sourceFilter ? { sourceFilter: filter.sourceFilter } : {}),
1842
+ ...(filter.configuredApiKeyIds ? { configuredApiKeyIds: filter.configuredApiKeyIds } : {}) },
1802
1843
  ...(summary.customWindow && summary.since !== null && summary.until !== undefined
1803
1844
  ? { window: { since: summary.since, until: summary.until } } : {}),
1804
1845
  });
@@ -1811,6 +1852,7 @@ export function projectUsageSummary<T extends UsageSummary>(
1811
1852
  models: projected.models,
1812
1853
  providers: projected.providers,
1813
1854
  accounts: projected.accounts,
1855
+ ...(projected.sourceFilter ? { sourceFilter: projected.sourceFilter, legacyUnclassifiedRequests: projected.legacyUnclassifiedRequests } : {}),
1814
1856
  filter: projected.filter,
1815
1857
  };
1816
1858
  }
@@ -11,6 +11,7 @@ import { CLAUDE_CODE_HEADERS, claudeCodeSessionId } from "../adapters/client-fin
11
11
  import { signalWithTimeout, cancelBodyOnAbort } from "../lib/abort";
12
12
  import { sidecarEnter } from "../lib/sidecar-tracker";
13
13
  import { applyUpstreamRecoveryInit, fetchWithResetRetry } from "../lib/upstream-retry";
14
+ import { sseFieldValue } from "../lib/sse-decoder";
14
15
  import {
15
16
  MAX_SIDECAR_RESPONSE_BYTES,
16
17
  cancelReaderWithoutWaiting,
@@ -22,6 +23,8 @@ import { BASE_INSTRUCTION, IMAGE_INSTRUCTION, type SidecarOutcome, type SidecarS
22
23
  const ANTHROPIC_MAX_USES = 3;
23
24
  /** Answer budget; the injected tool_result is clamped downstream, so this only bounds the sidecar turn. */
24
25
  const ANTHROPIC_MAX_TOKENS = 8192;
26
+ /** A terminal exactly at the wire cap must prove EOF without holding a reader indefinitely. */
27
+ const ANTHROPIC_CAP_EOF_PROBE_TIMEOUT_MS = 1_000;
25
28
 
26
29
  function isRec(v: unknown): v is Record<string, unknown> {
27
30
  return !!v && typeof v === "object" && !Array.isArray(v);
@@ -60,7 +63,8 @@ async function readBoundedText(res: Response): Promise<string> {
60
63
  * Anthropic streams the FULL `web_search_tool_result.content` array on `content_block_start` (not via
61
64
  * deltas), so sources are collected there; the answer text arrives as `text_delta` events, and
62
65
  * `citations_delta` (web_search_result_location) contributes any additional cited URLs. A
63
- * `web_search_tool_result_error` content object yields no sources. Never throws.
66
+ * `web_search_tool_result_error` content object yields an error. Never throws; partial text is
67
+ * retained with an error when the search or stream did not complete successfully.
64
68
  */
65
69
  export async function parseAnthropicSidecarSSE(res: Response): Promise<SidecarOutcome> {
66
70
  const sources: WebSearchSource[] = [];
@@ -73,6 +77,10 @@ export async function parseAnthropicSidecarSSE(res: Response): Promise<SidecarOu
73
77
 
74
78
  let text = "";
75
79
  let sawToolResultError = false;
80
+ let failure: string | undefined;
81
+ let sawMessageStop = false;
82
+ let cleanEof = false;
83
+ let stopReason: string | undefined;
76
84
  if (!res.body) return { text: "", sources, error: "anthropic sidecar returned no response body" };
77
85
 
78
86
  const decoder = new TextDecoder();
@@ -80,8 +88,21 @@ export async function parseAnthropicSidecarSSE(res: Response): Promise<SidecarOu
80
88
  let buffer = "";
81
89
  let responseBytes = 0;
82
90
 
83
- const handleFrame = (data: Record<string, unknown>): void => {
84
- const type = typeof data.type === "string" ? data.type : "";
91
+ const handleFrame = (data: Record<string, unknown>, type: string, complete: boolean): void => {
92
+ if (type === "ping") return;
93
+ if (sawMessageStop) failure ??= "anthropic sidecar returned data after completion";
94
+ cleanEof = false;
95
+ if (type === "error") {
96
+ failure ??= "anthropic sidecar returned a stream error";
97
+ } else if (type === "message_stop") {
98
+ sawMessageStop = true;
99
+ } else if (type === "message_delta") {
100
+ const delta = isRec(data.delta) ? data.delta : {};
101
+ if (typeof delta.stop_reason === "string") {
102
+ stopReason = delta.stop_reason;
103
+ cleanEof = complete;
104
+ }
105
+ }
85
106
  if (type === "content_block_start") {
86
107
  const block = isRec(data.content_block) ? data.content_block : {};
87
108
  if (block.type === "web_search_tool_result") {
@@ -105,15 +126,61 @@ export async function parseAnthropicSidecarSSE(res: Response): Promise<SidecarOu
105
126
  };
106
127
 
107
128
  // Parse one SSE frame's `data:` payload and fold it. Shared by the streaming loop and the EOF flush.
108
- const processFrame = (rawFrame: string): void => {
109
- let dataLine = "";
129
+ const processFrame = (rawFrame: string, complete = true): void => {
130
+ const invalidFrame = (): void => {
131
+ cleanEof = false;
132
+ failure ??= sawMessageStop
133
+ ? "anthropic sidecar returned data after completion"
134
+ : "anthropic sidecar returned an invalid stream frame";
135
+ };
136
+ const dataLines: string[] = [];
137
+ let eventName: string | undefined;
110
138
  for (const line of rawFrame.split("\n")) {
111
- if (line.startsWith("data:")) dataLine += line.slice(line.startsWith("data: ") ? 6 : 5);
139
+ const dataValue = sseFieldValue(line, "data");
140
+ if (dataValue !== null) dataLines.push(dataValue);
141
+ const eventValue = sseFieldValue(line, "event");
142
+ if (eventValue !== null) eventName = eventValue;
112
143
  }
113
- if (!dataLine || dataLine === "[DONE]") return;
144
+ if (eventName === "ping") return;
145
+ if (dataLines.length === 0 && (complete || rawFrame.split("\n").every(line => !line.trim() || line.startsWith(":")))) return;
146
+ const dataLine = dataLines.join("\n");
147
+ if (!dataLine || dataLine === "[DONE]") { invalidFrame(); return; }
114
148
  let data: unknown;
115
- try { data = JSON.parse(dataLine); } catch { return; }
116
- if (isRec(data)) handleFrame(data);
149
+ try { data = JSON.parse(dataLine); } catch { invalidFrame(); return; }
150
+ if (isRec(data)) {
151
+ const type = eventName || (typeof data.type === "string" ? data.type : "");
152
+ handleFrame(data, type, complete);
153
+ } else invalidFrame();
154
+ };
155
+
156
+ const processCompleteFrames = (): void => {
157
+ let sep: number;
158
+ while ((sep = buffer.indexOf("\n\n")) !== -1) {
159
+ const rawFrame = buffer.slice(0, sep);
160
+ buffer = buffer.slice(sep + 2);
161
+ processFrame(rawFrame);
162
+ }
163
+ };
164
+
165
+ const verifyEofAtCap = async (): Promise<boolean> => {
166
+ let timer: ReturnType<typeof setTimeout> | undefined;
167
+ let expired = false;
168
+ const timeout = new Promise<null>(resolve => {
169
+ timer = setTimeout(() => { expired = true; resolve(null); }, ANTHROPIC_CAP_EOF_PROBE_TIMEOUT_MS);
170
+ });
171
+ try {
172
+ for (;;) {
173
+ if (expired) return false;
174
+ const next = await Promise.race([reader.read(), timeout]);
175
+ if (expired || next === null) return false;
176
+ if (next.done) return true;
177
+ if (next.value.byteLength > 0) return false;
178
+ // A source that synchronously produces empty chunks must not starve the probe timer.
179
+ await new Promise<void>(resolve => setTimeout(resolve, 0));
180
+ }
181
+ } catch {
182
+ return false;
183
+ } finally { if (timer !== undefined) clearTimeout(timer); }
117
184
  };
118
185
 
119
186
  try {
@@ -125,33 +192,47 @@ export async function parseAnthropicSidecarSSE(res: Response): Promise<SidecarOu
125
192
  const remaining = MAX_SIDECAR_RESPONSE_BYTES - responseBytes;
126
193
  const accepted = value.byteLength <= remaining ? value : value.subarray(0, remaining);
127
194
  responseBytes += accepted.byteLength;
128
- // Normalize CRLF on the ACCUMULATED buffer so a `\r\n` pair split across two network chunks
129
- // (chunk ends in `\r`, next starts with `\n`) still collapses to `\n` (audit round-2 F2).
130
- buffer = (buffer + decoder.decode(accepted, { stream: true })).replace(/\r\n/g, "\n");
131
- let sep: number;
132
- while ((sep = buffer.indexOf("\n\n")) !== -1) {
133
- const rawFrame = buffer.slice(0, sep);
134
- buffer = buffer.slice(sep + 2);
135
- processFrame(rawFrame);
136
- }
195
+ // Preserve a trailing CR until the next chunk so split CRLF remains one line break.
196
+ buffer = (buffer + decoder.decode(accepted, { stream: true })).replace(/\r\n/g, "\n").replace(/\r(?!$)/g, "\n");
197
+ processCompleteFrames();
137
198
  if (responseBytes >= MAX_SIDECAR_RESPONSE_BYTES) {
138
- // Keep the frames already folded above, drop the unterminated tail, and do not wait on
139
- // upstream teardown.
199
+ // A trailing CR can complete a CR-only terminal delimiter at the exact boundary.
200
+ if (buffer.endsWith("\r")) {
201
+ buffer = buffer.slice(0, -1) + "\n";
202
+ processCompleteFrames();
203
+ }
204
+ const trailingCommentsOnly = buffer.split("\n").every(line => !line.trim() || line.startsWith(":"));
205
+ // A complete terminal or stop-reason delta at the cap needs bounded EOF proof; an
206
+ // oversized chunk or partial trailing frame is an error. Never await upstream teardown.
207
+ if ((!sawMessageStop && !cleanEof) || value.byteLength > remaining || !trailingCommentsOnly) {
208
+ failure ??= "anthropic sidecar response exceeded the byte limit";
209
+ } else if (!failure && !sawToolResultError
210
+ && (stopReason === undefined || stopReason === "end_turn" || stopReason === "stop_sequence")
211
+ && !(await verifyEofAtCap())) {
212
+ failure = "anthropic sidecar response byte limit reached before EOF";
213
+ }
140
214
  cancelReaderWithoutWaiting(reader, "sidecar response byte limit reached");
141
215
  buffer = "";
142
216
  break;
143
217
  }
144
218
  }
145
219
  // Flush the decoder and process any final unterminated frame (a stream that ends without \n\n).
146
- buffer = (buffer + decoder.decode()).replace(/\r\n/g, "\n");
147
- if (buffer.trim().length > 0) processFrame(buffer);
220
+ buffer = (buffer + decoder.decode()).replace(/\r\n|\r/g, "\n");
221
+ processCompleteFrames();
222
+ if (buffer.trim().length > 0) processFrame(buffer, false);
148
223
  } catch {
149
- /* mid-stream abort/decode failure: fall through with whatever text/sources were gathered */
224
+ failure ??= "anthropic sidecar stream could not be read";
150
225
  }
151
226
 
152
227
  const trimmed = text.trim();
228
+ if (sawToolResultError) failure ??= "anthropic web search returned an error result";
229
+ if (stopReason !== undefined && stopReason !== "end_turn" && stopReason !== "stop_sequence") {
230
+ failure ??= "anthropic sidecar produced an incomplete answer";
231
+ }
232
+ if (!sawMessageStop && !cleanEof) failure ??= "anthropic sidecar stream ended before completion";
233
+ if (failure) return { text: trimmed, sources, error: failure };
153
234
  if (trimmed.length === 0) {
154
- return { text: "", sources, error: sawToolResultError ? "anthropic web search returned an error result" : "anthropic sidecar produced no answer" };
235
+ return { text: "", sources, error: "anthropic sidecar produced no answer" };
155
236
  }
156
237
  return { text: trimmed, sources };
157
238
  }
@@ -1,5 +1,5 @@
1
1
  import type { OcxProviderConfig } from "../types";
2
- import { mergeCodexSelectedHeaders } from "../codex/forward-transport-headers";
2
+ import { CODEX_RESPONSES_LITE_HEADER, mergeCodexSelectedHeaders } from "../codex/forward-transport-headers";
3
3
  import { signalWithTimeout, cancelBodyOnAbort } from "../lib/abort";
4
4
  import { redactSecretString } from "../lib/redact";
5
5
  import { sidecarEnter } from "../lib/sidecar-tracker";
@@ -84,9 +84,11 @@ export async function runWebSearch(
84
84
  if (settings.reserveCompatibility && settings.model === NATIVE_RESERVE_MODEL) {
85
85
  return { text: "", sources: [], error: "Luna Reserve compatibility is only available as a conversation model, not a search helper. Choose another search helper model." };
86
86
  }
87
- const headers = Object.fromEntries(mergeCodexSelectedHeaders(
87
+ const mergedHeaders = mergeCodexSelectedHeaders(
88
88
  { "Content-Type": "application/json", ...forwardProvider.headers }, selectedForwardHeaders,
89
- ));
89
+ );
90
+ mergedHeaders.delete(CODEX_RESPONSES_LITE_HEADER);
91
+ const headers = Object.fromEntries(mergedHeaders);
90
92
  const body = {
91
93
  model: settings.model,
92
94
  instructions: settings.describeImages ? BASE_INSTRUCTION + IMAGE_INSTRUCTION : BASE_INSTRUCTION,
@@ -33,7 +33,7 @@ import {
33
33
  TranslatorBudgetExceededError,
34
34
  } from "../lib/translator-budget";
35
35
  import { formatWebSearchResults } from "./format-result";
36
- import { parseStreamWithProgress, RoutedModelInactivityError, WebSearchStreamProtocolError, WebSearchAdapterEventError } from "./progress-stream";
36
+ import { parseStreamWithProgress, RoutedModelInactivityError, WebSearchConsumerInactivityError, WebSearchStreamProtocolError, WebSearchAdapterEventError } from "./progress-stream";
37
37
  import { WEB_SEARCH_TOOL_NAME } from "./synthetic-tool";
38
38
  import { newSidecarSearchCellId, rememberSidecarSearch, restoreSidecarSearchResults } from "./sidecar-search-replay";
39
39
  import {
@@ -916,6 +916,7 @@ export async function runWithWebSearch(deps: WebSearchLoopDeps): Promise<Respons
916
916
  }
917
917
  if (error instanceof LoopError) throw error;
918
918
  if (error instanceof RoutedModelInactivityError) throw new LoopError(504, error.message);
919
+ if (error instanceof WebSearchConsumerInactivityError) throw new LoopError(504, error.message);
919
920
  if (error instanceof WebSearchStreamProtocolError) throw new LoopError(502, error.message);
920
921
  throw new LoopError(502, `Provider stream error: ${error instanceof Error ? error.message : String(error)}`);
921
922
  } finally { releaseCapture(); }
@@ -8,6 +8,7 @@ import {
8
8
  } from "../lib/translator-budget";
9
9
 
10
10
  const DEFAULT_POST_TERMINAL_DRAIN_TIMEOUT_MS = 5_000;
11
+ const DEFAULT_CONSUMER_TIMEOUT_MS = 300_000;
11
12
 
12
13
  export class RoutedModelInactivityError extends Error {
13
14
  readonly timeoutMs: number;
@@ -26,6 +27,13 @@ export class WebSearchStreamProtocolError extends Error {
26
27
  }
27
28
  }
28
29
 
30
+ export class WebSearchConsumerInactivityError extends Error {
31
+ constructor(readonly timeoutMs: number) {
32
+ super(`Web-search consumer did not accept an event within ${timeoutMs}ms`);
33
+ this.name = "WebSearchConsumerInactivityError";
34
+ }
35
+ }
36
+
29
37
  /** Opt-in preserves an adapter refusal before it becomes an untyped parser exception. */
30
38
  export class WebSearchAdapterEventError extends Error {
31
39
  constructor(readonly event: Extract<AdapterEvent, { type: "error" }>, readonly replaySafe: boolean) {
@@ -40,6 +48,8 @@ export interface ParseStreamWithProgressOptions {
40
48
  signal?: AbortSignal;
41
49
  /** Kept configurable for focused tests; production callers should use the 5 second default. */
42
50
  postTerminalDrainTimeoutMs?: number;
51
+ /** Bounds an abandoned downstream consumer separately from upstream byte inactivity. */
52
+ consumerTimeoutMs?: number;
43
53
  /** Receives the usage an adapter `error` event carries before the stream fails on it. */
44
54
  onErrorUsage?: (usage: OcxUsage) => void;
45
55
  }
@@ -156,11 +166,19 @@ export async function* parseStreamWithProgress(
156
166
  options.postTerminalDrainTimeoutMs ?? DEFAULT_POST_TERMINAL_DRAIN_TIMEOUT_MS,
157
167
  "postTerminalDrainTimeoutMs",
158
168
  );
169
+ const consumerTimeoutMs = normalizeTimeout(
170
+ options.consumerTimeoutMs ?? Math.max(DEFAULT_CONSUMER_TIMEOUT_MS, inactivityTimeoutMs),
171
+ "consumerTimeoutMs",
172
+ );
159
173
  const handoff = new ProgressHandoff();
160
174
  const reader = response.body?.getReader();
161
175
  if (!reader) throw new WebSearchStreamProtocolError("upstream response has no body");
162
176
 
163
177
  let inactivityTimer: ReturnType<typeof setTimeout> | undefined;
178
+ let inactivityRemainingMs = inactivityTimeoutMs;
179
+ let inactivityArmedAt = 0;
180
+ let consumerBlocked = false;
181
+ let terminalSeen = false;
164
182
  let tappedController: ReadableStreamDefaultController<Uint8Array> | undefined;
165
183
  let settled = false;
166
184
  let iterator: AsyncGenerator<AdapterEvent> | undefined;
@@ -210,10 +228,46 @@ export async function* parseStreamWithProgress(
210
228
  cancelReader(reason);
211
229
  };
212
230
 
231
+ // Count only active upstream-wait time; a blocked downstream handoff has its own deadline.
232
+ const armInactivity = (): void => {
233
+ if (settled || consumerBlocked || terminalSeen || inactivityTimer !== undefined) return;
234
+ if (inactivityRemainingMs <= 0) {
235
+ fail(new RoutedModelInactivityError(inactivityTimeoutMs));
236
+ return;
237
+ }
238
+ inactivityArmedAt = performance.now();
239
+ inactivityTimer = setTimeout(() => fail(new RoutedModelInactivityError(inactivityTimeoutMs)), inactivityRemainingMs);
240
+ };
241
+
242
+ const pauseInactivity = (): void => {
243
+ if (inactivityTimer === undefined) return;
244
+ inactivityRemainingMs = Math.max(0, inactivityRemainingMs - (performance.now() - inactivityArmedAt));
245
+ clearInactivity();
246
+ if (inactivityRemainingMs <= 0) fail(new RoutedModelInactivityError(inactivityTimeoutMs));
247
+ };
248
+
213
249
  const resetInactivity = (): void => {
214
- if (settled) return;
250
+ if (settled || terminalSeen) return;
215
251
  clearInactivity();
216
- inactivityTimer = setTimeout(() => fail(new RoutedModelInactivityError(inactivityTimeoutMs)), inactivityTimeoutMs);
252
+ inactivityRemainingMs = inactivityTimeoutMs;
253
+ armInactivity();
254
+ };
255
+
256
+ const deliverWithTimeout = async (event: AdapterEvent, suspendInactivity: boolean): Promise<void> => {
257
+ if (suspendInactivity) {
258
+ consumerBlocked = true;
259
+ pauseInactivity();
260
+ }
261
+ const timer = setTimeout(() => fail(new WebSearchConsumerInactivityError(consumerTimeoutMs)), consumerTimeoutMs);
262
+ try {
263
+ await handoff.deliver(event);
264
+ } finally {
265
+ clearTimeout(timer);
266
+ if (suspendInactivity) {
267
+ consumerBlocked = false;
268
+ armInactivity();
269
+ }
270
+ }
217
271
  };
218
272
 
219
273
  const onAbort = (): void => fail(options.signal?.reason);
@@ -233,7 +287,11 @@ export async function* parseStreamWithProgress(
233
287
  controller.close();
234
288
  return;
235
289
  }
236
- if (result.value.byteLength === 0) continue;
290
+ if (result.value.byteLength === 0) {
291
+ // A synchronous empty-chunk producer can otherwise starve the timer in microtasks.
292
+ await new Promise<void>(resolve => setTimeout(resolve, 0));
293
+ continue;
294
+ }
237
295
  resetInactivity();
238
296
  handoff.offerProgress();
239
297
  controller.enqueue(result.value);
@@ -287,7 +345,7 @@ export async function* parseStreamWithProgress(
287
345
 
288
346
  if (result.done) {
289
347
  if (!heldTerminal) throw new WebSearchStreamProtocolError("adapter returned without a terminal event");
290
- await handoff.deliver(heldTerminal);
348
+ await deliverWithTimeout(heldTerminal, false);
291
349
  if (!settled) {
292
350
  settled = true;
293
351
  clearInactivity();
@@ -319,13 +377,14 @@ export async function* parseStreamWithProgress(
319
377
  replaySafe &&= event.type === "heartbeat" && event.replayUnsafe !== true;
320
378
  if (event.type === "done" || event.type === "incomplete") {
321
379
  heldTerminal = event;
380
+ terminalSeen = true;
322
381
  // Response-byte inactivity ends once the adapter has produced a terminal event.
323
382
  // From here the separate post-terminal drain guard owns the bounded wait for
324
383
  // iterator cleanup, so leaving the inactivity timer armed creates a false timeout.
325
384
  clearInactivity();
326
385
  continue;
327
386
  }
328
- await handoff.deliver(event);
387
+ await deliverWithTimeout(event, true);
329
388
  }
330
389
  } catch (error) {
331
390
  fail(isTranslatorBudgetExceededError(error)