PreCompact hook: allow decision control to clear instead of compact
Summary
The PreCompact hook currently supports side effects only (no decision control). It would be valuable to allow the hook to respond with a decision that triggers a /clear instead of proceeding with lossy auto-compaction.
Use Case
We use Claude Code for long autonomous coding sessions (overnight Docker sprints, multi-hour interactive sessions). When context fills up:
- Auto-compaction produces a lossy summary — the agent continues with degraded context, which can cause confusion, repeated mistakes, or lost task state
/clearprovides a clean slate — combined with a well-structuredCONTINUE.mdfile, the agent restarts with focused, accurate context
We've built a PreCompact hook that commits work and writes a CONTINUE.md before compaction. But we can't redirect the compaction to a clear, which would produce better outcomes.
Proposed Behavior
Allow PreCompact hooks to return a decision, similar to how PreToolUse hooks can return {"decision": "block"}:
{
"decision": "clear",
"reason": "Clearing context — state saved to CONTINUE.md"
}
| Decision | Behavior |
|----------|----------|
| (none / empty) | Proceed with compaction as today (backward compatible) |
| "clear" | Cancel compaction, run /clear instead |
This way the hook script can: commit all work → write CONTINUE.md → return {"decision": "clear"} → agent starts fresh and reads CONTINUE.md on resume.
Additional Request: Context Threshold Event
A complementary feature would be a hookable event that fires at a configurable context usage percentage (e.g., 50%, 60%) — before compaction is imminent. This would allow proactive state-saving while the agent still has full reasoning capacity, rather than racing against compaction.
Current Workaround
PreCompacthook commits + writes CONTINUE.md (mechanical, no AI summary)SessionStarthook (matcher:compact) surfaces CONTINUE.md on resume- Rules instruct the agent to proactively
/clearat 60% budget — but this depends on agent self-awareness and isn't enforced
The workaround covers the failure case but can't achieve the optimal path (clear > compact).
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