PostCompact hook should support context injection (stdout visible to model)

Resolved 💬 4 comments Opened Mar 31, 2026 by chadptk1238 Closed Mar 31, 2026

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.

  • UserPromptSubmit and SessionStart stdout → visible to the model ✅
  • PostCompact stdout → only visible in verbose mode ❌ ("used for side effects like logging or cleanup")
  • PreCompact → can influence what goes into the summary via additionalContext, but cannot control the output

This means:

  1. No way to inject visible context after compaction — the moment it's needed most
  2. No way to replace or augment the compaction summary with custom content
  3. 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:

  1. On every user prompt, scan for new agent-acompact-*.jsonl files
  2. Compare count against a marker file to detect if compaction just happened
  3. If yes, query an external memory store (SQLite DB of all past conversations) and inject the last 40 raw messages via stdout
  4. 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.

View original on GitHub ↗

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