Preserve SSH connection settings when opening new windows

Forward the invoking shell's remote executable and agent through the Linux prefork launcher. Resolve mounted tabs by stable ID for the New Window shortcut and reconnect to nested destinations from the local host.
This commit is contained in:
James Magahern
2026-11-17 16:20:54 -08:00
parent 153838061e
commit cc2acb51e4
9 changed files with 408 additions and 125 deletions
+92 -16
View File
@@ -14,7 +14,7 @@ pub(super) enum RemoteEvent {
}
pub(super) struct RemoteSession {
destination: String,
target: RemoteWindow,
anchor: TabId,
slot: TabId,
cancelled: Arc<AtomicBool>,
@@ -52,6 +52,15 @@ impl RemoteSession {
(self.latest.is_none() && self.slot == local) || original(&self.tabs, local).is_some()
}
fn nested_tab(&self, local: TabId) -> Option<&TabInfo> {
let id = original(&self.tabs, local)?;
self.latest
.as_ref()?
.tabs
.iter()
.find(|tab| tab.id == id && tab.remote.is_some())
}
pub(super) fn send(&self, command: ClientMessage) -> Result<()> {
self.connection
.as_ref()
@@ -169,7 +178,7 @@ impl RemoteSession {
for tab in &mut mapped_tabs {
tab.id = mapped(&mut new_tab_ids, tab.id, next);
if tab.remote.is_none() {
tab.remote = Some(self.destination.clone());
tab.remote = Some(self.target.destination.clone());
}
for pane in &mut tab.panes {
*pane = mapped(&mut new_pane_ids, *pane, next);
@@ -285,6 +294,10 @@ impl SessionState {
let session = self.id;
let sender = self.sender.clone();
let dest = destination.clone();
let window_target = RemoteWindow {
destination,
ssh: ssh.clone(),
};
let cancelled = Arc::new(AtomicBool::new(false));
let worker_cancelled = cancelled.clone();
thread::Builder::new()
@@ -321,7 +334,7 @@ impl SessionState {
}
}
})?;
self.insert_remote_slot(index, group, destination, cancelled);
self.insert_remote_slot(index, group, window_target, cancelled);
Ok(())
}
@@ -329,7 +342,7 @@ impl SessionState {
&mut self,
index: usize,
group: u64,
destination: String,
target: RemoteWindow,
cancelled: Arc<AtomicBool>,
) {
let anchor = self.tabs[index].id;
@@ -338,7 +351,7 @@ impl SessionState {
TabInfo {
id: group,
title: "Connecting…".into(),
remote: Some(destination.clone()),
remote: Some(target.destination.clone()),
manual_title: None,
panes: Vec::new(),
active_pane: 0,
@@ -352,7 +365,7 @@ impl SessionState {
self.remotes.insert(
group,
RemoteSession {
destination,
target,
anchor,
slot: group,
cancelled,
@@ -366,6 +379,28 @@ impl SessionState {
);
}
pub(super) fn remote_window_for_tab(
&self,
tab: TabId,
fallback: SshOptions,
) -> Result<RemoteWindow> {
let remote = self
.remotes
.values()
.find(|remote| remote.owns_tab(tab))
.context("tab is no longer attached to an SSH session")?;
if let Some(destination) = remote
.nested_tab(tab)
.and_then(|nested| nested.remote.clone())
{
return Ok(RemoteWindow {
destination,
ssh: fallback,
});
}
Ok(remote.target.clone())
}
pub(super) fn detach_remote(&mut self, tab: TabId) -> Result<Option<DetachedRemote>> {
let (&group, remote) = self
.remotes
@@ -374,11 +409,8 @@ impl SessionState {
.context("tab is not attached to an SSH session")?;
// A nested SSH tab is owned by the upstream workspace. Detach it
// there so the enclosing SSH session and its other tabs stay open.
if let Some(frame) = &remote.latest
&& let Some(id) = original(&remote.tabs, tab)
&& frame.tabs.iter().any(|t| t.id == id && t.remote.is_some())
{
remote.send(ClientMessage::DetachRemote { tab: id })?;
if let Some(nested) = remote.nested_tab(tab) {
remote.send(ClientMessage::DetachRemote { tab: nested.id })?;
return Ok(None);
}
let target = remote.connection.as_ref().and_then(|client| {
@@ -386,9 +418,9 @@ impl SessionState {
.remote_identity()
.or_else(|| remote.latest.as_ref()?.lineage.first().copied())?;
Some(DetachedRemote {
destination: remote.destination.clone(),
destination: remote.target.destination.clone(),
identity,
executable: client.remote_executable().map(str::to_owned),
executable: remote.target.ssh.executable.clone(),
})
});
self.remove_remote(group);
@@ -526,7 +558,7 @@ impl Server {
let mut closed = update.disconnected;
let mut error = update
.error
.map(|error| format!("{}: {error}", remote.destination));
.map(|error| format!("{}: {error}", remote.target.destination));
if let Some(snapshot) = update.snapshot {
closed |= snapshot.tabs.is_empty();
match remote.apply(
@@ -540,7 +572,7 @@ impl Server {
closed = true;
error = Some(format!(
"Invalid layout from {}: {reason}",
remote.destination
remote.target.destination
));
}
}
@@ -587,7 +619,10 @@ mod tests {
local.insert_remote_slot(
anchor,
group,
"user@work".into(),
RemoteWindow {
destination: "user@work".into(),
ssh: SshOptions::default(),
},
Arc::new(AtomicBool::new(false)),
);
group
@@ -603,6 +638,47 @@ mod tests {
)
}
#[test]
fn remote_window_uses_the_selected_mount_and_nested_destination() {
let mut local = session(1);
local.new_tab(None).unwrap();
let first = reserve(&mut local, 0);
let second = reserve(&mut local, 0); // Same host, different credentials.
let ssh = SshOptions {
executable: Some("/mount/bin/psyterm".into()),
agent_socket: Some("/mount/agent.sock".into()),
};
local.remotes.get_mut(&second).unwrap().target.ssh = ssh.clone();
let fallback = SshOptions {
executable: Some("/caller/bin/psyterm".into()),
agent_socket: Some("/caller/agent.sock".into()),
};
let target = local
.remote_window_for_tab(first, fallback.clone())
.unwrap();
assert!(target.ssh.executable.is_none());
let target = local
.remote_window_for_tab(second, fallback.clone())
.unwrap();
assert_eq!(target.destination, "user@work");
assert_eq!(target.ssh.executable, ssh.executable);
assert_eq!(target.ssh.agent_socket, ssh.agent_socket);
let mut outer = session(2);
outer.new_tab(None).unwrap();
let mut frame = outer.snapshot();
frame.tabs[0].remote = Some("inner".into());
apply(&mut local, second, frame).unwrap();
let target = local
.remote_window_for_tab(second, fallback.clone())
.unwrap();
assert_eq!(target.destination, "inner");
assert_eq!(target.ssh.executable, fallback.executable);
assert_eq!(target.ssh.agent_socket, fallback.agent_socket);
local.remove_remote(second);
assert!(local.remote_window_for_tab(second, fallback).is_err());
}
fn receive_updates(updates: crate::client::Updates) -> mpsc::Receiver<Update> {
let (frames, receive) = mpsc::channel();
thread::spawn(move || {