OSS · Claude Code Plugin

compact-ops

After /compact, your agent forgets what it was doing.

compact-ops writes a 10-heading structured state file before every compaction and injects it back right after compact and on claude --resume. Official hooks only, every hook fail-open. Install it once, then just /compact as usual.

It does not replace the summary. It insures it from the outside.

Get it on GitHub How it works →

Free · MIT · official hooks only · nothing leaves your machine except your own claude CLI call

  • 10 fixed headings per state file
  • 60% usage warning, threshold configurable
  • 72h resume fallback window
  • 30d state retention on disk
  • No post-compact derailing the agent resumes on summary + state file, two cues instead of one
  • No surprise auto-compact one warning at 60% with a 3-line recitation of plan, phase, latest decision
  • Survives a PC reboot claude --resume re-injects the latest state within 72 hours
  • Breaks nothing when it breaks every hook is fail-open; standard compaction always goes through
Terminal demo: real event names from hooks.json, a live run of the PreCompact backup hook with an actual byte-count reduction, and the 10-heading structure of a real state file

Four hooks, no daemon

Every one of these is an official Claude Code hook event. No background process, no polling.

  • PreCompact Backs up the transcript (gzip) and has an LLM write the 10-heading state file
  • PostCompact Resets the usage-warning cooldown
  • SessionStart compact | resume matcher: injects the state file and a reread-the-originals note
  • UserPromptSubmit Computes context usage from the transcript; warns once past the threshold

All four are fail-open. If every one of them fails, compaction still runs exactly as if compact-ops were never installed.

Three steps

  1. claude plugin install compact-ops Clone the repo, add it as a local marketplace, install once.
  2. /compact Compact as usual. PreCompact backs up the transcript and writes the state file automatically.
  3. recovery injected The fresh context receives the state file, a reread-the-originals note, and the invoked-skills list.

Who it’s for

  • Heavy Claude Code users whose sessions routinely cross one or more compactions per day
  • Agent operators running long or scheduled sessions and picking them up next day with claude --resume
  • Teams handing sessions over the state file is plain Markdown a human can read as a handoff note
  • Hook authors a worked example of a plugin built on official hooks only, fail-open throughout

When to reach for it

  • When auto-compact has eaten a decision mid-task more than once: the 60% warning lets you pick the moment yourself
  • Before multi-day work: the state survives compaction, session close, and a PC reboot
  • When the agent re-asks for an approval it already had, or retries an approach that already failed: those are exactly the fixed headings in the state file
  • If you have ever told a post-compact agent "we already decided this", that is the incident this plugin exists for

Install

git clone https://github.com/kenimo49/compact-ops.git
claude plugin marketplace add /path/to/compact-ops --scope user
claude plugin install compact-ops@compact-ops-local

Requires Claude Code v2.x, jq, and the claude CLI as the LLM backend. Linux and macOS supported. After installing, just run /compact as usual; the hooks handle the rest.

What it does

  • PreCompact: backs up the transcript (gzip, JSONL shrinks to about 1/10) and has an LLM write a 10-heading state file: active plan, decisions, blockers, editing files, failed attempts
  • Right after compaction: injects the state file, a reread-the-originals note, and the list of invoked skills into the fresh context
  • On claude --resume: the same recovery injection, with a fallback to the project’s latest state within 72 hours, surviving a PC reboot
  • Context-usage warning computed by the plugin itself (default 60%): recommends /compact once, with a 3-line recitation of plan, phase, and latest decision
  • State persists under ~/.claude/compact-ops/ with 30-day retention, organized per project; session_id is allowlist-validated before it touches a file path
  • The LLM output is validated for all 10 headings before writing; invalid output keeps the previous state and fails open
  • Derived from u-ichi/compact-plus (MIT), made self-contained: no external statusline scripts, Claude-only LLM backends (Sonnet primary, Haiku fallback)

With and without compact-ops

Same compaction algorithm, same built-in summary. The difference is everything around it.

MomentPlain Claude CodeWith compact-ops
Usage passes 60% No warning. Auto-compact arrives unannounced One notification + a 3-line recitation of plan, phase, latest decision
The compact itself Built-in prompt writes the summary; content not controllable Same compression, plus a transcript backup and a 10-heading state file
Right after compact The agent continues on the summary alone State file + reread-the-originals note injected into the fresh context
After the session closes The summary exists only inside that session State stays on disk 30 days and is injected on claude --resume
When a hook fails Fail-open: standard compaction goes through untouched

Source: the project README. The compression algorithm itself is untouched; everything is official hooks bolted on from the outside.

What the state file looks like

Terminal screenshot: the 10 headings of a real compact-ops state file, from Active Plan and Session Decisions to Failed Attempts and Recovery Notes
A real state file, headings only (the body holds the conversation, so it stays private). This very page was revised across two compactions, resumed from files like this one.

Usage

