fix(agent-core): stop looping auto-compaction when the threshold is unreachable - #2498
Open
xy200303 wants to merge 3 commits into
Open
fix(agent-core): stop looping auto-compaction when the threshold is unreachable#2498xy200303 wants to merge 3 commits into
xy200303 wants to merge 3 commits into
Conversation
…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 detectedLatest commit: 17cd2c7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
💡 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".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue
Resolve #2325
Problem
With a self-hosted
openai_responsesmodel, 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) against0.85 * max_context_size(ormax - 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'sreserved_context_size = 100000on a 128k window → trigger at ~31k),shouldCompactis 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 withopenai_responsesbecause 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-v2and the v1agent-coremirror):warningevent (rendered by the TUI) tells the user to raisemax_context_size, lowerloop_control.reserved_context_size, or raisecompaction_trigger_ratio./compactand 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
gen-changesetsskill, or this PR needs no changeset. (changeset added)gen-docsskill, or this PR needs no doc update. (no doc update needed)