Repository navigation
fix(daemon): rebase durable checkpoints on the live terminal - #22732
Conversation
A durable checkpoint was folded from the previous checkpoint plus recorded output, so it inherited that checkpoint's modes forever. After a daemon restart killed a full-screen TUI and a new process started inline, the chain kept the dead TUI's alt screen and mouse tracking (?1049h ?1003h ?1006h) while the live emulator was clean. Every reattach and getBufferSnapshot served the stale chain, the renderer re-armed mouse tracking, and wheel scrolling went to a program that never asked for it: scrolling froze. Each full checkpoint is now the live snapshot verbatim (screen, layout, alt frame, modes, owner) with only the normal-buffer rows live has evicted taken from the durable replay. A checkpoint can no longer carry a dead process's modes, and checkpoints already poisoned on disk heal on the next compaction. - The first fold after a cold restore replays the same seed segments live was given, so rows line up even over a dead TUI's alt screen. - Idle zero-record folds keep the disk copy when it already agrees with live, so quit and relaunch bursts don't replay every session. - Held teardown bytes are already in the drained records and the live snapshot, so they are no longer replayed twice or appended as a tail. - The bounded getBufferSnapshot path honors the requested depth even when the live window is deeper, without phantom link rows. - The fold's ownership scanner and frame merge are removed; owner and frame come from live.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 📝 WalkthroughWalkthroughDurable checkpoint construction now rebases older history onto live terminal snapshots and bounds snapshots to a requested scrollback depth. Snapshot replay supports scrollback limits, preserves Kitty keyboard flags, and handles OSC-link ranges. Checkpoint persistence reconstructs history from drained records and uses first-take state. Added tests cover replay, restore, rebasing, remount, and teardown behavior. Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: 🟡 Moderate · up to A checkpoint can restore stale terminal content instead of the live screen. Check screen agreement before reusing the disk checkpoint; this should be fixed before merging. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The checkpoint change is contained to terminal sessions and does not increase the accepted scrollback limit. The main recovery paths preserve the live screen, but not every interrupted or stale-checkpoint case has direct verification. Retained concerns Security review detailsSecurity Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: c07b19e9-3dd4-4ed4-849e-da7bc8da6dac
📒 Files selected for processing (12)
src/main/daemon/daemon-durable-checkpoint-live-rebase.test.tssrc/main/daemon/daemon-durable-history-ownership-seed.test.tssrc/main/daemon/daemon-durable-history-snapshot.tssrc/main/daemon/daemon-pty-adapter-live-rebased-checkpoint.test.tssrc/main/daemon/daemon-pty-buffer-snapshots.tssrc/main/daemon/daemon-pty-checkpoint-persistence.tssrc/main/daemon/daemon-pty-runtime-state.tssrc/main/daemon/daemon-restore-scrollback-depth.test.tssrc/main/daemon/durable-history-replay-emulator.tssrc/main/daemon/headless-emulator.tssrc/main/daemon/headless-osc-link-ranges.tssrc/main/daemon/terminal-checkpoint-serializer.ts
💤 Files with no reviewable changes (1)
- src/main/daemon/daemon-durable-history-ownership-seed.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
There was a problem hiding this comment.
ℹ️ No critical issues — one minor suggestion inline.
Reviewed changes
- Live-first durable checkpoint —
buildDurableCheckpointSnapshotnow derives the screen, modes, alt frame, owner, cwd/title andoutputSequencefrom the live daemon snapshot; a durable replay only prepends the normal-buffer rows the live window evicted (rebaseOnOlderRows,DurableHistoryReplayEmulator.serializeNormalBufferHead). - Ownership/frame scanners deleted — the replayed-output
TerminalShellLifecycleScannerownership pass and frame merge are gone; owner comes from live, whose recovery barrier already revokes it when a TUI starts. - Idle-fold fast path —
diskCheckpointAgreesWithLive/diskCheckpointWithLiveIdentityreuse the disk copy with live's identity when grid, all mode keys,rehydrateSequencesand the alt frame already match, avoiding a full replay. - Persistence uses drained records only — a teardown take's held bytes are already in
drainedRecords, sotake.recordsis no longer replayed/appended;SnapshotCheckpointResultgainsliveSnapshotfor bounded remounts andcompactDurableRestoreSnapshotrebases on it instead of the deep durable copy. - Bounded-read handling —
boundLiveSnapshottrims the live snapshot directly when live is deeper than the requested depth;replayTerminalSnapshot's trimmed path skips seeded OSC ranges and relies on re-emitted OSC 8. - Tests — new
daemon-durable-checkpoint-live-rebase.test.tsanddaemon-pty-adapter-live-rebased-checkpoint.test.ts, plus reworked depth/ownership cases; the deleted ownership-seed test is replaced by coverage that takes ownership from live.
Verification: the full src/main/daemon vitest directory passes (187 files / 2087 tests), along with pnpm run typecheck:node, oxlint, and check:code-quality:changed.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
…t folds and trims Stop seeding persisted OSC link ranges into the fold replay: they index the base buffer, so rows evicted by pending output left a link on the wrong text. The serializer already writes OSC 8 into the ANSI the fold replays. Re-apply kitty keyboard flags when replaying a snapshot for trimming, since rehydrateSequences omits them. Bound a smaller restore request by trimming the committed checkpoint instead of re-reading disk and rebasing the live window at a smaller depth.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Since the prior pullfrog review at 432da239, one commit (916a9b2) landed:
- OSC links survive folds and trims — the fold replay no longer seeds persisted
restoreInfo.oscLinks; links are reconstructed from the OSC 8 the serializer re-emits, andcollectNormalBufferOscLinkRangesdrops itsrestoredLinksparameter. - Kitty keyboard flags carried through replay —
replayTerminalSnapshotre-appliesmodes.kittyKeyboardFlags(whichrehydrateSequencesomits) before snapshotting, soboundSnapshotand the oversized-checkpoint trim keep the negotiated flags. - Bounded reads trim the committed checkpoint —
compactDurableRestoreSnapshotbounds the durable snapshot withboundSnapshotinstead of re-reading disk and rebasing live at a smaller depth;SnapshotCheckpointResult.liveSnapshotis removed. - Mode-agreement check narrowed —
diskCheckpointAgreesWithLivenow compares onlykittyKeyboardFlagsplusrehydrateSequences(which encodes the other seven modes), replacing the explicitTERMINAL_MODE_KEYSsweep.
The prior kitty-flags thread is resolved, and both CodeRabbit threads were addressed in this commit. I re-ran daemon-durable-checkpoint-live-rebase.test.ts, terminal-checkpoint-serializer.test.ts and terminal-snapshot-osc8-roundtrip.test.ts (24 tests) at 916a9b2 — all pass. I also verified the OSC 8 re-emission assumption directly against the patched @xterm/addon-serialize: it emits OSC 8 for every cell carrying a urlId, spaces included, and erased cells never retain a urlId, so dropping the seeded ranges loses no hyperlink.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
…ble fold The live window's override cap now derives from the restore depth, so live can never be deeper than the fold. pendingRecords and isFirstTake are required.
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Since the prior pullfrog review at 916a9b2, one commit (eb75ca4) landed:
- Dropped the unreachable deep-live branch —
buildDurableCheckpointSnapshotno longer early-returns aboundSnapshot(liveSnapshot, DAEMON_RESTORE_SCROLLBACK_ROWS)whenliveSnapshot.scrollbackLinesexceeds the restore depth. Verified unreachable: the live session's emulator scrollback isresolveDaemonSessionScrollbackRows()(capped atMAX_OVERRIDE_ROWS),getSnapshot().scrollbackLines = buffer.normal.length - rows <= scrollback, andMAX_OVERRIDE_ROWSis now exactlyDAEMON_RESTORE_SCROLLBACK_ROWS, so live can never be deeper than the fold. - Tied the live-window override cap to the restore depth —
MAX_OVERRIDE_ROWS = DAEMON_RESTORE_SCROLLBACK_ROWSreplaces the literal5000(same runtime value, now an explicit coupling with a new import indaemon-session-scrollback-window.ts; no module cycle). - Made checkpoint inputs required —
pendingRecordsandisFirstTakeare now required onbuildDurableCheckpointSnapshot, removing the silent[]/undefineddefaults; every caller and test was updated accordingly.
Verification at eb75ca4: daemon-durable-checkpoint-live-rebase.test.ts (13), daemon-restore-scrollback-depth.test.ts (23) and daemon-session-scrollback-window.test.ts (3) all pass (39 total); pnpm tc:node clean. The prior kitty-flags thread remains resolved and the refactor introduces no behavioral change.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
🟠 Major · Check the live normal-buffer screen before reusing the… · daemon-durable-history-snapshot.ts:90
src/main/daemon/daemon-durable-history-snapshot.ts:90
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winCheck the live normal-buffer screen before reusing the disk checkpoint.
If a zero-record fold has different disk and live screen cells but matching dimensions and modes, this check accepts the disk checkpoint.
diskCheckpointWithLiveIdentitythen persists the disksnapshotAnsiinstead of the live screen. The early return also bypasses the new first-take seed replay. Compare the visible screen before reuse, while allowing the disk checkpoint to retain older scrollback. Add a zero-record regression case with different normal-buffer screens.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: e846c747-656b-4d0c-a37c-178183950aaf
📒 Files selected for processing (4)
src/main/daemon/daemon-durable-checkpoint-live-rebase.test.tssrc/main/daemon/daemon-durable-history-snapshot.tssrc/main/daemon/daemon-restore-scrollback-depth.test.tssrc/main/daemon/daemon-session-scrollback-window.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review.

