Skip to content

Commit c8e1d13

Browse files
author
Bertan Ari
committed
fix: encode vscode-lm tool names with Copilot-permitted characters
1 parent 77f64e2 commit c8e1d13

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ describe("VsCodeLmHandler", () => {
331331
},
332332
},
333333
},
334+
{
335+
type: "function" as const,
336+
function: { name: "read\uD801file", description: "other" },
337+
},
334338
],
335339
})
336340
for await (const _chunk of stream) {
@@ -343,7 +347,10 @@ describe("VsCodeLmHandler", () => {
343347

344348
const requestOptions = mockLanguageModelChat.sendRequest.mock.calls[0][1]
345349
const sentTool = requestOptions.tools[0]
346-
expect(codeUnits(sentTool.name)).toEqual(codeUnits("read\uFFFDD800file"))
350+
// Copilot rejects declared tool names that do not match this pattern before sending.
351+
expect(sentTool.name).toMatch(/^[\w-]+$/)
352+
expect(codeUnits(sentTool.name)).toEqual(codeUnits("read_uD800file"))
353+
expect(requestOptions.tools[1].name).toBe("read_uD801file")
347354
expect(codeUnits(sentTool.description)).toEqual(codeUnits("desc\uFFFDription"))
348355
const schemaProperties = (
349356
sentTool.inputSchema as { properties: Record<string, { description: string }> }

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { ApiStream, ApiStreamChunk } from "../transform/stream"
1818
import {
1919
convertToVsCodeLmMessages,
2020
extractTextCountFromMessage,
21-
sanitizeIdentifierSurrogates,
2221
sanitizeSurrogates,
2322
sanitizeSurrogatesDeep,
23+
sanitizeToolNameSurrogates,
2424
} from "../transform/vscode-lm-format"
2525

2626
import { BaseProvider } from "./base-provider"
@@ -37,9 +37,8 @@ function convertToVsCodeLmTools(tools: OpenAI.Chat.ChatCompletionTool[]): vscode
3737
return tools
3838
.filter((tool) => tool.type === "function")
3939
.map((tool) => ({
40-
// Names use the injective identifier form so two tools differing only in a lone
41-
// surrogate cannot collapse into one and misroute the model's tool calls.
42-
name: sanitizeIdentifierSurrogates(tool.function.name),
40+
// Declared names must stay within Copilot's ^[\w-]+$ validation while remaining distinct.
41+
name: sanitizeToolNameSurrogates(tool.function.name),
4342
description: sanitizeSurrogates(tool.function.description || ""),
4443
inputSchema: tool.function.parameters
4544
? (sanitizeSurrogatesDeep(

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

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

1415
// Mock crypto using Vitest
@@ -823,6 +824,19 @@ describe("convertToVsCodeLmMessages surrogate-safe identifiers", () => {
823824
expect(codeUnits(sanitizeIdentifierSurrogates(`call-${VALID_PAIR}`))).toEqual(codeUnits(`call-${VALID_PAIR}`))
824825
})
825826

827+
it("encodes tool names within the permitted identifier character set without collisions", () => {
828+
const encoded = [`read${LONE_HIGH}file`, "read_uD800file", "readD800file", `read${LONE_LOW}file`].map((name) =>
829+
sanitizeToolNameSurrogates(name),
830+
)
831+
832+
for (const name of encoded) {
833+
expect(name).toMatch(/^[\w-]+$/)
834+
expectNoLoneSurrogate(name)
835+
}
836+
expect(new Set(encoded).size).toBe(encoded.length)
837+
expect(codeUnits(sanitizeToolNameSurrogates(`read-${VALID_PAIR}`))).toEqual(codeUnits(`read-${VALID_PAIR}`))
838+
})
839+
826840
it("leaves an id with no surrogates byte-identical", () => {
827841
const plainId = "toolu_01ABCDEF"
828842
const messages: Anthropic.Messages.MessageParam[] = [

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

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

65+
/**
66+
* Sanitizes a request tool-definition name.
67+
*
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.
71+
*/
72+
export function sanitizeToolNameSurrogates(name: string): string {
73+
// Escaping MUST precede encoding, or a literal "_u" would be indistinguishable from a marker.
74+
return name
75+
.replace(/_u/g, "_uu")
76+
.replace(LONE_SURROGATE, (unit) => `_u${unit.charCodeAt(0).toString(16).toUpperCase()}`)
77+
}
78+
6579
/**
6680
* Applies {@link sanitizeSurrogates} to every string nested in a tool-call argument object. The
6781
* backend rejects the whole request for a lone surrogate anywhere in the JSON payload, so a tool

0 commit comments

Comments
 (0)