Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
test(sweep): the budget test reads an injected clock, not machine load
garrytan5d6ccb3063e2
diff --git a/src/core/sweep.ts b/src/core/sweep.tsindex 63ff524f3a..72eb6698ce 100644--- a/src/core/sweep.ts+++ b/src/core/sweep.ts@@ -100,6 +100,8 @@ export interface SweepOpts {batchLimit?: number;/** Wall-clock budget; the sweep stops between items when exceeded. Default 5000. */budgetMs?: number;+ /** The clock the budget reads (test seam, so a test's timing does not depend on machine load). Default Date.now. */+ now?: () => number;/** Recency window (days) for "recently-modified pages". Default 7. */recentDays?: number;/** Diagnostic sink (stderr in serve contexts). Default: silent. */@@ -149,7 +151,8 @@ export async function runMaintenanceSweep(engine: BrainEngine,opts: SweepOpts = {},): Promise<SweepReport> {- const started = Date.now();+ const now = opts.now ?? Date.now;+ const started = now();const sourceId = opts.sourceId ?? 'default';const batchLimit = Math.max(1, opts.batchLimit ?? 20);const budgetMs = Math.max(0, opts.budgetMs ?? 5_000);@@ -174,7 +177,7 @@ export async function runMaintenanceSweep(if (existing) existing.count += count;else report.skipped.push({ reason, count });};- const overBudget = () => Date.now() >= deadline;+ const overBudget = () => now() >= deadline;// Budget abort signal: threads into the fence pass's per-page loop so a// long item can be interrupted at its own checkpoints. unref'd — the sweep must never hold the@@ -182,7 +185,7 @@ export async function runMaintenanceSweep(const budgetController = new AbortController();const budgetTimer = setTimeout(() => budgetController.abort(),- Math.max(0, deadline - Date.now()),+ Math.max(0, deadline - now()),);budgetTimer.unref?.();@@ -284,7 +287,7 @@ export async function runMaintenanceSweep(clearTimeout(budgetTimer);}- report.durationMs = Date.now() - started;+ report.durationMs = now() - started;return report;}diff --git a/test/sweep-budget-between-items.test.ts b/test/sweep-budget-between-items.test.tsindex 217b33be50..05567331c6 100644--- a/test/sweep-budget-between-items.test.ts+++ b/test/sweep-budget-between-items.test.ts@@ -6,6 +6,12 @@* each scheduled `sweep --once` paid one model call and finished nothing.** Hermetic in-memory PGLite + chat-transport stub (sweep-corpus-windows harness).+ * The budget reads an injected clock that only the (stubbed) extraction+ * advances, by 400 ms per call: the passes before the corpus take no budget+ * however loaded the machine is, so the first extraction always starts, runs+ * past the 150 ms budget, and the budget then holds the next file. The stub+ * also really waits 400 ms, past the real budget timer, so an abort reaching+ * the extraction would still cut it off.*/import { afterAll, afterEach, beforeAll, beforeEach, expect, test } from 'bun:test';import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';@@ -28,6 +34,9 @@ const KEYED: CapabilityReport = {let engine: PGLiteEngine;let dir: string;let calls = 0;+/** The sweep's clock: advanced only by extractions. */+let clock = 0;+const now = () => clock;beforeAll(async () => {engine = new PGLiteEngine();@@ -41,9 +50,11 @@ beforeEach(async () => {dir = mkdtempSync(join(tmpdir(), 'gbrain-sweep-budget-'));await engine.setConfig('dream.synthesize.session_corpus_dir', dir);calls = 0;+ clock = 1_000_000;__resetCheckpointHarvestForTests();__setChatTransportForTests(async (): Promise<ChatResult> => {calls++;+ clock += 400;await new Promise((r) => setTimeout(r, 400));return {text: JSON.stringify({ facts: [] }),@@ -65,7 +76,7 @@ test('an extraction slower than the budget finishes and writes its sidecar; thewriteFileSync(join(dir, 'a.txt'), text);writeFileSync(join(dir, 'b.txt'), text.replace('Thursdays', 'Fridays'));- const r = await runMaintenanceSweep(engine, { sourceId: 'default', capabilities: KEYED, budgetMs: 150, batchLimit: 20 });+ const r = await runMaintenanceSweep(engine, { sourceId: 'default', capabilities: KEYED, budgetMs: 150, batchLimit: 20, now });expect(calls).toBe(1);expect(existsSync(join(dir, 'a.txt' + CORPUS_INGESTED_SUFFIX))).toBe(true);@@ -73,7 +84,7 @@ test('an extraction slower than the budget finishes and writes its sidecar; theexpect(existsSync(join(dir, 'b.txt' + CORPUS_INGESTED_SUFFIX))).toBe(false);expect(r.skipped.some((s) => s.reason === 'budget_exhausted:corpus')).toBe(true);- const again = await runMaintenanceSweep(engine, { sourceId: 'default', capabilities: KEYED, budgetMs: 150, batchLimit: 20 });+ const again = await runMaintenanceSweep(engine, { sourceId: 'default', capabilities: KEYED, budgetMs: 150, batchLimit: 20, now });expect(calls).toBe(2);expect(again.corpusIngested).toBe(1);expect(existsSync(join(dir, 'b.txt' + CORPUS_INGESTED_SUFFIX))).toBe(true);