Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Wave B PR 1: receipt coverage for segmentWriteScope; linearity ratios on a collected heap

garrytanb86286e3f217
diff --git a/test/helpers/cpu-time.ts b/test/helpers/cpu-time.ts
index e459595c4f..4cbda061a7 100644
--- a/test/helpers/cpu-time.ts
+++ b/test/helpers/cpu-time.ts
@@ -11,3 +11,23 @@ export function cpuMs(fn: () => unknown): number {
const used = process.threadCpuUsage(start);
return (used.user + used.system) / 1000;
}
+
+/**
+ * CPU-time scaling ratio `large / small` for a linearity bound: after one warm-up run of each, `runs` interleaved pairs,
+ * each call on a freshly collected heap, and the best of each side. Collecting first keeps the previous call's garbage
+ * out of the measured call (thread CPU time counts collection work done on the calling thread), and interleaving keeps
+ * a slow stretch of a loaded VM from landing on one side only.
+ */
+export function cpuScalingRatio(small: () => unknown, large: () => unknown, runs = 5): number {
+ small();
+ large();
+ let bestSmall = Infinity;
+ let bestLarge = Infinity;
+ for (let k = 0; k < runs; k++) {
+ Bun.gc(true);
+ bestSmall = Math.min(bestSmall, cpuMs(small));
+ Bun.gc(true);
+ bestLarge = Math.min(bestLarge, cpuMs(large));
+ }
+ return bestLarge / bestSmall;
+}
diff --git a/test/large-pages/body-scan-linear.test.ts b/test/large-pages/body-scan-linear.test.ts
index 986ffe6060..48d0c57c3a 100644
--- a/test/large-pages/body-scan-linear.test.ts
+++ b/test/large-pages/body-scan-linear.test.ts
@@ -2,7 +2,7 @@
* 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
+ * under 2.5x the time at n bytes (n = 4 MiB, best of five interleaved runs) 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
@@ -14,9 +14,9 @@
*/
import { describe, expect, test } from 'bun:test';
import { BodyScanner } from '../../src/core/page-body/body-scan.ts';
-import { cpuMs } from '../helpers/cpu-time.ts';
+import { cpuScalingRatio } from '../helpers/cpu-time.ts';
-const N = 1024 * 1024;
+const MB = 1024 * 1024;
const RATIO_LIMIT = 2.5;
function page(bytes: number): Uint8Array {
@@ -45,20 +45,18 @@ function quadraticScan(bytes: Uint8Array): void {
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);
+function ratio(run: (bytes: Uint8Array) => void, n: number, runs: number): number {
+ const small = page(n);
+ const large = page(2 * n);
+ return cpuScalingRatio(() => run(small), () => run(large), runs);
}
describe('body scan cost', () => {
test(`CPU time at 2n stays under ${RATIO_LIMIT}x the time at n`, () => {
- expect(ratio(scan)).toBeLessThan(RATIO_LIMIT);
+ expect(ratio(scan, 4 * MB, 5)).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);
+ expect(ratio(quadraticScan, MB, 3)).toBeGreaterThan(RATIO_LIMIT);
}, 120_000);
});
diff --git a/test/large-pages/segmenter-linear.slow.test.ts b/test/large-pages/segmenter-linear.slow.test.ts
index e429be834c..f3dfdf709c 100644
--- a/test/large-pages/segmenter-linear.slow.test.ts
+++ b/test/large-pages/segmenter-linear.slow.test.ts
@@ -11,13 +11,13 @@
* 3. Forced probe: the same ratio check run on an append path that
* resegments the whole prefix on each append (the quadratic variant)
* exceeds the bound, so the check can tell the two apart.
- * 4. After one warm-up run, each size takes its best of five runs in thread
- * CPU time (cpuMs).
+ * 4. After one warm-up run, each size takes its best of five interleaved runs
+ * in thread CPU time, each on a freshly collected heap (cpuScalingRatio).
*/
import { describe, expect, test } from 'bun:test';
import { initialEntryState } from '../../src/core/page-body/entry-state.ts';
import { Segmenter, segmentString, type SegmentOut } from '../../src/core/page-body/segmenter.ts';
-import { cpuMs } from '../helpers/cpu-time.ts';
+import { cpuScalingRatio } from '../helpers/cpu-time.ts';
import { mulberry32 } from '../helpers/seeded-random.ts';
import { bigPage, PEM_BLOCK, segmentOptions } from '../helpers/segment-bodies.ts';
@@ -25,20 +25,10 @@ const MB = 1024 * 1024;
const BOUND = 2.5;
const entry = () => initialEntryState({ region: 'truth' });
-function bestMs(fn: () => unknown): number {
- let best = Infinity;
- for (let k = 0; k < 5; k++) {
- best = Math.min(best, cpuMs(fn));
- }
- return best;
-}
-
function ratio(make: (bytes: number) => string, small: number, run: (body: string) => unknown): number {
const a = make(small);
const b = make(2 * small);
- run(a);
- run(b);
- return bestMs(() => run(b)) / bestMs(() => run(a));
+ return cpuScalingRatio(() => run(a), () => run(b));
}
const page = bigPage(mulberry32(7), 4.5 * MB);
diff --git a/test/receipt-coverage.test.ts b/test/receipt-coverage.test.ts
index 221c1764c2..dca87a1839 100644
--- a/test/receipt-coverage.test.ts
+++ b/test/receipt-coverage.test.ts
@@ -52,6 +52,7 @@ const RECEIPT_COVERAGE: Record<string, Coverage> = {
'src/core/persistence/connector-path.ts': { sites: 1, tables: ['sources'], other: 'clears an unbound connector source local_path (source registration, not knowledge)' },
'src/core/persistence/connector-sync.ts': { sites: 1, tables: ['sources'], other: 'the connector lease freshness stamp on its own source row' },
'src/core/persistence/sync-run.ts': { sites: 1, tables: ['sources'], other: 'the managed sync freshness heartbeat on its own source row' },
+ 'src/core/page-body/ingest.ts': { sites: 1, tables: [], other: 'segmentWriteScope: staged segment texts and manifest rows of an unpublished generation, and segment GC sweeps (page_segment_texts and page_body_manifest are guarded but hold no live body until the publication flips the generation)' },
};
/** Facts/takes writers allowed without a receipt this wave, each with its TODO above. */