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
fix(mux): keep the screen moving through floods of output
RoversX08cd13263090
diff --git a/mux/src/localpane.rs b/mux/src/localpane.rsindex cd4961cf..303f982a 100644--- a/mux/src/localpane.rs+++ b/mux/src/localpane.rs@@ -1,7 +1,7 @@use crate::domain::DomainId;use crate::pane::{- CachePolicy, CloseReason, ForEachPaneLogicalLine, LogicalLine, Pane, PaneId, PaneSummary,- Pattern, SearchResult, WithPaneLines,+ BatchRead, CachePolicy, CloseReason, ForEachPaneLogicalLine, LogicalLine, Pane, PaneId,+ PaneSummary, Pattern, SearchResult, WithPaneLines,};use crate::renderable::*;use crate::tmux::{TmuxDomain, TmuxDomainState};@@ -401,6 +401,9 @@ pub struct LocalPane {/// The progress the terminal last reported, for `get_progress` while/// the parser holds the terminal.last_progress: Mutex<Progress>,+ /// Waiting for the batch of output being applied to end; see+ /// `Pane::read_after_batch`.+ batch_reads: Mutex<Vec<BatchRead>>,/// What the shell last reported about its commands, for `run_state`/// while the parser holds the terminal.last_shell_commands: Mutex<wezterm_term::ShellCommands>,@@ -459,6 +462,11 @@ impl Pane for LocalPane {self.terminal.try_lock().is_none()}+ fn read_after_batch(&self, read: BatchRead) -> Result<(), BatchRead> {+ self.batch_reads.lock().push(read);+ Ok(())+ }+fn hold_render_state_for_the_test(&self, hold: Duration) {let _held = self.terminal.lock();std::thread::sleep(hold);@@ -786,13 +794,19 @@ impl Pane for LocalPane {term.perform_actions_in_place(actions);self.apply_pending_and_store(&mut term);drop(term);+ // This thread is the one that would take the terminal next, so+ // these find it free without having to be lucky.+ let reads = std::mem::take(&mut *self.batch_reads.lock());+ for read in reads {+ read(self);+ }// A resize that arrived after that last look, while the terminal- // was still held here, found both of its own probes failing and- // left its note; nothing else would read it until the next batch,- // which a quiet program may never write. Looked at again with the- // terminal free, for as long as notes keep arriving: one left- // after the last look is applied by the resize's own second- // probe, which finds the terminal free.+ // was still held here or read just now, found both of its own+ // probes failing and left its note; nothing else would read it+ // until the next batch, which a quiet program may never write.+ // Looked at again with the terminal free, for as long as notes+ // keep arriving: one left after the last look is applied by the+ // resize's own second probe, which finds the terminal free.while self.anything_pending() {let mut term = self.terminal.lock();self.apply_pending_and_store(&mut term);@@ -1916,6 +1930,7 @@ impl LocalPane {pending_seen: Mutex::new(false),shell_idle: Mutex::new(None),last_progress: Mutex::new(Progress::default()),+ batch_reads: Mutex::new(Vec::new()),last_shell_commands,exit: Mutex::new(None),}@@ -3329,6 +3344,70 @@ mod summary_tests {assert_eq!(title(&pane), "second", "a kept pause never resumes");}+ /// What a busy pane's parser is asked to read runs when the batch it is+ /// on ends, once, on the thread that applied the batch, with the batch+ /// in it and the terminal there for the taking.+ #[test]+ fn a_read_left_for_the_parser_runs_after_its_batch() {+ use std::sync::atomic::{AtomicUsize, Ordering};+ let (pane, _sizes, _exit) = pane();+ let batch = |text: &str| {+ let mut parser = termwiz::escape::parser::Parser::new();+ let mut actions = vec![];+ parser.parse(text.as_bytes(), |action| actions.push(action));+ pane.perform_actions(actions);+ };+ let parser_thread = std::thread::current().id();+ let runs = Arc::new(AtomicUsize::new(0));+ let seen = Arc::clone(&runs);+ pane.read_after_batch(Box::new(move |pane| {+ seen.fetch_add(1, Ordering::SeqCst);+ assert_eq!(std::thread::current().id(), parser_thread);+ assert!(!pane.render_state_is_contended(), "the terminal is free");+ let (_, lines) = pane.get_lines(0..1);+ assert!(lines[0].as_str().starts_with("first"), "the batch is in it");+ }))+ .ok()+ .expect("a local pane has a parser to ask");+ assert_eq!(runs.load(Ordering::SeqCst), 0, "no batch yet");++ batch("first");+ assert_eq!(runs.load(Ordering::SeqCst), 1, "after the batch");+ batch(" second");+ assert_eq!(runs.load(Ordering::SeqCst), 1, "and only once");+ }++ /// A resize that lands while a read has the terminal finds both of its+ /// probes failing and leaves its note, and with no output to follow+ /// nothing would come back for it.+ #[test]+ fn a_note_left_during_a_read_is_not_left_behind() {+ let (pane, _sizes, _exit) = pane();+ pane.read_after_batch(Box::new(|pane| {+ let pane = pane.downcast_ref::<LocalPane>().unwrap();+ *pane.pending_focus.lock() = Some(true);+ }))+ .ok()+ .expect("a local pane has a parser to ask");++ pane.perform_actions(vec![]);+ assert!(!pane.anything_pending(), "the batch saw to it");+ }++ /// A pane keeps these, so one that held its pane would never be freed.+ #[test]+ fn a_read_that_never_runs_goes_with_its_pane() {+ let (pane, _sizes, _exit) = pane();+ let held = Arc::new(());+ let kept = Arc::clone(&held);+ pane.read_after_batch(Box::new(move |_| drop(kept)))+ .ok()+ .expect("a local pane has a parser to ask");+ assert_eq!(Arc::strong_count(&held), 2, "waiting for a batch");+ drop(pane);+ assert_eq!(Arc::strong_count(&held), 1, "gone with the pane");+ }+#[cfg(unix)]#[test]fn synchronized_output_survives_reader_pause_and_eof() {diff --git a/mux/src/pane.rs b/mux/src/pane.rsindex 48b9bb75..46aab8b4 100644--- a/mux/src/pane.rs+++ b/mux/src/pane.rs@@ -28,6 +28,10 @@ pub use thinkterm_proto::{PaneId, Pattern, SearchResult};/// Dropping the last subscription releases the upstream subscription and cache.pub trait KittyFrameSubscription: std::fmt::Debug + Send + Sync {}+/// See `Pane::read_after_batch`. Passed the pane rather than holding it, as+/// a pane keeps these and must not end up owning itself.+pub type BatchRead = Box<dyn FnOnce(&dyn Pane) + Send>;+pub type KittyImageFuture = std::pin::Pin<Box<dyn std::future::Future<Output = anyhow::Result<thinkterm_proto::image::GetImageCellResponse>> + Send>>;pub fn alloc_pane_id() -> PaneId {@@ -436,6 +440,19 @@ pub trait Pane: Downcast + Send + Sync {false}+ /// Have the thread applying this pane's output run `read` once the+ /// batch it is on is done, for a reader `render_state_is_contended`+ /// turned away. Between two batches that thread is the only one that+ /// will not miss: a probe from anywhere else has to land in a gap of+ /// about 0.1 ms, and under a flood can go on missing it for a second.+ /// The read never happens if no batch follows, so the caller keeps+ /// another way of finding out. Whatever it does comes off the pane's+ /// throughput, so nothing that looks at the pane's processes belongs+ /// here. Handed back by a pane with no such thread.+ fn read_after_batch(&self, read: BatchRead) -> Result<(), BatchRead> {+ Err(read)+ }+/// Take whatever `render_state_is_contended` reports on and hold it/// for `hold`, sleeping. Only the wedge hook in `send_actions_to_mux`/// uses it; a pane with nothing to hold does nothing.diff --git a/wezterm-font/src/hbwrap.rs b/wezterm-font/src/hbwrap.rsindex 6b9164d3..1e927f83 100644--- a/wezterm-font/src/hbwrap.rs+++ b/wezterm-font/src/hbwrap.rs@@ -1100,6 +1100,29 @@ impl Buffer {hb_buffer_guess_segment_properties(self.buf);}}++ #[cfg(test)]+ pub fn script(&self) -> Option<hb_script_t> {+ let mut props = unsafe { mem::zeroed() };+ unsafe { hb_buffer_get_segment_properties(self.buf, &mut props) };+ (props.script != hb_script_t::HB_SCRIPT_INVALID).then_some(props.script)+ }+}++/// The script `Buffer::guess_segment_properties` settles on for `text`: that+/// of its first character with a script of its own, if it has one.+pub fn guess_script(text: &str) -> Option<hb_script_t> {+ let funcs = unsafe { hb_unicode_funcs_get_default() };+ text.chars()+ .map(|c| unsafe { hb_unicode_script(funcs, c as hb_codepoint_t) })+ .find(|script| {+ !matches!(+ script,+ hb_script_t::HB_SCRIPT_COMMON+ | hb_script_t::HB_SCRIPT_INHERITED+ | hb_script_t::HB_SCRIPT_UNKNOWN+ )+ })}pub struct FontFuncs {diff --git a/wezterm-font/src/shaper/harfbuzz.rs b/wezterm-font/src/shaper/harfbuzz.rsindex f0bb01e1..4af603f5 100644--- a/wezterm-font/src/shaper/harfbuzz.rs+++ b/wezterm-font/src/shaper/harfbuzz.rs@@ -7,9 +7,10 @@ use config::ConfigHandle;use finl_unicode::grapheme_clusters::Graphemes;use log::error;use ordered_float::NotNan;-use std::cell::{RefCell, RefMut};+use std::cell::{Cell, RefCell, RefMut};use std::collections::{HashMap, HashSet};use std::ops::Range;+use std::time::{Duration, Instant};use termwiz::cell::{unicode_column_width, Presentation};use wezterm_bidi::Direction;@@ -71,6 +72,8 @@ struct FontPair {presentation: Presentation,features: Vec<harfbuzz::hb_feature_t>,last_size_and_dpi: RefCell<Option<(f64, u32)>>,+ /// When a pass over this font last left it with nothing shaped.+ last_fruitless: Cell<Instant>,}#[derive(Debug, Hash, PartialEq, Eq, Clone)]@@ -80,6 +83,14 @@ struct MetricsKey {dpi: u32,}+/// How long a fallback that shapes nothing stays loaded after the last pass+/// that tried it. Loading one means opening the file, validating its tables+/// and measuring a glyph, so unloading after each pass was fine for a stray+/// character and ruinous for a screen of them: text a font covers all but a+/// little of goes through it cell by cell, and a frame of letters with+/// combining marks took 3960 loads.+const FRUITLESS_FALLBACK_LINGER: Duration = Duration::from_secs(10);+/// How many characters each font remembers lacking: more than the CJK text/// of a page, and the set starts over once it is full.const MISSING_CHARS_LIMIT: usize = 4096;@@ -91,11 +102,17 @@ pub struct HarfbuzzShaper {/// A run made only of them goes straight on to the next font. Without/// this, every run of CJK text in a UI font loaded each fallback ahead/// of the CJK font again -- face, size, cell metrics -- since a fallback- /// that shapes nothing is unloaded after each pass (see `do_shape`).+ /// that shapes nothing does not stay loaded for long (see+ /// `unload_fruitless_fallbacks`).missing: Vec<RefCell<HashSet<char>>>,+ /// When `unload_fruitless_fallbacks` next looks.+ next_unload: Cell<Instant>,/// How many times each font has been loaded.#[cfg(test)]loads: Vec<std::cell::Cell<usize>>,+ /// The font and the range of each pass, in order.+ #[cfg(test)]+ passes: RefCell<Vec<(FallbackIdx, Range<usize>)>>,lib: ftwrap::Library,metrics: RefCell<HashMap<MetricsKey, FontMetrics>>,features: Vec<harfbuzz::hb_feature_t>,@@ -120,6 +137,36 @@ fn make_question_string(s: &str) -> String {result}+/// Put glyphs shaped from `question`, in place of `text` at `start` of its+/// line, where the graphemes they stand for are and as wide as those are:+/// the rest of the line is laid out from each glyph's cells.+fn place_question_glyphs(+ glyphs: &mut [GlyphInfo],+ question: &str,+ text: &str,+ start: usize,+ presentation_width: Option<&PresentationWidth>,+) {+ let mut next = start;+ let graphemes: Vec<(usize, u8)> = Graphemes::new(text)+ .map(|grapheme| {+ let this = next;+ next += grapheme.len();+ let num_cells = match presentation_width {+ Some(pw) => pw.num_cells(this..next),+ None => unicode_column_width(grapheme, None) as u8,+ };+ (this, num_cells)+ })+ .collect();+ let char_len = question.len() / graphemes.len().max(1);+ for glyph in glyphs {+ let (cluster, num_cells) = graphemes[glyph.cluster as usize / char_len];+ glyph.cluster = cluster as u32;+ glyph.num_cells = num_cells;+ }+}+fn is_question_string(s: &str) -> bool {for c in s.chars() {if c != std::char::REPLACEMENT_CHARACTER {@@ -129,6 +176,14 @@ fn is_question_string(s: &str) -> bool {true}+fn presentation_of(handle: &ParsedFont) -> Presentation {+ if handle.assume_emoji_presentation {+ Presentation::Emoji+ } else {+ Presentation::Text+ }+}+impl HarfbuzzShaper {pub fn new(config: &ConfigHandle, handles: &[ParsedFont]) -> anyhow::Result<Self> {let lib = ftwrap::Library::new()?;@@ -156,8 +211,11 @@ impl HarfbuzzShaper {fonts,handles,missing,+ next_unload: Cell::new(Instant::now() + FRUITLESS_FALLBACK_LINGER),#[cfg(test)]loads,+ #[cfg(test)]+ passes: RefCell::new(vec![]),lib,metrics: RefCell::new(HashMap::new()),features,@@ -165,6 +223,31 @@ impl HarfbuzzShaper {})}+ /// Unload the fallbacks that have shaped nothing lately. They are+ /// probably fonts from the system that are no use for what is on screen,+ /// and not worth keeping around.+ fn unload_fruitless_fallbacks(&self) {+ let now = Instant::now();+ if now < self.next_unload.get() {+ return;+ }+ self.next_unload.set(now + FRUITLESS_FALLBACK_LINGER);+ for (font_idx, opt_pair) in self.fonts.iter().enumerate() {+ let mut opt_pair = opt_pair.borrow_mut();+ let fruitless = opt_pair.as_ref().is_some_and(|pair| {+ !pair.shaped_any+ && now.duration_since(pair.last_fruitless.get()) >= FRUITLESS_FALLBACK_LINGER+ });+ if fruitless {+ log::trace!(+ "Shaper didn't resolve glyphs from {:?}, so unload it",+ self.handles[font_idx]+ );+ opt_pair.take();+ }+ }+ }+/// Whether `font_idx` has been seen to have no glyph for any character/// of `text`, each a grapheme of its own. Characters that combine can/// shape into a glyph none of them has alone -- conjoining Hangul jamo@@ -238,13 +321,10 @@ impl HarfbuzzShaper {face,font: RefCell::new(font),shaped_any: false,- presentation: if handle.assume_emoji_presentation {- Presentation::Emoji- } else {- Presentation::Text- },+ presentation: presentation_of(handle),features,last_size_and_dpi: RefCell::new(None),+ last_fruitless: Cell::new(Instant::now()),});}@@ -267,6 +347,8 @@ impl HarfbuzzShaper {range: Range<usize>,presentation_width: Option<&PresentationWidth>,) -> anyhow::Result<Vec<GlyphInfo>> {+ #[cfg(test)]+ self.passes.borrow_mut().push((font_idx, range.clone()));let mut buf = harfbuzz::Buffer::new()?;// We deliberately omit setting the script and leave it to harfbuzz// to infer from the buffer contents so that it can correctly@@ -295,28 +377,27 @@ impl HarfbuzzShaper {loop {// A font already seen without any of these characters would only- // hand them all on to the next one. (`get`: the retry with a- // question string below keeps the range of the original text.)- if !no_more_fallbacks- && s.get(range.clone()).is_some_and(|text| self.lacks_all(font_idx, text))- {+ // hand them all on to the next one.+ if !no_more_fallbacks && self.lacks_all(font_idx, &s[range.clone()]) {font_idx += 1;continue;}+ // Before loading it: a font only ever passed over would have+ // nothing to show for being loaded, and go again.+ if let (Some(p), Some(handle)) = (presentation, self.handles.get(font_idx)) {+ let font_presentation = presentation_of(handle);+ if font_presentation != p {+ log::trace!(+ "wanted presentation is {p:?} != font \+ presentation {font_presentation:?} so skip \+ font_idx={font_idx}"+ );+ font_idx += 1;+ continue;+ }+ }match self.load_fallback(font_idx, dpi).context("load_fallback")? {Some(mut pair) => {- if let Some(p) = presentation {- if pair.presentation != p {- log::trace!(- "wanted presentation is {p:?} != font \- presentation {:?} so skip \- font_idx={font_idx}",- pair.presentation- );- font_idx += 1;- continue;- }- }let point_size = font_size * self.handles[font_idx].scale.unwrap_or(1.);// Tell harfbuzz to recompute important font metrics!@@ -528,57 +609,94 @@ impl HarfbuzzShaper {let mut direct_clusters = 0;+ // One or more entries of a cluster didn't have a corresponding glyph,+ // so it needs a fallback. Neighbours that do go on together: the+ // coalescing above only joins what got no glyph at all, which leaves+ // letters with marks the font lacks one at a time, and a pass per+ // cell made a screen of them take 70 ms. Only those of one script,+ // though: a pass is shaped under the script guessed from its text,+ // and a cell put under another's does not come out as it should.+ let mut incomplete: Option<(Range<usize>, Option<harfbuzz::hb_script_t>)> = None;+ let shape_incomplete = |run: Range<usize>,+ cluster: &mut Vec<GlyphInfo>,+ no_glyphs: &mut Vec<char>|+ -> anyhow::Result<()> {+ let mut shape = match self.do_shape(+ font_idx + 1,+ s,+ font_size,+ dpi,+ no_glyphs,+ presentation,+ direction,+ // NOT! substr; this is a coalesced sequence of incomplete clusters!+ run.clone(),+ presentation_width,+ ) {+ Ok(shape) => shape,+ Err(e) => {+ let substr = &s[run.clone()];+ error!("{:?} for {:?}", e, substr);+ let question = make_question_string(substr);+ let mut shape = self.do_shape(+ 0,+ &question,+ font_size,+ dpi,+ no_glyphs,+ presentation,+ direction,+ 0..question.len(),+ // That one describes the line, not this.+ None,+ )?;+ place_question_glyphs(+ &mut shape,+ &question,+ substr,+ run.start,+ presentation_width,+ );+ shape+ }+ };+ cluster.append(&mut shape);+ Ok(())+ };+for infos in &info_clusters {let cluster_info = cluster_resolver.get(infos[0].cluster).expect("assigned above");- let sub_range = cluster_info.start..cluster_info.start + cluster_info.byte_len;- let substr = &s[sub_range.clone()];+ let substr = &s[cluster_info.start..cluster_info.start + cluster_info.byte_len];if cluster_info.incomplete {- // One or more entries didn't have a corresponding glyph,- // so try a fallback-- /*- if font_idx == 0 {- log::error!("incomplete cluster for text={:?} {:?}", s, info_clusters);- }- */-let first_info = &infos[0];-- let mut shape = match self.do_shape(- font_idx + 1,- s,- font_size,- dpi,- no_glyphs,- presentation,- direction,- // NOT! substr; this is a coalesced sequence of incomplete clusters!- first_info.cluster..first_info.cluster + first_info.len,- presentation_width,- ) {- Ok(shape) => Ok(shape),- Err(e) => {- error!("{:?} for {:?}", e, substr);- self.do_shape(- 0,- &make_question_string(substr),- font_size,- dpi,- no_glyphs,- presentation,- direction,- sub_range,- presentation_width,- )+ let this = first_info.cluster..first_info.cluster + first_info.len;+ // Coalescing can have joined this cluster's first glyphs to+ // what came before and still left it glyphs of its own.+ if incomplete+ .as_ref()+ .is_some_and(|(run, _)| run.start <= this.start && this.end <= run.end)+ {+ continue;+ }+ let script = harfbuzz::guess_script(&s[this.clone()]);+ match &mut incomplete {+ Some((run, run_script)) if run.end == this.start && *run_script == script => {+ run.end = this.end}- }?;-- cluster.append(&mut shape);+ run => {+ if let Some((run, _)) = run.replace((this, script)) {+ shape_incomplete(run, &mut cluster, no_glyphs)?;+ }+ }+ }continue;}+ if let Some((run, _)) = incomplete.take() {+ shape_incomplete(run, &mut cluster, no_glyphs)?;+ }let total_width: f64 = infos.iter().map(|info| info.x_advance as f64).sum();let mut remaining_cells = cluster_info.cell_width;@@ -607,22 +725,22 @@ impl HarfbuzzShaper {direct_clusters += 1;}}+ if let Some((run, _)) = incomplete.take() {+ shape_incomplete(run, &mut cluster, no_glyphs)?;+ }if !shaped_any {if let Some(opt_pair) = self.fonts.get(font_idx) {- if direct_clusters == 0 {- // If we've never shaped anything from this font, and we didn't- // shape it just now, then we're probably a fallback font from- // the system and unlikely to be useful to keep around, so we- // unload it.- log::trace!(- "Shaper didn't resolve glyphs from {:?}, so unload it",- self.handles[font_idx]- );- opt_pair.borrow_mut().take();- } else if let Some(pair) = &mut *opt_pair.borrow_mut() {- // We shaped something: mark this pair up so that it sticks around- pair.shaped_any = true;+ if let Some(pair) = &mut *opt_pair.borrow_mut() {+ if direct_clusters == 0 {+ // We've never shaped anything from this font, and we+ // didn't shape anything just now: it goes if that+ // stays so (see `unload_fruitless_fallbacks`).+ pair.last_fruitless.set(Instant::now());+ } else {+ // We shaped something: mark this pair up so that it sticks around+ pair.shaped_any = true;+ }}}}@@ -644,6 +762,7 @@ impl FontShaper for HarfbuzzShaper {presentation_width: Option<&PresentationWidth>,) -> anyhow::Result<Vec<GlyphInfo>> {let range = range.unwrap_or_else(|| 0..text.len());+ self.unload_fruitless_fallbacks();log::trace!("shape {range:?} `{}` with presentation={presentation:?}",@@ -806,6 +925,7 @@ impl FontShaper for HarfbuzzShaper {#[derive(Debug)]struct ClusterInfo {start: usize,+ cell_idx: Option<usize>,byte_len: usize,cell_width: u8,incomplete: bool,@@ -813,9 +933,10 @@ struct ClusterInfo {#[derive(Default, Debug)]struct ClusterResolver<'a> {- map: HashMap<usize, ClusterInfo>,+ /// In order of `cell_idx` and `start`: every line of text comes through+ /// here for each font, so these are looked up, not hashed.+ clusters: Vec<ClusterInfo>,presentation_width: Option<&'a PresentationWidth<'a>>,- start_by_cell_idx: HashMap<usize, usize>,}impl<'a> ClusterResolver<'a> {@@ -826,30 +947,20 @@ impl<'a> ClusterResolver<'a> {start: usize,}- let mut map = HashMap::new();-- for info in hb_infos.iter() {- let start = info.cluster as usize;-- // Collect the cell index, which is the "true cluster"- // position from our perspective: the start of the grapheme- let cell_idx = match self.presentation_width {- Some(pw) => {- let cell_idx = pw.byte_to_cell_idx(start);-- let entry = self.start_by_cell_idx.entry(cell_idx).or_insert(start);- *entry = (*entry).min(start);-- Some(cell_idx)+ // Collect the cell index, which is the "true cluster"+ // position from our perspective: the start of the grapheme+ let mut cluster_starts: Vec<Item> = hb_infos+ .iter()+ .map(|info| {+ let start = info.cluster as usize;+ Item {+ cell_idx: self.presentation_width.map(|pw| pw.byte_to_cell_idx(start)),+ start,}- None => None,- };-- map.entry(start).or_insert_with(|| Item { start, cell_idx });- }-- let mut cluster_starts: Vec<Item> = map.into_values().collect();- cluster_starts.sort();+ })+ .collect();+ cluster_starts.sort_unstable();+ cluster_starts.dedup();// If we have multiple entries with the same starting cell index,// remove the duplicates. Don't do this for `None` as that will@@ -860,6 +971,7 @@ impl<'a> ClusterResolver<'a> {_ => false,});+ self.clusters.reserve(cluster_starts.len());let mut iter = cluster_starts.iter().peekable();while let Some(item) = iter.next().copied() {let start = item.start;@@ -869,8 +981,9 @@ impl<'a> ClusterResolver<'a> {Some(p) => p.num_cells(start..next_start),None => unicode_column_width(&s[start..next_start], None) as u8,};- self.map.entry(start).or_insert_with(|| ClusterInfo {+ self.clusters.push(ClusterInfo {start,+ cell_idx: item.cell_idx,byte_len,cell_width,incomplete: false,@@ -878,26 +991,28 @@ impl<'a> ClusterResolver<'a> {}}- pub fn get_mut(&mut self, start: usize) -> Option<&mut ClusterInfo> {+ fn position(&self, start: usize) -> Option<usize> {match self.presentation_width {Some(pw) => {- let cell_idx = pw.byte_to_cell_idx(start);- let actual_start = self.start_by_cell_idx.get(&cell_idx)?;- self.map.get_mut(&actual_start)+ let cell_idx = Some(pw.byte_to_cell_idx(start));+ self.clusters+ .binary_search_by_key(&cell_idx, |cluster| cluster.cell_idx)+ .ok()}- None => self.map.get_mut(&start),+ None => self+ .clusters+ .binary_search_by_key(&start, |cluster| cluster.start)+ .ok(),}}+ pub fn get_mut(&mut self, start: usize) -> Option<&mut ClusterInfo> {+ let position = self.position(start)?;+ self.clusters.get_mut(position)+ }+pub fn get(&self, start: usize) -> Option<&ClusterInfo> {- match self.presentation_width {- Some(pw) => {- let cell_idx = pw.byte_to_cell_idx(start);- let actual_start = self.start_by_cell_idx.get(&cell_idx)?;- self.map.get(&actual_start)- }- None => self.map.get(&start),- }+ self.clusters.get(self.position(start)?)}}@@ -1411,6 +1526,217 @@ mod test {}}+ /// A font with the letter and not its marks gets every cell of a line+ /// wrong in turn. Sent on together they come out as they do one by one.+ #[test]+ fn neighbouring_incomplete_cells_shape_as_they_do_alone() {+ let handles = emoji_last();+ let config = config::configuration();+ let cells = [+ "a\u{300}\u{301}\u{302}",+ "a\u{34f}\u{35c}\u{36f}",+ "b",+ "a\u{30a}\u{324}\u{336}",+ "a\u{361}\u{362}\u{36e}",+ "😀",+ "a\u{34f}\u{301}",+ ];+ let shaper = HarfbuzzShaper::new(&config, &handles).unwrap();+ let together: Vec<_> = shape_with(&shaper, &cells.concat(), None, None)+ .0+ .iter()+ .map(|glyph| (glyph.font_idx, glyph.glyph_pos, glyph.num_cells))+ .collect();+ let alone: Vec<_> = cells+ .iter()+ .flat_map(|cell| {+ let shaper = HarfbuzzShaper::new(&config, &handles).unwrap();+ shape_with(&shaper, cell, None, None).0+ })+ .map(|glyph| (glyph.font_idx, glyph.glyph_pos, glyph.num_cells))+ .collect();+ assert_eq!(together, alone);+ }++ /// A run sent on to a fallback is shaped under one script, HarfBuzz's+ /// guess from its first letter, and a cell of another script does not+ /// shape right under that. So a run is made of cells that guess alike:+ /// each gets what it would have got alone.+ #[test]+ fn a_run_sent_to_a_fallback_keeps_to_one_script() {+ let config = config::configuration();+ let shaper = HarfbuzzShaper::new(&config, &emoji_last()).unwrap();+ let marks = "\u{34f}\u{35c}\u{36f}";+ let latin = format!("a{marks}");+ let cyrillic = format!("\u{434}{marks}");+ let digit = format!("1{marks}");+ let text = [&latin, &latin, &cyrillic, &cyrillic, &digit, &digit, &latin]+ .map(String::as_str)+ .concat();+ shape_with(&shaper, &text, None, None);+ let at = |cells: usize| latin.len() * cells + cells.saturating_sub(2).min(2);+ let runs: Vec<_> = shaper+ .passes+ .borrow()+ .iter()+ .filter(|(font_idx, _)| *font_idx == 1)+ .map(|(_, range)| range.clone())+ .collect();+ assert_eq!(+ runs,+ [at(0)..at(2), at(2)..at(4), at(4)..at(6), at(6)..at(7)],+ "{text:?}"+ );+ }++ /// A font with no glyph for a whole cell and only part of the next, which+ /// it gives an invisible glyph for what follows, joins the two into the+ /// first and then has the second on its own as well. Sent on, the run+ /// already holds that second cell, and it must not go a second time: a+ /// cell drawn twice pushes the rest of the line along.+ #[test]+ fn a_cell_a_run_already_holds_is_not_sent_again() {+ let db = FontDatabase::with_built_in().unwrap();+ let handles: Vec<_> = ["JetBrains Mono", "Noto Color Emoji", "Symbols Nerd Font Mono"]+ .iter()+ .map(|family| db.resolve(&FontAttributes::new(family), 14).unwrap().clone())+ .collect();+ let config = config::configuration();+ let text = "o\u{361}a\u{34f}\u{35c}\u{36f}";+ let line = Line::from_text(text, &CellAttributes::default(), SEQ_ZERO, None);+ let clusters = line.cluster(None);+ let width = PresentationWidth::with_cluster(&clusters[0]);+ let shaper = HarfbuzzShaper::new(&config, &handles).unwrap();+ let (glyphs, _) = shape_with(&shaper, &clusters[0].text, None, Some(&width));+ let placed: Vec<_> = glyphs+ .iter()+ .map(|glyph| (glyph.cluster, glyph.num_cells))+ .collect();+ let cells: usize = glyphs.iter().map(|glyph| glyph.num_cells as usize).sum();+ assert_eq!(cells, 2, "{placed:?}");+ }++ /// `guess_script` is only worth having if it says what HarfBuzz says.+ #[test]+ fn the_script_guessed_is_the_one_harfbuzz_guesses() {+ use crate::hbwrap::hb_script_t::*;+ for (text, script) in [+ ("a\u{301}", Some(HB_SCRIPT_LATIN)),+ ("1\u{301}a", Some(HB_SCRIPT_LATIN)),+ ("\u{434}a", Some(HB_SCRIPT_CYRILLIC)),+ ("\u{e01}\u{e48}", Some(HB_SCRIPT_THAI)),+ ("\u{915}\u{93f}", Some(HB_SCRIPT_DEVANAGARI)),+ ("1\u{301}", None),+ ("\u{301}", None),+ ("", None),+ ] {+ let mut buf = harfbuzz::Buffer::new().unwrap();+ buf.add_str(text, 0..text.len());+ buf.guess_segment_properties();+ assert_eq!(harfbuzz::guess_script(text), script, "{text:?}");+ assert_eq!(buf.script(), script, "{text:?}");+ }+ }++ /// Loading a font to find it is the wrong sort of font cost the load,+ /// and a fallback that way never shapes anything to be kept for.+ #[test]+ fn a_font_of_the_wrong_presentation_is_not_loaded() {+ let config = config::configuration();+ let shaper = HarfbuzzShaper::new(&config, &emoji_last()).unwrap();+ shape_with(&shaper, "\u{1f600}", Some(Presentation::Emoji), None);+ let loads: Vec<_> = shaper.loads.iter().map(std::cell::Cell::get).collect();+ assert_eq!(loads, [0, 0, 0, 1]);+ }++ /// A fallback that will not load leaves what it was for showing as+ /// replacement characters, in the place of what they replace.+ #[test]+ fn text_a_broken_fallback_was_for_stays_where_it_was() {+ let mut handles = emoji_last();+ handles[1].handle.source =+ crate::locator::FontDataSource::OnDisk("/no/such/font.ttf".into());+ let config = config::configuration();+ let shaper = HarfbuzzShaper::new(&config, &handles).unwrap();+ let text = "ab\u{1100}\u{1161}\u{1100}\u{1161}cd";+ let clusters: Vec<_> = shape_with(&shaper, text, None, None)+ .0+ .iter()+ .map(|glyph| glyph.cluster)+ .collect();+ assert_eq!(clusters, [0, 1, 2, 8, 14, 15], "{text:?}");+ }++ /// The window lays a line out by each glyph's cells, so one that takes up+ /// less than what it replaces drags the rest of the line with it.+ #[test]+ fn text_a_broken_fallback_was_for_keeps_its_width() {+ let mut handles = emoji_last();+ handles[1].handle.source =+ crate::locator::FontDataSource::OnDisk("/no/such/font.ttf".into());+ let config = config::configuration();+ let shaper = HarfbuzzShaper::new(&config, &handles).unwrap();+ let line = Line::from_text(+ "x\u{4e2d}\u{6587}y",+ &CellAttributes::default(),+ SEQ_ZERO,+ None,+ );+ let clusters = line.cluster(None);+ assert_eq!(clusters.len(), 1, "one run of text");+ let width = PresentationWidth::with_cluster(&clusters[0]);+ let placed: Vec<_> = shape_with(&shaper, &clusters[0].text, None, Some(&width))+ .0+ .iter()+ .map(|glyph| (glyph.cluster, glyph.num_cells))+ .collect();+ assert_eq!(placed, [(0, 1), (1, 2), (4, 2), (7, 1)]);++ let placed: Vec<_> = shape_with(&shaper, "\u{4e2d}y", None, None)+ .0+ .iter()+ .map(|glyph| (glyph.cluster, glyph.num_cells))+ .collect();+ assert_eq!(placed, [(0, 2), (3, 1)], "with nothing to say how wide");+ }++ /// Text that combines is tried in every font each time, so a fallback+ /// unloaded after each pass was loaded again for each. One that has shaped+ /// nothing goes only once it has been left alone for a while.+ #[test]+ fn a_fallback_that_shapes_nothing_goes_after_a_while() {+ let config = config::configuration();+ let shaper = HarfbuzzShaper::new(&config, &emoji_last()).unwrap();+ let loads = |shaper: &HarfbuzzShaper| {+ shaper+ .loads+ .iter()+ .map(std::cell::Cell::get)+ .collect::<Vec<_>>()+ };+ // Conjoining jamo, which none of these fonts has.+ let text = "\u{1100}\u{1161}";+ shape_with(&shaper, text, None, None);+ let first = loads(&shaper);+ assert!(first[1..].iter().all(|&loads| loads == 1), "{first:?}");+ shape_with(&shaper, text, None, None);+ assert_eq!(loads(&shaper), first, "still loaded");++ let long_ago = Instant::now() - FRUITLESS_FALLBACK_LINGER * 2;+ for opt_pair in &shaper.fonts {+ if let Some(pair) = &*opt_pair.borrow() {+ pair.last_fruitless.set(long_ago);+ }+ }+ shaper.next_unload.set(long_ago);+ // Only Roboto is left, having shaped this; the rest were never any use.+ shape_with(&shaper, "x", None, None);+ assert!(shaper.fonts[0].borrow().is_some(), "it shapes what it can");+ for font_idx in 1..4 {+ assert!(shaper.fonts[font_idx].borrow().is_none(), "font {font_idx}");+ }+ }+/// Characters that combine shape together, so the run is tried even when/// the font lacks each alone; an empty run lacks nothing.#[test]diff --git a/wezterm-mux-server-impl/src/sessionhandler.rs b/wezterm-mux-server-impl/src/sessionhandler.rsindex 8d53c649..edf4b5ee 100644--- a/wezterm-mux-server-impl/src/sessionhandler.rs+++ b/wezterm-mux-server-impl/src/sessionhandler.rs@@ -16,6 +16,7 @@ use mux::{};use promise::spawn::spawn_into_main_thread;use std::collections::{HashMap, VecDeque};+use std::sync::atomic::{AtomicBool, Ordering};use std::sync::{Arc, Mutex, OnceLock};use std::time::{Duration, Instant};use termwiz::surface::SequenceNo;@@ -115,6 +116,17 @@ pub(crate) struct PerPane {frame_subscription: Option<Box<dyn mux::pane::KittyFrameSubscription>>,/// A push task exists for this pane and has not yet read the pane.push_scheduled: bool,+ /// Something asked for a push since the reading on its way was asked+ /// for, and may not be in it: see `finish_push`.+ push_wanted: bool,+ /// Output landed on a push on its way; see `FLOOD_FRAMES_PER_PUSH`.+ output_landed: bool,+ /// Moves whenever what a reading is held against changes, so a reading+ /// made away from the main thread can tell it was overtaken.+ reading_generation: u64,+ /// When this connection last had output sent, or a reading made by the+ /// parser; see `push_pause`.+ last_push: Option<Instant>,/// Terminal input from this connection for this pane, in the order it/// was sent, waiting for the pane to be free. See `drain_pane_inputs`.inputs: VecDeque<QueuedInput>,@@ -206,6 +218,7 @@ impl PerPane {self.frame_revision = None;self.scene_seqno = 0;self.frame_epoch += 1;+ self.reading_generation += 1;if !subscribe {self.frame_subscription = None;}@@ -215,13 +228,69 @@ impl PerPane {/// Claim the one push slot; false when a push is already on its way,/// which will carry whatever the caller wanted sent.fn claim_push(&mut self) -> bool {+ self.push_wanted = self.push_scheduled;!std::mem::replace(&mut self.push_scheduled, true)}+ /// `claim_push` for what the pane's output sets off, the only thing+ /// that tells a flood from a keystroke landing on a push.+ fn claim_push_for_output(&mut self) -> bool {+ let claimed = self.claim_push();+ self.output_landed = !claimed;+ claimed+ }+/// Give the slot back. Done just before the pane is read, so output/// that lands during the read schedules the next push.fn release_push(&mut self) {self.push_scheduled = false;+ self.push_wanted = false;+ self.output_landed = false;+ }++ /// End a push whose reading was made before now, by the pane's parser:+ /// what asked for a push since then may not be in it, and the slot was+ /// taken. True when that happened and the caller keeps the slot and+ /// goes round again; false and the slot is free otherwise. Decided+ /// under the one lock, so that nothing lands between the two.+ fn finish_push(&mut self) -> bool {+ // Found or not: it came off the output's throughput, and a flood that+ // shows nothing would pay for one after every batch.+ self.last_push = Some(Instant::now());+ self.output_landed = false;+ if std::mem::take(&mut self.push_wanted) {+ return true;+ }+ self.push_scheduled = false;+ false+ }++ /// What a reading of the pane needs from here.+ fn push_inputs(&self) -> PushInputs {+ PushInputs {+ last: self.last_sent(),+ frame_control: self.frame_control,+ frame_revision: self.frame_revision,+ scene_seqno: self.scene_seqno,+ viewer: self.viewer,+ generation: self.reading_generation,+ }+ }++ /// How long before this connection's next push of the pane is due, a+ /// `frame` after the last one that counts. A push of a free pane that+ /// found nothing does not: one sets out as soon as a key is written, and+ /// holding the output that key sets off behind it would be what a+ /// flood's frame costs, on every keystroke.+ fn push_pause(&self, frame: Duration, now: Instant) -> Duration {+ let interval = if self.output_landed {+ frame * FLOOD_FRAMES_PER_PUSH+ } else {+ frame+ };+ self.last_push.map_or(Duration::ZERO, |last| {+ interval.saturating_sub(now.saturating_duration_since(last))+ })}/// Queue `item`; Ok(true) when the caller has to start the drain.@@ -249,14 +318,13 @@ impl PerPane {fn record_application_palette(&mut self, palette: Option<ColorPalette>) {self.last_sent_application_palette = Some(palette);+ self.reading_generation += 1;}/// The pane as this connection last described it.fn last_sent(&self) -> LastSent {LastSent {cursor_position: self.cursor_position,- title: self.title.clone(),- working_dir: self.working_dir.clone(),dimensions: self.dimensions,mouse_grabbed: self.mouse_grabbed,alt_screen: self.alt_screen,@@ -266,11 +334,34 @@ impl PerPane {}}+ /// Whether `labels` have anything this connection has not been told.+ fn labels_are_news(&self, labels: &PaneLabels) -> bool {+ self.title != labels.title || self.working_dir != labels.working_dir+ }++ /// A reading of the pane as this connection last described it, for+ /// labels that are all there is to say.+ fn reading_of_nothing_new(&self) -> PaneReading {+ PaneReading {+ mouse_grabbed: self.mouse_grabbed,+ alt_screen: self.alt_screen,+ keyboard_encoding: self.keyboard_encoding,+ bracketed_paste: self.bracketed_paste,+ dimensions: self.dimensions,+ cursor_position: self.cursor_position,+ seqno: self.seqno,+ dirty_lines: Default::default(),+ bonus_lines: Vec::new(),+ input_serial: None,+ }+ }+/// Record what `read_pane_changes` found, and build the push for it.fn commit_changes(&mut self,pane_id: PaneId,reading: PaneReading,+ labels: PaneLabels,) -> GetPaneRenderChangesResponse {let PaneReading {mouse_grabbed,@@ -279,13 +370,12 @@ impl PerPane {bracketed_paste,dimensions,cursor_position,- title,- working_dir,seqno,dirty_lines,bonus_lines,input_serial,} = reading;+ let PaneLabels { title, working_dir } = labels;self.cursor_position = cursor_position;self.title = title.clone();self.working_dir = working_dir.clone();@@ -295,6 +385,12 @@ impl PerPane {self.keyboard_encoding = keyboard_encoding;self.bracketed_paste = bracketed_paste;self.seqno = seqno;+ self.reading_generation += 1;+ // The push for a key goes out with the key, whatever it found. The+ // output the key sets off comes next, and that is not to wait for it.+ if input_serial.is_none() {+ self.last_push = Some(Instant::now());+ }self.sent_images.remember(&bonus_lines);let bonus_lines = bonus_lines.into();@@ -327,8 +423,6 @@ impl PerPane {#[derive(Clone)]struct LastSent {cursor_position: StableCursorPosition,- title: String,- working_dir: Option<Url>,dimensions: RenderableDimensions,mouse_grabbed: bool,alt_screen: bool,@@ -345,14 +439,89 @@ struct PaneReading {bracketed_paste: bool,dimensions: RenderableDimensions,cursor_position: StableCursorPosition,- title: String,- working_dir: Option<Url>,seqno: SequenceNo,dirty_lines: rangeset::RangeSet<StableRowIndex>,bonus_lines: Vec<(StableRowIndex, wezterm_term::Line)>,input_serial: Option<InputSerial>,}+/// The viewport's rows and the cursor's, as a reading copied them.+struct RowsRead {+ first: StableRowIndex,+ viewport: Vec<wezterm_term::Line>,+ cursor: Option<(StableRowIndex, wezterm_term::Line)>,+}++/// A `PaneReading` whose rows have not been gone through yet.+struct RawReading {+ reading: PaneReading,+ rows: Option<RowsRead>,+}++impl RawReading {+ /// Pick the rows to send. Compressing them is most of the cost of a push+ /// of text with anything in it but ASCII, so it is done here, apart from+ /// the reading: a reading can be made by the pane's parser, whose time+ /// goes straight off the output's throughput.+ fn select_rows(self) -> PaneReading {+ let Self { mut reading, rows } = self;+ if let Some(RowsRead {+ first,+ viewport,+ cursor,+ }) = rows+ {+ reading.bonus_lines = select_dirty_lines(first, viewport, &mut reading.dirty_lines);+ // Always send the cursor's row with ordinary changes, as that+ // tends to be the busiest row.+ if let Some((row, mut line)) = cursor {+ line.compress_for_scrollback();+ reading.bonus_lines.push((row, line));+ }+ }+ reading+ }+}++/// A pane's title and directory. Either can take a look at the pane's+/// processes, which is not for its parser to do, so these are read on the+/// main thread, and after the rest of a reading, not before: a title's alert+/// goes out with the push, and the window redraws its title from what the+/// push said. A reading can be asked for in the middle of the batch that set+/// the title, which only the end of that batch makes readable from here.+struct PaneLabels {+ title: String,+ working_dir: Option<Url>,+}++impl PaneLabels {+ fn read(pane: &Arc<dyn Pane>) -> Self {+ Self {+ title: pane.title_without_waiting(),+ working_dir: pane.working_dir_without_waiting(CachePolicy::AllowStale),+ }+ }+}++/// What a reading of the pane needs from `PerPane`, copied out for the same+/// reason `LastSent` is, and so that it can be read off the main thread.+struct PushInputs {+ last: LastSent,+ frame_control: bool,+ frame_revision: Option<u64>,+ scene_seqno: u64,+ viewer: bool,+ generation: u64,+}++/// Everything one push sends, read from the pane by `read_for_push`.+struct PushReading {+ application_palette: Option<ColorPalette>,+ frames: Option<(u64, u64, Vec<wezterm_term::KittyFrameSelection>)>,+ scene_seqno: u64,+ changes: Option<RawReading>,+}+/// Select from owned rows after the pane's terminal lock has been released./// Keep dirty-set traversal, mutation and compression out of that lock:/// under full-screen output these do not save any row copies and would@@ -390,12 +559,12 @@ fn select_dirty_lines(/// with the screen they are on: the bounds read here before them may be/// of the other screen by then.fn read_pane_changes(- pane: &Arc<dyn Pane>,+ pane: &dyn Pane,last: &LastSent,force_with_input_serial: Option<InputSerial>,scene_seqno: u64,viewer: bool,-) -> Option<PaneReading> {+) -> Option<RawReading> {let mut changed = false;let mouse_grabbed = pane.is_mouse_grabbed();if mouse_grabbed != last.mouse_grabbed {@@ -427,18 +596,8 @@ fn read_pane_changes(changed = true;}- let title = pane.get_title();- if title != last.title {- changed = true;- }-- let working_dir = pane.get_current_working_dir(CachePolicy::AllowStale);- if working_dir != last.working_dir {- changed = true;- }-let seqno = pane.get_current_seqno();- let mut all_dirty_lines = pane.get_changed_since(+ let all_dirty_lines = pane.get_changed_since(0..dims.physical_top + dims.viewport_rows as StableRowIndex,last.seqno,);@@ -449,10 +608,10 @@ fn read_pane_changes(let sequence_only = !changed && force_with_input_serial.is_none();if sequence_only && !((last.seqno as u64) < scene_seqno && seqno as u64 >= scene_seqno) { return None; }- let bonus_lines = if sequence_only {+ let rows = if sequence_only {// A relative placement can move without touching text or the cursor.// Acknowledge its terminal sequence without copying unchanged rows.- Vec::new()+ None} else {let read = |lines| {if viewer {@@ -463,33 +622,29 @@ fn read_pane_changes(};let viewport_range =dims.physical_top..dims.physical_top + dims.viewport_rows as StableRowIndex;- let (first_line, lines) = read(viewport_range);- let mut bonus_lines = select_dirty_lines(first_line, lines, &mut all_dirty_lines);-- // Always send the cursor's row with ordinary changes, as that tends- // to be the busiest row. A viewer's may have left the screen.- let (cursor_line_idx, lines) = read(cursor_position.y..cursor_position.y + 1);- if let Some(mut cursor_line) = lines.into_iter().next() {- cursor_line.compress_for_scrollback();- bonus_lines.push((cursor_line_idx, cursor_line));- }- bonus_lines+ let (first, viewport) = read(viewport_range);+ // A viewer's cursor row may have left the screen.+ let (row, lines) = read(cursor_position.y..cursor_position.y + 1);+ Some(RowsRead {+ first,+ viewport,+ cursor: lines.into_iter().next().map(|line| (row, line)),+ })};- Some(PaneReading {+ let reading = PaneReading {bracketed_paste,mouse_grabbed,alt_screen,keyboard_encoding,dimensions: dims,cursor_position,- title,- working_dir,seqno,dirty_lines: all_dirty_lines,- bonus_lines,+ bonus_lines: Vec::new(),input_serial: force_with_input_serial,- })+ };+ Some(RawReading { reading, rows })}/// How long a push waits for a busy pane before looking again: short at@@ -502,6 +657,26 @@ const PUSH_RETRY_MIN: Duration = Duration::from_millis(2);const PUSH_RETRY_MAX: Duration = Duration::from_millis(50);const PUSH_DEFERRAL_LIMIT: Duration = Duration::from_secs(30);+/// The least time between two pushes of a pane to one connection: a frame+/// at `max_fps`, the fastest a window draws. A flood gives a push for every+/// batch, up to a thousand a second, and a client has to take in every one+/// of them to draw at most every eighth: a window behind on rows of+/// many-codepoint cells stopped drawing for seconds. A push waiting here+/// holds the pane's slot, so output landing meanwhile is in what it reads+/// when its time comes, the last of it included. Output after a pause is not+/// held back, and neither is what a key sends: `drain_pane_inputs` pushes+/// that itself.+fn push_interval() -> Duration {+ Duration::from_millis(1000 / config::configuration().max_fps.clamp(1, 1000))+}++/// How many of those a push waits while output goes on landing during the+/// wait: a flood, not a program drawing frames. Half the window's frame rate+/// is all anyone can follow of one, and the other half came straight off the+/// throughput: a frame drawn costs the mux a reading and the machine the+/// drawing of it, for Unicode text 5% of what `kitten __benchmark__` measures.+const FLOOD_FRAMES_PER_PUSH: u32 = 2;+/// The one push slot of a pane, given back however the push ends: a task/// dropped without running, a bail, a panic -- a slot left taken would/// stop the pane from ever pushing to this connection again.@@ -519,6 +694,13 @@ impl PushSlot {}}}++ /// `PerPane::finish_push`: true when the slot is still held.+ fn finish(&mut self) -> bool {+ let held = self.per_pane.lock().unwrap().finish_push();+ self.released = !held;+ held+ }}impl Drop for PushSlot {@@ -527,18 +709,78 @@ impl Drop for PushSlot {}}-/// Push `pane_id`'s changes to this connection once its render state can-/// be read without waiting.+/// A reading asked of a busy pane's parser, and how the two sides agree on+/// which of them makes it.+struct ReadAfterBatch {+ /// Set by whichever side reads: the parser after its batch, or this+ /// side, if it finds the pane free first.+ taken: Arc<AtomicBool>,+ reading: smol::channel::Receiver<PushReading>,+ generation: u64,+}++/// Ask `pane`'s parser to read for this connection after its batch. None for+/// a pane without a parser to ask, and for a connection that reads nothing+/// of the pane.+fn ask_parser_to_read(+ pane: &Arc<dyn Pane>,+ per_pane: &Arc<Mutex<PerPane>>,+) -> Option<ReadAfterBatch> {+ let inputs = {+ let mut per_pane = per_pane.lock().unwrap();+ if per_pane.events_only {+ return None;+ }+ // What asked for a push until now will be in this reading.+ per_pane.push_wanted = false;+ per_pane.push_inputs()+ };+ let generation = inputs.generation;+ let (found, reading) = smol::channel::bounded(1);+ let taken = Arc::new(AtomicBool::new(false));+ let claim = Arc::clone(&taken);+ pane.read_after_batch(Box::new(move |pane| {+ if found.is_closed() || claim.swap(true, Ordering::AcqRel) {+ return;+ }+ found.try_send(read_for_push(pane, &inputs, None)).ok();+ }))+ .ok()?;+ Some(ReadAfterBatch {+ taken,+ reading,+ generation,+ })+}++/// The pane, unless it or the connection has gone while waiting for it.+fn pane_still_wanted(pane_id: PaneId, sender: &PduSender) -> anyhow::Result<Arc<dyn Pane>> {+ if sender.is_closed() {+ anyhow::bail!("connection closed while pane {pane_id} was busy");+ }+ Mux::get()+ .get_pane(pane_id)+ .ok_or_else(|| anyhow!("no such pane {pane_id}"))+}++/// Push `pane_id`'s changes to this connection, reading the pane without+/// the main thread ever waiting for it.////// Pushes run on the main thread with the rest of the mux's work, while/// each pane's parser holds the pane's terminal lock for as long as a/// batch of output takes to apply. Reading here while a pane was busy/// with, say, a very large image stalled every other pane's pushes and/// every request that needs the main thread until that batch was done ---/// with a runaway pane, for good. So the lock is probed first, and a busy-/// pane's push is put off: that pane's own output is what waits, nobody-/// else's. The probe is a moment's glance, not a reservation; a pane that-/// takes the lock back right after it still has to be waited for, once.+/// with a runaway pane, for good. So the lock is probed first. The probe+/// is a moment's glance, not a reservation; a pane that takes the lock+/// back right after it still has to be waited for, once.+///+/// A probe alone is not enough for a pane that is merely busy. Under a+/// flood the lock is free for a tenth of a millisecond between batches of+/// a millisecond, and a retry on a timer lands in that gap by luck: output+/// went a second at a time without reaching the screen. So the first probe+/// to fail leaves the reading with the pane's parser, which makes it right+/// after its batch. The timer stays, for the batch that was the last.async fn push_pane_changes_when_free(pane_id: PaneId,sender: PduSender,@@ -548,29 +790,98 @@ async fn push_pane_changes_when_free(per_pane: Arc::clone(&per_pane),released: false,};- let Some(pane) =- pane_when_free(pane_id, &sender, "this push", Some(PUSH_DEFERRAL_LIMIT)).await?- else {- // Its next output will try again.- return Ok(());- };- // Released before the read, so output that lands during it- // schedules the next push rather than being carried by nobody.- slot.release();- let pushed_at = Instant::now();- let pushed = maybe_push_pane_changes(&pane, sender, per_pane, None);- metrics::histogram!("mux_server.push.latency").record(pushed_at.elapsed());- pushed+ let mut delay = PUSH_RETRY_MIN;+ let mut started = Instant::now();+ let mut asked: Option<ReadAfterBatch> = None;+ loop {+ let pane = pane_still_wanted(pane_id, &sender)?;+ if asked.is_none() {+ let pause = per_pane+ .lock()+ .unwrap()+ .push_pause(push_interval(), Instant::now());+ if !pause.is_zero() {+ smol::Timer::after(pause).await;+ continue;+ }+ }+ if !pane.render_state_is_contended() {+ // Unless the parser has this reading in hand already.+ if !asked+ .as_ref()+ .is_some_and(|asked| asked.taken.swap(true, Ordering::AcqRel))+ {+ // Released before the read, so output that lands during it+ // schedules the next push rather than being carried by+ // nobody.+ slot.release();+ let pushed_at = Instant::now();+ let pushed = maybe_push_pane_changes(&pane, sender, per_pane, None);+ metrics::histogram!("mux_server.push.latency").record(pushed_at.elapsed());+ return pushed;+ }+ } else {+ if started.elapsed() > PUSH_DEFERRAL_LIMIT {+ log::warn!(+ "pane {pane_id} has been busy for {:?}; giving up on this push",+ started.elapsed()+ );+ // Its next output will try again.+ return Ok(());+ }+ if asked.is_none() {+ asked = ask_parser_to_read(&pane, &per_pane);+ }+ }+ metrics::counter!("mux_server.push.deferred").increment(1);+ let waited = match &asked {+ Some(asked) => {+ smol::future::or(async { Some(asked.reading.recv().await) }, async {+ smol::Timer::after(delay).await;+ None+ })+ .await+ }+ None => {+ smol::Timer::after(delay).await;+ None+ }+ };+ delay = (delay * 2).min(PUSH_RETRY_MAX);+ let (Some(waited), Some(asked_for)) = (waited, &asked) else {+ continue;+ };+ let generation = asked_for.generation;+ asked = None;+ // An error is the reading dropped unmade, with its pane: the next+ // look round finds out.+ let Ok(reading) = waited else {+ continue;+ };+ delay = PUSH_RETRY_MIN;+ started = Instant::now();+ metrics::counter!("mux_server.push.after_batch").increment(1);+ // A push made here since overtook this one, which would put an older+ // screen on top of it. The next look round reads afresh.+ if per_pane.lock().unwrap().reading_generation != generation {+ continue;+ }+ let labels = PaneLabels::read(&pane);+ send_push_reading(pane_id, reading, labels, &sender, &per_pane)?;+ if !slot.finish() {+ return Ok(());+ }+ }}/// The pane, once its render state can be read without waiting on its/// parser. With a `limit`, None after that long of nothing but/// contention, with a warning naming `what` gave up; without one the wait/// is for as long as it takes, and the warning says so once. Every read-/// of a pane on the main thread goes through here, so a pane wedged in-/// its parser costs its own readers a wait and nobody else anything. The-/// probe is a moment's glance, not a reservation; a pane that takes the-/// lock back right after it still has to be waited for, once.+/// of a pane on the main thread but a push's goes through here, so a pane+/// wedged in its parser costs its own readers a wait and nobody else+/// anything. The probe is a moment's glance, not a reservation; a pane that+/// takes the lock back right after it still has to be waited for, once.async fn pane_when_free(pane_id: PaneId,sender: &PduSender,@@ -581,12 +892,7 @@ async fn pane_when_free(let started = Instant::now();let mut warned = false;loop {- if sender.is_closed() {- anyhow::bail!("connection closed while pane {pane_id} was busy");- }- let Some(pane) = Mux::get().get_pane(pane_id) else {- anyhow::bail!("no such pane {pane_id}");- };+ let pane = pane_still_wanted(pane_id, sender)?;if !pane.render_state_is_contended() {return Ok(Some(pane));}@@ -858,44 +1164,66 @@ fn relative_scene_seqno(selections: &[wezterm_term::KittyFrameSelection]) -> u64.map(|placement| placement.source_seqno).max().unwrap_or(0)}-/// Read `pane` and push what changed. Nothing of the pane is read while-/// `per_pane` is locked (see `LastSent`); this runs on the main thread,-/// where every writer of a `PerPane`'s state runs, so what is read out of-/// it and what is written back stay consistent.-fn maybe_push_pane_changes(- pane: &Arc<dyn Pane>,- sender: PduSender,- per_pane: Arc<Mutex<PerPane>>,+/// Read `pane` against `inputs`, on whichever thread: nothing here touches+/// `PerPane` (see `LastSent`).+fn read_for_push(+ pane: &dyn Pane,+ inputs: &PushInputs,force_with_input_serial: Option<InputSerial>,-) -> anyhow::Result<()> {- {- // A connection subscribed to events gets the pane's alerts and- // none of what it shows.- let mut per_pane = per_pane.lock().unwrap();- if per_pane.events_only {- return send_pane_alerts(pane.pane_id(), &sender, &mut per_pane);- }- }+) -> PushReading {// Application palette provenance must arrive even when the state is// `None`: that explicit reset prevents a server's configured/advisory- // palette from taking over client rendering. Send it before line changes- // so a real application override is installed before those lines paint.+ // palette from taking over client rendering.let application_palette = pane.palette_override();- let (needs_palette, last, frame_control, frame_revision, mut scene_seqno, viewer) = {- let per_pane = per_pane.lock().unwrap();- (- per_pane.needs_application_palette(&application_palette),- per_pane.last_sent(),- per_pane.frame_control,- per_pane.frame_revision,- per_pane.scene_seqno,- per_pane.viewer,- )- };+ let frames = inputs+ .frame_control+ .then(|| pane.kitty_frame_selections(inputs.frame_revision))+ .flatten();+ let scene_seqno = frames+ .as_ref()+ .map_or(inputs.scene_seqno, |(_, _, selections)| {+ relative_scene_seqno(selections)+ });+ let changes = read_pane_changes(+ pane,+ &inputs.last,+ force_with_input_serial,+ scene_seqno,+ inputs.viewer,+ );+ PushReading {+ application_palette,+ frames,+ scene_seqno,+ changes,+ }+}++/// Send what `read_for_push` found, and `labels`, read since then. This runs+/// on the main thread, where every writer of a `PerPane`'s state runs. The+/// palette goes before the line changes, so a real application override is+/// installed before those lines paint.+fn send_push_reading(+ pane_id: PaneId,+ reading: PushReading,+ labels: PaneLabels,+ sender: &PduSender,+ per_pane: &Arc<Mutex<PerPane>>,+) -> anyhow::Result<()> {+ let PushReading {+ application_palette,+ frames,+ scene_seqno,+ changes,+ } = reading;+ let needs_palette = per_pane+ .lock()+ .unwrap()+ .needs_application_palette(&application_palette);if needs_palette {sender.send(DecodedPdu {pdu: Pdu::SetApplicationPalette(SetApplicationPalette {- pane_id: pane.pane_id(),+ pane_id,palette: application_palette.clone(),}),serial: 0,@@ -906,31 +1234,58 @@ fn maybe_push_pane_changes(.record_application_palette(application_palette);}- if frame_control {- if let Some((revision, image_epoch, selections)) = pane.kitty_frame_selections(frame_revision) {- scene_seqno = relative_scene_seqno(&selections);- sender.send(DecodedPdu {- pdu: Pdu::KittyFrameSelections(KittyFrameSelections {- pane_id: pane.pane_id(), image_epoch, now_ms: wezterm_term::kitty_animation::monotonic_ms(), revision, selections,- }.fitted()),- serial: 0,- })?;- let mut per_pane = per_pane.lock().unwrap();- per_pane.frame_revision = Some(revision);- per_pane.scene_seqno = scene_seqno;- }+ if let Some((revision, image_epoch, selections)) = frames {+ sender.send(DecodedPdu {+ pdu: Pdu::KittyFrameSelections(KittyFrameSelections {+ pane_id, image_epoch, now_ms: wezterm_term::kitty_animation::monotonic_ms(), revision, selections,+ }.fitted()),+ serial: 0,+ })?;+ let mut per_pane = per_pane.lock().unwrap();+ per_pane.frame_revision = Some(revision);+ per_pane.scene_seqno = scene_seqno;+ per_pane.reading_generation += 1;}- let reading = read_pane_changes(pane, &last, force_with_input_serial, scene_seqno, viewer);+ let changes = changes.map(RawReading::select_rows);let mut per_pane = per_pane.lock().unwrap();- if let Some(reading) = reading {- let resp = per_pane.commit_changes(pane.pane_id(), reading);+ let changes = changes.or_else(|| {+ per_pane+ .labels_are_news(&labels)+ .then(|| per_pane.reading_of_nothing_new())+ });+ if let Some(changes) = changes {+ let resp = per_pane.commit_changes(pane_id, changes, labels);sender.send(DecodedPdu {pdu: Pdu::GetPaneRenderChangesResponse(resp),serial: 0,})?;}- send_pane_alerts(pane.pane_id(), &sender, &mut per_pane)+ send_pane_alerts(pane_id, sender, &mut per_pane)+}++/// Read `pane` and push what changed. Nothing of the pane is read while+/// `per_pane` is locked (see `LastSent`); this runs on the main thread,+/// where every writer of a `PerPane`'s state runs, so what is read out of+/// it and what is written back stay consistent.+fn maybe_push_pane_changes(+ pane: &Arc<dyn Pane>,+ sender: PduSender,+ per_pane: Arc<Mutex<PerPane>>,+ force_with_input_serial: Option<InputSerial>,+) -> anyhow::Result<()> {+ let inputs = {+ // A connection subscribed to events gets the pane's alerts and+ // none of what it shows.+ let mut per_pane = per_pane.lock().unwrap();+ if per_pane.events_only {+ return send_pane_alerts(pane.pane_id(), &sender, &mut per_pane);+ }+ per_pane.push_inputs()+ };+ let reading = read_for_push(pane.as_ref(), &inputs, force_with_input_serial);+ let labels = PaneLabels::read(pane);+ send_push_reading(pane.pane_id(), reading, labels, &sender, &per_pane)}fn send_pane_alerts(@@ -1508,7 +1863,9 @@ impl SessionHandler {fn subscribe_events(&mut self) {self.events_subscribed = true;for per_pane in self.per_pane.values() {- per_pane.lock().unwrap().events_only = true;+ let mut per_pane = per_pane.lock().unwrap();+ per_pane.events_only = true;+ per_pane.reading_generation += 1;}}@@ -1561,7 +1918,7 @@ impl SessionHandler {// One push on its way per pane. A burst of output notifications// used to queue a task each, and all but the first found nothing// new to send.- if !per_pane.lock().unwrap().claim_push() {+ if !per_pane.lock().unwrap().claim_push_for_output() {return;}spawn_into_main_thread(push_pane_changes_when_free(pane_id, sender, per_pane)).detach();@@ -2759,6 +3116,7 @@ impl SessionHandler {}state.frame_revision = Some(snapshot.0);state.scene_seqno = relative_scene_seqno(&snapshot.2);+ state.reading_generation += 1;(snapshot.0, snapshot.1, snapshot.2, state.scene_seqno != 0)};send_response(Ok(Pdu::KittyFrameSelections(KittyFrameSelections { pane_id, image_epoch, now_ms: wezterm_term::kitty_animation::monotonic_ms(), revision, selections }.fitted())));@@ -3897,13 +4255,17 @@ mod web_server_address_tests {#[cfg(test)]mod tests {use super::{- claim_viewport_for_pane, requires_existing_frontend_access, workspace_for_moved_pane,- InputDrain, PaneInput, PerPane, QueuedInput,+ claim_viewport_for_pane, requires_existing_frontend_access, send_push_reading,+ workspace_for_moved_pane, InputDrain, PaneInput, PaneLabels, PaneReading, PduSender,+ PerPane, PushReading, QueuedInput,};- use codec::{EnsureThinkTermThread, Pdu};+ use codec::{EnsureThinkTermThread, Pdu, WireKeyboardEncoding};use mux::client::ClientId;+ use mux::renderable::{RenderableDimensions, StableCursorPosition};use mux::Mux;- use std::sync::Arc;+ use std::sync::{Arc, Mutex};+ use std::time::{Duration, Instant};+ use termwiz::surface::SequenceNo;use wezterm_term::color::ColorPalette;use wezterm_term::TerminalSize;@@ -4410,6 +4772,244 @@ mod tests {);}+ #[test]+ fn a_reading_made_by_the_parser_is_followed_by_what_landed_after_it() {+ let mut state = PerPane::default();+ assert!(state.claim_push());+ assert!(+ !state.finish_push(),+ "nothing landed: the push is over and the slot free"+ );++ assert!(state.claim_push(), "the slot was free");+ assert!(!state.claim_push(), "output lands on a push on its way");+ assert!(+ state.finish_push(),+ "it may not be in the reading, and nothing else will carry it"+ );+ assert!(+ !state.claim_push(),+ "going round keeps the slot, so there is one push at a time"+ );+ state.release_push();+ assert!(+ !state.finish_push(),+ "a release reads past what landed before it"+ );+ }++ fn no_labels() -> PaneLabels {+ PaneLabels {+ title: String::new(),+ working_dir: None,+ }+ }++ #[test]+ fn a_reading_overtaken_on_its_way_back_can_tell() {+ let mut state = PerPane::default();+ let asked = state.push_inputs();+ assert_eq!(state.reading_generation, asked.generation);++ state.set_frame_control(true);+ assert_ne!(+ state.reading_generation, asked.generation,+ "what the reading was held against has moved"+ );++ let asked = state.push_inputs();+ state.commit_changes(1, reading_at(7), no_labels());+ assert_ne!(+ state.reading_generation, asked.generation,+ "a push made here since would be covered up by an older screen"+ );++ let asked = state.push_inputs();+ state.record_application_palette(Some(ColorPalette::default()));+ assert_ne!(+ state.reading_generation, asked.generation,+ "an older reading would put the palette back"+ );+ }++ #[test]+ fn a_reading_overtaken_by_a_subscription_to_events_can_tell() {+ use super::SessionHandler;+ let mut handler = SessionHandler::new(PduSender::new(|_| Ok(())));+ let per_pane = handler.per_pane(1);+ let asked = per_pane.lock().unwrap().push_inputs();+ handler.subscribe_events();+ let generation = per_pane.lock().unwrap().reading_generation;+ assert_ne!(+ generation, asked.generation,+ "the connection no longer wants what the reading found"+ );+ }++ /// What a key sets off is the next thing to go out, however soon: the+ /// push the key itself made holds nothing back, and neither does one+ /// that set out when the key was written and found nothing yet.+ #[test]+ fn a_push_holds_back_the_next_only_if_it_sent_output() {+ // Far longer than this could ever take, so only the rule is tested.+ let frame = Duration::from_secs(3600);+ let per_pane = Arc::new(Mutex::new(PerPane::default()));+ let sender = PduSender::new(|_| Ok(()));+ let due_now = || {+ per_pane+ .lock()+ .unwrap()+ .push_pause(frame, Instant::now())+ .is_zero()+ };++ let nothing = PushReading {+ application_palette: None,+ frames: None,+ scene_seqno: 0,+ changes: None,+ };+ per_pane.lock().unwrap().claim_push();+ send_push_reading(1, nothing, no_labels(), &sender, &per_pane).unwrap();+ per_pane.lock().unwrap().release_push();+ assert!(due_now(), "nothing went out");++ let mut key = reading_at(1);+ key.input_serial = Some(codec::InputSerial::now());+ per_pane.lock().unwrap().commit_changes(1, key, no_labels());+ assert!(due_now(), "that answered a key");++ per_pane+ .lock()+ .unwrap()+ .commit_changes(1, reading_at(2), no_labels());+ assert!(!due_now(), "a frame went out");+ }++ /// The window redraws its title on the alert, from what the last push+ /// said, and nothing tells it to look again: the alert has to come after+ /// the title it is about, whatever else there was to say.+ #[test]+ fn a_title_goes_out_ahead_of_its_alert() {+ use codec::DecodedPdu;+ use wezterm_term::Alert;++ let sent: Arc<Mutex<Vec<DecodedPdu>>> = Arc::new(Mutex::new(Vec::new()));+ let sink = Arc::clone(&sent);+ let sender = PduSender::new(move |pdu| {+ sink.lock().unwrap().push(pdu);+ Ok(())+ });+ let per_pane = Arc::new(Mutex::new(PerPane::default()));+ per_pane+ .lock()+ .unwrap()+ .notifications+ .push(Alert::WindowTitleChanged("vim".to_string()));+ let nothing = PushReading {+ application_palette: Some(ColorPalette::default()),+ frames: None,+ scene_seqno: 0,+ changes: None,+ };+ let labels = PaneLabels {+ title: "vim".to_string(),+ working_dir: None,+ };+ send_push_reading(1, nothing, labels, &sender, &per_pane).unwrap();++ let sent = sent.lock().unwrap();+ let names: Vec<&str> = sent.iter().map(|pdu| pdu.pdu.pdu_name()).collect();+ assert_eq!(+ names,+ [+ "SetApplicationPalette",+ "GetPaneRenderChangesResponse",+ "NotifyAlert"+ ]+ );+ let Pdu::GetPaneRenderChangesResponse(push) = &sent[1].pdu else {+ unreachable!()+ };+ assert_eq!(push.title, "vim");+ }++ /// Output that changes nothing on the screen is no less of a flood, and+ /// what the parser reads for it comes off the output's throughput.+ #[test]+ fn a_reading_the_parser_made_counts_whatever_it_found() {+ let frame = Duration::from_secs(3600);+ let mut state = PerPane::default();+ assert!(state.claim_push());+ assert!(!state.finish_push());+ assert!(!state.push_pause(frame, Instant::now()).is_zero());+ }++ fn reading_at(seqno: SequenceNo) -> PaneReading {+ PaneReading {+ mouse_grabbed: false,+ alt_screen: false,+ keyboard_encoding: WireKeyboardEncoding::default(),+ bracketed_paste: false,+ dimensions: RenderableDimensions::default(),+ cursor_position: StableCursorPosition::default(),+ seqno,+ dirty_lines: Default::default(),+ bonus_lines: Vec::new(),+ input_serial: None,+ }+ }++ #[test]+ fn pushes_of_a_pane_are_at_least_a_frame_apart() {+ let frame = Duration::from_millis(8);+ let now = Instant::now();+ let mut state = PerPane::default();+ assert!(+ state.push_pause(frame, now).is_zero(),+ "the first goes at once"+ );++ state.last_push = Some(now);+ assert_eq!(state.push_pause(frame, now), frame, "the next waits");+ assert_eq!(+ state.push_pause(frame, now + frame / 2),+ frame / 2,+ "for what is left of the frame"+ );+ assert!(+ state.push_pause(frame, now + frame).is_zero(),+ "output after a pause is not held back"+ );+ }++ /// Output landing on a push on its way is a flood's mark: a program+ /// drawing frames gives none while the frame's push waits. Nothing else+ /// that asks for a push is.+ #[test]+ fn a_flood_of_output_is_pushed_half_as_often() {+ let frame = Duration::from_millis(8);+ let now = Instant::now();+ let mut state = PerPane {+ last_push: Some(now),+ ..PerPane::default()+ };++ assert!(state.claim_push_for_output());+ assert!(!state.claim_push());+ assert_eq!(state.push_pause(frame, now), frame, "a key is no flood");++ assert!(!state.claim_push_for_output());+ assert_eq!(state.push_pause(frame, now), 2 * frame);++ assert!(state.finish_push(), "the output landed since the reading");+ assert_eq!(+ state.push_pause(frame, now),+ frame,+ "and each wait is judged alone"+ );+ }+#[test]fn application_palette_delivery_distinguishes_unsent_from_reset() {let mut state = PerPane::default();