PostCompact hook should support context injection (stdout visible to model)
Problem
After compaction, the model loses raw conversation context. There is currently no official hook that can inject context the model actually sees post-compaction.
UserPromptSubmitandSessionStartstdout → visible to the model ✅PostCompactstdout → only visible in verbose mode ❌ ("used for side effects like logging or cleanup")PreCompact→ can influence what goes into the summary viaadditionalContext, but cannot control the output
This means:
- No way to inject visible context after compaction — the moment it's needed most
- No way to replace or augment the compaction summary with custom content
- No way to inject raw messages alongside the summary to preserve high-fidelity recent context
Current workaround
The only way to re-inject context after compaction is a state machine hacked into UserPromptSubmit:
- On every user prompt, scan for new
agent-acompact-*.jsonlfiles - Compare count against a marker file to detect if compaction just happened
- If yes, query an external memory store (SQLite DB of all past conversations) and inject the last 40 raw messages via stdout
- Write new marker count so it only fires once per compaction
This works, but it's fragile — relying on filesystem scanning and marker files to detect an event that Claude Code already knows happened.
Proposal
Let PostCompact hook stdout (or an additionalContext return field) be visible to the model, the same way UserPromptSubmit and SessionStart already work.
This would enable:
- Re-injecting raw conversation messages from external memory after compaction
- Providing custom summaries or augmented context alongside the built-in summary
- Querying external knowledge bases and injecting relevant context at the exact moment it's needed
- Eliminating hacky compaction-detection workarounds in
UserPromptSubmit
Why this matters
Compaction is the single biggest context loss event in a session. It's the exact moment users most need to inject context from external systems — and it's the one lifecycle hook that can't do it. Every other "context injection" hook (UserPromptSubmit, SessionStart, PreCompact) can write to model-visible context. PostCompact is the gap.
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