claude-code - 💡(How to fix) Fix [FEATURE] PreCompact: Allow deferring compaction for context externalization [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#54118Fetched 2026-04-28 06:38:46
View on GitHub
Comments
1
Participants
2
Timeline
3
Reactions
0
Timeline (top)
labeled ×2commented ×1

Fix Action

Fix / Workaround

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

Code Example

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

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"

extent analysis

TL;DR

Implementing a "defer" capability in PreCompact hooks would allow the model to externalize critical context before compaction, preventing loss of early decisions and rationale.

Guidance

  • Review the proposed solution and consider adding a "defer" capability to PreCompact hooks to enable the model to update SESSION.md before compaction.
  • Evaluate the current workaround and its limitations to understand the need for a more robust solution.
  • Investigate related issues (#46191, #50467, #44308, #31845) to ensure the proposed solution is complementary and addresses the core issue.
  • Consider the use cases (large feature development, architecture analysis sessions) to understand the importance of preserving context and decision rationale.

Example

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

Notes

The proposed solution requires changes to the PreCompact hook functionality, which may involve updates to the underlying system or model architecture.

Recommendation

Apply the proposed workaround until the "defer" capability is implemented, as it provides a partial solution to the issue.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [FEATURE] PreCompact: Allow deferring compaction for context externalization [1 comments, 2 participants]