Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(autopilot): skip the tick on transient listAllSources errors instead of the legacy repo-root cycle (#5678)
garrytana5744606a508
diff --git a/src/commands/autopilot-fanout.ts b/src/commands/autopilot-fanout.tsindex 2f39f4fe79..d83c5f5934 100644--- a/src/commands/autopilot-fanout.ts+++ b/src/commands/autopilot-fanout.ts@@ -81,6 +81,14 @@ export interface FanoutOpts {pathExists?: (path: string) => boolean;}+/** True only for "the sources table does not exist" (pre-v0.18 brains). */+export function isMissingSourcesTable(e: unknown): boolean {+ const code = (e as { code?: unknown } | null)?.code;+ if (code === '42P01') return true; // Postgres undefined_table+ const message = e instanceof Error ? e.message : String(e);+ return /relation "?sources"? does not exist|no such table:? sources|sources table missing/i.test(message);+}+export interface FanoutResult {/** Source ids whose submission INSERTED a fresh job this tick. */dispatched: string[];@@ -433,11 +441,36 @@ export async function dispatchPerSource(const all = connectorIds.size ? await engine.listAllSources() : sources;sources = all.filter(s => connectorIds.has(s.id) || checkouts.has(s.id));} catch (e) {+ const message = e instanceof Error ? e.message : String(e);// Brand-new brain without sources table (pre-v0.18) — fall through- // to the legacy single-job path. The error path here also covers- // a misconfigured engine, but legacy fallback is safer than failing.+ // to the legacy single-job path.+ //+ // Any OTHER error (connection timeout, pooler CONNECTION_CLOSED, …) is+ // transient and says nothing about the brain's shape. Falling back then+ // dispatched a legacy cycle against repoPath on a multi-source brain,+ // which full-imported the repo root into the 'default' source and+ // duplicated every page under a new slug prefix. Skip this tick instead;+ // the next tick retries once the database is reachable.+ if (!isMissingSourcesTable(e)) {+ if (opts.jsonMode) {+ emit(JSON.stringify({ event: 'fanout_skipped', reason: 'sources_unavailable', error: message }));+ } else {+ log(`[dispatch] skipped tick: could not list sources (${message}); retrying next tick`);+ }+ return {+ dispatched: [],+ coalesced: [],+ skipped_fresh: [],+ skipped_cap: [],+ skipped_cooldown: [],+ skipped_unavailable_path: [],+ legacy_fallback: false,+ all_sources_fresh: false,+ all_sources_handled: false,+ };+ }if (opts.jsonMode) {- emit(JSON.stringify({ event: 'fanout_unavailable', error: e instanceof Error ? e.message : String(e) }));+ emit(JSON.stringify({ event: 'fanout_unavailable', error: message }));}sources = [];}diff --git a/test/autopilot-fanout.test.ts b/test/autopilot-fanout.test.tsindex 5a38b725fb..465b132a9f 100644--- a/test/autopilot-fanout.test.ts+++ b/test/autopilot-fanout.test.ts@@ -224,12 +224,13 @@ describe('resolveFanoutMax', () => {describe('dispatchPerSource — integration with stubbed engine + queue', () => {type AddedJob = { name: string; data: unknown; opts: Record<string, unknown> };- function makeStubs(sources: SourceRow[], opts?: { listThrows?: boolean }) {+ function makeStubs(sources: SourceRow[], opts?: { listThrows?: boolean; listError?: unknown }) {const added: AddedJob[] = [];let nextId = 100;const engine = {kind: 'postgres' as const,listAllSources: async () => {+ if (opts?.listError) throw opts.listError;if (opts?.listThrows) throw new Error('sources table missing');return sources;},@@ -272,6 +273,24 @@ describe('dispatchPerSource — integration with stubbed engine + queue', () =>expect(added.length).toBe(1);});+ test('missing sources table (Postgres 42P01) falls back to legacy', async () => {+ const err = Object.assign(new Error('relation "sources" does not exist'), { code: '42P01' });+ const { engine, queue, added, fanoutOpts } = makeStubs([], { listError: err });+ const result = await dispatchPerSource(engine, queue, fanoutOpts);+ expect(result.legacy_fallback).toBe(true);+ expect(added.length).toBe(1);+ });++ test('transient connection error skips the tick instead of the legacy repo-root cycle', async () => {+ const err = Object.assign(new Error('write CONNECT_TIMEOUT aws-0-us-west-1.pooler.supabase.com:5432'), { code: 'CONNECT_TIMEOUT' });+ const { engine, queue, added, events, fanoutOpts } = makeStubs([], { listError: err });+ const result = await dispatchPerSource(engine, queue, fanoutOpts);+ expect(result.legacy_fallback).toBe(false);+ expect(result.dispatched).toEqual([]);+ expect(added.length).toBe(0);+ expect(events.some((e) => e.includes('"fanout_skipped"'))).toBe(true);+ });+test('per-source fan-out: 2 stale sources, both dispatched with distinct keys', async () => {const { engine, queue, added, fanoutOpts } = makeStubs([src('alpha'), src('beta')]);const result = await dispatchPerSource(engine, queue, fanoutOpts);