Skip to content

Commit 70177fd

Browse files
author
Bertan Ari
committed
fix(vscode-lm): keep invoke bodies out of quoting state and fail closed on unclosed params
1 parent f880a9d commit 70177fd

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,29 @@ describe("leaked tool-call recovery", () => {
12761276
expect(calls).toHaveLength(0)
12771277
expect(leftoverText).toBe(text)
12781278
})
1279+
1280+
it("does not let a wrapper marker quoted inside an unrecovered invoke body arm a later bare invoke", () => {
1281+
// A wrapper tag is only real when it appears outside an invoke body, otherwise quoted
1282+
// markup in one block can authorize recovery of an unwrapped block after it.
1283+
const text =
1284+
invoke("some_other_tool", `<function${"_calls"}>`) +
1285+
"\n" +
1286+
invoke("update_todo_list", param("todos", "[x] one"))
1287+
1288+
const { calls, leftoverText } = extractLeakedToolCalls(text, new Set(["update_todo_list"]))
1289+
1290+
expect(calls).toHaveLength(0)
1291+
expect(leftoverText).toBe(text)
1292+
})
1293+
1294+
it("fails closed on an invoke whose parameter markup is left unclosed", () => {
1295+
// Partially parsed parameters would dispatch a call missing arguments the model wrote.
1296+
const text = wrap(invoke("update_todo_list", `<param${"eter"} name="todos">[x] one`))
1297+
1298+
const { calls } = extractLeakedToolCalls(text, new Set(["update_todo_list"]))
1299+
1300+
expect(calls).toHaveLength(0)
1301+
})
12791302
})
12801303

12811304
// The bare-invoke cases above short-circuit at the wrapper check, so they never exercise the

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,20 @@ function parseLeakedInvokeParams(
368368
): Record<string, unknown> | undefined {
369369
const input: Record<string, unknown> = {}
370370
const paramPattern = /<(?:antml:)?parameter\s+name="([^"]+)"\s*>([\s\S]*?)<\/(?:antml:)?parameter\s*>/gi
371+
let consumedUpTo = 0
371372
for (const match of body.matchAll(paramPattern)) {
372373
const name = match[1]
373374
const converted = convertLeakedParamValue(match[2].trim(), declaredParamType(schema, name))
374375
if (!converted) {
375376
return undefined
376377
}
377378
input[name] = converted.value
379+
consumedUpTo = (match.index ?? 0) + match[0].length
380+
}
381+
// An unclosed parameter tag is skipped by the pattern above, so recovering would dispatch a
382+
// call missing an argument the model wrote; fail closed instead.
383+
if (/<(?:antml:)?parameter\b/i.test(body.slice(consumedUpTo))) {
384+
return undefined
378385
}
379386
return input
380387
}
@@ -419,7 +426,9 @@ export function extractLeakedToolCalls(
419426
leftover += text.slice(lastIndex, open.index)
420427
const name = open[1]
421428
scan.advance(text.slice(scannedUpTo, open.index))
422-
scannedUpTo = open.index
429+
// Only text outside invoke bodies may move parser state, otherwise markup quoted in one
430+
// block could open a wrapper or fence that changes the verdict on a later block.
431+
scannedUpTo = blockEnd
423432
const recoverable =
424433
validTools.has(name) && scan.isInsideFunctionCallsWrapper() && !isQuotedAsCode(text, blockEnd, scan)
425434
// Parsing may still fail closed when a parameter doesn't match its declared type.

0 commit comments

Comments
 (0)