Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: refresh recovery no longer stalls on an unreadable commit ledger, and a compacted maintenance write still counts as a later write
garrytan0b33c894d269
diff --git a/src/core/persistence/refresh-barrier.ts b/src/core/persistence/refresh-barrier.tsindex 38e778eecd..382112bcde 100644--- a/src/core/persistence/refresh-barrier.ts+++ b/src/core/persistence/refresh-barrier.ts@@ -66,7 +66,7 @@ async function laterWriteRefusal(engine: SqlEngine, row: WriteRequest): Promise<if (APPENDING_OPERATIONS.includes(row.operation)) return null;const [later] = await engine.executeRaw<{ request_id: string }>(`SELECT r.request_id::text AS request_id FROM persistence_requests rWHERE r.source_id=$1 AND r.slug=$2 AND r.sequence>$3 AND r.state='committed' AND NOT (r.operation='submit_job'- AND (${SYNC_KIND('r')} OR (r.intent IS NULL AND r.outcome->>'imported_file'='true'))) ORDER BY r.sequence LIMIT 1`,+ AND (${SYNC_KIND('r')} OR (r.intent IS NULL AND COALESCE(r.outcome->>'imported_file','')='true'))) ORDER BY r.sequence LIMIT 1`,[row.source_id, row.slug, String(row.sequence)]);if (!later) return null;return { state: 'conflict', failure: { code: 'revision_conflict', message: `Request ${row.request_id} waited ${row.deferred_refresh_id ? `behind refresh ${row.deferred_refresh_id}` : 'for an import'}, and request ${later.request_id}, accepted after it, has already changed ${row.slug}, so nothing was applied and the later write was not overwritten. Read the page again and submit the change with a new request_id.` } };diff --git a/src/core/persistence/worktree-refresh.ts b/src/core/persistence/worktree-refresh.tsindex 11af43c2f1..d262d221b6 100644--- a/src/core/persistence/worktree-refresh.ts+++ b/src/core/persistence/worktree-refresh.ts@@ -153,13 +153,18 @@ async function memberOutsideHead(engine: BrainEngine, git: Git, root: string, ro}return false;}-/** GBEV-44: true when gbrain's commit ledger names every commit in from..to (writes admitted before the fence that landed while the refresh drained). */+/**+ * GBEV-44: true when gbrain's commit ledger names every commit in from..to (writes admitted before the fence that landed while the refresh drained).+ * Only the wording of a refusal depends on it, so a ledger that cannot be read proves nothing and the commits read as not gbrain's own.+ */async function ownCommits(git: Git, root: string, from: string, to: string): Promise<boolean> {const location = ledgerLocation(root);const listed = await git(['-C', root, 'rev-list', `${from}..${to}`]);const shas = listed.code === 0 ? listed.stdout.split('\n').map(sha => sha.trim()).filter(Boolean) : [];if (!location || !shas.length) return false;- const ledgered = new Set(readLedger(location.common).flatMap(record => record.e === 'commit' ? [record.sha] : []));+ let records: ReturnType<typeof readLedger>;+ try { records = readLedger(location.common); } catch { return false; }+ const ledgered = new Set(records.flatMap(record => record.e === 'commit' ? [record.sha] : []));return shas.every(sha => ledgered.has(sha));}const OWN_COMMITS = "gbrain's own commits: writes admitted before the fence that landed while the refresh drained";diff --git a/test/refresh-barrier-gst1623.test.ts b/test/refresh-barrier-gst1623.test.tsindex 438ea20761..39191a5476 100644--- a/test/refresh-barrier-gst1623.test.ts+++ b/test/refresh-barrier-gst1623.test.ts@@ -246,6 +246,9 @@ test('#8765 outside voice: a later write counts unless managed sync wrote it (su// A compacted foreground write still counts.await f.engine.executeRaw(`UPDATE persistence_requests SET operation='put_page', outcome=outcome-'imported_file' WHERE id=$1::uuid`, [later.id]);expect(await refusal()).toMatchObject({ state: 'conflict', failure: { code: 'revision_conflict' } });+ // So does a compacted submit_job that imported no file (maintenance work), whose outcome has no imported_file at all.+ await f.engine.executeRaw(`UPDATE persistence_requests SET operation='submit_job' WHERE id=$1::uuid`, [later.id]);+ expect(await refusal()).toMatchObject({ state: 'conflict', failure: { code: 'revision_conflict' } });const before = (await f.engine.readPageSnapshot('two', { sourceId: f.alpha }))?.revision;const captured = await local(f, 'capture', { slug: 'two', content: 'An event.', kind: 'managed_sync_import' }).catch(error => error);expect(captured).toBeInstanceOf(OperationError);diff --git a/test/refresh-write-barrier.test.ts b/test/refresh-write-barrier.test.tsindex c123c9f88c..018dd65454 100644--- a/test/refresh-write-barrier.test.ts+++ b/test/refresh-write-barrier.test.ts@@ -13,7 +13,7 @@import { afterAll, afterEach, beforeAll, beforeEach, expect, spyOn, test } from 'bun:test';import postgres from '#postgres';import { randomUUID } from 'node:crypto';-import { readFileSync, rmSync, writeFileSync } from 'node:fs';+import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { join } from 'node:path';import { run as runUpgradeCommand } from '../src/cli/commands/upgrade.ts';import { mintLegacyToken } from '../src/core/token-mint.ts';@@ -28,7 +28,7 @@ import { pendingReceiptOf, writeErrorExitCode } from '../src/core/persistence/wrimport { PENDING_WRITE_EXIT_CODE } from '../src/core/exit-codes.ts';import { OperationError } from '../src/core/ops/contract.ts';import { refreshWorktree, resumeWorktreeRefreshes } from '../src/core/persistence/worktree-refresh.ts';-import { recordCommit } from '../src/core/git/commit-ledger.ts';+import { ledgerFiles, ledgerLocation, recordCommit } from '../src/core/git/commit-ledger.ts';import { getWriteRequestById } from '../src/core/persistence/journal.ts';import { readWriteWatermark } from '../src/core/persistence/applied-watermark.ts';import { resetWriteSwitches } from '../src/core/persistence/switches.ts';@@ -646,6 +646,23 @@ test('GBEV-44: a refresh killed at fenced after its drain landed a gbrain commitexpect((await settle(f, deferred)).state).toBe('committed');}), 180_000);+test('#8766 review: a refresh killed at fenced after HEAD moved still aborts on resume when the commit ledger cannot be read', () => each(async f => {+ const { landed } = await crashAtFenced(f, () => commitFile(f.root, 'alpha/foreign.md', page('Foreign', 'A commit gbrain did not make.'), 'foreign commit'));+ const { current } = ledgerFiles(ledgerLocation(f.root)!.common);+ rmSync(current, { force: true });+ mkdirSync(current, { recursive: true });+ try {+ const resumed = await refreshWorktree(f.engine, f.alpha, { resume: true });+ expect(resumed.status).toBe('abandoned');+ const [row] = await f.refreshRows();+ expect(row.state).toBe('aborted');+ expect((row.outcome.refusal as { cause: string }).cause).toContain('a commit or checkout gbrain did not make');+ expect(f.head()).toBe(landed);+ } finally {+ rmSync(current, { recursive: true, force: true });+ }+}), 180_000);+test('GBEV-44: the owner scan aborts an orphaned fenced row whose HEAD moved only through gbrain commits before the merge', () => each(async f => {const { deferred, landed } = await crashAtFenced(f, () => {const first = commitFile(f.root, 'alpha/drained-1.md', page('Drained one', 'A write admitted before the fence.'), 'gbrain: persist canonical memory update');