#!/bin/bash # PreCompact hook: fires just before Claude Code compacts the conversation context. # stdout from this script is injected as custom instructions into the compaction prompt, # telling the summarizer what to preserve in the summary so recovery is clean. COMPACT_STATE_FILE="$HOME/.claude/compact-state.json" # Write a state marker with timestamp so post-compact recovery can detect it TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ") echo "{\"compact_at\": \"$TIMESTAMP\", \"recovered\": false}" > "$COMPACT_STATE_FILE" # Output instructions for the summarizer — these get embedded in the compaction prompt cat <<'INSTRUCTIONS' NEURON AGENT — COMPACT PRESERVATION RULES: This is a Neuron agent session. The agent has persistent memory via Neuron MCP. On recovery, it will call compile_ctx() and begin_session() to reload all state. The summary only needs to cover what Neuron doesn't already have. CRITICAL — the summary MUST preserve ALL of the following: 1. ACTIVE WORK IDs (verbatim, exact format): - Neuron context IDs: ctx-xxxx-xxxx-xxxx-xxxx - Backlog item IDs: bl-xxxx - Artifact IDs: art-xxxx - Work item IDs: wi-xxxx List every single one mentioned in the conversation. 2. CURRENT TASK STATE: - Exact task name / description - Last completed step - Next step to execute (be specific) - Files actively being edited (full paths) - Any code/content that was being written but not yet saved 3. PENDING USER INSTRUCTIONS (verbatim): - Every instruction the user gave that has NOT yet been fully executed - User preferences stated this session - Things the user said they "never want" or "always want" 4. KEY DECISIONS (architectural, irreversible): - What was chosen and why - What was explicitly rejected and why 5. ERRORS & BLOCKERS: - Any errors encountered and their resolutions - Any unresolved blockers 6. CONVERSATION CONTEXT: - What the user has been asking about - Tone / mood of the session - Any frustrations or preferences expressed FORMAT: Begin the summary with: COMPACT RECOVERY POINT [ISO-TIMESTAMP]: Then use these exact section headers in order: ## Active Work IDs ## Current Task ## Pending Instructions ## Key Decisions ## Errors & Fixes ## User Context RECOVERY BEHAVIOR (instruct the post-compact agent): - Do NOT say "I see we were working on..." or "Based on the previous conversation..." - Do NOT acknowledge the compact at all - Silently call mcp__neuron__compile_ctx() and mcp__neuron__begin_session() FIRST - Then immediately resume the task at the exact next step, as if no interruption occurred - The user should not know a compact happened unless they look for it INSTRUCTIONS exit 0