@oscharko-dev/keiko 1.1.6 → 1.1.7

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 (211) hide show
  1. package/dist/ui/csp-hashes.json +7 -7
  2. package/dist/ui/static/404.html +1 -1
  3. package/dist/ui/static/__next.__PAGE__.txt +10 -10
  4. package/dist/ui/static/__next._full.txt +12 -12
  5. package/dist/ui/static/__next._tree.txt +1 -1
  6. package/dist/ui/static/_next/static/chunks/{1gb6os129fico.js → 01rp_j2a-73pp.js} +1 -1
  7. package/dist/ui/static/_next/static/chunks/02i0eppipph7e.js +1 -0
  8. package/dist/ui/static/_next/static/chunks/0hc828_gdkyek.js +3 -0
  9. package/dist/ui/static/_next/static/chunks/0hq0i12hw0ypm.js +1 -0
  10. package/dist/ui/static/_next/static/chunks/{0ak5o5brryicl.js → 18grpx4tuavds.js} +1 -1
  11. package/dist/ui/static/_next/static/chunks/1be0-x1mk56wu.js +1 -0
  12. package/dist/ui/static/_next/static/chunks/{0bbv42deftomm.js → 1c1gbfapof8m0.js} +1 -1
  13. package/dist/ui/static/_next/static/chunks/{3d-10944zk2o9.js → 1lj5se4u-t-e9.js} +1 -1
  14. package/dist/ui/static/_next/static/chunks/{1th8j9weqwhgx.js → 1o2cihsnxhmh6.js} +1 -1
  15. package/dist/ui/static/_next/static/chunks/22x3pxeia7hk_.js +1 -0
  16. package/dist/ui/static/_next/static/chunks/{0e6em7dg4gr6q.js → 25lgzefdnbdh6.js} +1 -1
  17. package/dist/ui/static/_next/static/chunks/{182b4sf3i-krz.js → 29p86cxry0h-l.js} +1 -1
  18. package/dist/ui/static/_next/static/chunks/{28mbnwl9g65al.js → 2ctd04x-fzbwc.js} +1 -1
  19. package/dist/ui/static/_next/static/chunks/2jfg687n_mvf8.js +1 -0
  20. package/dist/ui/static/_next/static/chunks/{2t2wftn5liinr.js → 2mnldrncbucm0.js} +1 -1
  21. package/dist/ui/static/_next/static/chunks/{21t1klb1thn76.js → 306ima8lxo17-.js} +1 -1
  22. package/dist/ui/static/_next/static/chunks/{2dpxapmdvqlh1.js → 36-tv3y56oau8.js} +1 -1
  23. package/dist/ui/static/_next/static/chunks/{0ks_l2sa4nfn0.js → 3aizxizjzdddj.js} +1 -1
  24. package/dist/ui/static/_next/static/chunks/{3doin5vaz4zjs.js → 3fuqgie3p2068.js} +1 -1
  25. package/dist/ui/static/_next/static/chunks/3k6g7nzn589fs.js +1 -0
  26. package/dist/ui/static/_next/static/chunks/3r4fz572q3l7s.js +1 -0
  27. package/dist/ui/static/_next/static/chunks/{1lb8ju8qa0d9m.js → 40g2_9ovqiuiu.js} +1 -1
  28. package/dist/ui/static/_not-found/__next._full.txt +9 -9
  29. package/dist/ui/static/_not-found/__next._not-found.__PAGE__.txt +8 -8
  30. package/dist/ui/static/_not-found/__next._tree.txt +1 -1
  31. package/dist/ui/static/_not-found.html +1 -1
  32. package/dist/ui/static/_not-found.txt +9 -9
  33. package/dist/ui/static/atlassian-connectors/__next._full.txt +13 -13
  34. package/dist/ui/static/atlassian-connectors/__next._tree.txt +1 -1
  35. package/dist/ui/static/atlassian-connectors/__next.atlassian-connectors.__PAGE__.txt +10 -10
  36. package/dist/ui/static/atlassian-connectors.html +1 -1
  37. package/dist/ui/static/atlassian-connectors.txt +13 -13
  38. package/dist/ui/static/index.html +1 -1
  39. package/dist/ui/static/index.txt +12 -12
  40. package/dist/ui/static/launch/__next._full.txt +12 -12
  41. package/dist/ui/static/launch/__next._tree.txt +1 -1
  42. package/dist/ui/static/launch/__next.launch.__PAGE__.txt +10 -10
  43. package/dist/ui/static/launch.html +1 -1
  44. package/dist/ui/static/launch.txt +12 -12
  45. package/dist/ui/static/local-knowledge/__next._full.txt +12 -12
  46. package/dist/ui/static/local-knowledge/__next._tree.txt +1 -1
  47. package/dist/ui/static/local-knowledge/__next.local-knowledge.__PAGE__.txt +10 -10
  48. package/dist/ui/static/local-knowledge/capsule/__next._full.txt +13 -13
  49. package/dist/ui/static/local-knowledge/capsule/__next._tree.txt +1 -1
  50. package/dist/ui/static/local-knowledge/capsule/__next.local-knowledge.capsule.__PAGE__.txt +10 -10
  51. package/dist/ui/static/local-knowledge/capsule.html +1 -1
  52. package/dist/ui/static/local-knowledge/capsule.txt +13 -13
  53. package/dist/ui/static/local-knowledge.html +1 -1
  54. package/dist/ui/static/local-knowledge.txt +12 -12
  55. package/node_modules/@oscharko-dev/keiko-cli/package.json +14 -14
  56. package/node_modules/@oscharko-dev/keiko-connectors/dist/version.d.ts +1 -1
  57. package/node_modules/@oscharko-dev/keiko-connectors/dist/version.js +1 -1
  58. package/node_modules/@oscharko-dev/keiko-connectors/package.json +3 -3
  59. package/node_modules/@oscharko-dev/keiko-contracts/dist/activity-log-failure-class-contracts.d.ts +43 -3
  60. package/node_modules/@oscharko-dev/keiko-contracts/dist/activity-log-failure-class-contracts.d.ts.map +1 -1
  61. package/node_modules/@oscharko-dev/keiko-contracts/dist/activity-log-failure-class-contracts.js +79 -2
  62. package/node_modules/@oscharko-dev/keiko-contracts/dist/activity-log-registry.generated.d.ts +645 -18
  63. package/node_modules/@oscharko-dev/keiko-contracts/dist/activity-log-registry.generated.d.ts.map +1 -1
  64. package/node_modules/@oscharko-dev/keiko-contracts/dist/activity-log-registry.generated.js +768 -16
  65. package/node_modules/@oscharko-dev/keiko-contracts/dist/bff-wire.d.ts +7 -2
  66. package/node_modules/@oscharko-dev/keiko-contracts/dist/bff-wire.d.ts.map +1 -1
  67. package/node_modules/@oscharko-dev/keiko-contracts/dist/coding-workbench-runtime-api-validation.js +2 -1
  68. package/node_modules/@oscharko-dev/keiko-contracts/dist/coding-workbench-runtime-constants.d.ts +1 -1
  69. package/node_modules/@oscharko-dev/keiko-contracts/dist/coding-workbench-runtime-constants.d.ts.map +1 -1
  70. package/node_modules/@oscharko-dev/keiko-contracts/dist/coding-workbench.d.ts +1 -1
  71. package/node_modules/@oscharko-dev/keiko-contracts/dist/coding-workbench.d.ts.map +1 -1
  72. package/node_modules/@oscharko-dev/keiko-contracts/dist/context-engineering.d.ts +3 -0
  73. package/node_modules/@oscharko-dev/keiko-contracts/dist/context-engineering.d.ts.map +1 -1
  74. package/node_modules/@oscharko-dev/keiko-contracts/dist/context-engineering.js +20 -4
  75. package/node_modules/@oscharko-dev/keiko-contracts/dist/version.d.ts +2 -2
  76. package/node_modules/@oscharko-dev/keiko-contracts/dist/version.js +2 -2
  77. package/node_modules/@oscharko-dev/keiko-contracts/package.json +1 -1
  78. package/node_modules/@oscharko-dev/keiko-evaluations/package.json +9 -9
  79. package/node_modules/@oscharko-dev/keiko-evidence/dist/version.d.ts +1 -1
  80. package/node_modules/@oscharko-dev/keiko-evidence/dist/version.js +1 -1
  81. package/node_modules/@oscharko-dev/keiko-evidence/package.json +4 -4
  82. package/node_modules/@oscharko-dev/keiko-git/dist/version.d.ts +1 -1
  83. package/node_modules/@oscharko-dev/keiko-git/dist/version.js +1 -1
  84. package/node_modules/@oscharko-dev/keiko-git/package.json +1 -1
  85. package/node_modules/@oscharko-dev/keiko-harness/dist/version.d.ts +1 -1
  86. package/node_modules/@oscharko-dev/keiko-harness/dist/version.js +1 -1
  87. package/node_modules/@oscharko-dev/keiko-harness/package.json +7 -7
  88. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/version.d.ts +1 -1
  89. package/node_modules/@oscharko-dev/keiko-local-knowledge/dist/version.js +1 -1
  90. package/node_modules/@oscharko-dev/keiko-local-knowledge/package.json +5 -5
  91. package/node_modules/@oscharko-dev/keiko-memory-capture/dist/version.d.ts +1 -1
  92. package/node_modules/@oscharko-dev/keiko-memory-capture/dist/version.js +1 -1
  93. package/node_modules/@oscharko-dev/keiko-memory-capture/package.json +3 -3
  94. package/node_modules/@oscharko-dev/keiko-memory-consolidation/dist/version.d.ts +1 -1
  95. package/node_modules/@oscharko-dev/keiko-memory-consolidation/dist/version.js +1 -1
  96. package/node_modules/@oscharko-dev/keiko-memory-consolidation/package.json +3 -3
  97. package/node_modules/@oscharko-dev/keiko-memory-governance/dist/version.d.ts +1 -1
  98. package/node_modules/@oscharko-dev/keiko-memory-governance/dist/version.js +1 -1
  99. package/node_modules/@oscharko-dev/keiko-memory-governance/package.json +3 -3
  100. package/node_modules/@oscharko-dev/keiko-memory-retrieval/dist/version.d.ts +1 -1
  101. package/node_modules/@oscharko-dev/keiko-memory-retrieval/dist/version.js +1 -1
  102. package/node_modules/@oscharko-dev/keiko-memory-retrieval/package.json +3 -3
  103. package/node_modules/@oscharko-dev/keiko-memory-vault/dist/version.d.ts +1 -1
  104. package/node_modules/@oscharko-dev/keiko-memory-vault/dist/version.js +1 -1
  105. package/node_modules/@oscharko-dev/keiko-memory-vault/package.json +3 -3
  106. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/config.d.ts.map +1 -1
  107. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/config.js +4 -1
  108. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/gateway.d.ts.map +1 -1
  109. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/gateway.js +127 -23
  110. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/openai-adapter.d.ts.map +1 -1
  111. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/openai-adapter.js +32 -1
  112. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/openai-embedding-adapter.d.ts.map +1 -1
  113. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/openai-embedding-adapter.js +12 -3
  114. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/realtime-voice-adapter.d.ts.map +1 -1
  115. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/realtime-voice-adapter.js +3 -1
  116. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/rerank-adapter.d.ts.map +1 -1
  117. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/rerank-adapter.js +4 -1
  118. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/resilience.d.ts +9 -1
  119. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/resilience.d.ts.map +1 -1
  120. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/resilience.js +87 -5
  121. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/speech-to-text-adapter.d.ts +1 -0
  122. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/speech-to-text-adapter.d.ts.map +1 -1
  123. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/speech-to-text-adapter.js +158 -10
  124. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/text-to-speech-adapter.d.ts.map +1 -1
  125. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/text-to-speech-adapter.js +176 -12
  126. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/version.d.ts +1 -1
  127. package/node_modules/@oscharko-dev/keiko-model-gateway/dist/version.js +1 -1
  128. package/node_modules/@oscharko-dev/keiko-model-gateway/package.json +4 -4
  129. package/node_modules/@oscharko-dev/keiko-quality-intelligence/package.json +3 -3
  130. package/node_modules/@oscharko-dev/keiko-sandbox/package.json +2 -2
  131. package/node_modules/@oscharko-dev/keiko-sdk/package.json +9 -9
  132. package/node_modules/@oscharko-dev/keiko-security/dist/errors/gateway.d.ts +7 -1
  133. package/node_modules/@oscharko-dev/keiko-security/dist/errors/gateway.d.ts.map +1 -1
  134. package/node_modules/@oscharko-dev/keiko-security/dist/errors/gateway.js +21 -0
  135. package/node_modules/@oscharko-dev/keiko-security/dist/version.d.ts +1 -1
  136. package/node_modules/@oscharko-dev/keiko-security/dist/version.js +1 -1
  137. package/node_modules/@oscharko-dev/keiko-security/package.json +2 -2
  138. package/node_modules/@oscharko-dev/keiko-server/dist/coding-sidecar-gateway.d.ts +6 -0
  139. package/node_modules/@oscharko-dev/keiko-server/dist/coding-sidecar-gateway.d.ts.map +1 -1
  140. package/node_modules/@oscharko-dev/keiko-server/dist/coding-sidecar-gateway.js +299 -104
  141. package/node_modules/@oscharko-dev/keiko-server/dist/deps.d.ts +15 -0
  142. package/node_modules/@oscharko-dev/keiko-server/dist/deps.d.ts.map +1 -1
  143. package/node_modules/@oscharko-dev/keiko-server/dist/diagnostics-log.d.ts +5 -1
  144. package/node_modules/@oscharko-dev/keiko-server/dist/diagnostics-log.d.ts.map +1 -1
  145. package/node_modules/@oscharko-dev/keiko-server/dist/diagnostics-log.js +11 -0
  146. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-readiness.d.ts +24 -2
  147. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-readiness.d.ts.map +1 -1
  148. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-readiness.js +215 -38
  149. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-route-deadline.d.ts +6 -0
  150. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-route-deadline.d.ts.map +1 -0
  151. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-route-deadline.js +27 -0
  152. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-setup.d.ts +33 -0
  153. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-setup.d.ts.map +1 -1
  154. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-setup.js +256 -25
  155. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-tool-calling-probe.d.ts +2 -1
  156. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-tool-calling-probe.d.ts.map +1 -1
  157. package/node_modules/@oscharko-dev/keiko-server/dist/gateway-tool-calling-probe.js +11 -4
  158. package/node_modules/@oscharko-dev/keiko-server/dist/gitDelivery/commitRoutes.d.ts +2 -1
  159. package/node_modules/@oscharko-dev/keiko-server/dist/gitDelivery/commitRoutes.d.ts.map +1 -1
  160. package/node_modules/@oscharko-dev/keiko-server/dist/gitDelivery/commitRoutes.js +221 -38
  161. package/node_modules/@oscharko-dev/keiko-server/package.json +22 -22
  162. package/node_modules/@oscharko-dev/keiko-tool-catalog/package.json +3 -3
  163. package/node_modules/@oscharko-dev/keiko-tools/dist/version.d.ts +1 -1
  164. package/node_modules/@oscharko-dev/keiko-tools/dist/version.js +1 -1
  165. package/node_modules/@oscharko-dev/keiko-tools/package.json +7 -7
  166. package/node_modules/@oscharko-dev/keiko-verification/package.json +5 -5
  167. package/node_modules/@oscharko-dev/keiko-workflows/package.json +10 -10
  168. package/node_modules/@oscharko-dev/keiko-workspace/dist/version.d.ts +1 -1
  169. package/node_modules/@oscharko-dev/keiko-workspace/dist/version.js +1 -1
  170. package/node_modules/@oscharko-dev/keiko-workspace/package.json +3 -3
  171. package/package.json +26 -26
  172. package/release-impact.catalog.json +78 -23
  173. package/vendor/{oscharko-dev-keiko-cli-1.1.6.tgz → oscharko-dev-keiko-cli-1.1.7.tgz} +0 -0
  174. package/vendor/{oscharko-dev-keiko-connectors-1.1.6.tgz → oscharko-dev-keiko-connectors-1.1.7.tgz} +0 -0
  175. package/vendor/oscharko-dev-keiko-contracts-1.1.7.tgz +0 -0
  176. package/vendor/{oscharko-dev-keiko-evaluations-1.1.6.tgz → oscharko-dev-keiko-evaluations-1.1.7.tgz} +0 -0
  177. package/vendor/{oscharko-dev-keiko-evidence-1.1.6.tgz → oscharko-dev-keiko-evidence-1.1.7.tgz} +0 -0
  178. package/vendor/{oscharko-dev-keiko-git-1.1.6.tgz → oscharko-dev-keiko-git-1.1.7.tgz} +0 -0
  179. package/vendor/{oscharko-dev-keiko-harness-1.1.6.tgz → oscharko-dev-keiko-harness-1.1.7.tgz} +0 -0
  180. package/vendor/{oscharko-dev-keiko-local-knowledge-1.1.6.tgz → oscharko-dev-keiko-local-knowledge-1.1.7.tgz} +0 -0
  181. package/vendor/{oscharko-dev-keiko-memory-capture-1.1.6.tgz → oscharko-dev-keiko-memory-capture-1.1.7.tgz} +0 -0
  182. package/vendor/{oscharko-dev-keiko-memory-consolidation-1.1.6.tgz → oscharko-dev-keiko-memory-consolidation-1.1.7.tgz} +0 -0
  183. package/vendor/{oscharko-dev-keiko-memory-governance-1.1.6.tgz → oscharko-dev-keiko-memory-governance-1.1.7.tgz} +0 -0
  184. package/vendor/{oscharko-dev-keiko-memory-retrieval-1.1.6.tgz → oscharko-dev-keiko-memory-retrieval-1.1.7.tgz} +0 -0
  185. package/vendor/{oscharko-dev-keiko-memory-vault-1.1.6.tgz → oscharko-dev-keiko-memory-vault-1.1.7.tgz} +0 -0
  186. package/vendor/oscharko-dev-keiko-model-gateway-1.1.7.tgz +0 -0
  187. package/vendor/{oscharko-dev-keiko-quality-intelligence-1.1.6.tgz → oscharko-dev-keiko-quality-intelligence-1.1.7.tgz} +0 -0
  188. package/vendor/{oscharko-dev-keiko-sandbox-1.1.6.tgz → oscharko-dev-keiko-sandbox-1.1.7.tgz} +0 -0
  189. package/vendor/{oscharko-dev-keiko-sdk-1.1.6.tgz → oscharko-dev-keiko-sdk-1.1.7.tgz} +0 -0
  190. package/vendor/oscharko-dev-keiko-security-1.1.7.tgz +0 -0
  191. package/vendor/oscharko-dev-keiko-server-1.1.7.tgz +0 -0
  192. package/vendor/{oscharko-dev-keiko-tool-catalog-1.1.6.tgz → oscharko-dev-keiko-tool-catalog-1.1.7.tgz} +0 -0
  193. package/vendor/{oscharko-dev-keiko-tools-1.1.6.tgz → oscharko-dev-keiko-tools-1.1.7.tgz} +0 -0
  194. package/vendor/{oscharko-dev-keiko-verification-1.1.6.tgz → oscharko-dev-keiko-verification-1.1.7.tgz} +0 -0
  195. package/vendor/{oscharko-dev-keiko-workflows-1.1.6.tgz → oscharko-dev-keiko-workflows-1.1.7.tgz} +0 -0
  196. package/vendor/{oscharko-dev-keiko-workspace-1.1.6.tgz → oscharko-dev-keiko-workspace-1.1.7.tgz} +0 -0
  197. package/dist/ui/static/_next/static/chunks/0c3w2ubla6ek6.js +0 -1
  198. package/dist/ui/static/_next/static/chunks/0gnizm29lk3ll.js +0 -1
  199. package/dist/ui/static/_next/static/chunks/19fesd0w1gjhp.js +0 -3
  200. package/dist/ui/static/_next/static/chunks/1k6f6m-u2q69-.js +0 -1
  201. package/dist/ui/static/_next/static/chunks/1pze64zkvx8vt.js +0 -1
  202. package/dist/ui/static/_next/static/chunks/2h_zvmb272ozs.js +0 -1
  203. package/dist/ui/static/_next/static/chunks/3-5_wuwrev-8c.js +0 -1
  204. package/dist/ui/static/_next/static/chunks/3-l-8qt163_y6.js +0 -1
  205. package/vendor/oscharko-dev-keiko-contracts-1.1.6.tgz +0 -0
  206. package/vendor/oscharko-dev-keiko-model-gateway-1.1.6.tgz +0 -0
  207. package/vendor/oscharko-dev-keiko-security-1.1.6.tgz +0 -0
  208. package/vendor/oscharko-dev-keiko-server-1.1.6.tgz +0 -0
  209. /package/dist/ui/static/_next/static/{ytpWXc-OicMB-rH2zX5b3 → nXnJJ05BP_rtUCiZsedfT}/_buildManifest.js +0 -0
  210. /package/dist/ui/static/_next/static/{ytpWXc-OicMB-rH2zX5b3 → nXnJJ05BP_rtUCiZsedfT}/_clientMiddlewareManifest.js +0 -0
  211. /package/dist/ui/static/_next/static/{ytpWXc-OicMB-rH2zX5b3 → nXnJJ05BP_rtUCiZsedfT}/_ssgManifest.js +0 -0
