Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): lanes yield to a requeued predecessor; a used-up lease turn stops claims, not joins

garrytanf8b4c757b016
diff --git a/src/core/persistence/group-publish.ts b/src/core/persistence/group-publish.ts
index c5e5557971..23abf68705 100644
--- a/src/core/persistence/group-publish.ts
+++ b/src/core/persistence/group-publish.ts
@@ -231,7 +231,7 @@ export async function publishGroup(engine: BrainEngine, rows: WriteRequest[], pr
}
/** Why a group transaction did not commit, for the lane fallback and step-down. */
-export type GroupFailure = 'busy' | 'predecessor_failed' | 'wounded' | 'order_timeout' | 'lock_timeout' | 'statement_timeout' | 'failed';
+export type GroupFailure = 'busy' | 'predecessor_failed' | 'predecessor_requeued' | 'wounded' | 'order_timeout' | 'lock_timeout' | 'statement_timeout' | 'failed';
function groupFailure(error: unknown): GroupFailure {
if (error instanceof LaneAbort) return error.reason;
const code = (error as { code?: unknown } | null)?.code;
diff --git a/src/core/persistence/sync-lanes.ts b/src/core/persistence/sync-lanes.ts
index 34b878ff45..66015cfdee 100644
--- a/src/core/persistence/sync-lanes.ts
+++ b/src/core/persistence/sync-lanes.ts
@@ -64,7 +64,7 @@ export function laneClaimed(state: LaneState, rows: WriteRequest[]): void { stat
export function laneFinished(state: LaneState, rows: WriteRequest[]): void { state.claimed.delete(groupKey(rows)); state.begun.delete(groupKey(rows)); }
export class LaneAbort extends Error {
- constructor(readonly reason: 'predecessor_failed' | 'wounded' | 'order_timeout') { super(`lane ${reason}`); }
+ constructor(readonly reason: 'predecessor_failed' | 'predecessor_requeued' | 'wounded' | 'order_timeout') { super(`lane ${reason}`); }
}
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
@@ -85,8 +85,8 @@ export async function awaitLaneBegin(state: LaneState, rows: WriteRequest[], max
* Inside the group transaction, after its members are applied and before the
* counters are locked: waits until the previous group committed. Each poll is
* a fresh read of the predecessor's receipt. Throws LaneAbort when the
- * predecessor ended without committing, when the worktree's lease drains for
- * an exclusive writer, or after `maxMs`.
+ * predecessor ended without committing or went back to the queue, when an
+ * exclusive writer waits for the worktree's lease, or after `maxMs`.
*/
export async function awaitLaneTurn(tx: BrainEngine, state: LaneState, rows: WriteRequest[], maxMs = 60_000): Promise<void> {
const after = predecessorOf(rows);
@@ -98,6 +98,8 @@ export async function awaitLaneTurn(tx: BrainEngine, state: LaneState, rows: Wri
[head.principal_kind, head.principal_id, after]);
if (prior?.state === 'committed') return;
if (!prior || ['failed', 'conflict', 'cancelled'].includes(prior.state)) throw new LaneAbort('predecessor_failed');
+ // A released predecessor goes first: this lane gives its slot and claims back, rather than wait on it.
+ if (prior.state === 'queued') throw new LaneAbort('predecessor_requeued');
if (state.coordinationPath && leaseWounded(state.coordinationPath)) throw new LaneAbort('wounded');
if (Date.now() - started >= maxMs) throw new LaneAbort('order_timeout');
await sleep(Math.min(200, 25 * (poll + 1)));
diff --git a/src/core/persistence/worktree-lease.ts b/src/core/persistence/worktree-lease.ts
index 9ccf85dfe1..1ccfec1539 100644
--- a/src/core/persistence/worktree-lease.ts
+++ b/src/core/persistence/worktree-lease.ts
@@ -14,7 +14,8 @@
* waiting to commit roll back and release their claims, and the native lock
* is released as soon as the running lanes finish, so the exclusive writer
* gets it on its next attempt. A lease also drains (without wounding) after
- * `LEASE_TURN_MS` of continuous holding, so writers in other processes,
+ * `LEASE_TURN_MS` of continuous holding: lanes already claimed still join it,
+ * but no new lane is claimed until it ends, so writers in other processes,
* which cannot mark it, still get a turn.
*/
import type { NativeLockHandle } from './native-lock.ts';
@@ -40,13 +41,14 @@ export function leaseWounded(path: string): boolean {
/**
* Joins this process's lease on `path`, or takes the native lock through
- * `acquire` when there is none. Null when the lock is busy elsewhere or the
- * lease is draining; the caller releases its claim and retries later.
+ * `acquire` when there is none. Null when the lock is busy elsewhere or an
+ * exclusive writer waits for the lease; the caller releases its claim and retries later.
*/
export async function acquireShared(path: string, acquire: () => Promise<NativeLockHandle | null>): Promise<NativeLockHandle | null> {
const live = leases.get(path);
if (live) {
- if (live.lock.released || leaseDraining(path)) return null;
+ // A used-up turn only stops new lane claims (sync-lanes.ts laneRoots); a wounded lease also refuses joins.
+ if (live.lock.released || live.wounded) return null;
live.holders++;
return share(path, live);
}