cleoanka
TR

tool · Claude Code

smart-compact

Decide what survives compaction in Claude Code: pin what must never be lost, mark what is certainly garbage, and have the rest judged against the current goal. It runs entirely in hooks, so marking costs no extra model call.

The problem

When a long session compacts, Claude Code replaces the history with a summary written by the model. You don't control what the summary keeps: a constraint you stated two hours ago may come back paraphrased or not at all, while the contents of a file you deleted may still take up room. The model marks a line at the moment it writes it, when it knows best whether the line matters; when a compaction starts, a hook reads the marks from the session transcript.

MarkMeaningWhat happens at compaction
◆ textPinSaved by the hook and put back verbatim after every compaction, native or smart.
◇ textDiscardThe line and whatever it names are listed for the summarizer as “leave out completely, no pointer”.
◆✗ fragmentUnpinRetires an earlier pin that contains the fragment.
(no mark)UndecidedJudged at compaction time against the current goal.

A mark is a line of a reply, or of your own message, that starts with the symbol. Tool calls, tool results, the output of ! shell commands, subagent text, thinking, quoted lines (> …) and fenced code blocks are not scanned, so a web page or a file cannot create a mark by merely being read.

Install

git clone https://github.com/cleoanka/smart-compact
cd smart-compact
python3 install.py --claude-md

Then start a new Claude Code session. The installer copies hooks/smart-compact.py into ~/.claude/hooks/, adds five hook entries to ~/.claude/settings.json after backing the file up, and writes seven small command files into ~/.claude/commands/; with --claude-md it appends the marking rule to ~/.claude/CLAUDE.md. The rule matters: without it the model does not know the marks exist.

Requirements: Python 3.9 or newer, and a Claude Code version with the PreCompact, PostCompact, SessionStart, UserPromptExpansion and Stop hooks. Developed and tested on Claude Code 2.1.325 on macOS. The hook script has no dependencies outside the standard library; --uninstall removes the hooks, commands and script.

The ◆ and ◇ rules

Once the rule is in CLAUDE.md, the model writes marks on its own. You can mark things yourself: start a line of your message with ◆ , ◇ , !pin or !drop , or use these commands. They are answered by the hook; they never reach the model and use no tokens.

/pinPin one line
/unpinRemove a pin by number or fragment
/dropMark something as worthless for the next compaction
/undropRemove a discard mark before it is used
/pinsList pins and pending discard marks
/smartcompactCompact now with the smart policy
/smartautocompactSmart auto-compaction for this session

A discard mark that names a pin, or is named by one, is ignored: protecting wins over discarding. To see a session's ◆/◇ layer next to its compactions, drop the transcript into the Flight Recorder.

What it adds

Where it loses

This section comes from the “What it does not do” and “What was tested” parts of the repository README, as of commit b2738c4.

Details, install options and tests: github.com/cleoanka/smart-compact.

For a team’s whole Claude Code setup, see Agent Ops: a fixed-price audit and setup.