Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(refresh): an in-flight publication no longer refuses a worktree refresh as recovery_required

garrytanee5505f2caef
diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.ts
index 96541125c4..4c7ead167d 100644
--- a/src/core/persistence/worktree-refresh.ts
+++ b/src/core/persistence/worktree-refresh.ts
@@ -220,9 +220,15 @@ function dirtyRefusal(sourceId: string, overlap: string[], refreshId?: string):
return refusal('refresh_dirty', `Uncommitted or untracked paths overlap the incoming upstream changes: ${named}. Nothing changed.`,
`Commit or discard those paths (check gbrain sources writer status ${sourceId} --json for pending git effects first), then retry gbrain sources refresh ${sourceId}.`, refreshId);
}
+/**
+ * Recovery no live execution will finish. A recovery record under an unexpired
+ * running claim is an in-flight publication; the drain step waits for it.
+ */
async function worktreeRecoveryPending(engine: BrainEngine, worktreeId: string): Promise<boolean> {
- const [row] = await engine.executeRaw<{ pending: boolean }>(`SELECT EXISTS (SELECT 1 FROM persistence_requests WHERE worktree_id=$1::uuid AND recovery IS NOT NULL)
- OR EXISTS (SELECT 1 FROM persistence_effects WHERE worktree_id=$1::uuid AND recovery IS NOT NULL)
+ const [row] = await engine.executeRaw<{ pending: boolean }>(`SELECT EXISTS (SELECT 1 FROM persistence_requests WHERE worktree_id=$1::uuid AND recovery IS NOT NULL
+ AND NOT (state='running' AND claim_expires_at > now()))
+ OR EXISTS (SELECT 1 FROM persistence_effects WHERE worktree_id=$1::uuid AND recovery IS NOT NULL
+ AND NOT (state='running' AND claim_expires_at > now()))
OR EXISTS (SELECT 1 FROM persistence_topology_changes WHERE recovery IS NOT NULL AND recovery->>'worktreeId'=$1::text) AS pending`, [worktreeId]);
return row?.pending === true;
}
diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.ts
index 59b44c4c3d..e077190f02 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -272,6 +272,23 @@ test('13. drain starvation: a writer every 50 ms is refused during draining and
expect(existsSync(join(f.root, 'alpha/two.md'))).toBe(true);
}), 180_000);
+test('13b. a publication in flight (recovery under a live claim) is drained, not refused; one no live claim holds is recovery_required', () => each(async f => {
+ f.push('alpha/two.md', page('Alpha two', 'Upstream.'));
+ await f.put(f.alpha, 'notes/seed', page('Seed', 'Has a worktree request.'));
+ const [request] = await f.engine.executeRaw<{ id: string }>(
+ 'SELECT id::text FROM persistence_requests WHERE source_id=$1 AND worktree_id IS NOT NULL ORDER BY sequence DESC LIMIT 1', [f.alpha]);
+ const publishing = (claim: string) => f.engine.executeRaw(
+ `UPDATE persistence_requests SET state='running', recovery='{"version":1}'::jsonb, claim_expires_at=now()+$2::interval WHERE id=$1::uuid`, [request.id, claim]);
+ try {
+ await publishing('10 minutes');
+ await refusedWith(refreshWorktree(f.engine, f.alpha, { waitDrainMs: 300 }), 'refresh_drain_timeout');
+ await publishing('-1 minute');
+ await refusedWith(refreshWorktree(f.engine, f.alpha, { waitDrainMs: 300 }), 'refresh_recovery_required');
+ } finally {
+ await f.engine.executeRaw(`UPDATE persistence_requests SET state='committed', recovery=NULL, claim_expires_at=NULL WHERE id=$1::uuid`, [request.id]);
+ }
+}), 120_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');