Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(persistence): a db_only cache holding an earlier database state is refreshed, an edited cache still refuses (#6986)
garrytanb9cf163ab3f4
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.tsindex 7a4e5f14e5..eaed61749e 100644--- a/src/core/persistence/page-prepare.ts+++ b/src/core/persistence/page-prepare.ts@@ -36,6 +36,7 @@ import { assertPurgeParams } from './purge-params.ts';import { authorizeWrite, slugDisclosable } from './authority.ts';import { findLiveFileClaimants, type FileClaimant, type FileClaimTarget } from './file-claims.ts';import { digest, sha256 } from './digest.ts';+import { replaceableFile } from '../facts/file-body-guard.ts';import { getWorktreeBinding } from './ownership.ts';import type { PreparedContentImport } from './prepared-import.ts';import type { PreparedMutation } from './coordinator.ts';@@ -339,11 +340,13 @@ async function compareFileTarget(engine: BrainEngine, row: Pick<WriteRequest, 's// An inline page's file over the push-scan cap cannot hold its body (at most 24 MiB): drift, never read whole.const matches = bodyUnread(snapshot.page) ? await segmentedFileMatches(engine, row.slug, before, snapshot, importPath, options.activePack, options.between): before.length <= PUSH_SCAN_MAX_FILE_BYTES && await fileMatchesSnapshot(engine, row.slug, (before instanceof FileBytes ? Buffer.from(before.subarray()) : before).toString('utf8'), snapshot, importPath, options.activePack);- // #6986: a declared db_only page or a never-filed derive-phase page (atoms/, ...) is canonical in the database; a- // differing file at its path is a cache or a stray export, never its canonical file, so the write publishes to the- // database only and leaves that file as it is.- if (!matches && publishesDatabaseOnly(root, row.slug, snapshot)) return undefined;- if (!matches) {+ // #6986: a declared db_only page or a never-filed derive-phase page (atoms/, ...) is canonical in the database, so the+ // file at its path is a cache. A cache that holds a state the database already had (the live page or a recent+ // version), left behind by a writer that skipped it, is refreshed by this write under the bytes compared; a cache+ // holding content the database never had is an uncoordinated edit and still refuses below.+ const staleCache = !matches && before instanceof Buffer && publishesDatabaseOnly(root, row.slug, snapshot)+ && await replaceableFile(engine, row.source_id, row.slug, path).then(check => check.ok && check.expectedFileBytes === before.toString('utf8'));+ if (!matches && !staleCache) {const held = await heldFileRefusal(engine, row, root, path, 'drift', options.remote === true);if (held) throw held;// #6394: a page that never recorded its file reconciles against the file at its slug path only when adopted explicitly.diff --git a/test/pathological/d23-extract-atoms-db-only.test.ts b/test/pathological/d23-extract-atoms-db-only.test.tsindex d1024924d3..407f529217 100644--- a/test/pathological/d23-extract-atoms-db-only.test.ts+++ b/test/pathological/d23-extract-atoms-db-only.test.ts@@ -1,16 +1,15 @@/**- * D-23 (GBRA-131, #6986): extract_atoms reads a declared `storage.db_only` page from the database and publishes it- * whether or not a local cache file sits at its path.+ * D-23 (GBRA-131, #6986): extract_atoms reads a declared `storage.db_only` page from the database, and a db_only+ * page's stale cache file no longer wedges coordinated writes to that page.*- * Protects: on a managed brain whose gbrain.yml declares `meetings/` db_only, a meeting page is extracted and its- * atoms publish when (a) no file exists (the database is the only copy) and (b) a stale cache file exists at the- * slug path (db_only directories are written to disk as a gitignored cache that sync never imports, so the- * database copy is the copy of record). The cache file's bytes are left exactly as they were. A page that is NOT- * declared db_only and whose canonical file differs still refuses `file_database_drift`, so the drift guard is not- * loosened for file-backed pages.- * Fails when: the coordinated publication compares the db_only page with its stale cache file and refuses- * "The canonical file contains an uncoordinated local edit" (`source_changed` / `file_database_drift`), which halts- * every db_only meeting page on every run (doctor extract_health at 100% halt).+ * Protects: on a managed brain whose gbrain.yml declares `meetings/` db_only, (a) a meeting page is extracted and+ * its atoms publish whether its cache file is absent or stale, with the cache bytes left as they were; (b) a+ * coordinated write to the meeting whose cache holds an earlier database state (a version the database already+ * had, left behind by a writer that skipped the cache) publishes and refreshes the cache. A cache holding content+ * the database never had still refuses `source_changed` with both copies kept, and an edited published atom+ * file still refuses, so the drift guard is not loosened for uncoordinated edits.+ * Fails when: the coordinated publication treats a stale db_only cache as an uncoordinated local edit and refuses+ * "The canonical file contains an uncoordinated local edit" (`source_changed` / `file_database_drift`) on every write.* Seams: a stub chat model (no paid calls); the real managed coordinator. PGLite always, Postgres when DATABASE_URL* is set.*/@@ -22,6 +21,7 @@ import type { ChatOpts, ChatResult } from '../../src/core/ai/gateway.ts';import { runPhaseExtractAtoms } from '../../src/core/cycle/extract-atoms.ts';import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';import { operations } from '../../src/core/operations.ts';+import { importFromContent } from '../../src/core/import-file.ts';import { managedBrain, pathologicalArms } from './harness.ts';const MEETING = 'meetings/2026-04-03-acme-example';@@ -77,8 +77,8 @@ for (const { backend, databaseUrl } of pathologicalArms()) {}- test('D-23 a coordinated write to a db_only meeting whose cache file is stale publishes to the database and leaves the cache', async () => {- const cacheBytes = `---\ntype: meeting\ntitle: Acme example sync\n---\n\n${prose('older cache')}\n`;+ test('D-23 a coordinated write to a db_only meeting whose cache holds an earlier database state publishes and refreshes the cache', async () => {+ const older = `---\ntype: meeting\ntitle: Acme example sync\n---\n\n${prose('older cache')}\n`;await managedBrain(async ({ engine, root, ctx }) => {const snap = (await engine.readPageSnapshot(MEETING, { sourceId: 'default' }))!;const outcome = await operations.find(o => o.name === 'put_page')!.handler(ctx, { slug: MEETING, expected_revision: snap.revision,@@ -87,12 +87,32 @@ for (const { backend, databaseUrl } of pathologicalArms()) {await disposePersistenceConsumer(engine);expect(outcome).toBe('published');expect((await engine.getPage(MEETING, { sourceId: 'default' }))!.compiled_truth).toContain('edited in the database');- expect(readFileSync(join(root, `${MEETING}.md`), 'utf8')).toBe(cacheBytes);+ expect(readFileSync(join(root, `${MEETING}.md`), 'utf8')).toContain('edited in the database');}, { databaseUrl, setup: async ({ engine, root }) => {writeFileSync(join(root, 'gbrain.yml'), 'storage:\n db_only:\n - meetings/\n');- await engine.putPage(MEETING, { type: 'meeting', title: 'Acme example sync', compiled_truth: prose('current'), timeline: '' }, { sourceId: 'default' });+ await importFromContent(engine, MEETING, older, { sourceId: 'default', noEmbed: true });+ mkdirSync(dirname(join(root, `${MEETING}.md`)), { recursive: true });+ writeFileSync(join(root, `${MEETING}.md`), older);+ await importFromContent(engine, MEETING, `---\ntype: meeting\ntitle: Acme example sync\n---\n\n${prose('current')}\n`, { sourceId: 'default', noEmbed: true });+ } });+ }, 180_000);++ test('D-23 control: a db_only cache holding content the database never had still refuses and both copies stay', async () => {+ const edited = `---\ntype: meeting\ntitle: Acme example sync\n---\n\n${prose('local unpublished edit')}\n`;+ await managedBrain(async ({ engine, root, ctx }) => {+ const snap = (await engine.readPageSnapshot(MEETING, { sourceId: 'default' }))!;+ const outcome = await operations.find(o => o.name === 'put_page')!.handler(ctx, { slug: MEETING, expected_revision: snap.revision,+ content: `---\ntype: meeting\ntitle: Acme example sync\n---\n\n${prose('edited in the database')}\n` })+ .then(() => 'published', (error: { code?: string; detail?: string }) => `${error.code}/${error.detail}`);+ await disposePersistenceConsumer(engine);+ expect(outcome).toMatch(/^source_changed\//);+ expect((await engine.readPageSnapshot(MEETING, { sourceId: 'default' }))!.revision).toBe(snap.revision);+ expect(readFileSync(join(root, `${MEETING}.md`), 'utf8')).toBe(edited);+ }, { databaseUrl, setup: async ({ engine, root }) => {+ writeFileSync(join(root, 'gbrain.yml'), 'storage:\n db_only:\n - meetings/\n');+ await importFromContent(engine, MEETING, `---\ntype: meeting\ntitle: Acme example sync\n---\n\n${prose('current')}\n`, { sourceId: 'default', noEmbed: true });mkdirSync(dirname(join(root, `${MEETING}.md`)), { recursive: true });- writeFileSync(join(root, `${MEETING}.md`), cacheBytes);+ writeFileSync(join(root, `${MEETING}.md`), edited);} });}, 180_000);