Require a Clean Git Status Before Compaction
Compaction doesn't touch files on disk, but it does summarize away the conversational record of *why* an uncommitted change exists. For a regulated repo, this forces a commit (or an explicit stash) first, so the durable git history — not just the soon-to-be-compacted transcript — captures the work.
1. Save this as the hook script
#!/usr/bin/env python3
"""PreCompact hook — blocks compaction if git status shows uncommitted
changes."""
import json, subprocess, sys
data = json.load(sys.stdin)
cwd = data.get("cwd", ".")
try:
result = subprocess.run(["git", "status", "--porcelain"], cwd=cwd,
capture_output=True, text=True, timeout=10)
except Exception:
sys.exit(0) # not a git repo or git unavailable — nothing to check
if result.stdout.strip():
print("BLOCKED: uncommitted changes exist. Commit or stash before "
"compaction summarizes away the context behind them.", file=sys.stderr)
sys.exit(2)
sys.exit(0)
Save to a path inside your project, e.g. .claude/hooks/require-clean-git-status-before-compaction.sh, then chmod +x it.
2. Wire it into .claude/settings.json
{
"hooks": {
"PreCompact": [
{
"hooks": [
{"type": "command", "command": "python3 .claude/hooks/require-clean-git-status-before-compaction.py"}
]
}
]
}
}
Before you use this
A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.
Related hooks