fix(cli): recover from provider context-limit errors by compacting - #14635
Conversation
Classify provider/gateway errors whose message reads "<N> tokens long and exceeds this model's context length of <M> tokens" as context overflow, so the existing compact-and-retry recovery runs instead of the session dying at assistant_context_limit.
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Incremental review of Files Reviewed (2 files)
Previous Review Summary (commit 0faccb0)Current summary above is authoritative. Previous snapshots are kept for context only. Previous review (commit 0faccb0)Status: No Issues Found | Recommendation: Merge Verified the new overflow pattern on Files Reviewed (3 files)
Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0 Review guidance: REVIEW.md from base branch |
| /token limit exceeded/i, | ||
| // kilocode_change start - providers/gateways report over-long requests as | ||
| // "<N> tokens long and exceeds this model's context length of <M> tokens" | ||
| /exceeds (?:this|the) model'?s (?:(?:maximum|max) )?context length(?: of [\d,]+ tokens?)?/i, |
There was a problem hiding this comment.
it's all good but I wonder if that translates well in terms of i18n ... the , for thousands is not universally common so that:
- why do we have a thousand separator at all?
- once we answer previous point, should we add all other thousand separators in there too?
not a blocker, rather a question, and once answered, I'll approve
WebReflection
left a comment
There was a problem hiding this comment.
not the right RegExp
| /token limit exceeded/i, | ||
| // kilocode_change start - providers/gateways report over-long requests as | ||
| // "<N> tokens long and exceeds this model's context length" | ||
| /exceeds (?:this|the) model'?s (?:(?:maximum|max) )?context length/i, |
There was a problem hiding this comment.
there's something off in that model'?s expression:
- is
modelOK? here is not without ansafter:exceeds this model max context lengthisfalse - is model or models or model's meant? the RegExp is broken that way, it should be:
/exceeds (?:this|the) model(?:'s|s)? (?:(?:maximum|max) )?context length/iThere was a problem hiding this comment.
we're matching a specific error from the gateway and we don't need to support "model"
There was a problem hiding this comment.
is a tiny change that prevents future gotchas worth? I let you decide, I'll approve if the constrain was that short-sighting
Summary
Classify a provider/gateway context-overflow wording the CLI currently misses, so the existing compact-and-retry recovery runs instead of the session dying at
assistant_context_limit.Why
Cloud Code Reviewer sessions on
kilo-auto/freefail withterminal_reason = 'assistant_context_limit'. It is a real overflow, not a misclassification — the CLI log shows:The CLI's overflow check (
packages/opencode/src/provider/error.ts:192) misses this error:isContextOverflow(m)does not match. The nearest patterns needmaximum context length(packages/llm/src/provider-error.ts:9,14); the observed text saysexceeds this model's context length of <n> tokens.400, not413.error.metadata.provider_code, sobody?.error?.code === "context_length_exceeded"is false.Because the error is never classified as
context_overflow,needsCompactionis never set (packages/opencode/src/session/processor.ts) andprompt.tsnever compacts and retries.Change
One added pattern in
packages/llm/src/provider-error.ts(wrapped inkilocode_changemarkers; shared upstream file):This fixes both affected paths, since both go through
isContextOverflow:parseAPICallError→isContextOverflow(m)(packages/opencode/src/provider/error.ts:192)isContextOverflow(body)(packages/llm/src/route/executor.ts:262)No other behavior change: once matched,
context_overflow→MessageV2.ContextOverflowError→ processor halt →"compact"→ compact-and-retry already works.Tests
Extended
packages/llm/test/provider-error.test.tswith the production wordings. Existing rate-limit exclusion test stays green.bun test test/provider-error.test.ts(packages/llm): 2 passbun run typecheck(packages/llm): cleanbun test test/kilocode/provider/error.test.ts test/kilocode/session-overflow.test.ts(packages/opencode): 61 passbun run script/check-opencode-annotations.ts --worktree: all annotatedNotes
@opencode-ai/llmis private, so only apatchchangeset for@kilocode/cliis included.error_type/error.metadata.provider_code). The primary message fix covers the observed case, and the envelope shape is not represented in this repo. It can be added later if a captured response proves a case where the message text is absent.KiloSessionOverflow.shouldCompactreturnsfalsefor tool-result continuations, and the error must be classified as overflow first.Follow-up (other repo, not here)
After this ships, the cloud pin
KILOCODE_CLI_VERSIONinservices/cloud-agent-next/Dockerfilemust be bumped to the release containing the fix, or the Code Reviewer keeps failing.