[Enhancement] Scoped compaction: compress exploratory context segments independently
Preflight Checklist
- [x] I have searched existing requests and this feature hasn't been requested yet
- [x] This is a single feature request (not multiple features)
Problem Statement
The current /compact command compresses the entire conversation history into a single summary. This works for linear sessions, but creates a lossy tradeoff during exploratory work.
A typical agentic session looks like this:
[system prompt]
[old messages] ~200k tokens
[exploration messages] ~100k tokens ← Phase 1: tried A, B, C → found answer
When /compact fires (automatically or manually), it flattens everything into a single summary — including the 100k exploration tokens that have already reached a clear conclusion. The result loses precise conclusions, variable names, edge cases, and reasoning that Phase 1 produced.
With scoped compaction, the agent calls compact_scope after reaching a conclusion, producing:
[system prompt]
[old messages] ~200k tokens ← untouched
[exploration conclusion] ~5k tokens ← dense, precise summary of Phase 1
Only the exploration region is compressed. The rest of the context is left intact.
This is distinct from subagents: the problem isn't about giving a child agent partial context — it's that the main agent's own context gets lossy compression applied to valuable conclusions.
Proposed Solution
Introduce the concept of a compaction scope — a marked region of context that can be independently compressed into a conclusion block, leaving the rest of the context intact.
Usage: agent-callable tool
The agent calls compact_scope when it reaches a conclusion — no need to plan the scope in advance:
# Claude autonomously compresses the exploration phase after reaching a conclusion
compact_scope(
summary="Root cause identified: race condition in token refresh at line 247. Fix: add mutex lock.",
discard_from=5
)
This is a retroactive operation: the agent decides after exploring that the exploration can be compressed. It specifies a starting turn — everything from that turn to the current message is discarded and replaced with the summary at the tail.
Key behaviors
- Only the marked region is compressed — surrounding context is untouched
- The injected conclusion block is denser and more precise than a global compact summary (because the scope is narrow and the conclusion is known)
- Claude can trigger this autonomously when it detects it has "concluded" an exploratory phase
- Compatible with existing auto-compact: scoped compaction runs first, reducing pressure on global compaction
Why this is different from existing features
| | Current /compact | Subagent | Scoped Compaction |
|---|---|---|---|
| Compresses | Whole history | N/A (separate context) | A defined region only |
| Triggered | Near context limit | Manually | At logical conclusion |
| Conclusion precision | Low (global summary) | Isolated | High (narrow scope) |
| Main agent state | Disrupted | Unaffected | Unaffected |
Analogy
Like git squash — the messy exploratory commits (turns) get squashed into one clean commit (conclusion block), while the rest of the branch history stays intact.
Prompt cache compatibility
Compressing the oldest exploratory segment (head of history) is also cache-optimal. Once compacted, the conclusion block becomes a stable cache prefix — all subsequent turns continue appending at the tail, fully compatible with Anthropic's prompt caching behavior:
[system prompt] ← cached ✓
[old messages] ← cached ✓
[exploration conclusion] ← new stable cache prefix ✓
[recent messages, intact] ← tail, appended normally ✓
Alternative Solutions
_No response_
Priority
Medium - Would be very helpful
Feature Category
Interactive mode (TUI)
Use Case Example
_No response_
Additional Context
_No response_
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