@namzu/sdk 72.0.0 → 74.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (394) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/README.md +34 -34
  3. package/dist/agents/examples/SupervisorAgent.d.ts.map +1 -1
  4. package/dist/agents/examples/SupervisorAgent.js +5 -0
  5. package/dist/agents/examples/SupervisorAgent.js.map +1 -1
  6. package/dist/authorization/protected-paths.d.ts +7 -0
  7. package/dist/authorization/protected-paths.d.ts.map +1 -1
  8. package/dist/authorization/protected-paths.js +11 -0
  9. package/dist/authorization/protected-paths.js.map +1 -1
  10. package/dist/authorization/readonly-commands.d.ts +23 -0
  11. package/dist/authorization/readonly-commands.d.ts.map +1 -1
  12. package/dist/authorization/readonly-commands.js +89 -3
  13. package/dist/authorization/readonly-commands.js.map +1 -1
  14. package/dist/authorization/shell-paths.d.ts +8 -0
  15. package/dist/authorization/shell-paths.d.ts.map +1 -1
  16. package/dist/authorization/shell-paths.js +125 -2
  17. package/dist/authorization/shell-paths.js.map +1 -1
  18. package/dist/bridge/acp/permission.d.ts +6 -0
  19. package/dist/bridge/acp/permission.d.ts.map +1 -1
  20. package/dist/bridge/acp/permission.js.map +1 -1
  21. package/dist/bridge/acp/server.d.ts.map +1 -1
  22. package/dist/bridge/acp/server.js +21 -3
  23. package/dist/bridge/acp/server.js.map +1 -1
  24. package/dist/compaction/context-window.d.ts.map +1 -1
  25. package/dist/compaction/context-window.js +11 -0
  26. package/dist/compaction/context-window.js.map +1 -1
  27. package/dist/compaction/manual.d.ts +16 -1
  28. package/dist/compaction/manual.d.ts.map +1 -1
  29. package/dist/compaction/manual.js +75 -13
  30. package/dist/compaction/manual.js.map +1 -1
  31. package/dist/compaction/plan.d.ts +2 -1
  32. package/dist/compaction/plan.d.ts.map +1 -1
  33. package/dist/compaction/plan.js +6 -5
  34. package/dist/compaction/plan.js.map +1 -1
  35. package/dist/compaction/precompute.d.ts +179 -0
  36. package/dist/compaction/precompute.d.ts.map +1 -0
  37. package/dist/compaction/precompute.js +274 -0
  38. package/dist/compaction/precompute.js.map +1 -0
  39. package/dist/compaction/retained-images.d.ts +16 -0
  40. package/dist/compaction/retained-images.d.ts.map +1 -0
  41. package/dist/compaction/retained-images.js +113 -0
  42. package/dist/compaction/retained-images.js.map +1 -0
  43. package/dist/compaction/token-estimate.d.ts +8 -3
  44. package/dist/compaction/token-estimate.d.ts.map +1 -1
  45. package/dist/compaction/token-estimate.js +76 -8
  46. package/dist/compaction/token-estimate.js.map +1 -1
  47. package/dist/compaction/tool-result-editing.d.ts.map +1 -1
  48. package/dist/compaction/tool-result-editing.js +15 -8
  49. package/dist/compaction/tool-result-editing.js.map +1 -1
  50. package/dist/config/runtime.d.ts +4 -0
  51. package/dist/config/runtime.d.ts.map +1 -1
  52. package/dist/config/runtime.js +17 -0
  53. package/dist/config/runtime.js.map +1 -1
  54. package/dist/constants/acp/index.d.ts +2 -0
  55. package/dist/constants/acp/index.d.ts.map +1 -1
  56. package/dist/constants/acp/index.js +2 -0
  57. package/dist/constants/acp/index.js.map +1 -1
  58. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  59. package/dist/manager/agent/lifecycle.js +8 -0
  60. package/dist/manager/agent/lifecycle.js.map +1 -1
  61. package/dist/manager/session/turn-recorder.d.ts +26 -0
  62. package/dist/manager/session/turn-recorder.d.ts.map +1 -1
  63. package/dist/manager/session/turn-recorder.js +72 -5
  64. package/dist/manager/session/turn-recorder.js.map +1 -1
  65. package/dist/peers/protocol.d.ts +9 -0
  66. package/dist/peers/protocol.d.ts.map +1 -1
  67. package/dist/peers/protocol.js +4 -0
  68. package/dist/peers/protocol.js.map +1 -1
  69. package/dist/peers/record.d.ts +6 -0
  70. package/dist/peers/record.d.ts.map +1 -1
  71. package/dist/peers/record.js +4 -0
  72. package/dist/peers/record.js.map +1 -1
  73. package/dist/plugin/index.d.ts +2 -2
  74. package/dist/plugin/index.d.ts.map +1 -1
  75. package/dist/plugin/index.js +1 -1
  76. package/dist/plugin/index.js.map +1 -1
  77. package/dist/plugin/shell-hook.d.ts +30 -0
  78. package/dist/plugin/shell-hook.d.ts.map +1 -1
  79. package/dist/plugin/shell-hook.js +35 -0
  80. package/dist/plugin/shell-hook.js.map +1 -1
  81. package/dist/pricing/catalogue.generated.d.ts.map +1 -1
  82. package/dist/pricing/catalogue.generated.js +178 -1
  83. package/dist/pricing/catalogue.generated.js.map +1 -1
  84. package/dist/provider/attribution.js +1 -1
  85. package/dist/provider/attribution.js.map +1 -1
  86. package/dist/public-runtime.d.ts +9 -5
  87. package/dist/public-runtime.d.ts.map +1 -1
  88. package/dist/public-runtime.js +12 -4
  89. package/dist/public-runtime.js.map +1 -1
  90. package/dist/public-tools.d.ts +1 -1
  91. package/dist/public-tools.d.ts.map +1 -1
  92. package/dist/public-tools.js +1 -1
  93. package/dist/public-tools.js.map +1 -1
  94. package/dist/public-types.d.ts +3 -2
  95. package/dist/public-types.d.ts.map +1 -1
  96. package/dist/rag/embedding.d.ts +5 -1
  97. package/dist/rag/embedding.d.ts.map +1 -1
  98. package/dist/rag/embedding.js +22 -7
  99. package/dist/rag/embedding.js.map +1 -1
  100. package/dist/registry/command/kernel-commands.d.ts +7 -1
  101. package/dist/registry/command/kernel-commands.d.ts.map +1 -1
  102. package/dist/registry/command/kernel-commands.js +40 -2
  103. package/dist/registry/command/kernel-commands.js.map +1 -1
  104. package/dist/registry/tool/execute.d.ts +8 -0
  105. package/dist/registry/tool/execute.d.ts.map +1 -1
  106. package/dist/registry/tool/execute.js +12 -0
  107. package/dist/registry/tool/execute.js.map +1 -1
  108. package/dist/runtime/query/abandon-turn.d.ts.map +1 -1
  109. package/dist/runtime/query/abandon-turn.js +9 -0
  110. package/dist/runtime/query/abandon-turn.js.map +1 -1
  111. package/dist/runtime/query/budget-nudge.d.ts +27 -0
  112. package/dist/runtime/query/budget-nudge.d.ts.map +1 -0
  113. package/dist/runtime/query/budget-nudge.js +66 -0
  114. package/dist/runtime/query/budget-nudge.js.map +1 -0
  115. package/dist/runtime/query/compact-session.d.ts +19 -3
  116. package/dist/runtime/query/compact-session.d.ts.map +1 -1
  117. package/dist/runtime/query/compact-session.js +180 -93
  118. package/dist/runtime/query/compact-session.js.map +1 -1
  119. package/dist/runtime/query/guard.d.ts +8 -0
  120. package/dist/runtime/query/guard.d.ts.map +1 -1
  121. package/dist/runtime/query/guard.js +20 -0
  122. package/dist/runtime/query/guard.js.map +1 -1
  123. package/dist/runtime/query/index.d.ts.map +1 -1
  124. package/dist/runtime/query/index.js +1 -0
  125. package/dist/runtime/query/index.js.map +1 -1
  126. package/dist/runtime/query/iteration/index.d.ts +2 -0
  127. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  128. package/dist/runtime/query/iteration/index.js +41 -1
  129. package/dist/runtime/query/iteration/index.js.map +1 -1
  130. package/dist/runtime/query/iteration/phases/compaction.d.ts +1 -0
  131. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  132. package/dist/runtime/query/iteration/phases/compaction.js +203 -45
  133. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  134. package/dist/runtime/query/iteration/phases/context.d.ts +2 -0
  135. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  136. package/dist/runtime/query/iteration/phases/context.js +1 -0
  137. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  138. package/dist/runtime/query/iteration/phases/precompute.d.ts +60 -0
  139. package/dist/runtime/query/iteration/phases/precompute.d.ts.map +1 -0
  140. package/dist/runtime/query/iteration/phases/precompute.js +240 -0
  141. package/dist/runtime/query/iteration/phases/precompute.js.map +1 -0
  142. package/dist/runtime/query/iteration/request-prefix.d.ts +21 -0
  143. package/dist/runtime/query/iteration/request-prefix.d.ts.map +1 -0
  144. package/dist/runtime/query/iteration/request-prefix.js +111 -0
  145. package/dist/runtime/query/iteration/request-prefix.js.map +1 -0
  146. package/dist/runtime/query/review-policy.d.ts +11 -3
  147. package/dist/runtime/query/review-policy.d.ts.map +1 -1
  148. package/dist/runtime/query/review-policy.js +54 -3
  149. package/dist/runtime/query/review-policy.js.map +1 -1
  150. package/dist/runtime/query/turn/outcome-ledger.d.ts +14 -0
  151. package/dist/runtime/query/turn/outcome-ledger.d.ts.map +1 -1
  152. package/dist/runtime/query/turn/outcome-ledger.js +28 -0
  153. package/dist/runtime/query/turn/outcome-ledger.js.map +1 -1
  154. package/dist/sandbox/exec-stdin.d.ts +21 -0
  155. package/dist/sandbox/exec-stdin.d.ts.map +1 -0
  156. package/dist/sandbox/exec-stdin.js +40 -0
  157. package/dist/sandbox/exec-stdin.js.map +1 -0
  158. package/dist/sandbox/file-walk.d.ts +12 -7
  159. package/dist/sandbox/file-walk.d.ts.map +1 -1
  160. package/dist/sandbox/file-walk.js +13 -4
  161. package/dist/sandbox/file-walk.js.map +1 -1
  162. package/dist/sandbox/ignore-matcher.d.ts +1 -0
  163. package/dist/sandbox/ignore-matcher.d.ts.map +1 -1
  164. package/dist/sandbox/ignore-matcher.js.map +1 -1
  165. package/dist/sandbox/ignore-source.js +1 -1
  166. package/dist/sandbox/index.d.ts +3 -2
  167. package/dist/sandbox/index.d.ts.map +1 -1
  168. package/dist/sandbox/index.js +2 -1
  169. package/dist/sandbox/index.js.map +1 -1
  170. package/dist/sandbox/provider/local.d.ts.map +1 -1
  171. package/dist/sandbox/provider/local.js +2 -0
  172. package/dist/sandbox/provider/local.js.map +1 -1
  173. package/dist/session/paths.d.ts +6 -0
  174. package/dist/session/paths.d.ts.map +1 -1
  175. package/dist/session/paths.js +7 -0
  176. package/dist/session/paths.js.map +1 -1
  177. package/dist/session-query/index.d.ts +1 -0
  178. package/dist/session-query/index.d.ts.map +1 -1
  179. package/dist/session-query/index.js +26 -15
  180. package/dist/session-query/index.js.map +1 -1
  181. package/dist/store/evidence/disk.js +3 -1
  182. package/dist/store/evidence/disk.js.map +1 -1
  183. package/dist/store/evidence/index-page.d.ts +1 -0
  184. package/dist/store/evidence/index-page.d.ts.map +1 -1
  185. package/dist/store/evidence/index-page.js +8 -0
  186. package/dist/store/evidence/index-page.js.map +1 -1
  187. package/dist/store/goal/gates.d.ts +53 -0
  188. package/dist/store/goal/gates.d.ts.map +1 -0
  189. package/dist/store/goal/gates.js +664 -0
  190. package/dist/store/goal/gates.js.map +1 -0
  191. package/dist/store/goal/index.d.ts +16 -1
  192. package/dist/store/goal/index.d.ts.map +1 -1
  193. package/dist/store/goal/index.js +74 -0
  194. package/dist/store/goal/index.js.map +1 -1
  195. package/dist/store/memory/operation-lock.d.ts.map +1 -1
  196. package/dist/store/memory/operation-lock.js +15 -1
  197. package/dist/store/memory/operation-lock.js.map +1 -1
  198. package/dist/store/session-log/core.d.ts.map +1 -1
  199. package/dist/store/session-log/core.js +23 -0
  200. package/dist/store/session-log/core.js.map +1 -1
  201. package/dist/tools/builtins/bash.js +1 -1
  202. package/dist/tools/builtins/bash.js.map +1 -1
  203. package/dist/tools/builtins/browser.d.ts.map +1 -1
  204. package/dist/tools/builtins/browser.js +2 -0
  205. package/dist/tools/builtins/browser.js.map +1 -1
  206. package/dist/tools/builtins/computer-use-image.d.ts +9 -0
  207. package/dist/tools/builtins/computer-use-image.d.ts.map +1 -1
  208. package/dist/tools/builtins/computer-use-image.js +21 -0
  209. package/dist/tools/builtins/computer-use-image.js.map +1 -1
  210. package/dist/tools/builtins/computer-use.d.ts +1 -9
  211. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  212. package/dist/tools/builtins/computer-use.js +2 -22
  213. package/dist/tools/builtins/computer-use.js.map +1 -1
  214. package/dist/tools/builtins/monitor.d.ts.map +1 -1
  215. package/dist/tools/builtins/monitor.js +1 -0
  216. package/dist/tools/builtins/monitor.js.map +1 -1
  217. package/dist/tools/builtins/search-tools.js +2 -2
  218. package/dist/tools/builtins/search-tools.js.map +1 -1
  219. package/dist/tools/builtins/sleep-guard.js +1 -1
  220. package/dist/tools/builtins/sleep-guard.js.map +1 -1
  221. package/dist/tools/builtins/wait-for-job.js +1 -1
  222. package/dist/tools/builtins/wait-for-job.js.map +1 -1
  223. package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -1
  224. package/dist/tools/coordinator/ask-user-question.js +25 -0
  225. package/dist/tools/coordinator/ask-user-question.js.map +1 -1
  226. package/dist/tools/coordinator/index.d.ts.map +1 -1
  227. package/dist/tools/coordinator/index.js +3 -0
  228. package/dist/tools/coordinator/index.js.map +1 -1
  229. package/dist/tools/defineTool.d.ts +2 -0
  230. package/dist/tools/defineTool.d.ts.map +1 -1
  231. package/dist/tools/defineTool.js +3 -0
  232. package/dist/tools/defineTool.js.map +1 -1
  233. package/dist/tools/goal/confirmation.d.ts +40 -2
  234. package/dist/tools/goal/confirmation.d.ts.map +1 -1
  235. package/dist/tools/goal/confirmation.js +117 -0
  236. package/dist/tools/goal/confirmation.js.map +1 -1
  237. package/dist/tools/goal/index.d.ts.map +1 -1
  238. package/dist/tools/goal/index.js +14 -2
  239. package/dist/tools/goal/index.js.map +1 -1
  240. package/dist/tools/memory/save.d.ts.map +1 -1
  241. package/dist/tools/memory/save.js +1 -0
  242. package/dist/tools/memory/save.js.map +1 -1
  243. package/dist/tools/schedules/index.d.ts +1 -0
  244. package/dist/tools/schedules/index.d.ts.map +1 -1
  245. package/dist/tools/schedules/index.js +1 -0
  246. package/dist/tools/schedules/index.js.map +1 -1
  247. package/dist/tools/schedules/loop-tool.d.ts.map +1 -1
  248. package/dist/tools/schedules/loop-tool.js +1 -0
  249. package/dist/tools/schedules/loop-tool.js.map +1 -1
  250. package/dist/tools/schedules/present.d.ts.map +1 -1
  251. package/dist/tools/schedules/present.js +2 -0
  252. package/dist/tools/schedules/present.js.map +1 -1
  253. package/dist/tools/schedules/schedule-tool.d.ts.map +1 -1
  254. package/dist/tools/schedules/schedule-tool.js +79 -9
  255. package/dist/tools/schedules/schedule-tool.js.map +1 -1
  256. package/dist/tools/schedules/self-tool.d.ts +16 -0
  257. package/dist/tools/schedules/self-tool.d.ts.map +1 -0
  258. package/dist/tools/schedules/self-tool.js +140 -0
  259. package/dist/tools/schedules/self-tool.js.map +1 -0
  260. package/dist/tools/schedules/timer-tools.d.ts.map +1 -1
  261. package/dist/tools/schedules/timer-tools.js +1 -0
  262. package/dist/tools/schedules/timer-tools.js.map +1 -1
  263. package/dist/tools/schedules/types.d.ts +63 -0
  264. package/dist/tools/schedules/types.d.ts.map +1 -1
  265. package/dist/toolsets/manager.d.ts +14 -0
  266. package/dist/toolsets/manager.d.ts.map +1 -1
  267. package/dist/toolsets/manager.js +60 -7
  268. package/dist/toolsets/manager.js.map +1 -1
  269. package/dist/types/agent/supervisor.d.ts +10 -0
  270. package/dist/types/agent/supervisor.d.ts.map +1 -1
  271. package/dist/types/agent/task.d.ts +6 -0
  272. package/dist/types/agent/task.d.ts.map +1 -1
  273. package/dist/types/common/index.d.ts +20 -0
  274. package/dist/types/common/index.d.ts.map +1 -1
  275. package/dist/types/common/index.js +4 -1
  276. package/dist/types/common/index.js.map +1 -1
  277. package/dist/types/goal/index.d.ts +66 -0
  278. package/dist/types/goal/index.d.ts.map +1 -1
  279. package/dist/types/hitl/index.d.ts +11 -0
  280. package/dist/types/hitl/index.d.ts.map +1 -1
  281. package/dist/types/hitl/index.js.map +1 -1
  282. package/dist/types/rag/embedding.d.ts +4 -0
  283. package/dist/types/rag/embedding.d.ts.map +1 -1
  284. package/dist/types/sandbox/index.d.ts +19 -0
  285. package/dist/types/sandbox/index.d.ts.map +1 -1
  286. package/dist/types/sandbox/index.js.map +1 -1
  287. package/dist/types/session/events.d.ts +55 -0
  288. package/dist/types/session/events.d.ts.map +1 -1
  289. package/dist/types/session/events.js.map +1 -1
  290. package/dist/types/session/outcome.d.ts +12 -0
  291. package/dist/types/session/outcome.d.ts.map +1 -1
  292. package/dist/types/session/outcome.js.map +1 -1
  293. package/dist/types/session/records.d.ts +81 -1
  294. package/dist/types/session/records.d.ts.map +1 -1
  295. package/dist/types/session/records.js +33 -0
  296. package/dist/types/session/records.js.map +1 -1
  297. package/dist/types/tool/index.d.ts +7 -0
  298. package/dist/types/tool/index.d.ts.map +1 -1
  299. package/dist/types/tool/index.js.map +1 -1
  300. package/dist/utils/cost.d.ts +15 -0
  301. package/dist/utils/cost.d.ts.map +1 -1
  302. package/dist/utils/cost.js +24 -1
  303. package/dist/utils/cost.js.map +1 -1
  304. package/package.json +2 -2
  305. package/src/agents/examples/SupervisorAgent.ts +5 -0
  306. package/src/authorization/protected-paths.ts +11 -0
  307. package/src/authorization/readonly-commands.ts +85 -3
  308. package/src/authorization/shell-paths.ts +141 -3
  309. package/src/bridge/acp/permission.ts +6 -0
  310. package/src/bridge/acp/server.ts +20 -1
  311. package/src/compaction/context-window.ts +11 -0
  312. package/src/compaction/manual.ts +103 -17
  313. package/src/compaction/plan.ts +11 -5
  314. package/src/compaction/precompute.ts +390 -0
  315. package/src/compaction/retained-images.ts +130 -0
  316. package/src/compaction/token-estimate.ts +86 -8
  317. package/src/compaction/tool-result-editing.ts +20 -8
  318. package/src/config/runtime.ts +17 -0
  319. package/src/constants/acp/index.ts +2 -0
  320. package/src/manager/agent/lifecycle.ts +9 -0
  321. package/src/manager/session/turn-recorder.ts +92 -5
  322. package/src/peers/protocol.ts +4 -0
  323. package/src/peers/record.ts +4 -0
  324. package/src/plugin/index.ts +2 -0
  325. package/src/plugin/shell-hook.ts +60 -0
  326. package/src/pricing/catalogue.generated.ts +178 -1
  327. package/src/pricing/rates.source.json +155 -2
  328. package/src/provider/attribution.ts +1 -1
  329. package/src/public-runtime.ts +48 -0
  330. package/src/public-tools.ts +2 -0
  331. package/src/public-types.ts +8 -0
  332. package/src/rag/embedding.ts +23 -7
  333. package/src/registry/command/kernel-commands.ts +62 -3
  334. package/src/registry/tool/execute.ts +14 -0
  335. package/src/runtime/query/abandon-turn.ts +9 -0
  336. package/src/runtime/query/budget-nudge.ts +92 -0
  337. package/src/runtime/query/compact-session.ts +242 -87
  338. package/src/runtime/query/guard.ts +22 -0
  339. package/src/runtime/query/index.ts +1 -0
  340. package/src/runtime/query/iteration/index.ts +40 -1
  341. package/src/runtime/query/iteration/phases/compaction.ts +224 -46
  342. package/src/runtime/query/iteration/phases/context.ts +3 -0
  343. package/src/runtime/query/iteration/phases/precompute.ts +277 -0
  344. package/src/runtime/query/iteration/request-prefix.ts +161 -0
  345. package/src/runtime/query/review-policy.ts +79 -11
  346. package/src/runtime/query/turn/outcome-ledger.ts +34 -0
  347. package/src/sandbox/exec-stdin.ts +54 -0
  348. package/src/sandbox/file-walk.ts +25 -18
  349. package/src/sandbox/ignore-matcher.ts +1 -0
  350. package/src/sandbox/ignore-source.ts +1 -1
  351. package/src/sandbox/index.ts +8 -2
  352. package/src/sandbox/provider/local.ts +2 -0
  353. package/src/session/paths.ts +9 -0
  354. package/src/session-query/index.ts +23 -14
  355. package/src/store/evidence/disk.ts +3 -1
  356. package/src/store/evidence/index-page.ts +11 -0
  357. package/src/store/goal/gates.ts +738 -0
  358. package/src/store/goal/index.ts +109 -0
  359. package/src/store/memory/operation-lock.ts +19 -1
  360. package/src/store/session-log/core.ts +26 -0
  361. package/src/tools/builtins/bash.ts +1 -1
  362. package/src/tools/builtins/browser.ts +4 -0
  363. package/src/tools/builtins/computer-use-image.ts +20 -0
  364. package/src/tools/builtins/computer-use.ts +1 -20
  365. package/src/tools/builtins/monitor.ts +2 -0
  366. package/src/tools/builtins/search-tools.ts +2 -2
  367. package/src/tools/builtins/sleep-guard.ts +1 -1
  368. package/src/tools/builtins/wait-for-job.ts +1 -1
  369. package/src/tools/coordinator/ask-user-question.ts +37 -10
  370. package/src/tools/coordinator/index.ts +3 -0
  371. package/src/tools/defineTool.ts +5 -0
  372. package/src/tools/goal/confirmation.ts +171 -3
  373. package/src/tools/goal/index.ts +15 -3
  374. package/src/tools/memory/save.ts +2 -0
  375. package/src/tools/schedules/index.ts +1 -0
  376. package/src/tools/schedules/loop-tool.ts +2 -0
  377. package/src/tools/schedules/present.ts +2 -0
  378. package/src/tools/schedules/schedule-tool.ts +84 -9
  379. package/src/tools/schedules/self-tool.ts +152 -0
  380. package/src/tools/schedules/timer-tools.ts +2 -0
  381. package/src/tools/schedules/types.ts +62 -0
  382. package/src/toolsets/manager.ts +59 -8
  383. package/src/types/agent/supervisor.ts +11 -0
  384. package/src/types/agent/task.ts +7 -0
  385. package/src/types/common/index.ts +26 -1
  386. package/src/types/goal/index.ts +72 -0
  387. package/src/types/hitl/index.ts +11 -0
  388. package/src/types/rag/embedding.ts +7 -0
  389. package/src/types/sandbox/index.ts +19 -0
  390. package/src/types/session/events.ts +56 -0
  391. package/src/types/session/outcome.ts +16 -1
  392. package/src/types/session/records.ts +41 -0
  393. package/src/types/tool/index.ts +7 -0
  394. package/src/utils/cost.ts +44 -1
