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
Connect/exit fixes: workspace-true adoption; survive thread exit; calmer indicator
RoversX7eb25ea2e8ac
diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rsindex a8c79b4f..89b187f5 100644--- a/wezterm-client/src/domain.rs+++ b/wezterm-client/src/domain.rs@@ -698,22 +698,20 @@ impl ClientDomain {if let Some(local_window_id) = primary_window_id {// Adopt the remote window into the local primary window- // when the workspaces agree, or unconditionally when the- // local window is claimed for this domain (a ThinkTerm- // Space connect window): the claim carries the identity,- // and the local workspace name (the Space thread) wins.- let (workspace_matches, domain_owned, local_workspace) = {+ // only when the workspaces agree. Adopting on the+ // origin-domain claim alone used to grab whichever+ // remote window the server listed FIRST — typically the+ // server's own startup window, or another thread's+ // window — surfacing an unrelated old terminal as a tab+ // and renaming its workspace on the server, merging it+ // into the wrong thread for good.+ let workspace_matches = {let window = mux.get_window(local_window_id).expect("primary window to be valid");- let local_workspace = window.get_workspace().to_string();- (- Some(local_workspace.as_str()) == workspace.as_deref(),- window.origin_domain() == Some(inner.local_domain_id),- local_workspace,- )+ Some(window.get_workspace()) == workspace.as_deref()};- if workspace_matches || domain_owned {+ if workspace_matches {// Yes! We can use this windowlog::debug!("adding remote window {} as tab to local window {}",@@ -726,27 +724,6 @@ impl ClientDomain {);mux.add_tab_to_window(&tab, local_window_id)?;primary_window_id.take();- if !workspace_matches {- // Bring the server's workspace in line with the- // local window's so that future attaches fold- // this window by name as well.- let inner = Arc::clone(&inner);- promise::spawn::spawn(async move {- let request = codec::SetWindowWorkspace {- window_id: remote_window_id,- workspace: local_workspace,- };- if let Err(err) =- inner.client.set_window_workspace(request).await- {- log::error!(- "failed to align remote window {remote_window_id} \- workspace: {err:#}"- );- }- })- .detach();- }continue;}}@@ -968,7 +945,15 @@ impl Domain for ClientDomain {.inner().ok_or_else(|| anyhow!("domain is not attached"))?;- let workspace = Mux::get().active_workspace();+ // File the remote window under the workspace of the local window we+ // are spawning into, NOT the globally active workspace: with several+ // Spaces open the active workspace routinely belongs to a different+ // (even local) thread, and a remote window misfiled under that name+ // folds into the wrong window on every later attach.+ let workspace = Mux::get()+ .get_window(window)+ .map(|w| w.get_workspace().to_string())+ .unwrap_or_else(|| Mux::get().active_workspace());let result = inner.clientdiff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rsindex 209db0e7..ffd04610 100644--- a/wezterm-gui/src/frontend.rs+++ b/wezterm-gui/src/frontend.rs@@ -1,5 +1,6 @@use crate::scripting::guiwin::GuiWin;use crate::spawn::SpawnWhere;+use crate::termwindow::TermWindowNotif;use crate::TermWindow;use ::window::*;use anyhow::{anyhow, Context, Error};@@ -387,15 +388,23 @@ impl GuiFrontEnd {// workspace). Windows pinned to live mux windows in *other*// workspaces are never touched.- // 1. Drop GUI windows whose mux window no longer exists.+ // 1. Handle GUI windows whose mux window no longer exists: give the+ // TermWindow a chance to fall back to another thread of its Space+ // first. Closing it outright meant an `exit` in a thread's only+ // pane took the whole window down — and the app with it, when it+ // was the last window. The TermWindow either adopts a new mux+ // window (updating known_windows via rebind) or closes itself.let known_windows = std::mem::take(&mut *self.known_windows.borrow_mut());let mut windows = BTreeMap::new();for (window, window_id) in known_windows.into_iter() {if mux.get_window(window_id).is_some() {windows.insert(window, window_id);} else {- window.close();self.spawned_mux_window.borrow_mut().remove(&window_id);+ window.notify(TermWindowNotif::Apply(Box::new(|term_window| {+ term_window.recover_from_dead_mux_window();+ })));+ windows.insert(window, window_id);}}*self.known_windows.borrow_mut() = windows;diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rsindex a6b0620b..be6c9211 100644--- a/wezterm-gui/src/main.rs+++ b/wezterm-gui/src/main.rs@@ -459,7 +459,7 @@ pub(crate) async fn connect_domain_into_space(let thread_workspace = workspace_threads::ensure_mux_thread_workspace(&plan);let window_id = {let builder = mux.new_empty_window_for_domain(- Some(thread_workspace),+ Some(thread_workspace.clone()),None,Some(domain.domain_id()),);@@ -508,9 +508,13 @@ pub(crate) async fn connect_domain_into_space(}}- // Mirror spawn_tab_in_domain_if_mux_is_empty's empty-server handling.- let no_workspace_filter = None;- if !have_panes_in_domain_and_ws(&domain, &no_workspace_filter) {+ // Spawn the thread's first shell when its workspace has no panes on the+ // server. Keying this off "does the domain have any panes at all" left+ // the connect window empty whenever the server had unrelated windows+ // (its own startup window, other threads) — the old blind window+ // adoption papered over that by grabbing one of them as a tab.+ let thread_workspace_filter = Some(thread_workspace);+ if !have_panes_in_domain_and_ws(&domain, &thread_workspace_filter) {let _config_subscription = config::subscribe_to_config_reload(move || {promise::spawn::spawn_into_main_thread(async move {if let Err(err) = update_mux_domains(&config::configuration()) {diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rsindex be3391e0..06c5fce8 100644--- a/wezterm-gui/src/termwindow/mod.rs+++ b/wezterm-gui/src/termwindow/mod.rs@@ -978,6 +978,10 @@ pub struct TermWindow {next_mosh_connect_generation: u64,space_owner_id: u64,active_space_id: String,+ /// True for windows the user did not explicitly open (domain-owned+ /// windows spawned by the reconcile, e.g. reconnect/auth prompts): they+ /// close when their mux window dies instead of falling back to a thread.+ dies_with_mux_window: bool,workspace_layout_structure_fingerprint: Option<u64>,workspace_sidebar_width: usize,workspace_sidebar_pending_thread_selection: Option<String>,@@ -1444,6 +1448,7 @@ impl TermWindow {let render_state = None;let connection_name = Connection::get().unwrap().name();+ let dies_with_mux_window = claimed_space.is_none() && !restore_saved_thread;let (space_owner_id, active_space_id) = claimed_space.unwrap_or_else(|| {let space_owner_id = crate::workspace_threads::next_space_owner_id();// Windows created without a saved-thread restore are incidental@@ -1590,6 +1595,7 @@ impl TermWindow {next_mosh_connect_generation: 1,space_owner_id,active_space_id,+ dies_with_mux_window,workspace_layout_structure_fingerprint,workspace_sidebar_width,workspace_sidebar_pending_thread_selection: None,@@ -2377,6 +2383,40 @@ impl TermWindow {}}+ /// The mux window this GUI window displayed is gone (its last pane+ /// exited, e.g. `exit` in a thread's only shell). Fall back to another+ /// thread of the Space — or a fresh one — instead of dying with it,+ /// which used to take the whole app down when this was the last window.+ /// Incidental windows (reconnect/auth prompts) and remote mux Spaces+ /// keep the close-with-the-mux-window behavior.+ pub(crate) fn recover_from_dead_mux_window(&mut self) {+ let mux = Mux::get();+ if mux.get_window(self.mux_window_id).is_some() {+ // Already re-pointed at a live mux window (a thread switch or+ // Space delete raced the reconcile); nothing to recover.+ return;+ }+ let Some(window) = self.window.clone() else {+ return;+ };+ let recovery_thread = if self.dies_with_mux_window+ || crate::workspace_threads::client_domain_for_space(&self.active_space_id).is_some()+ {+ None+ } else {+ crate::workspace_threads::thread_to_recover_after_window_death(&self.active_space_id)+ };+ let Some(thread_id) = recovery_thread else {+ window.close();+ front_end().forget_known_window(&window);+ return;+ };+ // The mux subscription cancelled itself when our mux window was+ // removed; re-establish it for the window we are about to adopt.+ self.subscribe_to_pane_updates();+ self.activate_workspace_thread(thread_id, &window);+ }+/// Switch THIS window to display the (already-live) workspace `workspace`,/// adopting its mux window in place, and make it the active workspace so/// that spawn-new-window defaults stay correct. Returns false when thediff --git a/wezterm-gui/src/termwindow/ui/sidebar.rs b/wezterm-gui/src/termwindow/ui/sidebar.rsindex 42392be1..e0b513cf 100644--- a/wezterm-gui/src/termwindow/ui/sidebar.rs+++ b/wezterm-gui/src/termwindow/ui/sidebar.rs@@ -82,10 +82,6 @@ struct WorkspaceSidebarLayout {show_sidebar_toolbar: bool,space_menu_y: usize,space_menu_height: usize,- /// Height of the "Server disconnected" banner row shown below the Space- /// menu while a client-domain Space's server is unresponsive; 0 when- /// connected (the banner is absent and everything below moves up).- connection_banner_height: usize,top_action_y_offset: usize,top_action_height: usize,list_top: usize,@@ -274,8 +270,11 @@ impl crate::TermWindow {/// responding (a reconnect is pending or running). While the Space is/// remote we keep a slow self-driven repaint tick going: a dead/// connection produces no output, so nothing else would repaint the- /// indicator when the state flips.+ /// indicator when the state flips. Panes report tardy after ~3s of an+ /// unanswered request, which ordinary latency spikes trip all the+ /// time; require a longer sustained silence before alarming the user.fn space_connection_lost(&self, space_id: &str) -> bool {+ const SUSTAINED_LAG_MS: u64 = 5000;let Some(domain_name) = workspace_threads::client_domain_for_space(space_id) else {return false;};@@ -292,7 +291,8 @@ impl crate::TermWindow {let domain_id = domain.domain_id();mux.iter_panes().iter().any(|pane| {pane.domain_id() == domain_id- && crate::termwindow::render::pane::client_pane_lag_ms(pane.as_ref()).is_some()+ && crate::termwindow::render::pane::client_pane_lag_ms(pane.as_ref())+ .map_or(false, |ms| ms >= SUSTAINED_LAG_MS)})}@@ -440,14 +440,6 @@ impl crate::TermWindow {let space_menu_y = y;let space_menu_height = top_action_height + 6;y += space_menu_height + SIDEBAR_INSET;- let connection_banner_height = if self.space_connection_lost(&self.active_space_id) {- ui_cell_height + SIDEBAR_INSET- } else {- 0- };- if connection_banner_height > 0 {- y += connection_banner_height + SIDEBAR_INSET;- }let list_top = y + top_action_y_offset + top_action_height + SIDEBAR_INSET;WorkspaceSidebarLayout {@@ -462,7 +454,6 @@ impl crate::TermWindow {show_sidebar_toolbar,space_menu_y,space_menu_height,- connection_banner_height,top_action_y_offset,top_action_height,list_top,@@ -867,17 +858,28 @@ impl crate::TermWindow {&space_title,space_text_right.saturating_sub(space_text_x),)?;+ let (space_icon, space_icon_color) = if self.space_connection_lost(&self.active_space_id) {+ // Swap the icon in place rather than adding text or a banner:+ // the indicator must not change the row's width or height, so+ // transient lag spikes can't make the sidebar layout jump.+ (SvgIcon::CircleAlert, SPACE_DISCONNECTED_COLOR)+ } else {+ (+ SvgIcon::Layers,+ if space_menu_hovered {+ foreground+ } else {+ muted_fg+ },+ )+ };self.paint_sidebar_icon(layers,- SvgIcon::Layers,+ space_icon,space_icon_x,space_icon_y,space_icon_size,- if space_menu_hovered {- foreground- } else {- muted_fg- },+ space_icon_color,)?;self.paint_sidebar_text(layers,@@ -901,52 +903,7 @@ impl crate::TermWindow {muted_fg},)?;- let mut y = space_menu_y + space_menu_height + SIDEBAR_INSET;- if layout.connection_banner_height > 0 {- let banner_x = item_x + SIDEBAR_INSET;- let banner_width = item_width.saturating_sub(SIDEBAR_INSET * 2);- let banner_height = layout.connection_banner_height;- self.fill_rounded_rectangle(- layers,- 1,- euclid::rect(- banner_x as f32,- y as f32,- banner_width as f32,- banner_height as f32,- ),- SPACE_DISCONNECTED_COLOR.mul_alpha(0.18),- SIDEBAR_ROW_RADIUS,- )- .context("sidebar connection banner")?;- let banner_icon_size = ui_cell_height.min(banner_height.saturating_sub(6));- let banner_icon_x = banner_x + SIDEBAR_INSET;- let banner_icon_y = y + ((banner_height.saturating_sub(banner_icon_size)) / 2);- self.paint_sidebar_icon(- layers,- SvgIcon::CircleAlert,- banner_icon_x,- banner_icon_y,- banner_icon_size,- SPACE_DISCONNECTED_COLOR,- )?;- let banner_text_x = banner_icon_x + banner_icon_size + SIDEBAR_ICON_GAP;- let banner_text_max =- (banner_x + banner_width).saturating_sub(banner_text_x + SIDEBAR_INSET);- let banner_label =- self.ellipsize_sidebar_text(&ui_font, "Server disconnected", banner_text_max)?;- self.paint_sidebar_text(- layers,- &ui_font,- ui_metrics,- banner_label.as_ref(),- banner_text_x,- y + ((banner_height.saturating_sub(ui_cell_height)) / 2),- banner_text_max,- SPACE_DISCONNECTED_COLOR,- )?;- y += banner_height + SIDEBAR_INSET;- }+ let y = space_menu_y + space_menu_height + SIDEBAR_INSET;let top_action_x = item_x + SIDEBAR_INSET;let top_action_y = y + top_action_y_offset;let notification_action_size = top_action_height;diff --git a/wezterm-gui/src/workspace_threads.rs b/wezterm-gui/src/workspace_threads.rsindex a9b0331c..50073fc0 100644--- a/wezterm-gui/src/workspace_threads.rs+++ b/wezterm-gui/src/workspace_threads.rs@@ -798,6 +798,60 @@ pub fn create_thread(project_id: &str, name: Option<String>) -> WorkspaceThreadIthread_id}+/// Pick the thread a GUI window should fall back to after the mux window+/// showing its active thread died (e.g. `exit` in the thread's only pane):+/// the most recently used OTHER thread of the Space, or a brand-new thread+/// in the active project when no other thread is left. Returns None when+/// the Space (or any project to create a thread in) no longer exists.+pub fn thread_to_recover_after_window_death(space_id: &str) -> Option<WorkspaceThreadId> {+ let mut store = THREAD_STORE.lock();+ store.normalize_after_load();+ if !store.has_space(space_id) {+ return None;+ }+ let active_project_id = store.active_project_id_for_space(space_id);+ let dead_thread_id = active_project_id+ .as_ref()+ .and_then(|project_id| store.projects.iter().find(|p| &p.id == project_id))+ .and_then(|project| project.active_thread_id.clone());++ let mut best: Option<(i64, WorkspaceThreadId)> = None;+ for project in store.projects.iter().filter(|p| p.space_id == space_id) {+ for thread in &project.threads {+ if dead_thread_id.as_deref() == Some(thread.id.as_str()) {+ continue;+ }+ if best+ .as_ref()+ .map_or(true, |(ts, _)| thread.last_active_at > *ts)+ {+ best = Some((thread.last_active_at, thread.id.clone()));+ }+ }+ }+ if let Some((_, thread_id)) = best {+ return Some(thread_id);+ }++ let project_id = active_project_id+ .filter(|project_id| {+ store+ .projects+ .iter()+ .any(|p| p.space_id == space_id && &p.id == project_id)+ })+ .or_else(|| {+ store+ .projects+ .iter()+ .find(|p| p.space_id == space_id)+ .map(|p| p.id.clone())+ })?;+ let thread_id = store.create_thread(&project_id, None);+ persist_locked(&store);+ Some(thread_id)+}+pub fn create_project_from_path(space_id: &str, path: &str) -> Result<WorkspaceThreadId> {let path = normalize_project_path(path)?;let mut store = THREAD_STORE.lock();