diff --git a/src/main/daemon/history-manager.ts b/src/main/daemon/history-manager.ts index cea470485..4cc9a85f1 100644 --- a/src/main/daemon/history-manager.ts +++ b/src/main/daemon/history-manager.ts @@ -62,15 +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 + // Why: if a session ID is reused after a previous clean exit, stale + // recovery files may still be on disk. Without removing them, a crash // before the first 5s checkpoint tick would cause detectColdRestore to - // replay stale terminal content from the previous session. + // replay stale terminal content from the previous session. Both + // checkpoint.json and scrollback.bin (legacy) must be cleaned up + // because the reader falls back to scrollback.bin when no checkpoint + // exists. const checkpointPath = join(dir, 'checkpoint.json') - try { - unlinkSync(checkpointPath) - } catch { - // ENOENT is expected for new sessions + for (const staleFile of [checkpointPath, join(dir, 'scrollback.bin')]) { + try { + unlinkSync(staleFile) + } catch { + // ENOENT is expected for new sessions + } } this.writers.set(sessionId, { diff --git a/src/main/daemon/history-reader.ts b/src/main/daemon/history-reader.ts index 8d0da2a44..f8c6f785c 100644 --- a/src/main/daemon/history-reader.ts +++ b/src/main/daemon/history-reader.ts @@ -47,9 +47,20 @@ export class HistoryReader { try { const checkpoint = JSON.parse(readFileSync(checkpointPath, 'utf-8')) + // Why: HeadlessEmulator.getSnapshot() doesn't populate scrollbackAnsi + // (it's always ''). For alt-screen checkpoints, derive usable scrollback + // by truncating snapshotAnsi at the alt-screen boundary — same logic the + // scrollback.bin fallback uses. This ensures alt-screen cold restores + // recover normal shell output that existed before the TUI launched. + const rawScrollback = checkpoint.scrollbackAnsi ?? '' + const scrollbackAnsi = + rawScrollback || + (checkpoint.modes?.alternateScreen + ? this.truncateAltScreen(checkpoint.snapshotAnsi ?? '') + : (checkpoint.snapshotAnsi ?? '')) return { snapshotAnsi: checkpoint.snapshotAnsi, - scrollbackAnsi: checkpoint.scrollbackAnsi ?? '', + scrollbackAnsi, rehydrateSequences: checkpoint.rehydrateSequences, cwd: checkpoint.cwd, cols: checkpoint.cols,