@bastani/atomic 0.8.27 → 0.8.28-alpha.2

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 (397) hide show
  1. package/CHANGELOG.md +75 -0
  2. package/README.md +120 -118
  3. package/dist/builtin/intercom/package.json +1 -1
  4. package/dist/builtin/mcp/package.json +2 -2
  5. package/dist/builtin/subagents/package.json +1 -1
  6. package/dist/builtin/web-access/package.json +1 -1
  7. package/dist/builtin/workflows/CHANGELOG.md +22 -0
  8. package/dist/builtin/workflows/README.md +11 -9
  9. package/dist/builtin/workflows/builtin/open-claude-design.ts +150 -13
  10. package/dist/builtin/workflows/package.json +1 -1
  11. package/dist/builtin/workflows/src/authoring.d.ts +5 -2
  12. package/dist/builtin/workflows/src/extension/background-ui-adapter.ts +3 -1
  13. package/dist/builtin/workflows/src/extension/hil-answer-notifications.ts +17 -25
  14. package/dist/builtin/workflows/src/extension/index.ts +133 -18
  15. package/dist/builtin/workflows/src/extension/render-result.ts +22 -2
  16. package/dist/builtin/workflows/src/extension/workflow-schema.ts +3 -3
  17. package/dist/builtin/workflows/src/runs/foreground/executor.ts +210 -16
  18. package/dist/builtin/workflows/src/sdk-surface.ts +1 -1
  19. package/dist/builtin/workflows/src/shared/authoring-contract.d.ts +42 -5
  20. package/dist/builtin/workflows/src/shared/store-types.ts +8 -2
  21. package/dist/builtin/workflows/src/shared/store.ts +51 -0
  22. package/dist/builtin/workflows/src/shared/types.ts +14 -4
  23. package/dist/builtin/workflows/src/tui/chat-surface.ts +32 -33
  24. package/dist/builtin/workflows/src/tui/graph-view.ts +4 -1
  25. package/dist/builtin/workflows/src/tui/prompt-card.ts +6 -0
  26. package/dist/builtin/workflows/src/tui/run-detail.ts +11 -4
  27. package/dist/builtin/workflows/src/tui/stage-chat-view.ts +11 -1
  28. package/dist/builtin/workflows/src/tui/status-list.ts +32 -2
  29. package/dist/cli/args.d.ts +4 -0
  30. package/dist/cli/args.d.ts.map +1 -1
  31. package/dist/cli/args.js +35 -0
  32. package/dist/cli/args.js.map +1 -1
  33. package/dist/cli/project-trust.d.ts +10 -0
  34. package/dist/cli/project-trust.d.ts.map +1 -0
  35. package/dist/cli/project-trust.js +36 -0
  36. package/dist/cli/project-trust.js.map +1 -0
  37. package/dist/cli/startup-ui.d.ts +7 -0
  38. package/dist/cli/startup-ui.d.ts.map +1 -0
  39. package/dist/cli/startup-ui.js +57 -0
  40. package/dist/cli/startup-ui.js.map +1 -0
  41. package/dist/config.d.ts.map +1 -1
  42. package/dist/config.js +24 -3
  43. package/dist/config.js.map +1 -1
  44. package/dist/core/agent-session-runtime.d.ts +3 -1
  45. package/dist/core/agent-session-runtime.d.ts.map +1 -1
  46. package/dist/core/agent-session-runtime.js +1 -0
  47. package/dist/core/agent-session-runtime.js.map +1 -1
  48. package/dist/core/agent-session-services.d.ts +2 -1
  49. package/dist/core/agent-session-services.d.ts.map +1 -1
  50. package/dist/core/agent-session-services.js +2 -2
  51. package/dist/core/agent-session-services.js.map +1 -1
  52. package/dist/core/agent-session.d.ts +9 -5
  53. package/dist/core/agent-session.d.ts.map +1 -1
  54. package/dist/core/agent-session.js +205 -51
  55. package/dist/core/agent-session.js.map +1 -1
  56. package/dist/core/auth-guidance.d.ts +10 -1
  57. package/dist/core/auth-guidance.d.ts.map +1 -1
  58. package/dist/core/auth-guidance.js +26 -1
  59. package/dist/core/auth-guidance.js.map +1 -1
  60. package/dist/core/auth-storage.d.ts.map +1 -1
  61. package/dist/core/auth-storage.js +4 -3
  62. package/dist/core/auth-storage.js.map +1 -1
  63. package/dist/core/compaction/branch-summarization.d.ts +5 -3
  64. package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
  65. package/dist/core/compaction/branch-summarization.js +16 -10
  66. package/dist/core/compaction/branch-summarization.js.map +1 -1
  67. package/dist/core/compaction/compaction.d.ts +4 -84
  68. package/dist/core/compaction/compaction.d.ts.map +1 -1
  69. package/dist/core/compaction/compaction.js +20 -502
  70. package/dist/core/compaction/compaction.js.map +1 -1
  71. package/dist/core/compaction/context-compaction.d.ts.map +1 -1
  72. package/dist/core/compaction/context-compaction.js +39 -82
  73. package/dist/core/compaction/context-compaction.js.map +1 -1
  74. package/dist/core/compaction/index.d.ts +1 -1
  75. package/dist/core/compaction/index.d.ts.map +1 -1
  76. package/dist/core/compaction/index.js +1 -1
  77. package/dist/core/compaction/index.js.map +1 -1
  78. package/dist/core/compaction/utils.d.ts +1 -1
  79. package/dist/core/compaction/utils.d.ts.map +1 -1
  80. package/dist/core/compaction/utils.js +1 -1
  81. package/dist/core/compaction/utils.js.map +1 -1
  82. package/dist/core/experimental.d.ts +2 -0
  83. package/dist/core/experimental.d.ts.map +1 -0
  84. package/dist/core/experimental.js +5 -0
  85. package/dist/core/experimental.js.map +1 -0
  86. package/dist/core/export-html/template.js +19 -6
  87. package/dist/core/extensions/index.d.ts +1 -1
  88. package/dist/core/extensions/index.d.ts.map +1 -1
  89. package/dist/core/extensions/index.js.map +1 -1
  90. package/dist/core/extensions/loader.d.ts +1 -1
  91. package/dist/core/extensions/loader.d.ts.map +1 -1
  92. package/dist/core/extensions/loader.js +6 -4
  93. package/dist/core/extensions/loader.js.map +1 -1
  94. package/dist/core/extensions/runner.d.ts +11 -4
  95. package/dist/core/extensions/runner.d.ts.map +1 -1
  96. package/dist/core/extensions/runner.js +53 -3
  97. package/dist/core/extensions/runner.js.map +1 -1
  98. package/dist/core/extensions/types.d.ts +44 -12
  99. package/dist/core/extensions/types.d.ts.map +1 -1
  100. package/dist/core/extensions/types.js.map +1 -1
  101. package/dist/core/footer-data-provider.d.ts +2 -0
  102. package/dist/core/footer-data-provider.d.ts.map +1 -1
  103. package/dist/core/footer-data-provider.js +27 -1
  104. package/dist/core/footer-data-provider.js.map +1 -1
  105. package/dist/core/index.d.ts +2 -1
  106. package/dist/core/index.d.ts.map +1 -1
  107. package/dist/core/index.js +1 -0
  108. package/dist/core/index.js.map +1 -1
  109. package/dist/core/messages.d.ts +1 -11
  110. package/dist/core/messages.d.ts.map +1 -1
  111. package/dist/core/messages.js +10 -25
  112. package/dist/core/messages.js.map +1 -1
  113. package/dist/core/model-registry.d.ts.map +1 -1
  114. package/dist/core/model-registry.js +64 -7
  115. package/dist/core/model-registry.js.map +1 -1
  116. package/dist/core/model-resolver.d.ts.map +1 -1
  117. package/dist/core/model-resolver.js +1 -0
  118. package/dist/core/model-resolver.js.map +1 -1
  119. package/dist/core/output-guard.d.ts +1 -0
  120. package/dist/core/output-guard.d.ts.map +1 -1
  121. package/dist/core/output-guard.js +52 -22
  122. package/dist/core/output-guard.js.map +1 -1
  123. package/dist/core/package-manager.d.ts +1 -0
  124. package/dist/core/package-manager.d.ts.map +1 -1
  125. package/dist/core/package-manager.js +20 -8
  126. package/dist/core/package-manager.js.map +1 -1
  127. package/dist/core/project-trust.d.ts +15 -0
  128. package/dist/core/project-trust.d.ts.map +1 -0
  129. package/dist/core/project-trust.js +58 -0
  130. package/dist/core/project-trust.js.map +1 -0
  131. package/dist/core/prompt-templates.d.ts +5 -4
  132. package/dist/core/prompt-templates.d.ts.map +1 -1
  133. package/dist/core/prompt-templates.js +30 -29
  134. package/dist/core/prompt-templates.js.map +1 -1
  135. package/dist/core/provider-attribution.d.ts +4 -0
  136. package/dist/core/provider-attribution.d.ts.map +1 -0
  137. package/dist/core/provider-attribution.js +73 -0
  138. package/dist/core/provider-attribution.js.map +1 -0
  139. package/dist/core/provider-display-names.d.ts.map +1 -1
  140. package/dist/core/provider-display-names.js +3 -0
  141. package/dist/core/provider-display-names.js.map +1 -1
  142. package/dist/core/resolve-config-value.d.ts +9 -1
  143. package/dist/core/resolve-config-value.d.ts.map +1 -1
  144. package/dist/core/resolve-config-value.js +134 -11
  145. package/dist/core/resolve-config-value.js.map +1 -1
  146. package/dist/core/resource-loader.d.ts +12 -2
  147. package/dist/core/resource-loader.d.ts.map +1 -1
  148. package/dist/core/resource-loader.js +108 -18
  149. package/dist/core/resource-loader.js.map +1 -1
  150. package/dist/core/sdk.d.ts.map +1 -1
  151. package/dist/core/sdk.js +12 -42
  152. package/dist/core/sdk.js.map +1 -1
  153. package/dist/core/session-manager.d.ts +11 -15
  154. package/dist/core/session-manager.d.ts.map +1 -1
  155. package/dist/core/session-manager.js +111 -111
  156. package/dist/core/session-manager.js.map +1 -1
  157. package/dist/core/settings-manager.d.ts +15 -5
  158. package/dist/core/settings-manager.d.ts.map +1 -1
  159. package/dist/core/settings-manager.js +69 -14
  160. package/dist/core/settings-manager.js.map +1 -1
  161. package/dist/core/slash-commands.d.ts.map +1 -1
  162. package/dist/core/slash-commands.js +1 -0
  163. package/dist/core/slash-commands.js.map +1 -1
  164. package/dist/core/system-prompt.d.ts.map +1 -1
  165. package/dist/core/system-prompt.js +0 -3
  166. package/dist/core/system-prompt.js.map +1 -1
  167. package/dist/core/tools/bash.d.ts.map +1 -1
  168. package/dist/core/tools/bash.js +2 -1
  169. package/dist/core/tools/bash.js.map +1 -1
  170. package/dist/core/tools/edit.d.ts.map +1 -1
  171. package/dist/core/tools/edit.js +7 -10
  172. package/dist/core/tools/edit.js.map +1 -1
  173. package/dist/core/tools/find.d.ts.map +1 -1
  174. package/dist/core/tools/find.js +1 -1
  175. package/dist/core/tools/find.js.map +1 -1
  176. package/dist/core/tools/grep.d.ts.map +1 -1
  177. package/dist/core/tools/grep.js +1 -1
  178. package/dist/core/tools/grep.js.map +1 -1
  179. package/dist/core/tools/ls.d.ts.map +1 -1
  180. package/dist/core/tools/ls.js +1 -1
  181. package/dist/core/tools/ls.js.map +1 -1
  182. package/dist/core/tools/oversized-tool-result.d.ts +53 -0
  183. package/dist/core/tools/oversized-tool-result.d.ts.map +1 -0
  184. package/dist/core/tools/oversized-tool-result.js +206 -0
  185. package/dist/core/tools/oversized-tool-result.js.map +1 -0
  186. package/dist/core/tools/read.d.ts +12 -0
  187. package/dist/core/tools/read.d.ts.map +1 -1
  188. package/dist/core/tools/read.js +99 -34
  189. package/dist/core/tools/read.js.map +1 -1
  190. package/dist/core/tools/render-utils.d.ts +6 -0
  191. package/dist/core/tools/render-utils.d.ts.map +1 -1
  192. package/dist/core/tools/render-utils.js +17 -1
  193. package/dist/core/tools/render-utils.js.map +1 -1
  194. package/dist/core/tools/tool-definition-wrapper.d.ts +6 -0
  195. package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -1
  196. package/dist/core/tools/tool-definition-wrapper.js +2 -0
  197. package/dist/core/tools/tool-definition-wrapper.js.map +1 -1
  198. package/dist/core/tools/tool-limits.d.ts +25 -0
  199. package/dist/core/tools/tool-limits.d.ts.map +1 -0
  200. package/dist/core/tools/tool-limits.js +25 -0
  201. package/dist/core/tools/tool-limits.js.map +1 -0
  202. package/dist/core/tools/write.d.ts.map +1 -1
  203. package/dist/core/tools/write.js +1 -1
  204. package/dist/core/tools/write.js.map +1 -1
  205. package/dist/core/trust-manager.d.ts +31 -0
  206. package/dist/core/trust-manager.d.ts.map +1 -0
  207. package/dist/core/trust-manager.js +196 -0
  208. package/dist/core/trust-manager.js.map +1 -0
  209. package/dist/index.d.ts +12 -7
  210. package/dist/index.d.ts.map +1 -1
  211. package/dist/index.js +8 -4
  212. package/dist/index.js.map +1 -1
  213. package/dist/main.d.ts.map +1 -1
  214. package/dist/main.js +142 -30
  215. package/dist/main.js.map +1 -1
  216. package/dist/migrations.d.ts +3 -1
  217. package/dist/migrations.d.ts.map +1 -1
  218. package/dist/migrations.js +325 -7
  219. package/dist/migrations.js.map +1 -1
  220. package/dist/modes/index.d.ts +1 -1
  221. package/dist/modes/index.d.ts.map +1 -1
  222. package/dist/modes/index.js.map +1 -1
  223. package/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  224. package/dist/modes/interactive/components/bash-execution.js +2 -2
  225. package/dist/modes/interactive/components/bash-execution.js.map +1 -1
  226. package/dist/modes/interactive/components/chat-message-renderer.d.ts +1 -5
  227. package/dist/modes/interactive/components/chat-message-renderer.d.ts.map +1 -1
  228. package/dist/modes/interactive/components/chat-message-renderer.js +5 -9
  229. package/dist/modes/interactive/components/chat-message-renderer.js.map +1 -1
  230. package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -1
  231. package/dist/modes/interactive/components/chat-session-host.js +0 -3
  232. package/dist/modes/interactive/components/chat-session-host.js.map +1 -1
  233. package/dist/modes/interactive/components/footer.d.ts.map +1 -1
  234. package/dist/modes/interactive/components/footer.js +6 -0
  235. package/dist/modes/interactive/components/footer.js.map +1 -1
  236. package/dist/modes/interactive/components/index.d.ts +1 -1
  237. package/dist/modes/interactive/components/index.d.ts.map +1 -1
  238. package/dist/modes/interactive/components/index.js +1 -1
  239. package/dist/modes/interactive/components/index.js.map +1 -1
  240. package/dist/modes/interactive/components/login-dialog.d.ts +1 -1
  241. package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
  242. package/dist/modes/interactive/components/login-dialog.js +9 -16
  243. package/dist/modes/interactive/components/login-dialog.js.map +1 -1
  244. package/dist/modes/interactive/components/settings-selector.d.ts +3 -1
  245. package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
  246. package/dist/modes/interactive/components/settings-selector.js +20 -0
  247. package/dist/modes/interactive/components/settings-selector.js.map +1 -1
  248. package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  249. package/dist/modes/interactive/components/tool-execution.js +22 -0
  250. package/dist/modes/interactive/components/tool-execution.js.map +1 -1
  251. package/dist/modes/interactive/components/trust-selector.d.ts +23 -0
  252. package/dist/modes/interactive/components/trust-selector.d.ts.map +1 -0
  253. package/dist/modes/interactive/components/trust-selector.js +85 -0
  254. package/dist/modes/interactive/components/trust-selector.js.map +1 -0
  255. package/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  256. package/dist/modes/interactive/components/user-message.js +1 -1
  257. package/dist/modes/interactive/components/user-message.js.map +1 -1
  258. package/dist/modes/interactive/interactive-mode.d.ts +9 -0
  259. package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  260. package/dist/modes/interactive/interactive-mode.js +134 -36
  261. package/dist/modes/interactive/interactive-mode.js.map +1 -1
  262. package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  263. package/dist/modes/interactive/theme/theme.js +10 -0
  264. package/dist/modes/interactive/theme/theme.js.map +1 -1
  265. package/dist/modes/print-mode.d.ts.map +1 -1
  266. package/dist/modes/print-mode.js +1 -0
  267. package/dist/modes/print-mode.js.map +1 -1
  268. package/dist/modes/rpc/rpc-client.d.ts +4 -1
  269. package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  270. package/dist/modes/rpc/rpc-client.js +52 -8
  271. package/dist/modes/rpc/rpc-client.js.map +1 -1
  272. package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
  273. package/dist/modes/rpc/rpc-mode.js +24 -5
  274. package/dist/modes/rpc/rpc-mode.js.map +1 -1
  275. package/dist/modes/rpc/rpc-types.d.ts +1 -1
  276. package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
  277. package/dist/modes/rpc/rpc-types.js.map +1 -1
  278. package/dist/package-manager-cli.d.ts +6 -2
  279. package/dist/package-manager-cli.d.ts.map +1 -1
  280. package/dist/package-manager-cli.js +104 -10
  281. package/dist/package-manager-cli.js.map +1 -1
  282. package/dist/utils/changelog.d.ts +1 -0
  283. package/dist/utils/changelog.d.ts.map +1 -1
  284. package/dist/utils/changelog.js +72 -0
  285. package/dist/utils/changelog.js.map +1 -1
  286. package/dist/utils/deprecation.d.ts +4 -0
  287. package/dist/utils/deprecation.d.ts.map +1 -0
  288. package/dist/utils/deprecation.js +13 -0
  289. package/dist/utils/deprecation.js.map +1 -0
  290. package/dist/utils/git.d.ts.map +1 -1
  291. package/dist/utils/git.js +54 -22
  292. package/dist/utils/git.js.map +1 -1
  293. package/dist/utils/json.d.ts +3 -0
  294. package/dist/utils/json.d.ts.map +1 -0
  295. package/dist/utils/json.js +7 -0
  296. package/dist/utils/json.js.map +1 -0
  297. package/dist/utils/open-browser.d.ts +9 -0
  298. package/dist/utils/open-browser.d.ts.map +1 -0
  299. package/dist/utils/open-browser.js +22 -0
  300. package/dist/utils/open-browser.js.map +1 -0
  301. package/docs/compaction.md +210 -181
  302. package/docs/containerization.md +111 -0
  303. package/docs/custom-provider.md +9 -9
  304. package/docs/development.md +1 -1
  305. package/docs/docs.json +2 -0
  306. package/docs/extensions.md +71 -24
  307. package/docs/index.md +2 -0
  308. package/docs/json.md +3 -4
  309. package/docs/models.md +10 -10
  310. package/docs/packages.md +1 -1
  311. package/docs/prompt-templates.md +9 -2
  312. package/docs/providers.md +18 -5
  313. package/docs/quickstart.md +1 -0
  314. package/docs/rpc.md +3 -2
  315. package/docs/sdk.md +5 -0
  316. package/docs/security.md +56 -0
  317. package/docs/session-format.md +14 -23
  318. package/docs/sessions.md +11 -1
  319. package/docs/settings.md +23 -9
  320. package/docs/skills.md +1 -1
  321. package/docs/terminal-setup.md +44 -2
  322. package/docs/themes.md +1 -1
  323. package/docs/tmux.md +4 -2
  324. package/docs/tui.md +14 -5
  325. package/docs/usage.md +17 -3
  326. package/docs/workflows.md +11 -9
  327. package/examples/README.md +1 -1
  328. package/examples/extensions/README.md +9 -6
  329. package/examples/extensions/bash-spawn-hook.ts +1 -1
  330. package/examples/extensions/built-in-tool-renderer.ts +1 -1
  331. package/examples/extensions/claude-rules.ts +1 -1
  332. package/examples/extensions/commands.ts +1 -1
  333. package/examples/extensions/custom-compaction.ts +43 -106
  334. package/examples/extensions/custom-header.ts +1 -1
  335. package/examples/extensions/custom-provider-anthropic/index.ts +3 -3
  336. package/examples/extensions/custom-provider-anthropic/package-lock.json +4 -4
  337. package/examples/extensions/custom-provider-anthropic/package.json +6 -6
  338. package/examples/extensions/custom-provider-gitlab-duo/index.ts +55 -4
  339. package/examples/extensions/custom-provider-gitlab-duo/package.json +3 -3
  340. package/examples/extensions/doom-overlay/README.md +1 -1
  341. package/examples/extensions/doom-overlay/index.ts +2 -2
  342. package/examples/extensions/git-merge-and-resolve.ts +115 -0
  343. package/examples/extensions/gondolin/index.ts +523 -0
  344. package/examples/extensions/gondolin/package-lock.json +185 -0
  345. package/examples/extensions/gondolin/package.json +19 -0
  346. package/examples/extensions/handoff.ts +7 -45
  347. package/examples/extensions/hidden-thinking-label.ts +1 -1
  348. package/examples/extensions/inline-bash.ts +2 -2
  349. package/examples/extensions/input-transform-streaming.ts +39 -0
  350. package/examples/extensions/input-transform.ts +3 -3
  351. package/examples/extensions/interactive-shell.ts +2 -2
  352. package/examples/extensions/mac-system-theme.ts +2 -2
  353. package/examples/extensions/minimal-mode.ts +1 -1
  354. package/examples/extensions/modal-editor.ts +1 -1
  355. package/examples/extensions/model-status.ts +1 -1
  356. package/examples/extensions/overlay-qa-tests.ts +198 -179
  357. package/examples/extensions/overlay-test.ts +1 -1
  358. package/examples/extensions/pirate.ts +1 -1
  359. package/examples/extensions/preset.ts +14 -12
  360. package/examples/extensions/project-trust.ts +64 -0
  361. package/examples/extensions/prompt-customizer.ts +1 -1
  362. package/examples/extensions/qna.ts +1 -1
  363. package/examples/extensions/question.ts +1 -1
  364. package/examples/extensions/questionnaire.ts +1 -1
  365. package/examples/extensions/rainbow-editor.ts +1 -1
  366. package/examples/extensions/sandbox/index.ts +16 -14
  367. package/examples/extensions/sandbox/package-lock.json +90 -90
  368. package/examples/extensions/sandbox/package.json +17 -17
  369. package/examples/extensions/snake.ts +1 -1
  370. package/examples/extensions/space-invaders.ts +1 -1
  371. package/examples/extensions/ssh.ts +2 -2
  372. package/examples/extensions/subagent/README.md +13 -13
  373. package/examples/extensions/subagent/agents.ts +4 -2
  374. package/examples/extensions/subagent/index.ts +6 -6
  375. package/examples/extensions/summarize.ts +1 -1
  376. package/examples/extensions/tic-tac-toe.ts +1 -1
  377. package/examples/extensions/titlebar-spinner.ts +1 -1
  378. package/examples/extensions/todo.ts +1 -1
  379. package/examples/extensions/tool-override.ts +1 -1
  380. package/examples/extensions/tools.ts +6 -1
  381. package/examples/extensions/trigger-compact.ts +5 -4
  382. package/examples/extensions/with-deps/package-lock.json +4 -4
  383. package/examples/extensions/with-deps/package.json +7 -7
  384. package/examples/extensions/working-indicator.ts +4 -4
  385. package/examples/extensions/working-message-test.ts +1 -1
  386. package/examples/sdk/01-minimal.ts +1 -1
  387. package/examples/sdk/03-custom-prompt.ts +1 -1
  388. package/examples/sdk/04-skills.ts +1 -1
  389. package/examples/sdk/06-extensions.ts +2 -2
  390. package/examples/sdk/08-prompt-templates.ts +1 -1
  391. package/examples/sdk/09-api-keys-and-oauth.ts +2 -2
  392. package/examples/sdk/README.md +2 -2
  393. package/package.json +8 -8
  394. package/dist/modes/interactive/components/compaction-summary-message.d.ts +0 -16
  395. package/dist/modes/interactive/components/compaction-summary-message.d.ts.map +0 -1
  396. package/dist/modes/interactive/components/compaction-summary-message.js +0 -43
  397. package/dist/modes/interactive/components/compaction-summary-message.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"compaction.js","sourceRoot":"","sources":["../../../src/core/compaction/compaction.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EACN,YAAY,EACZ,0BAA0B,EAC1B,8BAA8B,EAC9B,mBAAmB,GACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACN,gCAAgC,EAChC,mBAAmB,GAGnB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACN,gBAAgB,EAChB,aAAa,EACb,yBAAyB,EAEzB,oBAAoB,EACpB,2BAA2B,EAC3B,qBAAqB,GACrB,MAAM,YAAY,CAAC;AAYpB;;GAEG;AACH,SAAS,qBAAqB,CAC7B,QAAwB,EACxB,OAAuB,EACvB,mBAA2B;IAE3B,MAAM,OAAO,GAAG,aAAa,EAAE,CAAC;IAEhC,+DAA+D;IAC/D,IAAI,mBAAmB,IAAI,CAAC,EAAE,CAAC;QAC9B,MAAM,cAAc,GAAG,OAAO,CAAC,mBAAmB,CAAoB,CAAC;QACvE,IAAI,CAAC,cAAc,CAAC,QAAQ,IAAI,cAAc,CAAC,OAAO,EAAE,CAAC;YACxD,qDAAqD;YAC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CAAC;IACjH,MAAM,qBAAqB,GAAG;QAC7B;YACC,IAAI,EAAE,MAAe;YACrB,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;YACtD,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;SACrB;KACD,CAAC;IAEF,MAAM,QAAQ,GAAG,MAAM,cAAc,CACpC,KAAK,EACL,EAAE,YAAY,EAAE,2BAA2B,EAAE,QAAQ,EAAE,qBAAqB,EAAE,EAC9E,KAAK,CAAC,SAAS,IAAI,aAAa,IAAI,aAAa,KAAK,KAAK;QAC1D,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE;QAClE,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CACzC,CAAC;IAEF,IAAI,QAAQ,CAAC,UAAU,KAAK,OAAO,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,qCAAqC,QAAQ,CAAC,YAAY,IAAI,eAAe,EAAE,CAAC,CAAC;IAClG,CAAC;IAED,OAAO,QAAQ,CAAC,OAAO;SACrB,MAAM,CAAC,CAAC,CAAC,EAAuC,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC;SACrE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;SAClB,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC","sourcesContent":["/**\n * Context compaction for long sessions.\n *\n * Pure functions for compaction logic. The session manager handles I/O,\n * and after compaction the session is reloaded.\n */\n\nimport type { AgentMessage, ThinkingLevel } from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model, Usage } from \"@earendil-works/pi-ai\";\nimport { completeSimple } from \"@earendil-works/pi-ai\";\nimport {\n\tconvertToLlm,\n\tcreateBranchSummaryMessage,\n\tcreateCompactionSummaryMessage,\n\tcreateCustomMessage,\n} from \"../messages.ts\";\nimport {\n\tbuildContextDeletionFilteredPath,\n\tbuildSessionContext,\n\ttype CompactionEntry,\n\ttype SessionEntry,\n} from \"../session-manager.ts\";\nimport {\n\tcomputeFileLists,\n\tcreateFileOps,\n\textractFileOpsFromMessage,\n\ttype FileOperations,\n\tformatFileOperations,\n\tSUMMARIZATION_SYSTEM_PROMPT,\n\tserializeConversation,\n} from \"./utils.ts\";\n\n// ============================================================================\n// File Operation Tracking\n// ============================================================================\n\n/** Details stored in CompactionEntry.details for file tracking */\nexport interface CompactionDetails {\n\treadFiles: string[];\n\tmodifiedFiles: string[];\n}\n\n/**\n * Extract file operations from messages and previous compaction entries.\n */\nfunction extractFileOperations(\n\tmessages: AgentMessage[],\n\tentries: SessionEntry[],\n\tprevCompactionIndex: number,\n): FileOperations {\n\tconst fileOps = createFileOps();\n\n\t// Collect from previous compaction's details (if pi-generated)\n\tif (prevCompactionIndex >= 0) {\n\t\tconst prevCompaction = entries[prevCompactionIndex] as CompactionEntry;\n\t\tif (!prevCompaction.fromHook && prevCompaction.details) {\n\t\t\t// fromHook field kept for session file compatibility\n\t\t\tconst details = prevCompaction.details as CompactionDetails;\n\t\t\tif (Array.isArray(details.readFiles)) {\n\t\t\t\tfor (const f of details.readFiles) fileOps.read.add(f);\n\t\t\t}\n\t\t\tif (Array.isArray(details.modifiedFiles)) {\n\t\t\t\tfor (const f of details.modifiedFiles) fileOps.edited.add(f);\n\t\t\t}\n\t\t}\n\t}\n\n\t// Extract from tool calls in messages\n\tfor (const msg of messages) {\n\t\textractFileOpsFromMessage(msg, fileOps);\n\t}\n\n\treturn fileOps;\n}\n\n// ============================================================================\n// Message Extraction\n// ============================================================================\n\n/**\n * Extract AgentMessage from an entry if it produces one.\n * Returns undefined for entries that don't contribute to LLM context.\n */\nfunction getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tif (entry.type === \"message\") {\n\t\treturn entry.message;\n\t}\n\tif (entry.type === \"custom_message\") {\n\t\treturn createCustomMessage(\n\t\t\tentry.customType,\n\t\t\tentry.content,\n\t\t\tentry.display,\n\t\t\tentry.details,\n\t\t\tentry.timestamp,\n\t\t\tentry.excludeFromContext,\n\t\t);\n\t}\n\tif (entry.type === \"branch_summary\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\tif (entry.type === \"compaction\") {\n\t\treturn createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nfunction getMessageFromEntryForCompaction(entry: SessionEntry): AgentMessage | undefined {\n\tif (entry.type === \"compaction\") {\n\t\treturn undefined;\n\t}\n\treturn getMessageFromEntry(entry);\n}\n\n/** Result from compact() - SessionManager adds uuid/parentUuid when saving */\nexport interface CompactionResult<T = unknown> {\n\tsummary: string;\n\tfirstKeptEntryId: string;\n\ttokensBefore: number;\n\t/** Extension-specific data (e.g., ArtifactIndex, version markers for structured compaction) */\n\tdetails?: T;\n}\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface CompactionSettings {\n\tenabled: boolean;\n\treserveTokens: number;\n\tkeepRecentTokens: number;\n}\n\nexport const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {\n\tenabled: true,\n\treserveTokens: 16384,\n\tkeepRecentTokens: 20000,\n};\n\n// ============================================================================\n// Token calculation\n// ============================================================================\n\n/**\n * Calculate total context tokens from usage.\n * Uses the native totalTokens field when available, falls back to computing from components.\n */\nexport function calculateContextTokens(usage: Usage): number {\n\treturn usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;\n}\n\n/**\n * Get usage from an assistant message if available.\n * Skips aborted and error messages as they don't have valid usage data.\n */\nfunction getAssistantUsage(msg: AgentMessage): Usage | undefined {\n\tif (msg.role === \"assistant\" && \"usage\" in msg) {\n\t\tconst assistantMsg = msg as AssistantMessage;\n\t\tif (assistantMsg.stopReason !== \"aborted\" && assistantMsg.stopReason !== \"error\" && assistantMsg.usage) {\n\t\t\treturn assistantMsg.usage;\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/**\n * Find the last non-aborted assistant message usage from session entries.\n */\nexport function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined {\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"message\") {\n\t\t\tconst usage = getAssistantUsage(entry.message);\n\t\t\tif (usage) return usage;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport interface ContextUsageEstimate {\n\ttokens: number;\n\tusageTokens: number;\n\ttrailingTokens: number;\n\tlastUsageIndex: number | null;\n}\n\nfunction getLastAssistantUsageInfo(messages: AgentMessage[]): { usage: Usage; index: number } | undefined {\n\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\tconst usage = getAssistantUsage(messages[i]);\n\t\tif (usage) return { usage, index: i };\n\t}\n\treturn undefined;\n}\n\n/**\n * Estimate context tokens from messages, using the last assistant usage when available.\n * If there are messages after the last usage, estimate their tokens with estimateTokens.\n */\nexport function estimateContextTokens(messages: AgentMessage[]): ContextUsageEstimate {\n\tconst usageInfo = getLastAssistantUsageInfo(messages);\n\n\tif (!usageInfo) {\n\t\tlet estimated = 0;\n\t\tfor (const message of messages) {\n\t\t\testimated += estimateTokens(message);\n\t\t}\n\t\treturn {\n\t\t\ttokens: estimated,\n\t\t\tusageTokens: 0,\n\t\t\ttrailingTokens: estimated,\n\t\t\tlastUsageIndex: null,\n\t\t};\n\t}\n\n\tconst usageTokens = calculateContextTokens(usageInfo.usage);\n\tlet trailingTokens = 0;\n\tfor (let i = usageInfo.index + 1; i < messages.length; i++) {\n\t\ttrailingTokens += estimateTokens(messages[i]);\n\t}\n\n\treturn {\n\t\ttokens: usageTokens + trailingTokens,\n\t\tusageTokens,\n\t\ttrailingTokens,\n\t\tlastUsageIndex: usageInfo.index,\n\t};\n}\n\n/**\n * Check if compaction should trigger based on context usage.\n */\nexport function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean {\n\tif (!settings.enabled) return false;\n\treturn contextTokens > contextWindow - settings.reserveTokens;\n}\n\n// ============================================================================\n// Cut point detection\n// ============================================================================\n\n/**\n * Estimate token count for a message using chars/4 heuristic.\n * This is conservative (overestimates tokens).\n */\nexport function estimateTokens(message: AgentMessage): number {\n\tlet chars = 0;\n\n\tswitch (message.role) {\n\t\tcase \"user\": {\n\t\t\tconst content = (message as { content: string | Array<{ type: string; text?: string }> }).content;\n\t\t\tif (typeof content === \"string\") {\n\t\t\t\tchars = content.length;\n\t\t\t} else if (Array.isArray(content)) {\n\t\t\t\tfor (const block of content) {\n\t\t\t\t\tif (block.type === \"text\" && block.text) {\n\t\t\t\t\t\tchars += block.text.length;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"assistant\": {\n\t\t\tconst assistant = message as AssistantMessage;\n\t\t\tfor (const block of assistant.content) {\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tchars += block.text.length;\n\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\tchars += block.thinking.length;\n\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\tchars += block.name.length + JSON.stringify(block.arguments).length;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"custom\":\n\t\tcase \"toolResult\": {\n\t\t\tif (typeof message.content === \"string\") {\n\t\t\t\tchars = message.content.length;\n\t\t\t} else {\n\t\t\t\tfor (const block of message.content) {\n\t\t\t\t\tif (block.type === \"text\" && block.text) {\n\t\t\t\t\t\tchars += block.text.length;\n\t\t\t\t\t}\n\t\t\t\t\tif (block.type === \"image\") {\n\t\t\t\t\t\tchars += 4800; // Estimate images as 4000 chars, or 1200 tokens\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"bashExecution\": {\n\t\t\tchars = message.command.length + message.output.length;\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"branchSummary\":\n\t\tcase \"compactionSummary\": {\n\t\t\tchars = message.summary.length;\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t}\n\n\treturn 0;\n}\n\n/**\n * Find valid cut points: indices of user, assistant, custom, or bashExecution messages.\n * Never cut at tool results (they must follow their tool call).\n * When we cut at an assistant message with tool calls, its tool results follow it\n * and will be kept.\n * BashExecutionMessage is treated like a user message (user-initiated context).\n */\nfunction findValidCutPoints(entries: SessionEntry[], startIndex: number, endIndex: number): number[] {\n\tconst cutPoints: number[] = [];\n\tfor (let i = startIndex; i < endIndex; i++) {\n\t\tconst entry = entries[i];\n\t\tswitch (entry.type) {\n\t\t\tcase \"message\": {\n\t\t\t\tconst role = entry.message.role;\n\t\t\t\tswitch (role) {\n\t\t\t\t\tcase \"bashExecution\":\n\t\t\t\t\tcase \"custom\":\n\t\t\t\t\tcase \"branchSummary\":\n\t\t\t\t\tcase \"compactionSummary\":\n\t\t\t\t\tcase \"user\":\n\t\t\t\t\tcase \"assistant\":\n\t\t\t\t\t\tcutPoints.push(i);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"toolResult\":\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"thinking_level_change\":\n\t\t\tcase \"model_change\":\n\t\t\tcase \"compaction\":\n\t\t\tcase \"context_compaction\":\n\t\t\tcase \"branch_summary\":\n\t\t\tcase \"custom\":\n\t\t\tcase \"custom_message\":\n\t\t\tcase \"label\":\n\t\t\tcase \"session_info\":\n\t\t\t\tbreak;\n\t\t}\n\n\t\t// branch_summary and custom_message are user-role messages, valid cut points\n\t\tif (entry.type === \"branch_summary\" || entry.type === \"custom_message\") {\n\t\t\tcutPoints.push(i);\n\t\t}\n\t}\n\treturn cutPoints;\n}\n\n/**\n * Find the user message (or bashExecution) that starts the turn containing the given entry index.\n * Returns -1 if no turn start found before the index.\n * BashExecutionMessage is treated like a user message for turn boundaries.\n */\nexport function findTurnStartIndex(entries: SessionEntry[], entryIndex: number, startIndex: number): number {\n\tfor (let i = entryIndex; i >= startIndex; i--) {\n\t\tconst entry = entries[i];\n\t\t// branch_summary and custom_message are user-role messages, can start a turn\n\t\tif (entry.type === \"branch_summary\" || entry.type === \"custom_message\") {\n\t\t\treturn i;\n\t\t}\n\t\tif (entry.type === \"message\") {\n\t\t\tconst role = entry.message.role;\n\t\t\tif (role === \"user\" || role === \"bashExecution\") {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\nexport interface CutPointResult {\n\t/** Index of first entry to keep */\n\tfirstKeptEntryIndex: number;\n\t/** Index of user message that starts the turn being split, or -1 if not splitting */\n\tturnStartIndex: number;\n\t/** Whether this cut splits a turn (cut point is not a user message) */\n\tisSplitTurn: boolean;\n}\n\n/**\n * Find the cut point in session entries that keeps approximately `keepRecentTokens`.\n *\n * Algorithm: Walk backwards from newest, accumulating estimated message sizes.\n * Stop when we've accumulated >= keepRecentTokens. Cut at that point.\n *\n * Can cut at user OR assistant messages (never tool results). When cutting at an\n * assistant message with tool calls, its tool results come after and will be kept.\n *\n * Returns CutPointResult with:\n * - firstKeptEntryIndex: the entry index to start keeping from\n * - turnStartIndex: if cutting mid-turn, the user message that started that turn\n * - isSplitTurn: whether we're cutting in the middle of a turn\n *\n * Only considers entries between `startIndex` and `endIndex` (exclusive).\n */\nexport function findCutPoint(\n\tentries: SessionEntry[],\n\tstartIndex: number,\n\tendIndex: number,\n\tkeepRecentTokens: number,\n): CutPointResult {\n\tconst cutPoints = findValidCutPoints(entries, startIndex, endIndex);\n\n\tif (cutPoints.length === 0) {\n\t\treturn { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };\n\t}\n\n\t// Walk backwards from newest, accumulating estimated message sizes\n\tlet accumulatedTokens = 0;\n\tlet cutIndex = cutPoints[0]; // Default: keep from first message (not header)\n\n\tfor (let i = endIndex - 1; i >= startIndex; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type !== \"message\") continue;\n\n\t\t// Estimate this message's size\n\t\tconst messageTokens = estimateTokens(entry.message);\n\t\taccumulatedTokens += messageTokens;\n\n\t\t// Check if we've exceeded the budget\n\t\tif (accumulatedTokens >= keepRecentTokens) {\n\t\t\t// Find the closest valid cut point at or after this entry\n\t\t\tfor (let c = 0; c < cutPoints.length; c++) {\n\t\t\t\tif (cutPoints[c] >= i) {\n\t\t\t\t\tcutIndex = cutPoints[c];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\n\t// Scan backwards from cutIndex to include any non-message entries (bash, settings, etc.)\n\twhile (cutIndex > startIndex) {\n\t\tconst prevEntry = entries[cutIndex - 1];\n\t\t// Stop at session header or compaction boundaries\n\t\tif (prevEntry.type === \"compaction\" || prevEntry.type === \"context_compaction\") {\n\t\t\tbreak;\n\t\t}\n\t\tif (prevEntry.type === \"message\") {\n\t\t\t// Stop if we hit any message\n\t\t\tbreak;\n\t\t}\n\t\t// Include this non-message entry (bash, settings change, etc.)\n\t\tcutIndex--;\n\t}\n\n\t// Determine if this is a split turn\n\tconst cutEntry = entries[cutIndex];\n\tconst isUserMessage = cutEntry.type === \"message\" && cutEntry.message.role === \"user\";\n\tconst turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);\n\n\treturn {\n\t\tfirstKeptEntryIndex: cutIndex,\n\t\tturnStartIndex,\n\t\tisSplitTurn: !isUserMessage && turnStartIndex !== -1,\n\t};\n}\n\n// ============================================================================\n// Summarization\n// ============================================================================\n\nconst SUMMARIZATION_PROMPT = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.\n\nUse this EXACT format:\n\n## Goal\n[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]\n\n## Constraints & Preferences\n- [Any constraints, preferences, or requirements mentioned by user]\n- [Or \"(none)\" if none were mentioned]\n\n## Progress\n### Done\n- [x] [Completed tasks/changes]\n\n### In Progress\n- [ ] [Current work]\n\n### Blocked\n- [Issues preventing progress, if any]\n\n## Key Decisions\n- **[Decision]**: [Brief rationale]\n\n## Next Steps\n1. [Ordered list of what should happen next]\n\n## Critical Context\n- [Any data, examples, or references needed to continue]\n- [Or \"(none)\" if not applicable]\n\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\n\nconst UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.\n\nUpdate the existing structured summary with new information. RULES:\n- PRESERVE all existing information from the previous summary\n- ADD new progress, decisions, and context from the new messages\n- UPDATE the Progress section: move items from \"In Progress\" to \"Done\" when completed\n- UPDATE \"Next Steps\" based on what was accomplished\n- PRESERVE exact file paths, function names, and error messages\n- If something is no longer relevant, you may remove it\n\nUse this EXACT format:\n\n## Goal\n[Preserve existing goals, add new ones if the task expanded]\n\n## Constraints & Preferences\n- [Preserve existing, add new ones discovered]\n\n## Progress\n### Done\n- [x] [Include previously done items AND newly completed items]\n\n### In Progress\n- [ ] [Current work - update based on progress]\n\n### Blocked\n- [Current blockers - remove if resolved]\n\n## Key Decisions\n- **[Decision]**: [Brief rationale] (preserve all previous, add new)\n\n## Next Steps\n1. [Update based on current state]\n\n## Critical Context\n- [Preserve important context, add new if needed]\n\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\n\n/**\n * Generate a summary of the conversation using the LLM.\n * If previousSummary is provided, uses the update prompt to merge.\n */\nexport async function generateSummary(\n\tcurrentMessages: AgentMessage[],\n\tmodel: Model<Api>,\n\treserveTokens: number,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tsignal?: AbortSignal,\n\tcustomInstructions?: string,\n\tpreviousSummary?: string,\n\tthinkingLevel?: ThinkingLevel,\n): Promise<string> {\n\tconst maxTokens = Math.min(\n\t\tMath.floor(0.8 * reserveTokens),\n\t\tmodel.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY,\n\t);\n\n\t// Use update prompt if we have a previous summary, otherwise initial prompt\n\tlet basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;\n\tif (customInstructions) {\n\t\tbasePrompt = `${basePrompt}\\n\\nAdditional focus: ${customInstructions}`;\n\t}\n\n\t// Serialize conversation to text so model doesn't try to continue it\n\t// Convert to LLM messages first (handles custom types like bashExecution, custom, etc.)\n\tconst llmMessages = convertToLlm(currentMessages);\n\tconst conversationText = serializeConversation(llmMessages);\n\n\t// Build the prompt with conversation wrapped in tags\n\tlet promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n`;\n\tif (previousSummary) {\n\t\tpromptText += `<previous-summary>\\n${previousSummary}\\n</previous-summary>\\n\\n`;\n\t}\n\tpromptText += basePrompt;\n\n\tconst summarizationMessages = [\n\t\t{\n\t\t\trole: \"user\" as const,\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\n\t\t\ttimestamp: Date.now(),\n\t\t},\n\t];\n\n\tconst completionOptions =\n\t\tmodel.reasoning && thinkingLevel && thinkingLevel !== \"off\"\n\t\t\t? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }\n\t\t\t: { maxTokens, signal, apiKey, headers };\n\n\tconst response = await completeSimple(\n\t\tmodel,\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\n\t\tcompletionOptions,\n\t);\n\n\tif (response.stopReason === \"error\") {\n\t\tthrow new Error(`Summarization failed: ${response.errorMessage || \"Unknown error\"}`);\n\t}\n\n\tconst textContent = response.content\n\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t.map((c) => c.text)\n\t\t.join(\"\\n\");\n\n\treturn textContent;\n}\n\n// ============================================================================\n// Compaction Preparation (for extensions)\n// ============================================================================\n\nexport interface CompactionPreparation {\n\t/** UUID of first entry to keep */\n\tfirstKeptEntryId: string;\n\t/** Messages that will be summarized and discarded */\n\tmessagesToSummarize: AgentMessage[];\n\t/** Messages that will be turned into turn prefix summary (if splitting) */\n\tturnPrefixMessages: AgentMessage[];\n\t/** Whether this is a split turn (cut point in middle of turn) */\n\tisSplitTurn: boolean;\n\ttokensBefore: number;\n\t/** Summary from previous compaction, for iterative update */\n\tpreviousSummary?: string;\n\t/** File operations extracted from messagesToSummarize */\n\tfileOps: FileOperations;\n\t/** Compaction settions from settings.jsonl\t*/\n\tsettings: CompactionSettings;\n}\n\nexport function prepareCompaction(\n\tpathEntries: SessionEntry[],\n\tsettings: CompactionSettings,\n): CompactionPreparation | undefined {\n\tif (pathEntries.length > 0 && pathEntries[pathEntries.length - 1].type === \"compaction\") {\n\t\treturn undefined;\n\t}\n\n\tlet prevCompactionIndex = -1;\n\tfor (let i = pathEntries.length - 1; i >= 0; i--) {\n\t\tif (pathEntries[i].type === \"compaction\") {\n\t\t\tprevCompactionIndex = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tlet previousSummary: string | undefined;\n\tlet boundaryStart = 0;\n\tif (prevCompactionIndex >= 0) {\n\t\tconst prevCompaction = pathEntries[prevCompactionIndex] as CompactionEntry;\n\t\tpreviousSummary = prevCompaction.summary;\n\t\tconst firstKeptEntryIndex = pathEntries.findIndex((entry) => entry.id === prevCompaction.firstKeptEntryId);\n\t\tboundaryStart = firstKeptEntryIndex >= 0 ? firstKeptEntryIndex : prevCompactionIndex + 1;\n\t}\n\n\tconst filteredPathEntries = buildContextDeletionFilteredPath(pathEntries);\n\tconst filteredIndexById = new Map(filteredPathEntries.map((entry, index) => [entry.id, index]));\n\tconst findFilteredIndexAtOrAfter = (rawIndex: number): number => {\n\t\tfor (let i = rawIndex; i < pathEntries.length; i++) {\n\t\t\tconst filteredIndex = filteredIndexById.get(pathEntries[i].id);\n\t\t\tif (filteredIndex !== undefined) return filteredIndex;\n\t\t}\n\t\treturn filteredPathEntries.length;\n\t};\n\tconst filteredBoundaryStart = findFilteredIndexAtOrAfter(boundaryStart);\n\tconst filteredPrevCompactionIndex =\n\t\tprevCompactionIndex >= 0 ? (filteredIndexById.get(pathEntries[prevCompactionIndex].id) ?? -1) : -1;\n\tconst boundaryEnd = filteredPathEntries.length;\n\n\tconst tokensBefore = estimateContextTokens(buildSessionContext(pathEntries).messages).tokens;\n\n\tconst cutPoint = findCutPoint(filteredPathEntries, filteredBoundaryStart, boundaryEnd, settings.keepRecentTokens);\n\n\t// Get UUID of first kept entry\n\tconst firstKeptEntry = filteredPathEntries[cutPoint.firstKeptEntryIndex];\n\tif (!firstKeptEntry?.id) {\n\t\treturn undefined; // Session needs migration\n\t}\n\tconst firstKeptEntryId = firstKeptEntry.id;\n\n\tconst historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;\n\n\t// Messages to summarize (will be discarded after summary)\n\tconst messagesToSummarize: AgentMessage[] = [];\n\tfor (let i = filteredBoundaryStart; i < historyEnd; i++) {\n\t\tconst msg = getMessageFromEntryForCompaction(filteredPathEntries[i]);\n\t\tif (msg) messagesToSummarize.push(msg);\n\t}\n\n\t// Messages for turn prefix summary (if splitting a turn)\n\tconst turnPrefixMessages: AgentMessage[] = [];\n\tif (cutPoint.isSplitTurn) {\n\t\tfor (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {\n\t\t\tconst msg = getMessageFromEntryForCompaction(filteredPathEntries[i]);\n\t\t\tif (msg) turnPrefixMessages.push(msg);\n\t\t}\n\t}\n\n\t// Extract file operations from filtered messages and previous compaction\n\tconst fileOps = extractFileOperations(messagesToSummarize, filteredPathEntries, filteredPrevCompactionIndex);\n\n\t// Also extract file ops from turn prefix if splitting\n\tif (cutPoint.isSplitTurn) {\n\t\tfor (const msg of turnPrefixMessages) {\n\t\t\textractFileOpsFromMessage(msg, fileOps);\n\t\t}\n\t}\n\n\treturn {\n\t\tfirstKeptEntryId,\n\t\tmessagesToSummarize,\n\t\tturnPrefixMessages,\n\t\tisSplitTurn: cutPoint.isSplitTurn,\n\t\ttokensBefore,\n\t\tpreviousSummary,\n\t\tfileOps,\n\t\tsettings,\n\t};\n}\n\n// ============================================================================\n// Main compaction function\n// ============================================================================\n\nconst TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.\n\nSummarize the prefix to provide context for the retained suffix:\n\n## Original Request\n[What did the user ask for in this turn?]\n\n## Early Progress\n- [Key decisions and work done in the prefix]\n\n## Context for Suffix\n- [Information needed to understand the retained recent work]\n\nBe concise. Focus on what's needed to understand the kept suffix.`;\n\n/**\n * Generate summaries for compaction using prepared data.\n * Returns CompactionResult - SessionManager adds uuid/parentUuid when saving.\n *\n * @param preparation - Pre-calculated preparation from prepareCompaction()\n * @param customInstructions - Optional custom focus for the summary\n */\nexport async function compact(\n\tpreparation: CompactionPreparation,\n\tmodel: Model<Api>,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tcustomInstructions?: string,\n\tsignal?: AbortSignal,\n\tthinkingLevel?: ThinkingLevel,\n): Promise<CompactionResult> {\n\tconst {\n\t\tfirstKeptEntryId,\n\t\tmessagesToSummarize,\n\t\tturnPrefixMessages,\n\t\tisSplitTurn,\n\t\ttokensBefore,\n\t\tpreviousSummary,\n\t\tfileOps,\n\t\tsettings,\n\t} = preparation;\n\n\t// Generate summaries (can be parallel if both needed) and merge into one\n\tlet summary: string;\n\n\tif (isSplitTurn && turnPrefixMessages.length > 0) {\n\t\t// Generate both summaries in parallel\n\t\tconst [historyResult, turnPrefixResult] = await Promise.all([\n\t\t\tmessagesToSummarize.length > 0\n\t\t\t\t? generateSummary(\n\t\t\t\t\t\tmessagesToSummarize,\n\t\t\t\t\t\tmodel,\n\t\t\t\t\t\tsettings.reserveTokens,\n\t\t\t\t\t\tapiKey,\n\t\t\t\t\t\theaders,\n\t\t\t\t\t\tsignal,\n\t\t\t\t\t\tcustomInstructions,\n\t\t\t\t\t\tpreviousSummary,\n\t\t\t\t\t\tthinkingLevel,\n\t\t\t\t\t)\n\t\t\t\t: Promise.resolve(\"No prior history.\"),\n\t\t\tgenerateTurnPrefixSummary(\n\t\t\t\tturnPrefixMessages,\n\t\t\t\tmodel,\n\t\t\t\tsettings.reserveTokens,\n\t\t\t\tapiKey,\n\t\t\t\theaders,\n\t\t\t\tsignal,\n\t\t\t\tthinkingLevel,\n\t\t\t),\n\t\t]);\n\t\t// Merge into single summary\n\t\tsummary = `${historyResult}\\n\\n---\\n\\n**Turn Context (split turn):**\\n\\n${turnPrefixResult}`;\n\t} else {\n\t\t// Just generate history summary\n\t\tsummary = await generateSummary(\n\t\t\tmessagesToSummarize,\n\t\t\tmodel,\n\t\t\tsettings.reserveTokens,\n\t\t\tapiKey,\n\t\t\theaders,\n\t\t\tsignal,\n\t\t\tcustomInstructions,\n\t\t\tpreviousSummary,\n\t\t\tthinkingLevel,\n\t\t);\n\t}\n\n\t// Compute file lists and append to summary\n\tconst { readFiles, modifiedFiles } = computeFileLists(fileOps);\n\tsummary += formatFileOperations(readFiles, modifiedFiles);\n\n\tif (!firstKeptEntryId) {\n\t\tthrow new Error(\"First kept entry has no UUID - session may need migration\");\n\t}\n\n\treturn {\n\t\tsummary,\n\t\tfirstKeptEntryId,\n\t\ttokensBefore,\n\t\tdetails: { readFiles, modifiedFiles } as CompactionDetails,\n\t};\n}\n\n/**\n * Generate a summary for a turn prefix (when splitting a turn).\n */\nasync function generateTurnPrefixSummary(\n\tmessages: AgentMessage[],\n\tmodel: Model<Api>,\n\treserveTokens: number,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tsignal?: AbortSignal,\n\tthinkingLevel?: ThinkingLevel,\n): Promise<string> {\n\tconst maxTokens = Math.min(\n\t\tMath.floor(0.5 * reserveTokens),\n\t\tmodel.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY,\n\t); // Smaller budget for turn prefix\n\tconst llmMessages = convertToLlm(messages);\n\tconst conversationText = serializeConversation(llmMessages);\n\tconst promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;\n\tconst summarizationMessages = [\n\t\t{\n\t\t\trole: \"user\" as const,\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\n\t\t\ttimestamp: Date.now(),\n\t\t},\n\t];\n\n\tconst response = await completeSimple(\n\t\tmodel,\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\n\t\tmodel.reasoning && thinkingLevel && thinkingLevel !== \"off\"\n\t\t\t? { maxTokens, signal, apiKey, headers, reasoning: thinkingLevel }\n\t\t\t: { maxTokens, signal, apiKey, headers },\n\t);\n\n\tif (response.stopReason === \"error\") {\n\t\tthrow new Error(`Turn prefix summarization failed: ${response.errorMessage || \"Unknown error\"}`);\n\t}\n\n\treturn response.content\n\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t.map((c) => c.text)\n\t\t.join(\"\\n\");\n}\n"]}
