@bitkyc08/opencodex 2.96.0 → 2.98.0-preview.20261107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (465) hide show
  1. package/bin/ocx.mjs +215 -20
  2. package/gui/dist/assets/App-6TFtZsUF.css +1 -0
  3. package/gui/dist/assets/App-B8_sOxoz.js +59 -0
  4. package/gui/dist/assets/Tray-DpWEp6ee.js +1 -0
  5. package/gui/dist/assets/WantedSansVariable.split.0-Dwvb1WRx.woff2 +0 -0
  6. package/gui/dist/assets/WantedSansVariable.split.1-1wKD2RxI.woff2 +0 -0
  7. package/gui/dist/assets/WantedSansVariable.split.10-CAKA6LIP.woff2 +0 -0
  8. package/gui/dist/assets/WantedSansVariable.split.11-k5H85R4b.woff2 +0 -0
  9. package/gui/dist/assets/WantedSansVariable.split.12-BHboLsUv.woff2 +0 -0
  10. package/gui/dist/assets/WantedSansVariable.split.13-9GV377ix.woff2 +0 -0
  11. package/gui/dist/assets/WantedSansVariable.split.14-DovDXnrj.woff2 +0 -0
  12. package/gui/dist/assets/WantedSansVariable.split.15-DTdykCig.woff2 +0 -0
  13. package/gui/dist/assets/WantedSansVariable.split.16-CKz2ccgK.woff2 +0 -0
  14. package/gui/dist/assets/WantedSansVariable.split.17-Z2RD_KRZ.woff2 +0 -0
  15. package/gui/dist/assets/WantedSansVariable.split.18-BcKtfonJ.woff2 +0 -0
  16. package/gui/dist/assets/WantedSansVariable.split.19-DEQPOSZn.woff2 +0 -0
  17. package/gui/dist/assets/WantedSansVariable.split.2-DryboyuA.woff2 +0 -0
  18. package/gui/dist/assets/WantedSansVariable.split.20-Bia5JMls.woff2 +0 -0
  19. package/gui/dist/assets/WantedSansVariable.split.21-Cr4jFw-E.woff2 +0 -0
  20. package/gui/dist/assets/WantedSansVariable.split.22-DjjB69TZ.woff2 +0 -0
  21. package/gui/dist/assets/WantedSansVariable.split.23-CvUzJskt.woff2 +0 -0
  22. package/gui/dist/assets/WantedSansVariable.split.24-i_eMg5ox.woff2 +0 -0
  23. package/gui/dist/assets/WantedSansVariable.split.25-2aKHwPX-.woff2 +0 -0
  24. package/gui/dist/assets/WantedSansVariable.split.26-B6wXoCEL.woff2 +0 -0
  25. package/gui/dist/assets/WantedSansVariable.split.27-D4h1WfGM.woff2 +0 -0
  26. package/gui/dist/assets/WantedSansVariable.split.28-kvRPCStd.woff2 +0 -0
  27. package/gui/dist/assets/WantedSansVariable.split.29-DHAiwleD.woff2 +0 -0
  28. package/gui/dist/assets/WantedSansVariable.split.3-7H--eyZi.woff2 +0 -0
  29. package/gui/dist/assets/WantedSansVariable.split.30-C66cfAY2.woff2 +0 -0
  30. package/gui/dist/assets/WantedSansVariable.split.31-FDmtiNim.woff2 +0 -0
  31. package/gui/dist/assets/WantedSansVariable.split.32-DouHt2Dd.woff2 +0 -0
  32. package/gui/dist/assets/WantedSansVariable.split.33-B8HVvkJX.woff2 +0 -0
  33. package/gui/dist/assets/WantedSansVariable.split.34-BtkRu1FN.woff2 +0 -0
  34. package/gui/dist/assets/WantedSansVariable.split.35-DBylQbtk.woff2 +0 -0
  35. package/gui/dist/assets/WantedSansVariable.split.36-Sg089Kgh.woff2 +0 -0
  36. package/gui/dist/assets/WantedSansVariable.split.37-CYtvbriS.woff2 +0 -0
  37. package/gui/dist/assets/WantedSansVariable.split.38-DKkSLf8Z.woff2 +0 -0
  38. package/gui/dist/assets/WantedSansVariable.split.39-CecotKZD.woff2 +0 -0
  39. package/gui/dist/assets/WantedSansVariable.split.4-Bv8O3ZMb.woff2 +0 -0
  40. package/gui/dist/assets/WantedSansVariable.split.40-DdCvkmOU.woff2 +0 -0
  41. package/gui/dist/assets/WantedSansVariable.split.41-ESn674Hc.woff2 +0 -0
  42. package/gui/dist/assets/WantedSansVariable.split.42-DL9V7NNV.woff2 +0 -0
  43. package/gui/dist/assets/WantedSansVariable.split.43-yipO0cyk.woff2 +0 -0
  44. package/gui/dist/assets/WantedSansVariable.split.44-6rTmLZhm.woff2 +0 -0
  45. package/gui/dist/assets/WantedSansVariable.split.45-BJAj8lrm.woff2 +0 -0
  46. package/gui/dist/assets/WantedSansVariable.split.46-BDTzMn2c.woff2 +0 -0
  47. package/gui/dist/assets/WantedSansVariable.split.47-Bxd4wGgi.woff2 +0 -0
  48. package/gui/dist/assets/WantedSansVariable.split.48-DxnFBLnH.woff2 +0 -0
  49. package/gui/dist/assets/WantedSansVariable.split.49-DnVD0aOD.woff2 +0 -0
  50. package/gui/dist/assets/WantedSansVariable.split.5-B1wI2pp5.woff2 +0 -0
  51. package/gui/dist/assets/WantedSansVariable.split.50-D9JlkZd6.woff2 +0 -0
  52. package/gui/dist/assets/WantedSansVariable.split.51-D9Vw6Bu3.woff2 +0 -0
  53. package/gui/dist/assets/WantedSansVariable.split.52-ClEJk4kn.woff2 +0 -0
  54. package/gui/dist/assets/WantedSansVariable.split.53-BSXU0vh-.woff2 +0 -0
  55. package/gui/dist/assets/WantedSansVariable.split.54-EcuxkR9v.woff2 +0 -0
  56. package/gui/dist/assets/WantedSansVariable.split.55-CYiFhQX8.woff2 +0 -0
  57. package/gui/dist/assets/WantedSansVariable.split.56--Gk9g8BK.woff2 +0 -0
  58. package/gui/dist/assets/WantedSansVariable.split.57-DnkOPZcy.woff2 +0 -0
  59. package/gui/dist/assets/WantedSansVariable.split.58-9MB8504z.woff2 +0 -0
  60. package/gui/dist/assets/WantedSansVariable.split.59-ByxIsN72.woff2 +0 -0
  61. package/gui/dist/assets/WantedSansVariable.split.6-Bmk0dRVw.woff2 +0 -0
  62. package/gui/dist/assets/WantedSansVariable.split.60-Bxwx_aWQ.woff2 +0 -0
  63. package/gui/dist/assets/WantedSansVariable.split.61-DA2z5sm7.woff2 +0 -0
  64. package/gui/dist/assets/WantedSansVariable.split.62-C5iJPJZn.woff2 +0 -0
  65. package/gui/dist/assets/WantedSansVariable.split.63-DAqrmaEc.woff2 +0 -0
  66. package/gui/dist/assets/WantedSansVariable.split.64-BgXFsljb.woff2 +0 -0
  67. package/gui/dist/assets/WantedSansVariable.split.65-DKiWujvu.woff2 +0 -0
  68. package/gui/dist/assets/WantedSansVariable.split.66-8po4vUZ4.woff2 +0 -0
  69. package/gui/dist/assets/WantedSansVariable.split.67-Cy5qyI9O.woff2 +0 -0
  70. package/gui/dist/assets/WantedSansVariable.split.68-BKny7Fy3.woff2 +0 -0
  71. package/gui/dist/assets/WantedSansVariable.split.69-B4VU4u_b.woff2 +0 -0
  72. package/gui/dist/assets/WantedSansVariable.split.7-BuqwNpgA.woff2 +0 -0
  73. package/gui/dist/assets/WantedSansVariable.split.70-BCd4n346.woff2 +0 -0
  74. package/gui/dist/assets/WantedSansVariable.split.71-CkQppaWn.woff2 +0 -0
  75. package/gui/dist/assets/WantedSansVariable.split.72-Db7vaoFs.woff2 +0 -0
  76. package/gui/dist/assets/WantedSansVariable.split.73-DI80TYWl.woff2 +0 -0
  77. package/gui/dist/assets/WantedSansVariable.split.74-tMCtDAHB.woff2 +0 -0
  78. package/gui/dist/assets/WantedSansVariable.split.75-Ciw0D5WG.woff2 +0 -0
  79. package/gui/dist/assets/WantedSansVariable.split.76-DoLjFgp-.woff2 +0 -0
  80. package/gui/dist/assets/WantedSansVariable.split.77-CpT2uvMy.woff2 +0 -0
  81. package/gui/dist/assets/WantedSansVariable.split.78-CkcJ4iZ1.woff2 +0 -0
  82. package/gui/dist/assets/WantedSansVariable.split.79-DsOUJjEM.woff2 +0 -0
  83. package/gui/dist/assets/WantedSansVariable.split.8-DsjypPd6.woff2 +0 -0
  84. package/gui/dist/assets/WantedSansVariable.split.80-C0VwHTte.woff2 +0 -0
  85. package/gui/dist/assets/WantedSansVariable.split.81-DTelu57i.woff2 +0 -0
  86. package/gui/dist/assets/WantedSansVariable.split.82-BiMndGN1.woff2 +0 -0
  87. package/gui/dist/assets/WantedSansVariable.split.83-CVcC6eDG.woff2 +0 -0
  88. package/gui/dist/assets/WantedSansVariable.split.84-DlhZVt_O.woff2 +0 -0
  89. package/gui/dist/assets/WantedSansVariable.split.85-S8I2D3pA.woff2 +0 -0
  90. package/gui/dist/assets/WantedSansVariable.split.86-DozXXpH5.woff2 +0 -0
  91. package/gui/dist/assets/WantedSansVariable.split.87-Dsn0Vuuo.woff2 +0 -0
  92. package/gui/dist/assets/WantedSansVariable.split.88-iNwxxDCr.woff2 +0 -0
  93. package/gui/dist/assets/WantedSansVariable.split.89-B63u_9CZ.woff2 +0 -0
  94. package/gui/dist/assets/WantedSansVariable.split.9-Bi3IzpMI.woff2 +0 -0
  95. package/gui/dist/assets/WantedSansVariable.split.90-ByeH1Pqf.woff2 +0 -0
  96. package/gui/dist/assets/WantedSansVariable.split.91-UQLlagpm.woff2 +0 -0
  97. package/gui/dist/assets/brand-mark-VPhzz-WE.js +1 -0
  98. package/gui/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
  99. package/gui/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
  100. package/gui/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
  101. package/gui/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
  102. package/gui/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
  103. package/gui/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
  104. package/gui/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
  105. package/gui/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
  106. package/gui/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
  107. package/gui/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
  108. package/gui/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
  109. package/gui/dist/assets/index-5REODpG3.css +2 -0
  110. package/gui/dist/assets/index-BWKBeK77.js +94 -0
  111. package/gui/dist/assets/segment-thumb-features-BQYzrze9.js +1 -0
  112. package/gui/dist/classic/assets/App-Bz65x1J6.js +58 -0
  113. package/gui/dist/{assets/Tray-BOPQyDbH.js → classic/assets/Tray-kk7mdO6I.js} +1 -1
  114. package/gui/dist/classic/assets/index-Dp2j8bSc.js +94 -0
  115. package/gui/dist/classic/assets/quota-summary-CvamtHjB.js +1 -0
  116. package/gui/dist/classic/index.html +47 -0
  117. package/gui/dist/favicon.png +0 -0
  118. package/gui/dist/favicon.svg +14 -0
  119. package/gui/dist/index.html +15 -2
  120. package/package.json +1 -1
  121. package/src/adapters/anthropic.ts +17 -8
  122. package/src/adapters/command-code.ts +15 -16
  123. package/src/adapters/cursor/transport-retry.ts +2 -2
  124. package/src/adapters/devin/cloud-direct/chat.ts +15 -6
  125. package/src/adapters/google.ts +7 -7
  126. package/src/adapters/kiro/adapter.ts +18 -3
  127. package/src/adapters/kiro/conversation.ts +8 -8
  128. package/src/adapters/kiro/payload.ts +45 -12
  129. package/src/adapters/kiro/stream.ts +230 -14
  130. package/src/adapters/kiro/structured-output-json.ts +191 -0
  131. package/src/adapters/kiro/structured-output-schema.ts +542 -0
  132. package/src/adapters/kiro/structured-output.ts +367 -0
  133. package/src/adapters/kiro-constants.ts +24 -1
  134. package/src/adapters/kiro-events.ts +31 -1
  135. package/src/adapters/kiro-retry.ts +3 -3
  136. package/src/adapters/kiro-tools.ts +14 -9
  137. package/src/adapters/kiro-wire.ts +9 -5
  138. package/src/adapters/ollama-native.ts +11 -1
  139. package/src/adapters/openai-chat/messages.ts +58 -5
  140. package/src/adapters/openai-chat/mistral-image-limit.ts +50 -0
  141. package/src/adapters/openai-chat/passthrough.ts +5 -3
  142. package/src/adapters/openai-chat/tool-schema.ts +83 -13
  143. package/src/adapters/openai-responses/empty-assistant.ts +15 -3
  144. package/src/adapters/openai-responses/opengateway-input.ts +18 -6
  145. package/src/adapters/openai-responses/passthrough.ts +5 -4
  146. package/src/adapters/openai-responses/reasoning.ts +9 -4
  147. package/src/adapters/openai-responses/request-strips.ts +1 -1
  148. package/src/adapters/openai-responses/tool-output-recovery.ts +79 -0
  149. package/src/adapters/physical-send.ts +3 -3
  150. package/src/adapters/qoder/live-models.ts +115 -1
  151. package/src/adapters/zed-openai-stream.ts +2 -1
  152. package/src/bridge/errors.ts +11 -2
  153. package/src/bridge/response-json.ts +2 -1
  154. package/src/bridge/sse.ts +14 -6
  155. package/src/chat/failure-verdict.ts +21 -0
  156. package/src/chat/outbound.ts +139 -21
  157. package/src/claude/failure-verdict.ts +21 -0
  158. package/src/claude/inbound-content-options.ts +15 -2
  159. package/src/claude/inbound-model-options.ts +2 -1
  160. package/src/claude/inbound-records.ts +2 -0
  161. package/src/claude/inbound.ts +17 -11
  162. package/src/claude/outbound.ts +98 -22
  163. package/src/cli/capabilities-access-remote.ts +13 -0
  164. package/src/cli/capabilities-base.ts +26 -3
  165. package/src/cli/capabilities-command.ts +12 -3
  166. package/src/cli/capabilities-provider-models.ts +3 -3
  167. package/src/cli/capability-contracts.ts +2 -0
  168. package/src/cli/codex-launch-auth.ts +30 -1
  169. package/src/cli/codex-launcher.ts +12 -5
  170. package/src/cli/codex-shim-autorestore.ts +12 -4
  171. package/src/cli/config-command.ts +11 -2
  172. package/src/cli/connect.ts +17 -2
  173. package/src/cli/dispatch.ts +14 -177
  174. package/src/cli/doctor.ts +95 -7
  175. package/src/cli/help.ts +2 -2
  176. package/src/cli/index.ts +17 -8
  177. package/src/cli/link-apply-restart.ts +9 -2
  178. package/src/cli/link.ts +1 -1
  179. package/src/cli/models.ts +124 -38
  180. package/src/cli/registry.ts +12 -4
  181. package/src/cli/restart-failure.ts +17 -0
  182. package/src/cli/root.ts +8 -0
  183. package/src/cli/stop-approval.ts +2 -2
  184. package/src/cli/sync-command.ts +243 -0
  185. package/src/cli/sync-fanout-process.ts +92 -0
  186. package/src/cli/sync-fanout.ts +161 -0
  187. package/src/cli/sync-flags.ts +10 -0
  188. package/src/cli/sync-link-status.ts +39 -0
  189. package/src/cli/sync-update-guard.ts +18 -0
  190. package/src/cli/system-restart-client.ts +5 -0
  191. package/src/cli/uninstall-client-state.ts +12 -1
  192. package/src/cli/update-restart-child.ts +13 -3
  193. package/src/cli/update-restart-home.ts +6 -3
  194. package/src/cli/update-restart.ts +36 -19
  195. package/src/client/connect.ts +35 -15
  196. package/src/client/hub-client.ts +5 -0
  197. package/src/client/link-home.ts +3 -2
  198. package/src/client/link-management-relay.ts +39 -2
  199. package/src/client/link-readiness.ts +3 -1
  200. package/src/client/machine-api.ts +2 -1
  201. package/src/client/machine-listener.ts +10 -0
  202. package/src/client/orphan-token-recovery.ts +322 -0
  203. package/src/client/restore-ownership.ts +19 -1
  204. package/src/client/runtime-restart.ts +12 -4
  205. package/src/client/runtime.ts +40 -11
  206. package/src/client/stop-routing.ts +62 -0
  207. package/src/client/sync-runtime.ts +65 -0
  208. package/src/clients/config-export.ts +32 -3
  209. package/src/codex/auth-context.ts +49 -103
  210. package/src/codex/catalog/antigravity-discovery.ts +16 -5
  211. package/src/codex/catalog/provider-models.ts +18 -12
  212. package/src/codex/forward-transport-headers.ts +35 -6
  213. package/src/codex/inject/config-toml.ts +88 -0
  214. package/src/codex/inject/multi-agent-v2.ts +30 -4
  215. package/src/codex/inject/plan.ts +34 -7
  216. package/src/codex/inject-coordination.ts +11 -0
  217. package/src/codex/inject.ts +18 -7
  218. package/src/codex/lineage.ts +13 -7
  219. package/src/codex/native-profile-api.ts +27 -1
  220. package/src/codex/quota.ts +22 -6
  221. package/src/codex/request-owned-affinity-key.ts +11 -0
  222. package/src/codex/request-owned-affinity.ts +118 -0
  223. package/src/codex/request-owned-main-pin.ts +86 -0
  224. package/src/codex/role-capability.ts +52 -41
  225. package/src/codex/routing/active-account.ts +13 -5
  226. package/src/codex/routing/selection.ts +40 -1
  227. package/src/codex/routing/thread-affinity-store.ts +916 -0
  228. package/src/codex/routing/thread-affinity.ts +90 -12
  229. package/src/codex/shim-diagnostics.ts +7 -0
  230. package/src/codex/shim-legacy-record.ts +290 -0
  231. package/src/codex/shim-windows-refresh.ts +9 -2
  232. package/src/codex/shim-wrapper-write.ts +7 -0
  233. package/src/codex/shim.ts +80 -21
  234. package/src/codex/toml-source-lines.ts +72 -13
  235. package/src/combos/failover.ts +6 -109
  236. package/src/combos/request.ts +2 -2
  237. package/src/combos/resolve.ts +29 -11
  238. package/src/config/diagnostics.ts +9 -9
  239. package/src/config/load-degrade.ts +27 -27
  240. package/src/config/process-state.ts +5 -0
  241. package/src/config/provider-validation.ts +9 -2
  242. package/src/config/removed-opencode-free-backup.ts +6 -3
  243. package/src/config/schema/leaf-validators.ts +1 -0
  244. package/src/config/tracked-runtime-inspection.ts +3 -3
  245. package/src/generated/compatibility-version.json +595 -255
  246. package/src/images/loop.ts +4 -3
  247. package/src/lib/client-error-body.ts +215 -0
  248. package/src/lib/client-retry-after.ts +66 -0
  249. package/src/lib/config-ownership-removal.ts +422 -0
  250. package/src/lib/config-ownership.ts +650 -81
  251. package/src/lib/context-overflow-verdict.ts +113 -0
  252. package/src/lib/crash-guard.ts +4 -2
  253. package/src/lib/desktop-cli-handoff.d.mts +4 -5
  254. package/src/lib/desktop-cli-handoff.mjs +18 -17
  255. package/src/lib/empty-tool-catalog.ts +25 -3
  256. package/src/lib/error-destination.ts +47 -0
  257. package/src/lib/errors.ts +1 -0
  258. package/src/lib/failure-detail.ts +66 -0
  259. package/src/lib/local-management-capability.ts +3 -1
  260. package/src/lib/messages-error-body.ts +162 -0
  261. package/src/lib/messages-sse-error-presentation.ts +93 -0
  262. package/src/lib/model-identity.ts +44 -0
  263. package/src/lib/passive-read.ts +9 -4
  264. package/src/lib/private-sqlite-path.ts +124 -0
  265. package/src/lib/response-service-tier.ts +17 -0
  266. package/src/lib/retry-after.ts +3 -0
  267. package/src/lib/socks5-fetch.ts +21 -9
  268. package/src/lib/spend-ledger-owner.ts +43 -4
  269. package/src/lib/upstream-condition.ts +142 -0
  270. package/src/lib/upstream-retry.ts +95 -12
  271. package/src/lib/windows-owner-acl.ts +22 -9
  272. package/src/lib/winsw.ts +5 -5
  273. package/src/lib/workflow-budget.ts +3 -1
  274. package/src/link/remote-platform.ts +14 -0
  275. package/src/link/remote-windows.ts +51 -9
  276. package/src/link/ssh-argv.ts +63 -10
  277. package/src/oauth/generic-account-failover.ts +13 -11
  278. package/src/oauth/index.ts +42 -73
  279. package/src/oauth/oauth-errors.ts +67 -0
  280. package/src/oauth/store.ts +21 -11
  281. package/src/protocols/codecs/messages-ir.ts +15 -9
  282. package/src/protocols/codecs/ollama.ts +197 -0
  283. package/src/protocols/encoders/adapter-events.ts +30 -7
  284. package/src/protocols/encoders/chat.ts +18 -10
  285. package/src/protocols/encoders/messages.ts +72 -28
  286. package/src/protocols/trace.ts +18 -0
  287. package/src/providers/antigravity-models.ts +45 -12
  288. package/src/providers/fastwire.ts +3 -2
  289. package/src/providers/free-directory.ts +2 -2
  290. package/src/providers/quota/anthropic-usage-windows.ts +13 -3
  291. package/src/providers/quota/vendor-probes-key.ts +60 -24
  292. package/src/providers/reasoning-metadata.ts +2 -1
  293. package/src/providers/registry/entries-extended.ts +9 -0
  294. package/src/providers/request-pacing.ts +3 -2
  295. package/src/reasoning-effort.ts +7 -2
  296. package/src/remote/hub-usage.ts +3 -0
  297. package/src/responses/apply-patch-envelope.ts +45 -19
  298. package/src/responses/compaction-history-framing.ts +5 -1
  299. package/src/responses/freeform-wrapper-scan.ts +5 -3
  300. package/src/responses/progressive-freeform-input.ts +354 -45
  301. package/src/routing/fallback-policy.ts +3 -1
  302. package/src/server/admission-model-scope.ts +17 -6
  303. package/src/server/admitted-http-turn.ts +57 -0
  304. package/src/server/anthropic-rate-limit-headers.ts +75 -3
  305. package/src/server/auth-cors.ts +5 -0
  306. package/src/server/chat-completions.ts +45 -20
  307. package/src/server/chat-native-sse.ts +52 -7
  308. package/src/server/chat-native.ts +60 -35
  309. package/src/server/claude-bridge-pool-session.ts +59 -0
  310. package/src/server/claude-messages.ts +174 -84
  311. package/src/server/context-history.ts +5 -1
  312. package/src/server/gui-static.ts +4 -1
  313. package/src/server/hub-usage.ts +7 -2
  314. package/src/server/inbound-body-admission.ts +11 -4
  315. package/src/server/index/link-management.ts +8 -0
  316. package/src/server/index/ollama-routes.ts +91 -0
  317. package/src/server/index/serve-options.ts +44 -24
  318. package/src/server/index/startup-warnings.ts +5 -12
  319. package/src/server/index/websocket-handler.ts +5 -2
  320. package/src/server/index.ts +6 -36
  321. package/src/server/inference/client-encoder-delivery.ts +9 -3
  322. package/src/server/json-serialized-bytes.ts +44 -0
  323. package/src/server/management/agent-settings-routes.ts +24 -21
  324. package/src/server/management/codex-cli-update-routes.ts +2 -1
  325. package/src/server/management/logs-usage-routes.ts +89 -4
  326. package/src/server/management/provider-routes.ts +2 -22
  327. package/src/server/management/qoder-provider-probe.ts +105 -0
  328. package/src/server/management/route-registry.ts +4 -1
  329. package/src/server/management/usage-aggregate-cache.ts +15 -5
  330. package/src/server/management/usage-source-facets.ts +38 -0
  331. package/src/server/messages-native-scope.ts +2 -1
  332. package/src/server/messages-native-tool-guard.ts +12 -4
  333. package/src/server/messages-native.ts +123 -47
  334. package/src/server/messages-response-headers.ts +25 -11
  335. package/src/server/messages-shared.ts +99 -65
  336. package/src/server/messages-sse-error-filter.ts +199 -0
  337. package/src/server/native-final-log.ts +93 -0
  338. package/src/server/ollama-inbound.ts +349 -0
  339. package/src/server/ollama-native-paths.ts +21 -0
  340. package/src/server/ollama-route-policy.ts +73 -0
  341. package/src/server/relay.ts +22 -3
  342. package/src/server/request-decompress.ts +4 -2
  343. package/src/server/request-log-failure-attribution.ts +22 -0
  344. package/src/server/request-log-failure-detail.ts +80 -0
  345. package/src/server/request-log-terminal-status.ts +4 -2
  346. package/src/server/request-log.ts +91 -42
  347. package/src/server/response-log-body.ts +6 -1
  348. package/src/server/responses/account-change-state.ts +17 -6
  349. package/src/server/responses/adapter-continuation.ts +17 -6
  350. package/src/server/responses/adapter-delivery.ts +17 -10
  351. package/src/server/responses/adapter-dispatch.ts +32 -31
  352. package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
  353. package/src/server/responses/codex-auth-error.ts +24 -19
  354. package/src/server/responses/codex-ws-exchange.ts +11 -8
  355. package/src/server/responses/codex-ws-metadata.ts +94 -21
  356. package/src/server/responses/codex-ws-request.ts +3 -1
  357. package/src/server/responses/codex-ws-wire.ts +19 -3
  358. package/src/server/responses/compact.ts +57 -24
  359. package/src/server/responses/compaction-recovery.ts +4 -2
  360. package/src/server/responses/compaction-routing.ts +9 -4
  361. package/src/server/responses/core-auth.ts +32 -14
  362. package/src/server/responses/core-codex-account.ts +17 -6
  363. package/src/server/responses/core-combo-failure.ts +7 -1
  364. package/src/server/responses/core-combo-native.ts +2 -1
  365. package/src/server/responses/core-combo.ts +43 -27
  366. package/src/server/responses/core-errors.ts +24 -21
  367. package/src/server/responses/core-lifetime.ts +6 -3
  368. package/src/server/responses/core-normalize.ts +1 -0
  369. package/src/server/responses/core-options.ts +9 -0
  370. package/src/server/responses/core-pipeline.ts +4 -3
  371. package/src/server/responses/empty-completion-guard.ts +4 -1
  372. package/src/server/responses/fetch-helpers.ts +7 -1
  373. package/src/server/responses/input-admission.ts +28 -2
  374. package/src/server/responses/native-injection.ts +1 -1
  375. package/src/server/responses/native-response-json.ts +13 -4
  376. package/src/server/responses/native-response-output.ts +1 -2
  377. package/src/server/responses/non-replayable-error.ts +2 -1
  378. package/src/server/responses/pacing-overload.ts +3 -2
  379. package/src/server/responses/passthrough-delivery.ts +55 -14
  380. package/src/server/responses/passthrough-dispatch.ts +20 -18
  381. package/src/server/responses/passthrough-error.ts +9 -9
  382. package/src/server/responses/policy-fallback.ts +2 -1
  383. package/src/server/responses/request-prepare.ts +69 -48
  384. package/src/server/responses/request-transport.ts +36 -31
  385. package/src/server/responses/response-effects.ts +3 -2
  386. package/src/server/responses/run-turn-execution.ts +6 -6
  387. package/src/server/responses/sidecar-execution.ts +16 -12
  388. package/src/server/responses/spend-storage-error.ts +2 -1
  389. package/src/server/responses/terminal-guard.ts +30 -0
  390. package/src/server/responses/ws-upstream.ts +9 -0
  391. package/src/server/responses-custom-tool-repair.ts +14 -3
  392. package/src/server/responses-terminal-repair.ts +5 -2
  393. package/src/server/responses-undeclared-tool-guard.ts +39 -16
  394. package/src/server/restart-replacement.ts +3 -1
  395. package/src/server/safe-response-headers.ts +162 -13
  396. package/src/server/sse-payload-rewrite.ts +23 -11
  397. package/src/server/turn-state-handles.ts +164 -0
  398. package/src/server/workflow-refusal.ts +2 -1
  399. package/src/server/ws-bridge.ts +4 -0
  400. package/src/service/definition.ts +36 -4
  401. package/src/service/launchd.ts +89 -32
  402. package/src/service/logging.ts +34 -7
  403. package/src/service/ownership-mutation-lease.d.mts +11 -2
  404. package/src/service/ownership-mutation-lease.mjs +53 -11
  405. package/src/service/plist-xml.ts +59 -0
  406. package/src/service/state.ts +8 -0
  407. package/src/service/systemd-env.ts +2 -0
  408. package/src/service/systemd.ts +31 -7
  409. package/src/service/windows-taskxml.ts +16 -7
  410. package/src/service-manager-probe.ts +4 -10
  411. package/src/types/config.ts +2 -0
  412. package/src/types/provider.ts +2 -0
  413. package/src/types/request.ts +13 -1
  414. package/src/update/config-preflight.ts +48 -0
  415. package/src/update/index.ts +148 -26
  416. package/src/update/npm-install.d.mts +8 -0
  417. package/src/update/npm-install.mjs +20 -1
  418. package/src/update/orchestrated-admission.d.mts +4 -0
  419. package/src/update/orchestrated-admission.mjs +12 -0
  420. package/src/update/orchestrated-arguments.d.mts +12 -0
  421. package/src/update/orchestrated-arguments.mjs +64 -0
  422. package/src/update/orchestrated-child.d.mts +12 -0
  423. package/src/update/orchestrated-child.mjs +70 -0
  424. package/src/update/orchestrated-entry.ts +234 -0
  425. package/src/update/orchestrated-identity-probe-entry.ts +20 -0
  426. package/src/update/orchestrated-identity.ts +53 -0
  427. package/src/update/orchestrated-live-identity.ts +48 -0
  428. package/src/update/orchestrated-node-driver.mjs +99 -0
  429. package/src/update/orchestrated-outcome.d.mts +21 -0
  430. package/src/update/orchestrated-outcome.mjs +40 -0
  431. package/src/update/orchestrated-plan.ts +49 -0
  432. package/src/update/orchestrated-pnpm.d.mts +8 -0
  433. package/src/update/orchestrated-pnpm.mjs +33 -0
  434. package/src/update/orchestrated-session.d.mts +37 -0
  435. package/src/update/orchestrated-session.mjs +264 -0
  436. package/src/update/orchestrated-token.d.mts +10 -0
  437. package/src/update/orchestrated-token.mjs +21 -0
  438. package/src/update/orchestrated-token.ts +3 -0
  439. package/src/update/orchestrated-tree.d.mts +1 -0
  440. package/src/update/orchestrated-tree.mjs +14 -0
  441. package/src/update/pnpm-global-install.d.mts +6 -0
  442. package/src/update/pnpm-global-install.mjs +9 -2
  443. package/src/update/posix-install-supervisor-entry.mjs +6 -0
  444. package/src/update/posix-install-supervisor.d.mts +10 -0
  445. package/src/update/posix-install-supervisor.mjs +88 -0
  446. package/src/update/sync-update-mutex.d.mts +10 -0
  447. package/src/update/sync-update-mutex.mjs +164 -0
  448. package/src/update/sync-update-mutex.ts +1 -0
  449. package/src/update/test-home-guard.mjs +33 -0
  450. package/src/update/transactional-install.mjs +2 -2
  451. package/src/usage/client.ts +43 -0
  452. package/src/usage/error-detail.ts +225 -0
  453. package/src/usage/log.ts +64 -27
  454. package/src/usage/source-facets.ts +51 -0
  455. package/src/usage/source-filter.ts +65 -0
  456. package/src/usage/summary.ts +50 -8
  457. package/src/web-search/anthropic-executor.ts +105 -24
  458. package/src/web-search/executor.ts +5 -3
  459. package/src/web-search/loop.ts +33 -6
  460. package/src/web-search/progress-stream.ts +64 -5
  461. package/gui/dist/assets/App-bwugJRKn.js +0 -58
  462. package/gui/dist/assets/index-ebuVUtB7.js +0 -94
  463. package/gui/dist/assets/quota-summary-6HsrHQAy.js +0 -1
  464. /package/gui/dist/{assets → classic/assets}/App-B4j-Txmb.css +0 -0
  465. /package/gui/dist/{assets → classic/assets}/index-CHWwDo9L.css +0 -0
