Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(fence-repair): keep the quoted-marker screen linear in the page (#8059)
garrytan7aade6706cfe
diff --git a/src/core/fence-repair/quote-markers.ts b/src/core/fence-repair/quote-markers.tsindex ae334ccad1..9ca3d528fc 100644--- a/src/core/fence-repair/quote-markers.ts+++ b/src/core/fence-repair/quote-markers.ts@@ -68,21 +68,43 @@ function nextLine(text: string, lineEnd: number): string {return '';}-/** The prose-quoted marker mentions of one section, in text order. */+/** How many of the ascending `sorted` values are below `value`. */+function countBelow(sorted: ReadonlyArray<number>, value: number): number {+ let lo = 0, hi = sorted.length;+ while (lo < hi) {+ const mid = (lo + hi) >> 1;+ if (sorted[mid]! < value) lo = mid + 1; else hi = mid;+ }+ return lo;+}++/** The prose-quoted marker mentions of one section, in text order. Linear in the section: lines come from one newline index. */function mentions(text: string, section: FenceSection): Mention[] {if (!hasFenceText(text)) return [];const code = scanMarkdownCode(text);const closesRows = new Set(extractRawRows(text, section).fences.filter(f => f.rows.length && f.end).map(f => f.end!.start));+ const newlines: number[] = [];+ for (let at = text.indexOf('\n'); at !== -1; at = text.indexOf('\n', at + 1)) newlines.push(at);+ const nextLines = new Map<number, string>();const found: Mention[] = [];for (const { token, fence } of MARKERS) {for (let at = text.indexOf(token); at !== -1; at = text.indexOf(token, at + token.length)) {if (closesRows.has(at) || codeEndAt(code, at) !== -1 || text[at - 1] === '`' || text[at + token.length] === '`') continue;- const [lineStart, lineEnd] = lineBounds(text, at);- if (token.endsWith('begin -->') && /^\s*\|/.test(nextLine(text, lineEnd))) continue;+ const index = countBelow(newlines, at);+ const lineStart = index ? newlines[index - 1]! + 1 : 0;+ let lineEnd = index < newlines.length ? newlines[index]! : text.length;+ if (text[lineEnd - 1] === '\r') lineEnd--;+ if (token.endsWith('begin -->')) {+ if (!nextLines.has(lineEnd)) nextLines.set(lineEnd, nextLine(text, lineEnd));+ if (/^\s*\|/.test(nextLines.get(lineEnd)!)) continue;+ }const line = text.slice(lineStart, lineEnd);if (/^\s*\|/.test(line)) continue;- if (!text.slice(at + token.length, lineEnd).trim() && !/^ {0,3}>/.test(line)) continue;- found.push({ fence, at, length: token.length, lineStart, lineEnd, line: text.slice(0, at).split('\n').length });+ const rest = /\S/g;+ rest.lastIndex = at + token.length;+ const after = rest.exec(text);+ if ((!after || after.index >= lineEnd) && !/^ {0,3}>/.test(line)) continue;+ found.push({ fence, at, length: token.length, lineStart, lineEnd, line: index + 1 });}}return found.sort((a, b) => a.at - b.at);@@ -97,7 +119,8 @@ function wrapLine(line: string, offsets: ReadonlyArray<{ at: number; length: numfrom = at + length;}out += line.slice(from);- const shift = (pos: number) => pos + 2 * offsets.filter(o => o.at < pos).length;+ const starts = offsets.map(o => o.at);+ const shift = (pos: number) => pos + 2 * countBelow(starts, pos);const expected = [...inlineCodeSpans(line).map(([s, e]) => [shift(s), shift(e)] as [number, number]),...offsets.map(o => [shift(o.at), shift(o.at) + o.length + 2] as [number, number]),@@ -111,7 +134,10 @@ type Inserts = number[];function quoteSection(text: string, section: FenceSection): { text: string; fixes: FenceFix[]; inserts: Inserts } {const byLine = new Map<number, Mention[]>();- for (const m of mentions(text, section)) byLine.set(m.lineStart, [...(byLine.get(m.lineStart) ?? []), m]);+ for (const m of mentions(text, section)) {+ const group = byLine.get(m.lineStart);+ if (group) group.push(m); else byLine.set(m.lineStart, [m]);+ }const inserts: Inserts = [];if (!byLine.size) return { text, fixes: [], inserts };let out = '';diff --git a/test/fence-repair-quote-markers.test.ts b/test/fence-repair-quote-markers.test.tsindex ac34f7d2c0..b89a4eb4fa 100644--- a/test/fence-repair-quote-markers.test.ts+++ b/test/fence-repair-quote-markers.test.ts@@ -7,6 +7,8 @@* `quoted_marker` only when the wrap alone would clear the page, and never edits it.* 2. Fails when: a real marker is wrapped (rows lost), an existing code span re-pairs, a wrap would show text the* boundary hid, or the screen keeps `repeated_marker` for a page the repair clears.+ * The screen runs on the write path, so it stays linear: a page of 40,000 mentions (on separate lines or one line)+ * is screened in under two seconds; the per-mention prefix split and array copies made it quadratic (minutes at 24 MiB).* 3. Pure; no engine.*/import { describe, expect, test } from 'bun:test';@@ -109,3 +111,21 @@ describe('the screen reason', () => {if (step.status === 'residual') expect(step.issues.map(i => i.reason)).not.toContain('quoted_marker');});});++describe('the screen stays linear in the page', () => {+ const timed = (body: string) => {+ const started = performance.now();+ const quoted = quoteMarkerStep(page(body));+ return { ms: performance.now() - started, fixes: quoted.fixes.length };+ };+ test('40,000 quoted markers on their own lines are screened in under two seconds', () => {+ const run = timed(Array.from({ length: 40_000 }, () => `See the ${E} marker in prose here.`).join('\n') + '\n');+ expect(run.fixes).toBe(40_000);+ expect(run.ms).toBeLessThan(2_000);+ });+ test('40,000 quoted markers on one line are screened as fast', () => {+ const run = timed(Array.from({ length: 40_000 }, () => `a ${E} b`).join(' ') + '\n');+ expect(run.fixes).toBe(40_000);+ expect(run.ms).toBeLessThan(2_000);+ });+});