Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): startup refresh recovery scans only interrupted rows; test admission's lazy completion

garrytan33ab99d659c2
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 1292bcac47..2be3e6eef7 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -466,14 +466,16 @@ async function recoverOne(engine: BrainEngine, git: Git, row: WorktreeRefreshRow
}
/**
- * Owner-startup recovery: every active refresh on this host's worktrees whose
- * refreshing process has exited (its refresh lock is free) is converged one
- * step. Member syncs are not run here; `--resume` or the next cycle sync does.
+ * Owner-startup recovery: every interrupted refresh (draining, fenced or
+ * merged) on this host's worktrees whose refreshing process has exited (its
+ * refresh lock is free) is converged one step, so the scan finds nothing on
+ * the next tick. Member syncs are not run here: a `syncing` row is finished by
+ * `--resume`, or by the next cycle sync plus admission's lazy completion.
*/
export async function resumeWorktreeRefreshes(engine: BrainEngine, opts: { hostId?: string } = {}): Promise<number> {
const host = opts.hostId ?? localHostId();
const rows = await engine.executeRaw<WorktreeRefreshRow>(`SELECT f.* FROM persistence_worktree_refreshes f
- JOIN persistence_worktrees w ON w.id=f.worktree_id WHERE w.owner_host_id=$1::uuid AND f.state IN ('draining','fenced','merged','syncing')
+ JOIN persistence_worktrees w ON w.id=f.worktree_id WHERE w.owner_host_id=$1::uuid AND f.state IN ('draining','fenced','merged')
ORDER BY f.created_at LIMIT 16`, [host]);
let moved = 0;
for (const row of rows) {
diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.ts
index f367fe560a..72265cfb0b 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -28,6 +28,7 @@ import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
import { runManagedSourceLifecycle } from '../src/core/persistence/source-lifecycle.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
import { refreshWorktree, resumeWorktreeRefreshes } from '../src/core/persistence/worktree-refresh.ts';
+import { withCoordinatedWrite } from '../src/core/persistence/context.ts';
import { _resetCliExitVerdictForTests, currentExitCode } from '../src/core/cli-force-exit.ts';
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
import { testBackends } from './helpers/test-backends.ts';
@@ -266,3 +267,17 @@ test('13. drain starvation: a writer every 50 ms is refused during draining and
expect(outcomes.filter(code => code === 'worktree_refreshing').length).toBeGreaterThan(3);
expect(existsSync(join(f.root, 'alpha/two.md'))).toBe(true);
}), 180_000);
+
+test('admission completes a syncing refresh whose members all reached the target, and refuses while one lags', () => each(async f => {
+ const target = f.push('alpha/two.md', page('Alpha two', 'Upstream.'));
+ expect((await refreshWorktree(f.engine, f.alpha)).status).toBe('completed');
+ const [row] = await f.refreshRows();
+ // A refresh whose process died in `syncing` after a later cycle sync caught one member up but not the other.
+ await f.engine.executeRaw(`UPDATE persistence_worktree_refreshes SET state='syncing',completed_at=NULL WHERE id=$1::uuid`, [row.id]);
+ const setLastCommit = (sha: string) => f.engine.transaction(tx => withCoordinatedWrite(tx, [f.beta], () => tx.executeRaw('UPDATE sources SET last_commit=$2 WHERE id=$1', [f.beta, sha])));
+ await setLastCommit(git(f.root, 'rev-parse', 'HEAD~1'));
+ await refusedWith(f.put(f.alpha, 'notes/lagging', page('Lagging', 'refused')), 'worktree_refreshing');
+ await setLastCommit(target);
+ expect((await f.put(f.alpha, 'notes/caught-up', page('Caught up', 'admitted'))).state ?? 'committed').toBe('committed');
+ expect((await f.refreshRows()).map(r => r.state)).toEqual(['completed']);
+}), 120_000);