Skip to content

fix(agent-core): stop looping auto-compaction when the threshold is unreachable - #2498

Open
xy200303 wants to merge 3 commits into
MoonshotAI:mainfrom
xy200303:fix/auto-compaction-circuit-breaker
Open

fix(agent-core): stop looping auto-compaction when the threshold is unreachable#2498
xy200303 wants to merge 3 commits into
MoonshotAI:mainfrom
xy200303:fix/auto-compaction-circuit-breaker

Conversation

@xy200303

@xy200303 xy200303 commented Aug 1, 2026

Copy link
Copy Markdown

Related Issue

Resolve #2325

Problem

With a self-hosted openai_responses model, the CLI compacts continuously from startup and never becomes usable. Root cause: the auto-compaction trigger compares the server-reported total request usage (system prompt + tool schemas + messages) against 0.85 * max_context_size (or max - reserved_context_size). kimi-code's fixed request overhead alone is ~29k tokens, so for any model whose threshold lands below that (e.g. max_context_size = 32768 → trigger at ~27.9k, or the commenter's reserved_context_size = 100000 on a 128k window → trigger at ~31k), shouldCompact is permanently true even with an empty conversation. Compaction can only shrink conversation messages, so the next step's server-reported usage jumps back over the threshold and the loop never terminates. It surfaces with openai_responses because those endpoints reliably report usage (chat-completions servers often omit streaming usage, leaving the counter at small estimates).

What changed

A circuit breaker in the full-compaction service (v2 agent-core-v2 and the v1 agent-core mirror):

  • Each auto-compaction records the token count that triggered it. At the next check — after a real step has re-measured the request — a steady-state count below 80% of that baseline means the compaction worked and resets the streak; otherwise the streak grows.
  • Two consecutive ineffective compactions mean auto-compaction cannot succeed for this model config, so it is disabled for the rest of the session and a warning event (rendered by the TUI) tells the user to raise max_context_size, lower loop_control.reserved_context_size, or raise compaction_trigger_ratio.
  • Normal operation is unaffected: compactions that genuinely shrink the context (e.g. 220k → 34k) reset the streak, so long sessions compact as often as needed. Manual /compact and overflow-error recovery (already capped at 3 attempts) intentionally bypass the circuit.

Tests: v2 gains a reproduction of the loop (compaction stops after 2 rounds, warning emitted) and a false-positive guard (an effective compaction resets the streak, so the circuit opens one round later than it would with an accumulated streak); v1 gains the loop reproduction.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my fix works.
  • Ran gen-changesets skill, or this PR needs no changeset. (changeset added)
  • Ran gen-docs skill, or this PR needs no doc update. (no doc update needed)

…reshold

When a model's context window is small relative to the CLI's fixed
request overhead (system prompt + tool schemas, ~29k tokens), the
auto-compaction trigger threshold sits below the baseline request size:
every compaction only shrinks conversation messages, and the next
server-reported usage lands back over the threshold, so auto-compaction
repeats forever (reported with self-hosted openai_responses models,
e.g. a 32k max_context_size where 0.85 * window < 29k baseline).

Add a circuit breaker: each auto-compaction records the token count
that triggered it, and the next check (after a real step re-measures
the request) compares the steady-state count against that baseline. A
genuine drop below 80% of the baseline resets the streak; otherwise it
grows, and at two consecutive ineffective compactions auto-compaction
is disabled for the rest of the session with a warning pointing at
max_context_size and loop_control settings, instead of looping.

Manual compaction and overflow recovery (capped at 3 attempts) bypass
the circuit.

Refs MoonshotAI#2325
@changeset-bot

changeset-bot Bot commented Aug 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 17cd2c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 33fd81b281

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts Outdated
Comment thread packages/agent-core-v2/src/agent/fullCompaction/fullCompactionService.ts Outdated
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.

[openai_responses] Continuous compaction loop on startup

1 participant