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
Fix reconnect storm after remote mux death; expose ThinkTerm Connect in hosts UI
RoversX6d34810e17bb
diff --git a/README.md b/README.mdindex 48f4bd0b..9405f038 100644--- a/README.md+++ b/README.md@@ -33,3 +33,9 @@ Why: I can't afford Apple's developer certificate ($99/year), so macOS may block- The package script uses the binaries from `target/release/`.- The macOS app template lives in `assets/macos/ThinkTerm.app`.- Change `TAG_NAME` for each release version.+- The `thinkterm` CLI ships inside the app bundle. To use it from a shell,+ symlink it onto your PATH:++ ```bash+ sudo ln -s /Applications/ThinkTerm.app/Contents/MacOS/thinkterm /usr/local/bin/thinkterm+ ```diff --git a/ci/deploy.sh b/ci/deploy.shindex 960b61e7..fc3c7f4c 100755--- a/ci/deploy.sh+++ b/ci/deploy.sh@@ -38,7 +38,7 @@ case $OSTYPE incp -r assets/shell-completion $zipdir/ThinkTerm.app/Contents/Resourcestic -xe wezterm -o $zipdir/ThinkTerm.app/Contents/Resources/terminfo termwiz/data/wezterm.terminfo- for bin in wezterm wezterm-mux-server wezterm-gui strip-ansi-escapes ; do+ for bin in wezterm thinkterm wezterm-mux-server wezterm-gui strip-ansi-escapes ; do# If the user ran a simple `cargo build --release`, then we want to allow# a single-arch package to be builtif [[ -f $TARGET_DIR/release/$bin ]] ; thendiff --git a/mux/src/ssh.rs b/mux/src/ssh.rsindex dac00ec1..3628919f 100644--- a/mux/src/ssh.rs+++ b/mux/src/ssh.rs@@ -64,16 +64,20 @@ pub fn ssh_connect_with_ui(ssh_connect_with_ui_and_password(ssh_config, ui, None)}-/// Like [`ssh_connect_with_ui`], but auto-answers the first password-/// (non-echo) auth prompt with a stored password, if any. Only once: a-/// wrong stored password then falls back to interactive entry.+/// Like [`ssh_connect_with_ui`], but auto-answers password (non-echo) auth+/// prompts with a stored password, if any. At most twice: servers commonly+/// issue a second prompt (e.g. `password` then `keyboard-interactive`, or a+/// retry), and a single-shot answer left the second one to an interactive+/// prompt that a headless reconnect cannot serve. A wrong stored password+/// still falls back to interactive entry after those attempts.pub fn ssh_connect_with_ui_and_password(ssh_config: wezterm_ssh::ConfigMap,ui: &mut ConnectionUI,- mut password: Option<String>,+ password: Option<String>,) -> anyhow::Result<Session> {let cloned_ui = ui.clone();cloned_ui.run_and_log_error(move || {+ let mut password_answers_left = if password.is_some() { 2usize } else { 0 };let remote_address = ssh_config.get("hostname").expect("ssh config to always set hostname");@@ -115,9 +119,10 @@ pub fn ssh_connect_with_ui_and_password(}let res = if prompt.echo {ui.input(editor_prompt)- } else if let Some(stored) = password.take() {+ } else if password_answers_left > 0 {+ password_answers_left -= 1;ui.output_str(&format!("{editor_prompt} (using stored password)\n"));- Ok(stored)+ Ok(password.clone().expect("password present when answers remain"))} else {ui.password(editor_prompt)};diff --git a/wezterm-client/src/client.rs b/wezterm-client/src/client.rsindex 7e39c4ef..63dca794 100644--- a/wezterm-client/src/client.rs+++ b/wezterm-client/src/client.rs@@ -704,7 +704,7 @@ impl Reconnectable {self.unix_connect(unix_dom, initial, ui, no_auto_start)}ClientDomainConfig::Tls(tls) => self.tls_connect(tls, initial, ui),- ClientDomainConfig::Ssh(ssh) => self.ssh_connect(ssh, initial, ui),+ ClientDomainConfig::Ssh(ssh) => self.ssh_connect(ssh, initial, ui, no_auto_start),}}@@ -735,6 +735,7 @@ impl Reconnectable {ssh_dom: SshDomain,initial: bool,ui: &mut ConnectionUI,+ no_auto_start: bool,) -> anyhow::Result<()> {let ssh_config = mux::ssh::ssh_domain_to_ssh_config(&ssh_dom)?;@@ -742,7 +743,7 @@ impl Reconnectable {ssh_connect_with_ui_and_password(ssh_config, ui, ssh_dom.stored_password.clone())?;let cmd = if let Some(cmd) = ssh_dom.override_proxy_command.clone() {cmd- } else if initial {+ } else if initial || !no_auto_start {Self::remote_mux_command(&ssh_dom.remote_wezterm_path, "cli --prefer-mux proxy")} else {Self::remote_mux_command(@@ -1104,9 +1105,27 @@ impl Client {thread::spawn(move || {const BASE_INTERVAL: Duration = Duration::from_secs(1);const MAX_INTERVAL: Duration = Duration::from_secs(10);+ // A connection that dies this quickly after a "successful"+ // connect never really worked. The classic shape is the ssh hop+ // coming up while the remote mux server is gone (the host+ // rebooted): the proxy spawns fine and then exits on its first+ // read, so connect() reports success and the session dies within+ // a second.+ const SHORT_SESSION: Duration = Duration::from_secs(15);let mut backoff = BASE_INTERVAL;+ // Consecutive sessions that died within SHORT_SESSION of+ // connecting. Only established-then-dead sessions count; a+ // connect() that fails outright (network still down) does not.+ let mut short_sessions = 0usize;+ // One visible reconnect tab for the lifetime of this client,+ // reused across reconnect cycles. It is closed by a successful+ // reattach or when we give up; creating one per cycle piles up+ // dead "Reconnecting..." tabs while the remote is unhealthy.+ let mut windowed_ui: Option<ConnectionUI> = None;+'client: loop {+ let session_started = std::time::Instant::now();if let Err(e) = client_thread(&mut reconnectable, local_domain_id, &mut receiver) {if !reconnectable.reconnectable() || local_domain_id.is_none() {log::debug!("client thread ended: {}", e);@@ -1130,21 +1149,38 @@ impl Client {break;}+ if session_started.elapsed() >= SHORT_SESSION {+ // The previous connection genuinely worked; restart+ // the retry schedule from scratch.+ short_sessions = 0;+ backoff = BASE_INTERVAL;+ } else {+ short_sessions += 1;+ }++ // A successful reattach closes the shared tab behind our+ // back; detect that so we build a fresh one when needed.+ if windowed_ui+ .as_ref()+ .map_or(false, |ui| !ui.test_alive())+ {+ windowed_ui = None;+ }+// The first couple of attempts run headless: with key// auth or a stored password the reconnect is completely// silent and the user just sees the pane resume. Only// if we still can't get back (interactive auth needed,// or a longer outage) do we escalate to a visible UI// tab hosted in a window of this domain.- let mut windowed_ui: Option<ConnectionUI> = None;let mut attempt = 0usize;loop {attempt += 1;- let mut ui = if attempt <= 2 {- ConnectionUI::new_headless()- } else if let Some(ui) = windowed_ui.clone() {+ let mut ui = if let Some(ui) = windowed_ui.clone() {ui+ } else if attempt <= 2 {+ ConnectionUI::new_headless()} else {// Host the reconnect UI (and any auth prompts)// as a tab inside a window that already shows@@ -1194,13 +1230,25 @@ impl Client {break 'client;}let initial = false;- let no_auto_start = true; // Don't auto-start on a reconnect+ // Normally a reconnect must not auto-start a server:+ // during a network blip the server is alive and would+ // be fought by a second instance. But when freshly+ // established sessions keep dying instantly, the ssh+ // hop is fine and it is the remote mux server that is+ // gone (e.g. the host rebooted); refusing auto-start+ // then loops forever without ever converging. Let the+ // proxy revive the server after a couple of instant+ // deaths — it still only spawns one if connecting to+ // the existing socket fails.+ let no_auto_start = short_sessions < 2;match reconnectable.connect(initial, &mut ui, no_auto_start) {Ok(_) => {- backoff = BASE_INTERVAL;log::error!("Reconnected!");+ let reattach_ui = ui.clone();promise::spawn::spawn_into_main_thread(async move {- ClientDomain::reattach(local_domain_id, ui).await.ok();+ ClientDomain::reattach(local_domain_id, reattach_ui)+ .await+ .ok();}).detach();break;@@ -1220,6 +1268,12 @@ impl Client {}}+ // Whatever ended the loop (cancelled by user, not+ // reconnectable), don't leave the reconnect tab behind.+ if let Some(ui) = windowed_ui.take() {+ ui.close();+ }+async fn detach(local_domain_id: DomainId) -> anyhow::Result<()> {if let Some(mux) = Mux::try_get() {let client_domain = muxdiff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rsindex b02db607..a6b0620b 100644--- a/wezterm-gui/src/main.rs+++ b/wezterm-gui/src/main.rs@@ -377,7 +377,7 @@ async fn spawn_tab_in_domain_if_mux_is_empty(/// multiplexing client domain built from that host, including its stored/// credentials, so hosts added in the UI are connectable (and reconnect/// silently) without a lua ssh_domains entry.-fn connect_domain_from_ssh_host(name: &str) -> anyhow::Result<Arc<dyn Domain>> {+pub(crate) fn connect_domain_from_ssh_host(name: &str) -> anyhow::Result<Arc<dyn Domain>> {let entry = crate::ssh_hosts::list_all_hosts().into_iter().find(|entry| entry.spec.label == name || entry.id == name)@@ -398,7 +398,7 @@ fn connect_domain_from_ssh_host(name: &str) -> anyhow::Result<Arc<dyn Domain>> {Ok(domain)}-async fn connect_domain_into_space(+pub(crate) async fn connect_domain_into_space(cmd: Option<CommandBuilder>,domain: Arc<dyn Domain>,) -> anyhow::Result<()> {diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rsindex 8cc122d6..836f37e7 100644--- a/wezterm-gui/src/termwindow/mouseevent.rs+++ b/wezterm-gui/src/termwindow/mouseevent.rs@@ -3026,6 +3026,30 @@ impl super::TermWindow {return false;};+ // A ThinkTerm Connect host attaches the persistent remote mux domain+ // in its dedicated Space — the same flow as `thinkterm connect+ // <name>` — rather than opening a direct-ssh remote thread.+ if spec.multiplexing && !spec.use_mosh {+ let name = spec.label.clone();+ promise::spawn::spawn(async move {+ let domain = match Mux::get().get_domain_by_name(&name) {+ Some(domain) => domain,+ None => match crate::connect_domain_from_ssh_host(&name) {+ Ok(domain) => domain,+ Err(err) => {+ log::error!("connect {name}: {err:#}");+ return;+ }+ },+ };+ if let Err(err) = crate::connect_domain_into_space(None, domain).await {+ log::error!("connect {name}: {err:#}");+ }+ })+ .detach();+ return true;+ }+self.snapshot_active_workspace_thread_layout();let use_mosh = spec.use_mosh;let thread_id = crate::workspace_threads::create_disconnected_remote_host_thread(diff --git a/wezterm-gui/src/termwindow/ssh_hosts_view.rs b/wezterm-gui/src/termwindow/ssh_hosts_view.rsindex a1cd5bf2..efc4a20b 100644--- a/wezterm-gui/src/termwindow/ssh_hosts_view.rs+++ b/wezterm-gui/src/termwindow/ssh_hosts_view.rs@@ -72,6 +72,7 @@ enum SshViewAction {FocusField(usize),ToggleDetect,ToggleMosh,+ ToggleMux,Save,SaveAndConnect,Cancel,@@ -92,6 +93,7 @@ struct HostForm {fields: [String; FIELD_COUNT],detect_os: bool,use_mosh: bool,+ multiplexing: bool,error: Option<String>,}@@ -215,12 +217,14 @@ impl SshHostsView {fields[FIELD_MOSH_SERVER] = spec.mosh_server_command.clone();let detect_os = spec.detect_os;let use_mosh = spec.use_mosh;+ let multiplexing = spec.multiplexing;self.form = Some(HostForm {editing: Some(project_id.to_string()),original: Some(spec),fields,detect_os,use_mosh,+ multiplexing,error: None,});self.focus = Focus::Field(FIELD_HOST);@@ -292,6 +296,7 @@ impl SshHostsView {spec.default_workspace = opt(&form.fields[FIELD_WORKSPACE]);spec.detect_os = form.detect_os;spec.use_mosh = form.use_mosh;+ spec.multiplexing = form.multiplexing;let mosh_server_command = form.fields[FIELD_MOSH_SERVER].trim();if spec.use_mosh && mosh_server_command.is_empty() {form.error = Some("Mosh server command is required".to_string());@@ -443,6 +448,11 @@ impl SshHostsView {SshViewAction::ToggleMosh => {if let Some(form) = self.form.as_mut() {form.use_mosh = !form.use_mosh;+ if form.use_mosh {+ // Mosh and ThinkTerm Connect are alternative+ // transports; only one can drive the connection.+ form.multiplexing = false;+ }if form.use_mosh && form.fields[FIELD_MOSH_SERVER].trim().is_empty() {form.fields[FIELD_MOSH_SERVER] =ssh_hosts::DEFAULT_MOSH_SERVER_COMMAND.to_string();@@ -453,6 +463,18 @@ impl SshHostsView {}ContentViewResponse::Redraw}+ SshViewAction::ToggleMux => {+ if let Some(form) = self.form.as_mut() {+ form.multiplexing = !form.multiplexing;+ if form.multiplexing {+ form.use_mosh = false;+ if self.focus == Focus::Field(FIELD_MOSH_SERVER) {+ self.focus = Focus::Field(FIELD_WORKSPACE);+ }+ }+ }+ ContentViewResponse::Redraw+ }SshViewAction::Save => {self.persist_form();ContentViewResponse::Redraw@@ -1228,6 +1250,8 @@ impl SshHostsView {let user = spec.username.as_deref().unwrap_or("");let source = if spec.use_mosh {"mosh"+ } else if spec.multiplexing {+ "thinkterm connect"} else {match entry.source {SshHostSource::ThinkTerm => "ssh",@@ -1301,6 +1325,7 @@ impl SshHostsView {let editing = form.editing.is_some();let detect_os = form.detect_os;let use_mosh = form.use_mosh;+ let multiplexing = form.multiplexing;let fields = form.fields.clone();let error = form.error.clone();let focus = self.focus;@@ -1430,6 +1455,29 @@ impl SshHostsView {cur_y += INPUT_H + 24.0;}+ // ThinkTerm-Connect toggle: attach the persistent remote mux domain+ // (`thinkterm connect`) instead of a direct SSH session. Requires a+ // thinkterm/wezterm binary on the remote host.+ ctx.draw_text(+ layers,+ font,+ x,+ cur_y + (toggle_h - line_h) / 2.0,+ "ThinkTerm Connect (persistent mux)",+ palette.text,+ field_w - toggle_w - 12.0,+ )?;+ draw_toggle(+ ctx,+ layers,+ &mut self.widgets,+ palette,+ rect(x + field_w - toggle_w, cur_y, toggle_w, toggle_h),+ multiplexing,+ SshViewAction::ToggleMux,+ )?;+ cur_y += toggle_h + 24.0;+if let Some(err) = &error {ctx.draw_text(layers,