Skip to content

fix(terminal): persist a parked remote pane's scrollback across a hard restart (#21295) - #21367

Merged
nwparker merged 3 commits into
mainfrom
fix/parked-scrollback-restart-persistence
Sep 21, 2026
Merged

nwparker merged 3 commits into
mainfrom
fix/parked-scrollback-restart-persistence

Conversation

@nwparker

@nwparker nwparker commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​625 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​624
Prod 7 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​82 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​76

The hop where the buffers are dropped

A parked remote-runtime terminal's scrollback is captured into the renderer store as terminalLayoutsByTabId[tabId].buffersByLeafId, but the debounced session-patch writer routes it to the wrong host partition, where the main process then strips it.

Instrumented on a real paired-desktop e2e run, one hop at a time:

storeAtPark        395   (renderer store has the capture)
split.patch        payloadTabsByWorktreeKeys: []   -> local slice bufLen 395   (misrouted)
main.setLocal      in 395 -> afterPrune 0          (stripped in the local partition)
onDisk runtime partition: bufferLength 0           (the remote host's partition never got it)

A park capture changes only terminalLayoutsByTabId, so buildWorkspaceSessionPatch emits a patch with no tabsByWorktree (workspace-session-patch.ts:66-71). splitWorkspaceSessionByHost builds its tab→worktree index from the patch alone (workspace-session-host-split.ts:179 → buildWorktreeIdByTabId), finds nothing, and routes every layout to the local partition (:141). Main receives a local patch and pruneLocalTerminalScrollbackBuffers (workspace-session-terminal-buffers.ts:80) can't resolve the tab's worktree inside the local partition, so it strips the buffers (:25-27). The remote host's runtime:<id> partition on disk keeps its old bufferless layout.

Root cause (not the symptom)

Tab-keyed patch fields (terminalLayoutsByTabId, remoteSessionIdsByTabId) are routed by a tab→worktree index derived only from the patch payload. A debounced patch carries only the fields that changed, so a layouts-only or ptyIds-only patch has no tab rows to route by and every row falls into local.

Only the beforeunload checkpoint masks this: it writes a full snapshot (buildWorkspaceSessionHostSnapshots, replace mode) that includes tabsByWorktree, so it routes correctly. That is why the loss depends on not getting a clean shutdown — a crash, SIGKILL, or a forced kill during an app update between the park and the next checkpoint loses everything captured since.

A correction to the tracking measurement: the reported onDiskAfterQuit: 0 came from a reader that inspected only the local workspaceSession blob. On a clean quit the checkpoint does persist the capture to workspaceSessionsByHostId['runtime:<id>']; the reader just never looked there. The genuine, reproducible loss is on the hot/debounced write path, realized by a hard kill.

The fix

Route tab-keyed patch fields with the renderer's live tab catalogs as a fallback when the payload names no tab rows.

  • extendWorktreeIdByTabId (workspace-session-host-records.ts) fills the payload-derived index from tabsByWorktree / unifiedTabsByWorktree. Payload rows win — main merges the payload's own tabsByWorktree into whichever partition the write lands in, so a layout must follow the tab row in this write.
  • splitWorkspaceSessionByHost takes an optional { worktreeIdByTabId }; the default is unchanged so every other caller and the orphan-tab behaviour are byte-identical.
  • splitWorkspaceSessionForWrite passes the extended index for all three write modes. No main-side change: once routed to runtime:<id>, main merges the partition's own prior tabsByWorktree and the prune preserves (a paired client's repos is empty, which fails open toward "remote"; a hydrated runtime-host repo preserves for any non-local host).

chooseRemoteTerminalLayout re-evaluated

Re-checked now that persistence delivers the buffer to disk. #21285's retainLocalScrollbackInRemoteLayout carry-through is present and the relaunch reveal works with it; no additional fix in chooseRemoteTerminalLayout was needed — the restart case is covered by that existing retention once the write path stops dropping the capture.

Test

tests/e2e/paired-remote-terminal-parked-scrollback-restart.spec.ts — paired-desktop topology, real remote-runtime ptys, host forced to answer no snapshot (ORCA_E2E_FORCE_REMOTE_TERMINAL_SNAPSHOT_UNAVAILABLE=1).

