diff --git a/nanobot/webui/remote_instances.py b/nanobot/webui/remote_instances.py index b846c43c4..006eb6ec3 100644 --- a/nanobot/webui/remote_instances.py +++ b/nanobot/webui/remote_instances.py @@ -333,11 +333,14 @@ class RemoteInstances: )) if instance_changed: self._remember_origins(profiles) - local_port = profiles[key].local_port if key in profiles and not instance_changed else 0 - proxy_origin = bool(local_port and profiles[key].proxy_origin) - profiles[key] = _SavedProfile(**profile.model_dump(), local_port=local_port, - proxy_origin=proxy_origin) + # Update editable fields without reconstructing (and losing) + # managed origin metadata for an unchanged destination. + profiles[key] = (profiles[key].model_copy(update=profile.model_dump()) + if key in profiles and not instance_changed + else _SavedProfile(**profile.model_dump())) self._write(profiles) + if instance_changed: + self._compatibility.pop(key, None) if target_changed: self._known_hosts(key).unlink(missing_ok=True) self._unverified.pop(key, None) diff --git a/tests/webui/test_remote_instances.py b/tests/webui/test_remote_instances.py index 4edda4f54..b2ce57bbc 100644 --- a/tests/webui/test_remote_instances.py +++ b/tests/webui/test_remote_instances.py @@ -481,13 +481,33 @@ async def test_health_budget_includes_waiting_for_connection_lock(manager, ssh, async def test_reconnect_keeps_origin_and_changing_target_does_not(manager, ssh): key = await save(manager) - await manager.action("connect", {"id": key}) + first = await manager.action("connect", {"id": key}) assert json.loads(manager.path.read_text())[key]["local_port"] == 23456 await manager.action("disconnect", {"id": key}) await manager.action("save", {"id": key, "profile": {"name": "Renamed", "host": "ubuntu@example.test"}}) assert json.loads(manager.path.read_text())[key]["local_port"] == 23456 + # Editing display metadata must preserve the local-client origin marker, + # not just its port: otherwise reconnect silently retires that origin. + assert json.loads(manager.path.read_text())[key]["local_client_origin"] is True + second = await manager.action("connect", {"id": key}) + assert second["url"] == first["url"] + RemoteProxy.open.assert_awaited_once() + await manager.action("disconnect", {"id": key}) await manager.action("save", {"id": key, "profile": {"name": "Other", "host": "other.example.test"}}) assert json.loads(manager.path.read_text())[key]["local_port"] == 0 + assert "compatibility" not in manager.snapshot()["profiles"][0] + + +async def test_editing_failed_target_clears_its_compatibility_diagnosis(manager, ssh): + key = await save(manager) + ssh.state["identity"].pop("webui") + with pytest.raises(RemoteError, match="webui_compatibility_unknown"): + await manager.action("connect", {"id": key}) + result = await manager.action("save", {"id": key, "profile": { + "name": "Other", "host": "other.example.test", + }}) + assert result["profiles"][0]["connection_error"] == "disconnected" + assert "compatibility" not in result["profiles"][0] async def test_corrupt_store_not_overwritten(manager): diff --git a/webui/src/components/remote/RemoteConnectionsPage.tsx b/webui/src/components/remote/RemoteConnectionsPage.tsx index 05126b8d2..44ed7e239 100644 --- a/webui/src/components/remote/RemoteConnectionsPage.tsx +++ b/webui/src/components/remote/RemoteConnectionsPage.tsx @@ -51,8 +51,8 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro const [editing, setEditing] = useState(false); const [savedId, setSavedId] = useState(""); const [busy, setBusy] = useState(""); - const [error, setError] = useState(""); const [errorCode, setErrorCode] = useState(""); + const error = errorCode ? t(`remote.errors.${errorCode}`, { defaultValue: t("remote.errors.unknown") }) : ""; const [sshOptions, setSSHOptions] = useState(false); const [manualLocation, setManualLocation] = useState(false); const [inspection, setInspection] = useState(null); @@ -96,12 +96,11 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro setInspection(null); setManualLocation(false); setInspectBeforeConnect(true); } setErrorCode(code); - setError(t(`remote.errors.${code}`, { defaultValue: t("remote.errors.unknown") })); }; const closeEditor = () => { operation.current += 1; if (busy) connections.cancel(); - setBusy(""); setEditorOpen(false); setFingerprint(null); setError(""); setErrorCode(""); + setBusy(""); setEditorOpen(false); setFingerprint(null); setErrorCode(""); }; const beginEditor = (profile?: RemoteProfile) => { operation.current += 1; @@ -111,20 +110,20 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro setForm(profile ? { name: profile.name, host: profile.host, port: profile.port, ssh_config: profile.ssh_config, identity_file: profile.identity_file, config_path: profile.config_path, runtime_user: profile.runtime_user } : emptyProfile()); - setError(""); setCommandImported(false); setEditorOpen(true); setFingerprint(null); + setCommandImported(false); setEditorOpen(true); setFingerprint(null); }; const importCommand = () => { if (!/^ssh\s/.test(form.host.trim())) return; try { setForm({ ...form, ...parseSSHAddress(form.host) }); - setCommandImported(true); setError(""); + setCommandImported(true); setErrorCode(""); } catch (reason) { showError(reason); } }; const pickFile = async (field: "ssh_config" | "identity_file") => { const attempt = ++operation.current; - setBusy(`pick-${field}`); setError(""); setErrorCode(""); + setBusy(`pick-${field}`); setErrorCode(""); try { const result = await remoteAction<{ path: string | null }>(client, "pick_file", {}); if (wanted(attempt) && typeof result.path === "string" && result.path) { @@ -140,7 +139,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro ; const connect = async (id: string, attempt = ++operation.current, inspect = false) => { - setBusy(inspect ? "inspect" : id); setError(""); setErrorCode(""); + setBusy(inspect ? "inspect" : id); setErrorCode(""); try { if (inspect) { const result = await remoteAction(client, "inspect", { id }); @@ -208,7 +207,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro if (!fingerprint) return; const attempt = ++operation.current; const id = fingerprint.id; - setBusy("trust"); setError(""); + setBusy("trust"); setErrorCode(""); try { await remoteAction(client, "trust", { id, challenge: fingerprint.challenge }); if (wanted(attempt)) { setFingerprint(null); await connect(id, attempt, fingerprint.inspect); } @@ -218,7 +217,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro const save = async () => { const attempt = ++operation.current; - setBusy("save"); setError(""); setErrorCode(""); + setBusy("save"); setErrorCode(""); try { // Also parse here for Enter submission, which doesn't blur the address. const address = parseSSHAddress(form.host); @@ -239,12 +238,10 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro const remove = async () => { if (!removing) return; - setBusy("remove"); setError(""); + setBusy("remove"); setErrorCode(""); try { - await disconnectHost(removing.id); - await remoteAction(client, "remove", { id: removing.id }); + await connections.remove(removing.id); if (mounted.current) setRemoving(null); - await refresh?.().catch(() => {}); } catch (reason) { if (mounted.current) showError(reason); } finally { if (mounted.current) setBusy(""); } }; @@ -259,7 +256,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro } finally { if (mounted.current) setBusy(""); } }; const disconnect = async (id: string) => { - setBusy(id); setError(""); + setBusy(id); setErrorCode(""); try { await disconnectHost(id); if (mounted.current) setDisconnecting(null); } catch (reason) { if (mounted.current) showError(reason); } finally { if (mounted.current) setBusy(""); } @@ -277,7 +274,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro const disconnectProfile = disconnecting || lastDisconnect.current; const editSSH = () => { setInspection(null); setManualLocation(false); setInspectBeforeConnect(true); - setError(""); setErrorCode(""); setSSHOptions(true); + setErrorCode(""); setSSHOptions(true); }; const groups = groupRemoteProfiles(directory?.profiles || [], connections.activeHostId); const detailGroup = groups.find((group) => group.id === details); @@ -286,8 +283,8 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro const connectionActions = (profile: RemoteProfile, grouped = false) => <> {!profile.paired && { setDetailsOpen(false); beginEditor(profile); }}>{t("remote.edit")}} {profile.paired && { setDetailsOpen(false); setPairRoute(profile.id); setRouteOpen(true); }}>{t("remote.pair.route")}} - {(profile.connected || connections.openHostIds.includes(profile.id)) && { setDetailsOpen(false); setError(""); setDisconnecting(profile); }}>{t("remote.disconnect")}} - { setDetailsOpen(false); setError(""); setRemoving(profile); }}>{t(grouped ? "remote.sameInstance.forgetConnection" : "remote.forget")} + {(profile.connected || connections.openHostIds.includes(profile.id)) && { setDetailsOpen(false); setErrorCode(""); setDisconnecting(profile); }}>{t("remote.disconnect")}} + { setDetailsOpen(false); setErrorCode(""); setRemoving(profile); }}>{t(grouped ? "remote.sameInstance.forgetConnection" : "remote.forget")} ; return
@@ -361,7 +358,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro {pageError && !editorOpen && !fingerprint && !removing && !disconnecting &&

{directoryError && }
}
@@ -444,7 +441,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro

{t("remote.verifyHint")}

{error &&

}
- {editorOpen && } + {editorOpen && }
@@ -466,7 +463,7 @@ export function RemoteConnectionsPage({ mainNavigationExpanded = false, hostChro {inspection.candidates.map((candidate) =>