diff --git a/server/src/__tests__/tool-content-guards.test.ts b/server/src/__tests__/tool-content-guards.test.ts index b5e26475ca1..65d054c7412 100644 --- a/server/src/__tests__/tool-content-guards.test.ts +++ b/server/src/__tests__/tool-content-guards.test.ts @@ -48,6 +48,14 @@ describe("tool content guards", () => { expect(identity).not.toBe(createHmac("sha256", signingSecret).update(canonicalToolArguments(first)).digest("hex")); }); + it("truncates audit summaries without splitting an emoji, so jsonb accepts them", () => { + // `{"text":"` is 9 code units, so the cut at 3997 lands between the emoji's surrogate halves. + const value = { text: `${"x".repeat(3987)}${"\u{1F600}".repeat(20)}` }; + const { summary } = summarizeToolValue(value); + expect(summary).toBe(`{"text":"${"x".repeat(3987)}...`); + expect(summary).not.toMatch(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])/); + }); + it("includes values beyond audit preview truncation in request identities", () => { const prefix = "x".repeat(5000); expect(toolRequestIdentityHash({ body: `${prefix}first` })).not.toBe(toolRequestIdentityHash({ body: `${prefix}second` })); diff --git a/server/src/services/tool-content-guards.ts b/server/src/services/tool-content-guards.ts index 8ed7ee1ca31..27c9aeecf16 100644 --- a/server/src/services/tool-content-guards.ts +++ b/server/src/services/tool-content-guards.ts @@ -295,12 +295,22 @@ export function readSignedToolArguments(input: { return readSignedToolArgumentsPayload(input)?.arguments ?? null; } +/** + * Cuts to at most `max` UTF-16 code units without splitting a surrogate pair. + * Audit summaries are stored in `jsonb`, which rejects a lone surrogate, so a + * split emoji would fail the audit insert and with it the tool call. + */ +function cutWholeCharacters(text: string, max: number): string { + const last = text.charCodeAt(max - 1); + return text.slice(0, last >= 0xd800 && last <= 0xdbff ? max - 1 : max); +} + export function summarizeToolValue(value: unknown) { const redacted = isPlainObject(value) ? redactEventPayload(value) : value; const serialized = stableSerialize(redacted); const redactedText = redactSensitiveText(serialized); return { - summary: redactedText.length > 4000 ? `${redactedText.slice(0, 3997)}...` : redactedText, + summary: redactedText.length > 4000 ? `${cutWholeCharacters(redactedText, 3997)}...` : redactedText, sizeBytes: Buffer.byteLength(serialized, "utf8"), sha256: createHash("sha256").update(serialized).digest("hex"), redactedFields: redactedText.includes(REDACTED_EVENT_VALUE) ? ["sensitive_value"] : [],