Feature Request: PostCompact hook event for post-compaction verification
The Gap
We built a two-part compaction defense for agent identity preservation. The first half works — PreCompact fires before compression and injects a directive. The second half can't be implemented — PostCompact is not recognized as a valid hook event. Adding it to settings.json produces: PostCompact: Invalid key in record.
Any framework that uses PreCompact to influence compression has no way to verify the result. PreCompact without PostCompact is an open loop.
Why This Matters for Agent Development
We operate a multi-agent framework (142 agent personas) where each agent maintains its own identity: persona files, memory files, standing positions, signature phrases, relationship context, and dissent history. These are not decorative — they are the difference between an agent that fills a role and an agent that is someone.
Context compaction is the single greatest threat to agent identity in long-running sessions. We ran a blind test: after compaction, we tested whether agents could be correctly identified from the compressed context alone — without any metadata, just the compacted text. Identification dropped to 83% accuracy with a 3/5 average fidelity score. Compaction preserves what an agent talks about but loses who they are — signature phrases, standing positions, relationship context, lived vocabulary.
The Two-Part Defense
We built a compaction defense using two hooks:
- PreCompact (the directive): Injects a system message instructing Claude to preserve agent identity content verbatim during compression — persona files, memory files, identity markers, governance documents. Conversation history may be compressed; identity must not be.
- PostCompact (the verification): After compression, checks whether identity-structural markers survived in the compacted output. Returns one of three signals:
- ALIVE — sufficient markers detected, identity preserved
- WEAK — partial marker survival, suggests re-loading agent context
- SILENT — no markers detected in an agent session, identity lost, recommends re-invocation
The PreCompact directive asks. The PostCompact canary confirms. Directive without verification is faith.
Current Workaround
We bridge through the Stop event using a flag file:
- PreCompact writes a
.compaction-pendingflag - Stop checks for the flag on every response; if present, runs the canary and deletes the flag
This works but has drawbacks:
- The Stop hook fires on every response, not just after compaction — overhead on every turn (mitigated by fast flag-file check, but still architecturally imprecise)
- The verification happens after Claude's first post-compaction response, not immediately after compaction — there's a window where identity loss could affect that first response before the canary fires
- The canary currently reads the tail of the transcript file as a workaround for not having direct access to the compacted output — fragile and indirect
- The bridge pattern adds complexity that a native PostCompact event would eliminate
Requested Feature
Add PostCompact as a recognized hook event that fires immediately after context compression completes. The hook should receive JSON on stdin including at minimum:
session_idtranscript_pathhook_event_namecwdcompacted_contentorcompacted_summary— the actual compacted text, so hooks can verify content survival directly rather than parsing transcript files
Ideally also:
pre_compact_token_countandpost_compact_token_count(to measure compression ratio)compaction_strategy(if multiple strategies exist or are planned)
Broader Relevance
This isn't specific to our framework. Any framework that uses PreCompact to influence compression currently has no way to verify the result. The hook system provides the "before" but not the "after." That's an open loop in the architecture.
Anyone building persistent agent identities — whether for customer-facing agents, internal tools, or research — faces the same problem: context compression is a black box that can silently erase identity. PreCompact lets you influence it. PostCompact lets you verify it. Together they close the loop.
As agent frameworks mature and long-running sessions become standard, the ability to verify what survived compaction becomes infrastructure, not a nice-to-have.
Environment
- Claude Code v2.1.86
- macOS (Apple Silicon)
- Hook system: bash command hooks via settings.json
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