Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Keep a repaired page file when its database commit landed but the reply was lost
garrytanead77d31d22a
diff --git a/src/core/trust/page-handlers.ts b/src/core/trust/page-handlers.tsindex fbd1912d0c..b63f213fa7 100644--- a/src/core/trust/page-handlers.ts+++ b/src/core/trust/page-handlers.ts@@ -384,25 +384,28 @@ async function publishOwnerPage(engine: BrainEngine, input: PageActionInput, intawait closeProposal(tx, intent.proposal, input.by);}))));} catch (error) {- if (rendered?.path && file?.ok && file.target) await restoreOwnerRepairFile(engine, input.slug, file.target, rendered);+ if (rendered?.path && file?.ok && file.target) await restoreOwnerRepairFile(engine, input, file.target, rendered);throw error;}}/*** #7960: puts back the bytes an owner repair's file held before `rendered` replaced them (removes a file it created), only- * while the file still holds what the repair wrote, and commits that restore when the repair's write was committed.- * Best-effort: the caller rethrows the database failure either way.+ * while the file still holds what the repair wrote and the page row still has the revision the repair started from (a+ * transaction whose COMMIT landed but whose acknowledgment was lost keeps its file), and commits that restore when the+ * repair's write was committed. Best-effort: the caller rethrows the database failure either way.*/-async function restoreOwnerRepairFile(engine: BrainEngine, slug: string, target: OwnerRepairTarget, rendered: WriteThroughResult): Promise<void> {+async function restoreOwnerRepairFile(engine: BrainEngine, input: PageActionInput, target: OwnerRepairTarget, rendered: WriteThroughResult): Promise<void> {const { existsSync, readFileSync, rmSync } = await import('node:fs');const { atomicWriteFileSync } = await import('../atomic-write.ts');const { commitWriteThroughFile } = await import('../brain-repo-durability.ts');const { withSourceFilesystemLock } = await import('../minions/source-filesystem.ts');+ const { slug } = input;const path = rendered.path!;try {await withSourceFilesystemLock(engine, target.writeRoot, async () => {if ((existsSync(path) ? readFileSync(path, 'utf-8') : null) !== rendered.content) return;+ if ((await engine.readPageSnapshot(slug, { sourceId: input.sourceId }))?.revision !== input.expectedRevision) return;if (target.expectedFileBytes === null) rmSync(path, { force: true });else atomicWriteFileSync(path, target.expectedFileBytes);if (rendered.committed) commitWriteThroughFile(target.writeRoot, path, slug, 'restore write-through');diff --git a/test/provenance-drift-7891.test.ts b/test/provenance-drift-7891.test.tsindex 0b55e09816..816109262b 100644--- a/test/provenance-drift-7891.test.ts+++ b/test/provenance-drift-7891.test.ts@@ -270,6 +270,37 @@ for (const kind of testBackends()) {expect(git(brainDir, 'rev-list', '--count', `${before.head}..HEAD`).trim()).toBe('2');}, 120_000);+ test.skipIf(!repo)('#7960: a database commit that landed but whose acknowledgment was lost keeps the repaired file and its commit', async () => {+ const slug = 'inbox/commit-ack-lost';+ const rel = `${slug}.md`;+ await seed(slug, STORED);+ await rewrite(slug, {}, 'oauth_client');+ durableGitRepo(brainDir, [rel]);+ const before = { file: fs.readFileSync(fileOf(slug), 'utf8'), head: git(brainDir, 'rev-parse', 'HEAD').trim(), revision: (await page(slug)).revision };+ const dry = await run(false);+ expect(dry.affected).toBe(1);+ const outer = engine;+ const transaction = Object.getPrototypeOf(outer).transaction as BrainEngine['transaction'];+ let repaired = null as string | null;+ outer.transaction = async function (this: BrainEngine, fn: (tx: BrainEngine) => Promise<unknown>, opts?: Parameters<BrainEngine['transaction']>[1]) {+ const result = await transaction.call(this, fn, opts);+ if (this === outer && repaired === null && fs.readFileSync(fileOf(slug), 'utf8') !== before.file) {+ repaired = fs.readFileSync(fileOf(slug), 'utf8');+ throw new Error('forced lost COMMIT acknowledgment');+ }+ return result;+ } as BrainEngine['transaction'];+ try {+ await run(true, { expect: dry.preview_hash }).catch(() => undefined);+ } finally { delete (outer as { transaction?: unknown }).transaction; }+ expect(repaired).not.toBeNull();+ expect(repaired).not.toBe(before.file);+ expect({ file: fs.readFileSync(fileOf(slug), 'utf8'), headFile: git(brainDir, 'show', `HEAD:${rel}`), status: git(brainDir, 'status', '--porcelain').trim() })+ .toEqual({ file: repaired!, headFile: repaired!, status: '' });+ expect((await page(slug)).revision).not.toBe(before.revision);+ expect(git(brainDir, 'rev-list', '--count', `${before.head}..HEAD`).trim()).toBe('1');+ }, 120_000);+test.skipIf(!repo)('#7098: a page of a source marked mirror-readonly is listed needs_review, not repaired in the database alone', async () => {const slug = 'inbox/read-only-mirror';await seed(slug, STORED);