fix(session-persistence): handle Windows directory fsync

This commit is contained in:
Tianyi Cui
2026-07-15 21:36:42 +08:00
parent b78cdbcd51
commit 7278fc21d8
4 changed files with 70 additions and 18 deletions
@@ -56,6 +56,9 @@ export class SessionPersistenceJsonl extends SessionPersistence implements Persi
private root: string
private coordinator: PersistenceCoordinator<number>
/** Runtime-only host-platform seam for directory-sync compatibility tests. */
readonly internals: { platform: NodeJS.Platform } = { platform: process.platform }
constructor(ctx: Context, public config: Config) {
super(ctx)
// Resolve once so later process.cwd() changes cannot split one backend across roots.
@@ -202,11 +205,18 @@ export class SessionPersistenceJsonl extends SessionPersistence implements Persi
}
}
/** fsync a directory so a just-created or published entry inside it is crash-durable. */
/** fsync a directory when the host exposes that durability primitive. */
private async syncDir(dir: string): Promise<void> {
const handle = await open(dir, 'r')
try {
await handle.sync()
try {
await handle.sync()
} catch (error: unknown) {
const code = (error as NodeJS.ErrnoException | null)?.code
// Node opens directories on Windows but its fsync binding rejects them.
// File-content fsync remains mandatory; only this unsupported primitive is skipped.
if (this.internals.platform !== 'win32' || code !== 'EPERM') throw error
}
} finally {
await handle.close()
}