Repository navigation
fix(terminal): preserve scrollback when captureBuffers runs during a transient empty-render window - #1509
Merged
Conversation
…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
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
…transient empty-render window (stablyai#1509) Co-authored-by: Orca <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
scrollbackLines: 0in their stored snapshotssnapshotAnsisize was ~106 bytes (just the current prompt, viewport-only)buffersByLeafIdentry but noptyIdsByLeafIdentry — runtime had no session to reattach toRoot cause
captureBuffersinTerminalPane.tsxwrites the full layout viasetTabLayout, which is a REPLACE (terminals.ts:1257). WhencaptureBuffersfires during a transient window where xterm hasn't yet rendered (post-remount, just-attached, mid-replay),serializereturns ~0 bytes for one or more leaves.The old code only set
layout.buffersByLeafId/layout.ptyIdsByLeafIdwhen 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
persistLayoutSnapshotalready guards against (it reads existing state and overlays preserved fields).captureBuffersdid 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.captureBuffersnow uses the helper for bothbuffersByLeafIdandptyIdsByLeafId.Behavior:
Why not periodic save
PR #461 tried periodic save and removed it because the 3-minute
setIntervalblocked the renderer main thread for several seconds on large layouts (see comment inApp.tsx:452-463). The merge approach is O(leaves) once per beforeunload/sleep, with no main-thread cost during normal use.Verification
mergeCapturedLeafState(wipe-protection, overlay, drop-removed, undefined prior, etc.)pnpm dev(CDP attach on port 9333):setTabLayoutwith an empty layout (the unfixedcaptureBufferspath) wipes a 557-byte buffer to 0 and drops the ptyIdbeforeunloadcaptures 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 helpersrc/renderer/src/components/terminal-pane/merge-captured-leaf-state.test.ts— 6 unit testssrc/renderer/src/components/terminal-pane/TerminalPane.tsx—captureBuffersnow uses the helperMade with Orca 🐋