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.

Fix tab/stack selection mixing over mux: local selection is authoritative

RoversXb596f3592d35
diff --git a/mux/src/tab.rs b/mux/src/tab.rs
index 36a9a30e..2349eecb 100644
--- a/mux/src/tab.rs
+++ b/mux/src/tab.rs
@@ -1164,32 +1164,83 @@ impl TabInner {
geometry_preserved,
self.size
);
- let mut cursor = t.cursor();
-
- self.active = 0;
- if let Some(active) = active {
- // Resolve the active pane to its index
+ // Capture the locally-selected state before adopting the rebuilt
+ // tree: which pane each surviving stack shows, and which stack
+ // holds the tab's focus. The wire's active markers describe the
+ // server's (possibly stale) snapshot; adopting them would flip a
+ // level-2 tab selection that the user changed while this resync
+ // was already in flight, with nothing left to switch it back.
+ // Genuinely external focus changes still arrive via PaneFocused,
+ // so preferring the local selection here does not hide them.
+ let mut prior_stack_actives: HashMap<PaneStackId, PaneId> = HashMap::new();
+ let mut prior_active_stack: Option<PaneStackId> = None;
+ if let Some(old) = self.pane.as_ref() {
+ fn walk(
+ tree: &Tree,
+ index: &mut usize,
+ active_index: usize,
+ actives: &mut HashMap<PaneStackId, PaneId>,
+ active_stack: &mut Option<PaneStackId>,
+ ) {
+ match tree {
+ Tree::Empty => {}
+ Tree::Leaf(stack) => {
+ if let Some(pane) = stack.active_pane() {
+ actives.insert(stack.id(), pane.pane_id());
+ }
+ if *index == active_index {
+ active_stack.replace(stack.id());
+ }
+ *index += 1;
+ }
+ Tree::Node { left, right, .. } => {
+ walk(left, index, active_index, actives, active_stack);
+ walk(right, index, active_index, actives, active_stack);
+ }
+ }
+ }
let mut index = 0;
- loop {
- if let Some(stack) = cursor.leaf_mut() {
+ walk(
+ old,
+ &mut index,
+ self.active,
+ &mut prior_stack_actives,
+ &mut prior_active_stack,
+ );
+ }
+
+ let mut cursor = t.cursor();
+ let mut wire_active_index = None;
+ let mut prior_active_index = None;
+ let mut index = 0;
+ loop {
+ if let Some(stack) = cursor.leaf_mut() {
+ // Restore the locally-selected pane in stacks that survived
+ // the rebuild (set_active_pane leaves the wire selection in
+ // place when that pane is no longer a member).
+ if let Some(pane_id) = prior_stack_actives.get(&stack.id()) {
+ stack.set_active_pane(*pane_id);
+ }
+ if let Some(active) = &active {
if stack.contains_pane(active.pane_id()) {
- // Found it
- self.active = index;
- self.recency.tag(index);
- break;
+ wire_active_index.get_or_insert(index);
}
- index += 1;
}
- match cursor.preorder_next() {
- Ok(c) => cursor = c,
- Err(c) => {
- // Didn't find it
- cursor = c;
- break;
- }
+ if prior_active_stack == Some(stack.id()) {
+ prior_active_index.get_or_insert(index);
+ }
+ index += 1;
+ }
+ match cursor.preorder_next() {
+ Ok(c) => cursor = c,
+ Err(c) => {
+ cursor = c;
+ break;
}
}
}
+ self.active = prior_active_index.or(wire_active_index).unwrap_or(0);
+ self.recency.tag(self.active);
self.pane.replace(cursor.tree());
self.zoomed = zoomed;
@@ -3757,6 +3808,44 @@ mod test {
assert!(tab.pane_stack_id(200).is_some());
}
+ #[test]
+ fn sync_with_pane_tree_keeps_local_selection_over_stale_wire_active() {
+ let size = test_size();
+ let tab = Tab::new(&size);
+
+ let make_root = |active: usize| {
+ PaneNode::Stack(PaneStackEntry {
+ active,
+ panes: vec![
+ pane_entry(200, size, active == 0),
+ pane_entry(201, size, active == 1),
+ ],
+ pane_stack_id: Some(7),
+ })
+ };
+
+ // The local selection is the second pane (e.g. the user clicked
+ // the second level-2 tab).
+ tab.sync_with_pane_tree(size, make_root(1), |entry| {
+ FakePane::new(entry.pane_id, entry.size)
+ });
+
+ // A resync whose snapshot predates the switch (wire still says the
+ // first pane is active) must not flip the selection back.
+ tab.sync_with_pane_tree(size, make_root(0), |entry| {
+ FakePane::new(entry.pane_id, entry.size)
+ });
+
+ let panes = tab.iter_panes();
+ assert_eq!(panes.len(), 1);
+ assert_eq!(panes[0].pane.pane_id(), 201);
+
+ let tabs = tab.pane_stack_tabs(201);
+ assert_eq!(tabs.len(), 2);
+ assert!(!tabs[0].is_active);
+ assert!(tabs[1].is_active);
+ }
+
fn is_send_and_sync<T: Send + Sync>() -> bool {
true
}
diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs
index dc9c099b..d8cb9e0d 100644
--- a/wezterm-client/src/domain.rs
+++ b/wezterm-client/src/domain.rs
@@ -31,6 +31,10 @@ pub struct ClientInner {
/// each remote stack's local id stable across resyncs.
remote_to_local_stack: Mutex<HashMap<usize, usize>>,
pub focused_remote_pane_id: Mutex<Option<PaneId>>,
+ /// When we last advised the server of a focus change. Used to discard
+ /// stale PaneFocused echoes that would otherwise yank the active tab
+ /// and stack away from a newer local selection.
+ pub focus_advised_at: Mutex<Option<std::time::Instant>>,
}
impl ClientInner {
@@ -276,6 +280,7 @@ impl ClientInner {
remote_to_local_pane: Mutex::new(HashMap::new()),
remote_to_local_stack: Mutex::new(HashMap::new()),
focused_remote_pane_id: Mutex::new(None),
+ focus_advised_at: Mutex::new(None),
}
}
}
diff --git a/wezterm-client/src/pane/clientpane.rs b/wezterm-client/src/pane/clientpane.rs
index e32fe4b7..3cf88934 100644
--- a/wezterm-client/src/pane/clientpane.rs
+++ b/wezterm-client/src/pane/clientpane.rs
@@ -224,19 +224,41 @@ impl ClientPane {
}
Pdu::PaneFocused(PaneFocused { pane_id }) => {
// We get here whenever the pane focus is changed on the
- // server. That might be due to the user here in the GUI
- // doing things, or it may be due to a "remote"
- // `wezterm cli activate-pane-direction` or similar call
- // from some other actor.
- // The latter case is the important one: it is desirable
- // for the focus change to be reflected locally after it
- // has been changed on the server, so we work to apply
- // it here.
+ // server. That might be an echo of a focus change we
+ // advised ourselves, or a "remote" `wezterm cli
+ // activate-pane-direction` style call from some other
+ // actor. Applying it yanks both the window's active tab
+ // and the pane stack's active pane, so a STALE echo (of an
+ // advisory older than the user's latest selection) must be
+ // discarded or rapid tab/stack switching visibly flips
+ // between the old and new selections. An echo that matches
+ // our latest advisory is applied: it is normally a no-op,
+ // and it heals a resync that carried a pre-switch snapshot.
+ let advised = *self.client.focused_remote_pane_id.lock().unwrap();
+ let advised_recently = self
+ .client
+ .focus_advised_at
+ .lock()
+ .unwrap()
+ .map_or(false, |at| at.elapsed() < std::time::Duration::from_secs(3));
+ if advised != Some(self.remote_pane_id) && advised_recently {
+ log::trace!(
+ "ignoring stale remote pane focus {pane_id}: \
+ newer local advisory for {advised:?} is in flight"
+ );
+ return Ok(());
+ }
log::trace!("advised of remote pane focus: {pane_id}");
let mux = Mux::get();
if let Err(err) = mux.focus_pane_and_containing_tab(self.local_pane_id) {
log::error!("Error reconciling remote PaneFocused notification: {err:#}");
+ } else if let Some((_domain, window_id, _tab)) =
+ mux.resolve_pane_id(self.local_pane_id)
+ {
+ // The reconcile flips tab/stack selection silently (to
+ // avoid focus advisory loops); nudge the GUI to repaint.
+ mux.notify(MuxNotification::WindowInvalidated(window_id));
}
}
_ => bail!("unhandled unilateral pdu: {:?}", pdu),
@@ -628,6 +650,11 @@ impl Pane for ClientPane {
let mut focused_pane = self.client.focused_remote_pane_id.lock().unwrap();
if *focused_pane != Some(self.remote_pane_id) {
focused_pane.replace(self.remote_pane_id);
+ self.client
+ .focus_advised_at
+ .lock()
+ .unwrap()
+ .replace(std::time::Instant::now());
let client = Arc::clone(&self.client);
let remote_pane_id = self.remote_pane_id;
promise::spawn::spawn(async move {