claude-code - 💡(How to fix) Fix Feature request: PostCompact hook event [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#54536Fetched 2026-04-30 06:42:59
View on GitHub
Comments
0
Participants
1
Timeline
5
Reactions
0
Participants
Timeline (top)
labeled ×3cross-referenced ×1subscribed ×1

Code Example

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

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.

extent analysis

TL;DR

Implementing a PostCompact hook event can help re-inject critical rules or context back into the conversation after compaction.

Guidance

  • Consider adding a PostCompact hook event that receives metadata about the current session and returns text to be injected into the post-compaction context.
  • Evaluate the proposed solution's example use case, which uses a JSON configuration to define a hook that injects rules from a file based on the active agent type.
  • Assess the trade-offs of alternative approaches, such as using a larger CLAUDE.md file or relying on existing hook events like Stop or UserPromptSubmit.
  • Investigate how to integrate the PostCompact hook with existing agent-specific rules and context management.

Example

The provided example use case demonstrates how to configure a PostCompact hook to inject rules from a file based on the active agent type:

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

Notes

The implementation of the PostCompact hook event may require changes to the underlying conversation management system, and its effectiveness depends on the specific requirements of the projects using specialized agents.

Recommendation

Apply the proposed workaround by implementing a PostCompact hook event, as it addresses the specific need to re-inject critical rules or context after compaction, and it provides a flexible way to manage agent-specific rules.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Feature request: PostCompact hook event [1 participants]