diff --git a/pm-stack/aggregator/src/activity.ts b/pm-stack/aggregator/src/activity.ts index 8189b0533a..9b65d67a8a 100644 --- a/pm-stack/aggregator/src/activity.ts +++ b/pm-stack/aggregator/src/activity.ts @@ -103,6 +103,19 @@ export const ACTIVITY_SOURCE_COVERAGE: readonly ActivityCoverage[] = [ }, ]; +// Cap `t` at `max` UTF-16 code units, but never leave half of a surrogate +// pair at the cut (#7725, mirroring the #7694/#7711 capUnits guard in +// actions.ts): an astral character straddling the cap is dropped whole, so +// a persisted summary can never end in a lone high surrogate. +function capUnits(t: string, max: number): string { + if (t.length <= max) return t; + 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); +} + // -- writers: pure mappings from source observations to deliveries ------- /** Map watchdog tier items to deliveries, one per breach episode. @@ -117,7 +130,7 @@ export function watchdogDeliveries(items: readonly TierItem[]): ActivityDelivery at: item.since, kind: "watchdog-event", ref: item.id, - summary: item.title.slice(0, 500), + summary: capUnits(item.title, 500), })); } @@ -193,7 +206,7 @@ export function kaneoChangeDeliveries( at: card.updatedAt ?? new Date().toISOString(), kind: "kaneo-change", ref: card.id, - summary: `Kaneo card appeared in ${card.status}: ${card.title}`.slice(0, 500), + summary: capUnits(`Kaneo card appeared in ${card.status}: ${card.title}`, 500), ...(card.kaneoUrl ? { link: card.kaneoUrl } : {}), }); } else if (before.status !== card.status) { @@ -204,7 +217,7 @@ export function kaneoChangeDeliveries( at: card.updatedAt ?? new Date().toISOString(), kind: "kaneo-change", ref: card.id, - summary: `Kaneo card moved ${before.status} -> ${card.status}: ${card.title}`.slice(0, 500), + summary: capUnits(`Kaneo card moved ${before.status} -> ${card.status}: ${card.title}`, 500), ...(card.kaneoUrl ? { link: card.kaneoUrl } : {}), }); } @@ -482,7 +495,7 @@ export class IngestionStore { actor: d.source, kind: d.kind, ref: d.ref, - summary: (d.link ? `${d.summary} — ${d.link}` : d.summary).slice(0, 500), + summary: capUnits(d.link ? `${d.summary} — ${d.link}` : d.summary, 500), }); } catch (err) { failed++; diff --git a/pm-stack/aggregator/test/activity-3321.test.ts b/pm-stack/aggregator/test/activity-3321.test.ts index 153631c23b..3ab6d1d43b 100644 --- a/pm-stack/aggregator/test/activity-3321.test.ts +++ b/pm-stack/aggregator/test/activity-3321.test.ts @@ -343,3 +343,55 @@ describe("ingestion store (#3321)", () => { expect(html).toContain("unsupported"); }); }); + +describe("activity summary caps never split a surrogate pair (#7725)", () => { + const roundTrips = (s: string) => new TextDecoder().decode(new TextEncoder().encode(s)) === s; + + test("watchdog writer drops an astral character straddling the 500 cap whole", () => { + const [d] = watchdogDeliveries([watchdogItem("watchdog:emoji", 60_000, "x".repeat(499) + "\u{1F600}" + "tail")]); + expect(d?.summary).toHaveLength(499); + expect(d?.summary.endsWith("x")).toBe(true); + expect(roundTrips(d?.summary ?? "")).toBe(true); + }); + + test("watchdog writer keeps an astral character fully inside the cap intact", () => { + const [d] = watchdogDeliveries([watchdogItem("watchdog:emoji-fit", 60_000, "x".repeat(498) + "\u{1F600}")]); + expect(d?.summary).toHaveLength(500); + expect(d?.summary.endsWith("\u{1F600}")).toBe(true); + expect(roundTrips(d?.summary ?? "")).toBe(true); + }); + + test("kaneo writer drops an astral character straddling the composed 500 cap whole", () => { + const prefix = "Kaneo card appeared in open: "; + const card: KaneoCardSnapshot = { + id: "kaneo:p/emoji", + status: "open", + title: "y".repeat(500 - prefix.length - 1) + "\u{1F600}" + "tail", + updatedAt: iso(60_000), + }; + const [d] = kaneoChangeDeliveries(new Map(), [card]); + expect(d?.summary).toHaveLength(499); + expect(d?.summary.endsWith("y")).toBe(true); + expect(roundTrips(d?.summary ?? "")).toBe(true); + }); + + test("record-time cap drops an astral character the link suffix pushes across 500", async () => { + const { events, store } = tmpStores(); + const delivery: ActivityDelivery = { + source: "kaneo", + sourceId: "kaneo:p/emoji:appeared:undated:1", + at: iso(0), + kind: "kaneo-change", + ref: "kaneo:p/emoji", + summary: "s".repeat(494), + link: "ab\u{1F600}cd", + }; + const result = await store.ingest(events, [delivery]); + expect(result.ingested).toBe(1); + const stream = await events.list(); + expect(stream).toHaveLength(1); + expect(stream[0]?.summary).toHaveLength(499); + expect(stream[0]?.summary.endsWith("b")).toBe(true); + expect(roundTrips(stream[0]?.summary ?? "")).toBe(true); + }); +});