Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sources): refuse sources refresh timeouts above the timer limit instead of firing at once

masashiono06110e4bbe67b9d2
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 4c7ead167d..ad1a585422 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -115,10 +115,10 @@ async function dirtyOverlap(git: Git, root: string, from: string, to: string): P
return { overlap: unique.filter(touches), preserved: unique.filter(path => !touches(path)), incoming: incoming.length };
}
-async function configuredMs(engine: BrainEngine, flag: number | undefined, env: string, key: string, fallback: number): Promise<number> {
+async function configuredMs(engine: BrainEngine, flag: number | undefined, env: string, key: string, fallback: number, max = Infinity): Promise<number> {
const raw = flag ?? (process.env[env] !== undefined && process.env[env] !== '' ? Number(process.env[env]) : undefined) ?? await engine.getConfig(key).then(v => v == null ? undefined : Number(v));
if (raw === undefined) return fallback;
- if (!Number.isFinite(raw) || raw < 0) throw new OperationError('invalid_params', `${key} (or ${env}) must be a whole number of milliseconds; got ${raw}.`,
+ if (!Number.isFinite(raw) || raw < 0 || raw > max) throw new OperationError('invalid_params', `${key} (or ${env}) must be a whole number of milliseconds${max < Infinity ? ` no greater than ${max} (the timer limit)` : ''}; got ${raw}.`,
`Run gbrain config set ${key} ${fallback} (or unset ${env}), then retry.`);
return Math.floor(raw);
}
@@ -202,7 +202,7 @@ async function precheck(engine: BrainEngine, git: Git, sourceId: string, fetchTi
if (fetched.code !== 0) throw refusal('fetch_failed',
fetched.timedOut ? `git fetch ${remote} did not finish within ${fetchTimeoutMs} ms; nothing changed.`
: `git fetch ${remote} failed (${fetched.stderr.trim().split('\n')[0] || `exit ${fetched.code}`}); nothing changed.`,
- `Retry gbrain sources refresh ${sourceId}; for a slow remote raise the bound: gbrain sources refresh ${sourceId} --fetch-timeout-ms ${Math.max(fetchTimeoutMs * 2, 120_000)} (or gbrain config set sources.refresh_fetch_timeout_ms <ms>).`);
+ `Retry gbrain sources refresh ${sourceId}; for a slow remote raise the bound: gbrain sources refresh ${sourceId} --fetch-timeout-ms ${Math.min(Math.max(fetchTimeoutMs * 2, 120_000), 2 ** 31 - 1)} (or gbrain config set sources.refresh_fetch_timeout_ms <ms>).`);
}
const oldHead = await revParse(git, root, 'HEAD');
const targetHead = await revParse(git, root, '@{upstream}');
@@ -318,7 +318,7 @@ function finished(row: WorktreeRefreshRow, sync: { synced: RefreshMemberSync[];
*/
export async function refreshWorktree(engine: BrainEngine, sourceId: string, opts: RefreshOptions = {}): Promise<WorktreeRefreshResult> {
if (opts.resume || opts.abandon) return resumeOrAbandon(engine, sourceId, opts);
- const fetchTimeoutMs = await configuredMs(engine, opts.fetchTimeoutMs, 'GBRAIN_REFRESH_FETCH_TIMEOUT_MS', 'sources.refresh_fetch_timeout_ms', 120_000);
+ const fetchTimeoutMs = await configuredMs(engine, opts.fetchTimeoutMs, 'GBRAIN_REFRESH_FETCH_TIMEOUT_MS', 'sources.refresh_fetch_timeout_ms', 120_000, 2 ** 31 - 1);
const waitDrainMs = await configuredMs(engine, opts.waitDrainMs, 'GBRAIN_REFRESH_DRAIN_WAIT_MS', 'sources.refresh_drain_wait_ms', 60_000);
return withGit(async git => {
const plan = await precheck(engine, git, sourceId, fetchTimeoutMs);
diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.ts
index e077190f02..40cfddd51c 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -21,7 +21,7 @@ import { join } from 'node:path';
import type { BrainEngine } from '../src/core/engine.ts';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { operations } from '../src/core/operations.ts';
-import { runSourcesRefresh } from '../src/commands/sources-refresh.ts';
+import { parseRefreshArgs, runSourcesRefresh } from '../src/commands/sources-refresh.ts';
import { claimCoalescedGitEffects, claimPersistenceEffect } from '../src/core/persistence/effect-journal.ts';
import { localHostId } from '../src/core/persistence/identity.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
@@ -62,6 +62,13 @@ const refusedWith = async (promise: Promise<unknown>, code: string) => {
expect(error!.code).toBe(code);
return error!;
};
+test('refresh timer bounds reject overflow and accept the maximum delay', () => each(async f => {
+ await refusedWith(refreshWorktree(f.engine, f.alpha, { fetchTimeoutMs: 2 ** 31 }), 'invalid_params');
+ const drain = parseRefreshArgs([f.alpha, '--wait-drain', String((2 ** 31) / 1000), '--dry-run']).options;
+ expect(drain.waitDrainMs).toBeGreaterThan(2 ** 31 - 1); // the drain wait is a performance.now() deadline, not a timer: still accepted
+ expect((await refreshWorktree(f.engine, f.alpha, drain)).status).toBe('dry_run');
+ expect((await refreshWorktree(f.engine, f.alpha, { fetchTimeoutMs: 2 ** 31 - 1, dryRun: true })).status).toBe('dry_run');
+}));
/** Requeue a committed git effect so the drain must wait for the consumer to process it again. */
async function requeueGitEffect(f: RefreshFixture, delayMs: number): Promise<number> {
const [effect] = await f.engine.executeRaw<{ id: number }>(`SELECT id FROM persistence_effects WHERE worktree_id=$1::uuid AND kind='git' ORDER BY id DESC LIMIT 1`, [f.worktreeId]);