Skip to content

fix(terminal): a WSL bridge pane whose distro went away keeps its tab and cold-restores - #34095

Merged
nwparker merged 3 commits into
mainfrom
nwparker/wsl-bridge-distro-loss-keeps-tab
Oct 26, 2026
Merged

nwparker merged 3 commits into
mainfrom
nwparker/wsl-bridge-distro-loss-keeps-tab

Conversation

@nwparker

@nwparker nwparker commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​254 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​254
Prod 9 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​157 $\color{#cf222e}{\Huge{\mathbf{−}}}$​25 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​132

ELI5

When a WSL distro was shut down or crashed (wsl --terminate, wsl --shutdown), Orca closed every WSL terminal tab that had a shell running in it, as if you had typed exit. The next time you opened Orca those terminals were gone or started fresh, even though their history was still saved. Now Orca treats this the way it already treats its own terminal service crashing: the tabs stay, and their history comes back when you open them.

What Changed

  • Before: a distro terminate exits every live WSL bridge shell. wsl.exe reports that as an ordinary exit code (1), the same code a shell's own exit 1 gives. The renderer treated it as a proven process exit:
  • After: when a WSL bridge session exits with a real code, the daemon adapter first asks whether its distro is still running.
    • Confirmed gone: the exit is reported as host loss (code -1, the same as a daemon crash), and the session's history is left unended. The renderer marks the pane as lost-contact, keeps the tab, and cold-restores it when shown.
    • Distro still running: the exit goes through unchanged, so exit in a shell still closes its tab.
    • Check can't answer (error or timeout): the exit also goes through unchanged.
  • How the check works: isWslBridgeDistroLost in daemon-wsl-bridge-exit.ts calls the existing readConfirmedRunningWslDistros.
    • That helper is the shared wsl --list --running probe. It never boots a distro.
    • It coalesces concurrent reads into one wsl.exe spawn, so a terminate that exits ten panes costs one probe.
    • Only a probe that started after the exit may answer (startedAfter, monotonic). A probe already in flight from before the exit might predate the pane's shell booting the distro, so it would wrongly say "gone". The exit instead waits for one fresh probe chained after it, and every exit in the same wave shares that probe.
    • It's bounded by its own 5 s wsl.exe timeout, and the check adds a 3 s outer bound. Every bridge exit, a user's own exit included, waits on the check before its tab reacts. On timeout the exit goes through unchanged.
  • Closing a kept tab later: the session is already gone from the daemon, so the kill hits SessionNotFound. For a session this adapter itself turned into host loss, an explicit close (no keep-history) now treats that as already stopped and still removes the session's history. Before, the history directory and writer leaked until the worktree was deleted. Any other id's SessionNotFound is rethrown as before, and no history is removed. A legacy daemon after a protocol-bump update shares the history directory and may still run that id. The host-loss mark is cleared when the session is spawned again or closed.
  • Scope: only sessions the adapter knows as WSL bridge sessions, i.e. those with a recorded distro, are affected. Native and SSH sessions skip the check. A spawn that replaced the session while the check ran owns it, and the stale exit is dropped.

Why

  • Evidence: verify-15a (5e). A live bridge pane @@4a01061d exited with code 1 at wsl --terminate, and its tab was closed. On the next launch it got a new session (@@4bd82ce1), while the old checkpoint, with 1,500 lines and cwd deep, sat unused on disk.
  • Reasoning: losing the whole distro is loss of the host, not the shell finishing. The repo rule is that loss of contact is never evidence of death, and Orca already keeps tabs through a daemon crash with the same code.
  • Alternatives considered:
    • Telling the cases apart by exit code alone isn't possible: both are 1.
    • Doing the check in the renderer would need a new IPC round trip on every exit.
    • Checking at the daemon adapter keeps one place, one shared probe, and the existing host-loss path.

Linked Issue

Internal: WSL via orcad, verify-15a 5e.

Visual Proof

N/A. Covered by unit tests; the next verify run repeats 5e. A terminated distro's open terminal stays open and restores, instead of disappearing.

Testing

  • I manually tested these changes locally. Not yet; this needs a Windows box. verify-15b repeats 5e: wsl --terminate, then quit and relaunch, and the open pane should cold-restore its session.
  • Automated tests added/updated:
    • daemon-pty-adapter-wsl-distro-loss.test.ts:

      • terminated distro: the exit is -1 and history is left unended (tab kept);
      • running distro: the normal code-1 exit, so exit still closes the tab;
      • check unavailable: unchanged;
      • many panes exiting at once: all classified from one answer;
      • native pane: never checks;
      • a user later closing a kept tab still removes its history, though the session is gone;
      • closing an id this adapter never lost still rethrows SessionNotFound and removes nothing;
      • a respawn clears the host-loss mark.

      The host-loss and many-panes cases fail without the fix, and I ran them that way.

    • daemon-wsl-bridge-exit.test.ts:

      • three concurrent checks spawn one probe through the real coalescing in resolveRunningWslDistros;
      • case-insensitive matching;
      • a hung check times out to "unchanged";
      • a failed check is "unchanged";
      • a probe already in flight before the exit is never trusted: one fresh probe follows it, and a running distro keeps exit an exit.
  • pnpm tc:node, the daemon adapter suites and check:code-quality:changed pass. CI runs full lint and tests.

EDR / process cost: each WSL bridge exit wave costs one wsl.exe --list --running spawn, through the existing shared probe. It's coalesced across panes, never boots a distro, and runs no interpreter. If a background probe was already running, one more spawn is chained after it. This is the same command Orca already runs for WSL host liveness and background-work gating.

User risk: this affects every Windows user with WSL bridge terminals. After wsl --shutdown, wsl --terminate or a distro crash, their open WSL terminals now stay open as disconnected and restore their history when shown, instead of closing. A user's own exit now waits up to 3 s (usually about 100 ms) for the check before the tab closes. Each WSL bridge exit costs one shared wsl --list --running, coalesced across panes and never booting a distro. Typing exit in a running distro closes the tab as before.

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

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)