# just compact as usual — hooks handle the rest
/compact

# arguments become priority guidance for the state writer
/compact keep every design decision

# tune via the env block in ~/.claude/settings.json
COMPACT_OPS_WARN_THRESHOLD=70
COMPACT_OPS_PRIMARY_BACKEND=""
COMPACT_OPS_DEBUG=1

Field-tested on its own release

The first beneficiary was the session that built it. Three moments from real use, not staged demos:

  • During the v0.2.0 release work, the plugin’s own 60% warning fired at 67% inside the very session doing the release: the author wrote the warning and was saved by it the same night.
  • Minutes later, a directory restructure broke three of its hooks mid-session. Compaction still completed untouched, because every hook is fail-open: the failure mode was tested in production by accident.
  • This page you are reading was revised in a session that crossed two compactions, each time resuming from the state file it describes.

The release-day log, including the 67% warning and the triple hook failure, is written up in full on the project blog.

How it works →

Costs to know: each compact adds one LLM call for state generation (default Sonnet; switch to Haiku or disable via env). State files and backups contain the conversation verbatim; they are created with umask 077 (directories 700, files 600), stay on your machine, and are auto-deleted after 30 days.

Why this plugin exists

The built-in summary is decent at code. What it drops are operational facts: "the push was already approved", "this approach was tried and failed", "that number came from this file". When those vanish, the agent re-asks for approvals and retries dead ends, and you pay for the same work twice. compact-ops gives those facts their own fixed headings and stores them outside the summary. One design line is non-negotiable: the state file is never treated as more authoritative than your project files. An LLM summary trusted blindly by another LLM is just a faster game of telephone, so the recovery guidance always points back to the originals.

Alongside compact-plus and plain Claude Code

compact-ops is a derivative of u-ichi/compact-plus (MIT), which pioneered the write-structured-state-before-compact idea. The differences below are the reasons the derivative exists.

compact-opscompact-plusPlain Claude Code
Usage warning Self-computed by the plugin, no extra setup Depends on a statusline script from a separate repo None; auto-compact is unannounced
State location ~/.claude/compact-ops/, 30-day retention, per project $TMPDIR (lost on reboot) Summary lives only inside the session
Recovery on --resume ✓ with a 72h same-project fallback
Injection after compact Direct, via the SessionStart compact matcher Marker file, then the next UserPromptSubmit (two hooks)
LLM backend Sonnet primary, Haiku fallback: Claude only Sonnet primary, Codex fallback (ChatGPT Pro assumed)
License MIT MIT

The core idea, a PreCompact hook calling a second LLM to write structured state, comes from compact-plus. The table lists the README-documented differences as of July 2026; if its assumptions match your setup, compact-plus is a fine choice.

FAQ

What does it cost per compact?

One extra LLM call for state generation (default Sonnet, falling back to Haiku). You can point COMPACT_OPS_PRIMARY_BACKEND at Haiku to make it cheaper, or set it empty to disable state generation entirely while keeping the other hooks.

Does it change the built-in summary?

No. The compression algorithm and the summary are untouched; everything happens in official hooks around them. Post-compact the agent simply has two cues, the summary and the state file, instead of one.

Where does my conversation data go?

State files and backups stay under ~/.claude/compact-ops/ on your machine, created with umask 077 and auto-deleted after 30 days. The only network call is the state-generation request through your own claude CLI, the same channel your session already uses.

Does it work with auto-compact too?

Yes. PreCompact fires for manual and automatic compaction alike, so the backup and state file are always written. One known limit: the usage warning is computed on UserPromptSubmit, so if a single turn burns enough context to trigger auto-compact before your next message, the warning cannot arrive first.

Should the agent trust the state file?

Only as a map, never as the territory. The injected guidance explicitly says the state is less authoritative than project files, plans, and skills, and tells the agent to reread the originals before acting on it.

How is this different from compact-plus?

Three changes: the usage warning is self-contained instead of depending on an external statusline script, state persists 30 days under ~/.claude/ instead of $TMPDIR, and claude --resume also gets recovery injection with a 72-hour fallback. Backends are Claude-only. The comparison table above has the full list.

Why not improve the summary prompt itself?

Because it is not yours to improve: the built-in compaction prompt is not exposed. Official hooks are the supported surface, and staying on them means the plugin keeps working across Claude Code updates and can afford to be fail-open.

Does it work outside Claude Code?

No. It is built on Claude Code v2.x hook events (PreCompact, SessionStart, UserPromptSubmit) and assumes the claude CLI as its backend. Other agent CLIs have different hook surfaces.

About the author

Built by Ken Imoto: 300+ technical articles across Zenn, Qiita, Dev.to and this site, 40+ books in 4 languages, 400K+ pageviews on Zenn and Qiita, 4 research papers on Zenodo, and creator of the LLMO Framework.

Related developer tools

All products →

← All products