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 remote pane drag synchronization

RoversX02ba44e51b89
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 16709508..2ea649a8 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -442,7 +442,8 @@ macro_rules! pdu {
/// This must be bumped when backwards incompatible changes
/// are made to the types and protocol.
/// 47: PaneStackEntry gained pane_stack_id; stack operation PDUs.
-pub const CODEC_VERSION: usize = 47;
+/// 48: MovePaneToStack moves an existing pane into another pane stack.
+pub const CODEC_VERSION: usize = 48;
// Defines the Pdu enum.
// Each struct has an explicit identifying number.
@@ -505,6 +506,7 @@ pdu! {
AdjustPaneSize: 62,
SpawnPaneInStack: 63,
ActivatePaneInStack: 64,
+ MovePaneToStack: 65,
}
impl Pdu {
@@ -522,7 +524,8 @@ impl Pdu {
| Self::SetPaneZoomed(_)
| Self::SpawnV2(_)
| Self::SpawnPaneInStack(_)
- | Self::ActivatePaneInStack(_) => true,
+ | Self::ActivatePaneInStack(_)
+ | Self::MovePaneToStack(_) => true,
_ => false,
}
}
@@ -683,6 +686,15 @@ pub struct ActivatePaneInStack {
pub pane_id: PaneId,
}
+/// Move an existing pane into the stack containing `target_pane_id`.
+/// Both ids are in the mux server's pane-id space. Responds with
+/// UnitResponse.
+#[derive(Deserialize, Serialize, PartialEq, Debug)]
+pub struct MovePaneToStack {
+ pub source_pane_id: PaneId,
+ pub target_pane_id: PaneId,
+}
+
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct MovePaneToNewTab {
pub pane_id: PaneId,
@@ -1209,6 +1221,28 @@ mod test {
);
}
+ #[test]
+ fn move_pane_to_stack_round_trip() {
+ let request = MovePaneToStack {
+ source_pane_id: 17,
+ target_pane_id: 29,
+ };
+ let mut encoded = Vec::new();
+ Pdu::MovePaneToStack(request)
+ .encode(&mut encoded, 0x41)
+ .unwrap();
+ assert_eq!(
+ Pdu::decode(encoded.as_slice()).unwrap(),
+ DecodedPdu {
+ serial: 0x41,
+ pdu: Pdu::MovePaneToStack(MovePaneToStack {
+ source_pane_id: 17,
+ target_pane_id: 29,
+ }),
+ }
+ );
+ }
+
#[test]
fn stream_decode() {
let mut encoded = Vec::new();
diff --git a/mux/src/domain.rs b/mux/src/domain.rs
index 5200a619..077c05b0 100644
--- a/mux/src/domain.rs
+++ b/mux/src/domain.rs
@@ -135,6 +135,26 @@ pub trait Domain: Downcast + Send + Sync {
self.spawn_pane(size, command, command_dir).await
}
+ /// Move an existing pane into the stack containing `target_pane_id`.
+ /// The default implementation mutates the local mux tree. Proxy domains
+ /// override this so the authoritative remote tree is changed as well.
+ async fn move_pane_to_stack(
+ &self,
+ src_pane_id: PaneId,
+ target_tab_id: TabId,
+ target_pane_id: PaneId,
+ ) -> anyhow::Result<Arc<dyn Pane>> {
+ let mux = Mux::get();
+ let tab = mux
+ .get_tab(target_tab_id)
+ .ok_or_else(|| anyhow::anyhow!("Invalid tab id {target_tab_id}"))?;
+ let pane = mux
+ .get_pane(src_pane_id)
+ .ok_or_else(|| anyhow::anyhow!("pane {src_pane_id} not found"))?;
+ tab.move_pane_to_stack(src_pane_id, target_pane_id)?;
+ Ok(pane)
+ }
+
/// The mux will call this method on the domain of the pane that
/// is being moved to give the domain a chance to handle the movement.
/// If this method returns Ok(None), then the mux will handle the
diff --git a/mux/src/lib.rs b/mux/src/lib.rs
index 63598cfb..94631253 100644
--- a/mux/src/lib.rs
+++ b/mux/src/lib.rs
@@ -1323,6 +1323,13 @@ impl Mux {
return Err(err);
}
+ // split_and_insert only selects the inserted pane when it is the
+ // right/bottom half. A move is an explicit user action, so make the
+ // moved pane active for all four directions. Doing this here also
+ // makes the server's authoritative tree agree with client mirrors
+ // before it is serialized for a resync.
+ target_tab.set_active_pane(&pane);
+
if src_tab.is_dead() {
self.remove_tab(src_tab_id);
}
@@ -1456,6 +1463,46 @@ impl Mux {
Ok(pane)
}
+ /// Move an existing pane into another pane stack in the same tab.
+ /// The domain owns the mutation so proxy domains can update their
+ /// authoritative server before the next resync.
+ pub async fn move_pane_to_stack(
+ &self,
+ src_pane_id: PaneId,
+ target_pane_id: PaneId,
+ ) -> anyhow::Result<Arc<dyn Pane>> {
+ if src_pane_id == target_pane_id {
+ anyhow::bail!("cannot move pane {src_pane_id} onto itself");
+ }
+
+ let (src_domain_id, _src_window_id, src_tab_id) = self
+ .resolve_pane_id(src_pane_id)
+ .ok_or_else(|| anyhow::anyhow!("pane {src_pane_id} not found"))?;
+ let (target_domain_id, _target_window_id, target_tab_id) = self
+ .resolve_pane_id(target_pane_id)
+ .ok_or_else(|| anyhow::anyhow!("pane {target_pane_id} not found"))?;
+
+ if src_domain_id != target_domain_id {
+ anyhow::bail!(
+ "cannot move pane {src_pane_id} from domain {src_domain_id} \
+ into domain {target_domain_id}"
+ );
+ }
+ if src_tab_id != target_tab_id {
+ anyhow::bail!(
+ "cannot move pane {src_pane_id} from tab {src_tab_id} \
+ into stack in tab {target_tab_id}"
+ );
+ }
+
+ let domain = self
+ .get_domain(target_domain_id)
+ .ok_or_else(|| anyhow::anyhow!("domain {target_domain_id} not found"))?;
+ domain
+ .move_pane_to_stack(src_pane_id, target_tab_id, target_pane_id)
+ .await
+ }
+
pub async fn move_pane_to_new_tab(
&self,
pane_id: PaneId,
diff --git a/mux/src/tab.rs b/mux/src/tab.rs
index b6062017..5a5e047f 100644
--- a/mux/src/tab.rs
+++ b/mux/src/tab.rs
@@ -2948,7 +2948,7 @@ impl TabInner {
// The only fallible step happens before the tree is touched: if the
// pane refuses to resize we bail with the layout intact.
- if let Some(dims) = target_dims {
+ if let Some(dims) = target_dims.filter(|_| !src_pane.is_remote_mirror()) {
src_pane.resize(dims)?;
}
@@ -2969,7 +2969,10 @@ impl TabInner {
if cursor.is_leaf() {
let stack = cursor.leaf_mut().unwrap();
if stack.contains_pane(target_pane_id) {
- if let Some(base) = stack.active_pane() {
+ if let Some(base) = stack
+ .active_pane()
+ .filter(|_| !removed.is_remote_mirror())
+ {
let dims = base.get_dimensions();
if let Err(err) = removed.resize(TerminalSize {
rows: dims.viewport_rows,
@@ -4099,6 +4102,51 @@ mod test {
assert!(tab.pane_index_for_pane(src.pane_id()).is_some());
}
+ #[test]
+ fn mux_move_pane_to_split_activates_moved_pane_in_all_directions() {
+ for (direction, target_is_second) in [
+ (SplitDirection::Horizontal, false),
+ (SplitDirection::Horizontal, true),
+ (SplitDirection::Vertical, false),
+ (SplitDirection::Vertical, true),
+ ] {
+ let _guard = install_mux();
+ let mux = Mux::get();
+ let size = test_size();
+
+ let src_tab = Arc::new(Tab::new(&size));
+ let src = FakePane::new(10_031, size);
+ src_tab.assign_pane(&src);
+ mux.add_tab_no_panes(&src_tab);
+ mux.add_pane(&src).unwrap();
+
+ let target_tab = Arc::new(Tab::new(&size));
+ let target = FakePane::new(10_032, size);
+ target_tab.assign_pane(&target);
+ mux.add_tab_no_panes(&target_tab);
+ mux.add_pane(&target).unwrap();
+
+ mux.move_pane_to_split(
+ src.pane_id(),
+ target_tab.tab_id(),
+ target.pane_id(),
+ SplitRequest {
+ direction,
+ target_is_second,
+ top_level: false,
+ size: Default::default(),
+ },
+ )
+ .unwrap();
+
+ assert_eq!(
+ target_tab.get_active_pane().unwrap().pane_id(),
+ src.pane_id(),
+ "{direction:?} second={target_is_second}"
+ );
+ }
+ }
+
#[test]
fn mux_move_pane_to_split_preflight_keeps_source_attached() {
let _guard = install_mux();
diff --git a/mux/src/tmux.rs b/mux/src/tmux.rs
index 7e221080..863817b8 100644
--- a/mux/src/tmux.rs
+++ b/mux/src/tmux.rs
@@ -379,11 +379,14 @@ impl Domain for TmuxDomain {
async fn split_pane(
&self,
- _source: SplitSource,
+ source: SplitSource,
tab: TabId,
pane_id: PaneId,
split_request: SplitRequest,
) -> anyhow::Result<Arc<dyn Pane>> {
+ if matches!(source, SplitSource::MovePane(_)) {
+ anyhow::bail!("moving an existing pane is not supported by TmuxDomain");
+ }
let mut promise = promise::Promise::new();
if let Some(future) = promise.get_future() {
{
@@ -401,6 +404,15 @@ impl Domain for TmuxDomain {
anyhow::bail!("Split_pane failed");
}
+ async fn move_pane_to_stack(
+ &self,
+ _src_pane_id: PaneId,
+ _target_tab_id: TabId,
+ _target_pane_id: PaneId,
+ ) -> anyhow::Result<Arc<dyn Pane>> {
+ anyhow::bail!("moving an existing pane is not supported by TmuxDomain")
+ }
+
async fn spawn_pane(
&self,
_size: TerminalSize,
diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rs
index 7c60ce44..1ceee344 100644
--- a/wezterm-client/src/client.rs
+++ b/wezterm-client/src/client.rs
@@ -1615,6 +1615,7 @@ impl Client {
rpc!(split_pane, SplitPane, SpawnResponse);
rpc!(spawn_pane_in_stack, SpawnPaneInStack, SpawnResponse);
rpc!(activate_pane_in_stack, ActivatePaneInStack, UnitResponse);
+ rpc!(move_pane_to_stack, MovePaneToStack, UnitResponse);
rpc!(
move_pane_to_new_tab,
MovePaneToNewTab,
diff --git a/wezterm-client/src/domain.rs b/wezterm-client/src/domain.rs
index 21cce632..2cc1b928 100644
--- a/wezterm-client/src/domain.rs
+++ b/wezterm-client/src/domain.rs
@@ -1060,6 +1060,97 @@ impl Domain for ClientDomain {
Ok(pane)
}
+ async fn move_pane_to_stack(
+ &self,
+ source_local_pane_id: PaneId,
+ target_tab_id: TabId,
+ target_local_pane_id: PaneId,
+ ) -> anyhow::Result<Arc<dyn Pane>> {
+ let inner = self
+ .inner()
+ .ok_or_else(|| anyhow!("domain is not attached"))?;
+ let _mutation = inner.begin_structure_mutation();
+ let mux = Mux::get();
+
+ let tab = mux
+ .get_tab(target_tab_id)
+ .ok_or_else(|| anyhow!("tab_id {target_tab_id} is invalid"))?;
+ if tab.pane_index_for_pane(source_local_pane_id).is_none()
+ || tab.pane_index_for_pane(target_local_pane_id).is_none()
+ {
+ bail!(
+ "remote stack move requires source pane {source_local_pane_id} and \
+ target pane {target_local_pane_id} to be in tab {target_tab_id}"
+ );
+ }
+
+ let source_pane = mux
+ .get_pane(source_local_pane_id)
+ .ok_or_else(|| anyhow!("source pane_id {source_local_pane_id} is invalid"))?;
+ let target_pane = mux
+ .get_pane(target_local_pane_id)
+ .ok_or_else(|| anyhow!("target pane_id {target_local_pane_id} is invalid"))?;
+ let source_client_pane =
+ source_pane
+ .downcast_ref::<ClientPane>()
+ .ok_or_else(|| {
+ anyhow!("source pane_id {source_local_pane_id} is not a ClientPane")
+ })?;
+ let target_client_pane =
+ target_pane
+ .downcast_ref::<ClientPane>()
+ .ok_or_else(|| {
+ anyhow!("target pane_id {target_local_pane_id} is not a ClientPane")
+ })?;
+ if !source_client_pane.belongs_to_client(&inner)
+ || !target_client_pane.belongs_to_client(&inner)
+ {
+ bail!("remote stack move panes belong to different client connections");
+ }
+
+ let source_remote_pane_id = remote_move_pane_id(
+ target_local_pane_id,
+ target_client_pane.remote_pane_id(),
+ source_local_pane_id,
+ source_client_pane.remote_pane_id(),
+ inner.local_domain_id,
+ source_pane.domain_id(),
+ )?;
+ let target_remote_pane_id = target_client_pane.remote_pane_id();
+ let target_remote_tab_id = target_client_pane.remote_tab_id();
+ if source_client_pane.remote_tab_id() != target_remote_tab_id {
+ inner.defer_resync();
+ bail!(
+ "remote stack move panes do not belong to the same remote tab: \
+ source={}, target={target_remote_tab_id}",
+ source_client_pane.remote_tab_id()
+ );
+ }
+
+ // This feature is restricted to one top-level tab. Apply the
+ // already-validated move locally before the network round-trip so
+ // dropping the drag preview does not reveal the old layout for one
+ // RTT. A failed RPC schedules an authoritative resync, which restores
+ // the server tree.
+ tab.move_pane_to_stack(source_local_pane_id, target_local_pane_id)?;
+
+ if let Err(err) = inner
+ .client
+ .move_pane_to_stack(codec::MovePaneToStack {
+ source_pane_id: source_remote_pane_id,
+ target_pane_id: target_remote_pane_id,
+ })
+ .await
+ {
+ inner.defer_resync();
+ return Err(err).context("moving remote pane into stack");
+ }
+
+ source_client_pane.set_remote_tab_id(target_remote_tab_id);
+ inner.defer_resync();
+ Ok(source_pane)
+ }
+
/// Forward the request to the remote; we need to translate the local ids
/// to those that match the remote for the request, resync the changed
/// structure, and then translate the results back to local
@@ -1197,12 +1288,13 @@ impl Domain for ClientDomain {
bail!("pane_id {pane_id} belongs to a stale client connection");
}
let target_remote_pane_id = pane.remote_pane_id();
+ let target_remote_tab_id = pane.remote_tab_id();
- let (command, command_dir, move_pane_id, moved_local_pane) = match source {
+ let (command, command_dir, move_pane_id, moved_local_pane, source_tab_id) = match source {
SplitSource::Spawn {
command,
command_dir,
- } => (command, command_dir, None, None),
+ } => (command, command_dir, None, None, None),
SplitSource::MovePane(source_local_pane_id) => {
let source_pane = mux
.get_pane(source_local_pane_id)
@@ -1230,17 +1322,42 @@ impl Domain for ClientDomain {
.ok_or_else(|| anyhow!("pane_id {pane_id} is not in tab {tab_id}"))?;
tab.validate_split_request(target_index, split_request)
.context("remote MovePane split preflight failed")?;
+ let (_, _, source_tab_id) = mux
+ .resolve_pane_id(source_local_pane_id)
+ .ok_or_else(|| anyhow!("source pane_id {source_local_pane_id} is invalid"))?;
(
None,
None,
Some(remote_source_pane_id),
Some((source_pane, remote_source_pane_id)),
+ Some(source_tab_id),
)
}
};
- let result = inner
+ // Pane-tab dragging is currently restricted to one top-level tab.
+ // Mirror that common case before waiting for the RPC so the old
+ // layout is never exposed between the drag overlay disappearing and
+ // the server response. Cross-tab callers retain the conservative
+ // post-response update below.
+ let optimistic_move_applied = if let Some((moved_pane, _)) = moved_local_pane.as_ref() {
+ if source_tab_id == Some(tab_id) {
+ mux.move_pane_to_split(
+ moved_pane.pane_id(),
+ tab_id,
+ pane_id,
+ split_request,
+ )?;
+ true
+ } else {
+ false
+ }
+ } else {
+ false
+ };
+
+ let result = match inner
.client
.split_pane(SplitPane {
domain: SpawnTabDomain::CurrentPaneDomain,
@@ -1250,7 +1367,17 @@ impl Domain for ClientDomain {
command_dir,
move_pane_id,
})
- .await?;
+ .await
+ {
+ Ok(result) => result,
+ Err(err) => {
+ // The transport may have failed after the server committed
+ // the mutation. Always fetch the authoritative tree instead
+ // of assuming that an RPC error means no structural change.
+ inner.defer_resync();
+ return Err(err);
+ }
+ };
if let Some((moved_pane, expected_remote_pane_id)) = moved_local_pane {
// The server must return the identity of the pane that it moved.
@@ -1264,20 +1391,31 @@ impl Domain for ClientDomain {
expected_remote_pane_id
);
}
+ if result.tab_id != target_remote_tab_id {
+ inner.defer_resync();
+ bail!(
+ "remote MovePane returned tab {}, expected {}",
+ result.tab_id,
+ target_remote_tab_id
+ );
+ }
let moved_client_pane = moved_pane
.downcast_ref::<ClientPane>()
.expect("MovePane source was validated as ClientPane");
moved_client_pane.set_remote_tab_id(result.tab_id);
- if let Err(err) =
- mux.move_pane_to_split(moved_pane.pane_id(), tab_id, pane_id, split_request)
- {
- // The server has already committed the move. The mux helper
- // guarantees that the local pane remains attached; force an
- // authoritative tree sync to converge on the remote result.
- inner.defer_resync();
- return Err(err).context("mirroring remote MovePane locally");
+ if !optimistic_move_applied {
+ if let Err(err) =
+ mux.move_pane_to_split(moved_pane.pane_id(), tab_id, pane_id, split_request)
+ {
+ // The server has already committed the move. The mux
+ // helper guarantees that the local pane remains attached;
+ // force an authoritative tree sync to converge on the
+ // remote result.
+ inner.defer_resync();
+ return Err(err).context("mirroring remote MovePane locally");
+ }
}
// Always converge with the server tree after the optimistic
diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs
index f9c2de2f..56103db8 100644
--- a/wezterm-gui/src/termwindow/mouseevent.rs
+++ b/wezterm-gui/src/termwindow/mouseevent.rs
@@ -2042,7 +2042,8 @@ impl super::TermWindow {
{
continue;
}
- // Remote mux / tmux panes cannot be split/moved into (v1)
+ // TmuxDomain cannot move existing panes; remote mux panes
+ // are supported by ClientDomain.
if !Self::pane_tab_is_movable(&pos.pane) {
break;
}
@@ -2140,11 +2141,37 @@ impl super::TermWindow {
match target.kind {
PaneDropKind::MoveIntoStack => {
- if let Err(err) = tab.move_pane_to_stack(src_pane_id, target.target_pane_id) {
- log::error!("pane tab drop move failed: {err:#}");
- return;
- }
- self.persist_workspace_layout_after_mutation("pane tab drop move");
+ let target_pane_id = target.target_pane_id;
+ let dest_tab_id = tab.tab_id();
+ let workspace = self.current_mux_workspace();
+ let window = GuiWin::new(self);
+ promise::spawn::spawn(async move {
+ match Mux::get()
+ .move_pane_to_stack(src_pane_id, target_pane_id)
+ .await
+ {
+ Ok(moved) => {
+ window.window.notify(TermWindowNotif::Apply(Box::new(
+ move |term_window| {
+ if let Some(tab) = Mux::get().get_tab(dest_tab_id) {
+ tab.set_active_pane(&moved);
+ }
+ if term_window.current_mux_workspace() == workspace {
+ term_window.persist_workspace_layout_after_mutation(
+ "pane tab drop move",
+ );
+ }
+ term_window.update_title();
+ if let Some(window) = term_window.window.as_ref() {
+ window.invalidate();
+ }
+ },
+ )));
+ }
+ Err(err) => log::error!("pane tab drop move failed: {err:#}"),
+ }
+ })
+ .detach();
}
PaneDropKind::Split => {
let stack_tabs = tab.pane_stack_tabs(target.target_pane_id);
diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rs
index b9c7a06b..fec34d09 100644
--- a/wezterm-mux-server-impl/src/sessionhandler.rs
+++ b/wezterm-mux-server-impl/src/sessionhandler.rs
@@ -781,6 +781,14 @@ impl SessionHandler {
.detach();
}
+ Pdu::MovePaneToStack(request) => {
+ let client_id = self.client_id.clone();
+ spawn_into_main_thread(async move {
+ schedule_move_pane_to_stack(request, send_response, client_id);
+ })
+ .detach();
+ }
+
Pdu::MovePaneToNewTab(request) => {
let client_id = self.client_id.clone();
spawn_into_main_thread(async move {
@@ -1109,6 +1117,25 @@ fn schedule_spawn_pane_in_stack<SND>(
.detach();
}
+fn schedule_move_pane_to_stack<SND>(
+ request: MovePaneToStack,
+ send_response: SND,
+ client_id: Option<Arc<ClientId>>,
+) where
+ SND: Fn(anyhow::Result<Pdu>) + 'static,
+{
+ promise::spawn::spawn(async move {
+ let mux = Mux::get();
+ let _identity = mux.with_identity(client_id);
+ send_response(
+ mux.move_pane_to_stack(request.source_pane_id, request.target_pane_id)
+ .await
+ .map(|_| Pdu::UnitResponse(UnitResponse {})),
+ );
+ })
+ .detach();
+}
+
async fn spawn_pane_in_stack(
request: SpawnPaneInStack,
client_id: Option<Arc<ClientId>>,