Skip to content

fix(remote-terminal): stop the reconnect banner after a worktree switch on remote runtimes - #27698

Closed
dexylitol wants to merge 3 commits into
stablyai:mainfrom
dexylitol:fix/remote-pty-parked-retry-27178
Closed

dexylitol wants to merge 3 commits into
stablyai:mainfrom
dexylitol:fix/remote-pty-parked-retry-27178

Conversation

@dexylitol

@dexylitol dexylitol commented Oct 12, 2026 •

Copy link
Copy Markdown

ELI5

When you switch away from a worktree on a remote runtime and come back, its terminals sometimes show "Reconnecting to remote runtime" for seconds to minutes, even though the connection is fine. Clicking Reconnect fixes it instantly. This PR removes the three client-side things that made the terminal wait instead of just reattaching.

What Changed

Before: after a worktree switch the banner showed for 15 s, sometimes ~3 min, and then turned into "Automatic retries stopped" until you clicked Reconnect.
After: when the stream ends, the terminal reattaches in about 1–2 s, and repeated ends never park it.

The stream end itself came from the host: hiding a worktree pauses its remote panes' output, the host dropped that output without advancing its source-range ledger, and the first chunk after reveal failed the ledger's continuity check, so a healthy stream ended as unverifiable. #27546 has since deleted the host-side ledger on main, so hosts built from main no longer end streams this way. Released hosts (1.4.224 and earlier) still do, and other unverifiable ends remain (a failed recovery snapshot, a failed PTY wait), so the client still has to recover quickly:

  1. Client: reattach a handle the host still reports ready (remote-runtime-pty-transport.ts). After an unverifiable end, the pane waited the whole 15 s inventory window for a replacement handle, while the host kept answering that the same handle was ready. It now reuses that handle after a 1 s grace. The exception is a send that is still unanswered and could come back terminal_handle_stale; that case keeps the full wait.
  2. Client: repeated ends slow the reattach instead of stopping it. A third end inside the reuse window switched to require-replacement, which parked the retry until the ~3 min deadline. A new reuse-after-backoff policy reattaches the still-ready handle with growing delays (1 s → 8 s max). Only an end within 10 s of reattach counts as a repeat. A genuinely stale handle (terminal_handle_stale) still uses require-replacement.
  3. Client: an unknown-liveness reattach is scheduled, not parked (first commit). A host that answered without a ready handle left the pane parked for an online / resume event that never fires on a healthy link.

Why

  • An earlier revision also fixed the host-side ledger resync. refactor(rpc): delete host-side terminal source-range acks #27546 deleted that ledger, so I rebased onto main and dropped those commits; only the client fixes remain. They are still needed because clients and hosts update independently, and released hosts keep ending streams this way.
  • I kept the flap guard from fix(terminal): add replacement policies for repeated same-handle stream #12003 instead of removing it. It still rate-limits a handle that ends right after every reattach. It now never gives up on a handle the host says is alive.
  • Same-handle reuse is fenced the same way on both recovery paths (web-terminal and persisted panes): wait out the flap backoff and any unanswered terminal.send, which could still come back terminal_handle_stale.

Linked Issue

Fixes #27178

Visual Proof

Measured on macOS. The client connects to an Orca desktop host on another Mac, which reaches its terminals over SSH. Banner time comes from renderer logs:

Scenario (host 1.4.224) Before With this PR
Switch back to a worktree whose terminal printed while hidden (while true; do date; sleep 2; done, 5+ switches) Banner every time, 15 s Banner every time, 1.0–1.6 s
Same pane ends 3+ times within a minute ~3 min, then "Automatic retries stopped" Reattaches in seconds, never parks

With a host that also had the (now dropped) host-side resync, there was no banner at all. A host built from main should behave the same, since #27546 removed the check that ended the stream, but I have not re-measured that live.

Opening a new remote terminal still flashes the banner for about 0.4 s. Released 1.4.224 does the same, so this PR doesn't cause it.

