Skip to content

Commit 7ccce11

Browse files
author
Bertan Ari
committed
perf(vscode-lm): skip rescans while carrying invoke whitespace; cover no-tools and fenced-prefix paths
1 parent b5791e9 commit 7ccce11

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

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

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,40 @@ describe("VsCodeLmHandler", () => {
355355
return drain()
356356
}
357357

358+
it.each([{ tools: undefined }, { tools: [] }])(
359+
"streams marker text immediately when tools are $tools",
360+
async ({ tools }) => {
361+
const parts = [
362+
'<function_calls><invoke name="calculator">',
363+
'<parameter name="operation">add</parameter></invoke></function_calls>',
364+
]
365+
let partsProduced = 0
366+
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({
367+
stream: (async function* () {
368+
for (const part of parts) {
369+
partsProduced++
370+
yield new vscode.LanguageModelTextPart(part)
371+
}
372+
})(),
373+
})
374+
const stream = handler.createMessage("system", [{ role: "user", content: "hi" }], {
375+
taskId: "test-task",
376+
tools,
377+
})
378+
379+
await expect(stream.next()).resolves.toEqual({
380+
done: false,
381+
value: { type: "text", text: parts[0] },
382+
})
383+
expect(partsProduced).toBe(1)
384+
await expect(stream.next()).resolves.toEqual({
385+
done: false,
386+
value: { type: "text", text: parts[1] },
387+
})
388+
expect((await collectStream(stream)).map((chunk) => chunk.type)).toEqual(["usage"])
389+
},
390+
)
391+
358392
it("recovers a tool call the model streamed as raw invoke XML", async () => {
359393
const chunks = await collect([
360394
"Thinking. ",
@@ -384,6 +418,73 @@ describe("VsCodeLmHandler", () => {
384418
])
385419
})
386420

421+
it("scans unfinished invoke whitespace with linear work across small chunks", async () => {
422+
const measure = async (partCount: number) => {
423+
const originalExec = RegExp.prototype.exec
424+
let scannedCharacters = 0
425+
const execSpy = vi.spyOn(RegExp.prototype, "exec").mockImplementation(function (
426+
this: RegExp,
427+
text: string,
428+
) {
429+
if (this.source.includes("invoke") || this.source.includes("function_calls")) {
430+
scannedCharacters += text.length
431+
}
432+
return originalExec.call(this, text)
433+
})
434+
try {
435+
const parts = ["<invoke", ...Array<string>(partCount).fill(" \t\n\r ")]
436+
const chunks = await collect(parts)
437+
expect(chunks.filter((chunk) => chunk.type !== "usage")).toEqual([
438+
{ type: "text", text: parts.join("") },
439+
])
440+
} finally {
441+
execSpy.mockRestore()
442+
}
443+
return scannedCharacters
444+
}
445+
446+
// Count regex input rather than wall time so shared runners do not make this flaky.
447+
const small = await measure(200)
448+
const large = await measure(800)
449+
expect(large / small).toBeGreaterThan(3)
450+
expect(large / small).toBeLessThan(6)
451+
})
452+
453+
it.each([
454+
['name="calculator"></invoke>'],
455+
["n", "a", "m", "e", "=", '"calculator"></invoke>'],
456+
["ordinary text"],
457+
['<function_calls><invoke name="calculator"></invoke></function_calls>'],
458+
])("preserves the whole parse after carried whitespace followed by %j", async (...ending) => {
459+
const parts = ["<ANTML:INVOKE", " ".repeat(80), "\t\n", ...ending]
460+
const text = parts.join("")
461+
const expected = extractLeakedToolCalls(text, new Set(["calculator"]))
462+
const chunks = await collect(parts)
463+
464+
expect(
465+
chunks
466+
.filter((chunk) => chunk.type === "text")
467+
.map((chunk) => chunk.text)
468+
.join(""),
469+
).toBe(expected.leftoverText)
470+
expect(chunks.filter((chunk) => chunk.type === "tool_call").map((chunk) => chunk.name)).toEqual(
471+
expected.calls.map((call) => call.name),
472+
)
473+
})
474+
475+
it("resumes immediate whitespace delivery after a native call flushes an unfinished invoke", async () => {
476+
const carried = "<invoke" + " ".repeat(80)
477+
streamMixedParts(["<invoke", " ".repeat(80), { name: "calculator", input: {} }, " \t", "tail"])
478+
const chunks = await drain()
479+
480+
expect(chunks.filter((chunk) => chunk.type !== "usage")).toEqual([
481+
{ type: "text", text: carried },
482+
{ type: "tool_call", id: "native-1", name: "calculator", arguments: "{}" },
483+
{ type: "text", text: " \t" },
484+
{ type: "text", text: "tail" },
485+
])
486+
})
487+
387488
it("emits a carried tail as plain text when it never becomes a marker", async () => {
388489
const chunks = await collect(["hello <par"])
389490

@@ -588,6 +689,20 @@ describe("VsCodeLmHandler", () => {
588689
expect(chunks.some((chunk) => chunk.type === "tool_call")).toBe(false)
589690
})
590691

692+
it("preserves a fenced wrapper when its fence precedes the marker in the same chunk", async () => {
693+
const text =
694+
'```xml\n<function_calls><invoke name="calculator"><parameter name="operation">add</parameter></invoke></function_calls>\n```'
695+
const chunks = await collect([text])
696+
697+
expect(chunks.some((chunk) => chunk.type === "tool_call")).toBe(false)
698+
expect(
699+
chunks
700+
.filter((chunk) => chunk.type === "text")
701+
.map((chunk) => chunk.text)
702+
.join(""),
703+
).toBe(text)
704+
})
705+
591706
it.each([
592707
{ name: "write_to_file", parameter: "content" },
593708
{ name: "update_todo_list", parameter: "todos" },

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
11431143
const salvageLeakedToolCalls = providedToolSchemas.size > 0
11441144
let salvageBuffer: string[] | undefined
11451145
let salvageCarry = ""
1146+
let salvageCarryIsInvokeWhitespace = false
11461147
let salvagePrecedingText: string[] = []
11471148
const salvageScan = new QuotingScanState()
11481149
let salvagedToolCallIndex = 0
@@ -1181,6 +1182,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
11811182
if (!salvageBuffer) {
11821183
const carried = salvageCarry
11831184
salvageCarry = ""
1185+
salvageCarryIsInvokeWhitespace = false
11841186
salvageScan.advance(carried)
11851187
return carried ? [{ type: "text", text: carried }] : []
11861188
}
@@ -1226,6 +1228,13 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
12261228
continue
12271229
}
12281230

1231+
// An unfinished invoke can carry unlimited whitespace; do not rescan it for every chunk.
1232+
if (salvageCarryIsInvokeWhitespace && /^\s*$/.test(chunk.value)) {
1233+
salvageCarry += chunk.value
1234+
continue
1235+
}
1236+
salvageCarryIsInvokeWhitespace = false
1237+
12291238
// Watch for the start of a leaked tool-call block, carrying a small tail across
12301239
// chunks so a marker split across chunk boundaries is still detected.
12311240
const combined = salvageCarry + chunk.value
@@ -1242,6 +1251,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
12421251
const carryLength = trailingPartialToolMarkerLength(combined)
12431252
const emit = carryLength > 0 ? combined.slice(0, combined.length - carryLength) : combined
12441253
salvageCarry = carryLength > 0 ? combined.slice(combined.length - carryLength) : ""
1254+
salvageCarryIsInvokeWhitespace = /^<(?:antml:)?invoke\s+$/i.test(salvageCarry)
12451255
if (emit) {
12461256
salvagePrecedingText.push(emit)
12471257
yield { type: "text", text: emit }

0 commit comments

Comments
 (0)