Skip to content

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.

wezterm-gui: keep thread layouts across the local-sessions setting

RoversX28850af0ca83
diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs
index 01b7585d..074d6a1a 100644
--- a/wezterm-gui/src/termwindow/mod.rs
+++ b/wezterm-gui/src/termwindow/mod.rs
@@ -2742,6 +2742,13 @@ impl TermWindow {
crate::workspace_threads::window_layout_structure_fingerprint(self.mux_window_id);
}
+ fn persist_workspace_layout_if_structure_changed(&mut self) {
+ let now = crate::workspace_threads::window_layout_structure_fingerprint(self.mux_window_id);
+ if now.is_some() && now != self.workspace_layout_structure_fingerprint {
+ self.persist_workspace_layout_after_mutation("tab structure changed");
+ }
+ }
+
pub(crate) fn persist_workspace_layout_after_mutation(&mut self, reason: &'static str) {
if let Some(workspace) = self.current_mux_workspace() {
if crate::workspace_threads::is_materializing_thread_layout(&workspace) {
@@ -2780,27 +2787,34 @@ impl TermWindow {
return false;
};
- // The local session host is not a remote to preserve: closing one
- // of its windows ends the terminals in it, as it does in process.
- let is_remote_client_domain = |domain: &Arc<dyn mux::domain::Domain>| {
- domain
- .downcast_ref::<ClientDomain>()
- .is_some_and(|client| !client.is_local_session_host())
+ // The local session host counts as a client domain here too: its
+ // terminals live in the session server, and closing a window is
+ // closing a view of them, as it is for a remote host. Closing a tab
+ // or a pane still ends what runs in it.
+ let is_client_domain = |domain: &Arc<dyn mux::domain::Domain>| {
+ domain.downcast_ref::<ClientDomain>().is_some()
};
let origin_client_domain = origin_domain
.and_then(|domain_id| mux.get_domain(domain_id))
- .filter(is_remote_client_domain)
+ .filter(is_client_domain)
.map(|domain| domain.domain_id());
let active_space_domain =
crate::workspace_threads::client_domain_for_space(&self.active_space_id)
.and_then(|domain_name| mux.get_domain_by_name(&domain_name))
- .filter(is_remote_client_domain)
+ .filter(is_client_domain)
.map(|domain| domain.domain_id());
+ // A window the layout restore made for the host carries no origin
+ // tag (`spawn_tab_or_window` tags nothing), so judge it by what it
+ // holds: every pane a mirror of the host's.
+ let host_pane_domain = client_pane_domains
+ .first()
+ .copied()
+ .filter(|domain_id| crate::local_sessions::is_host_domain_id(*domain_id));
preserve_mux_window_on_gui_close(
origin_client_domain,
- active_space_domain,
+ active_space_domain.or(host_pane_domain),
&pane_domains,
&client_pane_domains,
)
@@ -4618,6 +4632,13 @@ impl TermWindow {
MuxNotification::TabResized(_) => {
// Also handled by wezterm-client
self.update_title_post_status();
+ // A split lands here: PaneAdded goes out before the new
+ // pane is in the tab tree, and a mirror of a server-side
+ // split never sends it at all, so the snapshot "pane added"
+ // takes sees the old layout. Sizes are not part of the
+ // fingerprint, so live resizes cost a tree walk and no
+ // write.
+ self.persist_workspace_layout_if_structure_changed();
}
MuxNotification::TabTitleChanged { .. } => {
self.update_title_post_status();
@@ -4911,7 +4932,8 @@ impl TermWindow {
/// workspace has no mux window to adopt.
pub(crate) fn adopt_workspace_in_this_window(&mut self, workspace: &str) -> bool {
let mux = Mux::get();
- let Some(target) = mux.iter_windows_in_workspace(workspace).first().copied() else {
+ let Some(target) = crate::workspace_threads::window_to_show_in_workspace(workspace)
+ else {
return false;
};
mux.set_active_workspace(workspace);
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index 4b309027..831c55b5 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -6508,10 +6508,8 @@ impl super::TermWindow {
crate::workspace_threads::activation_plan_for_thread(&thread_id, &live_workspaces)
{
if !plan.needs_materialize {
- let target = mux
- .iter_windows_in_workspace(&plan.workspace_name)
- .into_iter()
- .next();
+ let target =
+ crate::workspace_threads::window_to_show_in_workspace(&plan.workspace_name);
if let Some(target) = target {
if target != self.mux_window_id {
if let Some(other) =
@@ -6939,12 +6937,17 @@ impl super::TermWindow {
{
self.show_project_root_problem(thread_id, space_id, display_name, failure);
}
+ // The workspaces this activation was told to kill are dead
+ // already (mirrors of a replaced server); a failed rebuild must
+ // not keep them for the life of the process.
+ kill_workspace_windows(&workspaces_to_kill_after_adopt, None);
release(self);
return;
}
if !self.adopt_workspace_in_this_window(&workspace) {
log::error!("materialized ThinkTerm workspace {workspace:?} has no window to adopt");
+ kill_workspace_windows(&workspaces_to_kill_after_adopt, None);
release(self);
return;
}
@@ -8752,7 +8755,7 @@ mod local_thread_activation_tests {
/// without disturbing any other GUI window already bound to the mux.
pub(crate) fn adopt_workspace_into_window(window: &Option<::window::Window>, workspace: &str) {
let mux = Mux::get();
- let Some(target) = mux.iter_windows_in_workspace(workspace).first().copied() else {
+ let Some(target) = crate::workspace_threads::window_to_show_in_workspace(workspace) else {
return;
};
mux.set_active_workspace(workspace);
diff --git a/wezterm-gui/src/termwindow/resize.rs b/wezterm-gui/src/termwindow/resize.rs
index 3d212928..5c1df3d8 100644
--- a/wezterm-gui/src/termwindow/resize.rs
+++ b/wezterm-gui/src/termwindow/resize.rs
@@ -1480,10 +1480,9 @@ impl super::TermWindow {
}
let ownership = self.tab_frontend_viewport_ownership(&tab);
- // The local session host has one screen, this one. A tab it does
- // not own yet (a lease left by a previous run of this GUI, or a tab
- // restored before anything was painted) is claimed the way a click
- // on the terminal claims it, so the same full sync follows.
+ // The local session host's lease is settled by the server: the
+ // device that was driving inherits it on its first viewport, so a
+ // tab this GUI does not own yet is claimed there, not here.
let previewing = matches!(
self.frontend_geometry_phases.get(&tab_id),
Some(super::FrontendGeometryPhase::Previewing { .. })
diff --git a/wezterm-gui/src/workspace_threads.rs b/wezterm-gui/src/workspace_threads.rs
index 608a28b8..655261af 100644
--- a/wezterm-gui/src/workspace_threads.rs
+++ b/wezterm-gui/src/workspace_threads.rs
@@ -8435,10 +8435,12 @@ pub fn client_domain_for_space(space_id: &str) -> Option<String> {
/// Parse a thread workspace name (`thinkterm:<project-id>:<thread-id>`,
/// optionally with a `:<remote-workspace>` suffix) back into its identity.
-/// Thread ids never contain `:`, project ids may (`::space::`).
+/// Thread ids never contain `:`, project ids may (`::space::`). Threads
+/// made before the id scheme settled carry a `session-` id; stores still
+/// hold them, so they are threads too.
pub(crate) fn parse_thread_workspace_name(workspace: &str) -> Option<(String, String)> {
let rest = workspace.strip_prefix("thinkterm:")?;
- let idx = rest.find(":thread-")?;
+ let idx = rest.find(":thread-").or_else(|| rest.find(":session-"))?;
let project_id = &rest[..idx];
let thread_id = rest[idx + 1..].split(':').next()?;
if project_id.is_empty() {
@@ -8447,6 +8449,42 @@ pub(crate) fn parse_thread_workspace_name(workspace: &str) -> Option<(String, St
Some((project_id.to_string(), thread_id.to_string()))
}
+/// The mux window a GUI window should show for `workspace`. A workspace
+/// normally has one; when a server holds more than one for the same thread
+/// (duplicates left by an earlier mirror rebuild), the one with the most
+/// panes is the one the user was working in. Ties go to the lowest id on
+/// the server, which is stable across launches and takeovers, unlike the
+/// local mirror ids, which follow the order the server listed them in.
+pub(crate) fn window_to_show_in_workspace(workspace: &str) -> Option<mux::window::WindowId> {
+ let mux = mux::Mux::get();
+ let rank = |window_id: mux::window::WindowId| {
+ let Some(window) = mux.get_window(window_id) else {
+ return (0, std::cmp::Reverse((true, window_id)));
+ };
+ let pane_count: usize = window
+ .iter()
+ .map(|tab| tab.count_panes().unwrap_or(0))
+ .sum();
+ let remote_id = window
+ .iter()
+ .flat_map(|tab| tab.iter_all_panes())
+ .next()
+ .and_then(|pane| mux.get_domain(pane.domain_id()))
+ .and_then(|domain| {
+ domain
+ .downcast_ref::<wezterm_client::domain::ClientDomain>()
+ .and_then(|client| client.local_to_remote_window_id(window_id))
+ });
+ (
+ pane_count,
+ std::cmp::Reverse((remote_id.is_none(), remote_id.unwrap_or(window_id))),
+ )
+ };
+ mux.iter_windows_in_workspace(workspace)
+ .into_iter()
+ .max_by_key(|window_id| rank(*window_id))
+}
+
/// Whether the workspace is managed by ThinkTerm's Space/thread store.
///
/// Client domains mirror every server-side workspace into the local mux,
@@ -12673,6 +12711,24 @@ mod tests {
);
}
+ /// Stores from before the id scheme settled hold `session-` thread ids;
+ /// their workspaces are thread workspaces like any other.
+ #[test]
+ fn legacy_session_ids_name_thread_workspaces() {
+ let ws =
+ workspace_name_for_thread("project-102e49b953fef4dc", "session-1779804751445-cafe1753");
+ assert_eq!(
+ parse_thread_workspace_name(&ws),
+ Some((
+ "project-102e49b953fef4dc".to_string(),
+ "session-1779804751445-cafe1753".to_string()
+ ))
+ );
+ assert!(is_thread_workspace_name(&ws));
+ assert!(!is_thread_workspace_name("thinkterm:project-1:other-1"));
+ assert!(!is_thread_workspace_name("default"));
+ }
+
/// The local store and `codec::apply_op` implement the same row-ordering
/// and pin rules in two different places. If they ever drift, a drag would
/// look right until the server's push snapped it back.