ELI5
Orca keeps two copies of every terminal: a live one in memory and a saved one on disk that holds deeper history. The saved copy was built from its own previous version, so a mistake in it stayed forever. After a restart it could still say "the mouse belongs to the program" for a program that had been killed. When you came back to the pane, Orca believed the saved copy, and your scroll wheel went to a program that ignored it, so scrolling froze. Now every save takes the screen and settings from the live copy, and only older history from disk.
What Changed
Before: a Claude Code pane could stop scrolling with the mouse wheel (typing still worked) after the terminal daemon restarted, and restarting Orca didn't help. Reproduced on a live session: the daemon's live emulator showed alt screen off and no mouse tracking, while the durable checkpoint showed alt screen on and
?1003h ?1006h.Mechanism: each durable checkpoint was
replay(previous checkpoint) + recorded output. After a daemon restart killed a full-screen TUI and a new process started inline, the new process never sent "mode off" sequences, so the checkpoint kept the dead TUI's alt screen and mouse modes on every compaction. Reattach andgetBufferSnapshotserved that checkpoint, and the renderer re-armed mouse tracking.After: each full checkpoint is the daemon's live snapshot verbatim (screen, layout, alt frame, modes, owner). The only thing added from the durable replay is the normal-buffer rows the live window has already evicted. Consequences:
Why
Other approaches we considered:
Making live the authority for everything it holds, and letting disk add only rows live evicted, is the pattern other terminal hosts use (Superset, herdr, iTerm2). It removes a class of divergence rather than one symptom. It's the first of three planned changes; the next two unify the "process is gone" reset (which also fixes stale focus and keypad modes after a cold restore) and broaden the daemon's crash detector.
Linked Issue
Related: #13077, the same stale-checkpoint path, where the recipient is a shell that prints the mouse reports as garbage. This PR removes the stale checkpoint modes; the in-stream crash detector for normal-buffer programs is a follow-up, so this doesn't close #13077.
Visual Proof
N/A: this is a daemon persistence change and has not been demoed in the running app. The behavior is covered by adapter-level tests that go through
getBufferSnapshot, the on-disk checkpoint and a relaunch reattach.Testing
New tests fail on
mainand pass here: a poisoned checkpoint (alt screen plus?1003h?1006h) over a clean live snapshot heals in one compaction for the fold,getBufferSnapshot, the on-disk checkpoint and a relaunch reattach; a zero-record boundary fold serves live modes; the live alt frame, modes and owner are kept verbatim.Also covered: cold restores from an 80x24 checkpoint into 80x40 and 100x30 panes and over an alt-screen base (consecutive rows, nothing duplicated or lost); held bytes appear once; depth stays at the restore limit; OSC link row offsets; the seam after a reflow; the bounded-depth path (trimming the saved checkpoint keeps the same rows as the old rebuild); kitty flags survive a trim; a link stays on its text across two saves that evict rows; agreeing idle folds don't replay; xterm headless
_core.writeSyncexists (guards the synchronous-parse assumption).src/main/daemon: everything passes except tworepro-13767tests. They time out on this machine because local sqlite3 3.54 prints a different prompt; they don't touch the changed code.pnpm tc:node,oxlint, andcheck:code-quality:changedare clean.Platform: tests run on macOS only. The change is in main-process history code with no platform branches.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
Review
Two independent review passes. The first found a lost-rows case, an idle-fold slowdown, and double-applied held bytes; all three are fixed. The second pass found one remaining low issue (phantom link rows on the bounded path), also fixed.
Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
cross-version-terminal-wirepasses.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)