… and cold-restores

wsl --terminate (or a shutdown or crash) exits every live bridge pane with an
ordinary code, so Orca closed those tabs as if the shell had exited, and the
next launch opened new sessions instead of restoring the old ones. A proven
exit of a WSL bridge session now checks the shared, coalesced
wsl --list --running (never boots, bounded); if the distro is confirmed gone
it is host loss: code -1, history left unended, tab kept, exactly as after a
daemon crash. A running distro or an unanswered check leaves the exit as is,
so  still closes the tab. Native and SSH sessions are untouched.

Co-Authored-By: Claude <[email protected]>
@nwparker
nwparker marked this pull request as ready for review October 26, 2026 21:37
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

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 5 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
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c61d1452-b812-4cbf-9a95-c2666c82efbf
Commits

Reviewing files that changed from the base of the PR and between 2e67ba5 and 86f12d2.

Files selected for processing: 11
  • src/main/daemon/daemon-pty-adapter-test-harness.ts
  • src/main/daemon/daemon-pty-adapter-wsl-distro-loss.test.ts
  • src/main/daemon/daemon-pty-adapter.ts
  • src/main/daemon/daemon-pty-runtime-state.ts
  • src/main/daemon/daemon-pty-session-control.ts
  • src/main/daemon/daemon-pty-spawn-result.ts
  • src/main/daemon/daemon-wsl-bridge-exit.test.ts
  • src/main/daemon/daemon-wsl-bridge-exit.ts
  • src/main/wsl-background-distro-gate.ts
  • src/main/wsl-running-distro-cache.ts
  • src/main/wsl.ts
  • 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.

nwparker and others added 2 commits October 26, 2026 15:08
… exit; close a kept tab's history

Review of the WSL bridge distro-loss check:
- A probe already in flight from before the exit can predate the distro's
  boot, so it could call a user's own quick exit host loss. Only a probe that
  started after the exit answers; one fresh probe is chained after an older
  in-flight one and shared by the whole wave.
- The outer bound drops to 3 s: every bridge exit waits on it.
- Closing a kept tab later hit SessionNotFound before removeSession, leaking
  its history; an explicit close now treats that as already stopped.

Co-Authored-By: Claude <[email protected]>
…se as already gone

Swallowing SessionNotFound for any id could remove the history of a session a
legacy daemon (sharing the history dir) still runs. The adapter now records
the sessions it turned into host loss, clears them on respawn or close, and
swallows SessionNotFound on an explicit close only for those.

Co-Authored-By: Claude <[email protected]>
@nwparker
nwparker merged commit a97c7ab into main Oct 26, 2026
52 checks passed
nwparker added a commit that referenced this pull request Oct 27, 2026
…on across a relaunch

#34095 turns a bridge pane's exit into host loss (code -1) when its distro is
gone, so the window keeps the tab and the history stays unended. But main's
exit handling still retired the pane's surface from the saved workspace
session, as for any provider-observed exit, so the next launch found no
session id for the tab and started a fresh shell instead of cold-restoring.

The adapter now labels that exit hostLost, the pty listener forwards it, and
the runtime keeps the surface, as it already does for a sleep or an SSH loss.

Co-Authored-By: Claude <[email protected]>
nwparker added a commit that referenced this pull request Oct 27, 2026
…on across a relaunch (#34200)

#34095 turns a bridge pane's exit into host loss (code -1) when its distro is
gone, so the window keeps the tab and the history stays unended. But main's
exit handling still retired the pane's surface from the saved workspace
session, as for any provider-observed exit, so the next launch found no
session id for the tab and started a fresh shell instead of cold-restoring.

The adapter now labels that exit hostLost, the pty listener forwards it, and
the runtime keeps the surface, as it already does for a sleep or an SSH loss.

Co-authored-by: Claude <[email protected]>
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