Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(backup): the #6649 size estimate skips entries the runtime cannot lstat (Windows AF_UNIX sockets read as EACCES)

garrytan9f3461ef3fd0
diff --git a/src/core/backup/preflight.ts b/src/core/backup/preflight.ts
index f6fe933515..61efc630c0 100644
--- a/src/core/backup/preflight.ts
+++ b/src/core/backup/preflight.ts
@@ -7,11 +7,12 @@
* Both refusals name the same recovery: the quiesced physical-copy runbook.
* Doctor's `pglite_data_dir` uses the same size estimate to warn early.
*/
-import { lstatSync, readdirSync } from 'node:fs';
+import { lstatSync, readdirSync, type Stats } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import type { Action } from '../agent-output.ts';
import { opError, type OperationError } from '../ops/contract.ts';
import { BACKUP_ARCHIVE_MAX_BYTES } from './archive.ts';
+import { UNREADABLE_CODES } from './runtime-artefacts.ts';
export const PHYSICAL_COPY_RUNBOOK = 'docs/guides/in-agent-setup.md#large-or-non-standard-brains-quiesced-physical-copy';
/** Doctor warns once the data dir's estimated archive size passes this share of the cap. */
@@ -22,12 +23,21 @@ const TAR_BLOCK = 512;
/**
* Bytes `dumpDataDir` would write for `dir`: every regular file's apparent
* size rounded up to a 512-byte tar block plus its header block, a header per
- * directory, and the two closing blocks. Symlinks are not followed.
+ * directory, and the two closing blocks. Symlinks are not followed. An entry
+ * that vanished or that the runtime cannot lstat (a Windows AF_UNIX socket
+ * reads as EACCES) adds nothing: it is not a regular file, and the runtime
+ * artefact sweep decides what happens to it.
*/
export function estimatedTarBytes(dir: string): number {
let total = 2 * TAR_BLOCK;
const walk = (path: string): void => {
- const stat = lstatSync(path);
+ let stat: Stats;
+ try { stat = lstatSync(path); }
+ catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (path !== dir && code && (code === 'ENOENT' || UNREADABLE_CODES.has(code))) return;
+ throw error;
+ }
if (stat.isDirectory()) {
total += TAR_BLOCK;
for (const name of readdirSync(path)) walk(join(path, name));
diff --git a/src/core/backup/runtime-artefacts.ts b/src/core/backup/runtime-artefacts.ts
index c3f536351c..e77d286ae1 100644
--- a/src/core/backup/runtime-artefacts.ts
+++ b/src/core/backup/runtime-artefacts.ts
@@ -40,7 +40,7 @@ export interface RuntimeArtefactSweep {
const GBRAIN_SOCKET_KINDS = ['resolve', 'persistence'] as const;
/** What the runtime reports for a stale Windows AF_UNIX reparse point (EACCES on Bun; the others are its neighbours). */
-const UNREADABLE_CODES = new Set(['EACCES', 'EPERM', 'EINVAL', 'UNKNOWN']);
+export const UNREADABLE_CODES: ReadonlySet<string> = new Set(['EACCES', 'EPERM', 'EINVAL', 'UNKNOWN']);
function entryStat(path: string): fs.Stats | 'missing' | 'unreadable' {
try { return fs.lstatSync(path); }
diff --git a/test/backup-preflight-6649.serial.test.ts b/test/backup-preflight-6649.serial.test.ts
index 32ec870e39..5f6240b766 100644
--- a/test/backup-preflight-6649.serial.test.ts
+++ b/test/backup-preflight-6649.serial.test.ts
@@ -17,7 +17,7 @@
* no disk use), so no test writes gigabytes.
*/
import { afterAll, afterEach, beforeAll, describe, expect, test } from 'bun:test';
-import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, symlinkSync, truncateSync, writeFileSync } from 'node:fs';
+import { chmodSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, rmSync, symlinkSync, truncateSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { privateWrite } from '../src/core/agent-install/state.ts';
@@ -112,6 +112,19 @@ describe('size preflight', () => {
expect(estimatedTarBytes(dir)).toBe(512 * (2 + 2 + 2 + 3));
});
+ test.skipIf(process.platform === 'win32' || process.getuid?.() === 0)('an entry the runtime cannot lstat adds nothing to the estimate instead of failing it', () => {
+ const dir = mkdtempSync(join(temporary, 'unreadable-'));
+ const locked = join(dir, 'locked');
+ mkdirSync(locked);
+ writeFileSync(join(locked, 'entry'), 'x');
+ chmodSync(locked, 0o600);
+ try {
+ expect(estimatedTarBytes(dir)).toBe(512 * (2 + 2));
+ } finally {
+ chmodSync(locked, 0o700);
+ }
+ });
+
test('a data dir over the 8 GiB cap is refused as backup_too_large before the lock, the dump or any output', async () => {
writeFileSync(sparse(), '');
truncateSync(sparse(), BACKUP_ARCHIVE_MAX_BYTES + 1);