@namzu/sdk 34.2.0 → 36.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 (669) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/README.md +2 -6
  3. package/dist/agents/PipelineAgent.d.ts.map +1 -1
  4. package/dist/agents/PipelineAgent.js +118 -100
  5. package/dist/agents/PipelineAgent.js.map +1 -1
  6. package/dist/agents/ReactiveAgent.d.ts.map +1 -1
  7. package/dist/agents/ReactiveAgent.js +2 -0
  8. package/dist/agents/ReactiveAgent.js.map +1 -1
  9. package/dist/agents/RouterAgent.d.ts.map +1 -1
  10. package/dist/agents/RouterAgent.js +105 -61
  11. package/dist/agents/RouterAgent.js.map +1 -1
  12. package/dist/agents/SupervisorAgent.d.ts.map +1 -1
  13. package/dist/agents/SupervisorAgent.js +8 -4
  14. package/dist/agents/SupervisorAgent.js.map +1 -1
  15. package/dist/agents/budget.d.ts +6 -0
  16. package/dist/agents/budget.d.ts.map +1 -0
  17. package/dist/agents/budget.js +33 -0
  18. package/dist/agents/budget.js.map +1 -0
  19. package/dist/agents/explore.d.ts +20 -0
  20. package/dist/agents/explore.d.ts.map +1 -0
  21. package/dist/agents/explore.js +26 -0
  22. package/dist/agents/explore.js.map +1 -0
  23. package/dist/agents/file-definitions.d.ts +80 -0
  24. package/dist/agents/file-definitions.d.ts.map +1 -0
  25. package/dist/agents/file-definitions.js +179 -0
  26. package/dist/agents/file-definitions.js.map +1 -0
  27. package/dist/agents/index.d.ts +3 -0
  28. package/dist/agents/index.d.ts.map +1 -1
  29. package/dist/agents/index.js +2 -0
  30. package/dist/agents/index.js.map +1 -1
  31. package/dist/bridge/a2a/mapper.d.ts.map +1 -1
  32. package/dist/bridge/a2a/mapper.js +6 -0
  33. package/dist/bridge/a2a/mapper.js.map +1 -1
  34. package/dist/bridge/sse/mapper.d.ts.map +1 -1
  35. package/dist/bridge/sse/mapper.js +22 -0
  36. package/dist/bridge/sse/mapper.js.map +1 -1
  37. package/dist/bus/lock.d.ts.map +1 -1
  38. package/dist/bus/lock.js +1 -10
  39. package/dist/bus/lock.js.map +1 -1
  40. package/dist/compaction/consolidation.d.ts +31 -0
  41. package/dist/compaction/consolidation.d.ts.map +1 -0
  42. package/dist/compaction/consolidation.js +63 -0
  43. package/dist/compaction/consolidation.js.map +1 -0
  44. package/dist/compaction/factory.d.ts.map +1 -1
  45. package/dist/compaction/factory.js +3 -0
  46. package/dist/compaction/factory.js.map +1 -1
  47. package/dist/compaction/index.d.ts +6 -1
  48. package/dist/compaction/index.d.ts.map +1 -1
  49. package/dist/compaction/index.js +6 -0
  50. package/dist/compaction/index.js.map +1 -1
  51. package/dist/compaction/manager.d.ts +7 -0
  52. package/dist/compaction/manager.d.ts.map +1 -1
  53. package/dist/compaction/manager.js +31 -0
  54. package/dist/compaction/manager.js.map +1 -1
  55. package/dist/compaction/plan.d.ts +23 -0
  56. package/dist/compaction/plan.d.ts.map +1 -1
  57. package/dist/compaction/plan.js +41 -24
  58. package/dist/compaction/plan.js.map +1 -1
  59. package/dist/compaction/salience/bm25.d.ts +26 -0
  60. package/dist/compaction/salience/bm25.d.ts.map +1 -0
  61. package/dist/compaction/salience/bm25.js +56 -0
  62. package/dist/compaction/salience/bm25.js.map +1 -0
  63. package/dist/compaction/salience/goal.d.ts +21 -0
  64. package/dist/compaction/salience/goal.d.ts.map +1 -0
  65. package/dist/compaction/salience/goal.js +32 -0
  66. package/dist/compaction/salience/goal.js.map +1 -0
  67. package/dist/compaction/salience/index.d.ts +4 -0
  68. package/dist/compaction/salience/index.d.ts.map +1 -0
  69. package/dist/compaction/salience/index.js +4 -0
  70. package/dist/compaction/salience/index.js.map +1 -0
  71. package/dist/compaction/salience/minhash.d.ts +17 -0
  72. package/dist/compaction/salience/minhash.d.ts.map +1 -0
  73. package/dist/compaction/salience/minhash.js +54 -0
  74. package/dist/compaction/salience/minhash.js.map +1 -0
  75. package/dist/compaction/salience/score.d.ts +64 -0
  76. package/dist/compaction/salience/score.d.ts.map +1 -0
  77. package/dist/compaction/salience/score.js +256 -0
  78. package/dist/compaction/salience/score.js.map +1 -0
  79. package/dist/compaction/salience/tokenize.d.ts +20 -0
  80. package/dist/compaction/salience/tokenize.d.ts.map +1 -0
  81. package/dist/compaction/salience/tokenize.js +92 -0
  82. package/dist/compaction/salience/tokenize.js.map +1 -0
  83. package/dist/compaction/salience/working-set.d.ts +59 -0
  84. package/dist/compaction/salience/working-set.d.ts.map +1 -0
  85. package/dist/compaction/salience/working-set.js +137 -0
  86. package/dist/compaction/salience/working-set.js.map +1 -0
  87. package/dist/compaction/serializer.d.ts +2 -0
  88. package/dist/compaction/serializer.d.ts.map +1 -1
  89. package/dist/compaction/serializer.js +15 -0
  90. package/dist/compaction/serializer.js.map +1 -1
  91. package/dist/compaction/token-estimate.d.ts +13 -0
  92. package/dist/compaction/token-estimate.d.ts.map +1 -0
  93. package/dist/compaction/token-estimate.js +58 -0
  94. package/dist/compaction/token-estimate.js.map +1 -0
  95. package/dist/compaction/tool-result-editing.d.ts +10 -2
  96. package/dist/compaction/tool-result-editing.d.ts.map +1 -1
  97. package/dist/compaction/tool-result-editing.js +22 -24
  98. package/dist/compaction/tool-result-editing.js.map +1 -1
  99. package/dist/compaction/types.d.ts +22 -1
  100. package/dist/compaction/types.d.ts.map +1 -1
  101. package/dist/compaction/wire.d.ts +3 -1
  102. package/dist/compaction/wire.d.ts.map +1 -1
  103. package/dist/compaction/wire.js +2 -0
  104. package/dist/compaction/wire.js.map +1 -1
  105. package/dist/config/runtime.d.ts +58 -28
  106. package/dist/config/runtime.d.ts.map +1 -1
  107. package/dist/config/runtime.js +13 -1
  108. package/dist/config/runtime.js.map +1 -1
  109. package/dist/connector/builtins/http.d.ts.map +1 -1
  110. package/dist/connector/builtins/http.js +2 -8
  111. package/dist/connector/builtins/http.js.map +1 -1
  112. package/dist/connector/builtins/webhook.js +2 -2
  113. package/dist/connector/builtins/webhook.js.map +1 -1
  114. package/dist/connector/mcp/adapter.d.ts +2 -0
  115. package/dist/connector/mcp/adapter.d.ts.map +1 -1
  116. package/dist/connector/mcp/adapter.js +30 -0
  117. package/dist/connector/mcp/adapter.js.map +1 -1
  118. package/dist/connector/tools/adapter.js +2 -2
  119. package/dist/connector/tools/adapter.js.map +1 -1
  120. package/dist/connector/tools/definitions.js +2 -2
  121. package/dist/connector/tools/definitions.js.map +1 -1
  122. package/dist/constants/compaction/index.d.ts +1 -0
  123. package/dist/constants/compaction/index.d.ts.map +1 -1
  124. package/dist/constants/compaction/index.js +1 -0
  125. package/dist/constants/compaction/index.js.map +1 -1
  126. package/dist/constants/telemetry/index.d.ts +0 -1
  127. package/dist/constants/telemetry/index.d.ts.map +1 -1
  128. package/dist/constants/telemetry/index.js +0 -1
  129. package/dist/constants/telemetry/index.js.map +1 -1
  130. package/dist/contracts/a2a.d.ts +5 -5
  131. package/dist/contracts/api.d.ts +4 -2
  132. package/dist/contracts/api.d.ts.map +1 -1
  133. package/dist/contracts/schemas.d.ts +1 -15
  134. package/dist/contracts/schemas.d.ts.map +1 -1
  135. package/dist/contracts/schemas.js +5 -20
  136. package/dist/contracts/schemas.js.map +1 -1
  137. package/dist/invariants/index.d.ts +1 -1
  138. package/dist/manager/agent/lifecycle.d.ts +2 -18
  139. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  140. package/dist/manager/agent/lifecycle.js +373 -340
  141. package/dist/manager/agent/lifecycle.js.map +1 -1
  142. package/dist/manager/run/persistence.d.ts +3 -0
  143. package/dist/manager/run/persistence.d.ts.map +1 -1
  144. package/dist/manager/run/persistence.js +35 -5
  145. package/dist/manager/run/persistence.js.map +1 -1
  146. package/dist/plugin/index.d.ts +2 -0
  147. package/dist/plugin/index.d.ts.map +1 -1
  148. package/dist/plugin/index.js +1 -0
  149. package/dist/plugin/index.js.map +1 -1
  150. package/dist/plugin/shell-hook.d.ts +128 -0
  151. package/dist/plugin/shell-hook.d.ts.map +1 -0
  152. package/dist/plugin/shell-hook.js +299 -0
  153. package/dist/plugin/shell-hook.js.map +1 -0
  154. package/dist/prompt/coding-agent-doctrine.d.ts +63 -0
  155. package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -0
  156. package/dist/prompt/coding-agent-doctrine.js +100 -0
  157. package/dist/prompt/coding-agent-doctrine.js.map +1 -0
  158. package/dist/prompt/index.d.ts +2 -0
  159. package/dist/prompt/index.d.ts.map +1 -1
  160. package/dist/prompt/index.js +1 -0
  161. package/dist/prompt/index.js.map +1 -1
  162. package/dist/provider/errors.d.ts +1 -1
  163. package/dist/provider/errors.js +1 -1
  164. package/dist/provider/fallback.d.ts +2 -0
  165. package/dist/provider/fallback.d.ts.map +1 -1
  166. package/dist/provider/fallback.js +3 -1
  167. package/dist/provider/fallback.js.map +1 -1
  168. package/dist/provider/retry.d.ts +2 -0
  169. package/dist/provider/retry.d.ts.map +1 -1
  170. package/dist/provider/retry.js +3 -1
  171. package/dist/provider/retry.js.map +1 -1
  172. package/dist/provider/token-budget.d.ts +13 -0
  173. package/dist/provider/token-budget.d.ts.map +1 -0
  174. package/dist/provider/token-budget.js +150 -0
  175. package/dist/provider/token-budget.js.map +1 -0
  176. package/dist/public-runtime.d.ts +14 -14
  177. package/dist/public-runtime.d.ts.map +1 -1
  178. package/dist/public-runtime.js +15 -10
  179. package/dist/public-runtime.js.map +1 -1
  180. package/dist/public-tools.d.ts +2 -0
  181. package/dist/public-tools.d.ts.map +1 -1
  182. package/dist/public-tools.js +2 -0
  183. package/dist/public-tools.js.map +1 -1
  184. package/dist/public-types.d.ts +12 -2
  185. package/dist/public-types.d.ts.map +1 -1
  186. package/dist/registry/command/kernel-commands.d.ts.map +1 -1
  187. package/dist/registry/command/kernel-commands.js +18 -6
  188. package/dist/registry/command/kernel-commands.js.map +1 -1
  189. package/dist/registry/tool/execute.d.ts.map +1 -1
  190. package/dist/registry/tool/execute.js +4 -1
  191. package/dist/registry/tool/execute.js.map +1 -1
  192. package/dist/run/LimitChecker.js +1 -1
  193. package/dist/run/LimitChecker.js.map +1 -1
  194. package/dist/run/drain.js +1 -1
  195. package/dist/run/reporter.d.ts.map +1 -1
  196. package/dist/run/reporter.js +15 -0
  197. package/dist/run/reporter.js.map +1 -1
  198. package/dist/run/token-budget.d.ts +108 -0
  199. package/dist/run/token-budget.d.ts.map +1 -0
  200. package/dist/run/token-budget.js +591 -0
  201. package/dist/run/token-budget.js.map +1 -0
  202. package/dist/runtime/jobs/registry.d.ts +29 -0
  203. package/dist/runtime/jobs/registry.d.ts.map +1 -1
  204. package/dist/runtime/jobs/registry.js +98 -18
  205. package/dist/runtime/jobs/registry.js.map +1 -1
  206. package/dist/runtime/query/checkpoint.d.ts.map +1 -1
  207. package/dist/runtime/query/checkpoint.js +10 -3
  208. package/dist/runtime/query/checkpoint.js.map +1 -1
  209. package/dist/runtime/query/context.d.ts +2 -26
  210. package/dist/runtime/query/context.d.ts.map +1 -1
  211. package/dist/runtime/query/context.js +1 -29
  212. package/dist/runtime/query/context.js.map +1 -1
  213. package/dist/runtime/query/executor.d.ts +13 -9
  214. package/dist/runtime/query/executor.d.ts.map +1 -1
  215. package/dist/runtime/query/executor.js +144 -55
  216. package/dist/runtime/query/executor.js.map +1 -1
  217. package/dist/runtime/query/guard.d.ts.map +1 -1
  218. package/dist/runtime/query/guard.js +8 -0
  219. package/dist/runtime/query/guard.js.map +1 -1
  220. package/dist/runtime/query/index.d.ts +42 -4
  221. package/dist/runtime/query/index.d.ts.map +1 -1
  222. package/dist/runtime/query/index.js +153 -52
  223. package/dist/runtime/query/index.js.map +1 -1
  224. package/dist/runtime/query/iteration/index.d.ts +3 -3
  225. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  226. package/dist/runtime/query/iteration/index.js +47 -11
  227. package/dist/runtime/query/iteration/index.js.map +1 -1
  228. package/dist/runtime/query/iteration/phases/compaction.d.ts.map +1 -1
  229. package/dist/runtime/query/iteration/phases/compaction.js +98 -30
  230. package/dist/runtime/query/iteration/phases/compaction.js.map +1 -1
  231. package/dist/runtime/query/iteration/phases/context.d.ts +4 -0
  232. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  233. package/dist/runtime/query/iteration/phases/context.js.map +1 -1
  234. package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
  235. package/dist/runtime/query/iteration/phases/tool-review.js +29 -7
  236. package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
  237. package/dist/runtime/query/iteration/phases/working-memory.d.ts.map +1 -1
  238. package/dist/runtime/query/iteration/phases/working-memory.js +25 -15
  239. package/dist/runtime/query/iteration/phases/working-memory.js.map +1 -1
  240. package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
  241. package/dist/runtime/query/iteration/stream-turn.js +10 -5
  242. package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
  243. package/dist/runtime/query/plugin-hooks.d.ts +1 -1
  244. package/dist/runtime/query/plugin-hooks.d.ts.map +1 -1
  245. package/dist/runtime/query/plugin-hooks.js +20 -1
  246. package/dist/runtime/query/plugin-hooks.js.map +1 -1
  247. package/dist/runtime/query/prompt.d.ts.map +1 -1
  248. package/dist/runtime/query/prompt.js +14 -4
  249. package/dist/runtime/query/prompt.js.map +1 -1
  250. package/dist/runtime/query/repeat-call.d.ts +20 -2
  251. package/dist/runtime/query/repeat-call.d.ts.map +1 -1
  252. package/dist/runtime/query/repeat-call.js +35 -7
  253. package/dist/runtime/query/repeat-call.js.map +1 -1
  254. package/dist/runtime/query/result.d.ts.map +1 -1
  255. package/dist/runtime/query/result.js +9 -0
  256. package/dist/runtime/query/result.js.map +1 -1
  257. package/dist/runtime/query/review-policy.d.ts +142 -0
  258. package/dist/runtime/query/review-policy.d.ts.map +1 -0
  259. package/dist/runtime/query/review-policy.js +144 -0
  260. package/dist/runtime/query/review-policy.js.map +1 -0
  261. package/dist/runtime/query/run-state.d.ts.map +1 -1
  262. package/dist/runtime/query/run-state.js +4 -0
  263. package/dist/runtime/query/run-state.js.map +1 -1
  264. package/dist/runtime/query/steering.d.ts +4 -0
  265. package/dist/runtime/query/steering.d.ts.map +1 -1
  266. package/dist/runtime/query/steering.js +29 -0
  267. package/dist/runtime/query/steering.js.map +1 -1
  268. package/dist/runtime/query/token-budget.d.ts +7 -0
  269. package/dist/runtime/query/token-budget.d.ts.map +1 -0
  270. package/dist/runtime/query/token-budget.js +101 -0
  271. package/dist/runtime/query/token-budget.js.map +1 -0
  272. package/dist/runtime/query/tool-output-budget.d.ts +2 -0
  273. package/dist/runtime/query/tool-output-budget.d.ts.map +1 -1
  274. package/dist/runtime/query/tool-output-budget.js +29 -11
  275. package/dist/runtime/query/tool-output-budget.js.map +1 -1
  276. package/dist/runtime/query/tool-pause.d.ts.map +1 -1
  277. package/dist/runtime/query/tool-pause.js +4 -2
  278. package/dist/runtime/query/tool-pause.js.map +1 -1
  279. package/dist/runtime/query/tooling.d.ts +4 -0
  280. package/dist/runtime/query/tooling.d.ts.map +1 -1
  281. package/dist/runtime/query/tooling.js +4 -0
  282. package/dist/runtime/query/tooling.js.map +1 -1
  283. package/dist/sandbox/file-walk-program.d.ts +3 -0
  284. package/dist/sandbox/file-walk-program.d.ts.map +1 -0
  285. package/dist/sandbox/file-walk-program.js +94 -0
  286. package/dist/sandbox/file-walk-program.js.map +1 -0
  287. package/dist/sandbox/file-walk.d.ts +12 -0
  288. package/dist/sandbox/file-walk.d.ts.map +1 -0
  289. package/dist/sandbox/file-walk.js +429 -0
  290. package/dist/sandbox/file-walk.js.map +1 -0
  291. package/dist/sandbox/index.d.ts +2 -0
  292. package/dist/sandbox/index.d.ts.map +1 -1
  293. package/dist/sandbox/index.js +1 -0
  294. package/dist/sandbox/index.js.map +1 -1
  295. package/dist/sandbox/provider/local.d.ts +3 -1
  296. package/dist/sandbox/provider/local.d.ts.map +1 -1
  297. package/dist/sandbox/provider/local.js +121 -12
  298. package/dist/sandbox/provider/local.js.map +1 -1
  299. package/dist/scheduler/completion-inbox.d.ts +3 -2
  300. package/dist/scheduler/completion-inbox.d.ts.map +1 -1
  301. package/dist/scheduler/completion-inbox.js +29 -15
  302. package/dist/scheduler/completion-inbox.js.map +1 -1
  303. package/dist/scheduler/delegating.d.ts +1 -0
  304. package/dist/scheduler/delegating.d.ts.map +1 -1
  305. package/dist/scheduler/delegating.js +29 -12
  306. package/dist/scheduler/delegating.js.map +1 -1
  307. package/dist/scheduler/local.d.ts +1 -0
  308. package/dist/scheduler/local.d.ts.map +1 -1
  309. package/dist/scheduler/local.js +4 -7
  310. package/dist/scheduler/local.js.map +1 -1
  311. package/dist/session/index.d.ts +0 -1
  312. package/dist/session/index.d.ts.map +1 -1
  313. package/dist/session/index.js +0 -1
  314. package/dist/session/index.js.map +1 -1
  315. package/dist/session/workspace/path-builder.d.ts.map +1 -1
  316. package/dist/session/workspace/path-builder.js +5 -4
  317. package/dist/session/workspace/path-builder.js.map +1 -1
  318. package/dist/store/feedback/disk.d.ts.map +1 -1
  319. package/dist/store/feedback/disk.js +4 -6
  320. package/dist/store/feedback/disk.js.map +1 -1
  321. package/dist/store/index.d.ts +2 -0
  322. package/dist/store/index.d.ts.map +1 -1
  323. package/dist/store/index.js +1 -0
  324. package/dist/store/index.js.map +1 -1
  325. package/dist/store/memory/disk.d.ts.map +1 -1
  326. package/dist/store/memory/disk.js +3 -12
  327. package/dist/store/memory/disk.js.map +1 -1
  328. package/dist/store/run/checkpoint-disk.d.ts +3 -3
  329. package/dist/store/run/checkpoint-disk.d.ts.map +1 -1
  330. package/dist/store/run/checkpoint-disk.js +14 -26
  331. package/dist/store/run/checkpoint-disk.js.map +1 -1
  332. package/dist/store/run/claim-disk.js +1 -1
  333. package/dist/store/run/conformance.d.ts +1 -1
  334. package/dist/store/run/conformance.d.ts.map +1 -1
  335. package/dist/store/run/conformance.js +41 -33
  336. package/dist/store/run/conformance.js.map +1 -1
  337. package/dist/store/run/disk.d.ts.map +1 -1
  338. package/dist/store/run/disk.js +20 -4
  339. package/dist/store/run/disk.js.map +1 -1
  340. package/dist/store/run/token-budget-disk.d.ts +37 -0
  341. package/dist/store/run/token-budget-disk.d.ts.map +1 -0
  342. package/dist/store/run/token-budget-disk.js +208 -0
  343. package/dist/store/run/token-budget-disk.js.map +1 -0
  344. package/dist/store/session/disk.d.ts +4 -13
  345. package/dist/store/session/disk.d.ts.map +1 -1
  346. package/dist/store/session/disk.js +34 -31
  347. package/dist/store/session/disk.js.map +1 -1
  348. package/dist/store/session/memory.d.ts +2 -0
  349. package/dist/store/session/memory.d.ts.map +1 -1
  350. package/dist/store/session/memory.js +41 -2
  351. package/dist/store/session/memory.js.map +1 -1
  352. package/dist/store/task/disk.d.ts.map +1 -1
  353. package/dist/store/task/disk.js +7 -8
  354. package/dist/store/task/disk.js.map +1 -1
  355. package/dist/store/topic/memory.d.ts +2 -0
  356. package/dist/store/topic/memory.d.ts.map +1 -1
  357. package/dist/store/topic/memory.js +13 -1
  358. package/dist/store/topic/memory.js.map +1 -1
  359. package/dist/test-support/ids.d.ts +4 -19
  360. package/dist/test-support/ids.d.ts.map +1 -1
  361. package/dist/test-support/ids.js +51 -59
  362. package/dist/test-support/ids.js.map +1 -1
  363. package/dist/tools/advisory/index.d.ts.map +1 -1
  364. package/dist/tools/advisory/index.js +3 -1
  365. package/dist/tools/advisory/index.js.map +1 -1
  366. package/dist/tools/builtins/bash.d.ts +2 -0
  367. package/dist/tools/builtins/bash.d.ts.map +1 -1
  368. package/dist/tools/builtins/bash.js +224 -70
  369. package/dist/tools/builtins/bash.js.map +1 -1
  370. package/dist/tools/builtins/computer-use.d.ts +2 -2
  371. package/dist/tools/builtins/computer-use.d.ts.map +1 -1
  372. package/dist/tools/builtins/computer-use.js +4 -2
  373. package/dist/tools/builtins/computer-use.js.map +1 -1
  374. package/dist/tools/builtins/edit.js +2 -2
  375. package/dist/tools/builtins/edit.js.map +1 -1
  376. package/dist/tools/builtins/glob.d.ts +2 -1
  377. package/dist/tools/builtins/glob.d.ts.map +1 -1
  378. package/dist/tools/builtins/glob.js +95 -103
  379. package/dist/tools/builtins/glob.js.map +1 -1
  380. package/dist/tools/builtins/grep.d.ts.map +1 -1
  381. package/dist/tools/builtins/grep.js +155 -111
  382. package/dist/tools/builtins/grep.js.map +1 -1
  383. package/dist/tools/builtins/ls.js +4 -4
  384. package/dist/tools/builtins/ls.js.map +1 -1
  385. package/dist/tools/builtins/lsp.js +2 -2
  386. package/dist/tools/builtins/lsp.js.map +1 -1
  387. package/dist/tools/builtins/read-file.js +2 -2
  388. package/dist/tools/builtins/read-file.js.map +1 -1
  389. package/dist/tools/builtins/write-file.js +2 -2
  390. package/dist/tools/builtins/write-file.js.map +1 -1
  391. package/dist/tools/coordinator/ask-user-question.d.ts +33 -0
  392. package/dist/tools/coordinator/ask-user-question.d.ts.map +1 -0
  393. package/dist/tools/coordinator/ask-user-question.js +237 -0
  394. package/dist/tools/coordinator/ask-user-question.js.map +1 -0
  395. package/dist/tools/coordinator/index.d.ts +1 -2
  396. package/dist/tools/coordinator/index.d.ts.map +1 -1
  397. package/dist/tools/coordinator/index.js +4 -220
  398. package/dist/tools/coordinator/index.js.map +1 -1
  399. package/dist/tools/memory/read.d.ts.map +1 -1
  400. package/dist/tools/memory/read.js +2 -4
  401. package/dist/tools/memory/read.js.map +1 -1
  402. package/dist/tools/paths.d.ts +13 -0
  403. package/dist/tools/paths.d.ts.map +1 -1
  404. package/dist/tools/paths.js +53 -0
  405. package/dist/tools/paths.js.map +1 -1
  406. package/dist/tools/roster.d.ts +27 -0
  407. package/dist/tools/roster.d.ts.map +1 -0
  408. package/dist/tools/roster.js +42 -0
  409. package/dist/tools/roster.js.map +1 -0
  410. package/dist/types/agent/base.d.ts +5 -0
  411. package/dist/types/agent/base.d.ts.map +1 -1
  412. package/dist/types/agent/pipeline.d.ts +3 -0
  413. package/dist/types/agent/pipeline.d.ts.map +1 -1
  414. package/dist/types/agent/router.d.ts +2 -3
  415. package/dist/types/agent/router.d.ts.map +1 -1
  416. package/dist/types/agent/scheduler.d.ts +3 -0
  417. package/dist/types/agent/scheduler.d.ts.map +1 -1
  418. package/dist/types/agent/task.d.ts +5 -16
  419. package/dist/types/agent/task.d.ts.map +1 -1
  420. package/dist/types/agent/task.js.map +1 -1
  421. package/dist/types/common/index.d.ts.map +1 -1
  422. package/dist/types/common/index.js +3 -0
  423. package/dist/types/common/index.js.map +1 -1
  424. package/dist/types/computer-use/index.d.ts +8 -0
  425. package/dist/types/computer-use/index.d.ts.map +1 -1
  426. package/dist/types/computer-use/index.js.map +1 -1
  427. package/dist/types/hitl/index.d.ts +5 -0
  428. package/dist/types/hitl/index.d.ts.map +1 -1
  429. package/dist/types/hitl/index.js.map +1 -1
  430. package/dist/types/ids/brand.d.ts +5 -47
  431. package/dist/types/ids/brand.d.ts.map +1 -1
  432. package/dist/types/ids/brand.js +2 -39
  433. package/dist/types/ids/brand.js.map +1 -1
  434. package/dist/types/ids/index.d.ts +59 -69
  435. package/dist/types/ids/index.d.ts.map +1 -1
  436. package/dist/types/ids/index.js +12 -10
  437. package/dist/types/ids/index.js.map +1 -1
  438. package/dist/types/permission/index.d.ts +9 -0
  439. package/dist/types/permission/index.d.ts.map +1 -1
  440. package/dist/types/permission/index.js +9 -1
  441. package/dist/types/permission/index.js.map +1 -1
  442. package/dist/types/plugin/index.d.ts +45 -4
  443. package/dist/types/plugin/index.d.ts.map +1 -1
  444. package/dist/types/plugin/index.js +7 -0
  445. package/dist/types/plugin/index.js.map +1 -1
  446. package/dist/types/run/config.d.ts +3 -0
  447. package/dist/types/run/config.d.ts.map +1 -1
  448. package/dist/types/run/emergency.d.ts +5 -0
  449. package/dist/types/run/emergency.d.ts.map +1 -1
  450. package/dist/types/run/emergency.js.map +1 -1
  451. package/dist/types/run/entity.d.ts +6 -2
  452. package/dist/types/run/entity.d.ts.map +1 -1
  453. package/dist/types/run/events.d.ts +35 -0
  454. package/dist/types/run/events.d.ts.map +1 -1
  455. package/dist/types/run/events.js.map +1 -1
  456. package/dist/types/run/index.d.ts +1 -0
  457. package/dist/types/run/index.d.ts.map +1 -1
  458. package/dist/types/run/index.js +1 -0
  459. package/dist/types/run/index.js.map +1 -1
  460. package/dist/types/run/state.d.ts +16 -15
  461. package/dist/types/run/state.d.ts.map +1 -1
  462. package/dist/types/run/state.js +41 -22
  463. package/dist/types/run/state.js.map +1 -1
  464. package/dist/types/run/stop-reason.d.ts +3 -14
  465. package/dist/types/run/stop-reason.d.ts.map +1 -1
  466. package/dist/types/run/token-budget-store.d.ts +29 -0
  467. package/dist/types/run/token-budget-store.d.ts.map +1 -0
  468. package/dist/types/run/token-budget-store.js +25 -0
  469. package/dist/types/run/token-budget-store.js.map +1 -0
  470. package/dist/types/sandbox/index.d.ts +47 -2
  471. package/dist/types/sandbox/index.d.ts.map +1 -1
  472. package/dist/types/sandbox/index.js.map +1 -1
  473. package/dist/types/session/store.d.ts +8 -0
  474. package/dist/types/session/store.d.ts.map +1 -1
  475. package/dist/types/tool/index.d.ts +15 -0
  476. package/dist/types/tool/index.d.ts.map +1 -1
  477. package/dist/types/tool/index.js.map +1 -1
  478. package/dist/utils/frontmatter.d.ts +1 -1
  479. package/dist/utils/frontmatter.js +1 -1
  480. package/dist/utils/id-format.d.ts +3 -0
  481. package/dist/utils/id-format.d.ts.map +1 -0
  482. package/dist/utils/id-format.js +9 -0
  483. package/dist/utils/id-format.js.map +1 -0
  484. package/dist/utils/id.d.ts +65 -32
  485. package/dist/utils/id.d.ts.map +1 -1
  486. package/dist/utils/id.js +168 -148
  487. package/dist/utils/id.js.map +1 -1
  488. package/dist/utils/log/exception.js +1 -1
  489. package/dist/utils/log/exception.js.map +1 -1
  490. package/dist/utils/log/index.d.ts.map +1 -1
  491. package/dist/utils/log/index.js +1 -2
  492. package/dist/utils/log/index.js.map +1 -1
  493. package/dist/utils/log/templates.d.ts.map +1 -1
  494. package/dist/utils/log/templates.js +0 -1
  495. package/dist/utils/log/templates.js.map +1 -1
  496. package/package.json +6 -1
  497. package/src/agents/AGENTS.md +1 -1
  498. package/src/agents/PipelineAgent.ts +129 -109
  499. package/src/agents/ReactiveAgent.ts +2 -0
  500. package/src/agents/RouterAgent.ts +120 -73
  501. package/src/agents/SupervisorAgent.ts +12 -4
  502. package/src/agents/budget.ts +42 -0
  503. package/src/agents/explore.ts +29 -0
  504. package/src/agents/file-definitions.ts +237 -0
  505. package/src/agents/index.ts +15 -0
  506. package/src/bridge/a2a/mapper.ts +6 -0
  507. package/src/bridge/sse/mapper.ts +22 -0
  508. package/src/bus/lock.ts +1 -10
  509. package/src/compaction/consolidation.ts +79 -0
  510. package/src/compaction/factory.ts +3 -0
  511. package/src/compaction/index.ts +34 -4
  512. package/src/compaction/manager.ts +39 -1
  513. package/src/compaction/plan.ts +52 -21
  514. package/src/compaction/salience/bm25.ts +72 -0
  515. package/src/compaction/salience/goal.ts +44 -0
  516. package/src/compaction/salience/index.ts +19 -0
  517. package/src/compaction/salience/minhash.ts +53 -0
  518. package/src/compaction/salience/score.ts +305 -0
  519. package/src/compaction/salience/tokenize.ts +89 -0
  520. package/src/compaction/salience/working-set.ts +187 -0
  521. package/src/compaction/serializer.ts +16 -0
  522. package/src/compaction/token-estimate.ts +57 -0
  523. package/src/compaction/tool-result-editing.ts +28 -26
  524. package/src/compaction/types.ts +24 -1
  525. package/src/compaction/wire.ts +5 -1
  526. package/src/config/runtime.ts +13 -1
  527. package/src/connector/builtins/http.ts +2 -8
  528. package/src/connector/builtins/webhook.ts +2 -2
  529. package/src/connector/mcp/adapter.ts +32 -0
  530. package/src/connector/tools/adapter.ts +2 -2
  531. package/src/connector/tools/definitions.ts +2 -2
  532. package/src/constants/compaction/index.ts +1 -0
  533. package/src/constants/telemetry/index.ts +0 -1
  534. package/src/contracts/api.ts +5 -1
  535. package/src/contracts/schemas.ts +6 -20
  536. package/src/invariants/index.ts +1 -1
  537. package/src/manager/agent/lifecycle.ts +380 -352
  538. package/src/manager/run/persistence.ts +35 -5
  539. package/src/plugin/index.ts +19 -0
  540. package/src/plugin/shell-hook.ts +403 -0
  541. package/src/prompt/coding-agent-doctrine.ts +118 -0
  542. package/src/prompt/index.ts +8 -0
  543. package/src/provider/errors.ts +1 -1
  544. package/src/provider/fallback.ts +4 -1
  545. package/src/provider/retry.ts +4 -1
  546. package/src/provider/token-budget.ts +158 -0
  547. package/src/public-runtime.ts +56 -28
  548. package/src/public-tools.ts +5 -0
  549. package/src/public-types.ts +65 -0
  550. package/src/registry/command/kernel-commands.ts +16 -6
  551. package/src/registry/tool/execute.ts +4 -1
  552. package/src/run/LimitChecker.ts +1 -1
  553. package/src/run/drain.ts +1 -1
  554. package/src/run/reporter.ts +15 -0
  555. package/src/run/token-budget.ts +692 -0
  556. package/src/runtime/jobs/registry.ts +123 -17
  557. package/src/runtime/query/checkpoint.ts +10 -3
  558. package/src/runtime/query/context.ts +3 -57
  559. package/src/runtime/query/executor.ts +181 -71
  560. package/src/runtime/query/guard.ts +8 -0
  561. package/src/runtime/query/index.ts +209 -62
  562. package/src/runtime/query/iteration/index.ts +53 -15
  563. package/src/runtime/query/iteration/phases/compaction.ts +130 -31
  564. package/src/runtime/query/iteration/phases/context.ts +4 -0
  565. package/src/runtime/query/iteration/phases/tool-review.ts +39 -7
  566. package/src/runtime/query/iteration/phases/working-memory.ts +23 -14
  567. package/src/runtime/query/iteration/stream-turn.ts +9 -5
  568. package/src/runtime/query/plugin-hooks.ts +24 -2
  569. package/src/runtime/query/prompt.ts +21 -4
  570. package/src/runtime/query/repeat-call.ts +42 -7
  571. package/src/runtime/query/result.ts +9 -0
  572. package/src/runtime/query/review-policy.ts +231 -0
  573. package/src/runtime/query/run-state.ts +4 -0
  574. package/src/runtime/query/steering.ts +32 -0
  575. package/src/runtime/query/token-budget.ts +127 -0
  576. package/src/runtime/query/tool-output-budget.ts +33 -10
  577. package/src/runtime/query/tool-pause.ts +4 -2
  578. package/src/runtime/query/tooling.ts +8 -0
  579. package/src/sandbox/file-walk-program.ts +93 -0
  580. package/src/sandbox/file-walk.ts +463 -0
  581. package/src/sandbox/index.ts +2 -0
  582. package/src/sandbox/provider/local.ts +136 -13
  583. package/src/scheduler/completion-inbox.ts +29 -19
  584. package/src/scheduler/delegating.ts +35 -13
  585. package/src/scheduler/local.ts +5 -7
  586. package/src/session/index.ts +0 -1
  587. package/src/session/workspace/path-builder.ts +5 -4
  588. package/src/store/feedback/disk.ts +4 -6
  589. package/src/store/index.ts +2 -0
  590. package/src/store/memory/disk.ts +3 -11
  591. package/src/store/run/checkpoint-disk.ts +14 -27
  592. package/src/store/run/claim-disk.ts +1 -1
  593. package/src/store/run/conformance.ts +55 -39
  594. package/src/store/run/disk.ts +19 -4
  595. package/src/store/run/token-budget-disk.ts +247 -0
  596. package/src/store/session/disk.ts +36 -29
  597. package/src/store/session/memory.ts +38 -1
  598. package/src/store/task/disk.ts +7 -8
  599. package/src/store/topic/memory.ts +13 -1
  600. package/src/test-support/ids.ts +62 -59
  601. package/src/tools/advisory/index.ts +3 -1
  602. package/src/tools/builtins/bash.ts +241 -70
  603. package/src/tools/builtins/computer-use.ts +6 -2
  604. package/src/tools/builtins/edit.ts +2 -2
  605. package/src/tools/builtins/glob.ts +104 -122
  606. package/src/tools/builtins/grep.ts +166 -132
  607. package/src/tools/builtins/ls.ts +4 -4
  608. package/src/tools/builtins/lsp.ts +2 -2
  609. package/src/tools/builtins/read-file.ts +2 -2
  610. package/src/tools/builtins/write-file.ts +2 -2
  611. package/src/tools/coordinator/ask-user-question.ts +284 -0
  612. package/src/tools/coordinator/index.ts +5 -248
  613. package/src/tools/memory/read.ts +2 -4
  614. package/src/tools/paths.ts +64 -0
  615. package/src/tools/roster.ts +46 -0
  616. package/src/types/agent/base.ts +6 -0
  617. package/src/types/agent/pipeline.ts +3 -0
  618. package/src/types/agent/router.ts +2 -3
  619. package/src/types/agent/scheduler.ts +4 -0
  620. package/src/types/agent/task.ts +5 -18
  621. package/src/types/common/index.ts +3 -0
  622. package/src/types/computer-use/index.ts +8 -0
  623. package/src/types/hitl/index.ts +5 -0
  624. package/src/types/ids/brand.ts +5 -48
  625. package/src/types/ids/index.ts +66 -75
  626. package/src/types/permission/index.ts +11 -0
  627. package/src/types/plugin/index.ts +49 -1
  628. package/src/types/run/config.ts +3 -0
  629. package/src/types/run/emergency.ts +5 -0
  630. package/src/types/run/entity.ts +6 -2
  631. package/src/types/run/events.ts +37 -0
  632. package/src/types/run/index.ts +1 -0
  633. package/src/types/run/state.ts +58 -30
  634. package/src/types/run/stop-reason.ts +3 -14
  635. package/src/types/run/token-budget-store.ts +54 -0
  636. package/src/types/sandbox/index.ts +53 -0
  637. package/src/types/session/store.ts +8 -0
  638. package/src/types/tool/index.ts +15 -0
  639. package/src/utils/frontmatter.ts +1 -1
  640. package/src/utils/id-format.ts +9 -0
  641. package/src/utils/id.ts +222 -181
  642. package/src/utils/log/exception.ts +1 -1
  643. package/src/utils/log/index.ts +1 -2
  644. package/src/utils/log/templates.ts +0 -1
  645. package/dist/session/migration/errors.d.ts +0 -26
  646. package/dist/session/migration/errors.d.ts.map +0 -1
  647. package/dist/session/migration/errors.js +0 -22
  648. package/dist/session/migration/errors.js.map +0 -1
  649. package/dist/session/migration/filesystem.d.ts +0 -122
  650. package/dist/session/migration/filesystem.d.ts.map +0 -1
  651. package/dist/session/migration/filesystem.js +0 -379
  652. package/dist/session/migration/filesystem.js.map +0 -1
  653. package/dist/session/migration/id-prefix.d.ts +0 -113
  654. package/dist/session/migration/id-prefix.d.ts.map +0 -1
  655. package/dist/session/migration/id-prefix.js +0 -132
  656. package/dist/session/migration/id-prefix.js.map +0 -1
  657. package/dist/session/migration/index.d.ts +0 -12
  658. package/dist/session/migration/index.d.ts.map +0 -1
  659. package/dist/session/migration/index.js +0 -12
  660. package/dist/session/migration/index.js.map +0 -1
  661. package/dist/session/migration/marker.d.ts +0 -57
  662. package/dist/session/migration/marker.d.ts.map +0 -1
  663. package/dist/session/migration/marker.js +0 -123
  664. package/dist/session/migration/marker.js.map +0 -1
  665. package/src/session/migration/errors.ts +0 -23
  666. package/src/session/migration/filesystem.ts +0 -468
  667. package/src/session/migration/id-prefix.ts +0 -166
  668. package/src/session/migration/index.ts +0 -41
  669. package/src/session/migration/marker.ts +0 -142
