Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(pattern-detector): measure N6 retention in a child process so its forced GCs never scan the heap earlier files left
garrytan58c4bb4832da
diff --git a/test/conversation-parser/pattern-detector.test.ts b/test/conversation-parser/pattern-detector.test.tsindex a869a5b72f..8fec61f8c6 100644--- a/test/conversation-parser/pattern-detector.test.ts+++ b/test/conversation-parser/pattern-detector.test.ts@@ -11,7 +11,7 @@* message body, fed in 1 MiB pieces (N6, round 6); a message line over the cap is still scored by its head.*/import { describe, expect, test } from 'bun:test';-import { heapSize } from 'bun:jsc';+import { join } from 'node:path';import { BUILTIN_PATTERNS } from '../../src/core/conversation-parser/builtins.ts';import { createPatternDetector, parseConversation } from '../../src/core/conversation-parser/parse.ts';@@ -84,32 +84,25 @@ describe('createPatternDetector', () => {expect({ parsed: parseConversation(text).matched_pattern_id, detected: detect(text, 1) }).toEqual({ parsed: 'bold-paren-time-12h', detected: 'bold-paren-time-12h' });});- /** Heap growth (bytes) over a baseline after each of 40 fresh 1 MiB pieces `piece(i)` the detector is fed: the most it retained. */- const peakRetained = (head: string, piece: (i: number) => string, tail: string) => {- const detector = createPatternDetector();- detector.push(head);- Bun.gc(true);- const base = heapSize();- let peak = 0;- for (let i = 0; i < 40; i++) {- detector.push(piece(i));- Bun.gc(true);- peak = Math.max(peak, heapSize() - base);- }- detector.push(tail);- return { peak, pattern: detector.finish() };+ /**+ * The most heap (bytes) a detector retained after any of 40 fresh 1 MiB pieces, and the pattern it detected, measured in+ * a child process (test/fixtures/detector-retention.ts): its forced GCs scan only its own heap, never what earlier test+ * files in this process left behind.+ */+ const peakRetained = (kind: 'line' | 'block') => {+ const child = Bun.spawnSync([process.execPath, '--no-env-file', join(import.meta.dir, '..', 'fixtures', 'detector-retention.ts'), kind], { stdout: 'pipe', stderr: 'pipe' });+ expect({ exit: child.exitCode, stderr: child.stderr.toString() }).toEqual({ exit: 0, stderr: '' });+ return JSON.parse(child.stdout.toString()) as { peak: number; pattern: string | null };};const MIB = 1024 ** 2;- const transcript = Array.from({ length: 60 }, (_, t) => `**${t % 2 ? 'Alice' : 'Bob'}** (${1 + (t % 11)}:0${t % 10} PM): turn ${t} about the roadmap review.`).join('\n');test('N6: a 40 MiB line with no newline, fed in 1 MiB pieces, is never held whole; only its head is scored', () => {- const { peak, pattern } = peakRetained(`${transcript}\n`, i => String.fromCharCode(97 + (i % 26)).repeat(MIB - 8) + String(i).padStart(8, '0'), `\n${transcript}\n`);+ const { peak, pattern } = peakRetained('line');expect({ held: peak < 4 * MIB, pattern }).toEqual({ held: true, pattern: 'bold-paren-time-12h' });});test('N6: a block message whose 40 MiB body arrives in 1 MiB pieces of short lines holds a bounded body', () => {- const line = (i: number, j: number) => ` body line ${j} of piece ${i} `.padEnd(1023, 'x');- const { peak, pattern } = peakRetained('- **Theo** (Mon 11:18)\n', i => Array.from({ length: 1024 }, (_, j) => `${line(i, j)}\n`).join(''), '- **Juan** (11:20 AM)\n done\n');+ const { peak, pattern } = peakRetained('block');expect({ held: peak < 4 * MIB, pattern }).toEqual({ held: true, pattern: 'bold-paren-time' });});});diff --git a/test/fixtures/detector-retention.ts b/test/fixtures/detector-retention.tsnew file mode 100644index 0000000000..9956b6ec98--- /dev/null+++ b/test/fixtures/detector-retention.ts@@ -0,0 +1,30 @@+/**+ * A child process for test/conversation-parser/pattern-detector.test.ts (N6): feeds createPatternDetector a head, 40+ * fresh 1 MiB pieces and a tail, and prints the most heap it retained after any piece (a full GC before each reading)+ * and the pattern it detected. Run in its own process so each forced GC scans only this heap, never what other test+ * files in the same bun process left behind. Argument: 'line' (a 40 MiB line with no newline) or 'block' (a block+ * message whose 40 MiB body is 1 KiB lines).+ */+import { heapSize } from 'bun:jsc';+import { createPatternDetector } from '../../src/core/conversation-parser/parse.ts';++const MIB = 1024 ** 2;+const transcript = Array.from({ length: 60 }, (_, t) => `**${t % 2 ? 'Alice' : 'Bob'}** (${1 + (t % 11)}:0${t % 10} PM): turn ${t} about the roadmap review.`).join('\n');+const line = (i: number, j: number) => ` body line ${j} of piece ${i} `.padEnd(1023, 'x');+const cases = {+ line: { head: `${transcript}\n`, piece: (i: number) => String.fromCharCode(97 + (i % 26)).repeat(MIB - 8) + String(i).padStart(8, '0'), tail: `\n${transcript}\n` },+ block: { head: '- **Theo** (Mon 11:18)\n', piece: (i: number) => Array.from({ length: 1024 }, (_, j) => `${line(i, j)}\n`).join(''), tail: '- **Juan** (11:20 AM)\n done\n' },+};+const { head, piece, tail } = cases[process.argv[2] as keyof typeof cases];+const detector = createPatternDetector();+detector.push(head);+Bun.gc(true);+const base = heapSize();+let peak = 0;+for (let i = 0; i < 40; i++) {+ detector.push(piece(i));+ Bun.gc(true);+ peak = Math.max(peak, heapSize() - base);+}+detector.push(tail);+process.stdout.write(JSON.stringify({ peak, pattern: detector.finish() }));