Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Wave B 1b-i: body-scan linear CPU-ratio test with forced quadratic probe
garrytand10b0388f07b
diff --git a/test/large-pages/body-scan-linear.test.ts b/test/large-pages/body-scan-linear.test.tsnew file mode 100644index 0000000000..986ffe6060--- /dev/null+++ b/test/large-pages/body-scan-linear.test.ts@@ -0,0 +1,64 @@+/**+ * Wave B (spec 3.1): pass 1 stays linear in the file size.+ *+ * Protects: the streamed body scan does O(bytes) work: CPU time at 2n bytes is+ * under 2.5x the time at n bytes on a page dense with every state the scan+ * carries (quoted sentinels inside fences, `---` lines awaiting a Timeline+ * H2, bare-section headings that fail their lookahead, CRLF, astral text).+ * Fails when: a step re-reads buffered input per line or per chunk (a+ * re-decoded prefix, a re-split carry, a per-candidate rescan); the forced+ * probe below runs exactly such a variant and asserts the ratio catches it.+ * Why existing coverage misses it: split-body-linear covers the whole-string+ * splitBody, not the streaming scanner.+ * Seams: none; the quadratic variant wraps the public push API.+ */+import { describe, expect, test } from 'bun:test';+import { BodyScanner } from '../../src/core/page-body/body-scan.ts';+import { cpuMs } from '../helpers/cpu-time.ts';++const N = 1024 * 1024;+const RATIO_LIMIT = 2.5;++function page(bytes: number): Uint8Array {+ const unit = [+ '## Message from alice-example', 'Some prose with **bold** and π π', '', '---', '```ts', 'const x = 1; <!-- timeline -->', '```',+ '- 2024-05-01: dated entry', '## History', 'prose disqualifies the bare section', ' indented continuation',+ ].join('\r\n') + '\r\n';+ const head = '---\ntitle: Linear Example\ntags: [a, b]\n---\n';+ return Buffer.from(head + unit.repeat(Math.ceil(bytes / unit.length)) + '<!-- timeline -->\n- 2024: end\n');+}++function scan(bytes: Uint8Array): void {+ const scanner = new BodyScanner('notes/linear-example.md', { validate: true });+ for (let at = 0; at < bytes.length; at += 64 * 1024) scanner.push(bytes.subarray(at, at + 64 * 1024));+ expect(scanner.finish().kind).toBe('ok');+}++/** The deliberately quadratic variant: re-decodes everything buffered so far before each 1 KiB push. */+function quadraticScan(bytes: Uint8Array): void {+ const scanner = new BodyScanner('notes/linear-example.md', { validate: true });+ const decoder = new TextDecoder();+ for (let at = 0; at < bytes.length; at += 1024) {+ decoder.decode(bytes.subarray(0, at));+ scanner.push(bytes.subarray(at, at + 1024));+ }+ expect(scanner.finish().kind).toBe('ok');+}++function ratio(run: (bytes: Uint8Array) => void): number {+ const small = page(N);+ const large = page(2 * N);+ const best = (bytes: Uint8Array) => Math.min(...[0, 1, 2].map(() => cpuMs(() => run(bytes))));+ run(small);+ return best(large) / best(small);+}++describe('body scan cost', () => {+ test(`CPU time at 2n stays under ${RATIO_LIMIT}x the time at n`, () => {+ expect(ratio(scan)).toBeLessThan(RATIO_LIMIT);+ }, 60_000);++ test('forced probe: the same ratio fails a scan that re-reads its buffered prefix', () => {+ expect(ratio(quadraticScan)).toBeGreaterThan(RATIO_LIMIT);+ }, 120_000);+});