Feature request: PreCompact hook event
Summary
Add a PreCompact hook event that fires before context compaction, complementing the existing PostCompact hook.
Problem
Currently, PostCompact fires after the context window has already been compressed. At that point, conversation state (in-progress file changes, task context, debugging state) is already lost. The hook can only remind the model to continue — it cannot preserve what was being worked on.
Proposed solution
Add a PreCompact hook event with the same interface as other hooks:
{
"PreCompact": [
{
"hooks": [
{
"type": "command",
"command": "~/.claude/hooks/pre-compact.sh",
"timeout": 10
}
]
}
]
}
The hook would fire when compaction is triggered (e.g., at CLAUDE_AUTOCOMPACT_PCT_OVERRIDE threshold) but before the context is actually compressed.
Use cases
- State snapshot — save current working state (modified files, branch, version, active tasks) to a temp file that PostCompact can then inject back
- Auto-commit — commit work-in-progress before context loss
- Task persistence — write in-progress task summaries to disk
- Logging/observability — track compaction frequency and what gets lost
Return value
The hook could optionally return a systemMessage (like PostCompact) that gets included in the compacted context, giving users control over what survives compaction.
Current workaround
Using PostCompact to reconstruct state from git (git diff, git log, CHANGELOG parsing). This works for committed state but cannot recover uncommitted conversation context like "what was I debugging" or "which approach did I just rule out."
Environment
- Claude Code CLI on macOS
- Using
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE: 75
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