Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Stop reindex --synopsis from hanging on a failed status read, and keep its --json output clean
garrytand21c25325693
diff --git a/src/commands/reindex-synopsis.ts b/src/commands/reindex-synopsis.tsindex 9f58ec3be4..5f9e2697e2 100644--- a/src/commands/reindex-synopsis.ts+++ b/src/commands/reindex-synopsis.ts@@ -189,18 +189,26 @@ export async function runReindexSynopsis(engine: BrainEngine, args: string[], opconst pollMs = opts.pollMs ?? 500;const worker = new MinionWorker(engine, { pollInterval: pollMs, healthCheckInterval: 0 });worker.register(SYNOPSIS_SWEEP_JOB, makeContextualReindexHandler({ engine }));- const running = worker.start();- let settled = false;- const watch = (async () => {- while (!settled) {- await new Promise(resolve => setTimeout(resolve, pollMs));- const rows = await engine.executeRaw<{ status: string }>('SELECT status FROM minion_jobs WHERE id = ANY($1::int[])', [pendingIds]);- if (rows.every(r => TERMINAL.has(r.status))) { settled = true; worker.stop(); }- }- })();- await running;- settled = true;- await watch.catch(() => undefined);+ const log = console.log;+ if (json) console.log = (...line: unknown[]) => console.error(...line);+ let watchError: unknown;+ try {+ const running = worker.start();+ let settled = false;+ const watch = (async () => {+ try {+ while (!settled) {+ await new Promise(resolve => setTimeout(resolve, pollMs));+ const rows = await engine.executeRaw<{ status: string }>('SELECT status FROM minion_jobs WHERE id = ANY($1::int[])', [pendingIds]);+ if (rows.every(r => TERMINAL.has(r.status))) { settled = true; worker.stop(); }+ }+ } catch (error) { watchError = error; worker.stop(); }+ })();+ await running;+ settled = true;+ await watch;+ } finally { console.log = log; }+ if (watchError) throw watchError;if (worker.configurationError) {const { reportInlineWorkerConfiguration } = await import('./jobs-readiness.ts');reportInlineWorkerConfiguration(worker.configurationError);diff --git a/test/synopsis-sweep-8744.serial.test.ts b/test/synopsis-sweep-8744.serial.test.tsindex 96ecaa8eee..be9a2fddc2 100644--- a/test/synopsis-sweep-8744.serial.test.ts+++ b/test/synopsis-sweep-8744.serial.test.ts@@ -226,4 +226,38 @@ for (const kind of testBackends()) describe(`#8744 synopsis sweep (${kind})`, ()expect(rerun.plan!.pages).toEqual([]);expect(chatCalls).toBe(0);});++ test('--yes --json: stdout is one JSON document, the in-process worker logs go to stderr', async () => {+ await importFromContent(engine, 'notes/gamma-example', body('Gamma example', 2), { sourceId: 'default' });+ const out: string[] = [];+ const { log, error } = console;+ const write = process.stdout.write.bind(process.stdout);+ const writeErr = process.stderr.write.bind(process.stderr);+ console.log = (...args: unknown[]) => { out.push(args.map(String).join(' ') + '\n'); };+ console.error = () => {};+ process.stdout.write = ((chunk: string | Uint8Array) => { out.push(String(chunk)); return true; }) as typeof process.stdout.write;+ process.stderr.write = (() => true) as typeof process.stderr.write;+ let result;+ try { result = await runReindexSynopsis(engine, ['--synopsis', '--yes', '--max-usd', '5', '--json'], { pollMs: 100 }); }+ finally { console.log = log; console.error = error; process.stdout.write = write; process.stderr.write = writeErr; }+ expect(result.run).toMatchObject({ synopsis: 1, failed: 0 });+ expect(JSON.parse(out.join('')).run).toMatchObject({ synopsis: 1, failed: 0 });+ });++ test('a failed status poll stops the in-process worker and the run fails with that error instead of hanging', async () => {+ await importFromContent(engine, 'notes/delta-example', body('Delta example', 2), { sourceId: 'default' });+ const original = engine.executeRaw;+ let failed = false;+ engine.executeRaw = (async function (this: BrainEngine, sql: string, params?: unknown[]) {+ if (!failed && sql.startsWith('SELECT status FROM minion_jobs')) { failed = true; throw new Error('connection reset by the pooler'); }+ return original.call(this, sql, params as never);+ }) as typeof engine.executeRaw;+ try {+ const outcome = await Promise.race([+ quiet(() => runReindexSynopsis(engine, ['--synopsis', '--yes', '--max-usd', '5'], { pollMs: 100 })).then(() => 'resolved', (e: Error) => e.message),+ new Promise<string>(resolve => setTimeout(() => resolve('hung'), 20_000)),+ ]);+ expect(outcome).toBe('connection reset by the pooler');+ } finally { engine.executeRaw = original; }+ }, 30_000);});