Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

sync: the lease-holder-only claim ignores a provably dead same-host holder (crash robot wedge: a killed drainer's unexpired lease)

garrytana5da8e863dd8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5362623fe2..8539f54870 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -55,7 +55,7 @@ identifiers and attribution are available in the pre-removal Git revision
- **`sync status`** (`sync-status.ts`, `commands/sync/operator.ts`): `state`, `reason` (`SYNC_STATUS_REASONS`), `oldest_blocker`, `recovery`, `checkpoint_blockers_read`, `cursors_read`, `terminal_failures_read`, `--detail`; every existing field keeps its type. One text headline per state; the drain summary says `resumed run <id> and converged` when a drain finishes a run an earlier invocation started.
- **`public_category`** (`public-category.ts`): a total mapping from `classifyTerminal`'s category and the fault code; `classifyRecordedFault` reads holds, failure rows and run stops as the failed receipt they stand for.
- AGENTS.md and the sync-unblock runbook: the operator loop checks `*_read` and never reads a `not_read` field as "none".
-- **One publisher per drained source** (`journal.ts` `claimNextWrite`, approved by Garry on 2026-10-16): while a source's drain lease is live, a non-resident consumer skips that source's managed-sync requests held by another process. Resident consumers (`serve`, `sync`, `jobs`, `autopilot`, `mcp`) and the lease holder claim as before. Test: `test/claim-lease-holder-only-6712.test.ts`.
+- **One publisher per drained source** (`journal.ts` `claimNextWrite`, approved by Garry on 2026-10-16): while a source's drain lease is live, a non-resident consumer skips that source's managed-sync requests held by another process. Resident consumers (`serve`, `sync`, `jobs`, `autopilot`, `mcp`) and the lease holder claim as before. A lease whose same-host holder is provably dead (a killed sync whose TTL has not lapsed) does not count, so a crash leaves nothing waiting on the TTL. Test: `test/claim-lease-holder-only-6712.test.ts`.
#### Hold exits and escalation
diff --git a/src/core/persistence/journal.ts b/src/core/persistence/journal.ts
index f302ca118e..44f9a1cfd9 100644
--- a/src/core/persistence/journal.ts
+++ b/src/core/persistence/journal.ts
@@ -20,7 +20,7 @@ import { intentCarriesContent, intentCarriesPurgedContent, PURGE_PRESENCE_COLUMN
import { hostname } from 'node:os';
import { claimOwner, claimOwnerKind, claimPhaseStamp, startClaimPhase } from './claim-phase.ts';
import { consumerIdentity, RESIDENT_CONSUMER_KINDS } from './consumer-heartbeat.ts';
-import { syncLockId } from '../db-lock.ts';
+import { classifyHolderLiveness, syncLockId } from '../db-lock.ts';
import { publicFailureDetail } from './publication-failure.ts';
import { catalogueError } from '../error-catalogue.ts';
import { ACTIVE_REFRESH_STATES_SQL, refreshFenceClear } from './worktree-refresh-schema.ts';
@@ -629,17 +629,21 @@ export async function hasClaimableWrite(engine: SqlEngine, hostId: string, exclu
/**
* #6712 PR 2 (Garry, 2026-10-16, via GBRA-40): while a source's drain lease is live, only the lease holder publishes that
* source's managed-sync requests. A non-resident consumer (a short-lived command such as `put` or `remember`, waiting on its
- * own write) skips a sync row whose source's lease another process holds, so it never publishes a 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.
- * $5 pid, $6 host, $7 the sync lock id prefix.
+ * own write) skips the sync rows of every source whose lease another live process holds, so it never publishes a drainer's
+ * pages or breaks up its lane groups. A lease whose same-host holder is provably dead (a killed sync whose TTL has not
+ * lapsed yet) does not count, and with no live lease nothing changes: a crashed drainer's rows still drain through any consumer.
*/
-const LEASE_HOLDER_ONLY = `NOT (${SYNC_KIND('r')} AND EXISTS (SELECT 1 FROM gbrain_cycle_locks l WHERE l.id=$7::text||r.source_id
- AND l.ttl_expires_at>now() AND NOT (l.holder_pid=$5::integer AND l.holder_host IS NOT DISTINCT FROM $6::text)))`;
+async function leasedSourcesHeldElsewhere(engine: BrainEngine): Promise<string[]> {
+ const prefix = syncLockId('');
+ const rows = await engine.executeRaw<{ id: string; holder_pid: number; holder_host: string | null }>(`SELECT id, holder_pid, holder_host FROM gbrain_cycle_locks
+ WHERE starts_with(id, $1::text) AND ttl_expires_at>now() AND NOT (holder_pid=$2::integer AND holder_host IS NOT DISTINCT FROM $3::text)`, [prefix, process.pid, hostname()]);
+ return rows.filter(row => classifyHolderLiveness(row.holder_pid, row.holder_host ?? '', 0, { graceMs: 0 }) !== 'dead_eligible').map(row => row.id.slice(prefix.length));
+}
export async function claimNextWrite(engine: BrainEngine, hostId: string, leaseMs = 30_000, excludeRoots: string[] = [], fifoRoots: string[] = [],
laneRoots: string[] = []): Promise<(WriteRequest & { passed_sync?: boolean }) | null> {
const first = `(${foregroundPrioritySql()}) AND NOT (COALESCE(r.worktree_id::text,'db:'||r.source_incarnation::text)=ANY($3::text[]))`;
- const holderOnly = !RESIDENT_CONSUMER_KINDS.includes(claimOwnerKind());
+ const leased = RESIDENT_CONSUMER_KINDS.includes(claimOwnerKind()) ? [] : await leasedSourcesHeldElsewhere(engine);
return engine.transactionDirect(async tx => {
// The protocol declaration and the FIFO head read are sent together.
const [, [row]] = await pipelined(tx, [
@@ -648,9 +652,9 @@ export async function claimNextWrite(engine: BrainEngine, hostId: string, leaseM
LEFT JOIN persistence_worktrees w ON w.id=r.worktree_id
WHERE ${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})))${holderOnly ? `
- AND ${LEASE_HOLDER_ONLY}` : ''}
- ORDER BY ${CLAIM_ORDER(`(${first})`)} LIMIT 1 FOR UPDATE OF r SKIP LOCKED`, [hostId, excludeRoots, fifoRoots, laneRoots, ...(holderOnly ? [process.pid, hostname(), syncLockId('')] : [])]),
+ AND NOT (r.worktree_id::text=ANY($4::text[]) AND (${SYNC_KIND('r')} OR NOT (${first})))${leased.length ? `
+ AND NOT (${SYNC_KIND('r')} AND r.source_id=ANY($5::text[]))` : ''}
+ ORDER BY ${CLAIM_ORDER(`(${first})`)} LIMIT 1 FOR UPDATE OF r SKIP LOCKED`, [hostId, excludeRoots, fifoRoots, laneRoots, ...(leased.length ? [leased] : [])]),
]) as [unknown, WriteRequest[]];
if (!row) return null;
// `passed_sync`: the claimed row went ahead of a queued sync row of its root.
diff --git a/test/claim-lease-holder-only-6712.test.ts b/test/claim-lease-holder-only-6712.test.ts
index 36cc40661d..f81b736c92 100644
--- a/test/claim-lease-holder-only-6712.test.ts
+++ b/test/claim-lease-holder-only-6712.test.ts
@@ -67,10 +67,13 @@ async function claimed(engine: BrainEngine): Promise<WriteRequest | null> {
return row;
}
-test('a short-lived consumer never claims a sync row while another process holds the source\'s drain lease; the holder and resident consumers still do', async () => {
+test('a short-lived consumer never claims a sync row while another live process holds the source\'s drain lease; the holder, resident consumers and a dead holder\'s rows still drain', async () => {
for (const engine of engines) {
const id = await syncRow(engine);
- const otherPid = process.pid + 100_000;
+ // A live process of this host holds the lease; a killed one's lease (TTL not lapsed) does not count.
+ const holder = Bun.spawn(['sleep', '120'], { stdin: 'ignore', stdout: 'ignore', stderr: 'ignore' });
+ const otherPid = holder.pid;
+ const dead = Bun.spawn(['true']); await dead.exited;
try {
as('put');
expect((await claimed(engine))?.id).toBe(id);
@@ -84,12 +87,15 @@ test('a short-lived consumer never claims a sync row while another process holds
expect((await claimed(engine))?.id).toBe(id);
await lease(engine, otherPid, '-1 second');
expect((await claimed(engine))?.id).toBe(id);
+ await lease(engine, dead.pid, '2 minutes');
+ expect((await claimed(engine))?.id).toBe(id);
// A foreground write of the same source is claimed beside a live lease.
await lease(engine, otherPid, '2 minutes');
await engine.executeRaw('DELETE FROM persistence_requests WHERE id=$1::uuid', [id]);
const own = await admitWrite(engine, await admission(engine, 'notes/foreground'));
expect((await claimed(engine))?.id).toBe(own.id);
} finally {
+ holder.kill('SIGKILL'); await holder.exited;
setClaimOwnerForTest(undefined);
await engine.executeRaw('DELETE FROM gbrain_cycle_locks WHERE id=$1', [syncLockId(sourceId)]);
await engine.executeRaw('DELETE FROM persistence_requests WHERE source_id=$1', [sourceId]);