Skip to content

Commit e01ca01

Browse files
authored
fix(fx): preserve literal unicode escape text in clipped replies (pingdotgg#30701)
1 parent 0d266fb commit e01ca01

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it } from "@effect/vitest";
2+
3+
import { fxSubagentOutcome } from "./adapter.ts";
4+
5+
describe("fx subagent replies", () => {
6+
it.each(["", "1", "12", "123"])("preserves literal Unicode spelling ending in u%s", (digits) => {
7+
for (const slashes of [1, 2]) {
8+
const suffix = "\\".repeat(slashes) + "u" + digits;
9+
const padding = "x".repeat(
10+
200 - '{"ok":true,"result":"'.length - JSON.stringify(suffix).length + 2,
11+
);
12+
const reply = padding + suffix + "4 more text";
13+
const whole = JSON.stringify({ ok: true, result: reply });
14+
expect(fxSubagentOutcome(whole)).toEqual({ ok: true, result: reply });
15+
expect(fxSubagentOutcome(Buffer.from(whole).subarray(0, 200).toString("utf8"))).toEqual({
16+
ok: true,
17+
result: padding + suffix + "…",
18+
});
19+
}
20+
});
21+
22+
it.each([0, 1, 2])(
23+
"recovers incomplete Unicode escapes after %i literal backslashes",
24+
(slashes) => {
25+
for (const digits of ["", "0", "00", "000"]) {
26+
const prefix = "reply" + "\\".repeat(slashes * 2);
27+
expect(fxSubagentOutcome('{"ok":true,"result":"' + prefix + "\\u" + digits)).toEqual({
28+
ok: true,
29+
result: "reply" + "\\".repeat(slashes) + "…",
30+
});
31+
}
32+
},
33+
);
34+
35+
it.each([
36+
['{"ok":true,"result":"hello', { ok: true, result: "hello…" }],
37+
['{"ok":false,"result":null}', { ok: false, result: null }],
38+
['{"ok":true,"result":"hello"', { ok: true, result: "hello" }],
39+
['{"unexpected":"body"}', undefined],
40+
['{"ok":true,"result":"\\q', { ok: true, result: null }],
41+
])("retains existing normalization for %s", (text, expected) => {
42+
expect(fxSubagentOutcome(text)).toEqual(expected);
43+
});
44+
});

‎packages/provider-fx/src/server/adapter.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ const decodeJsonString = Schema.decodeUnknownOption(Schema.fromJsonString(Schema
102102
* fx clips every tool result to 199 bytes, so a long sub-agent reply arrives
103103
* as cut-off `{"ok":…,"result":"…` JSON. Keeps the readable prefix and marks the cut.
104104
*/
105-
function fxSubagentOutcome(
105+
export function fxSubagentOutcome(
106106
text: string,
107107
): { readonly ok: boolean; readonly result: string | null } | undefined {
108108
const whole = decodeFxSubagentResult(text);
109109
if (Option.isSome(whole)) return whole.value;
110110
const clipped = /^\{"ok":(true|false),"result":"((?:[^"\\]|\\.)*)/.exec(text);
111111
if (clipped === null) return undefined;
112-
const prefix = decodeJsonString(`"${(clipped[2] ?? "").replace(/\\u[0-9a-fA-F]{0,3}$/, "")}"`);
112+
const fragment = clipped[2] ?? "";
113+
const prefix = decodeJsonString(`"${fragment}"`).pipe(
114+
Option.orElse(() => decodeJsonString(`"${fragment.replace(/\\u[0-9a-fA-F]{0,3}$/, "")}"`)),
115+
);
113116
const cut = text[clipped[0].length] !== '"';
114117
return {
115118
ok: clipped[1] === "true",

0 commit comments

Comments
 (0)