@iowarp/clio-coder 0.3.3 → 0.3.6

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 (370) hide show
  1. package/CHANGELOG.md +74 -0
  2. package/CONTRIBUTING.md +7 -7
  3. package/README.md +3 -3
  4. package/dist/{acp-P2AQILE2.js → acp-2BEHC4DL.js} +9 -8
  5. package/dist/{agents-72W3BI7I.js → agents-LNNFTM53.js} +29 -24
  6. package/dist/assets/codewiki.json +1 -1
  7. package/dist/{auth-5TWEIYDN.js → auth-KXXFI2VS.js} +14 -10
  8. package/dist/{chunk-GGXXDWE4.js → chunk-22NAGB7X.js} +2 -2
  9. package/dist/{chunk-YCWGATWI.js → chunk-24I7BN55.js} +2 -2
  10. package/dist/{chunk-EKMEHE4H.js → chunk-33YXPOE3.js} +2 -3
  11. package/dist/chunk-3BPUFZDL.js +37 -0
  12. package/dist/chunk-43AOLP7E.js +375 -0
  13. package/dist/{chunk-ZDOOVTXZ.js → chunk-4OC57DA6.js} +27 -4
  14. package/dist/{chunk-6SGHMWE3.js → chunk-5JGRAMKL.js} +5 -5
  15. package/dist/{chunk-V6RTAOC2.js → chunk-6US73PDB.js} +572 -51
  16. package/dist/{chunk-5UFT4SUX.js → chunk-6XXKFVSN.js} +3 -3
  17. package/dist/{chunk-A3CYT5EX.js → chunk-AD2SYQYC.js} +55 -2
  18. package/dist/chunk-AOCYTWAV.js +449 -0
  19. package/dist/chunk-CFGTUFWB.js +67 -0
  20. package/dist/chunk-CJUB2JJ2.js +1478 -0
  21. package/dist/{chunk-FNTMWMX5.js → chunk-CKXWIANG.js} +14 -12
  22. package/dist/{chunk-PIWWS5BL.js → chunk-CYQKWTG3.js} +63 -78
  23. package/dist/{chunk-LZSJBIVT.js → chunk-DJVECN66.js} +271 -762
  24. package/dist/{chunk-CBCAPZAA.js → chunk-E25LMLRW.js} +2 -2
  25. package/dist/{chunk-ZWMF7253.js → chunk-E2ER4LJF.js} +304 -9
  26. package/dist/{chunk-STBPMHSX.js → chunk-EKY57CSP.js} +51 -84
  27. package/dist/{chunk-DUYJ5IO6.js → chunk-EYPA3EGJ.js} +12 -4
  28. package/dist/{chunk-M6SHUN7Q.js → chunk-FO5ZOVUY.js} +2 -2
  29. package/dist/chunk-FYYLNIL5.js +313 -0
  30. package/dist/{chunk-OQ33BKR3.js → chunk-G7MUEIGA.js} +3 -60
  31. package/dist/chunk-GEYXPTRF.js +613 -0
  32. package/dist/chunk-GOXNB3AO.js +261 -0
  33. package/dist/{chunk-G4BMMOKF.js → chunk-HVDIIIQW.js} +2 -2
  34. package/dist/chunk-HWUFFB6L.js +83 -0
  35. package/dist/{chunk-4XUGQOHA.js → chunk-K7T3E2SR.js} +15 -8
  36. package/dist/chunk-K7VKOLQQ.js +15 -0
  37. package/dist/{chunk-UFIIWP2H.js → chunk-KHSFENX2.js} +8 -8
  38. package/dist/{chunk-BMEMKKIT.js → chunk-KOHPCX4K.js} +2 -2
  39. package/dist/chunk-LCGCVYZ4.js +57 -0
  40. package/dist/chunk-LL4KHSZI.js +22 -0
  41. package/dist/{chunk-PAJK6MAQ.js → chunk-LYF7OHWH.js} +42 -15
  42. package/dist/{chunk-POHLU5DW.js → chunk-M6L6IDJG.js} +3 -3
  43. package/dist/{chunk-5UUP6MWO.js → chunk-MV3K5QF2.js} +5 -436
  44. package/dist/{chunk-X4RCMKVQ.js → chunk-NDINPTJ4.js} +2 -2
  45. package/dist/{chunk-TZK7PACC.js → chunk-NILBFAPG.js} +14 -8
  46. package/dist/chunk-ODFEOB4F.js +1082 -0
  47. package/dist/{chunk-AGYYIBLL.js → chunk-OH3TOQTB.js} +6 -2
  48. package/dist/chunk-OZNBF4L3.js +23 -0
  49. package/dist/{chunk-DSELYM6W.js → chunk-PBTHKCPN.js} +30 -10
  50. package/dist/{verify-375KUB3Y.js → chunk-PCZJO5TI.js} +127 -42
  51. package/dist/{chunk-ED4KHGC3.js → chunk-PPAMZ32Z.js} +9 -2
  52. package/dist/{chunk-SRDMMSEP.js → chunk-QM3F2GKX.js} +1063 -1645
  53. package/dist/{chunk-X6IAEBZR.js → chunk-QNQHSOLF.js} +7 -7
  54. package/dist/{chunk-OC7FIQPC.js → chunk-R46L2BIR.js} +10 -7
  55. package/dist/{chunk-2TLUCQVG.js → chunk-RD5U66HV.js} +3 -3
  56. package/dist/{chunk-6N5PTWMY.js → chunk-RY3LY4J5.js} +50 -13
  57. package/dist/{chunk-OKGUZO2U.js → chunk-SPULKLCF.js} +4 -3
  58. package/dist/{chunk-OOJYHWRB.js → chunk-TSHXZTOQ.js} +6 -5
  59. package/dist/chunk-TZSKNMZG.js +434 -0
  60. package/dist/{chunk-7MNJORFF.js → chunk-UL3WSD3F.js} +6 -1
  61. package/dist/{chunk-VJWL6YS5.js → chunk-UUVG37B4.js} +2 -2
  62. package/dist/{chunk-COU2UHX6.js → chunk-VEZEGCGW.js} +170 -2
  63. package/dist/chunk-W6GROXXM.js +69 -0
  64. package/dist/{chunk-OAO4GE4M.js → chunk-WHGPSPT5.js} +2 -2
  65. package/dist/chunk-WHJYKASB.js +677 -0
  66. package/dist/{chunk-ORBHGJC5.js → chunk-WR67VIZY.js} +3 -3
  67. package/dist/{chunk-YHZX5GEU.js → chunk-XAKHZX5N.js} +2 -2
  68. package/dist/{chunk-TZTZS7QK.js → chunk-XE2VEJHX.js} +5 -3
  69. package/dist/{chunk-LM5TQCJZ.js → chunk-XF5N4U5A.js} +8 -7
  70. package/dist/{chunk-LWLEKMDQ.js → chunk-XXQNGV4M.js} +1073 -552
  71. package/dist/{chunk-KZWTDYJF.js → chunk-XYDYPRZI.js} +7 -7
  72. package/dist/chunk-ZGVHUX3M.js +66 -0
  73. package/dist/{chunk-LW6DSM3M.js → chunk-ZRGEBJ4T.js} +1192 -1119
  74. package/dist/{chunk-2DJ2KNFG.js → chunk-ZXF4XRKW.js} +202 -40
  75. package/dist/chunk-ZZMN5OM4.js +122 -0
  76. package/dist/cli/index.js +34 -30
  77. package/dist/{clio-JOU4FXVA.js → clio-M2KGYUFZ.js} +7 -6
  78. package/dist/{code-nav-7AX6FYE6.js → code-nav-GQNL7XA6.js} +8 -6
  79. package/dist/codewiki/build-worker.js +4 -4
  80. package/dist/{components-KELWS457.js → components-5TTYYX6G.js} +3 -3
  81. package/dist/{config-XCDVKR23.js → config-XUUYQIWO.js} +47 -35
  82. package/dist/{configure-4GAP54ZW.js → configure-IHJ7YOMV.js} +18 -15
  83. package/dist/{context-77FM5DV5.js → context-74JLXAWD.js} +18 -10
  84. package/dist/{context-4UOGGLQ5.js → context-75MIWW3U.js} +41 -29
  85. package/dist/{context-5VKGUVJJ.js → context-ZQ7SIFJV.js} +85 -9
  86. package/dist/{context-clear-XXJRLCJJ.js → context-clear-GYKWNUML.js} +41 -29
  87. package/dist/{context-index-BZ4UYMTC.js → context-index-SSR5ECNE.js} +3 -3
  88. package/dist/context-working-set-UX5KEP4J.js +1553 -0
  89. package/dist/{dispatch-runner-QPRDDBDX.js → dispatch-runner-GIJBHNFL.js} +47 -32
  90. package/dist/{docs-2C2LTVT2.js → docs-6FZSCG5B.js} +3 -3
  91. package/dist/{doctor-HR46URBJ.js → doctor-SVJ5BZCW.js} +12 -12
  92. package/dist/{eval-XSSNATB4.js → eval-CG6LLBLD.js} +54 -238
  93. package/dist/{evidence-6HG2PY2B.js → evidence-ZYFIEN42.js} +57 -28
  94. package/dist/{evolve-K7YU3NCY.js → evolve-QGEXEMDW.js} +36 -25
  95. package/dist/{extensions-QVDOHDGJ.js → extensions-ADGNCJJD.js} +3 -3
  96. package/dist/{fleet-VY3HHKN6.js → fleet-S5R4ZOQY.js} +73 -44
  97. package/dist/{fleet-preflight-DDN536IT.js → fleet-preflight-BHSNPBMH.js} +3 -3
  98. package/dist/{init-JYGXI3FK.js → init-5DRU55YR.js} +49 -37
  99. package/dist/memory-7YKKR6UC.js +467 -0
  100. package/dist/{models-I5QWSEOM.js → models-ZPOLRU2C.js} +24 -21
  101. package/dist/{monitor-GE4ID3IA.js → monitor-US5F5YGZ.js} +73 -46
  102. package/dist/{orchestrator-EM5MC3HM.js → orchestrator-E2AL4T5N.js} +1624 -1007
  103. package/dist/{paths-UXLN5YYZ.js → paths-E7KYAQWE.js} +3 -3
  104. package/dist/{reset-L2FQEE3E.js → reset-KZ652EK6.js} +6 -5
  105. package/dist/{run-ZU3QMZPZ.js → run-SRNBKDWD.js} +76 -54
  106. package/dist/{share-S5BZQC5I.js → share-CGZE33UP.js} +7 -6
  107. package/dist/{skills-X5VXCRNQ.js → skills-S2X4DLY5.js} +4 -4
  108. package/dist/{skills-eval-WKIHWTHR.js → skills-eval-W2GGIC4R.js} +40 -29
  109. package/dist/{targets-SNCPI2NR.js → targets-54SWINWB.js} +28 -23
  110. package/dist/{terminal-lease-BNAHVHBS.js → terminal-lease-SAIF2OGY.js} +6 -4
  111. package/dist/{uninstall-FZCQCDKC.js → uninstall-BVLWXKBT.js} +3 -3
  112. package/dist/{upgrade-JQHHPQ4K.js → upgrade-JKAR27XC.js} +20 -19
  113. package/dist/{usage-OR4O5SMZ.js → usage-MSAWCLX4.js} +79 -36
  114. package/dist/verifiers-NCBTHHN2.js +1220 -0
  115. package/dist/verify-X5HDROLA.js +25 -0
  116. package/dist/{wiki-generate-UEXP2ARI.js → wiki-generate-GUSOQ6ZP.js} +50 -37
  117. package/dist/worker/entry.js +90 -70
  118. package/dist/{workspace-G4ZWUIPR.js → workspace-ZJ6BFM3Q.js} +4 -4
  119. package/docs/README.md +8 -7
  120. package/docs/acp.md +1 -1
  121. package/docs/alcf-provider.md +1 -1
  122. package/docs/architecture.md +2 -2
  123. package/docs/artifact-placement.md +1 -2
  124. package/docs/artifact-versions.md +1 -1
  125. package/docs/built-in-agents.md +1 -1
  126. package/docs/capacity-and-scheduling.md +1 -1
  127. package/docs/commands-and-modes.md +60 -26
  128. package/docs/config-knobs-audit.md +1 -2
  129. package/docs/configuration-and-targets.md +26 -1
  130. package/docs/context-engine.md +67 -13
  131. package/docs/context-working-set.md +194 -0
  132. package/docs/development-pipeline.md +1 -1
  133. package/docs/documentation-coverage.md +6 -6
  134. package/docs/documentation-guide.md +7 -6
  135. package/docs/environment-variables.md +2 -1
  136. package/docs/eval-runner.md +1 -1
  137. package/docs/evals-internal.md +4 -32
  138. package/docs/evidence-and-memory.md +139 -7
  139. package/docs/evolution.md +1 -1
  140. package/docs/exit-codes-and-output.md +1 -1
  141. package/docs/extensions-and-sharing.md +2 -2
  142. package/docs/fleet-dispatch.md +49 -8
  143. package/docs/glossary.md +21 -1
  144. package/docs/installation-and-lifecycle.md +2 -2
  145. package/docs/middleware-and-components.md +19 -2
  146. package/docs/model-catalog.md +7 -9
  147. package/docs/observability.md +4 -4
  148. package/docs/proactive-memory.md +26 -16
  149. package/docs/prompt-envelope-and-tools.md +7 -5
  150. package/docs/provider-adapter-cookbook.md +1 -1
  151. package/docs/release-cut-checklist.md +43 -40
  152. package/docs/safety-model.md +49 -8
  153. package/docs/scientific-validation.md +21 -3
  154. package/docs/session-lifecycle.md +3 -3
  155. package/docs/skills-marketplace.md +1 -1
  156. package/docs/tool-usage.md +79 -12
  157. package/docs/trace-store.md +1 -1
  158. package/docs/troubleshooting.md +1 -1
  159. package/docs/tui-design.md +38 -4
  160. package/docs/worker-dispatch-mechanics.md +11 -1
  161. package/package.json +13 -13
  162. package/skills/meta/clio-test/SKILL.md +20 -17
  163. package/skills/meta/clio-test/evals.md +3 -3
  164. package/skills/meta/clio-test/references/harness.md +35 -6
  165. package/skills/meta/clio-test/references/test-map.md +20 -10
  166. package/skills/registry.yaml +2 -2
  167. package/skills/skill-marketplace.json +1 -1
  168. package/src/cli/agents.ts +2 -3
  169. package/src/cli/argv.ts +14 -1
  170. package/src/cli/context-working-set.ts +513 -0
  171. package/src/cli/context.ts +8 -0
  172. package/src/cli/evidence.ts +20 -2
  173. package/src/cli/fleet.ts +15 -0
  174. package/src/cli/index.ts +5 -1
  175. package/src/cli/memory.ts +272 -10
  176. package/src/cli/modes/json-stream.ts +2 -2
  177. package/src/cli/modes/print.ts +12 -1
  178. package/src/cli/run.ts +22 -2
  179. package/src/cli/targets.ts +12 -3
  180. package/src/cli/usage.ts +55 -7
  181. package/src/cli/verifiers.ts +325 -0
  182. package/src/core/bash-exec.ts +39 -14
  183. package/src/core/bus-events.ts +22 -4
  184. package/src/core/config.ts +54 -0
  185. package/src/core/defaults.ts +50 -3
  186. package/src/core/response-model-id.ts +134 -0
  187. package/src/core/toml.ts +62 -0
  188. package/src/core/verification-scripts.ts +6 -0
  189. package/src/core/workspace-files.ts +0 -1
  190. package/src/domains/agents/builtins/architect.md +1 -1
  191. package/src/domains/agents/builtins/verifier.md +3 -0
  192. package/src/domains/agents/catalog.ts +5 -4
  193. package/src/domains/agents/recipe.ts +54 -14
  194. package/src/domains/agents/result-contract.ts +7 -4
  195. package/src/domains/config/classify.ts +1 -0
  196. package/src/domains/context/bootstrap.ts +36 -27
  197. package/src/domains/context/project-metadata.ts +19 -63
  198. package/src/domains/context/prompt-context.ts +8 -0
  199. package/src/domains/context/working-set/contract.ts +161 -0
  200. package/src/domains/context/working-set/defaults.ts +28 -0
  201. package/src/domains/context/working-set/engine.ts +203 -0
  202. package/src/domains/context/working-set/fold.ts +62 -0
  203. package/src/domains/context/working-set/horizon.ts +38 -0
  204. package/src/domains/context/working-set/marker.ts +103 -0
  205. package/src/domains/context/working-set/path-index.ts +436 -0
  206. package/src/domains/context/working-set/payload.ts +152 -0
  207. package/src/domains/context/working-set/policies/age-horizon.ts +55 -0
  208. package/src/domains/context/working-set/policies/index.ts +20 -0
  209. package/src/domains/context/working-set/policies/structural.ts +160 -0
  210. package/src/domains/context/working-set/project.ts +132 -0
  211. package/src/domains/context/working-set/protect.ts +109 -0
  212. package/src/domains/context/working-set/recall.ts +177 -0
  213. package/src/domains/context/working-set/replay/controls.ts +112 -0
  214. package/src/domains/context/working-set/replay/load-clio.ts +199 -0
  215. package/src/domains/context/working-set/replay/metrics.ts +185 -0
  216. package/src/domains/context/working-set/replay/reference-graph.ts +79 -0
  217. package/src/domains/context/working-set/replay/report.ts +139 -0
  218. package/src/domains/context/working-set/replay/runner.ts +325 -0
  219. package/src/domains/context/working-set/replay/synthetic.ts +422 -0
  220. package/src/domains/context/working-set/replay/trace.ts +21 -0
  221. package/src/domains/context/working-set/visible.ts +54 -0
  222. package/src/domains/dispatch/budget-envelope.ts +396 -0
  223. package/src/domains/dispatch/contract.ts +2 -0
  224. package/src/domains/dispatch/extension.ts +81 -27
  225. package/src/domains/dispatch/orphan-recovery.ts +1 -0
  226. package/src/domains/dispatch/receipt-integrity.ts +4 -0
  227. package/src/domains/dispatch/state.ts +1 -0
  228. package/src/domains/dispatch/types.ts +10 -3
  229. package/src/domains/dispatch/validation.ts +14 -0
  230. package/src/domains/dispatch/worker-spawn.ts +14 -3
  231. package/src/domains/eval/metrics/evidence.ts +0 -116
  232. package/src/domains/eval/metrics/invariants.ts +1 -1
  233. package/src/domains/eval/runners/clio-run.ts +1 -10
  234. package/src/domains/eval/runners/external-command.ts +2 -29
  235. package/src/domains/eval/schema/suite.ts +0 -7
  236. package/src/domains/eval/suites/run.ts +1 -7
  237. package/src/domains/evidence/build.ts +112 -45
  238. package/src/domains/evidence/eval.ts +24 -7
  239. package/src/domains/evidence/index.ts +53 -0
  240. package/src/domains/evidence/ordering.ts +12 -0
  241. package/src/domains/evidence/run-trust.ts +221 -0
  242. package/src/domains/evidence/store.ts +46 -6
  243. package/src/domains/evidence/trust-status.ts +854 -0
  244. package/src/domains/evidence/types.ts +26 -0
  245. package/src/domains/memory/index.ts +22 -0
  246. package/src/domains/memory/operations.ts +58 -1
  247. package/src/domains/memory/promotion.ts +281 -0
  248. package/src/domains/memory/prompt-section.ts +25 -5
  249. package/src/domains/memory/proposal.ts +51 -7
  250. package/src/domains/memory/task-bank.ts +3 -2
  251. package/src/domains/memory/task-memory-handoff.ts +181 -24
  252. package/src/domains/memory/task-memory-policy.ts +3 -1
  253. package/src/domains/memory/types.ts +37 -0
  254. package/src/domains/memory/validate.ts +178 -0
  255. package/src/domains/middleware/memory-intervention.ts +38 -25
  256. package/src/domains/middleware/runtime.ts +6 -0
  257. package/src/domains/middleware/skills-reminder.ts +19 -4
  258. package/src/domains/middleware/stalled-turn.ts +208 -5
  259. package/src/domains/middleware/types.ts +10 -0
  260. package/src/domains/observability/contract.ts +6 -1
  261. package/src/domains/observability/cost.ts +20 -4
  262. package/src/domains/observability/extension.ts +2 -2
  263. package/src/domains/providers/index.ts +3 -0
  264. package/src/domains/providers/model-discovery.ts +9 -0
  265. package/src/domains/providers/runtime-resolution.ts +38 -1
  266. package/src/domains/providers/runtimes/common/probe-helpers.ts +97 -16
  267. package/src/domains/providers/types/context-window-slots.ts +18 -0
  268. package/src/domains/providers/types/runtime-descriptor.ts +3 -1
  269. package/src/domains/safety/autonomy.ts +1 -1
  270. package/src/domains/safety/call-target.ts +211 -14
  271. package/src/domains/safety/decision-presentation.ts +268 -0
  272. package/src/domains/safety/default-path-policy.ts +8 -0
  273. package/src/domains/safety/finish-contract.ts +4 -3
  274. package/src/domains/safety/policy-engine.ts +48 -6
  275. package/src/domains/safety/redaction.ts +73 -0
  276. package/src/domains/session/compaction/compact.ts +23 -1
  277. package/src/domains/session/compaction/cut-point.ts +2 -0
  278. package/src/domains/session/compaction/tokens.ts +16 -1
  279. package/src/domains/session/context-ledger.ts +12 -1
  280. package/src/domains/session/decision-board.ts +4 -0
  281. package/src/domains/session/entries.ts +110 -1
  282. package/src/domains/session/history.ts +68 -19
  283. package/src/domains/session/manager.ts +9 -2
  284. package/src/domains/session/migrations/index.ts +22 -3
  285. package/src/domains/session/usage.ts +24 -7
  286. package/src/engine/acp/event-mapper.ts +7 -0
  287. package/src/engine/acp/server.ts +32 -2
  288. package/src/engine/agent.ts +18 -1
  289. package/src/engine/apis/lmstudio.ts +25 -4
  290. package/src/engine/apis/openai-completions.ts +147 -22
  291. package/src/engine/claude/sdk-runtime.ts +8 -2
  292. package/src/engine/claude/tool-safety.ts +13 -0
  293. package/src/engine/loop-guard.ts +27 -3
  294. package/src/engine/session.ts +9 -3
  295. package/src/engine/worker-events.ts +4 -3
  296. package/src/engine/worker-runtime.ts +59 -54
  297. package/src/entry/orchestrator.ts +34 -5
  298. package/src/interactive/chat-loop-messages.ts +40 -6
  299. package/src/interactive/chat-loop.ts +13 -0
  300. package/src/interactive/chat-panel.ts +17 -1
  301. package/src/interactive/chat-renderer.ts +49 -24
  302. package/src/interactive/clio-editor.ts +44 -7
  303. package/src/interactive/context-meter.ts +10 -0
  304. package/src/interactive/context-overlay.ts +120 -7
  305. package/src/interactive/context-recall-command.ts +110 -0
  306. package/src/interactive/cost-overlay.ts +39 -8
  307. package/src/interactive/dispatch-board.ts +212 -35
  308. package/src/interactive/footer/widgets.ts +13 -0
  309. package/src/interactive/interactive-application.ts +6 -1
  310. package/src/interactive/interactive-input-runtime.ts +11 -1
  311. package/src/interactive/interactive-presentation.ts +11 -1
  312. package/src/interactive/interactive-slash-runtime.ts +37 -1
  313. package/src/interactive/memory-overlay.ts +89 -4
  314. package/src/interactive/model-session-replay.ts +21 -0
  315. package/src/interactive/overlay-ask-user-lifecycle.ts +1 -1
  316. package/src/interactive/overlay-frame.ts +5 -2
  317. package/src/interactive/overlay-general-openers.ts +46 -1
  318. package/src/interactive/overlay-key-routing.ts +41 -1
  319. package/src/interactive/overlay-lifecycle.ts +11 -4
  320. package/src/interactive/overlay-permission-lifecycle.ts +23 -8
  321. package/src/interactive/overlay-session-lifecycle.ts +8 -4
  322. package/src/interactive/overlay-transitions.ts +11 -0
  323. package/src/interactive/overlays/ask-user.ts +74 -30
  324. package/src/interactive/overlays/decisions.ts +3 -1
  325. package/src/interactive/permission-hint.ts +35 -0
  326. package/src/interactive/permission-overlay.ts +95 -45
  327. package/src/interactive/renderers/tool-execution.ts +37 -51
  328. package/src/interactive/session-last-turn.ts +8 -1
  329. package/src/interactive/session-transcript.ts +2 -2
  330. package/src/interactive/session-usage-reseed.ts +36 -10
  331. package/src/interactive/slash-commands.ts +31 -4
  332. package/src/interactive/status/summary.ts +5 -0
  333. package/src/interactive/status/types.ts +5 -0
  334. package/src/interactive/terminal-lease.ts +1 -0
  335. package/src/interactive/turn-context.ts +333 -110
  336. package/src/interactive/turn-middleware.ts +7 -6
  337. package/src/interactive/turn-runtime.ts +37 -8
  338. package/src/interactive/turn-state.ts +3 -0
  339. package/src/interactive/worker-progress.ts +440 -0
  340. package/src/interactive/worker-stream.ts +51 -110
  341. package/src/tools/agent-tools.ts +39 -7
  342. package/src/tools/ask-user.ts +21 -1
  343. package/src/tools/bash.ts +144 -82
  344. package/src/tools/builtin-tool-catalog.ts +11 -5
  345. package/src/tools/context/index.ts +107 -5
  346. package/src/tools/context/surface.ts +3 -2
  347. package/src/tools/core-bootstrap.ts +21 -0
  348. package/src/tools/dispatch-arguments.ts +8 -0
  349. package/src/tools/dispatch-event-text.ts +19 -0
  350. package/src/tools/dispatch-runner.ts +9 -7
  351. package/src/tools/dispatch.ts +24 -1
  352. package/src/tools/monitor.ts +43 -20
  353. package/src/tools/registry.ts +72 -10
  354. package/src/tools/result-disposition.ts +706 -0
  355. package/src/tools/result-shaping.ts +321 -20
  356. package/src/tools/safe-exec.ts +2 -0
  357. package/src/tools/verify/authoring.ts +1120 -0
  358. package/src/tools/verify/catalog.ts +346 -0
  359. package/src/tools/verify/index.ts +13 -3
  360. package/src/tools/verify/scripts.ts +135 -37
  361. package/src/tools/verify/surface.ts +9 -5
  362. package/src/tools/worker-evidence.ts +54 -12
  363. package/src/worker/spec-contract.ts +43 -3
  364. package/dist/chunk-J7CWMCQD.js +0 -255
  365. package/dist/chunk-T6YILFSB.js +0 -80
  366. package/dist/chunk-VAKQQHWR.js +0 -434
  367. package/dist/chunk-VPAYEGVX.js +0 -184
  368. package/dist/chunk-XBXAASKX.js +0 -18
  369. package/dist/memory-WFZMGYHX.js +0 -236
  370. package/src/domains/eval/metrics/chaos-stream.ts +0 -93
