Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(self-upgrade): flush the downloaded binary through its write descriptor; drop the durable-flush allowlist (#5595 class)

garrytan07e03dc238a1
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index c77b84feb7..f9e8848f79 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -154,7 +154,7 @@ jobs:
- name: Colon-slug Windows refusals and bound explicit reads (#5032, #5081)
run: bun test --timeout 60000 test/colon-slug-windows-5032.test.ts test/cross-lane-5081-5032.test.ts
- name: Windows durability, skill bundles and local-writer flags (#5595, #5475)
- run: bun test --timeout 60000 test/fs-durable.test.ts test/persistence-windows-flush-5595.test.ts test/bundle-files-windows-5475.test.ts test/init-packaged-skills-native.test.ts test/auth-local-writer-dry-run.test.ts test/scripts/durable-flush-guard.test.ts
+ run: bun test --timeout 60000 test/fs-durable.test.ts test/persistence-windows-flush-5595.test.ts test/bundle-files-windows-5475.test.ts test/init-packaged-skills-native.test.ts test/auth-local-writer-dry-run.test.ts test/scripts/durable-flush-guard.test.ts test/binary-self-update-durable-download.test.ts
dependency-audit:
if: ${{ github.event_name != 'workflow_dispatch' || inputs.native_only != true }}
diff --git a/docs/TESTING.md b/docs/TESTING.md
index cda0ad972c..0ccee82a5e 100644
--- a/docs/TESTING.md
+++ b/docs/TESTING.md
@@ -1079,8 +1079,7 @@ traced, a `Fix:` line and this anchor. Fix: fsync the descriptor you wrote
through before closing it (set its final mode with `fchmodSync(fd)` first), or
call `flushFile(path)` / `flushDirectory(path, { bestEffort? })` from
`src/core/fs-durable.ts`. A file that cannot migrate yet goes in the guard's
-`ALLOWLIST` with a reason (today only `src/core/binary-self-update.ts`, owned
-by the self-upgrade wave); an entry whose file no longer needs it fails as
+`ALLOWLIST` with a reason (empty today); an entry whose file no longer needs it fails as
`durable_flush_stale_allowlist`. Fixtures:
`test/fixtures/guards/check-durable-flush.ts/`; forms are driven in
`test/scripts/durable-flush-guard.test.ts`. The helper and the #5595/#5475
diff --git a/scripts/check-durable-flush.ts b/scripts/check-durable-flush.ts
index 928374fdde..8631c93c00 100644
--- a/scripts/check-durable-flush.ts
+++ b/scripts/check-durable-flush.ts
@@ -27,7 +27,6 @@ const HELPER = 'src/core/fs-durable.ts';
/** Files allowed to keep a read-only-handle flush until they migrate. Each entry names its reason; a stale entry fails. */
const ALLOWLIST: Record<string, string> = {
- 'src/core/binary-self-update.ts': 'AFTER-FW7, GBRA-35 self-upgrade',
};
function tsFiles(dir: string): string[] {
diff --git a/src/core/binary-self-update.ts b/src/core/binary-self-update.ts
index 717c8fcffb..4b4fa72b92 100644
--- a/src/core/binary-self-update.ts
+++ b/src/core/binary-self-update.ts
@@ -172,7 +172,7 @@ async function defaultFetchRelease(): Promise<{ tag: string; assets: ReleaseAsse
}
}
-async function defaultDownload(url: string, destPath: string): Promise<void> {
+export async function defaultDownload(url: string, destPath: string): Promise<void> {
const res = await fetch(url, {
headers: { 'User-Agent': 'gbrain-self-upgrade' },
redirect: 'follow',
@@ -181,10 +181,11 @@ async function defaultDownload(url: string, destPath: string): Promise<void> {
if (!res.ok) throw new Error(`download HTTP ${res.status}`);
const buf = Buffer.from(await res.arrayBuffer());
if (buf.length === 0) throw new Error('downloaded asset is empty');
- writeFileSync(destPath, buf);
- // fsync so a crash between write and rename can't leave a torn file.
- const fd = openSync(destPath, 'r');
+ // fsync the descriptor that wrote the bytes, so a crash between write and
+ // rename can't leave a torn file. A read-only reopen fails on Windows (EPERM).
+ const fd = openSync(destPath, 'w');
try {
+ writeFileSync(fd, buf);
fsyncSync(fd);
} finally {
closeSync(fd);
diff --git a/test/binary-self-update-durable-download.test.ts b/test/binary-self-update-durable-download.test.ts
new file mode 100644
index 0000000000..db5139a8f4
--- /dev/null
+++ b/test/binary-self-update-durable-download.test.ts
@@ -0,0 +1,33 @@
+/**
+ * The self-upgrade download flushes the descriptor it wrote through. A
+ * read-only reopen for fsync fails with EPERM on Windows (#5595 class), which
+ * aborted every binary self-upgrade there. `simulateReadOnlyFsyncEperm`
+ * reproduces the Windows rule on POSIX hosts.
+ */
+import { afterEach, describe, expect, test } from 'bun:test';
+import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { defaultDownload } from '../src/core/binary-self-update.ts';
+import { simulateReadOnlyFsyncEperm } from './helpers/win32-flush-semantics.ts';
+
+const dir = mkdtempSync(join(tmpdir(), 'gbrain-selfupdate-download-'));
+const realFetch = globalThis.fetch;
+afterEach(() => { globalThis.fetch = realFetch; });
+
+describe('binary self-update download durability', () => {
+ test('writes and flushes through a writable descriptor (no read-only fsync)', async () => {
+ const bytes = Buffer.from('gbrain binary bytes\n');
+ globalThis.fetch = (async () => new Response(bytes)) as unknown as typeof fetch;
+ const dest = join(dir, 'gbrain.staged');
+ const flush = simulateReadOnlyFsyncEperm();
+ try {
+ await defaultDownload('https://example.invalid/gbrain', dest);
+ } finally {
+ flush.restore();
+ }
+ expect(flush.readOnlyFsyncs).toBe(0);
+ expect(readFileSync(dest).equals(bytes)).toBe(true);
+ rmSync(dir, { recursive: true, force: true });
+ });
+});
diff --git a/test/scripts/durable-flush-guard.test.ts b/test/scripts/durable-flush-guard.test.ts
index 03b49b7a98..67fe5c880d 100644
--- a/test/scripts/durable-flush-guard.test.ts
+++ b/test/scripts/durable-flush-guard.test.ts
@@ -58,15 +58,10 @@ describe('check-durable-flush.ts', () => {
expect(r.code).toBe(0);
});
- test('a stale allowlist entry fails until it is removed', () => {
- const r = run({ 'src/core/binary-self-update.ts': IMPORTS + "export function f(p: string) { const fd = openSync(p, 'r+'); fsyncSync(fd); closeSync(fd); }\n" });
- expect(r.code).toBe(1);
- expect(r.out).toContain('FAIL [durable_flush_stale_allowlist]: src/core/binary-self-update.ts');
- });
-
- test('the allowlisted self-upgrade flush stays allowed while it still needs migrating', () => {
+ test('the self-upgrade download is no longer allowlisted: a read-only flush there fails', () => {
const r = run({ 'src/core/binary-self-update.ts': IMPORTS + "export function f(p: string) { const fd = openSync(p, 'r'); fsyncSync(fd); closeSync(fd); }\n" });
- expect(r.code).toBe(0);
+ expect(r.code).toBe(1);
+ expect(r.out).toContain('FAIL [durable_flush_read_handle]: src/core/binary-self-update.ts');
});
test('the repository passes', () => {