From 77ab0d7c43619190948b74d78ce317821968277c Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 07:22:40 -0600 Subject: [PATCH] fix(aggregator): cleanText caps never split a surrogate pair (#7694) --- pm-stack/aggregator/src/actions.ts | 11 ++++++- pm-stack/aggregator/test/actions.test.ts | 38 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/pm-stack/aggregator/src/actions.ts b/pm-stack/aggregator/src/actions.ts index f8d7ed445..976cd715c 100644 --- a/pm-stack/aggregator/src/actions.ts +++ b/pm-stack/aggregator/src/actions.ts @@ -289,7 +289,16 @@ const SESSION_SOURCES = new Set(["opencode-sandbox", "opencode-laptop", "claude- function cleanText(v: unknown, max: number): string | undefined { if (typeof v !== "string") return undefined; const t = v.trim(); - return t === "" ? undefined : t.slice(0, max); + if (t === "") return undefined; + if (t.length <= max) return t; + // Keep the UTF-16 cap, but never leave half of a surrogate pair at the + // cut (#7694, mirroring the #3178 guard in pm-dispatch validate.ts): + // an astral character straddling the cap is dropped whole. + let end = max; + const before = t.charCodeAt(end - 1); + const after = t.charCodeAt(end); + if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) end--; + return t.slice(0, end); } /** Validate an untrusted POST body into a NewAction, or explain why not. */ diff --git a/pm-stack/aggregator/test/actions.test.ts b/pm-stack/aggregator/test/actions.test.ts index c230024d9..fa0a308ed 100644 --- a/pm-stack/aggregator/test/actions.test.ts +++ b/pm-stack/aggregator/test/actions.test.ts @@ -361,6 +361,44 @@ describe("dashboard actions validation", () => { expect(v({ objectRef: "has space" }).ok).toBe(false); }); + test("caps never split a surrogate pair (#7694)", () => { + // reason cap 2000: 1999 units + an astral character straddling the cap. + const straddle = validateNewAction({ + ...base, + verb: "dismiss", + payload: { reason: "x".repeat(1999) + "\u{1F600}" + "tail" }, + }); + expect(straddle.ok).toBe(true); + if (straddle.ok) { + const reason = (straddle.value.payload as { reason?: string }).reason ?? ""; + expect(reason.length).toBe(1999); + expect(reason.endsWith("x")).toBe(true); + } + // A pair that fits exactly inside the cap is kept whole. + const fits = validateNewAction({ + ...base, + verb: "dismiss", + payload: { reason: "x".repeat(1998) + "\u{1F600}" }, + }); + expect(fits.ok).toBe(true); + if (fits.ok) { + const reason = (fits.value.payload as { reason?: string }).reason ?? ""; + expect(reason.length).toBe(2000); + expect(reason.endsWith("\u{1F600}")).toBe(true); + } + // Small cap, same rule: optionId cap 200. + const opt = validateNewAction({ + ...base, + verb: "answer", + payload: { optionId: "o".repeat(199) + "\u{1F600}" + "tail" }, + }); + expect(opt.ok).toBe(true); + if (opt.ok) { + const optionId = (opt.value.payload as { optionId?: string }).optionId ?? ""; + expect(optionId.length).toBe(199); + } + }); + test("status-request: gh object, status required, targetRef derived", () => { const ok = validateNewAction({ clientToken: "ct-valid-token-2",