Auto-compaction drops PreToolUse read-state, causing infinite re-read loop on multi-file edits

Open 💬 3 comments Opened Jun 15, 2026 by gtapps

What happens

The Edit tool requires a prior Read of the target file in the current conversation. When a session reads N files in order to edit them and the accumulated file bodies push context past the auto-compaction threshold, auto-compaction fires and drops the per-conversation read-state. The model's correct reflex is then to re-Read all N files before editing — which re-bloats context, re-triggers auto-compaction, and loops with zero forward progress.

This is a control-flow trap, not a knowledge gap: the model knows it must re-read (it says so), and re-reading is the only way to satisfy Edit. The very action required to proceed reproduces the bloat that caused the compaction.

Repro sketch

In a single session:

  1. Read ~15 files whose combined bodies cross the auto-compaction threshold.
  2. Attempt a cross-file edit (e.g. an import/DTO rename touching all of them).

Observed: repeated full-body re-Reads after each compaction, a files-applied counter that resets rather than advancing, and no termination within a reasonable turn ceiling. (Originally observed right after a planning step handed edit execution back to the main context — the one context that cannot hold N file bodies.)

Root cause

Read-state that Edit depends on does not survive auto-compaction, while the intent to edit does. Post-compaction state is therefore "must edit these files, must re-read first" — which deterministically re-creates the context pressure that triggered compaction.

Suggested directions (any one breaks the loop)

  • Preserve Edit read-state across auto-compaction — treat a Read as still satisfied if it occurred earlier in the same session before a compaction boundary.
  • Exclude already-applied edit targets from the re-read reflex — once a file has been successfully edited this session, don't re-read its full body to edit it again.
  • Surface a compaction / read-state signal to PreToolUse so harness authors can mitigate. Today PreToolUse stdin carries no context_usage or compaction field — only transcript_path. (PreCompact does fire with a trigger discriminator, which helps, but a forward signal at edit time would be cleaner.)

Field workaround (mitigation only)

Harnesses can fan edits out to per-file subagents (fresh context each, returning a one-line status rather than a file body), and/or add a PreToolUse + PreCompact hook pair that tracks edited paths in session-scoped state and blocks a re-Edit of an already-edited file immediately after an auto-compaction. These mitigate the symptom; the read-state lifetime across compaction is the harness-side root cause.

Environment

  • Claude Code 2.1.178

View original on GitHub ↗

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