Skip to content

Commit 8a5594b

Browse files
author
Bertan Ari
committed
fix(vscode-lm): keep narration cue in the decided span past the salvage cap
Cutting the drained span at the last </invoke> dropped a trailing quoting cue, so an over-cap stream recovered a call the one-shot path refuses. Extend the span to the end of that line and wait while the line is still open. Also assert distinct ids for multiple recovered calls.
1 parent 71ddc7c commit 8a5594b

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

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

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,12 +587,45 @@ describe("VsCodeLmHandler", () => {
587587
expect(streamedText).toContain(filler)
588588
})
589589

590+
it("keeps a narrated block quoted when the cap splits the stream after its close tag", async () => {
591+
// Cutting the drained span at `</invoke>` hid the trailing cue, so the streaming path
592+
// recovered a call the one-shot path refuses.
593+
const block = `<function${"_calls"}><in${"voke"} name="calculator"><parameter name="operation">add</parameter></in${"voke"}>`
594+
const narration = ` is what you must never emit`
595+
const chunks = await collect([block + narration, "x".repeat(270 * 1024)])
596+
597+
expect(chunks.filter((chunk) => chunk.type === "tool_call")).toEqual([])
598+
expect(
599+
extractLeakedToolCalls(
600+
block + narration,
601+
new Map([["calculator", salvageTools[0].function.parameters]]),
602+
).calls,
603+
).toEqual([])
604+
})
605+
606+
it("gives each recovered call in one response a distinct id", async () => {
607+
const chunks = await collect([
608+
`<function${"_calls"}><in${"voke"} name="calculator"><parameter name="operation">add</parameter></in${"voke"}>\n` +
609+
`<in${"voke"} name="calculator"><parameter name="operation">sub</parameter></in${"voke"}></function${"_calls"}>`,
610+
])
611+
612+
const ids = chunks
613+
.filter((chunk) => chunk.type === "tool_call")
614+
.map((chunk) => (chunk as { id: string }).id)
615+
616+
expect(ids).toHaveLength(2)
617+
expect(new Set(ids).size).toBe(2)
618+
expect(ids[0]).toMatch(/-0$/)
619+
expect(ids[1]).toMatch(/-1$/)
620+
})
621+
590622
it("recovers a completed call and releases text when the buffer passes the cap", async () => {
591623
// The cap used to be bypassed whenever a complete block sat in the buffer, so both the
592-
// call and 512 KB of trailing prose were withheld until the stream ended.
624+
// call and 512 KB of trailing prose were withheld until the stream ended. The block
625+
// ends its line: same-line trailing prose reads as narration and suppresses recovery.
593626
const filler = "y".repeat(256 * 1024)
594627
const parts = [
595-
'<function_calls><invoke name="calculator"><parameter name="operation">add</parameter></invoke></function_calls>',
628+
'<function_calls><invoke name="calculator"><parameter name="operation">add</parameter></invoke></function_calls>\n',
596629
filler,
597630
filler,
598631
]

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ function lastCompleteInvokeBlockEnd(text: string): number {
128128
return end
129129
}
130130

131+
/**
132+
* End of the decided span: the newline after the last closed `<invoke>`, or 0 while that line is
133+
* still open. The quoting cue that suppresses recovery can trail the block on its own line, so
134+
* cutting at the tag would hide it and replay narrated markup as a live call.
135+
*/
136+
function decidedSalvageEnd(text: string): number {
137+
const blockEnd = lastCompleteInvokeBlockEnd(text)
138+
if (blockEnd === 0) {
139+
return 0
140+
}
141+
const lineEnd = text.indexOf("\n", blockEnd)
142+
return lineEnd === -1 ? 0 : lineEnd + 1
143+
}
144+
131145
/**
132146
* Left-to-right scan state behind the quoting heuristics: open code fence, current line start,
133147
* backticks seen on the current line, and whether a `<function_calls>` wrapper is open.
@@ -1235,7 +1249,7 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
12351249
if (salvageBuffer.length > MAX_SALVAGE_BUFFER_CHARS) {
12361250
// Past the cap, drain only the decided prefix: emitting the undecided tail as
12371251
// text would strand a call whose closing tag is still in flight.
1238-
const decidedEnd = lastCompleteInvokeBlockEnd(salvageBuffer)
1252+
const decidedEnd = decidedSalvageEnd(salvageBuffer)
12391253
if (decidedEnd > 0) {
12401254
const decided = salvageBuffer.slice(0, decidedEnd)
12411255
salvageBuffer = salvageBuffer.slice(decidedEnd)

0 commit comments

Comments
 (0)