diff --git a/src/main/daemon/history-manager.ts b/src/main/daemon/history-manager.ts index d1c44fb1c..844642e92 100644 --- a/src/main/daemon/history-manager.ts +++ b/src/main/daemon/history-manager.ts @@ -1,5 +1,13 @@ import { join } from 'path' -import { mkdirSync, writeFileSync, readFileSync, existsSync, rmSync, renameSync } from 'fs' +import { + mkdirSync, + writeFileSync, + readFileSync, + existsSync, + rmSync, + renameSync, + unlinkSync +} from 'fs' import { getHistorySessionDirName } from './history-paths' import type { TerminalSnapshot } from './types' @@ -54,9 +62,20 @@ export class HistoryManager { } writeFileSync(join(dir, 'meta.json'), JSON.stringify(meta, null, 2)) + // Why: if a session ID is reused after a previous clean exit, the old + // checkpoint.json may still be on disk. Without removing it, a crash + // before the first 5s checkpoint tick would cause detectColdRestore to + // replay stale terminal content from the previous session. + const checkpointPath = join(dir, 'checkpoint.json') + try { + unlinkSync(checkpointPath) + } catch { + // ENOENT is expected for new sessions + } + this.writers.set(sessionId, { dir, - checkpointPath: join(dir, 'checkpoint.json') + checkpointPath }) } catch (err) { this.handleWriteError(sessionId, err) diff --git a/src/main/index.ts b/src/main/index.ts index b7aeb1d60..413d07649 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -461,7 +461,12 @@ app.on('before-quit', () => { rateLimits?.stop() }) -app.on('will-quit', () => { +// Why: will-quit fires twice when daemon disconnect needs an async flush. +// First pass: run all sync cleanup, then preventDefault to await the final +// checkpoint writes. Second pass (after disconnect resolves): skip the +// async work and let Electron exit. +let daemonDisconnectDone = false +app.on('will-quit', (e) => { // Why: stats.flush() must run before killAllPty() so it can read the // live agent state and emit synthetic agent_stop events for agents that // are still running. killAllPty() does not call runtime.onPtyExit(), @@ -474,15 +479,6 @@ app.on('will-quit', () => { // holding ports and leaving stale session state on disk. runtime?.getAgentBrowserBridge()?.destroyAllSessions() killAllPty() - // Why: in daemon mode, killAllPty is a no-op (daemon sessions survive app - // quit) but the client connection must be closed so sockets are released. - // disconnectDaemon only tears down the client transport — it does NOT kill - // the daemon process or mark its history as cleanly ended, preserving both - // warm reattach and crash recovery on next launch. - // Why void: will-quit is synchronous in Electron, but the internal await - // ensures checkpoints complete before socket disconnect. The promise chain - // finishes on the event loop before the process actually exits. - void disconnectDaemon() void closeAllWatchers() if (runtimeRpc) { void runtimeRpc.stop().catch((error) => { @@ -490,6 +486,18 @@ app.on('will-quit', () => { }) } store?.flush() + + // Why: disconnectDaemon writes final checkpoints via async getSnapshot RPCs. + // Without preventDefault, Electron exits before the RPCs complete and the + // checkpoint data is lost. The guard prevents an infinite quit loop — + // app.quit() re-fires will-quit, but the second pass skips straight through. + if (!daemonDisconnectDone) { + e.preventDefault() + disconnectDaemon().finally(() => { + daemonDisconnectDone = true + app.quit() + }) + } }) app.on('window-all-closed', () => {