Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(autopilot): the between-tick HEAD probe stands down while the process runs a resident commit notifier (no double dispatch)
garrytan779138b48011
diff --git a/src/commands/autopilot-daemon.ts b/src/commands/autopilot-daemon.tsindex a11d7a2ac0..e431cedbc3 100644--- a/src/commands/autopilot-daemon.ts+++ b/src/commands/autopilot-daemon.ts@@ -350,7 +350,7 @@ export async function runAutopilotDaemon(engine: BrainEngine, args: string[]): P// Wait for next cycle; #7003: --status grades staleness from this daemon's own next tick.writeAutopilotHeartbeat({ phase: 'sleep', interval_s: interval, base_interval_s: baseInterval, next_tick_at: new Date(Date.now() + interval * 1000).toISOString() });- if (useMinionsDispatch) await sleepWatchingHeads(engine, state, { sleepMs: interval * 1000, baseInterval, jsonMode });+ if (useMinionsDispatch) await sleepWatchingHeads(engine, state, { sleepMs: interval * 1000, baseInterval, jsonMode }, { notifierActive: () => notifier !== null });else await new Promise(r => setTimeout(r, interval * 1000));}}diff --git a/src/commands/autopilot-dispatch.ts b/src/commands/autopilot-dispatch.tsindex d6161aa5c8..4729096b67 100644--- a/src/commands/autopilot-dispatch.ts+++ b/src/commands/autopilot-dispatch.ts@@ -412,6 +412,8 @@ export interface HeadProbeDeps {sleep?: (ms: number) => Promise<void>;readHead?: (root: string) => Promise<{ head: string; operation: string | null } | null>;dispatch?: typeof dispatchFreshnessSyncs;+ /** True while this process runs a resident commit notifier (lane 2a.1): it, or the host's notifier owner, syncs on commit, so the probe stands down. */+ notifierActive?: () => boolean;}/** One probe pass's record: the sources whose HEAD moved past what was synced or decided, and what the dispatch decided. */@@ -422,7 +424,8 @@ export interface HeadProbeRecord { at: number; moved: string[]; decisions: Fresh* HEAD every `GBRAIN_AUTOPILOT_HEAD_PROBE_MS` (default 3 s, 1 to 30 s; git only, no database read) and runs the freshness* dispatch for the sources whose HEAD moved past both their last synced commit and the HEAD this daemon last decided* for, so a commit is queued within seconds instead of at the next tick (150 to 600 s). The flag is re-read every 30 s- * and the source list every 60 s. With the flag off it only sleeps, and the tick's HEAD check stays the trigger.+ * and the source list every 60 s. With the flag off, or while this process runs a resident commit notifier (which owns+ * on-commit syncs for the host), it only sleeps, and the tick's HEAD check stays the trigger.* Returns each pass that saw a move (the recorded decisions the tests read).*/export async function sleepWatchingHeads(@@ -450,7 +453,7 @@ export async function sleepWatchingHeads(flagReadAt = now();enabled = /^(1|true|on|yes)$/i.test((await engine.getConfig(SYNC_ON_COMMIT_KEY))?.trim() ?? '');}- if (!enabled || autopilotPaused()) continue;+ if (!enabled || autopilotPaused() || deps.notifierActive?.()) continue;if (now() - listedAt >= HEAD_PROBE_LIST_EVERY_MS) {listedAt = now();sources = (await loadAllSources(engine)).filter(s => !s.archived && s.local_path && !isConnectorSourceKind(parseSourceConfig(s.config).kind))diff --git a/test/pathological/s-autopilot-head-dispatch.test.ts b/test/pathological/s-autopilot-head-dispatch.test.tsindex 11d4223f59..327ee37e3a 100644--- a/test/pathological/s-autopilot-head-dispatch.test.ts+++ b/test/pathological/s-autopilot-head-dispatch.test.ts@@ -115,7 +115,7 @@ for (const { backend, databaseUrl } of pathologicalArms()) {});}, 240_000);- test('between ticks, with sync.on_commit on, the HEAD probe dispatches a new commit at its first pass; with it off nothing is read', async () => {+ test('between ticks, with sync.on_commit on, the HEAD probe dispatches a new commit at its first pass; with it off, or while a resident notifier runs in the process, nothing is read', async () => {const { sleepWatchingHeads } = await import('../../src/commands/autopilot-dispatch.ts');await withRepo(async r => {const head = await syncedThenCommitted(r, 'between-ticks');@@ -131,6 +131,10 @@ for (const { backend, databaseUrl } of pathologicalArms()) {expect({ off, reads, jobs: await syncJobs(r.engine, r.sourceId) }).toEqual({ off: [], reads: 0, jobs: [] });await r.engine.setConfig('sync.on_commit', 'true');+ clock = 1_500_000;+ const deferred = await quietly(() => sleepWatchingHeads(r.engine, state, { sleepMs: 30_000, baseInterval: 300, jsonMode: true }, { ...deps, notifierActive: () => true }));+ expect({ deferred, reads, jobs: await syncJobs(r.engine, r.sourceId) }).toEqual({ deferred: [], reads: 0, jobs: [] });+clock = 2_000_000;const on = await quietly(() => sleepWatchingHeads(r.engine, state, { sleepMs: 30_000, baseInterval: 300, jsonMode: true }, deps));expect(on.length).toBe(1);