There is no video. The timings come from renderer logs: temporary diagnostics, not part of this PR, plus a CDP watcher that records when the banner appears and disappears. Times below are relative to the moment the client started recovering from the stream end.

Log excerpts

Before: the host keeps reporting the same handle ready, but the pane waits the whole 15 s window.

+0.000  stream ended → resubscribe {replacementPolicy: "prefer-replacement"}
+0.046  BANNER ON  recovering
+0.096  resubscribe activate answered {handle: "same"}
+0.342  resubscribe list answered {handle: "same"}
        … 12 more polls, every one answered {handle: "same"} …
+14.455 resubscribe list answered {handle: "same"}
+15.026 markHealthy (reattached to the same handle)
+15.121 BANNER OFF

With this PR (host 1.4.224): the same handle is reused after the 1 s grace.

+0.000  stream onEnd {verdict: "unverifiable"}  → resubscribe {replacementPolicy: "prefer-replacement"}
+0.039  resubscribe activate answered {handle: "same"}
+0.118  BANNER ON  recovering
+0.313  resubscribe list answered {handle: "same"}
+0.694  resubscribe list answered {handle: "same"}
+1.415  resubscribe list answered {handle: "same"}  → past the grace, reuse it
+1.625  markHealthy
+1.726  BANNER OFF

Testing

  • I manually tested these changes locally, on macOS on both ends. A dev build of this branch was the client, paired over Tailscale with a 1.4.224 host on another Mac that reaches its worktrees over SSH. I switched worktrees repeatedly, including the always-printing terminal scenario above. (Before the rebase, I also ran a host with the dropped host-side fix; see Visual Proof.)
  • Automated tests added/updated:
    • remote-runtime-pty-transport-sticky-replacement-policy.test.ts: covers the 1 s reuse, the growing delays for real flaps, no growth for ends long after reattach, the same backoff on persisted panes, and no fallback to a handle with an unanswered send when the bounded wait ends.
    • remote-runtime-pty-transport-pending-host-surface-attach.test.ts: an unknown-liveness reattach retries instead of parking.
    • remote-runtime-pty-transport-stale-handle-recovery.test.ts: repeated ends keep reattaching instead of disconnecting. This intentionally replaces the old "flaps until it disconnects" expectation.
  • After rebasing onto main: ran pnpm tc, the remote-runtime transport and terminal multiplex suites (57 files, 347 tests), and pnpm run check:code-quality:changed.

AI Disclosure

Investigation, code, and tests were written with Claude Code (Claude Opus 5.5), and reviewed and tested by me.

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

  • Remote/SSH: this only changes how remote-runtime terminals recover.
  • Compatibility: client-only, no wire change. It helps against both released hosts and hosts built from main.
  • Performance: a flapping pane reattaches less often, not more.
  • Platforms: no platform-specific code.
  • Not addressed here: the ~0.4 s banner when a new remote terminal opens, and [terminal] replay guard released … (undeliverable write pipeline) errors after reattach. Both happen without this PR.

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)

🤖 Generated with Claude Code

@dexylitol
dexylitol marked this pull request as ready for review October 12, 2026 01:55
@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough
📝 Walkthrough

Walkthrough

The transport now backs off before reusing a recently ended handle and waits for in-flight terminal.send requests to settle. When bounded host-session lookup ends without a handle, the transport can schedule another attach or resubscription attempt. Tests cover repeated same-handle recovery, backoff state, and retry when a host surface becomes available.



Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: 🔵 Low · up to beb70

