5472d1d66c
The Sessions sidebar switcher (selectConnection) activated the target gateway first and wiped session bindings afterwards — across an IPC round-trip — so route/session effects saw the new source while $activeSessionId still named the previous backend's runtime id, sent it to a backend that never minted it, and got "session not found". The Settings → Gateway apply (softSwitch) never had this problem: it raises $gatewaySwitching, runs beforeConnectionSwitch and wipes before dialing. Give both doors one commit point. store/gateway-switch gains beginGatewaySwitch()/endGatewaySwitch(): barrier up, the registered machine-context reset, session wipe — in one synchronous step. useGatewayBoot registers its beforeConnectionSwitch/refreshSessions with the store and softSwitch goes through the same entry point. selectConnection becomes two-phase: dial the target WITHOUT activating (openGatewayAgent → openGatewayForAgent with an activation lease, so a live-work recompute can't prune it mid-spawn), then beginGatewaySwitch() and activate the already-open socket with no await between the wipe and the publication. A dead target fails with the current workspace intact; a superseded click never activates its target; an activation that fails after the wipe repaints the still-active source instead of leaving the sidebar skeleton. Tests: real-store end-to-end regression (real useGatewayBoot + gateway registry + selectConnection over fake sockets) — red on the previous code with the leaked runtime id observed at publication — plus store ordering/failure-path, commit-point and prune-lease coverage. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>