Context Re-injection on Compaction Hook
Automatically re-injects critical project context when Claude Code compacts its context window. Prevents loss of important instructions like build tool preferences, testing requirements, and current sprint goals after long conversations.
Hook Type
SessionStart with compact matcher -- Fires after context window compaction.
Description
During long Claude Code sessions, the context window gets compacted to free up space. This can cause Claude to "forget" important project-specific instructions (like which package manager to use, test requirements, or current sprint focus). This hook re-injects those critical reminders after every compaction.
Patterns/Rules
- Triggers on
SessionStartevents that match thecompactpattern - Outputs plain text that gets injected into Claude's context
- Keep the reminder concise -- every token counts
- Focus on instructions that Claude frequently forgets after compaction
- Use a file-based approach for easy editing without touching settings.json
Configuration
Simple Inline Version
{ "hooks": { "SessionStart": [ { "matcher": "compact", "hooks": [ { "type": "command", "command": "echo 'CRITICAL REMINDERS: Use pnpm (not npm). Run pnpm test before committing. Current sprint: auth-v2 refactor. Branch prefix: feat/ for features, fix/ for bugs.'" } ] } ] } }
File-Based Version (Recommended)
{ "hooks": { "SessionStart": [ { "matcher": "compact", "hooks": [ { "type": "command", "command": "cat .claude/compaction-context.md 2>/dev/null || echo 'No compaction context file found.'" } ] } ] } }
Then create .claude/compaction-context.md:
# Post-Compaction Context ## Build & Package Manager - Use pnpm, NEVER npm or yarn - Run `pnpm test` before every commit - Run `pnpm lint` before creating PRs ## Current Sprint - Sprint: Auth V2 Refactor (ends 2025-04-01) - Focus: Migrate from JWT to session-based auth - Key files: src/auth/, src/middleware/session.ts ## Branch Conventions - Features: `feat/description` - Bugfixes: `fix/description` - Always branch from `develop`, never from `main` ## Testing Requirements - Unit tests required for all new functions - Integration tests required for API endpoints - Minimum 80% coverage on changed files
Action
After context compaction:
- The SessionStart event fires with the
compactmatcher - The hook reads and outputs the compaction context file
- Claude receives the reminders as fresh context
- Project-specific rules are preserved across compaction boundaries
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Pre-Commit Security Scanner
Pre-commit hook that scans staged files for hardcoded secrets, API keys, passwords, and sensitive data patterns before allowing commits.
Agents Md Watcher
Streamline your workflow with this automatically, loads, agents, configuration. Includes structured workflows, validation checks, and reusable patterns for automation.
Automated Build Inspector
Boost productivity using this automatically, trigger, build, processes. Includes structured workflows, validation checks, and reusable patterns for automation.