Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(summary): truncate synopsis documents on a surrogate boundary (#5316)
Devin AIc574b0cfb9f8
diff --git a/src/core/page-summary.ts b/src/core/page-summary.tsindex d861d2578c..e50e052eec 100644--- a/src/core/page-summary.ts+++ b/src/core/page-summary.ts@@ -34,6 +34,7 @@ import { chat, type ChatOpts, type ChatResult } from './ai/gateway.ts';import { logSynopsisFailure, type SynopsisFailureKind } from './audit-synopsis.ts';import { sanitizeSynopsis } from './embedding-context.ts';import { resolveTierDefault } from './model-config.ts';+import { safeSplitIndex } from './text-safe.ts';/*** Default cap on synopsis output tokens. ~200 tokens gives 50-100 token@@ -258,8 +259,9 @@ function buildUserPrompt(// frontmatter, intro paragraphs carry the document-level anchor).let trimmedDoc = documentText;if (documentText.length > SYNOPSIS_DOC_MAX_CHARS) {- trimmedDoc = documentText.slice(0, SYNOPSIS_DOC_MAX_CHARS) +- `\n\n[... ${documentText.length - SYNOPSIS_DOC_MAX_CHARS} chars truncated for synopsis budget ...]`;+ const cut = safeSplitIndex(documentText, SYNOPSIS_DOC_MAX_CHARS);+ trimmedDoc = documentText.slice(0, cut) ++ `\n\n[... ${documentText.length - cut} chars truncated for synopsis budget ...]`;}return [`<page_title>${pageTitle}</page_title>`,diff --git a/test/page-summary-length.serial.test.ts b/test/page-summary-length.serial.test.tsindex c04bcc097a..661c2d1d2a 100644--- a/test/page-summary-length.serial.test.ts+++ b/test/page-summary-length.serial.test.ts@@ -120,3 +120,19 @@ describe('resolveSynopsisMaxTokens config resolver (#3883)', () => {expect(await resolveSynopsisMaxTokens(throwing)).toBeUndefined(); // fail-open});});++describe('surrogate-safe document truncation (#5316)', () => {+ test('an astral char straddling the cap produces no unpaired surrogate in the prompt', async () => {+ const { SYNOPSIS_DOC_MAX_CHARS } = await import('../src/core/page-summary.ts');+ const captured: ChatOpts[] = [];+ stubChat({ stopReason: 'end' }, captured);+ const documentText =+ 'a'.repeat(SYNOPSIS_DOC_MAX_CHARS - 1) + '𝑇' + 'b'.repeat(100);+ const result = await generatePerChunkSynopsis({ ...baseArgs, documentText });+ expect(result.kind).toBe('success');+ const prompt = captured[0].messages[0].content as string;+ expect(prompt.isWellFormed()).toBe(true);+ expect(() => JSON.parse(JSON.stringify(prompt))).not.toThrow();+ expect(prompt).toContain('chars truncated for synopsis budget');+ });+});