fix(daemon): checkpoint reconciled sessions, restore legacy cleanup, handle empty scrollback

Three fixes from eighth code review cycle:

1. reconcileOnStartup() now adds discovered live sessions to
   activeSessionIds and registers history writers, so background sessions
   get checkpointed even before the user reattaches their pane.

2. Restore historyPath for legacy adapters. Checkpoint writes still fail
   silently (pre-v4 daemons lack getSnapshot), but the HistoryManager is
   needed for cleanup — close/exit events must mark meta.json as ended
   and remove history dirs to prevent false cold restores.

3. Alt-screen cold restore now handles empty scrollbackAnsi gracefully.
   When the emulator had no scrollback above the viewport, skip cold
   restore entirely rather than showing a blank terminal.

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 d60a954364
commit c46160064b
2 changed files with 24 additions and 10 deletions
+7 -6
View File
@@ -327,16 +327,17 @@ async function createLegacyDaemonAdapters(runtimeDir: string): Promise<DaemonPty
// Legacy adapters intentionally do not respawn: respawning an old protocol
// daemon from new code would recreate stale env semantics and can be less
// predictable than letting the session fail if that old daemon dies.
// Why no historyPath: checkpoint-based persistence requires the getSnapshot
// RPC (protocol v4+). Pre-v4 daemons reject that call, so the checkpoint
// timer would silently discard every write. Omitting historyPath avoids
// wasted RPC round-trips. Pre-upgrade scrollback.bin files remain on disk
// and the history reader's fallback path can still cold-restore from them.
// Why historyPath is still passed: checkpoint writes will fail silently
// (pre-v4 daemons don't support getSnapshot), but the HistoryManager is
// still needed for cleanup — close/exit events must remove history dirs
// and mark meta.json as ended. Without it, a later v4 session reusing
// the same ID could false-restore stale scrollback.bin.
adapters.push(
new DaemonPtyAdapter({
socketPath,
tokenPath,
protocolVersion
protocolVersion,
historyPath: getHistoryDir()
})
)
}
+17 -4
View File
@@ -137,19 +137,26 @@ export class DaemonPtyAdapter implements IPtyProvider {
// scrollbackAnsi (rows above the viewport only) which excludes the alt
// buffer. For normal sessions, use the full snapshot with rehydrate
// sequences to restore terminal modes (colors, cursor position, etc).
// Why: scrollbackAnsi may be empty if the emulator hadn't accumulated
// scrollback before the alt-screen app launched. In that case, skip
// cold restore entirely rather than showing a blank terminal — no
// content is better than confusing the user with an empty restore.
const isAltScreen = restoreInfo.modes.alternateScreen
const scrollback = isAltScreen
? restoreInfo.scrollbackAnsi
? restoreInfo.scrollbackAnsi || null
: restoreInfo.rehydrateSequences + restoreInfo.snapshotAnsi
const coldRestore = { scrollback, cwd: restoreInfo.cwd }
this.coldRestoreCache.set(sessionId, coldRestore)
// Why: use registerWriter (not openSession) to avoid deleting the
// existing checkpoint.json. If the revived daemon crashes again before
// the next 5s tick, the checkpoint is the only recovery data available.
if (this.historyManager) {
this.historyManager.registerWriter(sessionId)
}
return { id: sessionId, coldRestore }
if (scrollback) {
const coldRestore = { scrollback, cwd: restoreInfo.cwd }
this.coldRestoreCache.set(sessionId, coldRestore)
return { id: sessionId, coldRestore }
}
return { id: sessionId }
}
if (this.historyManager && result.isNew) {
@@ -314,6 +321,12 @@ export class DaemonPtyAdapter implements IPtyProvider {
killed.push(session.sessionId)
} else {
alive.push(session.sessionId)
// Why: background sessions discovered here may produce output before
// the user reattaches their pane. Without adding them to the checkpoint
// set, disconnectOnly()'s final checkpoint would skip them, leaving
// stale recovery data if the daemon later crashes.
this.activeSessionIds.add(session.sessionId)
this.historyManager?.registerWriter(session.sessionId)
}
}