[Feature] PostCompact hook event to invalidate cached knowledge state
Problem
After context compaction, hooks that track "has Claude read file X?" via timestamp markers become dangerously wrong. The marker says "yes, file was read 5 minutes ago" but the file's content is no longer in Claude's context — it was lost in compaction.
This caused a real incident where Claude nearly destroyed a correct rule in production code because a cheatsheet read pre-compaction was gone from context, but the knowledge gate hook still showed it as "recently read."
The fundamental issue
There is no PostCompact hook event. Hooks cannot detect that compaction occurred and invalidate cached state. The existing PreCompact hook (#33088) helps serialize state before compaction, but there's no way to react after — e.g., to:
- Delete knowledge-gate marker files (forcing re-reads of critical docs)
- Log that compaction happened (for session audit trails)
- Trigger automatic re-loading of mandatory project files
- Reset any hook state that assumes continuous context
Current workaround (broken)
guard-domain-knowledge.sh uses a 30-minute timer: if a knowledge file was read within 30 min, edits to gated files are allowed. After compaction, the timer is still valid but the knowledge is gone. Time-based checks measure "was the file opened" not "does Claude still have this knowledge."
Proposed Solution
A PostCompact hook event that fires after compaction completes, with data like:
{
"hook": "PostCompact",
"context_tokens_before": 180000,
"context_tokens_after": 60000,
"messages_compacted": 89,
"compaction_summary_available": true
}
This would let hooks:
- Invalidate all cached knowledge markers
- Force re-reads of critical files before next action
- Log compaction events for debugging
Impact
Without this, any hook system that tracks "what has Claude read" is fundamentally broken across compaction boundaries. The PreCompact hook helps prepare, but PostCompact is needed to react — the two are complementary.
Real-world incident
A 40k-token spec file was read early in a session. After compaction, Claude lost all knowledge of its contents but the guard hook still said "recently read." Claude then edited the spec file's target (a Phase B LLM prompt machine) based on a wrong cheatsheet that was written post-compaction with degraded context — nearly destroying a correct rule that had taken hours to establish. The user caught it manually; the hooks did not.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