The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
Fix Windows native hook confirmation after writable reopen (#1614)
0xken15ce74aff88360
diff --git a/cli/src/engines/kit/homeAdoption.spec.ts b/cli/src/engines/kit/homeAdoption.spec.tsindex 6423fa86e..edc00a55a 100644--- a/cli/src/engines/kit/homeAdoption.spec.ts+++ b/cli/src/engines/kit/homeAdoption.spec.ts@@ -6,7 +6,15 @@ import { createHomeAdopter, homeIsAbsolute, homeIsNetworkOrDevice, inspectJournaimport { NATIVE_EVIDENCE_MS } from './nativeEvidence.js'const clock = vi.hoisted(() => ({ now: 0, remaining: Infinity }))+const platform = vi.hoisted(() => ({ windows: false }))vi.mock('node:perf_hooks', async original => ({ ...await original<object>(), performance: { now: () => --clock.remaining < 0 ? 3_001 : clock.now } })) // past NATIVE_EVIDENCE_MS+vi.mock('./windowsFiles.js', async original => {+ const actual = await original<typeof import('./windowsFiles.js')>()+ return { ...actual,+ hostPlatform: () => platform.windows ? 'win32' : actual.hostPlatform(),+ journalFlushFlags: () => actual.journalFlushFlags(platform.windows ? 'win32' : undefined),+ }+})vi.mock('node:fs', async original => {const actual = await original<typeof fs>()@@ -23,7 +31,7 @@ const seal = (index: number) => index ? join(file + '.confirmations', `${String(const unavailable = () => Object.assign(new Error('fixture storage unavailable'), { code: 'EIO' })const files = new Map<number, string>()beforeEach(async () => {- clock.now = 0; clock.remaining = Infinity+ clock.now = 0; clock.remaining = Infinity; platform.windows = falseactual = await vi.importActual<typeof fs>('node:fs')for (const name of ['openSync', 'closeSync', 'fsyncSync', 'linkSync', 'readSync', 'writeFileSync', 'opendirSync', 'lstatSync'] as const) {vi.mocked(fs[name]).mockReset().mockImplementation(actual[name] as never)@@ -40,6 +48,14 @@ beforeEach(async () => {afterEach(() => { vi.restoreAllMocks(); actual.rmSync(root, { recursive: true, force: true }) })describe('durable monotone home adoption', () => {+ it('does not reopen a Windows journal record for a flush after its staged bytes were synced', () => {+ platform.windows = true+ expect(adopter.adopt(add('C:\\saved'))).toEqual(add('C:\\saved'))+ const writableRecordOpens = vi.mocked(fs.openSync).mock.calls.filter(([path, flags]) =>+ String(path) === record(0) && typeof flags === 'number' && (flags & 3) === fs.constants.O_RDWR)+ expect(writableRecordOpens).toHaveLength(0)+ })+it('leaves a new installation alone for an empty request', () => {expect(adopter.adopt(empty())).toEqual(empty())expect(actual.existsSync(file + '.adoptions')).toBe(false)diff --git a/cli/src/engines/kit/homeAdoption.ts b/cli/src/engines/kit/homeAdoption.tsindex 7020ee8b4..a4cb7cccf 100644--- a/cli/src/engines/kit/homeAdoption.ts+++ b/cli/src/engines/kit/homeAdoption.ts@@ -272,11 +272,16 @@ function seal(entry: Entry, index: number, found: State, staging: string, check:// Another writer may finish a crashed writer's complete immutable record. No lock or partial slot is reaped.const source = read(entry.path, check)if (!source || digest(source.text) !== entry.digest) throw unavailable('changed before durability confirmation')- const fd = openSync(entry.path, journalFlushFlags())- try {- if (stamp(fstatSync(fd, { bigint: true })) !== source.version) throw unavailable('changed before its durable flush')- fsyncSync(fd)- } finally { closeSync(fd) }+ // On Windows, opening O_RDWR changes ctime without writing. The staged writer fsynced before linkSync, so a+ // second flush would invalidate the version we just read instead of proving anything more.+ const flags = journalFlushFlags()+ if (flags !== null) {+ const fd = openSync(entry.path, flags)+ try {+ if (stamp(fstatSync(fd, { bigint: true })) !== source.version) throw unavailable('changed before its durable flush')+ fsyncSync(fd)+ } finally { closeSync(fd) }+ }syncDirectory(found.directory, check)// Confirmations live outside the journal: losing its newest record cannot look like an older complete pool.const marker = index ? join(found.confirmations, String(index).padStart(3, '0') + '.committed') : found.markerdiff --git a/cli/src/engines/kit/nativeHookFiles.spec.ts b/cli/src/engines/kit/nativeHookFiles.spec.tsindex b8205c670..fe5220820 100644--- a/cli/src/engines/kit/nativeHookFiles.spec.ts+++ b/cli/src/engines/kit/nativeHookFiles.spec.ts@@ -1,10 +1,14 @@-import { mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, chmodSync, fstatSync, statSync, renameSync } from 'node:fs'+import { constants, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, chmodSync, fstatSync, statSync, renameSync } from 'node:fs'import { tmpdir } from 'node:os'import { join } from 'node:path'import { afterEach, expect, it, vi } from 'vitest'import { inspectHookFile, runHookPreparation } from './nativeHookFiles.js'-const fault = vi.hoisted(() => ({ sync: false, write: false, onSync: undefined as ((fd: number) => void) | undefined, openAs: undefined as string | undefined, onStat: undefined as ((file: unknown, options: unknown) => void) | undefined }))+const fault = vi.hoisted(() => ({ sync: false, write: false, windows: false, writableOpens: 0, onSync: undefined as ((fd: number) => void) | undefined, openAs: undefined as string | undefined, onStat: undefined as ((file: unknown, options: unknown) => void) | undefined }))+vi.mock('./windowsFiles.js', async original => {+ const actual = await original<typeof import('./windowsFiles.js')>()+ return { ...actual, unchangedFlushFlags: (mode: number) => actual.unchangedFlushFlags(mode, fault.windows ? 'win32' : undefined) }+})vi.mock('node:fs', async original => {const fs = await original<typeof import('node:fs')>()return { ...fs,@@ -13,7 +17,11 @@ vi.mock('node:fs', async original => {try { if (fault.sync) throw new Error('fixture sync unavailable'); fault.onSync?.(fd); fs.fsync(fd, callback) }catch (error) { queueMicrotask(() => callback(error as Error)) }},- openSync: (...args: Parameters<typeof fs.openSync>) => fs.openSync(fault.openAs ?? args[0], ...args.slice(1) as [number, number?]),+ openSync: (...args: Parameters<typeof fs.openSync>) => {+ if (fault.windows && String(args[0]).endsWith('config.json') && typeof args[1] === 'number'+ && (args[1] & 3) === constants.O_RDWR) fault.writableOpens+++ return fs.openSync(fault.openAs ?? args[0], ...args.slice(1) as [number, number?])+ },writeFileSync: (...args: Parameters<typeof fs.writeFileSync>) => {if (fault.write && typeof args[0] === 'number') throw new Error('fixture disk full')return fs.writeFileSync(...args)@@ -33,7 +41,7 @@ function fixture() {roots.push(root)return { root, file: join(root, 'config.json') }}-afterEach(() => { fault.sync = false; fault.write = false; fault.onSync = undefined; fault.openAs = undefined; fault.onStat = undefined; vi.restoreAllMocks(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) })+afterEach(() => { fault.sync = false; fault.write = false; fault.windows = false; fault.writableOpens = 0; fault.onSync = undefined; fault.openAs = undefined; fault.onStat = undefined; vi.restoreAllMocks(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) })it('publishes complete bytes durably, preserves private permissions and leaves no staging file', async () => {const { root, file } = fixture()@@ -138,6 +146,14 @@ it('cannot acknowledge a failed sync even when the desired bytes already exist',await expect(prepared(file).replace('already prepared')).resolves.toBeDefined()})+it('confirms unchanged Windows settings without a writable reopen that would bump ctime', async () => {+ const { file } = fixture()+ writeFileSync(file, 'already prepared')+ fault.windows = true+ await expect(prepared(file).replace('already prepared')).resolves.toBeDefined()+ expect(fault.writableOpens).toBe(0)+})+it('retains removal uncertainty until a retry can sync the parent directory', async () => {const { file } = fixture()writeFileSync(file, 'owned'); fault.sync = truediff --git a/cli/src/engines/kit/windowsFiles.spec.ts b/cli/src/engines/kit/windowsFiles.spec.tsindex 143825ec2..83578777c 100644--- a/cli/src/engines/kit/windowsFiles.spec.ts+++ b/cli/src/engines/kit/windowsFiles.spec.ts@@ -70,10 +70,10 @@ describe('isRoot', () => {})describe('flush flags', () => {- it('reopens an unchanged file read-write on Windows, where only a writable handle flushes', () => {- expect(unchangedFlushFlags(0o666, 'win32')).toBe(constants.O_RDWR)+ it('does not reopen an unchanged file on Windows, where a writable open changes ctime', () => {+ expect(unchangedFlushFlags(0o666, 'win32')).toBeNull()expect(unchangedFlushFlags(0o444, 'win32')).toBeNull()- expect(journalFlushFlags('win32')).toBe(constants.O_RDWR)+ expect(journalFlushFlags('win32')).toBeNull()})it('keeps the POSIX read-only, no-follow open', () => {diff --git a/cli/src/engines/kit/windowsFiles.ts b/cli/src/engines/kit/windowsFiles.tsindex e6d2cf8fe..a639b7951 100644--- a/cli/src/engines/kit/windowsFiles.ts+++ b/cli/src/engines/kit/windowsFiles.ts@@ -48,16 +48,15 @@ export async function renameReplacing(from: string, to: string, deps: RenameDepsconst POSIX_READ = constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK/**- * The flags to reopen an unchanged settings file for its flush. On Windows it is opened read-write, which neither- * writes nor truncates it. A read-only file there is not reopened (null): it is not one Harness can write, and its- * bytes are already what Harness wants.+ * POSIX reopens an unchanged settings file read-only for its flush. On Windows, a read-write open changes ctime even+ * without a write (measured on NTFS with Node 22.23.2), invalidating the file proof. The staged writer already+ * flushed before rename, and Windows directory handles cannot be flushed, so do not reopen the unchanged file.*/-export function unchangedFlushFlags(mode: number, platform: NodeJS.Platform = hostPlatform()): number | null {- if (platform !== 'win32') return POSIX_READ- return (mode & 0o200) === 0 ? null : constants.O_RDWR+export function unchangedFlushFlags(_mode: number, platform: NodeJS.Platform = hostPlatform()): number | null {+ return platform === 'win32' ? null : POSIX_READ}-/** The flags to reopen a journal record Harness wrote itself, for its flush: read-write on Windows. */-export function journalFlushFlags(platform: NodeJS.Platform = hostPlatform()): number {- return platform === 'win32' ? constants.O_RDWR : POSIX_READ+/** Reopen a journal record only on POSIX. A Windows O_RDWR open would change its reviewed ctime. */+export function journalFlushFlags(platform: NodeJS.Platform = hostPlatform()): number | null {+ return platform === 'win32' ? null : POSIX_READ}