@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
@@ -117,6 +117,14 @@ export interface ShellPathUse {
117
117
  * literal relative directory is followed; anything else is `unknown`.
118
118
  */
119
119
  readonly directory?: ShellDirectory
120
+ /**
121
+ * True for a path the reading derived rather than read: the file a copy or
122
+ * move into a directory would create there, which the line names only by its
123
+ * directory. The caller checks it the same way as any other write, but
124
+ * without treating its absence as proof that nothing was written at that
125
+ * name, because the reading never looked at the disk.
126
+ */
127
+ readonly candidate?: true
120
128
  }
121
129
 
122
130
  /** Where a `cd` earlier in the line left the shell, relative to where the line began. */
@@ -208,11 +216,25 @@ function isLongOption(name: string, full: string, minimum: number): boolean {
208
216
  * prefixes below are the shortest that no other option of `cp`, `mv` or
209
217
  * `install` shares: `--t` for `--target-directory`, `--su` for `--suffix`,
210
218
  * `--l` for `--link`, `--sy` for `--symbolic-link`. `--reflink` is a copy.
219
+ *
220
+ * `targetDirectory` is the value of `-t`/`--target-directory` when the line
221
+ * gave one: that word is a directory by definition, which the destination
222
+ * operand alone does not say. `--parents` keeps each source's own path under
223
+ * that directory, and `-T`/`--no-target-directory` says the destination is the
224
+ * file itself, so neither derives a name inside a directory.
211
225
  */
212
- function readCopyArguments(args: Args): { operands: CopyOperand[]; links: boolean } {
226
+ function readCopyArguments(args: Args): {
227
+ operands: CopyOperand[]
228
+ links: boolean
229
+ targetDirectory?: ShellWord
230
+ parents: boolean
231
+ noTargetDirectory: boolean
232
+ } {
213
233
  const out: CopyOperand[] = []
214
234
  let links = false
215
235
  let target: ShellWord | undefined
236
+ let parents = false
237
+ let noTargetDirectory = false
216
238
  let afterDashes = false
217
239
  for (let i = 0; i < args.length; i += 1) {
218
240
  const arg = args[i] as ShellWord
@@ -237,11 +259,14 @@ function readCopyArguments(args: Args): { operands: CopyOperand[]; links: boolea
237
259
  } else if (isLongOption(name, '--suffix', 4) && eq < 0) i += 1
238
260
  else if (isLongOption(name, '--link', 3) || isLongOption(name, '--symbolic-link', 4))
239
261
  links = true
262
+ else if (isLongOption(name, '--parents', 4) && eq < 0) parents = true
263
+ else if (isLongOption(name, '--no-target-directory', 6) && eq < 0) noTargetDirectory = true
240
264
  continue
241
265
  }
242
266
  for (let j = 1; j < text.length; j += 1) {
243
267
  const letter = text[j] as string
244
268
  if (letter === 'l' || letter === 's') links = true
269
+ if (letter === 'T') noTargetDirectory = true
245
270
  if (letter !== 't' && letter !== 'S') continue
246
271
  const rest = text.slice(j + 1)
247
272
  if (letter === 't') {
@@ -256,11 +281,104 @@ function readCopyArguments(args: Args): { operands: CopyOperand[]; links: boolea
256
281
  }
257
282
  if (target !== undefined) {
258
283
  out.push({ word: target, destination: true })
259
- return { operands: out, links }
284
+ return { operands: out, links, targetDirectory: target, parents, noTargetDirectory }
260
285
  }
261
286
  const last = out[out.length - 1]
262
287
  if (last !== undefined) out[out.length - 1] = { word: last.word, destination: true }
263
- return { operands: out, links }
288
+ return { operands: out, links, parents, noTargetDirectory }
289
+ }
290
+
291
+ /**
292
+ * Whether a word's spelling alone says it is a directory, with no look at the
293
+ * disk: a trailing slash, or a final `.`/`..` segment.
294
+ *
295
+ * Only `/` separates, because only POSIX shell text reaches this: the reader
296
+ * reads `bash` or `sh`, where a backslash is an ordinary character of a name,
297
+ * so `dir\` is a file called `dir\` and not a directory. The `cmd` dialect
298
+ * names no operands here.
299
+ */
300
+ function spelledAsDirectory(value: string): boolean {
301
+ if (value.endsWith('/')) return true
302
+ return /(?:^|\/)\.\.?$/.test(value)
303
+ }
304
+
305
+ /** `value` without its trailing slashes: a word ending in `/` names the directory before it. */
306
+ function withoutTrailingSlashes(value: string): string {
307
+ let end = value.length
308
+ while (end > 0 && value[end - 1] === '/') end -= 1
309
+ return value.slice(0, end)
310
+ }
311
+
312
+ /**
313
+ * The files a copy or move into a directory would create there: the target
314
+ * directory joined to each source's base name.
315
+ *
316
+ * Whether the destination is a directory cannot be read from one source and a
317
+ * destination alone — `cp a b` may put a file at `b` or copy `a` into a
318
+ * directory `b` — so only the spellings that say it on their own are followed:
319
+ * the value of `-t`/`--target-directory`, which is a directory by definition, a
320
+ * destination with `--parents`, which GNU also requires to be a directory, a
321
+ * destination written with a trailing slash or a final `.`/`..`, and a bare
322
+ * destination operand with more than one source, which GNU accepts only as a
323
+ * directory. Each file is
324
+ * derived, not read: the caller gets it as a candidate and checks the name
325
+ * without treating its absence as proof. A source whose name is decided at run
326
+ * time names no file, so it contributes none.
327
+ *
328
+ * `cp -T`/`--no-target-directory` says the destination is the file itself, so
329
+ * nothing is derived. `--parents` keeps the source's own path under the
330
+ * directory (`cp --parents a/b c/` writes `c/a/b`), so a `..` segment, whose
331
+ * landing place the word alone does not say, derives nothing.
332
+ *
333
+ * The names are POSIX: only `/` separates, and a backslash is an ordinary
334
+ * character of one, so a trailing backslash neither says "directory" nor is
335
+ * dropped from a name.
336
+ */
337
+ function copyDestinationFiles(
338
+ operands: readonly CopyOperand[],
339
+ targetDirectory: ShellWord | undefined,
340
+ parents: boolean,
341
+ noTargetDirectory: boolean,
342
+ ): ShellWord[] {
343
+ const destination = operands.find((operand) => operand.destination)?.word
344
+ const directory = targetDirectory ?? destination
345
+ if (directory === undefined || noTargetDirectory) return []
346
+ // GNU refuses to copy several sources anywhere but a directory, so a bare
347
+ // destination operand beside more than one source names one too.
348
+ const sources = operands.reduce((n, operand) => (operand.destination ? n : n + 1), 0)
349
+ const saysDirectory =
350
+ parents || targetDirectory !== undefined || sources > 1 || spelledAsDirectory(directory.value)
351
+ if (!saysDirectory) return []
352
+ // A trailing `/` or `/.` is how a word says "directory" and nothing else;
353
+ // `..` is a segment of its own and stays.
354
+ const trimmed = withoutTrailingSlashes(directory.value)
355
+ const base = trimmed.endsWith('/.') ? trimmed.slice(0, -2) : trimmed
356
+ const out: ShellWord[] = []
357
+ for (const operand of operands) {
358
+ if (operand.destination) continue
359
+ const source = operand.word
360
+ if (source.value === '' || expandsAtRuntime(source)) continue
361
+ const name = parents
362
+ ? parentedName(source.value)
363
+ : basename(withoutTrailingSlashes(source.value))
364
+ if (name === undefined || name === '' || name === '.' || name === '..') continue
365
+ out.push(derived(directory, `${base}/${name}`))
366
+ }
367
+ return out
368
+ }
369
+
370
+ /**
371
+ * The name `cp --parents` gives a source under the destination directory: the
372
+ * source's own path with trailing slashes removed and a leading `/` or `./`
373
+ * dropped, so `cp --parents a/b dest/` writes `dest/a/b`. A `..` segment is
374
+ * not followed, because where it lands cannot be read from the word alone.
375
+ */
376
+ function parentedName(value: string): string | undefined {
377
+ let name = withoutTrailingSlashes(value)
378
+ while (name.startsWith('/')) name = name.slice(1)
379
+ while (name.startsWith('./')) name = name.slice(2)
380
+ if (name === '' || name.split('/').includes('..')) return undefined
381
+ return name
264
382
  }
265
383
 
266
384
  /**
@@ -1505,6 +1623,26 @@ export function shellPathAccess(reading: ShellReading): ShellPathAccess {
1505
1623
  ...(directory !== undefined ? { directory } : {}),
1506
1624
  })
1507
1625
  }
1626
+ // A copy into a directory writes a file inside it that the line names
1627
+ // only by its directory: report that file beside the directory, as a
1628
+ // candidate, so a protected name that matches it still asks instead of
1629
+ // being bypassed by naming the directory.
1630
+ for (const file of copyDestinationFiles(
1631
+ copy.operands,
1632
+ copy.targetDirectory,
1633
+ copy.parents,
1634
+ copy.noTargetDirectory,
1635
+ )) {
1636
+ if (file.value === '' || isHarmlessTarget(file.value)) continue
1637
+ uses.push({
1638
+ path: file.value,
1639
+ access: 'write',
1640
+ via: program,
1641
+ unresolved: expandsAtRuntime(file),
1642
+ candidate: true,
1643
+ ...(directory !== undefined ? { directory } : {}),
1644
+ })
1645
+ }
1508
1646
  }
1509
1647
  if (program === 'touch') {
1510
1648
  for (const arg of touchReferences(args)) {
@@ -20,6 +20,12 @@ export interface AcpPermissionRequest {
20
20
  * name with the question; the answer goes back to that teammate's call only.
21
21
  */
22
22
  readonly teammate?: { readonly name: string; readonly team: string }
23
+ /**
24
+ * Aborts when the question stops being open on the agent's side: the person wrote a message
25
+ * instead of answering, or the turn was stopped. The client is told to take the question off
26
+ * screen (`$/cancel_request`) and any answer that still arrives is ignored.
27
+ */
28
+ readonly signal?: AbortSignal
23
29
  readonly toolCalls: readonly (ToolCallSummary & {
24
30
  /** Rides to the client untouched; see {@link AcpFileChangePreview}. */
25
31
  readonly preview?: AcpFileChangePreview
@@ -379,6 +379,13 @@ export class ACPServer {
379
379
  method: string,
380
380
  params: Record<string, unknown>,
381
381
  signal?: AbortSignal,
382
+ /**
383
+ * The turn's own signal, when the request is a question to the person. A request taken back
384
+ * while the turn goes on is told to the client (`$/cancel_request`) so its card comes down;
385
+ * one that ends because the turn stopped is not, since the stop settles the client's cards
386
+ * itself and says which actions never ran.
387
+ */
388
+ turn?: AbortSignal,
382
389
  ): Promise<T> {
383
390
  this.requestSeq += 1
384
391
  const id = `agent_${this.requestSeq}`
@@ -390,6 +397,14 @@ export class ACPServer {
390
397
  return true
391
398
  }
392
399
  const onAbort = (): void => {
400
+ // The client is told the question is closed, so a card it shows comes down. The
401
+ // protocol's own cancellation notice; a client that does not know it ignores it.
402
+ if (this.pending.get(id) === entry && !this.stopped && turn?.aborted === false)
403
+ void this.send({
404
+ jsonrpc: '2.0',
405
+ method: ACP_CLIENT_NOTIFICATIONS.CANCEL_REQUEST,
406
+ params: { requestId: id },
407
+ }).catch(() => {})
393
408
  const reason =
394
409
  signal?.reason instanceof Error
395
410
  ? signal.reason
@@ -850,6 +865,9 @@ export class ACPServer {
850
865
  signal: AbortSignal,
851
866
  ): Promise<AcpPermissionOutcome> {
852
867
  signal.throwIfAborted()
868
+ // Closed by the turn stopping or by the question itself being withdrawn.
869
+ const asking = request.signal ? AbortSignal.any([signal, request.signal]) : signal
870
+ asking.throwIfAborted()
853
871
  // The latch, read from the SESSION. Checked here rather than in the
854
872
  // gateway so no gateway can forget it.
855
873
  // A call marked `alwaysAsk` exists to put a question to a person, so the
@@ -866,9 +884,10 @@ export class ACPServer {
866
884
  toolCalls: request.toolCalls,
867
885
  ...(request.teammate ? { teammate: request.teammate } : {}),
868
886
  },
887
+ asking,
869
888
  signal,
870
889
  )
871
- signal.throwIfAborted()
890
+ asking.throwIfAborted()
872
891
 
873
892
  switch (answer?.outcome) {
874
893
  case 'approve':
@@ -174,6 +174,17 @@ const WINDOWS: ReadonlyArray<readonly [prefix: string, tokens: number]> = [
174
174
  ['claude-3-opus', 200_000],
175
175
  ['claude-', 200_000],
176
176
  ['gpt-5', 400_000],
177
+ // The gpt-6 API models state a 1,050,000-token window (922,000 input, 128,000
178
+ // output) on each model's page, e.g.
179
+ // https://developers.openai.com/api/docs/models/gpt-6-sol; every id the API
180
+ // lists is named here rather than matched by a bare `gpt-6`.
181
+ ['gpt-6-astra', 1_050_000],
182
+ ['gpt-6-luna', 1_050_000],
183
+ ['gpt-6-sol', 1_050_000],
184
+ ['gpt-6.1-sol', 1_050_000],
185
+ ['gpt-6.1-terra', 1_050_000],
186
+ ['gpt-6.2-astra', 1_050_000],
187
+ ['gpt-6.2-sol', 1_050_000],
177
188
  ['gpt-4.1', 1_047_576],
178
189
  ['gpt-4o', 128_000],
179
190
  ['gpt-4-turbo', 128_000],
@@ -1,5 +1,5 @@
1
1
  import type { CompactionConfig } from '../config/runtime.js'
2
- import { EMPTY_TOKEN_USAGE } from '../constants/limits.js'
2
+ import { CHARS_PER_TOKEN, EMPTY_TOKEN_USAGE } from '../constants/limits.js'
3
3
  import { resolveStreamIdleTimeoutMs } from '../provider/idle-timeout.js'
4
4
  import { isOperatorUserMessage } from '../runtime/query/steering.js'
5
5
  import { type TokenUsage, accumulateTokenUsage } from '../types/common/index.js'
@@ -31,6 +31,7 @@ import {
31
31
  summarizeWithModel,
32
32
  } from './model-summary.js'
33
33
  import { planCompaction } from './plan.js'
34
+ import { type PreparedSpend, claimPrepared, precomputeStoreFor } from './precompute.js'
34
35
  import {
35
36
  carriedWorkingCommands,
36
37
  collectToolsUsed,
@@ -38,9 +39,10 @@ import {
38
39
  recordRestorable,
39
40
  renderRestoredContext,
40
41
  } from './restore.js'
42
+ import { fitRetainedImages } from './retained-images.js'
41
43
  import { findRetainedIndices } from './retention.js'
42
44
  import { buildCompactionMessage, collectRevealedTools, isCompactionMessage } from './summary.js'
43
- import { estimateMessagesTokens } from './token-estimate.js'
45
+ import { estimateMessagesTokensForModel } from './token-estimate.js'
44
46
  import { type CompactionVerificationOptions, buildVerifiedSummary } from './verifier.js'
45
47
 
46
48
  /**
@@ -65,13 +67,17 @@ export interface CompactionResult {
65
67
  /** The summary that replaced them, as it appears in `messages`. */
66
68
  readonly summary: Message
67
69
  /**
68
- * Tokens spent by this host-triggered pass's optional verifier call.
70
+ * Tokens spent by this host-triggered pass: its optional verifier call and any
71
+ * summary prepared ahead (see `precompute.ts`) that this pass used, refused or
72
+ * stopped.
69
73
  *
70
74
  * A zero record means no verifier model call ran. The manual path has no
71
75
  * `TurnRecorder` to account for side-channel usage, so returning it is the
72
76
  * only way a host can include that real provider work in its own ledger.
73
77
  */
74
78
  readonly usage: TokenUsage
79
+ /** True when the summary was written ahead of time (see `precompute.ts`) and installed at once. */
80
+ readonly precomputed?: boolean
75
81
  }
76
82
 
77
83
  export interface CompactNowInput extends CompactionVerificationOptions {
@@ -115,6 +121,14 @@ export interface CompactNowInput extends CompactionVerificationOptions {
115
121
  * `instructions`. Absent, no hook runs.
116
122
  */
117
123
  readonly hooks?: ManualCompactionHooks
124
+ /**
125
+ * Use the summary prepared ahead for this session when it is still valid.
126
+ * The prepared one in flight is waited for. `only` makes a refusal a no-op
127
+ * (`null`) instead of writing a fresh summary: for a host that wants the
128
+ * instant swap or nothing. Absent, a prepared summary is neither used nor
129
+ * left running (the caller stops it).
130
+ */
131
+ readonly prepared?: { readonly sessionId: string; readonly only?: boolean }
118
132
  }
119
133
 
120
134
  /** The model-written summary of `span` for a host-triggered pass, if one can be had. */
@@ -157,7 +171,7 @@ async function beforePass(
157
171
  reason: 'manual',
158
172
  trigger: hooks.trigger === 'idle' ? 'auto' : 'manual',
159
173
  instructions: hooks.trigger === 'idle' ? null : input.instructions?.trim() || null,
160
- tokensBefore: estimateMessagesTokens(input.messages),
174
+ tokensBefore: estimateMessagesTokensForModel(input.messages, input.model),
161
175
  contextWindowTokens: windowTokens,
162
176
  },
163
177
  )
@@ -189,8 +203,8 @@ async function afterPass(
189
203
  compaction: {
190
204
  reason: 'manual',
191
205
  trigger: hooks.trigger === 'idle' ? 'auto' : 'manual',
192
- tokensBefore: estimateMessagesTokens(input.messages),
193
- tokensAfter: estimateMessagesTokens(result.messages),
206
+ tokensBefore: estimateMessagesTokensForModel(input.messages, input.model),
207
+ tokensAfter: estimateMessagesTokensForModel(result.messages, input.model),
194
208
  contextWindowTokens: windowTokens,
195
209
  ...postCompactSummary(messageText(result.summary.content)),
196
210
  },
@@ -373,6 +387,7 @@ export async function compactNow(input: CompactNowInput): Promise<CompactionResu
373
387
  config: input.config,
374
388
  contextWindowTokens: window.tokens,
375
389
  estimatedTokens: window.tokens,
390
+ ...(input.model ? { model: input.model } : {}),
376
391
  force: true,
377
392
  allowNoSystemFloor: true,
378
393
  skipToolResultClear: true,
@@ -380,12 +395,12 @@ export async function compactNow(input: CompactNowInput): Promise<CompactionResu
380
395
  if (plan.kind !== 'plan') return null
381
396
 
382
397
  const retained = findRetainedIndices(input.messages)
383
- const retainedOlder = plan.olderMessages.filter((_, offset) =>
384
- retained.has(plan.systemMessages.length + offset),
385
- )
398
+ const retainedOlderOf = (older: readonly Message[], floor: number): Message[] =>
399
+ older.filter((_, offset) => retained.has(floor + offset))
400
+ const retainedOlderPlanned = retainedOlderOf(plan.olderMessages, plan.systemMessages.length)
386
401
  const preservedSystem = preservedSystemFloor(plan.systemMessages)
387
402
  const projectedLength =
388
- preservedSystem.length + 1 + retainedOlder.length + plan.recentMessages.length
403
+ preservedSystem.length + 1 + retainedOlderPlanned.length + plan.recentMessages.length
389
404
  // Decide before building a summary (and before a possible verifier call).
390
405
  // A pinned older message remains verbatim, so replacing one removable
391
406
  // message with one summary has shed nothing and must be reported as a no-op.
@@ -395,24 +410,84 @@ export async function compactNow(input: CompactNowInput): Promise<CompactionResu
395
410
  if (hooked === 'cancelled') return null
396
411
  const hookInstructions = hooked.instructions
397
412
 
398
- const manager = new WorkingStateManager(input.config)
399
- populateWorkingState(manager, plan.olderMessages, input.config)
400
413
  let usage: TokenUsage = { ...EMPTY_TOKEN_USAGE }
401
414
  const onUsage = (reported: TokenUsage) => {
402
415
  usage = accumulateTokenUsage(usage, reported)
403
416
  }
417
+ // A summary prepared ahead stands in for the one written here when it still
418
+ // describes the start of the history; what came after stays verbatim.
419
+ let olderMessages = plan.olderMessages
420
+ let recentMessages = plan.recentMessages
421
+ let retainedOlder = retainedOlderPlanned
422
+ let preparedBody: string | undefined
423
+ // What the prepared summary cost is in `usage` once the pass produces a result;
424
+ // a pass that ends without one leaves it with the session for the next to book.
425
+ let claimedSpend: readonly PreparedSpend[] = []
426
+ const keepSpend = (): void => {
427
+ if (input.prepared) precomputeStoreFor(input.prepared.sessionId).addUnbooked(claimedSpend)
428
+ }
429
+ if (input.prepared) {
430
+ const claimed = await claimPrepared(
431
+ precomputeStoreFor(input.prepared.sessionId),
432
+ input.messages,
433
+ {
434
+ floorLength: plan.systemMessages.length,
435
+ hookInstructions,
436
+ awaitPending: true,
437
+ // The swap must leave the history smaller than it is.
438
+ limitTokens: estimateMessagesTokensForModel(input.messages, input.model),
439
+ // Only a window the host stated: the one resolved here may not know the
440
+ // provider's own figure the summary was sized for, and a guess must not
441
+ // refuse a summary that is still good.
442
+ ...(input.contextWindowTokens !== undefined
443
+ ? { windowTokens: input.contextWindowTokens }
444
+ : {}),
445
+ tokensAfterSwap: (keepStart, summary) =>
446
+ estimateMessagesTokensForModel(
447
+ input.messages.slice(0, plan.systemMessages.length),
448
+ input.model,
449
+ ) +
450
+ estimateMessagesTokensForModel(input.messages.slice(keepStart), input.model) +
451
+ Math.ceil(summary.length / CHARS_PER_TOKEN),
452
+ },
453
+ )
454
+ claimedSpend = claimed.spend
455
+ for (const { usage: spent } of claimed.spend) onUsage(spent)
456
+ if (claimed.ok) {
457
+ olderMessages = input.messages.slice(plan.systemMessages.length, claimed.keepStart)
458
+ recentMessages = input.messages.slice(claimed.keepStart)
459
+ retainedOlder = retainedOlderOf(olderMessages, plan.systemMessages.length)
460
+ preparedBody = claimed.summary
461
+ } else if (input.prepared.only) {
462
+ keepSpend()
463
+ return null
464
+ }
465
+ }
466
+ if (
467
+ preparedBody !== undefined &&
468
+ input.messages.length -
469
+ (preservedSystem.length + 1 + retainedOlder.length + recentMessages.length) <
470
+ 1
471
+ ) {
472
+ keepSpend()
473
+ return null
474
+ }
475
+
476
+ const manager = new WorkingStateManager(input.config)
477
+ populateWorkingState(manager, olderMessages, input.config)
404
478
  const body =
479
+ preparedBody ??
405
480
  (await modelWrittenBody(
406
481
  input,
407
482
  manager,
408
- plan.olderMessages,
483
+ olderMessages,
409
484
  plan.systemMessages,
410
485
  onUsage,
411
486
  hookInstructions,
412
487
  )) ??
413
488
  (await buildVerifiedSummary(
414
489
  manager,
415
- [...plan.olderMessages],
490
+ [...olderMessages],
416
491
  input.provider,
417
492
  input.config,
418
493
  onUsage,
@@ -431,21 +506,32 @@ export async function compactNow(input: CompactNowInput): Promise<CompactionResu
431
506
  body,
432
507
  manager,
433
508
  input,
434
- [...preservedSystem, ...retainedOlder, ...plan.recentMessages],
509
+ [...preservedSystem, ...retainedOlder, ...recentMessages],
435
510
  input.messages,
436
511
  ),
437
512
  retainedOlder,
438
- plan.recentMessages,
513
+ recentMessages,
439
514
  input.recoveryNote,
440
515
  input.messages,
441
516
  input.sessionFile,
442
517
  input.askWhenUnclear,
443
518
  )
444
519
  const published = await publishManualCompaction(input, {
445
- messages,
520
+ messages: fitRetainedImages(
521
+ messages,
522
+ Math.floor(window.tokens * input.config.resetThreshold),
523
+ input.model,
524
+ input.config.keepRecentTokens === undefined
525
+ ? undefined
526
+ : {
527
+ start: preservedSystem.length + 1 + retainedOlder.length,
528
+ tokens: input.config.keepRecentTokens,
529
+ },
530
+ ),
446
531
  shed: input.messages.length - messages.length,
447
532
  summary,
448
533
  usage,
534
+ ...(preparedBody !== undefined ? { precomputed: true } : {}),
449
535
  })
450
536
  await afterPass(input, published, window.tokens)
451
537
  return published
@@ -10,7 +10,7 @@ import {
10
10
  planNothingToShed,
11
11
  planWorkingSet,
12
12
  } from './salience/working-set.js'
13
- import { estimateMessageTokens, estimateMessagesTokens } from './token-estimate.js'
13
+ import { estimateMessageTokensForModel, estimateMessagesTokensForModel } from './token-estimate.js'
14
14
  import { clearStaleToolResults } from './tool-result-editing.js'
15
15
 
16
16
  /**
@@ -67,6 +67,7 @@ export type CompactionPlan =
67
67
  }
68
68
 
69
69
  export interface CompactionPlanInput {
70
+ readonly model?: string
70
71
  readonly messages: readonly Message[]
71
72
  readonly config: CompactionConfig
72
73
  readonly contextWindowTokens: number
@@ -116,11 +117,15 @@ export interface CompactionPlanInput {
116
117
  * one boundary calculation is the failure this extraction exists to end, so
117
118
  * the phase's copy is gone and its tests point here.
118
119
  */
119
- export function naiveKeepStartByTokens(messages: readonly Message[], budgetTokens: number): number {
120
+ export function naiveKeepStartByTokens(
121
+ messages: readonly Message[],
122
+ budgetTokens: number,
123
+ model?: string,
124
+ ): number {
120
125
  let tokens = 0
121
126
  let start = messages.length
122
127
  for (let index = messages.length - 1; index >= 0; index--) {
123
- const cost = estimateMessageTokens(messages[index] as Message)
128
+ const cost = estimateMessageTokensForModel(messages[index] as Message, model)
124
129
  // Checked BEFORE adding, so the boundary never includes a message
125
130
  // that pushes the tail over. Adding first and trimming after would
126
131
  // admit one oversized message on every turn.
@@ -236,7 +241,8 @@ export function planCompaction(input: CompactionPlanInput): CompactionPlan {
236
241
 
237
242
  if (edit.clearedCount > 0) {
238
243
  const reclaimedTokens =
239
- estimateMessagesTokens(input.messages) - estimateMessagesTokens(edit.messages)
244
+ estimateMessagesTokensForModel(input.messages, input.model) -
245
+ estimateMessagesTokensForModel(edit.messages, input.model)
240
246
  // NOT on a forced pass. A forced pass runs because the provider
241
247
  // REJECTED the prompt as too long, which is a measurement — and
242
248
  // answering it with the same estimate the provider just refuted
@@ -302,7 +308,7 @@ export function planCompaction(input: CompactionPlanInput): CompactionPlan {
302
308
  const naiveKeepStart =
303
309
  config.keepRecentTokens === undefined
304
310
  ? messages.length - config.keepRecentMessages
305
- : naiveKeepStartByTokens(messages, config.keepRecentTokens)
311
+ : naiveKeepStartByTokens(messages, config.keepRecentTokens, input.model)
306
312
  let keepStart = -1
307
313
  for (let candidate = naiveKeepStart; candidate > systemMessages.length; candidate--) {
308
314
  if (findSafeTrimIndex(messages as Message[], candidate) === candidate) {