@@ -3,7 +3,7 @@ import { findSafeTrimIndex } from './dangling.js';
3
3
  import { buildGoal } from './salience/goal.js';
4
4
  import { scoreMessages } from './salience/score.js';
5
5
  import { hasEvictableMessage, planNothingToShed, planWorkingSet, } from './salience/working-set.js';
6
- import { estimateMessageTokens, estimateMessagesTokens } from './token-estimate.js';
6
+ import { estimateMessageTokensForModel, estimateMessagesTokensForModel } from './token-estimate.js';
7
7
  import { clearStaleToolResults } from './tool-result-editing.js';
8
8
  /**
9
9
  * The last index whose tail fits in `budgetTokens`, walking backwards.
@@ -22,11 +22,11 @@ import { clearStaleToolResults } from './tool-result-editing.js';
22
22
  * one boundary calculation is the failure this extraction exists to end, so
23
23
  * the phase's copy is gone and its tests point here.
24
24
  */
25
- export function naiveKeepStartByTokens(messages, budgetTokens) {
25
+ export function naiveKeepStartByTokens(messages, budgetTokens, model) {
26
26
  let tokens = 0;
27
27
  let start = messages.length;
28
28
  for (let index = messages.length - 1; index >= 0; index--) {
29
- const cost = estimateMessageTokens(messages[index]);
29
+ const cost = estimateMessageTokensForModel(messages[index], model);
30
30
  // Checked BEFORE adding, so the boundary never includes a message
31
31
  // that pushes the tail over. Adding first and trimming after would
32
32
  // admit one oversized message on every turn.
@@ -114,7 +114,8 @@ export function planCompaction(input) {
114
114
  ...(config.preserveToolResultsFrom ? { preserveTools: config.preserveToolResultsFrom } : {}),
115
115
  });
116
116
  if (edit.clearedCount > 0) {
117
- const reclaimedTokens = estimateMessagesTokens(input.messages) - estimateMessagesTokens(edit.messages);
117
+ const reclaimedTokens = estimateMessagesTokensForModel(input.messages, input.model) -
118
+ estimateMessagesTokensForModel(edit.messages, input.model);
118
119
  // NOT on a forced pass. A forced pass runs because the provider
119
120
  // REJECTED the prompt as too long, which is a measurement — and
120
121
  // answering it with the same estimate the provider just refuted
@@ -168,7 +169,7 @@ export function planCompaction(input) {
168
169
  // kills it instead. Snap the boundary backward to a safe point.
169
170
  const naiveKeepStart = config.keepRecentTokens === undefined
170
171
  ? messages.length - config.keepRecentMessages
171
- : naiveKeepStartByTokens(messages, config.keepRecentTokens);
172
+ : naiveKeepStartByTokens(messages, config.keepRecentTokens, input.model);
172
173
  let keepStart = -1;
173
174
  for (let candidate = naiveKeepStart; candidate > systemMessages.length; candidate--) {
174
175
  if (findSafeTrimIndex(messages, candidate) === candidate) {
@@ -1 +1 @@
1
- {"version":3,"file":"plan.js","sourceRoot":"","sources":["../../src/compaction/plan.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAA;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAEN,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,GACd,MAAM,2BAA2B,CAAA;AAClC,OAAO,EAAE,qBAAqB,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAA;AACnF,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAA;AAwFhE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,sBAAsB,CAAC,QAA4B,EAAE,YAAoB;IACxF,IAAI,MAAM,GAAG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E,uEAAuE;IACvE,uEAAuE;IACvE,gDAAgD;IAChD,IAAI,aAAa,CAAC,MAAM,GAAG,6BAA6B,EAAE,CAAC;QAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,CAAA;IACjD,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,cAAc,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,CAAA;AAClF,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAAC,CAAA"}
@@ -0,0 +1,179 @@
1
+ import type { TokenUsage } from '../types/common/index.js';
2
+ import { type Message } from '../types/message/index.js';
3
+ /**
4
+ * A compaction summary written before it is needed.
5
+ *
6
+ * When the context nears the trigger, one summary request starts in the
7
+ * background for the span the pass would summarise. At the trigger the pass
8
+ * installs it at once when it is still valid for the conversation as it
9
+ * stands, and writes a fresh one when it is not. Two rules hold everywhere:
10
+ *
11
+ * - A prepared summary only ever replaces the messages it was written from.
12
+ * Anything said after it was armed stays verbatim after the summary, so no
13
+ * message the summary did not see is dropped.
14
+ * - One conversation has at most one summary in flight. A pass that finds one
15
+ * waits for it instead of starting another, and a pass a person asked for
16
+ * stops it first.
17
+ *
18
+ * The store lives in the process, per session. It holds no secrets beyond the
19
+ * summary text the session would have written anyway, and is never written to
20
+ * disk.
21
+ */
22
+ /** Consecutive prepared summaries that failed or were refused before the session stops preparing them. */
23
+ export declare const PRECOMPUTE_MAX_FAILURES = 3;
24
+ /** How much of the way to the trigger a summary starts when the host names nothing else: see `armUsage`. */
25
+ export declare const PRECOMPUTE_DEFAULT_ARM_FRACTION = 0.2;
26
+ /** Why a prepared summary was not used. */
27
+ export type PrecomputeRefusal = 'boundary_missing' | 'covered_changed'
28
+ /** An assistant reply sits among the covered messages that the summary never saw. */
29
+ | 'unseen_reply'
30
+ /** The model now serving the conversation has another context window than the one the summary was sized for. */
31
+ | 'window_changed'
32
+ /** A recalled-memory entry the summary was written from is gone or different. */
33
+ | 'memory_withdrawn'
34
+ /** A tool result the summary was written from is gone or different (cleared, replaced). */
35
+ | 'tool_results_withdrawn' | 'hook_rewrite' | 'would_not_fit' | 'summary_failed';
36
+ /** Where the arm came from, for the log: a loop entry, a reply, an idle host. */
37
+ export type PrecomputeArmSource = 'estimate' | 'after_turn' | 'idle';
38
+ /** What a prepared summary cost, kept until a pass takes it so the spend lands on the turn that used it. */
39
+ export interface PreparedSpend {
40
+ readonly usage: TokenUsage;
41
+ readonly route: {
42
+ readonly providerId: string;
43
+ readonly model: string;
44
+ };
45
+ }
46
+ export interface PrecomputeEntry {
47
+ status: 'pending' | 'ready' | 'failed';
48
+ readonly source: PrecomputeArmSource;
49
+ /** The messages the summary was written from, as they were when it was armed. */
50
+ readonly covered: readonly Message[];
51
+ /**
52
+ * What each covered message said when the summary was armed, by
53
+ * {@link messageDigest}. A message edited or replaced in place under the same
54
+ * id, or the same object changed after the summary read it, no longer matches.
55
+ */
56
+ readonly coveredDigests: readonly string[];
57
+ /** The context window the summary was sized for; a model with another window refuses it. */
58
+ readonly windowTokens?: number;
59
+ /** The user message the conversation was on when it was armed. */
60
+ readonly prompt: Message | undefined;
61
+ readonly controller: AbortController;
62
+ /** Settles when the summary is ready, failed or stopped. Never rejects. */
63
+ done: Promise<void>;
64
+ summary?: string;
65
+ hookInstructions?: string;
66
+ /**
67
+ * What the request spent and no turn has booked yet. Spend that arrives while
68
+ * the arming turn is open goes onto that turn at once and never lands here.
69
+ */
70
+ readonly spend: PreparedSpend[];
71
+ }
72
+ export declare class PrecomputeStore {
73
+ #private;
74
+ get entry(): PrecomputeEntry | undefined;
75
+ get failures(): number;
76
+ /** True when a prepared summary may be started for the conversation as it stands. */
77
+ canArm(prompt: Message | undefined): boolean;
78
+ set(entry: PrecomputeEntry): void;
79
+ /** Forget the entry (it was used, refused, or the context it described is gone). */
80
+ clear(entry?: PrecomputeEntry): void;
81
+ /** A prepared summary failed or was refused: count it, and do not retry for the same prompt. */
82
+ countFailure(prompt: Message | undefined): void;
83
+ /** A prepared summary was used: the failure run ends. */
84
+ countSuccess(): void;
85
+ /**
86
+ * Stop the summary in flight and wait until it has stopped. Returns what it
87
+ * spent that no turn had booked, which the caller owes a ledger: a stopped
88
+ * summary still cost the tokens it used.
89
+ */
90
+ cancel(): Promise<readonly PreparedSpend[]>;
91
+ /** Keep spend nobody could book yet for the next pass that can. */
92
+ addUnbooked(spend: readonly PreparedSpend[]): void;
93
+ /** Take what is waiting to be booked: spend kept by {@link addUnbooked}. Empties it. */
94
+ drainUnbooked(): PreparedSpend[];
95
+ /**
96
+ * Take every unbooked cost this session holds: the ones kept by
97
+ * {@link addUnbooked} and what the prepared summary in the store spent but no
98
+ * turn booked. The summary is left in place, so a ready one can still be used;
99
+ * its spend is moved out and counted once.
100
+ */
101
+ takeSpend(): PreparedSpend[];
102
+ /** True when the store holds spend nobody has booked. */
103
+ holdsSpend(): boolean;
104
+ }
105
+ /** The store of one session, made on first use. The oldest idle stores are dropped past a bound. */
106
+ export declare function precomputeStoreFor(sessionId: string): PrecomputeStore;
107
+ /**
108
+ * End a conversation's prepared summary: stop the one in flight or waiting and
109
+ * return what it and any earlier one cost that no turn or pass booked. A host
110
+ * calls it when the conversation closes, so a summary the conversation never
111
+ * used is still on its ledger once. The session's store is forgotten.
112
+ */
113
+ export declare function releasePreparedSummary(sessionId: string): Promise<readonly PreparedSpend[]>;
114
+ /** For tests: forget every store. */
115
+ export declare function resetPrecomputeStores(): void;
116
+ /**
117
+ * The context size, as a share of the window, at which a summary starts:
118
+ * `1 - armFraction` of the window, or the trigger when that is lower.
119
+ */
120
+ export declare function armUsage(trigger: number, armFraction: number): number;
121
+ /** The same message: the same object, or the same recorded id (an edit copies the object and keeps the id). */
122
+ export declare function sameMessage(a: Message | undefined, b: Message | undefined): boolean;
123
+ /** A stable digest of a message as it reads now: role, content, tool calls, source, id, everything it carries. */
124
+ export declare function messageDigest(message: Message): string;
125
+ export type PreparedAssessment = {
126
+ readonly ok: true;
127
+ readonly keepStart: number;
128
+ } | {
129
+ readonly ok: false;
130
+ readonly reason: PrecomputeRefusal;
131
+ };
132
+ /**
133
+ * Whether a ready summary still describes the start of the conversation.
134
+ *
135
+ * `floorLength` is the count of leading system messages. The summary stands
136
+ * for the run of messages right after them: every message it covered must
137
+ * still be there, in order, and say what it said (compared by digest, not by
138
+ * identity or id). What came after stays as it is, so the new cut is just past
139
+ * the covered run.
140
+ */
141
+ export declare function assessPrepared(entry: PrecomputeEntry, live: readonly Message[], input: {
142
+ readonly floorLength: number;
143
+ readonly hookInstructions: string | undefined;
144
+ /** Size of the context after the swap, and the size it must stay under. */
145
+ readonly tokensAfterSwap: (keepStart: number, summary: string) => number;
146
+ readonly limitTokens: number;
147
+ /** The window the conversation is sized for now. */
148
+ readonly windowTokens?: number;
149
+ }): PreparedAssessment;
150
+ export type ClaimedSummary = {
151
+ readonly ok: true;
152
+ readonly summary: string;
153
+ readonly keepStart: number;
154
+ readonly spend: readonly PreparedSpend[];
155
+ } | {
156
+ readonly ok: false;
157
+ /** `none`: nothing was prepared. */
158
+ readonly reason: PrecomputeRefusal | 'none';
159
+ readonly spend: readonly PreparedSpend[];
160
+ };
161
+ /**
162
+ * Take the session's prepared summary for a pass about to write one.
163
+ *
164
+ * A summary still in flight is waited for when `awaitPending` is set (it is
165
+ * the one this pass would have written) and left alone otherwise. The entry is
166
+ * consumed either way; a refusal counts toward the failure cap, a use ends it.
167
+ * What the prepared request spent comes back for the caller to put on its
168
+ * ledger.
169
+ */
170
+ export declare function claimPrepared(store: PrecomputeStore, live: readonly Message[], input: {
171
+ readonly floorLength: number;
172
+ readonly hookInstructions: string | undefined;
173
+ readonly tokensAfterSwap: (keepStart: number, summary: string) => number;
174
+ readonly limitTokens: number;
175
+ readonly windowTokens?: number;
176
+ readonly awaitPending: boolean;
177
+ readonly waitFor?: (promise: Promise<void>) => Promise<void>;
178
+ }): Promise<ClaimedSummary>;
179
+ //# sourceMappingURL=precompute.d.ts.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a,EAAE,CAAA;CACvC,GACD;IACA,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAA;IAClB,oCAAoC;IACpC,QAAQ,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CAAA;IAC3C,QAAQ,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,CAAA;CACvC,CAAA;AAEJ;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAClC,KAAK,EAAE,eAAe,EACtB,IAAI,EAAE,SAAS,OAAO,EAAE,EACxB,KAAK,EAAE;IACN,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,gBAAgB,EAAE,MAAM,GAAG,SAAS,CAAA;IAC7C,QAAQ,CAAC,eAAe,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,CAAA;IACxE,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAA;IAC9B,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAA;IAC9B,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAC5D,GACC,OAAO,CAAC,cAAc,CAAC,CAoBzB"}
@@ -0,0 +1,274 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { RECALLED_MEMORY_ENTRY_KEY } from '../turn/memory-recall.js';
3
+ import { turnContextKey } from '../types/message/index.js';
4
+ /**
5
+ * A compaction summary written before it is needed.
6
+ *
7
+ * When the context nears the trigger, one summary request starts in the
8
+ * background for the span the pass would summarise. At the trigger the pass
9
+ * installs it at once when it is still valid for the conversation as it
10
+ * stands, and writes a fresh one when it is not. Two rules hold everywhere:
11
+ *
12
+ * - A prepared summary only ever replaces the messages it was written from.
13
+ * Anything said after it was armed stays verbatim after the summary, so no
14
+ * message the summary did not see is dropped.
15
+ * - One conversation has at most one summary in flight. A pass that finds one
16
+ * waits for it instead of starting another, and a pass a person asked for
17
+ * stops it first.
18
+ *
19
+ * The store lives in the process, per session. It holds no secrets beyond the
20
+ * summary text the session would have written anyway, and is never written to
21
+ * disk.
22
+ */
23
+ /** Consecutive prepared summaries that failed or were refused before the session stops preparing them. */
24
+ export const PRECOMPUTE_MAX_FAILURES = 3;
25
+ /** How much of the way to the trigger a summary starts when the host names nothing else: see `armUsage`. */
26
+ export const PRECOMPUTE_DEFAULT_ARM_FRACTION = 0.2;
27
+ export class PrecomputeStore {
28
+ #entry;
29
+ /** Spend of prepared summaries that were dropped or outlived their turn, waiting for a pass to book it. */
30
+ #unbooked = [];
31
+ #failures = 0;
32
+ #vetoed;
33
+ get entry() {
34
+ return this.#entry;
35
+ }
36
+ get failures() {
37
+ return this.#failures;
38
+ }
39
+ /** True when a prepared summary may be started for the conversation as it stands. */
40
+ canArm(prompt) {
41
+ if (this.#entry)
42
+ return false;
43
+ if (this.#failures >= PRECOMPUTE_MAX_FAILURES)
44
+ return false;
45
+ return !(prompt !== undefined && this.#vetoed === prompt);
46
+ }
47
+ set(entry) {
48
+ this.#entry = entry;
49
+ }
50
+ /** Forget the entry (it was used, refused, or the context it described is gone). */
51
+ clear(entry) {
52
+ if (entry === undefined || this.#entry === entry)
53
+ this.#entry = undefined;
54
+ }
55
+ /** A prepared summary failed or was refused: count it, and do not retry for the same prompt. */
56
+ countFailure(prompt) {
57
+ this.#failures++;
58
+ this.#vetoed = prompt;
59
+ }
60
+ /** A prepared summary was used: the failure run ends. */
61
+ countSuccess() {
62
+ this.#failures = 0;
63
+ this.#vetoed = undefined;
64
+ }
65
+ /**
66
+ * Stop the summary in flight and wait until it has stopped. Returns what it
67
+ * spent that no turn had booked, which the caller owes a ledger: a stopped
68
+ * summary still cost the tokens it used.
69
+ */
70
+ async cancel() {
71
+ const entry = this.#entry;
72
+ if (!entry)
73
+ return [];
74
+ this.#entry = undefined;
75
+ entry.controller.abort();
76
+ await entry.done;
77
+ const spend = [...entry.spend];
78
+ entry.spend.length = 0;
79
+ return spend;
80
+ }
81
+ /** Keep spend nobody could book yet for the next pass that can. */
82
+ addUnbooked(spend) {
83
+ this.#unbooked.push(...spend);
84
+ }
85
+ /** Take what is waiting to be booked: spend kept by {@link addUnbooked}. Empties it. */
86
+ drainUnbooked() {
87
+ return this.#unbooked.splice(0);
88
+ }
89
+ /**
90
+ * Take every unbooked cost this session holds: the ones kept by
91
+ * {@link addUnbooked} and what the prepared summary in the store spent but no
92
+ * turn booked. The summary is left in place, so a ready one can still be used;
93
+ * its spend is moved out and counted once.
94
+ */
95
+ takeSpend() {
96
+ const taken = this.drainUnbooked();
97
+ const entry = this.#entry;
98
+ if (entry && entry.status !== 'pending')
99
+ taken.push(...entry.spend.splice(0));
100
+ return taken;
101
+ }
102
+ /** True when the store holds spend nobody has booked. */
103
+ holdsSpend() {
104
+ return this.#unbooked.length > 0 || (this.#entry?.spend.length ?? 0) > 0;
105
+ }
106
+ }
107
+ const stores = new Map();
108
+ const MAX_STORES = 32;
109
+ /** The store of one session, made on first use. The oldest idle stores are dropped past a bound. */
110
+ export function precomputeStoreFor(sessionId) {
111
+ let store = stores.get(sessionId);
112
+ if (store) {
113
+ stores.delete(sessionId);
114
+ stores.set(sessionId, store);
115
+ return store;
116
+ }
117
+ store = new PrecomputeStore();
118
+ stores.set(sessionId, store);
119
+ for (const [id, old] of stores) {
120
+ if (stores.size <= MAX_STORES)
121
+ break;
122
+ if (id !== sessionId && old.entry?.status !== 'pending' && !old.holdsSpend())
123
+ stores.delete(id);
124
+ }
125
+ return store;
126
+ }
127
+ /**
128
+ * End a conversation's prepared summary: stop the one in flight or waiting and
129
+ * return what it and any earlier one cost that no turn or pass booked. A host
130
+ * calls it when the conversation closes, so a summary the conversation never
131
+ * used is still on its ledger once. The session's store is forgotten.
132
+ */
133
+ export async function releasePreparedSummary(sessionId) {
134
+ const store = stores.get(sessionId);
135
+ if (!store)
136
+ return [];
137
+ const stopped = await store.cancel();
138
+ const spend = [...store.drainUnbooked(), ...stopped];
139
+ stores.delete(sessionId);
140
+ return spend;
141
+ }
142
+ /** For tests: forget every store. */
143
+ export function resetPrecomputeStores() {
144
+ stores.clear();
145
+ }
146
+ /**
147
+ * The context size, as a share of the window, at which a summary starts:
148
+ * `1 - armFraction` of the window, or the trigger when that is lower.
149
+ */
150
+ export function armUsage(trigger, armFraction) {
151
+ return Math.min(1 - armFraction, trigger);
152
+ }
153
+ /** The same message: the same object, or the same recorded id (an edit copies the object and keeps the id). */
154
+ export function sameMessage(a, b) {
155
+ if (a === undefined || b === undefined)
156
+ return false;
157
+ if (a === b)
158
+ return true;
159
+ return a.id !== undefined && a.id === b.id && a.role === b.role;
160
+ }
161
+ /** `value` as JSON with object keys in a fixed order, so two equal messages digest alike whatever order they were built in. */
162
+ function canonicalJson(value) {
163
+ return JSON.stringify(value, (_key, nested) => {
164
+ if (typeof nested !== 'object' || nested === null || Array.isArray(nested))
165
+ return nested;
166
+ const sorted = {};
167
+ for (const key of Object.keys(nested).sort())
168
+ sorted[key] = nested[key];
169
+ return sorted;
170
+ });
171
+ }
172
+ /** A stable digest of a message as it reads now: role, content, tool calls, source, id, everything it carries. */
173
+ export function messageDigest(message) {
174
+ return createHash('sha256')
175
+ .update(canonicalJson(message) ?? 'null')
176
+ .digest('hex');
177
+ }
178
+ /** Why the covered message at `index` is no longer what the summary read. */
179
+ function changedReason(entry, live, floorLength, index) {
180
+ const covered = entry.covered;
181
+ const wanted = covered[index];
182
+ const here = live[floorLength + index];
183
+ // An assistant reply the summary never saw, in the middle of what it covers.
184
+ if (here?.role === 'assistant' &&
185
+ !covered.some((message) => sameMessage(message, here)) &&
186
+ live.some((message, at) => at > floorLength + index && sameMessage(message, wanted))) {
187
+ return 'unseen_reply';
188
+ }
189
+ return withdrawnReason(wanted) ?? 'covered_changed';
190
+ }
191
+ /** The reason that names what a covered message was, when what it carried is the kind a host withdraws. */
192
+ function withdrawnReason(message) {
193
+ if (message === undefined)
194
+ return undefined;
195
+ if (turnContextKey(message) === RECALLED_MEMORY_ENTRY_KEY)
196
+ return 'memory_withdrawn';
197
+ if (message.role === 'tool')
198
+ return 'tool_results_withdrawn';
199
+ return undefined;
200
+ }
201
+ /**
202
+ * Whether a ready summary still describes the start of the conversation.
203
+ *
204
+ * `floorLength` is the count of leading system messages. The summary stands
205
+ * for the run of messages right after them: every message it covered must
206
+ * still be there, in order, and say what it said (compared by digest, not by
207
+ * identity or id). What came after stays as it is, so the new cut is just past
208
+ * the covered run.
209
+ */
210
+ export function assessPrepared(entry, live, input) {
211
+ if (entry.status !== 'ready' || entry.summary === undefined) {
212
+ return { ok: false, reason: 'summary_failed' };
213
+ }
214
+ const covered = entry.covered;
215
+ if (covered.length === 0 || !live.some((message) => sameMessage(message, covered[0]))) {
216
+ return { ok: false, reason: withdrawnReason(covered[0]) ?? 'boundary_missing' };
217
+ }
218
+ if (entry.windowTokens !== undefined &&
219
+ input.windowTokens !== undefined &&
220
+ entry.windowTokens !== input.windowTokens) {
221
+ return { ok: false, reason: 'window_changed' };
222
+ }
223
+ for (let i = 0; i < covered.length; i++) {
224
+ const here = live[input.floorLength + i];
225
+ if (here === undefined ||
226
+ !sameMessage(here, covered[i]) ||
227
+ messageDigest(here) !== entry.coveredDigests[i]) {
228
+ return { ok: false, reason: changedReason(entry, live, input.floorLength, i) };
229
+ }
230
+ }
231
+ if (entry.hookInstructions !== input.hookInstructions) {
232
+ return { ok: false, reason: 'hook_rewrite' };
233
+ }
234
+ const keepStart = input.floorLength + covered.length;
235
+ if (input.tokensAfterSwap(keepStart, entry.summary) >= input.limitTokens) {
236
+ return { ok: false, reason: 'would_not_fit' };
237
+ }
238
+ return { ok: true, keepStart };
239
+ }
240
+ /**
241
+ * Take the session's prepared summary for a pass about to write one.
242
+ *
243
+ * A summary still in flight is waited for when `awaitPending` is set (it is
244
+ * the one this pass would have written) and left alone otherwise. The entry is
245
+ * consumed either way; a refusal counts toward the failure cap, a use ends it.
246
+ * What the prepared request spent comes back for the caller to put on its
247
+ * ledger.
248
+ */
249
+ export async function claimPrepared(store, live, input) {
250
+ const entry = store.entry;
251
+ if (!entry)
252
+ return { ok: false, reason: 'none', spend: [] };
253
+ if (entry.status === 'pending') {
254
+ if (!input.awaitPending)
255
+ return { ok: false, reason: 'none', spend: [] };
256
+ await (input.waitFor ? input.waitFor(entry.done) : entry.done);
257
+ }
258
+ // Another pass may have taken it while this one waited.
259
+ if (store.entry !== entry)
260
+ return { ok: false, reason: 'none', spend: [] };
261
+ store.clear(entry);
262
+ const spend = [...entry.spend];
263
+ entry.spend.length = 0;
264
+ if (entry.status !== 'ready')
265
+ return { ok: false, reason: 'summary_failed', spend };
266
+ const assessment = assessPrepared(entry, live, input);
267
+ if (!assessment.ok) {
268
+ store.countFailure(entry.prompt);
269
+ return { ok: false, reason: assessment.reason, spend };
270
+ }
271
+ store.countSuccess();
272
+ return { ok: true, summary: entry.summary, keepStart: assessment.keepStart, spend };
273
+ }
274
+ //# sourceMappingURL=precompute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"precompute.js","sourceRoot":"","sources":["../../src/compaction/precompute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,EAAE,yBAAyB,EAAE,MAAM,0BAA0B,CAAA;AAEpE,OAAO,EAAgB,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAExE;;;;;;;;;;;;;;;;;;GAkBG;AAEH,0GAA0G;AAC1G,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAA;AAExC,4GAA4G;AAC5G,MAAM,CAAC,MAAM,+BAA+B,GAAG,GAAG,CAAA;AAsDlD,MAAM,OAAO,eAAe;IAC3B,MAAM,CAA6B;IACnC,2GAA2G;IAClG,SAAS,GAAoB,EAAE,CAAA;IACxC,SAAS,GAAG,CAAC,CAAA;IACb,OAAO,CAAqB;IAE5B,IAAI,KAAK;QACR,OAAO,IAAI,CAAC,MAAM,CAAA;I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AM,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,CAAA;IACvD,CAAC;IACD,KAAK,CAAC,YAAY,EAAE,CAAA;IACpB,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAiB,EAAE,SAAS,EAAE,UAAU,CAAC,SAAS,EAAE,KAAK,EAAE,CAAA;AAC9F,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { Message, MessageAttachment, ToolResultBlock } from '../types/message/index.js';
2
+ type RichBlock = MessageAttachment | Exclude<ToolResultBlock, {
3
+ type: 'text';
4
+ }>;
5
+ export declare function compactedImageMarker(block: RichBlock, source: string): string;
6
+ /**
7
+ * Fit images around the retained text and the summary. Keep a small recent
8
+ * set, including when a single tool call returned a batch of images.
9
+ * Replacements leave the producing call, caption and result ID in place.
10
+ */
11
+ export declare function fitRetainedImages(messages: Message[], tokenLimit: number, model?: string, tail?: {
12
+ readonly start: number;
13
+ readonly tokens: number;
14
+ }): Message[];
15
+ export {};
16
+ //# sourceMappingURL=retained-images.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retained-images.d.ts","sourceRoot":"","sources":["../../src/compaction/retained-images.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAW5F,KAAK,SAAS,GAAG,iBAAiB,GAAG,OAAO,CAAC,eAAe,EAAE;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAAA;AAU/E,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAG7E;AAED;;;;GAIG;AACH,wBAAgB,iBAAiB,CAChC,QAAQ,EAAE,OAAO,EAAE,EACnB,UAAU,EAAE,MAAM,EAClB,KAAK,CAAC,EAAE,MAAM,EACd,IAAI,CAAC,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GACxD,OAAO,EAAE,CA6FX"}