diff --git a/src/main/daemon/daemon-pty-adapter.ts b/src/main/daemon/daemon-pty-adapter.ts index 596fc4b51..cd59405ab 100644 --- a/src/main/daemon/daemon-pty-adapter.ts +++ b/src/main/daemon/daemon-pty-adapter.ts @@ -63,6 +63,7 @@ export class DaemonPtyAdapter implements IPtyProvider { private coldRestoreCache = new Map() private activeSessionIds = new Set() private checkpointInterval: ReturnType | null = null + private checkpointInFlight: Promise | null = null private static CHECKPOINT_INTERVAL_MS = 5_000 constructor(opts: DaemonPtyAdapterOptions) { @@ -416,6 +417,12 @@ export class DaemonPtyAdapter implements IPtyProvider { clearInterval(this.checkpointInterval) this.checkpointInterval = null } + // Why: wait for any in-flight timer pass to finish before starting + // the final checkpoint. Otherwise both passes race on the shared tmp + // file, risking ENOENT on rename and disabling future writes. + if (this.checkpointInFlight) { + await this.checkpointInFlight + } // Why: without a final checkpoint, sessions opened after the last timer // tick have no checkpoint.json on disk. If the detached daemon later // dies, detectColdRestore finds nothing to restore from. Must await @@ -438,7 +445,16 @@ export class DaemonPtyAdapter implements IPtyProvider { return } this.checkpointInterval = setInterval(() => { - void this.checkpointAllSessions() + // Why: if the previous pass is still in-flight (slow RPC or disk), + // skip this tick. Overlapping passes race on the shared tmp file + // in checkpoint(), and a lost rename triggers handleWriteError which + // permanently disables the session's history writes. + if (this.checkpointInFlight) { + return + } + this.checkpointInFlight = this.checkpointAllSessions().finally(() => { + this.checkpointInFlight = null + }) }, DaemonPtyAdapter.CHECKPOINT_INTERVAL_MS) }