docs/claude-code-lost-context-after-compaction.md
Compaction replaces your conversation with a summary. The summary keeps the broad strokes and drops the working state: which phase you were in, which fixes were already applied, which approaches already failed. If Claude Code seems to have amnesia after /compact or an automatic compaction, that is what happened. This page explains why it happens, how to recover the current task, and how to make the next compaction a non-event.
The mechanism described here is planning-with-files, a skill that keeps the plan on disk in three markdown files and re-injects it into context every turn. Overview: README.
Because the plan lived only in the context window. Compaction, whether manual /compact or autoCompact when the window fills, summarizes the transcript to free space. Summaries compress, and exact phase status, error history, and decisions are the first details to go. Afterwards the model knows roughly what the task was, but not where you were in it.
The context window is volatile memory. Anything that exists only there is equally lost to /clear, crashes, and compaction. The durable fix is the same for all three: write the working state to disk and read it back mechanically.
For every complex task, the skill maintains three files in your project root:
task_plan.md → phases and checkboxes; the resume point
findings.md → research notes and decisions
progress.md → session log and test results
Plain markdown, gitignored by default. Because the files live on the filesystem and not in the transcript, compaction cannot touch them. Claude Code runs 5 lifecycle hooks around them: UserPromptSubmit, PreToolUse, PostToolUse, Stop, and PreCompact.
The skill registers a PreCompact hook with matcher *, so it fires on both manual /compact and autoCompact. When task_plan.md is present, the hook:
progress.md before compaction completesPlan-SHA256 when the plan is attested, so the post-compaction session can verify it resumes the approved planThe protection model is deliberate: the plan does not survive compaction unchanged inside the context. The plan is on disk, and it is re-read after compaction. On the next turn the UserPromptSubmit hook re-injects the current plan between ===BEGIN PLAN DATA=== and ===END PLAN DATA=== markers, so the compacted session starts anchored to the same phases.
If the planning files were on disk before the wipe, recovery is mechanical rather than conversational:
~/.claude/projects/ for Claude Code).git diff --stat, read the three files, update them, and continue.Scope note: session catchup replays transcript and points at the files; the durable phase state itself comes from reading task_plan.md. In the project's internal recovery benchmark (v1, author-run), a fresh session with the files on disk resumed in 5.0 turns on average against 13.3 for a raw agent. Method and disclosed limits: docs/evals.md.
Yes. Disable auto-compact in Claude Code settings and compact or /clear on your own schedule:
{ "autoCompact": false }
With the planning files on disk this is a safe default, because clearing stops being fatal.
Claude Code, plugin route (ships the skill, hooks, and slash commands):
/plugin marketplace add OthmanAdi/planning-with-files
/plugin install planning-with-files@planning-with-files
Every other agent, one line via the Agent Skills standard:
npx skills add OthmanAdi/planning-with-files --skill planning-with-files -g
Full route matrix and verification: README and docs/installation.md.