Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: large-brain ceilings asserts the watchdog branch that ran, with a forced case for each order of first commit and deadline

garrytanbb97369e5014
diff --git a/test/helpers/sync-watchdog-order-preload.ts b/test/helpers/sync-watchdog-order-preload.ts
new file mode 100644
index 0000000000..e5c5ea68bb
--- /dev/null
+++ b/test/helpers/sync-watchdog-order-preload.ts
@@ -0,0 +1,76 @@
+/**
+ * Child-process preload for test/large-brain-ceilings.test.ts: fixes the order of a sync's first committed page and
+ * its hard deadline, so each branch of the sync watchdog's deadline message runs on demand.
+ * `$GBRAIN_TEST_SYNC_WATCHDOG_ORDER` picks the order; without it the preload does nothing.
+ *
+ * commit-first the watchdog worker starts at the first committed page, so that commit is noted before its
+ * deadline ("still progressing")
+ * deadline-first committed-page notes reach the worker 3 s after its deadline ("before the run committed anything")
+ *
+ * Only the worker labelled `sync-watchdog` is touched. Its source, deadline, grace and progress window are passed
+ * through unchanged. Loaded with `BUN_OPTIONS=--preload <this file>`.
+ */
+export {};
+
+type SyncWatchdogWorkerOptions = { workerData?: { label?: string; deadlineMs?: number } };
+type RealWorker = {
+ on(event: string, listener: (...args: unknown[]) => void): unknown;
+ postMessage(message: unknown): void;
+ terminate(): Promise<number>;
+ unref(): void;
+};
+
+const order = process.env.GBRAIN_TEST_SYNC_WATCHDOG_ORDER;
+const DELIVER_PAST_DEADLINE_MS = 3000;
+
+if (order) {
+ if (order !== 'commit-first' && order !== 'deadline-first') throw new Error(`GBRAIN_TEST_SYNC_WATCHDOG_ORDER is not commit-first or deadline-first: ${order}`);
+ // A named `import { Worker }` binds when its module loads; the CommonJS export object is what it reads from.
+ const workerThreads = require('node:worker_threads') as { Worker: new (src: string, opts: SyncWatchdogWorkerOptions) => RealWorker };
+ const Real = workerThreads.Worker;
+
+ class StartsAtFirstCommit {
+ #real: RealWorker | null = null;
+ #listeners: Array<[string, (...args: unknown[]) => void]> = [];
+ #unref = false;
+ #terminated = false;
+ constructor(private readonly src: string, private readonly opts: SyncWatchdogWorkerOptions) {}
+ on(event: string, listener: (...args: unknown[]) => void) {
+ this.#listeners.push([event, listener]);
+ this.#real?.on(event, listener);
+ return this;
+ }
+ unref() {
+ this.#unref = true;
+ this.#real?.unref();
+ }
+ postMessage(message: unknown) {
+ if (this.#terminated) return;
+ if (!this.#real) {
+ this.#real = new Real(this.src, this.opts);
+ for (const [event, listener] of this.#listeners) this.#real.on(event, listener);
+ if (this.#unref) this.#real.unref();
+ }
+ this.#real.postMessage(message);
+ }
+ terminate() {
+ this.#terminated = true;
+ return this.#real?.terminate() ?? Promise.resolve(0);
+ }
+ }
+
+ workerThreads.Worker = function OrderedWorker(src: string, opts: SyncWatchdogWorkerOptions) {
+ if (opts?.workerData?.label !== 'sync-watchdog') return new Real(src, opts);
+ if (order === 'commit-first') return new StartsAtFirstCommit(src, opts);
+ const real = new Real(src, opts);
+ const deliverAt = Date.now() + (opts.workerData.deadlineMs ?? 0) + DELIVER_PAST_DEADLINE_MS;
+ const post = real.postMessage.bind(real);
+ real.postMessage = (message: unknown) => {
+ const wait = deliverAt - Date.now();
+ if (wait <= 0) return post(message);
+ const timer = setTimeout(() => { try { post(message); } catch { /* worker gone */ } }, wait);
+ (timer as unknown as { unref?: () => void }).unref?.();
+ };
+ return real;
+ } as unknown as typeof Real;
+}
diff --git a/test/large-brain-ceilings.test.ts b/test/large-brain-ceilings.test.ts
index 7da2a2a21a..d3fe1edc4d 100644
--- a/test/large-brain-ceilings.test.ts
+++ b/test/large-brain-ceilings.test.ts
@@ -14,7 +14,11 @@
* Why new: test/process-watchdog-harness.test.ts drives the watchdog through a
* harness and test/persistence-large-manifest.test.ts calls the lifecycle
* function directly; neither runs the real CLI commands the operator runs.
- * Seam: none. Deadlines are shortened through the documented env knob, not by
+ * Either order of the 1 s deadline and the first committed page, each
+ * forced once (test/helpers/sync-watchdog-order-preload.ts): the
+ * watchdog's deadline line names the one that ran, and the run completes.
+ * Seam: the order preload only delays when the watchdog worker starts or hears
+ * of a commit. Deadlines are shortened through the documented env knob, not by
* waiting an hour.
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
@@ -25,6 +29,7 @@ import { runCli } from './helpers/cli-spawn.ts';
import { commitFixtureRepository, writeLargeWorktree } from './helpers/large-worktree.ts';
const LARGE_CHECKOUT_FILES = 20_000;
+const ORDER_PRELOAD = join(import.meta.dir, 'helpers', 'sync-watchdog-order-preload.ts');
const KEYS = { OPENAI_API_KEY: undefined, ANTHROPIC_API_KEY: undefined, VOYAGE_API_KEY: undefined };
function commitWorktree(root: string, files: number): void {
@@ -44,22 +49,46 @@ async function freshBrain(name: string): Promise<string> {
}
describe('large-brain ceilings (CLI)', () => {
- test('a sync that outlasts its deadline window keeps going while it imports, and completes', async () => {
- const home = await freshBrain('sync');
- const repo = join(dir, 'sync', 'repo');
+ // The watchdog's deadline line names which came first, the deadline or the first committed page: a commit by the
+ // 1 s deadline extends the run as "still progressing"; a run with no commit by then gets one progress window from
+ // its start. Either way it keeps importing and completes.
+ async function syncPastDeadline(name: string, order?: 'commit-first' | 'deadline-first') {
+ const home = await freshBrain(name);
+ const repo = join(dir, name, 'repo');
commitWorktree(repo, 150);
const add = await runCli(['sources', 'add', 'notes', '--path', repo], { home, env: KEYS, timeoutMs: 120_000 });
expect(add.exitCode, add.stderr).toBe(0);
const sync = await runCli(['sync', '--source', 'notes', '--no-pull'], {
home, timeoutMs: 180_000,
- env: { ...KEYS, GBRAIN_SYNC_MAX_RUNTIME_SECONDS: '1', GBRAIN_SYNC_STALL_ABORT_SECONDS: '60' },
+ env: {
+ ...KEYS, GBRAIN_SYNC_MAX_RUNTIME_SECONDS: '1', GBRAIN_SYNC_STALL_ABORT_SECONDS: '60',
+ ...(order ? { BUN_OPTIONS: `--preload ${ORDER_PRELOAD}`, GBRAIN_TEST_SYNC_WATCHDOG_ORDER: order } : {}),
+ },
});
expect(sync.stderr).toContain('extends while the sync keeps progressing');
- expect(sync.stderr).toContain('still progressing');
+ const deadline = sync.stderr.split('\n').filter((line) => line.startsWith('[sync-watchdog] deadline reached (1s)'));
+ expect(deadline, sync.stderr).toHaveLength(1);
+ const ran = deadline[0]!.includes('but the run is still progressing') ? 'commit-first' : 'deadline-first';
+ expect(deadline[0]).toContain(ran === 'commit-first'
+ ? 'but the run is still progressing'
+ : 'before the run committed anything; it stops as a stall unless it commits within 60s of its start');
expect(sync.stdout).not.toContain('sync_deadline_stop');
expect(sync.exitCode, sync.stderr).toBe(0);
expect(sync.stdout).toContain('150 file(s) imported');
+ return ran;
+ }
+
+ test('a sync that outlasts its deadline window keeps going while it imports, and completes', async () => {
+ await syncPastDeadline('sync');
+ }, 300_000);
+
+ test('a first page committed before the deadline extends the run as still progressing, and it completes', async () => {
+ expect(await syncPastDeadline('commit-first', 'commit-first')).toBe('commit-first');
+ }, 300_000);
+
+ test('a first page committed after the deadline gets the progress window from its start, and the run completes', async () => {
+ expect(await syncPastDeadline('deadline-first', 'deadline-first')).toBe('deadline-first');
}, 300_000);
test('sources add registers a 20,000-file checkout', async () => {