Skip to content

fix(session-tabs): keep recent worktree streams warm and stop resubscribing live ones - #29241

Merged
nwparker merged 4 commits into
mainfrom
nwparker/ssh-session-tabs-sub-reuse
Oct 16, 2026
Merged

nwparker merged 4 commits into
mainfrom
nwparker/ssh-session-tabs-sub-reuse

Conversation

@nwparker

@nwparker nwparker commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​393 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​393
Prod 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​308 $\color{#cf222e}{\Huge{\mathbf{−}}}$​61 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​247

ELI5

When you click between workspaces on the same SSH machine, Orca used to hang up and redial the machine's "what tabs are open here" feed every time, then reload the answer it already had. Now it keeps the feeds for the last few workspaces you visited open, so switching back just works. It also stops hanging up feeds that were still working whenever a background health check briefly lost and then regained the machine.

What Changed

Before: Every worktree switch on a paired host or SSH host (orcad) closed the per-worktree session.tabs.subscribe stream and opened a new one. Each switch cost one subscribe round trip, an orphan-recovery pass, and a full snapshot re-apply before the panes settled. Separately, every hostContactEpoch bump ("the host answered again") reinstalled both session-tabs streams, the per-worktree one and the all-worktrees one, even when neither had ended. So a flapping status probe forced a resubscribe each time it flapped.

After:

  • warm-worktree-subscriptions.ts keeps the three most recently selected worktree streams open, keyed by host and worktree. Switching A→B→A opens two streams, not three, and A's original stream is never closed. A warm stream is dropped when the host comes back as a new runtime session (connection generation or runtime id moved) or is re-paired, when the window is hidden (so reveal does not resume extra streams), when workspace session readiness drops, and when it is evicted from the three-stream pool.
  • A warm stream for a worktree you left still mirrors that worktree's tabs, but only the selected worktree may seed a first terminal or respawn one after wake. That matches the old behaviour, where such frames never arrived.
  • window-visibility-subscription-parking.ts gains restartEnded(). It restarts, immediately and without waiting for the retry backoff, only the streams that already ended or failed to subscribe. On a hostContactEpoch bump, both effects now call it instead of reinstalling. Live streams are untouched.

Why

The per-worktree stream exists alongside session.tabs.subscribeAll for three reasons: it resumes immediately on window reveal, it is the closure that decides the initial-terminal bootstrap and wake respawn for the selected worktree, and it runs orphan recovery before apply. Keeping it per-worktree and warm keeps all three. Replacing it with local filtering of the global stream would have moved those decisions into the global path, and the global stream resumes staggered behind other hosts.

The epoch edge exists because streams that ended during an outage need reviving (#20359). Since #27098, an ended stream already sits on a retry backoff, so a regained host only needs to skip that wait. A stream that never ended has nothing to recover: transport drops are owned by the shared-control reconnect replay. That is why the epoch now restarts ended streams only. The wire is unchanged: same methods, same params, no new opcodes. Mixed-version peers are unaffected.

I checked whether an unchanged snapshot re-apply itself retires or recreates panes. It does not: the store slices stay reference-equal, and a test pins that. The cost on each switch was the round trip and the recovery pass, not a store rewrite.

Not changed here. These per-switch calls come from other mechanisms:

  • worktree.activate + session.tabs.list (with forced re-accept) from web-runtime-worktree-activation.ts
  • git.status and files.watch re-subscribes
  • files.readDir, repo.hooksCheck, repo.setupScriptImports, git.checkIgnored, github.prForBranch, terminal.inspectProcess

Linked Issue

N/A (maintainer; SSH worktree-switch latency investigation)

Visual Proof

N/A. No visual change: the tabs and panes render identically. The change is fewer round trips per worktree switch, which the tests count.

Testing

  • New src/renderer/src/runtime/web-session-tabs-sync-worktree-switch.test.tsx:

    • A→B→A→B opens one stream per worktree, and A's stream is never unsubscribed. Fails on main.
    • A status-probe flap (unavailable, then verified, epoch +1) with live streams triggers no resubscribe and no unsubscribe. Fails on main.
    • An unchanged snapshot replay, and a switch back, leave tabsByWorktree, terminalLayoutsByTabId, ptyIdsByTabId, unifiedTabsByWorktree and groupsByWorktree reference-equal.
    • A warm stream of a worktree you left never seeds a terminal.
    • A replacement runtime drops warm streams, and they reinstall on reselect.
  • The existing web-session-tabs-sync-reconnect-resubscribe test still passes: streams that ended during an outage are revived when the host answers again.

  • pnpm test src/renderer/src/runtime src/renderer/src/app-shell (280 files) passes, as do pnpm tc and pnpm run check:code-quality:changed.

  • I manually tested these changes locally

  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • SSH and paired hosts: this is the target case.
  • Folder workspaces: keyed by worktree id, the same as before.
  • Web client: uses the same hook.
  • Mobile: has its own client and is unaffected.
  • Cross-platform: no platform code.
  • Wire compatibility: no wire change.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

…ing live ones

Switching worktrees on one SSH host tore down and reinstalled the per-worktree
session.tabs.subscribe stream on every switch (a subscribe round trip, an orphan
recovery pass and a snapshot re-apply). A hostContactEpoch bump also reinstalled
both session-tabs streams even when they never ended.

- Keep the three most recently selected worktree streams warm, keyed by
  connection identity; a new runtime session, re-pair or hidden window drops them.
  Only the selected worktree may seed or respawn a terminal.
- On a hostContactEpoch bump, restart only streams that ended or are waiting on
  the retry backoff; live streams are untouched.
@coderabbitai

coderabbitai Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

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

Next included review available in 17 minutes.

Check out review usage here.

View limit details

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

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4fcd3197-341a-4616-9071-25ef778c5064
📥 Commits

Reviewing files that changed from the base of the PR and between da8edec and 1b3e5d6.

📒 Files selected for processing: 2
  • src/renderer/src/runtime/web-session-tabs-sync-worktree-switch.test.tsx
  • src/renderer/src/runtime/web-session-tabs-sync/warm-worktree-subscriptions.ts

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 39493633-8ec9-4e83-914b-9188eaa52cdc
📥 Commits

Reviewing files that changed from the base of the PR and between dacc376 and da8edec.

📒 Files selected for processing: 4
  • src/renderer/src/runtime/web-session-tabs-sync-worktree-switch.test.tsx
  • src/renderer/src/runtime/web-session-tabs-sync/active-session-subscription.ts
  • src/renderer/src/runtime/web-session-tabs-sync/use-web-session-tabs-sync.ts
  • src/renderer/src/runtime/web-session-tabs-sync/warm-worktree-subscriptions.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.

📝 Walkthrough

The change adds managed subscriptions for selected and recently used worktrees. It adds restartable visibility-parking handles and limits terminal bootstrap and wake-triggered respawn to the selected worktree. The synchronization hook now selects and restarts subscriptions based on runtime, readiness, and host-contact state. Tests cover stream reuse, snapshot-state preservation, terminal-creation suppression, and recovery when the runtime identity stays the same or changes.

Priority: ⬇️ Low

Merge Risk: 🟡 Moderate · up to da8ed

A worktree can remain without its expected terminal after switching away and back. Restore the terminal follow-up on reselection before merging, unless this behavior is explicitly accepted.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: retaining recent worktree streams and avoiding unnecessary resubscriptions for live streams.
Description check Passed The description is detailed and covers the user impact, implementation, rationale, linked-issue exception, visual-proof exception, testing, compatibility considerations, and checklist status. It also …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 443ef322-6279-4e02-a946-f6e7dd070bf8
📥 Commits

Reviewing files that changed from the base of the PR and between 1b1d09c and dacc376.

📒 Files selected for processing: 7
  • src/renderer/src/runtime/web-session-tabs-sync-worktree-switch.test.tsx
  • src/renderer/src/runtime/web-session-tabs-sync/active-session-subscription.ts
  • src/renderer/src/runtime/web-session-tabs-sync/global-session-subscription.ts
  • src/renderer/src/runtime/web-session-tabs-sync/use-web-session-tabs-sync.ts
  • src/renderer/src/runtime/web-session-tabs-sync/warm-worktree-subscriptions.ts
  • src/renderer/src/runtime/window-visibility-subscription-entry.ts
  • src/renderer/src/runtime/window-visibility-subscription-parking.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

}
try {
if (isCurrent() && bootstrap) {
if (isCurrent() && isSelected() && bootstrap) {

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.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Reconsider terminal creation when a warm worktree becomes selected.

If an empty snapshot arrives after the user leaves a worktree, isSelected() suppresses its initial terminal bootstrap. When the user returns, the live stream is reused without another snapshot or bootstrap check. The worktree can remain without its initial terminal indefinitely. The same gap applies to a suppressed wake respawn. Retain the eligible follow-up and reconsider it on selection, without resubscribing.

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