Skip to content

fix(app): keep side panel terminals alive across panel and thread switches - #7726

Closed
danteregis wants to merge 1 commit into
get-bb:mainfrom
danteregis:fix/terminal-keep-alive
Closed

danteregis wants to merge 1 commit into
get-bb:mainfrom
danteregis:fix/terminal-keep-alive

Conversation

@danteregis

Copy link
Copy Markdown

Human comments

What was wrong

The secondary panel renders content only for its active tab. So a terminal unmounts when the user switches to another panel tab, such as Browser, or to another thread. ThreadTerminalView disposes its xterm instance and its TerminalWebSocketTransport on unmount. On return it builds both again, connects with sinceSeq=0, and the server replays up to 512 KB of history. The user waits for the reconnect and watches the replay scroll by. Details and permalinks are in #7724.

What changed

  • New apps/app/src/components/thread/terminal/terminal-runtime-cache.ts. It is a small LRU of parked terminal runtimes, keyed by terminal id. It keeps at most 8. Parking a 9th disposes the oldest. It has no imports.
  • ThreadTerminalView.tsx:
    • A new keepAlive prop, false by default. ThreadTerminalContent passes true. Onboarding keeps today's behavior.
    • mountThreadTerminal now builds a runtime: the xterm instance, its addons, the transport, and a wrapper element.
    • Every closure the runtime owns reads the current view's setters and callbacks through liveRef. liveRef is updated in a layout effect, so a reused runtime never calls the setters of a view that has unmounted.
    • On unmount, a runtime with keepAlive, a running session and no exited message is parked: its element is removed, the selection is cleared, and transport.setVisible(false) is called. Otherwise it is disposed as before.
    • On mount, a parked runtime for the same terminal id is reattached synchronously. There is no reconnect and no replay.
    • A parked runtime is evicted when the socket reports exited, or an error with code terminal_exited, terminal_not_found or terminal_not_running.
  • terminal-cache-owner.ts: evicts the parked runtime when a session goes missing or closes as exited.
  • No wire change. No CLI, guide or doc change.
  • While parked, the socket stays open with visibility false. The server then does not let that client govern PTY flow control. The parked xterm keeps parsing output and acknowledging it, so it is current when it comes back.

How you verified

  • New tests:
    • terminal-runtime-cache.test.ts: take, park, LRU eviction and evict.
    • ThreadTerminalView.keepalive.test.tsx: unmount and remount reuse one transport and the same xterm element, and call setVisible(false) on park. An exited message while parked disposes the runtime. keepAlive={false} still disposes. An unmount before the async mount finishes creates nothing.
    • terminal-cache-owner.test.ts: close and missing-session eviction.
  • With main's ThreadTerminalView.tsx restored, the keep-alive reuse test fails. With this change, all 4 tests in that file pass.
  • pnpm exec turbo run typecheck --filter=@bb/app passes.
  • Vitest passes in two groups:
    • src/components/thread/terminal, src/hooks/cache-owners and bundle-budget.test.ts: 241 tests.
    • The other test files that import the changed modules: 151 passed, 5 skipped.
  • oxfmt --check and oxlint are clean on the changed files. pnpm exec turbo run build --filter=@bb/app passes, including the React Compiler check.
  • Manual check: the same design, written against desktop-v0.49.0, was built and installed in a 0.49.0 desktop setup connected to a remote server. Returning to a terminal from the Browser tab and from another thread was instant, with no replay, and live output kept arriving while the terminal was hidden. This port to main has passed the automated checks only. It has not been run in the app.
  • An alternative was tried and rejected: hiding the terminal until the replay finished. It removed the visible scroll, but returning to the terminal was just as slow.

Fixes #7724

AGENT GENERATED

@github-actions github-actions Bot added the needs-approval PR author is not on the approved contributor list label Nov 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the pull request, @danteregis. It was closed automatically because it does not meet the requirements below yet. This is an automated check, not a review of the change.

Contributor approval. bb reviews pull requests only from approved contributors, and you are not on the list yet. Join the #contributors channel in Discord (https://discord.gg/kvBU6tJhcJ), post a link to your pull request, and explain what it changes and why. A maintainer then adds you to the list and reopens the pull request.

See CONTRIBUTING.md for details.

@lanjak

lanjak commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thanks for the fix and the analysis in #7724. PR #7801 continues this change: its first commit is this PR's commit, with you as its author.

A review and a browser test of this PR found three items, which #7801 fixes:

The PR body of #7801 has the before and after measurements.

AGENT GENERATED

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

Labels

needs-approval PR author is not on the approved contributor list

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Terminal reconnects and replays its history every time you return to its side-panel tab

2 participants