A persisted terminal pane can reconnect to the same handle too quickly after an initial stream end, potentially extending the reconnect banner. The fix is localized; address it before merge if possible.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 18.75% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 16 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed The PR satisfies the coding requirements for #27178. remote-runtime-pty-transport.ts schedules attach and resubscribe retries when liveness is unknown, instead of waiting only for online, resume, or…
Out of Scope Changes check Passed The reviewed changes stay within #27178. The production changes and tests address remote terminal recovery after stream ends, unknown-liveness retry scheduling, ready-handle reuse, repeated-end backof…
Title check Passed The title clearly identifies the primary user-facing change: stopping the reconnect banner after a worktree switch on remote runtimes.
Description check Passed The description is detailed and covers the user impact, implementation changes, rationale, linked issue, testing, compatibility, and limitations. It includes before-and-after measurements, although it…

  • Fix all pre-merge checks with AI
  • 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: 3


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b7e56f68-0110-4afd-b9f5-3183c3a87755
📥 Commits

Reviewing files that changed from the base of the PR and between 9d305f3 and a6f3417883fa6404adf438ea4f38155ad6af08d3.

📒 Files selected for processing (10)
  • src/main/runtime/rpc/methods/terminal/terminal-multiplex-cleanup.ts
  • src/main/runtime/rpc/methods/terminal/terminal-multiplex-frame-delivery.ts
  • src/main/runtime/rpc/methods/terminal/terminal-multiplex-slot-frames.ts
  • src/main/runtime/rpc/methods/terminal/terminal-multiplex-stream-initialization.ts
  • src/main/runtime/rpc/methods/terminal/terminal-stream-types.ts
  • src/main/runtime/rpc/terminal-multiplex-source-range-output-pause.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-pending-host-surface-attach.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-stale-handle-recovery.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport-sticky-replacement-policy.test.ts
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts

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

@dexylitol
dexylitol force-pushed the fix/remote-pty-parked-retry-27178 branch from a6f3417 to 23839f4 Compare October 12, 2026 02:52
dexylitol and others added 3 commits October 12, 2026 11:58
…arking it (stablyai#27178)

A host that answered without a ready handle left the pane parked for an
online/resume trigger that never fires on a healthy link, so the banner
spun for the whole ~3 min deadline until Reconnect.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g it out (stablyai#27178)

After an unverifiable stream end the pane waited the whole 15 s inventory
window for a replacement handle even while the host kept reporting the
same one ready, and a third end within 60 s parked it for the ~3 min
deadline. Reuse the ready handle after a 1 s grace, and treat repeated
ends as a slower reattach rather than a reason to stop retrying. Only an
end within 10 s of reattach counts as a flap, and a pending send that may
still prove the handle stale keeps the full wait.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd backoff (stablyai#27178)

The bounded-wait fallback no longer returns an ended handle with an unanswered
send, and persisted panes now wait out the flap backoff and in-flight sends
before reattaching the same handle, like web-terminal panes do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dexylitol
dexylitol force-pushed the fix/remote-pty-parked-retry-27178 branch from 23839f4 to beb7095 Compare October 12, 2026 04:00
@dexylitol

Copy link
Copy Markdown
Author

Rebased onto main and force-pushed; the PR now contains only the client-side fixes.

#27546 deleted the host-side source-range ledger, which is exactly what this PR's host commits patched (resyncing the ledger after output was dropped while a pane was hidden). On main the host no longer runs that continuity check, so the stream doesn't end on reveal, and the client already repaints dropped output with its hidden-output snapshot restore. The host commits had nothing left to fix, so I dropped them instead of resolving the conflict.

The client commits still apply: released hosts (1.4.224 and earlier, none of which include #27546) still end the stream this way, and other unverifiable ends remain on main. I updated the PR description to match.

@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: 1f8b7648-bc05-4897-823a-2b3d5b36b2f4
📥 Commits

Reviewing files that changed from the base of the PR and between 23839f4d003b20b862bc332b06c093205482d708 and beb7095.

📒 Files selected for processing (1)
  • src/renderer/src/components/terminal-pane/remote-runtime-pty-transport.ts

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

@nwparker

nwparker commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded — the linked issue (#27178) has been resolved on main by #29210 (36344bc1c0). Thanks for the contribution!

@nwparker nwparker closed this Nov 5, 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

2 participants