Benjamin Meisl · Writing · 5.5

Self-compaction

July 2026 · systems write-up · source on GitHub

Context windows end. When a long Claude Code session fills its window, the harness compacts: the conversation is replaced by a summary, written by the same model, under pressure, about work it is in the middle of. What survives is a paraphrase. What dies is usually the load-bearing detail: the exact failing test output, the file paths, the decision made an hour ago for a reason nobody wrote down.

The failure, precisely

Two properties make auto-compaction dangerous. It fires at a point well below the nominal context window, and that point moves between harness builds, so you cannot plan around a number. And it fires mid-task by definition, because it triggers on occupancy rather than convenience. The session that resumes from the summary knows roughly what it was doing. Roughly is how a fixed bug gets re-broken.

Ledger first

The system closes the gap from both sides. A watermark hook watches real context occupancy against an effective ceiling learned from its own flight recorder — the record of where past compactions actually fired — rather than trusting the advertised window. As occupancy climbs through tiers, the hook nudges the model to checkpoint its working state into a small on-disk ledger: active task, acceptance criteria, exact paths and branch names, unresolved errors verbatim, decisions made. If the model never writes one, a recorder extracts a mechanical ledger from the transcript at compaction time anyway, clearly labeled as the lower-fidelity fallback.

After compaction, a rehydration hook injects the ledger straight back into the fresh context. The session resumes from its own checkpoint.

Letting the agent choose the moment

The piece that changes the character of the system: in tmux, the agent compacts itself. When it judges that a natural boundary has arrived — tests green, work committed, next phase not yet begun — it writes the ledger and signals a detached watcher, which types the compact command with a focus argument into the session's own pane, waits for the compaction to land, and types a continuation prompt. Nothing is in flight at that moment, and the ledger is thirty seconds old, written by an agent that knew exactly what its successor would need.

How it earned the trigger

The autonomy came second, and the ordering is the point. For weeks the system only nudged: the hooks watched occupancy, the model wrote ledgers, the harness compacted wherever it compacted, and I read the post-compact sessions to see how much state survived. The ledgers were consistently good. Sessions resumed cleanly, picked up mid-task, cited their own prior decisions. Only after watching that track record did I hand over the trigger and let the agent schedule its own compactions. I put a version of this on my resume because I think it generalizes: autonomy for agents should be priced in demonstrated capability, and the humans operating them should be the ones noticing when the price has been met.

The whole stack is open source: stdlib Python and shell, wired through the harness's hook system, with 215 checks across its test suites.