Skip to content

Commit de43c07

Browse files
author
Bertan Ari
committed
fix(vscode-lm): keep middleOutTruncate within maxChars for small limits
1 parent db63951 commit de43c07

2 files changed

Lines changed: 44 additions & 9 deletions

File tree

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,6 +2677,33 @@ describe("context-window tool_result truncation", () => {
26772677
expect(middleOutTruncate("hello world", 100)).toBe("hello world")
26782678
})
26792679

2680+
it("returns text unchanged when its length exactly equals the limit", () => {
2681+
// Longer than the truncation marker, so `<=` is observable here: the marker-less
2682+
// fallback for tiny limits would otherwise return the same text under either operator.
2683+
const text = "A".repeat(200)
2684+
expect(middleOutTruncate(text, 200)).toBe(text)
2685+
})
2686+
2687+
it("stays within a limit too small to hold the truncation marker", () => {
2688+
const text = "A".repeat(500)
2689+
for (const limit of [1, 5, 50]) {
2690+
const result = middleOutTruncate(text, limit)
2691+
expect(result).toBe("A".repeat(limit))
2692+
}
2693+
})
2694+
2695+
it("does not end a marker-less truncation on a lone high surrogate", () => {
2696+
// Index-based so a surrogate pair contributes BOTH of its code units to the assertion.
2697+
const codeUnits = (value: string): number[] =>
2698+
Array.from({ length: value.length }, (_, index) => value.charCodeAt(index))
2699+
2700+
// The limit lands between the halves of the pair at index 3, which would otherwise be
2701+
// emitted alone and make the payload un-encodable as UTF-8.
2702+
const result = middleOutTruncate(`abc\uD83D\uDE00${"z".repeat(200)}`, 4)
2703+
2704+
expect(codeUnits(result)).toEqual(codeUnits("abc"))
2705+
})
2706+
26802707
it("keeps the head and tail and inserts a truncation marker", () => {
26812708
const text = "A".repeat(500) + "B".repeat(500)
26822709
const result = middleOutTruncate(text, 200)
@@ -2769,9 +2796,9 @@ describe("context-window tool_result truncation", () => {
27692796

27702797
const toolResult = findBlock(messages[1], "tool_result")
27712798
const parts = toolResult.content as Array<{ type: string; text?: string }>
2772-
expect(parts[0].type).toBe("text")
27732799
expect(parts[0].text).toContain("characters truncated")
2774-
expect(parts.some((part) => part.type === "image")).toBe(true)
2800+
// Exact shape: a surviving original text part would leave a third element behind.
2801+
expect(parts.map((part) => part.type)).toEqual(["text", "image"])
27752802
})
27762803

27772804
it("ignores string content and skips messages that cannot hold tool_result blocks", () => {

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ function writeToolResultText(block: Anthropic.Messages.ContentBlockParam, text:
434434
* middle with a marker noting how many characters were removed. Head/tail are preserved because
435435
* logs and file dumps carry the most signal at their start (structure) and end (recent output).
436436
*/
437+
/**
438+
* Drops a trailing lone high surrogate, whose low half was cut away. A lone surrogate cannot be
439+
* encoded as UTF-8, and the backend 400s the whole request when one is present.
440+
*/
441+
function trimTrailingHighSurrogate(text: string): string {
442+
return text.length > 0 && (text.charCodeAt(text.length - 1) & 0xfc00) === 0xd800 ? text.slice(0, -1) : text
443+
}
444+
437445
export function middleOutTruncate(text: string, maxChars: number): string {
438446
if (maxChars <= 0) {
439447
return ""
@@ -447,15 +455,15 @@ export function middleOutTruncate(text: string, maxChars: number): string {
447455

448456
// Reserve room for the marker, sized against the original length so the result never grows.
449457
const reservedMarkerLength = buildMarker(text.length).length
450-
const keep = Math.max(0, maxChars - reservedMarkerLength)
458+
// A budget too small to hold the marker cannot describe its own truncation without breaking the
459+
// maxChars promise this function makes to callers, so drop the marker and keep a bare head.
460+
if (maxChars <= reservedMarkerLength) {
461+
return trimTrailingHighSurrogate(text.slice(0, maxChars))
462+
}
463+
const keep = maxChars - reservedMarkerLength
451464
const headLength = Math.ceil(keep / 2)
452465
const tailLength = keep - headLength
453-
let head = text.slice(0, headLength)
454-
// Don't end the head on a lone high surrogate — its low half is in the removed middle, and a lone
455-
// surrogate cannot be encoded as UTF-8 (the backend 400s the whole request). Drop the split half.
456-
if (head.length > 0 && (head.charCodeAt(head.length - 1) & 0xfc00) === 0xd800) {
457-
head = head.slice(0, -1)
458-
}
466+
const head = trimTrailingHighSurrogate(text.slice(0, headLength))
459467
let tail = tailLength > 0 ? text.slice(text.length - tailLength) : ""
460468
// Likewise, don't start the tail on a lone low surrogate (its high half is in the removed middle).
461469
if (tail.length > 0 && (tail.charCodeAt(0) & 0xfc00) === 0xdc00) {

0 commit comments

Comments
 (0)