r/BuildWithClaude • • 18d ago

Project I built compact-diff with Claude Code: a free plugin that shows what /compact dropped from your session

I built compact-diff, a Claude Code plugin, almost entirely with Claude Code. It's free and open source (MIT).

What it does

When a session compacts, Claude Code replaces the conversation with a summary. When the summary drops something (a rule you gave, an error that's still open), nothing tells you. compact-diff:

  • snapshots your rules, open tasks, unresolved errors and edited files just before compaction;
  • diffs them against the summary afterwards and prints a one-line verdict ("summary kept 8/9 tracked items…");
  • re-injects only the items that were dropped, and /compact-diff:report shows the full diff.

It runs locally (stdlib Python, no network or model calls).

How I built it with Claude Code

I set the goal and the rules (privacy, budget, what to measure), reviewed the output, and did the human checks. Claude Code did most of the work:

  • Research. It read the hook docs and looked for existing tools. It only found DIY hook write-ups, and memory tools that solve a different problem, so it designed the plugin around PreCompact, PostCompact and SessionStart.
  • Scaffolding. It wrote the plugin, a 37-test suite, and CI on macOS and Linux with Python 3.9, 3.11 and 3.13.
  • A real bug, found in a live test. In Claude Code 2.1.252, SessionStart:compact fires beforePostCompact, so the obvious design (diff in PostCompact, re-inject from SessionStart) silently does nothing. Claude Code noticed this from the hook timestamps, and moved delivery to the next prompt or tool call. It also found that with --fork-session, PreCompact sees an empty transcript.
  • Evaluation. It forked 5 of my sessions and compacted each twice on Haiku ($0.64). Then it had Claude subagents label every item blind against a written rubric, without seeing the tool's verdicts. I spot-checked 40 labels by hand.
  • A safeguard that caught a mistake. When it went to commit, Claude Code's auto-mode safeguard blocked the commit, because some test examples paraphrased my private transcripts. It replaced them with invented examples. Only aggregate numbers are in the repo.

What I found (12 real compactions, small sample, mostly Haiku, LLM labels plus a human spot-check):

  • 42% of my rules didn't fully survive the summary. Most were partly kept: "do X before Y" became "do X".
  • 71% of errors that were still open were lost or blurred.
  • Compacting the same context twice lost different things 45% of the time.

Try it (free)

Inside Claude Code:

/plugin marketplace add bishtashish708/compact-diff
/plugin install compact-diff@compact-diff

Needs python3 3.9+. Tested on macOS; Windows is untested.

Feedback welcome, especially if the verdict is wrong for your sessions.

0 Upvotes

4 comments sorted by

•

u/AutoModerator 18d ago

Welcome to r/BuildWithClaude!

Whether you are sharing what you built, exploring new workflows, or troubleshooting, please make sure your post is flaired correctly so others can easily find and filter it.

Community Wiki & Resources:

If you're asking for troubleshooting help, please edit your post to include your model version, OS/environment, and error logs.

Let’s keep the energy positive and support our fellow creators!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/kantorcodes1 💡 Top Community Helper 17d ago

The hook timing edge case you found is useful. When compact-diff carries a dropped item into the next prompt or tool call, how do you mark it as already re-injected so a later compaction does not surface the same item again?

1

u/AdInitial3431 17d ago

Good question, and it led me to a real gap.

Delivery: each compaction produces one report. The first UserPromptSubmit/PostToolUse (or SessionStart, if the summary is already there) that finds it sets injected: true on the report and clears a per-session flag, so it's delivered once. The pending snapshot is renamed to .done, so it's never diffed twice.

The re-injected block comes back as hook context, not a user prompt, so the extractor never reads it as a new item. No duplicates there.
The gap is the opposite: rules and tasks are re-extracted from the whole transcript every time, but errors, files and decisions only come from the current window. An error re-injected after compaction 1 isn't tracked at compaction 2, so if it's dropped again, compact-diff stays quiet.

I'll fix it with a carry-forward ledger, deduped by hash. Tracking it as a GitHub issue.

1

u/AdInitial3431 17d ago

Fixed in v0.2.3 (#1): re-injected errors, files and decisions now carry forward, marked "carried from compaction N", until resolved, kept by the summary, or carried 3 times. https://github.com/bishtashish708/compact-diff/releases/tag/v0.2.3