@@ -0,0 +1,187 @@
1
+ /**
2
+ * The working set: which messages stay whole for the next call.
3
+ *
4
+ * Given every message's salience, choose the cheapest actions that bring
5
+ * the estimated context under a target, lowest salience first, then the
6
+ * larger result among equally salient candidates:
7
+ *
8
+ * clear replace a tool result with the same excerpt the stale pass
9
+ * uses, retaining a saved-artifact pointer when one exists;
10
+ * stub cut an assistant narration to its first sentence — the
11
+ * decision survives, the paragraph around it does not.
12
+ *
13
+ * Nothing protected is touched, and a message is never removed: the
14
+ * shape of the conversation — every `tool_use` beside its `tool_result`,
15
+ * every turn boundary — is exactly what it was, which is what makes this
16
+ * safe to run on every iteration. Folding a span into the structured
17
+ * state and summarising stay with the existing plan, which runs after
18
+ * this when the target could not be reached. Pure, like the rest of the
19
+ * compaction arithmetic.
20
+ */
21
+
22
+ import { CHARS_PER_TOKEN } from '../../constants/limits.js'
23
+ import type { AssistantMessage, Message, ToolMessage } from '../../types/message/index.js'
24
+ import { estimateMessageTokens } from '../token-estimate.js'
25
+ import { clearToolResult, isClearedToolResult } from '../tool-result-editing.js'
26
+ import type { ScoredMessage } from './score.js'
27
+
28
+ export interface WorkingSetAction {
29
+ readonly kind: 'clear' | 'stub'
30
+ readonly index: number
31
+ readonly charsReclaimed: number
32
+ }
33
+
34
+ export interface WorkingSetPlan {
35
+ readonly messages: Message[]
36
+ readonly actions: readonly WorkingSetAction[]
37
+ readonly clearedCount: number
38
+ readonly stubbedCount: number
39
+ readonly charsReclaimed: number
40
+ readonly reclaimedTokens: number
41
+ /** Whether the actions brought the estimate under the target. */
42
+ readonly reachedTarget: boolean
43
+ }
44
+
45
+ export interface WorkingSetOptions {
46
+ readonly estimatedTokens: number
47
+ readonly targetTokens: number
48
+ /** Tool results smaller than this are not worth a placeholder. */
49
+ readonly minToolResultChars?: number
50
+ /** Assistant narrations shorter than this keep their whole text. */
51
+ readonly minNarrationChars?: number
52
+ /** Tools whose results are never cleared. */
53
+ readonly preserveTools?: readonly string[]
54
+ /**
55
+ * A message the goal names or a later turn cited is not evicted to
56
+ * reach the target while it scores above this fraction of the most
57
+ * salient evictable message. A strict target evicted the one result
58
+ * the goal named to shave the last few tokens — an eval watched it
59
+ * happen — and a pass that stops short and says so is the better
60
+ * answer: the trigger threshold still guards the hard limit.
61
+ */
62
+ readonly keepAboveFraction?: number
63
+ }
64
+
65
+ const STUB_MARK = '… (elided'
66
+
67
+ function firstSentence(text: string): string {
68
+ const match = /^[\s\S]*?[.!?](?=\s|$)/u.exec(text.trim())
69
+ return (match ? match[0] : (text.trim().split('\n')[0] ?? '')).slice(0, 240)
70
+ }
71
+
72
+ export function isStubbedNarration(content: unknown): boolean {
73
+ return typeof content === 'string' && content.includes(STUB_MARK)
74
+ }
75
+
76
+ export function planWorkingSet(
77
+ messages: readonly Message[],
78
+ scored: readonly ScoredMessage[],
79
+ options: WorkingSetOptions,
80
+ ): WorkingSetPlan {
81
+ const minTool = options.minToolResultChars ?? 1_000
82
+ const minNarration = options.minNarrationChars ?? 240
83
+ const preserve = new Set(options.preserveTools ?? [])
84
+ const toolNameByCallId = new Map<string, string>()
85
+ for (const m of messages) {
86
+ if (m.role !== 'assistant') continue
87
+ for (const call of (m as AssistantMessage).toolCalls ?? []) {
88
+ toolNameByCallId.set(call.id, call.function.name)
89
+ }
90
+ }
91
+
92
+ // Ordered by salience, not salience per token. Per token was the plan's
93
+ // first answer and it evicts the wrong thing: a large result the goal
94
+ // names loses to a small one it does not, because size divides the
95
+ // score. Among equals the larger goes first, which is where the tokens
96
+ // are.
97
+ type Candidate = {
98
+ readonly index: number
99
+ readonly kind: 'clear' | 'stub'
100
+ readonly salience: number
101
+ readonly tokens: number
102
+ /** Named by the goal or cited by a later turn: what the floor protects. */
103
+ readonly goalBound: boolean
104
+ }
105
+ const candidates: Candidate[] = []
106
+ for (const s of scored) {
107
+ if (s.protected !== null || s.tokens === 0) continue
108
+ const m = messages[s.index] as Message
109
+ if (m.role === 'tool') {
110
+ const tool = m as ToolMessage
111
+ if (tool.isError || isClearedToolResult(tool.content)) continue
112
+ if (preserve.has(toolNameByCallId.get(tool.toolCallId) ?? '')) continue
113
+ if (s.tokens * CHARS_PER_TOKEN < minTool) continue
114
+ candidates.push({
115
+ index: s.index,
116
+ kind: 'clear',
117
+ salience: s.salience,
118
+ tokens: s.tokens,
119
+ goalBound: s.relevance >= 0.5 || s.utility >= 1,
120
+ })
121
+ } else if (m.role === 'assistant' && !Array.isArray((m as AssistantMessage).toolCalls)) {
122
+ const text = typeof m.content === 'string' ? m.content : ''
123
+ if (text.length < minNarration || isStubbedNarration(text)) continue
124
+ candidates.push({
125
+ index: s.index,
126
+ kind: 'stub',
127
+ salience: s.salience,
128
+ tokens: s.tokens,
129
+ goalBound: s.relevance >= 0.5 || s.utility >= 1,
130
+ })
131
+ }
132
+ }
133
+ candidates.sort((a, b) => a.salience - b.salience || b.tokens - a.tokens || a.index - b.index)
134
+ const maxSalience = candidates.reduce((m, c) => Math.max(m, c.salience), Number.NEGATIVE_INFINITY)
135
+ const floor = maxSalience * (options.keepAboveFraction ?? 0.5)
136
+
137
+ const next = [...messages]
138
+ const actions: WorkingSetAction[] = []
139
+ let charsReclaimed = 0
140
+ let reclaimedTokens = 0
141
+ let clearedCount = 0
142
+ let stubbedCount = 0
143
+ const excess = () => options.estimatedTokens - reclaimedTokens - options.targetTokens
144
+ for (const candidate of candidates) {
145
+ if (excess() <= 0) break
146
+ // The floor guards what the goal names or a later turn used, not
147
+ // every above-average message: a run of look-alike dumps must still
148
+ // be evicted from the bottom when the target asks for it.
149
+ if (candidate.goalBound && candidate.salience > floor && candidate.salience > 0) continue
150
+ const m = next[candidate.index] as Message
151
+ if (candidate.kind === 'clear') {
152
+ const tool = m as ToolMessage
153
+ const cleared = clearToolResult(tool, toolNameByCallId.get(tool.toolCallId) ?? 'unknown')
154
+ const tokenSaving = estimateMessageTokens(tool) - estimateMessageTokens(cleared.message)
155
+ if (tokenSaving <= 0) continue
156
+ next[candidate.index] = cleared.message
157
+ charsReclaimed += cleared.charsReclaimed
158
+ reclaimedTokens += tokenSaving
159
+ clearedCount += 1
160
+ actions.push({
161
+ kind: 'clear',
162
+ index: candidate.index,
163
+ charsReclaimed: cleared.charsReclaimed,
164
+ })
165
+ } else {
166
+ const text = m.content as string
167
+ const stub = `${firstSentence(text)} ${STUB_MARK}; ${(text.length - firstSentence(text).length).toLocaleString('en-US')} characters of narration)`
168
+ const reclaimed = text.length - stub.length
169
+ if (reclaimed <= 0) continue
170
+ const stubbed = { ...(m as AssistantMessage), content: stub }
171
+ next[candidate.index] = stubbed
172
+ charsReclaimed += reclaimed
173
+ reclaimedTokens += estimateMessageTokens(m) - estimateMessageTokens(stubbed)
174
+ stubbedCount += 1
175
+ actions.push({ kind: 'stub', index: candidate.index, charsReclaimed: reclaimed })
176
+ }
177
+ }
178
+ return {
179
+ messages: next,
180
+ actions,
181
+ clearedCount,
182
+ stubbedCount,
183
+ charsReclaimed,
184
+ reclaimedTokens,
185
+ reachedTarget: excess() <= 0,
186
+ }
187
+ }
@@ -58,6 +58,20 @@ function renderToolResults(results: { tool: string; summary: string }[], evicted
58
58
  return lines.join('\n')
59
59
  }
