kilocode icon indicating copy to clipboard operation
kilocode copied to clipboard

"Compaction exhausted" ends long turns although every compaction succeeded

Open altiber opened this issue 1 month ago • 2 comments

Component

CLI / TUI

Description

compactionAttempts in packages/opencode/src/session/prompt.ts (runLoop) is set to 0 once per turn and incremented on every compaction (compactionAttempts++ near guardCompactionAttempt); nothing resets it after a compaction that worked. So a long turn - e.g. a subagent that reads a lot of code and cycles its context several times - is closed with ContextOverflowError: Compaction exhausted: context still exceeds model limits after 3 attempts on its 4th compaction, even though each of the previous three shrank the context and the model completed many steps in between.

The comment on MAX_COMPACTION_ATTEMPTS says the cap is for "a normal overflow compaction plus a summary-self-overflow retry without spinning forever", i.e. compactions that do not help. Counting the successful ones too limits every turn to 3 context cycles.

Kilo version

7.8.8 (same code on main, 2026-10-13)

Steps to reproduce

  1. Use a model with a context window small enough that one turn needs 4+ compactions (reproduced with a local OpenAI-compatible model, limit.context 60000, a subagent asked to read ~390 KB of source with the read tool in 300-line chunks).
  2. Without a step limit, the turn compacts 3 times successfully and stops on the 4th with "Compaction exhausted" (22 reads done, both files not finished).
  3. Expected: the turn goes on as long as compactions keep shrinking the context; the cap should only catch compactions in a row without a completed step between them.

Operating System

Windows 11

Terminal

VS Code extension / CLI daemon

Related: #11076, #9494

altiber avatar Oct 14 '26 18:10 altiber

Fixed by the compactionAttempts reset implemented in commit 9be7edfe62 on branch fix/compaction-loop-bugs:

compactionAttempts is now reset to 0 after any model step that completes without error and without asking for a compaction. The cap now counts only consecutive compactions in a row, which is exactly the loop it guards against. Long turns (e.g. subagents reading a lot of code) no longer stop with "Compaction exhausted" after their third successful compaction.

Regression test: completes a long turn whose compactions are separated by completed steps in session-compaction-cap.test.ts.

HenricHuntsman avatar Oct 27 '26 12:10 HenricHuntsman

@HenricHuntsman thanks! I couldn't find commit 9be7edfe62 or a branch fix/compaction-loop-bugs in Kilo-Org/kilocode. Is it in a fork, or a different PR? The same reset (compactionAttempts = 0 after a completed step that doesn't ask for compaction) is in #15070, approved on Oct 28, so whichever one lands first should close this issue.

altiber avatar Oct 30 '26 10:10 altiber