Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): a permission refusal keeps the pass row open so it still reads interrupted, then abandoned (review of #7614)
garrytan31a324f9403b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d7a79a2979..84af11df99 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,7 +14,7 @@ identifiers and attribution are available in the pre-removal Git revision**`gbrain sync status` and doctor stop calling a running managed sync abandoned or killed, a sync that stops for a passing reason ends resumable, and a read-only Postgres serve no longer takes the socket a read-write serve needs.**-A pass whose holder still renews its drain lease now reads running (`pass_unfinished`) even when earlier passes were interrupted, and doctor's `managed_sync_blocked` lists nothing for it. Lock liveness is judged on the database clock, so a reader whose clock runs ahead no longer reads a renewing holder as dead, and a pass that ends between the pass read and the lease read is no longer reported killed. A pass that stops on a lost connection, a statement timeout, write back-pressure, a refresh fence, a permission refusal or a lost lease now ends `resumable` (`stop_reason` `transient` or `lease_lost`) instead of staying open and reading `pass_interrupted`, then `pass_abandoned`. A killed holder's lock row no longer defers the on-commit notifier, so the next sync takes the expired lease over. On Postgres, `gbrain serve --access read-only` (stdio) binds its own read socket for resolve, turn_context and context_pack, so a read-write serve started later for the same source always answers delegated sync requests instead of `unsupported_kind`.+A pass whose holder still renews its drain lease now reads running (`pass_unfinished`) even when earlier passes were interrupted, and doctor's `managed_sync_blocked` lists nothing for it. Lock liveness is judged on the database clock, so a reader whose clock runs ahead no longer reads a renewing holder as dead, and a pass that ends between the pass read and the lease read is no longer reported killed. A pass that stops on a lost connection, a statement timeout, write back-pressure, a refresh fence or a lost lease now ends `resumable` (`stop_reason` `transient` or `lease_lost`) instead of staying open and reading `pass_interrupted`, then `pass_abandoned`. A killed holder's lock row no longer defers the on-commit notifier, so the next sync takes the expired lease over. On Postgres, `gbrain serve --access read-only` (stdio) binds its own read socket for resolve, turn_context and context_pack, so a read-write serve started later for the same source always answers delegated sync requests instead of `unsupported_kind`.### How to use it@@ -26,7 +26,7 @@ gbrain doctor --only managed_sync_blocked### Itemized changes- Sync pass and lease readers (`sync-status`, doctor `managed_sync_blocked`): one observation per decision, database-clock lock liveness, `openPassRowsStill`, one `inspectLocks` query for every open-pass source (#7611, #7614).-- `unrecordedStop`: transient and lease-lost stops end the pass row `resumable`; a lost-lease row is written under its own lease token only (#7614).+- `resumableRowStop`: transient and lease-lost stops end the pass row `resumable`, while a permission refusal keeps the row open so it still reads interrupted, then abandoned; a lost-lease row is written under its own lease token only (#7614).- `readLiveDrainHolder`: the notifier block, the on-commit notifier and `sources status` `writer_heartbeat` read the drain holder only while it renews (#7614).- Lean read-only Postgres serve: a source-keyed read socket; hooks try source, then read, then legacy; delegated sync never targets the read socket (#7612).- `EffectWorkerOptions.lockWaitMs` (test seam, default unchanged) cuts the w5 contention scenario from 37 s to 2.5 s (#7782).diff --git a/src/core/persistence/sync-pass-run.ts b/src/core/persistence/sync-pass-run.tsindex c1353a4ddd..17deeb9594 100644--- a/src/core/persistence/sync-pass-run.ts+++ b/src/core/persistence/sync-pass-run.ts@@ -23,7 +23,7 @@ import type { Cursor, RetryWait } from './sync-run.ts';import { getWriteRequestById } from './journal.ts';import type { WriteRequest } from './model.ts';import { managedSyncWriteDiagnostic } from './sync-write-diagnostic.ts';-import { recordedStop, replaysUnchangedRefusal, unrecordedStop } from './sync-stop-class.ts';+import { recordedStop, replaysUnchangedRefusal, resumableRowStop } from './sync-stop-class.ts';import { adoptLegacyPage, incomingId, syncExcludePatterns, syncGit, type LegacyIdentity, type ManagedSyncContext, type SyncDiscovery, type SyncEntry, type SyncFileRefusal, type SyncSlugCollision } from './sync-discovery.ts';import { assertSyncDispatchActive, type SyncAuthority, type SyncProcessingOptions } from './sync-authority.ts';import { recordPassFenceTrend, type PassStamp, type SyncCursorOptions, type SyncIntent } from './sync-prepare.ts';@@ -1002,7 +1002,7 @@ export async function runSyncPass(engine: BrainEngine, input: SyncPassInput): Pr}// A Git listing that did not finish cleanly classifies nothing (design v3.2 §9): the pass stops blocked, having written nothing for that round.if (error instanceof SyncTreeReadIncomplete) { await save('blocked', 'reconcile_read_incomplete').catch(() => undefined); return result('blocked_by_failures', 'reconcile_read_incomplete'); }- const stop = unrecordedStop(error); if (stop) await save('resumable', stop, false, stop !== 'lease_lost').catch(() => undefined); throw error;+ const stop = resumableRowStop(error); if (stop) await save('resumable', stop, false, stop !== 'lease_lost').catch(() => undefined); throw error;} finally {unregisterPassCost(passId);}diff --git a/src/core/persistence/sync-stop-class.ts b/src/core/persistence/sync-stop-class.tsindex 29f8147810..3d5f9b2a8e 100644--- a/src/core/persistence/sync-stop-class.ts+++ b/src/core/persistence/sync-stop-class.ts@@ -24,6 +24,8 @@ import { isRetryableConnError, isStatementTimeoutError } from '../retry-matcher.* a statement timeout (#6340), or a permission refusal. #7614: the pass ends its row `resumable` with this stop reason, so* only a process that died leaves an open row (status and doctor read an open row with no live holder as killed). A pass* that lost its lease writes that row unfenced, guarded by the row's lease token alone: a pass that pinned since keeps its row.+ * A permission refusal is the exception (`resumableRowStop`): it repeats on every pass until an operator acts, so its row+ * stays open and reads `pass_interrupted`, then `pass_abandoned`, instead of a resumable pass a plain sync would retry forever.*/export function unrecordedStop(error: unknown): 'lease_lost' | 'transient' | null {if (error instanceof LockStolenError) return 'lease_lost';@@ -32,6 +34,11 @@ export function unrecordedStop(error: unknown): 'lease_lost' | 'transient' | nul|| isRetryableConnError(error) || isStatementTimeoutError(error) ? 'transient' : null;}+/** The stop reason a pass row ends `resumable` with: `unrecordedStop` less a permission refusal. */+export function resumableRowStop(error: unknown): 'lease_lost' | 'transient' | null {+ return error instanceof OperationError && error.code === 'permission_denied' ? null : unrecordedStop(error);+}+/*** #6712 (#6565): how the run treats a receipt that did not commit, from the terminal classifier: `transport` (a dropped* connection, a retryable or unavailable refusal) re-freezes in place now; `page` is a fault of the one entry (held whendiff --git a/test/sync-pass-readers-7614.test.ts b/test/sync-pass-readers-7614.test.tsindex 99149d4ff7..05e2ca3883 100644--- a/test/sync-pass-readers-7614.test.ts+++ b/test/sync-pass-readers-7614.test.ts@@ -6,6 +6,7 @@* archived source is not listed; a stop the run records nowhere (a lost connection, a lost lease) ends the row resumable, so* only a dead process leaves it open; and the notifier and watermark readers no longer stand a killed holder in for a live one.*/+import { OperationError } from '../src/core/ops/contract.ts';import { describe, expect, spyOn, test } from 'bun:test';import { execFileSync } from 'node:child_process';import { writeFileSync } from 'node:fs';@@ -203,4 +204,18 @@ for (const backend of testBackends()) {}expect((await sync()).status).toMatch(/^(up_to_date|synced)$/);});++ synced('#7614 review: a permission refusal is not a passing stop; the pass row stays open and reads interrupted, then abandoned', async ({ engine, root, sync, status, blocked }) => {+ const [{ incarnation }] = await engine.executeRaw<{ incarnation: string }>("SELECT incarnation::text AS incarnation FROM sources WHERE id='default'");+ for (const i of [0, 1]) {+ commitFile(root, `denied-${i}.md`);+ installFaultHook(async point => { if (point === 'sync:round_start') throw new OperationError('permission_denied', 'The writer grant for source default was revoked.'); });+ try { await expect(sync()).rejects.toThrow('revoked'); } finally { installFaultHook(undefined); }+ const stopped = (await readPassRow(engine, 'default', incarnation!))!;+ expect(stopped.value.telemetry.outcome).not.toBe('resumable');+ expect(stopped.value.telemetry.stop_reason ?? null).not.toBe('transient');+ }+ expect((await status()).reason).toBe('pass_abandoned');+ expect((await blocked()).map(entry => entry.code)).toEqual(['pass_abandoned']);+ });}