From a9e596c7c4f4cc1c00126e1d52d21ada911800e3 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:21:30 -0500 Subject: [PATCH 1/3] fix(tmux-link): faithful rendering, flow control and sturdier reconnect for remote tmux panes Found by driving a real tmux over a real ssh transport (private sshd, private tmux socket): Rendering - The remote tmux client is now as large as the linked pane's terminal, not the whole Bilterm window (remote was 160x44 for a 78x42 pane, so vim and wrapped lines drew in the wrong place). A remote window shown in a pane of its own gets its own size (refresh-client -C @W:CxR). - Hydrate restores alternate screen, cursor position/visibility, application cursor keys and mouse modes from one self-identifying display-message reply sent just before the captures. Reconnecting while vim runs used to leave the cursor at 30,41 instead of 8,0 and stale rows on the primary screen. - %output bytes >= 0x80 are kept exact through the String line channel and a UTF-8 character split across two %output lines is reassembled (it became two U+FFFD). - %extended-output is parsed. Flow control - refresh-client -f pause-after=2 after the hydrate; on %pause the pane's backlog is dropped, the screen re-captured and the pane continued (the continue argument must be quoted for tmux's parser). A flooding pane no longer starves input: Ctrl-C typed in Bilterm now reaches the remote. - A paste larger than 4096 bytes was silently truncated; typed bytes are now kept in a bounded per-pane backlog and sent in order without overflowing the command queue. Reconnect and usability - Link transport uses ServerAliveInterval=5 (dead peer noticed in ~15s, was ~55s) and jittered backoff in live mode. - The driver no longer sleeps 50 ms on the UI thread to learn the exit code. - tmux_link_state() reports the real machine state; removing the link's fleet node forgets the link at once instead of reading "idle" while the transport was still attached. - `:ssh-tmux -- [session]` reaches hosts named info, reconnect, disconnect or prefill. - Missing or too-old remote tmux is named in the failure text. - PtyLinkConnector::with_program lets tests run the link over a stand-in. Tests: tests/tmux_link_transcripts.rs feeds recorded control-mode transcripts through the real splitter, bridge and pumps into a pane. Pinned expectations that encoded the old sizes, the 4096-byte cap and the 15s link keepalive are updated. Co-Authored-By: Claude Fable 5.2 --- src/colon_command/parse_link.rs | 18 +- src/pane/tests.rs | 1 - src/remote/mod.rs | 11 +- src/tmux/decode.rs | 158 ++++++++++++++++ src/tmux/hydrate.rs | 43 ++++- src/tmux/link.rs | 15 +- src/tmux/mod.rs | 47 ++++- src/tmux/pane_state.rs | 164 ++++++++++++++++ src/tmux/reconnect.rs | 39 +++- src/tmux/splitter.rs | 5 +- src/tmux/transport.rs | 47 ++++- src/wm/mod.rs | 1 + src/wm/session.rs | 3 + src/wm/tmux_flow.rs | 145 ++++++++++++++ src/wm/tmux_io.rs | 75 ++++---- src/wm/tmux_link.rs | 13 +- tests/guard02_pane_transport.rs | 7 +- tests/phase51_1_e2e.rs | 36 ++-- tests/phase51_1_io.rs | 52 +++--- tests/phase51_1_ssh_argv.rs | 4 +- tests/tmux_link_transcripts.rs | 322 ++++++++++++++++++++++++++++++++ 21 files changed, 1085 insertions(+), 121 deletions(-) create mode 100644 src/tmux/decode.rs create mode 100644 src/tmux/pane_state.rs create mode 100644 src/wm/tmux_flow.rs create mode 100644 tests/tmux_link_transcripts.rs diff --git a/src/colon_command/parse_link.rs b/src/colon_command/parse_link.rs index 67fc6219..8170029f 100644 --- a/src/colon_command/parse_link.rs +++ b/src/colon_command/parse_link.rs @@ -1,11 +1,13 @@ //! Reserved words of `:ssh-tmux` / `:sshtmux` (Phase 51.1, P-13): `reconnect`, `disconnect`, //! `info`, `prefill`. Everything else (a host, `list`, `ls`, `status`) falls through to the -//! existing `ssh-tmux` arm in `parse.rs`. +//! existing `ssh-tmux` arm in `parse.rs`. A host that is literally named like a reserved word is +//! reached with `--`: `:ssh-tmux -- info [session]`. use super::ColonCommand; use crate::wm::link_cmd::RemoteLinkOp; -const USAGE: &str = "Usage: :ssh-tmux reconnect | disconnect | info | prefill"; +const USAGE: &str = + "Usage: :ssh-tmux reconnect | disconnect | info | prefill (a host with such a name: :ssh-tmux -- )"; /// Answers only for head `ssh-tmux`/`sshtmux` whose first argument is a reserved word. pub(super) fn parse_link_colon_command( @@ -15,6 +17,18 @@ pub(super) fn parse_link_colon_command( if !matches!(head, "ssh-tmux" | "sshtmux") { return None; } + // `--` ends the reserved words: `:ssh-tmux -- info [session]` connects to a host named `info`. + if parts.get(1) == Some(&"--") { + return Some(match parts.get(2) { + Some(target) if !target.starts_with('-') && parts.len() <= 4 => { + Ok(ColonCommand::SshTmuxConnect { + target: (*target).to_string(), + session: parts.get(3).copied().unwrap_or("bilterm").to_string(), + }) + } + _ => Err("Usage: :ssh-tmux -- [session]".to_string()), + }); + } let op = RemoteLinkOp::from_word(parts.get(1)?)?; if parts.len() > 2 { return Some(Err(USAGE.to_string())); diff --git a/src/pane/tests.rs b/src/pane/tests.rs index 91b02b1b..29739b9f 100644 --- a/src/pane/tests.rs +++ b/src/pane/tests.rs @@ -153,7 +153,6 @@ fn remote_input_tap_collects_and_caps() { // Capped at REMOTE_INPUT_CAP between drains; the excess is dropped. let cap = crate::tmux::link::REMOTE_INPUT_CAP; - assert_eq!(cap, 4096); pane.write_to_pty(&b"a".repeat(cap - 10)).unwrap(); pane.write_to_pty(&b"b".repeat(100)).unwrap(); pane.write_to_pty(b"more").unwrap(); diff --git a/src/remote/mod.rs b/src/remote/mod.rs index 8607ae05..b04c8c49 100644 --- a/src/remote/mod.rs +++ b/src/remote/mod.rs @@ -48,6 +48,8 @@ fn default_true() -> bool { true } +/// `ServerAliveInterval` of the tmux link transport. +pub const LINK_ALIVE_INTERVAL_SECS: u32 = 5; pub const CONTROL_PATH_LEAF: &str = "ssh-%C"; pub const CONTROL_DIR_MAX_BYTES: usize = 40; @@ -107,7 +109,7 @@ impl RemoteHostProfile { } fn push_common_args(&self, args: &mut Vec, control_dir: Option<&Path>, is_link: bool) { - push_keepalive_args(args); + push_keepalive_args(args, is_link); if is_link { args.push("-o".to_string()); args.push("ControlMaster=no".to_string()); @@ -190,9 +192,12 @@ impl RemoteHostProfile { } } -fn push_keepalive_args(args: &mut Vec) { +/// Keepalives: a link notices a dead peer after `5s x 3` (it reconnects by itself, so a short +/// detection time costs nothing); an interactive `:ssh` pane keeps the gentler `15s x 3`. +fn push_keepalive_args(args: &mut Vec, is_link: bool) { + let interval = if is_link { LINK_ALIVE_INTERVAL_SECS } else { 15 }; args.push("-o".to_string()); - args.push("ServerAliveInterval=15".to_string()); + args.push(format!("ServerAliveInterval={interval}")); args.push("-o".to_string()); args.push("ServerAliveCountMax=3".to_string()); args.push("-o".to_string()); diff --git a/src/tmux/decode.rs b/src/tmux/decode.rs new file mode 100644 index 00000000..7eae1d8a --- /dev/null +++ b/src/tmux/decode.rs @@ -0,0 +1,158 @@ +//! Byte-exact decoding of `tmux -CC` pane output and flow-control notifications. +//! +//! tmux octal-escapes only bytes below 0x20 and `\`; bytes >= 0x80 travel raw and tmux cuts +//! `%output` at arbitrary byte positions, so one UTF-8 character can be split across two lines. +//! Control lines travel through the link as `String`s, so [`ascii_safe_line`] re-escapes the high +//! bytes of an output line (a lossy UTF-8 conversion would turn each half into U+FFFD) and +//! [`Utf8Carry`] holds back an incomplete trailing sequence until the next chunk of that pane. +//! +//! With `refresh-client -f pause-after=N` tmux sends `%extended-output` instead of `%output` and +//! `%pause`/`%continue` when a pane falls behind; [`parse_flow_line`] reads those. + +use super::{TmuxControlEvent, unescape_tmux_output}; + +/// Seconds a pane may lag before tmux pauses it (`refresh-client -f pause-after=N`). +pub const PAUSE_AFTER_SECS: u32 = 2; + +const OUTPUT: &[u8] = b"%output "; +const EXTENDED: &[u8] = b"%extended-output "; + +/// `refresh-client -f pause-after=N` (tmux 3.2+; older servers answer `%error`, which is ignored). +pub fn pause_after_command() -> String { + format!("refresh-client -f pause-after={PAUSE_AFTER_SECS}\n") +} + +/// `refresh-client -A '%N:continue'`: resumes a paused pane. The argument must be quoted: tmux's +/// command parser rejects a bare leading `%` ("parse error: syntax error"). +pub fn continue_pane_command(tmux_pane: usize) -> String { + format!("refresh-client -A '%{tmux_pane}:continue'\n") +} + +/// Turns one raw control line into a `String` without losing bytes: output lines get every byte +/// `>= 0x80` (and DEL) rewritten as `\ooo`; other lines are converted lossily (they are text). +pub fn ascii_safe_line(bytes: &[u8]) -> String { + if !(bytes.starts_with(OUTPUT) || bytes.starts_with(EXTENDED)) || bytes.is_ascii() { + return String::from_utf8_lossy(bytes).into_owned(); + } + let mut out = String::with_capacity(bytes.len() + 16); + for &b in bytes { + if b < 0x80 { + out.push(b as char); + } else { + out.push_str(&format!("\\{b:03o}")); + } + } + out +} + +fn pane_id(tok: &str) -> Option { + tok.strip_prefix('%')?.parse().ok() +} + +/// Parses `%extended-output %P AGE ... : DATA`, `%pause %P` and `%continue %P`. +pub fn parse_flow_line(line: &str) -> Option { + if let Some(rest) = line.strip_prefix("%extended-output ") { + let (head, payload) = rest.split_once(" : ")?; + let pane_id = pane_id(head.split(' ').next()?)?; + return Some(TmuxControlEvent::Output { + pane_id, + data: unescape_tmux_output(payload), + }); + } + if let Some(rest) = line.strip_prefix("%pause ") { + return Some(TmuxControlEvent::Pause { pane_id: pane_id(rest.trim())? }); + } + if let Some(rest) = line.strip_prefix("%continue ") { + return Some(TmuxControlEvent::Continue { pane_id: pane_id(rest.trim())? }); + } + None +} + +/// Length of an incomplete UTF-8 sequence at the end of `bytes` (0 when the tail is complete or +/// is not UTF-8 at all). +fn incomplete_tail(bytes: &[u8]) -> usize { + let n = bytes.len(); + for back in 1..=n.min(3) { + let b = bytes[n - back]; + if b & 0xC0 == 0x80 { + continue; + } + let need = match b { + 0xC2..=0xDF => 2, + 0xE0..=0xEF => 3, + 0xF0..=0xF4 => 4, + _ => return 0, + }; + return if back < need { back } else { 0 }; + } + 0 +} + +/// Per-pane carry of an incomplete trailing UTF-8 sequence (at most 3 bytes). +#[derive(Clone, Debug, Default)] +pub struct Utf8Carry { + held: Vec, +} + +impl Utf8Carry { + /// Prepends the bytes held from the previous chunk and holds back a new incomplete tail. + pub fn feed(&mut self, data: &[u8]) -> Vec { + let mut all = std::mem::take(&mut self.held); + all.extend_from_slice(data); + let cut = all.len() - incomplete_tail(&all); + self.held = all.split_off(cut); + all + } + + /// Drops any held bytes (pane reset or closed). + pub fn clear(&mut self) { + self.held.clear(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn high_bytes_survive_the_string_channel() { + // "漢" = e6 bc a2, cut after the first byte the way tmux cuts `%output`. + let a = ascii_safe_line(b"%output %1 x\xe6"); + let b = ascii_safe_line(b"%output %1 \xbc\xa2y"); + assert!(a.is_ascii() && b.is_ascii()); + let mut bytes = unescape_tmux_output(a.strip_prefix("%output %1 ").unwrap()); + bytes.extend(unescape_tmux_output(b.strip_prefix("%output %1 ").unwrap())); + assert_eq!(String::from_utf8(bytes).unwrap(), "x漢y"); + // Non-output lines stay text. + assert_eq!(ascii_safe_line("%window-renamed @1 漢".as_bytes()), "%window-renamed @1 漢"); + } + + #[test] + fn carry_reassembles_split_characters() { + let mut c = Utf8Carry::default(); + assert_eq!(c.feed(b"a\xf0\x9f"), b"a"); + assert_eq!(c.feed(b"\x98\x80b"), "😀b".as_bytes()); + // Invalid bytes are not held forever. + assert_eq!(c.feed(b"\xff"), b"\xff"); + assert_eq!(c.feed(b"\x80\x80\x80"), b"\x80\x80\x80"); + } + + #[test] + fn continue_command_is_quoted_for_the_tmux_parser() { + assert_eq!(continue_pane_command(4), "refresh-client -A '%4:continue'\n"); + assert_eq!(pause_after_command(), "refresh-client -f pause-after=2\n"); + } + + #[test] + fn flow_lines_parse_and_garbage_is_rejected() { + assert_eq!( + parse_flow_line("%extended-output %3 1234 : a\\033[mb"), + Some(TmuxControlEvent::Output { pane_id: 3, data: b"a\x1b[mb".to_vec() }) + ); + assert_eq!(parse_flow_line("%pause %7"), Some(TmuxControlEvent::Pause { pane_id: 7 })); + assert_eq!(parse_flow_line("%continue %7"), Some(TmuxControlEvent::Continue { pane_id: 7 })); + for bad in ["%extended-output %3 12", "%pause 7", "%pause %x", "%continue", "%extended-output x 1 : y"] { + assert_eq!(parse_flow_line(bad), None, "{bad}"); + } + } +} diff --git a/src/tmux/hydrate.rs b/src/tmux/hydrate.rs index 0038aebe..e2d9330f 100644 --- a/src/tmux/hydrate.rs +++ b/src/tmux/hydrate.rs @@ -67,6 +67,10 @@ pub enum ReplyKind { CapturePane { tmux_pane: usize }, /// `list-panes -s -F "#{window_id} #{pane_id} #{window_name}"` pane discovery. ListPanes, + /// `display-message -p` screen state of every pane, sent just before the captures. Its reply + /// identifies itself by its marker (see [`ReplyCorrelator::set_state_marker`]) and takes no + /// place in the FIFO. + PaneStates, } /// One correlated reply block. @@ -107,6 +111,8 @@ pub struct ReplyCorrelator { open: Option, failed: Vec, overflowed: bool, + /// First word of a pane-state reply; such a block is recognised by content, not position. + state_marker: Option, } impl ReplyCorrelator { @@ -140,11 +146,26 @@ impl ReplyCorrelator { }); } + /// Sets the first word that marks a pane-state reply (kept across `clear()`). + pub fn set_state_marker(&mut self, marker: impl Into) { + self.state_marker = Some(marker.into()); + } + /// A body line inside the open block; ignored outside a block. pub fn on_line(&mut self, line: &str) { let Some(open) = self.open.as_mut() else { return; }; + // A state reply was never queued in the FIFO: give back the entry this block claimed. + if open.lines.is_empty() + && open.kind != Some(ReplyKind::PaneStates) + && self.state_marker.as_deref().is_some_and(|m| { + line.strip_prefix(m).is_some_and(|r| r.is_empty() || r.starts_with(' ')) + }) + && let Some(claimed) = open.kind.replace(ReplyKind::PaneStates) + { + self.queue.push_front(claimed); + } if open.lines.len() >= HYDRATE_REPLY_LINES_CAP { open.truncated = true; } else { @@ -187,6 +208,7 @@ impl ReplyCorrelator { self.open = None; self.failed.clear(); self.overflowed = false; + // `state_marker` is kept: it belongs to the bridge, not to one attach's FIFO. } } @@ -254,14 +276,29 @@ impl HydratePlanner { self.phase } - /// Commands to send once `Ready`: `refresh-client` (`Ignore`) then one capture per pane in - /// ascending order. Empty outside `Ready`. The phase moves on with `mark_sent()`. + /// Commands to send once `Ready`: `refresh-client` (`Ignore`), one state query for all panes + /// (`marker` is its reply's first word), then one capture per pane in ascending order. Empty outside `Ready`. The phase moves on with `mark_sent()`. + pub fn plan_with_state( + &mut self, + panes: &[usize], + cols: u16, + rows: u16, + marker: &str, + ) -> Vec<(String, ReplyKind)> { + let mut out = self.plan(panes, cols, rows); + if !out.is_empty() { + out.insert(1, (super::pane_state::pane_states_command(marker), ReplyKind::PaneStates)); + } + out + } + + /// `refresh-client` plus one capture per pane, without the state query (text only). pub fn plan(&mut self, panes: &[usize], cols: u16, rows: u16) -> Vec<(String, ReplyKind)> { if self.phase != HydratePhase::Ready { return Vec::new(); } self.planned = panes.iter().copied().collect(); - let mut out = Vec::with_capacity(self.planned.len() + 1); + let mut out = Vec::with_capacity(self.planned.len() + 2); out.push((format_refresh_client_size(cols, rows), ReplyKind::Ignore)); for &n in &self.planned { out.push(( diff --git a/src/tmux/link.rs b/src/tmux/link.rs index fbe46491..72381859 100644 --- a/src/tmux/link.rs +++ b/src/tmux/link.rs @@ -23,8 +23,11 @@ pub const DIAG_LINE_CAP: usize = 160; pub const CONTROL_LINE_MAX: usize = 256 * 1024; /// Queued tmux commands; the oldest non-hydrate commands are dropped beyond this. pub const OUTBOUND_CAP: usize = 256; -/// Bytes buffered per pane between ticks. -pub const REMOTE_INPUT_CAP: usize = 4096; +/// Bytes buffered per pane between ticks (a large paste arrives as one event). +pub const REMOTE_INPUT_CAP: usize = 1024 * 1024; +/// Typed bytes kept per pane while the command queue is busy; beyond this input is refused with a +/// status message. +pub const INPUT_BACKLOG_CAP: usize = 4 * 1024 * 1024; /// Payload bytes per `send-keys -H` command. pub const SEND_KEYS_CHUNK: usize = 512; /// Pending reply FIFO bound. @@ -180,6 +183,8 @@ pub struct LinkMachine { pub(crate) connected_since: Option, /// `true` once a `Spawn` was issued and no `Down`/kill has followed (a transport may exist). pub(crate) transport_live: bool, + /// Largest extra delay added to a backoff step, in percent of the step (0 = exact schedule). + pub(crate) jitter_pct: u32, } /// What one transport poll produced. @@ -247,6 +252,12 @@ pub struct LinkAux { pub last_mapped_count: usize, /// Free-form counters/flags keyed by name. pub notes: std::collections::BTreeMap, + /// Typed bytes not yet turned into `send-keys` (the command queue was busy), per pane. + pub input_backlog: std::collections::BTreeMap>, + /// Screen state reported for a tmux pane, consumed by that pane's next capture reply. + pub pane_states: std::collections::BTreeMap, + /// Client / per-window sizes last sent to tmux (change detection). + pub sent_sizes: std::collections::BTreeMap, (u16, u16)>, } /// All link state held by the `WindowManager` (one link at a time, P-01). diff --git a/src/tmux/mod.rs b/src/tmux/mod.rs index c9e6072c..125fb2eb 100644 --- a/src/tmux/mod.rs +++ b/src/tmux/mod.rs @@ -1,7 +1,9 @@ +pub mod decode; pub mod hydrate; pub mod link; pub mod reconnect; pub mod list_panes; +pub mod pane_state; pub mod splitter; pub mod transport; @@ -74,6 +76,10 @@ pub enum TmuxControlEvent { SessionChanged { session_id: usize, name: String }, /// `%exit [reason]` Exit { reason: Option }, + /// `%pause %`: tmux stopped sending this pane's output (client too far behind). + Pause { pane_id: usize }, + /// `%continue %` + Continue { pane_id: usize }, /// `%begin ` BeginBlock, /// `%end ` @@ -87,6 +93,9 @@ pub enum TmuxControlEvent { /// Parses a single line emitted by `tmux -CC` Control Mode (`TMUX-02`). pub fn parse_tmux_control_line(line: &str) -> TmuxControlEvent { let trimmed = line.trim_end_matches(&['\r', '\n'][..]); + if let Some(ev) = decode::parse_flow_line(trimmed) { + return ev; + } if let Some(rest) = trimmed.strip_prefix("%output ") && let Some((pane_tok, payload)) = rest.split_once(' ') @@ -305,6 +314,12 @@ pub struct TmuxControlBridge { first_line_seen: bool, cap_noted: bool, diag: Vec, + /// Incomplete trailing UTF-8 sequence of each pane's last `%output` chunk. + carry: BTreeMap, + /// First word of pane-state replies (see `set_state_marker`). + state_marker: Option, + /// tmux panes paused by the server (`%pause`), waiting for the WM to re-capture and continue. + paused: BTreeSet, } impl TmuxControlBridge { @@ -334,6 +349,8 @@ impl TmuxControlBridge { self.blocks_closed = 0; self.first_line_seen = false; self.cap_noted = false; + self.carry.clear(); + self.paused.clear(); } /// `(tmux pane, bilterm pane)` pairs currently mapped, ordered by tmux pane id. @@ -412,6 +429,23 @@ impl TmuxControlBridge { /// Resumes live output for `tmux_pane`. pub fn release_output(&mut self, tmux_pane: usize) { self.held.remove(&tmux_pane); + self.carry.remove(&tmux_pane); + } + + /// Sets the first word of pane-state replies for this attach and returns it. + pub fn set_state_marker(&mut self, marker: String) { + self.replies.set_state_marker(marker.clone()); + self.state_marker = Some(marker); + } + + /// First word of pane-state replies (`bilterm-state` until a link sets a per-attach one). + pub fn state_marker(&self) -> &str { + self.state_marker.as_deref().unwrap_or(pane_state::STATE_MARKER) + } + + /// Panes tmux paused since the last call (`%pause`), ascending. + pub fn take_paused(&mut self) -> Vec { + std::mem::take(&mut self.paused).into_iter().collect() } /// Reply blocks closed so far, greeting included. @@ -523,6 +557,8 @@ impl TmuxControlBridge { fn drop_tmux_pane(&mut self, tmux_pane_id: usize) { self.window_of_pane.remove(&tmux_pane_id); self.held.remove(&tmux_pane_id); + self.carry.remove(&tmux_pane_id); + self.paused.remove(&tmux_pane_id); if let Some(pane) = self.tmux_to_bilterm.remove(&tmux_pane_id) { self.bilterm_to_tmux.remove(&pane); if !self.removed.contains(&pane) { @@ -580,10 +616,16 @@ impl TmuxControlBridge { && let Some(id) = self.ensure_tmux_pane(wm, *pane_id) && let Some(pane) = wm.pane_mut(id) { + let data = &self.carry.entry(*pane_id).or_default().feed(data); pane.count_payload_rx(data.len()); pane.process_pty_output(data); } } + TmuxControlEvent::Pause { pane_id } => { + if self.tmux_to_bilterm.contains_key(pane_id) { + self.paused.insert(*pane_id); + } + } TmuxControlEvent::LayoutChange { window_id, layout } => { let panes = parse_tmux_layout_panes(layout); if !panes.is_empty() { @@ -685,7 +727,10 @@ impl TmuxControlBridge { .outbound_kinds .pop_front() .unwrap_or(hydrate::ReplyKind::Ignore); - self.replies.enqueue(kind); + // A state reply identifies itself by its marker; it has no FIFO position. + if kind != hydrate::ReplyKind::PaneStates { + self.replies.enqueue(kind); + } } self.outbound_kinds.clear(); cmds diff --git a/src/tmux/pane_state.rs b/src/tmux/pane_state.rs new file mode 100644 index 00000000..88b56194 --- /dev/null +++ b/src/tmux/pane_state.rs @@ -0,0 +1,164 @@ +//! Screen state of a remote tmux pane, so a hydrate restores more than text: alternate screen, +//! cursor position and visibility, application cursor keys and mouse reporting. +//! +//! `capture-pane` only returns cells. Without the state a re-attach while `vim` runs leaves the +//! local pane on the primary screen with the cursor after the last line, and the application's +//! later redraws land in the wrong place. + +use std::collections::BTreeMap; + +use super::link::{HYDRATE_HISTORY_LINES, MAPPED_PANES_CAP}; + +/// What `display-message -p` reports for one pane. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct PaneState { + pub alternate: bool, + pub cursor_x: u16, + pub cursor_y: u16, + pub width: u16, + pub height: u16, + pub cursor_visible: bool, + pub app_cursor_keys: bool, + pub mouse_standard: bool, + pub mouse_button: bool, + pub mouse_any: bool, + pub mouse_sgr: bool, +} + +const FIELDS: [&str; 11] = [ + "alternate_on", + "cursor_x", + "cursor_y", + "pane_width", + "pane_height", + "cursor_flag", + "keypad_cursor_flag", + "mouse_standard_flag", + "mouse_button_flag", + "mouse_any_flag", + "mouse_sgr_flag", +]; + +/// Default first word of the state reply line (the link adds a per-attach nonce, see +/// [`state_marker`]). The reply identifies itself by this word instead of by its position among +/// the reply blocks. +pub const STATE_MARKER: &str = "bilterm-state"; + +/// Marker for one attach: screen text captured from a pane cannot be mistaken for a state reply. +pub fn state_marker(nonce: u64) -> String { + format!("{STATE_MARKER}-{nonce:x}") +} + +/// One `display-message -p` for every pane of the session: `MARKER [%id f1 .. f11][%id ..]`. +/// It names no target, so it cannot fail because a pane is gone. +pub fn pane_states_command(marker: &str) -> String { + let fmt: Vec = FIELDS.iter().map(|f| format!("#{{{f}}}")).collect(); + format!("display-message -p \"{marker} #{{W:#{{P:[#{{pane_id}} {}]}}}}\"\n", fmt.join(" ")) +} + +fn parse_fields(text: &str) -> Option { + let nums: Vec = text + .split_whitespace() + .map(|t| t.parse::().ok()) + .collect::>>()?; + if nums.len() != FIELDS.len() { + return None; + } + Some(PaneState { + alternate: nums[0] != 0, + cursor_x: nums[1], + cursor_y: nums[2], + width: nums[3], + height: nums[4], + cursor_visible: nums[5] != 0, + app_cursor_keys: nums[6] != 0, + mouse_standard: nums[7] != 0, + mouse_button: nums[8] != 0, + mouse_any: nums[9] != 0, + mouse_sgr: nums[10] != 0, + }) +} + +/// Parses a state reply line; malformed groups are skipped and the count is capped (the line is +/// remote-controlled). `None` when the line does not start with `marker`. +pub fn parse_pane_states(marker: &str, line: &str) -> Option> { + let rest = line.strip_prefix(marker)?.strip_prefix(' ').unwrap_or(""); + let mut out = BTreeMap::new(); + for group in rest.split('[').skip(1).take(4 * MAPPED_PANES_CAP) { + let Some((body, _)) = group.split_once(']') else { continue }; + let Some((id, fields)) = body.split_once(' ') else { continue }; + if let (Some(id), Some(st)) = (id.strip_prefix('%').and_then(|n| n.parse().ok()), parse_fields(fields)) + && out.len() < MAPPED_PANES_CAP + { + out.insert(id, st); + } + } + Some(out) +} + +/// Bytes that rebuild a pane from a capture reply plus its state: full reset, alternate screen if +/// the remote is on it, every captured line (the last `height` lines are the visible screen, so +/// trailing blank lines are kept), then the cursor and the input modes. +/// +/// Lines beyond history plus one screen are dropped from the front (malformed or oversized reply). +pub fn render_capture_with_state(lines: &[String], st: &PaneState) -> Vec { + let keep = HYDRATE_HISTORY_LINES + usize::from(st.height.max(1)); + let lines = &lines[lines.len().saturating_sub(keep)..]; + let mut out = Vec::with_capacity(lines.iter().map(|l| l.len() + 2).sum::() + 64); + out.extend_from_slice(b"\x1bc\x1b[3J"); + if st.alternate { + out.extend_from_slice(b"\x1b[?1049h\x1b[H\x1b[2J"); + } + out.extend_from_slice(lines.join("\r\n").as_bytes()); + out.extend_from_slice(b"\x1b[0m"); + out.extend_from_slice( + format!("\x1b[{};{}H", u32::from(st.cursor_y) + 1, u32::from(st.cursor_x) + 1).as_bytes(), + ); + for (on, seq) in [ + (!st.cursor_visible, "\x1b[?25l"), + (st.app_cursor_keys, "\x1b[?1h"), + (st.mouse_standard, "\x1b[?1000h"), + (st.mouse_button, "\x1b[?1002h"), + (st.mouse_any, "\x1b[?1003h"), + (st.mouse_sgr, "\x1b[?1006h"), + ] { + if on { + out.extend_from_slice(seq.as_bytes()); + } + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn state_line_round_trips_and_rejects_garbage() { + let m = state_marker(0xabc); + assert_eq!(m, "bilterm-state-abc"); + let cmd = pane_states_command(&m); + assert!(cmd.starts_with("display-message -p \"bilterm-state-abc #{W:#{P:[#{pane_id} #{alternate_on} #{cursor_x} "), "{cmd}"); + assert!(cmd.ends_with("#{mouse_sgr_flag}]}}\"\n"), "{cmd}"); + let got = parse_pane_states(&m, "bilterm-state-abc [%0 1 8 0 78 42 1 0 0 0 0 1][%7 0 0 3 80 24 0 1 0 0 0 0]").unwrap(); + assert_eq!(got.len(), 2); + let st = got[&0]; + assert!(st.alternate && st.cursor_visible && st.mouse_sgr && !st.mouse_any); + assert_eq!((st.cursor_x, st.cursor_y, st.width, st.height), (8, 0, 78, 42)); + assert!(got[&7].app_cursor_keys && !got[&7].cursor_visible); + // Wrong marker (for example screen text of another attach) is not a state line. + assert!(parse_pane_states(&m, "bilterm-state [%0 1 8 0 78 42 1 0 0 0 0 1]").is_none()); + for bad in ["", " [", " [%x 1 2]", " [%1 1 8 0 78 42 1 0 0 0 0]", " [%1 99999999 0 0 0 0 0 0 0 0 0 0]", " %1 1 8 0 78 42 1 0 0 0 0 1"] { + assert_eq!(parse_pane_states(&m, &format!("{m}{bad}")).unwrap().len(), 0, "{bad}"); + } + } + + #[test] + fn render_restores_alt_screen_cursor_and_modes() { + let st = PaneState { alternate: true, cursor_x: 8, cursor_y: 0, width: 20, height: 2, mouse_sgr: true, ..Default::default() }; + let out = String::from_utf8(render_capture_with_state(&["HELLO".into(), "~".into()], &st)).unwrap(); + assert!(out.starts_with("\x1bc\x1b[3J\x1b[?1049h")); + assert!(out.contains("HELLO\r\n~\x1b[0m\x1b[1;9H")); + assert!(out.ends_with("\x1b[?25l\x1b[?1006h")); + } +} diff --git a/src/tmux/reconnect.rs b/src/tmux/reconnect.rs index d02671f5..24adc1c2 100644 --- a/src/tmux/reconnect.rs +++ b/src/tmux/reconnect.rs @@ -35,6 +35,20 @@ pub fn backoff_delay_ms(policy: &ReconnectPolicy, attempt: u32) -> u64 { delay.min(cap) } +/// Jitter added to a production link's backoff (percent of each step). +pub const LINK_JITTER_PCT: u32 = 25; + +/// `delay` plus up to `pct` percent, derived from the clock (no RNG dependency): many clients that +/// lost the same host do not retry in lockstep. +pub fn jittered(delay: u64, pct: u32, now_ms: u64) -> u64 { + let span = delay.saturating_mul(u64::from(pct)) / 100; + if span == 0 { + return delay; + } + let mix = now_ms.wrapping_mul(0x9E37_79B9_7F4A_7C15) >> 33; + delay.saturating_add(mix % (span + 1)) +} + const FATAL_EVIDENCE: [&str; 5] = [ "permission denied", "host key verification failed", @@ -43,6 +57,21 @@ const FATAL_EVIDENCE: [&str; 5] = [ "no such file", ]; +/// Evidence that the remote has no usable tmux (shown instead of a bare exit code). +const NO_TMUX_EVIDENCE: [&str; 3] = ["tmux: command not found", "tmux: not found", "exec: tmux"]; +const OLD_TMUX_EVIDENCE: [&str; 3] = ["unknown option -- c", "tmux: illegal option", "usage: tmux"]; + +/// A plain-language cause when the diagnostic tail shows a known setup problem. +fn setup_problem(tail: &[String]) -> Option<&'static str> { + if any_line_contains(tail, &NO_TMUX_EVIDENCE) { + Some("tmux is not installed on the remote host (install tmux 3.2 or newer)") + } else if any_line_contains(tail, &OLD_TMUX_EVIDENCE) { + Some("the remote tmux is too old for control mode (tmux 3.2 or newer is needed)") + } else { + None + } +} + const CONTROL_TRANSIENT: [&str; 4] = [ "server exited unexpectedly", "lost tty", @@ -126,9 +155,10 @@ pub fn disconnect_text(d: &TmuxDisconnect) -> String { Some(c) => format!("ssh exited {c}"), None => "ssh exited".to_string(), }; - match first_tail() { - Some(t) => format!("{head}: {t}"), - None => head, + match (setup_problem(&d.tail), first_tail()) { + (Some(problem), _) => format!("{head}: {problem}"), + (None, Some(t)) => format!("{head}: {t}"), + (None, None) => head, } } DisconnectCause::SpawnFailed(err) => format!("spawn failed: {}", clip(err)), @@ -205,6 +235,7 @@ impl LinkMachine { attempt: 0, connected_since: None, transport_live: false, + jitter_pct: 0, } } @@ -246,7 +277,7 @@ impl LinkMachine { }; return vec![LinkCmd::MarkFailed]; } - let delay = backoff_delay_ms(&self.policy, self.attempt); + let delay = jittered(backoff_delay_ms(&self.policy, self.attempt), self.jitter_pct, now_ms); self.state = LinkState::Backoff { attempt: self.attempt, retry_at_ms: now_ms.saturating_add(delay), diff --git a/src/tmux/splitter.rs b/src/tmux/splitter.rs index f3471e35..6d18642f 100644 --- a/src/tmux/splitter.rs +++ b/src/tmux/splitter.rs @@ -220,13 +220,14 @@ impl ControlLineSplitter { } } -/// Line text without any trailing `\r`/`\n`; invalid UTF-8 is replaced. +/// Line text without any trailing `\r`/`\n`; output lines keep every byte (high bytes are +/// octal-escaped), other lines have invalid UTF-8 replaced. fn line_text(bytes: &[u8]) -> String { let mut end = bytes.len(); while end > 0 && matches!(bytes[end - 1], b'\r' | b'\n') { end -= 1; } - String::from_utf8_lossy(&bytes[..end]).into_owned() + crate::tmux::decode::ascii_safe_line(&bytes[..end]) } /// Sanitised, non-empty diagnostic lines from raw transport text. diff --git a/src/tmux/transport.rs b/src/tmux/transport.rs index 1e66c8bd..17f7e99f 100644 --- a/src/tmux/transport.rs +++ b/src/tmux/transport.rs @@ -19,8 +19,10 @@ use crate::tmux::link::{ use crate::tmux::splitter::{diag_lines, ControlLineSplitter, SplitOut}; use crate::tmux::TmuxControlSubprocess; -/// Time given to `try_wait` after the PTY stream ended, to learn the exit code. -const EXIT_CODE_PATIENCE: Duration = Duration::from_millis(50); +/// How long later pumps keep asking for the exit code after the PTY stream ended. Nothing waits: +/// each pump asks once (`try_wait`) and the exit is reported as soon as the code is known or this +/// time has passed, so the UI thread never sleeps on the link path. +const EXIT_CODE_PATIENCE: Duration = Duration::from_millis(500); /// Driver over a PTY child. pub struct PtyLinkDriver { @@ -29,6 +31,8 @@ pub struct PtyLinkDriver { line_tx: Option>, cmd_rx: Option>, exit_reported: bool, + /// The PTY stream ended at this instant; the exit code is still being collected. + exit_seen: Option, } impl Default for PtyLinkDriver { @@ -40,6 +44,7 @@ impl Default for PtyLinkDriver { line_tx: None, cmd_rx: None, exit_reported: false, + exit_seen: None, } } } @@ -76,6 +81,7 @@ impl PtyLinkDriver { line_tx: Some(line_tx), cmd_rx: Some(cmd_rx), exit_reported: false, + exit_seen: None, }; Ok((control, driver)) } @@ -99,6 +105,16 @@ impl PtyLinkDriver { } } + /// Reports the child's exit once its code is known, or without one after + /// `EXIT_CODE_PATIENCE`; never blocks. + fn report_exit_when_known(&mut self, since: std::time::Instant, poll: &mut DriverPoll) { + let code = self.pty.as_ref().and_then(|pty| pty.exit_code(Duration::ZERO)); + if code.is_some() || since.elapsed() >= EXIT_CODE_PATIENCE { + self.exit_reported = true; + poll.exited = Some(DriverExit { code }); + } + } + fn write_queued_commands(&mut self) { let (Some(rx), Some(pty)) = (self.cmd_rx.as_mut(), self.pty.as_ref()) else { return; @@ -120,6 +136,10 @@ impl LinkDriver for PtyLinkDriver { if self.exit_reported { return poll; } + if let Some(since) = self.exit_seen { + self.report_exit_when_known(since, &mut poll); + return poll; + } let events = match self.pty.as_mut() { Some(pty) => pty.drain_events(), None => return poll, @@ -140,12 +160,9 @@ impl LinkDriver for PtyLinkDriver { if exited { let pieces = self.splitter.flush_partial(); self.absorb(pieces, &mut poll); - let code = self - .pty - .as_ref() - .and_then(|pty| pty.exit_code(EXIT_CODE_PATIENCE)); - self.exit_reported = true; - poll.exited = Some(DriverExit { code }); + let since = std::time::Instant::now(); + self.exit_seen = Some(since); + self.report_exit_when_known(since, &mut poll); } else { self.write_queued_commands(); } @@ -178,7 +195,17 @@ impl LinkDriver for PtyLinkDriver { /// Connector that spawns the real `ssh -tt` PTY child. #[derive(Debug, Default)] -pub struct PtyLinkConnector {} +pub struct PtyLinkConnector { + /// Program started instead of `ssh` (tests point this at a local stand-in). + program: Option, +} + +impl PtyLinkConnector { + /// A connector that runs `program` with the ssh argv (same contract as `ssh`). + pub fn with_program(program: impl Into) -> Self { + Self { program: Some(program.into()) } + } +} impl LinkConnector for PtyLinkConnector { fn connect(&mut self, target: &LinkTarget, size: (u16, u16)) -> std::io::Result { @@ -187,7 +214,7 @@ impl LinkConnector for PtyLinkConnector { .build_ssh_tmux_cc_args(&target.session, None) .map_err(|e| std::io::Error::other(e.to_string()))?; let cfg = PtyConfig { - program: "ssh".into(), + program: self.program.clone().unwrap_or_else(|| "ssh".into()), args, rows: size.0, cols: size.1, diff --git a/src/wm/mod.rs b/src/wm/mod.rs index 21d048f9..041c06ff 100644 --- a/src/wm/mod.rs +++ b/src/wm/mod.rs @@ -49,6 +49,7 @@ pub mod session_pack_layout; pub mod session_tree; pub mod split_tree; pub mod stt_ops; +pub mod tmux_flow; pub mod tmux_io; pub mod tmux_link; pub mod tmux_remote; diff --git a/src/wm/session.rs b/src/wm/session.rs index 35b1383c..2c80f5db 100644 --- a/src/wm/session.rs +++ b/src/wm/session.rs @@ -347,6 +347,9 @@ impl WindowManager { for pid in pane_ids { let _ = self.remove_pane(pid); } + if self.tmux_link.fleet_node.as_deref() == Some(name) { + self.tmux_link_forget(); + } } FleetNodeKind::HostProfile { .. } => {} } diff --git a/src/wm/tmux_flow.rs b/src/wm/tmux_flow.rs new file mode 100644 index 00000000..15089ce8 --- /dev/null +++ b/src/wm/tmux_flow.rs @@ -0,0 +1,145 @@ +//! Size and flow control of the tmux link. +//! +//! * Size: the remote tmux client is as large as the linked pane's terminal, not the whole +//! Bilterm window, so remote programs wrap and draw for the pane they are shown in. A remote +//! window shown in one pane of its own gets its own size (`refresh-client -C @W:CxR`). +//! * Flow: tmux pauses a pane whose output the client cannot keep up with (`pause-after`); the +//! pane is then re-captured and continued, so a flooding pane costs one screen per pause +//! instead of an unbounded backlog. + +use std::collections::BTreeMap; + +use crate::tmux::decode::continue_pane_command; +use crate::tmux::hydrate::{ReplyKind, capture_pane_command}; +use crate::tmux::link::LinkState; +use crate::tmux::pane_state::{pane_states_command, parse_pane_states, state_marker}; +use crate::wm::WindowManager; + +impl WindowManager { + /// `(rows, cols)` of the linked pane that defines the remote client size: the primary pane + /// when it is mapped, else the first mapped pane, else the workspace default. + pub(crate) fn link_client_size(&self) -> (u16, u16) { + let mapped = self.tmux_bridge.mapped_panes(); + let pick = self + .tmux_link + .primary_pane + .filter(|p| mapped.iter().any(|(_, b)| b == p)) + .or_else(|| mapped.first().map(|&(_, b)| b)) + .or(self.tmux_link.primary_pane); + match pick.and_then(|id| self.panes.get(&id)) { + Some(pane) => { + let (rows, cols) = pane.vt().size(); + (rows.max(1), cols.max(1)) + } + None => (self.default_size.0.saturating_sub(1).max(1), self.default_size.1.max(1)), + } + } + + /// Sizes wanted per remote window that is shown in exactly one pane of its own. + fn link_window_sizes(&self) -> BTreeMap { + let mut per_window: BTreeMap> = BTreeMap::new(); + for (tmux, pane) in self.tmux_bridge.mapped_panes() { + if let Some(w) = self.tmux_bridge.window_of(tmux) { + per_window.entry(w).or_default().push(pane); + } + } + per_window + .into_iter() + .filter(|(_, panes)| panes.len() == 1) + .filter_map(|(w, panes)| { + let (rows, cols) = self.panes.get(&panes[0])?.vt().size(); + Some((w, (rows.max(1), cols.max(1)))) + }) + .collect() + } + + fn queue_link_command(&mut self, cmd: String) { + // The bridge bounds its queue itself (oldest plain command dropped beyond `OUTBOUND_CAP`). + self.tmux_bridge.queue_command(cmd, ReplyKind::Ignore); + } + + /// Sends size changes of linked panes to tmux while `Connected` (after the hydrate batch set + /// the first size). + pub(crate) fn pump_tmux_resize(&mut self) { + let is_connected = matches!(self.tmux_link.machine.state(), LinkState::Connected { .. }); + let has_transport = self.tmux_control.is_some() || self.tmux_link.driver.is_some(); + let global = self.link_client_size(); + if !is_connected || !has_transport { + self.tmux_link.last_resize = Some(global); + self.tmux_link.aux.sent_sizes.clear(); + return; + } + let Some(&sent_global) = self.tmux_link.aux.sent_sizes.get(&None) else { + // First tick of this transport: remember the size; only later changes are sent (the + // hydrate batch announces the size itself). + self.tmux_link.aux.sent_sizes.insert(None, global); + self.tmux_link.last_resize = Some(global); + return; + }; + if sent_global != global { + self.tmux_link.aux.sent_sizes.insert(None, global); + self.tmux_link.last_resize = Some(global); + self.queue_link_command(crate::tmux::format_refresh_client_size(global.1, global.0)); + if let Some(driver) = self.tmux_link.driver.as_mut() { + driver.resize(global.0, global.1); + } + } + for (window, size) in self.link_window_sizes() { + let sent = self.tmux_link.aux.sent_sizes.get(&Some(window)).copied(); + if sent.unwrap_or(global) != size { + self.tmux_link.aux.sent_sizes.insert(Some(window), size); + self.queue_link_command(format!("refresh-client -C @{window}:{}x{}\n", size.1, size.0)); + } + } + } + + /// First word of pane-state replies for the current attach: set once per transport + /// generation, with a nonce in live mode so captured screen text cannot imitate a reply. + pub(crate) fn link_state_marker(&mut self) -> String { + let generation = self.tmux_link.generation.to_string(); + if self.tmux_link.aux.notes.get("state_marker_gen") != Some(&generation) { + self.tmux_link.aux.notes.insert("state_marker_gen".to_string(), generation); + let marker = if self.headless_mode { + crate::tmux::pane_state::STATE_MARKER.to_string() + } else { + let nanos = self.tmux_link.epoch.elapsed().as_nanos() as u64; + let pid = u64::from(std::process::id()); + state_marker(nanos ^ pid.rotate_left(32) ^ self.tmux_link.generation.rotate_left(48)) + }; + self.tmux_bridge.set_state_marker(marker); + } + self.tmux_bridge.state_marker().to_string() + } + + /// Stores the panes' screen state for the capture replies that follow it. + pub(crate) fn note_pane_states_reply(&mut self, ok: bool, lines: &[String]) { + self.tmux_link.aux.pane_states.clear(); + if ok + && let Some(states) = lines + .first() + .and_then(|l| parse_pane_states(self.tmux_bridge.state_marker(), l)) + { + self.tmux_link.aux.pane_states = states; + } + } + + /// Panes tmux paused: drop their backlog, re-capture the screen and continue. + pub(crate) fn pump_tmux_flow(&mut self) { + let paused = self.tmux_bridge.take_paused(); + if paused.is_empty() + || !matches!(self.tmux_link.machine.state(), LinkState::Connected { .. }) + { + return; + } + let marker = self.link_state_marker(); + for tmux_pane in paused { + self.tmux_bridge.hold_output(tmux_pane); + self.tmux_bridge + .queue_command(pane_states_command(&marker), ReplyKind::PaneStates); + self.tmux_bridge + .queue_command(capture_pane_command(tmux_pane), ReplyKind::CapturePane { tmux_pane }); + self.queue_link_command(continue_pane_command(tmux_pane)); + tracing::info!(target: "bilterm::link", pane = tmux_pane, "link.pause"); + } + } +} diff --git a/src/wm/tmux_io.rs b/src/wm/tmux_io.rs index 3fba8eef..20b36772 100644 --- a/src/wm/tmux_io.rs +++ b/src/wm/tmux_io.rs @@ -9,13 +9,14 @@ use crate::pane::{LinkBanner, PaneId}; use crate::tmux::format_send_keys_hex; use crate::tmux::hydrate::{HydratePhase, ReplyKind, render_capture}; use crate::tmux::link::{ - HYDRATE_REPLY_LINES_CAP, LinkDriver, LinkEvent, LinkState, OUTBOUND_CAP, SEND_KEYS_CHUNK, + HYDRATE_REPLY_LINES_CAP, INPUT_BACKLOG_CAP, LinkDriver, LinkEvent, LinkState, OUTBOUND_CAP, SEND_KEYS_CHUNK, }; use crate::wm::WindowManager; const ORPHAN_BANNER_TEXT: &str = "REMOTE PANE GONE | close with :close"; const LINK_DOWN_INPUT_STATUS: &str = "Link down: input not delivered (Enter/r reconnects)"; const OUTBOUND_OVERFLOW_STATUS: &str = "Tmux command queue full: oldest input was dropped"; +const INPUT_OVERFLOW_STATUS: &str = "Paste too large for the tmux link: input was not sent"; const HYDRATE_SETTLE_MS: u64 = 300; impl WindowManager { @@ -92,6 +93,9 @@ impl WindowManager { self.tmux_link.aux.last_new_pane_ms = 0; self.tmux_link.aux.hydrate_sent_ms = None; self.tmux_link.aux.notes.clear(); + self.tmux_link.aux.pane_states.clear(); + self.tmux_link.aux.input_backlog.clear(); + self.tmux_link.aux.sent_sizes.clear(); } self.tmux_link .aux @@ -255,33 +259,6 @@ impl WindowManager { self.tmux_link.aux.last_mapped_count = self.tmux_bridge.mapped_panes().len(); } - /// Propagates `default_size` changes while `Connected` to `refresh-client -C` and `driver.resize`. - fn pump_tmux_resize(&mut self) { - let is_connected = matches!(self.tmux_link.machine.state(), LinkState::Connected { .. }); - let has_transport = self.tmux_control.is_some() || self.tmux_link.driver.is_some(); - if !is_connected || !has_transport { - self.tmux_link.last_resize = Some(self.default_size); - return; - } - if self.tmux_link.last_resize.is_none() { - self.tmux_link.last_resize = Some(self.default_size); - return; - } - if self.tmux_link.last_resize != Some(self.default_size) { - self.tmux_link.last_resize = Some(self.default_size); - let cols = self.default_size.1.max(1); - let rows = self.default_size.0.saturating_sub(1).max(1); - let will_overflow = self.tmux_bridge.outbound_commands.len() >= OUTBOUND_CAP; - self.tmux_bridge.queue_resize_client(cols, rows); - if will_overflow { - self.note_outbound_overflow(); - } - if let Some(driver) = self.tmux_link.driver.as_mut() { - driver.resize(rows, cols); - } - } - } - /// Converts typed bytes of tapped panes into `send-keys` (or raw ssh input while connecting). pub(crate) fn pump_tmux_input(&mut self, now_ms: u64) { self.sync_io_generation(); @@ -313,7 +290,13 @@ impl WindowManager { if !pane.remote_input_tap() { continue; } - let bytes = pane.take_remote_input(); + let fresh = pane.take_remote_input(); + let mut bytes = self.tmux_link.aux.input_backlog.remove(&pane_id).unwrap_or_default(); + if bytes.len().saturating_add(fresh.len()) > INPUT_BACKLOG_CAP { + pane.set_status_message(Some(INPUT_OVERFLOW_STATUS.to_string())); + } else { + bytes.extend_from_slice(&fresh); + } if bytes.is_empty() { continue; } @@ -339,13 +322,16 @@ impl WindowManager { && (matches!(state, LinkState::Connected { .. } | LinkState::Connecting { .. }) || (!has_target && matches!(state, LinkState::Idle))) { - for chunk in bytes.chunks(SEND_KEYS_CHUNK) { - let will_overflow = self.tmux_bridge.outbound_commands.len() >= OUTBOUND_CAP; + // Never queue more than the command queue can hold (it drops its oldest entries + // when full, which would lose typed bytes); the rest waits for the next tick. + let room = (OUTBOUND_CAP / 2).saturating_sub(self.tmux_bridge.outbound_commands.len()); + let take = room.saturating_mul(SEND_KEYS_CHUNK).min(bytes.len()); + for chunk in bytes[..take].chunks(SEND_KEYS_CHUNK) { let cmd = format_send_keys_hex(tmux_pane, chunk); self.tmux_bridge.queue_command(cmd, ReplyKind::Ignore); - if will_overflow { - self.note_outbound_overflow(); - } + } + if take < bytes.len() { + self.tmux_link.aux.input_backlog.insert(pane_id, bytes.split_off(take)); } } else if let Some(pane) = self.panes.get_mut(&pane_id) && pane.status_message() != Some(LINK_DOWN_INPUT_STATUS) @@ -398,9 +384,11 @@ impl WindowManager { .saturating_add(HYDRATE_SETTLE_MS) { let tmux_ids: Vec = mapped.iter().map(|&(t, _)| t).collect(); - let cols = self.default_size.1.max(1); - let rows = self.default_size.0.saturating_sub(1).max(1); - let planned = self.tmux_link.hydrate.plan(&tmux_ids, cols, rows); + let (rows, cols) = self.link_client_size(); + let marker = self.link_state_marker(); + let planned = self.tmux_link.hydrate.plan_with_state(&tmux_ids, cols, rows, &marker); + self.tmux_link.aux.sent_sizes.clear(); + self.tmux_link.aux.sent_sizes.insert(None, (rows, cols)); self.tmux_link .aux .notes @@ -448,6 +436,10 @@ impl WindowManager { } continue; } + if reply.kind == ReplyKind::PaneStates { + self.note_pane_states_reply(reply.ok, &reply.lines); + continue; + } if let ReplyKind::CapturePane { tmux_pane } = reply.kind { let capped = if reply.lines.len() > HYDRATE_REPLY_LINES_CAP { &reply.lines[..HYDRATE_REPLY_LINES_CAP] @@ -458,7 +450,10 @@ impl WindowManager { if let Some(&pane_id) = self.tmux_bridge.tmux_to_bilterm.get(&tmux_pane) && let Some(pane) = self.panes.get_mut(&pane_id) { - let rendered = render_capture(capped); + let rendered = match self.tmux_link.aux.pane_states.remove(&tmux_pane) { + Some(st) => crate::tmux::pane_state::render_capture_with_state(capped, &st), + None => render_capture(capped), + }; if !rendered.is_empty() { pane.count_payload_rx(rendered.len()); pane.process_pty_output(&rendered); @@ -499,6 +494,10 @@ impl WindowManager { lines = hydrated_lines, "link.hydrate" ); + // Flow control starts once the screen is in place (tmux 3.2+; older servers answer + // `%error`, which is ignored). + self.tmux_bridge + .queue_command(crate::tmux::decode::pause_after_command(), ReplyKind::Ignore); } } } diff --git a/src/wm/tmux_link.rs b/src/wm/tmux_link.rs index 9f2caae4..a95554e1 100644 --- a/src/wm/tmux_link.rs +++ b/src/wm/tmux_link.rs @@ -175,13 +175,10 @@ impl WindowManager { self.tmux_control = None; } - /// Current link state (`Idle` when no link was ever started). + /// Current link state (`Idle` when no link was ever started). Always the machine's real + /// state: a link whose fleet node was removed is forgotten eagerly (`disconnect_fleet_node`) + /// or on the next tick, never reported as `Idle` while its transport is still running. pub fn tmux_link_state(&self) -> &LinkState { - if let Some(ref fname) = self.tmux_link.fleet_node - && !self.fleet.nodes.contains_key(fname) - { - return &LinkState::Idle; - } self.tmux_link.machine.state() } @@ -268,6 +265,9 @@ impl WindowManager { "link.connect" ); + if !self.headless_mode { + self.tmux_link.machine.jitter_pct = crate::tmux::reconnect::LINK_JITTER_PCT; + } let size = self.default_size; let res = if let Some(conn) = self.tmux_link.connector.as_mut() { conn.connect(&target, size) @@ -562,6 +562,7 @@ impl WindowManager { } // 3. I/O pumps (plan 13): + self.pump_tmux_flow(); self.pump_tmux_input(now_ms); self.discover_idle_panes(); self.pump_tmux_outbound(); diff --git a/tests/guard02_pane_transport.rs b/tests/guard02_pane_transport.rs index e5a16f31..d9122501 100644 --- a/tests/guard02_pane_transport.rs +++ b/tests/guard02_pane_transport.rs @@ -48,8 +48,11 @@ fn link_then_disconnect_transitions() { fn remote_buffer_is_capped() { let mut p = headless(); p.set_remote_input_tap(true); - p.write_to_pty(&vec![b'a'; 10_000]).unwrap(); - assert_eq!(p.take_remote_input().len(), 4096); + // The cap is sized for a large paste arriving as one event (it was 4096, which silently + // truncated pastes); beyond it bytes are still dropped rather than buffered without bound. + let cap = bilterm::tmux::link::REMOTE_INPUT_CAP; + p.write_to_pty(&vec![b'a'; cap + 10_000]).unwrap(); + assert_eq!(p.take_remote_input().len(), cap); } #[test] diff --git a/tests/phase51_1_e2e.rs b/tests/phase51_1_e2e.rs index a349008a..643ccd4b 100644 --- a/tests/phase51_1_e2e.rs +++ b/tests/phase51_1_e2e.rs @@ -223,14 +223,7 @@ fn drop_reconnect_hydrate_scenario() { // 4. Hydrate after 300 ms settle (at 700 ms): refresh-client + two capture-pane commands wm.tick_tmux_link(700); assert_eq!(wm.link_hydrate_phase(), HydratePhase::Sent); - assert_eq!( - drain_cmds(&mut h1.cmd_rx), - vec![ - "refresh-client -C 80x23\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %3\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %4\n".to_string(), - ] - ); + assert_hydrate_batch(drain_cmds(&mut h1.cmd_rx), &[3, 4]); feed( &h1.line_tx, &[ @@ -264,9 +257,13 @@ fn drop_reconnect_hydrate_scenario() { // 5. `ls` typed in pane for %3 sent as `send-keys -t %3 -H 6c 73 0a` wm.pane_mut(p3).unwrap().submit_command("ls").unwrap(); wm.tick_tmux_link(750); + // Flow control is requested once the hydrate is done (it precedes the first keystrokes). assert_eq!( drain_cmds(&mut h1.cmd_rx), - vec!["send-keys -t %3 -H 6c 73 0a\n".to_string()] + vec![ + "refresh-client -f pause-after=2\n".to_string(), + "send-keys -t %3 -H 6c 73 0a\n".to_string(), + ] ); // 6. EOF at 10 s (10_000 ms) -> LINK DOWN banners on both panes @@ -321,14 +318,7 @@ fn drop_reconnect_hydrate_scenario() { // 8. Hydrate again on generation 2 wm.tick_tmux_link(11_400); assert_eq!(wm.link_hydrate_phase(), HydratePhase::Sent); - assert_eq!( - drain_cmds(&mut h2.cmd_rx), - vec![ - "refresh-client -C 80x23\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %3\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %4\n".to_string(), - ] - ); + assert_hydrate_batch(drain_cmds(&mut h2.cmd_rx), &[3, 4]); feed( &h2.line_tx, &[ @@ -715,3 +705,15 @@ fn fake_connector_is_the_only_transport() { wm.tick_tmux_link(1100); assert_eq!(conn.calls(), 2); } + +/// The hydrate batch: the client size of the linked pane, one state query for all panes, then +/// one capture per pane (sizes follow the pane, so only the command shapes are pinned here). +#[allow(dead_code)] +fn assert_hydrate_batch(cmds: Vec, panes: &[usize]) { + assert_eq!(cmds.len(), panes.len() + 2, "{cmds:?}"); + assert!(cmds[0].starts_with("refresh-client -C "), "{cmds:?}"); + assert!(cmds[1].starts_with("display-message -p \"bilterm-state #{W:#{P:[#{pane_id} "), "{cmds:?}"); + for (cmd, pane) in cmds[2..].iter().zip(panes) { + assert_eq!(*cmd, format!("capture-pane -p -e -J -S -2000 -t %{pane}\n")); + } +} diff --git a/tests/phase51_1_io.rs b/tests/phase51_1_io.rs index 47c652c1..700411d0 100644 --- a/tests/phase51_1_io.rs +++ b/tests/phase51_1_io.rs @@ -317,27 +317,24 @@ mod input { assert!(drv_state.lock().unwrap().resizes.is_empty()); // Changing default_size twice with the same value queues one refresh-client and one resize. + // The remote client is as large as the linked pane (not the whole window), so the + // expected sizes are read from the pane. Changing the size twice with the same value + // queues one refresh-client and one resize. wm.resize_all(Rect::new(0, 0, 100, 31)).unwrap(); wm.tick_tmux_link(20); wm.resize_all(Rect::new(0, 0, 100, 31)).unwrap(); wm.tick_tmux_link(30); - assert_eq!( - drain_cmds(&mut cmd_rx), - vec!["refresh-client -C 100x30\n".to_string()] - ); - assert_eq!(drv_state.lock().unwrap().resizes, vec![(30, 100)]); + let (r1, c1) = wm.pane(_primary).unwrap().vt().size(); + assert!(c1 < 100, "the linked pane is narrower than the window ({c1})"); + assert_eq!(drain_cmds(&mut cmd_rx), vec![format!("refresh-client -C {c1}x{r1}\n")]); + assert_eq!(drv_state.lock().unwrap().resizes, vec![(r1, c1)]); // A different size queues another and calls driver.resize once more. wm.resize_all(Rect::new(0, 0, 120, 41)).unwrap(); wm.tick_tmux_link(40); - assert_eq!( - drain_cmds(&mut cmd_rx), - vec!["refresh-client -C 120x40\n".to_string()] - ); - assert_eq!( - drv_state.lock().unwrap().resizes, - vec![(30, 100), (40, 120)] - ); + let (r2, c2) = wm.pane(_primary).unwrap().vt().size(); + assert_eq!(drain_cmds(&mut cmd_rx), vec![format!("refresh-client -C {c2}x{r2}\n")]); + assert_eq!(drv_state.lock().unwrap().resizes, vec![(r1, c1), (r2, c2)]); } } @@ -369,14 +366,7 @@ fn hydrate_sends_refresh_then_captures_after_settle() { // At +300 ms refresh-client then capture-pane %3 and %4 are sent. wm.tick_tmux_link(400); assert_eq!(wm.link_hydrate_phase(), HydratePhase::Sent); - assert_eq!( - drain_cmds(&mut cmd_rx), - vec![ - "refresh-client -C 80x23\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %3\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %4\n".to_string(), - ] - ); + assert_hydrate_batch(drain_cmds(&mut cmd_rx), &[3, 4]); } #[test] @@ -607,13 +597,7 @@ fn generation_change_restarts_hydrate() { wm.tick_tmux_link(500); wm.tick_tmux_link(800); assert_eq!(wm.link_hydrate_phase(), HydratePhase::Sent); - assert_eq!( - drain_cmds(&mut cmd_rx2), - vec![ - "refresh-client -C 80x23\n".to_string(), - "capture-pane -p -e -J -S -2000 -t %3\n".to_string(), - ] - ); + assert_hydrate_batch(drain_cmds(&mut cmd_rx2), &[3]); send( &tx2, &[ @@ -989,3 +973,15 @@ fn removed_pane_does_not_receive_late_output() { .contains("resurrected") ); } + +/// The hydrate batch: the client size of the linked pane, one state query for all panes, then +/// one capture per pane (sizes follow the pane, so only the command shapes are pinned here). +#[allow(dead_code)] +fn assert_hydrate_batch(cmds: Vec, panes: &[usize]) { + assert_eq!(cmds.len(), panes.len() + 2, "{cmds:?}"); + assert!(cmds[0].starts_with("refresh-client -C "), "{cmds:?}"); + assert!(cmds[1].starts_with("display-message -p \"bilterm-state #{W:#{P:[#{pane_id} "), "{cmds:?}"); + for (cmd, pane) in cmds[2..].iter().zip(panes) { + assert_eq!(*cmd, format!("capture-pane -p -e -J -S -2000 -t %{pane}\n")); + } +} diff --git a/tests/phase51_1_ssh_argv.rs b/tests/phase51_1_ssh_argv.rs index cdc19b0c..6e1bceb5 100644 --- a/tests/phase51_1_ssh_argv.rs +++ b/tests/phase51_1_ssh_argv.rs @@ -17,7 +17,7 @@ fn link_args_use_tt_keepalive_and_no_mux() { assert!(!args.iter().any(|a| a == "-T")); // Keepalive options - assert!(args.contains(&"ServerAliveInterval=15".to_string())); + assert!(args.contains(&"ServerAliveInterval=5".to_string())); assert!(args.contains(&"ServerAliveCountMax=3".to_string())); assert!(args.contains(&"TCPKeepAlive=yes".to_string())); assert!(args.contains(&"ConnectTimeout=15".to_string())); @@ -211,5 +211,5 @@ fn redact_argv_keeps_remote_command_for_diagnosis() { assert!(redacted.contains("tmux -CC new-session -A -s main")); assert!(redacted.contains("ubuntu@10.0.0.42")); - assert!(redacted.contains("ServerAliveInterval=15")); + assert!(redacted.contains("ServerAliveInterval=5")); } diff --git a/tests/tmux_link_transcripts.rs b/tests/tmux_link_transcripts.rs new file mode 100644 index 00000000..a3c1ef4f --- /dev/null +++ b/tests/tmux_link_transcripts.rs @@ -0,0 +1,322 @@ +//! Recorded `tmux -CC` control-mode transcripts fed through the real splitter, bridge and pumps +//! into a headless pane, with assertions on the resulting screen and on every command Bilterm +//! writes back. Each test pins one defect found by driving a real tmux over a real ssh transport +//! (see `tests/tmux_link_live.rs` for the live counterpart). + +use bilterm::action::Action; +use bilterm::colon_command::parse_colon_command; +use bilterm::pane::PaneId; +use bilterm::remote::RemoteHostProfile; +use bilterm::tmux::TmuxControlSubprocess; +use bilterm::tmux::link::{DisconnectCause, ExitClass, LinkState, TmuxDisconnect}; +use bilterm::tmux::reconnect::{disconnect_text, jittered}; +use bilterm::tmux::splitter::{ControlLineSplitter, SplitOut}; +use bilterm::wm::WindowManager; +use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender}; + +/// A linked headless workspace plus a scripted tmux server on the other end of the channels. +struct Rig { + wm: WindowManager, + pane: PaneId, + line_tx: UnboundedSender, + cmd_rx: UnboundedReceiver, + splitter: ControlLineSplitter, + now: u64, + block: u64, + /// Every command Bilterm wrote, in order. + log: Vec, + /// Reply to the next `display-message` (pane state) and `capture-pane`. + state: String, + screen: Vec, +} + +impl Rig { + fn connect() -> Self { + let mut wm = WindowManager::new_headless(24, 80); + let pane = wm.connect_ssh_tmux("me@box", "main", vec![]).unwrap(); + let (cmd_tx, cmd_rx) = tokio::sync::mpsc::unbounded_channel::(); + let (line_tx, line_rx) = tokio::sync::mpsc::unbounded_channel::(); + wm.attach_tmux_control(TmuxControlSubprocess::from_channels("main", cmd_tx, line_rx)); + let mut rig = Rig { + wm, + pane, + line_tx, + cmd_rx, + splitter: ControlLineSplitter::new(), + now: 0, + block: 100, + log: Vec::new(), + state: "0 0 0 80 20 1 0 0 0 0 0".into(), + screen: vec!["$ ".into()], + }; + // What a real server sends first: the DCS sentinel, the greeting block, the session. + rig.feed(b"\x1bP1000p%begin 1 1 0\r\n%end 1 1 0\r\n%session-changed $0 main\r\n"); + rig.settle(); + assert!(matches!(rig.wm.tmux_link_state(), LinkState::Connected { .. })); + rig + } + + /// Raw transport bytes, split exactly as the PTY driver does. + fn feed(&mut self, raw: &[u8]) { + for piece in self.splitter.push(raw) { + if let SplitOut::Line(line) = piece { + self.line_tx.send(line).unwrap(); + } + } + self.tick(); + } + + fn tick(&mut self) { + self.now += 50; + self.wm.tick_tmux_link(self.now); + while let Ok(cmd) = self.cmd_rx.try_recv() { + self.answer(&cmd); + self.log.push(cmd); + } + } + + /// Replies the way tmux does: one `%begin`..`%end` block per command, in order. + fn answer(&mut self, cmd: &str) { + self.block += 1; + let n = self.block; + let mut body: Vec = Vec::new(); + if cmd.starts_with("list-panes") { + body.push("@0 %0 bash".into()); + } else if cmd.starts_with("display-message") { + body.push(format!("bilterm-state [%0 {}]", self.state)); + } else if cmd.starts_with("capture-pane") { + body.extend(self.screen.iter().cloned()); + } else if cmd.starts_with("refresh-client -A") { + body.push("%continue %0".into()); + } + let _ = self.line_tx.send(format!("%begin 1 {n} 1")); + for l in body { + let _ = self.line_tx.send(l); + } + let _ = self.line_tx.send(format!("%end 1 {n} 1")); + } + + fn settle(&mut self) { + for _ in 0..40 { + self.tick(); + } + } + + fn text(&self) -> String { + self.wm.pane(self.pane).unwrap().vt().contents() + } + + fn sent(&self, prefix: &str) -> Vec<&String> { + self.log.iter().filter(|c| c.starts_with(prefix)).collect() + } +} + +#[test] +fn utf8_split_across_output_lines_renders_intact() { + let mut rig = Rig::connect(); + // tmux cuts `%output` at arbitrary bytes and leaves bytes >= 0x80 raw: "漢" = e6 bc a2 and + // "😀" = f0 9f 98 80 are each split over two lines here, and the chunks over two reads. + rig.feed(b"%output %0 a\xe6\r\n%output %0 \xbc\xa2b \xf0\x9f"); + rig.feed(b"\r\n%output %0 \x98\x80 c\\015\\012\r\n"); + let text = rig.text(); + assert!(text.contains("a漢b 😀 c"), "got {text:?}"); + assert!(!text.contains('\u{fffd}'), "no replacement characters: {text:?}"); +} + +#[test] +fn extended_output_is_rendered_and_a_pause_recaptures_then_continues() { + let mut rig = Rig::connect(); + assert_eq!(rig.sent("refresh-client -f pause-after=").len(), 1, "flow control is requested once per attach"); + rig.feed(b"%extended-output %0 1500 : STALE-LINE\\015\\012\r\n"); + assert!(rig.text().contains("STALE-LINE")); + + rig.log.clear(); + rig.state = "0 4 1 80 20 1 0 0 0 0 0".into(); + rig.screen = vec!["FRESH-ONE".into(), "$ ok".into()]; + // The pane fell behind: tmux pauses it; output that was already in flight must not be drawn + // on top of the re-captured screen. + rig.feed(b"%pause %0\r\n%extended-output %0 9000 : LATE-BACKLOG\r\n"); + rig.settle(); + let order: Vec<&str> = rig.log.iter().map(|c| c.split(' ').next().unwrap()).collect(); + assert_eq!(order, ["display-message", "capture-pane", "refresh-client"], "{:?}", rig.log); + assert_eq!(rig.log[2], "refresh-client -A '%0:continue'\n", "quoted: tmux rejects a bare leading %"); + let text = rig.text(); + assert!(text.contains("FRESH-ONE") && text.contains("$ ok"), "{text:?}"); + assert!(!text.contains("STALE-LINE") && !text.contains("LATE-BACKLOG"), "{text:?}"); + assert_eq!(rig.wm.pane(rig.pane).unwrap().vt().cursor_position(), (1, 4)); + + // Live output flows again after the continue. + rig.feed(b"%extended-output %0 0 : \\015\\012AFTER\r\n"); + assert!(rig.text().contains("AFTER")); +} + +#[test] +fn hydrate_restores_alternate_screen_cursor_and_mouse_mode() { + let mut rig = Rig::connect(); + rig.settle(); + // Reconnect while `vim` runs remotely: alternate screen on, cursor at column 8 of row 0. + rig.state = "1 8 0 80 20 1 0 0 0 0 1".into(); + rig.screen = vec!["HELLO-VIM".into(), "~".into(), "~".into()]; + rig.feed(b"%pause %0\r\n"); + rig.settle(); + let vt = rig.wm.pane(rig.pane).unwrap().vt(); + assert!(vt.is_alternate_screen(), "the remote is on the alternate screen"); + assert_eq!(vt.cursor_position(), (0, 8)); + assert!(vt.contents().starts_with("HELLO-VIM")); + + // The application leaves the alternate screen: the primary screen must not show its rows. + rig.feed(b"%output %0 \\033[?1049l$ done\r\n"); + let vt = rig.wm.pane(rig.pane).unwrap().vt(); + assert!(!vt.is_alternate_screen()); + assert!(!vt.contents().contains("HELLO-VIM"), "{:?}", vt.contents()); +} + +#[test] +fn remote_client_size_is_the_linked_pane_not_the_whole_window() { + let mut rig = Rig::connect(); + rig.settle(); + let (rows, cols) = rig.wm.pane(rig.pane).unwrap().vt().size(); + assert!(cols < 80, "the linked pane is one half of an 80-column workspace (got {cols})"); + let sizes = rig.sent("refresh-client -C "); + assert_eq!(sizes.len(), 1, "{sizes:?}"); + assert_eq!(*sizes[0], format!("refresh-client -C {cols}x{rows}\n")); +} + +#[test] +fn a_100_kb_paste_reaches_tmux_complete_and_in_order() { + let mut rig = Rig::connect(); + rig.settle(); + rig.log.clear(); + let paste: Vec = (0..100_000u32).map(|i| b"abcdefghij\n"[(i % 11) as usize]).collect(); + rig.wm.pane_mut(rig.pane).unwrap().write_to_pty(&paste).unwrap(); + rig.settle(); + let mut got = Vec::new(); + for cmd in rig.sent("send-keys -t %0 -H") { + for hex in cmd.trim_end().split(' ').skip(4) { + got.push(u8::from_str_radix(hex, 16).unwrap()); + } + } + assert_eq!(got.len(), paste.len(), "no byte of the paste is dropped"); + assert!(got == paste, "bytes arrive in order"); +} + +#[test] +fn malformed_remote_input_never_panics_or_breaks_the_link() { + let mut rig = Rig::connect(); + let mut junk: Vec = (0u8..=255).cycle().take(4096).collect(); + junk.extend_from_slice(b"\r\n%output\r\n%output %x y\r\n%extended-output %0\r\n%pause\r\n%pause %99999999999999999999\r\n"); + junk.extend_from_slice(b"%layout-change @1 ,,,{{[[\r\n%begin x y z\r\n%end 1 2\r\n%future-notification @1 %2 $3\r\n%output %0 \\03\r\n"); + junk.extend_from_slice(&vec![b'A'; 300 * 1024]); + junk.extend_from_slice(b"\r\n%output %0 STILL-ALIVE\r\n"); + for chunk in junk.chunks(777) { + rig.feed(chunk); + } + assert!(matches!(rig.wm.tmux_link_state(), LinkState::Connected { .. })); + assert!(rig.text().contains("STILL-ALIVE")); +} + +#[test] +fn nothing_bilterm_writes_ever_kills_a_remote_session() { + let mut rig = Rig::connect(); + rig.feed(b"%pause %0\r\n"); + rig.settle(); + rig.wm.link_disconnect(); + rig.settle(); + rig.wm.link_reconnect(); + rig.settle(); + assert!(!rig.log.is_empty()); + for cmd in &rig.log { + assert!(!cmd.contains("kill-"), "{cmd}"); + } + // No link source file can even spell a kill command. + let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")); + let mut files: Vec = std::fs::read_dir(root.join("src/tmux")) + .unwrap() + .map(|e| e.unwrap().path()) + .collect(); + for f in ["src/wm/tmux_link.rs", "src/wm/tmux_io.rs", "src/wm/tmux_flow.rs", "src/wm/tmux_remote.rs", "src/remote/mod.rs"] { + files.push(root.join(f)); + } + for f in files { + let src = std::fs::read_to_string(&f).unwrap(); + for word in ["kill-session", "kill-server", "kill-window"] { + assert!(!src.contains(word), "{} mentions {word}", f.display()); + } + } +} + +#[test] +fn removing_the_link_node_stops_the_transport_instead_of_masking_the_state() { + let mut rig = Rig::connect(); + assert!(rig.wm.is_tmux_control_attached()); + assert!(rig.wm.disconnect_fleet_node("tmux-me@box-main")); + // Before: the state read `idle` while the control transport was still attached. + assert_eq!(*rig.wm.tmux_link_state(), LinkState::Idle); + assert!(!rig.wm.is_tmux_control_attached(), "idle means the transport is gone"); +} + +#[test] +fn double_dash_reaches_hosts_named_like_link_verbs() { + for word in ["info", "reconnect", "disconnect", "prefill"] { + let parsed = parse_colon_command(&format!(":ssh-tmux -- {word} work")).unwrap().unwrap(); + assert_eq!(parsed, Action::SshTmuxConnect { target: word.into(), session: "work".into() }); + assert!(matches!( + parse_colon_command(&format!(":ssh-tmux {word}")).unwrap().unwrap(), + Action::RemoteLink(_) + )); + } + assert_eq!( + parse_colon_command(":ssh-tmux -- box").unwrap().unwrap(), + Action::SshTmuxConnect { target: "box".into(), session: "bilterm".into() } + ); + assert!(parse_colon_command(":ssh-tmux -- -oProxyCommand=x").unwrap().is_err()); + assert!(parse_colon_command(":ssh-tmux --").unwrap().is_err()); +} + +#[test] +fn link_transport_detects_a_dead_peer_in_seconds_and_stays_attach_or_create() { + let prof = RemoteHostProfile::new("box", "me@box"); + let link = prof.build_ssh_tmux_cc_args("my session;rm -rf", None).unwrap(); + assert!(link.contains(&"ServerAliveInterval=5".to_string()), "{link:?}"); + assert!(link.contains(&"ServerAliveCountMax=3".to_string())); + assert!(link.contains(&"ConnectTimeout=15".to_string())); + let remote = link.last().unwrap(); + assert!(remote.ends_with("tmux -CC new-session -A -s mysessionrm-rf'"), "{remote}"); + // An interactive `:ssh` pane keeps the gentler interval. + let shell = prof.build_ssh_args(None).unwrap(); + assert!(shell.contains(&"ServerAliveInterval=15".to_string())); +} + +#[test] +fn backoff_jitter_is_bounded_and_spread() { + let mut seen = std::collections::BTreeSet::new(); + for now in 0..2000u64 { + let d = jittered(1000, 25, now * 37); + assert!((1000..=1250).contains(&d), "{d}"); + seen.insert(d); + } + assert!(seen.len() > 50, "delays are spread, not constant ({})", seen.len()); + assert_eq!(jittered(1000, 0, 12345), 1000); + assert_eq!(jittered(0, 25, 12345), 0); +} + +#[test] +fn missing_or_old_tmux_is_named_in_the_failure_text() { + let down = |tail: &str| TmuxDisconnect { + cause: DisconnectCause::ProcessExit(Some(127)), + reason: None, + code: Some(127), + tail: vec!["Connection to box closed.".into(), tail.into()], + at_ms: 0, + class: ExitClass::Fatal, + }; + let missing = disconnect_text(&down("bash: line 1: exec: tmux: not found")); + assert!(missing.contains("tmux is not installed on the remote host"), "{missing}"); + let old = disconnect_text(&down("tmux: unknown option -- C")); + assert!(old.contains("too old for control mode"), "{old}"); + // Authentication errors keep ssh's own line (it already says what happened). + let mut d = down("me@box: Permission denied (publickey)."); + d.tail.remove(0); + let auth = disconnect_text(&d); + assert!(auth.contains("Permission denied (publickey)"), "{auth}"); +} From 513bb4016c3d675f50ada075a959c86a32daa9aa Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:22:43 -0500 Subject: [PATCH 2/3] test(tmux-link): live integration test against a real tmux on a private socket Gated behind BILTERM_TMUX_LIVE=1. Uses an ssh stand-in that runs the remote command locally in a PTY with a private TMUX_TMPDIR and HOME: connect, size and render check, kill the transport, assert the remote session and its counter loop are alive, automatic reconnect, assert same session, same pane mapping, output produced while away is hydrated and nothing is duplicated. Co-Authored-By: Claude Fable 5.2 --- tests/tmux_link_live.rs | 189 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 tests/tmux_link_live.rs diff --git a/tests/tmux_link_live.rs b/tests/tmux_link_live.rs new file mode 100644 index 00000000..d4e21b2e --- /dev/null +++ b/tests/tmux_link_live.rs @@ -0,0 +1,189 @@ +//! Live integration test of the persistent tmux link against a REAL tmux server. +//! +//! Gated: runs only with `BILTERM_TMUX_LIVE=1` (CI without tmux skips it). +//! +//! ```text +//! BILTERM_TMUX_LIVE=1 cargo test --test tmux_link_live -- --nocapture +//! ``` +//! +//! Isolation: the tmux server lives on a private socket (`TMUX_TMPDIR` inside a fresh temp dir, +//! with `HOME` there too, so no `~/.tmux.conf` and no default-socket server is touched). The +//! transport is an `ssh` stand-in: a script that receives Bilterm's real ssh argv, ignores the +//! options and the host, and runs the remote command locally in a PTY. Everything after the +//! process boundary is real: `tmux -CC new-session -A`, control mode, `%output`, replies. +//! +//! Scenario: connect, render check, size check, kill the transport, assert the remote session +//! and its counter loop are alive, reconnect, assert continuity (same session, same pane, output +//! produced while away is on screen, nothing duplicated). + +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::{Duration, Instant}; + +use bilterm::pane::PaneId; +use bilterm::tmux::hydrate::HydratePhase; +use bilterm::tmux::link::{LinkState, ReconnectPolicy}; +use bilterm::tmux::transport::PtyLinkConnector; +use bilterm::wm::WindowManager; + +const SESSION: &str = "bilterm-live"; + +struct Live { + dir: PathBuf, + wm: WindowManager, + start: Instant, +} + +impl Live { + /// Private-socket tmux command (never the default socket). + fn tmux(&self, args: &[&str]) -> (bool, String) { + let out = Command::new("tmux") + .args(args) + .env("TMUX_TMPDIR", &self.dir) + .env("HOME", &self.dir) + .env_remove("TMUX") + .output() + .expect("run tmux"); + (out.status.success(), String::from_utf8_lossy(&out.stdout).trim().to_string()) + } + + fn tick(&mut self) { + let now = self.start.elapsed().as_millis() as u64; + self.wm.tick_tmux_link(now); + std::thread::sleep(Duration::from_millis(15)); + } + + fn wait(&mut self, what: &str, secs: u64, mut done: impl FnMut(&mut Live) -> bool) { + let deadline = Instant::now() + Duration::from_secs(secs); + while Instant::now() < deadline { + self.tick(); + if done(self) { + return; + } + } + panic!("timed out after {secs}s waiting for: {what} (state {:?})", self.wm.tmux_link_state()); + } + + fn connected_and_hydrated(&self) -> bool { + matches!(self.wm.tmux_link_state(), LinkState::Connected { .. }) + && self.wm.link_hydrate_phase() == HydratePhase::Done + } + + fn text(&self, pane: PaneId) -> String { + self.wm.pane(pane).unwrap().vt().contents() + } + + fn counter(&self) -> u64 { + std::fs::read_to_string(self.dir.join("counter")) + .ok() + .and_then(|s| s.trim().parse().ok()) + .unwrap_or(0) + } +} + +fn write_ssh_stand_in(dir: &Path) -> PathBuf { + use std::os::unix::fs::PermissionsExt; + let path = dir.join("ssh-stand-in"); + let script = format!( + "#!/bin/sh\n# Receives Bilterm's ssh argv; the last argument is the remote command.\n\ + for a in \"$@\"; do last=\"$a\"; done\n\ + unset TMUX TMUX_PANE\n\ + export TMUX_TMPDIR='{d}' HOME='{d}' SHELL=/bin/sh PS1='live$ '\n\ + cd '{d}' && exec /bin/sh -c \"$last\"\n", + d = dir.display() + ); + std::fs::write(&path, script).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + path +} + +#[test] +fn live_link_survives_transport_loss_and_reattaches_to_the_same_session() { + if std::env::var("BILTERM_TMUX_LIVE").as_deref() != Ok("1") { + eprintln!("tmux_link_live: skipped (set BILTERM_TMUX_LIVE=1 to run against a real tmux)"); + return; + } + if Command::new("tmux").arg("-V").output().is_err() { + eprintln!("tmux_link_live: skipped (no tmux binary)"); + return; + } + // Short path: the tmux socket lives below it and unix socket paths are limited to ~100 bytes. + let dir = PathBuf::from(format!("/tmp/btl-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let stand_in = write_ssh_stand_in(&dir); + + let mut wm = WindowManager::new_headless(24, 80); + wm.set_link_connector(Box::new(PtyLinkConnector::with_program(stand_in.to_string_lossy()))); + wm.set_link_policy(ReconnectPolicy { base_ms: 300, ..ReconnectPolicy::default() }); + let pane = wm.connect_ssh_tmux("live@rig", SESSION, vec![]).unwrap(); + let mut live = Live { dir: dir.clone(), wm, start: Instant::now() }; + + // 1. Connect: a new remote session is created and attached. + live.wait("first connect + hydrate", 20, |l| l.connected_and_hydrated()); + let (ok, sessions) = live.tmux(&["list-sessions", "-F", "#{session_name}"]); + assert!(ok && sessions == SESSION, "one session named {SESSION}: {sessions:?}"); + eprintln!("[live] connected: session={sessions} panes={:?}", live.wm.tmux_link_summary().panes); + + // 2. Size: the remote window is as large as the linked pane. + let (rows, cols) = live.wm.pane(pane).unwrap().vt().size(); + live.wait("remote window takes the pane size", 10, |l| { + l.tmux(&["list-panes", "-t", SESSION, "-F", "#{pane_width}x#{pane_height}"]).1 + == format!("{cols}x{rows}") + }); + eprintln!("[live] remote pane size {cols}x{rows} == linked pane"); + + // 3. Render: typed input reaches the remote shell, output (with SGR and UTF-8) comes back. + let typed = "i=0; while :; do i=$((i+1)); echo $i > $HOME/counter; sleep 0.2; done &\r\ + printf '\\033[1;32mMARK-%s\\033[0m \\346\\274\\242\\345\\255\\227\\n' $((6*7))\r"; + live.wm.pane_mut(pane).unwrap().write_to_pty(typed.as_bytes()).unwrap(); + live.wait("command output rendered", 10, |l| l.text(pane).contains("MARK-42 漢字")); + live.wait("counter loop running", 10, |l| l.counter() >= 2); + let mapping_before = live.wm.tmux_link_summary().panes; + let panes_before = live.wm.pane_count(); + eprintln!("[live] rendered 'MARK-42 漢字'; counter={}", live.counter()); + + // 4. Disconnect: kill the transport (the tmux control client behind the stand-in). + let (_, client_pid) = live.tmux(&["list-clients", "-F", "#{client_pid}"]); + let status = Command::new("kill").args(["-9", client_pid.trim()]).status().unwrap(); + assert!(status.success(), "killed transport pid {client_pid}"); + live.wait("link notices the loss", 10, |l| { + !matches!(l.wm.tmux_link_state(), LinkState::Connected { .. }) + }); + eprintln!("[live] transport pid {client_pid} killed -> state {}", live.wm.tmux_link_state().as_str()); + + // 5. THE requirement: the remote session and its programs are still alive. + assert!(live.tmux(&["has-session", "-t", SESSION]).0, "remote session survives the disconnect"); + let c1 = live.counter(); + std::thread::sleep(Duration::from_millis(900)); + let c2 = live.counter(); + assert!(c2 > c1, "counter keeps counting while disconnected ({c1} -> {c2})"); + // Output produced while Bilterm is away. + assert!(live.tmux(&["send-keys", "-t", SESSION, "echo AWAY-$((40+2))", "Enter"]).0); + eprintln!("[live] remote session alive while disconnected; counter {c1} -> {c2}"); + + // 6. Reconnect (automatic, backoff): same session, same pane, nothing duplicated or lost. + live.wait("automatic reconnect + re-hydrate", 20, |l| l.connected_and_hydrated()); + live.wait("output produced while away is on screen", 10, |l| l.text(pane).contains("AWAY-42")); + assert!(live.text(pane).contains("MARK-42 漢字"), "earlier output is still there"); + assert_eq!(live.text(pane).matches("MARK-42 漢字").count(), 1, "re-hydrate does not duplicate output"); + assert_eq!(live.wm.tmux_link_summary().panes, mapping_before, "same pane mapping"); + assert_eq!(live.wm.pane_count(), panes_before, "no duplicated panes"); + let (_, sessions) = live.tmux(&["list-sessions", "-F", "#{session_name}"]); + assert_eq!(sessions, SESSION, "re-attached to the same session, no second one"); + let c3 = live.counter(); + assert!(c3 > c2, "the counter never stopped ({c2} -> {c3})"); + eprintln!("[live] reconnected: same session, mapping {mapping_before:?}, 'AWAY-42' hydrated, counter {c3}"); + + // 7. User disconnect keeps the session too. + live.wm.link_disconnect(); + for _ in 0..20 { + live.tick(); + } + assert!(live.tmux(&["has-session", "-t", SESSION]).0, "session kept after :ssh-tmux disconnect"); + eprintln!("[live] :ssh-tmux disconnect -> remote session still alive"); + + // Cleanup of this test's own private server and directory. + let _ = live.tmux(&["kill-server"]); + let _ = std::fs::remove_dir_all(&dir); +} From 3270594b923ed1b6c188aefb7b17696d5e359f8a Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 05:31:24 -0500 Subject: [PATCH 3/3] fix(tmux-link): keep the remote session whatever its tmux.conf says; document link behaviour - After the hydrate the link sets destroy-unattached off on the session it attached to, so a remote tmux.conf with destroy-unattached on cannot make a disconnect destroy the session. - env's "'tmux': No such file or directory" (the text a real remote prints for the link command) is recognised as a missing tmux. - User guide chapter 5: pane-sized remote windows, state-aware hydrate, what survives each kind of disconnect, the -- escape, flow control and the known limits; troubleshooting keepalive text; README; CHANGELOG. Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 12 ++++++++ README.md | 2 +- docs/user-guide/05-tmux-integration.md | 30 +++++++++++++++---- docs/user-guide/07-troubleshooting-and-faq.md | 2 +- src/tmux/decode.rs | 6 ++++ src/tmux/reconnect.rs | 11 ++++++- src/wm/tmux_io.rs | 3 ++ tests/phase51_1_e2e.rs | 4 ++- tests/tmux_link_transcripts.rs | 12 ++++++-- 9 files changed, 71 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93ff4df3..95efbd86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- **Remote tmux link (`:ssh-tmux`) rendering**: the remote tmux client is now as large as the linked pane instead of the whole Bilterm window (full-screen programs and wrapped lines drew in the wrong place); a hydrate restores alternate screen, cursor position and visibility, application cursor keys and mouse modes (re-attaching while `vim` ran left the cursor in the wrong place and stale rows on the primary screen); a UTF-8 character split across two `%output` lines is no longer turned into replacement characters; `%extended-output` is understood. +- **Remote tmux link flooding**: Bilterm asks tmux for `pause-after` flow control and re-captures a paused pane, so a flooding remote pane no longer starves input (Ctrl-C typed in Bilterm now arrives). +- **Remote tmux link input**: a paste larger than 4096 bytes was silently truncated; pastes are now delivered complete and in order. +- **Remote tmux link reconnect**: a stalled connection is noticed in about 15 to 20 seconds instead of about 55 (link keepalive 5 s); backoff has jitter; the UI thread no longer sleeps while collecting the transport's exit code; a missing or too-old remote tmux is named in the failure text. +- `tmux_link_state()` / `:ssh-tmux info` no longer report `idle` while a removed link's transport is still attached. + +### Added +- `:ssh-tmux -- [session]` reaches hosts named `info`, `reconnect`, `disconnect` or `prefill`. +- The link sets `destroy-unattached off` on the session it attaches to, so a remote `tmux.conf` cannot make a disconnect destroy the session. +- `tests/tmux_link_transcripts.rs` (recorded control-mode transcripts through the real parser into a pane) and `tests/tmux_link_live.rs` (real tmux on a private socket, `BILTERM_TMUX_LIVE=1`). + ## [3.3.0] - 2026-11-09 Milestone v3.3.0 "Beyond Herdr": always-on foreground RPC, first-class agent/pane/workspace CLI, semantic agent introspection, worktree swarm DAG runner, Mission Control HUD, and hierarchical `D:(W,S,Y,P)` coordinates (Phases 65–71, 71 phases total). diff --git a/README.md b/README.md index 49473fe3..46e4c1e0 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ - **Agent-to-Agent Messaging & Coordination (`:agent`)**: Direct inter-agent communication across terminal panes without knowing terminal internals. Features stable per-pane agent addressing (`:agent name`), bounded mailboxes (`MAX_INBOX_DEPTH = 64`), and 3 delivery modes: `mailbox` (queued), `nudge` (injected only when idle/waiting; suppressed during password locks or `ALT_SCREEN`), and `file` (0600 payload files for large transfers). Includes hop-count loop guards (`MAX_HOPS = 8`) and rate limits. → *[User Guide Ch. 11](docs/user-guide/11-navigator-sidebar-and-agent-traffic.md)* - **Unified Registry & Fuzzy Finder (`:find`, `Ctrl+Alt+F`)**: Instant fuzzy search across agents, active and saved sessions, and command history (`:find agent|session|history `). Features zero-trust secret masking and redaction to protect tokens and passwords in history logs. → *[User Guide Ch. 12](docs/user-guide/12-find-registry-and-history.md)* - **UI Modernization & 8 Design Profiles (`:appearance`, `:mode`)**: Switch between 8 built-in visual profiles (`Obsidian` dark slate default, `Legacy` Borland Blue, `Midnight`, `Arctic`, `Liquid`, `Amber`, `Synthwave`, `Solarized`) and 3 display modes (`:mode standard|minimal|zen`), with custom profile saving, export, and import. → *[User Guide Ch. 4](docs/user-guide/04-themes-glyphs-and-config.md)* -- **Persistent SSH + Remote tmux Fleet (`:ssh-tmux`)**: Launch remote sessions via `:ssh-tmux [session]` backed by remote `tmux -CC`. Resilient state machine automatically reconnects with exponential backoff on network drop, hydrates scrollback via `capture-pane`, and prevents accidental closure with exit hold banners. → *[User Guide Ch. 5](docs/user-guide/05-tmux-integration.md)* +- **Persistent SSH + Remote tmux Fleet (`:ssh-tmux`)**: Launch remote sessions via `:ssh-tmux [session]` backed by remote `tmux -CC`. Resilient state machine automatically reconnects with exponential backoff on network drop, sizes the remote window to the linked pane, hydrates scrollback, cursor and alternate-screen state via `capture-pane`, leaves the remote session running on every kind of disconnect, and prevents accidental closure with exit hold banners. → *[User Guide Ch. 5](docs/user-guide/05-tmux-integration.md)* - **First-Class Utility Windows (`:fonts`, `:commands`, `:script` / `:studio`)**: Dedicated floating, pinnable (`*`), and dockable workbench windows. Live typography preview in `:fonts`, command palette execution history in `:commands`, and full Script Studio in `:script` / `:studio` featuring a multi-line `.bt` editor, live action recorder (`[● Rec]`), and single-step debugger (`[F10/n Step]`). - **Session Reorganization & Packing Viewer (`:sessions map`, `:window move`)**: Treemap-style container packing visualizer in Braille/UTF-8 box-drawing or native GUI vector rendering with mouse drag-and-drop. Migrate live windows across sessions (`:window move `), perform multi-select bulk operations (`:window bulk move|close|tile|pin`), and safely revert topology with single-step undo (`:window undo`). → *[User Guide Ch. 3](docs/user-guide/03-window-manager-and-z-stack.md)* - **Daemon Supervisor & Multi-Daemon Architecture (`:daemon`)**: Auto-spawned singleton background daemon with stale-lock recovery. Manage named daemons (`--daemon --name `) with cross-daemon discovery, RPC/MCP bridging, and headless HTTP+SSE transport (`--http 127.0.0.1:PORT`). diff --git a/docs/user-guide/05-tmux-integration.md b/docs/user-guide/05-tmux-integration.md index c8cb0d23..edcbd107 100644 --- a/docs/user-guide/05-tmux-integration.md +++ b/docs/user-guide/05-tmux-integration.md @@ -58,8 +58,8 @@ Persistent remote terminal sessions are managed via `:ssh-tmux` (`src/tmux/link. When `:ssh-tmux [session]` is invoked, Bilterm spawns a dedicated PTY child running `ssh -tt` with frozen connection options (`src/remote/mod.rs`): - `-tt`: Allocates a pseudo-terminal on the remote host so `tmux -CC` has an underlying tty for ioctls. -- `-o ServerAliveInterval=15`: Sends keepalive probes every 15 seconds to prevent NAT or stateful firewall timeouts. -- `-o ServerAliveCountMax=3`: Drops dead connections after 3 unanswered keepalives (45 seconds). +- `-o ServerAliveInterval=5`: Sends a keepalive probe every 5 seconds (an interactive `:ssh` pane uses 15). +- `-o ServerAliveCountMax=3`: Drops a dead connection after 3 unanswered keepalives, so a stalled network is noticed in about 15 to 20 seconds and the link starts reconnecting. - `-o TCPKeepAlive=yes`: Enables kernel-level TCP keepalives. - `-o ConnectTimeout=15`: Fails connection attempts quickly when the remote host is unreachable. - `-o ControlMaster=no` and `-o ControlPath=none`: Bypasses OpenSSH socket multiplexing. Every connection and reconnection opens a completely fresh TCP connection, preventing corrupted or stale control sockets from stalling reconnection loops. @@ -131,12 +131,29 @@ This applies both to a first `:ssh-tmux ` that attaches to 1. **First pane immediately**: Bilterm maps the first remote pane to the local pane as soon as the control stream is up. 2. **Pane discovery**: It discovers the remaining panes with `list-panes`. 3. Bilterm waits 300 ms after attach greeting for pane layouts to settle. -4. It sends `refresh-client -C x` followed by `capture-pane -p -e -J -S -2000 -t %N` for each mapped pane. +4. It sends `refresh-client -C x` with the size of the **linked pane** (not the whole Bilterm window), one `display-message` that reports every pane's screen state (alternate screen, cursor position and visibility, application cursor keys, mouse modes), then `capture-pane -p -e -J -S -2000 -t %N` for each mapped pane. A remote window shown in a pane of its own gets that pane's size (`refresh-client -C @:x`, tmux 3.4+). 5. Live `%output` lines are held during capture to prevent interleaved text corruption. -6. Screen lines and scrollback are parsed via `render_capture` and fed into each pane's `VtParser`. +6. Screen lines and scrollback are fed into each pane's `VtParser` together with the reported state, so a re-attach while `vim` or `htop` runs lands on the alternate screen with the cursor where the remote has it. 7. Normal output is released. Stale panes that no longer exist on the remote host are flagged with orphan banners rather than being abruptly removed. 8. **Idle sessions**: Typed commands are delivered right away, even if the remote session has not produced any output yet (for example, an idle shell). +### 4.6.1 What Survives a Disconnect + +The remote tmux session, and everything running in it, is left running in every case below. Bilterm never sends `kill-session`, `kill-server` or `kill-window`, and it sets `destroy-unattached off` on the session it attaches to. + +| Event | Remote session | Bilterm | +|---|---|---| +| ssh process dies, network drops | keeps running | reconnects by itself (backoff 1 s doubling to 30 s, plus up to 25 % jitter, 20 attempts), same session, same panes | +| Network stalls without closing | keeps running | noticed after about 15 to 20 s, then as above | +| `:ssh-tmux disconnect` (`x` on the banner, `Ctrl+X Q`) | keeps running | link stops; `Enter`/`r` or `:ssh-tmux reconnect` re-attaches | +| `:quit`, `:detach`, terminal closed, crash or `kill -9` of Bilterm | keeps running | run `:ssh-tmux [session]` again after the restart | + +`:ssh-tmux [session]` is attach-or-create (`tmux new-session -A -s `): the same host and session name always lead to the same remote session, never to a duplicate. The default session name is `bilterm`. + +While the link is down, typed input is not queued: the pane shows `Link down: input not delivered` and the bytes are dropped, so nothing unexpected runs in a remote shell after a reconnect. Pastes of any practical size (up to 1 MiB per paste) are sent complete and in order. + +A host that is literally named `info`, `reconnect`, `disconnect` or `prefill` is reached with `--`: `:ssh-tmux -- info [session]`. + ### 4.7 Single Link Guard and Fresh TCP Policy - **One Link at a Time**: Bilterm currently supports one active remote `tmux -CC` link. Attempting to run `:ssh-tmux` while a link is `Connecting`, `Connected`, or in `Backoff` returns an error: `"a tmux link to / is already active; run :ssh-tmux disconnect first"`. @@ -152,7 +169,10 @@ If your remote session terminates immediately upon disconnect, verify your remot ### 4.9 Known Limits - **`csh` / `tcsh` shells**: The remote wrapper relies on POSIX `sh`, `bash`, or `zsh` login shell syntax. C-shell variants are not supported. -- **Raw-mode capture**: `capture-pane` captures terminal grid text and colors, but cannot capture internal TUI cursor states or alternate-screen state machines of running programs (`vim`, `htop`). +- **Restored state**: A hydrate restores text, colours, alternate screen, cursor position and visibility, application cursor keys and mouse reporting. Other modes (bracketed paste, keypad mode, scroll regions, character sets) are not reported by tmux and come back when the program next sets them. +- **Several panes in one remote window**: The remote client size follows the linked primary pane. Panes that share one remote window keep the sizes tmux gives them, which can differ from the local pane sizes. +- **Detach and re-attach of Bilterm itself**: `:detach` / `bilterm attach` and a restart do not restore the link. The remote session keeps running; run `:ssh-tmux [session]` again to re-attach to it. +- **Floods**: When a remote pane prints faster than Bilterm can draw, tmux pauses that pane and Bilterm re-captures its screen and continues it (`pause-after`, tmux 3.2+). Lines that scrolled by during a pause are in tmux's history, not in Bilterm's scrollback. - **Multi-link**: Only one remote `tmux -CC` bridge is active at a time in this version. - **Window names**: Remote window names are hydrated when `%window-renamed` events arrive (gap G10 from Chapter 12). diff --git a/docs/user-guide/07-troubleshooting-and-faq.md b/docs/user-guide/07-troubleshooting-and-faq.md index f8fd5c93..46794dcd 100644 --- a/docs/user-guide/07-troubleshooting-and-faq.md +++ b/docs/user-guide/07-troubleshooting-and-faq.md @@ -129,7 +129,7 @@ ``` - If `tcpkeepalive 0` or sleep triggers, TCP connections are dropped when the display sleeps. Enable network keepalives with `sudo pmset -a tcpkeepalive 1`, or prevent sleep during long jobs using `caffeinate -s`. 6. **SSH Keepalives & Network Timeouts**: - - Bilterm automatically applies `-o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o TCPKeepAlive=yes -o ConnectTimeout=15` to both `:ssh` and `:ssh-tmux` connections. + - Bilterm automatically applies `-o ServerAliveCountMax=3 -o TCPKeepAlive=yes -o ConnectTimeout=15` to both `:ssh` and `:ssh-tmux` connections, with `-o ServerAliveInterval=15` for `:ssh` panes and `-o ServerAliveInterval=5` for the `:ssh-tmux` link (a stalled link is noticed in about 15 to 20 seconds). - If stateful NAT gateways or firewalls drop idle connections faster than 15 seconds, adjust your local `~/.ssh/config` or remote `sshd_config` `ClientAliveInterval`. 7. **Restored Workspace Shows `(not connected)`**: - When restoring a saved workspace session, Bilterm honestly labels previously remote panes as `(not connected)` if they are running a local shell rather than a live remote transport. diff --git a/src/tmux/decode.rs b/src/tmux/decode.rs index 7eae1d8a..9b1c5513 100644 --- a/src/tmux/decode.rs +++ b/src/tmux/decode.rs @@ -22,6 +22,12 @@ pub fn pause_after_command() -> String { format!("refresh-client -f pause-after={PAUSE_AFTER_SECS}\n") } +/// `set-option destroy-unattached off` for the attached session: a remote `~/.tmux.conf` with +/// `destroy-unattached on` would otherwise destroy the session the moment the link drops. +pub fn keep_session_command() -> String { + "set-option destroy-unattached off\n".to_string() +} + /// `refresh-client -A '%N:continue'`: resumes a paused pane. The argument must be quoted: tmux's /// command parser rejects a bare leading `%` ("parse error: syntax error"). pub fn continue_pane_command(tmux_pane: usize) -> String { diff --git a/src/tmux/reconnect.rs b/src/tmux/reconnect.rs index 24adc1c2..b4bf5ce0 100644 --- a/src/tmux/reconnect.rs +++ b/src/tmux/reconnect.rs @@ -58,7 +58,16 @@ const FATAL_EVIDENCE: [&str; 5] = [ ]; /// Evidence that the remote has no usable tmux (shown instead of a bare exit code). -const NO_TMUX_EVIDENCE: [&str; 3] = ["tmux: command not found", "tmux: not found", "exec: tmux"]; +/// The link runs `exec env PATH=... tmux`, so the usual text is env's +/// `env: 'tmux': No such file or directory` (GNU quotes it, BSD does not). +const NO_TMUX_EVIDENCE: [&str; 6] = [ + "'tmux': no such file", + "\u{2018}tmux\u{2019}: no such file", + "env: tmux: no such file", + "tmux: command not found", + "tmux: not found", + "exec: tmux", +]; const OLD_TMUX_EVIDENCE: [&str; 3] = ["unknown option -- c", "tmux: illegal option", "usage: tmux"]; /// A plain-language cause when the diagnostic tail shows a known setup problem. diff --git a/src/wm/tmux_io.rs b/src/wm/tmux_io.rs index 20b36772..35674e55 100644 --- a/src/wm/tmux_io.rs +++ b/src/wm/tmux_io.rs @@ -498,6 +498,9 @@ impl WindowManager { // `%error`, which is ignored). self.tmux_bridge .queue_command(crate::tmux::decode::pause_after_command(), ReplyKind::Ignore); + // The session must outlive the link whatever the remote tmux.conf says. + self.tmux_bridge + .queue_command(crate::tmux::decode::keep_session_command(), ReplyKind::Ignore); } } } diff --git a/tests/phase51_1_e2e.rs b/tests/phase51_1_e2e.rs index 643ccd4b..5ebde584 100644 --- a/tests/phase51_1_e2e.rs +++ b/tests/phase51_1_e2e.rs @@ -257,11 +257,13 @@ fn drop_reconnect_hydrate_scenario() { // 5. `ls` typed in pane for %3 sent as `send-keys -t %3 -H 6c 73 0a` wm.pane_mut(p3).unwrap().submit_command("ls").unwrap(); wm.tick_tmux_link(750); - // Flow control is requested once the hydrate is done (it precedes the first keystrokes). + // Flow control and session keeping are requested once the hydrate is done (before the + // first keystrokes). assert_eq!( drain_cmds(&mut h1.cmd_rx), vec![ "refresh-client -f pause-after=2\n".to_string(), + "set-option destroy-unattached off\n".to_string(), "send-keys -t %3 -H 6c 73 0a\n".to_string(), ] ); diff --git a/tests/tmux_link_transcripts.rs b/tests/tmux_link_transcripts.rs index a3c1ef4f..8155692e 100644 --- a/tests/tmux_link_transcripts.rs +++ b/tests/tmux_link_transcripts.rs @@ -127,6 +127,11 @@ fn utf8_split_across_output_lines_renders_intact() { fn extended_output_is_rendered_and_a_pause_recaptures_then_continues() { let mut rig = Rig::connect(); assert_eq!(rig.sent("refresh-client -f pause-after=").len(), 1, "flow control is requested once per attach"); + assert_eq!( + rig.sent("set-option destroy-unattached off").len(), + 1, + "a remote tmux.conf with destroy-unattached on must not destroy the session on disconnect" + ); rig.feed(b"%extended-output %0 1500 : STALE-LINE\\015\\012\r\n"); assert!(rig.text().contains("STALE-LINE")); @@ -310,8 +315,11 @@ fn missing_or_old_tmux_is_named_in_the_failure_text() { at_ms: 0, class: ExitClass::Fatal, }; - let missing = disconnect_text(&down("bash: line 1: exec: tmux: not found")); - assert!(missing.contains("tmux is not installed on the remote host"), "{missing}"); + // The exact line a real remote printed (recorded over ssh), plus the shell variants. + for line in ["env: 'tmux': No such file or directory", "env: tmux: No such file or directory", "bash: line 1: exec: tmux: not found"] { + let missing = disconnect_text(&down(line)); + assert!(missing.contains("tmux is not installed on the remote host"), "{line} -> {missing}"); + } let old = disconnect_text(&down("tmux: unknown option -- C")); assert!(old.contains("too old for control mode"), "{old}"); // Authentication errors keep ssh's own line (it already says what happened).