Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(write-through): re-check expectedFileBytes in the pre-rename verify, after staging (#6982)
garrytanaf684d5d78f8
diff --git a/src/core/write-through.ts b/src/core/write-through.tsindex b75d889768..804c7972a0 100644--- a/src/core/write-through.ts+++ b/src/core/write-through.ts@@ -484,7 +484,13 @@ export async function writePageThrough(if (!existsSync(dir)) mkdirSync(dir, { recursive: true });assertSourceFilesystemActive();if (fileChanged()) return { written: false, skipped: 'file_changed' };- atomicWriteFileSync(filePath, md, { verify: () => assertSourceFilesystemActive() });+ const changedWhileStaging = new Error('file_changed');+ try {+ atomicWriteFileSync(filePath, md, { verify: () => { assertSourceFilesystemActive(); if (fileChanged()) throw changedWhileStaging; } });+ } catch (error) {+ if (error === changedWhileStaging) return { written: false, skipped: 'file_changed' };+ throw error;+ }// #2426: on a durability-hardened repo (user ran `gbrain sources harden`),// commit the artifact so it reaches git — pre-fix, write-through contentdiff --git a/test/pathological/w1-write-through-staging-window.serial.test.ts b/test/pathological/w1-write-through-staging-window.serial.test.tsnew file mode 100644index 0000000000..0c9180e707--- /dev/null+++ b/test/pathological/w1-write-through-staging-window.serial.test.ts@@ -0,0 +1,70 @@+/**+ * W-1 (GBRA-131, #6982): writePageThrough's expectedFileBytes check covers the staging window.+ * Protects: a caller that compared a checkout file and passes its bytes as expectedFileBytes never replaces an+ * editor save that lands while the new bytes are being staged (written and fsynced) before the rename. The edit+ * survives byte for byte, the call reports skipped: 'file_changed', and no stage file is left behind.+ * Fails when: the comparison runs only before staging, so the rename installs the render over the late edit.+ * Seams: atomicWriteFileSync is wrapped to save the file between staging and its pre-rename verify callback (a+ * module mock, hence serial). PGLite always, Postgres when DATABASE_URL is set.+ */+import { describe, expect, mock, test } from 'bun:test';+import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import * as atomic from '../../src/core/atomic-write.ts';+import { importFromContent } from '../../src/core/import-file.ts';+import { isolatedSharedSkillsEngine } from '../helpers/shared-skills-engine.ts';+import { withEnv } from '../helpers/with-env.ts';+import { pathologicalArms } from './harness.ts';++const realAtomicWrite = atomic.atomicWriteFileSync;+let duringStaging: ((filePath: string) => void) | null = null;+mock.module('../../src/core/atomic-write.ts', () => ({+ ...atomic,+ atomicWriteFileSync: (filePath: string, content: string | Uint8Array, opts?: Parameters<typeof realAtomicWrite>[2]) =>+ realAtomicWrite(filePath, content, { ...opts, verify: staged => { duringStaging?.(filePath); duringStaging = null; opts?.verify?.(staged); } }),+}));+const { writePageThrough, _resetWriteThroughCacheForTest } = await import('../../src/core/write-through.ts');++const SLUG = 'notes/alice-example';+const LATE_EDIT = 'An editor save that landed while the new render was staged.';++for (const { backend, databaseUrl } of pathologicalArms()) {+ describe(`W-1 write-through staging window (${backend})`, () => {+ for (const late of [true, false]) {+ test(late ? 'W-1 a save during staging survives and the write reports file_changed'+ : 'W-1 control: with no save during staging the compared file is replaced', async () => {+ const home = mkdtempSync(join(tmpdir(), 'gbrain-w1-'));+ try {+ await withEnv({ GBRAIN_HOME: home, DATABASE_URL: undefined, GBRAIN_DATABASE_URL: undefined }, async () => {+ _resetWriteThroughCacheForTest();+ const { engine, close } = await isolatedSharedSkillsEngine(databaseUrl);+ try {+ const root = join(home, 'brain');+ mkdirSync(join(root, 'notes'), { recursive: true });+ await engine.executeRaw("UPDATE sources SET local_path = $1 WHERE id = 'default'", [root]);+ await engine.setConfig('sync.repo_path', root);+ const original = '---\ntitle: Alice example\ntype: note\n---\n\n# Alice example\n\nOriginal synthetic note.\n';+ const file = join(root, `${SLUG}.md`);+ writeFileSync(file, original);+ await importFromContent(engine, SLUG, original, { noEmbed: true, sourceId: 'default', sourcePath: `${SLUG}.md` });+ await importFromContent(engine, SLUG, original.replace('Original synthetic note.', 'Edited in the database.'), { noEmbed: true, sourceId: 'default', sourcePath: `${SLUG}.md` });+ const edited = original.replace('Original synthetic note.', `Original synthetic note.\n\n${LATE_EDIT}`);+ duringStaging = late ? () => writeFileSync(file, edited) : null;+ const result = await writePageThrough(engine, SLUG, { sourceId: 'default', expectedFileBytes: original });+ const after = readFileSync(file, 'utf8');+ if (late) {+ expect({ written: result.written, skipped: result.skipped, kept: after === edited }).toEqual({ written: false, skipped: 'file_changed', kept: true });+ } else {+ expect(result.written).toBe(true);+ expect(after).toContain('Edited in the database.');+ }+ expect(readdirSync(join(root, 'notes')).filter(name => name !== 'alice-example.md')).toEqual([]);+ expect(existsSync(file)).toBe(true);+ } finally { duringStaging = null; await close(); }+ });+ } finally { _resetWriteThroughCacheForTest(); rmSync(home, { recursive: true, force: true }); }+ }, 120_000);+ }+ });+}diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txtindex 5a107e0322..4d227a931f 100644--- a/test/postgres-unit-arms.txt+++ b/test/postgres-unit-arms.txt@@ -293,6 +293,7 @@ test/pathological/d6-phantom-file-unlink.test.tstest/pathological/d7-synthesize-reverse-write.test.tstest/pathological/d8-patterns-reverse-write.test.tstest/pathological/d9-dream-summary-overwrite.test.ts+test/pathological/w1-write-through-staging-window.serial.test.tstest/persistence-background-pause.test.tstest/persistence-bounded-read-session.test.tstest/persistence-capacity-forecast.test.ts