fix(terminal): prevent reconnect from killing live daemon sessions
Summary
- Separate explicit user terminal closes from reconnect/lifecycle observations with the additive
session.tabs.closeLifecycleRPC. - Require exact publication, terminal incarnation, runtime environment, ownership, liveness, and intent evidence before lifecycle retirement; missing or incompatible evidence keeps and audits instead of killing.
- Preserve legacy daemon discovery and warm PTY reattachment while preventing stale mirrors from routing destructive kills across surviving daemon generations.
- Add a deterministic native-Windows v21-v25 named-pipe harness covering production desktop discovery, repeated reconnects, client relaunch, simultaneous clients, remote-profile requests, exact root/descendant liveness, and bounded
shutdown-dispose-failedfencing.
Addresses #9749. The complete issue/PR investigation and call-flow reconstruction are included in docs/reference/plans/2026-07-21-windows-daemon-generation-safety-investigation.md.
Screenshots
No visual change.
Testing
- [ ]
pnpm lint— changed-fileoxlintand code formatting pass; the repository-wide switch-exhaustiveness lane has pre-existing unmatched cases inskill-freshness-group.tsxoutside this diff. - [x]
pnpm typecheck - [ ]
pnpm test— focused production/RPC/renderer coverage passes 948/949; the sole failure is the existing Windows/tmpversus\tmpbadgeColordedupe expectation in untouched code. - [ ]
pnpm build— fresh Electron Vite and paired web-client builds pass; the full native/package build was not run. - [x] Added or updated high-quality tests that would catch regressions, or explained why tests were not needed
Focused evidence:
- Native Windows five-generation reconnect oracle: 2/2 passed in 99.1s; v21-v25 each accepted 16 reconnect hellos, all ten PTY roots and ten descendants survived six lifecycle attempts per stale mirror, and zero
session-killedevents occurred. - The same oracle passed with a fresh Electron E2E build in 137.1s; a separate 25-burst stress run passed.
- Practical Electron clean quit/relaunch with a live daemon-backed terminal: 1/1 passed in 24.0s with restored output and working keyboard/direct PTY input.
- Mixed-version preservation/retirement: 2/2 passed; non-empty v22 remained reattachable while empty v24 retired.
- Daemon discovery/adoption/retirement suites: 224 passed, 3 skipped in the final broad lane; the focused changed scanner lane passed 133/133.
- Remote-runtime, real E2EE WebSocket, shared-control, SSH-provider, WSL-context, and PTY transport suites passed.
- Reliability-gate validator, max-lines ratchet, changed-code
oxfmt --check, changed-codeoxlint, andgit diff --checkpassed.
AI Review Report
Three full-diff review rounds ran against merge base 937a2015e.
Round 1 found and fixed four destructive-action safety issues: lifecycle retirement could still reach renderer/process teardown, unavailable PTY inventory was treated as death, close intent could cross runtime profiles, and the Windows harness allowed an unsafe PID-start tolerance. An elegance pass also removed a cached capability result that could outlive server replacement. Round 2 was clean. After strengthening the harness to call production desktop discovery, round 3 found and fixed a Windows socket-mock defect in scanner tests; the subsequent full-diff review was clean.
The review explicitly checked macOS, Linux, and Windows compatibility; legacy protocol behavior; Windows named pipes and process identity; SSH, WSL, remote-runtime, sleeping/reattachable sessions, and multi-client behavior; path handling; Electron lifecycle behavior; cleanup ownership; polling/listener/handle risks; and reconnect-storm performance. Platform-specific fixture behavior is runtime-gated, and production adds no PowerShell/CIM hot path, polling loop, listener, subprocess, or Job Object implementation.
Security Audit
The changed RPC is a destructive-action boundary. Lifecycle requests require typed provenance plus exact host publication and terminal-incarnation evidence, fail closed on missing/access-denied liveness, and never fall back to the legacy destructive method on an old server. Explicit user closes retain the existing destructive path.
Logging records client kind, connection/request identity, reason, publication, and allow/refusal decision without terminal contents, authentication tokens, pairing secrets, or environment data. No dependency, authentication primitive, shell-command, Git command, or production process-enumeration change was added. The Windows harness uses guarded temporary roots and exact PID/start identities, refuses cleanup outside its fixture namespace, and left no process, pipe, handle, or temp-directory residue.
Notes
- This intentionally does not retire non-empty legacy daemon generations. Cross-profile ownership and generation handoff remain #9138/#9229.
- Windows descendant reaping after an authorized kill remains #9704/#9752; this PR does not duplicate Job Object work.
- Live Docker SSH and WSL runs were unavailable on this host. Deterministic SSH/WSL/remote-server coverage passed, and the strongest end-to-end evidence is native Windows as required.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 27d735d3-4ba5-48f1-a8b9-5c56febc1cf6
📥 Commits
Reviewing files that changed from the base of the PR and between 7770f07b08e01f5286043a2964377eff881df607 and 20e6a5aa6de6a83d1c2c82bc3d443e3bf574a1a8.
📒 Files selected for processing (13)
config/reliability-gates.jsoncdocs/reference/plans/2026-07-21-windows-daemon-generation-safety-investigation.mdmobile/app/h/[hostId]/session/[worktreeId].tsxsrc/main/runtime/orca-runtime.test.tssrc/main/runtime/orca-runtime.tssrc/main/runtime/rpc/methods/session-tabs-schemas.test.tssrc/main/runtime/runtime-rpc.tssrc/renderer/src/runtime/web-runtime-session.test.tssrc/renderer/src/runtime/web-runtime-session.tstests/e2e/fixtures/daemon-generation-fixture-contract.tstests/e2e/fixtures/daemon-generation-reconnect-client.tstests/e2e/fixtures/daemon-generation-runtime-close.tstests/e2e/helpers/daemon-generation-safety-fixtures.ts
🚧 Files skipped from review as they are similar to previous changes (9)
- src/main/runtime/runtime-rpc.ts
- tests/e2e/fixtures/daemon-generation-runtime-close.ts
- mobile/app/h/[hostId]/session/[worktreeId].tsx
- src/main/runtime/rpc/methods/session-tabs-schemas.test.ts
- src/renderer/src/runtime/web-runtime-session.test.ts
- src/main/runtime/orca-runtime.test.ts
- tests/e2e/fixtures/daemon-generation-reconnect-client.ts
- src/main/runtime/orca-runtime.ts
- config/reliability-gates.jsonc
📝 Walkthrough
Walkthrough
Adds lifecycle-aware session-tab close RPCs with explicit reasons, publication epochs, terminal incarnation handles, and host-side liveness adjudication. Renderer close-intent tracking is scoped by environment and worktree, with replay and snapshot freshness handling. Daemon initialization supports isolated legacy history paths. New Windows-focused Playwright fixtures exercise multi-generation reconnect survival, bounded disposal failure, process liveness, and cleanup behavior. Reliability-gate documentation records the investigation, evidence, criteria, and validation gaps.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 5.19% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | ✅ Passed | The title clearly matches the main change: preventing reconnect/lifecycle flows from killing live daemon sessions. |
| Description check | ✅ Passed | The description covers all required template sections and includes testing, review, security, screenshots, and platform notes. |
| 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
📝 Generate docstrings
- [ ] Create stacked PR
- [ ] Commit on current branch
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.
Comment @coderabbitai help to get the list of available commands.
Related to #9952.