A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
use crate::client::{ClientId, ClientInfo};
use crate::pane::{CachePolicy, Pane, PaneId};
use crate::ssh_agent::AgentProxy;
use crate::tab::{SplitRequest, Tab, TabId};
use crate::window::{Window, WindowId, WindowUiSurfaceId};
use anyhow::{anyhow, Context, Error};
use config::keyassignment::SpawnTabDomain;
use config::{configuration, ExitBehavior, GuiPosition};
use domain::{Domain, DomainId, DomainState, SplitSource};
#[cfg(unix)]
use filedescriptor::{poll, pollfd, AsRawSocketDescriptor, FileDescriptor, POLLIN};
use log::error;
use metrics::histogram;
use parking_lot::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
use percent_encoding::percent_decode_str;
use portable_pty::{CommandBuilder, ExitStatus, PtySize};
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::io::{Read, Write};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Weak};
use std::thread;
use std::time::{Duration, Instant, SystemTime};
use termwiz::escape::csi::{DecPrivateMode, DecPrivateModeCode, Device, Mode};
use termwiz::escape::parser::ParsedAction;
use termwiz::escape::{Action, CSI};
use thiserror::*;
use wezterm_term::color::ColorPalette;
use wezterm_term::{
Clipboard, ClipboardSelection, DownloadHandler, TerminalConfiguration, TerminalSize,
};
pub mod activity;
pub mod agent_status;
pub mod client;
pub mod command_spec;
pub mod connect;
pub mod connui;
pub mod default_prog;
pub mod domain;
pub mod foreground_program;
pub mod geometrytrace;
pub mod kept_output;
pub mod localpane;
mod output_buffer;
pub mod pane;
mod parse_watchdog;
pub mod renderable;
pub mod session_server;
pub mod shell_integration;
pub mod ssh;
pub mod ssh_agent;
pub mod tab;
pub mod termwiztermtab;
pub mod tmux;
pub mod tmux_commands;
mod tmux_pty;
pub mod window;
/// The Mux is a process-wide singleton, and tests that install one assert on
/// its global state: they take this and run one at a time.
#[cfg(test)]
pub(crate) static GLOBAL_MUX_TESTS: parking_lot::Mutex<()> = parking_lot::Mutex::new(());
pub struct ConnectSuspension {
mux: Weak<Mux>,
resume: bool,
}
impl ConnectSuspension {
pub fn keep(mut self) {
self.resume = false;
}
}
impl Drop for ConnectSuspension {
fn drop(&mut self) {
if self.resume {
if let Some(mux) = self.mux.upgrade() {
mux.connect_suspended.store(false, Ordering::Release);
}
}
}
}
use crate::activity::Activity;
pub const DEFAULT_WORKSPACE: &str = "default";
#[derive(Clone, Debug)]
pub enum MuxNotification {
PaneOutput(PaneId),
PaneAdded(PaneId),
PaneRemoved(PaneId),
/// The program the pane started has ended. Raised before the pane is
/// removed, when it is removed because of it.
PaneExited {
pane_id: PaneId,
exit: thinkterm_proto::PaneExit,
},
/// The mux that owns this pane re-classified its agent status; read
/// the fresh value via `Pane::agent_status`.
AgentStatusChanged(PaneId),
/// The program leading this pane's terminal changed, as seen by the mux
/// that owns it; read the fresh value via `Pane::foreground_program`.
ForegroundProgramChanged(PaneId),
WindowCreated(WindowId),
WindowRemoved(WindowId),
WindowInvalidated(WindowId),
WindowWorkspaceChanged(WindowId),
ActiveWorkspaceChanged(Arc<ClientId>),
Alert {
pane_id: PaneId,
alert: wezterm_term::Alert,
},
Empty,
AssignClipboard {
pane_id: PaneId,
selection: ClipboardSelection,
clipboard: Option<String>,
},
SaveToDownloads {
name: Option<String>,
data: Arc<Vec<u8>>,
},
TabAddedToWindow {
tab_id: TabId,
window_id: WindowId,
},
PaneFocused(PaneId),
TabResized(TabId),
TabTitleChanged {
tab_id: TabId,
title: String,
},
/// A pane was labelled, relabelled or had its label taken away.
PaneLabelChanged {
pane_id: PaneId,
label: Option<String>,
/// Set on a mirror without telling the server that owns the pane:
/// the label came from it, or is told to it another way.
from_server: bool,
},
WindowTitleChanged {
window_id: WindowId,
title: String,
},
/// A client began showing panes: it set a viewport for the first time
/// since it connected. A command that only asks things never does.
ClientJoined(Arc<ClientId>),
/// A client that had shown panes disconnected.
ClientLeft(Arc<ClientId>),
WorkspaceRenamed {
old_workspace: String,
new_workspace: String,
},
/// The mux server's ThinkTerm sidebar tree (Space/Project/Thread) changed;
/// every connection re-sends it. Raised only on the server side — a GUI
/// mux never owns a tree of its own.
ThinkTermTreeChanged,
/// The authoritative server tree or live mux topology changed; thin
/// frontends should refresh their read-only session snapshot.
ThinkTermSessionChanged,
/// The renderer allowed to drive one tab's shared PTY geometry changed,
/// or that owner published a new canonical viewport.
FrontendLeaseChanged(FrontendViewportState),
/// The connection-wide A/B mode or the exclusive handoff owner changed.
FrontendAccessChanged(FrontendAccessState),
/// The server's configuration reloaded and its resolved colour scheme may
/// have changed; every connection re-sends its default palette.
DefaultPaletteChanged,
}
static SUB_ID: AtomicUsize = AtomicUsize::new(0);
static PALETTE_SESSION_ID: AtomicUsize = AtomicUsize::new(1);
static CLIENT_REGISTRATION_ID: AtomicUsize = AtomicUsize::new(1);
static SERVER_ID_SEQUENCE: AtomicUsize = AtomicUsize::new(1);
fn new_runtime_server_id() -> String {
let epoch_nanos = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_nanos();
let host = hostname::get()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_else(|_| "localhost".to_string());
format!(
"{host}:{}:{epoch_nanos}:{}",
std::process::id(),
SERVER_ID_SEQUENCE.fetch_add(1, Ordering::Relaxed)
)
}
/// Identifies one live transport registration for a `ClientId`. A reconnect
/// intentionally reuses `ClientId`, so disconnect cleanup and queued viewport
/// work also carry this generation and cannot tear down the replacement.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ClientRegistrationId(usize);
impl ClientRegistrationId {
fn new() -> Self {
Self(CLIENT_REGISTRATION_ID.fetch_add(1, Ordering::Relaxed))
}
}
/// A single mux-server transport connection. `ClientId` identifies the GUI
/// process and can be reused when that process reconnects, so it is not
/// sufficient to reject work queued by an older, disconnected transport.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct PaletteSessionId(usize);
#[derive(Clone, Debug, PartialEq)]
pub struct PaletteSelectionChange {
pub pane_id: PaneId,
pub palette: Option<ColorPalette>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FrontendPaneViewport {
pub pane_id: PaneId,
pub size: wezterm_term::TerminalSize,
pub frame: wezterm_term::TerminalSize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FrontendViewport {
CellGrid {
size: wezterm_term::TerminalSize,
},
Native {
size: wezterm_term::TerminalSize,
panes: Vec<FrontendPaneViewport>,
},
}
impl FrontendViewport {
pub fn size(&self) -> wezterm_term::TerminalSize {
match self {
Self::CellGrid { size } | Self::Native { size, .. } => *size,
}
}
}
/// What the renderer holding a tab's viewport is currently looking at.
///
/// Size alone makes two attached devices the same shape; it does not make them
/// the same view. Without this a phone and a desktop on one tab sit at
/// different points in the same scrollback, which reads as two sessions that
/// happen to share a name.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FrontendView {
/// Lines above the bottom of each pane's scrollback. A pane that is absent
/// is following its output.
pub scroll: Vec<(PaneId, u32)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FrontendViewportState {
pub tab_id: TabId,
/// Per-tab layout owner used by `TmuxLatest` mode.
pub owner: Option<ClientId>,
pub canonical_size: wezterm_term::TerminalSize,
/// What the owner is looking at, when the owner is a renderer that says.
/// `None` means nobody has offered one — followers keep their own view
/// rather than being pulled somewhere arbitrary.
pub view: Option<FrontendView>,
pub generation: u64,
pub access: FrontendAccessState,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FrontendAccessMode {
TmuxLatest,
#[default]
Handoff,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FrontendAccessState {
pub mode: FrontendAccessMode,
pub owner: Option<ClientId>,
pub generation: u64,
}
#[derive(Default)]
struct TabFrontendLease {
owner: Option<ClientId>,
ownership_generation: u64,
/// Last geometry advertised by each renderer. Network claims always carry
/// their geometry and never trust this cache; it remains useful for the
/// in-process GUI, whose Pane input reaches the mux without a wire PDU.
viewports: HashMap<ClientId, FrontendViewport>,
/// Written by the owner alone, and dropped the moment the lease moves, so
/// it can never describe a renderer that is no longer driving.
view: Option<FrontendView>,
generation: u64,
}
impl TabFrontendLease {
fn set_owner(&mut self, owner: Option<ClientId>) {
if self.owner != owner {
self.ownership_generation = self.ownership_generation.saturating_add(1);
self.owner = owner;
}
}
}
/// True when `me` is the only *live* client that has advertised geometry for
/// any tab. A sole renderer cannot be fighting anyone over the lease, so an
/// unclaimed lease is effectively its own. Two or more renderers must take it
/// explicitly — see `unregister_client`, which deliberately elects no
/// successor.
///
/// Handoff's owner is global, so the scan is global: a phone rendering tab B
/// must still stop the desktop from silently assuming tab A. Filtering by
/// `live` registrations guards against a viewport left behind by a client
/// whose transport died without an orderly unregister.
/// The same screen, as far as a lease is concerned: one user on one host.
/// The process behind it changes on every restart; the device does not.
fn same_device(a: &ClientId, b: &ClientId) -> bool {
a.hostname == b.hostname && a.username == b.username
}
fn is_sole_live_renderer(
tabs: &HashMap<TabId, TabFrontendLease>,
live: &HashSet<ClientId>,
me: &ClientId,
) -> bool {
!tabs.values().any(|state| {
state
.viewports
.keys()
.any(|client| client != me && live.contains(client))
})
}
/// The size the frontend driving `tab_id` last gave `pane_id` over the whole
/// tab, `root` as it is now: a page of a zoomed stack, which that frontend
/// sizes before it is shown. None when nobody drives the tab, the driver
/// reports cells rather than panes, or its layout no longer fits the tab.
fn driving_frontend_size_over_root(
lease: &FrontendLeaseState,
tab_id: TabId,
pane_id: PaneId,
root: wezterm_term::TerminalSize,
) -> Option<wezterm_term::TerminalSize> {
let state = lease.tabs.get(&tab_id)?;
let driver = match lease.access_mode {
FrontendAccessMode::TmuxLatest => state.owner.as_ref(),
FrontendAccessMode::Handoff => lease.handoff_owner.as_ref(),
}?;
let FrontendViewport::Native { panes, .. } = state.viewports.get(driver)? else {
return None;
};
panes
.iter()
.find(|pane| pane.pane_id == pane_id)
.filter(|pane| pane.frame.cols == root.cols && pane.frame.rows == root.rows)
.map(|pane| pane.size)
}
struct FrontendLeaseState {
tabs: HashMap<TabId, TabFrontendLease>,
access_mode: FrontendAccessMode,
handoff_owner: Option<ClientId>,
ownership_generation: u64,
/// Distinguishes initial server startup (first renderer auto-owns) from an
/// owner disconnect (everyone stays blocked until an explicit takeover).
handoff_ever_owned: bool,
/// The owner that disconnected, until someone owns again. The lease
/// belongs to a *device*, not a process: when the same device comes
/// back (the GUI restarted, a laptop's ssh re-established), it is not a
/// second screen to hand off to but the one that was already driving,
/// and it takes its lease back on its first viewport without a click.
/// Another device still has to take over explicitly.
handoff_last_owner: Option<ClientId>,
/// Clients that have ever advertised or claimed a viewport during their
/// current registration. Handoff's gate exists to stop two *screens* from
/// fighting over one terminal; a client that never renders -- `thinkterm
/// cli send-text` and friends -- is not a screen, so it is exempt from
/// that gate rather than being told the terminal "is being operated on
/// another device" by a session it can never see. Sticky, unlike the
/// per-tab `viewports` maps (which are pruned as tabs close): once a
/// connection has rendered anything it is a frontend for its whole life.
ever_rendered: HashSet<ClientId>,
access_initialized: bool,
access_generation: u64,
next_generation: u64,
}
impl Default for FrontendLeaseState {
fn default() -> Self {
Self {
tabs: HashMap::new(),
access_mode: FrontendAccessMode::Handoff,
handoff_owner: None,
ownership_generation: 0,
handoff_ever_owned: false,
handoff_last_owner: None,
ever_rendered: HashSet::new(),
access_initialized: false,
access_generation: 0,
next_generation: 1,
}
}
}
impl FrontendLeaseState {
fn set_handoff_owner(&mut self, owner: Option<ClientId>) {
if self.handoff_owner != owner {
self.ownership_generation = self.ownership_generation.saturating_add(1);
self.handoff_owner = owner;
}
}
fn set_access_mode(&mut self, mode: FrontendAccessMode) {
if self.access_mode != mode {
self.ownership_generation = self.ownership_generation.saturating_add(1);
self.access_mode = mode;
}
}
}
/// Server-side palette advice is scoped to the client that supplied it.
/// Merely attaching records advice; focus or real input activates it for OSC
/// queries. Application palette overrides are tracked by the terminal and are
/// intentionally not represented here.
#[derive(Default)]
struct PaletteAdvisoryState {
sessions: HashMap<PaletteSessionId, ClientId>,
advised: HashMap<(PaletteSessionId, PaneId), ColorPalette>,
activity: HashMap<(PaletteSessionId, PaneId), u64>,
owners: HashMap<PaneId, PaletteSessionId>,
next_activity: u64,
}
impl PaletteAdvisoryState {
fn register(&mut self, client_id: &ClientId) -> PaletteSessionId {
let session_id = PaletteSessionId(PALETTE_SESSION_ID.fetch_add(1, Ordering::Relaxed));
self.sessions.insert(session_id, client_id.clone());
session_id
}
fn advise(
&mut self,
session_id: PaletteSessionId,
pane_id: PaneId,
palette: ColorPalette,
) -> Option<ColorPalette> {
if !self.sessions.contains_key(&session_id) {
return None;
}
self.advised.insert((session_id, pane_id), palette.clone());
(self.owners.get(&pane_id) == Some(&session_id)).then_some(palette)
}
fn activate(&mut self, session_id: PaletteSessionId, pane_id: PaneId) -> Option<ColorPalette> {
if !self.sessions.contains_key(&session_id) {
return None;
}
let key = (session_id, pane_id);
let palette = self.advised.get(&key)?.clone();
self.next_activity = self.next_activity.saturating_add(1).max(1);
self.activity.insert(key, self.next_activity);
if self.owners.get(&pane_id) == Some(&session_id) {
return None;
}
self.owners.insert(pane_id, session_id);
Some(palette)
}
fn remove_session(&mut self, session_id: PaletteSessionId) -> Vec<PaletteSelectionChange> {
self.sessions.remove(&session_id);
self.advised.retain(|(id, _), _| *id != session_id);
self.activity.retain(|(id, _), _| *id != session_id);
let affected: Vec<PaneId> = self
.owners
.iter()
.filter_map(|(pane_id, owner)| (*owner == session_id).then_some(*pane_id))
.collect();
let mut changes = Vec::with_capacity(affected.len());
for pane_id in affected {
let replacement = self
.activity
.iter()
.filter(|((candidate_session, candidate_pane), _)| {
*candidate_pane == pane_id
&& self.sessions.contains_key(candidate_session)
&& self.advised.contains_key(&(*candidate_session, pane_id))
})
.max_by_key(|(_, activity)| **activity)
.map(|((id, _), _)| *id);
let palette = match replacement {
Some(id) => {
self.owners.insert(pane_id, id);
self.advised.get(&(id, pane_id)).cloned()
}
None => {
self.owners.remove(&pane_id);
None
}
};
changes.push(PaletteSelectionChange { pane_id, palette });
}
changes
}
fn deactivate_session(&mut self, session_id: PaletteSessionId) -> bool {
self.sessions.remove(&session_id).is_some()
}
fn remove_pane(&mut self, pane_id: PaneId) {
self.advised.retain(|(_, pane), _| *pane != pane_id);
self.activity.retain(|(_, pane), _| *pane != pane_id);
self.owners.remove(&pane_id);
}
}
pub struct Mux {
runtime_server_id: String,
connect_service: Mutex<Option<Arc<connect::service::Service>>>,
connect_suspended: AtomicBool,
tabs: RwLock<HashMap<TabId, Arc<Tab>>>,
panes: RwLock<HashMap<PaneId, Arc<dyn Pane>>>,
windows: RwLock<HashMap<WindowId, Window>>,
default_domain: RwLock<Option<Arc<dyn Domain>>>,
domains: RwLock<HashMap<DomainId, Arc<dyn Domain>>>,
domains_by_name: RwLock<HashMap<String, Arc<dyn Domain>>>,
subscribers: RwLock<HashMap<usize, Box<dyn Fn(MuxNotification) -> bool + Send + Sync>>>,
banner: RwLock<Option<String>>,
clients: RwLock<HashMap<ClientId, ClientInfo>>,
client_registrations: RwLock<HashMap<ClientId, ClientRegistrationId>>,
frontend_lease: Mutex<FrontendLeaseState>,
tab_resize_notifications: Mutex<TabResizeNotificationState>,
pane_output_generations: Mutex<PaneOutputGenerationState>,
#[cfg(test)]
frontend_geometry_failures: Mutex<std::collections::VecDeque<bool>>,
palette_advisories: Mutex<PaletteAdvisoryState>,
identity: RwLock<Option<Arc<ClientId>>>,
num_panes_by_workspace: RwLock<HashMap<String, usize>>,
main_thread_id: std::thread::ThreadId,
agent: Option<AgentProxy>,
/// How the last few panes' programs ended, so a `wait` that arrives
/// after its pane closed still learns the status. Oldest first, at most
/// `RECENT_PANE_EXITS` long, and each kept for `RECENT_PANE_EXIT_TTL`.
recent_pane_exits:
Mutex<std::collections::VecDeque<(PaneId, thinkterm_proto::PaneExit, Instant)>>,
/// Panes whose exit is recorded but not yet announced. The announcement
/// waits for the main thread, and goes out at once if the pane is
/// removed first, so that `PaneExited` always precedes `PaneRemoved`.
/// Payloads stay here until announced even if exit history evicts them.
unannounced_pane_exits: Mutex<HashMap<PaneId, thinkterm_proto::PaneExit>>,
/// The labels panes were given, by `cli set-pane-label` or by renaming
/// a pane tab. One per live pane at most: removing a pane drops its own.
pane_labels: RwLock<HashMap<PaneId, String>>,
}
/// How many characters a pane label keeps; the rest are cut.
const PANE_LABEL_MAX_CHARS: usize = 128;
/// `text` as a pane label: without control characters, since labels are
/// printed to terminals, trimmed and capped -- and trimmed again after the
/// cut, as `--pane` trims the name it is given. `None` when nothing is
/// left.
pub fn normalized_pane_label(text: &str) -> Option<String> {
let printable: String = text.chars().filter(|c| !c.is_control()).collect();
let capped: String = printable
.trim()
.chars()
.take(PANE_LABEL_MAX_CHARS)
.collect();
let label = capped.trim_end();
(!label.is_empty()).then(|| label.to_string())
}
/// How many ended panes `Mux::recent_pane_exit` remembers, and for how long.
const RECENT_PANE_EXITS: usize = 128;
const RECENT_PANE_EXIT_TTL: Duration = Duration::from_secs(10 * 60);
#[derive(Default)]
struct TabResizeNotificationState {
depth: HashMap<TabId, usize>,
pending: HashSet<TabId>,
aborted: HashSet<TabId>,
}
#[derive(Default)]
struct PaneOutputGenerationState {
generations: HashMap<PaneId, u64>,
}
impl PaneOutputGenerationState {
fn note_output(&mut self, pane_id: PaneId) -> u64 {
let generation = self.generations.entry(pane_id).or_default();
*generation = generation.wrapping_add(1).max(1);
*generation
}
fn generation(&self, pane_id: PaneId) -> u64 {
self.generations.get(&pane_id).copied().unwrap_or(0)
}
fn remove(&mut self, pane_id: PaneId) {
self.generations.remove(&pane_id);
}
}
impl TabResizeNotificationState {
fn begin(&mut self, tab_id: TabId) {
if self.depth.get(&tab_id).copied().unwrap_or_default() == 0 {
self.aborted.remove(&tab_id);
}
*self.depth.entry(tab_id).or_default() += 1;
}
fn defer(&mut self, tab_id: TabId) -> bool {
if self.depth.get(&tab_id).copied().unwrap_or_default() == 0 {
return false;
}
self.pending.insert(tab_id);
true
}
fn finish(&mut self, tab_id: TabId, commit: bool) -> bool {
if !commit {
self.aborted.insert(tab_id);
}
let Some(depth) = self.depth.get_mut(&tab_id) else {
return false;
};
*depth -= 1;
if *depth != 0 {
return false;
}
self.depth.remove(&tab_id);
let pending = self.pending.remove(&tab_id);
let aborted = self.aborted.remove(&tab_id);
commit && !aborted && pending
}
}
struct TabGeometryTransaction<'a> {
mux: &'a Mux,
tab_id: TabId,
commit: bool,
}
impl TabGeometryTransaction<'_> {
fn commit(&mut self) {
self.commit = true;
}
}
impl Drop for TabGeometryTransaction<'_> {
fn drop(&mut self) {
let publish = self
.mux
.tab_resize_notifications
.lock()
.finish(self.tab_id, self.commit);
if publish {
self.mux
.notify_immediate(MuxNotification::TabResized(self.tab_id));
}
}
}
/// Size of the per-pane pty read buffer. This lives for the lifetime of the
/// pane's reader thread, so it is sized for what a single read() can actually
/// return (the kernel tty buffer is far smaller than this) rather than
/// a large parser batch, which would pin excess memory per pane.
const PTY_READ_BUFSIZE: usize = 64 * 1024;
/// What a read of a pty with more waiting comes to: as much as macOS gives at
/// a time, and less than Linux does.
const FULL_PTY_READ: usize = 1024;
/// How many times a reader following a flood tries again when the pty runs
/// dry. The program fills it again within microseconds, which is much less
/// than waiting to be told takes, and a read that finds nothing is cheap.
/// More catch no more of a flood, and cost a program that writes in bursts.
#[cfg(unix)]
const FLOOD_RETRIES: usize = 8;
/// Bytes of external kitty payloads one flush may read into memory. A
/// parser that fell behind a frame stream finds hundreds of frame escapes
/// in a single read; reading every file they name held gigabytes for
/// pictures the next frame overwrites. Newest first, within this budget;
/// the rest are discarded unread (their temporary files still unlinked).
/// About nine full-pane 1632x1026 frames.
const KITTY_MATERIALIZE_BUDGET_PER_FLUSH: usize = 64 * 1024 * 1024;
/// Materialize Kitty file/shared-memory payloads on the parser thread, before
/// a pane implementation takes its terminal-model lock, so the lock never
/// covers disk IO.
pub(crate) fn materialize_kitty_image_data_sources(actions: &mut [Action]) {
let (bytes, discarded) = termwiz::escape::apc::materialize_kitty_actions_newest_first(
actions,
KITTY_MATERIALIZE_BUDGET_PER_FLUSH,
);
if discarded > 0 {
log::debug!(
"kitty: materialized {bytes} bytes of external image data, discarded {discarded} \
older frames unread"
);
} else if bytes > KITTY_MATERIALIZE_BUDGET_PER_FLUSH {
// Nothing to discard: the flush held distinct pictures (or a stream
// under fresh ids), which the protocol obliges us to read. The
// terminal's own image budget bounds what stays resident.
log::debug!(
"kitty: materialized {bytes} bytes of external image data in one flush, \
none superseded"
);
}
}
/// Whether `action` carries a Kitty payload still to be read from a file or
/// shared memory (see `materialize_kitty_image_data_sources`).
pub(crate) fn is_external_kitty_image_data_source(action: &Action) -> bool {
match action {
Action::KittyImage(image) => image.has_external_data_source(),
_ => false,
}
}
pub(crate) fn has_external_kitty_image_data_source(actions: &[Action]) -> bool {
actions.iter().any(is_external_kitty_image_data_source)
}
/// A pane whose parser holds the terminal for a long time, on request:
/// `THINKTERM_WEDGE_PANE=<pane id>` in the server's environment makes that
/// pane's parser take its terminal lock and keep it for
/// `THINKTERM_WEDGE_SECS` (default 60) once, on the pane's first batch of
/// output. What a runaway program does by accident, made to order, so the
/// promise that one such pane leaves every other pane, the pane list and
/// every client answering can be checked rather than assumed. Costs one
/// atomic load per batch when unset.
fn wedge_for_the_test(pane: &Arc<dyn Pane>) {
use std::sync::OnceLock;
static WEDGE: OnceLock<Option<(PaneId, Duration)>> = OnceLock::new();
static DONE: AtomicBool = AtomicBool::new(false);
let Some((wedged, hold)) = *WEDGE.get_or_init(|| {
let pane_id = std::env::var("THINKTERM_WEDGE_PANE").ok()?.parse().ok()?;
let secs = std::env::var("THINKTERM_WEDGE_SECS")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(60);
Some((pane_id, Duration::from_secs(secs)))
}) else {
return;
};
if pane.pane_id() != wedged || DONE.swap(true, Ordering::Relaxed) {
return;
}
log::warn!("THINKTERM_WEDGE_PANE: holding pane {wedged}'s terminal for {hold:?}");
pane.hold_render_state_for_the_test(hold);
log::warn!("THINKTERM_WEDGE_PANE: pane {wedged} released");
}
/// This function applies parsed actions to the pane and notifies any
/// mux subscribers about the output event. `actions` is left empty with its
/// allocation kept for the next batch. `external_kitty` says whether any of
/// the actions carries an external Kitty payload; the parser tracks that as
/// it appends, so a batch without one is not scanned here or by the pane.
fn send_actions_to_mux(
pane: &Weak<dyn Pane>,
dead: &Arc<AtomicBool>,
actions: &mut Vec<Action>,
external_kitty: bool,
) {
let start = Instant::now();
debug_assert_eq!(
external_kitty,
has_external_kitty_image_data_source(actions)
);
// External kitty payloads (a path or shm name) are read here, on the
// parser thread and per flush, so the terminal lock never covers disk
// IO and a backlog is trimmed to its newest frames before any of it is
// read. Reading at parse time instead held every frame of the backlog.
if external_kitty && configuration().enable_kitty_graphics {
materialize_kitty_image_data_sources(actions);
}
match pane.upgrade() {
Some(pane) => {
wedge_for_the_test(&pane);
pane.perform_actions_in_place(actions, external_kitty);
debug_assert!(actions.is_empty());
histogram!("send_actions_to_mux.perform_actions.latency").record(start.elapsed());
Mux::notify_from_any_thread(MuxNotification::PaneOutput(pane.pane_id()));
}
None => {
actions.clear();
// Something else removed the pane from
// the mux, so signal that we should stop
// trying to process it in read_from_pane_pty.
dead.store(true, Ordering::Relaxed);
}
}
histogram!("send_actions_to_mux.rate").record(1.);
}
/// Whether the parser should give its batch allocation back before waiting
/// for more output. It is kept while output keeps arriving, so a busy pane
/// does not regrow it for every batch; it is released once nothing is left
/// to read, and whenever a batch grew past one action per byte of a full
/// read, which only held synchronized output can reach.
fn release_action_buffer(actions: &Vec<Action>, read_capacity: usize, more_to_read: bool) -> bool {
actions.capacity() > 0 && (!more_to_read || actions.capacity() > read_capacity)
}
fn parse_buffered_data(
pane: Weak<dyn Pane>,
pane_id: PaneId,
dead: &Arc<AtomicBool>,
mut rx: output_buffer::OutputReceiver,
heartbeat: Arc<parse_watchdog::ParseHeartbeat>,
control: Option<Arc<ReaderControl>>,
) {
let mut read_capacity = configuration().mux_output_parser_buffer_size;
let mut buf = Vec::new();
// Bytes read and not yet turned into applied actions; settled against
// the reader's count whenever nothing is left waiting to be sent.
let mut unapplied = 0usize;
let mut parser = termwiz::escape::parser::Parser::new();
parser.set_text_keeps_line_controls(true);
let mut actions = vec![];
let mut hold = false;
let mut action_size = 0;
let mut delay = Duration::from_millis(configuration().mux_output_parser_coalesce_delay_ms);
let mut deadline = None;
let mut external_kitty = false;
loop {
if actions.is_empty() && actions.capacity() > 0 {
let more_to_read = rx.readable_now();
if release_action_buffer(&actions, read_capacity, more_to_read) {
actions = Vec::new();
}
}
match rx.read_buffered(&mut buf, read_capacity) {
Ok(size) if size == 0 => {
dead.store(true, Ordering::Relaxed);
break;
}
Err(_) => {
dead.store(true, Ordering::Relaxed);
break;
}
Ok(size) => {
unapplied += size;
let work_started = Instant::now();
parser.parse_with_borrowed_text(&buf[0..size], |event| {
let action = match event {
ParsedAction::Action(action) => action,
text @ ParsedAction::Print(_) => {
text.append_to(&mut actions);
return;
}
};
let mut flush = false;
match &action {
Action::CSI(CSI::Mode(Mode::SetDecPrivateMode(DecPrivateMode::Code(
DecPrivateModeCode::SynchronizedOutput,
)))) => {
hold = true;
// Flush prior actions
if !actions.is_empty() {
send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);
external_kitty = false;
action_size = 0;
}
}
Action::CSI(CSI::Mode(Mode::ResetDecPrivateMode(
DecPrivateMode::Code(DecPrivateModeCode::SynchronizedOutput),
))) => {
hold = false;
flush = true;
}
Action::CSI(CSI::Device(dev)) if matches!(**dev, Device::SoftReset) => {
hold = false;
flush = true;
}
_ => {}
};
external_kitty |= is_external_kitty_image_data_source(&action);
action.append_to(&mut actions);
if flush && !actions.is_empty() {
send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);
external_kitty = false;
action_size = 0;
}
});
// The receive wait below is idle time, so busy work is
// recorded here (the parse, including any mid-parse flushes
// into the terminal) and separately around the flush below.
heartbeat.add(work_started.elapsed(), size);
action_size += size;
if !actions.is_empty() && !hold {
// If we haven't accumulated too much data,
// pause for a short while to increase the chances
// that we coalesce a full "frame" from an unoptimized
// TUI program
if action_size < read_capacity {
let poll_delay = match deadline {
None => {
deadline.replace(Instant::now() + delay);
Some(delay)
}
Some(target) => target.checked_duration_since(Instant::now()),
};
if let Some(poll_delay) = poll_delay {
if rx.wait_readable(poll_delay) {
// We can read now without blocking, so accumulate
// more data into actions
continue;
}
// Not readable in time: let the data we have flow into
// the terminal model
}
}
let flush_started = Instant::now();
send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);
external_kitty = false;
heartbeat.add(flush_started.elapsed(), 0);
deadline = None;
action_size = 0;
}
let config = configuration();
read_capacity = config.mux_output_parser_buffer_size;
delay = Duration::from_millis(config.mux_output_parser_coalesce_delay_ms);
if actions.is_empty() {
if let Some(control) = &control {
control.pending_bytes.fetch_sub(unapplied, Ordering::SeqCst);
}
unapplied = 0;
}
if let Some(control) = &control {
control
.holding
.store(hold && !actions.is_empty(), Ordering::SeqCst);
}
}
}
}
// Don't forget to send anything that we might have buffered
// to be displayed before we return from here; this is important
// for very short lived commands so that we don't forget to
// display what they displayed.
if !actions.is_empty() {
send_actions_to_mux(&pane, &dead, &mut actions, external_kitty);
}
if let Some(control) = &control {
control.pending_bytes.fetch_sub(unapplied, Ordering::SeqCst);
}
parse_watchdog::unregister(pane_id);
}
/// A pane's reader thread, from the outside. A takeover hands the pty to
/// another process, and while it does nothing here may read it; the
/// control lets the reader be parked and says when everything it read
/// has been applied to the terminal.
pub struct ReaderControl {
/// A byte written here breaks the reader out of its poll.
#[cfg(unix)]
wake: parking_lot::Mutex<FileDescriptor>,
/// The reader thread. A pipe cannot be polled on Windows, so a pause
/// cancels the blocking read the thread is in instead; a cancelled read
/// takes nothing from the pipe.
#[cfg(windows)]
thread: std::os::windows::io::OwnedHandle,
/// Whether the reader is inside its read, the only call a cancel may
/// hit. A lock, not a flag: the reader cannot leave the read for its
/// write to the parser while a cancel is being issued, so the cancel
/// never lands on that write.
#[cfg(windows)]
in_read: parking_lot::Mutex<bool>,
paused: AtomicBool,
/// Whether the reader is parked; `changed` is signalled by both sides.
parked: std::sync::Mutex<bool>,
changed: std::sync::Condvar,
/// Bytes read from the pty and not yet applied to the terminal.
pending_bytes: AtomicUsize,
/// The parser holds a synchronized-output frame that is not finished:
/// what it holds cannot be applied until the program ends the frame,
/// which a parked reader would never deliver. A pause accepts that
/// state as quiescent; the held frame is lost, the next one redraws.
holding: AtomicBool,
}
impl ReaderControl {
#[cfg(unix)]
fn new(wake: FileDescriptor) -> Self {
Self {
wake: parking_lot::Mutex::new(wake),
paused: AtomicBool::new(false),
parked: std::sync::Mutex::new(false),
changed: std::sync::Condvar::new(),
pending_bytes: AtomicUsize::new(0),
holding: AtomicBool::new(false),
}
}
/// For the calling thread, which is the reader.
#[cfg(windows)]
fn for_this_thread() -> anyhow::Result<Self> {
// A real handle: GetCurrentThread's means "the calling thread".
let thread = unsafe {
std::os::windows::io::BorrowedHandle::borrow_raw(
winapi::um::processthreadsapi::GetCurrentThread() as _,
)
}
.try_clone_to_owned()
.context("a handle on the reader thread")?;
Ok(Self {
thread,
in_read: parking_lot::Mutex::new(false),
paused: AtomicBool::new(false),
parked: std::sync::Mutex::new(false),
changed: std::sync::Condvar::new(),
pending_bytes: AtomicUsize::new(0),
holding: AtomicBool::new(false),
})
}
/// Park the reader and wait until what it read has been applied.
/// Times out on a reader that never parks, or on a parser that never
/// finishes: an output batch held for synchronized output, or a pane
/// wedged for the test. Either way the reader runs again on return.
fn pause(&self, timeout: Duration) -> anyhow::Result<()> {
let deadline = Instant::now() + timeout;
self.paused.store(true, Ordering::SeqCst);
#[cfg(unix)]
if let Err(err) = self.wake.lock().write_all(&[1]) {
self.resume();
return Err(err).context("waking the pane reader");
}
// A cancel finds nothing when the reader is between checking the
// flag and entering its read, so it is repeated until the park.
#[cfg(windows)]
let interval = Duration::from_millis(10);
#[cfg(not(windows))]
let interval = timeout;
{
let mut parked = self.parked.lock().unwrap();
while !*parked {
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
drop(parked);
self.resume();
anyhow::bail!("the pane reader did not park within {timeout:?}");
}
#[cfg(windows)]
{
let in_read = self.in_read.lock();
if *in_read {
use std::os::windows::io::AsRawHandle;
unsafe {
winapi::um::ioapiset::CancelSynchronousIo(
self.thread.as_raw_handle() as _,
)
};
}
}
parked = self.changed.wait_timeout(parked, left.min(interval)).unwrap().0;
}
}
loop {
let pending = self.pending_bytes.load(Ordering::SeqCst);
if pending == 0 || self.holding.load(Ordering::SeqCst) {
return Ok(());
}
if Instant::now() >= deadline {
self.resume();
anyhow::bail!(
"{pending} bytes read from the pty were still being applied after {timeout:?}"
);
}
std::thread::sleep(Duration::from_millis(2));
}
}
fn resume(&self) {
self.paused.store(false, Ordering::SeqCst);
let _parked = self.parked.lock().unwrap();
self.changed.notify_all();
}
/// The reader's side: announce the park, wait for the resume.
fn park(&self) {
let mut parked = self.parked.lock().unwrap();
*parked = true;
self.changed.notify_all();
while self.paused.load(Ordering::SeqCst) {
parked = self.changed.wait(parked).unwrap();
}
*parked = false;
self.changed.notify_all();
}
pub fn pending_bytes(&self) -> usize {
self.pending_bytes.load(Ordering::SeqCst)
}
}
lazy_static::lazy_static! {
static ref PANE_READERS: parking_lot::Mutex<HashMap<PaneId, Arc<ReaderControl>>> =
parking_lot::Mutex::new(HashMap::new());
}
/// Reads a flood of output without waiting to be told of each piece. macOS
/// hands out a pty's output a KiB at a time, and finding out that there is
/// more takes longer than reading it, which made that the limit on how fast a
/// pane could take in output. Only for output with more behind it: anything
/// less than a full read goes back to waiting, so typing never comes here.
#[cfg(unix)]
struct FloodReader {
/// A duplicate of the pty that, unlike the pane's reader, says when there
/// is nothing to read. Non-blocking is a mode of the pty and not of this
/// duplicate: the pane's reader and writer see it too, and allow for it.
pty: FileDescriptor,
following: bool,
}
#[cfg(unix)]
impl FloodReader {
fn new(pty_fd: i32) -> Option<Self> {
Some(Self {
pty: FileDescriptor::dup(&pty_fd).ok()?,
following: false,
})
}
/// Whether what was just read has more behind it.
fn follow(&mut self, more: bool) {
self.following = more && (self.following || self.pty.set_non_blocking(true).is_ok());
}
/// Read what is there, or return None for the reader to go back to
/// waiting, which is also where the pty's end and any error are met.
fn read(&mut self, buf: &mut [u8]) -> Option<usize> {
if !self.following {
return None;
}
for _ in 0..FLOOD_RETRIES {
match self.pty.read(buf) {
Ok(size) if size > 0 => return Some(size),
Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => std::hint::spin_loop(),
_ => break,
}
}
self.following = false;
None
}
}
/// A pane reader that is parked. Dropping it lets the reader run again;
/// `keep` does not, for the pty that now belongs to another process.
pub struct PausedReader {
control: Arc<ReaderControl>,
kept: bool,
}
impl PausedReader {
/// The pty has been handed to another process: this reader must never
/// run again. It ends with this process.
pub fn keep(mut self) {
self.kept = true;
}
}
impl Drop for PausedReader {
fn drop(&mut self) {
if !self.kept {
self.control.resume();
}
}
}
/// Park the reader of `pane_id` and wait until everything it read has been
/// applied to the terminal, so that the terminal is exactly what the pty
/// produced up to the park. Output the program writes meanwhile waits in
/// the kernel's pty buffer for whoever reads next. Panes without a pty
/// cannot be paused on unix; on Windows every reader can, as a pause
/// cancels the read it blocks in.
pub fn pause_pane_reader(pane_id: PaneId, timeout: Duration) -> anyhow::Result<PausedReader> {
let control = PANE_READERS
.lock()
.get(&pane_id)
.cloned()
.ok_or_else(|| anyhow!("pane {pane_id} has no reader that can be paused"))?;
control.pause(timeout)?;
Ok(PausedReader {
control,
kept: false,
})
}
/// This function is run in a separate thread; its purpose is to read from
/// the pty, blocking except through a flood on macOS (see `FloodReader`), and
/// to feed the bytes to the parser thread through a bounded byte buffer. With
/// a pty file descriptor to poll, the reader can also be parked; see
/// [`pause_pane_reader`].
fn read_from_pane_pty(
pane: Weak<dyn Pane>,
banner: Option<String>,
mut reader: Box<dyn std::io::Read>,
pty_fd: Option<i32>,
) {
let mut buf = vec![0; PTY_READ_BUFSIZE];
let dead = Arc::new(AtomicBool::new(false));
let Some(held) = pane.upgrade() else {
return;
};
let (pane_id, exit_behavior) = (held.pane_id(), held.exit_behavior());
let (mut tx, rx) = output_buffer::channel();
// The wake pipe and the control exist only when the pty can be polled;
// without them the loop below is the plain blocking read.
#[cfg(unix)]
let pausable = pty_fd.and_then(|fd| match filedescriptor::Pipe::new() {
Ok(pipe) => Some((fd, pipe)),
Err(err) => {
log::warn!("pane {pane_id}: no wake pipe for the reader, it cannot be paused: {err:#}");
None
}
});
#[cfg(unix)]
let (control, mut wake_read, poll_fd) = match pausable {
Some((fd, pipe)) => {
let control = Arc::new(ReaderControl::new(pipe.write));
PANE_READERS.lock().insert(pane_id, Arc::clone(&control));
(Some(control), Some(pipe.read), Some(fd))
}
None => (None, None, None),
};
// Only while the pane is held: once it has gone, the number of its pty can
// be another pane's, and this would read that pane's output.
#[cfg(unix)]
let mut flood = if cfg!(target_os = "macos") {
poll_fd.and_then(FloodReader::new)
} else {
None
};
drop(held);
// Every reader can be paused here: a cancel ends the read it blocks in.
#[cfg(windows)]
let control = {
let _ = pty_fd;
match ReaderControl::for_this_thread() {
Ok(control) => {
let control = Arc::new(control);
PANE_READERS.lock().insert(pane_id, Arc::clone(&control));
Some(control)
}
Err(err) => {
log::warn!("pane {pane_id}: the reader cannot be paused: {err:#}");
None
}
}
};
let heartbeat = parse_watchdog::register(pane_id);
let exit_watch = pane.clone();
std::thread::spawn({
let dead = Arc::clone(&dead);
let control = control.clone();
move || parse_buffered_data(pane, pane_id, &dead, rx, heartbeat, control)
});
if let Some(banner) = banner {
tx.write_all(banner.as_bytes()).ok();
}
// What comes from elsewhere is not read in pty-sized pieces, and a parser
// left to come back on its own needs finer timers than Windows has.
let can_flood = cfg!(unix) && pty_fd.is_some();
while !dead.load(Ordering::Relaxed) {
#[cfg(windows)]
if let Some(control) = &control {
if control.paused.load(Ordering::SeqCst) {
control.park();
continue;
}
}
// What is read here needs no waiting. A pause goes the usual way, and
// whatever happens to the pty meanwhile, the flood starts again after.
#[cfg(unix)]
let flood_read = match (&control, &mut flood) {
(Some(control), Some(flood)) if control.paused.load(Ordering::SeqCst) => {
flood.follow(false);
None
}
(_, Some(flood)) => flood.read(&mut buf),
_ => None,
};
#[cfg(not(unix))]
let flood_read: Option<usize> = None;
#[cfg(unix)]
if let (None, Some(control), Some(wake_read), Some(fd)) =
(flood_read, &control, &mut wake_read, poll_fd)
{
let mut pfd = [
pollfd {
fd: fd as _,
events: POLLIN,
revents: 0,
},
pollfd {
fd: wake_read.as_socket_descriptor(),
events: POLLIN,
revents: 0,
},
];
match poll(&mut pfd, None) {
Ok(_) => {}
Err(err) => {
// EBADF: the pane closed its pty while this thread
// waited on it, which is how a pane torn down during
// startup ends; the read loop would have seen EOF.
let pty_closed = matches!(
&err,
filedescriptor::Error::Poll(io) | filedescriptor::Error::Io(io)
if io.raw_os_error() == Some(libc::EBADF)
);
if pty_closed {
log::trace!("read_pty poll: pane {pane_id} pty closed");
} else {
error!("read_pty poll failed: pane {} {:?}", pane_id, err);
}
break;
}
}
if pfd[1].revents != 0 {
let mut drained = [0u8; 16];
wake_read.read(&mut drained).ok();
}
if control.paused.load(Ordering::SeqCst) {
control.park();
continue;
}
if pfd[0].revents == 0 {
continue;
}
}
#[cfg(windows)]
if let Some(control) = &control {
*control.in_read.lock() = true;
}
let read = match flood_read {
Some(size) => Ok(size),
None => reader.read(&mut buf),
};
#[cfg(windows)]
if let Some(control) = &control {
*control.in_read.lock() = false;
// A pause cancelled the read; nothing was taken from the pipe.
if matches!(&read, Err(err) if err.raw_os_error()
== Some(winapi::shared::winerror::ERROR_OPERATION_ABORTED as i32))
{
continue;
}
}
match read {
Ok(size) if size == 0 => {
log::trace!("read_pty EOF: pane_id {}", pane_id);
break;
}
Err(err) => {
error!("read_pty failed: pane {} {:?}", pane_id, err);
break;
}
Ok(size) => {
histogram!("read_from_pane_pty.bytes.rate").record(size as f64);
log::trace!("read_pty pane {pane_id} read {size} bytes");
// Counted before the parser can see the bytes, so the
// count never dips below what is really pending.
if let Some(control) = &control {
control.pending_bytes.fetch_add(size, Ordering::SeqCst);
}
// A full read says more is coming, and the wake is the most
// expensive part of a read. Anything less goes straight on.
let flooding = can_flood && size >= FULL_PTY_READ;
#[cfg(unix)]
if let Some(flood) = &mut flood {
flood.follow(flooding);
}
let written = if flooding {
tx.write_all_flooding(&buf[..size])
} else {
tx.write_all(&buf[..size])
};
if let Err(err) = written {
if let Some(control) = &control {
control.pending_bytes.fetch_sub(size, Ordering::SeqCst);
}
error!(
"read_pty failed to write to parser: pane {} {:?}",
pane_id, err
);
break;
}
}
}
}
if control.is_some() {
PANE_READERS.lock().remove(&pane_id);
}
match exit_behavior.unwrap_or_else(|| configuration().exit_behavior) {
ExitBehavior::Hold | ExitBehavior::CloseOnCleanExit => {
promise::spawn::spawn_into_main_thread(async move {
let mux = Mux::get();
log::trace!("checking for dead windows after EOF on pane {}", pane_id);
mux.prune_dead_windows();
})
.detach();
}
ExitBehavior::Close => {
// The program's end usually closes the pty, and it is reaped a
// moment after. Give it that moment: then the mux has the
// status (`cli wait`, `PaneExited`) before it removes the pane.
if let Some(pane) = exit_watch.upgrade() {
let deadline = Instant::now() + EXIT_STATUS_GRACE;
while !pane.is_dead() && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
}
promise::spawn::spawn_into_main_thread(async move {
let mux = Mux::get();
mux.remove_pane(pane_id);
})
.detach();
}
}
dead.store(true, Ordering::Relaxed);
}
/// How long a pane closed by its pty's end waits for its program's status.
const EXIT_STATUS_GRACE: Duration = Duration::from_millis(250);
lazy_static::lazy_static! {
static ref MUX: Mutex<Option<Arc<Mux>>> = Mutex::new(None);
}
pub struct MuxWindowBuilder {
window_id: WindowId,
activity: Option<Activity>,
notified: bool,
}
struct ExistingWindowSpawnContext {
size: TerminalSize,
term_config: Option<Arc<dyn TerminalConfiguration>>,
}
impl MuxWindowBuilder {
fn notify(&mut self) {
if self.notified {
return;
}
self.notified = true;
let activity = self.activity.take().unwrap();
let window_id = self.window_id;
let mux = Mux::get();
if mux.is_main_thread() {
// If we're already on the mux thread, just send the notification
// immediately.
// This is super important for Wayland; if we push it to the
// spawn queue below then the extra milliseconds of delay
// causes it to get confused and shutdown the connection!?
mux.notify(MuxNotification::WindowCreated(window_id));
} else {
promise::spawn::spawn_into_main_thread(async move {
if let Some(mux) = Mux::try_get() {
mux.notify(MuxNotification::WindowCreated(window_id));
drop(activity);
}
})
.detach();
}
}
}
impl Drop for MuxWindowBuilder {
fn drop(&mut self) {
self.notify();
}
}
impl std::ops::Deref for MuxWindowBuilder {
type Target = WindowId;
fn deref(&self) -> &WindowId {
&self.window_id
}
}
impl Mux {
pub fn new(default_domain: Option<Arc<dyn Domain>>) -> Self {
Self::new_with_runtime_server_id(default_domain, new_runtime_server_id())
}
/// A mux that answers to a runtime server id chosen by the caller: the
/// one a server inherits from the predecessor it took over from, so
/// that clients reconnecting to it find the same server rather than a
/// replacement.
pub fn new_with_runtime_server_id(
default_domain: Option<Arc<dyn Domain>>,
runtime_server_id: String,
) -> Self {
let mut domains = HashMap::new();
let mut domains_by_name = HashMap::new();
if let Some(default_domain) = default_domain.as_ref() {
domains.insert(default_domain.domain_id(), Arc::clone(default_domain));
domains_by_name.insert(
default_domain.domain_name().to_string(),
Arc::clone(default_domain),
);
}
let agent = if config::configuration().mux_enable_ssh_agent {
Some(AgentProxy::new())
} else {
None
};
Self {
runtime_server_id,
connect_service: Mutex::new(None),
connect_suspended: AtomicBool::new(false),
tabs: RwLock::new(HashMap::new()),
panes: RwLock::new(HashMap::new()),
windows: RwLock::new(HashMap::new()),
default_domain: RwLock::new(default_domain),
domains_by_name: RwLock::new(domains_by_name),
domains: RwLock::new(domains),
subscribers: RwLock::new(HashMap::new()),
banner: RwLock::new(None),
clients: RwLock::new(HashMap::new()),
client_registrations: RwLock::new(HashMap::new()),
frontend_lease: Mutex::new(FrontendLeaseState::default()),
tab_resize_notifications: Mutex::new(TabResizeNotificationState::default()),
pane_output_generations: Mutex::new(PaneOutputGenerationState::default()),
#[cfg(test)]
frontend_geometry_failures: Mutex::new(std::collections::VecDeque::new()),
palette_advisories: Mutex::new(PaletteAdvisoryState::default()),
recent_pane_exits: Mutex::new(std::collections::VecDeque::new()),
unannounced_pane_exits: Mutex::new(HashMap::new()),
pane_labels: RwLock::new(HashMap::new()),
identity: RwLock::new(None),
num_panes_by_workspace: RwLock::new(HashMap::new()),
main_thread_id: std::thread::current().id(),
agent,
}
}
/// Identity of this running mux process. This is intentionally ephemeral:
/// today a mux restart cannot preserve any pane whose environment refers
/// to the old value.
pub fn runtime_server_id(&self) -> &str {
&self.runtime_server_id
}
pub fn connect_service(
self: &Arc<Self>,
) -> thinkterm_reverse_access::Result<Arc<connect::service::Service>> {
let mut service = self.connect_service.lock();
if self.connect_suspended.load(Ordering::Acquire) {
return Err(thinkterm_reverse_access::Error::Unavailable);
}
if service.is_none() {
*service = Some(connect::service::Service::start(self)?);
}
Ok(Arc::clone(
service.as_ref().expect("initialized Connect service"),
))
}
pub fn suspend_connect(self: &Arc<Self>) -> ConnectSuspension {
let mut service = self.connect_service.lock();
self.connect_suspended.store(true, Ordering::Release);
if let Some(service) = service.take() {
service.close();
}
ConnectSuspension {
mux: Arc::downgrade(self),
resume: true,
}
}
/// The agent socket panes here are given (`SSH_AUTH_SOCK`), if any.
pub fn ssh_agent_path(&self) -> Option<std::path::PathBuf> {
self.agent.as_ref().map(|agent| agent.path().to_path_buf())
}
fn get_default_workspace(&self) -> String {
let config = configuration();
config
.default_workspace
.as_deref()
.unwrap_or(DEFAULT_WORKSPACE)
.to_string()
}
pub fn is_main_thread(&self) -> bool {
std::thread::current().id() == self.main_thread_id
}
fn recompute_pane_count(&self) {
let mut count = HashMap::new();
for window in self.windows.read().values() {
let workspace = window.get_workspace();
for tab in window.iter() {
*count.entry(workspace.to_string()).or_insert(0) += match tab.count_panes() {
Some(n) => n,
None => {
// Busy: abort this and we'll retry later
return;
}
};
}
}
*self.num_panes_by_workspace.write() = count;
}
pub fn client_had_input(&self, client_id: &ClientId) {
if let Some(info) = self.clients.write().get_mut(client_id) {
info.update_last_input();
}
if let Some(agent) = &self.agent {
agent.update_target();
}
}
/// Transport-scoped input accounting. Returns false for work from a
/// connection that has already been superseded by a reconnect.
pub fn registered_client_had_input(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
) -> bool {
let registrations = self.client_registrations.read();
if registrations.get(client_id) != Some(®istration) {
return false;
}
self.client_had_input(client_id);
true
}
fn validate_frontend_viewport(
&self,
tab_id: TabId,
viewport: &FrontendViewport,
) -> anyhow::Result<()> {
let tab = self
.get_tab(tab_id)
.ok_or_else(|| anyhow!("no such tab {tab_id}"))?;
if viewport.size().rows == 0 || viewport.size().cols == 0 {
anyhow::bail!("viewport for tab {tab_id} has zero rows or columns");
}
if let FrontendViewport::Native { panes, .. } = viewport {
for pane in panes {
if !tab.contains_pane(pane.pane_id) {
anyhow::bail!(
"pane {} is not contained by viewport tab {tab_id}",
pane.pane_id
);
}
if pane.size.rows == 0 || pane.size.cols == 0 {
anyhow::bail!("viewport for pane {} is empty", pane.pane_id);
}
}
let frames = panes
.iter()
.map(|pane| (pane.pane_id, pane.frame))
.collect::<Vec<_>>();
tab.validate_frontend_frames(viewport.size(), &frames)?;
}
Ok(())
}
fn access_state_locked(lease: &FrontendLeaseState) -> FrontendAccessState {
FrontendAccessState {
mode: lease.access_mode,
owner: match lease.access_mode {
FrontendAccessMode::TmuxLatest => None,
FrontendAccessMode::Handoff => lease.handoff_owner.clone(),
},
generation: lease.access_generation,
}
}
pub fn frontend_access_state(&self) -> FrontendAccessState {
Self::access_state_locked(&self.frontend_lease.lock())
}
/// The lease state of `tab_id` as it stands, for a client that has just
/// arrived and heard none of the changes that led here. Nothing is
/// bumped or published. A tab no renderer has ever claimed reports no
/// owner and no view, which is itself an answer: it tells a renderer it
/// is free to drive.
pub fn frontend_viewport_state(&self, tab_id: TabId) -> Option<FrontendViewportState> {
let tab = self.get_tab(tab_id)?;
// The lease is released before the tab is asked its size, the way
// the publishing path does it: Tab methods notify with their own
// lock held, and a subscriber that ever touches the lease would
// close a cycle.
let (owner, view, generation, access) = {
let lease = self.frontend_lease.lock();
let access = Self::access_state_locked(&lease);
match lease.tabs.get(&tab_id) {
Some(state) => (
state.owner.clone(),
state.view.clone(),
state.generation,
access,
),
None => (None, None, 0, access),
}
};
Some(FrontendViewportState {
tab_id,
owner,
canonical_size: tab.get_size(),
view,
generation,
access,
})
}
/// Install the mode loaded by the mux-server persistence layer. This is
/// intentionally one-shot so a configuration reload cannot overwrite a
/// mode selected while this server process is running.
pub fn initialize_frontend_access_mode(&self, mode: FrontendAccessMode) {
let changed = {
let mut lease = self.frontend_lease.lock();
if lease.access_initialized {
return;
}
let changed = lease.access_mode != mode;
lease.set_access_mode(mode);
lease.access_initialized = true;
changed
};
// A client that registered before this ran was told the default
// mode at generation 0 and drops later states at that generation;
// a published change is the only correction it will accept.
if changed {
self.publish_frontend_access_state();
}
}
pub fn frontend_access_mode_is_initialized(&self) -> bool {
self.frontend_lease.lock().access_initialized
}
pub fn client_has_frontend_access(&self, client_id: &ClientId) -> bool {
let lease = self.frontend_lease.lock();
match lease.access_mode {
FrontendAccessMode::TmuxLatest => true,
// Non-rendering clients (never advertised a viewport) pass: the
// gate arbitrates between screens, and they are not one. This is
// also what lets a headless `thinkterm cli` work at all in
// Handoff -- with no owner and `handoff_ever_owned` set, nothing
// else would ever answer true for it.
FrontendAccessMode::Handoff => {
lease.handoff_owner.as_ref() == Some(client_id)
|| !lease.ever_rendered.contains(client_id)
}
}
}
/// Check the server-wide interaction gate for one live transport. This
/// does not claim anything: chrome actions such as closing a pane must be
/// rejected for B's opaque followers, but must not silently take A's
/// layout lease just because they originated outside the terminal area.
pub fn registered_client_has_frontend_access(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
) -> Option<bool> {
if self.client_registrations.read().get(client_id) != Some(®istration) {
return None;
}
Some(self.client_has_frontend_access(client_id))
}
fn viewport_state(&self, tab_id: TabId) -> Option<FrontendViewportState> {
let tab = self.get_tab(tab_id)?;
let lease = self.frontend_lease.lock();
let state = lease.tabs.get(&tab_id)?;
Some(FrontendViewportState {
tab_id,
owner: state.owner.clone(),
canonical_size: tab.get_size(),
view: state.view.clone(),
generation: state.generation,
access: Self::access_state_locked(&lease),
})
}
/// Record what the owner of a tab's viewport is looking at.
///
/// Only the owner may write it, for the same reason only the owner sets the
/// size: a renderer that is not being used does not get to move everyone
/// else. Returns whether anything changed, so a caller can avoid publishing
/// a scroll that already matches.
pub fn set_client_view(&self, client_id: &ClientId, tab_id: TabId, view: FrontendView) -> bool {
let changed = {
let mut lease = self.frontend_lease.lock();
let may_publish = match lease.access_mode {
FrontendAccessMode::TmuxLatest => {
lease
.tabs
.get(&tab_id)
.and_then(|state| state.owner.as_ref())
== Some(client_id)
}
FrontendAccessMode::Handoff => lease.handoff_owner.as_ref() == Some(client_id),
};
if !may_publish {
return false;
}
let Some(state) = lease.tabs.get_mut(&tab_id) else {
return false;
};
if state.view.as_ref() == Some(&view) {
false
} else {
state.view = Some(view);
true
}
};
if changed {
self.publish_frontend_viewport_state(tab_id);
}
changed
}
/// Record one renderer's desired viewport for one tab. A passive resize
/// never steals an established owner. The first renderer after server
/// startup seeds B's global handoff owner; after that owner disconnects,
/// passive reports remain blocked until an explicit claim.
pub fn set_client_viewport(
&self,
client_id: &ClientId,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<FrontendViewportState> {
self.validate_frontend_viewport(tab_id, &viewport)?;
let (should_apply, should_publish, access_changed, joined) = {
let mut lease = self.frontend_lease.lock();
let joined = lease.ever_rendered.insert(client_id.clone());
let mut access_changed = false;
if lease.access_mode == FrontendAccessMode::Handoff
&& lease.handoff_owner.is_none()
&& (!lease.handoff_ever_owned
|| lease
.handoff_last_owner
.as_ref()
.is_some_and(|last| same_device(last, client_id)))
{
lease.set_handoff_owner(Some(client_id.clone()));
lease.handoff_ever_owned = true;
lease.handoff_last_owner = None;
access_changed = true;
}
let mode = lease.access_mode;
let handoff_owner = lease.handoff_owner.clone();
let state = lease.tabs.entry(tab_id).or_default();
let viewport_changed = state.viewports.get(client_id) != Some(&viewport);
if viewport_changed {
state.viewports.insert(client_id.clone(), viewport.clone());
}
// Compare against the owner as it stood before this report:
// seeding a vacated tab IS an owner change and must apply and
// publish. Without the bumped generation the RPC reply is
// rejected as stale by the client, which then never learns it
// owns the tab and keeps reporting a bare paneless viewport
// forever - the server stays on the departed owner's geometry.
let owner_was_me = state.owner.as_ref() == Some(client_id);
let can_drive = match mode {
// An ownerless tab belongs to whoever reports. The write
// itself happens in the owner_changed branch below - the
// single place this path moves `state.owner`.
FrontendAccessMode::TmuxLatest => state.owner.is_none() || owner_was_me,
FrontendAccessMode::Handoff => handoff_owner.as_ref() == Some(client_id),
};
let owner_changed = can_drive && !owner_was_me;
if owner_changed {
state.set_owner(Some(client_id.clone()));
// Dropping the view on a takeover is safe because every path
// that vacates the owner also clears the view; a reseed
// therefore never discards a live scroll position.
state.view = None;
}
let should_apply = can_drive && (owner_changed || viewport_changed || access_changed);
(should_apply, should_apply, access_changed, joined)
};
if joined {
Mux::notify_from_any_thread(MuxNotification::ClientJoined(Arc::new(client_id.clone())));
}
if should_apply {
if let Err(err) = self.apply_frontend_viewport(tab_id, &viewport) {
return Err(self.fail_closed_frontend_geometry(tab_id, None, err));
}
}
if access_changed {
self.publish_frontend_access_state();
}
if should_publish {
self.publish_frontend_viewport_state(tab_id);
}
self.viewport_state(tab_id)
.ok_or_else(|| anyhow!("viewport state for tab {tab_id} disappeared"))
}
/// Transport-scoped viewport update. Old handlers can still have work in
/// the mux queue after a reconnect; reject it instead of applying stale
/// geometry to the replacement connection.
pub fn set_registered_client_viewport(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<Option<FrontendViewportState>> {
let registrations = self.client_registrations.read();
if registrations.get(client_id) != Some(®istration) {
return Ok(None);
}
drop(registrations);
let state = self.set_client_viewport(client_id, tab_id, viewport);
self.forget_renderer_if_unregistered(client_id);
state.map(Some)
}
/// A client that unregistered while its report was under way can be
/// counted as showing panes after `unregister_client` already let it
/// go: no `ClientLeft` would follow its `ClientJoined`, and the entry
/// would stay. Unregistering drops the registration before that
/// entry, so checking after the report catches every interleaving.
fn forget_renderer_if_unregistered(&self, client_id: &ClientId) {
if self.drop_unregistered_renderer(client_id) {
Mux::notify_from_any_thread(MuxNotification::ClientLeft(Arc::new(client_id.clone())));
}
}
/// Whether `client_id` was counted as showing panes though it is no
/// longer registered. The count is dropped.
fn drop_unregistered_renderer(&self, client_id: &ClientId) -> bool {
!self.client_registrations.read().contains_key(client_id)
&& self.frontend_lease.lock().ever_rendered.remove(client_id)
}
pub fn client_owns_frontend_lease(&self, client_id: &ClientId, tab_id: TabId) -> bool {
let lease = self.frontend_lease.lock();
match lease.access_mode {
FrontendAccessMode::TmuxLatest => {
lease
.tabs
.get(&tab_id)
.and_then(|state| state.owner.as_ref())
== Some(client_id)
}
FrontendAccessMode::Handoff => lease.handoff_owner.as_ref() == Some(client_id),
}
}
pub fn current_identity_owns_frontend_lease(&self, tab_id: TabId) -> bool {
// Lock order: registrations first, dropped before the lease lock is
// taken — the same order `set_registered_client_viewport` uses.
let identity = self.active_identity();
let live: HashSet<ClientId> = self.client_registrations.read().keys().cloned().collect();
let lease = self.frontend_lease.lock();
match lease.access_mode {
// Per-tab ownership is cheap to re-take and is re-seeded by the
// very next passive report (`set_client_viewport` seeds an
// ownerless tab unconditionally), so an unclaimed tab belongs to
// whoever asks — symmetric with the wire path.
FrontendAccessMode::TmuxLatest => {
match lease
.tabs
.get(&tab_id)
.and_then(|state| state.owner.as_ref())
{
None => true,
Some(owner) => identity.as_deref() == Some(owner),
}
}
FrontendAccessMode::Handoff => match lease.handoff_owner.as_ref() {
Some(owner) => identity.as_deref() == Some(owner),
// Bootstrap: nobody has ever rendered, nothing to protect.
None if !lease.handoff_ever_owned => true,
// Post-revocation. Symmetric with wire clients
// (`set_client_viewport` seeds only while
// `!handoff_ever_owned`): survivors must claim explicitly.
// The one exception is a sole renderer, which recovers on its
// own instead of sitting behind a takeover surface with
// nobody to take the terminal from — this preserves the fix
// for the measured paralysis (split-divider drags leaving
// TUIs at their old PTY size) without letting two surviving
// frontends fight over the geometry.
None => identity
.as_deref()
.is_some_and(|me| is_sole_live_renderer(&lease.tabs, &live, me)),
},
}
}
/// Account for an input PDU and enforce B's global input gate. In A, input
/// is allowed for every renderer and the most recently advertised local
/// geometry is used as a compatibility fallback; interactive frontends use
/// the explicit geometry-bearing claim before forwarding the input.
pub fn registered_client_had_tab_input(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
tab_id: TabId,
) -> bool {
let registrations = self.client_registrations.read();
if registrations.get(client_id) != Some(®istration) {
return false;
}
drop(registrations);
self.client_had_input(client_id);
let (mode, handoff_owner, viewport, ever_rendered) = {
let lease = self.frontend_lease.lock();
(
lease.access_mode,
lease.handoff_owner.clone(),
lease
.tabs
.get(&tab_id)
.and_then(|state| state.viewports.get(client_id).cloned()),
lease.ever_rendered.contains(client_id),
)
};
match mode {
// Same exemption as `client_has_frontend_access`: input from a
// client that never renders (CLI automation) is not a second
// screen fighting the owner.
FrontendAccessMode::Handoff => {
handoff_owner.as_ref() == Some(client_id) || !ever_rendered
}
FrontendAccessMode::TmuxLatest => {
if let Some(viewport) = viewport {
if let Err(err) = self.claim_frontend_viewport(client_id, tab_id, viewport) {
log::error!("failed to apply claimed viewport for tab {tab_id}: {err:#}");
}
self.forget_renderer_if_unregistered(client_id);
}
true
}
}
}
/// Explicit renderer claim used by terminal-area interaction and pane
/// layout changes. Geometry is supplied in this request and applied in the
/// same main-thread operation as the owner change.
pub fn claim_registered_client_viewport(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<Option<FrontendViewportState>> {
let registrations = self.client_registrations.read();
if registrations.get(client_id) != Some(®istration) {
return Ok(None);
}
drop(registrations);
self.client_had_input(client_id);
let state = self.claim_frontend_viewport(client_id, tab_id, viewport);
self.forget_renderer_if_unregistered(client_id);
state.map(Some)
}
fn claim_frontend_viewport(
&self,
client_id: &ClientId,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<FrontendViewportState> {
self.validate_frontend_viewport(tab_id, &viewport)?;
let unchanged = {
let lease = self.frontend_lease.lock();
let effective_owner = match lease.access_mode {
FrontendAccessMode::TmuxLatest => {
lease
.tabs
.get(&tab_id)
.and_then(|state| state.owner.as_ref())
== Some(client_id)
}
FrontendAccessMode::Handoff => lease.handoff_owner.as_ref() == Some(client_id),
};
effective_owner
&& lease.tabs.get(&tab_id).is_some_and(|state| {
state.owner.as_ref() == Some(client_id)
&& state.viewports.get(client_id) == Some(&viewport)
})
};
if unchanged {
return self
.viewport_state(tab_id)
.ok_or_else(|| anyhow!("viewport state for tab {tab_id} disappeared"));
}
let (access_changed, mut affected_tabs, joined) = {
let mut lease = self.frontend_lease.lock();
let joined = lease.ever_rendered.insert(client_id.clone());
let mode = lease.access_mode;
let mut access_changed = false;
let mut affected_tabs = vec![tab_id];
if mode == FrontendAccessMode::Handoff {
access_changed = lease.handoff_owner.as_ref() != Some(client_id);
lease.set_handoff_owner(Some(client_id.clone()));
lease.handoff_ever_owned = true;
// An explicit claim supersedes whoever drove before: the
// returning-device rule must not hand the lease back to it.
lease.handoff_last_owner = None;
if access_changed {
for (affected_tab_id, state) in &mut lease.tabs {
if state.view.take().is_some() && *affected_tab_id != tab_id {
affected_tabs.push(*affected_tab_id);
}
}
}
}
let state = lease.tabs.entry(tab_id).or_default();
state.viewports.insert(client_id.clone(), viewport.clone());
state.set_owner(Some(client_id.clone()));
state.view = None;
(access_changed, affected_tabs, joined)
};
if joined {
Mux::notify_from_any_thread(MuxNotification::ClientJoined(Arc::new(client_id.clone())));
}
if access_changed {
self.publish_frontend_access_state();
}
// Commit and publish B's new owner before Tab::resize can emit a
// synchronous TabResized notification. Any observer reacting to the
// new geometry must already mask the prior owner.
if let Err(err) = self.apply_frontend_viewport(tab_id, &viewport) {
return Err(self.fail_closed_frontend_geometry(tab_id, None, err));
}
affected_tabs.sort_unstable();
affected_tabs.dedup();
for affected_tab_id in affected_tabs {
self.publish_frontend_viewport_state(affected_tab_id);
}
self.viewport_state(tab_id)
.ok_or_else(|| anyhow!("viewport state for tab {tab_id} disappeared"))
}
/// In-process GUI equivalent of the wire claim. The GUI identity is
/// already registered with this mux, but there is no transport generation
/// involved because the call never crosses a connection.
pub fn claim_local_frontend_viewport(
&self,
client_id: &ClientId,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<FrontendViewportState> {
self.client_had_input(client_id);
self.claim_frontend_viewport(client_id, tab_id, viewport)
}
/// Validate a mode transition without mutating state. The server uses this
/// before atomically persisting the selected mode.
pub fn validate_registered_frontend_access_mode_change(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
mode: FrontendAccessMode,
tab_id: TabId,
viewport: &FrontendViewport,
) -> anyhow::Result<()> {
if self.client_registrations.read().get(client_id) != Some(®istration) {
anyhow::bail!("client connection was superseded");
}
self.validate_frontend_access_mode_change(client_id, mode, tab_id, viewport)
}
pub fn validate_frontend_access_mode_change(
&self,
client_id: &ClientId,
mode: FrontendAccessMode,
tab_id: TabId,
viewport: &FrontendViewport,
) -> anyhow::Result<()> {
self.validate_frontend_viewport(tab_id, viewport)?;
let lease = self.frontend_lease.lock();
if lease.access_mode == mode {
return Ok(());
}
let authorized = match lease.access_mode {
FrontendAccessMode::Handoff => lease.handoff_owner.as_ref() == Some(client_id),
FrontendAccessMode::TmuxLatest => {
lease
.tabs
.get(&tab_id)
.and_then(|state| state.owner.as_ref())
== Some(client_id)
}
};
if !authorized {
anyhow::bail!("only the current frontend owner may change access mode");
}
Ok(())
}
/// Commit a previously validated mode transition. The caller runs this on
/// the mux main thread after persistence succeeds.
pub fn set_registered_frontend_access_mode(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
mode: FrontendAccessMode,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<FrontendAccessState> {
if self.client_registrations.read().get(client_id) != Some(®istration) {
anyhow::bail!("client connection was superseded");
}
self.set_frontend_access_mode(client_id, mode, tab_id, viewport)
}
pub fn set_frontend_access_mode(
&self,
client_id: &ClientId,
mode: FrontendAccessMode,
tab_id: TabId,
viewport: FrontendViewport,
) -> anyhow::Result<FrontendAccessState> {
self.validate_frontend_access_mode_change(client_id, mode, tab_id, &viewport)?;
let prior_access = self.frontend_access_state();
if prior_access.mode == mode {
return Ok(self.frontend_access_state());
}
let affected_tabs = {
let mut lease = self.frontend_lease.lock();
lease.set_access_mode(mode);
lease.access_initialized = true;
match mode {
FrontendAccessMode::TmuxLatest => {
lease.set_handoff_owner(None);
}
FrontendAccessMode::Handoff => {
lease.set_handoff_owner(Some(client_id.clone()));
lease.handoff_ever_owned = true;
}
}
let mut affected = Vec::new();
for (affected_tab_id, state) in &mut lease.tabs {
if state.view.take().is_some() || *affected_tab_id == tab_id {
affected.push(*affected_tab_id);
}
}
let state = lease.tabs.entry(tab_id).or_default();
state.viewports.insert(client_id.clone(), viewport.clone());
state.set_owner(Some(client_id.clone()));
if !affected.contains(&tab_id) {
affected.push(tab_id);
}
affected
};
self.publish_frontend_access_state();
// As with a direct claim, mode/owner is authoritative before resize
// notifications expose geometry from the newly selected frontend.
if let Err(err) = self.apply_frontend_viewport(tab_id, &viewport) {
return Err(self.fail_closed_frontend_geometry(tab_id, Some(prior_access.mode), err));
}
for affected_tab_id in affected_tabs {
self.publish_frontend_viewport_state(affected_tab_id);
}
Ok(self.frontend_access_state())
}
/// `None` means the transport generation is stale. `Some(false)` means a
/// live non-owner submitted a legacy pane resize and it must be a no-op.
pub fn registered_client_may_resize_tab(
&self,
client_id: &ClientId,
registration: ClientRegistrationId,
tab_id: TabId,
) -> Option<bool> {
if self.client_registrations.read().get(client_id) != Some(®istration) {
return None;
}
let lease = self.frontend_lease.lock();
let owner = match lease.access_mode {
FrontendAccessMode::Handoff => lease.handoff_owner.as_ref(),
FrontendAccessMode::TmuxLatest => match lease.tabs.get(&tab_id) {
None => return Some(true),
Some(state) => state.owner.as_ref(),
},
};
let may = owner == Some(client_id);
if !may {
// Both ids in full: a client that believes it owns the viewport
// and is refused here is comparing them differently from us.
log::warn!("resize of tab {tab_id} refused: owner {owner:?}, asked by {client_id:?}");
}
Some(may)
}
fn publish_frontend_access_state(&self) {
let state = {
let mut lease = self.frontend_lease.lock();
let generation = lease.next_generation;
lease.next_generation = lease.next_generation.saturating_add(1);
lease.access_generation = generation;
Self::access_state_locked(&lease)
};
// Published from wherever the lease moved, which since connections
// run on their own threads includes a client's unregistration;
// subscribers are only ever run on the main thread.
self.notify_on_main_thread(MuxNotification::FrontendAccessChanged(state));
}
/// `notify`, from a thread that may not be the main one. Subscribers
/// assume the main thread (several spawn local tasks or reach
/// thread-local frontend state), and since connections run on their
/// own threads the lease moves from there too, when a client
/// unregisters.
fn notify_on_main_thread(&self, notification: MuxNotification) {
if self.is_main_thread() {
self.notify(notification);
return;
}
promise::spawn::spawn_into_main_thread(async move {
if let Some(mux) = Mux::try_get() {
mux.notify(notification);
}
})
.detach();
}
fn publish_frontend_viewport_state(&self, tab_id: TabId) {
let (generation, owner, view, access) = {
let mut lease = self.frontend_lease.lock();
let generation = lease.next_generation;
lease.next_generation = lease.next_generation.saturating_add(1);
let Some(state) = lease.tabs.get_mut(&tab_id) else {
return;
};
state.generation = generation;
let owner = state.owner.clone();
let view = state.view.clone();
let access = Self::access_state_locked(&lease);
(generation, owner, view, access)
};
let Some(tab) = self.get_tab(tab_id) else {
return;
};
self.notify_on_main_thread(MuxNotification::FrontendLeaseChanged(
FrontendViewportState {
tab_id,
owner,
canonical_size: tab.get_size(),
view,
generation,
access,
},
));
}
fn apply_frontend_viewport(
&self,
tab_id: TabId,
viewport: &FrontendViewport,
) -> anyhow::Result<()> {
let tab = self
.get_tab(tab_id)
.ok_or_else(|| anyhow!("no such tab {tab_id}"))?;
let native_frames = match viewport {
FrontendViewport::Native { panes, .. } => {
let frames = panes
.iter()
.map(|pane| (pane.pane_id, pane.frame))
.collect::<Vec<_>>();
let covers_all_stacks = tab.frontend_frames_cover_all_stacks(&frames)?;
Some((frames, covers_all_stacks))
}
FrontendViewport::CellGrid { .. } => None,
};
if crate::geometrytrace::trace_enabled() {
let incoming = match viewport {
FrontendViewport::CellGrid { size } => {
format!("kind=cellgrid in={}", crate::geometrytrace::size(size))
}
FrontendViewport::Native { size, panes } => format!(
"kind=native in={} covers_all={} in_panes=[{}]",
crate::geometrytrace::size(size),
native_frames
.as_ref()
.is_some_and(|(_, covers_all)| *covers_all),
panes
.iter()
.map(|pane| format!(
"{}:pty={} frame={}",
pane.pane_id,
crate::geometrytrace::size(&pane.size),
crate::geometrytrace::size(&pane.frame)
))
.collect::<Vec<_>>()
.join(" ")
),
};
crate::zoom_trace!(
"srv.viewport.recv tab={tab_id} {incoming} | {}",
tab.geometry_trace()
);
}
let mut transaction = self.begin_tab_geometry_transaction(tab_id);
let result = (|| {
let size = viewport.size();
self.frontend_geometry_step("resizing the tab root")?;
tab.resize(size);
if let FrontendViewport::Native { panes, .. } = viewport {
for pane_viewport in panes {
let pane = self
.get_pane(pane_viewport.pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_viewport.pane_id))?;
if !tab.contains_pane(pane_viewport.pane_id) {
anyhow::bail!("pane {} is not in tab {tab_id}", pane_viewport.pane_id);
}
self.frontend_geometry_step("resizing a native pane")?;
pane.resize(pane_viewport.size)?;
}
if let Some((frames, true)) = native_frames.as_ref() {
tab.rebuild_splits_sizes_from_frontend_frames(frames)?;
}
return Ok(());
}
for positioned in tab.iter_panes() {
let target = pane_size_for_cell_grid(size, positioned.width, positioned.height);
self.frontend_geometry_step("resizing a cell-grid pane")?;
positioned.pane.resize(target)?;
}
Ok(())
})();
if result.is_ok() {
transaction.commit();
}
crate::zoom_trace!(
"srv.viewport.done tab={tab_id} ok={} | {}",
result.is_ok(),
tab.geometry_trace()
);
result
}
#[cfg(test)]
fn frontend_geometry_step(&self, what: &str) -> anyhow::Result<()> {
if self
.frontend_geometry_failures
.lock()
.pop_front()
.unwrap_or(false)
{
anyhow::bail!("injected frontend geometry failure while {what}");
}
Ok(())
}
#[cfg(not(test))]
fn frontend_geometry_step(&self, _what: &str) -> anyhow::Result<()> {
Ok(())
}
fn fail_closed_frontend_geometry(
&self,
tab_id: TabId,
restore_mode: Option<FrontendAccessMode>,
apply_error: anyhow::Error,
) -> anyhow::Error {
let (mode, mut affected_tabs) = {
let mut lease = self.frontend_lease.lock();
if let Some(mode) = restore_mode {
lease.set_access_mode(mode);
lease.access_initialized = true;
}
let mode = lease.access_mode;
let mut affected = Vec::new();
match mode {
FrontendAccessMode::Handoff => {
// The screen whose geometry failed is the one that gets
// to come back without a click, not whoever drove
// before it.
lease.handoff_last_owner = lease.handoff_owner.clone();
lease.set_handoff_owner(None);
lease.handoff_ever_owned = true;
for (affected_tab_id, state) in &mut lease.tabs {
state.set_owner(None);
state.view = None;
affected.push(*affected_tab_id);
}
}
FrontendAccessMode::TmuxLatest => {
lease.set_handoff_owner(None);
let state = lease.tabs.entry(tab_id).or_default();
state.set_owner(None);
state.view = None;
affected.push(tab_id);
}
}
if !lease.tabs.contains_key(&tab_id) {
lease.tabs.entry(tab_id).or_default();
affected.push(tab_id);
}
(mode, affected)
};
affected_tabs.sort_unstable();
affected_tabs.dedup();
self.publish_frontend_access_state();
for affected_tab_id in affected_tabs {
self.publish_frontend_viewport_state(affected_tab_id);
}
self.notify(MuxNotification::TabResized(tab_id));
anyhow!(
"applying frontend geometry for tab {tab_id} failed; the {mode:?} lease was revoked: {apply_error:#}"
)
}
pub fn record_input_for_current_identity(&self) {
let Some(ident) = self.active_identity() else {
return;
};
self.client_had_input(&ident);
// The mode is read before the focused pane is resolved: that
// resolution walks every window and tab, and this runs for every
// keystroke to a remote pane.
if self.frontend_lease.lock().access_mode != FrontendAccessMode::TmuxLatest {
return;
}
let Some((_domain, _window, tab_id, _pane)) = self.resolve_focused_pane(&ident) else {
return;
};
let viewport = {
let lease = self.frontend_lease.lock();
if lease.access_mode != FrontendAccessMode::TmuxLatest {
return;
}
lease
.tabs
.get(&tab_id)
.and_then(|state| state.viewports.get(ident.as_ref()).cloned())
};
if let Some(viewport) = viewport {
if let Err(err) = self.claim_frontend_viewport(&ident, tab_id, viewport) {
log::error!("failed to claim local frontend viewport for tab {tab_id}: {err:#}");
}
}
}
pub fn record_focus_for_current_identity(&self, pane_id: PaneId) {
if let Some(ident) = self.identity.read().as_ref() {
self.record_focus_for_client(ident, pane_id);
}
}
pub fn resolve_focused_pane(
&self,
client_id: &ClientId,
) -> Option<(DomainId, WindowId, TabId, PaneId)> {
let pane_id = self.clients.read().get(client_id)?.focused_pane_id?;
let (domain, window, tab) = self.resolve_pane_id(pane_id)?;
Some((domain, window, tab, pane_id))
}
pub fn record_focus_for_client(&self, client_id: &ClientId, pane_id: PaneId) {
let mut prior = None;
if let Some(info) = self.clients.write().get_mut(client_id) {
prior = info.focused_pane_id;
info.update_focused_pane(pane_id);
}
if prior == Some(pane_id) {
return;
}
// Synthesize focus events
if let Some(prior_id) = prior {
if let Some(pane) = self.get_pane(prior_id) {
pane.focus_changed(false);
}
}
if let Some(pane) = self.get_pane(pane_id) {
pane.focus_changed(true);
}
}
/// Called by PaneFocused event handlers to reconcile a remote
/// pane focus event and apply its effects locally
pub fn focus_pane_and_containing_tab(&self, pane_id: PaneId) -> anyhow::Result<()> {
let pane = self
.get_pane(pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {pane_id} not found"))?;
let (_domain, window_id, tab_id) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow::anyhow!("can't find {pane_id} in the mux"))?;
// Focus/activate the containing tab within its window
{
let mut win = self
.get_window_mut(window_id)
.ok_or_else(|| anyhow::anyhow!("window_id {window_id} not found"))?;
let tab_idx = win
.idx_by_id(tab_id)
.ok_or_else(|| anyhow::anyhow!("tab {tab_id} not in {window_id}"))?;
win.save_and_then_set_active(tab_idx);
}
// Focus/activate the pane locally
let tab = self
.get_tab(tab_id)
.ok_or_else(|| anyhow::anyhow!("tab {tab_id} not found"))?;
tab.set_active_pane_silent(&pane);
Ok(())
}
pub fn register_client(&self, client_id: Arc<ClientId>) -> ClientRegistrationId {
let registration = ClientRegistrationId::new();
let key = (*client_id).clone();
{
// Registration generation and client metadata are one logical
// replacement. Keep the generation lock until both are visible so
// a stale Drop cannot remove the newly reconnected client between
// the two writes.
let mut registrations = self.client_registrations.write();
let mut clients = self.clients.write();
clients.insert(key.clone(), ClientInfo::new(client_id));
registrations.insert(key.clone(), registration);
}
registration
}
pub fn iter_clients(&self) -> Vec<ClientInfo> {
self.clients
.read()
.values()
.map(|info| info.clone())
.collect()
}
/// Returns a list of the unique workspace names known to the mux.
/// This is taken from all known windows.
pub fn iter_workspaces(&self) -> Vec<String> {
let mut names: Vec<String> = self
.windows
.read()
.values()
.map(|w| w.get_workspace().to_string())
.collect();
names.sort();
names.dedup();
names
}
/// Generate a new unique workspace name
pub fn generate_workspace_name(&self) -> String {
let used = self.iter_workspaces();
for candidate in names::Generator::default() {
if !used.contains(&candidate) {
return candidate;
}
}
unreachable!();
}
/// Returns the effective active workspace name
pub fn active_workspace(&self) -> String {
self.identity
.read()
.as_ref()
.and_then(|ident| {
self.clients
.read()
.get(&ident)
.and_then(|info| info.active_workspace.clone())
})
.unwrap_or_else(|| self.get_default_workspace())
}
/// Returns the effective active workspace name for a given client
pub fn active_workspace_for_client(&self, ident: &Arc<ClientId>) -> String {
self.clients
.read()
.get(&ident)
.and_then(|info| info.active_workspace.clone())
.unwrap_or_else(|| self.get_default_workspace())
}
/// The workspace a request from `client_id` should file new windows under.
///
/// Exactly equivalent to `active_workspace()` evaluated with `client_id`
/// installed as the global identity, but without touching that global.
/// Server handlers must resolve workspace attribution with this *before*
/// they await: an `IdentityHolder` held across a suspension point
/// publishes the wrong identity to every other main-thread task, and its
/// restore is not LIFO-safe.
pub fn active_workspace_for_optional_client(
&self,
client_id: Option<&Arc<ClientId>>,
) -> String {
match client_id {
Some(client_id) => self.active_workspace_for_client(client_id),
None => self.get_default_workspace(),
}
}
pub fn set_active_workspace_for_client(&self, ident: &Arc<ClientId>, workspace: &str) {
let mut clients = self.clients.write();
if let Some(info) = clients.get_mut(&ident) {
info.active_workspace.replace(workspace.to_string());
self.notify(MuxNotification::ActiveWorkspaceChanged(ident.clone()));
}
}
/// Assigns the active workspace name for the current identity
pub fn set_active_workspace(&self, workspace: &str) {
if let Some(ident) = self.identity.read().clone() {
self.set_active_workspace_for_client(&ident, workspace);
}
}
pub fn rename_workspace(&self, old_workspace: &str, new_workspace: &str) {
self.rename_workspace_impl(old_workspace, new_workspace, true);
}
/// Rename without announcing `WorkspaceRenamed`. The GUI follows that
/// announcement onto the renamed workspace — the right thing when a user
/// renames the workspace they are looking at, and exactly wrong for a
/// thread re-home that is about to switch the window elsewhere: the
/// queued follow would drag the window straight back to the moved
/// content. Windows still emit `WindowWorkspaceChanged` individually,
/// so per-window state keeps syncing.
pub fn rename_workspace_quietly(&self, old_workspace: &str, new_workspace: &str) {
self.rename_workspace_impl(old_workspace, new_workspace, false);
}
/// Move one window to `workspace` without announcing it. The other
/// windows of its workspace stay where they are.
pub fn move_window_to_workspace_quietly(&self, window_id: WindowId, workspace: &str) {
if let Some(mut window) = self.get_window_mut(window_id) {
window.set_workspace_quietly(workspace);
}
}
fn rename_workspace_impl(&self, old_workspace: &str, new_workspace: &str, announce: bool) {
if old_workspace == new_workspace {
return;
}
if announce {
self.notify(MuxNotification::WorkspaceRenamed {
old_workspace: old_workspace.to_string(),
new_workspace: new_workspace.to_string(),
});
}
for window in self.windows.write().values_mut() {
if window.get_workspace() == old_workspace {
window.set_workspace(new_workspace);
}
}
self.recompute_pane_count();
for client in self.clients.write().values_mut() {
if client.active_workspace.as_deref() == Some(old_workspace) {
client.active_workspace.replace(new_workspace.to_string());
self.notify(MuxNotification::ActiveWorkspaceChanged(
client.client_id.clone(),
));
}
}
}
/// Overrides the current client identity.
/// Returns `IdentityHolder` which will restore the prior identity
/// when it is dropped.
/// This can be used to change the identity for the duration of a block.
///
/// # This guard must not be held across an `.await`
///
/// The identity is a single process-global slot and `IdentityHolder`'s
/// restore is not LIFO-safe across interleaved tasks. Suspending with one
/// installed publishes the wrong identity to every other main-thread
/// task, including GUI mux subscribers (which run synchronously from
/// `notify`) and `record_input_for_current_identity`, which in
/// `TmuxLatest` mode will *claim* a tab's viewport for whoever happens to
/// be installed. Resolve what you need synchronously (e.g.
/// [`Mux::active_workspace_for_optional_client`]) and pass it as data.
/// `clippy.toml` arms `await_holding_invalid_type` for `IdentityHolder`.
pub fn with_identity(&self, id: Option<Arc<ClientId>>) -> IdentityHolder {
let prior = self.replace_identity(id);
IdentityHolder { prior }
}
/// Replace the identity, returning the prior identity
pub fn replace_identity(&self, id: Option<Arc<ClientId>>) -> Option<Arc<ClientId>> {
std::mem::replace(&mut *self.identity.write(), id)
}
/// Returns the active identity
pub fn active_identity(&self) -> Option<Arc<ClientId>> {
self.identity.read().clone()
}
/// Remove a client from the ordinary mux client list.
/// Palette ownership is connection-scoped and is cleaned independently.
pub fn unregister_client(&self, client_id: &ClientId, registration: ClientRegistrationId) {
{
let mut registrations = self.client_registrations.write();
if registrations.get(client_id) != Some(®istration) {
return;
}
self.clients.write().remove(client_id);
registrations.remove(client_id);
}
let (affected, access_changed, left) = {
let mut lease = self.frontend_lease.lock();
let left = lease.ever_rendered.remove(client_id);
let mut affected = Vec::new();
for (tab_id, state) in &mut lease.tabs {
state.viewports.remove(client_id);
if state.owner.as_ref() != Some(client_id) {
continue;
}
state.set_owner(None);
state.view = None;
affected.push(*tab_id);
}
let access_changed = lease.access_mode == FrontendAccessMode::Handoff
&& lease.handoff_owner.as_ref() == Some(client_id);
if access_changed {
// Do not select a surviving renderer automatically. Every
// remaining device stays opaque until one explicitly clicks,
// wheels or swipes the takeover surface -- except this very
// device coming back, which inherits (see handoff_last_owner).
lease.set_handoff_owner(None);
lease.handoff_ever_owned = true;
lease.handoff_last_owner = Some(client_id.clone());
}
(affected, access_changed, left)
};
if left {
// A connection is dropped on its own thread.
Mux::notify_from_any_thread(MuxNotification::ClientLeft(Arc::new(client_id.clone())));
}
if access_changed {
self.publish_frontend_access_state();
}
for tab_id in affected {
self.publish_frontend_viewport_state(tab_id);
}
}
/// Register one transport connection for palette advice. The returned
/// token must be captured by queued handlers so work from a disconnected
/// connection cannot be mistaken for a later connection with the same
/// `ClientId`.
pub fn register_palette_session(&self, client_id: &ClientId) -> PaletteSessionId {
self.palette_advisories.lock().register(client_id)
}
/// Remove exactly one transport connection and return any pane palette
/// selections that need to be applied as a result of owner fallback.
pub fn unregister_palette_session(
&self,
session_id: PaletteSessionId,
) -> Vec<PaletteSelectionChange> {
self.palette_advisories.lock().remove_session(session_id)
}
/// Immediately reject further queued work from a disconnected transport.
/// Final removal and pane fallback are deliberately a separate operation
/// so the caller can serialize their application on the mux main thread.
pub fn deactivate_palette_session(&self, session_id: PaletteSessionId) -> bool {
self.palette_advisories
.lock()
.deactivate_session(session_id)
}
/// Store a client's configured palette. A return value means that client
/// currently owns this pane and the server's OSC query base must update.
pub fn advise_client_palette(
&self,
session_id: PaletteSessionId,
pane_id: PaneId,
palette: ColorPalette,
) -> Option<ColorPalette> {
self.palette_advisories
.lock()
.advise(session_id, pane_id, palette)
}
/// Mark focus or input from a palette-capable client. Background clients
/// that have not supplied advice cannot take ownership.
pub fn activate_client_palette(
&self,
session_id: PaletteSessionId,
pane_id: PaneId,
) -> Option<ColorPalette> {
self.palette_advisories.lock().activate(session_id, pane_id)
}
pub fn subscribe<F>(&self, subscriber: F)
where
F: Fn(MuxNotification) -> bool + 'static + Send + Sync,
{
let sub_id = SUB_ID.fetch_add(1, Ordering::Relaxed);
self.subscribers
.write()
.insert(sub_id, Box::new(subscriber));
}
pub fn notify(&self, notification: MuxNotification) {
let removed_pane = match ¬ification {
MuxNotification::PaneRemoved(pane_id) => Some(*pane_id),
_ => None,
};
if let MuxNotification::PaneOutput(pane_id) = ¬ification {
// Record the durable fact before invoking subscribers. A native
// window can temporarily be unable to accept its notification,
// but it can later compare this generation with the last frame it
// successfully presented and recover the missing repaint.
self.pane_output_generations.lock().note_output(*pane_id);
}
match notification {
MuxNotification::TabResized(tab_id) => {
if self.tab_resize_notifications.lock().defer(tab_id) {
return;
}
self.notify_immediate(MuxNotification::TabResized(tab_id));
}
other => self.notify_immediate(other),
}
if let Some(pane_id) = removed_pane {
self.pane_output_generations.lock().remove(pane_id);
}
}
/// The latest PaneOutput generation published for this pane. Each GUI
/// window acknowledges generations independently after a successful
/// frame presentation.
pub fn pane_output_generation(&self, pane_id: PaneId) -> u64 {
self.pane_output_generations.lock().generation(pane_id)
}
fn notify_immediate(&self, notification: MuxNotification) {
thread_local! {
static NOTIFY_DEPTH: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
// The subscriber map is held (exclusively) while callbacks run, and
// the lock is not re-entrant: a subscriber that calls notify()
// synchronously deadlocks in release builds. Turn that hang into a
// debug panic with a stack trace; the fix is always to defer the
// nested notify via spawn_into_main_thread.
NOTIFY_DEPTH.with(|depth| {
debug_assert_eq!(
depth.get(),
0,
"re-entrant Mux::notify: a subscriber called notify() while \
the subscriber map was locked; defer the nested notify via \
spawn_into_main_thread (in release this deadlocks)"
);
depth.set(depth.get() + 1);
});
struct DepthGuard;
impl Drop for DepthGuard {
fn drop(&mut self) {
NOTIFY_DEPTH.with(|depth| depth.set(depth.get().saturating_sub(1)));
}
}
let _guard = DepthGuard;
let mut subscribers = self.subscribers.write();
subscribers.retain(|_, notify| notify(notification.clone()));
}
fn begin_tab_geometry_transaction(&self, tab_id: TabId) -> TabGeometryTransaction<'_> {
self.tab_resize_notifications.lock().begin(tab_id);
TabGeometryTransaction {
mux: self,
tab_id,
commit: false,
}
}
pub fn notify_from_any_thread(notification: MuxNotification) {
if let Some(mux) = Mux::try_get() {
if mux.is_main_thread() {
mux.notify(notification);
return;
}
}
promise::spawn::spawn_into_main_thread(async {
if let Some(mux) = Mux::try_get() {
mux.notify(notification);
}
})
.detach();
}
pub fn default_domain(&self) -> Arc<dyn Domain> {
self.default_domain.read().as_ref().map(Arc::clone).unwrap()
}
pub fn set_default_domain(&self, domain: &Arc<dyn Domain>) {
*self.default_domain.write() = Some(Arc::clone(domain));
}
pub fn get_domain(&self, id: DomainId) -> Option<Arc<dyn Domain>> {
self.domains.read().get(&id).cloned()
}
pub fn get_domain_by_name(&self, name: &str) -> Option<Arc<dyn Domain>> {
self.domains_by_name.read().get(name).cloned()
}
pub fn add_domain(&self, domain: &Arc<dyn Domain>) {
if self.default_domain.read().is_none() {
*self.default_domain.write() = Some(Arc::clone(domain));
}
self.domains
.write()
.insert(domain.domain_id(), Arc::clone(domain));
self.domains_by_name
.write()
.insert(domain.domain_name().to_string(), Arc::clone(domain));
}
pub fn set_mux(mux: &Arc<Mux>) {
MUX.lock().replace(Arc::clone(mux));
}
pub fn shutdown() {
MUX.lock().take();
}
pub fn get() -> Arc<Mux> {
Self::try_get().unwrap()
}
pub fn try_get() -> Option<Arc<Mux>> {
MUX.lock().as_ref().map(Arc::clone)
}
pub fn get_pane(&self, pane_id: PaneId) -> Option<Arc<dyn Pane>> {
self.panes.read().get(&pane_id).map(Arc::clone)
}
/// The program `pane_id` started has ended: remember how, for
/// `recent_pane_exit`, and tell the subscribers. Callable from any
/// thread, and with any lock held: the news is sent later, from the main
/// thread, and before the pane's removal if that comes first.
pub fn record_pane_exit(&self, pane_id: PaneId, exit: thinkterm_proto::PaneExit) {
{
let now = Instant::now();
let mut recent = self.recent_pane_exits.lock();
recent.retain(|(id, _, at)| {
*id != pane_id && now.duration_since(*at) < RECENT_PANE_EXIT_TTL
});
if recent.len() >= RECENT_PANE_EXITS {
recent.pop_front();
}
recent.push_back((pane_id, exit.clone(), now));
// Keep pending payloads independently of bounded exit history.
self.unannounced_pane_exits.lock().insert(pane_id, exit);
}
promise::spawn::spawn_into_main_thread(async move {
if let Some(mux) = Mux::try_get() {
mux.announce_pane_exit(pane_id, false);
}
})
.detach();
}
/// Send the news of a recorded exit, once: from the pane's removal,
/// just before its `PaneRemoved`, or later from the main thread. Later
/// is too late for a pane already removed -- every client has dropped
/// it -- and `recent_pane_exit` still has the status for `cli wait`.
fn announce_pane_exit(&self, pane_id: PaneId, removing: bool) {
let exit = match self.unannounced_pane_exits.lock().remove(&pane_id) {
Some(exit) => exit,
None => return,
};
if !removing && !self.panes.read().contains_key(&pane_id) {
return;
}
self.notify(MuxNotification::PaneExited { pane_id, exit });
}
/// How the program of a pane that has ended exited, while it is among
/// the last `RECENT_PANE_EXITS` to have done so, and recently.
pub fn recent_pane_exit(&self, pane_id: PaneId) -> Option<thinkterm_proto::PaneExit> {
self.recent_pane_exits
.lock()
.iter()
.rev()
.find(|(id, _, at)| *id == pane_id && at.elapsed() < RECENT_PANE_EXIT_TTL)
.map(|(_, exit, _)| exit.clone())
}
/// The label `pane_id` was given, if any.
pub fn pane_label(&self, pane_id: PaneId) -> Option<String> {
self.pane_labels.read().get(&pane_id).cloned()
}
/// Every label, by pane.
pub fn pane_labels(&self) -> HashMap<PaneId, String> {
self.pane_labels.read().clone()
}
/// Label `pane_id`, or with `None` or blank text take its label away;
/// see `normalized_pane_label`. Only a pane the mux has can be
/// labelled. Says whether anything changed; only a change is
/// announced, so a client and a server that tell each other about the
/// same label stop at once.
pub fn set_pane_label(&self, pane_id: PaneId, label: Option<&str>) -> bool {
self.change_pane_label(pane_id, label, false)
}
/// `set_pane_label` for a mirror, without telling the server that owns
/// the pane: what it reported, or a label told to it another way.
pub fn set_pane_label_from_server(&self, pane_id: PaneId, label: Option<&str>) -> bool {
self.change_pane_label(pane_id, label, true)
}
fn change_pane_label(&self, pane_id: PaneId, label: Option<&str>, from_server: bool) -> bool {
let label = label.and_then(normalized_pane_label);
let changed = {
let mut labels = self.pane_labels.write();
match &label {
Some(label) => {
if !self.panes.read().contains_key(&pane_id) {
return false;
}
labels.insert(pane_id, label.clone()).as_ref() != Some(label)
}
None => labels.remove(&pane_id).is_some(),
}
};
if changed {
self.notify(MuxNotification::PaneLabelChanged {
pane_id,
label,
from_server,
});
}
changed
}
pub fn get_tab(&self, tab_id: TabId) -> Option<Arc<Tab>> {
self.tabs.read().get(&tab_id).map(Arc::clone)
}
pub fn add_pane(&self, pane: &Arc<dyn Pane>) -> Result<(), Error> {
if self.panes.read().contains_key(&pane.pane_id()) {
return Ok(());
}
let clipboard: Arc<dyn Clipboard> = Arc::new(MuxClipboard {
pane_id: pane.pane_id(),
});
pane.set_clipboard(&clipboard);
let downloader: Arc<dyn DownloadHandler> = Arc::new(MuxDownloader {});
pane.set_download_handler(&downloader);
self.panes.write().insert(pane.pane_id(), Arc::clone(pane));
let pane_id = pane.pane_id();
if let Some(reader) = pane.reader()? {
let banner = self.banner.read().clone();
#[cfg(unix)]
let pty_fd = pane.pty_raw_fd();
#[cfg(not(unix))]
let pty_fd = None;
let pane = Arc::downgrade(pane);
thread::spawn(move || read_from_pane_pty(pane, banner, reader, pty_fd));
}
self.recompute_pane_count();
self.notify(MuxNotification::PaneAdded(pane_id));
Ok(())
}
pub fn add_tab_no_panes(&self, tab: &Arc<Tab>) {
self.tabs.write().insert(tab.tab_id(), Arc::clone(tab));
self.recompute_pane_count();
}
pub fn add_tab_and_active_pane(&self, tab: &Arc<Tab>) -> Result<(), Error> {
self.tabs.write().insert(tab.tab_id(), Arc::clone(tab));
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("tab MUST have an active pane"))?;
self.add_pane(&pane)
}
fn remove_pane_internal(&self, pane_id: PaneId) {
log::debug!("removing pane {}", pane_id);
self.palette_advisories.lock().remove_pane(pane_id);
crate::pane::set_frontend_cell_metrics(pane_id, None);
let mut changed = false;
// Bind the removal first: in edition 2018 the `if let` scrutinee's
// temporary (the panes write guard) would otherwise live across the
// notify below, deadlocking any subscriber that touches the pane map.
let removed = self.panes.write().remove(&pane_id);
self.pane_labels.write().remove(&pane_id);
if let Some(pane) = removed {
log::debug!("killing pane {}", pane_id);
pane.kill();
// How its program ended goes out first, if that is still to go.
self.announce_pane_exit(pane_id, true);
self.notify(MuxNotification::PaneRemoved(pane_id));
changed = true;
}
if changed {
self.recompute_pane_count();
}
}
fn remove_tab_internal(&self, tab_id: TabId) -> Option<Arc<Tab>> {
log::debug!("remove_tab_internal tab {}", tab_id);
let tab = self.tabs.write().remove(&tab_id)?;
self.frontend_lease.lock().tabs.remove(&tab_id);
if let Some(mut windows) = self.windows.try_write() {
for w in windows.values_mut() {
w.remove_by_id(tab_id);
}
}
let mut pane_ids = vec![];
for pane in tab.iter_all_panes() {
pane_ids.push(pane.pane_id());
}
log::debug!("panes to remove: {pane_ids:?}");
for pane_id in pane_ids {
self.remove_pane_internal(pane_id);
}
self.recompute_pane_count();
Some(tab)
}
fn remove_window_internal(&self, window_id: WindowId) {
log::debug!("remove_window_internal {}", window_id);
let window = self.windows.write().remove(&window_id);
if let Some(window) = window {
// Gather all the domains referenced by this window
let mut domains_of_window = HashSet::new();
for tab in window.iter() {
for pane in tab.iter_all_panes() {
domains_of_window.insert(pane.domain_id());
}
}
for domain_id in domains_of_window {
// Detach only when NO surviving window still references the
// domain. One thread's workspace window going away must not
// sever a client connection that other threads in the same
// Space are still displaying — that detach ripples out as
// "every pane of the domain removed", which empties every
// window and takes the whole process down with it.
let still_referenced = self.windows.read().values().any(|win| {
win.iter().any(|tab| {
tab.iter_all_panes()
.iter()
.any(|pane| pane.domain_id() == domain_id)
})
});
if still_referenced {
continue;
}
if let Some(domain) = self.get_domain(domain_id) {
if domain.detachable() {
log::info!("detaching domain");
if let Err(err) = domain.detach() {
log::error!(
"while detaching domain {domain_id} {}: {err:#}",
domain.domain_name()
);
}
}
}
}
for tab in window.iter() {
self.remove_tab_internal(tab.tab_id());
}
self.notify(MuxNotification::WindowRemoved(window_id));
}
self.recompute_pane_count();
}
pub fn remove_pane(&self, pane_id: PaneId) {
self.remove_pane_internal(pane_id);
self.prune_dead_windows();
}
pub fn remove_tab(&self, tab_id: TabId) -> Option<Arc<Tab>> {
let tab = self.remove_tab_internal(tab_id);
self.prune_dead_windows();
tab
}
pub fn prune_dead_windows(&self) {
if Activity::count() > 0 {
log::trace!("prune_dead_windows: Activity::count={}", Activity::count());
return;
}
let live_tab_ids: Vec<TabId> = self.tabs.read().keys().cloned().collect();
let mut dead_windows = vec![];
let dead_tab_ids: Vec<TabId>;
{
let mut windows = match self.windows.try_write() {
Some(w) => w,
None => {
// Our caller already holds the lock. "Later" must
// actually be scheduled: a mirror reaped from `panes`
// but skipped here stays in its tab tree — painted,
// is_dead()==false, yet unreachable via get_pane —
// which freezes it and turns every push for it into a
// full resync. One retry chain at a time: a bulk
// teardown hits this branch once per pane. (The
// Activity::count guard above needs no reschedule of
// its own — every Activity::drop schedules a prune.)
log::trace!("prune_dead_windows: self.windows already borrowed");
static RETRY_PENDING: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
if promise::spawn::is_scheduler_configured()
&& !RETRY_PENDING.swap(true, Ordering::AcqRel)
{
promise::spawn::spawn_into_main_thread(async {
smol::Timer::after(std::time::Duration::from_millis(100)).await;
RETRY_PENDING.store(false, Ordering::Release);
if let Some(mux) = Mux::try_get() {
mux.prune_dead_windows();
}
})
.detach();
}
return;
}
};
for (window_id, win) in windows.iter_mut() {
win.prune_dead_tabs(&live_tab_ids);
if win.is_empty() {
log::trace!("prune_dead_windows: window is now empty");
dead_windows.push(*window_id);
}
}
dead_tab_ids = self
.tabs
.read()
.iter()
.filter_map(|(&id, tab)| if tab.is_dead() { Some(id) } else { None })
.collect();
}
for tab_id in dead_tab_ids {
log::trace!("tab {} is dead", tab_id);
self.remove_tab_internal(tab_id);
}
for window_id in dead_windows {
log::trace!("window {} is dead", window_id);
self.remove_window_internal(window_id);
}
if self.is_empty() {
log::trace!("prune_dead_windows: is_empty, send MuxNotification::Empty");
self.notify(MuxNotification::Empty);
} else {
log::trace!("prune_dead_windows: not empty");
}
}
pub fn kill_window(&self, window_id: WindowId) {
self.remove_window_internal(window_id);
self.prune_dead_windows();
}
pub fn get_window(&self, window_id: WindowId) -> Option<MappedRwLockReadGuard<'_, Window>> {
// One acquisition: a check under one guard and an unwrap under the
// next was safe only while the main thread was the sole caller.
// Connection threads read windows now, and a window removed in the
// gap between the two would have been an unwrap of None.
let windows = self.windows.read();
if !windows.contains_key(&window_id) {
return None;
}
Some(RwLockReadGuard::map(windows, |windows| {
windows.get(&window_id).unwrap()
}))
}
pub fn get_window_mut(
&self,
window_id: WindowId,
) -> Option<MappedRwLockWriteGuard<'_, Window>> {
let windows = self.windows.write();
if !windows.contains_key(&window_id) {
return None;
}
Some(RwLockWriteGuard::map(windows, |windows| {
windows.get_mut(&window_id).unwrap()
}))
}
pub fn get_active_tab_for_window(&self, window_id: WindowId) -> Option<Arc<Tab>> {
let window = self.get_window(window_id)?;
window.get_active().map(Arc::clone)
}
pub fn new_empty_window(
&self,
workspace: Option<String>,
position: Option<GuiPosition>,
) -> MuxWindowBuilder {
self.new_empty_window_for_domain(workspace, position, None)
}
/// Like `new_empty_window`, but tags the window with the domain that is
/// creating it. The tag is set before the window becomes observable via
/// mux notifications, so the GUI can reliably distinguish domain-owned
/// windows (remote mux windows, tmux) from user-initiated ones.
pub fn new_empty_window_for_domain(
&self,
workspace: Option<String>,
position: Option<GuiPosition>,
origin_domain: Option<DomainId>,
) -> MuxWindowBuilder {
let window = Window::new(workspace, position, origin_domain);
let window_id = window.window_id();
self.windows.write().insert(window_id, window);
MuxWindowBuilder {
window_id,
activity: Some(Activity::new()),
notified: false,
}
}
/// Register a window built elsewhere, under its own id (one adopted
/// from a predecessor server). The builder announces it like a new one.
pub fn insert_window(&self, window: Window) -> MuxWindowBuilder {
let window_id = window.window_id();
self.windows.write().insert(window_id, window);
MuxWindowBuilder {
window_id,
activity: Some(Activity::new()),
notified: false,
}
}
pub fn add_tab_to_window(&self, tab: &Arc<Tab>, window_id: WindowId) -> anyhow::Result<()> {
let tab_id = tab.tab_id();
{
let mut window = self
.get_window_mut(window_id)
.ok_or_else(|| anyhow!("add_tab_to_window: no such window_id {}", window_id))?;
window.push(tab);
}
self.recompute_pane_count();
self.notify(MuxNotification::TabAddedToWindow { tab_id, window_id });
Ok(())
}
pub fn register_window_ui_surface(
&self,
window_id: WindowId,
surface_id: WindowUiSurfaceId,
) -> bool {
let changed = {
let mut windows = self.windows.write();
let Some(window) = windows.get_mut(&window_id) else {
return false;
};
window.add_ui_surface(surface_id)
};
if changed {
self.notify(MuxNotification::WindowInvalidated(window_id));
}
changed
}
pub fn unregister_window_ui_surface(&self, window_id: WindowId, surface_id: &str) -> bool {
let changed = {
let mut windows = self.windows.write();
let Some(window) = windows.get_mut(&window_id) else {
return false;
};
window.remove_ui_surface(surface_id)
};
if changed {
self.notify(MuxNotification::WindowInvalidated(window_id));
self.prune_dead_windows();
}
changed
}
pub fn window_containing_tab(&self, tab_id: TabId) -> Option<WindowId> {
for w in self.windows.read().values() {
for t in w.iter() {
if t.tab_id() == tab_id {
return Some(w.window_id());
}
}
}
None
}
pub fn is_empty(&self) -> bool {
self.panes.read().is_empty()
&& self
.windows
.read()
.values()
.all(|window| window.ui_surface_count() == 0)
}
pub fn is_workspace_empty(&self, workspace: &str) -> bool {
*self
.num_panes_by_workspace
.read()
.get(workspace)
.unwrap_or(&0)
== 0
}
pub fn is_active_workspace_empty(&self) -> bool {
let workspace = self.active_workspace();
self.is_workspace_empty(&workspace)
}
pub fn iter_panes(&self) -> Vec<Arc<dyn Pane>> {
self.panes
.read()
.iter()
.map(|(_, v)| Arc::clone(v))
.collect()
}
pub fn iter_windows_in_workspace(&self, workspace: &str) -> Vec<WindowId> {
let mut windows: Vec<WindowId> = self
.windows
.read()
.iter()
.filter_map(|(k, w)| {
if w.get_workspace() == workspace {
Some(k)
} else {
None
}
})
.cloned()
.collect();
windows.sort();
windows
}
pub fn iter_windows(&self) -> Vec<WindowId> {
self.windows.read().keys().cloned().collect()
}
pub fn iter_domains(&self) -> Vec<Arc<dyn Domain>> {
self.domains.read().values().cloned().collect()
}
pub fn resolve_pane_id(&self, pane_id: PaneId) -> Option<(DomainId, WindowId, TabId)> {
let mut ids = None;
'tabs: for tab in self.tabs.read().values() {
for pane in tab.iter_all_panes() {
if pane.pane_id() == pane_id {
ids = Some((tab.tab_id(), pane.domain_id()));
break 'tabs;
}
}
}
let (tab_id, domain_id) = ids?;
let window_id = self.window_containing_tab(tab_id)?;
Some((domain_id, window_id, tab_id))
}
pub fn pane_stack_tabs(&self, pane_id: PaneId) -> Vec<crate::tab::PaneStackTab> {
let Some((_domain_id, _window_id, tab_id)) = self.resolve_pane_id(pane_id) else {
return vec![];
};
self.get_tab(tab_id)
.map(|tab| tab.pane_stack_tabs(pane_id))
.unwrap_or_default()
}
pub fn pane_stack_id(&self, pane_id: PaneId) -> Option<crate::tab::PaneStackId> {
let (_domain_id, _window_id, tab_id) = self.resolve_pane_id(pane_id)?;
self.get_tab(tab_id)?.pane_stack_id(pane_id)
}
pub fn activate_pane_in_stack(&self, pane_id: PaneId) -> anyhow::Result<()> {
let (_domain_id, _window_id, tab_id) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow!("pane_id {} invalid", pane_id))?;
let tab = self
.get_tab(tab_id)
.ok_or_else(|| anyhow!("tab_id {} invalid", tab_id))?;
// A zoomed stack gives the page it switches to the whole tab. The
// frontend driving the tab has already sized every page for that,
// with each page's own font scale and nav bar; the raw tab size was
// only put back by that frontend later, the page meanwhile drawn at
// the wrong width.
let zoomed_size = driving_frontend_size_over_root(
&self.frontend_lease.lock(),
tab_id,
pane_id,
tab.get_size(),
);
tab.activate_pane_in_stack_sized(pane_id, zoomed_size)?;
Ok(())
}
pub fn domain_was_detached(&self, domain: DomainId) {
let mut dead_panes = vec![];
for pane in self.panes.read().values() {
if pane.domain_id() == domain {
dead_panes.push(pane.pane_id());
}
}
{
let mut windows = self.windows.write();
for (_, win) in windows.iter_mut() {
for tab in win.iter() {
tab.kill_panes_in_domain(domain);
}
}
}
log::info!("domain detached panes: {:?}", dead_panes);
for pane_id in dead_panes {
self.remove_pane_internal(pane_id);
}
self.prune_dead_windows();
}
pub fn set_banner(&self, banner: Option<String>) {
*self.banner.write() = banner;
}
pub fn resolve_spawn_tab_domain(
&self,
// TODO: disambiguate with TabId
pane_id: Option<PaneId>,
domain: &config::keyassignment::SpawnTabDomain,
) -> anyhow::Result<Arc<dyn Domain>> {
let domain = match domain {
SpawnTabDomain::DefaultDomain => self.default_domain(),
SpawnTabDomain::CurrentPaneDomain => match pane_id {
Some(pane_id) => {
let (pane_domain_id, _window_id, _tab_id) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow!("pane_id {} invalid", pane_id))?;
self.get_domain(pane_domain_id)
.expect("resolve_pane_id to give valid domain_id")
}
None => self.default_domain(),
},
SpawnTabDomain::DomainId(domain_id) => self
.get_domain(*domain_id)
.ok_or_else(|| anyhow!("domain id {} is invalid", domain_id))?,
SpawnTabDomain::DomainName(name) => {
self.get_domain_by_name(&name).ok_or_else(|| {
let names: Vec<String> = self
.domains_by_name
.read()
.keys()
.map(|name| format!("\"{name}\""))
.collect();
anyhow!(
"domain name \"{name}\" is invalid. Possible names are {}.",
names.join(", ")
)
})?
}
};
Ok(domain)
}
fn resolve_cwd(
&self,
command_dir: Option<String>,
pane: Option<Arc<dyn Pane>>,
target_domain: DomainId,
policy: CachePolicy,
) -> Option<String> {
command_dir.or_else(|| {
match pane {
// Without waiting for the pane's parser: this is the
// spawn path, on the main thread. The policy still
// applies to the divined directory, so a spawn right
// after a `cd` finds the new one.
Some(pane) if pane.domain_id() == target_domain => pane
.working_dir_without_waiting(policy)
.and_then(|url| {
percent_decode_str(url.path())
.decode_utf8()
.ok()
.map(|path| path.into_owned())
})
.map(|path| {
// On Windows the file URI can produce a path like:
// `/C:\Users` which is valid in a file URI, but the leading slash
// is not liked by the windows file APIs, so we strip it off here.
let bytes = path.as_bytes();
if bytes.len() > 2 && bytes[0] == b'/' && bytes[2] == b':' {
path[1..].to_owned()
} else {
path
}
}),
_ => None,
}
})
}
/// Move an already-registered pane into a new split without ever
/// leaving it detached if the insertion fails.
pub fn move_pane_to_split(
&self,
src_pane_id: PaneId,
target_tab_id: TabId,
target_pane_id: PaneId,
request: SplitRequest,
) -> anyhow::Result<Arc<dyn Pane>> {
if src_pane_id == target_pane_id {
anyhow::bail!("cannot split pane {src_pane_id} relative to itself");
}
let target_tab = self
.get_tab(target_tab_id)
.ok_or_else(|| anyhow!("tab_id {target_tab_id} is invalid"))?;
let target_index = target_tab
.pane_index_for_pane(target_pane_id)
.ok_or_else(|| anyhow!("pane_id {target_pane_id} is not in tab {target_tab_id}"))?;
target_tab
.validate_split_request(target_index, request)
.with_context(|| format!("cannot split pane {target_pane_id}"))?;
// This is a local topology primitive and does not require either tab
// to be attached to a window yet.
let src_tab = self
.tabs
.read()
.values()
.find(|tab| tab.pane_index_for_pane(src_pane_id).is_some())
.cloned()
.ok_or_else(|| anyhow!("pane {src_pane_id} not found in any tab"))?;
let src_tab_id = src_tab.tab_id();
if Arc::ptr_eq(&src_tab, &target_tab) {
return src_tab.move_pane_to_split(src_pane_id, target_pane_id, request);
}
let pane = src_tab.remove_pane(src_pane_id).ok_or_else(|| {
anyhow!("pane {src_pane_id} not found in its containing tab {src_tab_id}")
})?;
// Removing a pane can renumber the destination leaves when source
// and target are in the same tab.
let final_target_index = match target_tab.pane_index_for_pane(target_pane_id) {
Some(index) => index,
None => {
src_tab.rehome_orphan_pane(&pane);
anyhow::bail!("target pane {target_pane_id} vanished while moving pane");
}
};
if let Err(err) =
target_tab.split_and_insert(final_target_index, request, Arc::clone(&pane))
{
src_tab.rehome_orphan_pane(&pane);
return Err(err);
}
// split_and_insert only selects the inserted pane when it is the
// right/bottom half. A move is an explicit user action, so make the
// moved pane active for all four directions. Doing this here also
// makes the server's authoritative tree agree with client mirrors
// before it is serialized for a resync.
target_tab.set_active_pane(&pane);
if src_tab.is_dead() {
self.remove_tab(src_tab_id);
}
Ok(pane)
}
pub async fn split_pane(
&self,
// TODO: disambiguate with TabId
pane_id: PaneId,
request: SplitRequest,
source: SplitSource,
domain: config::keyassignment::SpawnTabDomain,
) -> anyhow::Result<(Arc<dyn Pane>, TerminalSize)> {
let (_pane_domain_id, window_id, tab_id) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow!("pane_id {} invalid", pane_id))?;
let domain = self
.resolve_spawn_tab_domain(Some(pane_id), &domain)
.context("resolve_spawn_tab_domain")?;
if domain.state() == DomainState::Detached {
domain.attach(Some(window_id)).await?;
}
let current_pane = self
.get_pane(pane_id)
.ok_or_else(|| anyhow!("pane_id {} is invalid", pane_id))?;
let term_config = current_pane.get_config();
let source = match source {
SplitSource::Spawn {
command,
command_dir,
} => SplitSource::Spawn {
command,
command_dir: self.resolve_cwd(
command_dir,
Some(Arc::clone(¤t_pane)),
domain.domain_id(),
CachePolicy::FetchImmediate,
),
},
other => other,
};
let pane = domain.split_pane(source, tab_id, pane_id, request).await?;
// A moved pane is already registered, so PaneAdded cannot advertise
// the changed tree to mux clients. Always publish the structural
// mutation; clients coalesce it into an authoritative resync.
self.notify(MuxNotification::TabResized(tab_id));
if let Some(config) = term_config {
pane.set_config(config);
}
// FIXME: clipboard
let dims = pane.get_dimensions();
let size = TerminalSize {
cols: dims.cols,
rows: dims.viewport_rows,
pixel_height: 0, // FIXME: split pane pixel dimensions
pixel_width: 0,
dpi: dims.dpi,
};
Ok((pane, size))
}
pub async fn spawn_pane_in_stack(
&self,
pane_id: PaneId,
domain: SpawnTabDomain,
command: Option<CommandBuilder>,
command_dir: Option<String>,
size: TerminalSize,
) -> anyhow::Result<Arc<dyn Pane>> {
let (pane_domain_id, window_id, tab_id) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow!("pane_id {} invalid", pane_id))?;
let tab = self
.get_tab(tab_id)
.ok_or_else(|| anyhow!("tab_id {} invalid", tab_id))?;
if tab.pane_index_for_pane(pane_id).is_none() {
anyhow::bail!("pane_id {} is not in tab {}", pane_id, tab_id);
}
let domain = self
.resolve_spawn_tab_domain(Some(pane_id), &domain)
.context("resolve_spawn_tab_domain")?;
if domain.state() == DomainState::Detached {
domain.attach(Some(window_id)).await?;
}
let current_pane = self
.get_pane(pane_id)
.ok_or_else(|| anyhow!("pane_id {} is invalid", pane_id))?;
let term_config = current_pane.get_config();
let cwd = self.resolve_cwd(
command_dir,
if pane_domain_id == domain.domain_id() {
Some(current_pane)
} else {
None
},
domain.domain_id(),
CachePolicy::FetchImmediate,
);
let pane = domain
.spawn_pane_in_stack(pane_id, size, command.clone(), cwd.clone())
.await
.with_context(|| {
format!(
"Spawning pane in domain `{}`: {size:?} command={command:?} cwd={cwd:?}",
domain.domain_name()
)
})?;
if let Some(config) = term_config {
pane.set_config(config);
}
tab.add_pane_to_stack(pane_id, Arc::clone(&pane))?;
Ok(pane)
}
/// Move an existing pane into another pane stack in the same tab.
/// The domain owns the mutation so proxy domains can update their
/// authoritative server before the next resync.
pub async fn move_pane_to_stack(
&self,
src_pane_id: PaneId,
target_pane_id: PaneId,
) -> anyhow::Result<Arc<dyn Pane>> {
if src_pane_id == target_pane_id {
anyhow::bail!("cannot move pane {src_pane_id} onto itself");
}
let (src_domain_id, _src_window_id, src_tab_id) = self
.resolve_pane_id(src_pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {src_pane_id} not found"))?;
let (target_domain_id, _target_window_id, target_tab_id) = self
.resolve_pane_id(target_pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {target_pane_id} not found"))?;
if src_domain_id != target_domain_id {
anyhow::bail!(
"cannot move pane {src_pane_id} from domain {src_domain_id} \
into domain {target_domain_id}"
);
}
if src_tab_id != target_tab_id {
anyhow::bail!(
"cannot move pane {src_pane_id} from tab {src_tab_id} \
into stack in tab {target_tab_id}"
);
}
let domain = self
.get_domain(target_domain_id)
.ok_or_else(|| anyhow::anyhow!("domain {target_domain_id} not found"))?;
domain
.move_pane_to_stack(src_pane_id, target_tab_id, target_pane_id)
.await
}
/// Swap two panes of the same tab, each into the other's place; the
/// first is left active. The domain owns the mutation, as for
/// `move_pane_to_stack`.
pub async fn swap_panes(&self, pane_id: PaneId, other_pane_id: PaneId) -> anyhow::Result<()> {
if pane_id == other_pane_id {
anyhow::bail!("cannot swap pane {pane_id} with itself");
}
let (domain_id, _window_id, tab_id) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {pane_id} not found"))?;
let (other_domain_id, _other_window_id, other_tab_id) = self
.resolve_pane_id(other_pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {other_pane_id} not found"))?;
if domain_id != other_domain_id || tab_id != other_tab_id {
anyhow::bail!(
"cannot swap pane {pane_id} with pane {other_pane_id}: \
they are not in the same tab of one domain"
);
}
let domain = self
.get_domain(domain_id)
.ok_or_else(|| anyhow::anyhow!("domain {domain_id} not found"))?;
domain.swap_panes(tab_id, pane_id, other_pane_id).await
}
pub async fn move_pane_to_new_tab(
&self,
pane_id: PaneId,
window_id: Option<WindowId>,
workspace_for_new_window: Option<String>,
) -> anyhow::Result<(Arc<Tab>, WindowId)> {
let (domain_id, _src_window, src_tab) = self
.resolve_pane_id(pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {} not found", pane_id))?;
let domain = self
.get_domain(domain_id)
.ok_or_else(|| anyhow::anyhow!("domain {domain_id} of pane {pane_id} not found"))?;
if let Some((tab, window_id)) = domain
.move_pane_to_new_tab(pane_id, window_id, workspace_for_new_window.clone())
.await?
{
return Ok((tab, window_id));
}
let src_tab = match self.get_tab(src_tab) {
Some(t) => t,
None => anyhow::bail!("Invalid tab id {}", src_tab),
};
let window_builder;
let (window_id, size) = if let Some(window_id) = window_id {
let window = self
.get_window_mut(window_id)
.ok_or_else(|| anyhow!("window_id {} not found on this server", window_id))?;
let tab = window
.get_active()
.ok_or_else(|| anyhow!("window {} has no tabs", window_id))?;
let size = tab.get_size();
(window_id, size)
} else {
window_builder = self.new_empty_window(workspace_for_new_window, None);
(*window_builder, src_tab.get_size())
};
let pane = src_tab
.remove_pane(pane_id)
.ok_or_else(|| anyhow::anyhow!("pane {} wasn't in its containing tab!?", pane_id))?;
let tab = Arc::new(Tab::new(&size));
tab.assign_pane(&pane);
pane.resize(size)?;
self.add_tab_and_active_pane(&tab)?;
self.add_tab_to_window(&tab, window_id)?;
if src_tab.is_dead() {
self.remove_tab(src_tab.tab_id());
}
Ok((tab, window_id))
}
pub async fn spawn_tab_or_window(
&self,
window_id: Option<WindowId>,
domain: SpawnTabDomain,
command: Option<CommandBuilder>,
command_dir: Option<String>,
size: TerminalSize,
current_pane_id: Option<PaneId>,
workspace_for_new_window: String,
window_position: Option<GuiPosition>,
) -> anyhow::Result<(Arc<Tab>, Arc<dyn Pane>, WindowId)> {
let domain = self
.resolve_spawn_tab_domain(current_pane_id, &domain)
.context("resolve_spawn_tab_domain")?;
let window_builder;
let (window_id, size, term_config) = if let Some(window_id) = window_id {
let context = self.existing_window_spawn_context(window_id, size)?;
(window_id, context.size, context.term_config)
} else {
window_builder = self.new_empty_window(Some(workspace_for_new_window), window_position);
(*window_builder, size, None)
};
if domain.state() == DomainState::Detached {
domain.attach(Some(window_id)).await?;
}
let cwd = self.resolve_cwd(
command_dir,
match current_pane_id {
Some(id) => {
// Only use the cwd from the current pane if the domain
// is the same as the one we are spawning into
let (current_domain_id, _, _) = self
.resolve_pane_id(id)
.ok_or_else(|| anyhow!("pane_id {} invalid", id))?;
if current_domain_id == domain.domain_id() {
self.get_pane(id)
} else {
None
}
}
None => None,
},
domain.domain_id(),
CachePolicy::FetchImmediate,
);
let tab = domain
.spawn(size, command.clone(), cwd.clone(), window_id)
.await
.with_context(|| {
format!(
"Spawning in domain `{}`: {size:?} command={command:?} cwd={cwd:?}",
domain.domain_name()
)
})?;
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("missing active pane on tab!?"))?;
if let Some(config) = term_config {
pane.set_config(config);
}
// FIXME: clipboard?
let mut window = self
.get_window_mut(window_id)
.ok_or_else(|| anyhow!("no such window!?"))?;
if let Some(idx) = window.idx_by_id(tab.tab_id()) {
window.save_and_then_set_active(idx);
}
Ok((tab, pane, window_id))
}
fn existing_window_spawn_context(
&self,
window_id: WindowId,
requested_size: TerminalSize,
) -> anyhow::Result<ExistingWindowSpawnContext> {
let window = self
.get_window_mut(window_id)
.ok_or_else(|| anyhow!("window_id {} not found on this server", window_id))?;
let Some(tab) = window.get_active() else {
return Ok(ExistingWindowSpawnContext {
size: requested_size,
term_config: None,
});
};
let size = tab.get_size();
let term_config = tab.get_active_pane().and_then(|pane| pane.get_config());
Ok(ExistingWindowSpawnContext { size, term_config })
}
}
impl Drop for Mux {
fn drop(&mut self) {
if let Some(service) = self.connect_service.get_mut().take() {
service.close();
}
}
}
fn pane_size_for_cell_grid(size: TerminalSize, cols: usize, rows: usize) -> TerminalSize {
let cols = cols.max(1);
let rows = rows.max(1);
let cell_width = (size.pixel_width != 0)
.then(|| size.pixel_width / size.cols.max(1))
.map(|width| width.max(1));
let cell_height = (size.pixel_height != 0)
.then(|| size.pixel_height / size.rows.max(1))
.map(|height| height.max(1));
TerminalSize {
rows,
cols,
pixel_width: cell_width.map_or(0, |width| cols.saturating_mul(width)),
pixel_height: cell_height.map_or(0, |height| rows.saturating_mul(height)),
dpi: size.dpi,
}
}
pub struct IdentityHolder {
prior: Option<Arc<ClientId>>,
}
impl Drop for IdentityHolder {
fn drop(&mut self) {
if let Some(mux) = Mux::try_get() {
mux.replace_identity(self.prior.take());
}
}
}
#[derive(Debug, Error)]
#[allow(dead_code)]
pub enum SessionTerminated {
#[error("Process exited: {:?}", status)]
ProcessStatus { status: ExitStatus },
#[error("Error: {:?}", err)]
Error { err: Error },
#[error("Window Closed")]
WindowClosed,
}
pub(crate) fn terminal_size_to_pty_size(size: TerminalSize) -> anyhow::Result<PtySize> {
Ok(PtySize {
rows: size.rows.try_into()?,
cols: size.cols.try_into()?,
pixel_height: size.pixel_height.try_into()?,
pixel_width: size.pixel_width.try_into()?,
})
}
struct MuxClipboard {
pane_id: PaneId,
}
impl Clipboard for MuxClipboard {
fn set_contents(
&self,
selection: ClipboardSelection,
clipboard: Option<String>,
) -> anyhow::Result<()> {
let mux =
Mux::try_get().ok_or_else(|| anyhow::anyhow!("MuxClipboard::set_contents: no Mux?"))?;
mux.notify(MuxNotification::AssignClipboard {
pane_id: self.pane_id,
selection,
clipboard,
});
Ok(())
}
}
struct MuxDownloader {}
impl wezterm_term::DownloadHandler for MuxDownloader {
fn save_to_downloads(&self, name: Option<String>, data: Vec<u8>) {
if let Some(mux) = Mux::try_get() {
mux.notify(MuxNotification::SaveToDownloads {
name,
data: Arc::new(data),
});
}
}
}
/// How often every pane checks for an abandoned image transfer, and how
/// many quiet sweeps release its fragments. Completed kitty images remain
/// reusable; explicit deletion and the byte budget control their lifetime.
pub const IDLE_IMAGE_SWEEP: Duration = Duration::from_secs(60);
pub const IDLE_IMAGE_TICKS: u32 = 5;
/// Start the idle image sweep on the main thread. Safe to call once per
/// process that hosts panes; a process with no scheduler yet gets nothing.
pub fn spawn_idle_image_sweeper() {
if !promise::spawn::is_scheduler_configured() {
log::warn!("idle image sweeper not started: no scheduler configured");
return;
}
log::debug!(
"idle image sweeper started: every {IDLE_IMAGE_SWEEP:?}, {IDLE_IMAGE_TICKS} quiet sweeps"
);
promise::spawn::spawn_into_main_thread(async {
loop {
smol::Timer::after(IDLE_IMAGE_SWEEP).await;
let Some(mux) = Mux::try_get() else {
return;
};
let panes = mux.iter_panes();
let released: usize = panes
.iter()
.map(|pane| pane.idle_image_tick(IDLE_IMAGE_TICKS))
.sum();
log::debug!(
"idle image sweep: {} panes, released {released} bytes of pictures",
panes.len()
);
if log::log_enabled!(log::Level::Debug) {
for pane in &panes {
let (images, placements, bytes) = pane.image_stats();
if images > 0 {
log::debug!(
"idle image sweep: pane {} holds {images} pictures ({placements} placed) {bytes} bytes",
pane.pane_id()
);
}
}
}
}
})
.detach();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pane_output_generation_is_visible_before_subscribers_run() {
let mux = Arc::new(Mux::new(None));
let observed = Arc::new(Mutex::new(Vec::new()));
let subscriber_mux = Arc::clone(&mux);
let subscriber_observed = Arc::clone(&observed);
mux.subscribe(move |notification| {
if let MuxNotification::PaneOutput(pane_id) = notification {
subscriber_observed
.lock()
.push(subscriber_mux.pane_output_generation(pane_id));
}
true
});
mux.notify(MuxNotification::PaneOutput(7));
mux.notify(MuxNotification::PaneOutput(7));
mux.notify(MuxNotification::PaneOutput(8));
assert_eq!(&*observed.lock(), &[1, 2, 1]);
assert_eq!(mux.pane_output_generation(7), 2);
assert_eq!(mux.pane_output_generation(8), 1);
mux.notify(MuxNotification::PaneRemoved(7));
assert_eq!(mux.pane_output_generation(7), 0);
assert_eq!(mux.pane_output_generation(8), 1);
}
#[test]
fn pane_output_generation_wraps_without_using_zero() {
let mut state = PaneOutputGenerationState::default();
state.generations.insert(9, u64::MAX);
assert_eq!(state.note_output(9), 1);
}
#[test]
fn action_buffer_is_kept_only_while_output_is_waiting() {
let empty: Vec<Action> = Vec::new();
assert!(
!release_action_buffer(&empty, 128, false),
"nothing to release"
);
let mut kept: Vec<Action> = Vec::with_capacity(64);
assert!(
!release_action_buffer(&kept, 128, true),
"busy: kept for the next batch"
);
assert!(release_action_buffer(&kept, 128, false), "idle: given back");
kept.reserve(256);
assert!(
release_action_buffer(&kept, 128, true),
"grew past one action per byte of a full read: given back"
);
}
#[test]
fn tab_geometry_transaction_coalesces_and_is_tab_scoped() {
let mut state = TabResizeNotificationState::default();
state.begin(10);
state.begin(10);
state.begin(11);
assert!(state.defer(10));
assert!(state.defer(10));
assert!(state.defer(11));
assert!(!state.finish(10, true), "nested scope is still open");
assert!(state.finish(10, true), "tab 10 publishes exactly once");
assert!(state.finish(11, true), "tab 11 is independent");
assert!(!state.defer(10), "completed tabs are no longer deferred");
}
#[test]
fn failed_tab_geometry_transaction_discards_partial_notification() {
let mut state = TabResizeNotificationState::default();
state.begin(12);
state.begin(12);
assert!(state.defer(12));
assert!(!state.finish(12, false));
assert!(!state.finish(12, true), "an inner abort poisons the batch");
assert!(!state.pending.contains(&12));
assert!(!state.depth.contains_key(&12));
assert!(!state.aborted.contains(&12));
}
#[test]
fn tab_geometry_transaction_publishes_one_final_notification() {
let mux = Mux::new(None);
let notifications = Arc::new(AtomicUsize::new(0));
let observed = Arc::clone(¬ifications);
mux.subscribe(move |notification| {
if matches!(notification, MuxNotification::TabResized(21)) {
observed.fetch_add(1, Ordering::Relaxed);
}
true
});
{
let mut transaction = mux.begin_tab_geometry_transaction(21);
mux.notify(MuxNotification::TabResized(21));
mux.notify(MuxNotification::TabResized(21));
assert_eq!(notifications.load(Ordering::Relaxed), 0);
transaction.commit();
}
assert_eq!(notifications.load(Ordering::Relaxed), 1);
}
fn client_id(id: usize) -> ClientId {
// One host per id: two renderers in a test are two devices, which is
// what handoff arbitrates between (one device restarting inherits).
ClientId {
hostname: format!("test-host-{id}"),
username: "test-user".to_string(),
pid: id as u32,
epoch: 1,
id,
ssh_auth_sock: None,
}
}
#[test]
fn a_renderer_counted_after_it_unregistered_is_let_go() {
let mux = Mux::new(None);
let stays = client_id(1);
let gone = client_id(2);
let registration = mux.register_client(Arc::new(stays.clone()));
{
let mut lease = mux.frontend_lease.lock();
lease.ever_rendered.insert(stays.clone());
lease.ever_rendered.insert(gone.clone());
}
assert!(!mux.drop_unregistered_renderer(&stays));
assert!(mux.drop_unregistered_renderer(&gone));
assert!(!mux.drop_unregistered_renderer(&gone), "it leaves once");
let lease = mux.frontend_lease.lock();
assert!(lease.ever_rendered.contains(&stays));
assert!(!lease.ever_rendered.contains(&gone));
drop(lease);
mux.frontend_lease.lock().ever_rendered.clear();
mux.unregister_client(&stays, registration);
}
#[test]
fn labels_lose_control_characters_and_blank_is_none() {
assert_eq!(normalized_pane_label(" build "), Some("build".to_string()));
assert_eq!(
normalized_pane_label("\x1b]52;c;aGk=\x07bad\nname"),
Some("]52;c;aGk=badname".to_string())
);
assert_eq!(normalized_pane_label(" \x1b\t "), None);
assert_eq!(
normalized_pane_label(&"x".repeat(200)).map(|l| l.len()),
Some(128)
);
let cut_at_a_space = format!("{} suffix", "x".repeat(127));
assert_eq!(
normalized_pane_label(&cut_at_a_space),
Some("x".repeat(127)),
"no blank is left at the cut"
);
}
fn frontend_test_size(cols: usize, rows: usize) -> TerminalSize {
TerminalSize {
cols,
rows,
pixel_width: cols * 8,
pixel_height: rows * 16,
dpi: 96,
}
}
fn frontend_test_viewport(cols: usize, rows: usize) -> FrontendViewport {
FrontendViewport::CellGrid {
size: frontend_test_size(cols, rows),
}
}
fn frontend_test_tab(mux: &Mux) -> TabId {
let tab = Arc::new(Tab::new(&frontend_test_size(80, 24)));
mux.add_tab_no_panes(&tab);
tab.tab_id()
}
#[test]
fn a_zoomed_page_takes_the_size_its_driving_frontend_gave_it() {
let driver = client_id(90);
let other = client_id(91);
let root = frontend_test_size(120, 34);
let page = FrontendPaneViewport {
pane_id: 7,
size: frontend_test_size(97, 26),
frame: root,
};
let beside = FrontendPaneViewport {
pane_id: 8,
size: frontend_test_size(50, 30),
frame: frontend_test_size(60, 34),
};
let tab = TabFrontendLease {
owner: Some(driver.clone()),
viewports: HashMap::from([
(
driver.clone(),
FrontendViewport::Native {
size: root,
panes: vec![page, beside],
},
),
(
other.clone(),
FrontendViewport::Native {
size: root,
panes: vec![FrontendPaneViewport {
size: frontend_test_size(10, 5),
..page
}],
},
),
]),
..Default::default()
};
let mut lease = FrontendLeaseState {
tabs: HashMap::from([(3, tab)]),
handoff_owner: Some(driver.clone()),
..Default::default()
};
assert_eq!(driving_frontend_size_over_root(&lease, 3, 7, root), Some(page.size));
assert_eq!(
driving_frontend_size_over_root(&lease, 3, 8, root),
None,
"a pane that does not cover the tab is not a zoomed page"
);
assert_eq!(
driving_frontend_size_over_root(&lease, 3, 7, frontend_test_size(130, 34)),
None,
"a layout for a tab of another size"
);
assert_eq!(driving_frontend_size_over_root(&lease, 3, 9, root), None);
assert_eq!(driving_frontend_size_over_root(&lease, 4, 7, root), None);
lease.handoff_owner = Some(other.clone());
assert_eq!(
driving_frontend_size_over_root(&lease, 3, 7, root),
Some(frontend_test_size(10, 5)),
"only the driving frontend's sizes count"
);
lease.set_handoff_owner(None);
assert_eq!(driving_frontend_size_over_root(&lease, 3, 7, root), None);
lease.access_mode = FrontendAccessMode::TmuxLatest;
assert_eq!(
driving_frontend_size_over_root(&lease, 3, 7, root),
Some(page.size),
"TmuxLatest drives each tab on its own"
);
lease.tabs.get_mut(&3).unwrap().viewports.insert(
driver.clone(),
FrontendViewport::CellGrid { size: root },
);
assert_eq!(driving_frontend_size_over_root(&lease, 3, 7, root), None);
}
#[test]
fn sole_live_renderer_ignores_itself_and_dead_clients() {
let me = client_id(150);
let other = client_id(151);
let mut live = HashSet::new();
live.insert(me.clone());
let mut tabs: HashMap<TabId, TabFrontendLease> = HashMap::new();
// No viewports anywhere: sole.
assert!(is_sole_live_renderer(&tabs, &live, &me));
// Only my own viewport: still sole.
tabs.entry(1)
.or_default()
.viewports
.insert(me.clone(), frontend_test_viewport(80, 24));
assert!(is_sole_live_renderer(&tabs, &live, &me));
// Another client's viewport, but that client is dead: still sole.
tabs.entry(2)
.or_default()
.viewports
.insert(other.clone(), frontend_test_viewport(60, 20));
assert!(is_sole_live_renderer(&tabs, &live, &me));
// Same viewport once the client is live: no longer sole.
live.insert(other.clone());
assert!(!is_sole_live_renderer(&tabs, &live, &me));
// A live client with no viewport anywhere (a `wezterm cli` style
// non-rendering client) does not count as a renderer.
tabs.get_mut(&2).unwrap().viewports.remove(&other);
assert!(is_sole_live_renderer(&tabs, &live, &me));
}
#[test]
fn initializing_a_different_mode_publishes_a_newer_generation() {
// The default state (Handoff, generation 0) may already have been
// sent to a client; the stored mode must arrive with a generation
// that client accepts, and an unchanged mode must stay quiet.
let mux = Mux::new(None);
let before = mux.frontend_access_state();
assert_eq!(before.mode, FrontendAccessMode::Handoff);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let after = mux.frontend_access_state();
assert_eq!(after.mode, FrontendAccessMode::TmuxLatest);
assert!(after.generation > before.generation);
let quiet = Mux::new(None);
let before = quiet.frontend_access_state();
quiet.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
assert_eq!(quiet.frontend_access_state().generation, before.generation);
}
#[test]
fn pending_exit_notifications_outlive_history_eviction_and_expiry() {
let _executor = promise::spawn::SimpleExecutor::new();
let mux = Mux::new(None);
let announced = Arc::new(Mutex::new(Vec::new()));
{
let announced = Arc::clone(&announced);
mux.subscribe(move |n| {
if let MuxNotification::PaneExited { pane_id, exit } = n {
announced.lock().push((pane_id, exit.code));
}
true
});
}
let count = RECENT_PANE_EXITS + 2;
for pane_id in 0..count {
mux.record_pane_exit(
pane_id,
thinkterm_proto::PaneExit {
code: Some(pane_id as u32),
signal: None,
},
);
}
assert_eq!(mux.recent_pane_exit(0), None);
// Expiry must not lose the pending payloads either.
for (_, _, at) in mux.recent_pane_exits.lock().iter_mut() {
*at = Instant::now() - RECENT_PANE_EXIT_TTL;
}
for pane_id in 0..count {
mux.announce_pane_exit(pane_id, true);
mux.announce_pane_exit(pane_id, true);
}
let expected: Vec<_> = (0..count).map(|id| (id, Some(id as u32))).collect();
assert_eq!(*announced.lock(), expected);
assert!(mux.unannounced_pane_exits.lock().is_empty());
}
#[test]
fn recent_pane_exits_are_bounded_and_keep_the_latest() {
// Announcements are queued for the main thread; a scheduler that
// merely holds them is enough.
let _executor = promise::spawn::SimpleExecutor::new();
let mux = Mux::new(None);
let announced = Arc::new(Mutex::new(Vec::new()));
{
let announced = Arc::clone(&announced);
mux.subscribe(move |n| {
if let MuxNotification::PaneExited { pane_id, .. } = n {
announced.lock().push(pane_id);
}
true
});
}
let exit = |code| thinkterm_proto::PaneExit {
code: Some(code),
signal: None,
};
for pane_id in 0..RECENT_PANE_EXITS + 2 {
mux.record_pane_exit(pane_id, exit(pane_id as u32));
}
assert!(
announced.lock().is_empty(),
"never from inside the recording"
);
// What removing the pane does: the news goes out then, and only once.
mux.announce_pane_exit(5, true);
mux.announce_pane_exit(5, true);
assert_eq!(*announced.lock(), vec![5]);
// Too late for a pane that is no longer in the mux.
mux.announce_pane_exit(6, false);
assert_eq!(*announced.lock(), vec![5]);
assert_eq!(
mux.recent_pane_exit(6),
Some(exit(6)),
"still there for `wait`"
);
assert_eq!(mux.recent_pane_exits.lock().len(), RECENT_PANE_EXITS);
// The oldest two made room.
assert_eq!(mux.recent_pane_exit(0), None);
assert_eq!(mux.recent_pane_exit(1), None);
assert_eq!(mux.recent_pane_exit(2), Some(exit(2)));
// A pane that ends again (a held pane respawned) has one entry.
mux.record_pane_exit(2, exit(9));
assert_eq!(mux.recent_pane_exit(2), Some(exit(9)));
assert_eq!(mux.recent_pane_exits.lock().len(), RECENT_PANE_EXITS);
}
#[test]
fn unclaimed_handoff_lease_belongs_to_the_first_renderer() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
// Bootstrap: nobody has ever rendered.
assert!(mux.current_identity_owns_frontend_lease(tab_id));
mux.replace_identity(Some(Arc::new(client_id(160))));
assert!(mux.current_identity_owns_frontend_lease(tab_id));
}
#[test]
fn revoked_handoff_lease_stays_unowned_while_two_renderers_are_present() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
let gui = Arc::new(client_id(161));
let tui = Arc::new(client_id(162));
mux.register_client(Arc::clone(&gui));
mux.register_client(Arc::clone(&tui));
{
let mut lease = mux.frontend_lease.lock();
lease.set_handoff_owner(None);
lease.handoff_ever_owned = true;
let state = lease.tabs.entry(tab_id).or_default();
state
.viewports
.insert((*gui).clone(), frontend_test_viewport(120, 40));
state
.viewports
.insert((*tui).clone(), frontend_test_viewport(60, 20));
}
// Symmetry is the point: neither survivor silently owns the lease.
mux.replace_identity(Some(Arc::clone(&gui)));
assert!(!mux.current_identity_owns_frontend_lease(tab_id));
mux.replace_identity(Some(Arc::clone(&tui)));
assert!(!mux.current_identity_owns_frontend_lease(tab_id));
}
#[test]
fn revoked_handoff_lease_returns_to_the_sole_surviving_renderer() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
let gui = Arc::new(client_id(163));
let tui = Arc::new(client_id(164));
mux.register_client(Arc::clone(&gui));
let tui_registration = mux.register_client(Arc::clone(&tui));
{
let mut lease = mux.frontend_lease.lock();
lease.set_handoff_owner(None);
lease.handoff_ever_owned = true;
let state = lease.tabs.entry(tab_id).or_default();
state
.viewports
.insert((*gui).clone(), frontend_test_viewport(120, 40));
state
.viewports
.insert((*tui).clone(), frontend_test_viewport(60, 20));
}
mux.replace_identity(Some(Arc::clone(&gui)));
assert!(!mux.current_identity_owns_frontend_lease(tab_id));
mux.unregister_client(&tui, tui_registration);
assert!(mux.current_identity_owns_frontend_lease(tab_id));
// A viewport left behind by a dead client must not block recovery.
mux.frontend_lease
.lock()
.tabs
.get_mut(&tab_id)
.unwrap()
.viewports
.insert((*tui).clone(), frontend_test_viewport(60, 20));
assert!(mux.current_identity_owns_frontend_lease(tab_id));
}
#[test]
fn claimed_handoff_lease_is_not_owned_by_a_non_owner() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
let owner = Arc::new(client_id(165));
let bystander = Arc::new(client_id(166));
{
let mut lease = mux.frontend_lease.lock();
lease.handoff_owner = Some((*owner).clone());
lease.handoff_ever_owned = true;
}
mux.replace_identity(Some(Arc::clone(&bystander)));
assert!(!mux.current_identity_owns_frontend_lease(tab_id));
mux.replace_identity(Some(Arc::clone(&owner)));
assert!(mux.current_identity_owns_frontend_lease(tab_id));
mux.replace_identity(None);
assert!(!mux.current_identity_owns_frontend_lease(tab_id));
}
#[test]
fn unclaimed_tmux_latest_tab_is_owned_by_the_asking_renderer() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let tab_id = frontend_test_tab(&mux);
let me = Arc::new(client_id(167));
let other = Arc::new(client_id(168));
mux.register_client(Arc::clone(&me));
mux.register_client(Arc::clone(&other));
{
let mut lease = mux.frontend_lease.lock();
let state = lease.tabs.entry(tab_id).or_default();
state
.viewports
.insert((*me).clone(), frontend_test_viewport(120, 40));
state
.viewports
.insert((*other).clone(), frontend_test_viewport(60, 20));
}
// Per-tab claims are cheap and symmetric: an ownerless tab belongs to
// whoever asks, even with another renderer present.
mux.replace_identity(Some(Arc::clone(&me)));
assert!(mux.current_identity_owns_frontend_lease(tab_id));
mux.frontend_lease
.lock()
.tabs
.get_mut(&tab_id)
.unwrap()
.owner = Some((*other).clone());
assert!(!mux.current_identity_owns_frontend_lease(tab_id));
}
#[test]
fn optional_client_workspace_matches_the_identified_fallback() {
config::use_test_configuration();
let mux = Mux::new(None);
let client = Arc::new(client_id(190));
mux.register_client(Arc::clone(&client));
mux.set_active_workspace_for_client(&client, "space-2");
assert_eq!(
mux.active_workspace_for_optional_client(Some(&client)),
"space-2"
);
// The None case must resolve to the default workspace — exactly what
// `active_workspace()` produced with no identity installed.
assert_eq!(
mux.active_workspace_for_optional_client(None),
mux.active_workspace()
);
assert_eq!(mux.active_workspace_for_optional_client(None), "default");
}
#[test]
fn failed_first_handoff_geometry_revokes_the_automatic_owner() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
let client = client_id(201);
mux.frontend_geometry_failures.lock().push_back(true);
assert!(mux
.set_client_viewport(&client, tab_id, frontend_test_viewport(120, 40))
.is_err());
let lease = mux.frontend_lease.lock();
assert_eq!(lease.handoff_owner, None);
assert!(lease.handoff_ever_owned);
assert_eq!(lease.tabs[&tab_id].owner, None);
}
#[test]
fn failed_handoff_claim_revokes_the_global_owner() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
let first = client_id(211);
let second = client_id(212);
{
let mut lease = mux.frontend_lease.lock();
lease.handoff_owner = Some(first.clone());
lease.handoff_ever_owned = true;
lease.tabs.entry(tab_id).or_default().owner = Some(first);
}
mux.frontend_geometry_failures.lock().push_back(true);
assert!(mux
.claim_frontend_viewport(&second, tab_id, frontend_test_viewport(120, 40))
.is_err());
let lease = mux.frontend_lease.lock();
assert_eq!(lease.handoff_owner, None);
assert_eq!(lease.tabs[&tab_id].owner, None);
}
#[test]
fn failed_mode_geometry_restores_the_prior_mode_but_clears_ownership() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::Handoff);
let tab_id = frontend_test_tab(&mux);
let owner = client_id(221);
{
let mut lease = mux.frontend_lease.lock();
lease.handoff_owner = Some(owner.clone());
lease.handoff_ever_owned = true;
lease.tabs.entry(tab_id).or_default().owner = Some(owner.clone());
}
mux.frontend_geometry_failures.lock().push_back(true);
assert!(mux
.set_frontend_access_mode(
&owner,
FrontendAccessMode::TmuxLatest,
tab_id,
frontend_test_viewport(120, 40),
)
.is_err());
let access = mux.frontend_access_state();
assert_eq!(access.mode, FrontendAccessMode::Handoff);
assert_eq!(access.owner, None);
}
fn palette(foreground: f32) -> ColorPalette {
let mut palette = ColorPalette::default();
palette.foreground = (foreground, foreground, foreground, 1.0).into();
palette
}
#[test]
fn palette_advice_requires_interaction_to_take_ownership() {
let mut state = PaletteAdvisoryState::default();
let white_client = client_id(1);
let gray_client = client_id(2);
let white_session = state.register(&white_client);
let gray_session = state.register(&gray_client);
let white = palette(1.0);
let gray = palette(0.7);
assert_eq!(state.advise(white_session, 9, white.clone()), None);
assert_eq!(state.activate(white_session, 9), Some(white.clone()));
// A background attach records its preference but cannot replace the
// active client's OSC query base.
assert_eq!(state.advise(gray_session, 9, gray.clone()), None);
assert_eq!(state.owners.get(&9), Some(&white_session));
assert_eq!(state.activate(gray_session, 9), Some(gray));
assert_eq!(state.owners.get(&9), Some(&gray_session));
// A config reload by the inactive client is stored without stealing.
let warmer_white = palette(0.95);
assert_eq!(state.advise(white_session, 9, warmer_white.clone()), None);
assert_eq!(state.owners.get(&9), Some(&gray_session));
// Removing the current owner falls back to the most recently active
// surviving client and its latest advice.
assert_eq!(
state.remove_session(gray_session),
vec![PaletteSelectionChange {
pane_id: 9,
palette: Some(warmer_white),
}]
);
assert_eq!(state.owners.get(&9), Some(&white_session));
}
#[test]
fn disconnect_does_not_promote_a_background_only_advisor() {
let mut state = PaletteAdvisoryState::default();
let active = client_id(1);
let background = client_id(2);
let active_session = state.register(&active);
let background_session = state.register(&background);
assert_eq!(state.advise(active_session, 7, palette(1.0)), None);
assert!(state.activate(active_session, 7).is_some());
assert_eq!(state.advise(background_session, 7, palette(0.7)), None);
assert_eq!(
state.remove_session(active_session),
vec![PaletteSelectionChange {
pane_id: 7,
palette: None,
}]
);
assert!(!state.owners.contains_key(&7));
}
#[test]
fn active_palette_reload_updates_without_new_interaction() {
let mut state = PaletteAdvisoryState::default();
let client = client_id(1);
let session = state.register(&client);
assert_eq!(state.advise(session, 4, palette(0.8)), None);
assert!(state.activate(session, 4).is_some());
let updated = palette(1.0);
assert_eq!(state.advise(session, 4, updated.clone()), Some(updated));
assert_eq!(state.activate(session, 4), None);
}
#[test]
fn stale_client_disconnect_cannot_remove_reconnected_frontend_lease() {
let mux = Mux::new(None);
let client = Arc::new(client_id(21));
let old_registration = mux.register_client(Arc::clone(&client));
let new_registration = mux.register_client(Arc::clone(&client));
assert!(!mux.registered_client_had_input(&client, old_registration));
assert!(mux.registered_client_had_input(&client, new_registration));
mux.unregister_client(&client, old_registration);
assert_eq!(mux.iter_clients().len(), 1);
mux.unregister_client(&client, new_registration);
assert!(mux.iter_clients().is_empty());
}
#[test]
fn frontend_ownership_is_scoped_per_tab() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let first = Arc::new(client_id(31));
let second = Arc::new(client_id(32));
let mut lease = mux.frontend_lease.lock();
lease.tabs.entry(10).or_default().owner = Some((*first).clone());
lease.tabs.entry(11).or_default().owner = Some((*second).clone());
drop(lease);
assert!(mux.client_owns_frontend_lease(&first, 10));
assert!(!mux.client_owns_frontend_lease(&first, 11));
assert!(mux.client_owns_frontend_lease(&second, 11));
}
#[test]
fn non_rendering_cli_input_does_not_steal_a_tab_viewport() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let tui = Arc::new(client_id(62));
let cli = Arc::new(client_id(63));
mux.register_client(Arc::clone(&tui));
let cli_registration = mux.register_client(Arc::clone(&cli));
mux.frontend_lease.lock().tabs.entry(30).or_default().owner = Some((*tui).clone());
assert!(mux.registered_client_had_tab_input(&cli, cli_registration, 30));
assert!(mux.client_owns_frontend_lease(&tui, 30));
}
/// A view outliving its author would pin every follower to a scrollback
/// position that the renderer now driving never chose.
#[test]
fn only_the_owner_publishes_a_view_and_a_handover_drops_it() {
fn grid() -> FrontendViewport {
FrontendViewport::CellGrid {
size: TerminalSize::default(),
}
}
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_40 = tab.tab_id();
let first = Arc::new(client_id(80));
let second = Arc::new(client_id(81));
{
let mut lease = mux.frontend_lease.lock();
let state = lease.tabs.entry(tab_40).or_default();
state.owner = Some((*first).clone());
state.viewports.insert((*first).clone(), grid());
state.viewports.insert((*second).clone(), grid());
}
assert!(mux.set_client_view(
&first,
tab_40,
FrontendView {
scroll: vec![(7, 120)]
}
));
assert!(
!mux.set_client_view(
&first,
tab_40,
FrontendView {
scroll: vec![(7, 120)]
}
),
"saying the same thing twice is not a change worth publishing"
);
assert!(
!mux.set_client_view(
&second,
tab_40,
FrontendView {
scroll: vec![(7, 999)]
}
),
"a renderer nobody is using does not get to move everyone else"
);
assert_eq!(
mux.frontend_lease.lock().tabs[&tab_40].view,
Some(FrontendView {
scroll: vec![(7, 120)]
})
);
let _ = mux.claim_frontend_viewport(&second, tab_40, grid());
assert_eq!(
mux.frontend_lease.lock().tabs[&tab_40].view,
None,
"the new owner publishes its own on its next frame"
);
}
#[test]
fn different_sized_renderers_converge_on_one_owner_without_oscillation() {
fn size(cols: usize, rows: usize) -> TerminalSize {
TerminalSize {
cols,
rows,
pixel_width: cols * 8,
pixel_height: rows * 16,
dpi: 96,
}
}
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let initial = size(132, 40);
let tab = Arc::new(Tab::new(&initial));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let large = Arc::new(client_id(70));
let small = Arc::new(client_id(71));
let large_registration = mux.register_client(Arc::clone(&large));
let small_registration = mux.register_client(Arc::clone(&small));
mux.set_registered_client_viewport(
&large,
large_registration,
tab_id,
FrontendViewport::CellGrid { size: initial },
)
.unwrap()
.unwrap();
mux.set_registered_client_viewport(
&small,
small_registration,
tab_id,
FrontendViewport::CellGrid { size: size(80, 24) },
)
.unwrap()
.unwrap();
assert_eq!(tab.get_size(), initial);
assert_eq!(
mux.registered_client_may_resize_tab(&large, large_registration, tab_id),
Some(true)
);
assert_eq!(
mux.registered_client_may_resize_tab(&small, small_registration, tab_id),
Some(false)
);
assert!(mux.registered_client_had_tab_input(&small, small_registration, tab_id));
assert_eq!(tab.get_size(), size(80, 24));
assert!(mux.client_owns_frontend_lease(&small, tab_id));
assert_eq!(
mux.registered_client_may_resize_tab(&large, large_registration, tab_id),
Some(false)
);
}
#[test]
fn explicit_viewport_claim_uses_request_geometry_atomically() {
let mux = Mux::new(None);
let initial = TerminalSize {
cols: 120,
rows: 36,
pixel_width: 960,
pixel_height: 576,
dpi: 96,
};
let tab = Arc::new(Tab::new(&initial));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let first = Arc::new(client_id(80));
let second = Arc::new(client_id(81));
let first_registration = mux.register_client(Arc::clone(&first));
let second_registration = mux.register_client(Arc::clone(&second));
mux.set_registered_client_viewport(
&first,
first_registration,
tab_id,
FrontendViewport::CellGrid { size: initial },
)
.unwrap();
let advertised = TerminalSize {
cols: 72,
rows: 20,
pixel_width: 576,
pixel_height: 320,
dpi: 96,
};
mux.set_registered_client_viewport(
&second,
second_registration,
tab_id,
FrontendViewport::CellGrid { size: advertised },
)
.unwrap();
let claimed = TerminalSize {
cols: 84,
rows: 26,
pixel_width: 672,
pixel_height: 416,
dpi: 96,
};
let state = mux
.claim_registered_client_viewport(
&second,
second_registration,
tab_id,
FrontendViewport::CellGrid { size: claimed },
)
.unwrap()
.unwrap();
assert_eq!(state.owner.as_ref(), Some(second.as_ref()));
assert_eq!(state.access.owner.as_ref(), Some(second.as_ref()));
assert_eq!(state.canonical_size, claimed);
assert_eq!(tab.get_size(), claimed);
let repeated = mux
.claim_registered_client_viewport(
&second,
second_registration,
tab_id,
FrontendViewport::CellGrid { size: claimed },
)
.unwrap()
.unwrap();
assert_eq!(repeated.generation, state.generation);
}
#[test]
fn handoff_disconnect_has_no_automatic_fallback() {
let mux = Mux::new(None);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let first = Arc::new(client_id(90));
let second = Arc::new(client_id(91));
let first_registration = mux.register_client(Arc::clone(&first));
let second_registration = mux.register_client(Arc::clone(&second));
let grid = FrontendViewport::CellGrid {
size: TerminalSize::default(),
};
mux.set_registered_client_viewport(&first, first_registration, tab_id, grid.clone())
.unwrap();
mux.set_registered_client_viewport(&second, second_registration, tab_id, grid.clone())
.unwrap();
assert_eq!(
mux.frontend_access_state().owner.as_ref(),
Some(first.as_ref())
);
assert_eq!(
mux.registered_client_has_frontend_access(&first, first_registration),
Some(true)
);
assert_eq!(
mux.registered_client_has_frontend_access(&second, second_registration),
Some(false)
);
assert!(mux.registered_client_had_tab_input(&first, first_registration, tab_id));
assert!(
!mux.registered_client_had_tab_input(&second, second_registration, tab_id),
"B must reject keyboard/mouse input from the opaque renderer"
);
mux.unregister_client(&first, first_registration);
assert_eq!(
mux.registered_client_has_frontend_access(&first, first_registration),
None
);
assert_eq!(mux.frontend_access_state().owner, None);
assert!(!mux.client_has_frontend_access(&second));
// A passive redraw after the disconnect still must not acquire B.
mux.set_registered_client_viewport(&second, second_registration, tab_id, grid.clone())
.unwrap();
assert_eq!(mux.frontend_access_state().owner, None);
mux.claim_registered_client_viewport(&second, second_registration, tab_id, grid)
.unwrap();
assert_eq!(
mux.frontend_access_state().owner.as_ref(),
Some(second.as_ref())
);
}
/// A's per-tab owner is reseeded by the survivor's next passive report
/// after a disconnect vacates it. That reseed must apply the survivor's
/// geometry AND publish with a bumped generation: the survivor's report
/// is typically byte-identical to its stored follower viewport, and an
/// unpublished reseed hands back a stale generation that the client
/// rejects - it then never learns it owns the tab, keeps sending bare
/// paneless viewports, and the server stays on the dead owner's geometry.
#[test]
fn the_same_device_returning_inherits_the_handoff_lease() {
let mux = Mux::new(None);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let first = Arc::new(client_id(70));
let first_registration = mux.register_client(Arc::clone(&first));
let grid = FrontendViewport::CellGrid {
size: TerminalSize::default(),
};
mux.set_registered_client_viewport(&first, first_registration, tab_id, grid.clone())
.unwrap();
assert_eq!(
mux.frontend_access_state().owner.as_ref(),
Some(first.as_ref())
);
mux.unregister_client(&first, first_registration);
assert_eq!(mux.frontend_access_state().owner, None);
// The GUI on that host restarted: same host and user, new process.
let again = Arc::new(ClientId {
pid: 7000,
id: 71,
..client_id(70)
});
let again_registration = mux.register_client(Arc::clone(&again));
mux.set_registered_client_viewport(&again, again_registration, tab_id, grid.clone())
.unwrap();
assert_eq!(
mux.frontend_access_state().owner.as_ref(),
Some(again.as_ref()),
"the returning device drives without a claim"
);
assert!(mux.registered_client_had_tab_input(&again, again_registration, tab_id));
// A different device after that owner leaves still has to take over.
mux.unregister_client(&again, again_registration);
let other = Arc::new(client_id(72));
let other_registration = mux.register_client(Arc::clone(&other));
mux.set_registered_client_viewport(&other, other_registration, tab_id, grid)
.unwrap();
assert_eq!(mux.frontend_access_state().owner, None);
}
#[test]
fn tmux_reseeding_a_vacated_tab_applies_and_publishes() {
let mux = Mux::new(None);
mux.initialize_frontend_access_mode(FrontendAccessMode::TmuxLatest);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let departing = Arc::new(client_id(95));
let survivor = Arc::new(client_id(96));
let departing_registration = mux.register_client(Arc::clone(&departing));
let survivor_registration = mux.register_client(Arc::clone(&survivor));
let departing_size = TerminalSize {
cols: 132,
rows: 40,
pixel_width: 1056,
pixel_height: 640,
dpi: 96,
};
let survivor_size = TerminalSize {
cols: 100,
rows: 30,
pixel_width: 800,
pixel_height: 480,
dpi: 96,
};
let survivor_grid = FrontendViewport::CellGrid {
size: survivor_size,
};
mux.set_registered_client_viewport(
&departing,
departing_registration,
tab_id,
FrontendViewport::CellGrid {
size: departing_size,
},
)
.unwrap();
let followed = mux
.set_registered_client_viewport(
&survivor,
survivor_registration,
tab_id,
survivor_grid.clone(),
)
.unwrap()
.unwrap();
assert_eq!(followed.owner.as_ref(), Some(departing.as_ref()));
assert_eq!(tab.get_size(), departing_size);
mux.unregister_client(&departing, departing_registration);
let vacated = mux.viewport_state(tab_id).unwrap();
assert_eq!(vacated.owner, None);
assert!(vacated.generation > followed.generation);
let reseeded = mux
.set_registered_client_viewport(&survivor, survivor_registration, tab_id, survivor_grid)
.unwrap()
.unwrap();
assert_eq!(reseeded.owner.as_ref(), Some(survivor.as_ref()));
assert!(
reseeded.generation > vacated.generation,
"an unpublished reseed is rejected as stale by the client"
);
assert_eq!(
tab.get_size(),
survivor_size,
"the takeover must apply the survivor's geometry"
);
}
/// B arbitrates between screens. A connection that never advertises a
/// viewport -- `thinkterm cli send-text` and friends -- is not a screen,
/// so its input and chrome mutations pass the gate; and passing must not
/// itself claim anything from the real owner.
#[test]
fn handoff_exempts_clients_that_never_render() {
let mux = Mux::new(None);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let screen = Arc::new(client_id(70));
let cli = Arc::new(client_id(71));
let screen_registration = mux.register_client(Arc::clone(&screen));
let cli_registration = mux.register_client(Arc::clone(&cli));
let grid = FrontendViewport::CellGrid {
size: TerminalSize::default(),
};
mux.set_registered_client_viewport(&screen, screen_registration, tab_id, grid.clone())
.unwrap();
assert_eq!(
mux.registered_client_has_frontend_access(&screen, screen_registration),
Some(true)
);
assert_eq!(
mux.registered_client_has_frontend_access(&cli, cli_registration),
Some(true)
);
assert!(mux.registered_client_had_tab_input(&cli, cli_registration, tab_id));
assert_eq!(
mux.frontend_access_state().owner.as_ref(),
Some(screen.as_ref()),
"the exemption must not steal the screen's lease"
);
// The moment the same connection renders, it is a second screen and
// the gate applies to it like any other.
mux.set_registered_client_viewport(&cli, cli_registration, tab_id, grid)
.unwrap();
assert_eq!(
mux.registered_client_has_frontend_access(&cli, cli_registration),
Some(false)
);
assert!(!mux.registered_client_had_tab_input(&cli, cli_registration, tab_id));
}
/// The headless shape of the same rule: after the owner disconnects
/// (`handoff_ever_owned` set, no owner elected), surviving screens stay
/// blocked until an explicit takeover -- but a non-rendering CLI still
/// passes, because there is no screen it could be fighting.
#[test]
fn handoff_exempts_non_renderers_after_owner_disconnect() {
let mux = Mux::new(None);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let screen = Arc::new(client_id(72));
let survivor = Arc::new(client_id(73));
let cli = Arc::new(client_id(74));
let screen_registration = mux.register_client(Arc::clone(&screen));
let survivor_registration = mux.register_client(Arc::clone(&survivor));
let cli_registration = mux.register_client(Arc::clone(&cli));
let grid = FrontendViewport::CellGrid {
size: TerminalSize::default(),
};
mux.set_registered_client_viewport(&screen, screen_registration, tab_id, grid.clone())
.unwrap();
mux.set_registered_client_viewport(&survivor, survivor_registration, tab_id, grid)
.unwrap();
mux.unregister_client(&screen, screen_registration);
assert_eq!(
mux.registered_client_has_frontend_access(&survivor, survivor_registration),
Some(false),
"a surviving screen still needs an explicit takeover"
);
assert_eq!(
mux.registered_client_has_frontend_access(&cli, cli_registration),
Some(true)
);
assert!(mux.registered_client_had_tab_input(&cli, cli_registration, tab_id));
}
#[test]
fn mode_changes_require_the_correct_current_owner() {
let mux = Mux::new(None);
let tab = Arc::new(Tab::new(&TerminalSize::default()));
mux.add_tab_no_panes(&tab);
let tab_id = tab.tab_id();
let first = Arc::new(client_id(92));
let second = Arc::new(client_id(93));
let first_registration = mux.register_client(Arc::clone(&first));
let second_registration = mux.register_client(Arc::clone(&second));
let grid = FrontendViewport::CellGrid {
size: TerminalSize::default(),
};
mux.set_registered_client_viewport(&first, first_registration, tab_id, grid.clone())
.unwrap();
mux.set_registered_client_viewport(&second, second_registration, tab_id, grid.clone())
.unwrap();
assert!(mux
.set_registered_frontend_access_mode(
&second,
second_registration,
FrontendAccessMode::TmuxLatest,
tab_id,
grid.clone(),
)
.is_err());
mux.set_registered_frontend_access_mode(
&first,
first_registration,
FrontendAccessMode::TmuxLatest,
tab_id,
grid.clone(),
)
.unwrap();
// In A the active tab's layout owner, not an unrelated renderer, is
// the one allowed to make itself B's global owner.
mux.claim_registered_client_viewport(&second, second_registration, tab_id, grid.clone())
.unwrap();
assert!(mux
.set_registered_frontend_access_mode(
&first,
first_registration,
FrontendAccessMode::Handoff,
tab_id,
grid.clone(),
)
.is_err());
let access = mux
.set_registered_frontend_access_mode(
&second,
second_registration,
FrontendAccessMode::Handoff,
tab_id,
grid,
)
.unwrap();
assert_eq!(access.mode, FrontendAccessMode::Handoff);
assert_eq!(access.owner.as_ref(), Some(second.as_ref()));
}
#[test]
fn unknown_cell_grid_pixels_stay_unknown_for_each_pane() {
let target = pane_size_for_cell_grid(
TerminalSize {
cols: 120,
rows: 40,
pixel_width: 0,
pixel_height: 0,
dpi: 96,
},
60,
20,
);
assert_eq!((target.pixel_width, target.pixel_height), (0, 0));
let known = pane_size_for_cell_grid(
TerminalSize {
cols: 120,
rows: 40,
pixel_width: 960,
pixel_height: 640,
dpi: 96,
},
60,
20,
);
assert_eq!((known.pixel_width, known.pixel_height), (480, 320));
}
#[test]
fn late_palette_updates_from_a_disconnected_client_are_rejected() {
let mut state = PaletteAdvisoryState::default();
let client = client_id(1);
let session = state.register(&client);
assert_eq!(state.advise(session, 4, palette(0.8)), None);
assert!(state.activate(session, 4).is_some());
assert!(state.deactivate_session(session));
// A SessionHandler can already have this work queued when Drop runs.
// Once disconnect cleanup has happened, that stale work must not be
// able to recreate advice or ownership for the dead connection.
assert_eq!(state.advise(session, 4, palette(0.6)), None);
assert_eq!(state.activate(session, 4), None);
assert_eq!(state.advised.get(&(session, 4)), Some(&palette(0.8)));
assert_eq!(state.owners.get(&4), Some(&session));
state.remove_session(session);
assert!(!state.owners.contains_key(&4));
}
#[test]
fn old_disconnect_cannot_remove_or_overwrite_a_reconnected_session() {
let mut state = PaletteAdvisoryState::default();
let client = client_id(1);
let old_session = state.register(&client);
assert_eq!(state.advise(old_session, 4, palette(0.6)), None);
assert!(state.activate(old_session, 4).is_some());
// A reconnect legitimately reuses ClientId, but is a distinct
// transport whose ownership must survive cleanup of the old one.
let new_session = state.register(&client);
let new_palette = palette(1.0);
assert_eq!(state.advise(new_session, 4, new_palette.clone()), None);
assert_eq!(state.activate(new_session, 4), Some(new_palette.clone()));
assert!(state.deactivate_session(old_session));
assert_eq!(state.advise(old_session, 4, palette(0.2)), None);
assert_eq!(state.owners.get(&4), Some(&new_session));
// Final cleanup happens later on the mux thread; it must observe that
// the reconnect has already become owner and leave it untouched.
assert_eq!(state.remove_session(old_session), vec![]);
assert_eq!(state.owners.get(&4), Some(&new_session));
assert_eq!(state.advised.get(&(new_session, 4)), Some(&new_palette));
}
#[test]
fn fallback_never_promotes_an_already_disconnected_session() {
let mut state = PaletteAdvisoryState::default();
let first = state.register(&client_id(1));
let owner = state.register(&client_id(2));
assert_eq!(state.advise(first, 4, palette(0.6)), None);
assert!(state.activate(first, 4).is_some());
assert_eq!(state.advise(owner, 4, palette(1.0)), None);
assert!(state.activate(owner, 4).is_some());
// Both drops can deactivate synchronously before either queued final
// cleanup runs. Removing the owner must not temporarily promote the
// other disconnected session as the pane's OSC query base.
assert!(state.deactivate_session(owner));
assert!(state.deactivate_session(first));
assert_eq!(
state.remove_session(owner),
vec![PaletteSelectionChange {
pane_id: 4,
palette: None,
}]
);
assert!(!state.owners.contains_key(&4));
}
#[test]
fn window_ui_surfaces_are_distinct_prune_anchors() {
let mux = Mux::new(None);
let window = Window::new(Some("test-workspace".to_string()), None, None);
let window_id = window.window_id();
mux.windows.write().insert(window_id, window);
assert!(mux.is_empty());
assert!(mux.register_window_ui_surface(window_id, "remote-thread:a".to_string()));
assert!(mux.register_window_ui_surface(window_id, "ssh-hosts".to_string()));
assert!(!mux.register_window_ui_surface(window_id, "ssh-hosts".to_string()));
assert!(!mux.is_empty());
mux.prune_dead_windows();
assert!(mux.get_window(window_id).is_some());
assert!(mux.unregister_window_ui_surface(window_id, "remote-thread:a"));
assert!(mux.get_window(window_id).is_some());
assert!(mux.unregister_window_ui_surface(window_id, "ssh-hosts"));
assert!(mux.get_window(window_id).is_none());
assert!(mux.is_empty());
}
#[test]
fn existing_window_spawn_context_allows_empty_window() {
let mux = Mux::new(None);
let window = Window::new(Some("test-workspace".to_string()), None, None);
let window_id = window.window_id();
mux.windows.write().insert(window_id, window);
let requested_size = TerminalSize {
rows: 33,
cols: 111,
pixel_width: 999,
pixel_height: 777,
dpi: 144,
};
let context = mux
.existing_window_spawn_context(window_id, requested_size)
.unwrap();
assert_eq!(context.size, requested_size);
assert!(context.term_config.is_none());
}
/// A subscriber that calls notify() synchronously re-enters the
/// subscriber lock and would deadlock a release build; debug builds
/// turn it into this assertion.
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "re-entrant Mux::notify")]
fn notify_is_not_reentrant() {
let mux = Arc::new(Mux::new(None));
let inner = Arc::clone(&mux);
mux.subscribe(move |_| {
inner.notify(MuxNotification::PaneRemoved(999_999));
true
});
mux.notify(MuxNotification::PaneRemoved(999_998));
}
}
#[cfg(all(test, target_os = "macos"))]
mod flood_reader_tests {
use super::*;
use portable_pty::{native_pty_system, PtySize};
/// Typing into the pty echoes it back as output. A child printing it
/// instead could not exit until that output had been read.
fn echo(typing: &mut Box<dyn Write + Send>, fd: i32, text: &str) {
typing.write_all(text.as_bytes()).unwrap();
let mut output = [pollfd {
fd,
events: POLLIN,
revents: 0,
}];
assert_eq!(poll(&mut output, Some(Duration::from_secs(10))).unwrap(), 1);
}
#[test]
fn a_flood_is_read_until_it_stops_and_the_pane_reader_carries_on() {
let pair = native_pty_system().openpty(PtySize::default()).unwrap();
let fd = pair.master.as_raw_fd().unwrap();
let mut reader = pair.master.try_clone_reader().unwrap();
let mut typing = pair.master.take_writer().unwrap();
let mut flood = FloodReader::new(fd).unwrap();
let mut buf = [0; 64];
// Only a full read starts it, or a quiet pane would never be waited on.
echo(&mut typing, fd, "first");
assert_eq!(flood.read(&mut buf), None);
let size = reader.read(&mut buf).unwrap();
assert_eq!(&buf[..size], b"first");
echo(&mut typing, fd, "second");
flood.follow(true);
let size = flood.read(&mut buf).unwrap();
assert_eq!(&buf[..size], b"second");
// Dry, it gives up rather than waiting for what may never come.
flood.follow(true);
assert_eq!(flood.read(&mut buf), None);
assert_eq!(flood.read(&mut buf), None);
// The pty stays non-blocking, so the pane's reader has to wait for the
// next output itself rather than take the empty pty for an error.
let late = std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(100));
typing.write_all(b"third").unwrap();
typing
});
let size = reader.read(&mut buf).unwrap();
assert_eq!(&buf[..size], b"third");
drop(late.join().unwrap());
}
}