A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
fix(ssh): connect saved hosts under their names again
RoversX83d1f4ae1ea1
diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rsindex b7d1513a..01a22814 100644--- a/wezterm-gui/src/main.rs+++ b/wezterm-gui/src/main.rs@@ -484,31 +484,64 @@ async fn spawn_tab_in_domain_if_mux_is_empty(/// credentials, so hosts added in the UI are connectable (and reconnect/// silently) without a lua ssh_domains entry.pub(crate) fn connect_domain_from_ssh_host(name: &str) -> anyhow::Result<Arc<dyn Domain>> {- let entry = crate::ssh_hosts::list_all_hosts()- .into_iter()- .find(|entry| {- entry.spec.label == name- || entry.id == name- || crate::ssh_hosts::connect_domain_name(&entry.spec) == name- })- .ok_or_else(|| {- anyhow!("invalid domain {name}: not in ssh_domains and not a saved SSH host")- })?;- let mut dom = crate::ssh_hosts::build_ssh_domain(&entry.spec);- dom.name = match entry.spec.network {+ let hosts = crate::ssh_hosts::list_all_hosts();+ let entry = crate::ssh_hosts::host_for_connect_domain(&hosts, name).ok_or_else(|| {+ anyhow!("invalid domain {name}: not in ssh_domains and not a saved SSH host")+ })?;+ let domain_name = match entry.spec.network {crate::ssh_hosts::SshNetwork::System => name.to_string(),crate::ssh_hosts::SshNetwork::Tailnet => crate::ssh_hosts::connect_domain_name(&entry.spec),};+ Ok(add_ssh_host_client_domain(&entry.spec, domain_name))+}++/// The ThinkTerm Connect domain of the saved host `host_id`. The id picks+/// that exact record even when another host shares its label, but the domain+/// is still named the way its Space knows it (`connect_domain_name`): named+/// after the id, it would get a Space of its own called `ssh-<hash>`.+pub(crate) fn connect_domain_for_ssh_host_id(host_id: &str) -> anyhow::Result<Arc<dyn Domain>> {+ let entry = crate::ssh_hosts::list_all_hosts()+ .into_iter()+ .find(|entry| entry.id == host_id)+ .ok_or_else(|| anyhow!("no saved SSH host {host_id}"))?;+ let domain_name = crate::ssh_hosts::connect_domain_name(&entry.spec);+ Ok(add_ssh_host_client_domain(&entry.spec, domain_name))+}++/// Rebuild a saved host's Connect domain from its record after an edit: a+/// client keeps the address, user and password it was built with, so one+/// registered earlier would go on with the old ones until a restart. Only a+/// domain doing nothing is replaced; one that is attached, attaching or+/// retrying keeps going as it is.+pub(crate) fn refresh_connect_domain_for_ssh_host(spec: &crate::ssh_hosts::SshHostSpec) {+ let name = crate::ssh_hosts::connect_domain_name(spec);+ let Some(domain) = Mux::get().get_domain_by_name(&name) else {+ return;+ };+ let idle = domain.state() == mux::domain::DomainState::Detached+ && domain+ .downcast_ref::<ClientDomain>()+ .is_some_and(|client| !client.is_attaching() && !client.is_attach_retrying());+ if idle {+ add_ssh_host_client_domain(spec, name);+ }+}++fn add_ssh_host_client_domain(+ spec: &crate::ssh_hosts::SshHostSpec,+ domain_name: String,+) -> Arc<dyn Domain> {+ let mut dom = crate::ssh_hosts::build_ssh_domain(spec);+ dom.name = domain_name;dom.multiplexing = SshMultiplexing::WezTerm;- dom.stored_password = entry- .spec+ dom.stored_password = spec.password.as_deref().map(crate::secret::reveal).filter(|p| !p.is_empty());let domain: Arc<dyn Domain> = Arc::new(ClientDomain::new(ClientDomainConfig::Ssh(dom)));Mux::get().add_domain(&domain);- Ok(domain)+ domain}async fn current_gui_terminal_size(diff --git a/wezterm-gui/src/ssh_hosts.rs b/wezterm-gui/src/ssh_hosts.rsindex 65b9aec3..3274d01c 100644--- a/wezterm-gui/src/ssh_hosts.rs+++ b/wezterm-gui/src/ssh_hosts.rs@@ -734,6 +734,22 @@ fn label_for_connect_domain<'a>(.filter(|label| !label.is_empty())}+/// The saved host a ThinkTerm Connect domain name stands for. The name a+/// Space keeps for its host is tried first: a host on the system network and+/// one on the embedded tailnet may share a label, and only that name tells+/// them apart. Then a record id, and a bare label (as typed for `thinkterm+/// connect`) last.+pub fn host_for_connect_domain<'a>(+ hosts: &'a [SshHostEntry],+ name: &str,+) -> Option<&'a SshHostEntry> {+ hosts+ .iter()+ .find(|entry| connect_domain_name(&entry.spec) == name)+ .or_else(|| hosts.iter().find(|entry| entry.id == name))+ .or_else(|| hosts.iter().find(|entry| entry.spec.label == name))+}+/// Every name this host can be connected under.////// A ThinkTerm Connect host attaches its persistent mux domain under the@@ -750,19 +766,53 @@ pub fn domain_names_for_host(spec: &SshHostSpec) -> Vec<String> {names}-/// Whether a mux domain of this name can still be reached.-///-/// Deliberately the same question `connect_domain_from_ssh_host` answers, so-/// "reachable" here means exactly "something would happen if you clicked it":-/// a lua-configured or already-attached domain, or a saved host that would be-/// connected under this name.+/// Whether a mux domain of this name can still be reached: a lua-configured+/// or already-attached domain, or a saved host that is connected under this+/// name. Not a host's record id, though `connect_domain_from_ssh_host` takes+/// one: Remote Hosts once connected hosts under their ids, and a Space left+/// under one is forgotten here so that the host's next connection brings it+/// back under the host's own name.fn domain_name_is_reachable(domain_name: &str) -> bool {if Mux::get().get_domain_by_name(domain_name).is_some() {return true;}list_all_hosts().into_iter()- .any(|entry| connect_domain_name(&entry.spec) == domain_name || entry.id == domain_name)+ .any(|entry| connect_domain_name(&entry.spec) == domain_name)+}++/// The names a host could be connected under that an edit takes from it: its+/// connection name, when it is renamed or moved to the other network.+fn domain_names_dropped(old: &SshHostSpec, new: &SshHostSpec) -> Vec<String> {+ let kept = domain_names_for_host(new);+ domain_names_for_host(old)+ .into_iter()+ .filter(|name| !kept.contains(name))+ .collect()+}++/// The names an edit of a host leaves with nothing to connect: those it no+/// longer answers to that no other saved host answers to either. A domain+/// registered under one earlier this session does not count, as it would for+/// `domain_name_is_reachable`: the Mux keeps every domain it was given, and+/// that one is the host as it was before the edit.+pub fn domain_names_left_unreachable(old: &SshHostSpec, new: &SshHostSpec) -> Vec<String> {+ names_left_unreachable(old, new, &list_all_hosts())+}++fn names_left_unreachable(+ old: &SshHostSpec,+ new: &SshHostSpec,+ hosts: &[SshHostEntry],+) -> Vec<String> {+ domain_names_dropped(old, new)+ .into_iter()+ .filter(|name| {+ !hosts+ .iter()+ .any(|entry| domain_names_for_host(&entry.spec).contains(name))+ })+ .collect()}/// Drop this device's copy of every Space whose mux server it can no longer@@ -1026,6 +1076,81 @@ mod tests {assert_eq!(connect_domain_label("server-b"), "server-b");}+ #[test]+ fn a_label_on_both_networks_finds_the_host_its_space_was_connected_through() {+ let mut system = spec_with_options(HashMap::new());+ system.label = "server-a".into();+ system.host = "192.0.2.10".into();+ let mut tailnet = system.clone();+ tailnet.host = "100.64.0.2".into();+ tailnet.network = SshNetwork::Tailnet;+ let entry = |spec: &SshHostSpec| SshHostEntry {+ id: host_id_for_host(spec),+ source: SshHostSource::ThinkTerm,+ spec: spec.clone(),+ };+ let network = |hosts: &[SshHostEntry], name: &str| {+ host_for_connect_domain(hosts, name).map(|entry| entry.spec.network)+ };+ // Listed first, the tailnet host shares the label but must not+ // answer for the Spaces of the one on the system network.+ let hosts = [entry(&tailnet), entry(&system)];+ assert_eq!(network(&hosts, "server-a"), Some(SshNetwork::System));+ assert_eq!(+ network(&hosts, &connect_domain_name(&tailnet)),+ Some(SshNetwork::Tailnet)+ );+ assert_eq!(+ network(&hosts, &host_id_for_host(&tailnet)),+ Some(SshNetwork::Tailnet)+ );+ // With nothing else called that, the label still finds its host.+ let hosts = [entry(&tailnet)];+ assert_eq!(network(&hosts, "server-a"), Some(SshNetwork::Tailnet));+ assert_eq!(network(&hosts, "server-b"), None);+ }++ #[test]+ fn a_rename_or_a_move_to_the_other_network_drops_the_name_its_spaces_know() {+ let mut old = spec_with_options(HashMap::new());+ old.label = "server-a".into();+ old.host = "100.64.0.2".into();+ let dropped = |new: &SshHostSpec| domain_names_dropped(&old, new);++ let mut new = old.clone();+ new.multiplexing = true;+ new.detect_os = false;+ assert!(dropped(&new).is_empty());++ new.label = "server-b".into();+ assert_eq!(dropped(&new), vec!["server-a".to_string()]);++ let mut new = old.clone();+ new.network = SshNetwork::Tailnet;+ assert!(dropped(&new).contains(&"server-a".to_string()));++ // Back again: the tailnet name goes, the label is the name once more.+ let mut back = new.clone();+ back.network = SshNetwork::System;+ let names = domain_names_dropped(&new, &back);+ assert!(names.contains(&connect_domain_name(&new)));+ assert!(!names.contains(&"server-a".to_string()));++ // A name another saved host still goes by is that host's to keep.+ let entry = |spec: &SshHostSpec| SshHostEntry {+ id: host_id_for_host(spec),+ source: SshHostSource::ThinkTerm,+ spec: spec.clone(),+ };+ let mut renamed = old.clone();+ renamed.label = "server-b".into();+ let mut other = old.clone();+ other.host = "192.0.2.10".into();+ let left = |hosts: &[SshHostEntry]| names_left_unreachable(&old, &renamed, hosts);+ assert!(left(&[entry(&renamed)]).contains(&"server-a".to_string()));+ assert!(!left(&[entry(&renamed), entry(&other)]).contains(&"server-a".to_string()));+ }+#[test]fn a_failed_catalog_load_is_retried_before_editing() {let mut cached = None;diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rsindex bd6d64c8..adb7b944 100644--- a/wezterm-gui/src/termwindow/mouseevent.rs+++ b/wezterm-gui/src/termwindow/mouseevent.rs@@ -6192,19 +6192,22 @@ impl super::TermWindow {.find(|space| space.domain.as_deref() == Some(domain)).map(|space| self.space_connection_state(&space.id)).unwrap_or(SpaceConnectionState::Connected);+ // The host's own name: one on the embedded tailnet is connected+ // under its address.+ let name = crate::ssh_hosts::connect_domain_label(domain);let header = match state {- SpaceConnectionState::Connected => tr_with_name("menu-space-server-group", domain),+ SpaceConnectionState::Connected => tr_with_name("menu-space-server-group", &name),SpaceConnectionState::Connecting => {- tr_with_name("menu-space-server-group-connecting", domain)+ tr_with_name("menu-space-server-group-connecting", &name)}// Distinct from disconnected: the transport is being retried,// and calling that "disconnected" reads as though the Spaces// below are unreachable for good.SpaceConnectionState::Reconnecting => {- tr_with_name("menu-space-server-group-reconnecting", domain)+ tr_with_name("menu-space-server-group-reconnecting", &name)}SpaceConnectionState::Disconnected => {- tr_with_name("menu-space-server-group-offline", domain)+ tr_with_name("menu-space-server-group-offline", &name)}};// A server is a heading, not something to switch to. Saying that@@ -6781,7 +6784,7 @@ impl super::TermWindow {promise::spawn::spawn(async move {let domain = match Mux::get().get_domain_by_name(&name) {Some(domain) => domain,- None => match crate::connect_domain_from_ssh_host(&host_id) {+ None => match crate::connect_domain_for_ssh_host_id(&host_id) {Ok(domain) => domain,Err(err) => {log::error!("connect {name}: {err:#}");diff --git a/wezterm-gui/src/termwindow/ssh_hosts_view.rs b/wezterm-gui/src/termwindow/ssh_hosts_view.rsindex 84a0e0e8..2fb8eb9e 100644--- a/wezterm-gui/src/termwindow/ssh_hosts_view.rs+++ b/wezterm-gui/src/termwindow/ssh_hosts_view.rs@@ -723,9 +723,10 @@ impl SshHostsView {self.form_content_h = 0.0;}- /// Validate + create/update. On success returns the project id and closes- /// the form; on error records the message and keeps the form open.- fn persist_form(&mut self) -> Option<String> {+ /// Validate + create/update. On success returns the project id, with the+ /// Spaces the save left no way to reach (see `release_spaces_then`), and+ /// closes the form; on error records the message and keeps the form open.+ fn persist_form(&mut self) -> Option<(String, Vec<workspace_threads::SpaceId>)> {let form = self.form.as_mut()?;if form.network == SshNetwork::Tailnet && form.use_mosh {form.error = Some(crate::i18n::tr("ssh-error-tailnet-mosh"));@@ -830,20 +831,41 @@ impl SshHostsView {return None;}+ let mut released = Vec::new();let project_id = match &form.editing {- Some(id) => match ssh_hosts::try_update_host(id, spec) {- // Not `id`: moving the host to another endpoint re-keys the- // record, and the caller selects what comes back.- Ok(Some(saved_id)) => saved_id,- Ok(None) => {- form.error = Some(crate::i18n::tr("ssh-error-host-missing"));- return None;- }- Err(err) => {- form.error = Some(ssh_error("ssh-error-save", format!("{err:#}")));- return None;+ Some(id) => {+ let before = ssh_hosts::host_spec(id);+ match ssh_hosts::try_update_host(id, spec) {+ // Not `id`: moving the host to another endpoint re-keys the+ // record, and the caller selects what comes back.+ Ok(Some(saved_id)) => {+ if let (Some(before), Some(after)) =+ (before, ssh_hosts::host_spec(&saved_id))+ {+ // Renamed or moved to the other network, the host+ // no longer answers to the name its Spaces know it+ // by. Let them go now, as the next launch would:+ // the host's next connection brings them back,+ // while kept, a click on one would connect it under+ // a name that Space does not know. References to+ // their threads stay, to resolve again then.+ released = workspace_threads::space_ids_for_domains(+ &ssh_hosts::domain_names_left_unreachable(&before, &after),+ );+ crate::refresh_connect_domain_for_ssh_host(&after);+ }+ saved_id+ }+ Ok(None) => {+ form.error = Some(crate::i18n::tr("ssh-error-host-missing"));+ return None;+ }+ Err(err) => {+ form.error = Some(ssh_error("ssh-error-save", format!("{err:#}")));+ return None;+ }}- },+ }None => match ssh_hosts::try_create_host(spec) {Ok(id) => id,Err(err) => {@@ -854,7 +876,7 @@ impl SshHostsView {};self.close_form();self.refresh();- Some(project_id)+ Some((project_id, released))}// ---- input ------------------------------------------------------------@@ -1163,12 +1185,12 @@ impl SshHostsView {}ContentViewResponse::Redraw}- SshViewAction::Save => {- self.persist_form();- ContentViewResponse::Redraw- }+ SshViewAction::Save => match self.persist_form() {+ Some((_, released)) => release_spaces_then(released, ContentViewResponse::Redraw),+ None => ContentViewResponse::Redraw,+ },SshViewAction::SaveAndConnect => match self.persist_form() {- Some(id) => open_thread_response(id),+ Some((id, released)) => release_spaces_then(released, open_thread_response(id)),None => ContentViewResponse::Redraw,},SshViewAction::Cancel => {@@ -1476,7 +1498,9 @@ impl SshHostsView {(KeyCode::Enter, _) => {if in_form {match self.persist_form() {- Some(id) => open_thread_response(id),+ Some((id, released)) => {+ release_spaces_then(released, open_thread_response(id))+ }None => ContentViewResponse::Redraw,}} else if let Some(entry) = self.filtered.get(self.selected) {@@ -5820,6 +5844,28 @@ fn open_thread_response(project_id: String) -> ContentViewResponse {}))}+/// `then` (a redraw, or a callback such as `open_thread_response`) after+/// dropping this device's copy of `released`: Spaces a saved host no longer+/// reaches. Through the window, as `delete_host` drops a deleted host's,+/// because it may be showing one; the server keeps them, and the host's next+/// connection brings them back.+fn release_spaces_then(+ released: Vec<workspace_threads::SpaceId>,+ then: ContentViewResponse,+) -> ContentViewResponse {+ if released.is_empty() {+ return then;+ }+ ContentViewResponse::Run(Box::new(move |term_window| {+ for space_id in released {+ term_window.start_delete_space(&space_id, workspace_threads::SpaceRemoval::Local, false);+ }+ if let ContentViewResponse::Run(then) = then {+ then(term_window);+ }+ }))+}+impl ContentView for SshHostsView {fn title(&self) -> String {crate::i18n::tr("ssh-hosts-title")diff --git a/wezterm-gui/src/workspace_threads.rs b/wezterm-gui/src/workspace_threads.rsindex cf3009c0..ae041f6f 100644--- a/wezterm-gui/src/workspace_threads.rs+++ b/wezterm-gui/src/workspace_threads.rs@@ -4731,7 +4731,7 @@ fn notify_remote_tree_mutation_unavailable(domain_name: &str) {);if crate::frontend::try_front_end().is_some() {let mut args = FluentArgs::new();- args.set("name", domain_name.to_string());+ args.set("name", crate::ssh_hosts::connect_domain_label(domain_name));wezterm_toast_notification::persistent_toast_notification("ThinkTerm",&crate::i18n::tr_args("remote-tree-mutation-offline", &args),