fix(daemon): clear stale checkpoints on session reuse, await disconnect on quit

Two fixes from fourth code review cycle:

1. openSession() now removes any leftover checkpoint.json before writing
   fresh meta.json. Without this, a reused session ID that crashes before
   the first 5s checkpoint tick would cold-restore stale terminal content
   from the previous session.

2. The will-quit handler now uses Electron's preventDefault pattern to
   await disconnectDaemon() before allowing the process to exit. This
   guarantees the final getSnapshot RPCs and checkpoint writes complete
   rather than racing with process teardown.

Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
Jinwoo-H
2026-04-26 22:23:11 -04:00
co-authored by Orca
parent ddfb35d0da
commit 146fa86bc0
2 changed files with 39 additions and 12 deletions
+21 -2
View File
@@ -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)
+18 -10
View File
@@ -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', () => {