Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
sync: the lease-holder-only claim keeps its claim read's shape; the holder is read only when the head row is a sync row (managed-atom retry fence intercepts the claim by its SQL text)
garrytana8a07186bb74
diff --git a/src/core/persistence/journal.ts b/src/core/persistence/journal.tsindex bf1fb26a1f..dfef9e3826 100644--- a/src/core/persistence/journal.ts+++ b/src/core/persistence/journal.ts@@ -632,11 +632,10 @@ export async function hasClaimableWrite(engine: SqlEngine, hostId: string, exclu* own write) ranks a sync row whose source's lease another process holds after every other row and claims it only when that* holder is provably dead (a killed same-host sync whose TTL has not lapsed yet), so it never publishes a live drainer's pages* or breaks up its lane groups. With no live lease nothing changes: a crashed drainer's rows still drain through any consumer.- * The holder rides on the claim's own read, so the claim costs no extra statement. $5 the sync lock id prefix, $6 pid, $7 host.+ * The holder is read only when the head row is a sync row, so a foreground write's claim costs no extra statement.*/-const LEASE_HOLDER = (column: string) => `(SELECT l.${column} FROM gbrain_cycle_locks l WHERE ${SYNC_KIND('r')} AND l.id=$5::text||r.source_id+const LEASE_HELD = `EXISTS (SELECT 1 FROM gbrain_cycle_locks l WHERE ${SYNC_KIND('r')} AND l.id=$5::text||r.source_idAND l.ttl_expires_at>now() AND NOT (l.holder_pid=$6::integer AND l.holder_host IS NOT DISTINCT FROM $7::text))`;-type ClaimCandidate = WriteRequest & { lease_holder_pid?: number | null; lease_holder_host?: string | null };export async function claimNextWrite(engine: BrainEngine, hostId: string, leaseMs = 30_000, excludeRoots: string[] = [], fifoRoots: string[] = [],laneRoots: string[] = []): Promise<(WriteRequest & { passed_sync?: boolean }) | null> {@@ -646,17 +645,20 @@ export async function claimNextWrite(engine: BrainEngine, hostId: string, leaseM// The protocol declaration and the FIFO head read are sent together.const [, [row]] = await pipelined(tx, [() => declarePersistenceProtocol(tx),- () => tx.executeRaw<ClaimCandidate>(`SELECT r.*${holderOnly- ? `, ${LEASE_HOLDER('holder_pid')} AS lease_holder_pid, ${LEASE_HOLDER('holder_host')} AS lease_holder_host` : ''} FROM persistence_requests r+ () => tx.executeRaw<WriteRequest>(`SELECT r.* FROM persistence_requests rLEFT JOIN persistence_worktrees w ON w.id=r.worktree_idWHERE ${claimableWriteSql(`(${first})`, '$4::text[]')}-- On a root whose lane groups run in this process only a foreground write is claimed (beside them).AND NOT (r.worktree_id::text=ANY($4::text[]) AND (${SYNC_KIND('r')} OR NOT (${first})))- ORDER BY ${holderOnly ? `${LEASE_HOLDER('holder_pid')} IS NOT NULL, ` : ''}${CLAIM_ORDER(`(${first})`)} LIMIT 1 FOR UPDATE OF r SKIP LOCKED`,+ ORDER BY ${holderOnly ? `${LEASE_HELD}, ` : ''}${CLAIM_ORDER(`(${first})`)} LIMIT 1 FOR UPDATE OF r SKIP LOCKED`,[hostId, excludeRoots, fifoRoots, laneRoots, ...(holderOnly ? [syncLockId(''), process.pid, hostname()] : [])]),- ]) as [unknown, ClaimCandidate[]];+ ]) as [unknown, WriteRequest[]];if (!row) return null;- if (row.lease_holder_pid != null && classifyHolderLiveness(row.lease_holder_pid, row.lease_holder_host ?? '', 0, { graceMs: 0 }) !== 'dead_eligible') return null;+ if (holderOnly && String(row.intent?.kind ?? '').startsWith('managed_sync_')) {+ const [lease] = await tx.executeRaw<{ holder_pid: number; holder_host: string | null }>(`SELECT holder_pid, holder_host FROM gbrain_cycle_locks+ WHERE id=$1::text AND ttl_expires_at>now() AND NOT (holder_pid=$2::integer AND holder_host IS NOT DISTINCT FROM $3::text)`, [syncLockId(row.source_id), process.pid, hostname()]);+ if (lease && classifyHolderLiveness(lease.holder_pid, lease.holder_host ?? '', 0, { graceMs: 0 }) !== 'dead_eligible') return null;+ }// `passed_sync`: the claimed row went ahead of a queued sync row of its root.// #6278: the claim stamps its own `preparing` phase, so a kill before the first renewal is charged like any other.const token = randomUUID();