@@ -0,0 +1,613 @@
1
+ import { createRequire as __clioCreateRequire } from "node:module"; const require = __clioCreateRequire(import.meta.url);
2
+ import {
3
+ cwdHash,
4
+ sessionPaths
5
+ } from "./chunk-FO5ZOVUY.js";
6
+ import {
7
+ init_esm_shims
8
+ } from "./chunk-3R73A4XB.js";
9
+
10
+ // src/domains/session/context-accounting.ts
11
+ init_esm_shims();
12
+ import { appendFileSync, existsSync, readFileSync } from "node:fs";
13
+ import { dirname, join } from "node:path";
14
+
15
+ // src/domains/session/compaction/auto.ts
16
+ init_esm_shims();
17
+ var DEFAULT_COMPACTION_THRESHOLD = 0.8;
18
+ function shouldCompact(contextTokens, threshold, contextWindow) {
19
+ const base = {
20
+ shouldCompact: false,
21
+ pressure: null,
22
+ contextTokens,
23
+ contextWindow,
24
+ threshold
25
+ };
26
+ if (!Number.isFinite(threshold) || threshold <= 0 || threshold > 1) return base;
27
+ if (!Number.isFinite(contextTokens) || contextTokens <= 0) return base;
28
+ if (!Number.isFinite(contextWindow) || contextWindow <= 0) return base;
29
+ const pressure = contextTokens / contextWindow;
30
+ return {
31
+ shouldCompact: pressure >= threshold,
32
+ pressure,
33
+ contextTokens,
34
+ contextWindow,
35
+ threshold
36
+ };
37
+ }
38
+ var AutoCompactionTrigger = class {
39
+ inFlight = null;
40
+ /** True while a task is in flight. Callers can short-circuit on this. */
41
+ isBusy() {
42
+ return this.inFlight !== null;
43
+ }
44
+ /**
45
+ * Run `task` unless one is already in flight. Returns the Promise the
46
+ * task is running under. Callers always get the eventual result of the
47
+ * first task, never a second call. Rejections clear the slot so a later
48
+ * fire can start fresh.
49
+ */
50
+ fire(task) {
51
+ if (this.inFlight) return this.inFlight;
52
+ const run = task().finally(() => {
53
+ this.inFlight = null;
54
+ });
55
+ this.inFlight = run;
56
+ return run;
57
+ }
58
+ };
59
+
60
+ // src/domains/session/context-ledger.ts
61
+ init_esm_shims();
62
+ var SEGMENT_CATEGORY = {
63
+ identity: "system",
64
+ "operating-contract": "system",
65
+ delegation: "agents",
66
+ skills: "skills",
67
+ safety: "system",
68
+ runtime: "system",
69
+ "tool-contract": "system",
70
+ "retrieval-hints": "system",
71
+ "tool-catalog": "tools",
72
+ "tools-and-agents": "agents",
73
+ "agent-fleet-deltas": "agents",
74
+ "skills-catalog": "skills",
75
+ memory: "memory",
76
+ "project-context": "project",
77
+ "context.project-rules": "project",
78
+ "context.operator-profile": "memory",
79
+ "context.clio-repo-awareness": "system",
80
+ "history-summary": "messages"
81
+ };
82
+ var CONTEXT_CATEGORY_LABEL = {
83
+ system: "System prompt",
84
+ tools: "Tool definitions",
85
+ agents: "Agent fleet",
86
+ skills: "Skills",
87
+ memory: "Memory",
88
+ project: "Project context",
89
+ messages: "Messages",
90
+ pending: "Pending input",
91
+ reserve: "Autocompact reserve",
92
+ free: "Free space",
93
+ streaming: "Streaming output"
94
+ };
95
+ var CONTENT_ORDER = [
96
+ "system",
97
+ "tools",
98
+ "agents",
99
+ "skills",
100
+ "memory",
101
+ "project",
102
+ "messages",
103
+ "pending",
104
+ "streaming"
105
+ ];
106
+ function categoryForSegment(id) {
107
+ return SEGMENT_CATEGORY[id] ?? "system";
108
+ }
109
+ function finiteNonNegative(value) {
110
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
111
+ }
112
+ function percentOf(tokens, window) {
113
+ if (window <= 0) return null;
114
+ return Math.min(100, tokens / window * 100);
115
+ }
116
+ function buildContextLedger(input) {
117
+ const window = finiteNonNegative(input.contextWindow);
118
+ const raw = {
119
+ system: 0,
120
+ tools: 0,
121
+ agents: 0,
122
+ skills: 0,
123
+ memory: 0,
124
+ project: 0,
125
+ messages: 0,
126
+ pending: 0,
127
+ reserve: 0,
128
+ free: 0,
129
+ streaming: 0
130
+ };
131
+ const segments = input.promptSegments ?? [];
132
+ if (segments.length > 0) {
133
+ for (const segment of segments) {
134
+ raw[categoryForSegment(segment.id)] += finiteNonNegative(segment.tokenEstimate);
135
+ }
136
+ } else {
137
+ raw.system += finiteNonNegative(input.systemPromptTokens);
138
+ }
139
+ raw.tools += finiteNonNegative(input.toolSchemaTokens);
140
+ raw.messages += finiteNonNegative(input.messageTokens);
141
+ raw.pending += finiteNonNegative(input.pendingTokens);
142
+ raw.streaming += finiteNonNegative(input.streamingTokens);
143
+ raw.agents += finiteNonNegative(input.agentsTokens);
144
+ raw.skills += finiteNonNegative(input.skillsTokens);
145
+ raw.memory += finiteNonNegative(input.memoryTokens);
146
+ raw.project += finiteNonNegative(input.projectTokens);
147
+ const decomposed = CONTENT_ORDER.reduce((sum, category) => sum + raw[category], 0);
148
+ const liveTotal = finiteNonNegative(input.liveTotalTokens);
149
+ if (liveTotal > decomposed) {
150
+ raw.messages += liveTotal - decomposed;
151
+ }
152
+ const usedTokens = Math.max(decomposed, liveTotal);
153
+ const effectiveThreshold = typeof input.compactionThreshold === "number" && input.compactionThreshold > 0 && input.compactionThreshold < 1 ? input.compactionThreshold : input.compactionAuto ? DEFAULT_COMPACTION_THRESHOLD : null;
154
+ let reserveTokens = 0;
155
+ let freeTokens = 0;
156
+ if (window > 0) {
157
+ const remaining = Math.max(0, window - usedTokens);
158
+ reserveTokens = effectiveThreshold !== null ? Math.min(Math.round(window * (1 - effectiveThreshold)), remaining) : 0;
159
+ freeTokens = Math.max(0, window - usedTokens - reserveTokens);
160
+ }
161
+ const groups = [];
162
+ for (const category of CONTENT_ORDER) {
163
+ const tokens = raw[category];
164
+ if (tokens <= 0) continue;
165
+ groups.push({
166
+ category,
167
+ label: CONTEXT_CATEGORY_LABEL[category],
168
+ tokens,
169
+ percent: percentOf(tokens, window)
170
+ });
171
+ }
172
+ const meter = [...groups];
173
+ if (reserveTokens > 0) {
174
+ meter.push({
175
+ category: "reserve",
176
+ label: CONTEXT_CATEGORY_LABEL.reserve,
177
+ tokens: reserveTokens,
178
+ percent: percentOf(reserveTokens, window)
179
+ });
180
+ }
181
+ if (window > 0) {
182
+ meter.push({
183
+ category: "free",
184
+ label: CONTEXT_CATEGORY_LABEL.free,
185
+ tokens: freeTokens,
186
+ percent: percentOf(freeTokens, window)
187
+ });
188
+ }
189
+ return {
190
+ provider: input.provider ?? null,
191
+ model: input.model ?? null,
192
+ contextWindow: window,
193
+ contextWindowSource: input.contextWindowSource ?? null,
194
+ contextWindowSlots: input.contextWindowSlots ?? null,
195
+ usedTokens,
196
+ reserveTokens,
197
+ freeTokens,
198
+ percent: percentOf(usedTokens, window),
199
+ measured: input.measured === true && liveTotal > 0,
200
+ compactionThreshold: effectiveThreshold,
201
+ compactionAuto: input.compactionAuto === true,
202
+ projectPreload: input.projectPreload ?? null,
203
+ projectHandbookFiles: input.projectHandbookFiles ?? null,
204
+ toolCount: Math.max(0, Math.floor(input.toolCount ?? 0)),
205
+ groups,
206
+ meter,
207
+ lastCompaction: input.lastCompaction ?? null,
208
+ promptCache: input.promptCache ?? null
209
+ };
210
+ }
211
+
212
+ // src/domains/session/context-accounting.ts
213
+ var IMAGE_CHAR_ESTIMATE = 4800;
214
+ var TOKEN_CHARS = 4;
215
+ var MESSAGE_OVERHEAD_TOKENS = 16;
216
+ function ceilChars(chars) {
217
+ return Math.ceil(Math.max(0, chars) / TOKEN_CHARS);
218
+ }
219
+ function isRecord(value) {
220
+ return !!value && typeof value === "object" && !Array.isArray(value);
221
+ }
222
+ function jsonLength(value) {
223
+ if (value === void 0 || value === null) return 0;
224
+ if (typeof value === "string") return value.length;
225
+ if (typeof value === "number" || typeof value === "boolean") return String(value).length;
226
+ try {
227
+ return JSON.stringify(value).length;
228
+ } catch {
229
+ return String(value).length;
230
+ }
231
+ }
232
+ function argsLength(value) {
233
+ if (value === void 0 || value === null) return 0;
234
+ return typeof value === "string" ? value.length : jsonLength(value);
235
+ }
236
+ function blockChars(block) {
237
+ if (typeof block === "string") return block.length;
238
+ if (!isRecord(block)) return jsonLength(block);
239
+ if (block.type === "text" && typeof block.text === "string") return block.text.length;
240
+ if (block.type === "thinking" && typeof block.thinking === "string") return block.thinking.length;
241
+ if (block.type === "image") return IMAGE_CHAR_ESTIMATE;
242
+ if (block.type === "toolCall") {
243
+ const name = typeof block.name === "string" ? block.name : "";
244
+ const id = typeof block.id === "string" ? block.id : "";
245
+ return id.length + name.length + argsLength(block.arguments ?? block.args ?? block.input);
246
+ }
247
+ if (Array.isArray(block.content)) return contentChars(block.content);
248
+ if (typeof block.text === "string") return block.text.length;
249
+ if (typeof block.thinking === "string") return block.thinking.length;
250
+ return jsonLength(block);
251
+ }
252
+ function contentChars(content) {
253
+ if (typeof content === "string") return content.length;
254
+ if (!Array.isArray(content)) return jsonLength(content);
255
+ return content.reduce((sum, block) => sum + blockChars(block), 0);
256
+ }
257
+ function messageChars(message) {
258
+ if (!isRecord(message)) return jsonLength(message);
259
+ let chars = 0;
260
+ chars += typeof message.role === "string" ? message.role.length : 0;
261
+ const payload = message.payload !== void 0 ? message.payload : message.content;
262
+ chars += contentChars(payload);
263
+ chars += typeof message.toolCallId === "string" ? message.toolCallId.length : 0;
264
+ chars += typeof message.toolName === "string" ? message.toolName.length : 0;
265
+ chars += typeof message.errorMessage === "string" ? message.errorMessage.length : 0;
266
+ return chars;
267
+ }
268
+ function toolSchemaChars(tool) {
269
+ if (!isRecord(tool)) return jsonLength(tool);
270
+ const name = typeof tool.name === "string" ? tool.name.length : 0;
271
+ const description = typeof tool.description === "string" ? tool.description.length : 0;
272
+ const parameters = jsonLength(tool.parameters);
273
+ return name + description + parameters;
274
+ }
275
+ function usageTotalTokens(usage) {
276
+ if (!isRecord(usage)) return null;
277
+ const total = usage.totalTokens;
278
+ if (typeof total === "number" && Number.isFinite(total) && total > 0) return total;
279
+ const input = typeof usage.input === "number" && Number.isFinite(usage.input) ? usage.input : 0;
280
+ const output = typeof usage.output === "number" && Number.isFinite(usage.output) ? usage.output : 0;
281
+ const cacheRead = typeof usage.cacheRead === "number" && Number.isFinite(usage.cacheRead) ? usage.cacheRead : 0;
282
+ const cacheWrite = typeof usage.cacheWrite === "number" && Number.isFinite(usage.cacheWrite) ? usage.cacheWrite : 0;
283
+ const sum = input + output + cacheRead + cacheWrite;
284
+ return sum > 0 ? sum : null;
285
+ }
286
+ function usageInvalidated(message) {
287
+ return message.contextUsageInvalidated === true;
288
+ }
289
+ function latestUsableAssistantUsage(messages) {
290
+ for (let i = messages.length - 1; i >= 0; i -= 1) {
291
+ const message = messages[i];
292
+ if (message?.role !== "assistant") continue;
293
+ if (usageInvalidated(message)) continue;
294
+ const stopReason = message.stopReason;
295
+ if (stopReason === "error" || stopReason === "aborted") continue;
296
+ const tokens = usageTotalTokens(message.usage);
297
+ if (tokens !== null) return { index: i, tokens };
298
+ }
299
+ return null;
300
+ }
301
+ function finiteNonNegative2(value) {
302
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null;
303
+ }
304
+ function nestedNumber(root, path) {
305
+ let current = root;
306
+ for (const key of path) {
307
+ if (!isRecord(current)) return null;
308
+ current = current[key];
309
+ }
310
+ return finiteNonNegative2(current);
311
+ }
312
+ function estimateReasoningTextTokens(text) {
313
+ if (typeof text !== "string" || text.trim().length === 0) return null;
314
+ return Math.max(1, Math.round(text.length / TOKEN_CHARS));
315
+ }
316
+ function extractReasoningTokens(usage) {
317
+ if (!isRecord(usage)) return null;
318
+ const direct = finiteNonNegative2(usage.reasoningTokens) ?? finiteNonNegative2(usage.reasoning_tokens) ?? finiteNonNegative2(usage.reasoning);
319
+ if (direct !== null) return direct;
320
+ const paths = [
321
+ ["outputDetails", "reasoningTokens"],
322
+ ["output_details", "reasoning_tokens"],
323
+ ["output_tokens_details", "reasoning_tokens"],
324
+ ["completion_tokens_details", "reasoning_tokens"],
325
+ ["completionTokensDetails", "reasoningTokens"],
326
+ ["details", "reasoningTokens"]
327
+ ];
328
+ for (const path of paths) {
329
+ const value = nestedNumber(usage, path);
330
+ if (value !== null) return value;
331
+ }
332
+ return null;
333
+ }
334
+ function estimateAgentMessageTokens(message) {
335
+ return ceilChars(messageChars(message)) + MESSAGE_OVERHEAD_TOKENS;
336
+ }
337
+ function estimateAgentContextBreakdown(input) {
338
+ return {
339
+ systemPromptTokens: input.systemPrompt ? ceilChars(input.systemPrompt.length) : 0,
340
+ messageTokens: input.messages.reduce((sum, message) => sum + estimateAgentMessageTokens(message), 0),
341
+ pendingUserTokens: input.pendingUserText ? ceilChars(input.pendingUserText.length) : 0,
342
+ toolSchemaTokens: (input.tools ?? []).reduce((sum, tool) => sum + ceilChars(toolSchemaChars(tool)), 0)
343
+ };
344
+ }
345
+ function estimateAgentContextTokens(input) {
346
+ const breakdown = estimateAgentContextBreakdown(input);
347
+ const projection = breakdown.systemPromptTokens + breakdown.messageTokens + breakdown.pendingUserTokens + breakdown.toolSchemaTokens;
348
+ const usage = latestUsableAssistantUsage(input.messages);
349
+ if (!usage) return projection;
350
+ const trailingTokens = input.messages.slice(usage.index + 1).reduce((sum, message) => sum + estimateAgentMessageTokens(message), 0);
351
+ const anchored = usage.tokens + trailingTokens + breakdown.pendingUserTokens + breakdown.toolSchemaTokens;
352
+ return Math.max(projection, anchored);
353
+ }
354
+ function contextUsageSnapshot(tokens, contextWindow, breakdown) {
355
+ const window = typeof contextWindow === "number" && Number.isFinite(contextWindow) ? Math.max(0, contextWindow) : 0;
356
+ const base = tokens === null || tokens <= 0 || window <= 0 ? { tokens, contextWindow: window, percent: null } : { tokens, contextWindow: window, percent: Math.min(100, tokens / window * 100) };
357
+ return breakdown ? { ...base, breakdown } : base;
358
+ }
359
+ function getSnapshotsFilePath(meta) {
360
+ const safeMeta = {
361
+ ...meta,
362
+ cwdHash: meta.cwdHash || cwdHash(meta.cwd || process.cwd())
363
+ };
364
+ const paths = sessionPaths(safeMeta);
365
+ return join(dirname(paths.current), "context-snapshots.jsonl");
366
+ }
367
+ function persistableSnapshot(snapshot) {
368
+ const {
369
+ systemPrompt: _systemPrompt,
370
+ conversationMessages: _messages,
371
+ activeToolSchemas: _tools,
372
+ pendingUserInput: _pending,
373
+ images: _images,
374
+ ...slim
375
+ } = snapshot;
376
+ return slim;
377
+ }
378
+ function appendContextSnapshot(meta, snapshot) {
379
+ try {
380
+ const file = getSnapshotsFilePath(meta);
381
+ appendFileSync(file, `${JSON.stringify(persistableSnapshot(snapshot))}
382
+ `, "utf8");
383
+ } catch {
384
+ }
385
+ }
386
+ function getContextSnapshots(meta) {
387
+ try {
388
+ const file = getSnapshotsFilePath(meta);
389
+ if (!existsSync(file)) return [];
390
+ const content = readFileSync(file, "utf8");
391
+ const snapshots = [];
392
+ for (const line of content.split("\n")) {
393
+ if (line.trim().length === 0) continue;
394
+ try {
395
+ snapshots.push(JSON.parse(line));
396
+ } catch {
397
+ }
398
+ }
399
+ return snapshots;
400
+ } catch {
401
+ return [];
402
+ }
403
+ }
404
+ function getLatestContextSnapshot(meta) {
405
+ const list = getContextSnapshots(meta);
406
+ return list.at(-1) ?? null;
407
+ }
408
+ function buildSnapshotCategories(inputs) {
409
+ const raw = {
410
+ system: 0,
411
+ tools: 0,
412
+ agents: 0,
413
+ skills: 0,
414
+ memory: 0,
415
+ project: 0,
416
+ messages: 0
417
+ };
418
+ if (inputs.promptSegments && inputs.promptSegments.length > 0) {
419
+ for (const segment of inputs.promptSegments) {
420
+ const cat = categoryForSegment(segment.id);
421
+ raw[cat] = (raw[cat] ?? 0) + segment.tokenEstimate;
422
+ }
423
+ } else if (inputs.systemPrompt) {
424
+ raw.system += ceilChars(inputs.systemPrompt.length);
425
+ }
426
+ if (inputs.tools) {
427
+ for (const tool of inputs.tools) {
428
+ raw.tools += ceilChars(toolSchemaChars(tool));
429
+ }
430
+ }
431
+ if (inputs.messages) {
432
+ for (const msg of inputs.messages) {
433
+ raw.messages += estimateAgentMessageTokens(msg);
434
+ }
435
+ }
436
+ const decomposed = Object.keys(raw).reduce((sum, k) => sum + (raw[k] ?? 0), 0);
437
+ const window = inputs.effectiveContextWindow;
438
+ const threshold = inputs.compactionThreshold ?? DEFAULT_COMPACTION_THRESHOLD;
439
+ let reserve = 0;
440
+ let free = 0;
441
+ if (window > 0) {
442
+ const remaining = Math.max(0, window - decomposed);
443
+ reserve = Math.min(Math.round(window * (1 - threshold)), remaining);
444
+ free = Math.max(0, window - decomposed - reserve);
445
+ }
446
+ return {
447
+ system: raw.system,
448
+ tools: raw.tools,
449
+ agents: raw.agents,
450
+ skills: raw.skills,
451
+ memory: raw.memory,
452
+ project: raw.project,
453
+ messages: raw.messages,
454
+ reserve,
455
+ free,
456
+ streaming: 0
457
+ };
458
+ }
459
+ var SNAPSHOT_SPLIT_KEYS = ["system", "tools", "agents", "skills", "memory", "project", "messages"];
460
+ function estimatedSplitSources() {
461
+ const splits = {};
462
+ for (const key of SNAPSHOT_SPLIT_KEYS) splits[key] = "estimated";
463
+ return splits;
464
+ }
465
+ var snapshotCounter = 0;
466
+ function nextSnapshotId() {
467
+ snapshotCounter += 1;
468
+ return `snap-${Date.now()}-${snapshotCounter.toString(36)}-${Math.random().toString(36).slice(2, 7)}`;
469
+ }
470
+ function captureContextSnapshot(input) {
471
+ const categories = buildSnapshotCategories({
472
+ systemPrompt: input.systemPrompt,
473
+ promptSegments: input.promptSegments,
474
+ tools: input.activeToolSchemas,
475
+ messages: input.conversationMessages,
476
+ effectiveContextWindow: input.effectiveContextWindow,
477
+ compactionThreshold: input.compactionThreshold
478
+ });
479
+ return {
480
+ snapshotId: nextSnapshotId(),
481
+ sessionId: input.sessionId,
482
+ turnId: input.turnId,
483
+ providerId: input.providerId,
484
+ runtimeId: input.runtimeId,
485
+ modelId: input.modelId,
486
+ systemPrompt: input.systemPrompt,
487
+ promptSegments: input.promptSegments ? [...input.promptSegments] : void 0,
488
+ conversationMessages: [...input.conversationMessages],
489
+ activeToolSchemas: [...input.activeToolSchemas],
490
+ pendingUserInput: input.pendingUserInput,
491
+ images: input.images ? [...input.images] : void 0,
492
+ desiredContextWindow: input.desiredContextWindow,
493
+ effectiveContextWindow: input.effectiveContextWindow,
494
+ contextWindowSource: input.contextWindowSource,
495
+ categories,
496
+ sources: { total: "estimated", splits: estimatedSplitSources() },
497
+ promptHash: input.promptHash,
498
+ toolSignature: input.toolSignature,
499
+ messageRange: input.conversationMessages.length > 0 ? { start: 0, end: input.conversationMessages.length - 1 } : void 0
500
+ };
501
+ }
502
+ function snapshotInputTokens(snapshot) {
503
+ return SNAPSHOT_SPLIT_KEYS.reduce((sum, key) => sum + (snapshot.categories[key] ?? 0), 0);
504
+ }
505
+ function reconcileSnapshot(snapshot, usage) {
506
+ const exactInput = (usage.input || 0) + (usage.cacheRead || 0) + (usage.cacheWrite || 0);
507
+ const exactOutput = usage.output || 0;
508
+ if (exactInput <= 0) {
509
+ const updatedCategories2 = {
510
+ ...snapshot.categories,
511
+ streaming: exactOutput
512
+ };
513
+ const window2 = snapshot.effectiveContextWindow;
514
+ const used2 = snapshotInputTokens(snapshot) + exactOutput;
515
+ let reserve2 = snapshot.categories.reserve;
516
+ let free2 = 0;
517
+ if (window2 > 0) {
518
+ const remaining = Math.max(0, window2 - used2);
519
+ reserve2 = Math.min(reserve2, remaining);
520
+ free2 = Math.max(0, window2 - used2 - reserve2);
521
+ }
522
+ updatedCategories2.reserve = reserve2;
523
+ updatedCategories2.free = free2;
524
+ return {
525
+ ...snapshot,
526
+ categories: updatedCategories2,
527
+ sources: {
528
+ total: "estimated",
529
+ splits: snapshot.sources.splits
530
+ }
531
+ };
532
+ }
533
+ const keys = SNAPSHOT_SPLIT_KEYS;
534
+ const currentCategories = { ...snapshot.categories };
535
+ const sum = keys.reduce((s, k) => s + (currentCategories[k] ?? 0), 0);
536
+ let normalized;
537
+ if (sum === 0) {
538
+ normalized = keys.reduce(
539
+ (acc, k) => {
540
+ acc[k] = k === "messages" ? exactInput : 0;
541
+ return acc;
542
+ },
543
+ {}
544
+ );
545
+ } else {
546
+ normalized = {};
547
+ let newSum = 0;
548
+ for (const key of keys) {
549
+ const val = currentCategories[key] ?? 0;
550
+ const norm = Math.round(val / sum * exactInput);
551
+ normalized[key] = norm;
552
+ newSum += norm;
553
+ }
554
+ const diff = exactInput - newSum;
555
+ if (diff !== 0) {
556
+ const largestKey = keys.reduce((a, b) => (normalized[a] ?? 0) >= (normalized[b] ?? 0) ? a : b);
557
+ normalized[largestKey] = (normalized[largestKey] ?? 0) + diff;
558
+ }
559
+ }
560
+ const updatedCategories = {
561
+ ...snapshot.categories,
562
+ ...normalized,
563
+ streaming: exactOutput
564
+ };
565
+ const window = snapshot.effectiveContextWindow;
566
+ const used = exactInput + exactOutput;
567
+ let reserve = snapshot.categories.reserve;
568
+ let free = 0;
569
+ if (window > 0) {
570
+ const remaining = Math.max(0, window - used);
571
+ reserve = Math.min(reserve, remaining);
572
+ free = Math.max(0, window - used - reserve);
573
+ }
574
+ updatedCategories.reserve = reserve;
575
+ updatedCategories.free = free;
576
+ const splitsSources = keys.reduce(
577
+ (acc, k) => {
578
+ acc[k] = "reconciled";
579
+ return acc;
580
+ },
581
+ {}
582
+ );
583
+ return {
584
+ ...snapshot,
585
+ categories: updatedCategories,
586
+ sources: {
587
+ total: "reconciled",
588
+ splits: splitsSources
589
+ }
590
+ };
591
+ }
592
+
593
+ export {
594
+ DEFAULT_COMPACTION_THRESHOLD,
595
+ shouldCompact,
596
+ AutoCompactionTrigger,
597
+ buildContextLedger,
598
+ ceilChars,
599
+ contentChars,
600
+ toolSchemaChars,
601
+ estimateReasoningTextTokens,
602
+ extractReasoningTokens,
603
+ estimateAgentMessageTokens,
604
+ estimateAgentContextBreakdown,
605
+ estimateAgentContextTokens,
606
+ contextUsageSnapshot,
607
+ appendContextSnapshot,
608
+ getLatestContextSnapshot,
609
+ captureContextSnapshot,
610
+ snapshotInputTokens,
611
+ reconcileSnapshot
612
+ };
613
+ //# sourceMappingURL=chunk-GEYXPTRF.js.map