Skip to content

Commit e594ff4

Browse files
committed
fix(scope): drop out-of-scope sanitize-surrogates changes
The cycle-detection change to sanitizeSurrogatesDeep (and its spec) rode in on an earlier shapes/upstream-main merge (c94e614) and is unrelated to the Opencode Go abort/timeout unit; CodeRabbit's Out-of-Scope pre-merge check flagged it as an error. Restore both files to main's state so the PR diff is exactly the unit's two files. The listener-detach regression test used a self-referential tool input to fail the message conversion, which only fails deterministically with that cycle detection; switch it to a throwing value getter (the test's stated "unstringifiable tool input" scenario) so the unit no longer depends on the out-of-scope change. 147/147 green; tsc/eslint/prettier clean.
1 parent 6255ad9 commit e594ff4

3 files changed

Lines changed: 25 additions & 49 deletions

File tree

‎src/api/providers/__tests__/opencode-go.spec.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,13 @@ describe("OpencodeGoHandler", () => {
900900
// request; the conversion must run inside the same cleanup scope
901901
// as the Anthropic and Responses branches so an unstringifiable
902902
// tool input does not leave the bridged listener attached.
903-
const circularInput: Record<string, unknown> = {}
904-
circularInput.self = circularInput
903+
// A throwing value getter fails the conversion deterministically
904+
// without depending on cycle detection in sanitizeSurrogatesDeep.
905+
const failingInput: Record<string, unknown> = {
906+
get value(): never {
907+
throw new Error("unstringifiable tool input")
908+
},
909+
}
905910

906911
const handler = new OpencodeGoHandler(mockOptions)
907912
const controller = new AbortController()
@@ -914,7 +919,7 @@ describe("OpencodeGoHandler", () => {
914919
{ role: "user", content: "hi" },
915920
{
916921
role: "assistant",
917-
content: [{ type: "tool_use", id: "tu_1", name: "calc", input: circularInput }],
922+
content: [{ type: "tool_use", id: "tu_1", name: "calc", input: failingInput }],
918923
},
919924
],
920925
makeCreateMessageMetadata({ abortSignal: controller.signal }),
@@ -927,7 +932,7 @@ describe("OpencodeGoHandler", () => {
927932
// The conversion failure propagates unchanged (the signal never
928933
// aborted) and the bridge listener is detached by reference.
929934
expect(error).toBeInstanceOf(Error)
930-
expect((error as Error).message).toMatch(/circular/i)
935+
expect((error as Error).message).toMatch(/unstringifiable tool input/)
931936
const abortAddCalls = addEventListenerSpy.mock.calls.filter(([event]) => event === "abort")
932937
const addedListener = abortAddCalls[abortAddCalls.length - 1]?.[1]
933938
expect(typeof addedListener).toBe("function")

‎src/api/transform/__tests__/sanitize-surrogates.spec.ts‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,4 @@ describe("sanitizeSurrogatesDeep", () => {
8686
expect(sanitizeSurrogatesDeep(undefined)).toBe(undefined)
8787
expect(sanitizeSurrogatesDeep(true)).toBe(true)
8888
})
89-
90-
it("reports a cycle instead of recursing to the stack limit", () => {
91-
const circular: Record<string, unknown> = {}
92-
circular.self = circular
93-
expect(() => sanitizeSurrogatesDeep(circular)).toThrow(/circular/)
94-
})
95-
96-
it("still sanitizes a node reached twice, which is not a cycle", () => {
97-
const shared = { path: "bad\uD800end" }
98-
expect(sanitizeSurrogatesDeep({ a: shared, b: shared })).toEqual({
99-
a: { path: "bad\uFFFDend" },
100-
b: { path: "bad\uFFFDend" },
101-
})
102-
})
103-
it("still sanitizes a shared array reached twice, which is not a cycle", () => {
104-
const lone = "bad\uD800end"
105-
const sanitized = "bad\uFFFDend"
106-
const shared = [lone]
107-
// The array branch has its own cleanup; without it the second visit would
108-
// be read as a cycle and throw.
109-
expect(sanitizeSurrogatesDeep({ a: shared, b: shared })).toEqual({
110-
a: [sanitized],
111-
b: [sanitized],
112-
})
113-
})
11489
})

‎src/api/transform/sanitize-surrogates.ts‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
* task. These helpers replace lone surrogates with U+FFFD at the request boundary while leaving
1010
* valid surrogate pairs untouched.
1111
*/
12+
1213
/**
1314
* Matches unpaired UTF-16 surrogate code units. Valid surrogate pairs are matched by the
1415
* lookahead/lookbehind and left untouched. The regex intentionally omits the `u` flag so it
1516
* operates on UTF-16 code units.
1617
*/
1718
export const LONE_SURROGATE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g
19+
1820
/**
1921
* Replaces unpaired UTF-16 surrogate code units with the Unicode replacement character (U+FFFD).
2022
*/
@@ -24,6 +26,7 @@ export function sanitizeSurrogates(text: string): string {
2426
}
2527
return text.replace(LONE_SURROGATE, "\uFFFD")
2628
}
29+
2730
/**
2831
* Sanitizes a tool call / tool result identifier without losing its distinctness.
2932
*
@@ -38,8 +41,10 @@ export function sanitizeIdentifierSurrogates(identifier: string): string {
3841
.replace(/\uFFFD/g, "\uFFFDFFFD")
3942
.replace(LONE_SURROGATE, (unit) => `\uFFFD${unit.charCodeAt(0).toString(16).toUpperCase()}`)
4043
}
44+
4145
/** Non-global twin of {@link LONE_SURROGATE}; `test` on a `/g` regex is stateful via `lastIndex`. */
4246
export const HAS_LONE_SURROGATE = new RegExp(LONE_SURROGATE.source)
47+
4348
/**
4449
* Applies {@link sanitizeSurrogates} to every string nested in a tool-call argument object. The
4550
* backend rejects the whole request for a lone surrogate anywhere in the JSON payload, so a tool
@@ -51,29 +56,20 @@ export const HAS_LONE_SURROGATE = new RegExp(LONE_SURROGATE.source)
5156
* with duplicate entries. Accepted deliberately: the alternative is rewriting keys into a form no
5257
* schema reference would match, and a request that reaches the backend beats one rejected outright.
5358
*/
54-
export function sanitizeSurrogatesDeep(value: unknown, path = new Set<unknown>()): unknown {
59+
export function sanitizeSurrogatesDeep(value: unknown): unknown {
5560
if (typeof value === "string") {
5661
return sanitizeSurrogates(value)
5762
}
58-
if (typeof value !== "object") {
59-
return value
60-
}
61-
if (value === null) {
62-
return value
63-
}
64-
if (path.has(value)) {
65-
throw new TypeError("Converting circular structure to JSON")
66-
}
67-
path.add(value)
6863
if (Array.isArray(value)) {
69-
const items = value.map((item) => sanitizeSurrogatesDeep(item, path))
70-
path.delete(value)
71-
return items
64+
return value.map(sanitizeSurrogatesDeep)
65+
}
66+
if (value && typeof value === "object") {
67+
return Object.fromEntries(
68+
Object.entries(value as Record<string, unknown>).map(([key, nested]) => [
69+
sanitizeSurrogates(key),
70+
sanitizeSurrogatesDeep(nested),
71+
]),
72+
)
7273
}
73-
const entries = Object.entries(value as Record<string, unknown>).map(([key, nested]) => [
74-
sanitizeSurrogates(key),
75-
sanitizeSurrogatesDeep(nested, path),
76-
])
77-
path.delete(value)
78-
return Object.fromEntries(entries)
74+
return value
7975
}

0 commit comments

Comments
 (0)