@@ -0,0 +1,542 @@
1
+ /**
2
+ * Full validation for strict structured output (`strict: true`) on Kiro.
3
+ *
4
+ * A strict caller is promised a value that conforms to the whole schema, which the light root
5
+ * checks in structured-output.ts cannot keep: `{"title":42,"extra":true}` passes a root `type` and
6
+ * `required` check against a schema that wants a string title and forbids extra keys. Kiro's wire
7
+ * cannot constrain decoding, so the emulation enforces the contract afterwards instead.
8
+ *
9
+ * Every schema this module accepts is validated exactly and in bounded work; anything else is
10
+ * refused before a request is sent, because relaying a value the proxy could not check would break
11
+ * the promise strict mode makes. The supported subset:
12
+ *
13
+ * - `type` (a known type or a non-empty array of them), `properties`, `required`,
14
+ * `additionalProperties` (boolean or schema), `items` (one schema), `enum`, `const`, `anyOf`.
15
+ * A `const` or `enum` literal that contains a number is refused: the request reaches this module
16
+ * already parsed, so a literal such as `1.0000000000000001` arrives as `1`, and an exact equality
17
+ * check would accept a value the caller excluded.
18
+ * - `$ref` only as `#/$defs/<name>` or `#/definitions/<name>`, naming a definition in the root's
19
+ * `$defs`/`definitions`. `$defs`/`definitions` are accepted only at the root. Root-relative
20
+ * references (`#`, `#/properties/...`), references into annotations and remote references are
21
+ * refused, and so is a cycle of references and `anyOf` branches that consumes none of the value.
22
+ * - `minLength`, `maxLength`, `minItems`, `maxItems` as non-negative safe integers.
23
+ * - `minimum`, `maximum`, `exclusiveMinimum`, `exclusiveMaximum` as finite numbers that are either
24
+ * safe integers or non-integers with at most 15 significant digits. This is a heuristic: a bound
25
+ * written with more digits than a double holds can still arrive rounded, so a value within about
26
+ * one unit in the last place of the bound may be judged on the wrong side. The model's numbers
27
+ * are compared against their original text exactly.
28
+ * - `multipleOf`, `pattern` and `patternProperties` are refused. `multipleOf` is exact equality on a
29
+ * possibly rounded number, and a regular expression evaluated against model output can take
30
+ * super-linear time on the request path.
31
+ *
32
+ * Annotations (`title`, `description`, `default`, ...) carry no assertion and are ignored.
33
+ *
34
+ * Work is bounded per value: one validation may visit at most
35
+ * {@link KIRO_STRICT_BASE_VISITS} schema nodes plus {@link KIRO_STRICT_VISITS_PER_VALUE_NODE} per JSON
36
+ * value in the answer, and nest at most {@link KIRO_STRICT_MAX_DEPTH} schema levels deep across
37
+ * references, `anyOf` branches, properties and items. A value that needs more is reported as
38
+ * non-conforming, so a recursive `anyOf` cannot branch exponentially and a deep value cannot
39
+ * exhaust the stack.
40
+ */
41
+ import {
42
+ compareKiroDecimal,
43
+ isKiroDecimalInteger,
44
+ kiroDecimal,
45
+ type KiroDecimal,
46
+ type KiroJsonValue,
47
+ } from "./structured-output-json";
48
+
49
+ type JsonRecord = Record<string, unknown>;
50
+
51
+ /** Schema-node visits every validation may spend regardless of the value's size. */
52
+ export const KIRO_STRICT_BASE_VISITS = 10_000;
53
+ /** Extra schema-node visits granted per JSON value in the answer. */
54
+ export const KIRO_STRICT_VISITS_PER_VALUE_NODE = 16;
55
+ /** One depth limit across references, anyOf branches, properties and items. */
56
+ export const KIRO_STRICT_MAX_DEPTH = 128;
57
+ /** Deepest schema nesting accepted at the boundary. */
58
+ const MAX_SCHEMA_DEPTH = 64;
59
+ /** Schema nodes accepted at the boundary; preparation work is linear in this. */
60
+ const MAX_SCHEMA_NODES = 10_000;
61
+ /** JSON values accepted across every `const`/`enum` literal of one schema. */
62
+ const MAX_LITERAL_NODES = 10_000;
63
+
64
+ const KNOWN_TYPES = new Set(["object", "array", "string", "number", "integer", "boolean", "null"]);
65
+ const ANNOTATIONS = new Set([
66
+ "title", "description", "default", "examples", "$schema", "$comment", "deprecated", "readOnly", "writeOnly",
67
+ ]);
68
+ const COUNTS = ["minLength", "maxLength", "minItems", "maxItems"] as const;
69
+ const BOUNDS = ["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum"] as const;
70
+ const REF = /^#\/(\$defs|definitions)\/([^/]+)$/;
71
+
72
+ /** One schema position, prepared once at the boundary. */
73
+ interface StrictNode {
74
+ never?: true;
75
+ ref?: StrictNode;
76
+ types?: string[];
77
+ enumValues?: unknown[];
78
+ constValue?: { value: unknown };
79
+ anyOf?: StrictNode[];
80
+ minLength?: number;
81
+ maxLength?: number;
82
+ minItems?: number;
83
+ maxItems?: number;
84
+ minimum?: KiroDecimal;
85
+ maximum?: KiroDecimal;
86
+ exclusiveMinimum?: KiroDecimal;
87
+ exclusiveMaximum?: KiroDecimal;
88
+ items?: StrictNode;
89
+ required?: string[];
90
+ properties?: Map<string, StrictNode>;
91
+ closed?: true;
92
+ additional?: StrictNode;
93
+ }
94
+
95
+ /** A plain JSON object: not null and not an array. */
96
+ function isRecord(value: unknown): value is JsonRecord {
97
+ return value !== null && typeof value === "object" && !Array.isArray(value);
98
+ }
99
+
100
+ /**
101
+ * JSON Schema keyword names a refusal may name. Any other key is the caller's own text and is not
102
+ * echoed: a refusal names a keyword only from this fixed vocabulary.
103
+ */
104
+ const NAMEABLE_KEYWORDS = new Set([
105
+ "schema", "type", "properties", "required", "additionalProperties", "items", "enum", "const", "anyOf",
106
+ "oneOf", "allOf", "not", "if", "then", "else", "$ref", "$defs", "definitions", "$anchor", "$dynamicRef",
107
+ "$dynamicAnchor", "$id", "$recursiveRef", "$recursiveAnchor", "multipleOf", "pattern", "patternProperties",
108
+ "minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum", "minLength", "maxLength", "minItems",
109
+ "maxItems", "minProperties", "maxProperties", "uniqueItems", "contains", "minContains", "maxContains",
110
+ "prefixItems", "additionalItems", "unevaluatedItems", "unevaluatedProperties", "propertyNames",
111
+ "dependentRequired", "dependentSchemas", "dependencies", "format", "contentEncoding",
112
+ "contentMediaType", "contentSchema",
113
+ ]);
114
+
115
+ class Unsupported extends Error {
116
+ readonly keyword: string | undefined;
117
+ constructor(keyword: string) {
118
+ super("unsupported strict schema keyword");
119
+ this.keyword = NAMEABLE_KEYWORDS.has(keyword) ? keyword : undefined;
120
+ }
121
+ }
122
+
123
+ /** Whether a schema number still states what the caller wrote after JSON.parse rounded it. */
124
+ function isFaithfulNumber(value: unknown): value is number {
125
+ if (typeof value !== "number" || !Number.isFinite(value)) return false;
126
+ if (Number.isInteger(value)) return Number.isSafeInteger(value);
127
+ return kiroDecimal(String(value)).digits.length <= 15;
128
+ }
129
+
130
+ /**
131
+ * The JSON values in a `const`/`enum` literal, or -1 when it contains a number. Iterative, so a deep
132
+ * literal is safe; it stops counting at `limit`.
133
+ */
134
+ function literalNodes(literal: unknown, limit: number): number {
135
+ const stack = [literal];
136
+ let count = 0;
137
+ while (stack.length > 0 && count <= limit) {
138
+ const item = stack.pop();
139
+ count += 1;
140
+ if (typeof item === "number") return -1;
141
+ if (Array.isArray(item)) for (const child of item) stack.push(child);
142
+ else if (isRecord(item)) for (const child of Object.values(item)) stack.push(child);
143
+ }
144
+ return count;
145
+ }
146
+
147
+ /** A prepared strict schema: the root node of its validation graph. */
148
+ interface PreparedSchema {
149
+ root: StrictNode;
150
+ }
151
+
152
+ /** Build the validation graph for `schema`, or throw {@link Unsupported} naming the first keyword it cannot enforce. */
153
+ function prepare(schema: JsonRecord): PreparedSchema {
154
+ const definitions = new Map<string, StrictNode>();
155
+ const pending: Array<[StrictNode, unknown]> = [];
156
+ for (const section of ["$defs", "definitions"] as const) {
157
+ if (!Object.hasOwn(schema, section)) continue;
158
+ const defs = schema[section];
159
+ if (!isRecord(defs)) throw new Unsupported(section);
160
+ for (const [name, def] of Object.entries(defs)) {
161
+ const node: StrictNode = {};
162
+ definitions.set(`${section}/${name}`, node);
163
+ pending.push([node, def]);
164
+ }
165
+ }
166
+ const nodes: StrictNode[] = [];
167
+ let literalBudget = MAX_LITERAL_NODES;
168
+ /** Charge a literal against the schema-wide cap; a number or an oversized literal is refused. */
169
+ const admitLiteral = (literal: unknown, key: string): void => {
170
+ const count = literalNodes(literal, literalBudget);
171
+ if (count < 0 || count > literalBudget) throw new Unsupported(key);
172
+ literalBudget -= count;
173
+ };
174
+
175
+ const resolve = (ref: unknown): StrictNode => {
176
+ if (typeof ref !== "string") throw new Unsupported("$ref");
177
+ // Percent-decode the whole fragment before reading pointer segments: an encoded "/" is a
178
+ // segment separator, so `#/$defs/a%2Fb` points into definition `a`, which is outside the subset.
179
+ let decoded: string;
180
+ try {
181
+ decoded = decodeURIComponent(ref);
182
+ } catch {
183
+ throw new Unsupported("$ref");
184
+ }
185
+ const match = REF.exec(decoded);
186
+ if (!match) throw new Unsupported("$ref");
187
+ // JSON Pointer defines only `~0` and `~1`; any other `~` makes the pointer invalid.
188
+ if (/~(?![01])/.test(match[2]!)) throw new Unsupported("$ref");
189
+ const name = match[2]!.replaceAll("~1", "/").replaceAll("~0", "~");
190
+ const target = definitions.get(`${match[1]}/${name}`);
191
+ if (!target) throw new Unsupported("$ref");
192
+ return target;
193
+ };
194
+
195
+ const build = (raw: unknown, depth: number, keyword: string, node: StrictNode = {}, root = false): StrictNode => {
196
+ if (depth > MAX_SCHEMA_DEPTH || nodes.length >= MAX_SCHEMA_NODES) throw new Unsupported(keyword);
197
+ nodes.push(node);
198
+ if (raw === false) node.never = true;
199
+ if (typeof raw === "boolean") return node;
200
+ if (!isRecord(raw)) throw new Unsupported("schema");
201
+ // Drafts disagree on whether keywords beside `$ref` apply (draft-07 ignores them, 2019-09 and
202
+ // later apply them), so a `$ref` with an assertion beside it has no single meaning to enforce.
203
+ if (Object.hasOwn(raw, "$ref")
204
+ && Object.keys(raw).some(key => key !== "$ref" && key !== "$defs" && key !== "definitions" && !ANNOTATIONS.has(key))) {
205
+ throw new Unsupported("$ref");
206
+ }
207
+ for (const [key, arg] of Object.entries(raw)) {
208
+ if (ANNOTATIONS.has(key)) continue;
209
+ switch (key) {
210
+ case "$defs":
211
+ case "definitions":
212
+ if (!root) throw new Unsupported(key);
213
+ break;
214
+ // A nested `$id` starts a new resource that `#/...` references would resolve against, and
215
+ // anchors name targets this resolver does not follow; only a root `$id` (same document) is kept.
216
+ case "$id":
217
+ if (!root) throw new Unsupported(key);
218
+ break;
219
+ case "$anchor":
220
+ case "$dynamicAnchor":
221
+ throw new Unsupported(key);
222
+ case "type": {
223
+ const types = Array.isArray(arg) ? arg : [arg];
224
+ // Distinct known types only, so a type list is at most seven entries long.
225
+ if (types.length === 0 || new Set(types).size !== types.length
226
+ || !types.every(t => typeof t === "string" && KNOWN_TYPES.has(t))) throw new Unsupported(key);
227
+ node.types = types as string[];
228
+ break;
229
+ }
230
+ case "properties":
231
+ if (!isRecord(arg)) throw new Unsupported(key);
232
+ node.properties = new Map(Object.entries(arg).map(([name, child]) => [name, build(child, depth + 1, key)]));
233
+ break;
234
+ case "required":
235
+ if (!(Array.isArray(arg) && arg.every(k => typeof k === "string"))) throw new Unsupported(key);
236
+ node.required = arg as string[];
237
+ break;
238
+ case "additionalProperties":
239
+ if (arg === false) node.closed = true;
240
+ else if (arg !== true) node.additional = build(arg, depth + 1, key);
241
+ break;
242
+ case "items":
243
+ if (Array.isArray(arg)) throw new Unsupported(key);
244
+ node.items = build(arg, depth + 1, key);
245
+ break;
246
+ case "enum":
247
+ if (!Array.isArray(arg)) throw new Unsupported(key);
248
+ admitLiteral(arg, key);
249
+ node.enumValues = arg;
250
+ break;
251
+ case "const":
252
+ admitLiteral(arg, key);
253
+ node.constValue = { value: arg };
254
+ break;
255
+ case "anyOf":
256
+ if (!Array.isArray(arg) || arg.length === 0) throw new Unsupported(key);
257
+ node.anyOf = arg.map(branch => build(branch, depth + 1, key));
258
+ break;
259
+ case "$ref":
260
+ node.ref = resolve(arg);
261
+ break;
262
+ default:
263
+ if ((COUNTS as readonly string[]).includes(key)) {
264
+ if (!(Number.isSafeInteger(arg) && (arg as number) >= 0)) throw new Unsupported(key);
265
+ node[key as (typeof COUNTS)[number]] = arg as number;
266
+ } else if ((BOUNDS as readonly string[]).includes(key)) {
267
+ if (!isFaithfulNumber(arg)) throw new Unsupported(key);
268
+ node[key as (typeof BOUNDS)[number]] = kiroDecimal(String(arg));
269
+ } else throw new Unsupported(key);
270
+ }
271
+ }
272
+ return node;
273
+ };
274
+
275
+ for (const [node, def] of pending) build(def, 1, "$ref", node);
276
+ const root = build(schema, 0, "schema", {}, true);
277
+ assertNoEmptyCycle(nodes);
278
+ return { root };
279
+ }
280
+
281
+ /**
282
+ * Refuse a cycle of `$ref` and `anyOf` edges: following it consumes none of the value, so it can
283
+ * never decide anything. Recursion through `properties`, `items` or `additionalProperties` is fine.
284
+ */
285
+ function assertNoEmptyCycle(nodes: StrictNode[]): void {
286
+ const state = new Map<StrictNode, 1 | 2>();
287
+ // Each frame builds its node's edge list once, so the walk is linear in nodes plus edges.
288
+ const edges = (node: StrictNode): StrictNode[] => [...(node.ref ? [node.ref] : []), ...(node.anyOf ?? [])];
289
+ for (const start of nodes) {
290
+ if (state.has(start)) continue;
291
+ const stack: Array<[StrictNode, StrictNode[], number]> = [[start, edges(start), 0]];
292
+ state.set(start, 1);
293
+ while (stack.length > 0) {
294
+ const frame = stack[stack.length - 1]!;
295
+ const next = frame[1][frame[2]++];
296
+ if (next === undefined) {
297
+ state.set(frame[0], 2);
298
+ stack.pop();
299
+ } else if (state.get(next) === 1) throw new Unsupported("$ref");
300
+ else if (!state.has(next)) {
301
+ state.set(next, 1);
302
+ stack.push([next, edges(next), 0]);
303
+ }
304
+ }
305
+ }
306
+ }
307
+
308
+ const prepared = new WeakMap<JsonRecord, PreparedSchema>();
309
+
310
+ /** The prepared graph for `schema`, built once per schema object. */
311
+ function preparedFor(schema: JsonRecord): PreparedSchema {
312
+ let plan = prepared.get(schema);
313
+ if (!plan) {
314
+ plan = prepare(schema);
315
+ prepared.set(schema, plan);
316
+ }
317
+ return plan;
318
+ }
319
+
320
+ /** Throw the structured-output refusal when a strict schema needs a check this emulation does not have. */
321
+ export function assertKiroStrictSchemaSupported(schema: JsonRecord): void {
322
+ try {
323
+ preparedFor(schema);
324
+ } catch (error) {
325
+ if (!(error instanceof Unsupported)) throw error;
326
+ throw new Error(error.keyword === undefined
327
+ ? "Kiro does not support Responses structured output: the strict schema uses a keyword that cannot be enforced"
328
+ : `Kiro does not support Responses structured output: strict schema keyword ${JSON.stringify(error.keyword)} cannot be enforced`);
329
+ }
330
+ }
331
+
332
+ /** Whether a parsed JSON value has the given JSON Schema type. */
333
+ /** A number value's decimal, parsed once per value however many nodes inspect it. */
334
+ const decimals = new WeakMap<KiroJsonValue, KiroDecimal>();
335
+ function decimalOf(value: KiroJsonValue & { kind: "number" }): KiroDecimal {
336
+ let decimal = decimals.get(value);
337
+ if (!decimal) {
338
+ decimal = kiroDecimal(value.lexeme);
339
+ decimals.set(value, decimal);
340
+ }
341
+ return decimal;
342
+ }
343
+
344
+ function hasType(value: KiroJsonValue, type: string): boolean {
345
+ switch (type) {
346
+ case "object": return value.kind === "object";
347
+ case "array": return value.kind === "array";
348
+ case "string": return value.kind === "string";
349
+ case "number": return value.kind === "number";
350
+ case "integer": return value.kind === "number" && isKiroDecimalInteger(decimalOf(value));
351
+ case "boolean": return value.kind === "boolean";
352
+ case "null": return value.kind === "null";
353
+ default: return false;
354
+ }
355
+ }
356
+
357
+ /** Whether the model's value equals a schema literal, comparing numbers exactly by their decimal value. */
358
+ function literalEquals(literal: unknown, value: KiroJsonValue, charge: (cost: number) => void): boolean {
359
+ // Every comparison step is charged, so one large literal referenced from many places cannot
360
+ // multiply work past the validation budget. Long strings cost in proportion to their length.
361
+ charge(1);
362
+ switch (value.kind) {
363
+ case "null": return literal === null;
364
+ case "boolean": return literal === value.value;
365
+ case "string":
366
+ charge(Math.floor(value.value.length / 256));
367
+ return literal === value.value;
368
+ case "number":
369
+ return typeof literal === "number"
370
+ && compareKiroDecimal(kiroDecimal(String(literal)), kiroDecimal(value.lexeme)) === 0;
371
+ case "array":
372
+ return Array.isArray(literal) && literal.length === value.items.length
373
+ && value.items.every((item, i) => literalEquals(literal[i], item, charge));
374
+ case "object": {
375
+ if (!isRecord(literal)) return false;
376
+ const keys = Object.keys(literal);
377
+ charge(keys.length);
378
+ return keys.length === value.entries.size
379
+ && keys.every(key => value.entries.has(key) && literalEquals(literal[key], value.entries.get(key)!, charge));
380
+ }
381
+ }
382
+ }
383
+
384
+ /** Code points in a string value, counted once per value however many nodes check its length. */
385
+ const codePointLengths = new WeakMap<KiroJsonValue, number>();
386
+ function codePointLength(value: KiroJsonValue & { kind: "string" }): number {
387
+ let length = codePointLengths.get(value);
388
+ if (length === undefined) {
389
+ length = 0;
390
+ for (const _ of value.value) length += 1;
391
+ codePointLengths.set(value, length);
392
+ }
393
+ return length;
394
+ }
395
+
396
+ class OverBudget extends Error {
397
+ constructor(readonly problem: string) {
398
+ super(problem);
399
+ }
400
+ }
401
+
402
+ /** The outcome of one strict validation, with the schema-node visits it spent. */
403
+ export interface KiroStrictValidation {
404
+ problem?: string;
405
+ visits: number;
406
+ budget: number;
407
+ }
408
+
409
+ /**
410
+ * Validate `value` (holding `valueNodes` JSON values) against the strict `schema`.
411
+ *
412
+ * Problems are fixed categories. They never quote a property name, a path or any other text from
413
+ * the model's answer or the caller's schema, because they reach the caller and diagnostics.
414
+ */
415
+ export function validateKiroStrictValue(schema: JsonRecord, value: KiroJsonValue, valueNodes: number): KiroStrictValidation {
416
+ const { root } = preparedFor(schema);
417
+ const budget = KIRO_STRICT_BASE_VISITS + KIRO_STRICT_VISITS_PER_VALUE_NODE * valueNodes;
418
+ let visits = 0;
419
+ const spend = (cost = 1): void => {
420
+ visits += cost;
421
+ if (visits > budget) throw new OverBudget("validating the value needs more work than strict validation allows");
422
+ };
423
+
424
+ const check = (node: StrictNode, value: KiroJsonValue, depth: number): string | undefined => {
425
+ spend();
426
+ if (depth > KIRO_STRICT_MAX_DEPTH) throw new OverBudget("the value nests deeper than strict validation allows");
427
+ if (node.never) return "a value is present where none is allowed";
428
+ if (node.ref) {
429
+ const problem = check(node.ref, value, depth + 1);
430
+ if (problem) return problem;
431
+ }
432
+ if (node.types) {
433
+ // An integer check walks the number's digits; long lexemes are charged in proportion.
434
+ if (value.kind === "number" && node.types.includes("integer")) spend(Math.floor(value.lexeme.length / 256));
435
+ if (!node.types.some(type => hasType(value, type))) return `expected a JSON ${node.types.join(" or ")}`;
436
+ }
437
+ if (node.enumValues && !node.enumValues.some(option => literalEquals(option, value, spend))) return "expected one of the enum values";
438
+ if (node.constValue && !literalEquals(node.constValue.value, value, spend)) return "expected the const value";
439
+ if (node.anyOf && !node.anyOf.some(branch => check(branch, value, depth + 1) === undefined)) {
440
+ return "matched no anyOf branch";
441
+ }
442
+ if (value.kind === "string") {
443
+ if (node.minLength !== undefined || node.maxLength !== undefined) {
444
+ // Counting code points walks the string, so repeated checks of one long string are charged,
445
+ // and only a node with a length constraint counts at all.
446
+ spend(Math.floor(value.value.length / 256));
447
+ const length = codePointLength(value);
448
+ if (node.minLength !== undefined && length < node.minLength) return "a string is shorter than minLength";
449
+ if (node.maxLength !== undefined && length > node.maxLength) return "a string is longer than maxLength";
450
+ }
451
+ }
452
+ if (value.kind === "number" && (node.minimum || node.maximum || node.exclusiveMinimum || node.exclusiveMaximum)) {
453
+ // Bound comparisons walk the digits too: parsed once per value, charged on every use.
454
+ spend(Math.floor(value.lexeme.length / 256));
455
+ const number = decimalOf(value);
456
+ if (node.minimum && compareKiroDecimal(number, node.minimum) < 0) return "a number is below minimum";
457
+ if (node.maximum && compareKiroDecimal(number, node.maximum) > 0) return "a number is above maximum";
458
+ if (node.exclusiveMinimum && compareKiroDecimal(number, node.exclusiveMinimum) <= 0) return "a number is not above exclusiveMinimum";
459
+ if (node.exclusiveMaximum && compareKiroDecimal(number, node.exclusiveMaximum) >= 0) return "a number is not below exclusiveMaximum";
460
+ }
461
+ if (value.kind === "array") {
462
+ if (node.minItems !== undefined && value.items.length < node.minItems) return "an array has fewer than minItems";
463
+ if (node.maxItems !== undefined && value.items.length > node.maxItems) return "an array has more than maxItems";
464
+ if (node.items) {
465
+ for (const item of value.items) {
466
+ const problem = check(node.items, item, depth + 1);
467
+ if (problem) return problem;
468
+ }
469
+ }
470
+ }
471
+ if (value.kind === "object") {
472
+ // Each pass over `required` and over the entries is charged, so a failing anyOf that re-checks
473
+ // one large object or one long `required` list stays within the budget.
474
+ spend(node.required?.length ?? 0);
475
+ if (node.required?.some(key => !value.entries.has(key))) return "an object is missing a required property";
476
+ spend(value.entries.size);
477
+ for (const [key, child] of value.entries) {
478
+ const declared = node.properties?.get(key);
479
+ if (!declared && node.closed) return "an object has a property the schema does not allow";
480
+ const childNode = declared ?? node.additional;
481
+ if (!childNode) continue;
482
+ const problem = check(childNode, child, depth + 1);
483
+ if (problem) return problem;
484
+ }
485
+ }
486
+ return undefined;
487
+ };
488
+
489
+ try {
490
+ const problem = check(root, value, 0);
491
+ return { ...(problem !== undefined ? { problem } : {}), visits, budget };
492
+ } catch (error) {
493
+ if (!(error instanceof OverBudget)) throw error;
494
+ return { problem: error.problem, visits, budget };
495
+ }
496
+ }
497
+
498
+ /** The first way `value` breaks the strict `schema`, or undefined when it conforms. */
499
+ export function kiroStrictSchemaProblem(schema: JsonRecord, value: KiroJsonValue, valueNodes: number): string | undefined {
500
+ return validateKiroStrictValue(schema, value, valueNodes).problem;
501
+ }
502
+
503
+ /** Keys whose values are JSON data, never schemas, so a `$ref`-named key inside them is left alone. */
504
+ const LITERAL_KEYS = new Set(["const", "enum", "default", "examples"]);
505
+ /** Keys whose values map names to schemas; a name such as "const" is not a keyword there. */
506
+ const SCHEMA_MAPS = new Set(["properties", "$defs", "definitions"]);
507
+
508
+ /**
509
+ * Point every local `$ref` at the original root after the schema moves under `properties.value`.
510
+ *
511
+ * `#` and root-relative pointers such as `#/items` now resolve against the wrapper, so they gain
512
+ * the `/properties/value` prefix. `#/$defs/...` and `#/definitions/...` keep their target because
513
+ * the wrapper hoists the definitions to its own root.
514
+ */
515
+ export function rewriteKiroWrappedRefs(schema: unknown, root = true): unknown {
516
+ if (Array.isArray(schema)) return schema.map(item => rewriteKiroWrappedRefs(item, false));
517
+ if (!isRecord(schema)) return schema;
518
+ // A nested `$id` starts its own resource: references inside it are relative to that resource,
519
+ // which moves along with it, so they stay as written.
520
+ if (!root && typeof schema.$id === "string") return schema;
521
+ // fromEntries defines own properties, so a key such as "__proto__" stays a key.
522
+ return Object.fromEntries(Object.entries(schema).map(([key, child]): [string, unknown] => {
523
+ if (SCHEMA_MAPS.has(key) && isRecord(child)) {
524
+ return [key, Object.fromEntries(Object.entries(child).map(([name, sub]) => [name, rewriteKiroWrappedRefs(sub, false)]))];
525
+ }
526
+ if (LITERAL_KEYS.has(key)) return [key, child];
527
+ if (key === "$ref" && typeof child === "string") return [key, movedRef(child)];
528
+ return [key, rewriteKiroWrappedRefs(child, false)];
529
+ }));
530
+ }
531
+
532
+ /** One reference rewritten for the wrapped document. */
533
+ function movedRef(ref: string): string {
534
+ // Judge the decoded pointer: `#%2Fitems` is root-relative and `#/%24defs/item` names the hoisted
535
+ // `$defs`, exactly like their unencoded forms. Prefixing the raw remainder keeps its encoding.
536
+ let decoded = ref;
537
+ try { decoded = decodeURIComponent(ref); } catch { /* an undecodable pointer is judged as written */ }
538
+ if (decoded !== "#" && !decoded.startsWith("#/")) return ref;
539
+ // The definition containers themselves, and anything under them, stay at the wrapper root.
540
+ if (["#/$defs", "#/definitions"].some(container => decoded === container || decoded.startsWith(`${container}/`))) return ref;
541
+ return `#/properties/value${ref.slice(1)}`;
542
+ }