Repository navigation
fix(daemon): self-heal write routing for undiscovered legacy sessions - #7456
brennanb2025 wants to merge 1 commit into
Conversation
DaemonPtyRouter fans out reads (onData/hasPty/listProcesses) across every adapter, but routes writes and resizes via a sessionAdapters map that is populated only by the one-shot discoverLegacySessions()/reconcile/spawn boot passes. When a surviving previous-protocol daemon owns a session that boot discovery missed (e.g. a Windows named-pipe/startup-order race, widened by the .perf startup reordering), adapterFor() fell back to `this.current` — so output kept streaming (reads fan out) while every keystroke was silently dispatched to the wrong daemon and dropped. Symptom: adopted terminals stream live output but accept no input after a protocol-bumping update. Make adapterFor() self-heal: on a routing-map miss, ask each adapter whether it owns the pty (synchronous Set lookup), cache the winner, and route there; current-first precedence keeps the safe default. This mirrors the self-healing providerFor() already used by DegradedDaemonPtyProvider. Adds regression tests covering the undiscovered-legacy-session route and the no-owner fallback.
📝 WalkthroughWalkthroughCompact Summary: The change modifies Related PRs: None identified. Suggested labels: bug, daemon, tests Suggested reviewers: None identified. Poem 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/daemon/daemon-pty-router.test.ts (1)
121-147: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winConsider a test for current-first precedence tie-break.
Both new tests exercise mutually-exclusive ownership (legacy-only or no-owner). The PR explicitly states current-first precedence is preserved during self-heal, but no test exercises the case where both
currentand alegacyadapter reporthasPty(id) === truefor the same session — the scenario that actually proves iteration order (allAdapters()=[current, ...legacy]) resolves ties correctly.✅ Suggested additional test
+ it('prefers current over legacy when both report ownership of the same session', () => { + const current = createAdapter('current', ['shared-session']) + const legacy = createAdapter('legacy', ['shared-session']) + const router = new DaemonPtyRouter({ current, legacy: [legacy] }) + + router.write('shared-session', 'input\n') + + expect(current.write).toHaveBeenCalledWith('shared-session', 'input\n') + expect(legacy.write).not.toHaveBeenCalled() + })
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f8b3334d-bc34-44e3-9127-29c4c7968259
📒 Files selected for processing (2)
src/main/daemon/daemon-pty-router.test.tssrc/main/daemon/daemon-pty-router.ts
|
Holding note: main has reverted the entire update-survival chain this PR belongs to (#7505 backs out #7421→#7499 after three consecutive field regressions: rc.5 console flashing, rc.6 inert fix, rc.7 daemon startup failure). Please keep this unmerged — the routing it self-heals only arises when a daemon survives an update, which no longer happens on the reverted baseline. A future attempt will come as a fresh design with packaged-installer E2E validation before release. |
|
Closing as superseded. The route-less fallback this PR self-heals is being removed at its root: the replacement fix keeps an unreachable previous daemon version in the session census as unverifiable instead of dropping it, and sends kills and writes through the resolver rather than defaulting to the current daemon. |
Problem
After an update that bumps the daemon protocol version while a previous-protocol daemon survives (holding live terminal sessions), adopted terminals stream live output but accept no keyboard input. Repro'd in production going
v1.4.124-rc.1(protocol v18) →v1.4.124-rc.2.perf(protocol v19): the old v18 daemon kept running with the codex/pwsh PTYs, output continued to stream, but every keystroke vanished.Root cause
DaemonPtyRoutertreats reads and writes asymmetrically:onData,hasPty,listProcesses) fan out across all adapters (current + legacy).adapterFor(), which consulted asessionAdaptersmap populated only by the one-shot boot passes (discoverLegacySessions()/reconcileOnStartup()/spawn()).When boot discovery misses a legacy-owned session — e.g. a Windows named-pipe / startup-ordering race, widened by the
.perfstartup reordering —adapterFor()fell back tothis.current(the new daemon). Result: reads fan out so output still streams andhasPty/writeAcceptedstill report healthy, masking that every write was dispatched to the wrong daemon and dropped.This is why the symptom is specifically frozen input with live output, and why it only bites on a protocol-bumping update where an old daemon is adopted as a legacy adapter.
Fix
Make
adapterFor()self-heal on a routing-map miss: ask each adapter whether it owns the pty (hasPty, a synchronousSetlookup), cache the winner insessionAdapters, and route there. Current-first precedence preserves the safe default. This mirrors the self-healingproviderFor()already used byDegradedDaemonPtyProvider— the same lesson, back-ported to the router.Scope is deliberately limited to write/resize routing;
spawn()is left untouched.Verification
discoverLegacySessions()never ran) — asserts writes/resizes reach the legacy adapter, notcurrent. Confirmed it fails on the unfixed router (legacy.writecalled 0×) and passes with the fix.currentwhen nobody owns the session).src/main/daemonsuite: no new failures (3 pre-existing, environment-dependent failures on this Windows checkout — WSL/ConPTY/path-parsing — fail identically on the clean tree).oxlintclean;tsgotypecheck clean (node + cli configs).Notes / limitations
discoverLegacySessions.