@@ -1,10 +1,9 @@
1
1
  import { gatewaySpendRejectionReason } from "./gateway-spend-budget.js";
2
- import { AuthenticationError, CircuitOpenError, ContextOverflowError, ModelRefusalError, ProviderError, RateLimitError, TimeoutError, TransportError, } from "@oscharko-dev/keiko-security/errors/gateway";
2
+ import { AuthenticationError, CircuitOpenError, ContextOverflowError, ModelRefusalError, ProviderError, ProviderOutputExhaustedError, RateLimitError, TimeoutError, TransportError, } from "@oscharko-dev/keiko-security/errors/gateway";
3
3
  import { createHash, randomUUID } from "node:crypto";
4
4
  import { findConfiguredCapability, resolveCodingSafeSidecarGatewayProfile, } from "@oscharko-dev/keiko-model-gateway";
5
5
  import { countGatewayPromptTokens, } from "@oscharko-dev/keiko-model-gateway/internal/prompt-token-accounting";
6
- import { codingWorkbenchProviderTimeoutMs, providerRequestBudgetMs, } from "@oscharko-dev/keiko-model-gateway/internal/resilience";
7
- import { MAX_TIMER_DELAY_MS } from "./abort-race.js";
6
+ import { gatewayRouteDeadlineMs } from "./gateway-route-deadline.js";
8
7
  import { compareStrings } from "@oscharko-dev/keiko-contracts/runtime/comparators";
