Skip to content

Commit e898755

Browse files
author
Bertan Ari
committed
fix(vscode-lm): mark the over-budget refusal as a context-window error
1 parent de43c07 commit e898755

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎src/api/providers/__tests__/vscode-lm.spec.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import {
7676
middleOutTruncate,
7777
truncateToolResultsToFitWindow,
7878
} from "../vscode-lm"
79+
import { checkContextWindowExceededError } from "../../../core/context/context-management/context-error-handling"
7980
import type { ApiHandlerOptions } from "../../../shared/api"
8081
import type { Anthropic } from "@anthropic-ai/sdk"
8182
import { openAiModelInfoSaneDefaults, vscodeLlmDefaultModelId, vscodeLlmModels } from "@roo-code/types"
@@ -579,6 +580,36 @@ describe("VsCodeLmHandler", () => {
579580
expect(mockLanguageModelChat.sendRequest).not.toHaveBeenCalled()
580581
})
581582

583+
it("refuses with an error the task recognises as a context-window failure", async () => {
584+
// Asserted through the real detector: without a recognised shape the task takes its
585+
// generic retry path and re-sends the same over-window history instead of condensing.
586+
const systemPrompt = "S".repeat(handler.getCondenseContextWindow() * 3)
587+
const messages: Anthropic.Messages.MessageParam[] = [
588+
{
589+
role: "assistant",
590+
content: [{ type: "tool_use", id: "t1", name: "some_tool", input: { a: 1 } }],
591+
},
592+
{
593+
role: "user",
594+
content: [{ type: "tool_result", tool_use_id: "t1", content: "X".repeat(50_000) }],
595+
},
596+
]
597+
598+
const stream = handler.createMessage(systemPrompt, messages, { taskId: "test-task" })
599+
const refusal = await (async () => {
600+
try {
601+
for await (const _chunk of stream) {
602+
// drain
603+
}
604+
} catch (error) {
605+
return error
606+
}
607+
throw new Error("expected the request to be refused")
608+
})()
609+
610+
expect(checkContextWindowExceededError(refusal)).toBe(true)
611+
})
612+
582613
it("sends a request that fits within a small positive raw budget", async () => {
583614
const targetRawBudgetChars = 1000
584615
const systemPrompt = "S".repeat(

‎src/api/providers/vscode-lm.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,11 +1070,17 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
10701070
// genuinely cannot hold whenever the raw budget falls below MIN_TOOL_RESULT_CHARS.
10711071
const remainingChars = estimateMessagesChars(cleanedMessages)
10721072
if (remainingChars > rawBudgetChars) {
1073-
throw new Error(
1074-
"Zoo Code <Language Model API>: The request is too large for this model's context window " +
1075-
`(estimated ${remainingChars.toLocaleString("en-US")} characters against a budget of ` +
1076-
`${Math.max(0, Math.floor(rawBudgetChars)).toLocaleString("en-US")}), and it cannot be reduced further without ` +
1077-
"breaking tool-call pairing. Condense the conversation or start a new task.",
1073+
// `status` is what makes checkContextWindowExceededError recognise this as a context
1074+
// -window failure; without it the task takes its generic retry path and re-sends the
1075+
// same over-window history instead of condensing.
1076+
throw Object.assign(
1077+
new Error(
1078+
"Zoo Code <Language Model API>: The request is too large for this model's context window " +
1079+
`(estimated ${remainingChars.toLocaleString("en-US")} characters against a budget of ` +
1080+
`${Math.max(0, Math.floor(rawBudgetChars)).toLocaleString("en-US")}), and it cannot be reduced further without ` +
1081+
"breaking tool-call pairing. Condense the conversation or start a new task.",
1082+
),
1083+
{ status: 400 },
10781084
)
10791085
}
10801086
}

0 commit comments

Comments
 (0)