diff --git a/pm-stack/dispatch/src/validate.ts b/pm-stack/dispatch/src/validate.ts index 30922208ad..373c2f02ce 100644 --- a/pm-stack/dispatch/src/validate.ts +++ b/pm-stack/dispatch/src/validate.ts @@ -311,5 +311,16 @@ export function titleFromPrompt(prompt: string, max = 120): string { .split("\n") .map((l) => l.trim()) .find((l) => l.length > 0) ?? "dispatch request"; - return first.length > max ? first.slice(0, max - 1) + "…" : first; + if (first.length <= max) return first; + + // Keep the UTF-16 cap and suffix, but don't leave half of a surrogate pair. + let end = max - 1; + if (end > 0 && end < first.length) { + const before = first.charCodeAt(end - 1); + const after = first.charCodeAt(end); + if (before >= 0xd800 && before <= 0xdbff && after >= 0xdc00 && after <= 0xdfff) { + end--; + } + } + return first.slice(0, end) + "…"; } diff --git a/pm-stack/dispatch/test/validate.test.ts b/pm-stack/dispatch/test/validate.test.ts index a602eb6c68..7ba2b59456 100644 --- a/pm-stack/dispatch/test/validate.test.ts +++ b/pm-stack/dispatch/test/validate.test.ts @@ -174,6 +174,13 @@ describe("request validation", () => { test("titleFromPrompt takes the first non-empty line, capped", () => { expect(titleFromPrompt("\n\n fix the thing \nmore")).toBe("fix the thing"); expect(titleFromPrompt("x".repeat(200)).length).toBe(120); + expect(titleFromPrompt("x".repeat(200))).toBe("x".repeat(119) + "…"); + }); + + test("titleFromPrompt does not split astral code points when truncating", () => { + expect(titleFromPrompt("x".repeat(118) + "😀" + "y".repeat(10))).toBe("x".repeat(118) + "…"); + expect(titleFromPrompt("ab😀cde", 4)).toBe("ab…"); + expect(titleFromPrompt("a😀bcde", 4)).toBe("a😀…"); }); });