Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(backup): forced cold-start probe through protectNewBackupPath via a PowerShell runner seam

garrytand2374cc73e83
diff --git a/src/core/backup/private-path.ts b/src/core/backup/private-path.ts
index b2d56748a0..dc96d16550 100644
--- a/src/core/backup/private-path.ts
+++ b/src/core/backup/private-path.ts
@@ -42,6 +42,27 @@ foreach ($rule in $rules) {
[Console]::Write('private')
`;
+type ProtectRunner = (path: string, kind: 'directory' | 'file') => Promise<string>;
+
+const runPowerShellProtect: ProtectRunner = (path, kind) => new Promise<string>((resolve, reject) => {
+ let inputFailed = false;
+ const child = execFile(join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
+ ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(protect, 'utf16le').toString('base64')], {
+ env: { ...process.env, GBRAIN_BACKUP_PRIVATE_PATH: path, GBRAIN_BACKUP_PRIVATE_KIND: kind },
+ encoding: 'utf8', timeout: 15_000, maxBuffer: 64 * 1024, windowsHide: true,
+ }, (error, stdout) => inputFailed ? reject(new Error('Private path input failed')) : error ? reject(error) : resolve(stdout));
+ const inputFailure = () => { inputFailed = true; child.kill(); };
+ if (!child.stdin) { inputFailure(); return; }
+ child.stdin.once('error', inputFailure);
+ try { child.stdin.end(); } catch { inputFailure(); }
+});
+
+let protectRunner: ProtectRunner = runPowerShellProtect;
+/** Test seam: replace the PowerShell invocation (undefined restores it). */
+export function __setPrivatePathRunnerForTests(runner?: ProtectRunner): void {
+ protectRunner = runner ?? runPowerShellProtect;
+}
+
/**
* PowerShell's first start on a cold Windows machine can take longer than the
* 15 s bound (measured 3.3-27.7 s on fresh CI runners; later starts take
@@ -64,18 +85,7 @@ export async function protectNewBackupPath(path: string, kind: 'directory' | 'fi
assertNoSymlinks(path);
const before = lstatSync(path, { bigint: true });
if (!before.ino || (kind === 'directory' ? !before.isDirectory() || readdirSync(path).length !== 0 : !before.isFile() || before.size !== 0n || before.nlink !== 1n)) throw new Error('Expected a new empty path with stable identity');
- const result = await withColdStartRetry(() => new Promise<string>((resolve, reject) => {
- let inputFailed = false;
- const child = execFile(join(process.env.SystemRoot ?? 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
- ['-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(protect, 'utf16le').toString('base64')], {
- env: { ...process.env, GBRAIN_BACKUP_PRIVATE_PATH: path, GBRAIN_BACKUP_PRIVATE_KIND: kind },
- encoding: 'utf8', timeout: 15_000, maxBuffer: 64 * 1024, windowsHide: true,
- }, (error, stdout) => inputFailed ? reject(new Error('Private path input failed')) : error ? reject(error) : resolve(stdout));
- const inputFailure = () => { inputFailed = true; child.kill(); };
- if (!child.stdin) { inputFailure(); return; }
- child.stdin.once('error', inputFailure);
- try { child.stdin.end(); } catch { inputFailure(); }
- }));
+ const result = await withColdStartRetry(() => protectRunner(path, kind));
assertNoSymlinks(path);
const after = lstatSync(path, { bigint: true });
if (result !== 'private' || before.dev !== after.dev || before.ino !== after.ino
diff --git a/test/backup-private-path-retry.test.ts b/test/backup-private-path-retry.test.ts
index 945ce7b963..16d920b512 100644
--- a/test/backup-private-path-retry.test.ts
+++ b/test/backup-private-path-retry.test.ts
@@ -1,5 +1,8 @@
-import { describe, expect, test } from 'bun:test';
-import { withColdStartRetry } from '../src/core/backup/private-path.ts';
+import { afterEach, describe, expect, test } from 'bun:test';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { __setPrivatePathRunnerForTests, protectNewBackupPath, withColdStartRetry } from '../src/core/backup/private-path.ts';
// A PowerShell cold start can outrun the 15 s bound on a fresh Windows machine
// (protectNewBackupPath). These cases pin the retry policy on every platform;
@@ -34,3 +37,45 @@ describe('withColdStartRetry', () => {
expect(calls).toBe(1);
});
});
+
+// Forced probe through protectNewBackupPath itself: the platform reads as
+// win32 and the PowerShell runner is replaced, so the first launch is killed
+// by the 15 s bound (as a cold start is) and the second answers.
+describe('protectNewBackupPath on a cold Windows start', () => {
+ const realPlatform = process.platform;
+ const dirs: string[] = [];
+ const asWindows = () => Object.defineProperty(process, 'platform', { value: 'win32', configurable: true });
+ afterEach(() => {
+ Object.defineProperty(process, 'platform', { value: realPlatform, configurable: true });
+ __setPrivatePathRunnerForTests();
+ while (dirs.length) rmSync(dirs.pop()!, { recursive: true, force: true });
+ });
+ const emptyDir = () => { const d = mkdtempSync(join(tmpdir(), 'gbrain-private-path-')); dirs.push(d); return d; };
+
+ test('a first launch killed by the timeout is retried and the path is protected', async () => {
+ const dir = emptyDir();
+ let launches = 0;
+ __setPrivatePathRunnerForTests(async () => { launches++; if (launches === 1) throw timedOut(); return 'private'; });
+ asWindows();
+ await protectNewBackupPath(dir, 'directory');
+ expect(launches).toBe(2);
+ });
+
+ test('two timeouts still refuse with private_backup_path_unavailable', async () => {
+ const dir = emptyDir();
+ let launches = 0;
+ __setPrivatePathRunnerForTests(async () => { launches++; throw timedOut(); });
+ asWindows();
+ await expect(protectNewBackupPath(dir, 'directory')).rejects.toMatchObject({ code: 'private_backup_path_unavailable' });
+ expect(launches).toBe(2);
+ });
+
+ test('a failed ACL check is not retried', async () => {
+ const dir = emptyDir();
+ let launches = 0;
+ __setPrivatePathRunnerForTests(async () => { launches++; throw new Error('Access rule mismatch'); });
+ asWindows();
+ await expect(protectNewBackupPath(dir, 'directory')).rejects.toMatchObject({ code: 'private_backup_path_unavailable' });
+ expect(launches).toBe(1);
+ });
+});