Skip to content

fix(terminal): a WSL bridge pane keeps its session when its distro is booted again before the check - #35050

Merged
nwparker merged 4 commits into
mainfrom
nwparker/wsl-bridge-boot-id
Oct 28, 2026
Merged

nwparker merged 4 commits into
mainfrom
nwparker/wsl-bridge-boot-id

Conversation

@nwparker

@nwparker nwparker commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 5 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​440 $\color{#cf222e}{\Huge{\mathbf{−}}}$​10 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​430
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​248 $\color{#cf222e}{\Huge{\mathbf{−}}}$​32 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​216

ELI5

When a WSL distro is shut down (for example wsl --terminate), Orca keeps the terminal panes that were running in it and restores them later with their history. That only worked if Orca's check ran before anything started the distro again, and if wsl.exe answered within 3 seconds. Otherwise the pane was treated as if its shell had exited, and the next launch opened a new empty terminal. Orca now recognises the distro was restarted, and asks once more when wsl.exe is slow.

What Changed

  • Before: a bridge pane's distro is terminated, then something boots it again before Orca's wsl --list --running answers (verify-15b saw a file-explorer read do this 0.3 s later). Or the list takes longer than 3 s. Either way the pane's history is marked ended, and after a relaunch it opens a new session with no scrollback, at the project root. Agents running in bridge panes (p3) are hit too.
  • After: both cases count as the distro being lost. The pane keeps its tab and history, and after a relaunch it restores its own session. A user's own exit still closes the tab as before.
  • Mechanism (DaemonPtyAdapter.finishWslBridgeExit → classifyWslBridgeExit):
    • Boot id. WslBridgeBootIdentity remembers each bridge pane's distro boot id (/proc/sys/kernel/random/boot_id) from its spawn. When a bridge exits and the running list says "running", it reads the id again. A different id means the distro was terminated and started again, so the exit is host loss (rebooted). The same id is a real exit.
      • Read once per distro boot. The cache is shared by that distro's panes, and reused only between exits. Any bridge exit of the distro drops it synchronously, as the exit event arrives and before its verdict runs. So a pane opened during another pane's 3–10 s verdict reads the new boot. The cache is also dropped when the distro's last pane goes away.
      • New incarnation, new id. A spawn or respawn of a session with a new incarnation id takes the distro's current id. A reattach of the same incarnation keeps its id.
      • Stale exit. An exit of an incarnation the session already replaced (a respawn that beat the dead process's exit event) also drops the cache. It re-reads the boot id of the session's current incarnation, because that respawn took the cached, possibly old, id.
      • Burst of exits. A terminate exits every pane, and those exits share one read of the current id.
      • Fallback. An id that can't be read on either side keeps today's behaviour.
    • Never boots a distro (E9). Every read goes through isWslDistroRunningForBackgroundWork, so it only runs into a distro a fresh running list says is up. The read is wsl.exe -d <distro> --exec cat /proc/sys/kernel/random/boot_id, built with buildWslExecArgs and spawned with runProcess from @orca/process-host. There is no shell, so no rc files or banner. Off Windows the gate answers "not running", so nothing is read.
    • Slow list. When the first running-list read doesn't answer, checkWslBridgeDistro retries once past the failure backoff (bypassFailureBackoff), within a 10 s total budget (the first read keeps its 3 s).
    • The cache lives on each adapter, injected through DaemonPtyAdapterOptions.wslBridgeBootIdentity. It is not a module global.
    • The spawn bookkeeping moved into DaemonPtySpawnRequest.noteSpawnWslDistro, so daemon-pty-spawn-result.ts stays under max-lines.

Why

  • A boot id is proof the distro restarted. Other signals (a relay link dropping, a running-list poll seeing a stop) are hints that can fire for other reasons, and the 1 s poll missed a reboot in verify-15b.
  • The read only runs into a running distro, through the same gate other background reads use. The remaining gap is the distro stopping in the milliseconds between that list and the read. That is the same window every gated background read on main already accepts.
  • Cost:
    • Boot id: about one wsl.exe per distro boot at the first bridge spawn, plus one shared read per exit burst the list calls "running".
    • This includes a user's own exit in a running distro: it adds one boot-id read (usually 0.2–1 s, at most 5 s) before the tab closes.
    • Not skipped for exit code 0. Skipping would only be safe if a terminate can never yield 0, and the only evidence on record is one verify-15a exit with code 1. The verdict log now names the code ([pty] WSL bridge exit: … (distro …, code N, probe …)), so the next verify run can confirm what a terminate yields before such a skip is added.
    • Retry: a second running-list read only when the first can't answer.
  • Alternatives considered:
    • Treating an unanswered list as loss. A wedged wsl.exe would then keep the tab of a genuine exit.
    • Reading the boot id per pane. That costs a wsl.exe per pane instead of per distro boot.
  • The other path checked, "terminate while Orca is quit", already restores on main: nothing marks the history ended without the app. It is now covered by two regression tests.

Flag-off: not behind the WSL hosts gate. This is WSL bridge-pane behaviour (projects opened from \\wsl.localhost\…), and it is the same with the gate on or off. Native, SSH and hosted-WSL panes are unaffected.

User risk: low.

  • The read runs only for WSL bridge panes, only into a distro a fresh list confirms is running, and an unreadable id falls back to today's behaviour.
  • The visible trade-off: when wsl.exe is wedged, a user's own exit in a bridge pane closes its tab up to 10 s later instead of 3.

SSH: the daemon adapter is shared, but this path only runs for sessions with a WSL distro recorded at spawn. SSH terminals run on the remote host and never enter it, so SSH behaviour is unchanged.

Linked Issue

Internal: flag-on checklist 5e hardening (verify-15b failures; residual races after #34095/#34200).

Visual Proof

N/A. This is terminal restore logic. The effect is the existing restored pane instead of a new empty one.

Testing

  • Not yet re-run on awin (5e: terminate, then quit and relaunch, with a booter racing the check).
  • Automated:
    • daemon-pty-adapter-wsl-distro-loss-relaunch.test.ts (new; real DaemonServer, history on disk):
      • A raced reboot (new boot id) and an unanswered-then-lost list each restore the pane after a relaunch. Both fail on main.
      • Two regressions: a terminate while Orca is quit restores, whether the daemon kept running or exited with its last session.
    • daemon-pty-adapter-wsl-distro-loss.test.ts: a new boot id is host loss; the same boot id stays a normal exit.
    • Review fixes:
      • Pane A exits and pane C opens during A's verdict: C's same-boot exit closes normally (adapter and unit tests). The adapter test fails without the synchronous drop.
      • The same session respawned into a rebooted distro takes the new id, and its real exit is an exit.
      • A reattach of the same incarnation keeps its id.
      • A burst of exits shares one current read.
      • Case 1b: a respawn that beat its old incarnation's exit re-reads its id. It and a later pane both close normally on their own exit; the test fails with only the cache drop.
      • The relaunch suite waits on the exit event, not the clock (CI's ast-grep no-real-time-wait rule).
    • daemon-wsl-bridge-boot-identity.test.ts (new):
      • One read per distro while its panes live.
      • Re-read after an exit or after the last pane goes away.
      • Unreadable or failing reads fall back.
      • Never read when the gate says not running.
    • daemon-wsl-bridge-exit.test.ts:
      • The retry uses bypassFailureBackoff.
      • A hung first read leaves the retry only what remains of the budget.
      • The boot id is asked about only when the list says "running".
    • wsl-distro-boot-id.test.ts (new): the argv is --exec cat … with no shell; a failure, a timeout or non-id output gives null.
  • pnpm tc, oxlint and pnpm run check:code-quality:changed pass. src/main/daemon, src/main/wsl and src/main/ipc/pty: 5,129 passed (one unrelated test, pty-runtime-hidden-at-spawn-mark, failed once under load and passes 3/3 alone). The ast-grep gate passes.

AI Disclosure

Written with Claude Code.

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.

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)

nwparker and others added 2 commits October 28, 2026 04:30
… booted again before the check

A bridge exit that the running list calls 'running' is now host loss when the distro's boot id
changed since the pane spawned (something booted it again first), and an unanswered list is asked
once more before the exit stays an exit. Boot ids are read once per distro boot, only into a
distro the background-work gate confirms running, so the read never boots one.

Co-Authored-By: Claude <[email protected]>
…incarnation takes the current one

Reviews of #35050: a pane spawned during another pane's exit verdict, or the same session
respawned into a rebooted distro, kept the old boot's id, so its own later exit read as
'rebooted' and its tab never closed. A burst of exits now shares one read of the current id,
and the verdict log names the exit code.

Co-Authored-By: Claude <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
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: 0fd8b56e-25a4-4a9b-8d38-1660e20addca
Commits

Reviewing files that changed from the base of the PR and between b2d582c and 50e8f52.

Files selected for processing: 6
  • src/main/daemon/daemon-pty-adapter-wsl-distro-loss.test.ts
  • src/main/daemon/daemon-pty-adapter.ts
  • src/main/daemon/daemon-pty-spawn-request.ts
  • src/main/daemon/daemon-pty-spawn-result.ts
  • src/main/daemon/daemon-wsl-bridge-boot-identity.test.ts
  • src/main/daemon/daemon-wsl-bridge-boot-identity.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 daemon now reads and tracks WSL distro boot IDs. Bridge exit checks retry unanswered running-distro reads within a total time limit. When a distro is running under a different boot ID, the adapter classifies the exit as a host loss and preserves the tab. Tests cover boot identity tracking, exit classification, and cold restoration after bridge or daemon exit.

Priority: Normal

Merge Risk: Low · up to 50e8f

A later pane may lose reboot detection after an unreadable boot-ID result, causing its tab to close rather than be preserved. This is a bounded fallback risk that can be addressed with follow-up.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: preserving WSL bridge-pane sessions when the distro restarts before the exit check.
Docstring Coverage Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
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.
Description check Passed The description follows the required structure and provides clear ELI5, change, rationale, testing, risk, compatibility, AI disclosure, and checklist details. The Linked Issue section names an interna…
Full details: Docstring Coverage

Explanation

Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 5 functions across 13 files.

Full details: Description check

Explanation

The description follows the required structure and provides clear ELI5, change, rationale, testing, risk, compatibility, AI disclosure, and checklist details. The Linked Issue section names an internal checklist but does not include an actual issue link, and several final validation checkboxes remain unchecked; these are minor completeness gaps because the description is otherwise detailed and relevant.

Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
📝 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: 3


Review info
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: 9332385a-42cd-4e5e-a0c4-e6f11d402163
Commits

Reviewing files that changed from the base of the PR and between f59bd43 and b2d582c.

Files selected for processing: 13
  • src/main/daemon/daemon-pty-adapter-test-harness.ts
  • src/main/daemon/daemon-pty-adapter-wsl-distro-loss-relaunch.test.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-spawn-request.ts
  • src/main/daemon/daemon-pty-spawn-result.ts
  • src/main/daemon/daemon-wsl-bridge-boot-identity.test.ts
  • src/main/daemon/daemon-wsl-bridge-boot-identity.ts
  • src/main/daemon/daemon-wsl-bridge-exit.test.ts
  • src/main/daemon/daemon-wsl-bridge-exit.ts
  • src/main/wsl/wsl-distro-boot-id.test.ts
  • src/main/wsl/wsl-distro-boot-id.ts

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

Comment on lines +37 to +41
let bootId = this.byDistro.get(key)
if (!bootId) {
bootId = this.read(distro)
this.byDistro.set(key, bootId)
}

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.

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

sed -n '1,120p' src/main/daemon/daemon-wsl-bridge-boot-identity.ts
sed -n '1,105p' src/main/daemon/daemon-wsl-bridge-boot-identity.test.ts

Repository: stablyai/orca

Length of output: 5875


Evict a null boot-ID read so later spawns retry.

noteSpawn caches the in-flight read in byDistro and keeps that promise after it resolves to null. read() resolves to null when isRunning returns false, when readBootId returns null, or when either call throws.

While the cached null remains, each later noteSpawn for the distro reuses it. Those panes have no spawn identity, so rebootedSinceSpawn returns false for them and a reboot closes the tab instead of keeping the session. The cache clears only on a later exit that calls rebootedSinceSpawn for that distro, or when the last pane is forgotten.

An unreadable ID proves nothing. Evict a null result so the next spawn reads again.

♻️ Proposed fix
--- "a/src/main/daemon/daemon-wsl-bridge-boot-identity.ts"
+++ "b/src/main/daemon/daemon-wsl-bridge-boot-identity.ts"
@@ -34,11 +34,18 @@
     if (current?.distro === key) {
       return
     }
     let bootId = this.byDistro.get(key)
     if (!bootId) {
-      bootId = this.read(distro)
-      this.byDistro.set(key, bootId)
+      const read = this.read(distro)
+      bootId = read
+      this.byDistro.set(key, read)
+      // An unreadable id proves nothing; let a later spawn read again.
+      void read.then((id) => {
+        if (id === null && this.byDistro.get(key) === read) {
+          this.byDistro.delete(key)
+        }
+      })
     }
     this.bySession.set(sessionId, { distro: key, bootId })
   }
 
Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let bootId = this.byDistro.get(key)
if (!bootId) {
bootId = this.read(distro)
this.byDistro.set(key, bootId)
}
let bootId = this.byDistro.get(key)
if (!bootId) {
const read = this.read(distro)
bootId = read
this.byDistro.set(key, read)
// An unreadable id proves nothing; let a later spawn read again.
void read.then((id) => {
if (id === null && this.byDistro.get(key) === read) {
this.byDistro.delete(key)
}
})
}

Minor · Functional Correctness · Quick win

Supported by static analysis

Script executed:

sed -n '1,120p' src/main/daemon/daemon-wsl-bridge-boot-identity.ts
sed -n '220,265p' src/main/daemon/daemon-pty-adapter.ts
sed -n '75,105p' src/main/daemon/daemon-pty-spawn-result.ts

Repository: stablyai/orca

Length of output: 6287


Script executed:

set -o pipefail
printf '%s\n' '--- references ---'
rg -n -F --glob 'src/main/daemon/**/*.ts' -e 'noteSpawn(' -e 'rebootedSinceSpawn(' -e 'forget(' -e 'finishWslBridgeExit' -e 'classifyWslBridgeExit' -e 'wslBridgeBootIdentity' src/main/daemon || test "$?" -eq 1
printf '%s\n' '--- adapter outlines ---'
ast-grep outline src/main/daemon/daemon-pty-adapter.ts --view expanded
printf '%s\n' '--- relevant adapter blocks ---'
sed -n '150,340p' src/main/daemon/daemon-pty-adapter.ts
sed -n '340,470p' src/main/daemon/daemon-pty-adapter.ts
printf '%s\n' '--- classifier ---'
rg -n -F --glob 'src/main/daemon/**/*.ts' -e 'function classifyWslBridgeExit' -e 'export async function classifyWslBridgeExit' src/main/daemon
sed -n '1,220p' src/main/daemon/daemon-wsl-bridge-exit.ts 2>/dev/null || true
printf '%s\n' '--- identity tests and adapter tests ---'
rg -n -F --glob 'src/main/**/*.test.ts' -e 'WslBridgeBootIdentity' -e 'noteSpawn' -e 'rebootedSinceSpawn' -e 'finishWslBridgeExit' -e 'rebooted' src/main/daemon src/main/wsl src/main/ipc/pty || test "$?" -eq 1

Repository: stablyai/orca

Length of output: 22139


Script executed:

set -o pipefail
rg -n -F --glob 'src/main/daemon/**/*.ts' -e 'noteSpawn(' -e 'rebootedSinceSpawn(' -e 'forget(' -e 'finishWslBridgeExit' -e 'classifyWslBridgeExit' -e 'wslBridgeBootIdentity' src/main/daemon || test "$?" -eq 1
sed -n '150,470p' src/main/daemon/daemon-pty-adapter.ts
rg -n -F --glob 'src/main/daemon/**/*.ts' -e 'function classifyWslBridgeExit' -e 'export async function classifyWslBridgeExit' src/main/daemon
rg -n -F --glob 'src/main/**/*.test.ts' -e 'WslBridgeBootIdentity' -e 'noteSpawn' -e 'rebootedSinceSpawn' -e 'finishWslBridgeExit' -e 'rebooted' src/main/daemon src/main/wsl src/main/ipc/pty || test "$?" -eq 1

Repository: stablyai/orca

Length of output: 17296


Script executed:

rg -n -C 12 -F --glob 'src/main/daemon/daemon-pty-adapter.ts' -e 'noteSpawn(' -e 'finishWslBridgeExit' -e 'wslBridgeBootIdentity' src/main/daemon/daemon-pty-adapter.ts; rg -n -C 10 -F --glob 'src/main/daemon/daemon-pty-adapter.ts' -e 'finishWslBridgeExit' -e 'finishExit(sessionId' src/main/daemon/daemon-pty-adapter.ts

Repository: stablyai/orca

Length of output: 4870


Script executed:

sed -n '205,250p' src/main/daemon/daemon-pty-spawn-request.ts; sed -n '1,100p' src/main/daemon/daemon-wsl-bridge-boot-identity.test.ts; sed -n '190,225p' src/main/daemon/daemon-pty-runtime-state.ts

Repository: stablyai/orca

Length of output: 6054


Invalidate the distro cache when the bridge exit is received.

finishWslBridgeExit waits for the running-list check before rebootedSinceSpawn invalidates byDistro. A new pane can spawn during that wait and receive the older pane's successful boot-ID promise. Its normal exit then compares the stale ID with the new boot ID, returns rebooted, and preserves the tab.

Invalidate the distro cache at exit dispatch time. This preserves the existing one-read-per-distro behavior until an exit starts and keeps existing panes’ stored identities unchanged.

Suggested fix
         if (wslDistro && isProvenProcessExit(event.payload.code)) {
           const exitAt = performance.now()
+          this.wslBridgeBootIdentity.invalidate(wslDistro)
           void this.finishWslBridgeExit(event.sessionId, event.payload, wslDistro, exitAt)
           return
         }
   noteSpawn(sessionId: string, distro: string): void {
     const key = distroKey(distro)
     const current = this.bySession.get(sessionId)
@@
     this.bySession.set(sessionId, { distro: key, bootId })
   }

+  invalidate(distro: string): void {
+    this.byDistro.delete(distroKey(distro))
+  }
+
   /** After a bridge exit the running list answered 'running': has the distro booted again since? */

@nwparker
nwparker merged commit 4916c09 into main Oct 28, 2026
63 of 64 checks passed
nwparker added a commit that referenced this pull request Oct 28, 2026
…elaunch test waits on the reap (#35133)

Follow-up to #35050 review: a pane spawned after a running list saw its distro stop, but before
any bridge exit reached the adapter, could take the old boot's id. The identity now drops the
distro's cache on a stop observation, listening only while bridge panes live. The relaunch
suite waits on the daemon reaping the session rather than assuming it is synchronous.

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