@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
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The line a run reads shortly before one of its own limits ends it.
3
+ *
4
+ * Without it the iteration cap and the time limit cut a run off between two
5
+ * calls with no word to the model, which is the moment it most needs to leave
6
+ * its work in a state someone can pick up. The line is a request, never a
7
+ * change of limit: nothing here moves the cap or the clock.
8
+ */
9
+
10
+ /** Where the line is due: first when the remaining share is small, again when it is smaller. */
11
+ export type BudgetNudgeStage = 0 | 1 | 2
12
+
13
+ export interface BudgetLeft {
14
+ /** Calls left before the iteration cap, counting the one being prepared. */
15
+ iterations?: { left: number; total: number }
16
+ /** Milliseconds left before the time limit. */
17
+ timeMs?: { left: number; total: number }
18
+ }
19
+
20
+ const FIRST_ITERATION_FLOOR = 5
21
+ const SECOND_ITERATION_FLOOR = 2
22
+ const FIRST_TIME_FLOOR_MS = 2 * 60_000
23
+ const SECOND_TIME_FLOOR_MS = 30_000
24
+ const FIRST_SHARE = 0.1
25
+ const SECOND_SHARE = 0.03
26
+
27
+ /**
28
+ * The larger of the fixed floor and the share, held to half of what the
29
+ * stage before it allowed so a short budget is not nagged from its first
30
+ * call: a ten-call run is told at four calls left, never at the start.
31
+ */
32
+ function threshold(total: number, floor: number, share: number, ceiling: number): number {
33
+ return Math.min(Math.max(floor, total * share), ceiling)
34
+ }
35
+
36
+ function reached(left: number, total: number, floors: [number, number], shares: [number, number]) {
37
+ const first = threshold(total, floors[0], shares[0], total / 2)
38
+ const second = threshold(total, floors[1], shares[1], first / 2)
39
+ if (left <= second) return 2
40
+ if (left <= first) return 1
41
+ return 0
42
+ }
43
+
44
+ /** The stage the remaining budget has reached; 0 when no limit is near or none is set. */
45
+ export function budgetNudgeStage(left: BudgetLeft): BudgetNudgeStage {
46
+ let stage: BudgetNudgeStage = 0
47
+ if (left.iterations && left.iterations.total > 0) {
48
+ stage = Math.max(
49
+ stage,
50
+ reached(
51
+ left.iterations.left,
52
+ left.iterations.total,
53
+ [FIRST_ITERATION_FLOOR, SECOND_ITERATION_FLOOR],
54
+ [FIRST_SHARE, SECOND_SHARE],
55
+ ),
56
+ ) as BudgetNudgeStage
57
+ }
58
+ if (left.timeMs && Number.isFinite(left.timeMs.total) && left.timeMs.total > 0) {
59
+ stage = Math.max(
60
+ stage,
61
+ reached(
62
+ left.timeMs.left,
63
+ left.timeMs.total,
64
+ [FIRST_TIME_FLOOR_MS, SECOND_TIME_FLOOR_MS],
65
+ [FIRST_SHARE, SECOND_SHARE],
66
+ ),
67
+ ) as BudgetNudgeStage
68
+ }
69
+ return stage
70
+ }
71
+
72
+ function duration(ms: number): string {
73
+ const seconds = Math.max(0, Math.round(ms / 1000))
74
+ if (seconds < 90) return `${seconds} seconds`
75
+ return `${Math.round(seconds / 60)} minutes`
76
+ }
77
+
78
+ /** The one line: how much is left, and what to do with it. */
79
+ export function budgetNudgeText(left: BudgetLeft, stage: BudgetNudgeStage): string | null {
80
+ if (stage === 0) return null
81
+ const parts: string[] = []
82
+ if (left.iterations && left.iterations.total > 0) {
83
+ const n = Math.max(0, left.iterations.left)
84
+ parts.push(`${n} more ${n === 1 ? 'step' : 'steps'}`)
85
+ }
86
+ if (left.timeMs && Number.isFinite(left.timeMs.total) && left.timeMs.total > 0) {
87
+ parts.push(`about ${duration(left.timeMs.left)}`)
88
+ }
89
+ if (parts.length === 0) return null
90
+ const urgency = stage === 2 ? 'Very little is left' : 'This run is nearing its limit'
91
+ return `${urgency}: ${parts.join(' and ')} before it is stopped. Leave the work in a working state, write down what is unfinished, then finish. The limit does not move.`
92
+ }
@@ -5,8 +5,23 @@ import {
5
5
  compactNow,
6
6
  compactRegion,
7
7
  } from '../../compaction/manual.js'
