Automatic context preservation for Claude Code sessions, including /clear transitions.
Long-running sessions degrade after repeated context compactions. This plugin preserves critical context and restores it after both compact and clear lifecycle transitions.
This project now has two layers:
- Hook layer (always-on)
PreCompact-> captures context before auto-compact or/compactSessionEnd(clear)-> captures context before/cleartears down sessionSessionStart(compact|clear)-> restores context asadditionalContext
- Supervisor layer (optional, recommended)
claude-handoff-supervisor.pylaunches Claude and rewrites manual/compactto/clear- This avoids waiting on manual compaction, which hooks cannot rewrite by themselves
auto-compact or /compact
-> PreCompact hook writes handoff
-> SessionStart(compact) restores handoff
/clear
-> SessionEnd(clear) writes handoff (and latest pointer)
-> SessionStart(clear) restores handoff
recommended manual path:
use supervisor
user types /compact
supervisor rewrites to /clear
clear transition hooks handle capture+restore
git clone https://github.com/who96/claude-code-context-handoff.git
cd claude-code-context-handoff
./install.shInstaller actions:
- Copies scripts to
/Users/<you>/.claude/hooks/ - Backs up
/Users/<you>/.claude/settings.json - Registers
PreCompact,SessionEnd(clear),SessionStart(compact|clear)hooks - Creates
/Users/<you>/.claude/handoff/
Restart Claude Code after install.
~/.claude/hooks/claude-handoff-supervisor.pyThis gives command rewrite:
/compact->/clear
If you want plain passthrough:
~/.claude/hooks/claude-handoff-supervisor.py --no-rewriteGenerated artifacts:
~/.claude/handoff/<session_id>.md~/.claude/handoff/latest-handoff.md~/.claude/handoff/latest-handoff.json
latest-handoff.md is used as fallback restore source for SessionStart(clear) when the new session id has no direct file yet.
Fallback safety guards:
- same
cwd(if both sides provide it) - max age window (
HANDOFF_LATEST_MAX_AGE_SEC, default900)
- Last 15 user messages (deduplicated, 85% threshold)
- Last 10 assistant snippets (junk filtered, truncated)
- File paths extracted from tool input (
file_path/path) - Command-like strings are filtered out from path extraction
Tune via env vars:
export HANDOFF_MAX_USER_MESSAGES=20
export HANDOFF_MAX_ASSISTANT_CHARS=1000
export HANDOFF_DEDUP_THRESHOLD=0.90
export HANDOFF_LATEST_MAX_AGE_SEC=900Run:
./test.shQuick manual verification:
/clear
ls -la ~/.claude/handoff/- Hooks cannot rewrite slash commands directly.
- Automatic replacement of
/compactrequires the external supervisor. - Auto-compact itself is still controlled by Claude Code; this plugin only preserves/recovers context around it.
Supervisor argument passthrough supports direct Claude flags:
~/.claude/hooks/claude-handoff-supervisor.py --model sonnet --debug./uninstall.shMIT. See /Users/huluobo/workSpace/claude-code-context-handoff/LICENSE.