The load-bearing oracle is on disk, not the reveal: the paired host's pty stays alive across the restart and keeps a retained tail, so a reveal can be served by the host rather than by the client's persisted copy. The contradiction-capable signal is which partition on disk holds the buffer after a debounced write.

  • hard kill (the proof): park, wait for the debounced write to land, then forceQuitElectronAppForE2E (SIGKILL, no beforeunload, no checkpoint) and relaunch on the same profile. Asserts the capture is in the runtime:<id> partition on disk and not stripped in local. Measured: storeAtPark 394 -> onDisk runtime bufferLength 394, local none -> relaunch reveal true.
  • clean quit (control): documents that the checkpoint path also lands the capture in the runtime partition — passes on main too, so it is harness sanity, not proof.

Result: 2 passed.

Mutation results

  • Unit (workspace-session-host-persistence.test.ts): removing the catalog extension fails the 3 catalog-dependent routing tests (layouts-only, remote-session-id-only, unified-only) while the payload-carries-tabs test still passes and the split-primitive tests still pass — the mutation is caught exactly where the fix lives.
  • E2E: with the catalog extension reverted, the hard-kill test goes red on runtimePartitionHoldsCapture (runtime partition on disk stays bufferless), confirming the assertion is not a survivor.

Verification

  • pnpm tc clean; oxlint / oxfmt clean on changed files.
  • Merge-commit gate: ORCA_CODE_QUALITY_BASE=origin/main node config/scripts/check-changed-code-quality.mjs → passed. The reader's as casts were removed (rewritten with type guards) rather than suppressed.
  • Unit routing tests: 47 passed across the three touched suites.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — the debounced session write now routes tab-keyed rows through the renderer's live tab catalogs when a patch names no tab rows, so a park capture's layouts-only patch reaches the remote host's partition instead of being pruned in local.

  • extendWorktreeIdByTabId fallback (src/shared/workspace-session-host-records.ts): fills a payload-derived tab→worktree index from the live tabsByWorktree / unifiedTabsByWorktree, leaving payload rows authoritative via !byTab.has.
  • splitWorkspaceSessionByHost option (workspace-session-host-split.ts): accepts an optional worktreeIdByTabId; the default stays buildWorktreeIdByTabId(state), so orphan tabs still stay local.
  • Write-path wiring (workspace-session-host-persistence.ts): splitWorkspaceSessionForWrite builds the extended index for all three write modes; HostPersistenceState now carries the catalog fields. Main needed no change because the runtime partition's own tabsByWorktree already attributes the tab, and the prune fails open toward remote.
  • Unit coverage: layouts-only, remote-session-id-only, unified-only routing, payload-precedence, and orphan-stays-local cases. I ran the two touched suites locally: 42 passed.
  • E2E (paired-remote-terminal-parked-scrollback-restart.spec.ts + quitPreservingProfile): the hard-kill test's on-disk runtime:<id> oracle is contradiction-capable, and the clean-quit test is documented as harness sanity rather than proof.

I independently probed the routing change for moved tabs, unified id !== entityId, and contested/restored worktree ids; the fallback only fills keys the payload did not resolve and hostIdByWorktreeId retains the partition decision, so no misroute or cross-partition overwrite is reachable.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker force-pushed the fix/park-discards-only-scrollback-copy branch 2 times, most recently from 1156da9 to 2a2c1a7 Compare September 18, 2026 11:24
@nwparker
nwparker force-pushed the fix/parked-scrollback-restart-persistence branch from cdda9b2 to 1092e35 Compare September 18, 2026 11:27

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — the commits since the prior pullfrog review add a session-addressed scrollback read, ratchet it to the cap's home list, and make the restart spec read both scrollback homes through the shared resolver. The routing fix itself is byte-identical after the rebase onto the newer base.

  • resolveTabScrollbackBuffers (leaf-scrollback-resolution.ts): addresses the existing two-home read by tab over a session-shaped record, typed off TERMINAL_SCROLLBACK_SESSION_HOMES.
  • Resolver ratchet (leaf-scrollback-resolution.test.ts): enumerates the cap's home constant so a third home fails until the resolver reads it, alongside the existing precedence cases.
  • E2E debug handle (terminal-parking-e2e-overrides.ts, env.d.ts): window.__terminalParkingDebug.resolveLeafScrollback routes the spec's store read through the same resolver instead of reading one home directly.
  • On-disk reader (paired-remote-terminal-parked-scrollback-restart.spec.ts): reads terminalLayoutsByTabId and localOnlyScrollbackByTabId through resolveLeafScrollbackBuffers, so an ordinary park's bytes no longer read as a false zero.
  • Clean-quit control: swaps the flaky tokenAfterReveal assertion for localPartitionDidNotKeepCapture, with the reason documented inline.
  • Normalization note (terminal-session-cleanup.ts): documents why localOnlyScrollbackByTabId stays out of the full-normalization trigger set.

