Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

persistence: the consumer's idle probe honors the refresh fence

garrytan7628fb7f2d1e
diff --git a/src/core/persistence/consumer.ts b/src/core/persistence/consumer.ts
index a9e3e56a23..fc990941e7 100644
--- a/src/core/persistence/consumer.ts
+++ b/src/core/persistence/consumer.ts
@@ -15,6 +15,7 @@ import { redactConnectionInfo } from '../audit/redact-connection-info.ts';
import { AsyncLocalStorage } from 'node:async_hooks';
import { monitorEventLoopDelay, type IntervalHistogram } from 'node:perf_hooks';
import { maybeRefreshPlannerStats } from '../planner-stats.ts';
+import { refreshFenceClear } from './worktree-refresh-schema.ts';
type PhaseObservation = { name: string; started_at: string; deadline_exceeded: boolean; attempt: number; first_conn_ms?: number };
/** #5801: the phase a connection checkout belongs to, carried through its async chain. */
@@ -146,7 +147,7 @@ export class PersistenceConsumer {
AND (r.worktree_id IS NULL OR EXISTS (SELECT 1 FROM persistence_worktrees w WHERE w.id=r.worktree_id AND w.owner_host_id=$1::uuid)))
OR EXISTS (SELECT 1 FROM persistence_effects e LEFT JOIN persistence_worktrees w ON w.id=e.worktree_id
WHERE (e.state='queued' OR e.state='running' AND e.claim_expires_at<now()) AND e.next_attempt_at<=now()
- AND (e.worktree_id IS NULL OR w.owner_host_id=$1::uuid) AND e.recovery IS NULL
+ AND (e.worktree_id IS NULL OR w.owner_host_id=$1::uuid) AND (e.worktree_id IS NULL OR ${refreshFenceClear('e')}) AND e.recovery IS NULL
AND NOT EXISTS (SELECT 1 FROM persistence_effects blocked WHERE blocked.worktree_id=e.worktree_id AND blocked.recovery IS NOT NULL)
AND NOT EXISTS (SELECT 1 FROM persistence_requests blocked WHERE blocked.worktree_id=e.worktree_id AND blocked.recovery IS NOT NULL)
AND (e.kind='withdrawal-mirror' OR NOT EXISTS (SELECT 1 FROM persistence_effects mirror
diff --git a/test/persistence-worktree-refresh.test.ts b/test/persistence-worktree-refresh.test.ts
index ef133f3d41..ed007f98eb 100644
--- a/test/persistence-worktree-refresh.test.ts
+++ b/test/persistence-worktree-refresh.test.ts
@@ -5,8 +5,9 @@
* Protects: a managed checkout shared by several sources is fast-forwarded only
* after every accepted write to it drained, never under a moving HEAD, and the
* refresh converges or refuses with a typed, filled fix.
- * Regression it catches: admission or claiming ignoring the refresh fence (a
- * write publishing onto a checkout whose HEAD is moving), a per-source fence,
+ * Regression it catches: admission, claiming or the consumer's idle probe
+ * ignoring the refresh fence (a write publishing onto a checkout whose HEAD is
+ * moving, or a fenced effect waking the consumer every poll), a per-source fence,
* a refresh that merges dirty or diverged checkouts, a refresh that leaves the
* fence up after a refusal, or topology changes racing a refresh.
* Existing coverage: none; managed sync refused to pull and lane B only
@@ -27,6 +28,7 @@ import { localHostId } from '../src/core/persistence/identity.ts';
import { tryAcquireNativeLock } from '../src/core/persistence/native-lock.ts';
import { getWorktreeBinding } from '../src/core/persistence/ownership.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
+import { PersistenceConsumer } from '../src/core/persistence/consumer.ts';
import { runManagedSourceLifecycle } from '../src/core/persistence/source-lifecycle.ts';
import { runPersistenceAdministration } from '../src/core/persistence/administration.ts';
import { performManagedSync } from '../src/core/persistence/sync-run.ts';
@@ -181,6 +183,9 @@ test('8. a queued git effect drains before fenced, and nothing on the worktree i
await f.engine.executeRaw(`UPDATE persistence_effects SET state='queued',next_attempt_at=now()-interval '1 second' WHERE id=$1`, [effectId]);
expect(await claimPersistenceEffect(f.engine, localHostId())).toBeNull();
expect(await claimCoalescedGitEffects(f.engine, localHostId(), f.worktreeId, 5)).toEqual([]);
+ // The idle probe mirrors the claims: a fenced effect is not work, so the consumer keeps backing off.
+ const probe = new PersistenceConsumer(f.engine, {} as never, async () => { throw new Error('no preparation in this probe'); });
+ expect(await (probe as unknown as { hasWork(): Promise<boolean> }).hasWork()).toBe(false);
// The resident consumer polls too; a claimable effect would leave 'queued' within a few polls.
await new Promise(resolve => setTimeout(resolve, 1200));
expect((await f.engine.executeRaw<{ state: string }>('SELECT state FROM persistence_effects WHERE id=$1', [effectId]))[0].state).toBe('queued');