agentfootprint 7.16.0 → 7.18.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 (295) hide show
  1. package/AGENTS.md +1 -1
  2. package/CLAUDE.md +2 -2
  3. package/ai-instructions/claude-code/SKILL.md +1 -1
  4. package/dist/conventions.js +8 -6
  5. package/dist/conventions.js.map +1 -1
  6. package/dist/core/Agent.js +81 -27
  7. package/dist/core/Agent.js.map +1 -1
  8. package/dist/core/RunnerBase.js +16 -1
  9. package/dist/core/RunnerBase.js.map +1 -1
  10. package/dist/core/agent/AgentBuilder.js +209 -43
  11. package/dist/core/agent/AgentBuilder.js.map +1 -1
  12. package/dist/core/agent/buildAgentChart.js +8 -8
  13. package/dist/core/agent/buildAgentChart.js.map +1 -1
  14. package/dist/core/agent/buildDynamicAgentChart.js +6 -6
  15. package/dist/core/agent/buildDynamicAgentChart.js.map +1 -1
  16. package/dist/core/agent/middleware/errors.js +54 -0
  17. package/dist/core/agent/middleware/errors.js.map +1 -0
  18. package/dist/core/agent/middleware/index.js +23 -0
  19. package/dist/core/agent/middleware/index.js.map +1 -0
  20. package/dist/core/agent/middleware/ledger.js +61 -0
  21. package/dist/core/agent/middleware/ledger.js.map +1 -0
  22. package/dist/core/agent/middleware/outcomes.js +66 -0
  23. package/dist/core/agent/middleware/outcomes.js.map +1 -0
  24. package/dist/core/agent/middleware/runChain.js +144 -0
  25. package/dist/core/agent/middleware/runChain.js.map +1 -0
  26. package/dist/core/agent/middleware/types.js +41 -0
  27. package/dist/core/agent/middleware/types.js.map +1 -0
  28. package/dist/core/agent/stages/prepareFinal.js +10 -0
  29. package/dist/core/agent/stages/prepareFinal.js.map +1 -1
  30. package/dist/core/agent/stages/route.js +62 -1
  31. package/dist/core/agent/stages/route.js.map +1 -1
  32. package/dist/core/agent/stages/seed.js +130 -85
  33. package/dist/core/agent/stages/seed.js.map +1 -1
  34. package/dist/core/agent/stages/toolCalls.js +195 -8
  35. package/dist/core/agent/stages/toolCalls.js.map +1 -1
  36. package/dist/core/agent/stages/window.js +141 -0
  37. package/dist/core/agent/stages/window.js.map +1 -0
  38. package/dist/core/agent/window/errors.js.map +1 -0
  39. package/dist/core/agent/window/index.js +36 -0
  40. package/dist/core/agent/window/index.js.map +1 -0
  41. package/dist/core/agent/window/notice.js +57 -0
  42. package/dist/core/agent/window/notice.js.map +1 -0
  43. package/dist/core/agent/window/options.js +93 -0
  44. package/dist/core/agent/window/options.js.map +1 -0
  45. package/dist/core/agent/window/removal.js +45 -0
  46. package/dist/core/agent/window/removal.js.map +1 -0
  47. package/dist/core/agent/window/strategies/drop.js +106 -0
  48. package/dist/core/agent/window/strategies/drop.js.map +1 -0
  49. package/dist/core/agent/window/strategies/slidingWindow.js +87 -0
  50. package/dist/core/agent/window/strategies/slidingWindow.js.map +1 -0
  51. package/dist/core/agent/window/strategies/summarizeOldest.js +178 -0
  52. package/dist/core/agent/window/strategies/summarizeOldest.js.map +1 -0
  53. package/dist/core/agent/window/strategies/tokenBudget.js +95 -0
  54. package/dist/core/agent/window/strategies/tokenBudget.js.map +1 -0
  55. package/dist/core/agent/window/strategy.js +42 -0
  56. package/dist/core/agent/window/strategy.js.map +1 -0
  57. package/dist/core/agent/window/summarize.js.map +1 -0
  58. package/dist/core/agent/{compaction → window}/turns.js +50 -28
  59. package/dist/core/agent/window/turns.js.map +1 -0
  60. package/dist/core/agent/window/types.js +23 -0
  61. package/dist/core/agent/window/types.js.map +1 -0
  62. package/dist/core/pause.js +19 -1
  63. package/dist/core/pause.js.map +1 -1
  64. package/dist/esm/conventions.d.ts +8 -6
  65. package/dist/esm/conventions.js +8 -6
  66. package/dist/esm/conventions.js.map +1 -1
  67. package/dist/esm/core/Agent.d.ts +14 -6
  68. package/dist/esm/core/Agent.js +82 -28
  69. package/dist/esm/core/Agent.js.map +1 -1
  70. package/dist/esm/core/RunnerBase.js +16 -1
  71. package/dist/esm/core/RunnerBase.js.map +1 -1
  72. package/dist/esm/core/agent/AgentBuilder.d.ts +157 -4
  73. package/dist/esm/core/agent/AgentBuilder.js +209 -43
  74. package/dist/esm/core/agent/AgentBuilder.js.map +1 -1
  75. package/dist/esm/core/agent/buildAgentChart.d.ts +16 -9
  76. package/dist/esm/core/agent/buildAgentChart.js +8 -8
  77. package/dist/esm/core/agent/buildAgentChart.js.map +1 -1
  78. package/dist/esm/core/agent/buildDynamicAgentChart.js +6 -6
  79. package/dist/esm/core/agent/buildDynamicAgentChart.js.map +1 -1
  80. package/dist/esm/core/agent/middleware/errors.d.ts +51 -0
  81. package/dist/esm/core/agent/middleware/errors.js +50 -0
  82. package/dist/esm/core/agent/middleware/errors.js.map +1 -0
  83. package/dist/esm/core/agent/middleware/index.d.ts +16 -0
  84. package/dist/esm/core/agent/middleware/index.js +16 -0
  85. package/dist/esm/core/agent/middleware/index.js.map +1 -0
  86. package/dist/esm/core/agent/middleware/ledger.d.ts +40 -0
  87. package/dist/esm/core/agent/middleware/ledger.js +57 -0
  88. package/dist/esm/core/agent/middleware/ledger.js.map +1 -0
  89. package/dist/esm/core/agent/middleware/outcomes.d.ts +45 -0
  90. package/dist/esm/core/agent/middleware/outcomes.js +60 -0
  91. package/dist/esm/core/agent/middleware/outcomes.js.map +1 -0
  92. package/dist/esm/core/agent/middleware/runChain.d.ts +104 -0
  93. package/dist/esm/core/agent/middleware/runChain.js +139 -0
  94. package/dist/esm/core/agent/middleware/runChain.js.map +1 -0
  95. package/dist/esm/core/agent/middleware/types.d.ts +191 -0
  96. package/dist/esm/core/agent/middleware/types.js +40 -0
  97. package/dist/esm/core/agent/middleware/types.js.map +1 -0
  98. package/dist/esm/core/agent/stages/prepareFinal.d.ts +10 -0
  99. package/dist/esm/core/agent/stages/prepareFinal.js +10 -0
  100. package/dist/esm/core/agent/stages/prepareFinal.js.map +1 -1
  101. package/dist/esm/core/agent/stages/route.d.ts +29 -0
  102. package/dist/esm/core/agent/stages/route.js +60 -0
  103. package/dist/esm/core/agent/stages/route.js.map +1 -1
  104. package/dist/esm/core/agent/stages/seed.d.ts +16 -1
  105. package/dist/esm/core/agent/stages/seed.js +130 -85
  106. package/dist/esm/core/agent/stages/seed.js.map +1 -1
  107. package/dist/esm/core/agent/stages/toolCalls.d.ts +23 -0
  108. package/dist/esm/core/agent/stages/toolCalls.js +195 -8
  109. package/dist/esm/core/agent/stages/toolCalls.js.map +1 -1
  110. package/dist/esm/core/agent/stages/window.d.ts +63 -0
  111. package/dist/esm/core/agent/stages/window.js +137 -0
  112. package/dist/esm/core/agent/stages/window.js.map +1 -0
  113. package/dist/esm/core/agent/types.d.ts +33 -8
  114. package/dist/esm/core/agent/window/errors.js.map +1 -0
  115. package/dist/esm/core/agent/window/index.d.ts +27 -0
  116. package/dist/esm/core/agent/window/index.js +25 -0
  117. package/dist/esm/core/agent/window/index.js.map +1 -0
  118. package/dist/esm/core/agent/window/notice.d.ts +46 -0
  119. package/dist/esm/core/agent/window/notice.js +52 -0
  120. package/dist/esm/core/agent/window/notice.js.map +1 -0
  121. package/dist/esm/core/agent/window/options.d.ts +33 -0
  122. package/dist/esm/core/agent/window/options.js +87 -0
  123. package/dist/esm/core/agent/window/options.js.map +1 -0
  124. package/dist/esm/core/agent/window/removal.d.ts +22 -0
  125. package/dist/esm/core/agent/window/removal.js +40 -0
  126. package/dist/esm/core/agent/window/removal.js.map +1 -0
  127. package/dist/esm/core/agent/window/strategies/drop.d.ts +54 -0
  128. package/dist/esm/core/agent/window/strategies/drop.js +102 -0
  129. package/dist/esm/core/agent/window/strategies/drop.js.map +1 -0
  130. package/dist/esm/core/agent/window/strategies/slidingWindow.d.ts +48 -0
  131. package/dist/esm/core/agent/window/strategies/slidingWindow.js +83 -0
  132. package/dist/esm/core/agent/window/strategies/slidingWindow.js.map +1 -0
  133. package/dist/esm/core/agent/window/strategies/summarizeOldest.d.ts +42 -0
  134. package/dist/esm/core/agent/window/strategies/summarizeOldest.js +174 -0
  135. package/dist/esm/core/agent/window/strategies/summarizeOldest.js.map +1 -0
  136. package/dist/esm/core/agent/window/strategies/tokenBudget.d.ts +47 -0
  137. package/dist/esm/core/agent/window/strategies/tokenBudget.js +91 -0
  138. package/dist/esm/core/agent/window/strategies/tokenBudget.js.map +1 -0
  139. package/dist/esm/core/agent/window/strategy.d.ts +181 -0
  140. package/dist/esm/core/agent/window/strategy.js +41 -0
  141. package/dist/esm/core/agent/window/strategy.js.map +1 -0
  142. package/dist/esm/core/agent/window/summarize.js.map +1 -0
  143. package/dist/esm/core/agent/window/turns.d.ts +108 -0
  144. package/dist/esm/core/agent/{compaction → window}/turns.js +48 -26
  145. package/dist/esm/core/agent/window/turns.js.map +1 -0
  146. package/dist/esm/core/agent/window/types.d.ts +263 -0
  147. package/dist/esm/core/agent/window/types.js +22 -0
  148. package/dist/esm/core/agent/window/types.js.map +1 -0
  149. package/dist/esm/core/pause.d.ts +42 -0
  150. package/dist/esm/core/pause.js +17 -0
  151. package/dist/esm/core/pause.js.map +1 -1
  152. package/dist/esm/events/dispatcher.d.ts +1 -1
  153. package/dist/esm/events/dispatcher.js.map +1 -1
  154. package/dist/esm/events/payloads.d.ts +26 -0
  155. package/dist/esm/events/registry.d.ts +5 -1
  156. package/dist/esm/events/registry.js +4 -0
  157. package/dist/esm/events/registry.js.map +1 -1
  158. package/dist/esm/index.d.ts +3 -2
  159. package/dist/esm/index.js +23 -6
  160. package/dist/esm/index.js.map +1 -1
  161. package/dist/esm/lib/mcp/mcpServe.js +39 -6
  162. package/dist/esm/lib/mcp/mcpServe.js.map +1 -1
  163. package/dist/esm/lib/mcp/types.d.ts +16 -0
  164. package/dist/esm/recorders/core/CompactionMeter.d.ts +25 -19
  165. package/dist/esm/recorders/core/CompactionMeter.js +27 -19
  166. package/dist/esm/recorders/core/CompactionMeter.js.map +1 -1
  167. package/dist/events/dispatcher.js.map +1 -1
  168. package/dist/events/registry.js +4 -0
  169. package/dist/events/registry.js.map +1 -1
  170. package/dist/index.js +40 -13
  171. package/dist/index.js.map +1 -1
  172. package/dist/lib/mcp/mcpServe.js +39 -6
  173. package/dist/lib/mcp/mcpServe.js.map +1 -1
  174. package/dist/recorders/core/CompactionMeter.js +27 -19
  175. package/dist/recorders/core/CompactionMeter.js.map +1 -1
  176. package/dist/types/conventions.d.ts +8 -6
  177. package/dist/types/conventions.d.ts.map +1 -1
  178. package/dist/types/core/Agent.d.ts +14 -6
  179. package/dist/types/core/Agent.d.ts.map +1 -1
  180. package/dist/types/core/RunnerBase.d.ts.map +1 -1
  181. package/dist/types/core/agent/AgentBuilder.d.ts +157 -4
  182. package/dist/types/core/agent/AgentBuilder.d.ts.map +1 -1
  183. package/dist/types/core/agent/buildAgentChart.d.ts +16 -9
  184. package/dist/types/core/agent/buildAgentChart.d.ts.map +1 -1
  185. package/dist/types/core/agent/middleware/errors.d.ts +52 -0
  186. package/dist/types/core/agent/middleware/errors.d.ts.map +1 -0
  187. package/dist/types/core/agent/middleware/index.d.ts +17 -0
  188. package/dist/types/core/agent/middleware/index.d.ts.map +1 -0
  189. package/dist/types/core/agent/middleware/ledger.d.ts +41 -0
  190. package/dist/types/core/agent/middleware/ledger.d.ts.map +1 -0
  191. package/dist/types/core/agent/middleware/outcomes.d.ts +46 -0
  192. package/dist/types/core/agent/middleware/outcomes.d.ts.map +1 -0
  193. package/dist/types/core/agent/middleware/runChain.d.ts +105 -0
  194. package/dist/types/core/agent/middleware/runChain.d.ts.map +1 -0
  195. package/dist/types/core/agent/middleware/types.d.ts +192 -0
  196. package/dist/types/core/agent/middleware/types.d.ts.map +1 -0
  197. package/dist/types/core/agent/stages/prepareFinal.d.ts +10 -0
  198. package/dist/types/core/agent/stages/prepareFinal.d.ts.map +1 -1
  199. package/dist/types/core/agent/stages/route.d.ts +29 -0
  200. package/dist/types/core/agent/stages/route.d.ts.map +1 -1
  201. package/dist/types/core/agent/stages/seed.d.ts +16 -1
  202. package/dist/types/core/agent/stages/seed.d.ts.map +1 -1
  203. package/dist/types/core/agent/stages/toolCalls.d.ts +23 -0
  204. package/dist/types/core/agent/stages/toolCalls.d.ts.map +1 -1
  205. package/dist/types/core/agent/stages/window.d.ts +64 -0
  206. package/dist/types/core/agent/stages/window.d.ts.map +1 -0
  207. package/dist/types/core/agent/types.d.ts +33 -8
  208. package/dist/types/core/agent/types.d.ts.map +1 -1
  209. package/dist/types/core/agent/window/errors.d.ts.map +1 -0
  210. package/dist/types/core/agent/window/index.d.ts +28 -0
  211. package/dist/types/core/agent/window/index.d.ts.map +1 -0
  212. package/dist/types/core/agent/window/notice.d.ts +47 -0
  213. package/dist/types/core/agent/window/notice.d.ts.map +1 -0
  214. package/dist/types/core/agent/window/options.d.ts +34 -0
  215. package/dist/types/core/agent/window/options.d.ts.map +1 -0
  216. package/dist/types/core/agent/window/removal.d.ts +23 -0
  217. package/dist/types/core/agent/window/removal.d.ts.map +1 -0
  218. package/dist/types/core/agent/window/strategies/drop.d.ts +55 -0
  219. package/dist/types/core/agent/window/strategies/drop.d.ts.map +1 -0
  220. package/dist/types/core/agent/window/strategies/slidingWindow.d.ts +49 -0
  221. package/dist/types/core/agent/window/strategies/slidingWindow.d.ts.map +1 -0
  222. package/dist/types/core/agent/window/strategies/summarizeOldest.d.ts +43 -0
  223. package/dist/types/core/agent/window/strategies/summarizeOldest.d.ts.map +1 -0
  224. package/dist/types/core/agent/window/strategies/tokenBudget.d.ts +48 -0
  225. package/dist/types/core/agent/window/strategies/tokenBudget.d.ts.map +1 -0
  226. package/dist/types/core/agent/window/strategy.d.ts +182 -0
  227. package/dist/types/core/agent/window/strategy.d.ts.map +1 -0
  228. package/dist/types/core/agent/window/summarize.d.ts.map +1 -0
  229. package/dist/types/core/agent/window/turns.d.ts +109 -0
  230. package/dist/types/core/agent/window/turns.d.ts.map +1 -0
  231. package/dist/types/core/agent/window/types.d.ts +264 -0
  232. package/dist/types/core/agent/window/types.d.ts.map +1 -0
  233. package/dist/types/core/pause.d.ts +42 -0
  234. package/dist/types/core/pause.d.ts.map +1 -1
  235. package/dist/types/events/dispatcher.d.ts +1 -1
  236. package/dist/types/events/dispatcher.d.ts.map +1 -1
  237. package/dist/types/events/payloads.d.ts +26 -0
  238. package/dist/types/events/payloads.d.ts.map +1 -1
  239. package/dist/types/events/registry.d.ts +5 -1
  240. package/dist/types/events/registry.d.ts.map +1 -1
  241. package/dist/types/index.d.ts +3 -2
  242. package/dist/types/index.d.ts.map +1 -1
  243. package/dist/types/lib/mcp/mcpServe.d.ts.map +1 -1
  244. package/dist/types/lib/mcp/types.d.ts +16 -0
  245. package/dist/types/lib/mcp/types.d.ts.map +1 -1
  246. package/dist/types/recorders/core/CompactionMeter.d.ts +25 -19
  247. package/dist/types/recorders/core/CompactionMeter.d.ts.map +1 -1
  248. package/package.json +1 -1
  249. package/dist/core/agent/compaction/errors.js.map +0 -1
  250. package/dist/core/agent/compaction/index.js +0 -24
  251. package/dist/core/agent/compaction/index.js.map +0 -1
  252. package/dist/core/agent/compaction/strategy.js +0 -152
  253. package/dist/core/agent/compaction/strategy.js.map +0 -1
  254. package/dist/core/agent/compaction/summarize.js.map +0 -1
  255. package/dist/core/agent/compaction/turns.js.map +0 -1
  256. package/dist/core/agent/compaction/types.js +0 -21
  257. package/dist/core/agent/compaction/types.js.map +0 -1
  258. package/dist/core/agent/stages/compact.js +0 -130
  259. package/dist/core/agent/stages/compact.js.map +0 -1
  260. package/dist/esm/core/agent/compaction/errors.js.map +0 -1
  261. package/dist/esm/core/agent/compaction/index.d.ts +0 -18
  262. package/dist/esm/core/agent/compaction/index.js +0 -18
  263. package/dist/esm/core/agent/compaction/index.js.map +0 -1
  264. package/dist/esm/core/agent/compaction/strategy.d.ts +0 -103
  265. package/dist/esm/core/agent/compaction/strategy.js +0 -148
  266. package/dist/esm/core/agent/compaction/strategy.js.map +0 -1
  267. package/dist/esm/core/agent/compaction/summarize.js.map +0 -1
  268. package/dist/esm/core/agent/compaction/turns.d.ts +0 -85
  269. package/dist/esm/core/agent/compaction/turns.js.map +0 -1
  270. package/dist/esm/core/agent/compaction/types.d.ts +0 -148
  271. package/dist/esm/core/agent/compaction/types.js +0 -20
  272. package/dist/esm/core/agent/compaction/types.js.map +0 -1
  273. package/dist/esm/core/agent/stages/compact.d.ts +0 -59
  274. package/dist/esm/core/agent/stages/compact.js +0 -126
  275. package/dist/esm/core/agent/stages/compact.js.map +0 -1
  276. package/dist/types/core/agent/compaction/errors.d.ts.map +0 -1
  277. package/dist/types/core/agent/compaction/index.d.ts +0 -19
  278. package/dist/types/core/agent/compaction/index.d.ts.map +0 -1
  279. package/dist/types/core/agent/compaction/strategy.d.ts +0 -104
  280. package/dist/types/core/agent/compaction/strategy.d.ts.map +0 -1
  281. package/dist/types/core/agent/compaction/summarize.d.ts.map +0 -1
  282. package/dist/types/core/agent/compaction/turns.d.ts +0 -86
  283. package/dist/types/core/agent/compaction/turns.d.ts.map +0 -1
  284. package/dist/types/core/agent/compaction/types.d.ts +0 -149
  285. package/dist/types/core/agent/compaction/types.d.ts.map +0 -1
  286. package/dist/types/core/agent/stages/compact.d.ts +0 -60
  287. package/dist/types/core/agent/stages/compact.d.ts.map +0 -1
  288. /package/dist/core/agent/{compaction → window}/errors.js +0 -0
  289. /package/dist/core/agent/{compaction → window}/summarize.js +0 -0
  290. /package/dist/esm/core/agent/{compaction → window}/errors.d.ts +0 -0
  291. /package/dist/esm/core/agent/{compaction → window}/errors.js +0 -0
  292. /package/dist/esm/core/agent/{compaction → window}/summarize.d.ts +0 -0
  293. /package/dist/esm/core/agent/{compaction → window}/summarize.js +0 -0
  294. /package/dist/types/core/agent/{compaction → window}/errors.d.ts +0 -0
  295. /package/dist/types/core/agent/{compaction → window}/summarize.d.ts +0 -0
