Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
P8 quote grounding: punctuation and elision at a quote's edges and markdown link syntax no longer flag supported quotes
garrytan98cae3c98e28
diff --git a/src/core/cycle/synthesize-verify.ts b/src/core/cycle/synthesize-verify.tsindex d9201dee42..d1686c5dbd 100644--- a/src/core/cycle/synthesize-verify.ts+++ b/src/core/cycle/synthesize-verify.ts@@ -210,6 +210,7 @@ export function normalizeForGrounding(s: string): { norm: string; map: number[]function foldForGrounding(s: string, withMap: boolean): { norm: string; map: number[] } | string {const out: string[] = [];const map: number[] = [];+ const skip = linkSyntaxMask(s);let pendingSpace = false;// Iterate by CODE POINT (for..of), not code unit: a surrogate pair// lowercases as a pair (Deseret 𐐀 → 𐐨) but never half by half, so a@@ -219,6 +220,7 @@ function foldForGrounding(s: string, withMap: boolean): { norm: string; map: numfor (const cp of s) {const i = idx;idx += cp.length;+ if (skip?.has(i)) continue;let ch = cp;if (/\s/.test(ch)) {pendingSpace = out.length > 0;@@ -246,6 +248,30 @@ function foldForGrounding(s: string, withMap: boolean): { norm: string; map: numreturn withMap ? { norm, map } : norm;}+const MD_LINK = /\[([^\[\]\n]{1,300})\]\([^()\s]{1,500}\)/g;++/** Code-unit offsets of markdown link syntax (`[` and `](target)`): a quote never carries a link target, so the fold reads `[Ana](people/ana)` as `Ana`. */+function linkSyntaxMask(s: string): Set<number> | null {+ if (!s.includes('](')) return null;+ const skip = new Set<number>();+ for (const m of s.matchAll(MD_LINK)) {+ const at = m.index!;+ skip.add(at);+ for (let k = at + 1 + m[1]!.length; k < at + m[0].length; k++) skip.add(k);+ }+ return skip;+}++/** A source slice as a reader sees it: link syntax reduced to the link text. */+function displayText(slice: string): string {+ return slice.replace(MD_LINK, '$1').replace(/\]\([^()\s]*\)/g, '');+}++/** A quote's core: the elision marks and closing punctuation writers put at a quotation's edges ("the deal," / "…edge cases o…"). */+function quoteCore(inner: string): string {+ return inner.replace(/^(?:\s|\.\.\.|…)+/u, '').replace(/(?:\s|[.,;:!?]|…)+$/u, '');+}+/*** Plain normalized form (no offset map) — for presence checks (the triage* rescue's segment verification, buildTriageMapBlock's quote filter, the@@ -567,6 +593,19 @@ const PUNCT_EDGE = /[.,;:!?]/;* in another's mouth.*/export function groundQuote(inner: string, t: GroundedTranscript): GroundResult {+ const whole = groundQuoteSpan(inner, t, false);+ if (whole.status !== 'none') return whole;+ const core = quoteCore(inner);+ if (core !== inner && core.split(/\s+/).filter(Boolean).length >= 2) {+ // Punctuation or elision marks at the edges are the writer's, not the source's: the words themselves are grounded.+ const r = groundQuoteSpan(core, t, false);+ if (r.status === 'exact' || (r.status === 'normalized' && r.replacement === core)) return { status: 'exact', spans: r.spans };+ if (r.status !== 'none') return r;+ }+ return groundQuoteSpan(inner, t, true);+}++function groundQuoteSpan(inner: string, t: GroundedTranscript, near: boolean): GroundResult {let crossed = false;// Rung 1: exact substring.@@ -592,7 +631,7 @@ export function groundQuote(inner: string, t: GroundedTranscript): GroundResult}if (normalized.length) {const [start, end] = normalized[0];- const replacement = t.content.slice(start, end);+ const replacement = displayText(t.content.slice(start, end));if (replacement.length === 0) return { status: 'none', reason: 'not_found' };return replacement === inner ? { status: 'exact', spans: normalized } : { status: 'normalized', replacement, spans: normalized };}@@ -602,7 +641,7 @@ export function groundQuote(inner: string, t: GroundedTranscript): GroundResult// overlap; accept a single clear winner ≥ floor, trimmed to the matched// tokens. Hard-bounded: total probes, trigrams (stride-sampled), quote size.const none: GroundResult = { status: 'none', reason: crossed ? 'crosses_speakers' : 'not_found' };- if (q.norm.length > MAX_NEAR_QUOTE_NORM_CHARS) return none;+ if (!near || q.norm.length > MAX_NEAR_QUOTE_NORM_CHARS) return none;const qTokens = q.norm.split(' ').filter(w => w.length > 0);if (qTokens.length < 4) return none;const qBare = new Set(qTokens.map(w => w.replace(/[^\p{L}\p{N}]/gu, '')).filter(w => w.length > 0));@@ -666,7 +705,7 @@ export function groundQuote(inner: string, t: GroundedTranscript): GroundResultconst 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--;- const replacement = t.content.slice(a, b).trim();+ const replacement = displayText(t.content.slice(a, b)).trim();if (replacement.length === 0) return none;if (normForGrounding(replacement).length > Math.ceil(q.norm.length * NEAR_MATCH_MAX_GROWTH)) return none;if (crossesTurn(t.turns, a, b)) return { status: 'none', reason: 'crosses_speakers' };diff --git a/test/think-quote-verify.test.ts b/test/think-quote-verify.test.tsindex 7d8a99a921..0e54f51954 100644--- a/test/think-quote-verify.test.ts+++ b/test/think-quote-verify.test.ts@@ -53,6 +53,21 @@ describe('groundAnswerQuotes (pure)', () => {expect(r.quote_check.grounded + r.quote_check.repaired).toBe(1);});+ test('punctuation and elision marks at a quote\'s edges are the writer\'s: the words still ground', () => {+ const r = groundAnswerQuotes('She called it "move to annual billing," then "…Bob Example asked about seats." and "annual billing in M…".', sources);+ expect(r.quote_check).toEqual({ grounded: 3, repaired: 0, unverified: 0 });+ expect(r.answer).toContain('"move to annual billing,"');+ const fabricated = groundAnswerQuotes('She said "we will move to quarterly billing."', sources);+ expect(fabricated.quote_check.unverified).toBe(1);+ });++ test('markdown link syntax in the evidence does not hide the quoted words; repairs carry no link targets', () => {+ const linked = [groundSource('evidence', '[Elena](people/elena-example) said the [Meridian](companies/meridian-example) deal is moving faster than expected.')];+ const r = groundAnswerQuotes('Elena: "the Meridian deal is moving faster than expected" and "Elena said the meridian deal".', linked);+ expect(r.quote_check.unverified).toBe(0);+ expect(r.answer).not.toContain('](');+ });+test('quotes-only mode never flags computed numbers; full mode does', () => {const body = 'Revenue grew 75 percent across 3 quarters.';expect(verifyBody(body, sources, { checks: 'quotes' }).quarantined).toEqual([]);