Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(serve): on-commit watcher skips a held source instead of starving others; stop() settles in-flight work

garrytan85430fc00050
diff --git a/src/commands/serve-http.ts b/src/commands/serve-http.ts
index fbb8288959..28b576331c 100644
--- a/src/commands/serve-http.ts
+++ b/src/commands/serve-http.ts
@@ -917,7 +917,7 @@ ${bootstrapFromEnv
const { startServeHeadWatcher } = await import('../core/serve-head-watcher.ts');
const headWatcher = startServeHeadWatcher(engine, { log: (line) => console.error(line) });
const deregisterEngineCleanup = registerCleanup('pglite-engine-disconnect', async () => {
- headWatcher.stop();
+ await headWatcher.stop();
movementWatch.stop();
await factsDrain.stop();
await engine.disconnect();
@@ -925,7 +925,7 @@ ${bootstrapFromEnv
try {
await waitForHttpServerLifecycle(httpServer);
} finally {
- headWatcher.stop();
+ await headWatcher.stop();
movementWatch.stop();
await factsDrain.stop();
// Close the IPC listener + reap the socket file on orderly shutdown
diff --git a/src/commands/serve.ts b/src/commands/serve.ts
index 495b2fce5c..37b7cd8412 100644
--- a/src/commands/serve.ts
+++ b/src/commands/serve.ts
@@ -603,7 +603,7 @@ function installResidentTickers(engine: BrainEngine, opts: ServeOptions, deps: S
// The watch keeps its own unref'd timer: the injected lifecycle timers are the parent watchdog's and the idle sweep's, which tests count.
const movementWatch: MovementWatch = startMovementWatch(engine, { log: deps.log });
const headWatcher = startServeHeadWatcher(engine, { log: deps.log });
- return { stop: async () => { headWatcher.stop(); movementWatch.stop(); await factsDrain?.stop(); } };
+ return { stop: async () => { const settled = headWatcher.stop(); movementWatch.stop(); await settled; await factsDrain?.stop(); } };
}
function installStdioLifecycle(
diff --git a/src/core/serve-head-watcher.ts b/src/core/serve-head-watcher.ts
index e44f3b0089..27d6ba9145 100644
--- a/src/core/serve-head-watcher.ts
+++ b/src/core/serve-head-watcher.ts
@@ -20,8 +20,10 @@
* flipping `sync.on_commit` needs no restart) and the source list (every 60 s while on). With the flag off nothing
* else runs: no git call, no sync. It never writes into the checkout or `.git` (`GIT_OPTIONAL_LOCKS=0`). Every error
* is caught and logged once per distinct message; the loop never throws into serve. serve.ts starts it beside the
- * movement watch (installResidentTickers) and stops it before shutdownDelegatedSync; serve-http.ts (runServeHttp)
- * starts it beside the movement watch and stops it on the same shutdown path.
+ * movement watch (installResidentTickers) and awaits its stop before shutdownDelegatedSync; serve-http.ts (runServeHttp)
+ * starts it beside the movement watch and awaits its stop on the same shutdown path, before the engine disconnects.
+ * `stop()` re-checks after every await, so discovery or a git read in flight when it lands installs nothing and runs
+ * no further git or database call; its promise settles once that step has (bounded by STOP_SETTLE_MS).
*/
import { randomUUID } from 'node:crypto';
import { existsSync, watch as fsWatch } from 'node:fs';
@@ -37,6 +39,8 @@ export const HEAD_FLAG_EVERY_MS = 30_000;
export const HEAD_LIST_EVERY_MS = 60_000;
/** Resumable syncs in a row the watcher resumes on its own before it leaves the source to the next move or cron. */
export const MAX_RESUMES = 3;
+/** How long `stop()` waits for an in-flight step before it resolves anyway (inside the stdio serve's 5 s cleanup deadline). */
+export const STOP_SETTLE_MS = 3_000;
const NUDGE_MS = 250;
const LOG_ONCE_CAP = 200;
@@ -66,7 +70,8 @@ export interface ServeHeadWatcherOptions {
watch?: (gitDir: string, onChange: () => void) => (() => void) | null;
}
-export interface ServeHeadWatcher { tick(): Promise<void>; stop(): void; readonly enabled: boolean }
+/** `stop()` stops polling at once and resolves when the in-flight step has settled (at most STOP_SETTLE_MS); await it before the engine disconnects. */
+export interface ServeHeadWatcher { tick(): Promise<void>; stop(): Promise<void>; readonly enabled: boolean }
interface SourceState extends WatchedSource { head: string | null; movedAt: number | null; synced: string | null; resumes: number }
@@ -192,7 +197,7 @@ export function startServeHeadWatcher(engine: BrainEngine, opts: ServeHeadWatche
const refreshSources = async () => {
const listed = await guard('listing managed sources', d.listSources, null);
- if (!listed) return;
+ if (!listed || stopped) return;
const seen = new Set<string>();
for (const s of listed) {
seen.add(s.sourceId);
@@ -206,6 +211,7 @@ export function startServeHeadWatcher(engine: BrainEngine, opts: ServeHeadWatche
for (const dir of dirs) {
if (watchers.has(dir)) continue;
const close = await guard(`watching ${dir}`, async () => d.watch(dir, nudge), null);
+ if (close && stopped) { try { close(); } catch { /* best effort */ } return; }
if (close) watchers.set(dir, close);
}
};
@@ -237,6 +243,7 @@ export function startServeHeadWatcher(engine: BrainEngine, opts: ServeHeadWatche
if (t - flagReadAt >= HEAD_FLAG_EVERY_MS) {
flagReadAt = t;
const on = await guard('reading sync.on_commit', d.readFlag, enabled);
+ if (stopped) return;
if (on !== enabled) {
enabled = on;
listedAt = -Infinity;
@@ -248,6 +255,7 @@ export function startServeHeadWatcher(engine: BrainEngine, opts: ServeHeadWatche
if (t - listedAt >= HEAD_LIST_EVERY_MS) { listedAt = t; await refreshSources(); }
const heads = new Map<string, string | null>();
for (const s of sources.values()) {
+ if (stopped) return;
if (!heads.has(s.root)) heads.set(s.root, await guard(`reading HEAD in ${s.root}`, () => d.revParse(s.root), null));
const head = heads.get(s.root) ?? null;
if (head && head !== s.head) { s.head = head; s.movedAt = t; armFire(debounceMs); }
@@ -258,23 +266,31 @@ export function startServeHeadWatcher(engine: BrainEngine, opts: ServeHeadWatche
settle(job, st);
}
if (stopped) return;
- const due = [...sources.values()].find(s => s.head && s.head !== s.synced && (s.movedAt === null || now() - s.movedAt >= debounceMs));
- if (!due || await d.syncRunning()) return;
- const synced = await guard(`reading the last synced commit of ${due.sourceId}`, () => d.lastCommit(due.sourceId), null);
- if (synced && synced === due.head) { due.synced = synced; due.resumes = 0; return; }
- if (await guard(`reading the drain lease of ${due.sourceId}`, () => d.drainHeld(due.sourceId), true)) return;
- if (stopped) return;
- const head = due.head!;
- const start = await guard(`starting a sync of ${due.sourceId}`, () => d.startSync(due.sourceId), null);
- if (!start) { due.synced = head; return; }
- if ('busy' in start) return;
- if ('refused' in start) {
- logOnce(`refused\u0000${due.sourceId}\u0000${start.refused}`, `${due.sourceId}: the serve refused the sync: ${start.refused} (not retried until HEAD moves)`);
- due.synced = head;
+ // Every due source in order: one whose drain lease is held or whose last_commit already covers HEAD is skipped, so a
+ // long sync of one source never starves another; at most one start per step (global single flight on syncRunning).
+ const due = [...sources.values()].filter(s => s.head && s.head !== s.synced && (s.movedAt === null || now() - s.movedAt >= debounceMs));
+ if (!due.length || await d.syncRunning()) return;
+ for (const s of due) {
+ if (stopped) return;
+ const synced = await guard(`reading the last synced commit of ${s.sourceId}`, () => d.lastCommit(s.sourceId), null);
+ if (stopped) return;
+ if (synced && synced === s.head) { s.synced = synced; s.resumes = 0; continue; }
+ const held = await guard(`reading the drain lease of ${s.sourceId}`, () => d.drainHeld(s.sourceId), true);
+ if (stopped) return;
+ if (held) continue;
+ const head = s.head!;
+ const start = await guard(`starting a sync of ${s.sourceId}`, () => d.startSync(s.sourceId), null);
+ if (!start) { s.synced = head; return; }
+ if ('busy' in start) return;
+ if ('refused' in start) {
+ logOnce(`refused\u0000${s.sourceId}\u0000${start.refused}`, `${s.sourceId}: the serve refused the sync: ${start.refused} (not retried until HEAD moves)`);
+ s.synced = head;
+ return;
+ }
+ job = { id: start.jobId, sourceId: s.sourceId, head };
+ log(`${s.sourceId}: HEAD moved to ${short(head)} (last synced ${short(s.synced)}); started managed sync job ${start.jobId}`);
return;
}
- job = { id: start.jobId, sourceId: due.sourceId, head };
- log(`${due.sourceId}: HEAD moved to ${short(head)} (last synced ${short(due.synced)}); started managed sync job ${start.jobId}`);
};
const tick = (): Promise<void> => {
@@ -293,12 +309,18 @@ export function startServeHeadWatcher(engine: BrainEngine, opts: ServeHeadWatche
return {
tick,
get enabled() { return enabled; },
- stop: () => {
- if (stopped) return;
- stopped = true;
- (opts.clearInterval ?? ((h: unknown) => clearInterval(h as ReturnType<typeof setInterval>)))(interval);
- if (fireTimer !== null) clearTimer(fireTimer);
- if (nudgeTimer !== null) clearTimer(nudgeTimer);
+ stop: async () => {
+ if (!stopped) {
+ stopped = true;
+ (opts.clearInterval ?? ((h: unknown) => clearInterval(h as ReturnType<typeof setInterval>)))(interval);
+ if (fireTimer !== null) clearTimer(fireTimer);
+ if (nudgeTimer !== null) clearTimer(nudgeTimer);
+ closeWatchers();
+ }
+ if (!inflight) return;
+ let bound: ReturnType<typeof setTimeout> | undefined;
+ await Promise.race([inflight, new Promise<void>(resolve => { bound = setTimeout(resolve, STOP_SETTLE_MS); bound.unref?.(); })]);
+ clearTimeout(bound);
closeWatchers();
},
};
diff --git a/test/serve-head-watcher.test.ts b/test/serve-head-watcher.test.ts
index 65bb553c46..2102c2485e 100644
--- a/test/serve-head-watcher.test.ts
+++ b/test/serve-head-watcher.test.ts
@@ -6,7 +6,9 @@
* - a commit during the watcher's own sync starts exactly one follow-up once that sync ends;
* - while another sync runs or another process holds the drain lease, the move waits and fires afterwards;
* - a resumable stop is resumed up to MAX_RESUMES times, then left to the next move;
- * - errors never leave the loop and are logged once per distinct message; stop() halts polling.
+ * - a source whose drain lease is held is skipped, so another due source still starts (no starvation);
+ * - errors never leave the loop and are logged once per distinct message; stop() halts polling, and a stop that lands
+ * while discovery is awaiting leaves no watcher open and no git call behind; its promise settles the in-flight step.
*/
import { describe, expect, test } from 'bun:test';
import type { BrainEngine } from '../src/core/engine.ts';
@@ -193,4 +195,59 @@ describe('serve HEAD watcher', () => {
expect(h.s.git).toBe(git);
expect(h.s.starts).toEqual([]);
});
+
+ test('a source whose drain lease is held does not starve another due source; it starts once its lease is free', async () => {
+ const held = new Set(['acme-example']);
+ const starts: string[] = [];
+ const h = harness({
+ listSources: async () => ['acme-example', 'alice-example'].map(sourceId => ({ sourceId, root: `/brains/${sourceId}`, gitDir: null, lastCommit: A })),
+ drainHeld: async (id) => held.has(id),
+ startSync: async (id) => { starts.push(id); h.s.jobs.set(`job-${id}`, { state: 'running' }); return { jobId: `job-${id}` }; },
+ }, { head: B });
+ for (let i = 0; i < 3; i++) { await h.w.tick(); h.advance(3000); }
+ expect(starts).toEqual(['alice-example']);
+ held.delete('acme-example');
+ h.finish('job-alice-example');
+ await h.w.tick();
+ expect(starts).toEqual(['alice-example', 'acme-example']);
+ });
+
+ test('a source whose last_commit already covers HEAD is skipped for the next due one', async () => {
+ const starts: string[] = [];
+ const h = harness({
+ listSources: async () => ['acme-example', 'alice-example'].map(sourceId => ({ sourceId, root: `/brains/${sourceId}`, gitDir: null, lastCommit: A })),
+ lastCommit: async (id) => id === 'acme-example' ? B : A,
+ startSync: async (id) => { starts.push(id); return { jobId: `job-${id}` }; },
+ jobStatus: () => ({ state: 'running' }),
+ }, { head: B });
+ await h.w.tick();
+ h.advance(3000);
+ await h.w.tick();
+ expect(starts).toEqual(['alice-example']);
+ });
+
+ test('stop() while discovery awaits: no watcher stays open, no git call runs, and stop() settles the in-flight step', async () => {
+ let release!: (rows: Array<{ sourceId: string; root: string; gitDir: string | null; lastCommit: string | null }>) => void;
+ let entered!: () => void;
+ const listed = new Promise<Parameters<typeof release>[0]>(resolve => { release = resolve; });
+ const entry = new Promise<void>(resolve => { entered = resolve; });
+ let open = 0;
+ const h = harness({
+ listSources: async () => { entered(); return listed; },
+ watch: () => { open++; return () => { open--; }; },
+ }, { head: B });
+ const flight = h.w.tick();
+ await entry;
+ let settled = false;
+ const stopping = h.w.stop().then(() => { settled = true; });
+ await Promise.resolve();
+ expect(settled).toBe(false);
+ release([{ sourceId: 'acme-example', root: '/brains/acme-example', gitDir: '/brains/acme-example/.git', lastCommit: A }]);
+ await stopping;
+ await flight;
+ expect(settled).toBe(true);
+ expect(open).toBe(0);
+ expect(h.s.git).toBe(0);
+ expect(h.s.starts).toEqual([]);
+ });
});