Skip to content

fix: back off compaction overflow retries by token budget - #211

Merged
kermanx merged 1 commit into
mainfrom
xtr/harden-compaction-real-session
May 29, 2026
Merged

fix: back off compaction overflow retries by token budget#211
kermanx merged 1 commit into
mainfrom
xtr/harden-compaction-real-session

Conversation

@kermanx

@kermanx kermanx commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue. This follows up on #207 after checking the exported session and finding that overflow compaction retries can spend the retry budget on tiny trailing messages.

Problem

When a compaction summary request exceeds the model context window, the retry path used to move the compacted prefix back to the previous safe split only. In the reported session, the tail contained repeated tiny continue turns and empty assistant placeholders, so each retry reduced the provider input by only a few tokens and never reached the large tool-result messages before the retry budget was exhausted.

What changed

  • Add minOverflowReductionRatio to CompactionConfig, defaulting to 0.05.
  • Change overflow compaction backoff so each retry removes at least that fraction of the model context window in estimated tokens, while still choosing only safe split points.
  • Add a regression test that covers repeated tiny trailing messages.

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.

Validation

  • Replayed the exported session wire in memory up to line 1549. Old first retry would remove 6 estimated tokens; the new first retry removes 15,183 estimated tokens for a 262,144-token context window, above the 13,108-token 5% floor.
  • pnpm vitest run packages/agent-core/test/agent/compaction.test.ts --testNamePattern "backs off overflow compaction"
  • pnpm vitest run packages/agent-core/test/agent/compaction.test.ts
  • pnpm --filter @moonshot-ai/agent-core run typecheck
  • pnpm exec oxlint --type-aware packages/agent-core/src/agent/compaction/strategy.ts packages/agent-core/test/agent/compaction.test.ts
  • git diff --check

@changeset-bot

changeset-bot Bot commented May 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d663d6c

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

This PR includes changesets to release 2 packages
Name Type
@moonshot-ai/agent-core Patch
@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

@pkg-pr-new

pkg-pr-new Bot commented May 29, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@d663d6c
npx https://pkg.pr.new/@moonshot-ai/kimi-code@d663d6c

commit: d663d6c

@kermanx
kermanx force-pushed the xtr/harden-compaction-real-session branch from d418dd7 to 082f468 Compare May 29, 2026 11:03
@kermanx kermanx changed the title fix: harden compaction for oversized sessions fix: back off compaction overflow retries by token budget May 29, 2026
@kermanx
kermanx force-pushed the xtr/harden-compaction-real-session branch from 082f468 to d663d6c Compare May 29, 2026 11:07

@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: d663d6c293

ℹ️ 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/src/agent/compaction/strategy.ts
@kermanx
kermanx merged commit 54590d3 into main May 29, 2026
7 checks passed
@kermanx
kermanx deleted the xtr/harden-compaction-real-session branch May 29, 2026 11:29
@github-actions github-actions Bot mentioned this pull request May 29, 2026
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.

1 participant