claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[Feature Request] Enhanced /compact with file-backed summaries and selective restoration

Open mrgoonie opened this issue 10 months ago • 32 comments

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.

Image

Problem

Current /compact behavior:

  1. Happens on server side (after compaction, no summary conversation JSONL logs are written locally -> hard to debug)
  2. Summarized conversation context stored in-memory after server-side compaction process
  3. Original content is permanently lost from context
  4. No way to selectively restore specific messages if context becomes relevant again
  5. 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:

Image

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

Image

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 head and tail to 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

  1. Higher compression - 80%+ reduction vs current approach
  2. No data loss - Original content always recoverable
  3. Selective restoration - Bring back specific messages without full undo
  4. Audit trail - Archive provides history of what was compacted
  5. 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

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.

mrgoonie avatar Jan 11 '26 04:01 mrgoonie

upvote

namnhfreelancer avatar Jan 11 '26 14:01 namnhfreelancer

upvote for this request

thieung avatar Jan 11 '26 15:01 thieung

nice, upvote

blu1606 avatar Jan 11 '26 17:01 blu1606

Upvote. Nice approach

hieubui2409 avatar Jan 11 '26 18:01 hieubui2409

Nice, upvote

hieutv-dng avatar Jan 12 '26 08:01 hieutv-dng

nice, upvote

t0dorakis avatar Jan 14 '26 10:01 t0dorakis

Upvote. Nice approach.

pawagit avatar Jan 14 '26 11:01 pawagit

upvote for this approach

xnohat avatar Jan 14 '26 13:01 xnohat

Nice, upvote!

nhtera avatar Jan 14 '26 13:01 nhtera

upvote!

viksant avatar Jan 14 '26 19:01 viksant

nice, upvote!

rerangst avatar Jan 15 '26 01:01 rerangst

Up vote

TanNguyenNet avatar Jan 15 '26 01:01 TanNguyenNet

Upvote!

thaiduy avatar Jan 15 '26 01:01 thaiduy

Up vote

votanquyen avatar Jan 15 '26 01:01 votanquyen

upvote

luongkn1 avatar Jan 15 '26 01:01 luongkn1

Upvote!

lph86 avatar Jan 15 '26 02:01 lph86

upvote

loditcodon avatar Jan 15 '26 02:01 loditcodon

upvote

quocnv15 avatar Jan 15 '26 02:01 quocnv15

upvote

duongletsbd avatar Jan 15 '26 02:01 duongletsbd

upvote.

jaynguyen-vn avatar Jan 15 '26 02:01 jaynguyen-vn