Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(dream): quote repairs end on a whole code point, never mid-surrogate (#6860)

time-attackfcb13f8341d8
diff --git a/src/core/cycle/synthesize-verify.ts b/src/core/cycle/synthesize-verify.ts
index a317627770..94d6b23376 100644
--- a/src/core/cycle/synthesize-verify.ts
+++ b/src/core/cycle/synthesize-verify.ts
@@ -715,6 +715,11 @@ function groundStrictQuote(inner: string, t: GroundedTranscript): GroundResult {
return r.status === 'none' ? full : r;
}
+/** One past the source code point that starts at `lastStart`: a slice end never splits a surrogate pair. */
+function sourceCodePointEnd(content: string, lastStart: number): number {
+ return lastStart + (content.codePointAt(lastStart)! > 0xffff ? 2 : 1);
+}
+
function groundQuoteSpan(inner: string, t: GroundedTranscript, near: boolean): GroundResult {
let crossed = false;
@@ -737,8 +742,9 @@ function groundQuoteSpan(inner: string, t: GroundedTranscript, near: boolean): G
const endIdx = t.map[pos + q.norm.length - 1];
// Defensive: a map hole must never become a "verbatim" repair.
if (start === undefined || endIdx === undefined) continue;
- if (crossesTurn(t.turns, start, endIdx + 1)) crossed = true;
- else normalized.push([start, endIdx + 1]);
+ const end = sourceCodePointEnd(t.content, endIdx);
+ if (crossesTurn(t.turns, start, end)) crossed = true;
+ else normalized.push([start, end]);
}
if (normalized.length) {
const [start, end] = normalized[0];
@@ -813,7 +819,7 @@ function groundQuoteSpan(inner: string, t: GroundedTranscript, near: boolean): G
const oFirst = t.map[tokens[0].start];
const oLast = t.map[tokens[tokens.length - 1].end - 1];
if (oFirst === undefined || oLast === undefined) return none;
- let a = oFirst, b = oLast + 1;
+ let a = oFirst, b = sourceCodePointEnd(t.content, oLast);
const innerTrim = inner.trim();
if (!PUNCT_EDGE.test(innerTrim[0] ?? '')) while (a < b && PUNCT_EDGE.test(t.content[a])) a++;
if (!PUNCT_EDGE.test(innerTrim[innerTrim.length - 1] ?? '')) while (b > a && PUNCT_EDGE.test(t.content[b - 1])) b--;
diff --git a/test/cycle-synthesize-verify.test.ts b/test/cycle-synthesize-verify.test.ts
index 47e9b83388..80133c9b64 100644
--- a/test/cycle-synthesize-verify.test.ts
+++ b/test/cycle-synthesize-verify.test.ts
@@ -114,6 +114,19 @@ describe('groundQuote β€” the repair ladder', () => {
}
});
+ test.each([
+ { name: 'normalized terminal emoji', content: 'hello world πŸ˜‚', quote: 'hello world πŸ˜‚', replacement: 'hello world πŸ˜‚', status: 'normalized' },
+ { name: 'normalized emoji before following text', content: 'prefix hello world πŸ˜‚ following text', quote: 'hello world πŸ˜‚', replacement: 'hello world πŸ˜‚', status: 'normalized' },
+ { name: 'non-BMP lowercase at both endpoints', content: 'prefix 𐐀 HELLO WORLD 𐐁 suffix', quote: '𐐨 hello world 𐐩', replacement: '𐐀 HELLO WORLD 𐐁', status: 'normalized' },
+ { name: 'near last shared token ends in emoji', content: 'alpha beta gamma deltaπŸ˜‚', quote: 'alpha beta gamma deltaπŸ˜‚ extra', replacement: 'alpha beta gamma deltaπŸ˜‚', status: 'near' },
+ { name: 'lowercase expansion at both endpoints', content: 'prefix İ HELLO WORLD İ suffix', quote: 'i̇ hello world i̇', replacement: 'İ HELLO WORLD İ', status: 'normalized' },
+ { name: 'normalized ASCII control', content: 'prefix HELLO WORLD suffix', quote: 'hello world', replacement: 'HELLO WORLD', status: 'normalized' },
+ ])('$name returns the complete verbatim slice and span (#6860)', ({ content, quote, replacement, status }) => {
+ const result = groundQuote(quote, groundSource('/t/unicode.txt', content));
+ const start = content.indexOf(replacement);
+ expect(result).toEqual({ status, replacement, spans: [[start, start + replacement.length]] });
+ });
+
test('heavy paraphrase (low overlap) β†’ none (caller quarantines)', () => {
const g = groundQuote('our fundamental product philosophy centers customer delight above metrics', t);
expect(g.status).toBe('none');
@@ -574,6 +587,33 @@ describe('verifyAndRepairDreamPages β€” PGLite write-back integration', () => {
expect(hits.filter(h => h.slug === slug)).toHaveLength(0);
}, 60_000);
+ test('repaired emoji quotes persist with complete spans and survive a JSONB page payload roundtrip (#6860)', async () => {
+ const transcript = 'user: hello brave world πŸ˜‚';
+ const since = await readVerifyEpoch(engine);
+ const slug = 'wiki/personal/reflections/2026-08-31-emoji-abc456';
+ await importFromContent(engine, slug, [
+ '---', 'type: note', '---', 'The user said "hello brave world πŸ˜‚".',
+ '', '## Timeline', '', '- **2026-08-31** | session β€” Said "hello brave world πŸ˜‚"',
+ ].join('\n'), { noEmbed: true, remote: false, sourceId: 'default' });
+
+ const stats = await verifyAndRepairDreamPages(engine, [{ slug, source_id: 'default', raw_source: '/t/emoji.md' }],
+ new Map([['/t/emoji.md', { content: transcript }]]), { since, checkedAt: '2026-08-31' });
+ expect(stats.errors).toBe(0);
+ expect(stats).toMatchObject({ pages_checked: 1, pages_repaired: 1, normalized_fixed: 2 });
+
+ const page = await engine.getPage(slug, { sourceId: 'default' });
+ expect(page).toBeDefined();
+ const serialized = JSON.stringify(page);
+ const [row] = await engine.executeRaw<{ payload: typeof page }>('SELECT $1::text::jsonb AS payload', [serialized]);
+ expect(row.payload).toEqual(JSON.parse(serialized));
+ expect(row.payload!.compiled_truth).toBe('The user said "hello brave world πŸ˜‚".');
+ expect(row.payload!.timeline).toContain('Said "hello brave world πŸ˜‚"');
+ expect(row.payload!.frontmatter.grounding).toMatchObject({
+ sources: ['/t/emoji.md'],
+ quotes: [{ text: 'hello brave world πŸ˜‚', source: '/t/emoji.md', speaker: 'user', span: [6, transcript.length] }],
+ });
+ }, 60_000);
+
test('re-projection deletes the rows of quarantined bullets and keeps database-only timeline history (#5567)', async () => {
const transcript = 'user: We shipped the repair pass on Monday.';
const since = await readVerifyEpoch(engine);