Auto-save session state before compaction for seamless resume
Feature Request
Problem
When running long tasks (especially multi-phase workflows like GSD), context fills up and auto-compaction loses critical state. Users must manually /clear and re-establish context, which is error-prone and breaks flow. There's no built-in mechanism to preserve session state across compaction or clear boundaries.
Current Workarounds
I built a custom hook system to address this:
- PreCompact hook that auto-saves state to a handoff file before compaction
- PostToolUse context monitor that detects low context (35% remaining) and proactively saves state
- StatusLine bridge that shares
context_window.remaining_percentagebetween hooks
This works but requires ~200 lines of custom JavaScript across 3 hook files plus settings configuration. Every user doing long-running tasks faces this same problem.
Proposed Solution
Built-in context state preservation, ideally with three levels:
- Auto-save session state before compaction (minimum viable):
- When PreCompact fires for
autotrigger, Claude Code writes a structured state file (modified files, recent actions, current task, key decisions) - SessionStart with
compactmatcher automatically loads this state - No user intervention needed — compaction becomes seamless
- Proactive state snapshots at configurable thresholds (better):
- New setting:
contextSaveThreshold: 35(percentage remaining) - When threshold is hit, state file is written automatically
- Agent receives context about the save so it can wrap up gracefully
- Auto-clear-and-resume (ideal):
- When context hits a threshold, Claude Code automatically: saves state → clears → resumes with state loaded
- The user experiences a seamless continuation with a brief "Context refreshed" notification
- This is the equivalent of what IDEs do with memory management — invisible to the user
Additional Context
- Auto-compaction triggers at ~83.5% usage (16.5% remaining / ~33K buffer)
- Research shows quality degrades well before that threshold (context rot)
- Community consensus is to compact at 70% usage, but most users don't monitor this
- The PreCompact hook exists but has no decision control — it can only do side effects
- Hooks cannot inject
/clearor/compact(noupdatedPromptfield on UserPromptSubmit) - The StatusLine is the only mechanism that receives
context_window.remaining_percentage
Key Insight
The pieces are already there (PreCompact hook, SessionStart compact matcher, StatusLine metrics) but they're not connected into a cohesive auto-save-and-resume flow. Making this built-in would benefit every user doing tasks longer than a single context window.
Environment
- Claude Code on macOS (Apple Silicon)
- Using GSD workflow framework for multi-phase projects
- Typical sessions fill context in 30-60 minutes of active work
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