9
8
  import { CODING_WORKBENCH_MINIMUM_CODING_CONTEXT_PROMPT_TOKENS } from "@oscharko-dev/keiko-contracts/runtime/coding-workbench";
10
9
  import { activityLogEvent, defineActivityLogOperation, } from "@oscharko-dev/keiko-contracts/runtime/observability";
@@ -15,9 +14,11 @@ import { createOpenCodeGatewayToolCatalogAdvertisement, opencodeGatewayOfferLife
15
14
  import { correlationIdOrUnknown, UNKNOWN_CORRELATION_ID } from "./correlation.js";
16
15
  import { emitServerDiagnostic, serverDiagnosticFromError } from "./diagnostics-log.js";
17
16
  import { readJsonObject } from "./files.js";
18
- import { ensureCodingWorkbenchContextWindows } from "./gateway-readiness.js";
17
+ import { safetyMarginTokensFor } from "@oscharko-dev/keiko-contracts/context-engineering";
18
+ import { ensureCodingWorkbenchContextWindows, isAnyCodingWorkbenchProbePending, isCodingWorkbenchProbePending, } from "./gateway-readiness.js";
19
19
  import { getServerLogger } from "./observability/index.js";
20
20
  import { STREAMING, errorBody } from "./routes.js";
21
+ import { sseBackpressureReporter, writeOrDestroy, } from "./sse-write.js";
21
22
  import { startSseHeartbeat } from "./sse.js";
22
23
  import { createCanonicalOpenCodeHandlerCoverage } from "./tool-catalog/catalogToolFacadeBridge.js";
23
24
  const ENABLE_TOKENS = new Set(["1", "true", "on", "yes", "enabled"]);
@@ -62,31 +63,45 @@ function observedPromptSettlement(outcome, selected, unverified) {
62
63
  * authenticator (not every deployment wires it) is a silent no-op, never a failure.
63
64
  */
64
65
  function settlePromptTokenReservation(deps, reservation, actualPromptTokens) {
66
+ const providerReported = actualPromptTokens !== undefined && actualPromptTokens > 0;
67
+ return applyPromptTokenSettlement(deps, reservation, {
68
+ promptTokens: providerReported ? actualPromptTokens : reservation.reservedPromptTokens,
69
+ source: providerReported ? "provider-reported" : "reserved-estimate",
70
+ });
71
+ }
72
+ /**
73
+ * Releases a reservation whose provider call never ran — the client would not take the opening
74
+ * SSE frame, so the buffered path never called the chat factory and the streamed path never pulled
75
+ * the gateway's generator. The conservative rule above keeps the estimate only for a call that WAS
76
+ * dispatched; here nothing was spent, so the whole reservation goes back to the run's budget
77
+ * (#3602 review). Same idempotence and same not-wired fallback as a settlement.
78
+ */
79
+ function releasePromptTokenReservation(deps, reservation) {
80
+ return applyPromptTokenSettlement(deps, reservation, {
81
+ promptTokens: 0,
82
+ source: "released-unspent",
83
+ });
84
+ }
85
+ function applyPromptTokenSettlement(deps, reservation, usage) {
65
86
  if (reservation.settled) {
66
87
  if (reservation.settlement === undefined)
67
88
  throw new TypeError("missing prompt settlement");
68
89
  return reservation.settlement;
69
90
  }
70
91
  reservation.settled = true;
71
- const providerReported = actualPromptTokens !== undefined && actualPromptTokens > 0;
72
- const promptTokens = providerReported ? actualPromptTokens : reservation.reservedPromptTokens;
73
92
  const unverified = {
74
93
  promptTokens: reservation.reservedPromptTokens,
75
94
  source: "reserved-estimate",
76
95
  status: "unverified",
77
96
  };
78
- const selected = {
79
- promptTokens,
80
- source: providerReported ? "provider-reported" : "reserved-estimate",
81
- status: "settled",
82
- };
97
+ const selected = { ...usage, status: "settled" };
83
98
  reservation.settlement = unverified;
84
99
  const authenticator = runtimeCapabilityAuthenticator(deps);
85
100
  if (authenticator?.settlePromptTokens === undefined) {
86
101
  reservation.settlement = { ...selected, status: "not-wired" };
87
102
  return reservation.settlement;
88
103
  }
89
- const outcome = authenticator.settlePromptTokens(reservation.capability, reservation.reservedPromptTokens, promptTokens);
104
+ const outcome = authenticator.settlePromptTokens(reservation.capability, reservation.reservedPromptTokens, usage.promptTokens);
90
105
  reservation.settlement = observedPromptSettlement(outcome, selected, unverified);
91
106
  return reservation.settlement;
92
107
  }
@@ -114,6 +129,9 @@ const CODING_SIDECAR_GATEWAY_REQUEST_VALIDATED_OPERATION = defineActivityLogOper
114
129
  maxRequestBytes: { type: "integer", dataClass: "count", required: true },
115
130
  maxPromptTokens: { type: "integer", dataClass: "count", required: true },
116
131
  estimatedPromptTokens: { type: "integer", dataClass: "count", required: true },
132
+ // #3591 (1.1.7): the output allowance sent with this request, clamped to the window that
133
+ // remains after the prompt — the value an output-exhausted turn has to be read against.
134
+ maxOutputTokens: { type: "integer", dataClass: "count", required: false },
117
135
  inputMessageCount: { type: "integer", dataClass: "count", required: true },
118
136
  completeness: { type: "string", dataClass: "completeness-state", required: true },
119
137
  loss: { type: "string", dataClass: "loss-state", required: true },
@@ -137,11 +155,21 @@ const CODING_SIDECAR_GATEWAY_READINESS_INSUFFICIENT_OPERATION = defineActivityLo
137
155
  type: "string",
138
156
  dataClass: "closed-enum",
139
157
  required: true,
140
- values: ["model-context-window-insufficient", "no-tool-calling", "tool-calling-unverified"],
158
+ values: [
159
+ "model-context-window-insufficient",
160
+ "no-tool-calling",
161
+ "tool-calling-unverified",
162
+ "model-verification-pending",
163
+ ],
141
164
  },
142
165
  maxPromptTokens: { type: "integer", dataClass: "count", required: false },
143
166
  minimumRequiredPromptTokens: { type: "integer", dataClass: "count", required: false },
144
- probeMode: { type: "string", dataClass: "closed-enum", required: true, values: ["passive"] },
167
+ probeMode: {
168
+ type: "string",
169
+ dataClass: "closed-enum",
170
+ required: true,
171
+ values: ["passive", "pending"],
172
+ },
145
173
  completeness: { type: "string", dataClass: "completeness-state", required: true },
146
174
  loss: { type: "string", dataClass: "loss-state", required: true },
147
175
  },
