Skip to content

fix(cli): compact before configured context threshold - #11010

Merged
marius-kilocode merged 5 commits into
mainfrom
button-viburnum
Jun 8, 2026
Merged

fix(cli): compact before configured context threshold#11010
marius-kilocode merged 5 commits into
mainfrom
button-viburnum

Conversation

@marius-kilocode

Copy link
Copy Markdown
Collaborator

Provider-reported token usage is only available after a response and can be missing or inaccurate, so the configured auto-compaction threshold could be crossed without compaction. Kilo could then send an oversized request and recover only after the provider returned a context-limit error.

This change estimates the assembled outgoing request before provider dispatch, including conversation text, system instructions, and tool schemas. It starts compaction when that estimate reaches compaction.threshold_percent or the existing reserved safety limit, whichever comes first, while retaining provider overflow handling as a fallback.

Preflight replay preserves media attached to the current user turn. Tool-result continuations skip proactive replay so completed tools are not executed twice, and conservative raw media accounting remains in place when capping output tokens.

Related user issue: #9605

Comment thread packages/opencode/src/kilocode/session/overflow.ts Outdated
Comment thread packages/opencode/src/session/compaction.ts Outdated
@kilo-code-bot

kilo-code-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Overview

Both issues from the previous review have been resolved in the latest commits:

Previous Issue Status
overflow.tsUint8Array payloads silently undercounted via JSON.stringify ✅ Fixed: now uses value.byteLength / 4 for binary payloads
compaction.ts — fragile pending heuristic for preflight replay detection ✅ Fixed: replaced with explicit overflow !== undefined sentinel semantics (false = preflight replay, undefined = disable replay)

The Uint8Array fix is covered by a new unit test at packages/opencode/test/kilocode/session-overflow.test.ts:248 that verifies a 600KB binary image registers raw > 100_000. The compaction semantics fix is validated by the assertion added to the preflight e2e test confirming marker.overflow === false.

Files Reviewed (5 files)
  • packages/opencode/src/kilocode/session/overflow.ts — fix verified
  • packages/opencode/src/session/compaction.ts — fix verified
  • packages/opencode/test/kilocode/session-overflow.test.ts — new test added
  • packages/opencode/test/kilocode/session-prompt-compaction-safety.test.ts — assertion added
  • packages/opencode/test/kilocode/session-export/e2e.test.ts — network-dependent test removed

Fix these issues in Kilo Cloud


Reviewed by claude-4.6-sonnet-20260217 · 853,271 tokens

Review guidance: REVIEW.md from base branch main

@marius-kilocode
marius-kilocode merged commit 08d0dd4 into main Jun 8, 2026
23 checks passed
@marius-kilocode
marius-kilocode deleted the button-viburnum branch June 8, 2026 19:38
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
fix(cli): compact before configured context threshold
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.

2 participants