C

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.

HookAnthropicautomationv1.0.0MIT
0 views0 copies

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 SessionStart events that match the compact pattern
  • 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.'" } ] } ] } }
{ "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:

  1. The SessionStart event fires with the compact matcher
  2. The hook reads and outputs the compaction context file
  3. Claude receives the reminders as fresh context
  4. Project-specific rules are preserved across compaction boundaries
Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates