Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test: guard the 24 MiB page limit by linearity; alternate sizes in ratio checks
garrytan61f5bb8fd857
diff --git a/test/chunker-spans-linear.slow.test.ts b/test/chunker-spans-linear.slow.test.tsindex 9064a3fe69..ba69beb41b 100644--- a/test/chunker-spans-linear.slow.test.ts+++ b/test/chunker-spans-linear.slow.test.ts@@ -8,7 +8,10 @@* bound is generous (linear is 2x, quadratic 4x) and each size takes its best* of three runs, so a busy machine does not fail it. Each run is timed in* process CPU time, which measures the work rather than how long the- * scheduler made it wait.+ * scheduler made it wait. The 2 MB and 4 MB runs alternate: CPU time still+ * grows when a co-tenant slows the core (a 2-vCPU CI runner read 3.8x with+ * the three 4 MB runs after the three 2 MB runs), so a slow spell has to cover+ * both sizes to move the ratio.*/import { describe, test, expect } from 'bun:test';import { chunkText } from '../src/core/chunkers/recursive.ts';@@ -38,14 +41,22 @@ function chatLog(chars: number): string {return parts.join('');}-function bestMs(text: string): number {- let best = Infinity;+function runMs(text: string): number {+ let chunks: ReturnType<typeof chunkText> = [];+ const ms = cpuMs(() => { chunks = chunkText(text, { spans: true }); });+ expect(chunks.every(c => c.byte_start !== undefined)).toBe(true);+ return ms;+}++/** Best of three runs per size, the sizes alternating. */+function bestPair(small: string, large: string): [number, number] {+ let s = Infinity;+ let l = Infinity;for (let k = 0; k < 3; k++) {- let chunks: ReturnType<typeof chunkText> = [];- best = Math.min(best, cpuMs(() => { chunks = chunkText(text, { spans: true }); }));- expect(chunks.every(c => c.byte_start !== undefined)).toBe(true);+ s = Math.min(s, runMs(small));+ l = Math.min(l, runMs(large));}- return best;+ return [s, l];}const MB = 1024 * 1024;@@ -56,8 +67,7 @@ describe('chunkText(spans: true) scales linearly', () => {['one-word paragraphs', (chars: number) => 'x\n\n'.repeat(Math.floor(chars / 3))],] as const) {test(`${name}: 4 MB takes under 3x the time of 2 MB`, () => {- const small = bestMs(make(2 * MB));- const large = bestMs(make(4 * MB));+ const [small, large] = bestPair(make(2 * MB), make(4 * MB));expect(large / small).toBeLessThan(3);}, 120_000);}diff --git a/test/write-gate-perf.slow.test.ts b/test/write-gate-perf.slow.test.tsindex 91fe30dc02..6a3cd0736e 100644--- a/test/write-gate-perf.slow.test.ts+++ b/test/write-gate-perf.slow.test.ts@@ -27,7 +27,10 @@* than how long the scheduler made it wait. The 5 MB check keeps its own 5 MB* size: #6733 raised the page import limit to 24 MiB, and sizing this check by* that limit timed ~5x the bytes against bounds calibrated for 5 MB (p95 1585 ms- * against 1500 on a loaded runner).+ * against 1500 on a loaded runner). The page-limit check guards the 24 MiB size+ * with a linearity ratio against 5 MB samples alternated with it in the same+ * process: ~5x the bytes reads ~5x the CPU, and a quadratic pass reads ~25x, so+ * the bound is 15x.*/import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import { readdirSync, readFileSync, statSync } from 'node:fs';@@ -36,6 +39,7 @@ import { createHash } from 'node:crypto';import { PGLiteEngine } from '../src/core/pglite-engine.ts';import { assessPageForGate, DEFAULT_WRITE_GATE_CONFIG } from '../src/core/write-gate.ts';import { recordPageGateReceipt } from '../src/core/write-gate-store.ts';+import { MAX_FILE_SIZE as PAGE_LIMIT_BYTES } from '../src/core/import-screen.ts';import { cpuMsAsync } from './helpers/cpu-time.ts';const ROOT = join(import.meta.dir, '..');@@ -126,7 +130,7 @@ function withRoundSamples(rounds: Round[], check: () => void): void {}}-/** Thread CPU time per sample (PGLite's WASM runs on this thread, and nothing else runs in the process across the await). */+/** Thread CPU time per sample (PGLite's WASM runs on this thread, and nothing else runs in the process across the await) */async function measure(text: string, runs: number, warmup: number): Promise<{ p50: number; p95: number; max: number }> {const times: number[] = [];for (let i = 0; i < warmup + runs; i++) {@@ -195,4 +199,26 @@ describe('write gate p95 (assessment + receipt insert)', () => {expect(adv.p95).toBeLessThan(BUDGET_MS * 300);expect(typical.p95).toBeLessThan(BUDGET_MS * 100);}, 240_000);++ test('page import limit (MAX_FILE_SIZE) scales linearly from 5 MB', async () => {+ const scale = PAGE_LIMIT_BYTES / FIVE_MB;+ const sample = (text: string) => cpuMsAsync(async () => {+ const a = assessPageForGate({ title: 'Perf', compiled_truth: text }, { tier: 'external_untrusted', requestId: 'perf' }, DEFAULT_WRITE_GATE_CONFIG);+ await recordPageGateReceipt(engine, { slug: 'notes/perf', sourceId: 'default', assessment: a, requestId: 'perf' });+ });+ for (const [name, corpus] of [['adversarial', adversarial], ['typical', typicalCorpus]] as const) {+ const smallText = corpus(FIVE_MB);+ const largeText = corpus(PAGE_LIMIT_BYTES);+ const small: number[] = [];+ const large: number[] = [];+ for (let i = 0; i < 6; i++) {+ const s = await sample(smallText);+ const l = await sample(largeText);+ if (i > 0) { small.push(s); large.push(l); }+ }+ const ratio = stats(large).p50 / stats(small).p50;+ console.log(`[write-gate perf] ${name} ${(PAGE_LIMIT_BYTES / 2 ** 20).toFixed(0)} MiB vs 5 MB: p50 ${stats(large).p50.toFixed(0)} / ${stats(small).p50.toFixed(0)} ms = ${ratio.toFixed(1)}x for ${scale.toFixed(1)}x the bytes`);+ expect(ratio).toBeLessThan(15);+ }+ }, 240_000);});