[FEATURE] PreCompact: Allow deferring compaction for context externalization

Resolved 💬 3 comments Opened Apr 28, 2026 by justinjeong5 Closed May 1, 2026

Problem

When working on large tasks (e.g., multi-day feature development with RFC documents, architecture analysis), automatic compaction triggers frequently — sometimes 10+ times in a single session. Each compaction is lossy, and after several rounds, early decisions, rationale, and tried-but-failed approaches are effectively lost.

The current PreCompact hook can output text, but it cannot defer compaction to allow the model to take action first (e.g., updating a SESSION.md file with current decisions and progress before context is compressed).

Proposed Solution

Add a "defer" capability to PreCompact hooks, allowing the hook to signal that compaction should be paused while the model externalizes critical context:

// Example: PreCompact hook returns a defer signal
{
  "action": "defer",
  "reason": "SESSION.md needs updating before compaction",
  "maxDeferMs": 30000  // timeout safety
}

Flow:

  1. Compaction triggered (auto or manual)
  2. PreCompact hook fires → returns defer
  3. Model gets a system message: "Compaction deferred. Update SESSION.md with current decisions before proceeding."
  4. Model updates SESSION.md
  5. Compaction proceeds automatically after model turn completes (or after timeout)

Workaround (Current)

I've built a multi-layer workaround:

  • PreCompact hook: Outputs SESSION.md content so it's included in the compaction summary (best-effort)
  • SessionStart(compact) hook: Re-injects SESSION.md + MEMORY.md + git state after compaction
  • CLAUDE.md rules: Instructs the model to proactively manage context and update SESSION.md

This works partially, but the core issue remains: the model can't act between "compaction decided" and "compaction executed."

Related Issues

  • #46191 — additionalContext support in PreCompact/PostCompact
  • #50467 — PreCompact hook not firing on auto-compaction
  • #44308 — No visibility into what's being lost during compaction
  • #31845 — Allow decision control (clear vs compact)

This proposal is complementary: those issues improve what data hooks receive; this one allows the model to respond before context is lost.

Use Case

  • Large feature development (planning + implementation across 500K+ tokens)
  • Architecture analysis sessions with many intermediate decisions
  • Any workflow where "why we decided X" matters as much as "what we decided"

View original on GitHub ↗

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