Skip to content

Auto-compact again when the context refills in the same turn - #647

Open
brdloush wants to merge 2 commits into
editor-code-assistant:masterfrom
brdloush:fix/auto-compact-again-in-same-turn
Open

brdloush wants to merge 2 commits into
editor-code-assistant:masterfrom
brdloush:fix/auto-compact-again-in-same-turn

Conversation

@brdloush

Copy link
Copy Markdown

Related to #615.

Problem

After an auto-compaction, ECA does not auto-compact again until the user turn ends. A long turn can fill the context a second time and end with Context window exceeded. Steering makes this common, because steered messages join the running turn.

Real case: 262k context, 75%. One compaction at ~196k, then 6 steered messages in the same turn, no second compaction, and finally Input length (267765) exceeds model's maximum context length (262144).

Cause: resume-after-compact! sets :auto-compacted? true on the chat-ctx, and every later tool-call round of the turn reuses it. So the threshold check and the context-overflow fallback stay blocked for the whole turn. The flag comes from #391 (stop a compact → overflow → compact loop). That goal is right, but it only needs to cover the request right after a compaction.

Changes

  • Per-compaction guard. The resumed turn gets a :post-compact-guard* atom. It is released on the first usage update that shows the compacted context under the threshold. After that, the turn can compact again. If the compacted context is still too big, the guard stays on, so the ECA becomes unresponsive when tool output causes LLM context overflow #391 protection holds.
  • autoCompactMaxPerTurn (default 3, global or per agent). Each compaction sends the whole context to the LLM, so a turn that keeps refilling it (e.g. re-reading a huge file) must not compact without end. When the limit is reached, the turn ends with the normal overflow error.
  • The "Continue with the task" prefix no longer stacks up. Docs and schema are updated. A separate commit fixes a stale autoCompactPercentage: 85 in the default config docs (the default is 75).

Alternatives

Tests

  • auto-compact-again-later-in-same-turn-test and context-overflow-later-in-same-turn-auto-compacts-test: a second compaction in the same turn (both fail on master), and no loop when the compacted context is still too big.
  • auto-compact-limit-per-turn-test: stops at the limit, including autoCompactMaxPerTurn 1.
  • context-overflow-auto-compact-guard-test now goes through the real resume-after-compact!.

A/B with the real server against a fake LLM: master overflows after one compaction. This PR compacts twice and finishes the task, and a longer task stops at 3 compactions.

  • I added a entry in changelog under unreleased section.
  • This is not an AI slop. (I have read, simplified and pinpointed the important parts myself, the human 😄)

🤖 Generated with Claude Code

brdloush and others added 2 commits October 12, 2026 23:25
After an auto-compaction, :auto-compacted? stayed true for the rest of
the turn, so neither the threshold check nor the context-overflow
fallback could compact again. Long turns, common with steered messages,
filled the context a second time and ended with "Context window
exceeded".

The turn-wide flag is now a per-compaction guard. It is released on the
first usage update that shows the compacted context under the
threshold, so later compactions can run. If the compacted context is
still too big the guard stays on, keeping the editor-code-assistant#391 loop protection. The
"Continue with the task" prefix no longer stacks on each compaction.

A turn auto-compacts at most autoCompactMaxPerTurn times (default 3,
global or per agent), so a turn that keeps refilling the context can't
keep sending full-context compaction requests.

Related to editor-code-assistant#615

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The default config listed in introduction.md still showed 85. The
default was lowered to 75 in 3a26d6d, and the code, the schema and
context-management.md already say 75.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant