Feature Request: Add post_compact Hook for Policy Re-injection After Context Compaction

Resolved 💬 7 comments Opened Dec 26, 2025 by mraarone Closed Feb 18, 2026

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:

  1. Custom rules get summarized away: Project CLAUDE.md files, development protocols, agent configurations get compressed during compaction
  2. No way to restore policies: Current hooks (session_start, pre_compact) cannot inject into the post-compaction context
  3. 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

  1. Prevents policy drift across compaction
  2. Complements existing hooks (pre → compact → post lifecycle)
  3. Simple implementation - same pattern as existing hooks
  4. Enables advanced workflows for teams with strict development protocols

Alternative Approaches Considered

  1. Include policies in system prompt: Not project-specific, can't vary per-project
  2. Use session_start only: Doesn't help mid-session compaction
  3. Detect compaction in other hooks: Fragile, requires external state tracking
  4. Manual re-injection: User burden, defeats automation purpose

---

Thank you for considering this feature!

View original on GitHub ↗

This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