kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

Auto-compaction races with the agent loop and discards every completed summary when compaction_trigger_ratio < 0.85

Open belenov-maker opened this issue 1 month ago • 0 comments

Environment

  • kimi-code (Node) v2.1.1, macOS arm64
  • Main model: GLM-5.3, 256K context, thinking effort max
  • Config: compaction_trigger_ratio = 0.7 (default), reserved_context_size = 16000
  • Workload: long autonomous agent-loop sessions (dozens of tool calls per turn)

Component

agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts (beforeStep, historySafeToCompact; bundle ~269758–270431).

Mechanism

blockRatio = max(triggerRatio, 0.85), so whenever triggerRatio < 0.85 there is a zone where checkAutoCompaction() starts compaction in the background while shouldBlock() does not yet block the loop. For any actively running session (autonomous/agent loop) the history keeps growing with assistant/tool messages while the summary is being generated; historySafeToCompact then returns false, the finished summary is thrown away, lastCompactedTokenCount is never set, and maxCompactionPerTurn = Infinity — an endless cycle of heavy LLM calls. Even a blocked compaction can be broken by an immediate context.append from reminder.notify (file-watcher on AGENTS.md, plugin change).

Two aggravating details:

  • compactionMaxAttempts (5) only counts request failures — invalidating cancellations are not counted, so the cancel/retry loop is effectively unbounded.
  • The blocking compaction path only runs at the start of a turn; when triggerRatio < blockRatio the session never reaches it and stays in the racy after-step mode.

Facts

  • 121 consecutive discarded compactions in one day; cancellations as fast as 1 ms after usage.record.
  • ~9.5M input tokens wasted for a single user.
  • Worst-day aggregate: 163 attempts / 129 cancellations (79%); cancelled attempts cluster at 210–290K, frequently above the model's 256K window — the loop keeps retrying while the context overflows.

Expected

Synchronous auto-compaction (block the loop for the duration of summary generation), or merge the finished summary with the interim history instead of discarding it; at minimum, count invalidating cancellations toward compaction_max_attempts and retry with backoff.

Reproduction

  1. Default config (compaction_trigger_ratio = 0.7), a thinking model with a 256K window.
  2. Run a session with one long agent-loop turn (dozens of tool calls) growing the context past ~70% of the window.
  3. Observe the telemetry: repeated full_compaction.begin → cancel cycles, cancellations 1 ms after usage.record, the context keeps growing through the retries, attempts continue past the model window.

Workaround

Set compaction_trigger_ratio = 0.85 in [loop_control] — triggerRatio == blockRatio disables the after-step background mode; compaction then runs once, synchronously, at the start of a turn. This matches the behavior of the Python kimi-cli, which compacts reliably in the same setup.


Related issues:

  • https://github.com/MoonshotAI/kimi-code/issues/4203
  • https://github.com/MoonshotAI/kimi-code/issues/4204
  • https://github.com/MoonshotAI/kimi-code/issues/4205

belenov-maker avatar Oct 14 '26 06:10 belenov-maker