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
mux: reclaim idle kitty images and abandoned transfers safely
RoversX7560d2cdcab0
diff --git a/mux/src/lib.rs b/mux/src/lib.rsindex a45d384e..ccbbbb5c 100644--- a/mux/src/lib.rs+++ b/mux/src/lib.rs@@ -3706,6 +3706,51 @@ impl wezterm_term::DownloadHandler for MuxDownloader {}}+/// How often every pane is asked to let go of pictures nothing refers to+/// any more, and how many quiet sweeps that takes: five minutes without an+/// image transfer. A frame stream that stopped (a browser pane closed, a+/// viewer quit) otherwise keeps its last frames, within budget, for as+/// long as the pane lives.+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()+ );+ 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::*;diff --git a/mux/src/localpane.rs b/mux/src/localpane.rsindex c9e8af90..865b2fab 100644--- a/mux/src/localpane.rs+++ b/mux/src/localpane.rs@@ -501,6 +501,14 @@ impl Pane for LocalPane {}}+ fn idle_image_tick(&self, ticks_until_release: u32) -> usize {+ self.terminal.lock().idle_image_tick(ticks_until_release)+ }++ fn image_stats(&self) -> (usize, usize, usize) {+ self.terminal.lock().kitty_image_stats()+ }+fn is_dead(&self) -> bool {let mut proc = self.process.lock();diff --git a/mux/src/pane.rs b/mux/src/pane.rsindex 64d1ade7..ef49c503 100644--- a/mux/src/pane.rs+++ b/mux/src/pane.rs@@ -354,6 +354,19 @@ pub trait Pane: Downcast + Send + Sync {fn perform_actions(&self, _actions: Vec<termwiz::escape::Action>) {}fn is_dead(&self) -> bool;fn kill(&self) {}++ /// One tick of the idle image sweep (`crate::spawn_idle_image_sweeper`):+ /// drop the pictures nothing refers to any more once the pane has seen+ /// no image traffic for `ticks_until_release` ticks. Returns the bytes+ /// released; a pane without pictures of its own has nothing to do.+ fn idle_image_tick(&self, _ticks_until_release: u32) -> usize {+ 0+ }++ /// (images, placements, bytes) of pictures this pane holds, for logs.+ fn image_stats(&self) -> (usize, usize, usize) {+ (0, 0, 0)+ }fn palette(&self) -> ColorPalette;/// Palette state explicitly established by the application. `None`/// means that consumers should use their configured palette.diff --git a/term/src/terminalstate/kitty.rs b/term/src/terminalstate/kitty.rsindex 7f4d17d4..ea9304f0 100644--- a/term/src/terminalstate/kitty.rs+++ b/term/src/terminalstate/kitty.rs@@ -50,9 +50,9 @@ enum ChunkedTransfer {pub struct KittyImageState {accumulator: Vec<KittyImage>,accumulated_bytes: usize,- /// Set when a transfer was abandoned for exceeding the limits above, and- /// cleared by the `m=0` that ends it. Without it every subsequent chunk- /// would log and reply again, turning one bad transfer into a flood.+ /// Set when a transfer was abandoned after exceeding the limits or+ /// becoming idle. Discards its tail until `m=0` or a fresh keyed opening;+ /// without it the remaining fragments would be parsed without their header.accumulator_overflowed: bool,max_image_id: u32,number_to_id: HashMap<u32, u32>,@@ -64,6 +64,13 @@ pub struct KittyImageState {next_seq: u64,placements: HashMap<(u32, Option<u32>), PlacementInfo>,used_memory: usize,+ /// The idle sweep's memory of `next_seq` and of the transfer in+ /// progress, and how many sweeps in a row found both unchanged. Ticks,+ /// not a clock: this crate also builds for the browser, where there is+ /// no monotonic time to read.+ idle_seen_seq: u64,+ idle_seen_transfer: (usize, usize),+ idle_ticks: u32,}impl TerminalState {@@ -221,6 +228,63 @@ impl KittyImageState {/// Evict unplaced images, oldest transmission first, until the stored/// bytes fit `budget`. Placed images and the most recently transmitted/// image are never touched.+ /// One sweep of the idle release. Anything that arrived since the last+ /// sweep -- a stored picture, or one more fragment of a transfer still+ /// being delivered -- resets the count; after `ticks_until_release`+ /// quiet sweeps the fragments of the abandoned transfer and every image+ /// no placement refers to are dropped. A frame stream that ended leaves+ /// its last frames within budget and therefore kept for as long as the+ /// pane lives; once it has been quiet this long nobody is going to+ /// place them. Placed images stay: they are content, visible on screen+ /// or further up the scrollback. The newest stored image stays too, for+ /// the same reason the budget spares it: a transmission stores now and+ /// places in a later escape. Returns the bytes released.+ pub(crate) fn idle_tick(&mut self, ticks_until_release: u32) -> usize {+ let transfer = (self.accumulator.len(), self.accumulated_bytes);+ if self.next_seq != self.idle_seen_seq || transfer != self.idle_seen_transfer {+ self.idle_seen_seq = self.next_seq;+ self.idle_seen_transfer = transfer;+ self.idle_ticks = 0;+ return 0;+ }+ self.idle_ticks = self.idle_ticks.saturating_add(1);+ if self.idle_ticks < ticks_until_release {+ return 0;+ }+ // A transfer that never finished (its producer was killed between+ // chunks) would otherwise hold its fragments until the pane closes.+ // Discard any late tail just as for an over-limit transfer. Preserve+ // an existing latch, but don't arm one when no transfer was pending.+ let mut released = self.accumulated_bytes;+ self.accumulator_overflowed |= !self.accumulator.is_empty();+ self.accumulator = Vec::new();+ self.accumulated_bytes = 0;+ self.idle_seen_transfer = (0, 0);+ let newest = self.next_seq;+ let referenced: HashSet<u32> = self.placements.keys().map(|(k, _)| *k).collect();+ let victims: Vec<u32> = self+ .id_to_data+ .keys()+ .filter(|id| !referenced.contains(id) && self.id_seq.get(id).copied() != Some(newest))+ .copied()+ .collect();+ if victims.is_empty() {+ return released;+ }+ // Removed in one pass and measured once: `evict` re-measures the+ // whole map on every call, which over a map this size is quadratic.+ let before = self.used_memory;+ for id in &victims {+ self.id_to_data.remove(id);+ self.id_seq.remove(id);+ }+ let gone: HashSet<u32> = victims.into_iter().collect();+ self.number_to_id.retain(|_, id| !gone.contains(id));+ self.recompute_used_memory();+ released += before.saturating_sub(self.used_memory);+ released+ }+pub(crate) fn prune_unreferenced(&mut self, budget: usize) {if self.used_memory <= budget {return;@@ -267,6 +331,51 @@ mod prune_tests {)))}+ #[test]+ fn idle_ticks_release_unplaced_images_after_a_quiet_spell() {+ let mut state = KittyImageState::default();+ let one = image(16).len();+ for id in 1..=3 {+ state.record_id_to_data(id, image(16), 10 * one);+ }+ // The first tick only notices the transfers; the next ones count.+ assert_eq!(state.idle_tick(2), 0);+ assert_eq!(state.idle_tick(2), 0);+ // A transfer in between starts the count over.+ state.record_id_to_data(4, image(16), 10 * one);+ assert_eq!(state.idle_tick(2), 0);+ assert_eq!(state.idle_tick(2), 0);+ assert_eq!(state.id_to_data.len(), 4, "still held");+ assert_eq!(+ state.idle_tick(2),+ 3 * one,+ "quiet long enough: the unplaced ones go"+ );+ assert_eq!(+ state.id_to_data.keys().copied().collect::<Vec<_>>(),+ vec![4],+ "the newest is still waiting to be placed"+ );+ assert_eq!(state.used_memory, one);+ assert_eq!(state.idle_tick(2), 0, "nothing left to release");+ }++ #[test]+ fn idle_sweeps_preserve_an_existing_discard_latch() {+ let mut state = KittyImageState::default();+ for _ in 0..3 {+ assert_eq!(state.idle_tick(2), 0);+ assert!(!state.accumulator_overflowed);+ }+ // A rejected transfer has already released its fragments but must+ // keep discarding the tail, no matter how many idle sweeps follow.+ state.accumulator_overflowed = true;+ for _ in 0..3 {+ assert_eq!(state.idle_tick(2), 0);+ assert!(state.accumulator_overflowed);+ }+ }+#[test]fn the_oldest_unplaced_images_go_first_and_placed_ones_never() {let mut state = KittyImageState::default();@@ -330,6 +439,21 @@ impl TerminalState {/// one -- grows the terminal without bound until it happens to clear/// the screen. Unplaced images go first; the image transmitted last is/// always kept.+ /// See `KittyImageState::idle_tick`; driven by the mux's periodic sweep.+ pub fn idle_image_tick(&mut self, ticks_until_release: u32) -> usize {+ self.kitty_img.idle_tick(ticks_until_release)+ }++ /// Stored kitty images, their placements, and the bytes they hold: for+ /// the sweep's log line, so a pane that keeps pictures says why.+ pub fn kitty_image_stats(&self) -> (usize, usize, usize) {+ (+ self.kitty_img.id_to_data.len(),+ self.kitty_img.placements.len(),+ self.kitty_img.used_memory,+ )+ }+pub(crate) fn kitty_enforce_image_budget(&mut self) {let budget = self.config.kitty_image_memory_budget();self.kitty_img.prune_unreferenced(budget);diff --git a/term/src/test/graphics.rs b/term/src/test/graphics.rsindex 862a39d6..5f9ae602 100644--- a/term/src/test/graphics.rs+++ b/term/src/test/graphics.rs@@ -9,6 +9,7 @@use super::*;use crate::color::ColorPalette;+use std::assert_eq;use std::io::Write;use std::sync::{Arc, Mutex};@@ -240,6 +241,74 @@ fn a_chunked_transfer_reassembles() {);}+#[test]+fn idle_sweep_keeps_a_chunked_transfer_that_is_still_arriving() {+ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes(format!("\x1b_Ga=t,i=9,f=32,s=2,v=2,m=1;{HALF_2X2}\x1b\\"));+ assert_eq!(term.idle_image_tick(2), 0);+ assert_eq!(term.idle_image_tick(2), 0);+ // Even an empty continuation is activity; only the chunk count changes.+ for _ in 0..3 {+ term.advance_bytes("\x1b_Gm=1;\x1b\\");+ assert_eq!(term.idle_image_tick(2), 0);+ assert_eq!(term.idle_image_tick(2), 0);+ }+ term.advance_bytes(format!("\x1b_Gm=0;{HALF_2X2}\x1b\\"));+ // The newest completed image must survive idle sweeps until placement.+ for _ in 0..4 {+ assert_eq!(term.idle_image_tick(2), 0);+ }+ term.advance_bytes("\x1b_Ga=p,i=9\x1b\\");+ assert!(term+ .screen_mut()+ .line_mut(0)+ .get_cell(0)+ .unwrap()+ .attrs()+ .images()+ .is_some());+ let reply = drain(&mut term, &tap);+ assert!(reply.contains("i=9;OK"), "{:?}", reply);+ assert!(!reply.contains(";E"), "{:?}", reply);+}++#[test]+fn idle_sweep_discards_late_chunks_and_accepts_the_next_image() {+ for tail_ends in [false, true] {+ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes(format!("\x1b_Ga=t,i=9,f=32,s=2,v=2,m=1;{HALF_2X2}\x1b\\"));+ assert_eq!(term.idle_image_tick(2), 0);+ assert_eq!(term.idle_image_tick(2), 0);+ assert!(term.idle_image_tick(2) > 0);+ term.advance_bytes(format!("\x1b_Gm=1;{HALF_2X2}\x1b\\"));+ // A late continuation must not start accumulating again.+ for _ in 0..3 {+ assert_eq!(term.idle_image_tick(2), 0);+ }+ if tail_ends {+ term.advance_bytes(format!("\x1b_Gm=0;{HALF_2X2}\x1b\\"));+ }+ // A fresh opening must work even when the old producer never sends m=0.+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");+ assert!(term+ .screen_mut()+ .line_mut(0)+ .get_cell(0)+ .unwrap()+ .attrs()+ .images()+ .is_some());+ let reply = drain(&mut term, &tap);+ assert!(reply.contains("i=1;OK"), "{:?}", reply);+ assert!(+ !reply.contains(";E"),+ "late chunks must be silent: {:?}",+ reply+ );+ }+}+#[test]fn an_unterminated_chunked_transfer_is_abandoned() {let (mut term, tap) = term_with_tap(640, 384);diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rsindex 70ca882c..a1ffd8e4 100644--- a/wezterm-gui/src/main.rs+++ b/wezterm-gui/src/main.rs@@ -924,6 +924,9 @@ async fn async_run_terminal_gui(if let Err(err) = spawn_mux_server(unix_socket_path, should_publish) {log::warn!("{:#}", err);}+ // For the terminals this process runs itself; the session server+ // sweeps its own.+ mux::spawn_idle_image_sweeper();// This process is the GUI now (a launch that handed its command to a// running GUI returned before this), so a server the setting no longerdiff --git a/wezterm-mux-server/src/main.rs b/wezterm-mux-server/src/main.rsindex b0725679..35a5939d 100644--- a/wezterm-mux-server/src/main.rs+++ b/wezterm-mux-server/src/main.rs@@ -443,6 +443,7 @@ async fn async_run(// are available before agent detection starts its safety tick.mux::agent_status::initialize_mux(&mux);update_mux_domains_for_server(&config)?;+ mux::spawn_idle_image_sweeper();#[cfg(unix)]if let Some(mut takeover) = takeover {