1
+ {"version":3,"file":"compaction.js","sourceRoot":"","sources":["../../../src/core/compaction/compaction.ts"],"names":[],"mappings":"AAAA;;GAEG;AAWH,MAAM,CAAC,MAAM,2BAA2B,GAAuB;IAC9D,OAAO,EAAE,IAAI;IACb,aAAa,EAAE,KAAK;CACpB,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAY;IAClD,OAAO,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,UAAU,CAAC;AAC7F,CAAC;AAED;;;GAGG;AACH,SAAS,iBAAiB,CAAC,GAAiB;IAC3C,IAAI,GAAG,CAAC,IAAI,KAAK,WAAW,IAAI,OAAO,IAAI,GAAG,EAAE,CAAC;QAChD,MAAM,YAAY,GAAG,GAAuB,CAAC;QAC7C,IAAI,YAAY,CAAC,UAAU,KAAK,SAAS,IAAI,YAAY,CAAC,UAAU,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C;QAC7B,CAAC;QACD,KAAK,eAAe,EAAE,CAAC;YACtB,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC;YACvD,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;QAC7B,CAAC;QACD,KAAK,eAAe,EAAE,CAAC;YACtB,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC;YAC/B,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;QAC7B,CAAC;IACF,CAAC;IAED,OAAO,CAAC,CAAC;AACV,CAAC","sourcesContent":["/**\n * Neutral context-usage metrics for deciding when a session needs compaction.\n */\n\nimport type { AgentMessage } from \"@earendil-works/pi-agent-core\";\nimport type { AssistantMessage, Usage } from \"@earendil-works/pi-ai\";\nimport type { SessionEntry } from \"../session-manager.ts\";\n\nexport interface CompactionSettings {\n\tenabled: boolean;\n\treserveTokens: number;\n}\n\nexport const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {\n\tenabled: true,\n\treserveTokens: 16384,\n};\n\n/**\n * Calculate total context tokens from usage.\n * Uses the native totalTokens field when available, falls back to computing from components.\n */\nexport function calculateContextTokens(usage: Usage): number {\n\treturn usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;\n}\n\n/**\n * Get usage from an assistant message if available.\n * Skips aborted and error messages as they don't have valid usage data.\n */\nfunction getAssistantUsage(msg: AgentMessage): Usage | undefined {\n\tif (msg.role === \"assistant\" && \"usage\" in msg) {\n\t\tconst assistantMsg = msg as AssistantMessage;\n\t\tif (assistantMsg.stopReason !== \"aborted\" && assistantMsg.stopReason !== \"error\" && assistantMsg.usage) {\n\t\t\treturn assistantMsg.usage;\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/**\n * Find the last non-aborted assistant message usage from session entries.\n */\nexport function getLastAssistantUsage(entries: SessionEntry[]): Usage | undefined {\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"message\") {\n\t\t\tconst usage = getAssistantUsage(entry.message);\n\t\t\tif (usage) return usage;\n\t\t}\n\t}\n\treturn undefined;\n}\n\nexport interface ContextUsageEstimate {\n\ttokens: number;\n\tusageTokens: number;\n\ttrailingTokens: number;\n\tlastUsageIndex: number | null;\n}\n\nfunction getLastAssistantUsageInfo(messages: AgentMessage[]): { usage: Usage; index: number } | undefined {\n\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\tconst usage = getAssistantUsage(messages[i]);\n\t\tif (usage) return { usage, index: i };\n\t}\n\treturn undefined;\n}\n\n/**\n * Estimate context tokens from messages, using the last assistant usage when available.\n * If there are messages after the last usage, estimate their tokens with estimateTokens.\n */\nexport function estimateContextTokens(messages: AgentMessage[]): ContextUsageEstimate {\n\tconst usageInfo = getLastAssistantUsageInfo(messages);\n\n\tif (!usageInfo) {\n\t\tlet estimated = 0;\n\t\tfor (const message of messages) {\n\t\t\testimated += estimateTokens(message);\n\t\t}\n\t\treturn {\n\t\t\ttokens: estimated,\n\t\t\tusageTokens: 0,\n\t\t\ttrailingTokens: estimated,\n\t\t\tlastUsageIndex: null,\n\t\t};\n\t}\n\n\tconst usageTokens = calculateContextTokens(usageInfo.usage);\n\tlet trailingTokens = 0;\n\tfor (let i = usageInfo.index + 1; i < messages.length; i++) {\n\t\ttrailingTokens += estimateTokens(messages[i]);\n\t}\n\n\treturn {\n\t\ttokens: usageTokens + trailingTokens,\n\t\tusageTokens,\n\t\ttrailingTokens,\n\t\tlastUsageIndex: usageInfo.index,\n\t};\n}\n\n/**\n * Check if compaction should trigger based on context usage.\n */\nexport function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean {\n\tif (!settings.enabled) return false;\n\treturn contextTokens > contextWindow - settings.reserveTokens;\n}\n\nconst ESTIMATED_IMAGE_CHARS = 4800;\n\nfunction estimateTextAndImageContentChars(content: string | Array<{ type: string; text?: string }>): number {\n\tif (typeof content === \"string\") {\n\t\treturn content.length;\n\t}\n\n\tlet chars = 0;\n\tfor (const block of content) {\n\t\tif (block.type === \"text\" && block.text) {\n\t\t\tchars += block.text.length;\n\t\t} else if (block.type === \"image\") {\n\t\t\tchars += ESTIMATED_IMAGE_CHARS;\n\t\t}\n\t}\n\treturn chars;\n}\n\n/**\n * Estimate token count for a message using chars/4 heuristic.\n * This is conservative (overestimates tokens).\n */\nexport function estimateTokens(message: AgentMessage): number {\n\tlet chars = 0;\n\n\tswitch (message.role) {\n\t\tcase \"user\": {\n\t\t\tchars = estimateTextAndImageContentChars(\n\t\t\t\t(message as { content: string | Array<{ type: string; text?: string }> }).content,\n\t\t\t);\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"assistant\": {\n\t\t\tconst assistant = message as AssistantMessage;\n\t\t\tfor (const block of assistant.content) {\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tchars += block.text.length;\n\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\tchars += block.thinking.length;\n\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\tchars += block.name.length + JSON.stringify(block.arguments).length;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"custom\":\n\t\tcase \"toolResult\": {\n\t\t\tchars = estimateTextAndImageContentChars(message.content);\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"bashExecution\": {\n\t\t\tchars = message.command.length + message.output.length;\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"branchSummary\": {\n\t\t\tchars = message.summary.length;\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t}\n\n\treturn 0;\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"context-compaction.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/context-compaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YA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qBAAqB,EACjC,OAAO,GAAE,gCAAqC,GAC5C,8BAA8B,CAwFhC;AAgVD,wBAAgB,yBAAyB,CACxC,UAAU,EAAE,qBAAqB,EACjC,OAAO,GAAE,2BAAgC,GACvC,6BAA6B,CAmX/B;AAED,wBAAgB,2BAA2B,CAAC,IAAI,EAAE,MAAM,GAAG,sBAAsB,CAShF;AAaD,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,gBAAgB,GAAG,sBAAsB,CAe/F;AAqGD,wBAAgB,4BAA4B,CAC3C,UAAU,EAAE,qBAAqB,EACjC,kBAAkB,SAAgE,EAClF,IAAI,GAAE,qBAAkC,GACtC,MAAM,CAER;AAgID,wBAAsB,cAAc,CACnC,WAAW,EAAE,4BAA4B,EACzC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,MAAM,CAAC,EAAE,WAAW,EACpB,aAAa,GAAE,aAAqB,EACpC,IAAI,GAAE,qBAAsD,GAC1D,OAAO,CAAC,8BAA8B,CAAC,CAgBzC","sourcesContent":["import { Agent, type AgentMessage, type AgentTool, type AgentToolResult, type ThinkingLevel } from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model, ToolCall } from \"@earendil-works/pi-ai\";\nimport {\n\tcreateAssistantMessageEventStream,\n\tisContextOverflow,\n\tstreamSimple,\n\tStringEnum,\n} from \"@earendil-works/pi-ai\";\nimport { mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { Type } from \"typebox\";\nimport {\n\tcreateBranchSummaryMessage,\n\tcreateCompactionSummaryMessage,\n\tcreateCustomMessage,\n} from \"../messages.ts\";\nimport {\n\tbuildContextDeletionFilteredPath,\n\tbuildContextDeletionFilters,\n\ttype ContextCompactionStats,\n\ttype ContextDeletionTarget,\n\ttype SessionEntry,\n} from \"../session-manager.ts\";\nimport type { CompactionSettings } from \"./compaction.ts\";\nimport { estimateTokens } from \"./compaction.ts\";\n\nexport const CONTEXT_COMPACTION_PROMPT_VERSION = 1 as const;\n\nexport type ContextCompactionMode = \"standard\" | \"critical_overflow\";\n\nexport interface ContextDeletionRequest {\n\tdeletions: Array<{\n\t\tkind: \"entry\" | \"content_block\";\n\t\tentryId: string;\n\t\tblockIndex?: number;\n\t\trationale?: string;\n\t}>;\n}\n\nexport interface CompactableContentBlock {\n\tentryId: string;\n\tblockIndex: number;\n\ttype: string;\n\ttext: string;\n\ttokenEstimate: number;\n\tprotected: boolean;\n\ttoolCallId?: string;\n}\n\nexport interface CompactableTranscriptEntry {\n\tentryId: string;\n\tentryType: SessionEntry[\"type\"];\n\trole: AgentMessage[\"role\"];\n\ttext: string;\n\ttokenEstimate: number;\n\tprotected: boolean;\n\tcontentBlocks: CompactableContentBlock[];\n\tmessage: AgentMessage;\n\ttoolCallIds: string[];\n\ttoolResultFor?: string;\n}\n\nexport interface CompactableTranscript {\n\tentries: CompactableTranscriptEntry[];\n\tprotectedEntryIds: string[];\n\ttokensBefore: number;\n\tsettings: CompactionSettings;\n}\n\nexport interface ContextCompactionPreparation {\n\ttranscript: CompactableTranscript;\n\tbranchEntries: SessionEntry[];\n\tmode?: ContextCompactionMode;\n}\n\nexport interface ValidatedContextDeletionResult {\n\tdeletedTargets: ContextDeletionTarget[];\n\tprotectedEntryIds: string[];\n\tstats: ContextCompactionStats;\n}\n\nexport interface ContextCompactionResult extends ValidatedContextDeletionResult {\n\tpromptVersion: typeof CONTEXT_COMPACTION_PROMPT_VERSION;\n\tbackupPath?: string;\n}\n\nconst CONTEXT_DELETE_TOOL_NAME = \"context_delete\";\nconst CONTEXT_GREP_DELETE_TOOL_NAME = \"context_grep_delete\";\nconst CONTEXT_SEARCH_TRANSCRIPT_TOOL_NAME = \"context_search_transcript\";\nconst CONTEXT_READ_ENTRY_TOOL_NAME = \"context_read_entry\";\nexport const CONTEXT_COMPACTION_MAX_TURNS = 50 as const;\nconst CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES = 50;\nconst CONTEXT_GREP_DELETE_MAX_REGEX_PATTERN_CHARS = 512;\nconst CONTEXT_GREP_DELETE_MAX_REGEX_SCAN_CHARS = 250_000;\nconst CONTEXT_MANIFEST_MAX_ENTRIES = 80;\nconst CONTEXT_MANIFEST_PREVIEW_CHARS = 240;\nconst CONTEXT_CRITICAL_OVERFLOW_RECENT_ENTRY_COUNT = 5;\nconst CONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS = 4000;\nconst CONTEXT_READ_ENTRY_MAX_CHARS = 12_000;\nconst CONTEXT_SEARCH_DEFAULT_MAX_MATCHES = 20;\nconst CONTEXT_SEARCH_MAX_MATCHES = 100;\nconst CONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS = 160;\nconst CONTEXT_SEARCH_MAX_CONTEXT_CHARS = 500;\n\nconst ContextDeleteToolParameters = Type.Object(\n\t{\n\t\tdeletions: Type.Array(\n\t\t\tType.Object(\n\t\t\t\t{\n\t\t\t\t\tkind: StringEnum([\"entry\", \"content_block\"] as const, {\n\t\t\t\t\t\tdescription: \"Delete an entire transcript entry or a single content block within one entry.\",\n\t\t\t\t\t}),\n\t\t\t\t\tentryId: Type.String({ minLength: 1, description: \"Stable transcript entry id to delete from.\" }),\n\t\t\t\t\tblockIndex: Type.Optional(\n\t\t\t\t\t\tType.Integer({\n\t\t\t\t\t\t\tminimum: 0,\n\t\t\t\t\t\t\tdescription: \"Required when kind is content_block; omit when kind is entry.\",\n\t\t\t\t\t\t}),\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t\t{ additionalProperties: false },\n\t\t\t),\n\t\t\t{ description: \"Deletion targets only. Protected entries and recent active context must not be included.\" },\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst ContextGrepDeleteToolParameters = Type.Object(\n\t{\n\t\tpattern: Type.String({ minLength: 1, description: \"Literal text or regular expression to match in transcript text.\" }),\n\t\tregex: Type.Optional(Type.Boolean({ description: \"Treat pattern as a JavaScript regular expression. Defaults to false.\" })),\n\t\tcaseSensitive: Type.Optional(Type.Boolean({ description: \"Use case-sensitive matching. Defaults to false.\" })),\n\t\ttarget: Type.Optional(\n\t\t\tStringEnum([\"entry\", \"content_block\"] as const, {\n\t\t\t\tdescription: \"Delete whole matching entries or matching content blocks. Defaults to entry.\",\n\t\t\t}),\n\t\t),\n\t\tmaxMatches: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 1,\n\t\t\t\tmaximum: 200,\n\t\t\t\tdescription:\n\t\t\t\t\t\"Safety cap. If more unprotected, not-yet-deleted candidate targets are found, no deletions are applied. Defaults to 50.\",\n\t\t\t}),\n\t\t),\n\t\texpectedMatchCount: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 0,\n\t\t\t\tdescription: \"Optional safety check. If the match count differs, no deletions are applied.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst ContextSearchTranscriptToolParameters = Type.Object(\n\t{\n\t\tpattern: Type.String({ minLength: 1, description: \"Literal text or regular expression to search for.\" }),\n\t\tregex: Type.Optional(Type.Boolean({ description: \"Treat pattern as a JavaScript regular expression. Defaults to false.\" })),\n\t\tcaseSensitive: Type.Optional(Type.Boolean({ description: \"Use case-sensitive matching. Defaults to false.\" })),\n\t\ttarget: Type.Optional(\n\t\t\tStringEnum([\"entry\", \"content_block\"] as const, {\n\t\t\t\tdescription: \"Search whole entry text or individual content-block text. Defaults to entry.\",\n\t\t\t}),\n\t\t),\n\t\tmaxMatches: Type.Optional(\n\t\t\tType.Integer({ minimum: 1, maximum: CONTEXT_SEARCH_MAX_MATCHES, description: \"Maximum matches to return. Defaults to 20.\" }),\n\t\t),\n\t\tcontextChars: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 0,\n\t\t\t\tmaximum: CONTEXT_SEARCH_MAX_CONTEXT_CHARS,\n\t\t\t\tdescription: \"Characters of context to include before and after each match. Defaults to 160.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst ContextReadEntryToolParameters = Type.Object(\n\t{\n\t\tentryId: Type.String({ minLength: 1, description: \"Stable transcript entry id to read.\" }),\n\t\tblockIndex: Type.Optional(\n\t\t\tType.Integer({ minimum: 0, description: \"Optional content block index to read instead of the whole entry text.\" }),\n\t\t),\n\t\toffset: Type.Optional(Type.Integer({ minimum: 0, description: \"Character offset to begin reading. Defaults to 0.\" })),\n\t\tmaxChars: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 1,\n\t\t\t\tmaximum: CONTEXT_READ_ENTRY_MAX_CHARS,\n\t\t\t\tdescription: \"Maximum characters to return. Defaults to 4000; keep reads small to avoid overflowing context.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst CONTEXT_DELETE_TOOL = {\n\tname: CONTEXT_DELETE_TOOL_NAME,\n\tdescription: \"Record context compaction deletion targets directly against the transcript.\",\n\tparameters: ContextDeleteToolParameters,\n} as const;\n\nconst CONTEXT_GREP_DELETE_TOOL = {\n\tname: CONTEXT_GREP_DELETE_TOOL_NAME,\n\tdescription: \"Bulk-delete transcript entries or content blocks matching a guarded grep/regex query.\",\n\tparameters: ContextGrepDeleteToolParameters,\n} as const;\n\nconst CONTEXT_SEARCH_TRANSCRIPT_TOOL = {\n\tname: CONTEXT_SEARCH_TRANSCRIPT_TOOL_NAME,\n\tdescription: \"Search the full transcript working copy and return small snippets without mutating deletion state.\",\n\tparameters: ContextSearchTranscriptToolParameters,\n} as const;\n\nconst CONTEXT_READ_ENTRY_TOOL = {\n\tname: CONTEXT_READ_ENTRY_TOOL_NAME,\n\tdescription: \"Read a small slice of one transcript entry or content block from the full transcript working copy.\",\n\tparameters: ContextReadEntryToolParameters,\n} as const;\n\nexport interface ContextDeletionToolDetails {\n\tdeletions: ContextDeletionRequest[\"deletions\"];\n\tdeletedTargets: ContextDeletionTarget[];\n\tstats: ContextCompactionStats;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextGrepDeletionMatch {\n\tentryId: string;\n\ttarget: \"entry\" | \"content_block\";\n\tblockIndex?: number;\n\ttext: string;\n}\n\nexport interface ContextGrepDeletionSkipped {\n\tentryId?: string;\n\ttarget?: \"entry\" | \"content_block\";\n\tblockIndex?: number;\n\treason:\n\t\t| \"protected_entry\"\n\t\t| \"protected_block\"\n\t\t| \"already_deleted\"\n\t\t| \"max_matches_exceeded\"\n\t\t| \"expected_match_count_mismatch\";\n\ttext?: string;\n}\n\nexport interface ContextGrepDeletionToolDetails {\n\tpattern: string;\n\tregex: boolean;\n\tcaseSensitive: boolean;\n\ttarget: \"entry\" | \"content_block\";\n\tmatches: ContextGrepDeletionMatch[];\n\tskipped: ContextGrepDeletionSkipped[];\n\tdeletedTargets: ContextDeletionTarget[];\n\tstats: ContextCompactionStats;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextTranscriptSearchMatch {\n\tentryId: string;\n\ttarget: \"entry\" | \"content_block\";\n\tblockIndex?: number;\n\tmatchIndex: number;\n\tsnippet: string;\n\tprotected: boolean;\n}\n\nexport interface ContextTranscriptSearchToolDetails {\n\tpattern: string;\n\tregex: boolean;\n\tcaseSensitive: boolean;\n\ttarget: \"entry\" | \"content_block\";\n\tmatches: ContextTranscriptSearchMatch[];\n\ttruncated: boolean;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextReadEntryToolDetails {\n\tentryId: string;\n\tblockIndex?: number;\n\toffset: number;\n\tmaxChars: number;\n\ttotalChars: number;\n\ttext: string;\n\ttruncatedBefore: boolean;\n\ttruncatedAfter: boolean;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextDeletionToolController {\n\ttool: AgentTool<typeof ContextDeleteToolParameters, ContextDeletionToolDetails>;\n\tgrepTool: AgentTool<typeof ContextGrepDeleteToolParameters, ContextGrepDeletionToolDetails>;\n\tsearchTool: AgentTool<typeof ContextSearchTranscriptToolParameters, ContextTranscriptSearchToolDetails>;\n\treadEntryTool: AgentTool<typeof ContextReadEntryToolParameters, ContextReadEntryToolDetails>;\n\ttools: AgentTool[];\n\tgetDeletionRequest(): ContextDeletionRequest;\n\tgetValidatedResult(): ValidatedContextDeletionResult | undefined;\n\tgetLastError(): string | undefined;\n\tgetCallCount(): number;\n}\n\nexport interface ContextCompactionRunOptions {\n\tmode?: ContextCompactionMode;\n}\n\nconst CONTEXT_COMPACTION_SYSTEM_PROMPT = `You are a context compaction assistant.\n\nYour task is to read relevant parts of a conversation between a user and an AI assistant provided via a transcript file, then run a series of tools to apply deletion-only verbatim compaction using the exact context_delete or context_grep_delete format specified.`;\n\nconst CONTEXT_COMPACTION_FIXED_PROMPT = `Reference the provided transcript file transcript and use your search/read tools for small inspections, then use context_delete or context_grep_delete for deletions.\n\nYou MUST NOT summarize.\nYou MUST NOT paraphrase.\nYou MUST NOT generate replacement context.\nYou MUST NOT mutate retained transcript objects or content.\nDeletion tool calls are the compaction action; record only deletion targets by stable ID.\n\nWhat Gets Deleted:\n- Redundant tool outputs: file reads already acted on, grep/search results already processed, passing test output no longer needed.\n- Exploratory dead ends: irrelevant files read, unhelpful or empty searches.\n- Verbose boilerplate: license headers, import blocks the agent isn't modifying, configuration files read for reference.\n- Superseded information: earlier versions of files that have since been edited, old error messages from bugs already fixed.\n\nWhat Survives:\n- Active file paths and line numbers: Any reference the agent might need to navigate.\n- Current error messages: Unresolved bugss and their exact text.\n- Reasoning decisions: Why the agent chose approach A over B. An agent's chain of thought (why it chose this file, what pattern it noticed, what fix it decided on) carries more information-per-token than the raw grep output or file content that informed those decisions.\n- Recent tool calls and their results: The last 3-5 operations.\n- User instructions: The original task and any clarifications.\n\n<output_format>\nCall the context_delete tool one or more times with deletion targets in this shape:\n{ \"deletions\": [{ \"kind\": \"entry\", \"entryId\": \"...\" }] }\n\nFor content-block deletions, use:\n{ \"kind\": \"content_block\", \"entryId\": \"...\", \"blockIndex\": 0 }\n\nThe tool applies and validates deletion targets immediately. You can continue calling it for additional deletions if useful.\n\nFor guarded bulk deletion by text match, call context_grep_delete with a literal pattern or regex. It skips protected context, enforces maxMatches and expectedMatchCount, and validates through the same tool-call/tool-result safety rules.\n\nThe full transcript is available as a JSONL file path in the prompt, but do NOT try to load the whole file into context. Use context_search_transcript to find candidate entry IDs and context_read_entry to read only small slices (for example maxChars 1000-4000) before deleting.\n\nWhen you are done, reply with a brief plain-text completion message. Do not write deletion JSON or deletion target IDs outside tool calls.\n</output_format>`;\n\nfunction getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tif (entry.type === \"message\") {\n\t\treturn entry.message;\n\t}\n\tif (entry.type === \"custom_message\") {\n\t\treturn createCustomMessage(\n\t\t\tentry.customType,\n\t\t\tentry.content,\n\t\t\tentry.display,\n\t\t\tentry.details,\n\t\t\tentry.timestamp,\n\t\t\tentry.excludeFromContext,\n\t\t);\n\t}\n\tif (entry.type === \"branch_summary\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nfunction isExcludedFromLlmContext(message: AgentMessage): boolean {\n\tswitch (message.role) {\n\t\tcase \"bashExecution\":\n\t\t\treturn Boolean(message.excludeFromContext);\n\t\tcase \"custom\":\n\t\t\treturn (message as { excludeFromContext?: boolean }).excludeFromContext === true;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction getContextEligibleMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tconst message = getMessageFromEntry(entry);\n\tif (!message || isExcludedFromLlmContext(message)) return undefined;\n\treturn message;\n}\n\nfunction textFromUnknownContent(content: unknown): string {\n\tif (typeof content === \"string\") return content;\n\tif (!Array.isArray(content)) return JSON.stringify(content);\n\treturn content.map((block) => textFromContentBlock(block)).join(\"\\n\");\n}\n\nfunction textFromContentBlock(block: unknown): string {\n\tif (!block || typeof block !== \"object\") return String(block);\n\tconst record = block as Record<string, unknown>;\n\tif (record.type === \"text\" && typeof record.text === \"string\") return record.text;\n\tif (record.type === \"thinking\" && typeof record.thinking === \"string\") return record.thinking;\n\tif (record.type === \"toolCall\") {\n\t\tconst name = typeof record.name === \"string\" ? record.name : \"tool\";\n\t\tconst id = typeof record.id === \"string\" ? record.id : \"unknown\";\n\t\tconst args = \"arguments\" in record ? JSON.stringify(record.arguments) : \"\";\n\t\treturn `toolCall ${id} ${name} ${args}`.trim();\n\t}\n\tif (record.type === \"image\") return \"[image]\";\n\treturn JSON.stringify(record);\n}\n\nconst IMAGE_BLOCK_CHAR_ESTIMATE = 4800;\nconst IMAGE_BLOCK_TOKEN_ESTIMATE = Math.ceil(IMAGE_BLOCK_CHAR_ESTIMATE / 4);\n\nfunction estimateTextTokens(text: string): number {\n\treturn Math.max(1, Math.ceil(text.length / 4));\n}\n\nfunction estimateContentBlockTokens(block: unknown, text: string): number {\n\tif (block && typeof block === \"object\" && (block as { type?: unknown }).type === \"image\") {\n\t\treturn IMAGE_BLOCK_TOKEN_ESTIMATE;\n\t}\n\treturn estimateTextTokens(text);\n}\n\nfunction getToolCallIdFromBlock(block: unknown): string | undefined {\n\tif (!block || typeof block !== \"object\") return undefined;\n\tconst record = block as Record<string, unknown>;\n\tif (record.type !== \"toolCall\") return undefined;\n\treturn typeof record.id === \"string\" ? record.id : undefined;\n}\n\nfunction getToolResultCallId(message: AgentMessage): string | undefined {\n\tif (message.role !== \"toolResult\") return undefined;\n\tconst callId = (message as { toolCallId?: unknown }).toolCallId;\n\treturn typeof callId === \"string\" ? callId : undefined;\n}\n\nfunction contentBlocksForEntry(\n\tentryId: string,\n\tmessage: AgentMessage,\n\tprotectedEntry: boolean,\n\texistingDeletedBlocks: ReadonlySet<number> | undefined,\n): CompactableContentBlock[] {\n\tif (message.role === \"compactionSummary\") {\n\t\tconst text = message.summary;\n\t\treturn [\n\t\t\t{\n\t\t\t\tentryId,\n\t\t\t\tblockIndex: 0,\n\t\t\t\ttype: \"summary\",\n\t\t\t\ttext,\n\t\t\t\ttokenEstimate: estimateTextTokens(text),\n\t\t\t\tprotected: protectedEntry,\n\t\t\t},\n\t\t];\n\t}\n\n\tconst content = (message as { content?: unknown }).content;\n\tif (!Array.isArray(content)) return [];\n\n\treturn content\n\t\t.map((block, blockIndex): CompactableContentBlock | undefined => {\n\t\t\tif (existingDeletedBlocks?.has(blockIndex)) return undefined;\n\t\t\tconst text = textFromContentBlock(block);\n\t\t\treturn {\n\t\t\t\tentryId,\n\t\t\t\tblockIndex,\n\t\t\t\ttype:\n\t\t\t\t\tblock && typeof block === \"object\" && typeof (block as { type?: unknown }).type === \"string\"\n\t\t\t\t\t\t? ((block as { type: string }).type)\n\t\t\t\t\t\t: \"unknown\",\n\t\t\t\ttext,\n\t\t\t\ttokenEstimate: estimateContentBlockTokens(block, text),\n\t\t\t\tprotected: protectedEntry,\n\t\t\t\ttoolCallId: getToolCallIdFromBlock(block),\n\t\t\t};\n\t\t})\n\t\t.filter((block): block is CompactableContentBlock => block !== undefined);\n}\n\nfunction messageText(message: AgentMessage): string {\n\tswitch (message.role) {\n\t\tcase \"bashExecution\":\n\t\t\treturn `Ran ${message.command}\\n${message.output}`;\n\t\tcase \"branchSummary\":\n\t\tcase \"compactionSummary\":\n\t\t\treturn message.summary;\n\t\tcase \"custom\":\n\t\tcase \"toolResult\":\n\t\tcase \"user\":\n\t\t\treturn textFromUnknownContent(message.content);\n\t\tcase \"assistant\":\n\t\t\treturn textFromUnknownContent(message.content);\n\t}\n}\n\nfunction hasAssistantError(message: AgentMessage): boolean {\n\treturn message.role === \"assistant\" && (message as AssistantMessage).stopReason === \"error\";\n}\n\nfunction hasToolResultError(message: AgentMessage): boolean {\n\treturn message.role === \"toolResult\" && (message as { isError?: unknown }).isError === true;\n}\n\nfunction hasFailedBashExecution(message: AgentMessage): boolean {\n\treturn message.role === \"bashExecution\" && typeof message.exitCode === \"number\" && message.exitCode !== 0;\n}\n\nfunction collectLatestSummaryCompactionIndex(pathEntries: SessionEntry[]): number {\n\tfor (let i = pathEntries.length - 1; i >= 0; i--) {\n\t\tif (pathEntries[i].type === \"compaction\") return i;\n\t}\n\treturn -1;\n}\n\nfunction collectActiveEntryIndices(pathEntries: SessionEntry[], latestCompactionIndex: number): number[] {\n\tif (latestCompactionIndex < 0) {\n\t\treturn pathEntries.map((_, index) => index);\n\t}\n\n\tconst latestCompaction = pathEntries[latestCompactionIndex];\n\tif (latestCompaction.type !== \"compaction\") return pathEntries.map((_, index) => index);\n\n\tconst indices: number[] = [];\n\tlet foundFirstKept = false;\n\tfor (let i = 0; i < latestCompactionIndex; i++) {\n\t\tconst entry = pathEntries[i];\n\t\tif (entry.id === latestCompaction.firstKeptEntryId) {\n\t\t\tfoundFirstKept = true;\n\t\t}\n\t\tif (foundFirstKept) indices.push(i);\n\t}\n\tfor (let i = latestCompactionIndex + 1; i < pathEntries.length; i++) {\n\t\tindices.push(i);\n\t}\n\treturn indices;\n}\n\nfunction isProtectedEntry(\n\tentry: SessionEntry,\n\tmessage: AgentMessage,\n\trecentEntryIds: ReadonlySet<string>,\n): boolean {\n\tif (recentEntryIds.has(entry.id)) return true;\n\tif (message.role === \"user\") return true;\n\tif (message.role === \"custom\") return true;\n\tif (message.role === \"branchSummary\" || message.role === \"compactionSummary\") return true;\n\tif (hasAssistantError(message) || hasToolResultError(message)) return true;\n\tif (hasFailedBashExecution(message)) return true;\n\tif (entry.type === \"branch_summary\") return true;\n\treturn false;\n}\n\nexport function prepareContextCompaction(\n\tpathEntries: SessionEntry[],\n\tsettings: CompactionSettings,\n\toptions: ContextCompactionRunOptions = {},\n): ContextCompactionPreparation | undefined {\n\tif (pathEntries.length === 0) return undefined;\n\n\tconst latestCompactionIndex = collectLatestSummaryCompactionIndex(pathEntries);\n\tconst deletionFilters = buildContextDeletionFilters(pathEntries);\n\tconst filteredPathEntries = buildContextDeletionFilteredPath(pathEntries, deletionFilters);\n\tconst filteredEntryById = new Map(filteredPathEntries.map((entry) => [entry.id, entry]));\n\tconst activeEntryIndices = collectActiveEntryIndices(pathEntries, latestCompactionIndex);\n\tconst messageEntryIds = activeEntryIndices\n\t\t.map((index) => filteredEntryById.get(pathEntries[index].id))\n\t\t.filter((entry): entry is SessionEntry => entry !== undefined && getContextEligibleMessageFromEntry(entry) !== undefined)\n\t\t.map((entry) => entry.id);\n\tconst recentEntryIds = new Set(messageEntryIds.slice(-CONTEXT_CRITICAL_OVERFLOW_RECENT_ENTRY_COUNT));\n\tconst protectedEntryIds = new Set<string>();\n\tconst entries: CompactableTranscriptEntry[] = [];\n\n\tif (latestCompactionIndex >= 0) {\n\t\tconst latestCompaction = pathEntries[latestCompactionIndex];\n\t\tif (latestCompaction.type === \"compaction\") {\n\t\t\tconst message = createCompactionSummaryMessage(\n\t\t\t\tlatestCompaction.summary,\n\t\t\t\tlatestCompaction.tokensBefore,\n\t\t\t\tlatestCompaction.timestamp,\n\t\t\t);\n\t\t\tconst contentBlocks = contentBlocksForEntry(latestCompaction.id, message, true, undefined);\n\t\t\tprotectedEntryIds.add(latestCompaction.id);\n\t\t\tentries.push({\n\t\t\t\tentryId: latestCompaction.id,\n\t\t\t\tentryType: latestCompaction.type,\n\t\t\t\trole: message.role,\n\t\t\t\ttext: messageText(message),\n\t\t\t\ttokenEstimate: estimateTokens(message),\n\t\t\t\tprotected: true,\n\t\t\t\tcontentBlocks,\n\t\t\t\tmessage,\n\t\t\t\ttoolCallIds: [],\n\t\t\t\ttoolResultFor: undefined,\n\t\t\t});\n\t\t}\n\t}\n\n\tfor (const index of activeEntryIndices) {\n\t\tconst rawEntry = pathEntries[index];\n\t\tconst entry = filteredEntryById.get(rawEntry.id);\n\t\tif (!entry || entry.type === \"context_compaction\") continue;\n\t\tconst message = getContextEligibleMessageFromEntry(entry);\n\t\tif (!message) continue;\n\t\tconst protectedEntry = isProtectedEntry(entry, message, recentEntryIds);\n\t\tif (protectedEntry) protectedEntryIds.add(entry.id);\n\t\tconst rawMessage = getContextEligibleMessageFromEntry(rawEntry) ?? message;\n\t\tconst contentBlocks = contentBlocksForEntry(\n\t\t\tentry.id,\n\t\t\trawMessage,\n\t\t\tprotectedEntry,\n\t\t\tdeletionFilters.deletedContentBlocks.get(entry.id),\n\t\t);\n\t\tconst toolCallIds = contentBlocks.map((block) => block.toolCallId).filter((id): id is string => id !== undefined);\n\t\tconst text = contentBlocks.length > 0 ? contentBlocks.map((block) => block.text).join(\"\\n\") : messageText(message);\n\t\tentries.push({\n\t\t\tentryId: entry.id,\n\t\t\tentryType: entry.type,\n\t\t\trole: message.role,\n\t\t\ttext,\n\t\t\ttokenEstimate: estimateTokens(message),\n\t\t\tprotected: protectedEntry,\n\t\t\tcontentBlocks,\n\t\t\tmessage,\n\t\t\ttoolCallIds,\n\t\t\ttoolResultFor: getToolResultCallId(message),\n\t\t});\n\t}\n\n\tif (entries.length < 2) return undefined;\n\n\treturn {\n\t\tbranchEntries: pathEntries,\n\t\tmode: options.mode ?? \"standard\",\n\t\ttranscript: {\n\t\t\tentries,\n\t\t\tprotectedEntryIds: [...protectedEntryIds],\n\t\t\ttokensBefore: entries.reduce((total, entry) => total + entry.tokenEstimate, 0),\n\t\t\tsettings,\n\t\t},\n\t};\n}\n\nfunction targetKey(target: ContextDeletionTarget): string {\n\treturn target.kind === \"entry\" ? `entry:${target.entryId}` : `content_block:${target.entryId}:${target.blockIndex}`;\n}\n\nfunction rawTargetKey(target: ContextDeletionRequest[\"deletions\"][number]): string {\n\treturn target.kind === \"entry\" ? `entry:${target.entryId}` : `content_block:${target.entryId}:${target.blockIndex}`;\n}\n\nfunction normalizeRawTarget(target: ContextDeletionRequest[\"deletions\"][number]): ContextDeletionTarget {\n\tif (target.kind === \"entry\") return { kind: \"entry\", entryId: target.entryId };\n\treturn { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex as number };\n}\n\nfunction rawDeletionFromTarget(target: ContextDeletionTarget): ContextDeletionRequest[\"deletions\"][number] {\n\tif (target.kind === \"entry\") return { kind: \"entry\", entryId: target.entryId };\n\treturn { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nfunction deletionRequestFromTargets(targets: readonly ContextDeletionTarget[]): ContextDeletionRequest {\n\treturn { deletions: targets.map(rawDeletionFromTarget) };\n}\n\nfunction getDeletedEntryIds(targets: readonly ContextDeletionTarget[]): Set<string> {\n\treturn new Set(targets.filter((target) => target.kind === \"entry\").map((target) => target.entryId));\n}\n\nfunction getDeletedContentBlocks(targets: readonly ContextDeletionTarget[]): Map<string, Set<number>> {\n\tconst blocksByEntry = new Map<string, Set<number>>();\n\tfor (const target of targets) {\n\t\tif (target.kind !== \"content_block\") continue;\n\t\tconst blocks = blocksByEntry.get(target.entryId) ?? new Set<number>();\n\t\tblocks.add(target.blockIndex);\n\t\tblocksByEntry.set(target.entryId, blocks);\n\t}\n\treturn blocksByEntry;\n}\n\nfunction isToolCallBlockDeleted(\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n\tdeletedEntryIds: ReadonlySet<string>,\n\tdeletedContentBlocks: ReadonlyMap<string, ReadonlySet<number>>,\n): boolean {\n\tif (deletedEntryIds.has(entry.entryId)) return true;\n\tconst deletedBlocks = deletedContentBlocks.get(entry.entryId);\n\tif (!deletedBlocks) return false;\n\treturn entry.contentBlocks.some((block) => block.toolCallId === callId && deletedBlocks.has(block.blockIndex));\n}\n\nfunction toolCallBlockIndexes(entry: CompactableTranscriptEntry, callId: string): number[] {\n\treturn entry.contentBlocks\n\t\t.filter((block) => block.toolCallId === callId)\n\t\t.map((block) => block.blockIndex);\n}\n\nfunction addTarget(targets: ContextDeletionTarget[], target: ContextDeletionTarget): boolean {\n\tif (targets.some((existing) => targetKey(existing) === targetKey(target))) return false;\n\ttargets.push(target);\n\treturn true;\n}\n\nfunction deleteEntryTarget(targets: ContextDeletionTarget[], entryId: string): boolean {\n\tlet changed = false;\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"content_block\" && target.entryId === entryId) {\n\t\t\ttargets.splice(index, 1);\n\t\t\tchanged = true;\n\t\t}\n\t}\n\treturn addTarget(targets, { kind: \"entry\", entryId }) || changed;\n}\n\nfunction removeEntryDeletion(targets: ContextDeletionTarget[], entryId: string): boolean {\n\tconst originalLength = targets.length;\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"entry\" && target.entryId === entryId) targets.splice(index, 1);\n\t}\n\treturn targets.length !== originalLength;\n}\n\nfunction mergeContextDeletionTargets(\n\tbaseTargets: readonly ContextDeletionTarget[],\n\tadditionalTargets: readonly ContextDeletionTarget[],\n): ContextDeletionTarget[] {\n\tconst targets = [...baseTargets];\n\tfor (const target of additionalTargets) {\n\t\tif (target.kind === \"entry\") {\n\t\t\tdeleteEntryTarget(targets, target.entryId);\n\t\t\tcontinue;\n\t\t}\n\t\tif (!getDeletedEntryIds(targets).has(target.entryId)) {\n\t\t\taddTarget(targets, target);\n\t\t}\n\t}\n\treturn targets;\n}\n\nfunction canonicalizeEntryTargets(targets: ContextDeletionTarget[], entry: CompactableTranscriptEntry): boolean {\n\tif (entry.protected || getDeletedEntryIds(targets).has(entry.entryId)) return false;\n\tconst deletedBlocks = getDeletedContentBlocks(targets).get(entry.entryId);\n\tif (!deletedBlocks || !entry.contentBlocks.every((block) => deletedBlocks.has(block.blockIndex))) return false;\n\t// Only repair/promote when dependency reconciliation reaches this entry. Non-tool entries that\n\t// request every block individually stay invalid so the assistant must choose explicit entry deletion.\n\treturn deleteEntryTarget(targets, entry.entryId);\n}\n\nfunction removeToolCallDeletion(\n\ttargets: ContextDeletionTarget[],\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n): boolean {\n\tlet changed = removeEntryDeletion(targets, entry.entryId);\n\tconst blockIndexes = new Set(toolCallBlockIndexes(entry, callId));\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"content_block\" && target.entryId === entry.entryId && blockIndexes.has(target.blockIndex)) {\n\t\t\ttargets.splice(index, 1);\n\t\t\tchanged = true;\n\t\t}\n\t}\n\treturn changed;\n}\n\nfunction addToolCallDeletion(targets: ContextDeletionTarget[], entry: CompactableTranscriptEntry, callId: string): boolean {\n\tif (entry.protected) return false;\n\tlet changed = false;\n\tfor (const blockIndex of toolCallBlockIndexes(entry, callId)) {\n\t\tif (!getDeletedEntryIds(targets).has(entry.entryId)) {\n\t\t\tchanged = addTarget(targets, { kind: \"content_block\", entryId: entry.entryId, blockIndex }) || changed;\n\t\t}\n\t}\n\treturn canonicalizeEntryTargets(targets, entry) || changed;\n}\n\nlet warnedReconciliationNonConvergence = false;\n\nfunction reconcileToolDependencies(\n\ttranscript: CompactableTranscript,\n\tinitialTargets: readonly ContextDeletionTarget[],\n): ContextDeletionTarget[] {\n\tconst targets = [...initialTargets];\n\tconst callEntries = new Map<string, CompactableTranscriptEntry>();\n\tconst entriesWithToolCalls = new Set<CompactableTranscriptEntry>();\n\tconst resultEntries = new Map<string, CompactableTranscriptEntry[]>();\n\n\tfor (const entry of transcript.entries) {\n\t\tfor (const callId of entry.toolCallIds) {\n\t\t\tcallEntries.set(callId, entry);\n\t\t\tentriesWithToolCalls.add(entry);\n\t\t}\n\t\tif (entry.toolResultFor) {\n\t\t\tconst results = resultEntries.get(entry.toolResultFor) ?? [];\n\t\t\tresults.push(entry);\n\t\t\tresultEntries.set(entry.toolResultFor, results);\n\t\t}\n\t}\n\n\t// Bounded fixpoint repair: each pass can add/remove paired call/result targets. In practice this\n\t// converges within one or two passes; the cap protects against accidental oscillation.\n\tlet changed = true;\n\tlet remainingPasses = Math.max(1, transcript.entries.length * 2);\n\twhile (changed && remainingPasses > 0) {\n\t\tchanged = false;\n\t\tremainingPasses -= 1;\n\t\tlet deletedEntryIds = getDeletedEntryIds(targets);\n\t\tlet deletedContentBlocks = getDeletedContentBlocks(targets);\n\t\tconst recordChange = (nextChanged: boolean): void => {\n\t\t\tif (!nextChanged) return;\n\t\t\tchanged = true;\n\t\t\tdeletedEntryIds = getDeletedEntryIds(targets);\n\t\t\tdeletedContentBlocks = getDeletedContentBlocks(targets);\n\t\t};\n\n\t\tfor (const [callId, callEntry] of callEntries) {\n\t\t\tconst callDeleted = isToolCallBlockDeleted(callEntry, callId, deletedEntryIds, deletedContentBlocks);\n\t\t\tconst results = resultEntries.get(callId) ?? [];\n\n\t\t\tif (callDeleted) {\n\t\t\t\tconst retainedProtectedResult = results.find((entry) => entry.protected && !deletedEntryIds.has(entry.entryId));\n\t\t\t\tif (retainedProtectedResult) {\n\t\t\t\t\trecordChange(removeToolCallDeletion(targets, callEntry, callId));\n\t\t\t\t} else {\n\t\t\t\t\tfor (const result of results) {\n\t\t\t\t\t\trecordChange(deleteEntryTarget(targets, result.entryId));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (isToolCallBlockDeleted(callEntry, callId, deletedEntryIds, deletedContentBlocks)) continue;\n\n\t\t\tfor (const result of results) {\n\t\t\t\tif (!deletedEntryIds.has(result.entryId)) continue;\n\t\t\t\trecordChange(deleteEntryTarget(targets, result.entryId));\n\t\t\t\tif (callEntry.protected) {\n\t\t\t\t\trecordChange(removeEntryDeletion(targets, result.entryId));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\trecordChange(addToolCallDeletion(targets, callEntry, callId));\n\t\t\t}\n\t\t}\n\n\t\tfor (const entry of entriesWithToolCalls) {\n\t\t\trecordChange(canonicalizeEntryTargets(targets, entry));\n\t\t}\n\t}\n\n\tif (changed && !warnedReconciliationNonConvergence) {\n\t\twarnedReconciliationNonConvergence = true;\n\t\tconsole.warn(\n\t\t\t`Context compaction tool dependency reconciliation did not converge within the bounded pass limit; validation will continue with the last reconciled target set. entries=${transcript.entries.length} callEntries=${callEntries.size} targets=${targets.length}`,\n\t\t);\n\t}\n\n\treturn targets;\n}\n\nfunction validateToolDependencies(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): void {\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tconst deletedContentBlocks = getDeletedContentBlocks(targets);\n\tconst callEntries = new Map<string, CompactableTranscriptEntry>();\n\tconst resultEntries = new Map<string, CompactableTranscriptEntry[]>();\n\n\tfor (const entry of transcript.entries) {\n\t\tfor (const callId of entry.toolCallIds) {\n\t\t\tcallEntries.set(callId, entry);\n\t\t}\n\t\tif (entry.toolResultFor) {\n\t\t\tconst results = resultEntries.get(entry.toolResultFor) ?? [];\n\t\t\tresults.push(entry);\n\t\t\tresultEntries.set(entry.toolResultFor, results);\n\t\t}\n\t}\n\n\tfor (const [callId, callEntry] of callEntries) {\n\t\tconst callDeleted = isToolCallBlockDeleted(callEntry, callId, deletedEntryIds, deletedContentBlocks);\n\t\tconst results = resultEntries.get(callId) ?? [];\n\t\tif (callDeleted) {\n\t\t\tconst danglingResult = results.find((entry) => !deletedEntryIds.has(entry.entryId));\n\t\t\tif (danglingResult) {\n\t\t\t\tthrow new Error(`Deleting tool call ${callId} would leave tool result entry ${danglingResult.entryId} orphaned`);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst deletedResult = results.find((entry) => deletedEntryIds.has(entry.entryId));\n\t\tif (deletedResult) {\n\t\t\tthrow new Error(`Deleting tool result entry ${deletedResult.entryId} would leave tool call ${callId} dangling`);\n\t\t}\n\t}\n}\n\nfunction computeContextCompactionStats(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): ContextCompactionStats {\n\tconst entryById = new Map(transcript.entries.map((entry) => [entry.entryId, entry]));\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tlet deletedTokens = 0;\n\tlet objectsDeleted = 0;\n\n\tfor (const entryId of deletedEntryIds) {\n\t\tconst entry = entryById.get(entryId);\n\t\tif (!entry) continue;\n\t\tdeletedTokens += entry.tokenEstimate;\n\t\tobjectsDeleted += 1 + entry.contentBlocks.length;\n\t}\n\n\tfor (const target of targets) {\n\t\tif (target.kind !== \"content_block\" || deletedEntryIds.has(target.entryId)) continue;\n\t\tconst entry = entryById.get(target.entryId);\n\t\tif (!entry) continue;\n\t\tconst block = entry.contentBlocks.find((item) => item.blockIndex === target.blockIndex);\n\t\tif (!block) continue;\n\t\tdeletedTokens += block.tokenEstimate;\n\t\tobjectsDeleted += 1;\n\t}\n\n\tconst objectsBefore = transcript.entries.length + transcript.entries.reduce((total, entry) => total + entry.contentBlocks.length, 0);\n\tconst tokensBefore = transcript.tokensBefore;\n\tconst tokensAfter = Math.max(0, tokensBefore - deletedTokens);\n\tconst percentReduction = tokensBefore > 0 ? Math.round(((tokensBefore - tokensAfter) / tokensBefore) * 1000) / 10 : 0;\n\treturn {\n\t\tobjectsBefore,\n\t\tobjectsAfter: Math.max(0, objectsBefore - objectsDeleted),\n\t\tobjectsDeleted,\n\t\ttokensBefore,\n\t\ttokensAfter,\n\t\tpercentReduction,\n\t};\n}\n\ninterface ContextDeletionValidationOptions {\n\tmode?: ContextCompactionMode;\n}\n\nfunction isCriticalOverflowProtectedEntryDeletable(\n\tentry: CompactableTranscriptEntry,\n\ttranscript: CompactableTranscript,\n): boolean {\n\tif (!entry.protected) return true;\n\tconst entryIndex = transcript.entries.findIndex((candidate) => candidate.entryId === entry.entryId);\n\tif (entryIndex < 0) return false;\n\tconst recentBoundary = Math.max(0, transcript.entries.length - CONTEXT_CRITICAL_OVERFLOW_RECENT_ENTRY_COUNT);\n\tif (entryIndex >= recentBoundary) return false;\n\tif (hasAssistantError(entry.message) || hasToolResultError(entry.message) || hasFailedBashExecution(entry.message)) {\n\t\treturn false;\n\t}\n\treturn (\n\t\tentry.role === \"user\" ||\n\t\tentry.role === \"custom\" ||\n\t\tentry.role === \"branchSummary\" ||\n\t\tentry.role === \"compactionSummary\" ||\n\t\tentry.entryType === \"branch_summary\"\n\t);\n}\n\nfunction canDeleteProtectedTargetInMode(\n\ttranscript: CompactableTranscript,\n\ttarget: ContextDeletionTarget,\n\tmode: ContextCompactionMode,\n): boolean {\n\tif (mode !== \"critical_overflow\") return false;\n\tconst entry = transcript.entries.find((candidate) => candidate.entryId === target.entryId);\n\tif (!entry || !isCriticalOverflowProtectedEntryDeletable(entry, transcript)) return false;\n\tif (target.kind === \"entry\") return true;\n\tconst block = entry.contentBlocks.find((candidate) => candidate.blockIndex === target.blockIndex);\n\treturn block !== undefined;\n}\n\nexport function validateContextDeletionRequest(\n\trequest: ContextDeletionRequest,\n\ttranscript: CompactableTranscript,\n\toptions: ContextDeletionValidationOptions = {},\n): ValidatedContextDeletionResult {\n\tconst mode = options.mode ?? \"standard\";\n\tif (!request || typeof request !== \"object\" || !Array.isArray(request.deletions)) {\n\t\tthrow new Error(\"Context deletion request must be an object with a deletions array\");\n\t}\n\n\tconst entryById = new Map(transcript.entries.map((entry) => [entry.entryId, entry]));\n\tconst seen = new Set<string>();\n\tconst deletedTargets: ContextDeletionTarget[] = [];\n\n\tfor (const deletion of request.deletions) {\n\t\tif (!deletion || typeof deletion !== \"object\") {\n\t\t\tthrow new Error(\"Deletion target must be an object\");\n\t\t}\n\t\tif (deletion.kind !== \"entry\" && deletion.kind !== \"content_block\") {\n\t\t\tthrow new Error(`Unsupported deletion target kind: ${String((deletion as { kind?: unknown }).kind)}`);\n\t\t}\n\t\tif (typeof deletion.entryId !== \"string\" || deletion.entryId.length === 0) {\n\t\t\tthrow new Error(\"Deletion target entryId must be a non-empty string\");\n\t\t}\n\t\tconst entry = entryById.get(deletion.entryId);\n\t\tif (!entry) {\n\t\t\tthrow new Error(`Unknown deletion target entryId: ${deletion.entryId}`);\n\t\t}\n\t\tif (entry.protected && !canDeleteProtectedTargetInMode(transcript, normalizeRawTarget(deletion), mode)) {\n\t\t\tthrow new Error(`Deletion target ${deletion.entryId} is protected`);\n\t\t}\n\n\t\tif (deletion.kind === \"content_block\") {\n\t\t\tif (!Number.isInteger(deletion.blockIndex) || deletion.blockIndex === undefined || deletion.blockIndex < 0) {\n\t\t\t\tthrow new Error(`Invalid content block index for entry ${deletion.entryId}`);\n\t\t\t}\n\t\t\tconst block = entry.contentBlocks.find((item) => item.blockIndex === deletion.blockIndex);\n\t\t\tif (!block) {\n\t\t\t\tthrow new Error(`Unknown content block ${deletion.blockIndex} for entry ${deletion.entryId}`);\n\t\t\t}\n\t\t\tif (block.protected && !canDeleteProtectedTargetInMode(transcript, normalizeRawTarget(deletion), mode)) {\n\t\t\t\tthrow new Error(`Content block ${deletion.entryId}:${deletion.blockIndex} is protected`);\n\t\t\t}\n\t\t\tif (entry.contentBlocks.length <= 1) {\n\t\t\t\tthrow new Error(`Deleting the only content block of ${deletion.entryId} must be an entry deletion`);\n\t\t\t}\n\t\t}\n\n\t\tconst key = rawTargetKey(deletion);\n\t\tif (seen.has(key)) {\n\t\t\tthrow new Error(`Duplicate deletion target: ${key}`);\n\t\t}\n\t\tseen.add(key);\n\t\tconst normalized = normalizeRawTarget(deletion);\n\t\tdeletedTargets.push(normalized);\n\t}\n\n\tconst reconciledTargets = reconcileToolDependencies(transcript, deletedTargets);\n\tconst reconciledDeletedEntryIds = getDeletedEntryIds(reconciledTargets);\n\n\tfor (const target of reconciledTargets) {\n\t\tif (target.kind === \"content_block\" && reconciledDeletedEntryIds.has(target.entryId)) {\n\t\t\tthrow new Error(`Deletion target ${targetKey(target)} overlaps with entry deletion`);\n\t\t}\n\t}\n\n\tconst deletedContentBlocks = getDeletedContentBlocks(reconciledTargets);\n\tfor (const [entryId, blockIndexes] of deletedContentBlocks) {\n\t\tconst entry = entryById.get(entryId);\n\t\tif (entry?.contentBlocks.every((block) => blockIndexes.has(block.blockIndex))) {\n\t\t\tthrow new Error(`Content-block deletions for ${entryId} would remove every content block`);\n\t\t}\n\t}\n\n\tvalidateToolDependencies(transcript, reconciledTargets);\n\n\tconst remainingEntries = transcript.entries.filter((entry) => !reconciledDeletedEntryIds.has(entry.entryId));\n\tif (remainingEntries.length === 0) {\n\t\tthrow new Error(\"Deletion request would remove all context entries\");\n\t}\n\tconst hasTaskBearingContext = remainingEntries.some(\n\t\t(entry) => entry.role === \"user\" || (entry.role === \"compactionSummary\" && entry.protected),\n\t);\n\tif (!hasTaskBearingContext) {\n\t\tthrow new Error(\"Deletion request would leave no user task in context\");\n\t}\n\n\treturn {\n\t\tdeletedTargets: reconciledTargets,\n\t\tprotectedEntryIds: [...transcript.protectedEntryIds],\n\t\tstats: computeContextCompactionStats(transcript, reconciledTargets),\n\t};\n}\n\nfunction stripJsonFence(text: string): string {\n\tconst trimmed = text.trim();\n\tif (!trimmed.startsWith(\"```\") || !trimmed.endsWith(\"```\")) return trimmed;\n\n\tconst firstLineEnd = trimmed.indexOf(\"\\n\");\n\tif (firstLineEnd < 0) return trimmed;\n\n\tconst fenceInfo = trimmed.slice(3, firstLineEnd).trim().toLowerCase();\n\tif (fenceInfo !== \"\" && fenceInfo !== \"json\") return trimmed;\n\n\treturn trimmed.slice(firstLineEnd + 1, -3).trim();\n}\n\nfunction contextDeletionRequestFromObject(value: unknown, source: string): ContextDeletionRequest {\n\tif (!value || typeof value !== \"object\" || !Array.isArray((value as { deletions?: unknown }).deletions)) {\n\t\tthrow new Error(`${source} must contain a deletions array`);\n\t}\n\treturn value as ContextDeletionRequest;\n}\n\nfunction escapeRegExpLiteral(text: string): string {\n\treturn text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nfunction formatErrorMessage(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n\nfunction createContextDeletionToolResult<TDetails>(text: string, details: TDetails): AgentToolResult<TDetails> {\n\treturn { content: [{ type: \"text\", text }], details, terminate: false };\n}\n\nfunction assertSafeRegexPattern(pattern: string): void {\n\tif (pattern.length > CONTEXT_GREP_DELETE_MAX_REGEX_PATTERN_CHARS) {\n\t\tthrow new Error(\n\t\t\t`Regex pattern is too long (${pattern.length} characters); maximum is ${CONTEXT_GREP_DELETE_MAX_REGEX_PATTERN_CHARS}`,\n\t\t);\n\t}\n\n\t// Heuristic ReDoS guard for common catastrophic-backtracking shapes. JavaScript's RegExp engine\n\t// does not expose a timeout, so reject nested quantified groups and backreferences instead of\n\t// relying only on transcript scan-size caps.\n\tconst hasNestedQuantifiedGroup = /\\((?:[^()\\\\]|\\\\.)*[+*](?:[^()\\\\]|\\\\.)*\\)\\s*(?:[+*]|\\{\\d)/u.test(pattern);\n\tconst hasQuantifiedAlternation = /\\((?:[^()\\\\]|\\\\.)*\\|(?:[^()\\\\]|\\\\.)*\\)\\s*(?:[+*]|\\{\\d)/u.test(pattern);\n\tconst hasBackreference = /\\\\[1-9]/u.test(pattern);\n\tif (hasNestedQuantifiedGroup || hasQuantifiedAlternation || hasBackreference) {\n\t\tthrow new Error(\n\t\t\t\"Regex pattern is not allowed because it may cause excessive backtracking; use a literal pattern or exact deletion targets instead.\",\n\t\t);\n\t}\n}\n\nfunction createGrepMatcher(pattern: string, regex: boolean, caseSensitive: boolean): RegExp {\n\tif (regex) {\n\t\tassertSafeRegexPattern(pattern);\n\t}\n\n\ttry {\n\t\treturn new RegExp(regex ? pattern : escapeRegExpLiteral(pattern), caseSensitive ? \"u\" : \"iu\");\n\t} catch (error) {\n\t\tthrow new Error(`Invalid grep ${regex ? \"regex\" : \"pattern\"}: ${formatErrorMessage(error)}`);\n\t}\n}\n\nfunction assertSafeRegexScan(scanChars: number): void {\n\tif (scanChars <= CONTEXT_GREP_DELETE_MAX_REGEX_SCAN_CHARS) return;\n\tthrow new Error(\n\t\t`Regex grep would scan ${scanChars} characters; maximum is ${CONTEXT_GREP_DELETE_MAX_REGEX_SCAN_CHARS}. Use a literal pattern or exact deletion targets instead.`,\n\t);\n}\n\nfunction clampInteger(value: number | undefined, defaultValue: number, minimum: number, maximum: number): number {\n\tif (value === undefined) return defaultValue;\n\treturn Math.max(minimum, Math.min(maximum, value));\n}\n\nfunction textSlice(text: string, offset: number, maxChars: number): string {\n\treturn text.slice(offset, Math.min(text.length, offset + maxChars));\n}\n\nfunction findMatchIndex(matcher: RegExp, text: string): number {\n\tconst match = matcher.exec(text);\n\tmatcher.lastIndex = 0;\n\treturn match?.index ?? -1;\n}\n\nfunction snippetForMatch(text: string, matchIndex: number, contextChars: number): string {\n\tconst start = Math.max(0, matchIndex - contextChars);\n\tconst end = Math.min(text.length, matchIndex + contextChars);\n\tconst prefix = start > 0 ? \"…\" : \"\";\n\tconst suffix = end < text.length ? \"…\" : \"\";\n\treturn `${prefix}${text.slice(start, end)}${suffix}`;\n}\n\nfunction currentTargetDeleted(targets: readonly ContextDeletionTarget[], target: ContextDeletionTarget): boolean {\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tif (deletedEntryIds.has(target.entryId)) return true;\n\tif (target.kind === \"entry\") return false;\n\treturn getDeletedContentBlocks(targets).get(target.entryId)?.has(target.blockIndex) === true;\n}\n\nfunction addGrepCandidate(\n\tcandidates: ContextDeletionTarget[],\n\tmatches: ContextGrepDeletionMatch[],\n\tseenTargets: Set<string>,\n\tcandidate: ContextDeletionTarget,\n\tmatch: ContextGrepDeletionMatch,\n): void {\n\tconst key = targetKey(candidate);\n\tif (seenTargets.has(key)) return;\n\tseenTargets.add(key);\n\tcandidates.push(candidate);\n\tmatches.push(match);\n}\n\ninterface EntryTextRow {\n\tentry_id: string;\n\ttext: string;\n\tis_protected: number;\n}\n\ninterface EntryReadRow extends EntryTextRow {\n\trole: string;\n\ttoken_estimate: number;\n}\n\ninterface ContentBlockTextRow {\n\tentry_id: string;\n\tblock_index: number;\n\ttext: string;\n\tentry_protected: number;\n\tblock_protected: number;\n\tblock_count: number;\n}\n\ninterface ContentBlockReadRow extends ContentBlockTextRow {\n\ttype: string;\n\ttoken_estimate: number;\n}\n\ninterface StoredTranscriptEntry {\n\tentryId: string;\n\trole: AgentMessage[\"role\"];\n\tprotected: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\ninterface StoredContentBlock {\n\tentryPosition: number;\n\tentryId: string;\n\tblockIndex: number;\n\ttype: string;\n\tprotected: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\ninterface ContextDeletionMemorySnapshot {\n\tdeletionTargets: ContextDeletionTarget[];\n\tcallCount: number;\n\tlastError?: string;\n}\n\nfunction copyDeletionTarget(target: ContextDeletionTarget): ContextDeletionTarget {\n\treturn target.kind === \"entry\"\n\t\t? { kind: \"entry\", entryId: target.entryId }\n\t\t: { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nclass ContextDeletionMemoryStore {\n\tprivate readonly entries: StoredTranscriptEntry[];\n\tprivate readonly entriesById: Map<string, StoredTranscriptEntry>;\n\tprivate readonly contentBlocks: StoredContentBlock[];\n\tprivate readonly contentBlockCountByEntryId: Map<string, number>;\n\tprivate deletionTargets: ContextDeletionTarget[] = [];\n\tprivate callCount = 0;\n\tprivate lastError: string | undefined;\n\n\tconstructor(transcript: CompactableTranscript) {\n\t\tconst entryIds = new Set<string>();\n\t\tconst blockKeys = new Set<string>();\n\t\tthis.entries = transcript.entries.map((entry) => {\n\t\t\tif (entryIds.has(entry.entryId)) {\n\t\t\t\tthrow new Error(`Duplicate transcript entry id: ${entry.entryId}`);\n\t\t\t}\n\t\t\tentryIds.add(entry.entryId);\n\t\t\treturn {\n\t\t\t\tentryId: entry.entryId,\n\t\t\t\trole: entry.role,\n\t\t\t\tprotected: entry.protected,\n\t\t\t\ttokenEstimate: entry.tokenEstimate,\n\t\t\t\ttext: entry.text,\n\t\t\t};\n\t\t});\n\t\tthis.entriesById = new Map<string, StoredTranscriptEntry>(this.entries.map((entry) => [entry.entryId, entry] as const));\n\t\tthis.contentBlocks = transcript.entries.flatMap((entry, entryPosition) =>\n\t\t\tentry.contentBlocks.map((block) => {\n\t\t\t\tif (block.entryId !== entry.entryId) {\n\t\t\t\t\tthrow new Error(`Transcript content block ${block.entryId}:${block.blockIndex} does not belong to entry ${entry.entryId}`);\n\t\t\t\t}\n\t\t\t\tconst blockKey = `${block.entryId}:${block.blockIndex}`;\n\t\t\t\tif (blockKeys.has(blockKey)) {\n\t\t\t\t\tthrow new Error(`Duplicate transcript content block: ${blockKey}`);\n\t\t\t\t}\n\t\t\t\tblockKeys.add(blockKey);\n\t\t\t\treturn {\n\t\t\t\t\tentryPosition,\n\t\t\t\t\tentryId: block.entryId,\n\t\t\t\t\tblockIndex: block.blockIndex,\n\t\t\t\t\ttype: block.type,\n\t\t\t\t\tprotected: block.protected,\n\t\t\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\t\t\ttext: block.text,\n\t\t\t\t};\n\t\t\t}),\n\t\t);\n\t\tthis.contentBlockCountByEntryId = new Map();\n\t\tfor (const block of this.contentBlocks) {\n\t\t\tthis.contentBlockCountByEntryId.set(block.entryId, (this.contentBlockCountByEntryId.get(block.entryId) ?? 0) + 1);\n\t\t}\n\t}\n\n\ttransaction<T>(operation: () => T): T {\n\t\tconst snapshot = this.snapshot();\n\t\ttry {\n\t\t\treturn operation();\n\t\t} catch (error) {\n\t\t\tthis.restore(snapshot);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treadTargets(): ContextDeletionTarget[] {\n\t\treturn this.deletionTargets.map(copyDeletionTarget);\n\t}\n\n\treplaceTargets(targets: readonly ContextDeletionTarget[]): void {\n\t\tthis.deletionTargets = targets.map(copyDeletionTarget);\n\t}\n\n\tlistEntriesForGrep(): EntryTextRow[] {\n\t\treturn this.entries.map((entry) => ({\n\t\t\tentry_id: entry.entryId,\n\t\t\ttext: entry.text,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t}));\n\t}\n\n\tlistContentBlocksForGrep(): ContentBlockTextRow[] {\n\t\treturn [...this.contentBlocks]\n\t\t\t.sort((a, b) => a.entryPosition - b.entryPosition || a.blockIndex - b.blockIndex)\n\t\t\t.map((block) => ({\n\t\t\t\tentry_id: block.entryId,\n\t\t\t\tblock_index: block.blockIndex,\n\t\t\t\ttext: block.text,\n\t\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t\t}));\n\t}\n\n\tgetEntryForRead(entryId: string): EntryReadRow | undefined {\n\t\tconst entry = this.entriesById.get(entryId);\n\t\tif (!entry) return undefined;\n\t\treturn {\n\t\t\tentry_id: entry.entryId,\n\t\t\trole: entry.role,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t\ttoken_estimate: entry.tokenEstimate,\n\t\t\ttext: entry.text,\n\t\t};\n\t}\n\n\tgetContentBlockForRead(entryId: string, blockIndex: number): ContentBlockReadRow | undefined {\n\t\tconst block = this.contentBlocks.find((candidate) => candidate.entryId === entryId && candidate.blockIndex === blockIndex);\n\t\tif (!block) return undefined;\n\t\treturn {\n\t\t\tentry_id: block.entryId,\n\t\t\tblock_index: block.blockIndex,\n\t\t\ttype: block.type,\n\t\t\ttoken_estimate: block.tokenEstimate,\n\t\t\ttext: block.text,\n\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t};\n\t}\n\n\tgetGrepScanTextLength(target: \"entry\" | \"content_block\"): number {\n\t\tconst texts = target === \"entry\" ? this.entries : this.contentBlocks;\n\t\treturn texts.reduce((sum, item) => sum + item.text.length, 0);\n\t}\n\n\tincrementCallCount(): number {\n\t\tthis.callCount += 1;\n\t\treturn this.callCount;\n\t}\n\n\tgetCallCount(): number {\n\t\treturn this.callCount;\n\t}\n\n\tsetLastError(message: string): void {\n\t\tthis.lastError = message;\n\t}\n\n\tclearLastError(): void {\n\t\tthis.lastError = undefined;\n\t}\n\n\tgetLastError(): string | undefined {\n\t\treturn this.lastError;\n\t}\n\n\tprivate snapshot(): ContextDeletionMemorySnapshot {\n\t\treturn {\n\t\t\tdeletionTargets: this.readTargets(),\n\t\t\tcallCount: this.callCount,\n\t\t\t...(this.lastError === undefined ? {} : { lastError: this.lastError }),\n\t\t};\n\t}\n\n\tprivate restore(snapshot: ContextDeletionMemorySnapshot): void {\n\t\tthis.deletionTargets = snapshot.deletionTargets.map(copyDeletionTarget);\n\t\tthis.callCount = snapshot.callCount;\n\t\tthis.lastError = snapshot.lastError;\n\t}\n}\n\nfunction createContextDeletionStore(transcript: CompactableTranscript): ContextDeletionMemoryStore {\n\treturn new ContextDeletionMemoryStore(transcript);\n}\n\nexport function createContextDeletionTool(\n\ttranscript: CompactableTranscript,\n\toptions: ContextCompactionRunOptions = {},\n): ContextDeletionToolController {\n\tconst mode = options.mode ?? \"standard\";\n\tconst store = createContextDeletionStore(transcript);\n\tlet validatedResult: ValidatedContextDeletionResult | undefined;\n\n\tfunction readTargets(): ContextDeletionTarget[] {\n\t\treturn store.readTargets();\n\t}\n\n\tfunction applyValidatedTargets(additionalTargets: readonly ContextDeletionTarget[]): ValidatedContextDeletionResult {\n\t\tconst mergedTargets = mergeContextDeletionTargets(readTargets(), additionalTargets);\n\t\tvalidatedResult = validateContextDeletionRequest(deletionRequestFromTargets(mergedTargets), transcript, { mode });\n\t\tstore.replaceTargets(validatedResult.deletedTargets);\n\t\treturn validatedResult;\n\t}\n\n\tfunction currentStats(): ContextCompactionStats {\n\t\treturn validatedResult?.stats ?? computeContextCompactionStats(transcript, readTargets());\n\t}\n\n\tfunction canDeleteProtectedTarget(target: ContextDeletionTarget): boolean {\n\t\treturn canDeleteProtectedTargetInMode(transcript, target, mode);\n\t}\n\n\tconst tool: AgentTool<typeof ContextDeleteToolParameters, ContextDeletionToolDetails> = {\n\t\t...CONTEXT_DELETE_TOOL,\n\t\tlabel: \"context deletion request\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\ttry {\n\t\t\t\t\tconst incomingRequest = contextDeletionRequestFromObject(params, `${CONTEXT_DELETE_TOOL_NAME} arguments`);\n\t\t\t\t\tconst incomingValidated = validateContextDeletionRequest(incomingRequest, transcript, { mode });\n\t\t\t\t\tconst applied = applyValidatedTargets(incomingValidated.deletedTargets);\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst deletedTargets = readTargets();\n\n\t\t\t\t\tconst details: ContextDeletionToolDetails = {\n\t\t\t\t\t\tdeletions: deletionRequestFromTargets(deletedTargets).deletions,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: applied.stats,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Recorded ${incomingValidated.deletedTargets.length} deletion target(s); ${deletedTargets.length} total validated deletion target(s) are selected. Continue calling ${CONTEXT_DELETE_TOOL_NAME} or ${CONTEXT_GREP_DELETE_TOOL_NAME} for additional deletions, or respond done when finished.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst deletedTargets = readTargets();\n\t\t\t\t\tconst details: ContextDeletionToolDetails = {\n\t\t\t\t\t\tdeletions: deletionRequestFromTargets(deletedTargets).deletions,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error recording context deletion targets: ${message}. No new deletion targets were applied; continue with a corrected tool call.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst grepTool: AgentTool<typeof ContextGrepDeleteToolParameters, ContextGrepDeletionToolDetails> = {\n\t\t...CONTEXT_GREP_DELETE_TOOL,\n\t\tlabel: \"context grep delete\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst pattern = params.pattern;\n\t\t\t\tconst regex = params.regex === true;\n\t\t\t\tconst caseSensitive = params.caseSensitive === true;\n\t\t\t\tconst target = params.target ?? \"entry\";\n\t\t\t\tconst maxMatches = params.maxMatches ?? CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES;\n\t\t\t\tconst candidates: ContextDeletionTarget[] = [];\n\t\t\t\tconst matches: ContextGrepDeletionMatch[] = [];\n\t\t\t\tconst skipped: ContextGrepDeletionSkipped[] = [];\n\t\t\t\tconst seenTargets = new Set<string>();\n\n\t\t\t\ttry {\n\t\t\t\t\tif (regex) {\n\t\t\t\t\t\tassertSafeRegexScan(store.getGrepScanTextLength(target));\n\t\t\t\t\t}\n\t\t\t\t\tconst matcher = createGrepMatcher(pattern, regex, caseSensitive);\n\t\t\t\t\tconst currentTargets = readTargets();\n\n\t\t\t\t\tif (target === \"entry\") {\n\t\t\t\t\t\tfor (const entry of store.listEntriesForGrep()) {\n\t\t\t\t\t\t\tif (!matcher.test(entry.text)) continue;\n\t\t\t\t\t\t\tconst candidate: ContextDeletionTarget = { kind: \"entry\", entryId: entry.entry_id };\n\t\t\t\t\t\t\tif (entry.is_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"protected_entry\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentTargetDeleted(currentTargets, candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"already_deleted\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taddGrepCandidate(candidates, matches, seenTargets, candidate, {\n\t\t\t\t\t\t\t\tentryId: entry.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\ttext: entry.text,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfor (const block of store.listContentBlocksForGrep()) {\n\t\t\t\t\t\t\tif (!matcher.test(block.text)) continue;\n\t\t\t\t\t\t\tconst candidate: ContextDeletionTarget =\n\t\t\t\t\t\t\t\tblock.block_count <= 1\n\t\t\t\t\t\t\t\t\t? { kind: \"entry\", entryId: block.entry_id }\n\t\t\t\t\t\t\t\t\t: { kind: \"content_block\", entryId: block.entry_id, blockIndex: block.block_index };\n\t\t\t\t\t\t\tif (block.entry_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\t\treason: \"protected_entry\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (block.block_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\t\treason: \"protected_block\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentTargetDeleted(currentTargets, candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\t\treason: \"already_deleted\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taddGrepCandidate(candidates, matches, seenTargets, candidate, {\n\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet applied: ValidatedContextDeletionResult | undefined;\n\t\t\t\t\tif (params.expectedMatchCount !== undefined && candidates.length !== params.expectedMatchCount) {\n\t\t\t\t\t\tskipped.push({ reason: \"expected_match_count_mismatch\" });\n\t\t\t\t\t} else if (candidates.length > maxMatches) {\n\t\t\t\t\t\tskipped.push({ reason: \"max_matches_exceeded\" });\n\t\t\t\t\t} else if (candidates.length > 0) {\n\t\t\t\t\t\tapplied = applyValidatedTargets(candidates);\n\t\t\t\t\t}\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst deletedTargets = readTargets();\n\n\t\t\t\t\tconst details: ContextGrepDeletionToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\tskipped,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: applied?.stats ?? currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Matched ${matches.length} deletion target(s), skipped ${skipped.length}, and ${applied ? \"applied\" : \"did not apply\"} grep deletion for pattern ${JSON.stringify(pattern)}. Total validated deletion target(s): ${deletedTargets.length}.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst deletedTargets = readTargets();\n\t\t\t\t\tconst details: ContextGrepDeletionToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\tskipped,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error applying grep deletion for pattern ${JSON.stringify(pattern)}: ${message}. No new deletion targets were applied; continue with a corrected tool call.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst searchTool: AgentTool<typeof ContextSearchTranscriptToolParameters, ContextTranscriptSearchToolDetails> = {\n\t\t...CONTEXT_SEARCH_TRANSCRIPT_TOOL,\n\t\tlabel: \"context transcript search\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst pattern = params.pattern;\n\t\t\t\tconst regex = params.regex === true;\n\t\t\t\tconst caseSensitive = params.caseSensitive === true;\n\t\t\t\tconst target = params.target ?? \"entry\";\n\t\t\t\tconst maxMatches = clampInteger(params.maxMatches, CONTEXT_SEARCH_DEFAULT_MAX_MATCHES, 1, CONTEXT_SEARCH_MAX_MATCHES);\n\t\t\t\tconst contextChars = clampInteger(\n\t\t\t\t\tparams.contextChars,\n\t\t\t\t\tCONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS,\n\t\t\t\t\t0,\n\t\t\t\t\tCONTEXT_SEARCH_MAX_CONTEXT_CHARS,\n\t\t\t\t);\n\t\t\t\tconst matches: ContextTranscriptSearchMatch[] = [];\n\t\t\t\tlet truncated = false;\n\n\t\t\t\ttry {\n\t\t\t\t\tif (regex) {\n\t\t\t\t\t\tassertSafeRegexScan(store.getGrepScanTextLength(target));\n\t\t\t\t\t}\n\t\t\t\t\tconst matcher = createGrepMatcher(pattern, regex, caseSensitive);\n\t\t\t\t\tif (target === \"entry\") {\n\t\t\t\t\t\tfor (const entry of store.listEntriesForGrep()) {\n\t\t\t\t\t\t\tconst matchIndex = findMatchIndex(matcher, entry.text);\n\t\t\t\t\t\t\tif (matchIndex < 0) continue;\n\t\t\t\t\t\t\tif (matches.length >= maxMatches) {\n\t\t\t\t\t\t\t\ttruncated = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmatches.push({\n\t\t\t\t\t\t\t\tentryId: entry.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\tmatchIndex,\n\t\t\t\t\t\t\t\tsnippet: snippetForMatch(entry.text, matchIndex, contextChars),\n\t\t\t\t\t\t\t\tprotected: entry.is_protected === 1,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfor (const block of store.listContentBlocksForGrep()) {\n\t\t\t\t\t\t\tconst matchIndex = findMatchIndex(matcher, block.text);\n\t\t\t\t\t\t\tif (matchIndex < 0) continue;\n\t\t\t\t\t\t\tif (matches.length >= maxMatches) {\n\t\t\t\t\t\t\t\ttruncated = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmatches.push({\n\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\tmatchIndex,\n\t\t\t\t\t\t\t\tsnippet: snippetForMatch(block.text, matchIndex, contextChars),\n\t\t\t\t\t\t\t\tprotected: block.entry_protected === 1 || block.block_protected === 1,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst details: ContextTranscriptSearchToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\ttruncated,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Found ${matches.length}${truncated ? \"+\" : \"\"} ${target} match(es) for ${JSON.stringify(pattern)}. Use ${CONTEXT_READ_ENTRY_TOOL_NAME} with small maxChars to inspect exact content before deleting.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst details: ContextTranscriptSearchToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\ttruncated,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error searching transcript for ${JSON.stringify(pattern)}: ${message}. Try a literal pattern or narrower query.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst readEntryTool: AgentTool<typeof ContextReadEntryToolParameters, ContextReadEntryToolDetails> = {\n\t\t...CONTEXT_READ_ENTRY_TOOL,\n\t\tlabel: \"context read entry\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst offset = clampInteger(params.offset, 0, 0, Number.MAX_SAFE_INTEGER);\n\t\t\t\tconst maxChars = clampInteger(\n\t\t\t\t\tparams.maxChars,\n\t\t\t\t\tCONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS,\n\t\t\t\t\t1,\n\t\t\t\t\tCONTEXT_READ_ENTRY_MAX_CHARS,\n\t\t\t\t);\n\t\t\t\ttry {\n\t\t\t\t\tconst row =\n\t\t\t\t\t\tparams.blockIndex === undefined\n\t\t\t\t\t\t\t? store.getEntryForRead(params.entryId)\n\t\t\t\t\t\t\t: store.getContentBlockForRead(params.entryId, params.blockIndex);\n\t\t\t\t\tif (!row) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\tparams.blockIndex === undefined\n\t\t\t\t\t\t\t\t? `Unknown transcript entry: ${params.entryId}`\n\t\t\t\t\t\t\t\t: `Unknown transcript content block: ${params.entryId}:${params.blockIndex}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tconst text = row.text;\n\t\t\t\t\tconst slice = textSlice(text, offset, maxChars);\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst details: ContextReadEntryToolDetails = {\n\t\t\t\t\t\tentryId: params.entryId,\n\t\t\t\t\t\t...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),\n\t\t\t\t\t\toffset,\n\t\t\t\t\t\tmaxChars,\n\t\t\t\t\t\ttotalChars: text.length,\n\t\t\t\t\t\ttext: slice,\n\t\t\t\t\t\ttruncatedBefore: offset > 0,\n\t\t\t\t\t\ttruncatedAfter: offset + maxChars < text.length,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst textResult = `Read ${slice.length} of ${text.length} characters from ${params.blockIndex === undefined ? params.entryId : `${params.entryId}:${params.blockIndex}`}. Keep reads small; increase offset for the next slice if needed.`;\n\t\t\t\t\treturn createContextDeletionToolResult(textResult, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst details: ContextReadEntryToolDetails = {\n\t\t\t\t\t\tentryId: params.entryId,\n\t\t\t\t\t\t...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),\n\t\t\t\t\t\toffset,\n\t\t\t\t\t\tmaxChars,\n\t\t\t\t\t\ttotalChars: 0,\n\t\t\t\t\t\ttext: \"\",\n\t\t\t\t\t\ttruncatedBefore: false,\n\t\t\t\t\t\ttruncatedAfter: false,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(`Error reading transcript entry: ${message}`, details);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\treturn {\n\t\ttool,\n\t\tgrepTool,\n\t\tsearchTool,\n\t\treadEntryTool,\n\t\ttools: [tool, grepTool, searchTool, readEntryTool],\n\t\tgetDeletionRequest: () => deletionRequestFromTargets(readTargets()),\n\t\tgetValidatedResult: () => validatedResult,\n\t\tgetLastError: () => store.getLastError(),\n\t\tgetCallCount: () => store.getCallCount(),\n\t};\n}\n\nexport function parseContextDeletionRequest(text: string): ContextDeletionRequest {\n\tconst stripped = stripJsonFence(text);\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(stripped);\n\t} catch (error) {\n\t\tthrow new Error(`Failed to parse context deletion request JSON: ${error instanceof Error ? error.message : String(error)}`);\n\t}\n\treturn contextDeletionRequestFromObject(parsed, \"Context deletion request JSON\");\n}\n\nfunction isContextDeleteToolCall(content: AssistantMessage[\"content\"][number]): content is ToolCall {\n\treturn content.type === \"toolCall\" && content.name === CONTEXT_DELETE_TOOL_NAME;\n}\n\nfunction textContentFromResponse(response: AssistantMessage): string {\n\treturn response.content\n\t\t.filter((content): content is { type: \"text\"; text: string } => content.type === \"text\")\n\t\t.map((content) => content.text)\n\t\t.join(\"\\n\");\n}\n\nexport function parseContextDeletionResponse(response: AssistantMessage): ContextDeletionRequest {\n\tconst toolCalls = response.content.filter(isContextDeleteToolCall);\n\tif (toolCalls.length > 1) {\n\t\tthrow new Error(`Context compaction assistant called ${CONTEXT_DELETE_TOOL_NAME} more than once`);\n\t}\n\tconst toolCall = toolCalls[0];\n\tif (toolCall) {\n\t\treturn contextDeletionRequestFromObject(toolCall.arguments, `${CONTEXT_DELETE_TOOL_NAME} arguments`);\n\t}\n\n\tconst textContent = textContentFromResponse(response);\n\tif (textContent.trim().length === 0) {\n\t\tthrow new Error(`Context compaction assistant did not call ${CONTEXT_DELETE_TOOL_NAME}`);\n\t}\n\treturn parseContextDeletionRequest(textContent);\n}\n\nfunction truncateForPrompt(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, maxChars)}\\n[... ${text.length - maxChars} more characters omitted from context compaction prompt]`;\n}\n\nfunction transcriptEntryFilePayload(entry: CompactableTranscriptEntry): unknown {\n\treturn {\n\t\tentryId: entry.entryId,\n\t\tentryType: entry.entryType,\n\t\trole: entry.role,\n\t\tprotected: entry.protected,\n\t\ttokenEstimate: entry.tokenEstimate,\n\t\ttoolCallIds: entry.toolCallIds,\n\t\ttoolResultFor: entry.toolResultFor,\n\t\ttext: entry.text,\n\t\tcontentBlocks: entry.contentBlocks.map((block) => ({\n\t\t\tblockIndex: block.blockIndex,\n\t\t\ttype: block.type,\n\t\t\tprotected: block.protected,\n\t\t\ttoolCallId: block.toolCallId,\n\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\ttext: block.text,\n\t\t})),\n\t};\n}\n\ninterface ContextCompactionTranscriptFile {\n\tpath: string;\n\tcleanup(): void;\n}\n\nfunction writeContextCompactionTranscriptFile(transcript: CompactableTranscript): ContextCompactionTranscriptFile {\n\tconst directory = mkdtempSync(join(tmpdir(), \"atomic-context-transcript-\"));\n\tconst path = join(directory, \"transcript.jsonl\");\n\tconst lines = transcript.entries\n\t\t.filter((entry) => !isExcludedFromLlmContext(entry.message))\n\t\t.map((entry) => JSON.stringify(transcriptEntryFilePayload(entry)));\n\twriteFileSync(path, `${lines.join(\"\\n\")}\\n`, \"utf8\");\n\treturn {\n\t\tpath,\n\t\tcleanup: () => rmSync(directory, { recursive: true, force: true }),\n\t};\n}\n\nfunction contextCompactionTranscriptManifest(transcript: CompactableTranscript, transcriptFilePath: string): unknown {\n\tconst eligibleEntries = transcript.entries.filter((entry) => !isExcludedFromLlmContext(entry.message));\n\tconst selectedEntryIds = new Set<string>();\n\tconst selectedEntries: CompactableTranscriptEntry[] = [];\n\tconst addEntry = (entry: CompactableTranscriptEntry): void => {\n\t\tif (selectedEntryIds.has(entry.entryId) || selectedEntries.length >= CONTEXT_MANIFEST_MAX_ENTRIES) return;\n\t\tselectedEntryIds.add(entry.entryId);\n\t\tselectedEntries.push(entry);\n\t};\n\n\tfor (const entry of eligibleEntries.filter((entry) => entry.protected)) {\n\t\taddEntry(entry);\n\t}\n\tfor (const entry of [...eligibleEntries]\n\t\t.filter((entry) => !entry.protected)\n\t\t.sort((left, right) => right.tokenEstimate - left.tokenEstimate)) {\n\t\taddEntry(entry);\n\t}\n\tselectedEntries.sort((left, right) => eligibleEntries.indexOf(left) - eligibleEntries.indexOf(right));\n\n\treturn {\n\t\ttranscriptFilePath,\n\t\ttranscriptFileFormat: \"jsonl: one compactable transcript entry per line with full text and contentBlocks text\",\n\t\ttotalEntries: eligibleEntries.length,\n\t\tmanifestEntries: selectedEntries.length,\n\t\tomittedEntries: Math.max(0, eligibleEntries.length - selectedEntries.length),\n\t\ttokensBefore: transcript.tokensBefore,\n\t\tprotectedEntryIds: transcript.protectedEntryIds,\n\t\tentries: selectedEntries.map((entry) => ({\n\t\t\tentryId: entry.entryId,\n\t\t\trole: entry.role,\n\t\t\tprotected: entry.protected,\n\t\t\ttokenEstimate: entry.tokenEstimate,\n\t\t\ttoolCallIds: entry.toolCallIds,\n\t\t\ttoolResultFor: entry.toolResultFor,\n\t\t\tcontentBlockCount: entry.contentBlocks.length,\n\t\t\tcontentBlocks: entry.contentBlocks.map((block) => ({\n\t\t\t\tblockIndex: block.blockIndex,\n\t\t\t\ttype: block.type,\n\t\t\t\tprotected: block.protected,\n\t\t\t\ttoolCallId: block.toolCallId,\n\t\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\t})),\n\t\t\tpreview: truncateForPrompt(entry.text, CONTEXT_MANIFEST_PREVIEW_CHARS),\n\t\t})),\n\t};\n}\n\nfunction contextCompactionModePrompt(mode: ContextCompactionMode): string {\n\tif (mode === \"critical_overflow\") {\n\t\treturn `\\n<critical-overflow-mode>\\nThe previous model request overflowed its context window. This is a critical LRU-style compaction pass. First delete stale unprotected context. If that is not enough, you may also delete the earliest protected entries or protected content shown in the manifest, especially old user/custom/summary context, while preserving recent entries, unresolved errors, failed commands, and enough task-bearing context for the assistant to continue.\\n</critical-overflow-mode>`;\n\t}\n\treturn `\\n<standard-mode>\\nDo not delete entries or content blocks marked protected. Protected context is only eligible during critical overflow recovery, not during standard compaction.\\n</standard-mode>`;\n}\n\nexport function buildContextCompactionPrompt(\n\ttranscript: CompactableTranscript,\n\ttranscriptFilePath = \"<transcript file will be written during context compaction>\",\n\tmode: ContextCompactionMode = \"standard\",\n): string {\n\treturn `${CONTEXT_COMPACTION_FIXED_PROMPT}${contextCompactionModePrompt(mode)}\\n\\n<transcript-file>\\n${transcriptFilePath}\\n</transcript-file>\\n\\n<context-manifest>\\n${JSON.stringify(contextCompactionTranscriptManifest(transcript, transcriptFilePath), null, 2)}\\n</context-manifest>`;\n}\n\nfunction createContextCompactionAssistantMessage(\n\tmodel: Model<Api>,\n\tcontent: AssistantMessage[\"content\"],\n\tstopReason: AssistantMessage[\"stopReason\"],\n\terrorMessage?: string,\n): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent,\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason,\n\t\t...(errorMessage !== undefined ? { errorMessage } : {}),\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction createContextCompactionStopStream(model: Model<Api>, text: string) {\n\tconst stream = createAssistantMessageEventStream();\n\tqueueMicrotask(() => {\n\t\tconst message = createContextCompactionAssistantMessage(model, [{ type: \"text\", text }], \"stop\");\n\t\tstream.push({ type: \"done\", reason: \"stop\", message });\n\t\tstream.end(message);\n\t});\n\treturn stream;\n}\n\nfunction isContextCompactionOverflowError(model: Model<Api>, errorMessage: string): boolean {\n\treturn isContextOverflow(\n\t\tcreateContextCompactionAssistantMessage(model, [], \"error\", errorMessage),\n\t\tmodel.contextWindow,\n\t);\n}\n\ninterface ContextDeletionRun {\n\tvalidatedResult: ValidatedContextDeletionResult | undefined;\n\tlastToolError: string | undefined;\n}\n\nasync function runContextDeletionAssistant(\n\ttranscript: CompactableTranscript,\n\tmodel: Model<Api>,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tsignal?: AbortSignal,\n\tthinkingLevel: ThinkingLevel = \"off\",\n\tmode: ContextCompactionMode = \"standard\",\n): Promise<ContextDeletionRun> {\n\tconst maxTokens = model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY;\n\tif (signal?.aborted) {\n\t\tthrow new Error(\"Context compaction failed: Request was aborted\");\n\t}\n\tconst transcriptFile = writeContextCompactionTranscriptFile(transcript);\n\tconst promptMessage: AgentMessage = {\n\t\trole: \"user\",\n\t\tcontent: [{ type: \"text\", text: buildContextCompactionPrompt(transcript, transcriptFile.path, mode) }],\n\t\ttimestamp: Date.now(),\n\t};\n\tconst deletionTool = createContextDeletionTool(transcript, { mode });\n\tlet compactionTurnCount = 0;\n\tconst agent = new Agent({\n\t\tinitialState: {\n\t\t\tsystemPrompt: CONTEXT_COMPACTION_SYSTEM_PROMPT,\n\t\t\tmodel,\n\t\t\tthinkingLevel,\n\t\t\ttools: deletionTool.tools,\n\t\t},\n\t\ttoolExecution: \"parallel\",\n\t\tstreamFn: async (requestModel, context, streamOptions) => {\n\t\t\tcompactionTurnCount += 1;\n\t\t\tif (compactionTurnCount > CONTEXT_COMPACTION_MAX_TURNS) {\n\t\t\t\treturn createContextCompactionStopStream(\n\t\t\t\t\trequestModel,\n\t\t\t\t\t`Reached the context compaction turn cap (${CONTEXT_COMPACTION_MAX_TURNS}); using the deletions recorded so far.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn streamSimple(requestModel, context, {\n\t\t\t\t...streamOptions,\n\t\t\t\tmaxTokens,\n\t\t\t\tapiKey,\n\t\t\t\theaders: headers ?? streamOptions?.headers,\n\t\t\t});\n\t\t},\n\t});\n\n\tconst abortOnSignal = () => agent.abort();\n\tsignal?.addEventListener(\"abort\", abortOnSignal, { once: true });\n\ttry {\n\t\tawait agent.prompt(promptMessage);\n\t} finally {\n\t\tsignal?.removeEventListener(\"abort\", abortOnSignal);\n\t\ttranscriptFile.cleanup();\n\t}\n\n\tif (signal?.aborted) {\n\t\tthrow new Error(\"Context compaction failed: Request was aborted\");\n\t}\n\tif (agent.state.errorMessage) {\n\t\tif (isContextCompactionOverflowError(model, agent.state.errorMessage)) {\n\t\t\treturn {\n\t\t\t\tvalidatedResult: deletionTool.getValidatedResult(),\n\t\t\t\tlastToolError: deletionTool.getLastError(),\n\t\t\t};\n\t\t}\n\t\tthrow new Error(`Context compaction failed: ${agent.state.errorMessage}`);\n\t}\n\tif (deletionTool.getCallCount() === 0) {\n\t\tthrow new Error(\n\t\t\t`Context compaction did not call any transcript inspection or deletion tools (${CONTEXT_SEARCH_TRANSCRIPT_TOOL_NAME}, ${CONTEXT_READ_ENTRY_TOOL_NAME}, ${CONTEXT_DELETE_TOOL_NAME}, or ${CONTEXT_GREP_DELETE_TOOL_NAME})`,\n\t\t);\n\t}\n\treturn {\n\t\tvalidatedResult: deletionTool.getValidatedResult(),\n\t\tlastToolError: deletionTool.getLastError(),\n\t};\n}\n\nexport async function contextCompact(\n\tpreparation: ContextCompactionPreparation,\n\tmodel: Model<Api>,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tsignal?: AbortSignal,\n\tthinkingLevel: ThinkingLevel = \"off\",\n\tmode: ContextCompactionMode = preparation.mode ?? \"standard\",\n): Promise<ValidatedContextDeletionResult> {\n\tconst { validatedResult, lastToolError } = await runContextDeletionAssistant(\n\t\tpreparation.transcript,\n\t\tmodel,\n\t\tapiKey,\n\t\theaders,\n\t\tsignal,\n\t\tthinkingLevel,\n\t\tmode,\n\t);\n\tif (!validatedResult || validatedResult.deletedTargets.length === 0) {\n\t\tthrow new Error(\n\t\t\tlastToolError ? `No safe context deletions proposed; last deletion tool error: ${lastToolError}` : \"No safe context deletions proposed\",\n\t\t);\n\t}\n\treturn validatedResult;\n}\n"]}
1
+ {"version":3,"file":"context-compaction.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/context-compaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,KAAK,YAAY,EAAE,KAAK,SAAS,EAAwB,KAAK,aAAa,EAAE,MAAM,+BAA+B,CAAC;AACnI,OAAO,KAAK,EAAE,GAAG,EAAE,gBAAgB,EAAE,KAAK,EAAY,MAAM,uBAAuB,CAAC;AAUpF,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE/B,OAAO,EAGN,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,YAAY,EACjB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAG1D,eAAO,MAAM,iCAAiC,EAAG,CAAU,CAAC;AAE5D,MAAM,MAAM,qBAAqB,GAAG,UAAU,GAAG,mBAAmB,CAAC;AAErE,MAAM,WAAW,sBAAsB;IACtC,SAAS,EAAE,KAAK,CAAC;QAChB,IAAI,EAAE,OAAO,GAAG,eAAe,CAAC;QAChC,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,SAAS,CAAC,EAAE,M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,qBAAsD,GAC1D,OAAO,CAAC,8BAA8B,CAAC,CAgBzC","sourcesContent":["import { Agent, type AgentMessage, type AgentTool, type AgentToolResult, type ThinkingLevel } from \"@earendil-works/pi-agent-core\";\nimport type { Api, AssistantMessage, Model, ToolCall } from \"@earendil-works/pi-ai\";\nimport {\n\tcreateAssistantMessageEventStream,\n\tisContextOverflow,\n\tstreamSimple,\n\tStringEnum,\n} from \"@earendil-works/pi-ai\";\nimport { mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { Type } from \"typebox\";\nimport { createBranchSummaryMessage, createCustomMessage } from \"../messages.ts\";\nimport {\n\tbuildContextDeletionFilteredPath,\n\tbuildContextDeletionFilters,\n\ttype ContextCompactionStats,\n\ttype ContextDeletionTarget,\n\ttype SessionEntry,\n} from \"../session-manager.ts\";\nimport type { CompactionSettings } from \"./compaction.ts\";\nimport { estimateTokens } from \"./compaction.ts\";\n\nexport const CONTEXT_COMPACTION_PROMPT_VERSION = 1 as const;\n\nexport type ContextCompactionMode = \"standard\" | \"critical_overflow\";\n\nexport interface ContextDeletionRequest {\n\tdeletions: Array<{\n\t\tkind: \"entry\" | \"content_block\";\n\t\tentryId: string;\n\t\tblockIndex?: number;\n\t\trationale?: string;\n\t}>;\n}\n\nexport interface CompactableContentBlock {\n\tentryId: string;\n\tblockIndex: number;\n\ttype: string;\n\ttext: string;\n\ttokenEstimate: number;\n\tprotected: boolean;\n\ttoolCallId?: string;\n}\n\nexport interface CompactableTranscriptEntry {\n\tentryId: string;\n\tentryType: SessionEntry[\"type\"];\n\trole: AgentMessage[\"role\"];\n\ttext: string;\n\ttokenEstimate: number;\n\tprotected: boolean;\n\tcontentBlocks: CompactableContentBlock[];\n\tmessage: AgentMessage;\n\ttoolCallIds: string[];\n\ttoolResultFor?: string;\n}\n\nexport interface CompactableTranscript {\n\tentries: CompactableTranscriptEntry[];\n\tprotectedEntryIds: string[];\n\ttokensBefore: number;\n\tsettings: CompactionSettings;\n}\n\nexport interface ContextCompactionPreparation {\n\ttranscript: CompactableTranscript;\n\tbranchEntries: SessionEntry[];\n\tmode?: ContextCompactionMode;\n}\n\nexport interface ValidatedContextDeletionResult {\n\tdeletedTargets: ContextDeletionTarget[];\n\tprotectedEntryIds: string[];\n\tstats: ContextCompactionStats;\n}\n\nexport interface ContextCompactionResult extends ValidatedContextDeletionResult {\n\tpromptVersion: typeof CONTEXT_COMPACTION_PROMPT_VERSION;\n\tbackupPath?: string;\n}\n\nconst CONTEXT_DELETE_TOOL_NAME = \"context_delete\";\nconst CONTEXT_GREP_DELETE_TOOL_NAME = \"context_grep_delete\";\nconst CONTEXT_SEARCH_TRANSCRIPT_TOOL_NAME = \"context_search_transcript\";\nconst CONTEXT_READ_ENTRY_TOOL_NAME = \"context_read_entry\";\nexport const CONTEXT_COMPACTION_MAX_TURNS = 50 as const;\nconst CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES = 50;\nconst CONTEXT_GREP_DELETE_MAX_REGEX_PATTERN_CHARS = 512;\nconst CONTEXT_GREP_DELETE_MAX_REGEX_SCAN_CHARS = 250_000;\nconst CONTEXT_MANIFEST_MAX_ENTRIES = 80;\nconst CONTEXT_MANIFEST_PREVIEW_CHARS = 240;\nconst CONTEXT_CRITICAL_OVERFLOW_RECENT_ENTRY_COUNT = 5;\nconst CONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS = 4000;\nconst CONTEXT_READ_ENTRY_MAX_CHARS = 12_000;\nconst CONTEXT_SEARCH_DEFAULT_MAX_MATCHES = 20;\nconst CONTEXT_SEARCH_MAX_MATCHES = 100;\nconst CONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS = 160;\nconst CONTEXT_SEARCH_MAX_CONTEXT_CHARS = 500;\n\nconst ContextDeleteToolParameters = Type.Object(\n\t{\n\t\tdeletions: Type.Array(\n\t\t\tType.Object(\n\t\t\t\t{\n\t\t\t\t\tkind: StringEnum([\"entry\", \"content_block\"] as const, {\n\t\t\t\t\t\tdescription: \"Delete an entire transcript entry or a single content block within one entry.\",\n\t\t\t\t\t}),\n\t\t\t\t\tentryId: Type.String({ minLength: 1, description: \"Stable transcript entry id to delete from.\" }),\n\t\t\t\t\tblockIndex: Type.Optional(\n\t\t\t\t\t\tType.Integer({\n\t\t\t\t\t\t\tminimum: 0,\n\t\t\t\t\t\t\tdescription: \"Required when kind is content_block; omit when kind is entry.\",\n\t\t\t\t\t\t}),\n\t\t\t\t\t),\n\t\t\t\t},\n\t\t\t\t{ additionalProperties: false },\n\t\t\t),\n\t\t\t{ description: \"Deletion targets only. Protected entries and recent active context must not be included.\" },\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst ContextGrepDeleteToolParameters = Type.Object(\n\t{\n\t\tpattern: Type.String({ minLength: 1, description: \"Literal text or regular expression to match in transcript text.\" }),\n\t\tregex: Type.Optional(Type.Boolean({ description: \"Treat pattern as a JavaScript regular expression. Defaults to false.\" })),\n\t\tcaseSensitive: Type.Optional(Type.Boolean({ description: \"Use case-sensitive matching. Defaults to false.\" })),\n\t\ttarget: Type.Optional(\n\t\t\tStringEnum([\"entry\", \"content_block\"] as const, {\n\t\t\t\tdescription: \"Delete whole matching entries or matching content blocks. Defaults to entry.\",\n\t\t\t}),\n\t\t),\n\t\tmaxMatches: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 1,\n\t\t\t\tmaximum: 200,\n\t\t\t\tdescription:\n\t\t\t\t\t\"Safety cap. If more unprotected, not-yet-deleted candidate targets are found, no deletions are applied. Defaults to 50.\",\n\t\t\t}),\n\t\t),\n\t\texpectedMatchCount: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 0,\n\t\t\t\tdescription: \"Optional safety check. If the match count differs, no deletions are applied.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst ContextSearchTranscriptToolParameters = Type.Object(\n\t{\n\t\tpattern: Type.String({ minLength: 1, description: \"Literal text or regular expression to search for.\" }),\n\t\tregex: Type.Optional(Type.Boolean({ description: \"Treat pattern as a JavaScript regular expression. Defaults to false.\" })),\n\t\tcaseSensitive: Type.Optional(Type.Boolean({ description: \"Use case-sensitive matching. Defaults to false.\" })),\n\t\ttarget: Type.Optional(\n\t\t\tStringEnum([\"entry\", \"content_block\"] as const, {\n\t\t\t\tdescription: \"Search whole entry text or individual content-block text. Defaults to entry.\",\n\t\t\t}),\n\t\t),\n\t\tmaxMatches: Type.Optional(\n\t\t\tType.Integer({ minimum: 1, maximum: CONTEXT_SEARCH_MAX_MATCHES, description: \"Maximum matches to return. Defaults to 20.\" }),\n\t\t),\n\t\tcontextChars: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 0,\n\t\t\t\tmaximum: CONTEXT_SEARCH_MAX_CONTEXT_CHARS,\n\t\t\t\tdescription: \"Characters of context to include before and after each match. Defaults to 160.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst ContextReadEntryToolParameters = Type.Object(\n\t{\n\t\tentryId: Type.String({ minLength: 1, description: \"Stable transcript entry id to read.\" }),\n\t\tblockIndex: Type.Optional(\n\t\t\tType.Integer({ minimum: 0, description: \"Optional content block index to read instead of the whole entry text.\" }),\n\t\t),\n\t\toffset: Type.Optional(Type.Integer({ minimum: 0, description: \"Character offset to begin reading. Defaults to 0.\" })),\n\t\tmaxChars: Type.Optional(\n\t\t\tType.Integer({\n\t\t\t\tminimum: 1,\n\t\t\t\tmaximum: CONTEXT_READ_ENTRY_MAX_CHARS,\n\t\t\t\tdescription: \"Maximum characters to return. Defaults to 4000; keep reads small to avoid overflowing context.\",\n\t\t\t}),\n\t\t),\n\t},\n\t{ additionalProperties: false },\n);\n\nconst CONTEXT_DELETE_TOOL = {\n\tname: CONTEXT_DELETE_TOOL_NAME,\n\tdescription: \"Record context compaction deletion targets directly against the transcript.\",\n\tparameters: ContextDeleteToolParameters,\n} as const;\n\nconst CONTEXT_GREP_DELETE_TOOL = {\n\tname: CONTEXT_GREP_DELETE_TOOL_NAME,\n\tdescription: \"Bulk-delete transcript entries or content blocks matching a guarded grep/regex query.\",\n\tparameters: ContextGrepDeleteToolParameters,\n} as const;\n\nconst CONTEXT_SEARCH_TRANSCRIPT_TOOL = {\n\tname: CONTEXT_SEARCH_TRANSCRIPT_TOOL_NAME,\n\tdescription: \"Search the full transcript working copy and return small snippets without mutating deletion state.\",\n\tparameters: ContextSearchTranscriptToolParameters,\n} as const;\n\nconst CONTEXT_READ_ENTRY_TOOL = {\n\tname: CONTEXT_READ_ENTRY_TOOL_NAME,\n\tdescription: \"Read a small slice of one transcript entry or content block from the full transcript working copy.\",\n\tparameters: ContextReadEntryToolParameters,\n} as const;\n\nexport interface ContextDeletionToolDetails {\n\tdeletions: ContextDeletionRequest[\"deletions\"];\n\tdeletedTargets: ContextDeletionTarget[];\n\tstats: ContextCompactionStats;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextGrepDeletionMatch {\n\tentryId: string;\n\ttarget: \"entry\" | \"content_block\";\n\tblockIndex?: number;\n\ttext: string;\n}\n\nexport interface ContextGrepDeletionSkipped {\n\tentryId?: string;\n\ttarget?: \"entry\" | \"content_block\";\n\tblockIndex?: number;\n\treason:\n\t\t| \"protected_entry\"\n\t\t| \"protected_block\"\n\t\t| \"already_deleted\"\n\t\t| \"max_matches_exceeded\"\n\t\t| \"expected_match_count_mismatch\";\n\ttext?: string;\n}\n\nexport interface ContextGrepDeletionToolDetails {\n\tpattern: string;\n\tregex: boolean;\n\tcaseSensitive: boolean;\n\ttarget: \"entry\" | \"content_block\";\n\tmatches: ContextGrepDeletionMatch[];\n\tskipped: ContextGrepDeletionSkipped[];\n\tdeletedTargets: ContextDeletionTarget[];\n\tstats: ContextCompactionStats;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextTranscriptSearchMatch {\n\tentryId: string;\n\ttarget: \"entry\" | \"content_block\";\n\tblockIndex?: number;\n\tmatchIndex: number;\n\tsnippet: string;\n\tprotected: boolean;\n}\n\nexport interface ContextTranscriptSearchToolDetails {\n\tpattern: string;\n\tregex: boolean;\n\tcaseSensitive: boolean;\n\ttarget: \"entry\" | \"content_block\";\n\tmatches: ContextTranscriptSearchMatch[];\n\ttruncated: boolean;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextReadEntryToolDetails {\n\tentryId: string;\n\tblockIndex?: number;\n\toffset: number;\n\tmaxChars: number;\n\ttotalChars: number;\n\ttext: string;\n\ttruncatedBefore: boolean;\n\ttruncatedAfter: boolean;\n\tcallCount: number;\n\terror?: string;\n}\n\nexport interface ContextDeletionToolController {\n\ttool: AgentTool<typeof ContextDeleteToolParameters, ContextDeletionToolDetails>;\n\tgrepTool: AgentTool<typeof ContextGrepDeleteToolParameters, ContextGrepDeletionToolDetails>;\n\tsearchTool: AgentTool<typeof ContextSearchTranscriptToolParameters, ContextTranscriptSearchToolDetails>;\n\treadEntryTool: AgentTool<typeof ContextReadEntryToolParameters, ContextReadEntryToolDetails>;\n\ttools: AgentTool[];\n\tgetDeletionRequest(): ContextDeletionRequest;\n\tgetValidatedResult(): ValidatedContextDeletionResult | undefined;\n\tgetLastError(): string | undefined;\n\tgetCallCount(): number;\n}\n\nexport interface ContextCompactionRunOptions {\n\tmode?: ContextCompactionMode;\n}\n\nconst CONTEXT_COMPACTION_SYSTEM_PROMPT = `You are a context compaction assistant.\n\nYour task is to read relevant parts of a conversation between a user and an AI assistant provided via a transcript file, then run a series of tools to apply deletion-only verbatim compaction using the exact context_delete or context_grep_delete format specified.`;\n\nconst CONTEXT_COMPACTION_FIXED_PROMPT = `Reference the provided transcript file transcript and use your search/read tools for small inspections, then use context_delete or context_grep_delete for deletions.\n\nYou MUST NOT summarize.\nYou MUST NOT paraphrase.\nYou MUST NOT generate replacement context.\nYou MUST NOT mutate retained transcript objects or content.\nDeletion tool calls are the compaction action; record only deletion targets by stable ID.\n\nWhat Gets Deleted:\n- Redundant tool outputs: file reads already acted on, grep/search results already processed, passing test output no longer needed.\n- Exploratory dead ends: irrelevant files read, unhelpful or empty searches.\n- Verbose boilerplate: license headers, import blocks the agent isn't modifying, configuration files read for reference.\n- Superseded information: earlier versions of files that have since been edited, old error messages from bugs already fixed.\n\nWhat Survives:\n- Active file paths and line numbers: Any reference the agent might need to navigate.\n- Current error messages: Unresolved bugss and their exact text.\n- Reasoning decisions: Why the agent chose approach A over B. An agent's chain of thought (why it chose this file, what pattern it noticed, what fix it decided on) carries more information-per-token than the raw grep output or file content that informed those decisions.\n- Recent tool calls and their results: The last 3-5 operations.\n- User instructions: The original task and any clarifications.\n\n<output_format>\nCall the context_delete tool one or more times with deletion targets in this shape:\n{ \"deletions\": [{ \"kind\": \"entry\", \"entryId\": \"...\" }] }\n\nFor content-block deletions, use:\n{ \"kind\": \"content_block\", \"entryId\": \"...\", \"blockIndex\": 0 }\n\nThe tool applies and validates deletion targets immediately. You can continue calling it for additional deletions if useful.\n\nFor guarded bulk deletion by text match, call context_grep_delete with a literal pattern or regex. It skips protected context, enforces maxMatches and expectedMatchCount, and validates through the same tool-call/tool-result safety rules.\n\nThe full transcript is available as a JSONL file path in the prompt, but do NOT try to load the whole file into context. Use context_search_transcript to find candidate entry IDs and context_read_entry to read only small slices (for example maxChars 1000-4000) before deleting.\n\nWhen you are done, reply with a brief plain-text completion message. Do not write deletion JSON or deletion target IDs outside tool calls.\n</output_format>`;\n\nfunction getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tif (entry.type === \"message\") {\n\t\treturn entry.message;\n\t}\n\tif (entry.type === \"custom_message\") {\n\t\treturn createCustomMessage(\n\t\t\tentry.customType,\n\t\t\tentry.content,\n\t\t\tentry.display,\n\t\t\tentry.details,\n\t\t\tentry.timestamp,\n\t\t\tentry.excludeFromContext,\n\t\t);\n\t}\n\tif (entry.type === \"branch_summary\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nfunction isExcludedFromLlmContext(message: AgentMessage): boolean {\n\tswitch (message.role) {\n\t\tcase \"bashExecution\":\n\t\t\treturn Boolean(message.excludeFromContext);\n\t\tcase \"custom\":\n\t\t\treturn (message as { excludeFromContext?: boolean }).excludeFromContext === true;\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction getContextEligibleMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {\n\tconst message = getMessageFromEntry(entry);\n\tif (!message || isExcludedFromLlmContext(message)) return undefined;\n\treturn message;\n}\n\nfunction textFromUnknownContent(content: unknown): string {\n\tif (typeof content === \"string\") return content;\n\tif (!Array.isArray(content)) return JSON.stringify(content);\n\treturn content.map((block) => textFromContentBlock(block)).join(\"\\n\");\n}\n\nfunction textFromContentBlock(block: unknown): string {\n\tif (!block || typeof block !== \"object\") return String(block);\n\tconst record = block as Record<string, unknown>;\n\tif (record.type === \"text\" && typeof record.text === \"string\") return record.text;\n\tif (record.type === \"thinking\" && typeof record.thinking === \"string\") return record.thinking;\n\tif (record.type === \"toolCall\") {\n\t\tconst name = typeof record.name === \"string\" ? record.name : \"tool\";\n\t\tconst id = typeof record.id === \"string\" ? record.id : \"unknown\";\n\t\tconst args = \"arguments\" in record ? JSON.stringify(record.arguments) : \"\";\n\t\treturn `toolCall ${id} ${name} ${args}`.trim();\n\t}\n\tif (record.type === \"image\") return \"[image]\";\n\treturn JSON.stringify(record);\n}\n\nconst IMAGE_BLOCK_CHAR_ESTIMATE = 4800;\nconst IMAGE_BLOCK_TOKEN_ESTIMATE = Math.ceil(IMAGE_BLOCK_CHAR_ESTIMATE / 4);\n\nfunction estimateTextTokens(text: string): number {\n\treturn Math.max(1, Math.ceil(text.length / 4));\n}\n\nfunction estimateContentBlockTokens(block: unknown, text: string): number {\n\tif (block && typeof block === \"object\" && (block as { type?: unknown }).type === \"image\") {\n\t\treturn IMAGE_BLOCK_TOKEN_ESTIMATE;\n\t}\n\treturn estimateTextTokens(text);\n}\n\nfunction getToolCallIdFromBlock(block: unknown): string | undefined {\n\tif (!block || typeof block !== \"object\") return undefined;\n\tconst record = block as Record<string, unknown>;\n\tif (record.type !== \"toolCall\") return undefined;\n\treturn typeof record.id === \"string\" ? record.id : undefined;\n}\n\nfunction getToolResultCallId(message: AgentMessage): string | undefined {\n\tif (message.role !== \"toolResult\") return undefined;\n\tconst callId = (message as { toolCallId?: unknown }).toolCallId;\n\treturn typeof callId === \"string\" ? callId : undefined;\n}\n\nfunction contentBlocksForEntry(\n\tentryId: string,\n\tmessage: AgentMessage,\n\tprotectedEntry: boolean,\n\texistingDeletedBlocks: ReadonlySet<number> | undefined,\n): CompactableContentBlock[] {\n\tconst content = (message as { content?: unknown }).content;\n\tif (!Array.isArray(content)) return [];\n\n\treturn content\n\t\t.map((block, blockIndex): CompactableContentBlock | undefined => {\n\t\t\tif (existingDeletedBlocks?.has(blockIndex)) return undefined;\n\t\t\tconst text = textFromContentBlock(block);\n\t\t\treturn {\n\t\t\t\tentryId,\n\t\t\t\tblockIndex,\n\t\t\t\ttype:\n\t\t\t\t\tblock && typeof block === \"object\" && typeof (block as { type?: unknown }).type === \"string\"\n\t\t\t\t\t\t? ((block as { type: string }).type)\n\t\t\t\t\t\t: \"unknown\",\n\t\t\t\ttext,\n\t\t\t\ttokenEstimate: estimateContentBlockTokens(block, text),\n\t\t\t\tprotected: protectedEntry,\n\t\t\t\ttoolCallId: getToolCallIdFromBlock(block),\n\t\t\t};\n\t\t})\n\t\t.filter((block): block is CompactableContentBlock => block !== undefined);\n}\n\nfunction messageText(message: AgentMessage): string {\n\tswitch (message.role) {\n\t\tcase \"bashExecution\":\n\t\t\treturn `Ran ${message.command}\\n${message.output}`;\n\t\tcase \"branchSummary\":\n\t\t\treturn message.summary;\n\t\tcase \"custom\":\n\t\tcase \"toolResult\":\n\t\tcase \"user\":\n\t\t\treturn textFromUnknownContent(message.content);\n\t\tcase \"assistant\":\n\t\t\treturn textFromUnknownContent(message.content);\n\t\tcase \"compactionSummary\":\n\t\t\t// Legacy summary-compaction message type retained in the upstream AgentMessage union\n\t\t\t// after summary compaction was removed; surface its archival summary text.\n\t\t\treturn message.summary;\n\t\tdefault: {\n\t\t\t// Exhaustiveness guard: adding a new AgentMessage role must fail the build here instead\n\t\t\t// of silently degrading to an empty string.\n\t\t\tconst _exhaustiveCheck: never = message;\n\t\t\tvoid _exhaustiveCheck;\n\t\t\treturn \"\";\n\t\t}\n\t}\n}\n\nfunction hasAssistantError(message: AgentMessage): boolean {\n\treturn message.role === \"assistant\" && (message as AssistantMessage).stopReason === \"error\";\n}\n\nfunction hasToolResultError(message: AgentMessage): boolean {\n\treturn message.role === \"toolResult\" && (message as { isError?: unknown }).isError === true;\n}\n\nfunction hasFailedBashExecution(message: AgentMessage): boolean {\n\treturn message.role === \"bashExecution\" && typeof message.exitCode === \"number\" && message.exitCode !== 0;\n}\n\nfunction isProtectedEntry(\n\tentry: SessionEntry,\n\tmessage: AgentMessage,\n\trecentEntryIds: ReadonlySet<string>,\n): boolean {\n\tif (recentEntryIds.has(entry.id)) return true;\n\tif (message.role === \"user\") return true;\n\tif (message.role === \"custom\") return true;\n\tif (message.role === \"branchSummary\") return true;\n\tif (hasAssistantError(message) || hasToolResultError(message)) return true;\n\tif (hasFailedBashExecution(message)) return true;\n\tif (entry.type === \"branch_summary\") return true;\n\treturn false;\n}\n\nexport function prepareContextCompaction(\n\tpathEntries: SessionEntry[],\n\tsettings: CompactionSettings,\n\toptions: ContextCompactionRunOptions = {},\n): ContextCompactionPreparation | undefined {\n\tif (pathEntries.length === 0) return undefined;\n\n\tconst deletionFilters = buildContextDeletionFilters(pathEntries);\n\tconst filteredPathEntries = buildContextDeletionFilteredPath(pathEntries, deletionFilters);\n\tconst rawEntryById = new Map(pathEntries.map((entry) => [entry.id, entry]));\n\tconst messageEntryIds = filteredPathEntries\n\t\t.filter((entry) => entry.type !== \"context_compaction\" && getContextEligibleMessageFromEntry(entry) !== undefined)\n\t\t.map((entry) => entry.id);\n\tconst recentEntryIds = new Set(messageEntryIds.slice(-CONTEXT_CRITICAL_OVERFLOW_RECENT_ENTRY_COUNT));\n\tconst protectedEntryIds = new Set<string>();\n\tconst entries: CompactableTranscriptEntry[] = [];\n\n\tfor (const entry of filteredPathEntries) {\n\t\tif (entry.type === \"context_compaction\") continue;\n\t\tconst message = getContextEligibleMessageFromEntry(entry);\n\t\tif (!message) continue;\n\t\tconst rawEntry = rawEntryById.get(entry.id) ?? entry;\n\t\tconst protectedEntry = isProtectedEntry(entry, message, recentEntryIds);\n\t\tif (protectedEntry) protectedEntryIds.add(entry.id);\n\t\tconst rawMessage = getContextEligibleMessageFromEntry(rawEntry) ?? message;\n\t\tconst contentBlocks = contentBlocksForEntry(\n\t\t\tentry.id,\n\t\t\trawMessage,\n\t\t\tprotectedEntry,\n\t\t\tdeletionFilters.deletedContentBlocks.get(entry.id),\n\t\t);\n\t\tconst toolCallIds = contentBlocks.map((block) => block.toolCallId).filter((id): id is string => id !== undefined);\n\t\tconst text = contentBlocks.length > 0 ? contentBlocks.map((block) => block.text).join(\"\\n\") : messageText(message);\n\t\tentries.push({\n\t\t\tentryId: entry.id,\n\t\t\tentryType: entry.type,\n\t\t\trole: message.role,\n\t\t\ttext,\n\t\t\ttokenEstimate: estimateTokens(message),\n\t\t\tprotected: protectedEntry,\n\t\t\tcontentBlocks,\n\t\t\tmessage,\n\t\t\ttoolCallIds,\n\t\t\ttoolResultFor: getToolResultCallId(message),\n\t\t});\n\t}\n\n\tif (entries.length < 2) return undefined;\n\n\treturn {\n\t\tbranchEntries: pathEntries,\n\t\tmode: options.mode ?? \"standard\",\n\t\ttranscript: {\n\t\t\tentries,\n\t\t\tprotectedEntryIds: [...protectedEntryIds],\n\t\t\ttokensBefore: entries.reduce((total, entry) => total + entry.tokenEstimate, 0),\n\t\t\tsettings,\n\t\t},\n\t};\n}\n\nfunction targetKey(target: ContextDeletionTarget): string {\n\treturn target.kind === \"entry\" ? `entry:${target.entryId}` : `content_block:${target.entryId}:${target.blockIndex}`;\n}\n\nfunction rawTargetKey(target: ContextDeletionRequest[\"deletions\"][number]): string {\n\treturn target.kind === \"entry\" ? `entry:${target.entryId}` : `content_block:${target.entryId}:${target.blockIndex}`;\n}\n\nfunction normalizeRawTarget(target: ContextDeletionRequest[\"deletions\"][number]): ContextDeletionTarget {\n\tif (target.kind === \"entry\") return { kind: \"entry\", entryId: target.entryId };\n\treturn { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex as number };\n}\n\nfunction rawDeletionFromTarget(target: ContextDeletionTarget): ContextDeletionRequest[\"deletions\"][number] {\n\tif (target.kind === \"entry\") return { kind: \"entry\", entryId: target.entryId };\n\treturn { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nfunction deletionRequestFromTargets(targets: readonly ContextDeletionTarget[]): ContextDeletionRequest {\n\treturn { deletions: targets.map(rawDeletionFromTarget) };\n}\n\nfunction getDeletedEntryIds(targets: readonly ContextDeletionTarget[]): Set<string> {\n\treturn new Set(targets.filter((target) => target.kind === \"entry\").map((target) => target.entryId));\n}\n\nfunction getDeletedContentBlocks(targets: readonly ContextDeletionTarget[]): Map<string, Set<number>> {\n\tconst blocksByEntry = new Map<string, Set<number>>();\n\tfor (const target of targets) {\n\t\tif (target.kind !== \"content_block\") continue;\n\t\tconst blocks = blocksByEntry.get(target.entryId) ?? new Set<number>();\n\t\tblocks.add(target.blockIndex);\n\t\tblocksByEntry.set(target.entryId, blocks);\n\t}\n\treturn blocksByEntry;\n}\n\nfunction isToolCallBlockDeleted(\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n\tdeletedEntryIds: ReadonlySet<string>,\n\tdeletedContentBlocks: ReadonlyMap<string, ReadonlySet<number>>,\n): boolean {\n\tif (deletedEntryIds.has(entry.entryId)) return true;\n\tconst deletedBlocks = deletedContentBlocks.get(entry.entryId);\n\tif (!deletedBlocks) return false;\n\treturn entry.contentBlocks.some((block) => block.toolCallId === callId && deletedBlocks.has(block.blockIndex));\n}\n\nfunction toolCallBlockIndexes(entry: CompactableTranscriptEntry, callId: string): number[] {\n\treturn entry.contentBlocks\n\t\t.filter((block) => block.toolCallId === callId)\n\t\t.map((block) => block.blockIndex);\n}\n\nfunction addTarget(targets: ContextDeletionTarget[], target: ContextDeletionTarget): boolean {\n\tif (targets.some((existing) => targetKey(existing) === targetKey(target))) return false;\n\ttargets.push(target);\n\treturn true;\n}\n\nfunction deleteEntryTarget(targets: ContextDeletionTarget[], entryId: string): boolean {\n\tlet changed = false;\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"content_block\" && target.entryId === entryId) {\n\t\t\ttargets.splice(index, 1);\n\t\t\tchanged = true;\n\t\t}\n\t}\n\treturn addTarget(targets, { kind: \"entry\", entryId }) || changed;\n}\n\nfunction removeEntryDeletion(targets: ContextDeletionTarget[], entryId: string): boolean {\n\tconst originalLength = targets.length;\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"entry\" && target.entryId === entryId) targets.splice(index, 1);\n\t}\n\treturn targets.length !== originalLength;\n}\n\nfunction mergeContextDeletionTargets(\n\tbaseTargets: readonly ContextDeletionTarget[],\n\tadditionalTargets: readonly ContextDeletionTarget[],\n): ContextDeletionTarget[] {\n\tconst targets = [...baseTargets];\n\tfor (const target of additionalTargets) {\n\t\tif (target.kind === \"entry\") {\n\t\t\tdeleteEntryTarget(targets, target.entryId);\n\t\t\tcontinue;\n\t\t}\n\t\tif (!getDeletedEntryIds(targets).has(target.entryId)) {\n\t\t\taddTarget(targets, target);\n\t\t}\n\t}\n\treturn targets;\n}\n\nfunction canonicalizeEntryTargets(targets: ContextDeletionTarget[], entry: CompactableTranscriptEntry): boolean {\n\tif (entry.protected || getDeletedEntryIds(targets).has(entry.entryId)) return false;\n\tconst deletedBlocks = getDeletedContentBlocks(targets).get(entry.entryId);\n\tif (!deletedBlocks || !entry.contentBlocks.every((block) => deletedBlocks.has(block.blockIndex))) return false;\n\t// Only repair/promote when dependency reconciliation reaches this entry. Non-tool entries that\n\t// request every block individually stay invalid so the assistant must choose explicit entry deletion.\n\treturn deleteEntryTarget(targets, entry.entryId);\n}\n\nfunction removeToolCallDeletion(\n\ttargets: ContextDeletionTarget[],\n\tentry: CompactableTranscriptEntry,\n\tcallId: string,\n): boolean {\n\tlet changed = removeEntryDeletion(targets, entry.entryId);\n\tconst blockIndexes = new Set(toolCallBlockIndexes(entry, callId));\n\tfor (let index = targets.length - 1; index >= 0; index--) {\n\t\tconst target = targets[index];\n\t\tif (target.kind === \"content_block\" && target.entryId === entry.entryId && blockIndexes.has(target.blockIndex)) {\n\t\t\ttargets.splice(index, 1);\n\t\t\tchanged = true;\n\t\t}\n\t}\n\treturn changed;\n}\n\nfunction addToolCallDeletion(targets: ContextDeletionTarget[], entry: CompactableTranscriptEntry, callId: string): boolean {\n\tif (entry.protected) return false;\n\tlet changed = false;\n\tfor (const blockIndex of toolCallBlockIndexes(entry, callId)) {\n\t\tif (!getDeletedEntryIds(targets).has(entry.entryId)) {\n\t\t\tchanged = addTarget(targets, { kind: \"content_block\", entryId: entry.entryId, blockIndex }) || changed;\n\t\t}\n\t}\n\treturn canonicalizeEntryTargets(targets, entry) || changed;\n}\n\nlet warnedReconciliationNonConvergence = false;\n\nfunction reconcileToolDependencies(\n\ttranscript: CompactableTranscript,\n\tinitialTargets: readonly ContextDeletionTarget[],\n): ContextDeletionTarget[] {\n\tconst targets = [...initialTargets];\n\tconst callEntries = new Map<string, CompactableTranscriptEntry>();\n\tconst entriesWithToolCalls = new Set<CompactableTranscriptEntry>();\n\tconst resultEntries = new Map<string, CompactableTranscriptEntry[]>();\n\n\tfor (const entry of transcript.entries) {\n\t\tfor (const callId of entry.toolCallIds) {\n\t\t\tcallEntries.set(callId, entry);\n\t\t\tentriesWithToolCalls.add(entry);\n\t\t}\n\t\tif (entry.toolResultFor) {\n\t\t\tconst results = resultEntries.get(entry.toolResultFor) ?? [];\n\t\t\tresults.push(entry);\n\t\t\tresultEntries.set(entry.toolResultFor, results);\n\t\t}\n\t}\n\n\t// Bounded fixpoint repair: each pass can add/remove paired call/result targets. In practice this\n\t// converges within one or two passes; the cap protects against accidental oscillation.\n\tlet changed = true;\n\tlet remainingPasses = Math.max(1, transcript.entries.length * 2);\n\twhile (changed && remainingPasses > 0) {\n\t\tchanged = false;\n\t\tremainingPasses -= 1;\n\t\tlet deletedEntryIds = getDeletedEntryIds(targets);\n\t\tlet deletedContentBlocks = getDeletedContentBlocks(targets);\n\t\tconst recordChange = (nextChanged: boolean): void => {\n\t\t\tif (!nextChanged) return;\n\t\t\tchanged = true;\n\t\t\tdeletedEntryIds = getDeletedEntryIds(targets);\n\t\t\tdeletedContentBlocks = getDeletedContentBlocks(targets);\n\t\t};\n\n\t\tfor (const [callId, callEntry] of callEntries) {\n\t\t\tconst callDeleted = isToolCallBlockDeleted(callEntry, callId, deletedEntryIds, deletedContentBlocks);\n\t\t\tconst results = resultEntries.get(callId) ?? [];\n\n\t\t\tif (callDeleted) {\n\t\t\t\tconst retainedProtectedResult = results.find((entry) => entry.protected && !deletedEntryIds.has(entry.entryId));\n\t\t\t\tif (retainedProtectedResult) {\n\t\t\t\t\trecordChange(removeToolCallDeletion(targets, callEntry, callId));\n\t\t\t\t} else {\n\t\t\t\t\tfor (const result of results) {\n\t\t\t\t\t\trecordChange(deleteEntryTarget(targets, result.entryId));\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (isToolCallBlockDeleted(callEntry, callId, deletedEntryIds, deletedContentBlocks)) continue;\n\n\t\t\tfor (const result of results) {\n\t\t\t\tif (!deletedEntryIds.has(result.entryId)) continue;\n\t\t\t\trecordChange(deleteEntryTarget(targets, result.entryId));\n\t\t\t\tif (callEntry.protected) {\n\t\t\t\t\trecordChange(removeEntryDeletion(targets, result.entryId));\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\trecordChange(addToolCallDeletion(targets, callEntry, callId));\n\t\t\t}\n\t\t}\n\n\t\tfor (const entry of entriesWithToolCalls) {\n\t\t\trecordChange(canonicalizeEntryTargets(targets, entry));\n\t\t}\n\t}\n\n\tif (changed && !warnedReconciliationNonConvergence) {\n\t\twarnedReconciliationNonConvergence = true;\n\t\tconsole.warn(\n\t\t\t`Context compaction tool dependency reconciliation did not converge within the bounded pass limit; validation will continue with the last reconciled target set. entries=${transcript.entries.length} callEntries=${callEntries.size} targets=${targets.length}`,\n\t\t);\n\t}\n\n\treturn targets;\n}\n\nfunction validateToolDependencies(transcript: CompactableTranscript, targets: readonly ContextDeletionTarget[]): void {\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tconst deletedContentBlocks = getDeletedContentBlocks(targets);\n\tconst callEntries = new Map<string, CompactableTranscriptEntry>();\n\tconst resultEntries = new Map<string, CompactableTranscriptEntry[]>();\n\n\tfor (const entry of transcript.entries) {\n\t\tfor (const callId of entry.toolCallIds) {\n\t\t\tcallEntries.set(callId, entry);\n\t\t}\n\t\tif (entry.toolResultFor) {\n\t\t\tconst results = resultEntries.get(entry.toolResultFor) ?? [];\n\t\t\tresults.push(entry);\n\t\t\tresultEntries.set(entry.toolResultFor, results);\n\t\t}\n\t}\n\n\tfor (const [callId, callEntry] of callEntries) {\n\t\tconst callDeleted = isToolCallBlockDeleted(callEntry, callId, deletedEntryIds, deletedContentBlocks);\n\t\tconst results = resultEntries.get(callId) ?? [];\n\t\tif (callDeleted) {\n\t\t\tconst danglingResult = results.find((entry) => !deletedEntryIds.has(entry.entryId));\n\t\t\tif (danglingResult) {\n\t\t\t\tthrow new Error(`Deleting tool call ${callId} would leave tool result entry ${danglingResult.entryId} orphaned`);\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst deletedResult = results.find((entry) => deletedEntryIds.has(entry.entryId));\n\t\tif (deletedResult) {\n\t\t\tthrow new Error(`Deleting tool result entry ${deletedResult.entryId} would leave tool call ${callId} dangling`);\n\t\t}\n\t}\n}\n\nfunction computeContextCompactionStats(\n\ttranscript: CompactableTranscript,\n\ttargets: readonly ContextDeletionTarget[],\n): ContextCompactionStats {\n\tconst entryById = new Map(transcript.entries.map((entry) => [entry.entryId, entry]));\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tlet deletedTokens = 0;\n\tlet objectsDeleted = 0;\n\n\tfor (const entryId of deletedEntryIds) {\n\t\tconst entry = entryById.get(entryId);\n\t\tif (!entry) continue;\n\t\tdeletedTokens += entry.tokenEstimate;\n\t\tobjectsDeleted += 1 + entry.contentBlocks.length;\n\t}\n\n\tfor (const target of targets) {\n\t\tif (target.kind !== \"content_block\" || deletedEntryIds.has(target.entryId)) continue;\n\t\tconst entry = entryById.get(target.entryId);\n\t\tif (!entry) continue;\n\t\tconst block = entry.contentBlocks.find((item) => item.blockIndex === target.blockIndex);\n\t\tif (!block) continue;\n\t\tdeletedTokens += block.tokenEstimate;\n\t\tobjectsDeleted += 1;\n\t}\n\n\tconst objectsBefore = transcript.entries.length + transcript.entries.reduce((total, entry) => total + entry.contentBlocks.length, 0);\n\tconst tokensBefore = transcript.tokensBefore;\n\tconst tokensAfter = Math.max(0, tokensBefore - deletedTokens);\n\tconst percentReduction = tokensBefore > 0 ? Math.round(((tokensBefore - tokensAfter) / tokensBefore) * 1000) / 10 : 0;\n\treturn {\n\t\tobjectsBefore,\n\t\tobjectsAfter: Math.max(0, objectsBefore - objectsDeleted),\n\t\tobjectsDeleted,\n\t\ttokensBefore,\n\t\ttokensAfter,\n\t\tpercentReduction,\n\t};\n}\n\ninterface ContextDeletionValidationOptions {\n\tmode?: ContextCompactionMode;\n}\n\n/**\n * An entry \"bears task context\" when it carries the user's intent for the session: a real `user`\n * message, an extension-injected `custom` message, or a branch summary (`branchSummary` role /\n * `branch_summary` entry type) that recaps an earlier branch's task.\n *\n * Verbatim compaction must always leave at least one task-bearing entry in context. The same set\n * also defines which protected entries `critical_overflow` may delete, because the intent each one\n * carries is recoverable from any other surviving task-bearing entry. As a deliberate consequence,\n * `critical_overflow` MAY delete every literal `user` message as long as a branch summary or custom\n * entry survives — branch summaries intentionally carry the task forward.\n */\nfunction isTaskBearingEntry(entry: CompactableTranscriptEntry): boolean {\n\treturn (\n\t\tentry.role === \"user\" ||\n\t\tentry.role === \"custom\" ||\n\t\tentry.role === \"branchSummary\" ||\n\t\tentry.entryType === \"branch_summary\"\n\t);\n}\n\nfunction isCriticalOverflowProtectedEntryDeletable(\n\tentry: CompactableTranscriptEntry,\n\ttranscript: CompactableTranscript,\n): boolean {\n\tif (!entry.protected) return true;\n\tconst entryIndex = transcript.entries.findIndex((candidate) => candidate.entryId === entry.entryId);\n\tif (entryIndex < 0) return false;\n\tconst recentBoundary = Math.max(0, transcript.entries.length - CONTEXT_CRITICAL_OVERFLOW_RECENT_ENTRY_COUNT);\n\tif (entryIndex >= recentBoundary) return false;\n\tif (hasAssistantError(entry.message) || hasToolResultError(entry.message) || hasFailedBashExecution(entry.message)) {\n\t\treturn false;\n\t}\n\treturn isTaskBearingEntry(entry);\n}\n\nfunction canDeleteProtectedTargetInMode(\n\ttranscript: CompactableTranscript,\n\ttarget: ContextDeletionTarget,\n\tmode: ContextCompactionMode,\n): boolean {\n\tif (mode !== \"critical_overflow\") return false;\n\tconst entry = transcript.entries.find((candidate) => candidate.entryId === target.entryId);\n\tif (!entry || !isCriticalOverflowProtectedEntryDeletable(entry, transcript)) return false;\n\tif (target.kind === \"entry\") return true;\n\tconst block = entry.contentBlocks.find((candidate) => candidate.blockIndex === target.blockIndex);\n\treturn block !== undefined;\n}\n\nexport function validateContextDeletionRequest(\n\trequest: ContextDeletionRequest,\n\ttranscript: CompactableTranscript,\n\toptions: ContextDeletionValidationOptions = {},\n): ValidatedContextDeletionResult {\n\tconst mode = options.mode ?? \"standard\";\n\tif (!request || typeof request !== \"object\" || !Array.isArray(request.deletions)) {\n\t\tthrow new Error(\"Context deletion request must be an object with a deletions array\");\n\t}\n\n\tconst entryById = new Map(transcript.entries.map((entry) => [entry.entryId, entry]));\n\tconst seen = new Set<string>();\n\tconst deletedTargets: ContextDeletionTarget[] = [];\n\n\tfor (const deletion of request.deletions) {\n\t\tif (!deletion || typeof deletion !== \"object\") {\n\t\t\tthrow new Error(\"Deletion target must be an object\");\n\t\t}\n\t\tif (deletion.kind !== \"entry\" && deletion.kind !== \"content_block\") {\n\t\t\tthrow new Error(`Unsupported deletion target kind: ${String((deletion as { kind?: unknown }).kind)}`);\n\t\t}\n\t\tif (typeof deletion.entryId !== \"string\" || deletion.entryId.length === 0) {\n\t\t\tthrow new Error(\"Deletion target entryId must be a non-empty string\");\n\t\t}\n\t\tconst entry = entryById.get(deletion.entryId);\n\t\tif (!entry) {\n\t\t\tthrow new Error(`Unknown deletion target entryId: ${deletion.entryId}`);\n\t\t}\n\t\tif (entry.protected && !canDeleteProtectedTargetInMode(transcript, normalizeRawTarget(deletion), mode)) {\n\t\t\tthrow new Error(`Deletion target ${deletion.entryId} is protected`);\n\t\t}\n\n\t\tif (deletion.kind === \"content_block\") {\n\t\t\tif (!Number.isInteger(deletion.blockIndex) || deletion.blockIndex === undefined || deletion.blockIndex < 0) {\n\t\t\t\tthrow new Error(`Invalid content block index for entry ${deletion.entryId}`);\n\t\t\t}\n\t\t\tconst block = entry.contentBlocks.find((item) => item.blockIndex === deletion.blockIndex);\n\t\t\tif (!block) {\n\t\t\t\tthrow new Error(`Unknown content block ${deletion.blockIndex} for entry ${deletion.entryId}`);\n\t\t\t}\n\t\t\tif (block.protected && !canDeleteProtectedTargetInMode(transcript, normalizeRawTarget(deletion), mode)) {\n\t\t\t\tthrow new Error(`Content block ${deletion.entryId}:${deletion.blockIndex} is protected`);\n\t\t\t}\n\t\t\tif (entry.contentBlocks.length <= 1) {\n\t\t\t\tthrow new Error(`Deleting the only content block of ${deletion.entryId} must be an entry deletion`);\n\t\t\t}\n\t\t}\n\n\t\tconst key = rawTargetKey(deletion);\n\t\tif (seen.has(key)) {\n\t\t\tthrow new Error(`Duplicate deletion target: ${key}`);\n\t\t}\n\t\tseen.add(key);\n\t\tconst normalized = normalizeRawTarget(deletion);\n\t\tdeletedTargets.push(normalized);\n\t}\n\n\tconst reconciledTargets = reconcileToolDependencies(transcript, deletedTargets);\n\tconst reconciledDeletedEntryIds = getDeletedEntryIds(reconciledTargets);\n\n\tfor (const target of reconciledTargets) {\n\t\tif (target.kind === \"content_block\" && reconciledDeletedEntryIds.has(target.entryId)) {\n\t\t\tthrow new Error(`Deletion target ${targetKey(target)} overlaps with entry deletion`);\n\t\t}\n\t}\n\n\tconst deletedContentBlocks = getDeletedContentBlocks(reconciledTargets);\n\tfor (const [entryId, blockIndexes] of deletedContentBlocks) {\n\t\tconst entry = entryById.get(entryId);\n\t\tif (entry?.contentBlocks.every((block) => blockIndexes.has(block.blockIndex))) {\n\t\t\tthrow new Error(`Content-block deletions for ${entryId} would remove every content block`);\n\t\t}\n\t}\n\n\tvalidateToolDependencies(transcript, reconciledTargets);\n\n\tconst remainingEntries = transcript.entries.filter((entry) => !reconciledDeletedEntryIds.has(entry.entryId));\n\tif (remainingEntries.length === 0) {\n\t\tthrow new Error(\"Deletion request would remove all context entries\");\n\t}\n\tconst hasTaskBearingContext = remainingEntries.some(isTaskBearingEntry);\n\tif (!hasTaskBearingContext) {\n\t\tthrow new Error(\"Deletion request would leave no user task in context\");\n\t}\n\n\treturn {\n\t\tdeletedTargets: reconciledTargets,\n\t\tprotectedEntryIds: [...transcript.protectedEntryIds],\n\t\tstats: computeContextCompactionStats(transcript, reconciledTargets),\n\t};\n}\n\nfunction stripJsonFence(text: string): string {\n\tconst trimmed = text.trim();\n\tif (!trimmed.startsWith(\"```\") || !trimmed.endsWith(\"```\")) return trimmed;\n\n\tconst firstLineEnd = trimmed.indexOf(\"\\n\");\n\tif (firstLineEnd < 0) return trimmed;\n\n\tconst fenceInfo = trimmed.slice(3, firstLineEnd).trim().toLowerCase();\n\tif (fenceInfo !== \"\" && fenceInfo !== \"json\") return trimmed;\n\n\treturn trimmed.slice(firstLineEnd + 1, -3).trim();\n}\n\nfunction contextDeletionRequestFromObject(value: unknown, source: string): ContextDeletionRequest {\n\tif (!value || typeof value !== \"object\" || !Array.isArray((value as { deletions?: unknown }).deletions)) {\n\t\tthrow new Error(`${source} must contain a deletions array`);\n\t}\n\treturn value as ContextDeletionRequest;\n}\n\nfunction escapeRegExpLiteral(text: string): string {\n\treturn text.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nfunction formatErrorMessage(error: unknown): string {\n\treturn error instanceof Error ? error.message : String(error);\n}\n\nfunction createContextDeletionToolResult<TDetails>(text: string, details: TDetails): AgentToolResult<TDetails> {\n\treturn { content: [{ type: \"text\", text }], details, terminate: false };\n}\n\nfunction assertSafeRegexPattern(pattern: string): void {\n\tif (pattern.length > CONTEXT_GREP_DELETE_MAX_REGEX_PATTERN_CHARS) {\n\t\tthrow new Error(\n\t\t\t`Regex pattern is too long (${pattern.length} characters); maximum is ${CONTEXT_GREP_DELETE_MAX_REGEX_PATTERN_CHARS}`,\n\t\t);\n\t}\n\n\t// Heuristic ReDoS guard for common catastrophic-backtracking shapes. JavaScript's RegExp engine\n\t// does not expose a timeout, so reject nested quantified groups and backreferences instead of\n\t// relying only on transcript scan-size caps.\n\tconst hasNestedQuantifiedGroup = /\\((?:[^()\\\\]|\\\\.)*[+*](?:[^()\\\\]|\\\\.)*\\)\\s*(?:[+*]|\\{\\d)/u.test(pattern);\n\tconst hasQuantifiedAlternation = /\\((?:[^()\\\\]|\\\\.)*\\|(?:[^()\\\\]|\\\\.)*\\)\\s*(?:[+*]|\\{\\d)/u.test(pattern);\n\tconst hasBackreference = /\\\\[1-9]/u.test(pattern);\n\tif (hasNestedQuantifiedGroup || hasQuantifiedAlternation || hasBackreference) {\n\t\tthrow new Error(\n\t\t\t\"Regex pattern is not allowed because it may cause excessive backtracking; use a literal pattern or exact deletion targets instead.\",\n\t\t);\n\t}\n}\n\nfunction createGrepMatcher(pattern: string, regex: boolean, caseSensitive: boolean): RegExp {\n\tif (regex) {\n\t\tassertSafeRegexPattern(pattern);\n\t}\n\n\ttry {\n\t\treturn new RegExp(regex ? pattern : escapeRegExpLiteral(pattern), caseSensitive ? \"u\" : \"iu\");\n\t} catch (error) {\n\t\tthrow new Error(`Invalid grep ${regex ? \"regex\" : \"pattern\"}: ${formatErrorMessage(error)}`);\n\t}\n}\n\nfunction assertSafeRegexScan(scanChars: number): void {\n\tif (scanChars <= CONTEXT_GREP_DELETE_MAX_REGEX_SCAN_CHARS) return;\n\tthrow new Error(\n\t\t`Regex grep would scan ${scanChars} characters; maximum is ${CONTEXT_GREP_DELETE_MAX_REGEX_SCAN_CHARS}. Use a literal pattern or exact deletion targets instead.`,\n\t);\n}\n\nfunction clampInteger(value: number | undefined, defaultValue: number, minimum: number, maximum: number): number {\n\tif (value === undefined) return defaultValue;\n\treturn Math.max(minimum, Math.min(maximum, value));\n}\n\nfunction textSlice(text: string, offset: number, maxChars: number): string {\n\treturn text.slice(offset, Math.min(text.length, offset + maxChars));\n}\n\nfunction findMatchIndex(matcher: RegExp, text: string): number {\n\tconst match = matcher.exec(text);\n\tmatcher.lastIndex = 0;\n\treturn match?.index ?? -1;\n}\n\nfunction snippetForMatch(text: string, matchIndex: number, contextChars: number): string {\n\tconst start = Math.max(0, matchIndex - contextChars);\n\tconst end = Math.min(text.length, matchIndex + contextChars);\n\tconst prefix = start > 0 ? \"…\" : \"\";\n\tconst suffix = end < text.length ? \"…\" : \"\";\n\treturn `${prefix}${text.slice(start, end)}${suffix}`;\n}\n\nfunction currentTargetDeleted(targets: readonly ContextDeletionTarget[], target: ContextDeletionTarget): boolean {\n\tconst deletedEntryIds = getDeletedEntryIds(targets);\n\tif (deletedEntryIds.has(target.entryId)) return true;\n\tif (target.kind === \"entry\") return false;\n\treturn getDeletedContentBlocks(targets).get(target.entryId)?.has(target.blockIndex) === true;\n}\n\nfunction addGrepCandidate(\n\tcandidates: ContextDeletionTarget[],\n\tmatches: ContextGrepDeletionMatch[],\n\tseenTargets: Set<string>,\n\tcandidate: ContextDeletionTarget,\n\tmatch: ContextGrepDeletionMatch,\n): void {\n\tconst key = targetKey(candidate);\n\tif (seenTargets.has(key)) return;\n\tseenTargets.add(key);\n\tcandidates.push(candidate);\n\tmatches.push(match);\n}\n\ninterface EntryTextRow {\n\tentry_id: string;\n\ttext: string;\n\tis_protected: number;\n}\n\ninterface EntryReadRow extends EntryTextRow {\n\trole: string;\n\ttoken_estimate: number;\n}\n\ninterface ContentBlockTextRow {\n\tentry_id: string;\n\tblock_index: number;\n\ttext: string;\n\tentry_protected: number;\n\tblock_protected: number;\n\tblock_count: number;\n}\n\ninterface ContentBlockReadRow extends ContentBlockTextRow {\n\ttype: string;\n\ttoken_estimate: number;\n}\n\ninterface StoredTranscriptEntry {\n\tentryId: string;\n\trole: AgentMessage[\"role\"];\n\tprotected: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\ninterface StoredContentBlock {\n\tentryPosition: number;\n\tentryId: string;\n\tblockIndex: number;\n\ttype: string;\n\tprotected: boolean;\n\ttokenEstimate: number;\n\ttext: string;\n}\n\ninterface ContextDeletionMemorySnapshot {\n\tdeletionTargets: ContextDeletionTarget[];\n\tcallCount: number;\n\tlastError?: string;\n}\n\nfunction copyDeletionTarget(target: ContextDeletionTarget): ContextDeletionTarget {\n\treturn target.kind === \"entry\"\n\t\t? { kind: \"entry\", entryId: target.entryId }\n\t\t: { kind: \"content_block\", entryId: target.entryId, blockIndex: target.blockIndex };\n}\n\nclass ContextDeletionMemoryStore {\n\tprivate readonly entries: StoredTranscriptEntry[];\n\tprivate readonly entriesById: Map<string, StoredTranscriptEntry>;\n\tprivate readonly contentBlocks: StoredContentBlock[];\n\tprivate readonly contentBlockCountByEntryId: Map<string, number>;\n\tprivate deletionTargets: ContextDeletionTarget[] = [];\n\tprivate callCount = 0;\n\tprivate lastError: string | undefined;\n\n\tconstructor(transcript: CompactableTranscript) {\n\t\tconst entryIds = new Set<string>();\n\t\tconst blockKeys = new Set<string>();\n\t\tthis.entries = transcript.entries.map((entry) => {\n\t\t\tif (entryIds.has(entry.entryId)) {\n\t\t\t\tthrow new Error(`Duplicate transcript entry id: ${entry.entryId}`);\n\t\t\t}\n\t\t\tentryIds.add(entry.entryId);\n\t\t\treturn {\n\t\t\t\tentryId: entry.entryId,\n\t\t\t\trole: entry.role,\n\t\t\t\tprotected: entry.protected,\n\t\t\t\ttokenEstimate: entry.tokenEstimate,\n\t\t\t\ttext: entry.text,\n\t\t\t};\n\t\t});\n\t\tthis.entriesById = new Map<string, StoredTranscriptEntry>(this.entries.map((entry) => [entry.entryId, entry] as const));\n\t\tthis.contentBlocks = transcript.entries.flatMap((entry, entryPosition) =>\n\t\t\tentry.contentBlocks.map((block) => {\n\t\t\t\tif (block.entryId !== entry.entryId) {\n\t\t\t\t\tthrow new Error(`Transcript content block ${block.entryId}:${block.blockIndex} does not belong to entry ${entry.entryId}`);\n\t\t\t\t}\n\t\t\t\tconst blockKey = `${block.entryId}:${block.blockIndex}`;\n\t\t\t\tif (blockKeys.has(blockKey)) {\n\t\t\t\t\tthrow new Error(`Duplicate transcript content block: ${blockKey}`);\n\t\t\t\t}\n\t\t\t\tblockKeys.add(blockKey);\n\t\t\t\treturn {\n\t\t\t\t\tentryPosition,\n\t\t\t\t\tentryId: block.entryId,\n\t\t\t\t\tblockIndex: block.blockIndex,\n\t\t\t\t\ttype: block.type,\n\t\t\t\t\tprotected: block.protected,\n\t\t\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\t\t\ttext: block.text,\n\t\t\t\t};\n\t\t\t}),\n\t\t);\n\t\tthis.contentBlockCountByEntryId = new Map();\n\t\tfor (const block of this.contentBlocks) {\n\t\t\tthis.contentBlockCountByEntryId.set(block.entryId, (this.contentBlockCountByEntryId.get(block.entryId) ?? 0) + 1);\n\t\t}\n\t}\n\n\ttransaction<T>(operation: () => T): T {\n\t\tconst snapshot = this.snapshot();\n\t\ttry {\n\t\t\treturn operation();\n\t\t} catch (error) {\n\t\t\tthis.restore(snapshot);\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treadTargets(): ContextDeletionTarget[] {\n\t\treturn this.deletionTargets.map(copyDeletionTarget);\n\t}\n\n\treplaceTargets(targets: readonly ContextDeletionTarget[]): void {\n\t\tthis.deletionTargets = targets.map(copyDeletionTarget);\n\t}\n\n\tlistEntriesForGrep(): EntryTextRow[] {\n\t\treturn this.entries.map((entry) => ({\n\t\t\tentry_id: entry.entryId,\n\t\t\ttext: entry.text,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t}));\n\t}\n\n\tlistContentBlocksForGrep(): ContentBlockTextRow[] {\n\t\treturn [...this.contentBlocks]\n\t\t\t.sort((a, b) => a.entryPosition - b.entryPosition || a.blockIndex - b.blockIndex)\n\t\t\t.map((block) => ({\n\t\t\t\tentry_id: block.entryId,\n\t\t\t\tblock_index: block.blockIndex,\n\t\t\t\ttext: block.text,\n\t\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t\t}));\n\t}\n\n\tgetEntryForRead(entryId: string): EntryReadRow | undefined {\n\t\tconst entry = this.entriesById.get(entryId);\n\t\tif (!entry) return undefined;\n\t\treturn {\n\t\t\tentry_id: entry.entryId,\n\t\t\trole: entry.role,\n\t\t\tis_protected: entry.protected ? 1 : 0,\n\t\t\ttoken_estimate: entry.tokenEstimate,\n\t\t\ttext: entry.text,\n\t\t};\n\t}\n\n\tgetContentBlockForRead(entryId: string, blockIndex: number): ContentBlockReadRow | undefined {\n\t\tconst block = this.contentBlocks.find((candidate) => candidate.entryId === entryId && candidate.blockIndex === blockIndex);\n\t\tif (!block) return undefined;\n\t\treturn {\n\t\t\tentry_id: block.entryId,\n\t\t\tblock_index: block.blockIndex,\n\t\t\ttype: block.type,\n\t\t\ttoken_estimate: block.tokenEstimate,\n\t\t\ttext: block.text,\n\t\t\tentry_protected: this.entriesById.get(block.entryId)?.protected ? 1 : 0,\n\t\t\tblock_protected: block.protected ? 1 : 0,\n\t\t\tblock_count: this.contentBlockCountByEntryId.get(block.entryId) ?? 0,\n\t\t};\n\t}\n\n\tgetGrepScanTextLength(target: \"entry\" | \"content_block\"): number {\n\t\tconst texts = target === \"entry\" ? this.entries : this.contentBlocks;\n\t\treturn texts.reduce((sum, item) => sum + item.text.length, 0);\n\t}\n\n\tincrementCallCount(): number {\n\t\tthis.callCount += 1;\n\t\treturn this.callCount;\n\t}\n\n\tgetCallCount(): number {\n\t\treturn this.callCount;\n\t}\n\n\tsetLastError(message: string): void {\n\t\tthis.lastError = message;\n\t}\n\n\tclearLastError(): void {\n\t\tthis.lastError = undefined;\n\t}\n\n\tgetLastError(): string | undefined {\n\t\treturn this.lastError;\n\t}\n\n\tprivate snapshot(): ContextDeletionMemorySnapshot {\n\t\treturn {\n\t\t\tdeletionTargets: this.readTargets(),\n\t\t\tcallCount: this.callCount,\n\t\t\t...(this.lastError === undefined ? {} : { lastError: this.lastError }),\n\t\t};\n\t}\n\n\tprivate restore(snapshot: ContextDeletionMemorySnapshot): void {\n\t\tthis.deletionTargets = snapshot.deletionTargets.map(copyDeletionTarget);\n\t\tthis.callCount = snapshot.callCount;\n\t\tthis.lastError = snapshot.lastError;\n\t}\n}\n\nfunction createContextDeletionStore(transcript: CompactableTranscript): ContextDeletionMemoryStore {\n\treturn new ContextDeletionMemoryStore(transcript);\n}\n\nexport function createContextDeletionTool(\n\ttranscript: CompactableTranscript,\n\toptions: ContextCompactionRunOptions = {},\n): ContextDeletionToolController {\n\tconst mode = options.mode ?? \"standard\";\n\tconst store = createContextDeletionStore(transcript);\n\tlet validatedResult: ValidatedContextDeletionResult | undefined;\n\n\tfunction readTargets(): ContextDeletionTarget[] {\n\t\treturn store.readTargets();\n\t}\n\n\tfunction applyValidatedTargets(additionalTargets: readonly ContextDeletionTarget[]): ValidatedContextDeletionResult {\n\t\tconst mergedTargets = mergeContextDeletionTargets(readTargets(), additionalTargets);\n\t\tvalidatedResult = validateContextDeletionRequest(deletionRequestFromTargets(mergedTargets), transcript, { mode });\n\t\tstore.replaceTargets(validatedResult.deletedTargets);\n\t\treturn validatedResult;\n\t}\n\n\tfunction currentStats(): ContextCompactionStats {\n\t\treturn validatedResult?.stats ?? computeContextCompactionStats(transcript, readTargets());\n\t}\n\n\tfunction canDeleteProtectedTarget(target: ContextDeletionTarget): boolean {\n\t\treturn canDeleteProtectedTargetInMode(transcript, target, mode);\n\t}\n\n\tconst tool: AgentTool<typeof ContextDeleteToolParameters, ContextDeletionToolDetails> = {\n\t\t...CONTEXT_DELETE_TOOL,\n\t\tlabel: \"context deletion request\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\ttry {\n\t\t\t\t\tconst incomingRequest = contextDeletionRequestFromObject(params, `${CONTEXT_DELETE_TOOL_NAME} arguments`);\n\t\t\t\t\tconst incomingValidated = validateContextDeletionRequest(incomingRequest, transcript, { mode });\n\t\t\t\t\tconst applied = applyValidatedTargets(incomingValidated.deletedTargets);\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst deletedTargets = readTargets();\n\n\t\t\t\t\tconst details: ContextDeletionToolDetails = {\n\t\t\t\t\t\tdeletions: deletionRequestFromTargets(deletedTargets).deletions,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: applied.stats,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Recorded ${incomingValidated.deletedTargets.length} deletion target(s); ${deletedTargets.length} total validated deletion target(s) are selected. Continue calling ${CONTEXT_DELETE_TOOL_NAME} or ${CONTEXT_GREP_DELETE_TOOL_NAME} for additional deletions, or respond done when finished.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst deletedTargets = readTargets();\n\t\t\t\t\tconst details: ContextDeletionToolDetails = {\n\t\t\t\t\t\tdeletions: deletionRequestFromTargets(deletedTargets).deletions,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error recording context deletion targets: ${message}. No new deletion targets were applied; continue with a corrected tool call.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst grepTool: AgentTool<typeof ContextGrepDeleteToolParameters, ContextGrepDeletionToolDetails> = {\n\t\t...CONTEXT_GREP_DELETE_TOOL,\n\t\tlabel: \"context grep delete\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst pattern = params.pattern;\n\t\t\t\tconst regex = params.regex === true;\n\t\t\t\tconst caseSensitive = params.caseSensitive === true;\n\t\t\t\tconst target = params.target ?? \"entry\";\n\t\t\t\tconst maxMatches = params.maxMatches ?? CONTEXT_GREP_DELETE_DEFAULT_MAX_MATCHES;\n\t\t\t\tconst candidates: ContextDeletionTarget[] = [];\n\t\t\t\tconst matches: ContextGrepDeletionMatch[] = [];\n\t\t\t\tconst skipped: ContextGrepDeletionSkipped[] = [];\n\t\t\t\tconst seenTargets = new Set<string>();\n\n\t\t\t\ttry {\n\t\t\t\t\tif (regex) {\n\t\t\t\t\t\tassertSafeRegexScan(store.getGrepScanTextLength(target));\n\t\t\t\t\t}\n\t\t\t\t\tconst matcher = createGrepMatcher(pattern, regex, caseSensitive);\n\t\t\t\t\tconst currentTargets = readTargets();\n\n\t\t\t\t\tif (target === \"entry\") {\n\t\t\t\t\t\tfor (const entry of store.listEntriesForGrep()) {\n\t\t\t\t\t\t\tif (!matcher.test(entry.text)) continue;\n\t\t\t\t\t\t\tconst candidate: ContextDeletionTarget = { kind: \"entry\", entryId: entry.entry_id };\n\t\t\t\t\t\t\tif (entry.is_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"protected_entry\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentTargetDeleted(currentTargets, candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({ entryId: entry.entry_id, target, reason: \"already_deleted\", text: entry.text });\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taddGrepCandidate(candidates, matches, seenTargets, candidate, {\n\t\t\t\t\t\t\t\tentryId: entry.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\ttext: entry.text,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfor (const block of store.listContentBlocksForGrep()) {\n\t\t\t\t\t\t\tif (!matcher.test(block.text)) continue;\n\t\t\t\t\t\t\tconst candidate: ContextDeletionTarget =\n\t\t\t\t\t\t\t\tblock.block_count <= 1\n\t\t\t\t\t\t\t\t\t? { kind: \"entry\", entryId: block.entry_id }\n\t\t\t\t\t\t\t\t\t: { kind: \"content_block\", entryId: block.entry_id, blockIndex: block.block_index };\n\t\t\t\t\t\t\tif (block.entry_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\t\treason: \"protected_entry\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (block.block_protected === 1 && !canDeleteProtectedTarget(candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\t\treason: \"protected_block\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentTargetDeleted(currentTargets, candidate)) {\n\t\t\t\t\t\t\t\tskipped.push({\n\t\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\t\treason: \"already_deleted\",\n\t\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\tcontinue;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\taddGrepCandidate(candidates, matches, seenTargets, candidate, {\n\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\ttarget: candidate.kind,\n\t\t\t\t\t\t\t\t...(candidate.kind === \"content_block\" ? { blockIndex: candidate.blockIndex } : {}),\n\t\t\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet applied: ValidatedContextDeletionResult | undefined;\n\t\t\t\t\tif (params.expectedMatchCount !== undefined && candidates.length !== params.expectedMatchCount) {\n\t\t\t\t\t\tskipped.push({ reason: \"expected_match_count_mismatch\" });\n\t\t\t\t\t} else if (candidates.length > maxMatches) {\n\t\t\t\t\t\tskipped.push({ reason: \"max_matches_exceeded\" });\n\t\t\t\t\t} else if (candidates.length > 0) {\n\t\t\t\t\t\tapplied = applyValidatedTargets(candidates);\n\t\t\t\t\t}\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst deletedTargets = readTargets();\n\n\t\t\t\t\tconst details: ContextGrepDeletionToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\tskipped,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: applied?.stats ?? currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Matched ${matches.length} deletion target(s), skipped ${skipped.length}, and ${applied ? \"applied\" : \"did not apply\"} grep deletion for pattern ${JSON.stringify(pattern)}. Total validated deletion target(s): ${deletedTargets.length}.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst deletedTargets = readTargets();\n\t\t\t\t\tconst details: ContextGrepDeletionToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\tskipped,\n\t\t\t\t\t\tdeletedTargets,\n\t\t\t\t\t\tstats: currentStats(),\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error applying grep deletion for pattern ${JSON.stringify(pattern)}: ${message}. No new deletion targets were applied; continue with a corrected tool call.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst searchTool: AgentTool<typeof ContextSearchTranscriptToolParameters, ContextTranscriptSearchToolDetails> = {\n\t\t...CONTEXT_SEARCH_TRANSCRIPT_TOOL,\n\t\tlabel: \"context transcript search\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst pattern = params.pattern;\n\t\t\t\tconst regex = params.regex === true;\n\t\t\t\tconst caseSensitive = params.caseSensitive === true;\n\t\t\t\tconst target = params.target ?? \"entry\";\n\t\t\t\tconst maxMatches = clampInteger(params.maxMatches, CONTEXT_SEARCH_DEFAULT_MAX_MATCHES, 1, CONTEXT_SEARCH_MAX_MATCHES);\n\t\t\t\tconst contextChars = clampInteger(\n\t\t\t\t\tparams.contextChars,\n\t\t\t\t\tCONTEXT_SEARCH_DEFAULT_CONTEXT_CHARS,\n\t\t\t\t\t0,\n\t\t\t\t\tCONTEXT_SEARCH_MAX_CONTEXT_CHARS,\n\t\t\t\t);\n\t\t\t\tconst matches: ContextTranscriptSearchMatch[] = [];\n\t\t\t\tlet truncated = false;\n\n\t\t\t\ttry {\n\t\t\t\t\tif (regex) {\n\t\t\t\t\t\tassertSafeRegexScan(store.getGrepScanTextLength(target));\n\t\t\t\t\t}\n\t\t\t\t\tconst matcher = createGrepMatcher(pattern, regex, caseSensitive);\n\t\t\t\t\tif (target === \"entry\") {\n\t\t\t\t\t\tfor (const entry of store.listEntriesForGrep()) {\n\t\t\t\t\t\t\tconst matchIndex = findMatchIndex(matcher, entry.text);\n\t\t\t\t\t\t\tif (matchIndex < 0) continue;\n\t\t\t\t\t\t\tif (matches.length >= maxMatches) {\n\t\t\t\t\t\t\t\ttruncated = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmatches.push({\n\t\t\t\t\t\t\t\tentryId: entry.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\tmatchIndex,\n\t\t\t\t\t\t\t\tsnippet: snippetForMatch(entry.text, matchIndex, contextChars),\n\t\t\t\t\t\t\t\tprotected: entry.is_protected === 1,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tfor (const block of store.listContentBlocksForGrep()) {\n\t\t\t\t\t\t\tconst matchIndex = findMatchIndex(matcher, block.text);\n\t\t\t\t\t\t\tif (matchIndex < 0) continue;\n\t\t\t\t\t\t\tif (matches.length >= maxMatches) {\n\t\t\t\t\t\t\t\ttruncated = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tmatches.push({\n\t\t\t\t\t\t\t\tentryId: block.entry_id,\n\t\t\t\t\t\t\t\ttarget,\n\t\t\t\t\t\t\t\tblockIndex: block.block_index,\n\t\t\t\t\t\t\t\tmatchIndex,\n\t\t\t\t\t\t\t\tsnippet: snippetForMatch(block.text, matchIndex, contextChars),\n\t\t\t\t\t\t\t\tprotected: block.entry_protected === 1 || block.block_protected === 1,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst details: ContextTranscriptSearchToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\ttruncated,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst text = `Found ${matches.length}${truncated ? \"+\" : \"\"} ${target} match(es) for ${JSON.stringify(pattern)}. Use ${CONTEXT_READ_ENTRY_TOOL_NAME} with small maxChars to inspect exact content before deleting.`;\n\t\t\t\t\treturn createContextDeletionToolResult(text, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst details: ContextTranscriptSearchToolDetails = {\n\t\t\t\t\t\tpattern,\n\t\t\t\t\t\tregex,\n\t\t\t\t\t\tcaseSensitive,\n\t\t\t\t\t\ttarget,\n\t\t\t\t\t\tmatches,\n\t\t\t\t\t\ttruncated,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(\n\t\t\t\t\t\t`Error searching transcript for ${JSON.stringify(pattern)}: ${message}. Try a literal pattern or narrower query.`,\n\t\t\t\t\t\tdetails,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\tconst readEntryTool: AgentTool<typeof ContextReadEntryToolParameters, ContextReadEntryToolDetails> = {\n\t\t...CONTEXT_READ_ENTRY_TOOL,\n\t\tlabel: \"context read entry\",\n\t\texecutionMode: \"parallel\",\n\t\tasync execute(_toolCallId, params) {\n\t\t\treturn store.transaction(() => {\n\t\t\t\tconst callCount = store.incrementCallCount();\n\t\t\t\tconst offset = clampInteger(params.offset, 0, 0, Number.MAX_SAFE_INTEGER);\n\t\t\t\tconst maxChars = clampInteger(\n\t\t\t\t\tparams.maxChars,\n\t\t\t\t\tCONTEXT_READ_ENTRY_DEFAULT_MAX_CHARS,\n\t\t\t\t\t1,\n\t\t\t\t\tCONTEXT_READ_ENTRY_MAX_CHARS,\n\t\t\t\t);\n\t\t\t\ttry {\n\t\t\t\t\tconst row =\n\t\t\t\t\t\tparams.blockIndex === undefined\n\t\t\t\t\t\t\t? store.getEntryForRead(params.entryId)\n\t\t\t\t\t\t\t: store.getContentBlockForRead(params.entryId, params.blockIndex);\n\t\t\t\t\tif (!row) {\n\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\tparams.blockIndex === undefined\n\t\t\t\t\t\t\t\t? `Unknown transcript entry: ${params.entryId}`\n\t\t\t\t\t\t\t\t: `Unknown transcript content block: ${params.entryId}:${params.blockIndex}`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tconst text = row.text;\n\t\t\t\t\tconst slice = textSlice(text, offset, maxChars);\n\t\t\t\t\tstore.clearLastError();\n\t\t\t\t\tconst details: ContextReadEntryToolDetails = {\n\t\t\t\t\t\tentryId: params.entryId,\n\t\t\t\t\t\t...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),\n\t\t\t\t\t\toffset,\n\t\t\t\t\t\tmaxChars,\n\t\t\t\t\t\ttotalChars: text.length,\n\t\t\t\t\t\ttext: slice,\n\t\t\t\t\t\ttruncatedBefore: offset > 0,\n\t\t\t\t\t\ttruncatedAfter: offset + maxChars < text.length,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t};\n\t\t\t\t\tconst textResult = `Read ${slice.length} of ${text.length} characters from ${params.blockIndex === undefined ? params.entryId : `${params.entryId}:${params.blockIndex}`}. Keep reads small; increase offset for the next slice if needed.`;\n\t\t\t\t\treturn createContextDeletionToolResult(textResult, details);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconst message = formatErrorMessage(error);\n\t\t\t\t\tstore.setLastError(message);\n\t\t\t\t\tconst details: ContextReadEntryToolDetails = {\n\t\t\t\t\t\tentryId: params.entryId,\n\t\t\t\t\t\t...(params.blockIndex === undefined ? {} : { blockIndex: params.blockIndex }),\n\t\t\t\t\t\toffset,\n\t\t\t\t\t\tmaxChars,\n\t\t\t\t\t\ttotalChars: 0,\n\t\t\t\t\t\ttext: \"\",\n\t\t\t\t\t\ttruncatedBefore: false,\n\t\t\t\t\t\ttruncatedAfter: false,\n\t\t\t\t\t\tcallCount,\n\t\t\t\t\t\terror: message,\n\t\t\t\t\t};\n\t\t\t\t\treturn createContextDeletionToolResult(`Error reading transcript entry: ${message}`, details);\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\t};\n\n\treturn {\n\t\ttool,\n\t\tgrepTool,\n\t\tsearchTool,\n\t\treadEntryTool,\n\t\ttools: [tool, grepTool, searchTool, readEntryTool],\n\t\tgetDeletionRequest: () => deletionRequestFromTargets(readTargets()),\n\t\tgetValidatedResult: () => validatedResult,\n\t\tgetLastError: () => store.getLastError(),\n\t\tgetCallCount: () => store.getCallCount(),\n\t};\n}\n\nexport function parseContextDeletionRequest(text: string): ContextDeletionRequest {\n\tconst stripped = stripJsonFence(text);\n\tlet parsed: unknown;\n\ttry {\n\t\tparsed = JSON.parse(stripped);\n\t} catch (error) {\n\t\tthrow new Error(`Failed to parse context deletion request JSON: ${error instanceof Error ? error.message : String(error)}`);\n\t}\n\treturn contextDeletionRequestFromObject(parsed, \"Context deletion request JSON\");\n}\n\nfunction isContextDeleteToolCall(content: AssistantMessage[\"content\"][number]): content is ToolCall {\n\treturn content.type === \"toolCall\" && content.name === CONTEXT_DELETE_TOOL_NAME;\n}\n\nfunction textContentFromResponse(response: AssistantMessage): string {\n\treturn response.content\n\t\t.filter((content): content is { type: \"text\"; text: string } => content.type === \"text\")\n\t\t.map((content) => content.text)\n\t\t.join(\"\\n\");\n}\n\nexport function parseContextDeletionResponse(response: AssistantMessage): ContextDeletionRequest {\n\tconst toolCalls = response.content.filter(isContextDeleteToolCall);\n\tif (toolCalls.length > 1) {\n\t\tthrow new Error(`Context compaction assistant called ${CONTEXT_DELETE_TOOL_NAME} more than once`);\n\t}\n\tconst toolCall = toolCalls[0];\n\tif (toolCall) {\n\t\treturn contextDeletionRequestFromObject(toolCall.arguments, `${CONTEXT_DELETE_TOOL_NAME} arguments`);\n\t}\n\n\tconst textContent = textContentFromResponse(response);\n\tif (textContent.trim().length === 0) {\n\t\tthrow new Error(`Context compaction assistant did not call ${CONTEXT_DELETE_TOOL_NAME}`);\n\t}\n\treturn parseContextDeletionRequest(textContent);\n}\n\nfunction truncateForPrompt(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, maxChars)}\\n[... ${text.length - maxChars} more characters omitted from context compaction prompt]`;\n}\n\nfunction transcriptEntryFilePayload(entry: CompactableTranscriptEntry): unknown {\n\treturn {\n\t\tentryId: entry.entryId,\n\t\tentryType: entry.entryType,\n\t\trole: entry.role,\n\t\tprotected: entry.protected,\n\t\ttokenEstimate: entry.tokenEstimate,\n\t\ttoolCallIds: entry.toolCallIds,\n\t\ttoolResultFor: entry.toolResultFor,\n\t\ttext: entry.text,\n\t\tcontentBlocks: entry.contentBlocks.map((block) => ({\n\t\t\tblockIndex: block.blockIndex,\n\t\t\ttype: block.type,\n\t\t\tprotected: block.protected,\n\t\t\ttoolCallId: block.toolCallId,\n\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\ttext: block.text,\n\t\t})),\n\t};\n}\n\ninterface ContextCompactionTranscriptFile {\n\tpath: string;\n\tcleanup(): void;\n}\n\nfunction writeContextCompactionTranscriptFile(transcript: CompactableTranscript): ContextCompactionTranscriptFile {\n\tconst directory = mkdtempSync(join(tmpdir(), \"atomic-context-transcript-\"));\n\tconst path = join(directory, \"transcript.jsonl\");\n\tconst lines = transcript.entries\n\t\t.filter((entry) => !isExcludedFromLlmContext(entry.message))\n\t\t.map((entry) => JSON.stringify(transcriptEntryFilePayload(entry)));\n\twriteFileSync(path, `${lines.join(\"\\n\")}\\n`, \"utf8\");\n\treturn {\n\t\tpath,\n\t\tcleanup: () => rmSync(directory, { recursive: true, force: true }),\n\t};\n}\n\nfunction contextCompactionTranscriptManifest(transcript: CompactableTranscript, transcriptFilePath: string): unknown {\n\tconst eligibleEntries = transcript.entries.filter((entry) => !isExcludedFromLlmContext(entry.message));\n\tconst selectedEntryIds = new Set<string>();\n\tconst selectedEntries: CompactableTranscriptEntry[] = [];\n\tconst addEntry = (entry: CompactableTranscriptEntry): void => {\n\t\tif (selectedEntryIds.has(entry.entryId) || selectedEntries.length >= CONTEXT_MANIFEST_MAX_ENTRIES) return;\n\t\tselectedEntryIds.add(entry.entryId);\n\t\tselectedEntries.push(entry);\n\t};\n\n\tfor (const entry of eligibleEntries.filter((entry) => entry.protected)) {\n\t\taddEntry(entry);\n\t}\n\tfor (const entry of [...eligibleEntries]\n\t\t.filter((entry) => !entry.protected)\n\t\t.sort((left, right) => right.tokenEstimate - left.tokenEstimate)) {\n\t\taddEntry(entry);\n\t}\n\tselectedEntries.sort((left, right) => eligibleEntries.indexOf(left) - eligibleEntries.indexOf(right));\n\n\treturn {\n\t\ttranscriptFilePath,\n\t\ttranscriptFileFormat: \"jsonl: one compactable transcript entry per line with full text and contentBlocks text\",\n\t\ttotalEntries: eligibleEntries.length,\n\t\tmanifestEntries: selectedEntries.length,\n\t\tomittedEntries: Math.max(0, eligibleEntries.length - selectedEntries.length),\n\t\ttokensBefore: transcript.tokensBefore,\n\t\tprotectedEntryIds: transcript.protectedEntryIds,\n\t\tentries: selectedEntries.map((entry) => ({\n\t\t\tentryId: entry.entryId,\n\t\t\trole: entry.role,\n\t\t\tprotected: entry.protected,\n\t\t\ttokenEstimate: entry.tokenEstimate,\n\t\t\ttoolCallIds: entry.toolCallIds,\n\t\t\ttoolResultFor: entry.toolResultFor,\n\t\t\tcontentBlockCount: entry.contentBlocks.length,\n\t\t\tcontentBlocks: entry.contentBlocks.map((block) => ({\n\t\t\t\tblockIndex: block.blockIndex,\n\t\t\t\ttype: block.type,\n\t\t\t\tprotected: block.protected,\n\t\t\t\ttoolCallId: block.toolCallId,\n\t\t\t\ttokenEstimate: block.tokenEstimate,\n\t\t\t})),\n\t\t\tpreview: truncateForPrompt(entry.text, CONTEXT_MANIFEST_PREVIEW_CHARS),\n\t\t})),\n\t};\n}\n\nfunction contextCompactionModePrompt(mode: ContextCompactionMode): string {\n\tif (mode === \"critical_overflow\") {\n\t\treturn `\\n<critical-overflow-mode>\\nThe previous model request overflowed its context window. This is a critical LRU-style compaction pass. First delete stale unprotected context. If that is not enough, you may also delete the earliest protected entries or protected content shown in the manifest. Evict in priority order: remove old reasoning traces first, then old user/custom/summary context, while preserving recent entries, unresolved errors, failed commands, and enough task-bearing context for the assistant to continue.\\n</critical-overflow-mode>`;\n\t}\n\treturn `\\n<standard-mode>\\nDo not delete entries or content blocks marked protected. Protected context is only eligible during critical overflow recovery, not during standard compaction.\\n</standard-mode>`;\n}\n\nexport function buildContextCompactionPrompt(\n\ttranscript: CompactableTranscript,\n\ttranscriptFilePath = \"<transcript file will be written during context compaction>\",\n\tmode: ContextCompactionMode = \"standard\",\n): string {\n\treturn `${CONTEXT_COMPACTION_FIXED_PROMPT}${contextCompactionModePrompt(mode)}\\n\\n<transcript-file>\\n${transcriptFilePath}\\n</transcript-file>\\n\\n<context-manifest>\\n${JSON.stringify(contextCompactionTranscriptManifest(transcript, transcriptFilePath), null, 2)}\\n</context-manifest>`;\n}\n\nfunction createContextCompactionAssistantMessage(\n\tmodel: Model<Api>,\n\tcontent: AssistantMessage[\"content\"],\n\tstopReason: AssistantMessage[\"stopReason\"],\n\terrorMessage?: string,\n): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent,\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason,\n\t\t...(errorMessage !== undefined ? { errorMessage } : {}),\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction createContextCompactionStopStream(model: Model<Api>, text: string) {\n\tconst stream = createAssistantMessageEventStream();\n\tqueueMicrotask(() => {\n\t\tconst message = createContextCompactionAssistantMessage(model, [{ type: \"text\", text }], \"stop\");\n\t\tstream.push({ type: \"done\", reason: \"stop\", message });\n\t\tstream.end(message);\n\t});\n\treturn stream;\n}\n\nfunction isContextCompactionOverflowError(model: Model<Api>, errorMessage: string): boolean {\n\treturn isContextOverflow(\n\t\tcreateContextCompactionAssistantMessage(model, [], \"error\", errorMessage),\n\t\tmodel.contextWindow,\n\t);\n}\n\ninterface ContextDeletionRun {\n\tvalidatedResult: ValidatedContextDeletionResult | undefined;\n\tlastToolError: string | undefined;\n}\n\nasync function runContextDeletionAssistant(\n\ttranscript: CompactableTranscript,\n\tmodel: Model<Api>,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tsignal?: AbortSignal,\n\tthinkingLevel: ThinkingLevel = \"off\",\n\tmode: ContextCompactionMode = \"standard\",\n): Promise<ContextDeletionRun> {\n\tconst maxTokens = model.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY;\n\tif (signal?.aborted) {\n\t\tthrow new Error(\"Context compaction failed: Request was aborted\");\n\t}\n\tconst transcriptFile = writeContextCompactionTranscriptFile(transcript);\n\tconst promptMessage: AgentMessage = {\n\t\trole: \"user\",\n\t\tcontent: [{ type: \"text\", text: buildContextCompactionPrompt(transcript, transcriptFile.path, mode) }],\n\t\ttimestamp: Date.now(),\n\t};\n\tconst deletionTool = createContextDeletionTool(transcript, { mode });\n\tlet compactionTurnCount = 0;\n\tconst agent = new Agent({\n\t\tinitialState: {\n\t\t\tsystemPrompt: CONTEXT_COMPACTION_SYSTEM_PROMPT,\n\t\t\tmodel,\n\t\t\tthinkingLevel,\n\t\t\ttools: deletionTool.tools,\n\t\t},\n\t\ttoolExecution: \"parallel\",\n\t\tstreamFn: async (requestModel, context, streamOptions) => {\n\t\t\tcompactionTurnCount += 1;\n\t\t\tif (compactionTurnCount > CONTEXT_COMPACTION_MAX_TURNS) {\n\t\t\t\treturn createContextCompactionStopStream(\n\t\t\t\t\trequestModel,\n\t\t\t\t\t`Reached the context compaction turn cap (${CONTEXT_COMPACTION_MAX_TURNS}); using the deletions recorded so far.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn streamSimple(requestModel, context, {\n\t\t\t\t...streamOptions,\n\t\t\t\tmaxTokens,\n\t\t\t\tapiKey,\n\t\t\t\theaders: headers ?? streamOptions?.headers,\n\t\t\t});\n\t\t},\n\t});\n\n\tconst abortOnSignal = () => agent.abort();\n\tsignal?.addEventListener(\"abort\", abortOnSignal, { once: true });\n\ttry {\n\t\tawait agent.prompt(promptMessage);\n\t} finally {\n\t\tsignal?.removeEventListener(\"abort\", abortOnSignal);\n\t\ttranscriptFile.cleanup();\n\t}\n\n\tif (signal?.aborted) {\n\t\tthrow new Error(\"Context compaction failed: Request was aborted\");\n\t}\n\tif (agent.state.errorMessage) {\n\t\tif (isContextCompactionOverflowError(model, agent.state.errorMessage)) {\n\t\t\treturn {\n\t\t\t\tvalidatedResult: deletionTool.getValidatedResult(),\n\t\t\t\tlastToolError: deletionTool.getLastError(),\n\t\t\t};\n\t\t}\n\t\tthrow new Error(`Context compaction failed: ${agent.state.errorMessage}`);\n\t}\n\tif (deletionTool.getCallCount() === 0) {\n\t\tthrow new Error(\n\t\t\t`Context compaction did not call any transcript inspection or deletion tools (${CONTEXT_SEARCH_TRANSCRIPT_TOOL_NAME}, ${CONTEXT_READ_ENTRY_TOOL_NAME}, ${CONTEXT_DELETE_TOOL_NAME}, or ${CONTEXT_GREP_DELETE_TOOL_NAME})`,\n\t\t);\n\t}\n\treturn {\n\t\tvalidatedResult: deletionTool.getValidatedResult(),\n\t\tlastToolError: deletionTool.getLastError(),\n\t};\n}\n\nexport async function contextCompact(\n\tpreparation: ContextCompactionPreparation,\n\tmodel: Model<Api>,\n\tapiKey: string,\n\theaders?: Record<string, string>,\n\tsignal?: AbortSignal,\n\tthinkingLevel: ThinkingLevel = \"off\",\n\tmode: ContextCompactionMode = preparation.mode ?? \"standard\",\n): Promise<ValidatedContextDeletionResult> {\n\tconst { validatedResult, lastToolError } = await runContextDeletionAssistant(\n\t\tpreparation.transcript,\n\t\tmodel,\n\t\tapiKey,\n\t\theaders,\n\t\tsignal,\n\t\tthinkingLevel,\n\t\tmode,\n\t);\n\tif (!validatedResult || validatedResult.deletedTargets.length === 0) {\n\t\tthrow new Error(\n\t\t\tlastToolError ? `No safe context deletions proposed; last deletion tool error: ${lastToolError}` : \"No safe context deletions proposed\",\n\t\t);\n\t}\n\treturn validatedResult;\n}\n"]}