Every attached client pushed its own dimensions straight to `tmux resize-window`, so two clients of different sizes resized the shared window past each other on every report — 198 resizes in 90 seconds on a four-session daemon. Each flip repaints a full-screen agent at a new height and orphans the rows it drew at the old one, leaving a stale input box the operator types into and reads as a stuck pane. The window now goes to the smallest attached client per axis, tmux's own rule for multiple clients, and only when that size actually changes. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
187 lines
6.3 KiB
Rust
187 lines
6.3 KiB
Rust
//! What size the shared tmux window should be, given every client watching it.
|
|
//!
|
|
//! A tmux window has one size, but a session can have several clients attached
|
|
//! at once — two browser panes, a browser and the TUI. Each reports the size
|
|
//! *it* can display, and applying whichever arrived last makes the window
|
|
//! flip-flop. Every flip forces a full-screen application (Claude Code, vim) to
|
|
//! repaint at a new height, and the rows it drew at the old height are never
|
|
//! cleared: the operator ends up looking at a stale input box, types into a
|
|
//! box that is not where the screen says it is, and concludes the pane is
|
|
//! stuck.
|
|
//!
|
|
//! So the window is sized to the smallest attached client on each axis — the
|
|
//! same rule tmux uses for its own multiple clients, and the only size every
|
|
//! client can render without truncation. The size last handed to tmux is
|
|
//! remembered, so a report that changes nothing costs no `tmux` fork at all.
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use uuid::Uuid;
|
|
|
|
/// One attached client within a session. Allocated by
|
|
/// [`crate::session::SessionManager`] and opaque to everything else.
|
|
pub type ViewportId = u64;
|
|
|
|
/// Per-session client sizes, and what tmux was last told.
|
|
#[derive(Debug, Default)]
|
|
pub struct Viewports {
|
|
clients: HashMap<Uuid, HashMap<ViewportId, (u16, u16)>>,
|
|
applied: HashMap<Uuid, (u16, u16)>,
|
|
}
|
|
|
|
impl Viewports {
|
|
pub fn new() -> Self {
|
|
Self::default()
|
|
}
|
|
|
|
/// Record what `viewport` can display. Returns the size tmux must be set
|
|
/// to, or `None` when the window already has it.
|
|
pub fn set(
|
|
&mut self,
|
|
session: Uuid,
|
|
viewport: ViewportId,
|
|
cols: u16,
|
|
rows: u16,
|
|
) -> Option<(u16, u16)> {
|
|
self.clients
|
|
.entry(session)
|
|
.or_default()
|
|
.insert(viewport, (cols, rows));
|
|
self.settle(session)
|
|
}
|
|
|
|
/// Drop a client that has detached. Returns the size tmux must be set to
|
|
/// now that it no longer constrains the window.
|
|
///
|
|
/// The last client leaving settles nothing: the window keeps the size it
|
|
/// has rather than snapping to some remembered default no one is watching.
|
|
pub fn release(&mut self, session: Uuid, viewport: ViewportId) -> Option<(u16, u16)> {
|
|
let clients = self.clients.get_mut(&session)?;
|
|
clients.remove(&viewport);
|
|
if clients.is_empty() {
|
|
self.clients.remove(&session);
|
|
self.applied.remove(&session);
|
|
return None;
|
|
}
|
|
self.settle(session)
|
|
}
|
|
|
|
/// The smallest size every attached client can render, but only when it
|
|
/// differs from what tmux was last told.
|
|
///
|
|
/// A zero on either axis is a client that cannot display anything yet — a
|
|
/// pane still laying out, a hidden tab — and must not drag the shared
|
|
/// window down with it, so it is held but left out of the arbitration.
|
|
fn settle(&mut self, session: Uuid) -> Option<(u16, u16)> {
|
|
let clients = self.clients.get(&session)?;
|
|
let mut cols = u16::MAX;
|
|
let mut rows = u16::MAX;
|
|
for &(c, r) in clients.values() {
|
|
if c == 0 || r == 0 {
|
|
continue;
|
|
}
|
|
cols = cols.min(c);
|
|
rows = rows.min(r);
|
|
}
|
|
if cols == u16::MAX || rows == u16::MAX {
|
|
return None;
|
|
}
|
|
let target = (cols, rows);
|
|
if self.applied.get(&session) == Some(&target) {
|
|
return None;
|
|
}
|
|
self.applied.insert(session, target);
|
|
Some(target)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn the_first_report_settles_the_window() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
assert_eq!(v.set(s, 1, 80, 24), Some((80, 24)));
|
|
}
|
|
|
|
#[test]
|
|
fn a_report_that_changes_nothing_costs_no_resize() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
v.set(s, 1, 80, 24);
|
|
assert_eq!(v.set(s, 1, 80, 24), None);
|
|
assert_eq!(v.set(s, 1, 80, 24), None);
|
|
}
|
|
|
|
/// The bug this module exists for: two clients of different sizes used to
|
|
/// resize the window past each other on every report.
|
|
#[test]
|
|
fn two_clients_settle_on_the_smaller_instead_of_fighting() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
assert_eq!(v.set(s, 1, 53, 46), Some((53, 46)));
|
|
assert_eq!(v.set(s, 2, 53, 20), Some((53, 20)));
|
|
// Whatever order they re-report in, the answer stays the smaller one.
|
|
assert_eq!(v.set(s, 1, 53, 46), None);
|
|
assert_eq!(v.set(s, 2, 53, 20), None);
|
|
assert_eq!(v.set(s, 1, 53, 46), None);
|
|
}
|
|
|
|
#[test]
|
|
fn each_axis_is_arbitrated_on_its_own() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
v.set(s, 1, 200, 20);
|
|
assert_eq!(v.set(s, 2, 53, 46), Some((53, 20)));
|
|
}
|
|
|
|
#[test]
|
|
fn a_zero_sized_client_does_not_shrink_the_window() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
assert_eq!(v.set(s, 1, 80, 24), Some((80, 24)));
|
|
assert_eq!(v.set(s, 2, 0, 0), None);
|
|
assert_eq!(v.set(s, 2, 0, 30), None);
|
|
}
|
|
|
|
#[test]
|
|
fn releasing_the_small_client_gives_the_window_back() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
v.set(s, 1, 80, 46);
|
|
v.set(s, 2, 80, 20);
|
|
assert_eq!(v.release(s, 2), Some((80, 46)));
|
|
}
|
|
|
|
#[test]
|
|
fn releasing_the_last_client_settles_nothing() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
v.set(s, 1, 80, 24);
|
|
assert_eq!(v.release(s, 1), None);
|
|
// ...and the session is forgotten, so a fresh attach settles again.
|
|
assert_eq!(v.set(s, 2, 80, 24), Some((80, 24)));
|
|
}
|
|
|
|
#[test]
|
|
fn releasing_an_unknown_viewport_is_harmless() {
|
|
let mut v = Viewports::new();
|
|
let s = Uuid::new_v4();
|
|
assert_eq!(v.release(s, 7), None);
|
|
v.set(s, 1, 80, 24);
|
|
assert_eq!(v.release(s, 7), None);
|
|
}
|
|
|
|
#[test]
|
|
fn sessions_do_not_arbitrate_against_each_other() {
|
|
let mut v = Viewports::new();
|
|
let a = Uuid::new_v4();
|
|
let b = Uuid::new_v4();
|
|
assert_eq!(v.set(a, 1, 80, 24), Some((80, 24)));
|
|
assert_eq!(v.set(b, 2, 53, 20), Some((53, 20)));
|
|
assert_eq!(v.set(a, 1, 80, 24), None);
|
|
}
|
|
}
|