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
Bound and repair the kitty graphics state machine
RoversX904385bc9f8d
diff --git a/term/src/terminalstate/image.rs b/term/src/terminalstate/image.rsindex 572cffcd..e0d5aad6 100644--- a/term/src/terminalstate/image.rs+++ b/term/src/terminalstate/image.rs@@ -69,8 +69,20 @@ impl TerminalState {let seqno = self.seqno;let physical_cols = self.screen().physical_cols;let physical_rows = self.screen().physical_rows;- let cell_pixel_width = self.pixel_width / physical_cols;- let cell_pixel_height = self.pixel_height / physical_rows;+ // A pane can be asked to show an image before anything has told it how+ // many pixels it has: the tmux and mux-server paths never report a pixel+ // size at all, and the GUI only corrects one after the window has laid+ // the pane out. Every division below would then be by zero.+ let cell_pixel_width = self.pixel_width.checked_div(physical_cols).unwrap_or(0);+ let cell_pixel_height = self.pixel_height.checked_div(physical_rows).unwrap_or(0);+ anyhow::ensure!(+ cell_pixel_width > 0 && cell_pixel_height > 0,+ "no usable cell geometry: {}x{} pixels across {}x{} cells",+ self.pixel_width,+ self.pixel_height,+ physical_cols,+ physical_rows+ );let cell_padding_left = params.cell_padding_left.min(cell_pixel_width.saturating_sub(1) as u16);@@ -88,6 +100,18 @@ impl TerminalState {.source_height.unwrap_or(image_max_height).min(image_max_height);+ // w=0/h=0, or an x=/y= origin at or past the edge of the image, leaves+ // nothing to draw and divides by zero further down.+ anyhow::ensure!(+ draw_width > 0 && draw_height > 0,+ "source rectangle is empty: origin {},{} extent {:?}x{:?} within a {}x{} image",+ params.source_origin_x,+ params.source_origin_y,+ params.source_width,+ params.source_height,+ params.image_width,+ params.image_height+ );let (fullcells_width, remainder_width_cell, x_delta_divisor) = params.columns@@ -283,8 +307,10 @@ impl TerminalState {}}+/// The largest decoded image we are willing to hold, in bytes of RGBA.+pub(crate) const MAX_IMAGE_SIZE: u32 = 100_000_000;+pub(crate) fn check_image_dimensions(width: u32, height: u32) -> anyhow::Result<()> {- const MAX_IMAGE_SIZE: u32 = 100_000_000;let size = width.saturating_mul(height).saturating_mul(4);if size > MAX_IMAGE_SIZE {anyhow::bail!(diff --git a/term/src/terminalstate/kitty.rs b/term/src/terminalstate/kitty.rsindex eb0db83a..9d9cd433 100644--- a/term/src/terminalstate/kitty.rs+++ b/term/src/terminalstate/kitty.rs@@ -17,9 +17,43 @@ use wezterm_escape_parser::apc::{};use wezterm_surface::change::ImageData;+/// A chunked transfer holds every fragment in memory until the final `m=0`+/// arrives. A producer that never sends it — a runaway program as easily as a+/// hostile one — grows the accumulator without bound. The byte limit is set+/// well above a legal single image (100MB decoded is roughly 133MB of base64)+/// so that honest transfers are unaffected.+pub(crate) const MAX_ACCUM_BYTES: usize = 256 * 1024 * 1024;+pub(crate) const MAX_ACCUM_CHUNKS: usize = 65536;++/// An animation grows one frame at a time with no natural end, and `d=f`+/// (drop the frames again) is not implemented, so without a ceiling a client+/// can append until the process dies. Sized for real content: a terminal-sized+/// 640x384 frame is under a megabyte, so this is a few hundred of them.+pub(crate) const MAX_ANIM_BYTES: usize = 256 * 1024 * 1024;++/// The byte cap cannot see the per-frame bookkeeping (frame Vec, duration,+/// hash), which for tiny frames dwarfs the pixels: 256MB worth of 2x2 frames+/// is sixteen million entries. Real animations run into the byte cap first;+/// this one bounds the degenerate shapes.+pub(crate) const MAX_ANIM_FRAMES: usize = 4096;++/// What the opening fragment of a chunked transfer asked for. The fragments+/// that follow carry payload only, so this is captured from the head and+/// decides which command the reassembled transfer becomes.+enum ChunkedTransfer {+ Transmit,+ Display(KittyImagePlacement),+ Frame(KittyImageFrame),+}+#[derive(Debug, Default)]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.+ accumulator_overflowed: bool,max_image_id: u32,number_to_id: HashMap<u32, u32>,id_to_data: HashMap<u32, Arc<ImageData>>,@@ -35,15 +69,25 @@ impl KittyImageState {}fn record_id_to_data(&mut self, image_id: u32, data: Arc<ImageData>) {- if image_id != 0 {- self.remove_data_for_id(image_id);- }+ // Unconditionally, id 0 included. The insert below replaces whatever+ // was at this key either way, so skipping the bookkeeping for the+ // anonymous-transmission key does not keep that image alive — it only+ // loses track of its bytes. Left uncounted, `used_memory` climbs+ // forever, and once it passes the budget `prune_unreferenced` starts+ // evicting every unplaced image on every transfer, which breaks+ // transmit-now-place-later.+ self.remove_data_for_id(image_id);self.prune_unreferenced();self.used_memory += data.len();self.id_to_data.insert(image_id, data);}- fn prune_unreferenced(&mut self) {+ #[cfg(test)]+ pub(crate) fn used_memory(&self) -> usize {+ self.used_memory+ }++ pub(crate) fn prune_unreferenced(&mut self) {let budget = 320 * 1024 * 1024; // FIXME: make this configurableif self.used_memory > budget {let referenced: HashSet<u32> = self.placements.keys().map(|(k, _)| *k).collect();@@ -68,7 +112,31 @@ impl KittyImageState {}}+/// True for a fragment that can only be the continuation of a chunked+/// transfer: no action key (`a=` defaults to `t`), no identifying or format+/// keys, just `m=` and payload. `q=` is deliberately not consulted — the+/// protocol permits it on continuation fragments.+fn is_bare_continuation(img: &KittyImage) -> bool {+ match img {+ KittyImage::TransmitData { transmit, .. } => {+ transmit.format.is_none()+ && transmit.width.is_none()+ && transmit.height.is_none()+ && transmit.image_id.is_none()+ && transmit.image_number.is_none()+ && transmit.compression == KittyImageCompression::None+ && matches!(transmit.data, KittyImageData::Direct(_))+ }+ _ => false,+ }+}+impl TerminalState {+ #[cfg(test)]+ pub(crate) fn kitty_used_memory(&self) -> usize {+ self.kitty_img.used_memory()+ }+fn kitty_img_place(&mut self,image_id: Option<u32>,@@ -100,9 +168,25 @@ impl TerminalState {placement,verbosity);++ // Replace any previous placement under this key before the virtual+ // check below: converting a real placement to a virtual one must+ // erase the pixels the real one painted, or they stay on screen with+ // nothing left that can remove them.if image_id != 0 {self.kitty_remove_placement(image_id, placement.placement_id);}++ if placement.virtual_placement {+ // A virtual placement is a promise that the image is ready; the+ // application decides where it goes by printing U+10EEEE+ // placeholder cells. Rendering those is not implemented, so draw+ // nothing rather than painting a real placement at the cursor —+ // that would put a stray image on screen, move the cursor, and+ // leave the placeholder cells rendering as tofu on top of it.+ log::trace!("ignoring virtual placement for image_id {}", image_id);+ return Ok(());+ }let img = Arc::clone(self.kitty_img.id_to_data.get(&image_id).ok_or_else(|| {anyhow::anyhow!("no matching image id {} in id_to_data for image_number {:?}",@@ -145,6 +229,147 @@ impl TerminalState {Ok(())}+ /// Drops any parked fragments and gives their memory back. `Vec::clear`+ /// would keep the allocation, which is the thing being reclaimed here.+ pub(crate) fn kitty_reset_accumulator(&mut self) {+ self.kitty_img.accumulator = Vec::new();+ self.kitty_img.accumulated_bytes = 0;+ self.kitty_img.accumulator_overflowed = false;+ }++ /// Routes one fragment of a possibly-chunked transfer: park it until the+ /// closing `m=0` arrives, or run it now if this is that closing fragment.+ /// A transfer that outgrows the limits is abandoned rather than allowed to+ /// consume the terminal's memory, and the client is told once.+ fn kitty_accumulate_or_run(+ &mut self,+ img: KittyImage,+ more_data_follows: bool,+ ) -> anyhow::Result<()> {+ if self.kitty_img.accumulator_overflowed {+ // Nothing that arrives after an abandoned transfer can be decoded,+ // since the fragments we would have needed are already gone. But+ // only that transfer's own residue may be swallowed: a true+ // continuation fragment carries nothing but m= and payload, while+ // anything with an identifying key is the next program's opening+ // fragment — if the producer that overflowed was killed, the m=0+ // this latch waits for never comes at all.+ if is_bare_continuation(&img) {+ if !more_data_follows {+ self.kitty_reset_accumulator();+ }+ return Ok(());+ }+ // A client that redundantly repeats its keys on continuation+ // fragments lands here and has its dead transfer's tail parsed as+ // a fresh one, which decodes to garbage and earns an error —+ // still better than eating a healthy image. A keyless standalone+ // transfer is misread the other way, but without s=/v= it could+ // never have decoded anyway.+ self.kitty_reset_accumulator();+ }++ let chunk_len = match &img {+ KittyImage::TransmitData { transmit, .. }+ | KittyImage::TransmitDataAndDisplay { transmit, .. }+ | KittyImage::TransmitFrame { transmit, .. } => transmit.data.in_memory_len(),+ _ => 0,+ };++ // Charge the fragment before taking it, and charge the closing one as+ // well. Letting `m=0` through unmeasured would let a transfer park the+ // whole budget and then hand over a second payload just as large, and+ // taking a fragment before checking would overshoot by however much+ // one APC can carry.+ if self.kitty_img.accumulated_bytes + chunk_len > MAX_ACCUM_BYTES+ || self.kitty_img.accumulator.len() + 1 > MAX_ACCUM_CHUNKS+ {+ self.kitty_reject_transfer(&img, more_data_follows);+ return Ok(());+ }++ if !more_data_follows {+ return self.kitty_img_inner(img);+ }++ self.kitty_img.accumulated_bytes += chunk_len;+ self.kitty_img.accumulator.push(img);++ Ok(())+ }++ /// Abandons the transfer in progress and tells the client once.+ ///+ /// Both the identifying keys and the `q=` verbosity ride on the opening+ /// fragment — continuation fragments carry only `m=`, which would read as+ /// the default `q=0` — so a transfer that asked for silence keeps it.+ fn kitty_reject_transfer(&mut self, current: &KittyImage, more_data_follows: bool) {+ log::warn!(+ "abandoning kitty image transfer: {} bytes across {} chunks reached \+ the {} byte / {} chunk limit",+ self.kitty_img.accumulated_bytes,+ self.kitty_img.accumulator.len(),+ MAX_ACCUM_BYTES,+ MAX_ACCUM_CHUNKS+ );++ let (verbosity, image_id, image_number) = {+ let opening = self.kitty_img.accumulator.first().unwrap_or(current);+ let (image_id, image_number) = match opening {+ KittyImage::TransmitData { transmit, .. }+ | KittyImage::TransmitDataAndDisplay { transmit, .. }+ | KittyImage::TransmitFrame { transmit, .. } => {+ (transmit.image_id, transmit.image_number)+ }+ _ => (None, None),+ };+ (opening.verbosity(), image_id, image_number)+ };++ self.kitty_reset_accumulator();+ // Latch only while the transfer is still running. A rejected closing+ // fragment has already ended it, and latching here would swallow the+ // opening fragment of whatever comes next.+ self.kitty_img.accumulator_overflowed = more_data_follows;++ self.kitty_send_response(+ verbosity,+ false,+ image_id,+ image_number,+ "EFBIG:chunked transfer too large".to_string(),+ );+ }++ /// Abandons the transfer in progress because an unrelated command arrived+ /// in the middle of it, and tells its opener. Leaving the fragments+ /// parked instead means the next transmission is glued onto them: one+ /// image corrupted, the other never created.+ fn kitty_interrupt_transfer(&mut self) {+ let (verbosity, image_id, image_number) = {+ let opening = &self.kitty_img.accumulator[0];+ let (image_id, image_number) = match opening {+ KittyImage::TransmitData { transmit, .. }+ | KittyImage::TransmitDataAndDisplay { transmit, .. }+ | KittyImage::TransmitFrame { transmit, .. } => {+ (transmit.image_id, transmit.image_number)+ }+ _ => (None, None),+ };+ (opening.verbosity(), image_id, image_number)+ };++ self.kitty_reset_accumulator();++ self.kitty_send_response(+ verbosity,+ false,+ image_id,+ image_number,+ "EINVAL:chunked transfer interrupted".to_string(),+ );+ }+fn kitty_img_inner(&mut self, img: KittyImage) -> anyhow::Result<()> {match self.coalesce_kitty_accumulation(img)@@ -163,11 +388,49 @@ impl TerminalState {verbosity,} => {log::trace!("TransmitDataAndDisplay {:#?} {:#?}", transmit, placement);- let image_number = transmit.image_number;- let image_id = self.kitty_img_transmit(transmit, verbosity)?;- self.kitty_img_place(Some(image_id), image_number, placement, verbosity)+ // Store quietly and answer only once the display half is+ // done: acknowledging the transmit alone claims success for+ // an image that may never appear.+ let (image_id, image_number) =+ self.kitty_img_transmit_quietly(transmit, verbosity)?;+ match self.kitty_img_place(Some(image_id), image_number, placement, verbosity) {+ Ok(()) => {+ if image_id != 0 || image_number.is_some() {+ self.kitty_send_response(+ verbosity,+ true,+ Some(image_id),+ image_number,+ "OK".to_string(),+ );+ }+ Ok(())+ }+ Err(err) => {+ if image_id != 0 || image_number.is_some() {+ self.kitty_send_response(+ verbosity,+ false,+ Some(image_id),+ image_number,+ format!("ERROR:{:#}", err),+ );+ }+ Err(err)+ }+ }+ }+ KittyImage::TransmitFrame {+ transmit,+ frame,+ verbosity,+ } => {+ if let Err(err) = self.kitty_frame_transmit(transmit, frame, verbosity) {+ log::error!("Error {:#} while handling KittyImage::TransmitFrame", err,);+ }+ Ok(())}- _ => anyhow::bail!("impossible KittImage variant"),+ other => anyhow::bail!("{:?} is not a transfer", other),}}@@ -176,9 +439,35 @@ impl TerminalState {if !self.config.enable_kitty_graphics() {return Ok(());}++ // The latch never coexists with parked fragments: rejection resets+ // the accumulator before arming it.+ debug_assert!(+ !self.kitty_img.accumulator_overflowed || self.kitty_img.accumulator.is_empty()+ );++ // Any command other than a transmission ends the chunked transfer it+ // interrupts. The spec forbids interleaving, and the alternative is+ // worse than an error: the parked fragments would wait for the next+ // transmission and quietly absorb it. The overflow latch is left+ // alone — that transfer was already reported once, and its remaining+ // fragments may still be streaming in.+ if !self.kitty_img.accumulator.is_empty()+ && !matches!(+ img,+ KittyImage::TransmitData { .. }+ | KittyImage::TransmitDataAndDisplay { .. }+ | KittyImage::TransmitFrame { .. }+ )+ {+ self.kitty_interrupt_transfer();+ }+let verbosity = img.verbosity();match img {- KittyImage::Query { transmit } => match transmit.data.load_data() {+ // `verbosity` above is `img.verbosity()`, which now reports the+ // query's own `q=` rather than assuming q=0.+ KittyImage::Query { transmit, .. } => match transmit.data.load_data() {Ok(_) => {self.kitty_send_response(verbosity,@@ -207,11 +496,7 @@ impl TerminalState {transmit,verbosity,};- if more_data_follows {- self.kitty_img.accumulator.push(img);- } else {- self.kitty_img_inner(img)?;- }+ self.kitty_accumulate_or_run(img, more_data_follows)?;}KittyImage::TransmitDataAndDisplay {transmit,@@ -224,11 +509,7 @@ impl TerminalState {placement,verbosity,};- if more_data_follows {- self.kitty_img.accumulator.push(img);- } else {- self.kitty_img_inner(img)?;- }+ self.kitty_accumulate_or_run(img, more_data_follows)?;}KittyImage::Display {image_id,@@ -275,9 +556,13 @@ impl TerminalState {frame,verbosity,} => {- if let Err(err) = self.kitty_frame_transmit(transmit, frame, verbosity) {- log::error!("Error {:#} while handling KittyImage::TransmitFrame", err,);- }+ let more_data_follows = transmit.more_data_follows;+ let img = KittyImage::TransmitFrame {+ transmit,+ frame,+ verbosity,+ };+ self.kitty_accumulate_or_run(img, more_data_follows)?;}KittyImage::ComposeFrame { frame, verbosity } => {if let Err(err) = self.kitty_frame_compose(frame, verbosity) {@@ -289,6 +574,33 @@ impl TerminalState {Ok(())}+ /// Marks every line covered by a placement of `image_id` as changed.+ ///+ /// Editing an animation edits pixels behind a shared `Arc`: it touches no+ /// `Line` and bumps no sequence number, so the quad cache goes on serving+ /// the frame it first rendered and a mux server decides the line is clean+ /// and never resends it. The image identity is deliberately left alone —+ /// it doubles as the glyph cache's key, and changing it per frame would+ /// rebuild the decoded image on every paint and reset the animation clock.+ fn kitty_touch_placements_for_image(&mut self, image_id: u32) {+ let placements: Vec<(StableRowIndex, usize)> = self+ .kitty_img+ .placements+ .iter()+ .filter(|((id, _), _)| *id == image_id)+ .map(|(_, info)| (info.first_row, info.rows))+ .collect();++ let seqno = self.seqno;+ let screen = self.screen_mut();+ for (first_row, rows) in placements {+ let range = screen.stable_range(&(first_row..first_row + rows as StableRowIndex));+ for idx in range {+ screen.line_mut(idx).update_last_change_seqno(seqno);+ }+ }+ }+fn kitty_remove_placement_from_model(&mut self,image_id: u32,@@ -381,7 +693,12 @@ impl TerminalState {write!(self.writer, "\x1b_GI={};{}\x1b\\", no, message).ok();}(None, None) => {- write!(self.writer, "\x1b_G{}\x1b\\", message).ok();+ // The protocol says not to answer a request that identified no+ // image. There is also nothing well-formed to say: the reply+ // syntax is `<keys> ; <message>`, and with no keys this used to+ // emit `ESC _ G OK ESC \` — a reply that this crate's own+ // parser rejects, because every comma-separated token before+ // the `;` has to contain an `=`.}}self.writer.flush().ok();@@ -439,11 +756,12 @@ impl TerminalState {anyhow::anyhow!("missing target frame")})? as usize;- let img = self- .kitty_img- .id_to_data- .get(&image_id)- .ok_or_else(|| anyhow::anyhow!("invalid image id {}", image_id))?;+ let img = Arc::clone(+ self.kitty_img+ .id_to_data+ .get(&image_id)+ .ok_or_else(|| anyhow::anyhow!("invalid image id {}", image_id))?,+ );let mut img = img.data();match &mut *img {@@ -534,6 +852,9 @@ impl TerminalState {}}+ drop(img);+ self.kitty_touch_placements_for_image(image_id);+Ok(())}@@ -554,7 +875,7 @@ impl TerminalState {}}- let (image_id, image_number, img) = self.kitty_img_transmit_inner(transmit)?;+ let (image_id, image_number, img) = self.kitty_img_transmit_inner(transmit, verbosity)?;let img = match img.decode() {ImageDataType::Rgba8 {@@ -575,8 +896,8 @@ impl TerminalState {(background_pixel & 0xff) as u8,]);- let anim = match self.kitty_img.id_to_data.get(&image_id) {- Some(anim) => anim,+ let image = match self.kitty_img.id_to_data.get(&image_id) {+ Some(anim) => Arc::clone(anim),None => {self.kitty_send_response(verbosity,@@ -593,7 +914,12 @@ impl TerminalState {}};- let mut anim = anim.data();+ // Measured before and after, because frames are appended behind the+ // Mutex: the size reported to the memory budget when the image was+ // transmitted goes stale the moment an animation grows.+ let bytes_before = image.len();++ let mut anim = image.data();let x = frame.x.unwrap_or(0);let y = frame.y.unwrap_or(0);let frame_gap = Duration::from_millis(match frame.duration_ms {@@ -642,7 +968,11 @@ impl TerminalState {*hash = ImageDataType::content_key(data);}Some(2) | None => {- // Create a second frame+ // Create a second frame. Deliberately not gated by+ // the animation caps: this makes exactly two frames+ // of a dimension-checked image, and all further+ // growth goes through the append branch and its+ // gates.let mut new_frame = if base_frame.is_some() {RgbaImage::from_vec(*width, *height, data.clone()).unwrap()@@ -684,6 +1014,37 @@ impl TerminalState {if frame_no == frames.len() as u32 + 1 {// Append a new frame+ // Every frame is exactly width*height*4 bytes, so the+ // held total is a product, not a walk over the frame+ // list on each append.+ let frame_bytes = (*width as usize)+ .saturating_mul(*height as usize)+ .saturating_mul(4);+ let held = frames.len().saturating_mul(frame_bytes);+ if frames.len() + 1 > MAX_ANIM_FRAMES+ || held.saturating_add(frame_bytes) > MAX_ANIM_BYTES+ {+ // Answered from here because the caller only logs+ // errors; a refusal the client never hears leaves it+ // streaming frames into nothing.+ self.kitty_send_response(+ verbosity,+ false,+ Some(image_id),+ image_number,+ "EFBIG:animation too large".to_string(),+ );+ anyhow::bail!(+ "animation for image {} would reach {} bytes across {} \+ frames, over the {} byte / {} frame limit",+ image_id,+ held.saturating_add(frame_bytes),+ frames.len() + 1,+ MAX_ANIM_BYTES,+ MAX_ANIM_FRAMES+ );+ }+let mut new_frame = match frame.base_frame {None => RgbaImage::from_pixel(*width, *height, background_pixel),Some(n) => {@@ -737,17 +1098,35 @@ impl TerminalState {}}+ drop(anim);++ let bytes_after = image.len();+ self.kitty_img.used_memory = self+ .kitty_img+ .used_memory+ .saturating_add(bytes_after)+ .saturating_sub(bytes_before);++ self.kitty_touch_placements_for_image(image_id);+Ok(())}fn kitty_img_transmit_inner(&mut self,transmit: KittyImageTransmit,+ verbosity: KittyImageVerbosity,) -> anyhow::Result<(u32, Option<u32>, ImageDataType)> {log::trace!("transmit {:?}", transmit);let (id, no) = match (transmit.image_id, transmit.image_number) {- (Some(_), Some(_)) => {- // TODO: send an EINVAL error back here+ (Some(id), Some(no)) => {+ self.kitty_send_response(+ verbosity,+ false,+ Some(id),+ Some(no),+ "EINVAL:i= and I= are mutually exclusive".to_string(),+ );anyhow::bail!("cannot use both i= and I= in the same request");}(None, None) => {@@ -756,7 +1135,29 @@ impl TerminalState {}(Some(id), None) => (id, None),(None, Some(no)) => {- let id = self.kitty_img.max_image_id + 1;+ // Claim the id here rather than leaving it to the caller. Only+ // `kitty_img_transmit` used to advance the counter, so an+ // `a=f` naming an unknown image number handed out an id that+ // the next allocation would hand out again.+ //+ // Checked, because the counter follows the largest client+ // chosen i=: after i=4294967295 a plain `+ 1` panics the+ // parser thread in debug builds, and in release it wraps+ // onto 0 — the anonymous-transmission slot.+ let id = match self.kitty_img.max_image_id.checked_add(1) {+ Some(id) => id,+ None => {+ self.kitty_send_response(+ verbosity,+ false,+ None,+ Some(no),+ "ENOSPC:image id space exhausted".to_string(),+ );+ anyhow::bail!("image id space exhausted");+ }+ };+ self.kitty_img.max_image_id = id;self.kitty_img.number_to_id.insert(no, id);(id, Some(no))}@@ -770,8 +1171,19 @@ impl TerminalState {let data = match transmit.compression {KittyImageCompression::None => data,KittyImageCompression::Deflate => {- miniz_oxide::inflate::decompress_to_vec_zlib(&data)- .map_err(|e| anyhow::anyhow!("decompressing data: {:?}", e))?+ // The payload picks its own expansion ratio, and deflate+ // reaches roughly 1000:1, so a few megabytes of `o=z` data+ // inflates to gigabytes long before anything downstream gets+ // to reject it for being too large.+ miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(+ &data,+ MAX_IMAGE_SIZE as usize,+ )+ // Display, not Debug: the error owns everything decompressed+ // before the limit tripped, and debug-formatting it would turn+ // 100MB of bytes into a several-hundred-megabyte string — the+ // very blowup the limit above exists to prevent.+ .map_err(|e| anyhow::anyhow!("decompressing data: {}", e))?}};@@ -822,12 +1234,15 @@ impl TerminalState {Ok((id, no, img))}- fn kitty_img_transmit(+ /// Stores the transmitted image without answering. The caller decides+ /// when success can honestly be claimed — for transmit-and-display that+ /// is only after the display half worked.+ fn kitty_img_transmit_quietly(&mut self,transmit: KittyImageTransmit,verbosity: KittyImageVerbosity,- ) -> anyhow::Result<u32> {- let (image_id, image_number, img) = self.kitty_img_transmit_inner(transmit)?;+ ) -> anyhow::Result<(u32, Option<u32>)> {+ let (image_id, image_number, img) = self.kitty_img_transmit_inner(transmit, verbosity)?;self.kitty_img.max_image_id = self.kitty_img.max_image_id.max(image_id);let img = self@@ -835,7 +1250,20 @@ impl TerminalState {.context("storing image data")?;self.kitty_img.record_id_to_data(image_id, img);- if image_number.is_some() {+ Ok((image_id, image_number))+ }++ fn kitty_img_transmit(+ &mut self,+ transmit: KittyImageTransmit,+ verbosity: KittyImageVerbosity,+ ) -> anyhow::Result<u32> {+ let (image_id, image_number) = self.kitty_img_transmit_quietly(transmit, verbosity)?;++ // Acknowledge whenever the request identified the image, by id or by+ // number. Answering only for `I=` left the far more common `i=` form+ // with no reply at all, success or failure.+ if image_id != 0 || image_number.is_some() {self.kitty_send_response(verbosity,true,@@ -854,16 +1282,21 @@ impl TerminalState {} else {let mut data = vec![];let mut trans;- let place;- let final_verbosity = img.verbosity();+ let kind;self.kitty_img.accumulator.push(img);let mut empty_data = KittyImageData::Direct(String::new());- match self.kitty_img.accumulator.remove(0) {+ // The opening fragment decides what this transfer is and who it is+ // for. Continuation fragments carry only `m=` and payload, so+ // reading the command, the image id or `q=` off the closing+ // fragment would pick up defaults the client never sent.+ let opening = self.kitty_img.accumulator.remove(0);+ let verbosity = opening.verbosity();+ match opening {KittyImage::TransmitData { transmit, .. } => {+ kind = ChunkedTransfer::Transmit;trans = transmit;- place = None;std::mem::swap(&mut empty_data, &mut trans.data);}KittyImage::TransmitDataAndDisplay {@@ -871,21 +1304,36 @@ impl TerminalState {placement,..} => {- place = Some(placement);+ kind = ChunkedTransfer::Display(placement);+ trans = transmit;+ std::mem::swap(&mut empty_data, &mut trans.data);+ }+ KittyImage::TransmitFrame {+ transmit, frame, ..+ } => {+ kind = ChunkedTransfer::Frame(frame);trans = transmit;std::mem::swap(&mut empty_data, &mut trans.data);}- _ => unreachable!(),+ other => anyhow::bail!("{:?} carries no payload to accumulate", other),}data.push(empty_data);+ self.kitty_img.accumulated_bytes = 0;+for item in self.kitty_img.accumulator.drain(..) {match item {KittyImage::TransmitData { transmit, .. }- | KittyImage::TransmitDataAndDisplay { transmit, .. } => {+ | KittyImage::TransmitDataAndDisplay { transmit, .. }+ | KittyImage::TransmitFrame { transmit, .. } => {data.push(transmit.data);}- _ => unreachable!(),+ // A stream that switches to a payload-free command midway+ // through a transfer reaches this, so refuse the transfer+ // rather than panicking on it.+ other => {+ anyhow::bail!("{:?} carries no payload to accumulate", other)+ }}}@@ -908,18 +1356,22 @@ impl TerminalState {trans.data = KittyImageData::DirectBin(b64_decoded);- if let Some(placement) = place {- Ok(KittyImage::TransmitDataAndDisplay {+ Ok(match kind {+ ChunkedTransfer::Transmit => KittyImage::TransmitData {+ transmit: trans,+ verbosity,+ },+ ChunkedTransfer::Display(placement) => KittyImage::TransmitDataAndDisplay {transmit: trans,placement,- verbosity: final_verbosity,- })- } else {- Ok(KittyImage::TransmitData {+ verbosity,+ },+ ChunkedTransfer::Frame(frame) => KittyImage::TransmitFrame {transmit: trans,- verbosity: final_verbosity,- })- }+ frame,+ verbosity,+ },+ })}}}diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rsindex a92eafff..fdbfef1f 100644--- a/term/src/terminalstate/mod.rs+++ b/term/src/terminalstate/mod.rs@@ -29,7 +29,7 @@ use wezterm_surface::{CursorShape, CursorVisibility, SequenceNo};mod image;mod iterm;mod keyboard;-mod kitty;+pub(crate) mod kitty;mod mouse;pub(crate) mod performer;mod sixel;@@ -759,6 +759,17 @@ impl TerminalState {// we need to ensure that we increment the seqno in// order to correctly invalidate the displayself.increment_seqno();++ // Clear the placements first, while their rows still name real+ // lines. The image data stays: applications transmit once and+ // re-place on every redraw, and deleting the data breaks every a=p+ // that follows (kitty keeps it too). The memory ask is served by the+ // budget sweep — with the placements gone the images are+ // unreferenced, and anything over the budget is reclaimed right now.+ self.kitty_remove_all_placements(false);+ self.kitty_img.prune_unreferenced();+ self.kitty_reset_accumulator();+self.erase_in_display(EraseInDisplay::EraseScrollback);let row_index = self.screen.phys_row(self.cursor.y);@@ -1314,6 +1325,7 @@ impl TerminalState {self.screen.activate_primary_screen(self.seqno);self.screen.saved_cursor().take();self.kitty_remove_all_placements(true);+ self.kitty_reset_accumulator();self.reverse_wraparound_mode = false;self.reverse_video_mode = false;diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rsindex 4acbe6cf..a7daa8ee 100644--- a/term/src/terminalstate/performer.rs+++ b/term/src/terminalstate/performer.rs@@ -717,6 +717,14 @@ impl<'a> Performer<'a> {self.accumulating_title.take();self.progress = Progress::default();+ // Before the screen is torn down, while the placement rows+ // still refer to real lines. The soft reset (DECSTR) already+ // did this; leaving it out of the hard reset meant `reset`+ // wiped the display but kept every image pinned in memory,+ // with their ids still resolvable by a later `a=p`.+ self.kitty_remove_all_placements(true);+ self.kitty_reset_accumulator();+self.screen.full_reset();self.screen.activate_alt_screen(seqno);self.erase_in_display(EraseInDisplay::EraseDisplay);diff --git a/term/src/test/graphics.rs b/term/src/test/graphics.rsnew file mode 100644index 00000000..8d96b21b--- /dev/null+++ b/term/src/test/graphics.rs@@ -0,0 +1,861 @@+//! Regression tests for the image attachment path shared by the Kitty+//! graphics, sixel and iTerm protocols.+//!+//! Everything here funnels through `assign_image_to_cells`, which used to+//! divide by zero whenever a pane had no usable pixel geometry or a placement+//! selected an empty source rectangle. Both are reachable from a hostile or+//! merely unlucky byte stream, and a panic there kills the pane's parser+//! thread and freezes the pane.++use super::*;+use crate::color::ColorPalette;+use std::io::Write;+use std::sync::{Arc, Mutex};++#[derive(Debug)]+struct GraphicsConfig {+ kitty: bool,+}++impl TerminalConfiguration for GraphicsConfig {+ fn scrollback_size(&self) -> usize {+ 24+ }++ fn color_palette(&self) -> ColorPalette {+ ColorPalette::default()+ }++ fn enable_kitty_graphics(&self) -> bool {+ self.kitty+ }+}++fn term(pixel_width: usize, pixel_height: usize, kitty: bool) -> Terminal {+ Terminal::new(+ TerminalSize {+ rows: 24,+ cols: 80,+ pixel_width,+ pixel_height,+ dpi: 96,+ },+ Arc::new(GraphicsConfig { kitty }),+ "ThinkTerm",+ "O_o",+ Box::new(Vec::new()),+ )+}++/// A 2x2 all-zero RGBA image, transmitted and stored as image id 1.+const XMIT_2X2: &str = "\x1b_Ga=t,i=1,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\";++/// A 2x2 sixel. The sixel path always passes `columns: None, rows: None`, so it+/// takes the dividing branch that a Kitty placement can avoid with `c=`/`r=`.+const SIXEL_2X2: &str = "\x1bPq\"1;1;2;2#0;2;100;0;0#0~~$-\x1b\\";++/// Captures what the terminal writes back to the application.+#[derive(Clone, Default)]+struct Tap(Arc<Mutex<Vec<u8>>>);++impl Write for Tap {+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {+ self.0.lock().unwrap().extend_from_slice(buf);+ Ok(buf.len())+ }++ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+}++impl Tap {+ /// `TerminalState` hands replies to a `BufWriter<ThreadedWriter>`, so they+ /// arrive on a worker thread rather than during `advance_bytes`. Wait for+ /// `needle` to show up instead of guessing at a delay: replies are written+ /// in order, so once it appears everything sent before it is here too. On+ /// deadline the buffer is returned as-is so the caller's assertion can+ /// print it.+ fn read_until(&self, needle: &str) -> String {+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);+ loop {+ let buf = String::from_utf8_lossy(&self.0.lock().unwrap()).to_string();+ if buf.contains(needle) || std::time::Instant::now() >= deadline {+ return buf;+ }+ std::thread::sleep(std::time::Duration::from_millis(10));+ }+ }+}++/// The image id of the sentinel query `drain` sends. Queries never allocate+/// ids, so it cannot collide with anything a test transmits.+const SENTINEL: &str = "987654321";++/// Returns everything the terminal has replied so far, deterministically:+/// sends a query that always earns a reply, waits for that reply, and strips+/// it. Replies arrive in write order, so a reply that "should not exist" is+/// proven absent rather than merely slow, and no test waits longer than the+/// actual write latency.+///+/// The sentinel is a Query, which aborts a chunked transfer in progress —+/// only drain once the transfer under test has completed or been abandoned.+fn drain(term: &mut Terminal, tap: &Tap) -> String {+ term.advance_bytes(format!(+ "\x1b_Ga=q,i={SENTINEL},f=32,s=1,v=1;AAAAAA==\x1b\\"+ ));+ tap.read_until(&format!("i={SENTINEL}"))+ .replace(&format!("\x1b_Gi={SENTINEL};OK\x1b\\"), "")+}++fn term_with_tap(pixel_width: usize, pixel_height: usize) -> (Terminal, Tap) {+ let tap = Tap::default();+ let term = Terminal::new(+ TerminalSize {+ rows: 24,+ cols: 80,+ pixel_width,+ pixel_height,+ dpi: 96,+ },+ Arc::new(GraphicsConfig { kitty: true }),+ "ThinkTerm",+ "O_o",+ Box::new(tap.clone()),+ );+ (term, tap)+}++fn kitty(pixel_width: usize, pixel_height: usize, place: &str) {+ let mut term = term(pixel_width, pixel_height, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes(place);+}++#[test]+fn kitty_placement_survives_degenerate_pixel_geometry() {+ // tmux panes, thinkterm-tui panes and `mux.spawn_window{width, height}` all+ // reach the terminal with no pixel size at all.+ kitty(0, 384, "\x1b_Ga=p,i=1\x1b\\");+ kitty(640, 0, "\x1b_Ga=p,i=1\x1b\\");+ kitty(0, 0, "\x1b_Ga=p,i=1\x1b\\");+ // Fewer pixels than columns leaves cells zero pixels wide.+ kitty(40, 384, "\x1b_Ga=p,i=1\x1b\\");+ // Specifying c=/r= takes the other branch, which used to reach a NaN+ // instead of a division by zero.+ kitty(0, 0, "\x1b_Ga=p,i=1,c=1,r=1\x1b\\");+}++#[test]+fn kitty_placement_survives_empty_source_rect() {+ // Zero extent, with and without cell scaling.+ kitty(640, 384, "\x1b_Ga=p,i=1,w=0,c=1\x1b\\");+ kitty(640, 384, "\x1b_Ga=p,i=1,h=0,r=1\x1b\\");+ kitty(640, 384, "\x1b_Ga=p,i=1,w=0\x1b\\");+ kitty(640, 384, "\x1b_Ga=p,i=1,h=0\x1b\\");+ // Origin at or past the far edge of a 2x2 image.+ kitty(640, 384, "\x1b_Ga=p,i=1,x=2,c=1\x1b\\");+ kitty(640, 384, "\x1b_Ga=p,i=1,y=2,r=1\x1b\\");+ kitty(640, 384, "\x1b_Ga=p,i=1,x=99,y=99\x1b\\");+}++#[test]+fn sixel_survives_degenerate_pixel_geometry() {+ // The sixel path is not gated by enable_kitty_graphics, so turning Kitty+ // graphics off is not a mitigation for this class of crash.+ for (w, h) in [(0, 384), (640, 0), (0, 0), (40, 384)] {+ let mut term = term(w, h, false);+ term.advance_bytes(SIXEL_2X2);+ }+}++#[test]+fn kitty_placement_still_works_on_a_normal_pane() {+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ let images = cell+ .attrs()+ .images()+ .expect("a placement should attach an image to the cell it covers");+ assert!(!images.is_empty());+}++#[test]+fn sixel_still_works_on_a_normal_pane() {+ let mut term = term(640, 384, false);+ term.advance_bytes(SIXEL_2X2);++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ assert!(+ cell.attrs().images().is_some(),+ "a sixel should attach an image to the cell it covers"+ );+}++/// Half of a 2x2 RGBA image, base64 encoded: eight zero bytes.+const HALF_2X2: &str = "AAAAAAAAAAA=";++#[test]+fn a_chunked_transfer_reassembles() {+ 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\\"+ ));+ term.advance_bytes(format!("\x1b_Gm=0;{HALF_2X2}\x1b\\"));+ term.advance_bytes("\x1b_Ga=p,i=9\x1b\\");++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ assert!(+ cell.attrs().images().is_some(),+ "the two chunks should have reassembled into a placeable image"+ );+}++#[test]+fn an_unterminated_chunked_transfer_is_abandoned() {+ let (mut term, tap) = term_with_tap(640, 384);+ // The opening fragment carries the identifying keys; continuation+ // fragments carry only m=.+ term.advance_bytes("\x1b_Ga=t,i=7,f=32,s=2,v=2,m=1;AAAAAAAAAAA=\x1b\\");+ for _ in 0..70_000 {+ term.advance_bytes("\x1b_Gm=1;AAAAAAAAAAA=\x1b\\");+ }++ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("EFBIG"),+ "an over-long transfer should be refused, got {:?}",+ reply+ );+ assert!(+ reply.matches("EFBIG").count() == 1,+ "the client should be told once, not once per chunk, got {:?}",+ reply+ );+ assert!(+ reply.contains("i=7"),+ "the reply should name the image from the opening fragment, got {:?}",+ reply+ );+}++#[test]+fn a_transfer_after_an_abandoned_one_still_works() {+ let (mut term, _tap) = term_with_tap(640, 384);+ term.advance_bytes("\x1b_Ga=t,i=7,f=32,s=2,v=2,m=1;AAAAAAAAAAA=\x1b\\");+ for _ in 0..70_000 {+ term.advance_bytes("\x1b_Gm=1;AAAAAAAAAAA=\x1b\\");+ }+ // Ending the abandoned transfer should clear the latch.+ term.advance_bytes("\x1b_Gm=0;AAAAAAAAAAA=\x1b\\");++ term.advance_bytes(format!(+ "\x1b_Ga=t,i=11,f=32,s=2,v=2,m=1;{HALF_2X2}\x1b\\"+ ));+ term.advance_bytes(format!("\x1b_Gm=0;{HALF_2X2}\x1b\\"));+ term.advance_bytes("\x1b_Ga=p,i=11\x1b\\");++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ assert!(+ cell.attrs().images().is_some(),+ "a transfer following an abandoned one should succeed"+ );+}++/// Parks exactly `MAX_ACCUM_CHUNKS` fragments, so that the *next* fragment is+/// the first one over the limit. Anything more would trip the cap early and+/// stop the test from saying anything about which fragment was refused.+fn fill_accumulator_to_the_brim(term: &mut Terminal, opening: &str) {+ term.advance_bytes(opening);+ for _ in 0..crate::terminalstate::kitty::MAX_ACCUM_CHUNKS - 1 {+ term.advance_bytes("\x1b_Gm=1;AAAAAAAAAAA=\x1b\\");+ }+}++#[test]+fn the_closing_fragment_is_charged_against_the_cap() {+ let (mut term, tap) = term_with_tap(640, 384);+ fill_accumulator_to_the_brim(&mut term, "\x1b_Ga=t,i=13,f=32,s=2,v=2,m=1;AAAAAAAAAAA=\x1b\\");++ // Nothing has been refused yet, so this closing fragment is the first one+ // over the limit. Letting m=0 through unmeasured would park the whole+ // budget and then accept another payload on top of it.+ term.advance_bytes("\x1b_Gm=0;AAAAAAAAAAA=\x1b\\");++ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("EFBIG"),+ "the closing fragment should be charged and refused too, got {:?}",+ reply+ );+ assert!(+ reply.contains("i=13"),+ "the refusal should name the image from the opening fragment, got {:?}",+ reply+ );+}++#[test]+fn a_rejected_transfer_respects_the_opening_fragments_verbosity() {+ let (mut term, tap) = term_with_tap(640, 384);+ // q=2 asks for silence. Continuation fragments carry only m=, which reads+ // as the default q=0, so the rejection must not consult them.+ term.advance_bytes("\x1b_Ga=t,i=17,q=2,f=32,s=2,v=2,m=1;AAAAAAAAAAA=\x1b\\");+ for _ in 0..70_000 {+ term.advance_bytes("\x1b_Gm=1;AAAAAAAAAAA=\x1b\\");+ }++ let reply = drain(&mut term, &tap);+ assert!(+ reply.is_empty(),+ "a transfer that asked for silence should get it, got {:?}",+ reply+ );+}++#[test]+fn a_rejected_unchunked_transfer_does_not_swallow_the_next_one() {+ let (mut term, _tap) = term_with_tap(640, 384);+ fill_accumulator_to_the_brim(&mut term, "\x1b_Ga=t,i=19,f=32,s=2,v=2,m=1;AAAAAAAAAAA=\x1b\\");+ // Refused as a closing fragment, which ends the transfer; latching on the+ // way out would eat the start of the next one.+ term.advance_bytes("\x1b_Gm=0;AAAAAAAAAAA=\x1b\\");++ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ assert!(+ cell.attrs().images().is_some(),+ "the transfer after a rejected closing fragment should still work"+ );+}++#[test]+fn anonymous_transfers_do_not_inflate_the_memory_accounting() {+ // Every transmission without i= or I= lands on image id 0 and replaces+ // whatever was there. If the replaced image's bytes are not credited back,+ // used_memory climbs forever, and once it passes the prune budget every+ // unplaced image gets evicted on every transfer.+ let mut term = term(640, 384, true);+ let anonymous = "\x1b_Ga=t,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\";++ term.advance_bytes(anonymous);+ let after_one = term.kitty_used_memory();+ assert!(after_one > 0, "the first transfer should be counted");++ for _ in 0..50 {+ term.advance_bytes(anonymous);+ }++ assert!(+ term.kitty_used_memory() == after_one,+ "51 anonymous transfers hold one image, so the accounting should still \+ read one image; got {} where one image is {}",+ term.kitty_used_memory(),+ after_one+ );+}++#[test]+fn a_stored_image_survives_later_anonymous_transfers() {+ // The functional consequence of the accounting bug: transmit-now,+ // place-later stops working once the drift crosses the prune budget.+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ for _ in 0..50 {+ term.advance_bytes("\x1b_Ga=t,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ }+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ assert!(+ cell.attrs().images().is_some(),+ "an image stored with a= t should still be placeable later"+ );+}++#[test]+fn a_virtual_placement_draws_nothing_and_leaves_the_cursor_alone() {+ // U=1 says "the image is ready, I will print U+10EEEE placeholder cells to+ // say where it goes". Rendering those is not implemented, so the placement+ // must draw nothing. Treating U=1 as an ordinary placement paints a stray+ // image at the cursor and moves the cursor, and the placeholder cells the+ // application prints next then render as tofu on top of it.+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);++ let (before_x, before_y) = (term.cursor_pos().x, term.cursor_pos().y);+ term.advance_bytes("\x1b_Ga=p,i=1,U=1,c=20,r=10\x1b\\");+ let (after_x, after_y) = (term.cursor_pos().x, term.cursor_pos().y);++ assert!(+ (before_x, before_y) == (after_x, after_y),+ "a virtual placement must not move the cursor: {:?} -> {:?}",+ (before_x, before_y),+ (after_x, after_y)+ );++ // Nothing was painted, so the line may hold no cells at all — which is the+ // strongest form of "drew nothing".+ let line = term.screen_mut().line_mut(0);+ let attached = line+ .get_cell(0)+ .and_then(|cell| cell.attrs().images())+ .is_some();+ assert!(+ !attached,+ "a virtual placement must not attach an image to any cell"+ );+}++#[test]+fn an_ordinary_placement_is_unaffected_by_the_virtual_check() {+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1,U=0\x1b\\");++ let line = term.screen_mut().line_mut(0);+ let cell = line.get_cell(0).expect("cell 0,0 should exist");+ assert!(+ cell.attrs().images().is_some(),+ "U=0 is an ordinary placement and must still paint"+ );+}++/// Sends `seq` to a fresh terminal and returns whatever it wrote back.+fn reply_to(seq: &str) -> String {+ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes(seq);+ drain(&mut term, &tap)+}++#[test]+fn a_query_without_an_image_id_gets_no_reply() {+ // With neither i= nor I= there are no keys to put in the reply, and the+ // old `ESC _ G OK ESC \` was not parseable by this crate's own parser.+ assert!(+ reply_to("\x1b_Ga=q,f=32,s=1,v=1;AAAAAA==\x1b\\").is_empty(),+ "a query that identified no image should not be answered"+ );+}++#[test]+fn a_query_honours_its_verbosity() {+ let loud = reply_to("\x1b_Ga=q,i=31,f=32,s=1,v=1;AAAAAA==\x1b\\");+ assert!(+ loud.contains("i=31") && loud.contains("OK"),+ "q= absent means answer, got {:?}",+ loud+ );++ assert!(+ reply_to("\x1b_Ga=q,i=31,q=2,f=32,s=1,v=1;AAAAAA==\x1b\\").is_empty(),+ "q=2 asks for silence, including for queries"+ );+}++#[test]+fn a_transmit_identified_by_image_id_is_acknowledged() {+ let reply = reply_to("\x1b_Ga=t,i=99,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ assert!(+ reply.contains("i=99") && reply.contains("OK"),+ "a transfer identified by i= should be acknowledged, got {:?}",+ reply+ );+}++#[test]+fn an_unidentified_transmit_is_not_acknowledged() {+ assert!(+ reply_to("\x1b_Ga=t,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\").is_empty(),+ "a transfer that named no image has nothing to acknowledge"+ );+}++/// Returns the frame count and per-frame gaps of the image attached at 0,0.+fn animation_at_origin(term: &mut Terminal) -> Option<(usize, Vec<std::time::Duration>)> {+ use wezterm_cell::image::ImageDataType;++ let line = term.screen_mut().line_mut(0);+ let images = line.get_cell(0)?.attrs().images()?;+ let data = images.first()?.image_data().data();+ match &*data {+ ImageDataType::AnimRgba8 {+ frames, durations, ..+ } => Some((frames.len(), durations.clone())),+ _ => None,+ }+}++#[test]+fn a_chunked_animation_frame_reaches_the_image() {+ // Raw RGBA frames are far too big to fit one escape sequence, so every+ // real animation arrives chunked. The frame path used to hand the first+ // fragment straight to the decoder, which rejected it as a short payload,+ // while the fragments after it were misread as a fresh image transfer.+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes(format!(+ "\x1b_Ga=f,i=1,f=32,s=2,v=2,z=100,m=1;{HALF_2X2}\x1b\\"+ ));+ term.advance_bytes(format!("\x1b_Gm=0;{HALF_2X2}\x1b\\"));+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let (frames, durations) =+ animation_at_origin(&mut term).expect("the image should have become an animation");+ assert!(+ frames == 2,+ "the base image plus one transmitted frame is two frames, got {}",+ frames+ );+ // And the gap rides on `z=`, not `Z=`: reading the wrong case left every+ // frame on the 40ms default, pinning every animation to 25fps.+ assert!(+ durations[1] == std::time::Duration::from_millis(100),+ "the frame should keep the gap it asked for, got {:?}",+ durations[1]+ );+}++#[test]+fn an_unchunked_animation_frame_still_works() {+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=f,i=1,f=32,s=2,v=2,z=70;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let (frames, durations) =+ animation_at_origin(&mut term).expect("a single-sequence frame should still land");+ assert!(frames == 2, "expected two frames, got {}", frames);+ assert!(durations[1] == std::time::Duration::from_millis(70));+}++#[test]+fn an_interleaved_delete_aborts_the_transfer_and_still_executes() {+ // The spec forbids interleaving other commands into a chunked transfer.+ // The failure being prevented: the parked fragments used to wait around+ // and quietly absorb the next transmission — one image corrupted, the+ // other never created, and the client left waiting for an ack.+ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ term.advance_bytes(format!(+ "\x1b_Ga=t,i=31,f=32,s=2,v=2,m=1;{HALF_2X2}\x1b\\"+ ));+ term.advance_bytes("\x1b_Ga=d,d=i,i=1\x1b\\");++ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("EINVAL") && reply.contains("i=31"),+ "the interrupted transfer's opener should be told, got {:?}",+ reply+ );+ assert!(+ reply.matches("EINVAL").count() == 1,+ "told once, got {:?}",+ reply+ );+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_none(),+ "the interrupting delete should still execute"+ );++ // And nothing is left parked to corrupt the next transfer.+ term.advance_bytes("\x1b_Ga=t,i=32,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ term.advance_bytes("\x1b[H");+ term.advance_bytes("\x1b_Ga=p,i=32\x1b\\");+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "transfers after the aborted one should work"+ );+}++#[test]+fn an_interleaved_placement_aborts_the_transfer_and_still_places() {+ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes(format!(+ "\x1b_Ga=t,i=41,f=32,s=2,v=2,m=1;{HALF_2X2}\x1b\\"+ ));+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("EINVAL") && reply.contains("i=41"),+ "the interrupted transfer's opener should be told, got {:?}",+ reply+ );+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "the interrupting placement should still paint"+ );+}++#[test]+fn a_fresh_keyed_transfer_clears_the_overflow_latch() {+ let (mut term, tap) = term_with_tap(640, 384);+ fill_accumulator_to_the_brim(&mut term, "\x1b_Ga=t,i=51,f=32,s=2,v=2,m=1;AAAAAAAAAAA=\x1b\\");+ // The fragment over the cap still promises more data, so the latch arms.+ term.advance_bytes("\x1b_Gm=1;AAAAAAAAAAA=\x1b\\");++ // The producer dies here: no m=0 ever ends the abandoned transfer. The+ // next program's opening fragment carries identifying keys, which no+ // true continuation fragment does, so it must be let through rather+ // than swallowed — swallowing it eats a whole image and leaves that+ // client waiting for an acknowledgement forever.+ term.advance_bytes("\x1b_Ga=t,i=52,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ term.advance_bytes("\x1b_Ga=p,i=52\x1b\\");++ let reply = drain(&mut term, &tap);+ assert!(+ reply.matches("EFBIG").count() == 1 && reply.contains("i=51"),+ "the abandoned transfer should be reported once, got {:?}",+ reply+ );+ assert!(+ reply.contains("i=52") && reply.contains("OK"),+ "the fresh transfer should be acknowledged, got {:?}",+ reply+ );+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "the fresh transfer should be placeable"+ );+}++#[test]+fn image_id_space_exhaustion_is_enospc_not_a_panic() {+ let (mut term, tap) = term_with_tap(640, 384);+ // Parks the id counter at the ceiling; the ack is expected.+ term.advance_bytes("\x1b_Ga=t,i=4294967295,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ // I= asks the terminal to allocate the next id, which does not exist.+ // The old `+ 1` panicked the parser thread here in debug builds and+ // wrapped onto the anonymous id 0 slot in release builds.+ term.advance_bytes("\x1b_Ga=t,I=1,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");++ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("ENOSPC") && reply.contains("I=1"),+ "id exhaustion should be reported to the client, got {:?}",+ reply+ );++ // Explicitly chosen ids keep working.+ term.advance_bytes("\x1b_Ga=t,i=5,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ term.advance_bytes("\x1b_Ga=p,i=5\x1b\\");+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "explicit ids should still work after exhaustion"+ );+}++#[test]+fn the_animation_frame_cap_is_reported_to_the_client() {+ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes(XMIT_2X2);++ // The first a=f converts the still image into a two-frame animation;+ // each one after appends a frame. Command number MAX_ANIM_FRAMES is+ // therefore the first append past the cap. The byte cap is far away+ // (2x2 frames), so this exercises the frame-count gate.+ for _ in 0..crate::terminalstate::kitty::MAX_ANIM_FRAMES {+ term.advance_bytes("\x1b_Ga=f,i=1,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ }++ let reply = drain(&mut term, &tap);+ assert!(+ reply.matches("EFBIG").count() == 1 && reply.contains("i=1"),+ "the first refused frame should be reported once, got {:?}",+ reply+ );+}++#[test]+fn transmit_and_display_is_not_acknowledged_until_it_displays() {+ // a=T is transmit and display in one command. Answering OK after the+ // transmit half claims success for an image that never appeared — a+ // pane with no pixel geometry makes the display half fail.+ let (mut term, tap) = term_with_tap(0, 0);+ term.advance_bytes("\x1b_Ga=T,i=61,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("i=61") && reply.contains("ERROR") && !reply.contains("OK"),+ "a display that failed must not be acknowledged with OK, got {:?}",+ reply+ );++ let (mut term, tap) = term_with_tap(640, 384);+ term.advance_bytes("\x1b_Ga=T,i=62,f=32,s=2,v=2;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ let reply = drain(&mut term, &tap);+ assert!(+ reply.contains("i=62") && reply.contains("OK"),+ "a display that worked should be acknowledged, got {:?}",+ reply+ );+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "the transmit-and-display image should be on screen"+ );+}++#[test]+fn converting_a_real_placement_to_virtual_erases_it() {+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "the real placement should paint first"+ );++ let (before_x, before_y) = (term.cursor_pos().x, term.cursor_pos().y);+ term.advance_bytes("\x1b_Ga=p,i=1,U=1,c=20,r=10\x1b\\");+ let (after_x, after_y) = (term.cursor_pos().x, term.cursor_pos().y);++ assert!(+ (before_x, before_y) == (after_x, after_y),+ "the virtual conversion must not move the cursor"+ );+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_none(),+ "converting a placement to virtual must erase the real one"+ );+}++#[test]+fn a_frame_transmit_marks_the_placed_rows_changed() {+ // Frame edits mutate pixels behind a shared Arc. Nothing downstream+ // notices unless the lines the image occupies are marked changed: the+ // local quad cache keeps its first render, and a mux server sees a clean+ // line and never resends it.+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");++ let before = term.screen_mut().line_mut(0).current_seqno();+ term.advance_bytes("\x1b_Ga=f,i=1,f=32,s=2,v=2,z=70;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");+ let after = term.screen_mut().line_mut(0).current_seqno();++ assert!(+ after > before,+ "the placed row should be marked changed by a frame transmit: {} -> {}",+ before,+ after+ );+}++#[test]+fn animation_growth_is_counted_against_the_memory_budget() {+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ let one_frame = term.kitty_used_memory();++ term.advance_bytes("\x1b_Ga=f,i=1,f=32,s=2,v=2,z=70;AAAAAAAAAAAAAAAAAAAAAA==\x1b\\");++ assert!(+ term.kitty_used_memory() > one_frame,+ "appending a frame grows the image, so the budget should see it: \+ still {} after growing past {}",+ term.kitty_used_memory(),+ one_frame+ );+}++#[test]+fn ris_releases_image_memory() {+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ assert!(term.kitty_used_memory() > 0);++ term.advance_bytes("\x1bc");++ assert!(+ term.kitty_used_memory() == 0,+ "a hard reset should release image data, still holding {}",+ term.kitty_used_memory()+ );+ // And the id must no longer resolve.+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");+ let line = term.screen_mut().line_mut(0);+ assert!(+ line.get_cell(0).and_then(|c| c.attrs().images()).is_none(),+ "an image dropped by RIS must not be placeable"+ );+}++#[test]+fn clearing_the_scrollback_and_viewport_keeps_reusable_image_data() {+ // Applications transmit an image once and re-place it on every redraw.+ // Clearing the screen erases the placements; the data stays until it+ // outgrows the memory budget (kitty behaves the same way), so an a=p+ // naming the old id after a clear must still work.+ let mut term = term(640, 384, true);+ term.advance_bytes(XMIT_2X2);+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");+ assert!(term.kitty_used_memory() > 0);++ term.erase_scrollback_and_viewport();++ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_none(),+ "clearing must remove the painted placement"+ );++ term.advance_bytes("\x1b[H");+ term.advance_bytes("\x1b_Ga=p,i=1\x1b\\");+ assert!(+ term.screen_mut()+ .line_mut(0)+ .get_cell(0)+ .and_then(|c| c.attrs().images())+ .is_some(),+ "the stored image data should still be placeable after a clear"+ );+}diff --git a/term/src/test/mod.rs b/term/src/test/mod.rsindex 1069fe87..f0d5abe2 100644--- a/term/src/test/mod.rs+++ b/term/src/test/mod.rs@@ -6,6 +6,7 @@ mod c0;use bitflags::bitflags;mod c1;mod csi;+mod graphics;// mod selection; FIXME: port to render layeruse crate::color::ColorPalette;use k9::assert_equal as assert_eq;