Re-inject project context after compaction
Restores your project's key facts into Claude's context right after a compaction wipes the details.
SessionStartcompactlow riskneeds manual checkWhat it does
Fires on SessionStart with the compact matcher - the moment after context compaction. The script prints your standing context (stack, commands, conventions - edit the heredoc) to stdout, and SessionStart stdout is added to Claude's context. Official-guide pattern. Keep the output short; it rides along for the rest of the session.
When it runs
| Event | SessionStart |
| Matcher | compact |
| Scope | project |
| Platforms | macos, linux, windows |
| Timeout | 10s |
| Failure mode | observe |
| Verified against | Claude Code hooks reference, event/matcher schema checked 2026-09-10 |
| Test status | needs manual check - run the test steps below before relying on it |
Config
Merge this into .claude/settings.json - or use the generator to combine hooks.
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/reinject-context-compact.sh",
"timeout": 10
}
],
"matcher": "compact"
}
]
}
}Script
Save as .claude/hooks/reinject-context-compact.sh and chmod +x it.
#!/bin/bash
# reinject-context-compact: print standing context after compaction.
# SessionStart stdout is added to Claude's context - keep this SHORT.
cat <<'CTX'
Project context (re-injected after compaction):
- Stack: <your stack, e.g. Next.js 15 + Postgres>
- Test command: <your test command>
- Conventions: <3-5 bullets Claude keeps forgetting>
CTX
exit 0
Install
- Save the script to .claude/hooks/reinject-context-compact.sh and chmod +x it.
- Edit the heredoc: 5-10 lines of the context Claude always needs.
- Add the config fragment to .claude/settings.json.
Test it
- Positive: run /compact, then ask Claude a question that depends on the injected facts - it should answer without re-reading files.
- Negative: on a fresh session start (startup matcher), this hook must NOT fire - that is what the matcher is for.
Sources
- Claude Code hooks reference (checked 2026-09-10)
- Claude Code hooks guide (checked 2026-09-10)
Related hooks
Session-start repo snapshot
Hands Claude a bounded snapshot - branch, dirty state, recent commits - at every session start and resume.
SessionStartstartup|resumelow riskneeds manual checkPro Pack (later)
Framework bundles, cross-platform scripts, tested blocking policies, and a test harness. Free recipes and the generator stay free.
Waitlist opens soon.