Feature Request: Add post_compact Hook for Policy Re-injection After Context Compaction
Summary
Add a new post_compact hook that fires after context compaction completes, allowing users to re-inject critical execution policies, development protocols, and project-specific configurations that get lost during compaction summarization.
Problem
Context compaction creates a policy drift problem:
- Custom rules get summarized away: Project CLAUDE.md files, development protocols, agent configurations get compressed during compaction
- No way to restore policies: Current hooks (
session_start,pre_compact) cannot inject into the post-compaction context - Behavioral degradation: After compaction, Claude may not consistently follow project-specific protocols, invoke configured agents, or adhere to development standards
Current Hooks vs. Needed Hook
| Hook | Timing | Can Inject Post-Compact? |
|------|--------|--------------------------|
| session_start | New conversation | ✅ (but compaction is mid-session) |
| pre_compact | Before compaction | ❌ (context wiped after) |
| post_compact | After compaction | ✅ Needed! |
Proposed Solution
Hook: post_compact
When it fires: Immediately after context compaction completes.
Input (stdin JSON):
{
"compaction": {
"tokens_before": 180000,
"tokens_after": 45000,
"messages_before": 87,
"messages_after": 12,
"timestamp": "2025-12-26T10:30:00Z"
},
"project_dir": "/path/to/project"
}
Output: stderr text injected into new context
Example:
#!/usr/bin/env bash
PROJECT_DIR="${CLAUDE_PROJECT_DIR:-$(pwd)}"
echo "=== POLICIES RESTORED ===" >&2
cat "$PROJECT_DIR/CLAUDE.md" >&2
Use Case
Projects with complex execution policies, agent systems, or development protocols need those rules to persist across compaction. Currently there's no clean way to restore them - workarounds require fragile heuristics to detect compaction occurred.
Benefits
- Prevents policy drift across compaction
- Complements existing hooks (pre → compact → post lifecycle)
- Simple implementation - same pattern as existing hooks
- Enables advanced workflows for teams with strict development protocols
Alternative Approaches Considered
- Include policies in system prompt: Not project-specific, can't vary per-project
- Use
session_startonly: Doesn't help mid-session compaction - Detect compaction in other hooks: Fragile, requires external state tracking
- Manual re-injection: User burden, defeats automation purpose
---
Thank you for considering this feature!
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