Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(large-pages): splitBody checks prefix content in O(1) (byte-identical)
garrytand654c1f8c31d
diff --git a/src/core/markdown.ts b/src/core/markdown.tsindex da15cd3617..6feba2c2ec 100644--- a/src/core/markdown.ts+++ b/src/core/markdown.ts@@ -853,10 +853,24 @@ export function findTimelineSplitIndex(lines: string[]): number {// A sentinel quoted inside a fenced code block (a markdown example) is not// a separator. Mispaired fences (see MarkdownCodeMap) leave every candidate// live, as before.- return findLineOutsideFencedCode(lines, (i) => isTimelineSentinelLine(lines, i));+ const firstContent = firstContentLineIndex(lines);+ return findLineOutsideFencedCode(lines, (i) => isTimelineSentinelLine(lines, i, firstContent));}-function isTimelineSentinelLine(lines: string[], i: number): boolean {+/**+ * Index of the first line holding a non-whitespace character, or+ * `lines.length`. `lines.slice(0, i).join('\n').trim()` is empty exactly when+ * `i <= firstContentLineIndex(lines)`, so the sentinel rules ask "does the+ * prefix above line i have content?" in O(1) instead of re-joining every line+ * above each candidate (O(n^2) on chat exports separated by `---`).+ */+function firstContentLineIndex(lines: readonly string[]): number {+ let i = 0;+ while (i < lines.length && lines[i]!.trim().length === 0) i++;+ return i;+}++function isTimelineSentinelLine(lines: string[], i: number, firstContent: number): boolean {const trimmed = lines[i].trim();if (trimmed === '<!-- timeline -->' || trimmed === '<!--timeline-->') {@@ -868,8 +882,7 @@ function isTimelineSentinelLine(lines: string[], i: number): boolean {}if (trimmed === '---') {- const beforeContent = lines.slice(0, i).join('\n').trim();- if (beforeContent.length === 0) return false;+ if (i <= firstContent) return false;for (let j = i + 1; j < lines.length; j++) {const next = lines[j].trim();@@ -899,6 +912,7 @@ const DATED_BULLET_RE = /^\s*[-*+]\s+\**\d{4}\b/;* included, next H2 excluded) or null.*/function findBareTimelineSection(lines: string[]): { start: number; end: number } | null {+ const firstContent = firstContentLineIndex(lines);let inFence = false;for (let i = 0; i < lines.length; i++) {const fence = /^\s*(`{3,}|~{3,})/.exec(lines[i]);@@ -908,8 +922,7 @@ function findBareTimelineSection(lines: string[]): { start: number; end: number}if (inFence) continue;if (!/^##\s+(timeline|history)\b/i.test(lines[i].trim())) continue;- const beforeContent = lines.slice(0, i).join('\n').trim();- if (beforeContent.length === 0) continue;+ if (i <= firstContent) continue;// Lookahead: section extent + timeline shape. Any non-blank line that is// neither a dated bullet nor a continuation of one (incl. fence openers)diff --git a/test/helpers/seeded-random.ts b/test/helpers/seeded-random.tsnew file mode 100644index 0000000000..7f11a9dbf5--- /dev/null+++ b/test/helpers/seeded-random.ts@@ -0,0 +1,11 @@+/** mulberry32: a small seeded PRNG returning floats in [0, 1), for reproducible fuzz corpora. */+export function mulberry32(seed: number): () => number {+ let a = seed >>> 0;+ return () => {+ a = (a + 0x6d2b79f5) >>> 0;+ let t = a;+ t = Math.imul(t ^ (t >>> 15), t | 1);+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);+ return ((t ^ (t >>> 14)) >>> 0) / 2 ** 32;+ };+}diff --git a/test/large-pages/split-body-linear.test.ts b/test/large-pages/split-body-linear.test.tsnew file mode 100644index 0000000000..c3060ff1a9--- /dev/null+++ b/test/large-pages/split-body-linear.test.ts@@ -0,0 +1,170 @@+/**+ * splitBody must stay byte-identical (content_hash is computed over its+ * output) while no longer re-joining every line above each `---` or+ * `## Timeline` candidate.+ *+ * 1. Protects: the compiled_truth / timeline split of every page, and the+ * linear cost of parsing a chat export separated by `---` lines.+ * 2. Fails when: the "prefix has content" test drifts from the old+ * `lines.slice(0, i).join('\n').trim()` meaning (differential), or the+ * per-candidate prefix join comes back (operation-count probe).+ * 3. Existing coverage: test/markdown.test.ts pins named cases but neither a+ * fuzz corpus against the old implementation nor the cost shape.+ * 4. No production seam: the probe counts Array.prototype.join work.+ */+import { describe, expect, test } from 'bun:test';+import { splitBody } from '../../src/core/markdown.ts';+import { findLineOutsideFencedCode } from '../../src/core/fence-scan.ts';+import { mulberry32 } from '../helpers/seeded-random.ts';++// ---- Oracle: splitBody as it was before the incremental prefix check. ----+function oracleSplitBody(body: string): { compiled_truth: string; timeline: string } {+ const lines = body.split('\n');+ const splitIndex = findLineOutsideFencedCode(lines, (i) => oracleIsSentinel(lines, i));+ if (splitIndex !== -1) {+ return {+ compiled_truth: lines.slice(0, splitIndex).join('\n'),+ timeline: lines.slice(splitIndex + 1).join('\n'),+ };+ }+ const section = oracleBareSection(lines);+ if (section) {+ return {+ compiled_truth: lines.slice(0, section.start).concat(lines.slice(section.end)).join('\n'),+ timeline: lines.slice(section.start, section.end).join('\n'),+ };+ }+ return { compiled_truth: body, timeline: '' };+}++function oracleIsSentinel(lines: string[], i: number): boolean {+ const trimmed = lines[i]!.trim();+ if (trimmed === '<!-- timeline -->' || trimmed === '<!--timeline-->') return true;+ if (trimmed === '--- timeline ---' || /^---\s+timeline\s+---$/i.test(trimmed)) return true;+ if (trimmed === '---') {+ const beforeContent = lines.slice(0, i).join('\n').trim();+ if (beforeContent.length === 0) return false;+ for (let j = i + 1; j < lines.length; j++) {+ const next = lines[j]!.trim();+ if (next.length === 0) continue;+ return /^##\s+(timeline|history)\s*$/i.test(next);+ }+ }+ return false;+}++const ORACLE_DATED_BULLET_RE = /^\s*[-*+]\s+\**\d{4}\b/;++function oracleBareSection(lines: string[]): { start: number; end: number } | null {+ let inFence = false;+ for (let i = 0; i < lines.length; i++) {+ const fence = /^\s*(`{3,}|~{3,})/.exec(lines[i]!);+ if (fence) {+ inFence = !inFence;+ continue;+ }+ if (inFence) continue;+ if (!/^##\s+(timeline|history)\b/i.test(lines[i]!.trim())) continue;+ const beforeContent = lines.slice(0, i).join('\n').trim();+ if (beforeContent.length === 0) continue;+ let end = lines.length;+ let datedBullets = 0;+ let shaped = true;+ for (let j = i + 1; j < lines.length; j++) {+ const trimmed = lines[j]!.trim();+ if (/^##\s+\S/.test(trimmed)) { end = j; break; }+ if (trimmed.length === 0) continue;+ if (ORACLE_DATED_BULLET_RE.test(lines[j]!)) { datedBullets++; continue; }+ if (datedBullets > 0 && /^\s{2,}\S/.test(lines[j]!)) continue;+ shaped = false;+ break;+ }+ if (shaped && datedBullets > 0) return { start: i, end };+ }+ return null;+}++// Line shapes that exercise every sentinel rule, blank and whitespace-only+// prefixes (including BOM and NBSP, which trim() removes), fences and CRLF.+const LINE_POOL = [+ '', '', ' ', '\t', '\r', '\uFEFF', '\u00A0', ' \r',+ '---', ' --- ', '---\r', '--- timeline ---', '--- TIMELINE ---', '<!-- timeline -->', '<!--timeline-->',+ '## Timeline', '## History', '## timeline ', '## Timeline\r', '## History of acme-example', '## Notes',+ '- 2024-05-01: alice-example joined', '* **2020** founded', ' continued bullet text', '- not dated',+ '```', '~~~', '```ts', 'plain prose line', 'alice-example: hi there', '# Title',+];++function randomBody(rand: () => number): string {+ const n = Math.floor(rand() * 40);+ const out: string[] = [];+ for (let i = 0; i < n; i++) out.push(LINE_POOL[Math.floor(rand() * LINE_POOL.length)]!);+ return out.join(rand() < 0.1 ? '\r\n' : '\n');+}++describe('splitBody differential (old vs new)', () => {+ test('identical output over a seeded fuzz corpus', () => {+ const rand = mulberry32(0x5eed_b0d1);+ let splits = 0;+ for (let k = 0; k < 20_000; k++) {+ const body = randomBody(rand);+ const got = splitBody(body);+ expect(got).toEqual(oracleSplitBody(body));+ if (got.timeline !== '') splits++;+ }+ // The corpus must reach the split arms, not only the no-sentinel path.+ expect(splits).toBeGreaterThan(1_000);+ });++ test('identical output on whitespace-only prefixes before each rule', () => {+ for (const prefix of ['', '\n', ' \n\t\n', '\uFEFF\n', '\u00A0\n\n']) {+ for (const tail of ['---\n## Timeline\n- 2024-01-01: x', '## Timeline\n- 2024-01-01: x', '---\n\n## History\n- 2020 y']) {+ for (const lead of ['', 'content\n']) {+ const body = prefix + lead + tail;+ expect(splitBody(body)).toEqual(oracleSplitBody(body));+ }+ }+ }+ });+});++/** Total array elements passed through Array.prototype.join while `fn` runs. */+function countJoinedElements(fn: () => void): number {+ const original = Array.prototype.join;+ let joined = 0;+ Array.prototype.join = function (this: unknown[], sep?: string): string {+ joined += this.length;+ return original.call(this, sep);+ };+ try {+ fn();+ } finally {+ Array.prototype.join = original;+ }+ return joined;+}++describe('splitBody cost probe', () => {+ // n chat messages separated by `---` (the rule-3 candidate on every+ // separator) then n prose `## History` headings (the #2225 candidate). The+ // old code joined every line above each candidate: ~n^2 joined elements.+ // Peak memory stays O(n) either way, so the probe never allocates the+ // quadratic case.+ const N = 1_500;+ const lines: string[] = ['# Conversation with alice-example', ''];+ for (let i = 0; i < N; i++) lines.push(`alice-example: message ${i}`, '---');+ lines.push('end of messages');+ for (let i = 0; i < N; i++) lines.push('## History', 'prose, not a dated bullet');+ const body = lines.join('\n');++ test('joins O(lines) elements, not O(lines^2)', () => {+ const lineCount = body.split('\n').length;+ const joined = countJoinedElements(() => splitBody(body));+ expect(joined).toBeLessThanOrEqual(4 * lineCount);+ });++ test('the oracle shows the quadratic shape the probe catches', () => {+ const lineCount = body.split('\n').length;+ const joined = countJoinedElements(() => oracleSplitBody(body));+ expect(joined).toBeGreaterThan(100 * lineCount);+ });+});