From f2390ca9b479c3ad26b6a739652db92497d28617 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 17 Nov 2026 16:13:02 -0800 Subject: [PATCH] Keep SSH pane focus and visible grids in sync --- src/client.rs | 2 +- src/server.rs | 9 ++- src/server/remote.rs | 171 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 142 insertions(+), 40 deletions(-) diff --git a/src/client.rs b/src/client.rs index 2d6e550..2712bb2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -409,7 +409,7 @@ fn shell_quote(value: &str) -> String { format!("'{}'", value.replace('\'', "'\\''")) } -fn validate_snapshot(snapshot: &Snapshot) -> Result<()> { +pub(crate) fn validate_snapshot(snapshot: &Snapshot) -> Result<()> { if snapshot.tabs.len() > 256 || snapshot.lineage.len() > 256 || snapshot.panes.len() > 64 { bail!("Server layout exceeds client limits"); } diff --git a/src/server.rs b/src/server.rs index b67ecb7..2cba074 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1194,17 +1194,20 @@ impl SessionState { fn snapshot(&mut self) -> Snapshot { self.update_titles(); self.revision += 1; + let mut tabs = self.tabs.clone(); if let Some(remote) = self .remotes .values() .find(|remote| remote.owns_tab(self.active_tab)) + && let Some(active) = tabs.iter_mut().find(|tab| tab.id == self.active_tab) { + let panes = remote.visible_panes(active); return Snapshot { revision: self.revision, lineage: self.lineage(), - tabs: self.tabs.clone(), + tabs, active_tab: self.active_tab, - panes: remote.visible_panes(self.active_tab), + panes, }; } let visible = self @@ -1269,7 +1272,7 @@ impl SessionState { Snapshot { lineage: self.lineage(), revision: self.revision, - tabs: self.tabs.clone(), + tabs, active_tab: self.active_tab, panes, } diff --git a/src/server/remote.rs b/src/server/remote.rs index 9df6c47..1376b30 100644 --- a/src/server/remote.rs +++ b/src/server/remote.rs @@ -100,21 +100,31 @@ impl RemoteSession { } } - pub(super) fn visible_panes(&self, local: TabId) -> Vec { - self.latest - .as_ref() - .filter(|s| self.tabs.get(&s.active_tab) == Some(&local)) - .map(|s| { - s.panes - .iter() - .filter_map(|pane| { - let mut copy = pane.clone(); - copy.id = *self.panes.get(&pane.id)?; - Some(copy) - }) - .collect() + pub(super) fn visible_panes(&self, tab: &mut TabInfo) -> Vec { + let Some(frame) = self.latest.as_ref() else { + return Vec::new(); + }; + let Some(confirmed) = frame + .active_tab() + .filter(|remote| self.tabs.get(&remote.id) == Some(&tab.id) && !remote.is_connecting()) + else { + return Vec::new(); + }; + // The actor tracks requested focus so consecutive shortcuts can navigate + // before an SSH round trip. Publish focus and grids from the same frame. + tab.active_pane = self.panes[&confirmed.active_pane]; + for (stack, confirmed) in tab.stacks.iter_mut().zip(&confirmed.stacks) { + stack.active_pane = self.panes[&confirmed.active_pane]; + } + frame + .panes + .iter() + .map(|pane| { + let mut copy = pane.clone(); + copy.id = self.panes[&pane.id]; + copy }) - .unwrap_or_default() + .collect() } /// Reconcile a whole remote session, including other tabs that may already @@ -438,21 +448,20 @@ impl SessionState { *tab = original(&remote.tabs, *tab)?; Some(remote) }), - NewStackPane { pane, .. } => { - let Some(local) = (*pane).or_else(|| { - self.tabs - .iter() - .find(|t| t.id == self.active_tab) - .map(|t| t.active_pane) - }) else { - return Ok(Some(command)); - }; - self.remote_pane(local).map(|(remote, original)| { - *pane = Some(original); - remote - }) + // These commands stay within a tab. Let the owner resolve the + // current pane after all preceding focus requests on its stream. + FocusDirection { + direction: Direction::Up | Direction::Down, } - Input { pane, .. } + | CyclePane { .. } + | NewStackPane { pane: None, .. } => self + .remotes + .values_mut() + .find(|remote| original(&remote.tabs, self.active_tab).is_some()), + NewStackPane { + pane: Some(pane), .. + } + | Input { pane, .. } | Resize { pane, .. } | Selection { pane, .. } | Copy { pane } @@ -584,6 +593,103 @@ mod tests { ) } + fn receive_updates(updates: crate::client::Updates) -> mpsc::Receiver { + let (frames, receive) = mpsc::channel(); + thread::spawn(move || { + while let Some(update) = updates.recv_blocking() { + if frames.send(update).is_err() { + break; + } + } + }); + receive + } + + fn next_frame( + receive: &mpsc::Receiver, + accept: impl Fn(&Snapshot) -> bool, + ) -> Arc { + loop { + let update = receive.recv_timeout(Duration::from_secs(5)).unwrap(); + assert!(update.error.is_none(), "{:?}", update.error); + if let Some(frame) = update.snapshot + && accept(&frame) + { + return frame; + } + } + } + + #[test] + fn stacked_remote_focus_publishes_matching_grids_while_requests_are_in_flight() { + let upstream = super::super::tests::TestServer::start(); + let (client, updates) = + Transport::connect(&upstream.socket, None, SessionTarget::New { cwd: None }).unwrap(); + let receive = receive_updates(updates); + for _ in 0..3 { + client + .send(ClientMessage::NewStackPane { + pane: None, + direction: VerticalDirection::Down, + cwd: None, + }) + .unwrap(); + } + let initial = next_frame(&receive, |frame| { + frame.active_tab().unwrap().panes.len() == 4 + }); + let upstream_top = initial.active_tab().unwrap().panes[0]; + let upstream_third = initial.active_tab().unwrap().panes[2]; + let mut local = session(1); + local.new_tab(None).unwrap(); + let origin = local.active_tab; + let group = reserve(&mut local, 0); + local.remotes.get_mut(&group).unwrap().connection = Some(client); + apply(&mut local, group, (*initial).clone()).unwrap(); + let top = local.tabs[1].panes[0]; + let second = local.tabs[1].panes[1]; + assert_eq!(local.tabs[1].active_pane, local.tabs[1].panes[3]); + + let output = Arc::new(Outbox::default()); + let check_frame = |local: &mut SessionState| { + let frame = local.snapshot(); + crate::client::validate_snapshot(&frame).unwrap(); + let tab = frame.active_tab().unwrap(); + assert_eq!(frame.panes[0].id, tab.stacks[0].active_pane); + assert_eq!(frame.panes[0].id, tab.active_pane); + }; + let up = || ClientMessage::FocusDirection { + direction: Direction::Up, + }; + local.command(up(), &output).unwrap(); + check_frame(&mut local); + // Hold the reply to Option+Up while a click selects a different + // collapsed pane. Both intermediate snapshots must remain renderable. + let third = next_frame(&receive, |frame| { + frame.active_tab().unwrap().active_pane == upstream_third + }); + local + .command(ClientMessage::FocusPane { pane: second }, &output) + .unwrap(); + check_frame(&mut local); + apply(&mut local, group, (*third).clone()).unwrap(); + // The next Option+Up must move from the clicked pane, even though + // the most recent upstream frame still shows the third pane. + local.command(up(), &output).unwrap(); + check_frame(&mut local); + local.focus_tab(origin).unwrap(); + let ack = next_frame(&receive, |frame| { + frame.active_tab().unwrap().active_pane == upstream_top + }); + apply(&mut local, group, (*ack).clone()).unwrap(); + assert_eq!(local.snapshot().active_tab, origin); + local.focus_tab(group).unwrap(); + let frame = local.snapshot(); + crate::client::validate_snapshot(&frame).unwrap(); + assert_eq!(frame.active_tab().unwrap().active_pane, top); + assert_eq!(frame.panes[0].id, top); + } + #[test] fn remote_ids_remain_stable_and_do_not_collide_with_local_tabs_or_other_mirrors() { let mut local = session(1); @@ -649,14 +755,7 @@ mod tests { let upstream = super::super::tests::TestServer::start(); let (client, updates) = Transport::connect(&upstream.socket, None, SessionTarget::New { cwd: None }).unwrap(); - let (frames, receive) = mpsc::channel(); - thread::spawn(move || { - while let Some(update) = updates.recv_blocking() { - if frames.send(update).is_err() { - break; - } - } - }); + let receive = receive_updates(updates); let mut local = session(1); local.new_tab(None).unwrap(); let group = reserve(&mut local, 0);