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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
mux: tell a registering client where the frontend lease stands
RoversXfdfa34672485
diff --git a/mux/src/lib.rs b/mux/src/lib.rsindex 0a3903fe..553b1be8 100644--- a/mux/src/lib.rs+++ b/mux/src/lib.rs@@ -1038,6 +1038,29 @@ impl Mux {Self::access_state_locked(&self.frontend_lease.lock())}+ /// The lease state of `tab_id` as it stands, for a client that has just+ /// arrived and heard none of the changes that led here. Nothing is+ /// bumped or published. A tab no renderer has ever claimed reports no+ /// owner and no view, which is itself an answer: it tells a renderer it+ /// is free to drive.+ pub fn frontend_viewport_state(&self, tab_id: TabId) -> Option<FrontendViewportState> {+ let tab = self.get_tab(tab_id)?;+ let lease = self.frontend_lease.lock();+ let access = Self::access_state_locked(&lease);+ let (owner, view, generation) = match lease.tabs.get(&tab_id) {+ Some(state) => (state.owner.clone(), state.view.clone(), state.generation),+ None => (None, None, 0),+ };+ Some(FrontendViewportState {+ tab_id,+ owner,+ canonical_size: tab.get_size(),+ view,+ generation,+ access,+ })+ }+/// Install the mode loaded by the mux-server persistence layer. This is/// intentionally one-shot so a configuration reload cannot overwrite a/// mode selected while this server process is running.diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rsindex 9288bf47..bccaf34e 100644--- a/wezterm-client/src/client.rs+++ b/wezterm-client/src/client.rs@@ -765,6 +765,11 @@ fn process_unilateral(Ok(())}+/// Whether the server answering the handshake is the process asking.+fn leads_back_to_this_process(own_server_id: Option<&str>, server_id: &str) -> bool {+ own_server_id.is_some_and(|own| own == server_id)+}+#[derive(Error, Debug, Clone, PartialEq, Eq)]enum NotReconnectableError {#[error("Client was destroyed")]@@ -2015,6 +2020,20 @@ impl Client {info.version_string,info.codec_vers);+ // A domain whose socket leads back to this very process. A+ // config shared between a mux server and its clients lists+ // the unix domain the server serves, and the server holds a+ // client domain for it too. Attaching it would mirror every+ // window this mux has as a new window in the same mux, which+ // the mirror then mirrors again, without end.+ let own_id = mux::Mux::try_get().map(|mux| mux.runtime_server_id().to_string());+ if leads_back_to_this_process(own_id.as_deref(), &info.server_id) {+ anyhow::bail!(+ "refusing to attach: this connection leads back to this very process \+ (server id {})",+ info.server_id+ );+ }if let Some(mismatch) =describe_server_build_mismatch(config::wezterm_version(), &info.version_string){@@ -2460,4 +2479,14 @@ mod handshake_classification_tests {assert!(msg.contains("transient"), "{msg}");assert!(msg.contains("NOT a version mismatch"), "{msg}");}++ #[test]+ fn a_server_with_our_own_id_is_never_attached() {+ assert!(super::leads_back_to_this_process(Some("abc"), "abc"));+ assert!(!super::leads_back_to_this_process(Some("abc"), "def"));+ assert!(+ !super::leads_back_to_this_process(None, "abc"),+ "a process without a mux of its own cannot be the server"+ );+ }}diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rsindex dc7535f6..f4777ea5 100644--- a/wezterm-mux-server-impl/src/sessionhandler.rs+++ b/wezterm-mux-server-impl/src/sessionhandler.rs@@ -536,6 +536,43 @@ impl SessionHandler {}}+ /// Tell a client that has just registered where the frontend lease+ /// stands: the access mode and every tab's ownership.+ ///+ /// The server otherwise pushes this state only when it changes, and a+ /// renderer will not publish its own viewport until it knows whether+ /// the tab is spoken for. A renderer reattaching to a tab that it, or+ /// its predecessor, used to drive therefore waited for a push that was+ /// never going to come, and sat behind "Restoring terminal state".+ /// Queued behind the registration's acknowledgement on the same+ /// channel, so it arrives after it.+ fn push_frontend_state(&self) {+ let mux = Mux::get();+ let mut pdus = vec![Pdu::FrontendAccessState(codec_access_state(+ mux.frontend_access_state(),+ ))];+ let tab_ids: Vec<TabId> = mux+ .iter_windows()+ .into_iter()+ .filter_map(|window_id| mux.get_window(window_id))+ .flat_map(|window| window.iter().map(|tab| tab.tab_id()).collect::<Vec<_>>())+ .collect();+ for tab_id in tab_ids {+ if let Some(state) = mux.frontend_viewport_state(tab_id) {+ pdus.push(Pdu::ClientViewportState(codec_viewport_state(state)));+ }+ }+ for pdu in pdus {+ if self+ .to_write_tx+ .send(DecodedPdu { serial: 0, pdu })+ .is_err()+ {+ return;+ }+ }+ }+pub(crate) fn per_pane(&mut self, pane_id: PaneId) -> Arc<Mutex<PerPane>> {Arc::clone(self.per_pane@@ -659,7 +696,10 @@ impl SessionHandler {self.client_id.replace(client_id);self.client_registration.replace(registration);}- send_response(Ok(Pdu::UnitResponse(UnitResponse {})))+ send_response(Ok(Pdu::UnitResponse(UnitResponse {})));+ if !is_proxy {+ self.push_frontend_state();+ }}Pdu::SetFocusedPane(SetFocusedPane {pane_id,@@ -2192,6 +2232,42 @@ mod tests {);}+ #[test]+ fn a_registering_client_is_told_where_the_lease_stands() {+ use super::{PduSender, SessionHandler};+ use codec::{DecodedPdu, SetClientId};+ use std::sync::Mutex;++ // The registration path consults the process-wide mux, and parts of+ // it schedule follow-up work; a scheduler that merely queues it is+ // enough here.+ let mux = Arc::new(Mux::new(None));+ Mux::set_mux(&mux);+ let _executor = promise::spawn::SimpleExecutor::new();++ let sent: Arc<Mutex<Vec<DecodedPdu>>> = Arc::new(Mutex::new(Vec::new()));+ let sink = Arc::clone(&sent);+ let mut handler = SessionHandler::new(PduSender::new(move |pdu| {+ sink.lock().unwrap().push(pdu);+ Ok(())+ }));+ handler.process_one(DecodedPdu {+ serial: 7,+ pdu: Pdu::SetClientId(SetClientId {+ client_id: mux::client::generate_client_id(),+ is_proxy: false,+ }),+ });++ let sent = sent.lock().unwrap();+ let names: Vec<(u64, &str)> = sent.iter().map(|d| (d.serial, d.pdu.pdu_name())).collect();+ assert_eq!(+ names,+ vec![(7, "UnitResponse"), (0, "FrontendAccessState")],+ "the acknowledgement first, then the state; no tabs, so no viewports"+ );+ }+#[test]fn only_one_push_is_scheduled_per_pane_at_a_time() {let mut state = PerPane::default();