Skip to content

fix(agent-core): block turns during auto compaction - #2755

Open
030611 wants to merge 1 commit into
MoonshotAI:mainfrom
030611:fix-compaction-race-2720
Open

fix(agent-core): block turns during auto compaction#2755
030611 wants to merge 1 commit into
MoonshotAI:mainfrom
030611:fix-compaction-race-2720

Conversation

@030611

@030611 030611 commented Aug 9, 2026

Copy link
Copy Markdown

Related Issue

Resolve #2720

Problem

See linked issue.

What changed

Wait for an in-flight auto compaction before starting the next agent step, even when the context is still below the blocking threshold. This keeps the compaction snapshot stable until commit and prevents the repeated cancel/abort cycle.

Defer soft auto compaction across tool-call continuations so the next model request can consume fresh tool results. Hard-threshold compaction still runs and blocks before the continuation.

Added regressions for both the in-flight race and the soft-compaction/tool-continuation ordering.

Validation:

  • vitest run packages/agent-core-v2/test/agent/fullCompaction/fullCompaction.test.ts (75 passed)
  • tsc -p packages/agent-core-v2/tsconfig.json --noEmit
  • node packages/agent-core-v2/scripts/check-import-boundaries.mjs
  • oxlint on the changed TypeScript files

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 feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Aug 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f9417c7

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: e0e9417447

ℹ️ 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.activeTurnId = turnId;
this.checkAutoCompaction();
if (this.strategy.shouldBlock(this.tokenCountWithPending())) {
if (this._compacting !== null || this.strategy.shouldBlock(this.tokenCountWithPending())) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid compacting before tool continuations consume results

When compactionTriggerRatio is below the blocking ratio, afterStep() can start a soft auto-compaction immediately after a step that ended with tool calls; with this new condition the next continuation step now waits for that compaction to finish before making its LLM request. The compaction rewrite keeps user messages plus the generated summary and drops the raw assistant/tool-result messages, so the continuation that is supposed to consume fresh tool results may only see a lossy summary even though the context is still below the hard block threshold. This affects multi-step tool turns under lower-ratio auto compaction; consider not blocking/compacting ahead of tool-call continuations unless the hard block condition is actually met.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch. I reproduced the ordering as agent -> compaction -> continuation -> compaction.

Updated in f9417c7: soft auto compaction is now deferred across tool-call continuations, while hard-threshold compaction still blocks before the continuation. The new regression asserts agent -> continuation -> compaction and verifies that the continuation sees the raw tool result. The full test file is 75/75; typecheck, import boundaries, and oxlint also pass.

Signed-off-by: Errant <2843409461@qq.com>
@030611
030611 force-pushed the fix-compaction-race-2720 branch from e0e9417 to f9417c7 Compare August 9, 2026 09:48
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.

Auto compaction cancelled at commit time (historySafeToCompact race with running loop), turn killed with 'This operation was aborted'

1 participant