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
Scope shape-cache invalidation to what actually changed
RoversX567cedcd4676
diff --git a/lfucache/src/lib.rs b/lfucache/src/lib.rsindex 984d6dbd..954569f1 100644--- a/lfucache/src/lib.rs+++ b/lfucache/src/lib.rs@@ -292,6 +292,30 @@ impl<K: Hash + Eq + Clone + Debug, V, S: Default + BuildHasher> LfuCache<K, V, Sself.total_weight = 0;}+ /// Drop every entry for which `keep` returns false, leaving the rest+ /// untouched (frequency and recency state included). This is the+ /// selective alternative to `clear` for invalidations that only affect+ /// entries matching a predicate.+ pub fn retain(&mut self, mut keep: impl FnMut(&K, &V) -> bool) {+ for bucket in &mut self.buckets {+ let mut cursor = bucket.front_mut();+ while let Some(entry) = cursor.get() {+ if keep(&entry.key, &entry.value) {+ cursor.move_next();+ } else {+ let weight = entry.weight;+ unsafe {+ self.frequency_index.cursor_mut_from_ptr(entry).remove();+ self.recency_index.cursor_mut_from_ptr(entry).remove();+ }+ cursor.remove();+ self.len -= 1;+ self.total_weight = self.total_weight.saturating_sub(weight);+ }+ }+ }+ }+pub fn get<'a, Q: ?Sized + Debug>(&'a mut self, k: &Q) -> Option<&'a V>whereK: Borrow<Q>,diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rsindex d249a5bb..bdf2203a 100644--- a/wezterm-font/src/lib.rs+++ b/wezterm-font/src/lib.rs@@ -141,7 +141,7 @@ impl LoadedFont {let (async_resolve, res) = match self.shape_impl(text,- move || {+ move |_: &[char]| {let _ = tx.send(());},|_| {},@@ -166,7 +166,7 @@ impl LoadedFont {}}- pub fn shape<F: FnOnce() + Send + 'static, FS: FnMut(&mut Vec<char>)>(+ pub fn shape<F: FnOnce(&[char]) + Send + 'static, FS: FnMut(&mut Vec<char>)>(&self,text: &str,completion: F,@@ -188,7 +188,7 @@ impl LoadedFont {Ok(res)}- fn shape_impl<F: FnOnce() + Send + 'static, FS: FnMut(&mut Vec<char>)>(+ fn shape_impl<F: FnOnce(&[char]) + Send + 'static, FS: FnMut(&mut Vec<char>)>(&self,text: &str,completion: F,@@ -348,7 +348,10 @@ impl LoadedFont {struct FallbackResolveInfo {no_glyphs: Vec<char>,pending: Arc<Mutex<Vec<ParsedFont>>>,- completion: Box<dyn FnOnce() + Send>,+ /// Called with the codepoints this resolve was about, so the consumer+ /// can evict only the shape-cache entries containing them instead of+ /// clearing every cache in the window.+ completion: Box<dyn FnOnce(&[char]) + Send>,font_dirs: Arc<FontDatabase>,built_in: Arc<FontDatabase>,locator: Arc<dyn FontLocator + Send + Sync>,@@ -445,14 +448,14 @@ impl FallbackResolveInfo {// LoadedFont missing these same codepoints resolves synchronously// instead of repeating this search and clearing every shape cache.FALLBACK_RESOLVE_MEMO.lock().unwrap().insert(- (self.config.generation(), memo_glyphs),+ (self.config.generation(), memo_glyphs.clone()),extra_handles.clone(),);if !extra_handles.is_empty() {let mut pending = self.pending.lock().unwrap();pending.append(&mut extra_handles);- (self.completion)();+ (self.completion)(&memo_glyphs);}if !wanted.is_empty() {@@ -583,7 +586,7 @@ impl FontConfigInner {Ok(())}- fn schedule_fallback_resolve<F: FnOnce() + Send + 'static>(+ fn schedule_fallback_resolve<F: FnOnce(&[char]) + Send + 'static>(&self,no_glyphs: Vec<char>,pending: &Arc<Mutex<Vec<ParsedFont>>>,diff --git a/wezterm-gui/src/shapecache.rs b/wezterm-gui/src/shapecache.rsindex 6c30baba..32e43073 100644--- a/wezterm-gui/src/shapecache.rs+++ b/wezterm-gui/src/shapecache.rs@@ -336,6 +336,14 @@ impl UiShapeDomainCache {self.cache.clear();}+ /// Evict only the entries whose text contains any of `chars`. Used when+ /// a font fallback resolve lands: entries without the newly resolved+ /// codepoints shaped correctly the first time and stay warm.+ pub fn evict_containing(&mut self, chars: &[char]) {+ self.cache+ .retain(|key, _| !key.text.chars().any(|c| chars.contains(&c)));+ }+pub fn update_config(&mut self, config: &config::ConfigHandle) {self.cache.update_config(config);self.cap = (self.cap_func)(config);@@ -402,6 +410,14 @@ impl UiShapeCaches {self.file_preview.clear();}+ /// Selective cross-domain eviction for a completed font fallback+ /// resolve: see [`UiShapeDomainCache::evict_containing`].+ pub fn evict_containing(&mut self, chars: &[char]) {+ self.chrome.evict_containing(chars);+ self.note.evict_containing(chars);+ self.file_preview.evict_containing(chars);+ }+/// Note idle release: only the Note domain is dropped.pub fn clear_note(&mut self) {self.note.clear();@@ -522,7 +538,7 @@ mod test {let mut infos = font.shape(&cluster.text,- || {},+ |_: &[char]| {},|_| {},None,Direction::LeftToRight,@@ -720,7 +736,7 @@ mod test {let _x = font.shape(&cluster.text,- || {},+ |_: &[char]| {},|_| {},None,Direction::LeftToRight,diff --git a/wezterm-gui/src/termwindow/box_model.rs b/wezterm-gui/src/termwindow/box_model.rsindex 27a5fde1..a9fcb2ef 100644--- a/wezterm-gui/src/termwindow/box_model.rs+++ b/wezterm-gui/src/termwindow/box_model.rs@@ -607,7 +607,7 @@ impl super::TermWindow {let direction = wezterm_bidi::Direction::LeftToRight;let infos = element.font.shape(&s,- move || window.notify(TermWindowNotif::InvalidateShapeCache),+ move |chars: &[char]| window.notify(TermWindowNotif::InvalidateShapeCacheForChars(chars.to_vec())),BlockKey::filter_out_synthetic,element.presentation,direction,diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rsindex ab89a2b1..69cb7130 100644--- a/wezterm-gui/src/termwindow/mod.rs+++ b/wezterm-gui/src/termwindow/mod.rs@@ -350,6 +350,18 @@ pub enum MouseCapture {/// context of the window-specific event looppub enum TermWindowNotif {InvalidateShapeCache,+ /// A font fallback resolve completed for exactly these codepoints. Only+ /// entries whose text contains one of them shaped with a placeholder+ /// and need re-shaping; everything else stays warm. The wholesale+ /// variant above cost the Note pane its entire cache several times per+ /// second while a CJK document's fallbacks trickled in.+ InvalidateShapeCacheForChars(Vec<char>),+ /// Invalidate only the terminal-side shape caches (the ones whose+ /// resolved values embed palette colors). The UI text domains key on+ /// `{font_identity, style, text}` with no color anywhere, so a palette+ /// change must not throw them away: pane OSC palette traffic used to+ /// clear the Note caches every frame and re-shape the whole sidebar.+ InvalidateTerminalShapeCache,PerformAssignment {pane_id: PaneId,assignment: KeyAssignment,@@ -3442,6 +3454,29 @@ impl TermWindow {self.invalidate_modal();window.invalidate();}+ TermWindowNotif::InvalidateShapeCacheForChars(chars) => {+ // The generation bump retires the terminal line caches+ // (their rendered runs may hold placeholder glyphs for these+ // codepoints); the shape caches evict selectively so lines+ // without the new codepoints stay warm.+ self.shape_generation += 1;+ self.shape_cache+ .borrow_mut()+ .retain(|key, _| !key.text.chars().any(|c| chars.contains(&c)));+ self.ui_shape_caches.borrow_mut().evict_containing(&chars);+ self.publish_ui_shape_cache_diagnostics();+ self.invalidate_modal();+ window.invalidate();+ }+ TermWindowNotif::InvalidateTerminalShapeCache => {+ // Bumping the generation retires every color-bearing terminal+ // cache entry (LineToEleShapeCacheKey, PreviewQuadKey carry+ // it). The UI text caches stay: their keys have no colors.+ self.shape_generation += 1;+ self.shape_cache.borrow_mut().clear();+ self.invalidate_modal();+ window.invalidate();+ }TermWindowNotif::PerformAssignment {pane_id,assignment,@@ -3554,10 +3589,11 @@ impl TermWindow {alert: Alert::PaletteChanged,pane_id,} => {- // Shape cache includes color information, so- // ensure that we invalidate that as part of- // this overall invalidation for the palette- self.dispatch_notif(TermWindowNotif::InvalidateShapeCache, window)?;+ // Terminal-side shape caches include resolved palette+ // colors, so invalidate them — but only them. The UI text+ // caches are colorless and clearing them here made every+ // OSC palette write re-shape the whole sidebar.+ self.dispatch_notif(TermWindowNotif::InvalidateTerminalShapeCache, window)?;self.mux_pane_output_event(pane_id);}MuxNotification::Alert {diff --git a/wezterm-gui/src/termwindow/render/fancy_tab_bar.rs b/wezterm-gui/src/termwindow/render/fancy_tab_bar.rsindex ec0d3c15..a1007a89 100644--- a/wezterm-gui/src/termwindow/render/fancy_tab_bar.rs+++ b/wezterm-gui/src/termwindow/render/fancy_tab_bar.rs@@ -1353,7 +1353,7 @@ impl crate::TermWindow {let miss_stage = crate::input_diagnostics::StageTimer::begin(domain.miss_stage_name());let infos = match font.shape(text,- move || window.notify(TermWindowNotif::InvalidateShapeCache),+ move |chars: &[char]| window.notify(TermWindowNotif::InvalidateShapeCacheForChars(chars.to_vec())),BlockKey::filter_out_synthetic,None,Direction::LeftToRight,diff --git a/wezterm-gui/src/termwindow/render/mod.rs b/wezterm-gui/src/termwindow/render/mod.rsindex c799dc54..9c789796 100644--- a/wezterm-gui/src/termwindow/render/mod.rs+++ b/wezterm-gui/src/termwindow/render/mod.rs@@ -888,7 +888,7 @@ impl crate::TermWindow {let hb_started = crate::perf::now();match font.shape(&cluster.text,- move || window.notify(TermWindowNotif::InvalidateShapeCache),+ move |chars: &[char]| window.notify(TermWindowNotif::InvalidateShapeCacheForChars(chars.to_vec())),BlockKey::filter_out_synthetic,Some(cluster.presentation),cluster.direction,@@ -1008,7 +1008,7 @@ impl crate::TermWindow {let hb_started = crate::perf::now();match font.shape(seg,- move || window.notify(TermWindowNotif::InvalidateShapeCache),+ move |chars: &[char]| window.notify(TermWindowNotif::InvalidateShapeCacheForChars(chars.to_vec())),BlockKey::filter_out_synthetic,Some(cluster.presentation),cluster.direction,diff --git a/window/src/os/wayland/frame.rs b/window/src/os/wayland/frame.rsindex dbd01ffb..0cb412b4 100644--- a/window/src/os/wayland/frame.rs+++ b/window/src/os/wayland/frame.rs@@ -451,7 +451,7 @@ impl ConceptFrame {let infos = font.shape(title,- || {+ |_: &[char]| {// TODO: font fallback completed, trigger title repaint!},|_| {