8
- import { readFoldedHistory } from '../../manager/session/turn-recorder.js'
9
- import { DiskSessionLog } from '../../store/session-log/index.js'
8
+ import {
9
+ type PrecomputeStore,
10
+ type PreparedSpend,
11
+ precomputeStoreFor,
12
+ } from '../../compaction/precompute.js'
13
+ import { estimateMessagesTokens } from '../../compaction/token-estimate.js'
14
+ import {
15
+ measuredDraft,
16
+ readCumulativeDropped,
17
+ readFoldedHistory,
18
+ } from '../../manager/session/turn-recorder.js'
19
+ import {
20
+ DiskSessionLog,
21
+ type SessionLease,
22
+ type SessionLog,
23
+ } from '../../store/session-log/index.js'
24
+ import { accumulateTokenUsage } from '../../types/common/index.js'
10
25
  import { NamzuError } from '../../types/errors/index.js'
11
26
  import type { MessageId, SessionId } from '../../types/ids/index.js'
12
27
  import type { Message } from '../../types/message/index.js'
@@ -29,6 +44,21 @@ export interface CompactSessionParams
29
44
  * lapses. Recorded on the `compaction` record.
30
45
  */
31
46
  readonly trigger?: 'manual' | 'idle'
47
+ /**
48
+ * What an `'idle'` pass does with a summary prepared ahead (see
49
+ * `CompactionConfig.precompute`). Absent: it is used when it is still valid
50
+ * (waiting for one still being written) and a fresh summary is written when
51
+ * it is not. `'only'`: used or nothing, a refusal ends the pass as a no-op
52
+ * (`null`). `false`: never used, and stopped first. Ignored for `'manual'`,
53
+ * which always stops it.
54
+ */
55
+ readonly preparedSummary?: 'only' | false
56
+ /**
57
+ * Close a turn whose writer is gone before a manual pass, keeping its
58
+ * recorded messages. Ignored for idle passes. Running and paused turns
59
+ * are always refused.
60
+ */
61
+ readonly abandonInterrupted?: boolean
32
62
  /** Where to find the session's log, and the lease to append under. */
33
63
  readonly locator?: SessionLocatorOptions
34
64
  }
@@ -39,51 +69,69 @@ export interface CompactSessionParams
39
69
  * with no `turnId`: the messages the pass kept are named by id, the summary
40
70
  * is recorded in full, and the shed messages stay in the log for audit.
41
71
  *
42
- * Refused while a turn is active (`TurnInProgressError`). `null` when there
43
- * was nothing to compact; nothing is appended then.
72
+ * Refused while a turn is active (`TurnInProgressError`), unless a manual
73
+ * pass opts into closing an interrupted turn. `null` when there was nothing
74
+ * to compact.
44
75
  */
