Skip to content

fix(terminal): preserve scrollback when captureBuffers runs during a transient empty-render window - #1509

Merged
Jinwoo-H merged 2 commits into
mainfrom
Jinwoo-H/investigate-checkpoint-scrollback
May 6, 2026
Merged

Jinwoo-H merged 2 commits into
mainfrom
Jinwoo-H/investigate-checkpoint-scrollback

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Symptom

After relaunching Orca (or moving a worktree off-screen and back), old terminal tabs render only a viewport-height prompt — the prior scrollback is gone.

Inspecting orca-data.json on disk confirmed the pattern:

  • 80/90 sessions had scrollbackLines: 0 in their stored snapshots
  • median snapshotAnsi size was ~106 bytes (just the current prompt, viewport-only)
  • 177/354 leaves had a buffersByLeafId entry but no ptyIdsByLeafId entry — runtime had no session to reattach to

Root cause

captureBuffers in TerminalPane.tsx writes the full layout via setTabLayout, which is a REPLACE (terminals.ts:1257). When captureBuffers fires during a transient window where xterm hasn't yet rendered (post-remount, just-attached, mid-replay), serialize returns ~0 bytes for one or more leaves.

The old code only set layout.buffersByLeafId / layout.ptyIdsByLeafId when the maps were non-empty — but if even one pane was empty, the other panes still got included and the now-empty leaf was silently dropped, then the entire prior layout was REPLACED. Net effect: a transient empty render wipes a known-good persisted buffer.

This is the exact asymmetry persistLayoutSnapshot already guards against (it reads existing state and overlays preserved fields). captureBuffers did not.

Fix

Introduce a small pure helper, mergeCapturedLeafState, that overlays fresh leaf state on top of prior state for leaves still present in the current pane tree. captureBuffers now uses the helper for both buffersByLeafId and ptyIdsByLeafId.

Behavior:

  • Leaves removed from the layout are still dropped (cleanup is preserved)
  • Leaves whose live capture is empty fall back to the prior known-good entry (no wipe)
  • Non-empty captures still win (fresh data overrides prior)

Why not periodic save

PR #461 tried periodic save and removed it because the 3-minute setInterval blocked the renderer main thread for several seconds on large layouts (see comment in App.tsx:452-463). The merge approach is O(leaves) once per beforeunload/sleep, with no main-thread cost during normal use.

Verification

  • Unit tests: 6 new tests for mergeCapturedLeafState (wipe-protection, overlay, drop-removed, undefined prior, etc.)
  • Renderer suite: 1587/1587 pass
  • Terminal-pane suite: 202/202 pass
  • Lint + typecheck: clean (node + cli + web)
  • Live repro in pnpm dev (CDP attach on port 9333):
    • Failure mode confirmed: direct setTabLayout with an empty layout (the unfixed captureBuffers path) wipes a 557-byte buffer to 0 and drops the ptyId
    • Fix verified: 5 successive beforeunload captures preserve all 557 bytes; across 373 persisted buffers, all preserved (0 wiped, 0 shrunk)

Files

  • src/renderer/src/components/terminal-pane/merge-captured-leaf-state.ts — new pure helper
  • src/renderer/src/components/terminal-pane/merge-captured-leaf-state.test.ts — 6 unit tests
  • src/renderer/src/components/terminal-pane/TerminalPane.tsx — captureBuffers now uses the helper

Made with Orca 🐋

Jinwoo-H and others added 2 commits May 6, 2026 15:46
…transient empty-render window

Symptom: after relaunching Orca (or moving a worktree off-screen and back),
old terminal tabs render only a viewport-height prompt — the prior scrollback
is gone. Inspecting orca-data.json showed the same pattern: most tabs had a
buffersByLeafId entry of only ~80–500 bytes (just the current prompt), and
many leaves had a buffer but no ptyIdsByLeafId entry, so the runtime had no
session to reattach to.

Root cause: captureBuffers in TerminalPane.tsx writes the full layout via
setTabLayout, which REPLACES (terminals.ts:1257). When captureBuffers fires
during a transient window where xterm hasn't yet rendered (post-remount,
just-attached, mid-replay), serialize returns ~0 bytes for one or more leaves.
The old code only set layout.buffersByLeafId / layout.ptyIdsByLeafId when the
maps were non-empty — but if just one pane was empty, the other panes still
got included AND the now-empty leaf was silently dropped, then the entire
prior layout was REPLACED. Net effect: a transient empty render wipes a
known-good persisted buffer.

