Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(backup): restore flushes the staged cluster once, not per file while extracting

garrytan47964be397dc
diff --git a/src/core/backup/archive.ts b/src/core/backup/archive.ts
index bfa9cc9748..51a4bdc2f3 100644
--- a/src/core/backup/archive.ts
+++ b/src/core/backup/archive.ts
@@ -164,6 +164,8 @@ export function readBackupArchive(archive: string, destination: string): Archive
* PGLite dumpDataDir emits ustar with /-prefixed cluster-relative paths.
* Parse its narrow format ourselves: never hand an unchecked tar to a runtime.
* Reject links/devices/PAX/duplicates and verify checksums before writing.
+ * Files are not flushed here: the restore flushes every file of its staged tree once before the ready receipt
+ * (a per-file flush here cost 11-22 s per restore on win32-arm64 and was repeated by that tree sync).
*/
export function extractPgliteDump(file: string, destination: string): void {
const fd = openSync(file, 'r');
@@ -222,7 +224,6 @@ export function extractPgliteDump(file: string, destination: string): void {
const block = readExactly(fd, Math.min(size - copied, 1024 * 1024), offset + copied);
writeAll(out, block); copied += block.length;
}
- fsyncSync(out);
} finally { closeSync(out); }
}
offset += Math.ceil(size / 512) * 512;
diff --git a/test/backup-portability-native.serial.test.ts b/test/backup-portability-native.serial.test.ts
index 6858b16198..5a46147455 100644
--- a/test/backup-portability-native.serial.test.ts
+++ b/test/backup-portability-native.serial.test.ts
@@ -3,7 +3,7 @@ import * as fs from 'node:fs';
import { execFileSync } from 'node:child_process';
import * as childProcess from 'node:child_process';
import { tmpdir } from 'node:os';
-import { dirname, join, resolve } from 'node:path';
+import { dirname, join, resolve, sep } from 'node:path';
import { pathToFileURL } from 'node:url';
import { AgentInstallError, checkedManagedPaths, confinedPath, privateWrite, readInstallReceipt, sha256, type AgentInstallReceipt } from '../src/core/agent-install/state.ts';
import { extractPgliteDump, readBackupArchive, writeBackupArchive } from '../src/core/backup/archive.ts';
@@ -1150,3 +1150,36 @@ for (const failureCode of [null, 'EPERM', 'EIO'] as const) test(failureCode
}
await expectOriginal();
}, 120_000);
+
+test('restore flushes every restored cluster file in the tree sync before the ready receipt, none while extracting', async () => {
+ // win32-arm64 (2026-10-22, native-only probe runs): a per-file flush while extracting the cluster cost 11-22 s per
+ // restore, and the tree sync flushed every file again; the tree sync alone keeps the durability contract.
+ const into = join(temporary, 'single-flush');
+ const open = fs.openSync; const fsync = fs.fsyncSync;
+ const handles = new Map<number, { path: string; flags: string }>();
+ const extracted: string[] = [];
+ const treeSynced = new Set<string>();
+ const cluster = join('restored', '.gbrain', 'brain.pglite') + sep;
+ const opened = spyOn(fs, 'openSync').mockImplementation((path, flags, mode) => {
+ const fd = open(path, flags, mode);
+ const at = String(path).indexOf(cluster);
+ if (String(path).startsWith(join(into, '.restore-')) && at >= 0) handles.set(fd, { path: String(path).slice(at + cluster.length), flags: String(flags) });
+ return fd;
+ });
+ const synced = spyOn(fs, 'fsyncSync').mockImplementation(fd => {
+ const handle = handles.get(fd);
+ if (handle?.flags === 'wx') extracted.push(handle.path);
+ if (handle?.flags === 'r+') treeSynced.add(handle.path);
+ fsync(fd);
+ });
+ try { expect((await restorePgliteBackup({ archive, into })).root).toBe(into); }
+ finally { opened.mockRestore(); synced.mockRestore(); }
+ expect(JSON.parse(fs.readFileSync(join(into, 'restore-receipt.json'), 'utf8')).state).toBe('ready');
+ const published = (function walk(dir: string, prefix: string): string[] {
+ return fs.readdirSync(dir, { withFileTypes: true }).flatMap(entry => entry.isDirectory()
+ ? walk(join(dir, entry.name), prefix + entry.name + sep) : [prefix + entry.name]);
+ })(join(into, '.gbrain', 'brain.pglite'), '');
+ expect(published.length).toBeGreaterThan(100);
+ expect(extracted).toEqual([]);
+ expect(published.filter(path => !treeSynced.has(path))).toEqual([]);
+}, 120_000);