Feature request: PostCompact hook event

Resolved 💬 2 comments Opened Apr 29, 2026 by AndersHeie Closed Jun 10, 2026

Problem

After context compaction, there's no way to re-inject critical rules or context back into the conversation. This is a problem for projects that use specialized agents with different rule sets.

Current state: The available hook events are SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Stop, and SessionEnd. None fire after compaction.

Why CLAUDE.md / .claude/rules/ isn't sufficient

  • CLAUDE.md is one-size-fits-all — it's loaded for every session regardless of which agent is running. Projects with many specialized agents (e.g., DB agent, API agent, CDK agent, security reviewer) each need different critical rules. Stuffing all agent-specific rules into CLAUDE.md bloats context for every session.
  • .claude/rules/ files are path-based, not agent-based or compaction-aware. They help with file-scoped conventions but can't target a specific agent's active context.
  • Agent prompts (initial system instructions) survive, but rules established mid-conversation through tool results or injected context get compacted away.

The gap

When compaction happens, agent-specific invariants that were established during the conversation can be lost. There's no mechanism to detect that compaction occurred and re-inject the relevant rules.

Proposed solution

Add a PostCompact hook event that fires after context compaction completes. The hook could:

  1. Receive metadata about the current session (e.g., active agent type, session ID)
  2. Return text to be injected into the post-compaction context as a system reminder
  3. Allow different rules per agent or context

Example use case

{
  "hooks": {
    "PostCompact": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "cat .claude/compaction-rules/${CLAUDE_AGENT_TYPE:-default}.md"
          }
        ]
      }
    ]
  }
}

This would let a project maintain per-agent rule files that get re-injected after compaction, keeping critical invariants present without bloating every session's base context.

Alternatives considered

  • Bigger CLAUDE.md: Works but doesn't scale — bloats all sessions equally.
  • Stop hook + re-prompt: Fires at the wrong time (after response, not after compaction).
  • UserPromptSubmit hook: Fires every turn regardless of compaction — wasteful and doesn't know if compaction just happened.

View original on GitHub ↗

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