This is the exact asymmetry persistLayoutSnapshot already guards against
(it reads existing state and overlays preserved fields). captureBuffers did
not.

Fix: introduce a small pure helper, mergeCapturedLeafState, that overlays
fresh leaf state on top of prior state for leaves still present in the
current pane tree. captureBuffers now uses the helper for both
buffersByLeafId and ptyIdsByLeafId. Leaves removed from the layout are
still dropped; leaves whose live capture is empty fall back to the prior
known-good entry; non-empty captures still win.

Why not periodic save: PR #461 tried this and removed it because the
3-minute setInterval blocked the renderer main thread for several seconds
on large layouts (App.tsx:452-463). The merge approach is O(leaves) once
per beforeunload/sleep, with no main-thread cost during normal use.

Verification:
  - 6 unit tests for mergeCapturedLeafState (wipe-protection, overlay,
    drop-removed, undefined prior, etc.)
  - 1587/1587 renderer tests pass
  - 202/202 terminal-pane tests pass
  - Live repro in pnpm dev (CDP attach):
      * Failure mode confirmed: setTabLayout with empty layout wipes
        557-byte buffer to 0 and drops ptyId
      * Fix verified: 5 successive beforeunload captures preserve all
        557 bytes; 373 persisted buffers across all tabs preserved
        (0 wiped, 0 shrunk)
  - Lint clean, typecheck clean (node + cli + web)

Co-authored-by: Orca <[email protected]>
…ot + filter fresh by currentLeafIds

Two follow-ups from code review:

1. mergeCapturedLeafState now filters `fresh` by `currentLeafIds` too (defense
   in depth — previously only `prior` was filtered). In the current call sites
   `fresh` is always built from the same `panes` set as `currentLeafIds`, so
   this is a no-op today, but it locks the invariant down for future callers.
   The misnamed unit test (which passed currentLeafIds matching every fresh
   key) is rewritten to actually exercise the new filter.

2. persistLayoutSnapshot now uses mergeCapturedLeafState for both
   buffersByLeafId and ptyIdsByLeafId, eliminating the duplicated
   filter/merge logic. Behavior is identical: prior preserved for
   still-mounted leaves, live PTY entries (when present) overwrite prior.

All 202 terminal-pane tests pass, 1587 renderer tests pass, lint + typecheck
clean.

Co-authored-by: Orca <[email protected]>
@Jinwoo-H
Jinwoo-H merged commit f4a238b into main May 6, 2026
2 checks passed
Jinwoo-H added a commit that referenced this pull request May 6, 2026
…r responses

Symptom: '(node) MaxListenersExceededWarning: Possible EventEmitter memory
leak detected. 11 pty:serializeBuffer:response listeners added to
[IpcMainImpl]. MaxListeners is 10.' fires whenever many sleeping PTYs wake
together — typical on relaunch when shell-ready signals fan out across
worktrees.

Cause: requestSerializedBuffer in src/main/ipc/pty.ts registered one
ipcMain.on('pty:serializeBuffer:response', ...) listener per call and
removed it on response or 750ms timeout. With 11+ concurrent in-flight
calls, the listener count crosses Node's default MaxListeners (10) and
emits the warning. The pattern also runs every listener for every response,
which is O(N) per dispatch instead of O(1).

Fix: install a single persistent ipcMain listener at registerPtyHandlers
time and route responses to the originating request via a request-ID
dispatch table (Map<requestId, { resolve, timeout }>). The 750ms timeout
behavior is unchanged. removeAllListeners is called on re-registration so
HMR / window-recreate paths don't double-bind.

Tests (5 new, in src/main/ipc/pty.test.ts):
  - registers exactly one listener regardless of concurrent in-flight
    requests (12 calls → 1 listener)
  - routes each response to the originating request via requestId
  - ignores responses with unknown / undefined requestId
  - resolves to null on the 750ms timeout
  - resolves to null on malformed snapshot

Verification:
  - 70/70 pty.test.ts pass
  - lint: 0 errors (7 pre-existing warnings, none in this file)
  - typecheck: clean (node + cli + web)

Discovered while investigating the renderer-side scrollback fix in #1509.
That PR doesn't touch this code path; this is a separate, pre-existing
issue introduced by PR #1245 (mobile-prefer-renderer-scrollback).

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
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