Verified locally: leaf-scrollback-resolution.test.ts + terminal-parking-e2e-overrides.test.ts = 15 passed; oxlint clean on the six changed files; tc:web clean.

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

…ost's partition

A park capture changes only terminalLayoutsByTabId, so its debounced session
patch carries no tabsByWorktree. splitWorkspaceSessionByHost built its
tab->worktree index from the patch alone, resolved nothing, and routed every
layout to the 'local' partition, where main's pruneLocalTerminalScrollbackBuffers
strips scrollback it cannot attribute to a remote worktree. The remote host's
runtime:<id> partition never received the capture, so anything parked since the
last clean checkpoint was lost on a crash, SIGKILL, or a forced kill during an
app update (#21295).

Route tab-keyed patch fields with the renderer's live tab catalogs as a fallback
when the payload names no tab rows. Payload rows still win, so full-payload
writes are byte-identical. Once routed to runtime:<id>, main merges the
partition's own prior tabsByWorktree and the prune preserves.

Proven by tests/e2e/paired-remote-terminal-parked-scrollback-restart.spec.ts: a
hard kill (no checkpoint) then relaunch, asserting the capture is in the remote
host's partition on disk. Mutation: reverting the routing fix turns that
assertion red and fails the 3 catalog-dependent unit routing tests.

(cherry picked from commit 58a344c)
…atchet the resolver to the cap's home list

The restart spec read only buffersByLeafId, but the ordinary park now writes
localOnlyScrollbackByTabId, so its own proof reported a false zero and both tests failed for the
wrong reason. Both readers now go through resolveLeafScrollbackBuffers: the on-disk reader calls
it directly (it is a pure function), and the store reader — which runs inside page.evaluate —
reaches it through a new window.__terminalParkingDebug.resolveLeafScrollback(tabId) handle.

resolveTabScrollbackBuffers is typed off TERMINAL_SCROLLBACK_SESSION_HOMES and its unit test
enumerates that constant, so adding a third home fails to compile and fails a test until the
resolver reads it — the 'no consumer reads a home directly' invariant becomes enforceable.

The clean-quit control no longer asserts tokenAfterReveal (measured true, true, false on identical
product code; the live host can serve the reveal from its own tail). It keeps the five
deterministic fields and logs the reveal; the hard-kill test still asserts it, because there the
host is forced unavailable and the reveal must come from the client copy.

(cherry picked from commit 0cd3db1)
…de full normalization

The two scrollback homes look symmetric (TERMINAL_SCROLLBACK_SESSION_HOMES), so the missing key
reads as an oversight. It is load-bearing: adding it would route the field through the fail-closed
strip and reintroduce the loss this branch fixes. The renderer prunes it with attribution before
the patch is sent, so the cap still holds without main as a second line.

(cherry picked from commit 1092e35)
@nwparker
nwparker force-pushed the fix/parked-scrollback-restart-persistence branch from 1092e35 to a6b4f34 Compare September 21, 2026 05:46
@nwparker
nwparker changed the base branch from fix/park-discards-only-scrollback-copy to main September 21, 2026 05:46
@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 5 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: 8a16e2fe-9ac2-462f-9728-c2418aa12529

📥 Commits

Reviewing files that changed from the base of the PR and between 2872c3f and a6b4f34.

📒 Files selected for processing (11)
  • src/main/persistence/loading-store/terminal-session-cleanup.ts
  • src/renderer/src/components/terminal-pane/leaf-scrollback-resolution.test.ts
  • src/renderer/src/components/terminal-pane/leaf-scrollback-resolution.ts
  • src/renderer/src/components/terminal-pane/terminal-parking-e2e-overrides.ts
  • src/renderer/src/env.d.ts
  • src/renderer/src/lib/workspace-session-host-persistence.test.ts
  • src/renderer/src/lib/workspace-session-host-persistence.ts
  • src/renderer/src/lib/workspace-session-host-split.test.ts
  • src/renderer/src/lib/workspace-session-host-split.ts
  • src/shared/workspace-session-host-records.ts
  • tests/e2e/paired-remote-terminal-parked-scrollback-restart.spec.ts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant