Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sources): the canonical-root overlap scan skips subdirectories that vanish mid-scan

garrytan9612e33b1325
diff --git a/src/core/persistence/physical-root-record.ts b/src/core/persistence/physical-root-record.ts
index bb1db4fbe9..3fa895134f 100644
--- a/src/core/persistence/physical-root-record.ts
+++ b/src/core/persistence/physical-root-record.ts
@@ -87,7 +87,10 @@ export function assertNoPhysicalRootOverlap(root: string): void {
}
if (!existsSync(root)) return;
const visit = (directory: string) => {
- for (const entry of readdirSync(directory, { withFileTypes: true })) {
+ let entries;
+ try { entries = readdirSync(directory, { withFileTypes: true }); }
+ catch (error) { if (directory !== root && (error as NodeJS.ErrnoException).code === 'ENOENT') return; throw error; }
+ for (const entry of entries) {
if (entry.name.startsWith(RESERVATION_PREFIX) && entry.name.endsWith('.json') || directory !== root && entry.name === PHYSICAL_ROOT_MARKER) {
throw physicalRootError('This root contains another reserved canonical root.');
}
diff --git a/test/physical-root-overlap-vanished-dir.serial.test.ts b/test/physical-root-overlap-vanished-dir.serial.test.ts
new file mode 100644
index 0000000000..2b1767e983
--- /dev/null
+++ b/test/physical-root-overlap-vanished-dir.serial.test.ts
@@ -0,0 +1,43 @@
+/**
+ * Protects: the canonical-root overlap scan in `sources add` treats a
+ * subdirectory that disappears mid-scan (Git's background auto-gc packing
+ * loose objects under .git/objects) as holding no reservation, instead of
+ * failing the add with ENOENT.
+ * Fails when: assertNoPhysicalRootOverlap rethrows ENOENT for a vanished
+ * subdirectory, or swallows ENOENT for the root itself.
+ * Seam: mock.module('node:fs') makes readdirSync throw ENOENT for one path.
+ */
+import { afterAll, describe, expect, mock, test } from 'bun:test';
+import * as fs from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const realFs = { ...fs };
+const root = realFs.mkdtempSync(join(tmpdir(), 'gbrain-overlap-'));
+const vanished = join(root, '.git', 'objects', '9c');
+realFs.mkdirSync(vanished, { recursive: true });
+let failPath = vanished;
+const enoent = (path: string) => Object.assign(new Error(`ENOENT: no such file or directory, scandir '${path}'`), { code: 'ENOENT' });
+
+mock.module('node:fs', () => ({
+ ...realFs,
+ readdirSync: (path: fs.PathLike, ...rest: unknown[]) => {
+ if (String(path) === failPath) throw enoent(String(path));
+ return (realFs.readdirSync as (...args: unknown[]) => unknown)(path, ...rest);
+ },
+}));
+const { assertNoPhysicalRootOverlap } = await import('../src/core/persistence/physical-root-record.ts');
+
+afterAll(() => realFs.rmSync(root, { recursive: true, force: true }));
+
+describe('assertNoPhysicalRootOverlap', () => {
+ test('a subdirectory that vanishes during the scan holds no reservation', () => {
+ failPath = vanished;
+ expect(() => assertNoPhysicalRootOverlap(root)).not.toThrow();
+ });
+
+ test('a root that cannot be read still fails', () => {
+ failPath = root;
+ expect(() => assertNoPhysicalRootOverlap(root)).toThrow('ENOENT');
+ });
+});