Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): a test run without the preloads never writes the real sync failure ledger (#7144)

garrytan4131233e9037
diff --git a/src/core/sync-failure-ledger.ts b/src/core/sync-failure-ledger.ts
index 46e44f6942..3e7bc534e9 100644
--- a/src/core/sync-failure-ledger.ts
+++ b/src/core/sync-failure-ledger.ts
@@ -15,7 +15,7 @@
// forever — without ever silently dropping a FRESH failure, and
// never auto-skipping an infra sentinel like `<head>`.
//
-// This module is a LEAF (imports only fs/path/crypto/config and the import-free
+// This module is a LEAF (imports only fs/os/path/crypto/config and the import-free
// ai/errors.ts) so it can be
// re-exported from sync.ts without a circular dependency. The state lives
// in `~/.gbrain/sync-failures.jsonl`, one JSON object per line.
@@ -51,7 +51,9 @@ import {
closeSync as _closeSync,
unlinkSync as _unlinkSync,
statSync as _statSync,
+ mkdtempSync as _mkdtempSync,
} from 'fs';
+import { tmpdir as _tmpdir } from 'os';
import { join as _joinPath } from 'path';
import { gbrainPath as _gbrainPath } from './config.ts';
import { createHash as _createHash } from 'crypto';
@@ -405,7 +407,27 @@ export function formatFailedFileList(
* unset-fallback behavior directly).
*/
function _failuresDir(): string {
- return process.env.GBRAIN_SYNC_FAILURES_DIR || _gbrainPath();
+ if (process.env.GBRAIN_SYNC_FAILURES_DIR) return process.env.GBRAIN_SYNC_FAILURES_DIR;
+ if (process.env.NODE_ENV === 'test' && !process.env.GBRAIN_HOME) return _testScratchDir();
+ return _gbrainPath();
+}
+
+let _testScratch: string | null = null;
+
+/**
+ * #7144: `bun test` loads `bunfig.toml` (and so every isolation preload) only
+ * from the directory it starts in, so a run started in `test/` or outside the
+ * repo set neither GBRAIN_SYNC_FAILURES_DIR nor GBRAIN_HOME, and broken-fixture
+ * sync tests appended their rows (`srcE / notes/bad.md SLUG_MISMATCH`) to the
+ * operator's real ledger. Bun sets NODE_ENV=test for every test run, so with
+ * neither override a test process gets its own scratch ledger instead.
+ */
+function _testScratchDir(): string {
+ if (!_testScratch) {
+ _testScratch = _mkdtempSync(_joinPath(_tmpdir(), 'gbrain-test-syncfail-'));
+ process.stderr.write(`[sync-failures] NODE_ENV=test without GBRAIN_SYNC_FAILURES_DIR or GBRAIN_HOME (bun test started outside the repo root?): this run's sync failure ledger is ${_testScratch}, not the real one.\n`);
+ }
+ return _testScratch;
}
export function syncFailuresPath(): string {
diff --git a/test/sync-failures-ledger-isolation-7144.test.ts b/test/sync-failures-ledger-isolation-7144.test.ts
new file mode 100644
index 0000000000..c880f6cca3
--- /dev/null
+++ b/test/sync-failures-ledger-isolation-7144.test.ts
@@ -0,0 +1,54 @@
+/**
+ * #7144: a test run must never write the operator's real sync failure ledger.
+ * `bun test` loads bunfig.toml, and with it sync-failures-preload and
+ * gbrain-home-preload, only from the directory it starts in: a run started in
+ * `test/` or outside the repo has neither GBRAIN_SYNC_FAILURES_DIR nor
+ * GBRAIN_HOME, and the broken-fixture sync tests appended their rows to
+ * ~/.gbrain/sync-failures.jsonl. Bun sets NODE_ENV=test for every test run, so
+ * the ledger falls back to a per-process scratch directory there. Outside a test
+ * run (NODE_ENV unset) the ledger stays in the gbrain home.
+ */
+import { describe, expect, test } from 'bun:test';
+import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+
+const LEDGER = fileURLToPath(new URL('../src/core/sync-failure-ledger.ts', import.meta.url));
+const script = `const l = await import(${JSON.stringify(LEDGER)}); l.recordFailures('srcE', [{ path: 'notes/bad.md', error: 'SLUG_MISMATCH: frontmatter slug wrong-slug' }], 'c1'); console.log(l.syncFailuresPath());`;
+
+async function record(home: string, nodeEnv: string | undefined): Promise<{ path: string; stderr: string }> {
+ const env: Record<string, string> = { PATH: process.env.PATH ?? '', HOME: home };
+ if (nodeEnv) env.NODE_ENV = nodeEnv;
+ const proc = Bun.spawn(['bun', '--no-env-file', '-e', script], { cwd: home, env, stdout: 'pipe', stderr: 'pipe' });
+ const [stdout, stderr, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
+ expect(code).toBe(0);
+ return { path: stdout.trim(), stderr };
+}
+
+describe('sync failure ledger isolation without the test preloads (#7144)', () => {
+ test('a test process with no ledger or home override writes a scratch ledger, never ~/.gbrain', async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-7144-home-'));
+ try {
+ const { path, stderr } = await record(home, 'test');
+ expect(existsSync(join(home, '.gbrain', 'sync-failures.jsonl'))).toBe(false);
+ expect(path.startsWith(join(home, '.gbrain'))).toBe(false);
+ expect(readFileSync(path, 'utf8')).toContain('notes/bad.md');
+ expect(stderr).toContain('not the real one');
+ rmSync(join(path, '..'), { recursive: true, force: true });
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ });
+
+ test('outside a test run the ledger stays in the gbrain home', async () => {
+ const home = mkdtempSync(join(tmpdir(), 'gbrain-7144-home-'));
+ try {
+ const { path } = await record(home, undefined);
+ expect(path).toBe(join(home, '.gbrain', 'sync-failures.jsonl'));
+ expect(readFileSync(path, 'utf8')).toContain('notes/bad.md');
+ } finally {
+ rmSync(home, { recursive: true, force: true });
+ }
+ });
+});