@bastani/atomic 0.8.26-alpha.1 → 0.8.26-alpha.10
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.
- package/CHANGELOG.md +73 -0
- package/README.md +5 -5
- package/dist/builtin/intercom/CHANGELOG.md +54 -0
- package/dist/builtin/intercom/package.json +2 -2
- package/dist/builtin/mcp/CHANGELOG.md +54 -0
- package/dist/builtin/mcp/package.json +3 -3
- package/dist/builtin/subagents/CHANGELOG.md +55 -0
- package/dist/builtin/subagents/agents/codebase-online-researcher.md +9 -9
- package/dist/builtin/subagents/agents/debugger.md +6 -6
- package/dist/builtin/subagents/package.json +4 -4
- package/dist/builtin/subagents/prompts/parallel-handoff-plan.md +1 -1
- package/dist/builtin/subagents/skills/browser/EXAMPLES.md +151 -0
- package/dist/builtin/subagents/skills/browser/LICENSE.txt +21 -0
- package/dist/builtin/subagents/skills/browser/REFERENCE.md +451 -0
- package/dist/builtin/subagents/skills/browser/SKILL.md +170 -0
- package/dist/builtin/subagents/skills/subagent/SKILL.md +4 -4
- package/dist/builtin/subagents/src/runs/background/subagent-runner.ts +55 -12
- package/dist/builtin/subagents/src/runs/foreground/execution.ts +71 -12
- package/dist/builtin/subagents/src/runs/shared/acceptance.ts +2 -1
- package/dist/builtin/subagents/src/runs/shared/final-drain.ts +34 -0
- package/dist/builtin/subagents/src/runs/shared/model-fallback.ts +416 -7
- package/dist/builtin/subagents/src/runs/shared/worktree.ts +2 -2
- package/dist/builtin/web-access/CHANGELOG.md +54 -0
- package/dist/builtin/web-access/package.json +2 -2
- package/dist/builtin/workflows/CHANGELOG.md +66 -0
- package/dist/builtin/workflows/README.md +10 -8
- package/dist/builtin/workflows/builtin/deep-research-codebase.ts +11 -8
- package/dist/builtin/workflows/builtin/goal.ts +137 -109
- package/dist/builtin/workflows/builtin/index.d.ts +2 -0
- package/dist/builtin/workflows/builtin/open-claude-design.ts +228 -151
- package/dist/builtin/workflows/builtin/ralph.d.ts +2 -0
- package/dist/builtin/workflows/builtin/ralph.ts +452 -279
- package/dist/builtin/workflows/package.json +2 -2
- package/dist/builtin/workflows/skills/create-spec/SKILL.md +14 -0
- package/dist/builtin/workflows/skills/research-codebase/SKILL.md +29 -10
- package/dist/builtin/workflows/src/extension/index.ts +10 -2
- package/dist/builtin/workflows/src/extension/runtime.ts +35 -3
- package/dist/builtin/workflows/src/extension/wiring.ts +13 -1
- package/dist/builtin/workflows/src/runs/background/status.ts +52 -6
- package/dist/builtin/workflows/src/runs/foreground/executor.ts +453 -21
- package/dist/builtin/workflows/src/runs/foreground/stage-runner.ts +77 -11
- package/dist/builtin/workflows/src/runs/shared/model-fallback.ts +402 -8
- package/dist/builtin/workflows/src/runs/shared/worktree.ts +2 -2
- package/dist/builtin/workflows/src/shared/authoring-contract.d.ts +2 -2
- package/dist/builtin/workflows/src/shared/persistence-restore.ts +182 -6
- package/dist/builtin/workflows/src/shared/persistence-session-entries.ts +76 -6
- package/dist/builtin/workflows/src/shared/stage-prompt.ts +33 -2
- package/dist/builtin/workflows/src/shared/store-types.ts +31 -0
- package/dist/builtin/workflows/src/shared/store.ts +160 -18
- package/dist/builtin/workflows/src/shared/types.ts +3 -3
- package/dist/builtin/workflows/src/shared/workflow-failures.ts +758 -132
- package/dist/builtin/workflows/src/tui/stage-chat-view.ts +39 -3
- package/dist/builtin/workflows/src/tui/store-widget-installer.ts +74 -74
- package/dist/core/agent-session.d.ts +33 -6
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +157 -182
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/atomic-guide-command.d.ts.map +1 -1
- package/dist/core/atomic-guide-command.js +11 -9
- package/dist/core/atomic-guide-command.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +1 -1
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js +6 -3
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/compaction/compaction.d.ts.map +1 -1
- package/dist/core/compaction/compaction.js +23 -10
- package/dist/core/compaction/compaction.js.map +1 -1
- package/dist/core/compaction/context-compaction.d.ts +175 -0
- package/dist/core/compaction/context-compaction.d.ts.map +1 -0
- package/dist/core/compaction/context-compaction.js +1636 -0
- package/dist/core/compaction/context-compaction.js.map +1 -0
- package/dist/core/compaction/index.d.ts +1 -0
- package/dist/core/compaction/index.d.ts.map +1 -1
- package/dist/core/compaction/index.js +1 -0
- package/dist/core/compaction/index.js.map +1 -1
- package/dist/core/extensions/types.d.ts +3 -2
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/footer-data-provider.d.ts.map +1 -1
- package/dist/core/footer-data-provider.js +3 -0
- package/dist/core/footer-data-provider.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +14 -7
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/session-manager.d.ts +41 -1
- package/dist/core/session-manager.d.ts.map +1 -1
- package/dist/core/session-manager.js +146 -7
- package/dist/core/session-manager.js.map +1 -1
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js +1 -1
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/core/tools/ask-user-question/tool/format-answer.d.ts +5 -5
- package/dist/core/tools/ask-user-question/tool/format-answer.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/tool/format-answer.js +5 -5
- package/dist/core/tools/ask-user-question/tool/format-answer.js.map +1 -1
- package/dist/core/tools/ask-user-question/tool/response-envelope.d.ts +16 -3
- package/dist/core/tools/ask-user-question/tool/response-envelope.d.ts.map +1 -1
- package/dist/core/tools/ask-user-question/tool/response-envelope.js +21 -3
- package/dist/core/tools/ask-user-question/tool/response-envelope.js.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/modes/index.d.ts +1 -1
- package/dist/modes/index.d.ts.map +1 -1
- package/dist/modes/index.js.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.d.ts.map +1 -1
- package/dist/modes/interactive/components/chat-session-host.js +17 -0
- package/dist/modes/interactive/components/chat-session-host.js.map +1 -1
- package/dist/modes/interactive/components/context-compaction-summary-message.d.ts +17 -0
- package/dist/modes/interactive/components/context-compaction-summary-message.d.ts.map +1 -0
- package/dist/modes/interactive/components/context-compaction-summary-message.js +83 -0
- package/dist/modes/interactive/components/context-compaction-summary-message.js.map +1 -0
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +4 -1
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/index.d.ts +1 -0
- package/dist/modes/interactive/components/index.d.ts.map +1 -1
- package/dist/modes/interactive/components/index.js +1 -0
- package/dist/modes/interactive/components/index.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +1 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +75 -10
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-client.d.ts +13 -8
- package/dist/modes/rpc/rpc-client.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-client.js +8 -1
- package/dist/modes/rpc/rpc-client.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +4 -0
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +14 -3
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/utils/git-env.d.ts +10 -0
- package/dist/utils/git-env.d.ts.map +1 -0
- package/dist/utils/git-env.js +33 -0
- package/dist/utils/git-env.js.map +1 -0
- package/docs/compaction.md +185 -50
- package/docs/custom-provider.md +11 -9
- package/docs/extensions.md +46 -42
- package/docs/index.md +13 -6
- package/docs/json.md +15 -12
- package/docs/packages.md +2 -0
- package/docs/providers.md +4 -1
- package/docs/quickstart.md +18 -11
- package/docs/rpc.md +38 -23
- package/docs/sdk.md +17 -8
- package/docs/session-format.md +26 -13
- package/docs/sessions.md +3 -3
- package/docs/settings.md +2 -2
- package/docs/skills.md +1 -15
- package/docs/termux.md +9 -10
- package/docs/themes.md +2 -2
- package/docs/tmux.md +3 -3
- package/docs/tui.md +19 -32
- package/docs/usage.md +2 -2
- package/docs/workflows.md +60 -16
- package/package.json +6 -12
- package/dist/builtin/subagents/skills/browser-use/SKILL.md +0 -234
- package/dist/builtin/subagents/skills/browser-use/references/cdp-python.md +0 -76
- package/dist/builtin/subagents/skills/browser-use/references/multi-session.md +0 -92
- package/node_modules/@earendil-works/pi-tui/README.md +0 -779
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.d.ts +0 -54
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.js +0 -632
- package/node_modules/@earendil-works/pi-tui/dist/autocomplete.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/box.d.ts +0 -22
- package/node_modules/@earendil-works/pi-tui/dist/components/box.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/box.js +0 -104
- package/node_modules/@earendil-works/pi-tui/dist/components/box.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/cancellable-loader.d.ts +0 -22
- package/node_modules/@earendil-works/pi-tui/dist/components/cancellable-loader.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/cancellable-loader.js +0 -35
- package/node_modules/@earendil-works/pi-tui/dist/components/cancellable-loader.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/editor.d.ts +0 -249
- package/node_modules/@earendil-works/pi-tui/dist/components/editor.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/editor.js +0 -1857
- package/node_modules/@earendil-works/pi-tui/dist/components/editor.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/image.d.ts +0 -28
- package/node_modules/@earendil-works/pi-tui/dist/components/image.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/image.js +0 -89
- package/node_modules/@earendil-works/pi-tui/dist/components/image.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/input.d.ts +0 -37
- package/node_modules/@earendil-works/pi-tui/dist/components/input.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/input.js +0 -378
- package/node_modules/@earendil-works/pi-tui/dist/components/input.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/loader.d.ts +0 -31
- package/node_modules/@earendil-works/pi-tui/dist/components/loader.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/loader.js +0 -69
- package/node_modules/@earendil-works/pi-tui/dist/components/loader.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/markdown.d.ts +0 -96
- package/node_modules/@earendil-works/pi-tui/dist/components/markdown.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/markdown.js +0 -644
- package/node_modules/@earendil-works/pi-tui/dist/components/markdown.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/select-list.d.ts +0 -50
- package/node_modules/@earendil-works/pi-tui/dist/components/select-list.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/select-list.js +0 -159
- package/node_modules/@earendil-works/pi-tui/dist/components/select-list.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/settings-list.d.ts +0 -50
- package/node_modules/@earendil-works/pi-tui/dist/components/settings-list.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/settings-list.js +0 -185
- package/node_modules/@earendil-works/pi-tui/dist/components/settings-list.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/spacer.d.ts +0 -12
- package/node_modules/@earendil-works/pi-tui/dist/components/spacer.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/spacer.js +0 -23
- package/node_modules/@earendil-works/pi-tui/dist/components/spacer.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/text.d.ts +0 -19
- package/node_modules/@earendil-works/pi-tui/dist/components/text.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/text.js +0 -89
- package/node_modules/@earendil-works/pi-tui/dist/components/text.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/truncated-text.d.ts +0 -13
- package/node_modules/@earendil-works/pi-tui/dist/components/truncated-text.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/components/truncated-text.js +0 -51
- package/node_modules/@earendil-works/pi-tui/dist/components/truncated-text.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/editor-component.d.ts +0 -39
- package/node_modules/@earendil-works/pi-tui/dist/editor-component.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/editor-component.js +0 -2
- package/node_modules/@earendil-works/pi-tui/dist/editor-component.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/fuzzy.d.ts +0 -16
- package/node_modules/@earendil-works/pi-tui/dist/fuzzy.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/fuzzy.js +0 -110
- package/node_modules/@earendil-works/pi-tui/dist/fuzzy.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/index.d.ts +0 -23
- package/node_modules/@earendil-works/pi-tui/dist/index.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/index.js +0 -32
- package/node_modules/@earendil-works/pi-tui/dist/index.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/keybindings.d.ts +0 -193
- package/node_modules/@earendil-works/pi-tui/dist/keybindings.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/keybindings.js +0 -174
- package/node_modules/@earendil-works/pi-tui/dist/keybindings.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/keys.d.ts +0 -184
- package/node_modules/@earendil-works/pi-tui/dist/keys.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/keys.js +0 -1173
- package/node_modules/@earendil-works/pi-tui/dist/keys.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/kill-ring.d.ts +0 -28
- package/node_modules/@earendil-works/pi-tui/dist/kill-ring.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/kill-ring.js +0 -44
- package/node_modules/@earendil-works/pi-tui/dist/kill-ring.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/native-modifiers.d.ts +0 -3
- package/node_modules/@earendil-works/pi-tui/dist/native-modifiers.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/native-modifiers.js +0 -53
- package/node_modules/@earendil-works/pi-tui/dist/native-modifiers.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/stdin-buffer.d.ts +0 -50
- package/node_modules/@earendil-works/pi-tui/dist/stdin-buffer.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/stdin-buffer.js +0 -361
- package/node_modules/@earendil-works/pi-tui/dist/stdin-buffer.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.d.ts +0 -90
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.js +0 -366
- package/node_modules/@earendil-works/pi-tui/dist/terminal-image.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.d.ts +0 -113
- package/node_modules/@earendil-works/pi-tui/dist/terminal.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.js +0 -472
- package/node_modules/@earendil-works/pi-tui/dist/terminal.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui.d.ts +0 -227
- package/node_modules/@earendil-works/pi-tui/dist/tui.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/tui.js +0 -1106
- package/node_modules/@earendil-works/pi-tui/dist/tui.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/undo-stack.d.ts +0 -17
- package/node_modules/@earendil-works/pi-tui/dist/undo-stack.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/undo-stack.js +0 -25
- package/node_modules/@earendil-works/pi-tui/dist/undo-stack.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/utils.d.ts +0 -84
- package/node_modules/@earendil-works/pi-tui/dist/utils.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/utils.js +0 -1029
- package/node_modules/@earendil-works/pi-tui/dist/utils.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/word-navigation.d.ts +0 -25
- package/node_modules/@earendil-works/pi-tui/dist/word-navigation.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-tui/dist/word-navigation.js +0 -96
- package/node_modules/@earendil-works/pi-tui/dist/word-navigation.js.map +0 -1
- package/node_modules/@earendil-works/pi-tui/native/darwin/prebuilds/darwin-arm64/darwin-modifiers.node +0 -0
- package/node_modules/@earendil-works/pi-tui/native/darwin/prebuilds/darwin-x64/darwin-modifiers.node +0 -0
- package/node_modules/@earendil-works/pi-tui/native/win32/prebuilds/win32-arm64/win32-console-mode.node +0 -0
- package/node_modules/@earendil-works/pi-tui/native/win32/prebuilds/win32-x64/win32-console-mode.node +0 -0
- package/node_modules/@earendil-works/pi-tui/package.json +0 -47
- package/node_modules/get-east-asian-width/index.d.ts +0 -60
- package/node_modules/get-east-asian-width/index.js +0 -30
- package/node_modules/get-east-asian-width/license +0 -9
- package/node_modules/get-east-asian-width/lookup-data.js +0 -21
- package/node_modules/get-east-asian-width/lookup.js +0 -138
- package/node_modules/get-east-asian-width/package.json +0 -71
- package/node_modules/get-east-asian-width/readme.md +0 -65
- package/node_modules/get-east-asian-width/utilities.js +0 -24
- package/node_modules/marked/LICENSE.md +0 -44
- package/node_modules/marked/README.md +0 -106
- package/node_modules/marked/bin/main.js +0 -282
- package/node_modules/marked/bin/marked.js +0 -15
- package/node_modules/marked/lib/marked.cjs +0 -2211
- package/node_modules/marked/lib/marked.cjs.map +0 -7
- package/node_modules/marked/lib/marked.d.cts +0 -728
- package/node_modules/marked/lib/marked.d.ts +0 -728
- package/node_modules/marked/lib/marked.esm.js +0 -2189
- package/node_modules/marked/lib/marked.esm.js.map +0 -7
- package/node_modules/marked/lib/marked.umd.js +0 -2213
- package/node_modules/marked/lib/marked.umd.js.map +0 -7
- package/node_modules/marked/man/marked.1 +0 -111
- package/node_modules/marked/man/marked.1.md +0 -92
- package/node_modules/marked/marked.min.js +0 -69
- package/node_modules/marked/package.json +0 -111
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { type AgentMessage, type AgentTool, type ThinkingLevel } from "@earendil-works/pi-agent-core";
|
|
2
|
+
import type { Api, AssistantMessage, Model } from "@earendil-works/pi-ai";
|
|
3
|
+
import { Type } from "typebox";
|
|
4
|
+
import { type ContextCompactionStats, type ContextDeletionTarget, type SessionEntry } from "../session-manager.ts";
|
|
5
|
+
import type { CompactionSettings } from "./compaction.ts";
|
|
6
|
+
export declare const CONTEXT_COMPACTION_PROMPT_VERSION: 1;
|
|
7
|
+
export type ContextCompactionMode = "standard" | "critical_overflow";
|
|
8
|
+
export interface ContextDeletionRequest {
|
|
9
|
+
deletions: Array<{
|
|
10
|
+
kind: "entry" | "content_block";
|
|
11
|
+
entryId: string;
|
|
12
|
+
blockIndex?: number;
|
|
13
|
+
rationale?: string;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export interface CompactableContentBlock {
|
|
17
|
+
entryId: string;
|
|
18
|
+
blockIndex: number;
|
|
19
|
+
type: string;
|
|
20
|
+
text: string;
|
|
21
|
+
tokenEstimate: number;
|
|
22
|
+
protected: boolean;
|
|
23
|
+
toolCallId?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface CompactableTranscriptEntry {
|
|
26
|
+
entryId: string;
|
|
27
|
+
entryType: SessionEntry["type"];
|
|
28
|
+
role: AgentMessage["role"];
|
|
29
|
+
text: string;
|
|
30
|
+
tokenEstimate: number;
|
|
31
|
+
protected: boolean;
|
|
32
|
+
contentBlocks: CompactableContentBlock[];
|
|
33
|
+
message: AgentMessage;
|
|
34
|
+
toolCallIds: string[];
|
|
35
|
+
toolResultFor?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface CompactableTranscript {
|
|
38
|
+
entries: CompactableTranscriptEntry[];
|
|
39
|
+
protectedEntryIds: string[];
|
|
40
|
+
tokensBefore: number;
|
|
41
|
+
settings: CompactionSettings;
|
|
42
|
+
}
|
|
43
|
+
export interface ContextCompactionPreparation {
|
|
44
|
+
transcript: CompactableTranscript;
|
|
45
|
+
branchEntries: SessionEntry[];
|
|
46
|
+
mode?: ContextCompactionMode;
|
|
47
|
+
}
|
|
48
|
+
export interface ValidatedContextDeletionResult {
|
|
49
|
+
deletedTargets: ContextDeletionTarget[];
|
|
50
|
+
protectedEntryIds: string[];
|
|
51
|
+
stats: ContextCompactionStats;
|
|
52
|
+
}
|
|
53
|
+
export interface ContextCompactionResult extends ValidatedContextDeletionResult {
|
|
54
|
+
promptVersion: typeof CONTEXT_COMPACTION_PROMPT_VERSION;
|
|
55
|
+
backupPath?: string;
|
|
56
|
+
}
|
|
57
|
+
export declare const CONTEXT_COMPACTION_MAX_TURNS: 50;
|
|
58
|
+
declare const ContextDeleteToolParameters: Type.TObject<{
|
|
59
|
+
deletions: Type.TArray<Type.TObject<{
|
|
60
|
+
kind: Type.TUnsafe<"content_block" | "entry">;
|
|
61
|
+
entryId: Type.TString;
|
|
62
|
+
blockIndex: Type.TOptional<Type.TInteger>;
|
|
63
|
+
}>>;
|
|
64
|
+
}>;
|
|
65
|
+
declare const ContextGrepDeleteToolParameters: Type.TObject<{
|
|
66
|
+
pattern: Type.TString;
|
|
67
|
+
regex: Type.TOptional<Type.TBoolean>;
|
|
68
|
+
caseSensitive: Type.TOptional<Type.TBoolean>;
|
|
69
|
+
target: Type.TOptional<Type.TUnsafe<"content_block" | "entry">>;
|
|
70
|
+
maxMatches: Type.TOptional<Type.TInteger>;
|
|
71
|
+
expectedMatchCount: Type.TOptional<Type.TInteger>;
|
|
72
|
+
}>;
|
|
73
|
+
declare const ContextSearchTranscriptToolParameters: Type.TObject<{
|
|
74
|
+
pattern: Type.TString;
|
|
75
|
+
regex: Type.TOptional<Type.TBoolean>;
|
|
76
|
+
caseSensitive: Type.TOptional<Type.TBoolean>;
|
|
77
|
+
target: Type.TOptional<Type.TUnsafe<"content_block" | "entry">>;
|
|
78
|
+
maxMatches: Type.TOptional<Type.TInteger>;
|
|
79
|
+
contextChars: Type.TOptional<Type.TInteger>;
|
|
80
|
+
}>;
|
|
81
|
+
declare const ContextReadEntryToolParameters: Type.TObject<{
|
|
82
|
+
entryId: Type.TString;
|
|
83
|
+
blockIndex: Type.TOptional<Type.TInteger>;
|
|
84
|
+
offset: Type.TOptional<Type.TInteger>;
|
|
85
|
+
maxChars: Type.TOptional<Type.TInteger>;
|
|
86
|
+
}>;
|
|
87
|
+
export interface ContextDeletionToolDetails {
|
|
88
|
+
deletions: ContextDeletionRequest["deletions"];
|
|
89
|
+
deletedTargets: ContextDeletionTarget[];
|
|
90
|
+
stats: ContextCompactionStats;
|
|
91
|
+
callCount: number;
|
|
92
|
+
error?: string;
|
|
93
|
+
}
|
|
94
|
+
export interface ContextGrepDeletionMatch {
|
|
95
|
+
entryId: string;
|
|
96
|
+
target: "entry" | "content_block";
|
|
97
|
+
blockIndex?: number;
|
|
98
|
+
text: string;
|
|
99
|
+
}
|
|
100
|
+
export interface ContextGrepDeletionSkipped {
|
|
101
|
+
entryId?: string;
|
|
102
|
+
target?: "entry" | "content_block";
|
|
103
|
+
blockIndex?: number;
|
|
104
|
+
reason: "protected_entry" | "protected_block" | "already_deleted" | "max_matches_exceeded" | "expected_match_count_mismatch";
|
|
105
|
+
text?: string;
|
|
106
|
+
}
|
|
107
|
+
export interface ContextGrepDeletionToolDetails {
|
|
108
|
+
pattern: string;
|
|
109
|
+
regex: boolean;
|
|
110
|
+
caseSensitive: boolean;
|
|
111
|
+
target: "entry" | "content_block";
|
|
112
|
+
matches: ContextGrepDeletionMatch[];
|
|
113
|
+
skipped: ContextGrepDeletionSkipped[];
|
|
114
|
+
deletedTargets: ContextDeletionTarget[];
|
|
115
|
+
stats: ContextCompactionStats;
|
|
116
|
+
callCount: number;
|
|
117
|
+
error?: string;
|
|
118
|
+
}
|
|
119
|
+
export interface ContextTranscriptSearchMatch {
|
|
120
|
+
entryId: string;
|
|
121
|
+
target: "entry" | "content_block";
|
|
122
|
+
blockIndex?: number;
|
|
123
|
+
matchIndex: number;
|
|
124
|
+
snippet: string;
|
|
125
|
+
protected: boolean;
|
|
126
|
+
}
|
|
127
|
+
export interface ContextTranscriptSearchToolDetails {
|
|
128
|
+
pattern: string;
|
|
129
|
+
regex: boolean;
|
|
130
|
+
caseSensitive: boolean;
|
|
131
|
+
target: "entry" | "content_block";
|
|
132
|
+
matches: ContextTranscriptSearchMatch[];
|
|
133
|
+
truncated: boolean;
|
|
134
|
+
callCount: number;
|
|
135
|
+
error?: string;
|
|
136
|
+
}
|
|
137
|
+
export interface ContextReadEntryToolDetails {
|
|
138
|
+
entryId: string;
|
|
139
|
+
blockIndex?: number;
|
|
140
|
+
offset: number;
|
|
141
|
+
maxChars: number;
|
|
142
|
+
totalChars: number;
|
|
143
|
+
text: string;
|
|
144
|
+
truncatedBefore: boolean;
|
|
145
|
+
truncatedAfter: boolean;
|
|
146
|
+
callCount: number;
|
|
147
|
+
error?: string;
|
|
148
|
+
}
|
|
149
|
+
export interface ContextDeletionToolController {
|
|
150
|
+
tool: AgentTool<typeof ContextDeleteToolParameters, ContextDeletionToolDetails>;
|
|
151
|
+
grepTool: AgentTool<typeof ContextGrepDeleteToolParameters, ContextGrepDeletionToolDetails>;
|
|
152
|
+
searchTool: AgentTool<typeof ContextSearchTranscriptToolParameters, ContextTranscriptSearchToolDetails>;
|
|
153
|
+
readEntryTool: AgentTool<typeof ContextReadEntryToolParameters, ContextReadEntryToolDetails>;
|
|
154
|
+
tools: AgentTool[];
|
|
155
|
+
getDeletionRequest(): ContextDeletionRequest;
|
|
156
|
+
getValidatedResult(): ValidatedContextDeletionResult | undefined;
|
|
157
|
+
getLastError(): string | undefined;
|
|
158
|
+
getCallCount(): number;
|
|
159
|
+
}
|
|
160
|
+
export interface ContextCompactionRunOptions {
|
|
161
|
+
mode?: ContextCompactionMode;
|
|
162
|
+
}
|
|
163
|
+
export declare function prepareContextCompaction(pathEntries: SessionEntry[], settings: CompactionSettings, options?: ContextCompactionRunOptions): ContextCompactionPreparation | undefined;
|
|
164
|
+
interface ContextDeletionValidationOptions {
|
|
165
|
+
mode?: ContextCompactionMode;
|
|
166
|
+
}
|
|
167
|
+
export declare function validateContextDeletionRequest(request: ContextDeletionRequest, transcript: CompactableTranscript, options?: ContextDeletionValidationOptions): ValidatedContextDeletionResult;
|
|
168
|
+
export declare function createContextDeletionTool(transcript: CompactableTranscript, options?: ContextCompactionRunOptions): ContextDeletionToolController;
|
|
169
|
+
export declare function parseContextDeletionRequest(text: string): ContextDeletionRequest;
|
|
170
|
+
export declare function parseContextDeletionResponse(response: AssistantMessage): ContextDeletionRequest;
|
|
171
|
+
export declare function buildContextCompactionPrompt(transcript: CompactableTranscript, transcriptFilePath?: string, mode?: ContextCompactionMode): string;
|
|
172
|
+
export declare function getLowestContextCompactionThinkingLevel(model: Model<Api>): ThinkingLevel;
|
|
173
|
+
export declare function contextCompact(preparation: ContextCompactionPreparation, model: Model<Api>, apiKey: string, headers?: Record<string, string>, signal?: AbortSignal, _thinkingLevel?: ThinkingLevel, mode?: ContextCompactionMode): Promise<ValidatedContextDeletionResult>;
|
|
174
|
+
export {};
|
|
175
|
+
//# sourceMappingURL=context-compaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context-compaction.d.ts","sourceRoot":"","sources":["../../../src/core/compaction/context-compaction.ts"],"names":[],"mappings":"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","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\tgetSupportedThinkingLevels,\n\tisContextOverflow,\n\tstreamSimple,\n\tStringEnum,\n} from \"@earendil-works/pi-ai\";\nimport { mkdtempSync, rmSync, writeFileSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\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\nconst CONTEXT_COMPACTION_THINKING_LEVEL_ORDER: ThinkingLevel[] = [\"off\", \"minimal\", \"low\", \"medium\", \"high\", \"xhigh\"];\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\ntype SqliteValue = string | number | bigint | null;\ntype SqliteRow = Record<string, SqliteValue>;\n\ninterface SqliteStatementLike {\n\tall(...params: SqliteValue[]): SqliteRow[];\n\tget(...params: SqliteValue[]): SqliteRow | undefined;\n\trun(...params: SqliteValue[]): unknown;\n}\n\ninterface SqliteDatabaseLike {\n\texec(sql: string): void;\n\tprepare(sql: string): SqliteStatementLike;\n\tclose(): unknown;\n}\n\ninterface SqliteConstructorLike {\n\tnew (filename: string): SqliteDatabaseLike;\n}\n\ninterface BunSqliteModule {\n\tDatabase: SqliteConstructorLike;\n}\n\nconst moduleRequire = createRequire(import.meta.url);\n\nfunction createTransientSqliteDatabase(): SqliteDatabaseLike {\n\t// better-sqlite3 is the portable/package dependency for Node-based Atomic installs.\n\t// Bun cannot dlopen better-sqlite3 yet, so Bun runtime/tests use the API-compatible builtin.\n\tif (process.versions.bun) {\n\t\tconst sqlite = moduleRequire(\"bun:sqlite\") as BunSqliteModule;\n\t\treturn new sqlite.Database(\":memory:\");\n\t}\n\n\tconst BetterSqliteDatabase = moduleRequire(\"better-sqlite3\") as SqliteConstructorLike;\n\treturn new BetterSqliteDatabase(\":memory:\");\n}\n\nclass SqliteAdapter {\n\tprivate readonly db: SqliteDatabaseLike;\n\n\tconstructor(db: SqliteDatabaseLike) {\n\t\tthis.db = db;\n\t}\n\n\tstatic createTransient(): SqliteAdapter {\n\t\treturn new SqliteAdapter(createTransientSqliteDatabase());\n\t}\n\n\texec(sql: string): void {\n\t\tthis.db.exec(sql);\n\t}\n\n\trun(sql: string, ...params: SqliteValue[]): void {\n\t\tthis.db.prepare(sql).run(...params);\n\t}\n\n\tall<TRow extends SqliteRow>(sql: string, ...params: SqliteValue[]): TRow[] {\n\t\treturn this.db.prepare(sql).all(...params) as TRow[];\n\t}\n\n\tget<TRow extends SqliteRow>(sql: string, ...params: SqliteValue[]): TRow | undefined {\n\t\treturn this.db.prepare(sql).get(...params) as TRow | undefined;\n\t}\n\n\ttransaction<T>(operation: () => T): T {\n\t\tthis.exec(\"BEGIN IMMEDIATE\");\n\t\ttry {\n\t\t\tconst result = operation();\n\t\t\tthis.exec(\"COMMIT\");\n\t\t\treturn result;\n\t\t} catch (error) {\n\t\t\ttry {\n\t\t\t\tthis.exec(\"ROLLBACK\");\n\t\t\t} catch {}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\tclose(): void {\n\t\tthis.db.close();\n\t}\n}\n\ninterface DeletionTargetRow extends SqliteRow {\n\tkind: \"entry\" | \"content_block\";\n\tentry_id: string;\n\tblock_index: number | null;\n}\n\ninterface EntryTextRow extends SqliteRow {\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 extends SqliteRow {\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\nclass ContextDeletionSqliteStore {\n\tprivate readonly sqlite: SqliteAdapter;\n\n\tconstructor(sqlite: SqliteAdapter) {\n\t\tthis.sqlite = sqlite;\n\t}\n\n\tinitialize(transcript: CompactableTranscript): void {\n\t\tthis.sqlite.transaction(() => {\n\t\t\tthis.sqlite.exec(`\n\t\t\t\tPRAGMA foreign_keys = ON;\n\t\t\t\tCREATE TABLE transcript_entries (\n\t\t\t\t\tposition INTEGER PRIMARY KEY,\n\t\t\t\t\tentry_id TEXT NOT NULL UNIQUE,\n\t\t\t\t\trole TEXT NOT NULL,\n\t\t\t\t\tis_protected INTEGER NOT NULL,\n\t\t\t\t\ttoken_estimate INTEGER NOT NULL,\n\t\t\t\t\ttext TEXT NOT NULL,\n\t\t\t\t\ttool_result_for TEXT\n\t\t\t\t);\n\t\t\t\tCREATE TABLE transcript_content_blocks (\n\t\t\t\t\tentry_id TEXT NOT NULL,\n\t\t\t\t\tblock_index INTEGER NOT NULL,\n\t\t\t\t\ttype TEXT NOT NULL,\n\t\t\t\t\tis_protected INTEGER NOT NULL,\n\t\t\t\t\ttoken_estimate INTEGER NOT NULL,\n\t\t\t\t\ttext TEXT NOT NULL,\n\t\t\t\t\ttool_call_id TEXT,\n\t\t\t\t\tPRIMARY KEY (entry_id, block_index),\n\t\t\t\t\tFOREIGN KEY (entry_id) REFERENCES transcript_entries(entry_id) ON DELETE CASCADE\n\t\t\t\t);\n\t\t\t\tCREATE TABLE deletion_targets (\n\t\t\t\t\tposition INTEGER PRIMARY KEY AUTOINCREMENT,\n\t\t\t\t\ttarget_key TEXT NOT NULL UNIQUE,\n\t\t\t\t\tkind TEXT NOT NULL CHECK (kind IN ('entry', 'content_block')),\n\t\t\t\t\tentry_id TEXT NOT NULL,\n\t\t\t\t\tblock_index INTEGER\n\t\t\t\t);\n\t\t\t\tCREATE TABLE context_compaction_state (\n\t\t\t\t\tkey TEXT PRIMARY KEY,\n\t\t\t\t\tvalue TEXT NOT NULL\n\t\t\t\t);\n\t\t\t\tINSERT INTO context_compaction_state (key, value) VALUES ('call_count', '0');\n\t\t\t`);\n\n\t\t\tfor (const [position, entry] of transcript.entries.entries()) {\n\t\t\t\tthis.sqlite.run(\n\t\t\t\t\t\"INSERT INTO transcript_entries (position, entry_id, role, is_protected, token_estimate, text, tool_result_for) VALUES (?, ?, ?, ?, ?, ?, ?)\",\n\t\t\t\t\tposition,\n\t\t\t\t\tentry.entryId,\n\t\t\t\t\tentry.role,\n\t\t\t\t\tentry.protected ? 1 : 0,\n\t\t\t\t\tentry.tokenEstimate,\n\t\t\t\t\tentry.text,\n\t\t\t\t\tentry.toolResultFor ?? null,\n\t\t\t\t);\n\t\t\t\tfor (const block of entry.contentBlocks) {\n\t\t\t\t\tthis.sqlite.run(\n\t\t\t\t\t\t\"INSERT INTO transcript_content_blocks (entry_id, block_index, type, is_protected, token_estimate, text, tool_call_id) VALUES (?, ?, ?, ?, ?, ?, ?)\",\n\t\t\t\t\t\tblock.entryId,\n\t\t\t\t\t\tblock.blockIndex,\n\t\t\t\t\t\tblock.type,\n\t\t\t\t\t\tblock.protected ? 1 : 0,\n\t\t\t\t\t\tblock.tokenEstimate,\n\t\t\t\t\t\tblock.text,\n\t\t\t\t\t\tblock.toolCallId ?? null,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\t}\n\n\ttransaction<T>(operation: () => T): T {\n\t\treturn this.sqlite.transaction(operation);\n\t}\n\n\treadTargets(): ContextDeletionTarget[] {\n\t\treturn this.sqlite\n\t\t\t.all<DeletionTargetRow>(\"SELECT kind, entry_id, block_index FROM deletion_targets ORDER BY position\")\n\t\t\t.map((row) =>\n\t\t\t\trow.kind === \"entry\"\n\t\t\t\t\t? { kind: \"entry\", entryId: row.entry_id }\n\t\t\t\t\t: { kind: \"content_block\", entryId: row.entry_id, blockIndex: row.block_index as number },\n\t\t\t);\n\t}\n\n\treplaceTargets(targets: readonly ContextDeletionTarget[]): void {\n\t\tthis.sqlite.run(\"DELETE FROM deletion_targets\");\n\t\tfor (const target of targets) {\n\t\t\tthis.sqlite.run(\n\t\t\t\t\"INSERT INTO deletion_targets (target_key, kind, entry_id, block_index) VALUES (?, ?, ?, ?)\",\n\t\t\t\ttargetKey(target),\n\t\t\t\ttarget.kind,\n\t\t\t\ttarget.entryId,\n\t\t\t\ttarget.kind === \"content_block\" ? target.blockIndex : null,\n\t\t\t);\n\t\t}\n\t}\n\n\tlistEntriesForGrep(): EntryTextRow[] {\n\t\treturn this.sqlite.all<EntryTextRow>(\n\t\t\t\"SELECT entry_id, text, is_protected FROM transcript_entries ORDER BY position\",\n\t\t);\n\t}\n\n\tlistContentBlocksForGrep(): ContentBlockTextRow[] {\n\t\treturn this.sqlite.all<ContentBlockTextRow>(`\n\t\t\tSELECT\n\t\t\t\tblocks.entry_id,\n\t\t\t\tblocks.block_index,\n\t\t\t\tblocks.text,\n\t\t\t\tentries.is_protected AS entry_protected,\n\t\t\t\tblocks.is_protected AS block_protected,\n\t\t\t\t(\n\t\t\t\t\tSELECT COUNT(*)\n\t\t\t\t\tFROM transcript_content_blocks sibling\n\t\t\t\t\tWHERE sibling.entry_id = blocks.entry_id\n\t\t\t\t) AS block_count\n\t\t\tFROM transcript_content_blocks blocks\n\t\t\tJOIN transcript_entries entries ON entries.entry_id = blocks.entry_id\n\t\t\tORDER BY entries.position, blocks.block_index\n\t\t`);\n\t}\n\n\tgetEntryForRead(entryId: string): EntryReadRow | undefined {\n\t\treturn this.sqlite.get<EntryReadRow>(\n\t\t\t\"SELECT entry_id, role, is_protected, token_estimate, text FROM transcript_entries WHERE entry_id = ?\",\n\t\t\tentryId,\n\t\t);\n\t}\n\n\tgetContentBlockForRead(entryId: string, blockIndex: number): ContentBlockReadRow | undefined {\n\t\treturn this.sqlite.get<ContentBlockReadRow>(\n\t\t\t`\n\t\t\t\tSELECT\n\t\t\t\t\tblocks.entry_id,\n\t\t\t\t\tblocks.block_index,\n\t\t\t\t\tblocks.type,\n\t\t\t\t\tblocks.token_estimate,\n\t\t\t\t\tblocks.text,\n\t\t\t\t\tentries.is_protected AS entry_protected,\n\t\t\t\t\tblocks.is_protected AS block_protected,\n\t\t\t\t\t(\n\t\t\t\t\t\tSELECT COUNT(*)\n\t\t\t\t\t\tFROM transcript_content_blocks sibling\n\t\t\t\t\t\tWHERE sibling.entry_id = blocks.entry_id\n\t\t\t\t\t) AS block_count\n\t\t\t\tFROM transcript_content_blocks blocks\n\t\t\t\tJOIN transcript_entries entries ON entries.entry_id = blocks.entry_id\n\t\t\t\tWHERE blocks.entry_id = ? AND blocks.block_index = ?\n\t\t\t`,\n\t\t\tentryId,\n\t\t\tblockIndex,\n\t\t);\n\t}\n\n\tgetGrepScanTextLength(target: \"entry\" | \"content_block\"): number {\n\t\tconst table = target === \"entry\" ? \"transcript_entries\" : \"transcript_content_blocks\";\n\t\tconst row = this.sqlite.get<{ scan_chars: number | null }>(`SELECT SUM(LENGTH(text)) AS scan_chars FROM ${table}`);\n\t\treturn row?.scan_chars ?? 0;\n\t}\n\n\tincrementCallCount(): number {\n\t\tconst next = this.getCallCount() + 1;\n\t\tthis.setState(\"call_count\", String(next));\n\t\treturn next;\n\t}\n\n\tgetCallCount(): number {\n\t\treturn Number(this.getState(\"call_count\") ?? \"0\");\n\t}\n\n\tsetLastError(message: string): void {\n\t\tthis.setState(\"last_error\", message);\n\t}\n\n\tclearLastError(): void {\n\t\tthis.sqlite.run(\"DELETE FROM context_compaction_state WHERE key = ?\", \"last_error\");\n\t}\n\n\tgetLastError(): string | undefined {\n\t\treturn this.getState(\"last_error\");\n\t}\n\n\tprivate getState(key: string): string | undefined {\n\t\treturn this.sqlite.get<{ value: string }>(\"SELECT value FROM context_compaction_state WHERE key = ?\", key)?.value;\n\t}\n\n\tprivate setState(key: string, value: string): void {\n\t\tthis.sqlite.run(\n\t\t\t\"INSERT INTO context_compaction_state (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value\",\n\t\t\tkey,\n\t\t\tvalue,\n\t\t);\n\t}\n\n\tclose(): void {\n\t\tthis.sqlite.close();\n\t}\n}\n\nfunction createContextDeletionSqliteStore(transcript: CompactableTranscript): ContextDeletionSqliteStore {\n\tconst store = new ContextDeletionSqliteStore(SqliteAdapter.createTransient());\n\tstore.initialize(transcript);\n\treturn store;\n}\n\nexport function createContextDeletionTool(\n\ttranscript: CompactableTranscript,\n\toptions: ContextCompactionRunOptions = {},\n): ContextDeletionToolController {\n\tconst mode = options.mode ?? \"standard\";\n\tconst store = createContextDeletionSqliteStore(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\nexport function getLowestContextCompactionThinkingLevel(model: Model<Api>): ThinkingLevel {\n\tconst supportedLevels = getSupportedThinkingLevels(model) as ThinkingLevel[];\n\tfor (const level of CONTEXT_COMPACTION_THINKING_LEVEL_ORDER) {\n\t\tif (supportedLevels.includes(level)) return level;\n\t}\n\treturn \"off\";\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\tmode: ContextCompactionMode = \"standard\",\n): Promise<ContextDeletionRun> {\n\tconst maxTokens = Math.min(4096, 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\tconst effectiveThinkingLevel = getLowestContextCompactionThinkingLevel(model);\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: effectiveThinkingLevel,\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\t_thinkingLevel?: ThinkingLevel,\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\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"]}
|