agentleFS
mkarami313mkarami313/Claude-skill/skills/compaction-guard/SKILL.md

compaction-guard

Skill0 starsChanged 13 days agoMITView on GitHub

Keep goals, user instructions, decisions and next steps alive across Claude Code context compaction (auto-compact or /compact) and long sessions. Use when a session is long or will be, when the user complains that Claude forgot instructions or lost the thread after compacting, when starting a big multi-step task, or to install the PreCompact/SessionStart hooks that snapshot and re-inject state.

The file

We split this skill into the rules it gives an agent

9 rules. About 0 are common, 7 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈7

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    when the user gives an instruction, preference or correction that should outlive this turn.

    line 36
  2. Distinctive:

    after a decision, after finishing a step, before starting a long command or a subagent batch.

    line 37
  3. Distinctive:

    when the conversation is getting long (many large tool results) — compaction is near.

    line 38
  4. Distinctive:

    Don't put tool output, code, or secrets in it.

    line 39
  5. Very specific:

    When context starts with [compaction-guard] Context was just compacted:

    line 42
  6. Distinctive:

    Read the injected NOW.md and snapshot before doing anything else.

    line 43
  7. Distinctive:

    If the snapshot's latest user messages contain something NOW.md doesn't (a new instruction, a changed goal), update NOW.md first.

    line 44
  8. Distinctive:

    If anything is ambiguous (a half-done edit, an unclear next step), re-check the file or git diff — don't guess from the summary.

    line 45
  9. Very specific:

    If the user doesn't want hooks: still keep NOW.md, and after a compaction (the conversation starts with a summary) read it yourself first.

    line 49

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Compaction guard

Compaction summarizes the conversation and throws away the rest. What gets lost is predictable: the user's exact instructions, why decisions were made, which files were mid-change, and what was next. This skill keeps that in a small state file and re-injects it automatically after every compaction.

Three layers, cheapest first:

  1. NOW.md — a living state file you keep current (the part that actually matters).
  2. PreCompact hook — writes an automatic snapshot from the transcript and tells the compactor what to keep.
  3. SessionStart(compact) hook — puts NOW.md + the snapshot back into context right after compaction.

Install the hooks (once per project, or --user for all projects)

python3 <skill-dir>/scripts/guard.py install — copies guard.py to .claude/compaction-guard/, merges the hooks into .claude/settings.json (keeps existing hooks, writes a .bak), and is safe to run twice. --on-clear also re-injects after /clear; uninstall removes only its own hooks. Hooks load at session start: tell the user to restart Claude Code (or check with /hooks). Add .claude/compaction-guard/ to .gitignore unless the team wants to share it.

guard.py status shows exactly what would be re-injected and its size.

NOW.md — the state file

Path: .claude/compaction-guard/NOW.md. Current state, not a log. ≤60 lines (re-injection is capped at ~6,000 chars; Claude Code cuts SessionStart context past ~10,000).

# NOW — <task in a few words>
Goal: <what the user wants, in their words>
Constraints (user's explicit instructions — verbatim):
- <"don't touch the public API">
- <"answer me in Persian, code in English">
Decisions:
- <decision> — because <reason>
Done: <short list>
In progress: <file:area — what's half-changed>
Next: <the very next concrete step>, then <…>
Open questions / blocked on: <…>

Update it (Edit, a few lines — never rewrite from memory):

  • when the user gives an instruction, preference or correction that should outlive this turn;
  • after a decision, after finishing a step, before starting a long command or a subagent batch;
  • when the conversation is getting long (many large tool results) — compaction is near. Don't put tool output, code, or secrets in it.

After compaction

When context starts with [compaction-guard] Context was just compacted:

  1. Read the injected NOW.md and snapshot before doing anything else.
  2. If the snapshot's latest user messages contain something NOW.md doesn't (a new instruction, a changed goal), update NOW.md first.
  3. If anything is ambiguous (a half-done edit, an unclear next step), re-check the file or git diff — don't guess from the summary.
  4. Continue from Next. Tell the user one line only if something was unclear and you had to re-check.

Without hooks

If the user doesn't want hooks: still keep NOW.md, and after a compaction (the conversation starts with a summary) read it yourself first. For a manual /compact, suggest instructions such as /compact keep the constraints and next steps from NOW.md.

Hook behaviour (for debugging)

  • precompact: reads transcript_path from the hook input, writes snapshot.md (first request, last 5 user messages, files changed, recent tool failures, last assistant notes — secrets redacted), prints ≤1,500 chars of instructions that Claude Code appends to the compaction prompt. Never exits 2 (that would block compaction); any error → exit 0.
  • sessionstart: prints hookSpecificOutput.additionalContext with NOW.md (≤6,000 chars) + snapshot (≤3,000).

When NOT to use

Short sessions, one-off questions, or when the user prefers /clear and a fresh start (then NOW.md is still a good handoff note).

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.