Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(chunker): bound span work by deterministic counts, not CPU time
garrytan81e93ddbf144
diff --git a/src/core/chunkers/recursive.ts b/src/core/chunkers/recursive.tsindex e46893a947..f32d0f9bc7 100644--- a/src/core/chunkers/recursive.ts+++ b/src/core/chunkers/recursive.ts@@ -156,8 +156,18 @@ interface Run { text: string; segs: Seg[] }interface Slice { text: string; at: number }interface Placed { text: string; start: number | null; end: number | null }+/**+ * @internal Work the span bookkeeping does, for the linearity test: seg entries+ * visited or copied (`segs`) and steps the UTF-8 offset walk takes+ * (`units`). Counts, not time, so a busy machine cannot move them.+ */+export const spanWork = { segs: 0, units: 0 };+function runOffset(run: Run, t: number): number | null {- for (const seg of run.segs) if (t >= seg.t && t < seg.t + seg.n) return seg.s + (t - seg.t);+ for (const seg of run.segs) {+ spanWork.segs++;+ if (t >= seg.t && t < seg.t + seg.n) return seg.s + (t - seg.t);+ }return null;}@@ -175,6 +185,7 @@ function trimmedRun(run: Run): Run {}function sliceRun(run: Run, from: number, to: number): Run {+ spanWork.segs += run.segs.length;const segs: Seg[] = [];for (const seg of run.segs) {const a = Math.max(from, seg.t);@@ -185,6 +196,7 @@ function sliceRun(run: Run, from: number, to: number): Run {}function joinRuns(a: Run, b: Run): Run {+ spanWork.segs += a.segs.length + b.segs.length;return { text: a.text + b.text, segs: [...a.segs, ...b.segs.map(seg => ({ ...seg, t: seg.t + a.text.length }))] };}@@ -209,8 +221,10 @@ export function utf16ToUtf8Offsets(text: string, offsets: readonly number[]): Maconst out = new Map<number, number>();let bytes = 0;let i = 0;+ let walked = 0;for (const target of sorted) {while (i < target && i < text.length) {+ walked++;const c = text.charCodeAt(i);if (c < 0x80) bytes += 1;else if (c < 0x800) bytes += 2;@@ -223,6 +237,7 @@ export function utf16ToUtf8Offsets(text: string, offsets: readonly number[]): Ma}if (!out.has(target)) out.set(target, bytes);}+ spanWork.units += walked;return out;}diff --git a/test/chunker-spans-linear.slow.test.ts b/test/chunker-spans-linear.slow.test.tsindex ba69beb41b..2338b837f2 100644--- a/test/chunker-spans-linear.slow.test.ts+++ b/test/chunker-spans-linear.slow.test.ts@@ -3,19 +3,17 @@** Span tracking carries offset segments through merge and overlap; copying a* run's segments on every merge made the merge quadratic in pieces per chunk.- * Doubling the input must roughly double the time with spans on, for chat-log- * text and for a page of one-word paragraphs (the most pieces per chunk). The- * 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. 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.+ * That cost is per chunk, so doubling the input only doubles it: a time ratio+ * between 2 MB and 4 MB read 2.0x on the quadratic merge too (3.2x slower in+ * absolute terms), and on shared CI runners thread CPU time itself grows+ * when a co-tenant slows the core (a 2 MB / 4 MB ratio of 3.04 on linear+ * code; GC is under 2% of the run). So this test counts the bookkeeping's+ * work instead (`spanWork`: seg entries visited or copied, steps the+ * UTF-8 offset walk takes) and bounds it per chunk and per input code unit.+ * Counts are deterministic: no machine can move them.*/import { describe, test, expect } from 'bun:test';-import { chunkText } from '../src/core/chunkers/recursive.ts';-import { cpuMs } from './helpers/cpu-time.ts';+import { chunkText, spanWork } from '../src/core/chunkers/recursive.ts';function chatLog(chars: number): string {let a = 0x9e3779b9;@@ -41,34 +39,37 @@ function chatLog(chars: number): string {return parts.join('');}-function runMs(text: string): number {- let chunks: ReturnType<typeof chunkText> = [];- const ms = cpuMs(() => { chunks = chunkText(text, { spans: true }); });+/** Span bookkeeping work for one chunkText call. */+function work(text: string): { chunks: number; segs: number; units: number } {+ spanWork.segs = 0;+ spanWork.units = 0;+ const 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++) {- s = Math.min(s, runMs(small));- l = Math.min(l, runMs(large));- }- return [s, l];+ return { chunks: chunks.length, segs: spanWork.segs, units: spanWork.units };}const MB = 1024 * 1024;+/** Seg entries per chunk: about 10 with in-place merging; the per-merge copy read 470 (chat) and 103,000 (one-word). */+const SEGS_PER_CHUNK = 32;describe('chunkText(spans: true) scales linearly', () => {for (const [name, make] of [['chat log', chatLog],['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, large] = bestPair(make(2 * MB), make(4 * MB));- expect(large / small).toBeLessThan(3);+ test(`${name}: span work per chunk is bounded and doubles with the input`, () => {+ const smallText = make(2 * MB);+ const largeText = make(4 * MB);+ const small = work(smallText);+ const large = work(largeText);+ for (const [w, text] of [[small, smallText], [large, largeText]] as const) {+ expect(w.segs / w.chunks).toBeLessThan(SEGS_PER_CHUNK);+ // One pass over the projected text, never a walk per offset.+ expect(w.units).toBeLessThanOrEqual(text.length);+ }+ expect(large.chunks / small.chunks).toBeLessThan(2.1);+ expect(large.segs / small.segs).toBeLessThan(2.1);+ expect(large.units / small.units).toBeLessThan(2.1);}, 120_000);}});