@@ -1,103 +0,0 @@
1
- /**
2
- * compaction/strategy — INTERNAL. How a window strategy is shaped.
3
- *
4
- * Pattern: Strategy (GoF), deliberately kept private for now.
5
- * Role: core/ layer. The compaction stage does the wiring — read the
6
- * meter, write the window, emit, record, cost — and delegates the
7
- * one interesting question to a strategy:
8
- *
9
- * given the segmented turns, the meter readings, and the
10
- * options → what should the window become, and what does the
11
- * ledger need to be told about it?
12
- *
13
- * Emits: N/A. A strategy is pure decision + (optionally) its own LLM call;
14
- * it never touches scope, never emits, and never writes. That is
15
- * what makes it testable without a chart, and what keeps the
16
- * "record everything you did" duty in ONE place (the stage) rather
17
- * than duplicated per strategy.
18
- *
19
- * **Not exported from any barrel, and not part of the public API.** The only
20
- * shipped implementation is `summarizeOldestStrategy` (what `.compaction()`
21
- * configures). The interface lives here so that the day a second window
22
- * strategy is warranted it is an added file rather than a rewrite of the
23
- * stage — but a seam nobody has used twice is a guess, so it stays private
24
- * until it has a second implementation to be right about.
25
- */
26
- import type { LLMMessage } from '../../../adapters/types.js';
27
- import type { MessageOrigin } from '../../../recorders/core/CompactionMeter.js';
28
- import { type FoldabilityContext, type Turn } from './turns.js';
29
- import type { CompactionRecord, ResolvedCompaction } from './types.js';
30
- /** Everything a strategy is allowed to look at. */
31
- export interface WindowStrategyInput {
32
- /** The window as it stands, detached. */
33
- readonly history: readonly LLMMessage[];
34
- /** The same window, segmented into turns. */
35
- readonly turns: readonly Turn[];
36
- /** Per-message provenance, aligned index-for-index with `history`. */
37
- readonly origins: readonly MessageOrigin[];
38
- /** What the provider REPORTED for the last call. Counted, never guessed. */
39
- readonly measuredTokens: number;
40
- /** What must not fold: unanswered calls, the paused tool, a pending check-in. */
41
- readonly foldability: FoldabilityContext;
42
- /** The ReAct iteration this decision belongs to. */
43
- readonly iteration: number;
44
- /** The run's cancellation signal, when there is one. */
45
- readonly signal: AbortSignal | undefined;
46
- /** Wall clock, injectable so a caller can pin `survivalMs`. */
47
- readonly now: () => number;
48
- }
49
- /** One message leaving the window, with the facts an eviction event needs. */
50
- export interface WindowEviction {
51
- /** Index in the PRE-change window — the index the content hash was built on. */
52
- readonly index: number;
53
- /** How long it lived in the window. Exact; 0 when its birth is unknown. */
54
- readonly survivalMs: number;
55
- }
56
- /** What the stage should do next. Everything is optional except the record. */
57
- export interface WindowStrategyResult {
58
- /** The new window. Absent = leave the window alone. */
59
- readonly window?: readonly LLMMessage[];
60
- /**
61
- * How the meter must re-align its provenance to the new window, which is
62
- * `[...head, <one new message>, ...tail]`. Present exactly when `window` is.
63
- */
64
- readonly rebase?: {
65
- readonly headCount: number;
66
- readonly keptTailCount: number;
67
- };
68
- /** What the ledger is told. Always present — a visit always explains itself. */
69
- readonly record: CompactionRecord;
70
- /** Messages that left the window, for `context.evicted`. */
71
- readonly evictions: readonly WindowEviction[];
72
- /** `planAction` for `context.budget_pressure`. */
73
- readonly planAction: 'summarize' | 'none';
74
- /** A billed call the strategy made, for the cost channel. */
75
- readonly spend?: {
76
- readonly model: string;
77
- readonly usage: {
78
- readonly input: number;
79
- readonly output: number;
80
- };
81
- };
82
- /** A one-per-run dev warning the stage should print. */
83
- readonly warning?: string;
84
- }
85
- /**
86
- * A window strategy: what the live window should become when it is over
87
- * budget, and what the record must say about the change.
88
- */
89
- export interface WindowStrategy {
90
- /** Stable name — goes nowhere public yet; used in messages and tests. */
91
- readonly name: string;
92
- plan(input: WindowStrategyInput): Promise<WindowStrategyResult>;
93
- }
94
- /**
95
- * The shipped strategy: fold the oldest contiguous run of foldable turns into
96
- * one summary message, keeping the recent turns and stepping over anything
97
- * unresolved.
98
- *
99
- * Everything it decides, it explains. Every path returns a record — including
100
- * the paths that change nothing, which are the ones a person debugging an
101
- * over-budget window actually needs.
102
- */
103
- export declare function summarizeOldestStrategy(config: ResolvedCompaction, defaultModel: string): WindowStrategy;
@@ -1,148 +0,0 @@
1
- /**
2
- * compaction/strategy — INTERNAL. How a window strategy is shaped.
3
- *
4
- * Pattern: Strategy (GoF), deliberately kept private for now.
5
- * Role: core/ layer. The compaction stage does the wiring — read the
6
- * meter, write the window, emit, record, cost — and delegates the
7
- * one interesting question to a strategy:
8
- *
9
- * given the segmented turns, the meter readings, and the
10
- * options → what should the window become, and what does the
11
- * ledger need to be told about it?
12
- *
13
- * Emits: N/A. A strategy is pure decision + (optionally) its own LLM call;
14
- * it never touches scope, never emits, and never writes. That is
15
- * what makes it testable without a chart, and what keeps the
16
- * "record everything you did" duty in ONE place (the stage) rather
17
- * than duplicated per strategy.
18
- *
19
- * **Not exported from any barrel, and not part of the public API.** The only
20
- * shipped implementation is `summarizeOldestStrategy` (what `.compaction()`
21
- * configures). The interface lives here so that the day a second window
22
- * strategy is warranted it is an added file rather than a rewrite of the
23
- * stage — but a seam nobody has used twice is a guess, so it stays private
24
- * until it has a second implementation to be right about.
25
- */
26
- import { buildSummaryMessage, isCompactedSummary, runSummarizer } from './summarize.js';
27
- import { planFold, windowChars } from './turns.js';
28
- /**
29
- * The shipped strategy: fold the oldest contiguous run of foldable turns into
30
- * one summary message, keeping the recent turns and stepping over anything
31
- * unresolved.
32
- *
33
- * Everything it decides, it explains. Every path returns a record — including
34
- * the paths that change nothing, which are the ones a person debugging an
35
- * over-budget window actually needs.
36
- */
37
- export function summarizeOldestStrategy(config, defaultModel) {
38
- const model = config.model ?? defaultModel;
39
- return {
40
- name: 'summarize-oldest',
41
- async plan(input) {
42
- const { history, turns, origins, iteration } = input;
43
- const charsBefore = windowChars(history);
44
- const base = {
45
- iteration,
46
- measuredTokens: input.measuredTokens,
47
- thresholdTokens: config.thresholdTokens,
48
- overBudget: true,
49
- windowCharsBefore: charsBefore,
50
- };
51
- const unchanged = (refusals, extra = {}) => ({
52
- record: {
53
- ...base,
54
- foldedStageIds: [],
55
- foldedMessageCount: 0,
56
- windowCharsAfter: charsBefore,
57
- summaryChars: 0,
58
- refusals,
59
- ...extra,
60
- },
61
- evictions: [],
62
- planAction: 'none',
63
- });
64
- const plan = planFold(turns, config.keepRecentTurns, input.foldability, (turn) => isCompactedSummary(turn.messages[0]));
65
- if (plan.from === -1) {
66
- // Nothing foldable. The window stays over budget and the run proceeds
67
- // — reported, not silently truncated.
68
- return unchanged(plan.refusals);
69
- }
70
- const spanStart = turns[plan.from].start;
71
- const spanEnd = turns[plan.to].start + turns[plan.to].length;
72
- const head = history.slice(0, spanStart);
73
- const span = history.slice(spanStart, spanEnd);
74
- const tail = history.slice(spanEnd);
75
- // A broken summarizer must not take down the run.
76
- let summary;
77
- try {
78
- const result = await runSummarizer(config.summarizer, model, span, input.signal);
79
- summary = {
80
- text: result.text,
81
- usage: { input: result.usage.input, output: result.usage.output },
82
- };
83
- }
84
- catch (err) {
85
- return {
86
- ...unchanged([
87
- { reason: 'summarizer-failed', turnIndex: plan.from, messageIndex: spanStart },
88
- ...plan.refusals,
89
- ]),
90
- warning: `the summarizer threw, so nothing was folded this iteration and the window ` +
91
- `stays over budget (${input.measuredTokens} tokens vs a threshold of ` +
92
- `${config.thresholdTokens}). The run continues. Cause: ` +
93
- `${err instanceof Error ? err.message : String(err)}`,
94
- };
95
- }
96
- const summaryMessage = buildSummaryMessage(summary.text, {
97
- foldedMessageCount: span.length,
98
- iteration,
99
- model,
100
- });
101
- const spend = { model, usage: summary.usage };
102
- // Would the fold actually help? A summary plus its authored frame can be
103
- // LONGER than a handful of short turns; folding then spends a call to
104
- // grow the window and lose detail at the same time. Both sides are
105
- // measured in chars — one unit, an exact comparison, not a token guess.
106
- if (summaryMessage.content.length >= windowChars(span)) {
107
- return {
108
- ...unchanged([
109
- { reason: 'summary-not-smaller', turnIndex: plan.from, messageIndex: spanStart },
110
- ...plan.refusals,
111
- ], { summaryChars: summaryMessage.content.length, summarizerTokens: summary.usage }),
112
- spend, // the call still happened, so it still counts
113
- };
114
- }
115
- const foldedAtMs = input.now();
116
- const foldedStageIds = [];
117
- const evictions = [];
118
- for (let i = spanStart; i < spanEnd; i++) {
119
- const origin = origins[i];
120
- if (origin !== undefined && !foldedStageIds.includes(origin.stageId)) {
121
- foldedStageIds.push(origin.stageId);
122
- }
123
- evictions.push({
124
- index: i,
125
- survivalMs: origin === undefined ? 0 : Math.max(0, foldedAtMs - origin.bornAtMs),
126
- });
127
- }
128
- const window = [...head, summaryMessage, ...tail];
129
- return {
130
- window,
131
- rebase: { headCount: head.length, keptTailCount: tail.length },
132
- record: {
133
- ...base,
134
- foldedStageIds,
135
- foldedMessageCount: span.length,
136
- windowCharsAfter: windowChars(window),
137
- summaryChars: summaryMessage.content.length,
138
- summarizerTokens: summary.usage,
139
- refusals: plan.refusals,
140
- },
141
- evictions,
142
- planAction: 'summarize',
143
- spend,
144
- };
145
- },
146
- };
147
- }
148
- //# sourceMappingURL=strategy.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"strategy.js","sourceRoot":"","sources":["../../../../../src/core/agent/compaction/strategy.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAIH,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AACxF,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAsC,MAAM,YAAY,CAAC;AAiEvF;;;;;;;;GAQG;AACH,MAAM,UAAU,uBAAuB,CACrC,MAA0B,EAC1B,YAAoB;IAEpB,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,YAAY,CAAC;IAE3C,OAAO;QACL,IAAI,EAAE,kBAAkB;QAExB,KAAK,CAAC,IAAI,CAAC,KAA0B;YACnC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC;YACrD,MAAM,WAAW,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;YACzC,MAAM,IAAI,GAAG;gBACX,SAAS;gBACT,cAAc,EAAE,KAAK,CAAC,cAAc;gBACpC,eAAe,EAAE,MAAM,CAAC,eAAe;gBACvC,UAAU,EAAE,IAAI;gBAChB,iBAAiB,EAAE,WAAW;aACtB,CAAC;YACX,MAAM,SAAS,GAAG,CAChB,QAAgC,EAChC,QAAmC,EAAE,EACf,EAAE,CAAC,CAAC;gBAC1B,MAAM,EAAE;oBACN,GAAG,IAAI;oBACP,cAAc,EAAE,EAAE;oBAClB,kBAAkB,EAAE,CAAC;oBACrB,gBAAgB,EAAE,WAAW;oBAC7B,YAAY,EAAE,CAAC;oBACf,QAAQ;oBACR,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-1,85 +0,0 @@
1
- /**
2
- * compaction/turns — where a turn boundary is, and which turns may fold.
3
- *
4
- * Pattern: Pure functions over the window (no scope, no I/O, no clock).
5
- * Role: core/ layer. The whole safety argument of compaction lives here:
6
- * a fold that splits an assistant's `tool_use` from its
7
- * `tool_result` produces a request the vendor rejects, and a fold
8
- * that swallows an unanswered question destroys the referent of the
9
- * answer that has not arrived yet.
10
- * Emits: N/A.
11
- *
12
- * Testable on its own — see `test/core/agent/compaction-turns.test.ts`.
13
- */
14
- import type { LLMMessage } from '../../../adapters/types.js';
15
- import type { FoldRefusal, FoldRefusalReason } from './types.js';
16
- /**
17
- * One turn: a `user` / `assistant` / `system` message plus every `tool`
18
- * message that answers it. Tool results belong to the assistant turn that
19
- * requested them — that pairing is the thing a fold must never break.
20
- */
21
- export interface Turn {
22
- /** Index of this turn in the segmentation. */
23
- readonly index: number;
24
- /** Index of the turn's FIRST message in the window. */
25
- readonly start: number;
26
- /** Number of messages in the turn. */
27
- readonly length: number;
28
- readonly messages: readonly LLMMessage[];
29
- }
30
- /**
31
- * Segment a window into turns.
32
- *
33
- * A new turn starts at any non-`tool` message; `tool` messages join the turn
34
- * in progress. A leading `tool` message (only reachable from a hand-built
35
- * history) starts its own turn rather than being silently dropped.
36
- */
37
- export declare function segmentTurns(history: readonly LLMMessage[]): readonly Turn[];
38
- /** Context a foldability decision needs beyond the turn itself. */
39
- export interface FoldabilityContext {
40
- /** Every `toolCallId` answered anywhere in the window. */
41
- readonly answeredCallIds: ReadonlySet<string>;
42
- /** The tool call this run is paused on, when it is paused. */
43
- readonly pausedToolCallId?: string;
44
- /** True when the pause is a check-in (human consent) rather than askHuman. */
45
- readonly pausedCheckIn?: boolean;
46
- }
47
- /** Every tool_call id that has a matching `role: 'tool'` message. */
48
- export declare function answeredCallIds(history: readonly LLMMessage[]): ReadonlySet<string>;
49
- /**
50
- * Why this turn may NOT fold, or `undefined` when it may.
51
- *
52
- * Order matters only for which reason gets reported first; every check is
53
- * independent. `paused-tool` / `pending-check-in` are separated from
54
- * `unresolved-tool-call` on purpose: they are the same shape but a different
55
- * fact about the world, and "we are waiting on a human" is what the person
56
- * reading the trace needs to see.
57
- */
58
- export declare function refusalFor(turn: Turn, ctx: FoldabilityContext): FoldRefusalReason | undefined;
59
- /** The span a fold will take, plus every refusal it had to name to get there. */
60
- export interface FoldPlan {
61
- /** First turn index in the fold span; -1 when nothing folds. */
62
- readonly from: number;
63
- /** Last turn index in the fold span (inclusive); -1 when nothing folds. */
64
- readonly to: number;
65
- readonly refusals: readonly FoldRefusal[];
66
- }
67
- /**
68
- * Choose the fold span: the LONGEST CONTIGUOUS run of foldable candidate
69
- * turns, starting at the oldest foldable one.
70
- *
71
- * Contiguity is not fussiness — it is what keeps the conversation in order.
72
- * A fold replaces its span with ONE summary message; if the span skipped over
73
- * an unfoldable turn, that turn would end up after a summary of things that
74
- * happened before it. So an unfoldable turn at the front is stepped over (the
75
- * fold "takes the next oldest instead") and an unfoldable turn in the middle
76
- * ends the span. Everything not folded keeps its position.
77
- *
78
- * @param turns the window's turn segmentation
79
- * @param keepRecent how many trailing turns are off-limits
80
- * @param ctx foldability inputs
81
- * @param isSummaryTurn true for a turn that is a summary a prior fold wrote
82
- */
83
- export declare function planFold(turns: readonly Turn[], keepRecent: number, ctx: FoldabilityContext, isSummaryTurn: (turn: Turn) => boolean): FoldPlan;
84
- /** Total characters of message content in a window. Exact; not tokens. */
85
- export declare function windowChars(history: readonly LLMMessage[]): number;
@@ -1 +0,0 @@
1
- {"version":3,"file":"turns.js","sourceRoot":"","sources":["../../../../../src/core/agent/compaction/turns.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAoBH;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,OAA8B;IACzD,MAAM,KAAK,GAAW,EAAE,CAAC;IACzB,IAAI,OAAO,GAAiB,EAAE,CAAC;IAC/B,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,MAAM,KAAK,GAAG,GAAS,EA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EAAE,MAAM,EAAE,uBAAuB,EAAE,SAAS,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,CAAC,IAAI,CAAE,CAAC,KAAK,EAAE;gBACtF,GAAG,QAAQ;aACZ;SACF,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,QAAQ,EAAE,CAAC,GAAG,MAAM,EAAE,GAAG,QAAQ,CAAC,EAAE,CAAC;AAC1D,CAAC;AAED,0EAA0E;AAC1E,MAAM,UAAU,WAAW,CAAC,OAA8B;IACxD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,GAAG,IAAI,OAAO;QAAE,KAAK,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC;IACvD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -1,148 +0,0 @@
1
- /**
2
- * compaction/types — the public shape of `.compaction()` and the record it
3
- * writes into the ledger.
4
- *
5
- * Pattern: Value objects (no behavior) + one resolved-config type.
6
- * Role: core/ layer. The law this feature exists to keep is stated once,
7
- * here, because every other file in this folder implements a piece
8
- * of it: **compaction edits the WINDOW, never the LEDGER.**
9
- * Emits: N/A (types only).
10
- *
11
- * The window is `scope.history` — the array `call-llm` hands the provider.
12
- * The ledger is footprintjs's commit log, which is append-only: the turns a
13
- * fold removes from the window were committed by `seed#0` / `tool-calls#N`
14
- * BEFORE the fold and stay in those bundles byte-identical forever. A fold
15
- * therefore cannot destroy history even in principle; it can only stop
16
- * re-sending it. The summary is a CLAIM about the past, so it is filed as a
17
- * claim — its own recorded step, naming every `runtimeStageId` it folded.
18
- */
19
- import type { LLMProvider } from '../../../adapters/types.js';
20
- /**
21
- * What `.compaction({...})` accepts.
22
- *
23
- * @example
24
- * ```ts
25
- * const agent = Agent.create({ provider: anthropic(), model: 'claude-sonnet-4-5' })
26
- * .compaction({
27
- * thresholdTokens: 120_000,
28
- * summarizer: anthropic(), // usually the cheap one
29
- * model: 'claude-haiku-4-5',
30
- * keepRecentTurns: 6,
31
- * })
32
- * .build();
33
- * ```
34
- */
35
- export interface CompactionOptions {
36
- /**
37
- * Fold when the LAST call's adapter-reported input tokens exceed this.
38
- *
39
- * REQUIRED, with no default. A default budget here would be a number the
40
- * library invented for a window whose size only the consumer's model and
41
- * wallet know — and every run would silently inherit it.
42
- */
43
- readonly thresholdTokens: number;
44
- /**
45
- * How many of the most recent turns are never folded. Default 6.
46
- *
47
- * The recent turns are what the model is actually reasoning over; folding
48
- * them is how a compacting agent loses the thread.
49
- */
50
- readonly keepRecentTurns?: number;
51
- /**
52
- * The provider that writes the summary. Explicitly chosen — the library
53
- * never quietly bills your main model for compaction.
54
- */
55
- readonly summarizer: LLMProvider;
56
- /**
57
- * Model id for the summarizer call. Defaults to the agent's own model, so
58
- * `summarizer: anthropic()` alone works; name a cheap model to spend less.
59
- */
60
- readonly model?: string;
61
- }
62
- /** Resolved form — defaults applied at build time, validated once. */
63
- export interface ResolvedCompaction {
64
- readonly thresholdTokens: number;
65
- readonly keepRecentTurns: number;
66
- readonly summarizer: LLMProvider;
67
- readonly model: string | undefined;
68
- }
69
- /**
70
- * Why a turn refused to fold. Every one of these is NAMED in the commit —
71
- * a fold that took less than it could have has to say why, or the next
72
- * person debugging an over-budget window has to guess.
73
- */
74
- export type FoldRefusalReason =
75
- /** The turn holds a `role: 'system'` message. The envelope never folds. */
76
- 'system-envelope'
77
- /**
78
- * An assistant `tool_use` in this turn has no matching `tool_result` in the
79
- * window. Folding an unanswered question destroys the answer's referent —
80
- * and the referent may still arrive (a paused run resumes).
81
- */
82
- | 'unresolved-tool-call'
83
- /** The turn holds the tool call this run is currently paused on. */
84
- | 'paused-tool'
85
- /** The turn holds a tool call waiting on a human check-in decision. */
86
- | 'pending-check-in'
87
- /** Inside `keepRecentTurns` — the recent window is never a candidate. */
88
- | 'inside-keep-window'
89
- /**
90
- * The only foldable candidate is a summary a previous fold wrote. Folding a
91
- * summary of a summary with nothing new to add spends a call to lose detail.
92
- */
93
- | 'only-existing-summary'
94
- /** The summarizer threw. No fold this iteration; the window stays big. */
95
- | 'summarizer-failed'
96
- /**
97
- * The summary came back no smaller than the span it would replace, so the
98
- * fold was abandoned. Measured in chars on both sides — the same unit, an
99
- * exact comparison, not a token guess. Folding here would spend a call to
100
- * make the window BIGGER and lose the detail as well.
101
- */
102
- | 'summary-not-smaller';
103
- /** One named refusal, positioned so a reader can find the turn. */
104
- export interface FoldRefusal {
105
- readonly reason: FoldRefusalReason;
106
- /** Index of the turn in this iteration's turn segmentation. */
107
- readonly turnIndex: number;
108
- /** Index of the turn's first message in the pre-fold window. */
109
- readonly messageIndex: number;
110
- }
111
- /**
112
- * What one visit to the compaction stage put in the ledger. Appended to
113
- * `scope.compactions`, so the run's whole compaction story is one array in
114
- * the commit log — including the visits that folded NOTHING, which are the
115
- * interesting ones.
116
- *
117
- * On `windowChars*` vs tokens: the char counts are EXACT and measured here.
118
- * There is deliberately no `tokensAfter` — nothing can count the tokens of a
119
- * window that has not been sent yet, and inventing one would be exactly the
120
- * guess this feature exists to refuse. The honest "after" is the NEXT call's
121
- * `stream.llm_end` usage.
122
- */
123
- export interface CompactionRecord {
124
- /** ReAct iteration this visit belongs to. */
125
- readonly iteration: number;
126
- /** Adapter-reported input tokens of the last call — what tripped the check. */
127
- readonly measuredTokens: number;
128
- /** The budget it was compared against. */
129
- readonly thresholdTokens: number;
130
- /** True when the measurement was over budget (a fold was attempted). */
131
- readonly overBudget: boolean;
132
- /** `runtimeStageId`s of the stages that appended the folded messages. */
133
- readonly foldedStageIds: readonly string[];
134
- /** How many messages left the window. */
135
- readonly foldedMessageCount: number;
136
- /** Window size in chars before / after this visit. Exact, and not tokens. */
137
- readonly windowCharsBefore: number;
138
- readonly windowCharsAfter: number;
139
- /** Length of the summary text the summarizer produced (0 when none). */
140
- readonly summaryChars: number;
141
- /** What the summarizer call itself cost, when it reported usage. */
142
- readonly summarizerTokens?: {
143
- readonly input: number;
144
- readonly output: number;
145
- };
146
- /** Every turn that refused to fold, named. */
147
- readonly refusals: readonly FoldRefusal[];
148
- }
@@ -1,20 +0,0 @@
1
- /**
2
- * compaction/types — the public shape of `.compaction()` and the record it
3
- * writes into the ledger.
4
- *
5
- * Pattern: Value objects (no behavior) + one resolved-config type.
6
- * Role: core/ layer. The law this feature exists to keep is stated once,
7
- * here, because every other file in this folder implements a piece
8
- * of it: **compaction edits the WINDOW, never the LEDGER.**
9
- * Emits: N/A (types only).
10
- *
11
- * The window is `scope.history` — the array `call-llm` hands the provider.
12
- * The ledger is footprintjs's commit log, which is append-only: the turns a
13
- * fold removes from the window were committed by `seed#0` / `tool-calls#N`
14
- * BEFORE the fold and stay in those bundles byte-identical forever. A fold
15
- * therefore cannot destroy history even in principle; it can only stop
16
- * re-sending it. The summary is a CLAIM about the past, so it is filed as a
17
- * claim — its own recorded step, naming every `runtimeStageId` it folded.
18
- */
19
- export {};
20
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../src/core/agent/compaction/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG"}
@@ -1,59 +0,0 @@
1
- /**
2
- * compact — the loop-head stage that keeps the window inside budget.
3
- *
4
- * Reads the last call's ADAPTER-REPORTED input tokens from the compaction
5
- * meter and, when they exceed `thresholdTokens`, hands the window to a window
6
- * strategy. Runs once per ReAct iteration boundary, as the loop target, so
7
- * everything downstream in the turn — the injection engine's triggers, all
8
- * three context slots, and the wire itself — sees ONE window. No component
9
- * gets a different past than the model does.
10
- *
11
- * The law: **it edits the window, never the ledger.** The turns a fold removes
12
- * were committed by `seed#0` / `tool-calls#N` before this stage ran and stay
13
- * in those bundles byte-identical; footprintjs's commit log is append-only, so
14
- * a fold cannot erase history even in principle. What it can do is stop
15
- * re-sending it — and say so, in its own commit, naming every runtimeStageId
16
- * it folded. A compacted run is still a provable run.
17
- *
18
- * This file is deliberately all WIRING. The decision — what the window should
19
- * become — lives behind `WindowStrategy` (compaction/strategy.ts, internal),
20
- * so the duty to record, emit and cost what happened stays in exactly one
21
- * place instead of being re-implemented per strategy. `.compaction()` is the
22
- * only strategy that ships.
23
- *
24
- * Emits (existing vocabulary only — no new event types):
25
- * agentfootprint.context.evicted — one per message leaving the window
26
- * agentfootprint.context.budget_pressure — one per over-budget visit
27
- * agentfootprint.cost.tick — when a pricingTable is set: the
28
- * summarizer's call is a real
29
- * billed call and counts
30
- *
31
- * The summarizer call is deliberately NOT bracketed with
32
- * `stream.llm_start` / `llm_end`: those carry an `iteration` and every
33
- * consumer pairs them by it, so a second bracket inside one iteration would
34
- * corrupt the pairing. Its cost rides the cost channel and its tokens are
35
- * recorded in the commit instead.
36
- */
37
- import type { TypedScope } from 'footprintjs';
38
- import type { PricingTable } from '../../../adapters/types.js';
39
- import type { CompactionMeterHandle } from '../../../recorders/core/CompactionMeter.js';
40
- import type { ResolvedCompaction } from '../compaction/types.js';
41
- import type { AgentState } from '../types.js';
42
- export interface CompactStageDeps {
43
- /** Resolved `.compaction()` config (defaults already applied). */
44
- readonly config: ResolvedCompaction;
45
- /** The meter this stage reads. Attached inline by `Agent.createExecutor`. */
46
- readonly meter: CompactionMeterHandle;
47
- /** The agent's own model — the summarizer's default. */
48
- readonly defaultModel: string;
49
- /** The MAIN provider's name, for the unmeasurable refusal. */
50
- readonly providerName: string;
51
- /** Optional pricing adapter, so a summarizer call is costed like any other. */
52
- readonly pricingTable?: PricingTable;
53
- /** Optional cumulative USD cap per run. */
54
- readonly costBudget?: number;
55
- /** Injectable clock (tests pin survivalMs). */
56
- readonly now?: () => number;
57
- }
58
- /** Build the compaction stage function. */
59
- export declare function buildCompactStage(deps: CompactStageDeps): (scope: TypedScope<AgentState>) => Promise<void>;