PreCompact hook: allow decision control to clear instead of compact

Resolved 💬 3 comments Opened Mar 7, 2026 by ziezolddev-web Closed Apr 30, 2026

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:

  1. Auto-compaction produces a lossy summary — the agent continues with degraded context, which can cause confusion, repeated mistakes, or lost task state
  2. /clear provides a clean slate — combined with a well-structured CONTINUE.md file, 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

  • PreCompact hook commits + writes CONTINUE.md (mechanical, no AI summary)
  • SessionStart hook (matcher: compact) surfaces CONTINUE.md on resume
  • Rules instruct the agent to proactively /clear at 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).

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