diff --git a/nanobot/webui/remote_instances.py b/nanobot/webui/remote_instances.py index 7bef0c7b6..274ee09b6 100644 --- a/nanobot/webui/remote_instances.py +++ b/nanobot/webui/remote_instances.py @@ -475,8 +475,11 @@ class RemoteInstances: raise RemoteError("same_instance") # SSH aliases identify routes, not nanobot instances. Don't open # two independent browser views of the same live gateway. - if any(item.gateway_id == identity["gatewayId"] - for item in self.connections.values()): + duplicates = [(saved_key, item) for saved_key, item in self.connections.items() + if item.gateway_id == identity["gatewayId"]] + for _, item in duplicates: + await self._check(item) + if any(not item.error for _, item in duplicates): raise RemoteError("duplicate_instance") # Require the installed WebUI too, not just a port answering HTTP. page = await client.get(base + "/") @@ -502,6 +505,12 @@ class RemoteInstances: profiles[key].local_port = proxy.port profiles[key].proxy_origin = True self._write(profiles) + # A failed route must not block a newly verified route to this bot. + # Retire it only after the replacement is saved; retain its profile + # and browser origin, so other tabs keep their drafts and fail closed. + for saved_key, item in duplicates: + await item.proxy.pause() + del self.connections[saved_key] self._proxies[proxy.port] = proxy self.connections[key] = connection return self._launch(key, profile, connection) diff --git a/tests/webui/test_remote_instances.py b/tests/webui/test_remote_instances.py index 55d0fed86..adfba8209 100644 --- a/tests/webui/test_remote_instances.py +++ b/tests/webui/test_remote_instances.py @@ -329,6 +329,24 @@ async def test_forget_only_removes_saved_profile(manager, ssh): ssh.tunnel.close.assert_not_awaited() +async def test_offline_alias_does_not_block_replacement_connection(manager, ssh, monkeypatch): + first = await save(manager) + await manager.action("connect", {"id": first}) + ssh.tunnel.active = False + alias = await save(manager, host="working-route") + second_tunnel = SimpleNamespace(active=True, error="", port=23457, close=AsyncMock()) + second_proxy = SimpleNamespace(port=23458, origin="http://127.0.0.1:23458", secret="local-two") + monkeypatch.setattr(remote_ssh, "open_tunnel", AsyncMock(return_value=second_tunnel)) + monkeypatch.setattr(RemoteProxy, "open", AsyncMock(return_value=second_proxy)) + + assert (await manager.action("connect", {"id": alias}))["gateway_id"] == "remote-one" + assert first not in manager.connections + assert manager.connections[alias].proxy is second_proxy + ssh.tunnel.pause.assert_awaited_once() + ssh.tunnel.close.assert_not_awaited() # Keep the old browser origin reserved. + assert len(manager.snapshot()["profiles"]) == 2 # No saved routes/drafts were deleted. + + async def test_closing_local_manager_reaps_ssh(manager, ssh): key = await save(manager) await manager.action("connect", {"id": key})