Skip to content

Commit 7e1a03e

Browse files
author
Bertan Ari
committed
fix(vscode-lm): round-trip tool names between declaration, call and history
Preserve valid names, encode only when needed, and decode returned call names so dispatch matches the registry. Document the lossy object-key behavior of sanitizeSurrogatesDeep.
1 parent 58427d4 commit 7e1a03e

4 files changed

Lines changed: 233 additions & 8 deletions

File tree

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,118 @@ describe("VsCodeLmHandler", () => {
285285
})
286286
})
287287

288+
it("returns the original registry name for a tool declared with an encoded name", async () => {
289+
const systemPrompt = "You are a helpful assistant"
290+
const originalName = `read\uD800file`
291+
292+
// The model echoes the DECLARED name; dispatch must still see the registry name.
293+
mockLanguageModelChat.sendRequest.mockImplementationOnce(async (_messages, options) => {
294+
const declaredName = options.tools[0].name
295+
return {
296+
stream: (async function* () {
297+
yield new vscode.LanguageModelToolCallPart("call-1", declaredName, { a: 1 })
298+
return
299+
})(),
300+
text: (async function* () {
301+
yield ""
302+
return
303+
})(),
304+
}
305+
})
306+
307+
const tools = [
308+
{
309+
type: "function" as const,
310+
function: { name: originalName, description: "d", parameters: { type: "object" } },
311+
},
312+
]
313+
314+
const chunks = []
315+
for await (const chunk of handler.createMessage(systemPrompt, [{ role: "user", content: "hi" }], {
316+
taskId: "test-task",
317+
tools,
318+
})) {
319+
chunks.push(chunk)
320+
}
321+
322+
const declaredName = mockLanguageModelChat.sendRequest.mock.calls[0][1].tools[0].name
323+
expect(declaredName).toBe("read_uD800file")
324+
expect(declaredName).toMatch(/^[\w-]+$/)
325+
326+
const toolCall = chunks.find((chunk) => chunk.type === "tool_call") as { name: string }
327+
expect(Array.from({ length: toolCall.name.length }, (_, index) => toolCall.name.charCodeAt(index))).toEqual(
328+
Array.from({ length: originalName.length }, (_, index) => originalName.charCodeAt(index)),
329+
)
330+
})
331+
332+
it("round-trips a surrogate-free name that looks like the encoding marker", async () => {
333+
const systemPrompt = "You are a helpful assistant"
334+
mockLanguageModelChat.sendRequest.mockImplementationOnce(async (_messages, options) => {
335+
const declaredName = options.tools[0].name
336+
return {
337+
stream: (async function* () {
338+
yield new vscode.LanguageModelToolCallPart("call-1", declaredName, {})
339+
return
340+
})(),
341+
text: (async function* () {
342+
yield ""
343+
return
344+
})(),
345+
}
346+
})
347+
348+
const chunks = []
349+
for await (const chunk of handler.createMessage(systemPrompt, [{ role: "user", content: "hi" }], {
350+
taskId: "test-task",
351+
tools: [
352+
{
353+
type: "function" as const,
354+
function: { name: "get_uuid", description: "d", parameters: { type: "object" } },
355+
},
356+
],
357+
})) {
358+
chunks.push(chunk)
359+
}
360+
361+
expect(mockLanguageModelChat.sendRequest.mock.calls[0][1].tools[0].name).toBe("get_uuuid")
362+
expect(chunks.find((chunk) => chunk.type === "tool_call")).toMatchObject({ name: "get_uuid" })
363+
})
364+
365+
it("preserves an ordinary tool name end to end", async () => {
366+
const systemPrompt = "You are a helpful assistant"
367+
mockLanguageModelChat.sendRequest.mockImplementationOnce(async (_messages, options) => {
368+
const declaredName = options.tools[0].name
369+
return {
370+
stream: (async function* () {
371+
yield new vscode.LanguageModelToolCallPart("call-1", declaredName, {})
372+
return
373+
})(),
374+
text: (async function* () {
375+
yield ""
376+
return
377+
})(),
378+
}
379+
})
380+
381+
const tools = [
382+
{
383+
type: "function" as const,
384+
function: { name: "get_user", description: "d", parameters: { type: "object" } },
385+
},
386+
]
387+
388+
const chunks = []
389+
for await (const chunk of handler.createMessage(systemPrompt, [{ role: "user", content: "hi" }], {
390+
taskId: "test-task",
391+
tools,
392+
})) {
393+
chunks.push(chunk)
394+
}
395+
396+
expect(mockLanguageModelChat.sendRequest.mock.calls[0][1].tools[0].name).toBe("get_user")
397+
expect(chunks.find((chunk) => chunk.type === "tool_call")).toMatchObject({ name: "get_user" })
398+
})
399+
288400
describe("system prompt sanitization", () => {
289401
it("sanitizes lone surrogates in the system prompt", async () => {
290402
mockLanguageModelChat.sendRequest.mockResolvedValueOnce({

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { normalizeToolSchema } from "../../utils/json-schema"
1717
import { ApiStream, ApiStreamChunk } from "../transform/stream"
1818
import {
1919
convertToVsCodeLmMessages,
20+
decodeToolNameSurrogates,
2021
extractTextCountFromMessage,
2122
sanitizeSurrogates,
2223
sanitizeSurrogatesDeep,
@@ -917,7 +918,8 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
917918
yield {
918919
type: "tool_call",
919920
id: chunk.callId,
920-
name: chunk.name,
921+
// Undo the declaration-time encoding; dispatch matches registry names.
922+
name: decodeToolNameSurrogates(chunk.name),
921923
arguments: argumentsString,
922924
}
923925
}

‎src/api/transform/__tests__/vscode-lm-format.spec.ts‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
extractTextCountFromMessage,
1010
sanitizeSurrogates,
1111
sanitizeIdentifierSurrogates,
12+
sanitizeSurrogatesDeep,
1213
sanitizeToolNameSurrogates,
14+
decodeToolNameSurrogates,
1315
} from "../vscode-lm-format"
1416

1517
// Mock crypto using Vitest
@@ -658,9 +660,10 @@ describe("convertToVsCodeLmMessages surrogate-safe identifiers", () => {
658660
const toolCall = (result[0].content as unknown as MockLanguageModelToolCallPart[])[0]
659661

660662
expect(toolCall.callId).toBe("call\uFFFDD8001")
661-
expect(toolCall.name).toBe("tool\uFFFDx")
663+
// Names use the declaration encoding so the replayed call matches a declared tool.
664+
expect(toolCall.name).toBe("tool_uDC00x")
662665
expect(codeUnits(toolCall.callId)).toContain(0xfffd)
663-
expect(codeUnits(toolCall.name)).toContain(0xfffd)
666+
expect(toolCall.name).toMatch(/^[\w-]+$/)
664667
expectNoLoneSurrogate(toolCall.callId)
665668
expectNoLoneSurrogate(toolCall.name)
666669
})
@@ -837,6 +840,65 @@ describe("convertToVsCodeLmMessages surrogate-safe identifiers", () => {
837840
expect(codeUnits(sanitizeToolNameSurrogates(`read-${VALID_PAIR}`))).toEqual(codeUnits(`read-${VALID_PAIR}`))
838841
})
839842

843+
it("leaves a valid tool name untouched so it still matches its registry entry", () => {
844+
for (const name of ["get_user", "read_file", "apply_diff", "update_todo_list"]) {
845+
expect(sanitizeToolNameSurrogates(name)).toBe(name)
846+
expect(decodeToolNameSurrogates(sanitizeToolNameSurrogates(name))).toBe(name)
847+
}
848+
})
849+
850+
it("round-trips encoded tool names back to the original registry name", () => {
851+
for (const name of [`read${LONE_HIGH}file`, `read${LONE_LOW}file`, "a_uu_b", "x_uD800y", "get_user"]) {
852+
const declared = sanitizeToolNameSurrogates(name)
853+
expect(declared).toMatch(/^[\w-]+$/)
854+
expect(codeUnits(decodeToolNameSurrogates(declared))).toEqual(codeUnits(name))
855+
}
856+
})
857+
858+
it("replays a tool call in history under the same name it was declared with", () => {
859+
const originalName = `read${LONE_HIGH}file`
860+
const messages: Anthropic.Messages.MessageParam[] = [
861+
{
862+
role: "assistant",
863+
content: [{ type: "tool_use", id: "toolu_1", name: originalName, input: {} }],
864+
},
865+
]
866+
867+
const toolCall = (
868+
convertToVsCodeLmMessages(messages)[0].content as unknown as MockLanguageModelToolCallPart[]
869+
)[0]
870+
871+
expect(toolCall.name).toBe(sanitizeToolNameSurrogates(originalName))
872+
expect(toolCall.name).toMatch(/^[\w-]+$/)
873+
expect(decodeToolNameSurrogates(toolCall.name)).toBe(originalName)
874+
})
875+
876+
it("keeps a valid history tool name unchanged", () => {
877+
const messages: Anthropic.Messages.MessageParam[] = [
878+
{
879+
role: "assistant",
880+
content: [{ type: "tool_use", id: "toolu_1", name: "get_user", input: {} }],
881+
},
882+
]
883+
884+
const toolCall = (
885+
convertToVsCodeLmMessages(messages)[0].content as unknown as MockLanguageModelToolCallPart[]
886+
)[0]
887+
888+
expect(toolCall.name).toBe("get_user")
889+
})
890+
891+
it("collapses argument keys differing only in their lone surrogate, last value winning", () => {
892+
// Pins the documented lossy-key limitation of sanitizeSurrogatesDeep.
893+
const collapsed = sanitizeSurrogatesDeep({ [`a${LONE_HIGH}`]: 1, [`a${LONE_LOW}`]: 2 }) as Record<
894+
string,
895+
unknown
896+
>
897+
898+
expect(Object.keys(collapsed)).toEqual(["a\uFFFD"])
899+
expect(collapsed["a\uFFFD"]).toBe(2)
900+
})
901+
840902
it("leaves an id with no surrogates byte-identical", () => {
841903
const plainId = "toolu_01ABCDEF"
842904
const messages: Anthropic.Messages.MessageParam[] = [

‎src/api/transform/vscode-lm-format.ts‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,71 @@ export function sanitizeIdentifierSurrogates(identifier: string): string {
6262
.replace(LONE_SURROGATE, (unit) => `\uFFFD${unit.charCodeAt(0).toString(16).toUpperCase()}`)
6363
}
6464

65+
/** Non-global twin of {@link LONE_SURROGATE}; `test` on a `/g` regex is stateful via `lastIndex`. */
66+
const HAS_LONE_SURROGATE = new RegExp(LONE_SURROGATE.source)
67+
68+
/** Marks a name whose `_u` sequences would otherwise be read as encoding markers when decoded. */
69+
const TOOL_NAME_MARKER = /_u(?:u|[0-9A-Fa-f]{4})/
70+
6571
/**
66-
* Sanitizes a request tool-definition name.
72+
* Sanitizes a request tool-definition name into Copilot's permitted `^[\w-]+$` alphabet.
6773
*
68-
* VS Code Copilot validates declared tool names against `^[\w-]+$` before sending the request, so
69-
* the U+FFFD produced by {@link sanitizeIdentifierSurrogates} would be rejected. Encoding each lone
70-
* surrogate as `_u<HEX>` stays inside the permitted set and keeps distinct names distinct.
74+
* The U+FFFD produced by {@link sanitizeIdentifierSurrogates} would be rejected by that validation,
75+
* so each lone surrogate is encoded as `_u<HEX>`. Names that neither carry a lone surrogate nor
76+
* could be mistaken for this encoding are returned unchanged, so an ordinary `get_user` reaches the
77+
* model under its registry name; {@link decodeToolNameSurrogates} inverts the encoded form. Outputs
78+
* of the two branches are disjoint — an encoded name always contains a marker, an untouched one
79+
* never does — which keeps the whole mapping injective and the decode unambiguous. A surrogate-free
80+
* name that merely looks like a marker (`get_uuid`) is still escaped, so it reaches the model
81+
* slightly altered; it round-trips correctly, so dispatch is unaffected.
7182
*/
7283
export function sanitizeToolNameSurrogates(name: string): string {
84+
if (!HAS_LONE_SURROGATE.test(name) && !TOOL_NAME_MARKER.test(name)) {
85+
return name
86+
}
7387
// Escaping MUST precede encoding, or a literal "_u" would be indistinguishable from a marker.
7488
return name
7589
.replace(/_u/g, "_uu")
7690
.replace(LONE_SURROGATE, (unit) => `_u${unit.charCodeAt(0).toString(16).toUpperCase()}`)
7791
}
7892

93+
/**
94+
* Inverts {@link sanitizeToolNameSurrogates} so a returned tool call carries the name the tool is
95+
* registered under, which is what dispatch matches on.
96+
*/
97+
export function decodeToolNameSurrogates(name: string): string {
98+
let decoded = ""
99+
let index = 0
100+
while (index < name.length) {
101+
if (name[index] === "_" && name[index + 1] === "u") {
102+
if (name[index + 2] === "u") {
103+
decoded += "_u"
104+
index += 3
105+
continue
106+
}
107+
const hex = name.slice(index + 2, index + 6)
108+
if (/^[0-9A-Fa-f]{4}$/.test(hex)) {
109+
decoded += String.fromCharCode(parseInt(hex, 16))
110+
index += 6
111+
continue
112+
}
113+
}
114+
decoded += name[index]
115+
index += 1
116+
}
117+
return decoded
118+
}
119+
79120
/**
80121
* Applies {@link sanitizeSurrogates} to every string nested in a tool-call argument object. The
81122
* backend rejects the whole request for a lone surrogate anywhere in the JSON payload, so a tool
82123
* argument carrying a sliced astral character fails the request just as message text would.
124+
*
125+
* LIMITATION: keys are sanitized with the same lossy mapping, so keys differing only in their lone
126+
* surrogate (`"a\uD800"`, `"a\uD801"`) both become `"a\uFFFD"` and the last value wins. This also
127+
* applies to tool schemas, where colliding property definitions collapse and `required` can end up
128+
* with duplicate entries. Accepted deliberately: the alternative is rewriting keys into a form no
129+
* schema reference would match, and a request that reaches the backend beats one rejected outright.
83130
*/
84131
export function sanitizeSurrogatesDeep(value: unknown): unknown {
85132
if (typeof value === "string") {
@@ -219,7 +266,9 @@ export function convertToVsCodeLmMessages(
219266
new vscode.LanguageModelToolCallPart(
220267
// Deterministic, so a call id and its paired tool_use_id stay equal after sanitizing.
221268
sanitizeIdentifierSurrogates(toolMessage.id),
222-
sanitizeSurrogates(toolMessage.name),
269+
// History MUST use the declaration encoding, or the replayed call names a tool
270+
// the model was never offered.
271+
sanitizeToolNameSurrogates(toolMessage.name),
223272
sanitizeSurrogatesDeep(asObjectSafe(toolMessage.input)) as object,
224273
),
225274
),

0 commit comments

Comments
 (0)