45
76
  export async function compactSession(
46
77
  params: CompactSessionParams,
47
78
  ): Promise<CompactionResult | null> {
48
- const { sessionId, locator = {}, trigger = 'manual', hooks, ...input } = params
79
+ const {
80
+ sessionId,
81
+ locator = {},
82
+ trigger = 'manual',
83
+ hooks,
84
+ preparedSummary,
85
+ abandonInterrupted,
86
+ ...input
87
+ } = params
49
88
  const log = await locateSessionLog(sessionId, locator)
50
89
  return withSessionLease(log, locator.lease, async (lease) => {
51
- const active = await log.activeTurn({ lease })
52
- if (active) {
53
- throw new TurnInProgressError({ sessionId, activeTurnId: active.turnId, state: active.state })
54
- }
55
- const history = await readFoldedHistory(log)
56
- const ids = new Map<Message, MessageId>()
57
- for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
58
- const result = await compactNow({
59
- // The session's own file, so the summary says where exact text is stored.
60
- ...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
61
- ...input,
62
- messages: history.map((entry) => entry.message),
63
- ...(hooks ? { hooks: { ...hooks, sessionId, trigger } } : {}),
64
- })
65
- if (!result) return null
66
- const head = await log.head()
67
- if (!head) {
68
- throw new NamzuError({
69
- code: 'not_found',
70
- message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
71
- details: { sessionId },
90
+ await prepareCompaction(log, lease, trigger === 'manual' && abandonInterrupted)
91
+ // One summary at a time. A host compacting an idle conversation takes the
92
+ // summary prepared ahead when it is still valid (or waits for the one being
93
+ // written); any other pass stops it first.
94
+ const usePrepared = trigger === 'idle' && preparedSummary !== false
95
+ const store = precomputeStoreFor(sessionId)
96
+ const owed = [...store.drainUnbooked(), ...(usePrepared ? [] : await store.cancel())]
97
+ return carrying(store, owed, async () => {
98
+ const history = await readFoldedHistory(log)
99
+ const ids = new Map<Message, MessageId>()
100
+ for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
101
+ const startedAt = performance.now()
102
+ const result = await compactNow({
103
+ // The session's own file, so the summary says where exact text is stored.
104
+ ...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
105
+ ...input,
106
+ messages: history.map((entry) => entry.message),
107
+ ...(hooks ? { hooks: { ...hooks, sessionId, trigger } } : {}),
108
+ ...(usePrepared
109
+ ? { prepared: { sessionId, ...(preparedSummary === 'only' ? { only: true } : {}) } }
110
+ : {}),
72
111
  })
73
- }
74
- const { summary, keptMessageIds } = compactionShape(result.messages, ids)
75
- await log.append(lease, {
76
- type: 'compaction',
77
- compactionId: generateMessageId(),
78
- strategy: input.config.strategy,
79
- trigger,
80
- replacesSeqRange: [1, head.pointer.seq],
81
- summary,
82
- keptMessageIds,
83
- tokensBefore: 0,
84
- tokensAfter: 0,
112
+ if (!result) return null
113
+ const head = await log.head()
114
+ if (!head) {
115
+ throw new NamzuError({
116
+ code: 'not_found',
117
+ message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
118
+ details: { sessionId },
119
+ })
120
+ }
121
+ const { summary, keptMessageIds } = compactionShape(result.messages, ids)
122
+ await appendCompaction(log, lease, {
123
+ strategy: input.config.strategy,
124
+ trigger,
125
+ replacesSeqRange: [1, head.pointer.seq],
126
+ summary,
127
+ keptMessageIds,
128
+ before: history.map((entry) => entry.message),
129
+ after: result.messages,
130
+ startedAt,
131
+ precomputed: result.precomputed === true,
132
+ })
133
+ return result
85
134
  })
86
- return result
87
135
  })
88
136
  }
89
137
 
@@ -114,69 +162,176 @@ export interface CompactSessionRangeParams extends CompactSessionParams {
114
162
  * context (a compaction may already have folded it away), and
115
163
  * `invalid_config` when a boundary would split a tool call from its result.
116
164
  * `null` when the span holds nothing worth summarising; nothing is appended
117
- * then. Refused while a turn is active (`TurnInProgressError`).
165
+ * then except to close an interrupted turn when explicitly requested.
118
166
  */
119
167
  export async function compactSessionRange(
120
168
  params: CompactSessionRangeParams,
121
169
  ): Promise<CompactionResult | null> {
122
- const { sessionId, locator = {}, from, until, hooks, ...input } = params
170
+ const { sessionId, locator = {}, from, until, hooks, abandonInterrupted, ...input } = params
123
171
  const log = await locateSessionLog(sessionId, locator)
124
172
  return withSessionLease(log, locator.lease, async (lease) => {
125
- const active = await log.activeTurn({ lease })
126
- if (active) {
127
- throw new TurnInProgressError({ sessionId, activeTurnId: active.turnId, state: active.state })
128
- }
129
- const history = await readFoldedHistory(log)
130
- const position = (id: MessageId | undefined, fallback: number): number => {
131
- if (id === undefined) return fallback
132
- const at = history.findIndex((entry) => entry.messageId === id)
133
- if (at < 0) {
173
+ await prepareCompaction(log, lease, abandonInterrupted)
174
+ // One summary at a time: a pass asked for between turns stops one being prepared.
175
+ const store = precomputeStoreFor(sessionId)
176
+ const owed = [...store.drainUnbooked(), ...(await store.cancel())]
177
+ return carrying(store, owed, async () => {
178
+ const history = await readFoldedHistory(log)
179
+ const position = (id: MessageId | undefined, fallback: number): number => {
180
+ if (id === undefined) return fallback
181
+ const at = history.findIndex((entry) => entry.messageId === id)
182
+ if (at < 0) {
183
+ throw new NamzuError({
184
+ code: 'not_found',
185
+ message: `Message ${id} is not in session ${sessionId}'s current context; it may already have been summarised.`,
186
+ details: { sessionId, messageId: id },
187
+ })
188
+ }
189
+ return at
190
+ }
191
+ const start = position(from, 0)
192
+ const end = position(until, history.length)
193
+ const ids = new Map<Message, MessageId>()
194
+ for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
195
+ const startedAt = performance.now()
196
+ const result = await compactRegion({
197
+ // The session's own file, so the summary says where exact text is stored.
198
+ ...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
199
+ ...input,
200
+ messages: history.map((entry) => entry.message),
201
+ start,
202
+ end,
203
+ ...(hooks ? { hooks: { ...hooks, sessionId, trigger: 'manual' as const } } : {}),
204
+ })
205
+ if (!result) return null
206
+ const head = await log.head()
207
+ if (!head) {
134
208
  throw new NamzuError({
135
209
  code: 'not_found',
136
- message: `Message ${id} is not in session ${sessionId}'s current context; it may already have been summarised.`,
137
- details: { sessionId, messageId: id },
210
+ message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
211
+ details: { sessionId },
138
212
  })
139
213
  }
140
- return at
141
- }
142
- const start = position(from, 0)
143
- const end = position(until, history.length)
144
- const ids = new Map<Message, MessageId>()
145
- for (const entry of history) if (entry.messageId) ids.set(entry.message, entry.messageId)
146
- const result = await compactRegion({
147
- // The session's own file, so the summary says where exact text is stored.
148
- ...(log instanceof DiskSessionLog ? { sessionFile: log.file } : {}),
149
- ...input,
150
- messages: history.map((entry) => entry.message),
151
- start,
152
- end,
153
- ...(hooks ? { hooks: { ...hooks, sessionId, trigger: 'manual' as const } } : {}),
154
- })
155
- if (!result) return null
156
- const head = await log.head()
157
- if (!head) {
158
- throw new NamzuError({
159
- code: 'not_found',
160
- message: `Session ${sessionId} has an empty log; there is nothing to compact.`,
161
- details: { sessionId },
214
+ const { summary, keptMessageIds } = compactionShape(result.messages, ids)
215
+ await appendCompaction(log, lease, {
216
+ strategy: input.config.strategy,
217
+ trigger: 'manual',
218
+ replacesSeqRange: [1, head.pointer.seq],
219
+ summary,
220
+ keptMessageIds,
221
+ before: history.map((entry) => entry.message),
222
+ after: result.messages,
223
+ startedAt,
224
+ precomputed: false,
162
225
  })
163
- }
164
- const { summary, keptMessageIds } = compactionShape(result.messages, ids)
165
- await log.append(lease, {
166
- type: 'compaction',
167
- compactionId: generateMessageId(),
168
- strategy: input.config.strategy,
169
- trigger: 'manual',
170
- replacesSeqRange: [1, head.pointer.seq],
171
- summary,
172
- keptMessageIds,
173
- tokensBefore: 0,
174
- tokensAfter: 0,
226
+ return result
175
227
  })
176
- return result
177
228
  })
178
229
  }
179
230
 
231
+ /** Check and close an interrupted turn under the same lease used for compaction. */
232
+ async function prepareCompaction(
233
+ log: SessionLog,
234
+ lease: SessionLease,
235
+ abandonInterrupted?: boolean,
236
+ ): Promise<void> {
237
+ const active = await log.activeTurn({ lease })
238
+ if (!active) return
239
+ if (active.state === 'interrupted' && abandonInterrupted) {
240
+ await log.abandonTurn(
241
+ lease,
242
+ active.turnId,
243
+ 'The reply was interrupted before manual compaction.',
244
+ )
245
+ return
246
+ }
247
+ throw new TurnInProgressError({
248
+ sessionId: log.sessionId,
249
+ activeTurnId: active.turnId,
250
+ state: active.state,
251
+ })
252
+ }
253
+
254
+ /**
255
+ * Run a between-turns pass that owes a ledger what prepared summaries spent
256
+ * (one this pass stopped, one that outlived its turn): the cost joins the
257
+ * result's `usage`, and a pass that ends without a result, or fails, leaves it
258
+ * with the session for the next pass or turn to book. Every prepared summary
259
+ * is booked once.
260
+ */
261
+ async function carrying(
262
+ store: PrecomputeStore,
263
+ owed: readonly PreparedSpend[],
264
+ run: () => Promise<CompactionResult | null>,
265
+ ): Promise<CompactionResult | null> {
266
+ try {
267
+ const result = await run()
268
+ if (!result) {
269
+ store.addUnbooked(owed)
270
+ return null
271
+ }
272
+ let usage = result.usage
273
+ for (const spent of owed) usage = accumulateTokenUsage(usage, spent.usage)
274
+ return { ...result, usage }
275
+ } catch (error) {
276
+ store.addUnbooked(owed)
277
+ throw error
278
+ }
279
+ }
280
+
281
+ /**
282
+ * Append a between-turns pass: the `compaction` record, in the shape an
283
+ * earlier release reads (real sizes, a trigger it knows), then the
284
+ * `compaction_measured` record with what only this release records. A refused
285
+ * second write loses only the measures.
286
+ */
287
+ async function appendCompaction(
288
+ log: SessionLog,
289
+ lease: Parameters<SessionLog['append']>[0],
290
+ pass: {
291
+ readonly strategy: string
292
+ readonly trigger: 'manual' | 'idle'
293
+ readonly replacesSeqRange: [number, number]
294
+ readonly summary: Message[]
295
+ readonly keptMessageIds: MessageId[]
296
+ readonly before: readonly Message[]
297
+ readonly after: readonly Message[]
298
+ readonly startedAt: number
299
+ readonly precomputed: boolean
300
+ },
301
+ ): Promise<void> {
302
+ const tokensBefore = estimateMessagesTokens(pass.before)
303
+ const tokensAfter = estimateMessagesTokens(pass.after)
304
+ const compactionId = generateMessageId()
305
+ const priorDropped = await readCumulativeDropped(log)
306
+ await log.append(lease, {
307
+ type: 'compaction',
308
+ compactionId,
309
+ strategy: pass.strategy,
310
+ trigger: pass.trigger,
311
+ replacesSeqRange: pass.replacesSeqRange,
312
+ summary: pass.summary,
313
+ keptMessageIds: pass.keptMessageIds,
314
+ tokensBefore,
315
+ tokensAfter,
316
+ })
317
+ await log
318
+ .append(
319
+ lease,
320
+ measuredDraft(
321
+ compactionId,
322
+ {
323
+ trigger: pass.trigger,
324
+ tokensBefore,
325
+ tokensAfter,
326
+ durationMs: Math.max(0, Math.round(performance.now() - pass.startedAt)),
327
+ precomputed: pass.precomputed,
328
+ },
329
+ priorDropped + Math.max(0, tokensBefore - tokensAfter),
330
+ ),
331
+ )
332
+ .catch(() => undefined)
333
+ }
334
+
180
335
  /**
181
336
  * Express a compacted context as a `compaction` record: the new messages
182
337
  * before the first recorded one are its summary and the recorded ones are
@@ -1,6 +1,7 @@
1
1
  import type { TurnRecorder } from '../../manager/session/turn-recorder.js'
2
2
  import { buildLimitConfig, checkLimitsDetailed } from '../../turn/LimitChecker.js'
3
3
  import type { LimitCheckerConfig, StopReason } from '../../types/session/index.js'
4
+ import type { BudgetLeft } from './budget-nudge.js'
4
5
 
5
6
  export interface GuardConfig {
6
7
  tokenBudget: number
@@ -101,6 +102,27 @@ export class GuardCoordinator {
101
102
  return Math.max(0, this.limitConfig.timeoutMs - (Date.now() - this.startTime))
102
103
  }
103
104
 
105
+ /**
106
+ * What is left of each limit this turn has, for the request note that
107
+ * asks the model to leave its work tidy. `completedIterations` is the
108
+ * count before the call being prepared, so the call itself is counted.
109
+ * A limit that is off (zero) is left out.
110
+ */
111
+ budgetLeft(completedIterations: number): BudgetLeft {
112
+ const left: BudgetLeft = {}
113
+ const { maxIterations, timeoutMs } = this.limitConfig
114
+ if (maxIterations > 0) {
115
+ left.iterations = {
116
+ left: Math.max(0, maxIterations - completedIterations),
117
+ total: maxIterations,
118
+ }
119
+ }
120
+ if (timeoutMs > 0) {
121
+ left.timeMs = { left: this.remainingUntilTimeoutMs(), total: timeoutMs }
122
+ }
123
+ return left
124
+ }
125
+
104
126
  beforeIteration(recorder: TurnRecorder, abortSignal: AbortSignal): GuardCheckResult {
105
127
  const limitState = {
106
128
  aborted: abortSignal.aborted,
@@ -2109,6 +2109,7 @@ export async function* query(params: QueryParams): AsyncGenerator<SessionEvent,
2109
2109
  ...(params.durableInbound ? { durableInbound: params.durableInbound } : {}),
2110
2110
  ...(params.resumeFromCheckpoint ? { resumedInput: queuedForThisRun } : {}),
2111
2111
  ...(params.waitForInbound ? { waitForInbound: params.waitForInbound } : {}),
2112
+ ...(params.waitForOperator ? { waitForOperator: params.waitForOperator } : {}),
2112
2113
  ...(params.projectInstructionContext
2113
2114
  ? { projectInstructionContext: params.projectInstructionContext }
2114
2115
  : {}),
@@ -67,6 +67,7 @@ import { toErrorMessage } from '../../../utils/error.js'
67
67
  import { stableDigest } from '../../../utils/hash.js'
68
68
  import { generateMessageId } from '../../../utils/id.js'
69
69
  import { parseStructuredResultJson } from '../../../utils/structured-result-json.js'
70
+ import { type BudgetNudgeStage, budgetNudgeStage, budgetNudgeText } from '../budget-nudge.js'
70
71
  import { createCallbackInference } from '../callback-inference.js'
71
72
  import { SandboxAcquisitionStopped } from '../deferred-sandbox.js'
72
73
  import { deliverDurableInbound } from '../durable-inbound.js'
@@ -135,6 +136,7 @@ import {
135
136
  streamWithProviderRejectedImageRecovery,
136
137
  } from './provider-rejected-image.js'
137
138
  import { appendRecordedContext } from './recorded-context.js'
139
+ import { digestRequestPrefix } from './request-prefix.js'
138
140
  import { type MediaRemoval, RequestRepair } from './request-repair.js'
139
141
  import { placeRequestContext } from './step-context.js'
140
142
  import {
@@ -345,6 +347,8 @@ export class IterationOrchestrator {
345
347
  * sets it.
346
348
  */
347
349
  private stopDeferredForOutstandingWork = false
350
+ /** The latest budget line this turn has recorded, so each stage is said once. */
351
+ private budgetNudgeStage: BudgetNudgeStage = 0
348
352
  /** Nudges sent since the model last finished a call without hitting its output cap. */
349
353
  private outputContinuations = 0
350
354
  /** Watches the turn's steps for reasoning that piles up while nothing changes. */
@@ -888,6 +892,12 @@ export class IterationOrchestrator {
888
892
  // edge BEFORE compaction runs (so the refreshed slot is what
889
893
  // compaction preserves). No-op when no provider is configured.
890
894
  await refreshWorkingMemory(this.ctx)
895
+ // A turn that starts after the prompt cache lapsed installs a summary
896
+ // written earlier, when one is ready and still valid, before the old
897
+ // tool results are trimmed.
898
+ if (iterationNum === 1 && this.ctx.compactionConfig?.clearStaleToolResultsAtStart) {
899
+ await runCompactionCheck(this.ctx, { preparedOnly: true })
900
+ }
891
901
  if (iterationNum === 1) await runStaleResultClear(this.ctx)
892
902
  await runCompactionCheck(this.ctx)
893
903
  yield* this.ctx.drainPending()
@@ -922,6 +932,16 @@ export class IterationOrchestrator {
922
932
  this.ctx.log,
923
933
  )
924
934
  }
935
+ // Before the limit ends the run, say so once per stage. Recorded, so
936
+ // the model keeps reading it, and only after the guard has passed
937
+ // this call: a run that is about to be stopped has no call to spend.
938
+ const budgetLeft = this.ctx.guard.budgetLeft(iterationNum - 1)
939
+ const budgetStage = budgetNudgeStage(budgetLeft)
940
+ if (budgetStage > this.budgetNudgeStage) {
941
+ this.budgetNudgeStage = budgetStage
942
+ const text = budgetNudgeText(budgetLeft, budgetStage)
943
+ if (text) appendRecordedContext(recorder, [{ key: 'budget-nudge', text }], this.ctx.log)
944
+ }
925
945
  const contextSections =
926
946
  this.ctx.promptContributions?.render('context', { iteration: iterationNum }) ?? []
927
947
  const deferredMcpHints =
@@ -1500,6 +1520,19 @@ export class IterationOrchestrator {
1500
1520
  ...(response.usage.cacheMiss !== undefined
1501
1521
  ? { cacheMiss: response.usage.cacheMiss }
1502
1522
  : {}),
1523
+ requestPrefix: digestRequestPrefix({
1524
+ conversation: recorder.sessionId,
1525
+ model: stepModel,
1526
+ tools: llmTools,
1527
+ messages,
1528
+ wire: response.usage.requestShape,
1529
+ params: {
1530
+ effort: stepEffort,
1531
+ speed: turnConfig.speed,
1532
+ thinking: turnConfig.thinking,
1533
+ choice: step.toolChoice,
1534
+ },
1535
+ }),
1503
1536
  ...contextFigures,
1504
1537
  })
1505
1538
 
@@ -3627,7 +3660,13 @@ export class IterationOrchestrator {
3627
3660
  this.hydrateOutcome()
3628
3661
  return await this.outcomeLedger.preview()
3629
3662
  },
3630
- { finalMessage },
3663
+ {
3664
+ finalMessage,
3665
+ recordCheck: async (check) => {
3666
+ this.hydrateOutcome()
3667
+ await this.outcomeLedger.recordHostCheck(check)
3668
+ },
3669
+ },
3631
3670
  )
3632
3671
  if (typeof note !== 'string' || note.trim() === '') return undefined
3633
3672
  this.beforeEndNotes += 1