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
mobile: every completion carries its connection's serial
RoversX86f02d4ca53b
diff --git a/android/build.sh b/android/build.shindex a88b0961..53f9b082 100755--- a/android/build.sh+++ b/android/build.sh@@ -40,7 +40,17 @@ mkdir -p android/app/src/debug/assets[ -f /tmp/ttp-ssh/userkey ] && cp /tmp/ttp-ssh/userkey android/app/src/debug/assets/probe_key || trueecho "== gradle"-(cd android && "$GRADLE" --quiet assembleDebug 2>&1 | grep -E "error|warning: unused|BUILD|FAIL" || true)+build_log=$(mktemp "${TMPDIR:-/tmp}/thinkterm-android-build.XXXXXX")+trap 'rm -f "$build_log"' EXIT+build_status=0+(cd android && "$GRADLE" --quiet assembleDebug) >"$build_log" 2>&1 || build_status=$?+if [ "$build_status" -ne 0 ]; then+ cat "$build_log"+ exit "$build_status"+fi+grep -E "error|warning: unused|BUILD|FAIL" "$build_log" || true+rm -f "$build_log"+trap - EXITAPK="android/app/build/outputs/apk/debug/app-debug.apk"test -f "$APK" || { echo "no APK at $APK" >&2; exit 1; }diff --git a/ios/.gitignore b/ios/.gitignoreindex 907a7d94..dfb97407 100644--- a/ios/.gitignore+++ b/ios/.gitignore@@ -1,5 +1,6 @@# Regenerated by ios/build.shGenerated/+build/ThinkTerm.xcodeproj/# The developer's signing teamLocal.xcconfigdiff --git a/ios/build.sh b/ios/build.shindex 9550e815..5350d88f 100755--- a/ios/build.sh+++ b/ios/build.sh@@ -66,10 +66,20 @@ if [ "${1:-}" = xcode ]; thenfiecho "== xcodebuild"+build_log=$(mktemp "${TMPDIR:-/tmp}/thinkterm-ios-build.XXXXXX")+trap 'rm -f "$build_log"' EXIT+build_status=0xcodebuild -project ios/ThinkTerm.xcodeproj -scheme ThinkTerm \-configuration Debug -sdk iphonesimulator \-destination "platform=iOS Simulator,name=$SIM_NAME" \- -derivedDataPath "$DERIVED" build 2>&1 | grep -E "error:|warning: .*Sources/|BUILD (SUCCEEDED|FAILED)" || true+ -derivedDataPath "$DERIVED" build >"$build_log" 2>&1 || build_status=$?+if [ "$build_status" -ne 0 ]; then+ cat "$build_log"+ exit "$build_status"+fi+grep -E "error:|warning: .*Sources/|BUILD (SUCCEEDED|FAILED)" "$build_log" || true+rm -f "$build_log"+trap - EXITAPP="$DERIVED/Build/Products/Debug-iphonesimulator/ThinkTerm.app"test -d "$APP" || { echo "no app bundle at $APP" >&2; exit 1; }diff --git a/thinkterm-mobile/src/core.rs b/thinkterm-mobile/src/core.rsindex c7102960..a514d450 100644--- a/thinkterm-mobile/src/core.rs+++ b/thinkterm-mobile/src/core.rs@@ -75,9 +75,9 @@ pub enum Cmd {/// so a retiring thread's last words never reach its successor.Net { serial: u64, net: Net },/// The first dial finished.- Dialed(Result<(), String>),+ Dialed { serial: u64, outcome: Result<(), String> },/// The attach handshake finished.- Attached(Result<Attached, String>),+ Attached { serial: u64, outcome: Result<Attached, String> },Key {name: String,ctrl: bool,@@ -228,42 +228,7 @@ struct State {}pub fn run(cmd_tx: Sender<Cmd>, rx: Receiver<Cmd>, notify: Box<dyn Notify>) {- let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {- #[cfg(target_vendor = "apple")]- backends: wgpu::Backends::METAL,- #[cfg(not(target_vendor = "apple"))]- backends: wgpu::Backends::PRIMARY,- ..Default::default()- });- let pool = LocalPool::new();- let notify: Arc<dyn Notify> = Arc::from(notify);- let platform = Rc::new(MobilePlatform::new(pool.spawner(), Arc::clone(¬ify)));- let mut state = State {- cmd_tx,- instance,- adapter: None,- gpu: None,- app: None,- target: None,- next_generation: 1,- link_serial: 0,- attaching: false,- animating: false,- stats: Stats::default(),- notify,- pool,- platform,- link: None,- fonts: None,- glyph_platform: None,- size_pt: 11.0,- device_id: String::new(),- conn: Conn::Idle,- status: String::new(),- composing: false,- settings: Vec::new(),- palette: None,- };+ let mut state = State::new(cmd_tx, notify);state.notify.on_log("core thread up".into());state.set_status("idle");@@ -295,6 +260,45 @@ pub fn run(cmd_tx: Sender<Cmd>, rx: Receiver<Cmd>, notify: Box<dyn Notify>) {}impl State {+ fn new(cmd_tx: Sender<Cmd>, notify: Box<dyn Notify>) -> Self {+ let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {+ #[cfg(target_vendor = "apple")]+ backends: wgpu::Backends::METAL,+ #[cfg(not(target_vendor = "apple"))]+ backends: wgpu::Backends::PRIMARY,+ ..Default::default()+ });+ let pool = LocalPool::new();+ let notify: Arc<dyn Notify> = Arc::from(notify);+ let platform = Rc::new(MobilePlatform::new(pool.spawner(), Arc::clone(¬ify)));+ Self {+ cmd_tx,+ instance,+ adapter: None,+ gpu: None,+ app: None,+ target: None,+ next_generation: 1,+ link_serial: 0,+ attaching: false,+ animating: false,+ stats: Stats::default(),+ notify,+ pool,+ platform,+ link: None,+ fonts: None,+ glyph_platform: None,+ size_pt: 11.0,+ device_id: String::new(),+ conn: Conn::Idle,+ status: String::new(),+ composing: false,+ settings: Vec::new(),+ palette: None,+ }+ }+fn handle(&mut self, cmd: Cmd) {match cmd {Cmd::Attach {@@ -346,8 +350,8 @@ impl State {self.set_status("disconnected");}Cmd::Net { serial, net } => self.on_net(serial, net),- Cmd::Dialed(outcome) => self.on_dialed(outcome),- Cmd::Attached(outcome) => self.on_attached(outcome),+ Cmd::Dialed { serial, outcome } => self.on_dialed(serial, outcome),+ Cmd::Attached { serial, outcome } => self.on_attached(serial, outcome),Cmd::Key {name,ctrl,@@ -638,7 +642,6 @@ impl State {self.glyph_platform = Some(Rc::new(GlyphSeams::new(painter)));let tx = self.cmd_tx.clone();- self.link_serial += 1;let serial = self.link_serial;let deliver: Box<dyn Fn() -> Box<dyn Fn(Net) + Send>> = Box::new(move || {let tx = tx.clone();@@ -659,7 +662,7 @@ impl State {.spawner().spawn_local(async move {let outcome = dial.await.map_err(|e| format!("{e:#}"));- let _ = tx.send(Cmd::Dialed(outcome));+ let _ = tx.send(Cmd::Dialed { serial, outcome });}).map_err(|e| anyhow!("spawning the dial: {e}"))?;Ok(())@@ -670,6 +673,9 @@ impl State {/// surface, if lent, is dropped: the shell re-attaches it by calling/// `attach_surface` again, which is what it does on any change anyway.fn disconnect(&mut self) {+ // Invalidate queued completions even if no new connection follows,+ // or the next connect fails before it can create its link.+ self.link_serial += 1;self.attaching = false;if let Some(link) = self.link.take() {link.shutdown();@@ -729,7 +735,10 @@ impl State {}}- fn on_dialed(&mut self, outcome: Result<(), String>) {+ fn on_dialed(&mut self, serial: u64, outcome: Result<(), String>) {+ if serial != self.link_serial {+ return;+ }match outcome {Ok(()) => {self.conn = Conn::Attaching;@@ -842,12 +851,13 @@ impl State {ssh_auth_sock: None,};let tx = self.cmd_tx.clone();+ let serial = self.link_serial;self.attaching = true;let spawned = self.pool.spawner().spawn_local(async move {let outcome = attach(&link, size, 0, me).await.map_err(|e| format!("{e:#}"));- let _ = tx.send(Cmd::Attached(outcome));+ let _ = tx.send(Cmd::Attached { serial, outcome });});if let Err(err) = spawned {self.attaching = false;@@ -855,7 +865,10 @@ impl State {}}- fn on_attached(&mut self, outcome: Result<Attached, String>) {+ fn on_attached(&mut self, serial: u64, outcome: Result<Attached, String>) {+ if serial != self.link_serial {+ return;+ }self.attaching = false;let attached = match outcome {Ok(attached) => attached,@@ -1250,3 +1263,100 @@ fn stable_identity(device_id: &str) -> (u64, usize) {let id = (fnv(device_id.as_bytes(), 0x9e37_79b9_7f4a_7c15) & 0x7fff_ffff) as usize;(epoch, id)}++#[cfg(test)]+mod tests {+ use super::*;++ struct QuietNotify;++ impl Notify for QuietNotify {+ fn on_frame_needed(&self) {}+ fn on_status(&self, _: String) {}+ fn on_log(&self, _: String) {}+ fn on_title(&self, _: String) {}+ fn on_change(&self) {}+ fn on_clipboard(&self, _: String) {}+ fn on_focus_input(&self) {}+ fn on_ime_anchor(&self, _: f64, _: f64, _: f64, _: f64) {}+ fn on_host_key(&self, _: String) {}+ fn on_published(&self, _: String, _: String) {}+ fn on_bell(&self) {}+ fn on_preview(&self, _: u32, _: String) {}+ }++ fn state() -> State {+ let (tx, _) = std::sync::mpsc::channel();+ State::new(tx, Box::new(QuietNotify))+ }++ fn attached() -> Attached {+ Attached {+ pane_id: 1,+ tab_id: 2,+ window_id: 3,+ workspace: String::new(),+ title: String::new(),+ dims: Default::default(),+ alt_screen: false,+ server_version: String::new(),+ server_id: String::new(),+ }+ }++ fn old_completions(serial: u64) -> Vec<Cmd> {+ vec![+ Cmd::Dialed { serial, outcome: Ok(()) },+ Cmd::Dialed { serial, outcome: Err("old dial".into()) },+ Cmd::Attached { serial, outcome: Ok(attached()) },+ Cmd::Attached { serial, outcome: Err("old handshake".into()) },+ ]+ }++ #[test]+ fn disconnect_ignores_queued_completions_without_a_successor() {+ let mut state = state();+ let old = state.link_serial;+ state.handle(Cmd::Disconnect);+ for cmd in old_completions(old) {+ state.handle(cmd);+ assert_eq!(state.conn, Conn::Idle);+ assert_eq!(state.status, "disconnected");+ assert_eq!(state.stats.errors, 0);+ assert!(!state.attaching);+ assert!(state.link.is_none());+ }+ }++ #[test]+ fn old_completions_cannot_change_a_successor_or_clear_its_handshake() {+ let mut state = state();+ let old = state.link_serial;+ state.handle(Cmd::Disconnect);+ // Install a successor without opening a real SSH connection or GPU.+ let link = crate::link::test_link();+ link.opened();+ state.link = Some(link.clone());+ state.conn = Conn::Connecting;+ state.set_status("new connection");+ state.attaching = true;+ for cmd in old_completions(old) {+ state.handle(cmd);+ assert_eq!(state.conn, Conn::Connecting);+ assert_eq!(state.status, "new connection");+ assert_eq!(state.stats.errors, 0);+ assert!(state.attaching);+ assert!(state.link.is_some());+ assert!(link.is_open());+ }+ // The current generation must still deliver failures normally.+ state.handle(Cmd::Attached {+ serial: state.link_serial,+ outcome: Err("current handshake".into()),+ });+ assert_eq!(state.conn, Conn::Disconnected("current handshake".into()));+ assert!(!state.attaching);+ assert!(state.link.is_none());+ assert!(!link.is_open());+ }+}diff --git a/thinkterm-mobile/src/link.rs b/thinkterm-mobile/src/link.rsindex 298d127b..5ca9ab12 100644--- a/thinkterm-mobile/src/link.rs+++ b/thinkterm-mobile/src/link.rs@@ -29,6 +29,22 @@ use tokio::sync::mpsc::UnboundedSender;#[derive(Clone)]pub struct SshLink(Rc<Inner>);+#[cfg(test)]+pub(crate) fn test_link() -> SshLink {+ SshLink::new(+ SshParams {+ host: "unused".into(),+ port: 22,+ user: "test".into(),+ auth: ssh::Auth::Password(String::new()),+ known_host: None,+ remote_command: String::new(),+ keepalive_secs: 0,+ },+ Box::new(|| panic!("this test must not open a network connection")),+ )+}+struct Inner {params: SshParams,/// How the network thread reaches the core thread with what arrives.@@ -331,6 +347,9 @@ impl Link for SshLink {}self.0.open.set(false);self.0.serials.borrow_mut().drain();+ // A reconnect task can still hold this link alive. Its dial must+ // finish without waiting for network events the core now discards.+ self.0.connecting.borrow_mut().take();}fn lease(&self) -> std::cell::Ref<'_, Lease> {@@ -372,3 +391,36 @@ impl Link for SshLink {Box::pin(async move { link.report_viewport_now(tab_id).await })}}++#[cfg(test)]+mod tests {+ use super::*;+ use futures::executor::LocalPool;+ use futures::task::LocalSpawnExt;++ #[test]+ fn shutdown_releases_a_dial_waiter_that_still_holds_the_link() {+ let link = test_link();+ let weak = Rc::downgrade(&link.0);+ // The pending response installed by connect, without a network thread.+ let (tx, rx) = oneshot::channel();+ *link.0.connecting.borrow_mut() = Some(tx);+ let finished = Rc::new(Cell::new(false));+ let mut pool = LocalPool::new();+ let retained = link.clone();+ let done = Rc::clone(&finished);+ pool.spawner().spawn_local(async move {+ assert!(rx.await.is_err());+ drop(retained);+ done.set(true);+ }).unwrap();+ pool.run_until_stalled();+ assert!(!finished.get());++ link.shutdown();+ drop(link);+ pool.run_until_stalled();+ assert!(finished.get());+ assert!(weak.upgrade().is_none());+ }+}