@@ -240,6 +268,9 @@ const CODING_SIDECAR_GATEWAY_REJECTED_OPERATION = defineActivityLogOperation({
240
268
  },
241
269
  estimatedPromptTokens: { type: "integer", dataClass: "count", required: false },
242
270
  maxPromptTokens: { type: "integer", dataClass: "count", required: false },
271
+ // #3591 review: the bound the prompt was actually admitted against — `maxPromptTokens` less
272
+ // the safety margin and the minimum output allowance (`admissiblePromptTokens`).
273
+ admissiblePromptTokens: { type: "integer", dataClass: "count", required: false },
243
274
  inputMessageCount: { type: "integer", dataClass: "count", required: false },
244
275
  maxInputMessages: { type: "integer", dataClass: "count", required: false },
245
276
  completeness: { type: "string", dataClass: "completeness-state", required: true },
@@ -272,7 +303,7 @@ const CODING_SIDECAR_GATEWAY_TURN_FAILED_OPERATION = defineActivityLogOperation(
272
303
  type: "string",
273
304
  dataClass: "closed-enum",
274
305
  required: true,
275
- values: ["provider-failed", "stream-incomplete", "turn-rejected"],
306
+ values: ["provider-failed", "stream-incomplete", "turn-rejected", "output-exhausted"],
276
307
  },
277
308
  published: { type: "boolean", dataClass: "closed-enum", required: true },
278
309
  publicationReason: {
@@ -309,11 +340,12 @@ const CODING_SIDECAR_GATEWAY_USAGE_SETTLED_OPERATION = defineActivityLogOperatio
309
340
  runId: { type: "string", dataClass: "opaque-id", required: true, maxLength: 128 },
310
341
  completionTokens: { type: "integer", dataClass: "count", required: true },
311
342
  promptTokens: { type: "integer", dataClass: "count", required: true },
343
+ // `released-unspent`: the provider call never ran, the whole reservation went back (#3602).
312
344
  promptSource: {
313
345
  type: "string",
314
346
  dataClass: "closed-enum",
315
347
  required: true,
316
- values: ["provider-reported", "reserved-estimate"],
348
+ values: ["provider-reported", "reserved-estimate", "released-unspent"],
317
349
  },
318
350
  promptSettlementStatus: {
319
351
  type: "string",
@@ -355,6 +387,16 @@ const CODING_SIDECAR_GATEWAY_OUTCOME_OPERATION = defineActivityLogOperation({
355
387
  },
356
388
  completionTokens: { type: "integer", dataClass: "count", required: true },
357
389
  outputBytes: { type: "integer", dataClass: "count", required: true },
390
+ // The route backstop armed for this turn (#3602 review); absent on lines written before 1.1.7.
391
+ deadlineMs: { type: "integer", dataClass: "duration", required: false },
392
+ // On a cancelled outcome only: which armed abort source ended the turn, so a stall that ran into
393
+ // the backstop, or a slow client the shared SSE path killed, never reads like a client that left.
394
+ cancellationCause: {
395
+ type: "string",
396
+ dataClass: "closed-enum",
397
+ required: false,
398
+ values: ["client-disconnect", "route-deadline", "backpressure-killed", "run-stopped"],
399
+ },
358
400
  completeness: { type: "string", dataClass: "completeness-state", required: true },
359
401
  loss: { type: "string", dataClass: "loss-state", required: true },
360
402
  },
@@ -895,8 +937,25 @@ function emitGatewayEvidenceAggregationDiagnostic(deps, runId) {
895
937
  message: "sidecar-gateway-evidence-aggregation-failed",
896
938
  });
897
939
  }
898
- function recordGatewayOutcome(ctx, deps, runId, outcome, completionTokens, outputBytes) {
899
- getServerLogger().info(activityLogEvent(CODING_SIDECAR_GATEWAY_OUTCOME_OPERATION, { correlationId: correlationIdOrUnknown(ctx.correlationId), parentCorrelationId: runId }, { runId, outcome, completionTokens, outputBytes, completeness: "complete", loss: "none" }));
940
+ // A cancelled outcome names the abort source that ended the turn. Without an aborted source it is
941
+ // the transport path — an SSE or terminal write found the response gone before its `close`
942
+ // listener ran — which is the client leaving, never the deadline.
943
+ function gatewayOutcomeCancellationCause(cancellation, outcome) {
944
+ if (outcome !== "cancelled")
945
+ return undefined;
946
+ return { cancellationCause: cancellation.cause() ?? "client-disconnect" };
947
+ }
948
+ function recordGatewayOutcome(ctx, deps, runId, cancellation, outcome, completionTokens, outputBytes) {
949
+ getServerLogger().info(activityLogEvent(CODING_SIDECAR_GATEWAY_OUTCOME_OPERATION, { correlationId: correlationIdOrUnknown(ctx.correlationId), parentCorrelationId: runId }, {
950
+ runId,
951
+ outcome,
952
+ completionTokens,
953
+ outputBytes,
954
+ deadlineMs: cancellation.deadlineMs,
955
+ ...gatewayOutcomeCancellationCause(cancellation, outcome),
956
+ completeness: "complete",
957
+ loss: "none",
958
+ }));
900
959
  try {
901
960
  void Promise.resolve(evidenceAggregator(deps)?.record({ runId, outcome, completionTokens, outputBytes })).catch(() => {
902
961
  emitGatewayEvidenceAggregationDiagnostic(deps, runId);
@@ -969,8 +1028,9 @@ function budgetValidationError(parsed, runMetadata, estimatedPromptTokens) {
969
1028
  if (parsed.messages.length > runMetadata.maxInputMessages) {
970
1029
  return contextOverflowRequest(`Request body messages exceed profile maxInputMessages (${String(runMetadata.maxInputMessages)}).`);
971
1030
  }
972
- if (estimatedPromptTokens > runMetadata.maxPromptTokens) {
973
- return contextOverflowRequest(`Request body estimated prompt tokens exceed profile maxPromptTokens (${String(runMetadata.maxPromptTokens)}).`);
1031
+ const admissible = admissiblePromptTokens(runMetadata);
1032
+ if (estimatedPromptTokens > admissible) {
1033
+ return contextOverflowRequest(`Request body estimated prompt tokens exceed profile maxPromptTokens (${String(runMetadata.maxPromptTokens)}) less the reserved output allowance (${String(admissible)} admissible).`);
974
1034
  }
975
1035
  return undefined;
976
1036
  }
@@ -1052,6 +1112,8 @@ function logGatewayTurnFailure(ctx, runId, revision, state, failureCode, publica
1052
1112
  function gatewayTurnFailureCode(error) {
1053
1113
  if (error instanceof ContextOverflowError || error instanceof ModelRefusalError)
1054
1114
  return "turn-rejected";
1115
+ if (error instanceof ProviderOutputExhaustedError)
1116
+ return "output-exhausted";
1055
1117
  if (error instanceof TimeoutError ||
1056
1118
  error instanceof TransportError ||
1057
1119
  (error instanceof ProviderError && error.httpStatus === 200))
@@ -1063,6 +1125,8 @@ function gatewayStreamFailureCode(error) {
1063
1125
  return "turn-rejected";
1064
1126
  if (error instanceof ContextOverflowError || error instanceof ModelRefusalError)
1065
1127
  return "turn-rejected";
1128
+ if (error instanceof ProviderOutputExhaustedError)
1129
+ return "output-exhausted";
1066
1130
  if (error instanceof AuthenticationError ||
1067
1131
  error instanceof RateLimitError ||
1068
1132
  error instanceof CircuitOpenError ||
@@ -1351,25 +1415,37 @@ function gatewayAuthorityKind(authentication) {
1351
1415
  function unavailableGatewayReason(resolved) {
1352
1416
  return resolved.result.status === "unavailable" ? resolved.result.reason : "missing-provider";
1353
1417
  }
1418
+ function sidecarSseTransport(ctx, deps) {
1419
+ const correlationId = correlationIdOrUnknown(ctx.correlationId);
1420
+ return {
1421
+ backpressure: new AbortController(),
1422
+ onBackpressure: sseBackpressureReporter(deps, "coding-sidecar-gateway", correlationId),
1423
+ correlationId,
1424
+ };
1425
+ }
1426
+ // `AbortSignal.any` carries the reason of the source that aborted first, so a client that leaves
1427
+ // after the deadline already fired still reads as the deadline, never the other way round.
1428
+ function gatewayCancellationCause(signal, sources) {
1429
+ if (!signal.aborted)
1430
+ return undefined;
1431
+ if (signal.reason === sources.client.reason)
1432
+ return "client-disconnect";
1433
+ if (signal.reason === sources.deadline.reason)
1434
+ return "route-deadline";
1435
+ if (signal.reason === sources.backpressure.reason)
1436
+ return "backpressure-killed";
1437
+ return "run-stopped";
1438
+ }
1354
1439
  // The route's deadline is a backstop BEHIND the gateway's own end-to-end budget, never the budget
1355
- // itself. It used to be the provider's per-attempt `timeoutMs`: the first attempt that hung spent
1356
- // it, and this deadline, armed before the gateway started its own clock, aborted the retry the
1357
- // gateway had just scheduled, so a provider timeout surfaced as a cancellation nobody had asked
1358
- // for and failed the run (coding run 23, 2026-09-11). The grace lets the gateway settle its own
1359
- // timeout or exhausted-retry error first.
1360
- const GATEWAY_ROUTE_DEADLINE_GRACE_MS = 1_000;
1440
+ // itself (`gateway-route-deadline.ts`, shared with the commit draft since the #3602 review). It used
1441
+ // to be the provider's per-attempt `timeoutMs`: the first attempt that hung spent it, and this
1442
+ // deadline, armed before the gateway started its own clock, aborted the retry the gateway had just
1443
+ // scheduled, so a provider timeout surfaced as a cancellation nobody had asked for and failed the
1444
+ // run (coding run 23, 2026-09-11).
1361
1445
  export function codingSidecarGatewayRequestDeadlineMs(config, modelId) {
1362
- const provider = config.providers.find((candidate) => candidate.modelId === modelId);
1363
- // An unconfigured model is refused before any provider call; 30 s only bounds that refusal.
1364
- const budget = provider === undefined
1365
- ? 30_000
1366
- : providerRequestBudgetMs({
1367
- ...provider,
1368
- timeoutMs: codingWorkbenchProviderTimeoutMs(provider.timeoutMs),
1369
- });
1370
- // Armed with AbortSignal.timeout, which fires at once past 2^31 - 1 ms: an absurd budget must not
1371
- // turn the backstop into an immediate abort.
1372
- return Math.min(budget + GATEWAY_ROUTE_DEADLINE_GRACE_MS, MAX_TIMER_DELAY_MS);
1446
+ // The sidecar reaches the gateway both ways — a buffered `chat()` answer or a `chatStream()`
1447
+ // read — so its backstop sits behind the longer of the two budgets.
1448
+ return gatewayRouteDeadlineMs(config, modelId, ["buffered", "streamed"]);
1373
1449
  }
1374
1450
  function gatewayRequestCancellation(ctx, deps, config, modelId, runId) {
1375
1451
  const client = new AbortController();
@@ -1378,11 +1454,18 @@ function gatewayRequestCancellation(ctx, deps, config, modelId, runId) {
1378
1454
  };
1379
1455
  ctx.req.once("aborted", abortClient);
1380
1456
  ctx.res.once("close", abortClient);
1381
- const deadline = AbortSignal.timeout(codingSidecarGatewayRequestDeadlineMs(config, modelId));
1457
+ const deadlineMs = codingSidecarGatewayRequestDeadlineMs(config, modelId);
1458
+ const deadline = AbortSignal.timeout(deadlineMs);
1459
+ const transport = sidecarSseTransport(ctx, deps);
1382
1460
  const runSignal = cancellationRegistry(deps)?.signalFor(runId);
1383
- const signals = [client.signal, deadline, runSignal].filter((signal) => signal !== undefined);
1461
+ const signals = [client.signal, deadline, transport.backpressure.signal, runSignal].filter((signal) => signal !== undefined);
1462
+ const signal = AbortSignal.any(signals);
1463
+ const sources = { client: client.signal, deadline, backpressure: transport.backpressure.signal };
1384
1464
  return {
1385
- signal: AbortSignal.any(signals),
1465
+ signal,
1466
+ deadlineMs,
1467
+ transport,
1468
+ cause: () => gatewayCancellationCause(signal, sources),
1386
1469
  dispose: () => {
1387
1470
  ctx.req.removeListener("aborted", abortClient);
1388
1471
  ctx.res.removeListener("close", abortClient);
@@ -1395,23 +1478,23 @@ function requestForGatewayDelivery(ctx, parsed, delivery, signal) {
1395
1478
  async function executeGatewayChat(ctx, deps, binding, parsed, runId, delivery) {
1396
1479
  const cancellation = gatewayRequestCancellation(ctx, deps, binding.config, delivery.modelAlias, runId);
1397
1480
  try {
1398
- return await dispatchGatewayChat(ctx, deps, binding, parsed, runId, delivery, cancellation.signal);
1481
+ return await dispatchGatewayChat(ctx, deps, binding, parsed, runId, delivery, cancellation);
1399
1482
  }
1400
1483
  finally {
1401
1484
  cancellation.dispose();
1402
1485
  }
1403
1486
  }
1404
1487
  // Extracted so `executeGatewayChat` stays under AGENTS.md §6's 50-line ceiling.
1405
- async function dispatchGatewayChat(ctx, deps, binding, parsed, runId, delivery, cancellationSignal) {
1488
+ async function dispatchGatewayChat(ctx, deps, binding, parsed, runId, delivery, cancellation) {
1406
1489
  const { modelAlias, upstreamStreamingSupported } = delivery;
1407
- const request = requestForGatewayDelivery(ctx, parsed, delivery, cancellationSignal);
1490
+ const request = requestForGatewayDelivery(ctx, parsed, delivery, cancellation.signal);
1408
1491
  const dispatch = {
1409
1492
  deps,
1410
1493
  binding,
1411
1494
  modelAlias,
1412
1495
  request,
1413
1496
  runId,
1414
- cancellationSignal,
1497
+ cancellation,
1415
1498
  promptTokenReservation: delivery.promptTokenReservation,
1416
1499
  };
1417
1500
  let bufferedStream;
@@ -1419,20 +1502,40 @@ async function dispatchGatewayChat(ctx, deps, binding, parsed, runId, delivery,
1419
1502
  if (parsed.stream && upstreamStreamingSupported) {
1420
1503
  return await streamGatewayChat(ctx, dispatch);
1421
1504
  }
1422
- if (parsed.stream)
1423
- bufferedStream = beginBufferedOpenAiStream(ctx, modelAlias);
1505
+ if (parsed.stream) {
1506
+ bufferedStream = beginBufferedOpenAiStream(ctx, modelAlias, cancellation.transport);
1507
+ if (bufferedStream === undefined)
1508
+ return settleUndeliverableBufferedStream(ctx, dispatch);
1509
+ }
1424
1510
  return await executeBufferedGatewayChat(ctx, dispatch, bufferedStream);
1425
1511
  }
1426
1512
  catch (error) {
1427
- return settleFailedGatewayChat(ctx, deps, runId, cancellationSignal, error, delivery, bufferedStream);
1513
+ return settleFailedGatewayChat(ctx, deps, runId, cancellation, error, delivery, bufferedStream);
1428
1514
  }
1429
1515
  }
1516
+ // The opening frame never reached the client — the shared path killed the stream, or the response
1517
+ // was already gone — so no provider call is started for an answer nobody can receive, exactly as
1518
+ // `beginGatewayStream` ends the streamed path (#3602 review). The outcome names the abort source.
1519
+ function settleUndeliverableBufferedStream(ctx, dispatch) {
1520
+ const { deps, runId, cancellation, promptTokenReservation } = dispatch;
1521
+ recordGatewayOutcome(ctx, deps, runId, cancellation, "cancelled", 0, 0);
1522
+ releaseUndispatchedPromptBudget(ctx, deps, runId, promptTokenReservation);
1523
+ return STREAMING;
1524
+ }
1525
+ // A handshake the client would not take leaves no provider call behind, so the reservation is
1526
+ // released rather than kept as spent, and the usage line records the release (zero prompt tokens,
1527
+ // `released-unspent`) so the ledger movement is reconstructible from the log.
1528
+ function releaseUndispatchedPromptBudget(ctx, deps, runId, reservation) {
1529
+ const settlement = releasePromptTokenReservation(deps, reservation);
1530
+ logGatewayCompletionUsage(ctx, runId, { completionTokens: 0, outputBytes: 0 }, "output-byte-estimate", settlement);
1531
+ }
1430
1532
  // Extracted so `executeGatewayChat` stays under AGENTS.md §6's 50-line ceiling.
1431
- function settleFailedGatewayChat(ctx, deps, runId, cancellationSignal, error, delivery, bufferedStream) {
1432
- recordGatewayOutcome(ctx, deps, runId, cancellationSignal.aborted ? "cancelled" : "failed", 0, 0);
1533
+ function settleFailedGatewayChat(ctx, deps, runId, cancellation, error, delivery, bufferedStream) {
1534
+ const cancelled = cancellation.signal.aborted;
1535
+ recordGatewayOutcome(ctx, deps, runId, cancellation, cancelled ? "cancelled" : "failed", 0, 0);
1433
1536
  emitGatewayFailureDiagnostic(ctx, deps, error, runId);
1434
1537
  const spendReason = gatewaySpendRejectionReason(error);
1435
- if (!cancellationSignal.aborted)
1538
+ if (!cancelled)
1436
1539
  reportGatewayTurnFailure(ctx, deps, runId, spendReason === undefined ? gatewayTurnFailureCode(error) : "turn-rejected");
1437
1540
  settlePromptTokenReservation(deps, delivery.promptTokenReservation);
1438
1541
  if (spendReason !== undefined && bufferedStream === undefined) {
@@ -1444,7 +1547,7 @@ function settleFailedGatewayChat(ctx, deps, runId, cancellationSignal, error, de
1444
1547
  : settleBufferedOpenAiStreamError(bufferedStream, "error");
1445
1548
  }
1446
1549
  async function executeBufferedGatewayChat(ctx, dispatch, stream) {
1447
- const { deps, binding, modelAlias, request, runId, cancellationSignal, promptTokenReservation } = dispatch;
1550
+ const { deps, binding, modelAlias, request, runId, cancellation, promptTokenReservation } = dispatch;
1448
1551
  const response = await chatFactoryFor(deps, binding.gateway)(binding.config, modelAlias)(request);
1449
1552
  const promptSettlement = settlePromptTokenReservation(deps, promptTokenReservation, response.usage.promptTokens);
1450
1553
  const output = outputMetrics(response);
@@ -1456,9 +1559,9 @@ async function executeBufferedGatewayChat(ctx, dispatch, stream) {
1456
1559
  };
1457
1560
  logGatewayCompletionUsage(ctx, runId, metrics, usage.source, promptSettlement);
1458
1561
  const record = (outcome) => {
1459
- recordGatewayOutcome(ctx, deps, runId, outcome, metrics.completionTokens, metrics.outputBytes);
1562
+ recordGatewayOutcome(ctx, deps, runId, cancellation, outcome, metrics.completionTokens, metrics.outputBytes);
1460
1563
  };
1461
- if (cancellationSignal.aborted) {
1564
+ if (cancellation.signal.aborted) {
1462
1565
  record("cancelled");
1463
1566
  return stream === undefined
1464
1567
  ? unavailableError()
@@ -1470,15 +1573,15 @@ async function executeBufferedGatewayChat(ctx, dispatch, stream) {
1470
1573
  ? unavailableError()
1471
1574
  : settleBufferedOpenAiStreamError(stream, "length");
1472
1575
  }
1473
- return deliverBufferedGatewayAnswer(ctx, deps, runId, stream, modelAlias, settledResponse, metrics);
1576
+ return deliverBufferedGatewayAnswer(ctx, modelAlias, stream, settledResponse, record);
1474
1577
  }
1475
- function deliverBufferedGatewayAnswer(ctx, deps, runId, stream, modelAlias, response, metrics) {
1578
+ function deliverBufferedGatewayAnswer(ctx, modelAlias, stream, response, record) {
1476
1579
  if (stream === undefined) {
1477
- recordGatewayOutcome(ctx, deps, runId, "accepted", metrics.completionTokens, metrics.outputBytes);
1580
+ record("accepted");
1478
1581
  return openAiResponse(modelAlias, response);
1479
1582
  }
1480
1583
  completeBufferedOpenAiStream(stream, response);
1481
- recordGatewayOutcome(ctx, deps, runId, ctx.res.writableEnded && !ctx.res.destroyed ? "accepted" : "cancelled", metrics.completionTokens, metrics.outputBytes);
1584
+ record(ctx.res.writableEnded && !ctx.res.destroyed ? "accepted" : "cancelled");
1482
1585
  return STREAMING;
1483
1586
  }
1484
1587
  // Closed stream state machine keeps iterator, cancellation, and SSE backpressure transitions together.
@@ -1489,7 +1592,7 @@ async function streamGatewayChat(ctx, dispatch) {
1489
1592
  iterator = chatStreamFactoryFor(deps, binding.gateway)(binding.config, modelAlias)(request)[Symbol.asyncIterator]();
1490
1593
  }
1491
1594
  catch (error) {
1492
- recordGatewayOutcome(ctx, deps, runId, "failed", 0, 0);
1595
+ recordGatewayOutcome(ctx, deps, runId, dispatch.cancellation, "failed", 0, 0);
1493
1596
  emitGatewayFailureDiagnostic(ctx, deps, error, runId);
1494
1597
  reportGatewayTurnFailure(ctx, deps, runId, gatewayTurnFailureCode(error));
1495
1598
  settlePromptTokenReservation(deps, promptTokenReservation);
@@ -1497,8 +1600,14 @@ async function streamGatewayChat(ctx, dispatch) {
1497
1600
  }
1498
1601
  const session = createGatewayStreamSession(ctx, dispatch, iterator);
1499
1602
  try {
1500
- if (beginGatewayStream(session))
1603
+ if (beginGatewayStream(session)) {
1501
1604
  await pumpGatewayStreamWithCancellation(deps, session);
1605
+ }
1606
+ else {
1607
+ // The gateway's stream is an async generator: nothing was sent before the first pull, and
1608
+ // the handshake failed before it, so the reservation is released, not kept as spent.
1609
+ releaseUndispatchedPromptBudget(ctx, deps, runId, promptTokenReservation);
1610
+ }
1502
1611
  return STREAMING;
1503
1612
  }
1504
1613
  finally {
@@ -1509,7 +1618,8 @@ async function streamGatewayChat(ctx, dispatch) {
1509
1618
  }
1510
1619
  }
1511
1620
  async function pumpGatewayStreamWithCancellation(deps, session) {
1512
- const { cancellationSignal, iterator } = session;
1621
+ const { cancellation, iterator } = session;
1622
+ const cancellationSignal = cancellation.signal;
1513
1623
  const cancelIterator = () => {
1514
1624
  void iterator.return?.();
1515
1625
  };
@@ -1519,7 +1629,7 @@ async function pumpGatewayStreamWithCancellation(deps, session) {
1519
1629
  }
1520
1630
  catch (error) {
1521
1631
  emitGatewayStreamFailureDiagnostic(session.ctx, deps, error, session.runId);
1522
- if (!session.cancellationSignal.aborted) {
1632
+ if (!cancellationSignal.aborted) {
1523
1633
  reportGatewayTurnFailure(session.ctx, deps, session.runId, gatewayStreamFailureCode(error));
1524
1634
  }
1525
1635
  settleGatewayStreamError(session);
@@ -1529,7 +1639,7 @@ async function pumpGatewayStreamWithCancellation(deps, session) {
1529
1639
  }
1530
1640
  }
1531
1641
  function createGatewayStreamSession(ctx, dispatch, iterator) {
1532
- const { deps, modelAlias, request, runId, cancellationSignal, promptTokenReservation } = dispatch;
1642
+ const { deps, modelAlias, request, runId, cancellation, promptTokenReservation } = dispatch;
1533
1643
  return {
1534
1644
  ctx,
1535
1645
  deps,
@@ -1538,7 +1648,7 @@ function createGatewayStreamSession(ctx, dispatch, iterator) {
1538
1648
  modelId: modelAlias,
1539
1649
  request,
1540
1650
  runId,
1541
- cancellationSignal,
1651
+ cancellation,
1542
1652
  iterator,
1543
1653
  promptTokenReservation,
1544
1654
  metrics: {
@@ -1557,7 +1667,8 @@ function beginGatewayStream(session) {
1557
1667
  "Cache-Control": "no-store",
1558
1668
  Connection: "keep-alive",
1559
1669
  });
1560
- if (!writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { role: "assistant" }, null))) {
1670
+ const opened = writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { role: "assistant" }, null), session.cancellation.transport);
1671
+ if (!opened) {
1561
1672
  ctx.res.destroy();
1562
1673
  recordSessionOutcome(session, "cancelled");
1563
1674
  return false;
@@ -1565,15 +1676,16 @@ function beginGatewayStream(session) {
1565
1676
  return true;
1566
1677
  }
1567
1678
  function recordSessionOutcome(session, outcome) {
1568
- const { ctx, deps, runId, metrics } = session;
1569
- recordGatewayOutcome(ctx, deps, runId, outcome, metrics.completionTokens, metrics.outputBytes);
1679
+ const { ctx, deps, runId, cancellation, metrics } = session;
1680
+ recordGatewayOutcome(ctx, deps, runId, cancellation, outcome, metrics.completionTokens, metrics.outputBytes);
1570
1681
  }
1571
1682
  function writeSessionTerminal(session, finishReason) {
1572
- const { ctx, id, created, modelId, metrics } = session;
1573
- writeStreamTerminal(ctx, id, created, modelId, finishReason, metrics.promptTokens, metrics.completionTokens);
1683
+ const { ctx, id, created, modelId, metrics, cancellation } = session;
1684
+ writeStreamTerminal({ ctx, id, created, modelId, transport: cancellation.transport }, finishReason, metrics.promptTokens, metrics.completionTokens);
1574
1685
  }
1575
1686
  async function pumpGatewayStream(session) {
1576
- const { cancellationSignal, iterator } = session;
1687
+ const { cancellation, iterator } = session;
1688
+ const cancellationSignal = cancellation.signal;
1577
1689
  for (;;) {
1578
1690
  if (isGatewayRequestCancelled(cancellationSignal)) {
1579
1691
  await iterator.return?.();
@@ -1613,7 +1725,8 @@ async function streamGatewayDelta(session, token) {
1613
1725
  writeSessionTerminal(session, "length");
1614
1726
  return false;
1615
1727
  }
1616
- if (!writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { content: token }, null))) {
1728
+ const wrote = writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { content: token }, null), session.cancellation.transport);
1729
+ if (!wrote) {
1617
1730
  ctx.res.destroy();
1618
1731
  await iterator.return?.();
1619
1732
  recordSessionOutcome(session, "cancelled");
@@ -1635,7 +1748,7 @@ async function streamGatewayResponse(session, response) {
1635
1748
  return;
1636
1749
  }
1637
1750
  if (response.toolCalls.length > 0) {
1638
- const wrote = writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { tool_calls: openAiToolCalls(response.toolCalls) }, null));
1751
+ const wrote = writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { tool_calls: openAiToolCalls(response.toolCalls) }, null), session.cancellation.transport);
1639
1752
  if (!wrote) {
1640
1753
  ctx.res.destroy();
1641
1754
  await iterator.return?.();
@@ -1688,7 +1801,7 @@ function logGatewayCompletionUsage(ctx, runId, metrics, source, promptSettlement
1688
1801
  }));
1689
1802
  }
1690
1803
  function settleGatewayStreamError(session) {
1691
- if (!session.cancellationSignal.aborted) {
1804
+ if (!session.cancellation.signal.aborted) {
1692
1805
  recordSessionOutcome(session, "failed");
1693
1806
  writeSessionTerminal(session, "error");
1694
1807
  }
@@ -1699,7 +1812,8 @@ function settleGatewayStreamError(session) {
1699
1812
  function isGatewayRequestCancelled(signal) {
1700
1813
  return signal.aborted;
1701
1814
  }
1702
- function beginBufferedOpenAiStream(ctx, modelId) {
1815
+ /** Returns undefined when the opening SSE frame could not be delivered; the response is destroyed. */
1816
+ function beginBufferedOpenAiStream(ctx, modelId, transport) {
1703
1817
  const id = `chatcmpl-${randomUUID()}`;
1704
1818
  const created = Math.floor(Date.now() / 1000);
1705
1819
  ctx.res.writeHead(200, {
@@ -1707,19 +1821,26 @@ function beginBufferedOpenAiStream(ctx, modelId) {
1707
1821
  "Cache-Control": "no-store",
1708
1822
  Connection: "keep-alive",
1709
1823
  });
1710
- if (!writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { role: "assistant" }, null))) {
1824
+ const opened = writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, { role: "assistant" }, null), transport);
1825
+ if (!opened) {
1711
1826
  ctx.res.destroy();
1827
+ return undefined;
1712
1828
  }
1713
1829
  return {
1714
1830
  ctx,
1715
1831
  id,
1716
1832
  created,
1717
1833
  modelId,
1718
- stopHeartbeat: startSseHeartbeat(ctx.res, BUFFERED_STREAM_HEARTBEAT_MS),
1834
+ transport,
1835
+ stopHeartbeat: startSseHeartbeat(ctx.res, BUFFERED_STREAM_HEARTBEAT_MS, undefined, {
1836
+ controller: transport.backpressure,
1837
+ onBackpressure: transport.onBackpressure,
1838
+ correlationId: transport.correlationId,
1839
+ }),
1719
1840
  };
1720
1841
  }
1721
1842
  function completeBufferedOpenAiStream(session, response) {
1722
- const { ctx, id, created, modelId, stopHeartbeat } = session;
1843
+ const { ctx, id, created, modelId, transport, stopHeartbeat } = session;
1723
1844
  stopHeartbeat();
1724
1845
  if (response.content.length > 0 || response.toolCalls.length > 0) {
1725
1846
  const wrote = writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, {
@@ -1727,32 +1848,35 @@ function completeBufferedOpenAiStream(session, response) {
1727
1848
  ...(response.toolCalls.length === 0
1728
1849
  ? {}
1729
1850
  : { tool_calls: openAiToolCalls(response.toolCalls) }),
1730
- }, null));
1851
+ }, null), transport);
1731
1852
  if (!wrote) {
1732
1853
  ctx.res.destroy();
1733
1854
  return STREAMING;
1734
1855
  }
1735
1856
  }
1736
- writeStreamTerminal(ctx, id, created, modelId, response.finishReason, response.usage.promptTokens, response.usage.completionTokens);
1857
+ writeStreamTerminal(session, response.finishReason, response.usage.promptTokens, response.usage.completionTokens);
1737
1858
  return STREAMING;
1738
1859
  }
1739
1860
  function settleBufferedOpenAiStreamError(session, finishReason) {
1740
- const { ctx, id, created, modelId, stopHeartbeat } = session;
1741
- stopHeartbeat();
1742
- writeStreamTerminal(ctx, id, created, modelId, finishReason, 0, 0);
1861
+ session.stopHeartbeat();
1862
+ writeStreamTerminal(session, finishReason, 0, 0);
1743
1863
  return STREAMING;
1744
1864
  }
1745
- function bufferedOpenAiStream(ctx, modelId, response) {
1746
- return completeBufferedOpenAiStream(beginBufferedOpenAiStream(ctx, modelId), response);
1865
+ function bufferedOpenAiStream(ctx, deps, modelId, response) {
1866
+ const session = beginBufferedOpenAiStream(ctx, modelId, sidecarSseTransport(ctx, deps));
1867
+ return session === undefined ? STREAMING : completeBufferedOpenAiStream(session, response);
1747
1868
  }
1748
- function writeOpenAiSse(ctx, payload) {
1749
- if (!ctx.res.writableEnded && !ctx.res.destroyed) {
1750
- return ctx.res.write(`data: ${JSON.stringify(payload)}\n\n`);
1751
- }
1752
- return false;
1869
+ // False means the frame did not reach the client: either the response was already gone (the
1870
+ // client's own `close` is the abort source that names it) or the shared path killed the stream for
1871
+ // backpressure (its controller is). A caller that sees false stops producing; it never resumes.
1872
+ function writeOpenAiSse(ctx, payload, transport) {
1873
+ if (ctx.res.writableEnded || ctx.res.destroyed)
1874
+ return false;
1875
+ return writeOrDestroy(ctx.res, `data: ${JSON.stringify(payload)}\n\n`, transport.backpressure, transport.onBackpressure, transport.correlationId);
1753
1876
  }
1754
- function writeStreamTerminal(ctx, id, created, modelId, finishReason, promptTokens, completionTokens) {
1755
- writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, {}, finishReason));
1877
+ function writeStreamTerminal(stream, finishReason, promptTokens, completionTokens) {
1878
+ const { ctx, id, created, modelId, transport } = stream;
1879
+ writeOpenAiSse(ctx, openAiStreamChunk(id, created, modelId, {}, finishReason), transport);
1756
1880
  writeOpenAiSse(ctx, {
1757
1881
  id,
1758
1882
  object: "chat.completion.chunk",
@@ -1764,7 +1888,7 @@ function writeStreamTerminal(ctx, id, created, modelId, finishReason, promptToke
1764
1888
  completion_tokens: completionTokens,
1765
1889
  total_tokens: promptTokens + completionTokens,
1766
1890
  },
1767
- });
1891
+ }, transport);
1768
1892
  if (!ctx.res.writableEnded && !ctx.res.destroyed)
1769
1893
  ctx.res.end("data: [DONE]\n\n");
1770
1894
  }
@@ -1791,17 +1915,44 @@ function gatewayReadinessProjection(ctx, deps) {
1791
1915
  if (result.status === "available" &&
1792
1916
  result.runMetadata.maxPromptTokens >= CODING_WORKBENCH_MINIMUM_CODING_CONTEXT_PROMPT_TOKENS)
1793
1917
  return result;
1794
- const reason = result.status === "available" ? "model-context-window-insufficient" : result.reason;
1795
- if (reason !== "model-context-window-insufficient" &&
1796
- reason !== "no-tool-calling" &&
1797
- reason !== "tool-calling-unverified")
1918
+ const shortfall = result.status === "available" ? "model-context-window-insufficient" : result.reason;
1919
+ if (shortfall !== "model-context-window-insufficient" &&
1920
+ shortfall !== "no-tool-calling" &&
1921
+ shortfall !== "tool-calling-unverified")
1798
1922
  return result;
1923
+ // #3591 (1.1.7): while the automatic probe is still running against a slow gateway the shortfall
1924
+ // is not a verdict. The profile says so, and the Workbench re-reads it instead of refusing.
1925
+ const pending = readinessProbePending(deps, result, shortfall);
1926
+ const reason = pending
1927
+ ? "model-verification-pending"
1928
+ : shortfall;
1929
+ logReadinessShortfall(ctx, result, reason, pending);
1930
+ // An open verification replaces the stored shortfall for an unavailable projection too (an
1931
+ // unverified tool-calling proof whose probe is still running), or the Workbench would stop
1932
+ // reading and keep the refusal until an unrelated refresh.
1933
+ if (result.status === "available" || pending)
1934
+ return { status: "unavailable", reason };
1935
+ return result;
1936
+ }
1937
+ function readinessProbePending(deps, result, shortfall) {
1938
+ if (shortfall === "no-tool-calling")
1939
+ return false;
1940
+ const config = currentGatewayConfig(deps);
1941
+ if (config === undefined)
1942
+ return false;
1943
+ // An unavailable projection (an unverified tool-calling proof) names no model: its verification
1944
+ // is open while any model the Workbench could still elect has an open probe.
1945
+ return result.status === "available"
1946
+ ? isCodingWorkbenchProbePending(config, result.modelAlias)
1947
+ : isAnyCodingWorkbenchProbePending(config);
1948
+ }
1949
+ function logReadinessShortfall(ctx, result, reason, pending) {
1799
1950
  getServerLogger().warn(activityLogEvent(CODING_SIDECAR_GATEWAY_READINESS_INSUFFICIENT_OPERATION, {
1800
1951
  correlationId: correlationIdOrUnknown(ctx.correlationId),
1801
1952
  errorKind: "unavailable",
1802
1953
  }, {
1803
1954
  reason,
1804
- probeMode: "passive",
1955
+ probeMode: pending ? "pending" : "passive",
1805
1956
  ...(result.status === "available"
1806
1957
  ? {
1807
1958
  maxPromptTokens: result.runMetadata.maxPromptTokens,
@@ -1811,9 +1962,6 @@ function gatewayReadinessProjection(ctx, deps) {
1811
1962
  completeness: "complete",
1812
1963
  loss: "none",
1813
1964
  }));
1814
- return result.status === "available"
1815
- ? { status: "unavailable", reason: "model-context-window-insufficient" }
1816
- : result;
1817
1965
  }
1818
1966
  export async function handleCodingSidecarGatewayProfile(ctx, deps) {
1819
1967
  // What Keiko can determine itself it determines itself: a model whose gateway declared no token
@@ -1822,10 +1970,25 @@ export async function handleCodingSidecarGatewayProfile(ctx, deps) {
1822
1970
  // Only while the gateway is the usable source: a subscription source, a disabled policy or a
1823
1971
  // missing configuration must never cause a paid provider probe (ADR-0124 D5).
1824
1972
  if (elected.status === "available" || elected.reason === "tool-calling-unverified") {
1825
- await ensureCodingWorkbenchContextWindows(deps, elected.status === "available" ? elected.modelAlias : undefined, ctx.correlationId);
1973
+ // #3591 (1.1.7): the browser reads this profile with a 15 s deadline while a probe against a
1974
+ // slow gateway may take minutes. Wait a bounded moment for the elected model's proof; past it,
1975
+ // answer with the projection (`model-verification-pending` while the probe runs) and let
1976
+ // the Workbench read again — never leave the read hanging until the browser gives up.
1977
+ await Promise.race([
1978
+ ensureCodingWorkbenchContextWindows(deps, elected.status === "available" ? elected.modelAlias : undefined, ctx.correlationId),
1979
+ boundedProfileWait(),
1980
+ ]);
1826
1981
  }
1827
1982
  return { status: 200, body: gatewayReadinessProjection(ctx, deps) };
1828
1983
  }
1984
+ // Under the browser's 15 s read deadline for this profile (keiko-ui `DEFAULT_READ_TIMEOUT_MS`).
1985
+ export const PROFILE_PROBE_WAIT_MS = 8_000;
1986
+ function boundedProfileWait() {
1987
+ return new Promise((resolve) => {
1988
+ const timer = setTimeout(resolve, PROFILE_PROBE_WAIT_MS);
1989
+ timer.unref();
1990
+ });
1991
+ }
1829
1992
  function upstreamGatewayStreamingSupported(deps, advertisedSupport) {
1830
1993
  return advertisedSupport || deps.codingSidecarGatewayChatStreamFactory !== undefined;
1831
1994
  }
@@ -1859,7 +2022,7 @@ function authenticatedGatewayAdmission(ctx, deps, parsed, authentication, modelA
1859
2022
  registry?.claim(authentication.runId) === true) {
1860
2023
  return {
1861
2024
  kind: "handled",
1862
- result: fixedReadinessResponse(ctx, modelAlias, parsed.stream === true),
2025
+ result: fixedReadinessResponse(ctx, deps, modelAlias, parsed.stream === true),
1863
2026
  };
1864
2027
  }
1865
2028
  if (isExactManagedToolSet(parsed.tools))
@@ -1893,6 +2056,7 @@ function logChatRequestRejection(ctx, deps, runId, validationError, observed) {
1893
2056
  ? {
1894
2057
  estimatedPromptTokens: observed.estimatedPromptTokens,
1895
2058
  maxPromptTokens: observed.bounds.maxPromptTokens,
2059
+ admissiblePromptTokens: admissiblePromptTokens(observed.bounds),
1896
2060
  inputMessageCount: observed.parsed.messages.length,
1897
2061
  maxInputMessages: observed.bounds.maxInputMessages,
1898
2062
  }
@@ -1933,13 +2097,41 @@ async function runHandleCodingSidecarGatewayChatCompletions(ctx, deps) {
1933
2097
  return admission.result;
1934
2098
  return executeBudgetedGatewayChat(ctx, deps, resolved, parsed, authentication, estimatedPromptTokens, {
1935
2099
  modelAlias: resolved.result.modelAlias,
1936
- maxOutputTokens: resolved.result.runMetadata.maxOutputTokens,
2100
+ maxOutputTokens: admittedOutputTokens(resolved.result.runMetadata, estimatedPromptTokens),
1937
2101
  upstreamStreamingSupported: upstreamGatewayStreamingSupported(deps, resolved.result.supportsStreaming),
1938
2102
  ...(authentication.reasoningEffort === undefined
1939
2103
  ? {}
1940
2104
  : { reasoningEffort: authentication.reasoningEffort }),
1941
2105
  });
1942
2106
  }
2107
+ // #3591 (1.1.7): the run's output reserve (8k for an undeclared limit) is a reserve against the
2108
+ // whole window, and `maxPromptTokens` IS that whole window. A prompt close to the window would
2109
+ // leave the provider a request larger than its window, so the allowance actually sent is what
2110
+ // remains after the prompt and the safety margin — and the least allowance that still lets the
2111
+ // model answer (and report an exhausted budget instead of failing silently) is reserved at
2112
+ // admission, never added on top of a prompt that already fills the window.
2113
+ export const MINIMUM_ADMITTED_OUTPUT_TOKENS = 512;
2114
+ // What a prompt must leave free of `maxPromptTokens`: the window's safety margin plus the minimum
2115
+ // allowance (or the whole reserve, when that is smaller).
2116
+ function reservedWindowTokens(bounds) {
2117
+ return (safetyMarginTokensFor(bounds.maxPromptTokens, bounds.maxOutputTokens) +
2118
+ Math.min(MINIMUM_ADMITTED_OUTPUT_TOKENS, bounds.maxOutputTokens));
2119
+ }
2120
+ // Review of #3591 (P1): admission used to check the prompt against `maxPromptTokens` alone while
2121
+ // the allowance floored at 512, so a prompt just under the window was sent WITH 512 output tokens
2122
+ // — past the window the probe had proven, and the provider refused the turn. The prompt must
2123
+ // leave the margin and the minimum allowance free, or the turn is refused before any call.
2124
+ export function admissiblePromptTokens(bounds) {
2125
+ return bounds.maxPromptTokens - reservedWindowTokens(bounds);
2126
+ }
2127
+ // The allowance an ADMITTED turn sends: the run's reserve, shrunk to what the prompt leaves after
2128
+ // the safety margin. Admission (`admissiblePromptTokens`) guarantees at least the minimum.
2129
+ export function admittedOutputTokens(bounds, estimatedPromptTokens) {
2130
+ const remaining = bounds.maxPromptTokens -
2131
+ estimatedPromptTokens -
2132
+ safetyMarginTokensFor(bounds.maxPromptTokens, bounds.maxOutputTokens);
2133
+ return Math.min(bounds.maxOutputTokens, remaining);
2134
+ }
1943
2135
  function logValidatedRequestBounds(ctx, runId, request, bounds, estimatedPromptTokens) {
1944
2136
  getServerLogger().info(activityLogEvent(CODING_SIDECAR_GATEWAY_REQUEST_VALIDATED_OPERATION, {
1945
2137
  correlationId: correlationIdOrUnknown(ctx.correlationId),
@@ -1949,6 +2141,7 @@ function logValidatedRequestBounds(ctx, runId, request, bounds, estimatedPromptT
1949
2141
  maxRequestBytes: bounds.maxRequestBytes,
1950
2142
  maxPromptTokens: bounds.maxPromptTokens,
1951
2143
  estimatedPromptTokens,
2144
+ maxOutputTokens: admittedOutputTokens(bounds, estimatedPromptTokens),
1952
2145
  inputMessageCount: request.messages.length,
1953
2146
  completeness: "complete",
1954
2147
  loss: "none",
@@ -1968,7 +2161,7 @@ function executeBudgetedGatewayChat(ctx, deps, binding, parsed, authentication,
1968
2161
  offerLifetimeMs: opencodeGatewayOfferLifetimeMs(codingSidecarGatewayRequestDeadlineMs(binding.config, profile.modelAlias)),
1969
2162
  });
1970
2163
  }
1971
- function fixedReadinessResponse(ctx, modelId, stream) {
2164
+ function fixedReadinessResponse(ctx, deps, modelId, stream) {
1972
2165
  const response = {
1973
2166
  modelId,
1974
2167
  content: "",
@@ -1983,5 +2176,7 @@ function fixedReadinessResponse(ctx, modelId, stream) {
1983
2176
  costClass: "low",
1984
2177
  },
1985
2178
  };
1986
- return stream ? bufferedOpenAiStream(ctx, modelId, response) : openAiResponse(modelId, response);
2179
+ return stream
2180
+ ? bufferedOpenAiStream(ctx, deps, modelId, response)
2181
+ : openAiResponse(modelId, response);
1987
2182
  }