From bcfcd143650fc9a31571687fcaa741534c63bd47 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 07:49:19 -0600 Subject: [PATCH] fix(aggregator): markResult caps never split a surrogate pair (#7711) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit markResultTransition capped appliedByRun/resultNote with raw slice, storing a lone high surrogate whenever an astral character straddled the 200 / 2000 caps — the #7694 defect class on the result path. Route both caps through the shared capUnits guard extracted from cleanText, and add regression tests (straddle at both caps dropped whole, exact-fit pair kept whole; new test fails pre-fix). --- pm-stack/aggregator/src/actions.ts | 25 +++++++++++++-------- pm-stack/aggregator/test/actions.test.ts | 28 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/pm-stack/aggregator/src/actions.ts b/pm-stack/aggregator/src/actions.ts index 976cd715c8..93bedc3a22 100644 --- a/pm-stack/aggregator/src/actions.ts +++ b/pm-stack/aggregator/src/actions.ts @@ -286,14 +286,11 @@ function answerQuestionIds(action: Pick): string[ const SESSION_SOURCES = new Set(["opencode-sandbox", "opencode-laptop", "claude-code", "cron"]); -function cleanText(v: unknown, max: number): string | undefined { - if (typeof v !== "string") return undefined; - const t = v.trim(); - if (t === "") return undefined; +// Cap `t` at `max` UTF-16 code units, 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. +function capUnits(t: string, max: number): string { 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); @@ -301,6 +298,13 @@ function cleanText(v: unknown, max: number): string | undefined { return t.slice(0, end); } +function cleanText(v: unknown, max: number): string | undefined { + if (typeof v !== "string") return undefined; + const t = v.trim(); + if (t === "") return undefined; + return capUnits(t, max); +} + /** Validate an untrusted POST body into a NewAction, or explain why not. */ export function validateNewAction(body: unknown): { ok: true; value: NewAction } | { ok: false; error: string } { if (typeof body !== "object" || body === null) return { ok: false, error: "body must be a JSON object" }; @@ -794,8 +798,11 @@ export class ActionsStore { if (a.state !== "new") return { action: a, transitioned: false }; a.state = result.outcome; a.appliedAt = new Date().toISOString(); - a.appliedByRun = result.byRun.slice(0, 200); - if (result.note) a.resultNote = result.note.slice(0, 2_000); + // Same surrogate-safe cap as cleanText (#7711): a raw slice here stored + // a lone high surrogate in appliedByRun/resultNote whenever an astral + // character straddled the cap. + a.appliedByRun = capUnits(result.byRun, 200); + if (result.note) a.resultNote = capUnits(result.note, 2_000); await this.persist(); return { action: a, transitioned: true }; } diff --git a/pm-stack/aggregator/test/actions.test.ts b/pm-stack/aggregator/test/actions.test.ts index fa0a308ed9..bc0b715c2d 100644 --- a/pm-stack/aggregator/test/actions.test.ts +++ b/pm-stack/aggregator/test/actions.test.ts @@ -88,6 +88,34 @@ describe("dashboard actions store", () => { expect(retry?.action.state).toBe("applied"); }); + test("markResult caps never split a surrogate pair (#7711)", async () => { + const dir = mkdtempSync(join(tmpdir(), "agg-actions-result-surrogate-")); + const store = new ActionsStore(join(dir, "actions.json")); + // byRun cap 200: 199 units + an astral character straddling the cap is + // dropped whole, never stored as a lone high surrogate. + const a1 = await store.add(newAction({ objectRef: "orch:item-result-byRun" })); + const byRun = await store.markResult(a1.action.id, { + outcome: "applied", + byRun: "r".repeat(199) + "\u{1F600}" + "tail", + }); + expect(byRun?.appliedByRun?.length).toBe(199); + expect(byRun?.appliedByRun?.endsWith("r")).toBe(true); + // note cap 2000: same rule at the larger cap. + const a2 = await store.add(newAction({ objectRef: "orch:item-result-note" })); + const noted = await store.markResult(a2.action.id, { + outcome: "applied", + byRun: "orch-test", + note: "n".repeat(1999) + "\u{1F600}" + "tail", + }); + expect(noted?.resultNote?.length).toBe(1999); + expect(noted?.resultNote?.endsWith("n")).toBe(true); + // A pair that fits exactly inside the cap is kept whole. + const a3 = await store.add(newAction({ objectRef: "orch:item-result-fit" })); + const fit = "n".repeat(1998) + "\u{1F600}"; + const fitted = await store.markResult(a3.action.id, { outcome: "applied", byRun: "orch-test", note: fit }); + expect(fitted?.resultNote).toBe(fit); + }); + test("answer question slots dedupe option and survey submissions with an audit pointer", async () => { const dir = mkdtempSync(join(tmpdir(), "agg-answer-dedupe-")); const store = new ActionsStore(join(dir, "actions.json"));