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
thinkterm-session: fetch the rows a scroll is about to reveal, and warm the local scrollback
RoversX482e4acd4660
diff --git a/thinkterm-session/src/host.rs b/thinkterm-session/src/host.rsindex 34ed1666..cfff80a8 100644--- a/thinkterm-session/src/host.rs+++ b/thinkterm-session/src/host.rs@@ -101,6 +101,12 @@ pub trait HostConfig {fn hyperlink_rules(&self) -> std::sync::Arc<Vec<termwiz::hyperlink::Rule>>;/// How many line prefetch batches a second the pane may ask for.fn fetch_rate_per_second(&self) -> u32;+ /// How far past a painted range rows are fetched ahead, on each side,+ /// in viewports. Zero fetches only what is painted.+ fn scrollback_lookahead_screens(&self) -> usize;+ /// Whether a pane's whole scrollback is fetched once it is first shown,+ /// so scrolling never paints a row that is still on its way.+ fn warm_scrollback(&self) -> bool;}/// Everything the session tells the host about a pane. Synchronous, anddiff --git a/thinkterm-session/src/pane.rs b/thinkterm-session/src/pane.rsindex 820ef659..2b88069c 100644--- a/thinkterm-session/src/pane.rs+++ b/thinkterm-session/src/pane.rs@@ -442,9 +442,132 @@ impl<H: SessionHost> PaneSession<H> {);self.schedule_fetch_lines(st.dead, to_fetch, fetch_token);+ self.fetch_ahead(st, &lines, fetch_token);(lines.start, result)}+ /// Rows a scroll is about to reveal are asked for before they are+ /// painted: a row fetched on demand arrives a frame after it became+ /// visible, and a fast scroll paints a fresh band of blank rows every+ /// frame. Uncached rows around `painted` are tagged Fetching and+ /// requested separately, so the painted rows' fetch is not held up;+ /// once per cache epoch, the whole scrollback is fetched the same way+ /// where the host asks for it (the session server of this machine).+ ///+ /// Two limits keep this speculation harmless. Rows are only tagged+ /// while the cache can hold them beside the painted range: an insert+ /// past the capacity would evict a painted row, whose fetch would then+ /// be discarded and re-issued on every paint. And each speculative+ /// batch is admitted by the host's fetch rate limit first, so a slow+ /// link is not flooded; a refused batch leaves its rows untagged for a+ /// later paint to try again.+ fn fetch_ahead(+ self: &Arc<Self>,+ st: &mut PaneState,+ painted: &Range<StableRowIndex>,+ fetch_token: FetchToken,+ ) {+ if st.dead || !st.received {+ return;+ }+ let config = self.host.config();+ let dims = st.dimensions;+ let lowest = dims.scrollback_top;+ let highest = dims.physical_top + dims.viewport_rows as StableRowIndex;+ let painted_rows = painted.end.saturating_sub(painted.start).max(0) as usize;+ let mut budget = st.lines.cap().get().saturating_sub(painted_rows);+ let rate = config.fetch_rate_per_second();+ let now = self.now();++ // Uncached rows among the `limit` rows of `rows` nearest its+ // `nearest` end. The limit bounds the region, cached rows included:+ // bounding only the misses would let a band wider than the cache+ // fetch a different slice on every paint, each evicting the last.+ let uncached =+ |st: &mut PaneState, rows: Range<StableRowIndex>, limit: usize, nearest: Nearest| {+ let mut set = RangeSet::new();+ let mut seen = 0;+ let mut walk = rows.clone();+ while seen < limit {+ let idx = match nearest {+ Nearest::End => match walk.next_back() {+ Some(idx) => idx,+ None => break,+ },+ Nearest::Start => match walk.next() {+ Some(idx) => idx,+ None => break,+ },+ };+ if idx < lowest || idx >= highest {+ continue;+ }+ seen += 1;+ if !st.lines.contains(&idx) {+ set.add(idx);+ }+ }+ set+ };+ let tag = |st: &mut PaneState, set: &RangeSet<StableRowIndex>| {+ for r in set.iter() {+ for idx in r.clone() {+ st.lines.put(idx, LineEntry::Fetching(fetch_token));+ }+ }+ };++ let band =+ (dims.viewport_rows.max(1) * config.scrollback_lookahead_screens()) as StableRowIndex;+ if band > 0 && budget > 0 {+ let mut ahead = uncached(+ st,+ painted.start - band..painted.start,+ budget / 2,+ Nearest::End,+ );+ let below = uncached(+ st,+ painted.end..painted.end + band,+ budget / 2,+ Nearest::Start,+ );+ for r in below.iter() {+ ahead.add_range(r.clone());+ }+ if !ahead.is_empty() {+ if !st.fetch_limiter.admit(rate, 1, now) {+ log::trace!("fetch ahead of {:?} refused by the rate limit", painted);+ return;+ }+ let count: usize = ahead.iter().map(|r| r.len()).sum();+ budget = budget.saturating_sub(count);+ tag(st, &ahead);+ log::trace!("fetching ahead {:?} around {:?}", ahead, painted);+ self.schedule_fetch_lines(st.dead, ahead, fetch_token);+ }+ }++ let grew = dims.physical_top - st.warmed_top >= dims.viewport_rows as StableRowIndex;+ if config.warm_scrollback()+ && budget > 0+ && (st.warmed_epoch != Some(st.line_cache_epoch) || grew)+ {+ let warm = uncached(st, lowest..highest, budget, Nearest::End);+ if !warm.is_empty() {+ if !st.fetch_limiter.admit(rate, 1, now) {+ log::trace!("scrollback warm refused by the rate limit");+ return;+ }+ tag(st, &warm);+ log::trace!("warming scrollback {:?}", warm);+ self.schedule_fetch_lines(st.dead, warm, fetch_token);+ }+ st.warmed_epoch = Some(st.line_cache_epoch);+ st.warmed_top = dims.physical_top;+ }+ }+pub fn changed_since(self: &Arc<Self>,lines: Range<StableRowIndex>,@@ -1042,6 +1165,13 @@ impl<H: SessionHost> Drop for DrainingDeltas<H> {}}+/// Which end of a range `fetch_ahead` takes rows from first.+#[derive(Clone, Copy)]+enum Nearest {+ Start,+ End,+}+#[cfg(test)]mod tests {use super::*;@@ -1117,13 +1247,32 @@ mod tests {fn input_recorded(&self) {}}- struct TestConfig;+ struct TestConfig {+ lookahead: Cell<usize>,+ warm: Cell<bool>,+ rate: Cell<u32>,+ }+ impl Default for TestConfig {+ fn default() -> Self {+ Self {+ lookahead: Cell::new(0),+ warm: Cell::new(false),+ rate: Cell::new(100),+ }+ }+ }impl HostConfig for TestConfig {fn hyperlink_rules(&self) -> Arc<Vec<termwiz::hyperlink::Rule>> {Arc::new(Vec::new())}fn fetch_rate_per_second(&self) -> u32 {- 100+ self.rate.get()+ }+ fn scrollback_lookahead_screens(&self) -> usize {+ self.lookahead.get()+ }+ fn warm_scrollback(&self) -> bool {+ self.warm.get()}}@@ -1133,6 +1282,7 @@ mod tests {struct TestLink {rows: RefCell<Vec<(StableRowIndex, String)>>,asked: RefCell<Vec<&'static str>>,+ rows_asked: Cell<usize>,}impl PduLink for TestLink {type Request = LocalFuture<Result<Pdu, LinkError>>;@@ -1140,6 +1290,9 @@ mod tests {self.asked.borrow_mut().push(pdu.pdu_name());let answer = match pdu {Pdu::GetLines(req) => {+ self.rows_asked.set(+ self.rows_asked.get() + req.lines.iter().map(|r| r.len()).sum::<usize>(),+ );let table = self.rows.borrow();let lines: Vec<(StableRowIndex, Line)> = req.lines@@ -1244,19 +1397,26 @@ mod tests {}fn session(rows: &[(StableRowIndex, &str)]) -> (Arc<TestHost>, Arc<PaneSession<TestHost>>) {+ session_with_cache(rows, 256)+ }++ fn session_with_cache(+ rows: &[(StableRowIndex, &str)],+ scrollback_lines: usize,+ ) -> (Arc<TestHost>, Arc<PaneSession<TestHost>>) {let host = Arc::new(TestHost {clock: TestClock(Cell::new(0)),spawner: TestSpawner::default(),events: TestEvents::default(),link: TestLink::default(),- config: TestConfig,+ config: TestConfig::default(),});*host.link.rows.borrow_mut() = rows.iter().map(|(r, t)| (*r, t.to_string())).collect();let session = PaneSession::new(Arc::clone(&host),Arc::new(Lock::new(ImageStore::default())),SessionConfig {- scrollback_lines: 256,+ scrollback_lines,local_echo_threshold_ms: None,overlay_lag_indicator: false,},@@ -1371,6 +1531,182 @@ mod tests {);}+ fn rows_requested(host: &TestHost) -> usize {+ host.link.rows_asked.get()+ }++ /// Rows just outside a painted range are fetched with it, tagged so a+ /// later paint does not ask twice, and only within the scrollback.+ #[test]+ fn a_paint_fetches_the_rows_a_scroll_would_reveal_next() {+ let (host, session) = session(&(-24..24).map(|r| (r, "row")).collect::<Vec<_>>());+ host.config.lookahead.set(1);+ // dims(): 24 viewport rows, physical_top 0, scrollback_top 0.+ session.queue_render_delta(delta(1, false, false));+ host.spawner.run_all();+ host.link.rows_asked.set(0);++ let _ = session.get_lines(10..14);+ host.spawner.run_all();+ // 4 painted + up to 24 on each side, clamped to rows 0..24: 0..10+ // below and 14..24 above, none beyond the scrollback top.+ assert_eq!(rows_requested(&host), 24);+ let st = session.state();+ assert!(matches!(st.lines.peek(&0), Some(LineEntry::Line(_))));+ assert!(matches!(st.lines.peek(&23), Some(LineEntry::Line(_))));+ assert!(st.lines.peek(&-1).is_none(), "nothing above the scrollback top");+ drop(st);++ host.link.rows_asked.set(0);+ let _ = session.get_lines(10..14);+ host.spawner.run_all();+ assert_eq!(rows_requested(&host), 0, "everything around is cached now");+ }++ /// The whole scrollback is fetched once per cache epoch when the host+ /// asks for it, and again after the epoch moves.+ #[test]+ fn a_warming_host_fetches_the_scrollback_once_per_epoch() {+ let (host, session) = session(&(0..24).map(|r| (r, "row")).collect::<Vec<_>>());+ host.config.warm.set(true);+ session.queue_render_delta(delta(1, false, false));+ host.spawner.run_all();+ host.link.rows_asked.set(0);++ let _ = session.get_lines(20..24);+ host.spawner.run_all();+ assert_eq!(rows_requested(&host), 24, "the painted rows plus the rest");+ let _ = session.get_lines(0..4);+ host.spawner.run_all();+ assert_eq!(rows_requested(&host), 24, "already warm");++ let mut size = size();+ size.cols += 1;+ assert!(session.apply_local_resize(size));+ let _ = session.get_lines(20..24);+ host.spawner.run_all();+ assert!(rows_requested(&host) > 24, "a new epoch warms again");+ }++ /// Output that scrolled past between two pushes was never sent; once+ /// the physical top has moved a viewport, the scrollback is warmed+ /// again for the rows that appeared.+ #[test]+ fn scrollback_that_grew_a_viewport_is_warmed_again() {+ let (host, session) = session(&(0..72).map(|r| (r, "row")).collect::<Vec<_>>());+ host.config.warm.set(true);+ session.queue_render_delta(delta(1, false, false));+ host.spawner.run_all();+ let _ = session.get_lines(0..24);+ host.spawner.run_all();+ host.link.rows_asked.set(0);++ let mut grown = delta(2, false, false);+ grown.dimensions.physical_top = 48;+ grown.dimensions.scrollback_rows = 72;+ session.queue_render_delta(grown);+ host.spawner.run_all();+ let _ = session.get_lines(48..72);+ host.spawner.run_all();+ // The painted 48..72 plus the never-pushed 24..48; 0..24 is cached.+ assert_eq!(rows_requested(&host), 48);+ }++ /// Speculation never pushes a painted row out of the cache: with a+ /// cache barely larger than the viewport, the lookahead is cut to what+ /// fits beside the painted rows, which all come back as lines.+ #[test]+ fn speculative_fetches_leave_the_painted_rows_in_the_cache() {+ // scrollback_lines below the floor: the cache holds 128 rows.+ let (host, session) =+ session_with_cache(&(0..200).map(|r| (r, "row")).collect::<Vec<_>>(), 100);+ host.config.lookahead.set(3);+ let mut tall = delta(1, false, false);+ tall.dimensions.physical_top = 176;+ tall.dimensions.scrollback_rows = 200;+ session.queue_render_delta(tall);+ host.spawner.run_all();+ host.link.rows_asked.set(0);++ let _ = session.get_lines(176..200);+ host.spawner.run_all();+ let st = session.state();+ for row in 176..200 {+ assert!(+ matches!(st.lines.peek(&row), Some(LineEntry::Line(_))),+ "painted row {row} survived the speculation"+ );+ }+ assert!(st.lines.len() <= 128);+ // 24 painted plus at most the 104 the cache has room for.+ assert!(rows_requested(&host) <= 128, "{}", rows_requested(&host));+ assert!(rows_requested(&host) > 24, "some lookahead happened");+ }++ /// A band wider than the cache settles: the second paint of the same+ /// range finds the bounded region cached and asks for nothing, rather+ /// than fetching a fresh slice that evicts the last one every frame.+ #[test]+ fn speculation_wider_than_the_cache_settles_on_the_second_paint() {+ let (host, session) =+ session_with_cache(&(0..168).map(|r| (r, "row")).collect::<Vec<_>>(), 100);+ host.config.lookahead.set(3);+ let mut tall = delta(1, false, false);+ tall.dimensions.physical_top = 128;+ tall.dimensions.scrollback_rows = 168;+ session.queue_render_delta(tall);+ host.spawner.run_all();+ host.link.rows_asked.set(0);++ let _ = session.get_lines(64..104);+ host.spawner.run_all();+ let first = rows_requested(&host);+ assert!(first > 40, "painted rows plus a bounded lookahead");++ for _ in 0..3 {+ let _ = session.get_lines(64..104);+ host.spawner.run_all();+ }+ assert_eq!(rows_requested(&host), first, "nothing more on repeated paints");+ }++ /// A speculative batch that the fetch rate limit refuses leaves its+ /// rows untagged, so a later paint can ask for them once the limit+ /// allows.+ #[test]+ fn speculative_fetches_respect_the_fetch_rate_limit() {+ let (host, session) = session(&(0..72).map(|r| (r, "row")).collect::<Vec<_>>());+ host.config.lookahead.set(1);+ host.config.rate.set(1);+ let mut tall = delta(1, false, false);+ tall.dimensions.physical_top = 48;+ tall.dimensions.scrollback_rows = 72;+ session.queue_render_delta(tall);+ host.spawner.run_all();++ // One batch is admitted: rows 36..60 and 64..72 around the paint.+ let _ = session.get_lines(60..64);+ host.spawner.run_all();+ assert!(matches!(+ session.state().lines.peek(&40),+ Some(LineEntry::Line(_))+ ));++ // The next, in the same second, is refused: nothing is tagged.+ let _ = session.get_lines(30..34);+ host.spawner.run_all();+ assert!(session.state().lines.peek(&10).is_none(), "left for later");++ // A second later the limit allows it again.+ host.clock.0.set(1_000_000);+ let _ = session.get_lines(30..34);+ host.spawner.run_all();+ assert!(matches!(+ session.state().lines.peek(&10),+ Some(LineEntry::Line(_))+ ));+ }+/// The input path: queue, drain through the link in order, settle.#[test]fn queued_input_leaves_in_order_and_settles() {diff --git a/thinkterm-session/src/pane_state.rs b/thinkterm-session/src/pane_state.rsindex a66d5664..a6895acc 100644--- a/thinkterm-session/src/pane_state.rs+++ b/thinkterm-session/src/pane_state.rs@@ -55,6 +55,14 @@ pub struct PaneState {pub(crate) lines: LruCache<StableRowIndex, LineEntry>,pub(crate) line_cache_epoch: u64,+ /// The cache epoch whose scrollback was last fetched whole; a flush+ /// (screen switch, resize) moves the epoch and the next paint warms+ /// the cache again.+ pub(crate) warmed_epoch: Option<u64>,+ /// Where the physical top was when the scrollback was last warmed;+ /// rows that scrolled past since were never pushed, so a viewport of+ /// growth warms again.+ pub(crate) warmed_top: StableRowIndex,/// The epoch for which a discarded-fetch PaneOutput has already been/// emitted; during a live resize every frame bumps the epoch and can/// discard several in-flight fetches, and one repaint per epoch is@@ -104,6 +112,8 @@ impl PaneState {frontend_preview: None,lines: LruCache::new(NonZeroUsize::new(config.scrollback_lines.max(128)).unwrap()),line_cache_epoch: 0,+ warmed_epoch: None,+ warmed_top: 0,epoch_discard_notified: 0,title: title.to_string(),working_dir: None,diff --git a/thinkterm-web/src/host.rs b/thinkterm-web/src/host.rsindex fc62f2f0..6676c827 100644--- a/thinkterm-web/src/host.rs+++ b/thinkterm-web/src/host.rs@@ -107,6 +107,12 @@ impl HostConfig for WebConfig {// ratelimit for line fetches is 10 per second per pane.10}+ fn scrollback_lookahead_screens(&self) -> usize {+ 1+ }+ fn warm_scrollback(&self) -> bool {+ false+ }}pub struct WebHost {diff --git a/wezterm-client/src/pane/events.rs b/wezterm-client/src/pane/events.rsindex b1998639..461abd20 100644--- a/wezterm-client/src/pane/events.rs+++ b/wezterm-client/src/pane/events.rs@@ -46,12 +46,17 @@ impl Clock for SystemClock {/// push: the session asks for them on every delta.pub(crate) struct DesktopConfig {rules: Mutex<Option<(usize, Arc<Vec<termwiz::hyperlink::Rule>>)>>,+ /// The session server of this machine: a screenful of rows costs a+ /// millisecond over its socket, so the pane fetches well ahead and+ /// keeps its whole scrollback at hand.+ local_session_host: bool,}impl DesktopConfig {- pub(crate) fn new() -> Self {+ pub(crate) fn new(local_session_host: bool) -> Self {Self {rules: Mutex::new(None),+ local_session_host,}}}@@ -74,6 +79,18 @@ impl HostConfig for DesktopConfig {fn fetch_rate_per_second(&self) -> u32 {configuration().ratelimit_mux_line_prefetches_per_second}++ fn scrollback_lookahead_screens(&self) -> usize {+ if self.local_session_host {+ 2+ } else {+ 1+ }+ }++ fn warm_scrollback(&self) -> bool {+ self.local_session_host+ }}/// The session's own tasks run on the promise executor, as they did@@ -202,12 +219,13 @@ pub(crate) struct DesktopHost {impl DesktopHost {pub(crate) fn new(client: Arc<ClientInner>) -> Self {+ let config = DesktopConfig::new(client.client.is_local_session_host());Self {image_domain: client.local_domain_id,link: DesktopLink::new(Arc::clone(&client)),events: MuxEvents::new(client),clock: SystemClock,- config: DesktopConfig::new(),+ config,spawner: PromiseSpawner,}}