60
60
 
61
+ /** The pinned-facts section alone, or null when nothing is pinned. */
62
+ export function renderPins(state: WorkingState): string | null {
63
+ const pins = state.pins ? [...state.pins.values()] : []
64
+ if (pins.length === 0) return null
65
+ const lines = pins.map((p) => `- **${p.key}**: ${p.text} _(${p.source})_`)
66
+ const dropped = state.evicted.pins ?? 0
67
+ if (dropped > 0) {
68
+ lines.push(
69
+ `- _(${dropped} older ${dropped === 1 ? 'pin' : 'pins'} dropped to stay within the pin budget)_`,
70
+ )
71
+ }
72
+ return `${SECTION_HEADERS.pins}\n\n${lines.join('\n')}`
73
+ }
74
+
61
75
  export function serializeState(state: WorkingState): string {
62
76
  const sections: string[] = []
63
77
 
@@ -74,6 +88,8 @@ export function serializeState(state: WorkingState): string {
74
88
  if (state.plan.length > 0) {
75
89
  sections.push(`${SECTION_HEADERS.plan}\n\n${renderPlan(state.plan)}`)
76
90
  }
91
+ const pinned = renderPins(state)
92
+ if (pinned) sections.push(pinned)
77
93
 
78
94
  if (state.environment.length > 0) {
79
95
  sections.push(
@@ -0,0 +1,57 @@
1
+ import { CHARS_PER_TOKEN } from '../constants/limits.js'
2
+ import type { Message, MessageAttachment, ToolResultBlock } from '../types/message/index.js'
3
+
4
+ // Heuristic allowances, not provider billing formulas or upper bounds. Encoded
5
+ // byte length depends on compression, not the number of visual tokens. The
6
+ // provider's measured prompt usage replaces these guesses after the next call.
7
+ const IMAGE_TOKENS = 1_024
8
+ const DOCUMENT_TOKENS = 4_096
9
+ const OMITTED_IMAGE_TOKENS = 64
10
+
11
+ function richTokens(block: Exclude<ToolResultBlock, { type: 'text' }> | MessageAttachment): number {
12
+ if (block.type === 'stored') return block.kind === 'image' ? IMAGE_TOKENS : DOCUMENT_TOKENS
13
+ if (block.type === 'document') return DOCUMENT_TOKENS
14
+ return block.modelOmission ? OMITTED_IMAGE_TOKENS : IMAGE_TOKENS
15
+ }
16
+
17
+ /**
18
+ * Provider-neutral prompt estimate shared by triggers, retention and eviction.
19
+ *
20
+ * Text and tool inputs use the existing characters-per-token approximation.
21
+ * Images and documents have an explicit allowance independent of compression
22
+ * and of whether the bytes are inline or stored externally. Without dimensions,
23
+ * page counts and a provider tokenizer their exact cost is unknown. In
24
+ * particular, serialized base64 is never treated as model-visible prose.
25
+ */
26
+ export function estimateMessageTokens(message: Message): number {
27
+ let chars = 0
28
+ let rich = 0
29
+ if (typeof message.content === 'string') chars += message.content.length
30
+ else if (message.content) {
31
+ for (const block of message.content) {
32
+ if (block.type === 'text') chars += block.text.length
33
+ else {
34
+ rich += richTokens(block)
35
+ if (block.type === 'document') chars += block.name?.length ?? 0
36
+ }
37
+ }
38
+ }
39
+ if (message.role === 'assistant') {
40
+ for (const call of message.toolCalls ?? []) {
41
+ chars += call.function.name.length + call.function.arguments.length
42
+ }
43
+ }
44
+ if (message.role === 'user') {
45
+ for (const attachment of message.attachments ?? []) {
46
+ rich += richTokens(attachment)
47
+ if (attachment.type === 'document' || attachment.type === 'stored') {
48
+ chars += attachment.name?.length ?? 0
49
+ }
50
+ }
51
+ }
52
+ return Math.ceil(chars / CHARS_PER_TOKEN) + rich
53
+ }
54
+
55
+ export function estimateMessagesTokens(messages: readonly Message[]): number {
56
+ return messages.reduce((total, message) => total + estimateMessageTokens(message), 0)
57
+ }
@@ -1,7 +1,9 @@
1
+ import { CHARS_PER_TOKEN } from '../constants/limits.js'
1
2
  import { SPILL_MARKER } from '../runtime/query/tool-output-budget.js'
2
3
  import { toolResultToText } from '../types/message/content.js'
3
4
  import type { Message, ToolMessage, ToolResultBlock } from '../types/message/index.js'
4
5
  import { findRetainedIndices } from './retention.js'
6
+ import { estimateMessageTokens } from './token-estimate.js'
5
7
 
6
8
  /**
7
9
  * Clear stale tool OUTPUT in place, without touching the conversation's
@@ -91,7 +93,9 @@ function clearedPlaceholder(
91
93
  'Read a window of it with `read` (offset/limit) or search it with `grep`.',
92
94
  )
93
95
  } else {
94
- parts.push('Call the tool again if you still need the rest.')
96
+ parts.push(
97
+ 'Full result unavailable here. Recover from a saved artifact or a read-only observation; do not repeat a state-changing action to recover its output.',
98
+ )
95
99
  }
96
100
 
97
101
  if (text.length > KEEP_HEAD_CHARS + KEEP_TAIL_CHARS) {
@@ -116,10 +120,23 @@ function clearedPlaceholder(
116
120
  export interface ToolResultEditOutcome {
117
121
  readonly messages: Message[]
118
122
  readonly clearedCount: number
119
- /** Characters removed from the model-visible history. */
123
+ /** Payload characters removed, including encoded data; not a token estimate. */
120
124
  readonly charsReclaimed: number
121
125
  }
122
126
 
127
+ /**
128
+ * One result cleared on its own, for a caller that chose it by some
129
+ * other rule than staleness. The same placeholder, the same accounting.
130
+ */
131
+ export function clearToolResult(
132
+ tool: ToolMessage,
133
+ toolName: string,
134
+ ): { readonly message: ToolMessage; readonly charsReclaimed: number } {
135
+ const size = measureContent(tool.content)
136
+ const content = clearedPlaceholder(tool.content, toolName, size)
137
+ return { message: { ...tool, content }, charsReclaimed: Math.max(0, size - content.length) }
138
+ }
139
+
123
140
  export function isClearedToolResult(content: unknown): boolean {
124
141
  return typeof content === 'string' && content.startsWith(CLEARED_PREFIX)
125
142
  }
@@ -183,39 +200,24 @@ export function clearStaleToolResults(
183
200
  if (preserve.has(toolName)) return msg
184
201
 
185
202
  const size = measureContent(tool.content)
186
- if (size < minChars) return msg
203
+ if (estimateMessageTokens(tool) * CHARS_PER_TOKEN < minChars) return msg
187
204
 
205
+ const content = clearedPlaceholder(tool.content, toolName, size)
206
+ if (estimateMessageTokens({ ...tool, content }) >= estimateMessageTokens(tool)) return msg
188
207
  clearedCount++
189
- charsReclaimed += size - 0
208
+ charsReclaimed += size - content.length
190
209
  return {
191
210
  ...tool,
192
- content: clearedPlaceholder(tool.content, toolName, size),
211
+ content,
193
212
  } satisfies ToolMessage
194
213
  })
195
214
 
196
- // Subtract what the placeholders themselves cost, so the number a
197
- // caller uses to decide "was that enough?" is the real saving and not
198
- // an overstatement.
199
- if (clearedCount > 0) {
200
- let placeholderChars = 0
201
- for (const msg of edited) {
202
- if (msg.role === 'tool' && isClearedToolResult(msg.content)) {
203
- placeholderChars += measureContent(msg.content)
204
- }
205
- }
206
- charsReclaimed = Math.max(0, charsReclaimed - placeholderChars)
207
- }
208
-
209
- return { messages: edited, clearedCount, charsReclaimed }
215
+ return { messages: edited, clearedCount, charsReclaimed: Math.max(0, charsReclaimed) }
210
216
  }
211
217
 
212
218
  /**
213
- * Size of a tool result as the model sees it.
214
- *
215
- * An image block is measured by its base64 payload, which is the whole
216
- * point: a screenshot is the single largest thing a tool result can carry,
217
- * and it is exactly the kind of output an agent reads once and never
218
- * needs again.
219
+ * Payload character count for storage telemetry. Encoded image length is
220
+ * independent of visual token cost; context decisions use the shared estimate.
219
221
  */
220
222
  function measureContent(content: ToolMessage['content']): number {
221
223
  if (typeof content === 'string') return content.length
@@ -223,7 +225,7 @@ function measureContent(content: ToolMessage['content']): number {
223
225
  let total = 0
224
226
  for (const block of content as readonly ToolResultBlock[]) {
225
227
  if (block.type === 'text') total += block.text.length
226
- else if (block.type === 'image') total += block.data.length
228
+ else if (block.type === 'image' || block.type === 'document') total += block.data.length
227
229
  }
228
230
  return total
229
231
  }
@@ -1,6 +1,15 @@
1
1
  export interface WorkingState {
2
2
  task: string
3
3
  plan: PlanSlot[]
4
+ /**
5
+ * Facts pinned by tools, by key. A tool that knows something the model
6
+ * must not lose — what its controls do, where the piece is, what has
7
+ * already failed — pins it with a key, and a later pin under the same
8
+ * key replaces it. Pins live in the working-memory slot, so they are in
9
+ * front of the model every iteration and survive compaction; the
10
+ * rule-based extractor below only guesses, a pin is stated.
11
+ */
12
+ pins: Map<string, PinSlot>
4
13
  files: Map<string, FileSlot>
5
14
  decisions: string[]
6
15
  failures: string[]
@@ -21,6 +30,20 @@ export interface WorkingState {
21
30
  evicted: Record<string, number>
22
31
  }
23
32
 
33
+ export interface PinSlot {
34
+ key: string
35
+ text: string
36
+ /** The tool that pinned it. */
37
+ source: string
38
+ updatedAt: number
39
+ }
40
+
41
+ /** What a tool result carries to pin a fact; see `ToolResult.workingState`. */
42
+ export interface WorkingStatePin {
43
+ readonly key: string
44
+ readonly text: string
45
+ }
46
+
24
47
  export interface PlanSlot {
25
48
  id: string
26
49
  label: string
@@ -44,4 +67,4 @@ export interface ToolResultSlot {
44
67
  timestamp: number
45
68
  }
46
69
 
47
- export type CompactionStrategy = 'structured' | 'sliding-window' | 'disabled'
70
+ export type CompactionStrategy = 'structured' | 'salience' | 'sliding-window' | 'disabled'
@@ -1,6 +1,6 @@
1
1
  import type { CompactionConfig } from '../config/runtime.js'
2
2
  import { WorkingStateManager } from './manager.js'
3
- import type { FileSlot, PlanSlot, ToolResultSlot, WorkingState } from './types.js'
3
+ import type { FileSlot, PinSlot, PlanSlot, ToolResultSlot, WorkingState } from './types.js'
4
4
 
5
5
  /**
6
6
  * The working state in a form that survives a process boundary.
@@ -26,6 +26,8 @@ import type { FileSlot, PlanSlot, ToolResultSlot, WorkingState } from './types.j
26
26
  export interface WorkingStateSnapshot {
27
27
  readonly task: string
28
28
  readonly plan: readonly PlanSlot[]
29
+ /** Absent in snapshots written before pins existed; restored as none. */
30
+ readonly pins?: readonly PinSlot[]
29
31
  readonly files: readonly FileSlot[]
30
32
  readonly decisions: readonly string[]
31
33
  readonly failures: readonly string[]
@@ -47,6 +49,7 @@ export function snapshotWorkingState(manager: WorkingStateManager): WorkingState
47
49
  return {
48
50
  task: state.task,
49
51
  plan: [...state.plan],
52
+ pins: [...(state.pins?.values() ?? [])],
50
53
  files: [...state.files.values()],
51
54
  decisions: [...state.decisions],
52
55
  failures: [...state.failures],
@@ -76,6 +79,7 @@ export function restoreWorkingState(
76
79
  const state: WorkingState = {
77
80
  task: snapshot.task,
78
81
  plan: [...snapshot.plan],
82
+ pins: new Map((snapshot.pins ?? []).map((p) => [p.key, p])),
79
83
  files: new Map(snapshot.files.map((f) => [f.path, f])),
80
84
  decisions: [...snapshot.decisions],
81
85
  failures: [...snapshot.failures],
@@ -17,7 +17,19 @@ export const TaskRouterConfigSchema = z
17
17
  .optional()
18
18
 
19
19
  export const CompactionConfigSchema = z.object({
20
- strategy: z.enum(['structured', 'sliding-window', 'disabled']).default('structured'),
20
+ /**
21
+ * `salience` is the default: every message scored without a model, the
22
+ * context held from half the window. `structured` is the previous
23
+ * default — positional retention, a pass only at the trigger — and one
24
+ * word away for a host that wants it back.
25
+ */
26
+ strategy: z.enum(['structured', 'salience', 'sliding-window', 'disabled']).default('salience'),
27
+ /**
28
+ * `salience` only: the fraction of the window at which the working-set
29
+ * pass starts evicting low-salience messages, well before
30
+ * `triggerThreshold` summarises. The context is held around here.
31
+ */
32
+ softTarget: z.number().min(0).max(1).optional(),
21
33
  /**
22
34
  * Optional model context-window size (tokens) the compaction trigger
23
35
  * measures the CURRENT window against. NO default: when omitted the
@@ -71,14 +71,8 @@ function stripTrailingSlashes(value: string): string {
71
71
  return value.slice(0, end)
72
72
  }
73
73
 
74
- /**
75
- * Checked once at module load, not per instance.
76
- *
77
- * `'conn_http' as const` typed this as its own literal, which satisfied
78
- * `ConnectorId` only because the id types are still structural. The
79
- * constructor is what makes the prefix a fact rather than a spelling.
80
- */
81
- const HTTP_CONNECTOR_ID = asConnectorId('conn_http')
74
+ /** Stable UUID for this built-in definition, checked once at module load. */
75
+ const HTTP_CONNECTOR_ID = asConnectorId('dafa33b2-7035-47d2-986c-8a6d2ef338f3')
82
76
 
83
77
  export class HttpConnector extends BaseConnector<HttpConnectorConfig> {
84
78
  readonly id = HTTP_CONNECTOR_ID
@@ -56,8 +56,8 @@ const WebhookSendInputSchema = z.object({
56
56
  url: z.string().url().optional(),
57
57
  })
58
58
 
59
- /** Checked once at module load see `HttpConnector`'s own constant. */
60
- const WEBHOOK_CONNECTOR_ID = asConnectorId('conn_webhook')
59
+ /** Stable UUID for this built-in definition, checked once at module load. */
60
+ const WEBHOOK_CONNECTOR_ID = asConnectorId('5d1e831d-d079-47c8-bdb6-ddf4489bb000')
61
61
 
62
62
  export class WebhookConnector extends BaseConnector<WebhookConnectorConfig> {
63
63
  readonly id = WEBHOOK_CONNECTOR_ID
@@ -1,5 +1,6 @@
1
1
  import { z } from 'zod'
2
2
  import { zodToJsonSchema } from 'zod-to-json-schema'
3
+ import type { WorkingStatePin } from '../../compaction/types.js'
3
4
  import { type UntrustedEnvelope, wrapUntrusted } from '../../tools/untrusted-envelope.js'
4
5
  import type {
5
6
  MCPJsonSchema,
@@ -11,6 +12,28 @@ import type { ToolContext, ToolDefinition, ToolResult } from '../../types/tool/i
11
12
  import type { MCPClient } from './client.js'
12
13
  import { MCPHttpRedirectError } from './http-redirect.js'
13
14
  import { admitMcpImageBatch } from './image-admission.js'
15
+
16
+ /** The resource type an MCP server returns to pin facts into working memory. */
17
+ export const WORKING_STATE_MIME = 'application/vnd.namzu.working-state+json'
18
+
19
+ function parsePins(text: string | undefined): WorkingStatePin[] {
20
+ if (!text) return []
21
+ try {
22
+ const raw: unknown = JSON.parse(text)
23
+ if (!Array.isArray(raw)) return []
24
+ return raw
25
+ .filter(
26
+ (p): p is { key: string; text: string } =>
27
+ typeof p === 'object' &&
28
+ p !== null &&
29
+ typeof (p as { key?: unknown }).key === 'string' &&
30
+ typeof (p as { text?: unknown }).text === 'string',
31
+ )
32
+ .map((p) => ({ key: p.key, text: p.text }))
33
+ } catch {
34
+ return []
35
+ }
36
+ }
14
37
  import { inlineSchemaRefs } from './schema-refs.js'
15
38
 
16
39
  /**
@@ -579,7 +602,15 @@ export function mcpToolResultToToolResult(result: MCPToolResult): ToolResult {
579
602
  // modelled `image` and `resource` blocks all along. Pass them through
580
603
  // as model-visible content when any are present.
581
604
  const blocks: ToolResultBlock[] = []
605
+ const pins: WorkingStatePin[] = []
582
606
  for (const block of result.content) {
607
+ if (block.type === 'resource' && block.resource?.mimeType === WORKING_STATE_MIME) {
608
+ // A server pins facts by returning them as a resource of this type:
609
+ // a JSON array of { key, text }. Not text for the model; the kernel
610
+ // keeps them in the working-memory slot.
611
+ for (const pin of parsePins(block.resource.text)) pins.push(pin)
612
+ continue
613
+ }
583
614
  if (block.type === 'text') {
584
615
  blocks.push({ type: 'text', text: block.text })
585
616
  } else if (block.type === 'image') {
@@ -620,6 +651,7 @@ export function mcpToolResultToToolResult(result: MCPToolResult): ToolResult {
620
651
  const output = [visibleText, invalidImageNotice].filter((part) => part.length > 0).join('\n')
621
652
 
622
653
  return {
654
+ ...(pins.length > 0 ? { workingState: pins } : {}),
623
655
  success: !result.isError,
624
656
  output,
625
657
  ...(hasRichContent ? { content: blocks } : {}),
@@ -4,7 +4,7 @@ import { renderToolSchema } from '../../registry/tool/schema.js'
4
4
  import type { ConnectorMethod } from '../../types/connector/index.js'
5
5
  import type { ConnectorId, ConnectorInstanceId } from '../../types/ids/index.js'
6
6
  import type { ToolContext, ToolDefinition, ToolResult } from '../../types/tool/index.js'
7
- import { parseConnectorInstanceId } from '../../utils/id.js'
7
+ import { asConnectorInstanceId } from '../../utils/id.js'
8
8
  import { connectorToolError } from './result.js'
9
9
 
10
10
  export function connectorMethodToTool(
@@ -96,7 +96,7 @@ export function createConnectorRouterTool(
96
96
  isConcurrencySafe: () => true,
97
97
 
98
98
  async execute(input: ConnectorRouterInput, context: ToolContext): Promise<ToolResult> {
99
- const instanceId = parseConnectorInstanceId(String(input.instanceId))
99
+ const instanceId = asConnectorInstanceId(String(input.instanceId))
100
100
  const instance = manager.getInstance(instanceId)
101
101
  if (!instance) {
102
102
  return {
@@ -2,7 +2,7 @@ import { z } from 'zod'
2
2
  import type { ConnectorManager } from '../../manager/connector/lifecycle.js'
3
3
  import { defineTool } from '../../tools/defineTool.js'
4
4
  import type { ToolDefinition } from '../../types/tool/index.js'
5
- import { parseConnectorInstanceId } from '../../utils/id.js'
5
+ import { asConnectorInstanceId } from '../../utils/id.js'
6
6
  import { connectorToolError } from './result.js'
7
7
 
8
8
  export interface ConnectorToolConfig {
@@ -30,7 +30,7 @@ export function createConnectorExecuteTool(config: ConnectorToolConfig): ToolDef
30
30
  concurrencySafe: true,
31
31
 
32
32
  async execute(input, context) {
33
- const instanceId = parseConnectorInstanceId(String(input.instance_id))
33
+ const instanceId = asConnectorInstanceId(String(input.instance_id))
34
34
  const instance = config.manager.getInstance(instanceId)
35
35
 
36
36
  if (!instance) {
@@ -12,6 +12,7 @@ export const SHELL_TOOLS = new Set(['bash'])
12
12
  export const SEARCH_TOOLS = new Set(['glob', 'grep'])
13
13
 
14
14
  export const SECTION_HEADERS = {
15
+ pins: '## Pinned by tools',
15
16
  task: '## Task',
16
17
  userRequirements: '## User Requirements',
17
18
  plan: '## Plan',
@@ -146,7 +146,6 @@ export const BOOT_EVENT_NAMES = {
146
146
  CAPABILITY_DETECTED: 'namzu.capability.detected',
147
147
  CAPABILITY_BROKEN: 'namzu.capability.broken',
148
148
  TELEMETRY_STATUS: 'namzu.telemetry.status',
149
- MIGRATION_COMPLETED: 'namzu.migration.completed',
150
149
  DISCOVERY_COMPLETED: 'namzu.discovery.completed',
151
150
  BOOT_REFUSED: 'namzu.boot.refused',
152
151
  BOOT_READY: 'namzu.boot.ready',
@@ -1,6 +1,7 @@
1
1
  import type { AgentCapabilities, AgentType } from '../types/agent/base.js'
2
2
  import type { ProjectId, RunId, SessionId } from '../types/ids/index.js'
3
3
  import type { MessageRole } from '../types/message/index.js'
4
+ import type { PermissionMode } from '../types/permission/index.js'
4
5
  import type { StopReason } from '../types/run/events.js'
5
6
 
6
7
  export type { MessageRole, StopReason, AgentType, AgentCapabilities }
@@ -106,7 +107,8 @@ export interface RunHierarchyNode {
106
107
  children: RunHierarchyNode[]
107
108
  }
108
109
 
109
- export type ApiPermissionMode = 'plan' | 'auto'
110
+ /** The wire name for the kernel's `PermissionMode`; one union, not a second copy of it. */
111
+ export type ApiPermissionMode = PermissionMode
110
112
 
111
113
  export interface RunConfig {
112
114
  model?: string
@@ -168,6 +170,8 @@ export type StreamEventType =
168
170
  | 'compaction.completed'
169
171
  /** Oversized tool results were emptied instead of the history summarized. */
170
172
  | 'compaction.tool_results_cleared'
173
+ | 'memory.consolidated'
174
+ | 'background_job.exited'
171
175
  | 'compaction.failed'
172
176
  /** A guardrail refused or corrected the run. */
173
177
  | 'guardrail.triggered'