[Feature Request] Enhanced /compact with file-backed summaries and selective restoration
Summary
Propose enhancing the /compact command to use a file-backed summary approach inspired by Cursor's Dynamic Context Discovery.
Instead of just summarizing content in memory, compact would write original content to local files and replace context with summaries + file references, enabling selective restoration when needed.
Problem
Current /compact behavior:
- Happens on server side (after compaction, no summary conversation JSONL logs are written locally -> hard to debug)
- Summarized conversation context stored in-memory after server-side compaction process
- Original content is permanently lost from context
- No way to selectively restore specific messages if context becomes relevant again
- Binary choice: full context OR summarized (no middle ground)
When working on long sessions, users often need to reference earlier decisions or code that was compacted away. Currently, the only option is to manually search through JSONL files in
~/.claude/projects/.
I'm totally aware of there is already "Context editing" from Anthropic:
But that's not enough and I think it can be enhanced a little bit more.
Proposed Solution
Adopt a file-backed compaction approach similar to Cursor's dynamic context discovery:
1. Write-to-File Before Compacting
~/.claude/projects/<project>/
├── <session>.jsonl # Active session (compacted summaries)
├── <session>.jsonl.bak # Full pre-compaction backup
└── messages/
├── <uuid-1>.txt # Original message content
├── <uuid-2>.txt # Original tool result
└── archive.jsonl # Metadata index
2. Message/Tool Call Summary + Reference Format
After compaction, each message or tool call in the context would contain:
[Summary of the message / tool call]
[Full message: ./compacted/a1b2c3d4-detailed-message-or-tool-call.txt]
3. Selective Restoration & Post-Compaction
After compaction, rewrite the terminal view to display compacted summaries, so we (and claude) will know exactly what happened and see each compacted message with reference files to look back again.
Here is what happens
Testing & Results
We built a prototype implementation and tested on real sessions:
| Session | Before | After | Token Reduction |
|---|---|---|---|
| 4.6MB session | 277,790 tokens | 46,379 tokens | 83.3% |
| 900KB session | 47,331 tokens | 9,692 tokens | 79.5% |
What Gets Compacted
Analysis showed the bulk of context isn't assistant text - it's tool results:
| Content Type | % of Session |
|---|---|
| Tool results (file reads, grep) | 68% |
| Tool use inputs | 23% |
| User text | 6% |
| Assistant text | 3% |
By compacting tool results (which are re-fetchable) alongside text, we achieve much higher reduction than text-only compaction.
Key Insight from Cursor
From their article:
"Rather than truncating long tool responses, agents now write them to files. Agents can then use commands like
headandtailto selectively read what's needed."
This same principle applies to compaction - write full content to files, keep summaries in context, let agents fetch originals when needed.
Benefits
- Higher compression - 80%+ reduction vs current approach
- No data loss - Original content always recoverable
- Selective restoration - Bring back specific messages without full undo
- Audit trail - Archive provides history of what was compacted
- Agent-friendly - Agents can grep/read compacted files if needed
Implementation Considerations
- Backup strategy: Dual backup (full
.bak+ individual files) for safety - Atomic writes: Use temp-file-then-rename to prevent corruption
- Storage: Individual files allow granular cleanup if needed
- Compatibility: JSONL structure preserved, just content replaced
References
- Inspiration: Cursor - Dynamic Context Discovery
- Related: #5135 (request for
--include-historyflag)
Prototype
We've implemented a working prototype as a skill/script that demonstrates this approach. Happy to share code or collaborate on integration if there's interest.
upvote
upvote for this request
nice, upvote
Upvote. Nice approach
Nice, upvote
nice, upvote
Upvote. Nice approach.
upvote for this approach
Nice, upvote!
upvote!
nice, upvote!
Up vote
Upvote!
Up vote
upvote
Upvote!
upvote
upvote
upvote
upvote.