diff --git a/Cargo.lock b/Cargo.lock index 6548304..9e4e38b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -666,7 +666,7 @@ dependencies = [ "bitflags 2.13.0", "cexpr", "clang-sys", - "itertools 0.13.0", + "itertools 0.11.0", "log", "prettyplease", "proc-macro2", @@ -1472,6 +1472,7 @@ version = "0.1.6" dependencies = [ "async-broadcast", "async-channel 2.5.0", + "bytes", "crabport-core", "crabport-terminal", "parking_lot", @@ -1502,6 +1503,7 @@ dependencies = [ "async-broadcast", "async-channel 2.5.0", "async-trait", + "bytes", "crabport-core", "crabport-proxy", "crabport-sftp", @@ -1524,6 +1526,7 @@ version = "0.1.6" dependencies = [ "async-broadcast", "async-channel 2.5.0", + "bytes", "crabport-core", "crabport-proxy", "crabport-terminal", @@ -1539,6 +1542,7 @@ dependencies = [ "alacritty_terminal", "async-broadcast", "async-channel 2.5.0", + "bytes", "crabport-sftp", "libc", "parking_lot", @@ -2944,9 +2948,8 @@ dependencies = [ [[package]] name = "gpui-animation" -version = "0.2.63" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e99de9de440af28ad84219e2e6ac9e736d3d0e11fa3e914098c7153b6878fbb9" +version = "0.2.64" +source = "git+https://github.com/chi11321/gpui-animation?rev=75bd41bea2b9bcaea159ed235e920f5740592bfb#75bd41bea2b9bcaea159ed235e920f5740592bfb" dependencies = [ "dashmap", "gpui", diff --git a/Cargo.toml b/Cargo.toml index 5418d29..f9be311 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,6 +51,21 @@ codegen-units = 1 strip = true panic = "abort" +# Profiling build: release-speed, but with symbols and line tables kept so a +# system sampler (`sample`, Instruments, samply) can name our own functions. +# The shipped release profile strips them, which turns every frame inside +# CrabPort into a bare address — useless for finding a hotspot. +# cargo build --profile profiling +# scripts/profile.sh --build +[profile.profiling] +inherits = "release" +strip = false +debug = 1 +# Thin LTO and parallel codegen: a profiling build is linked often enough that +# the full-LTO wait is not worth it, and neither changes what the profile says. +lto = "thin" +codegen-units = 16 + [workspace] members = [ "crabport-agent", @@ -66,8 +81,8 @@ members = [ "crabport-serial", ] # The vendored `gpui-component` copy is a dependency (via `[patch.crates-io]` -# below), not a workspace member — excluding it keeps cargo from treating it -# as one. See `vendor/gpui-component/PATCHES.md` for what we patch and why. +# below), not a workspace member — excluding it keeps cargo from treating it as +# one. See `vendor/gpui-component/PATCHES.md` for what we patch and why. exclude = ["vendor/gpui-component"] # Patched third-party crates. `gpui-component`'s text rendering has a @@ -75,6 +90,9 @@ exclude = ["vendor/gpui-component"] # panel on long messages (see `vendor/gpui-component/PATCHES.md`); upstream # has no fix on the gpui 0.2 line, so we build our patched copy. Reverting = # delete the `vendor/` directory and this block. +# +# `gpui-animation` used to be vendored here too; its fix now lives upstream and +# `crabport-ui` depends on the git revision directly. [patch.crates-io] gpui-component = { path = "vendor/gpui-component" } diff --git a/crabport-agent/src/agent.rs b/crabport-agent/src/agent.rs index efee61e..0a5894d 100644 --- a/crabport-agent/src/agent.rs +++ b/crabport-agent/src/agent.rs @@ -264,22 +264,54 @@ impl Agent { smol::spawn(async move { let started = Instant::now(); let mut last = before.clone(); - let mut quiet_since = Instant::now(); let mut timed_out = false; let mut cancelled = false; + // `try_dump_text` materializes every scrollback line into a + // `String`, so the loop reads the grid only on ticks that actually + // saw a write. When the session reports a write counter that + // counter is also the authority on quietness — a command that + // stopped printing is exactly a command whose counter stopped + // moving, whereas "the dumped text looks the same" cannot be + // judged on a tick where the read failed (a flooding terminal + // holds the lock) or where the program redraws identical text. + let mut last_rev = session.grid_revision(); + let revision_tracking = last_rev.is_some(); + // Quietness clocks: `last_write` for the counter path, + // `quiet_since` for the text-comparison fallback. + let mut last_write = Instant::now(); + let mut quiet_since = Instant::now(); loop { smol::Timer::after(OUTPUT_POLL).await; if task_cancel.is_cancelled() { cancelled = true; break; } - let Some(now) = session.try_dump_text(AGENT_READ_LINES) else { - continue; // terminal lock held by the reader; try next tick + let rev = session.grid_revision(); + let grid_changed = match (rev, last_rev) { + (Some(rev), Some(prev)) => rev != prev, + // No counter: poll the grid every tick, as before. + _ => true, + }; + let mut dumped = false; + if grid_changed { + last_rev = rev; + last_write = Instant::now(); + if let Some(now) = session.try_dump_text(AGENT_READ_LINES) { + dumped = true; + if now != last { + last = now; + quiet_since = Instant::now(); + } + } + } + let quiet = if revision_tracking { + last_write.elapsed() >= OUTPUT_QUIET + } else { + // Without a counter, quiet is only knowable from a read + // that succeeded. + dumped && quiet_since.elapsed() >= OUTPUT_QUIET }; - if now != last { - last = now; - quiet_since = Instant::now(); - } else if quiet_since.elapsed() >= OUTPUT_QUIET { + if quiet { break; } if started.elapsed() >= OUTPUT_TIMEOUT { diff --git a/crabport-agent/src/session.rs b/crabport-agent/src/session.rs index 549b377..cf101a9 100644 --- a/crabport-agent/src/session.rs +++ b/crabport-agent/src/session.rs @@ -68,6 +68,19 @@ pub trait AgentSession: Send + Sync { String::new() } + /// Monotonic counter of writes into the terminal grid, when the session + /// can provide one. + /// + /// Pollers compare it against the value from their previous tick and skip + /// [`Self::try_dump_text`] entirely while nothing has been written — the + /// extraction allocates a `String` per grid line, so calling it on a fixed + /// timer just to discover "no change" is pure waste. Implementors that + /// cannot report a counter return `None` (the default), and pollers then + /// fall back to reading the grid every tick. + fn grid_revision(&self) -> Option { + None + } + /// Non-blocking [`Self::dump_text`]: `None` while the terminal reader /// holds its lock — the caller retries on its next tick. fn try_dump_text(&self, _lines: usize) -> Option { diff --git a/crabport-core/src/config.rs b/crabport-core/src/config.rs index ce964d1..0e379d2 100644 --- a/crabport-core/src/config.rs +++ b/crabport-core/src/config.rs @@ -1266,10 +1266,24 @@ pub fn update(f: impl FnOnce(&mut CrabPortConfig) -> R) -> Result CrabPortConfig { CONFIG.read().clone() } +/// Run `f` against the live config without cloning it. +/// +/// Use this from render hot paths: the read lock is held for the duration of +/// the closure only. Do not call [`update`] (or anything else that takes the +/// write lock) from inside `f` — `parking_lot`'s `RwLock` cannot upgrade a +/// held read lock, and the write would deadlock. +pub fn with(f: impl FnOnce(&CrabPortConfig) -> R) -> R { + f(&CONFIG.read()) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crabport-serial/Cargo.toml b/crabport-serial/Cargo.toml index 0f6333c..1568c10 100644 --- a/crabport-serial/Cargo.toml +++ b/crabport-serial/Cargo.toml @@ -9,6 +9,8 @@ crabport-core = { path = "../crabport-core" } serialport = { version = "4", default-features = false } tokio = { version = "1", features = ["rt", "macros", "io-util", "sync"] } async-broadcast = "0.7" +# `BackendEvent::Data` payload type (see crabport-terminal). +bytes = "1" async-channel = "2.5" tracing = { version = "0.1", default-features = false, features = ["std"] } parking_lot = "0.12" diff --git a/crabport-serial/src/backend.rs b/crabport-serial/src/backend.rs index 08fc737..f4cb6ac 100644 --- a/crabport-serial/src/backend.rs +++ b/crabport-serial/src/backend.rs @@ -200,7 +200,7 @@ impl SerialBackend { loop { match data_rx.try_recv() { Ok(data) => { - let _ = event_tx2.broadcast(BackendEvent::Data(data)).await; + let _ = event_tx2.broadcast(BackendEvent::Data(data.into())).await; } Err(std::sync::mpsc::TryRecvError::Empty) => { tokio::time::sleep(Duration::from_millis(5)).await; diff --git a/crabport-ssh/Cargo.toml b/crabport-ssh/Cargo.toml index 197640f..cf777e6 100644 --- a/crabport-ssh/Cargo.toml +++ b/crabport-ssh/Cargo.toml @@ -13,6 +13,8 @@ russh = "0.45" secrecy = "0.10" zeroize = "1" async-broadcast = "0.7" +# `BackendEvent::Data` payload type (see crabport-terminal). +bytes = "1" async-channel = "2.5" tokio = { version = "1", features = ["rt", "net", "macros", "io-util", "time"] } async-trait = "0.1" diff --git a/crabport-ssh/src/backend.rs b/crabport-ssh/src/backend.rs index b39719f..74cae7f 100644 --- a/crabport-ssh/src/backend.rs +++ b/crabport-ssh/src/backend.rs @@ -527,8 +527,14 @@ impl SshBackend { msg = channel.wait() => { match msg { Some(ChannelMsg::Data { data }) => { + // russh hands out its own `CryptoVec`, which + // cannot be adopted by `Bytes`; copy it into + // one shared buffer here (the broadcast clones + // per subscriber are then refcount bumps). let _ = event_tx2 - .broadcast(BackendEvent::Data(data.to_vec())) + .broadcast(BackendEvent::Data( + bytes::Bytes::copy_from_slice(&data), + )) .await; } Some(ChannelMsg::Eof) | Some(ChannelMsg::Close) | None => { @@ -731,8 +737,14 @@ impl SshBackend { msg = channel.wait() => { match msg { Some(ChannelMsg::Data { data }) => { + // russh hands out its own `CryptoVec`, which + // cannot be adopted by `Bytes`; copy it into + // one shared buffer here (the broadcast clones + // per subscriber are then refcount bumps). let _ = event_tx2 - .broadcast(BackendEvent::Data(data.to_vec())) + .broadcast(BackendEvent::Data( + bytes::Bytes::copy_from_slice(&data), + )) .await; } Some(ChannelMsg::Eof) | Some(ChannelMsg::Close) | None => { diff --git a/crabport-ssh/src/monitor.rs b/crabport-ssh/src/monitor.rs index 3f8c725..b5df036 100644 --- a/crabport-ssh/src/monitor.rs +++ b/crabport-ssh/src/monitor.rs @@ -27,35 +27,37 @@ pub(crate) async fn monitor_loop( ) { let mut prev_net_sent: u64 = 0; let mut prev_net_recv: u64 = 0; - - // ---- First collection immediately on connection ---- - { - let h = handle.lock().await; - let latency_ms = measure_latency(&h).await; - let memory = collect_memory(&h).await; - let cpu = collect_cpu(&h).await; - let disk = collect_disk(&h).await; - let network = collect_network(&h).await; - - if let Some(net) = network { - prev_net_sent = net.bytes_sent; - prev_net_recv = net.bytes_recv; - } - - let mut m = monitor.write(); - m.metrics = RemoteMetrics { - latency_ms, - memory, - cpu, - disk, - network: None, // No rate on first tick - }; - } + // The first sample has no previous counters to diff against, so it reports + // the totals as "no rate" and only seeds them (same as before the + // combined-exec change). + let mut first_sample = true; + + // One combined exec per sample. The collectors used to run as five + // separate `channel_open_session` + `exec` round trips (a latency probe, + // `cat /proc/meminfo`, `cat /proc/loadavg`, `nproc`, `df`, `cat + // /proc/net/dev`) while holding the shared handle lock. On a + // high-latency link that is seconds of lock time per tick for data that + // only feeds a status chip, and each exec blocks SFTP work sharing the + // same handle. The commands are all cheap and read-only, so they run as + // one shell command with markers between sections and are split apart + // locally. + const SAMPLE_CMD: &str = concat!( + "echo __MEM__; cat /proc/meminfo 2>/dev/null; ", + "echo __LOAD__; cat /proc/loadavg 2>/dev/null; ", + "echo __NCPU__; (nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null); ", + "echo __DISK__; df -kP 2>/dev/null; ", + "echo __NET__; cat /proc/net/dev 2>/dev/null", + ); + + /// How often the remote metrics are sampled. The toolbar only shows a + /// handful of numbers, and every sample costs an SSH exec plus + /// (because the lock is shared) a window where SFTP and tunnel setup + /// queue behind it — so this is deliberately not per-second. + const SAMPLE_INTERVAL: std::time::Duration = std::time::Duration::from_secs(3); loop { - tokio::time::sleep(std::time::Duration::from_secs(1)).await; - - // Skip if disconnected + // Skip if disconnected — checked before the exec so a session that + // already dropped does not pay for one more round trip. { let m = monitor.read(); if m.status == RemoteStatus::Disconnected { @@ -63,32 +65,36 @@ pub(crate) async fn monitor_loop( } } - let h = handle.lock().await; - - // ---- Latency: measure RTT of a small exec command ---- - let latency_ms = measure_latency(&h).await; - - // ---- Memory: parse /proc/meminfo ---- - let memory = collect_memory(&h).await; - - // ---- CPU: parse /proc/loadavg (portable across Linux/macOS) ---- - let cpu = collect_cpu(&h).await; - - // ---- Disk: stat the root filesystem via `df` ---- - let disk = collect_disk(&h).await; + let sample = { + let h = handle.lock().await; + exec_and_read_timed(&h, SAMPLE_CMD).await + }; - // ---- Network: parse /proc/net/dev ---- - let raw_network = collect_network(&h).await; - let network = raw_network.map(|net| { + let (latency_ms, output): (Option, String) = sample.unwrap_or_default(); + + let sections = split_sections(&output); + let memory = parse_meminfo(§ions.meminfo); + let cpu = parse_cpu( + §ions.loadavg, + Some(sections.ncpus.trim()).filter(|s| !s.is_empty()), + ); + let disk = parse_df(§ions.df); + let network = parse_net_dev(§ions.netdev).and_then(|net| { + if first_sample { + prev_net_sent = net.bytes_sent; + prev_net_recv = net.bytes_recv; + return None; + } let rate_sent = net.bytes_sent.saturating_sub(prev_net_sent); let rate_recv = net.bytes_recv.saturating_sub(prev_net_recv); prev_net_sent = net.bytes_sent; prev_net_recv = net.bytes_recv; - NetworkStats { + Some(NetworkStats { bytes_sent: rate_sent, bytes_recv: rate_recv, - } + }) }); + first_sample = false; // ---- Update shared state ---- { @@ -101,36 +107,57 @@ pub(crate) async fn monitor_loop( network, }; } + + tokio::time::sleep(SAMPLE_INTERVAL).await; } } -/// Measure round-trip latency by executing `echo ping` over SSH. -pub(crate) async fn measure_latency(handle: &client::Handle) -> Option { - let start = std::time::Instant::now(); - match handle.channel_open_session().await { - Ok(mut ch) => { - if ch.exec(true, "echo ping").await.is_err() { - return None; +/// The sections of one combined sample command. +#[derive(Default)] +struct Sample { + meminfo: String, + loadavg: String, + ncpus: String, + df: String, + netdev: String, +} + +/// Split a combined `echo __MARK__` sample into its sections. Marker lines +/// are matched on the trimmed line, so ordinary output cannot be mistaken for +/// a section boundary. +fn split_sections(output: &str) -> Sample { + let mut sample = Sample::default(); + let mut target: Option<&mut String> = None; + for line in output.lines() { + match line.trim() { + "__MEM__" => { + target = Some(&mut sample.meminfo); + continue; } - // Drain output until channel closes - loop { - match ch.wait().await { - Some(ChannelMsg::Data { .. }) => {} - Some(ChannelMsg::Eof) | Some(ChannelMsg::Close) | None => break, - _ => {} - } + "__LOAD__" => { + target = Some(&mut sample.loadavg); + continue; + } + "__NCPU__" => { + target = Some(&mut sample.ncpus); + continue; + } + "__DISK__" => { + target = Some(&mut sample.df); + continue; } - let elapsed = start.elapsed().as_millis() as u32; - Some(elapsed) + "__NET__" => { + target = Some(&mut sample.netdev); + continue; + } + _ => {} + } + if let Some(buf) = target.as_deref_mut() { + buf.push_str(line); + buf.push('\n'); } - Err(_) => None, } -} - -/// Collect remote memory stats via `cat /proc/meminfo`. -async fn collect_memory(handle: &client::Handle) -> Option { - let output = exec_and_read(handle, "cat /proc/meminfo").await?; - parse_meminfo(&output) + sample } /// Parse `/proc/meminfo` output into memory stats. Values are in kB; @@ -164,32 +191,9 @@ fn parse_meminfo(output: &str) -> Option { }) } -/// Collect remote CPU stats. We use `/proc/loadavg` because it's the most -/// portable single source across Linux distros and macOS-likes, and it -/// doesn't require a follow-up sample (unlike `/proc/stat`, which needs -/// two reads ~1s apart to compute a usage percentage). -/// -/// `usage_pct` is derived as `load_avg * 100 / num_cpus`, clamped to -/// 100.0. This isn't a true instantaneous CPU usage, but it's a -/// reasonable proxy for "how busy is this box" and matches what -/// `htop`/`top` show in their headers. We compute the CPU count from -/// `nproc` (Linux) — failing that, we fall back to assuming the load is -/// per-core (load_avg_1m * 100). -/// -/// We no longer surface the raw 1-minute load average as a separate -/// field — the toolbar only shows the percentage, so carrying the extra -/// number through `RemoteMetrics` was dead weight. -async fn collect_cpu(handle: &client::Handle) -> Option { - let loadavg = exec_and_read(handle, "cat /proc/loadavg").await?; - // Try to get CPU count. `nproc` is universally available on Linux; - // `sysctl -n hw.ncpu` on macOS/BSD. We try both sequentially. - let ncpus = exec_and_read(handle, "nproc 2>/dev/null || sysctl -n hw.ncpu").await; - parse_cpu(&loadavg, ncpus.as_deref()) -} - /// Derive the usage percentage from `/proc/loadavg` output plus the raw /// CPU-count command output (`None` / unparseable → assume 1 core). See -/// [`collect_cpu`] for the rationale behind the load-average proxy. +/// the combined sample for the rationale behind the load-average proxy. fn parse_cpu(loadavg: &str, ncpus_raw: Option<&str>) -> Option { let first = loadavg.split_whitespace().next()?; let load = first.parse::().ok()?; @@ -203,18 +207,6 @@ fn parse_cpu(loadavg: &str, ncpus_raw: Option<&str>) -> Option { Some(CpuStats { usage_pct }) } -/// Collect remote disk stats by parsing `df` output for the root -/// filesystem (`/`). We use `df -k -P` to get POSIX-compatible output: -/// `-k` forces 1KB blocks, `-P` keeps the line-format stable (no splitting -/// long device paths across two lines). We pick the row whose mount point -/// is exactly `/` because that's the most universal "primary disk" on a -/// remote server. If the root isn't present (unusual but possible on -/// BSD/macOS), we fall back to the row with the largest total size. -async fn collect_disk(handle: &client::Handle) -> Option { - let output = exec_and_read(handle, "df -kP 2>/dev/null || df -kP /").await?; - parse_df(&output) -} - /// Parse `df -kP` output. Picks the row mounted at `/` (or the largest /// filesystem as a fallback) and converts 1KB blocks to bytes. fn parse_df(output: &str) -> Option { @@ -251,13 +243,6 @@ fn parse_df(output: &str) -> Option { }) } -/// Collect remote network stats via `cat /proc/net/dev`. -/// Sums across all interfaces. -async fn collect_network(handle: &client::Handle) -> Option { - let output = exec_and_read(handle, "cat /proc/net/dev").await?; - parse_net_dev(&output) -} - /// Parse `/proc/net/dev` output, summing receive/transmit byte counters /// across all interfaces. fn parse_net_dev(output: &str) -> Option { @@ -289,6 +274,40 @@ fn parse_net_dev(output: &str) -> Option { }) } +/// Like [`exec_and_read`], but also reports the time to the first output +/// byte — a round-trip measure suitable for the connection's latency chip. +/// +/// The command's first line is echoed before any real work, so the first +/// packet back arrives one round trip after the exec, independent of how long +/// the command itself takes to run. +pub(crate) async fn exec_and_read_timed( + handle: &client::Handle, + cmd: &str, +) -> Option<(Option, String)> { + let start = std::time::Instant::now(); + let mut ch = handle.channel_open_session().await.ok()?; + if ch.exec(true, cmd).await.is_err() { + return None; + } + + let mut first_byte_ms = None; + let mut output = Vec::new(); + loop { + match ch.wait().await { + Some(ChannelMsg::Data { data }) => { + if first_byte_ms.is_none() { + first_byte_ms = Some(start.elapsed().as_millis() as u32); + } + output.extend_from_slice(&data); + } + Some(ChannelMsg::Eof) | Some(ChannelMsg::Close) | None => break, + _ => {} + } + } + + Some((first_byte_ms, String::from_utf8(output).ok()?)) +} + /// Execute a command over SSH and read all its stdout output. pub(crate) async fn exec_and_read( handle: &client::Handle, @@ -470,6 +489,49 @@ pub(crate) async fn drain_exec_capture( mod tests { use super::*; + /// The combined sample command is split on its marker lines, and each + /// section is handed to the same parser the separate execs used, so the + /// merge cannot change what the toolbar shows. + #[test] + fn combined_sample_splits_into_sections() { + let output = "\n__MEM__\nMemTotal: 16384000 kB\nMemAvailable: 8192000 kB\n\ + __LOAD__\n0.50 0.40 0.30 1/234 5678\n\ + __NCPU__\n4\n\ + __DISK__\nFilesystem 1024-blocks Used Available Capacity Mounted on\n\ + /dev/sda1 10240000 5120000 5120000 50% /\n\ + __NET__\nInter-| Receive\n face |bytes\n lo: 100 2 0 0 0 0 0 0 200 3 0 0 0 0 0 0\n"; + let sections = split_sections(output); + + let mem = parse_meminfo(§ions.meminfo).expect("meminfo parses"); + assert_eq!(mem.total, 16384000 * 1024); + assert_eq!(mem.used, (16384000 - 8192000) * 1024); + + let cpu = parse_cpu( + §ions.loadavg, + Some(sections.ncpus.trim()).filter(|s| !s.is_empty()), + ) + .expect("cpu parses"); + assert!((cpu.usage_pct - 12.5).abs() < 0.001); + + let disk = parse_df(§ions.df).expect("disk parses"); + assert_eq!(disk.total, 10240000 * 1024); + + let net = parse_net_dev(§ions.netdev).expect("network parses"); + assert_eq!(net.bytes_sent, 200); + assert_eq!(net.bytes_recv, 100); + } + + /// Output that never emits a marker yields empty sections rather than + /// leaking one section's text into another. + #[test] + fn combined_sample_without_markers_is_empty() { + let sections = split_sections("bash: cat: command not found\n"); + assert!(sections.meminfo.is_empty()); + assert!(sections.loadavg.is_empty()); + assert!(sections.df.is_empty()); + assert!(parse_meminfo(§ions.meminfo).is_none()); + } + #[test] fn meminfo_parses_total_and_available() { let out = "MemTotal: 16384000 kB\n\ diff --git a/crabport-telnet/Cargo.toml b/crabport-telnet/Cargo.toml index f2c165c..c28ce60 100644 --- a/crabport-telnet/Cargo.toml +++ b/crabport-telnet/Cargo.toml @@ -9,6 +9,8 @@ crabport-core = { path = "../crabport-core" } crabport-proxy = { path = "../crabport-proxy" } tokio = { version = "1", features = ["rt", "rt-multi-thread", "net", "io-util", "macros", "time"] } async-broadcast = "0.7" +# `BackendEvent::Data` payload type (see crabport-terminal). +bytes = "1" async-channel = "2.5" tracing = { version = "0.1", default-features = false, features = ["std"] } parking_lot = "0.12" diff --git a/crabport-telnet/src/backend.rs b/crabport-telnet/src/backend.rs index 6c27331..6ff2c08 100644 --- a/crabport-telnet/src/backend.rs +++ b/crabport-telnet/src/backend.rs @@ -429,7 +429,9 @@ impl TelnetBackend { let _ = stream.flush().await; } tracing::debug!("telnet read: {} data bytes", chunk.len()); - let _ = event_tx2.broadcast(BackendEvent::Data(chunk)).await; + let _ = event_tx2 + .broadcast(BackendEvent::Data(chunk.into())) + .await; } } Err(e) => { @@ -524,7 +526,7 @@ impl TelnetBackend { async fn flush_data(event_tx: &BroadcastSender, data_out: &mut Vec) { if !data_out.is_empty() { let chunk = std::mem::take(data_out); - let _ = event_tx.broadcast(BackendEvent::Data(chunk)).await; + let _ = event_tx.broadcast(BackendEvent::Data(chunk.into())).await; } } diff --git a/crabport-terminal/Cargo.toml b/crabport-terminal/Cargo.toml index 21180a7..45324a7 100644 --- a/crabport-terminal/Cargo.toml +++ b/crabport-terminal/Cargo.toml @@ -12,5 +12,9 @@ libc = "0.2.186" smol = "2.0.2" async-channel = "2.5.0" async-broadcast = "0.7.2" +# `BackendEvent::Data` carries `bytes::Bytes` so backends that already hold one +# (russh hands out `Bytes` for every SSH channel packet) can broadcast it +# without copying, and cloned payloads become a refcount bump. +bytes = "1" sysinfo = { version = "0.35", default-features = false, features = ["system", "network", "disk"] } tokio = { version = "1", features = ["rt", "rt-multi-thread", "net", "io-util", "macros", "sync", "fs", "time"] } diff --git a/crabport-terminal/src/pty.rs b/crabport-terminal/src/pty.rs index 1f39022..ecb4718 100644 --- a/crabport-terminal/src/pty.rs +++ b/crabport-terminal/src/pty.rs @@ -473,6 +473,8 @@ struct FgProcessSnapshot { /// PID of the direct child (the shell we spawned). Used as a /// fallback when `tcgetpgrp` returns 0 (no fg pgid assigned yet). shell_pid: u32, + /// When the last full probe ran. See [`Self::probe_if_due`]. + last_probe: Option, } #[cfg(unix)] @@ -494,9 +496,35 @@ impl FgProcessSnapshot { prev_cmd: None, pty_fd, shell_pid, + last_probe: None, } } + /// Rate-limited [`Self::probe`] for the PTY reader loop. + /// + /// The reader calls this for every chunk it reads, but a full probe costs + /// a `tcgetpgrp` syscall plus a `libproc`/`/proc` lookup of the process's + /// cwd and argv. At an 8 KiB chunk size a `cat` of a large file easily + /// produces thousands of chunks per second, all of which would re-read + /// the same unchanged cwd and argv — on the reader thread, so the probing + /// also gates the PTY itself and caps throughput. + /// + /// The probe is therefore throttled to at most one per `MIN_INTERVAL`. + /// That still catches shell state changes (`cd`, starting a command), + /// because those are followed by output, and the tail of any pause always + /// lands on a probe eventually — the change simply becomes visible within + /// `MIN_INTERVAL` instead of instantly. + fn probe_if_due(&mut self) -> Option<(std::path::PathBuf, String)> { + const MIN_INTERVAL: std::time::Duration = std::time::Duration::from_millis(150); + if let Some(last) = self.last_probe + && last.elapsed() < MIN_INTERVAL + { + return None; + } + self.last_probe = Some(std::time::Instant::now()); + self.probe() + } + /// Probe the current foreground process. Returns `Some((cwd, cmdline))` /// if the cwd or command string differs from the previous probe. /// @@ -701,10 +729,10 @@ impl PtyBackend { Ok(n) => { tracing::debug!("pty reader: {} bytes", n); - let _ = smol::block_on( - event_tx.broadcast(BackendEvent::Data(buf[..n].to_vec())), - ); - if let Some((cwd, name)) = snapshot.probe() { + let _ = smol::block_on(event_tx.broadcast(BackendEvent::Data( + bytes::Bytes::copy_from_slice(&buf[..n]), + ))); + if let Some((cwd, name)) = snapshot.probe_if_due() { tracing::debug!("process watcher: {} in {}", name, cwd.display()); let _ = smol::block_on(event_tx.broadcast( BackendEvent::ProcessChanged { @@ -981,7 +1009,9 @@ impl PtyBackend { } tracing::debug!("conpty reader: {} bytes", n); let _ = - smol::block_on(event_tx.broadcast(BackendEvent::Data(buf[..n].to_vec()))); + smol::block_on(event_tx.broadcast(BackendEvent::Data( + bytes::Bytes::copy_from_slice(&buf[..n]), + ))); } }); } diff --git a/crabport-terminal/src/terminal.rs b/crabport-terminal/src/terminal.rs index 2b6a210..4a40b50 100644 --- a/crabport-terminal/src/terminal.rs +++ b/crabport-terminal/src/terminal.rs @@ -1,6 +1,6 @@ use std::sync::{ Arc, - atomic::{AtomicBool, AtomicU8, Ordering}, + atomic::{AtomicBool, AtomicU8, AtomicU64, Ordering}, }; use alacritty_terminal::{ @@ -47,7 +47,14 @@ impl SearchMatch { #[derive(Debug, Clone)] pub enum BackendEvent { - Data(Vec), + /// Raw bytes from the terminal stream. + /// + /// `Bytes` rather than `Vec` so a chunk can be shared with every + /// subscriber by refcount instead of being copied per receiver, and so + /// backends that already hold a `Bytes` (or own a `Vec` they can hand + /// over) do not pay an extra copy. Backends whose buffer type cannot be + /// adopted (russh's `CryptoVec`) pay exactly one copy, same as before. + Data(bytes::Bytes), Closed, Error(String), /// A file transfer (download or upload) finished. @@ -787,6 +794,13 @@ pub struct TerminalSession { /// disabled (plain byte input via the IME path). Non-zero bits track /// which progressive feature levels are active. kitty_keyboard: Arc, + /// Bumped every time bytes are fed into the terminal grid. + /// + /// Lets pollers (the AI agent waiting for a command's output to settle) + /// tell "the screen changed" from "the screen is identical" without + /// re-materializing the whole grid into a `String` on every tick — the + /// text extraction is 400 heap-allocated lines per poll. + grid_rev: Arc, } impl TerminalSession { @@ -815,6 +829,7 @@ impl TerminalSession { std::time::Instant::now() - std::time::Duration::from_secs(60), )), kitty_keyboard: Arc::new(AtomicU8::new(0)), + grid_rev: Arc::new(AtomicU64::new(0)), } } @@ -848,6 +863,7 @@ impl TerminalSession { std::time::Instant::now() - std::time::Duration::from_secs(60), )), kitty_keyboard: Arc::new(AtomicU8::new(0)), + grid_rev: Arc::new(AtomicU64::new(0)), } } @@ -861,6 +877,7 @@ impl TerminalSession { let wakeup_tx = self.wakeup_tx.clone(); let command_history = self.command_history.clone(); let kitty_state = self.kitty_keyboard.clone(); + let grid_rev = self.grid_rev.clone(); smol::spawn(async move { let mut parser = Processor::::new(); @@ -945,6 +962,7 @@ impl TerminalSession { } } drop(terminal); + grid_rev.fetch_add(1, Ordering::Release); let _ = wakeup_tx.try_broadcast(()); } BackendEvent::Closed => { @@ -1686,6 +1704,12 @@ impl TerminalSession { } // ----------------------------------------------------------------- + /// Counter that changes whenever the grid was fed data. Pollers use it to + /// skip work while nothing has been written. + pub fn grid_revision(&self) -> u64 { + self.grid_rev.load(Ordering::Acquire) + } + /// Plain-text dump of the terminal's last `max_lines` lines (scrollback /// plus the visible screen), for callers that need to *read* the session /// rather than render it — the AI agent's `terminal_read` tool, and any diff --git a/crabport-ui/Cargo.toml b/crabport-ui/Cargo.toml index 6c9156f..0499e48 100644 --- a/crabport-ui/Cargo.toml +++ b/crabport-ui/Cargo.toml @@ -23,7 +23,10 @@ async-channel = "2.5" # Used by the AI panel's SFTP tools to await a transfer's completion event # (the same broadcast the terminal view and session already consume). async-broadcast = "0.7" -gpui-animation = "0.2.63" +# Pinned to the upstream fix for settled persistent animations (0.2.64): +# https://github.com/chi11321/gpui-animation/tree/fix/settled-persistent-animations +# Once 0.2.64 is published this becomes `gpui-animation = "0.2.64"`. +gpui-animation = { git = "https://github.com/chi11321/gpui-animation", rev = "75bd41bea2b9bcaea159ed235e920f5740592bfb" } gpui-component = "0.5" rust-i18n = "3" lru = "0.12" diff --git a/crabport-ui/src/app.rs b/crabport-ui/src/app.rs index b8d8bed..47dd584 100644 --- a/crabport-ui/src/app.rs +++ b/crabport-ui/src/app.rs @@ -525,12 +525,9 @@ impl Render for CrabportApp { .unwrap_or_else(|| { // No pick for this tab yet: fall back to the configured // default page (`appearance.terminal.panel_page`). - crate::views::panel::PanelKind::from_config( - crabport_core::config::snapshot() - .appearance - .terminal - .panel_page, - ) + crate::views::panel::PanelKind::from_config(crabport_core::config::with(|cfg| { + cfg.appearance.terminal.panel_page + })) }); // Panel width: live drag value takes priority; otherwise read the // persisted config value (clamped to a sane range). The max is also @@ -539,10 +536,11 @@ impl Render for CrabportApp { let eff_max = crate::layouts::panel::effective_max_panel_width(win_w); let panel_width = self.panel_drag.map_or_else( || { - crabport_core::config::snapshot() - .appearance - .panel_width - .clamp(crate::layouts::panel::MIN_PANEL_WIDTH, eff_max) + crabport_core::config::with(|cfg| { + cfg.appearance + .panel_width + .clamp(crate::layouts::panel::MIN_PANEL_WIDTH, eff_max) + }) }, |drag| drag.width, ); @@ -553,10 +551,8 @@ impl Render for CrabportApp { // connect" option, so the user can opt out of the auto-expand // behavior globally while still being able to override it per-tab // via the toolbar toggle button. - let panel_open_default = crabport_core::config::snapshot() - .appearance - .terminal - .expand_panel_on_connect; + let panel_open_default = + crabport_core::config::with(|cfg| cfg.appearance.terminal.expand_panel_on_connect); let panel_open = self .panel_open .get(&self.active_tab_id) diff --git a/crabport-ui/src/app/tabs.rs b/crabport-ui/src/app/tabs.rs index b8c86f6..3f37a33 100644 --- a/crabport-ui/src/app/tabs.rs +++ b/crabport-ui/src/app/tabs.rs @@ -1352,10 +1352,8 @@ impl CrabportApp { /// Once toggled, the per-tab value wins so the user's explicit choice /// sticks for that tab regardless of the global setting. pub fn toggle_right_panel(&mut self, tab_id: u64, cx: &mut Context) { - let default_open = crabport_core::config::snapshot() - .appearance - .terminal - .expand_panel_on_connect; + let default_open = + crabport_core::config::with(|cfg| cfg.appearance.terminal.expand_panel_on_connect); let current = self .panel_open .get(&tab_id) diff --git a/crabport-ui/src/views/panel/ai.rs b/crabport-ui/src/views/panel/ai.rs index 2012239..9a272ff 100644 --- a/crabport-ui/src/views/panel/ai.rs +++ b/crabport-ui/src/views/panel/ai.rs @@ -335,6 +335,10 @@ impl AgentSession for UiAgentSession { self.handles.session.try_dump_text(lines) } + fn grid_revision(&self) -> Option { + Some(self.handles.session.grid_revision()) + } + fn write_raw(&self, data: &[u8]) { self.handles.session.write_raw(data); } @@ -456,13 +460,22 @@ enum DisplayRole { /// live streaming tail always shows it expanded). struct DisplayMessage { role: DisplayRole, - content: String, - reasoning: String, + /// Refcounted so the per-frame list snapshot clones a pointer, not the + /// text: the conversation is rebuilt for the virtual list on every + /// render, and a streaming turn notifies ~60 times a second. + content: SharedString, + reasoning: SharedString, reasoning_expanded: bool, /// Tool calls this turn asked for. Empty for plain turns; the assistant /// message is only sent back to the model with its calls and results /// once every call has been resolved by the user. - tool_calls: Vec, + /// + /// `Rc` for the same reason as `content`: tool results can be large + /// (a command's captured output), and they are cloned into the list + /// snapshot on every frame. Mutation goes through `Rc::make_mut` in + /// [`AiPanel::tool_state_mut`], so the copy-on-write only happens when a + /// card actually changes. + tool_calls: Vec>, } /// One in-flight compaction round. @@ -564,6 +577,12 @@ pub struct AiPanel { /// layer avoids. stream_pending: String, stream_pending_reasoning: String, + /// Character counts of the two buffers above, maintained incrementally. + /// `reveal_step` runs ~60×/s and needs the pending length to pace the + /// reveal; recounting it with `chars().count()` each tick would be O(n) + /// over a buffer that can hold a whole burst of tokens. + stream_pending_chars: usize, + stream_pending_reasoning_chars: usize, /// A finished turn whose reveal is still draining; `(generation, /// result)`. The markdown commit waits for the drain so the final swap /// doesn't flash a wall of text. @@ -609,6 +628,8 @@ impl AiPanel { scroll_to_end_pending: false, stream_pending: String::new(), stream_pending_reasoning: String::new(), + stream_pending_chars: 0, + stream_pending_reasoning_chars: 0, finish_pending: None, reveal_ticking: false, queue: Vec::new(), @@ -636,17 +657,21 @@ impl AiPanel { // picker has options without a manual refresh. Scheduled via // `defer_in` — this runs inside the app's render pass, and spawning // network work must not happen there. - let ai_cfg = config::snapshot().ai; + // + // Borrowed rather than snapshotted: this is per-frame work, and + // `config::snapshot()` clones every provider entry. let mut to_fetch: Vec = Vec::new(); - for entry in &ai_cfg.providers { - if entry.base_url.trim().is_empty() { - continue; - } - let key = format!("{}|{}", entry.id, entry.base_url); - if self.fetch_attempts.insert(key) { - to_fetch.push(entry.id.clone()); + crabport_core::config::with(|cfg| { + for entry in &cfg.ai.providers { + if entry.base_url.trim().is_empty() { + continue; + } + let key = format!("{}|{}", entry.id, entry.base_url); + if self.fetch_attempts.insert(key) { + to_fetch.push(entry.id.clone()); + } } - } + }); if !to_fetch.is_empty() { cx.defer_in(window, move |panel, _w, cx| { for provider_id in to_fetch { @@ -795,8 +820,8 @@ impl AiPanel { fn commit_and_send(&mut self, text: String, cx: &mut Context) { self.messages.push(DisplayMessage { role: DisplayRole::User, - content: text, - reasoning: String::new(), + content: text.into(), + reasoning: SharedString::default(), reasoning_expanded: false, tool_calls: Vec::new(), }); @@ -842,6 +867,8 @@ impl AiPanel { self.status = None; self.stream_pending.clear(); self.stream_pending_reasoning.clear(); + self.stream_pending_chars = 0; + self.stream_pending_reasoning_chars = 0; self.finish_pending = None; self.generation += 1; // Every unresolved tool call is skipped: running ones record @@ -920,6 +947,8 @@ impl AiPanel { self.stream_reasoning.clear(); self.stream_pending.clear(); self.stream_pending_reasoning.clear(); + self.stream_pending_chars = 0; + self.stream_pending_reasoning_chars = 0; self.finish_pending = None; self.error = None; // The "responding" row appears now; when the user was already pinned @@ -943,9 +972,13 @@ impl AiPanel { // visible tail at a smooth pace (stick-to-bottom happens // per reveal step, in `reveal_step`). match event { - StreamEvent::Content(chunk) => panel.stream_pending.push_str(&chunk), + StreamEvent::Content(chunk) => { + panel.stream_pending_chars += chunk.chars().count(); + panel.stream_pending.push_str(&chunk); + } StreamEvent::Reasoning(chunk) => { - panel.stream_pending_reasoning.push_str(&chunk) + panel.stream_pending_reasoning_chars += chunk.chars().count(); + panel.stream_pending_reasoning.push_str(&chunk); } _ => return, } @@ -983,6 +1016,8 @@ impl AiPanel { if matches!(outcome, Err(AiError::Cancelled)) { self.stream_pending.clear(); self.stream_pending_reasoning.clear(); + self.stream_pending_chars = 0; + self.stream_pending_reasoning_chars = 0; self.finalize_turn(outcome, cx); return; } @@ -998,28 +1033,40 @@ impl AiPanel { /// backlog is large — catching up beats literal typing). Returns whether /// the ticker should keep running. fn reveal_step(&mut self, cx: &mut Context) -> bool { - fn reveal_into(pending: &mut String, shown: &mut String) -> bool { + fn reveal_into( + pending: &mut String, + pending_chars: &mut usize, + shown: &mut String, + ) -> bool { if pending.is_empty() { return false; } - let steps = (pending.chars().count() / 6).clamp(3, 240); + // The length comes from the maintained counter, not a recount: + // this runs every tick, and the pending buffer holds whatever the + // endpoint delivered since the last one. + let steps = (*pending_chars / 6).clamp(3, 240); let split = pending .char_indices() .nth(steps) .map(|(index, _)| index) .unwrap_or(pending.len()); let head: String = pending.drain(..split).collect(); + *pending_chars = pending_chars.saturating_sub(head.chars().count()); shown.push_str(&head); true } // Pin first (reads the last rendered frame), then grow the tail. let stick = self.is_list_at_bottom(); - let moved = reveal_into(&mut self.stream_pending, &mut self.stream_buf) - | reveal_into( - &mut self.stream_pending_reasoning, - &mut self.stream_reasoning, - ); + let moved = reveal_into( + &mut self.stream_pending, + &mut self.stream_pending_chars, + &mut self.stream_buf, + ) | reveal_into( + &mut self.stream_pending_reasoning, + &mut self.stream_pending_reasoning_chars, + &mut self.stream_reasoning, + ); if moved { if stick { self.scroll_to_end_pending = true; @@ -1124,7 +1171,7 @@ impl AiPanel { }; let mut auto_resolved = false; - let states: Vec = calls + let states: Vec> = calls .into_iter() .map(|call| { let args = serde_json::from_str::(&call.arguments).ok(); @@ -1148,7 +1195,7 @@ impl AiPanel { state.result = Some(reason); auto_resolved = true; } - state + Rc::new(state) }) .collect(); @@ -1161,8 +1208,8 @@ impl AiPanel { } else { self.messages.push(DisplayMessage { role: DisplayRole::Assistant, - content: String::new(), - reasoning: String::new(), + content: SharedString::default(), + reasoning: SharedString::default(), reasoning_expanded: false, tool_calls: states, }); @@ -1244,7 +1291,7 @@ impl AiPanel { wire.push(ChatMessage::system(system_prompt())); for msg in &self.messages { match msg.role { - DisplayRole::User => wire.push(ChatMessage::user(msg.content.clone())), + DisplayRole::User => wire.push(ChatMessage::user(msg.content.to_string())), DisplayRole::Summary => wire.push(ChatMessage::user(format!( "{COMPACTED_HEADER}{}", msg.content @@ -1253,7 +1300,7 @@ impl AiPanel { if msg.tool_calls.iter().any(|c| c.result.is_none()) { continue; } - let mut assistant = ChatMessage::assistant(msg.content.clone()); + let mut assistant = ChatMessage::assistant(msg.content.to_string()); assistant.tool_calls = msg.tool_calls.iter().map(|c| c.call.clone()).collect(); wire.push(assistant); for call in &msg.tool_calls { @@ -1489,6 +1536,10 @@ impl AiPanel { .get_mut(msg_index)? .tool_calls .get_mut(call_index) + // The list snapshot holds `Rc` clones of every call; this is the + // single place that mutates one, so the copy-on-write clone + // happens here only when a card actually changes. + .map(Rc::make_mut) } /// Store a decision plus its result, re-measure the card and continue @@ -1727,8 +1778,8 @@ impl AiPanel { 0, DisplayMessage { role: DisplayRole::Summary, - content: summary, - reasoning: String::new(), + content: summary.into(), + reasoning: SharedString::default(), // Reused as the block's disclosure flag. reasoning_expanded: false, tool_calls: Vec::new(), @@ -1820,14 +1871,19 @@ impl AiPanel { /// Commit an assistant turn unless both streams (answer and reasoning) /// are blank. - fn push_partial(&mut self, content: String, reasoning: String, tool_calls: Vec) { + fn push_partial( + &mut self, + content: String, + reasoning: String, + tool_calls: Vec>, + ) { if content.trim().is_empty() && reasoning.trim().is_empty() && tool_calls.is_empty() { return; } self.messages.push(DisplayMessage { role: DisplayRole::Assistant, - content, - reasoning, + content: content.into(), + reasoning: reasoning.into(), // Disclosure starts collapsed; the live streaming tail shows // reasoning in full while it streams. reasoning_expanded: false, @@ -1998,16 +2054,16 @@ impl AiPanel { items.push(MessageItem::Tool { message_ix, call_ix, - state: call.clone(), - cwd: cwd.map(str::to_string), + state: Rc::clone(call), + cwd: cwd.map(SharedString::new), tunnel, }); } } if self.stream.is_some() || self.finish_pending.is_some() { items.push(MessageItem::Streaming { - reasoning: self.stream_reasoning.clone(), - content: self.stream_buf.clone(), + reasoning: SharedString::new(self.stream_reasoning.as_str()), + content: SharedString::new(self.stream_buf.as_str()), streaming: self.stream.is_some(), }); } @@ -2503,21 +2559,21 @@ impl Drop for AiPanel { #[allow(clippy::large_enum_variant)] enum MessageItem { User { - content: String, + content: SharedString, }, /// The compacted-history marker: one dim block where the summarized turns /// used to be, clicked to show the summary text. Summary { message_ix: usize, - text: String, + text: SharedString, expanded: bool, }, Assistant { /// Index into `AiPanel::messages` — the thinking toggle needs the /// message, not the list row (they differ once cards exist). message_ix: usize, - reasoning: String, - content: String, + reasoning: SharedString, + content: SharedString, reasoning_expanded: bool, }, /// One tool call awaiting the user's approval — or showing what they @@ -2529,17 +2585,17 @@ enum MessageItem { message_ix: usize, /// Index within that turn's calls. call_ix: usize, - state: ToolCallState, + state: Rc, /// Directory the command would run in, when the shell reports one. - cwd: Option, + cwd: Option, /// The tunnel a tunnel_* call refers to, described at snapshot time /// (`#2 socks — active on 127.0.0.1:1080`). `None` for every other /// tool. tunnel: Option, }, Streaming { - reasoning: String, - content: String, + reasoning: SharedString, + content: SharedString, streaming: bool, }, } @@ -2638,7 +2694,7 @@ fn render_list_item( *message_ix, *call_ix, state.clone(), - cwd.as_deref(), + cwd.as_ref().map(SharedString::as_str), tunnel.as_deref(), entity, md_style, @@ -2672,7 +2728,7 @@ fn render_list_item( fn tool_card( message_ix: usize, call_ix: usize, - state: ToolCallState, + state: Rc, cwd: Option<&str>, tunnel: Option<&str>, entity: &WeakEntity, @@ -3368,8 +3424,8 @@ fn tool_card_field(label: &str, value: String, monospace: bool) -> AnyElement { #[allow(clippy::too_many_arguments)] fn assistant_block( ix: usize, - reasoning: String, - content: String, + reasoning: SharedString, + content: SharedString, expanded: bool, entity: &WeakEntity, md_style: &TextViewStyle, @@ -3407,7 +3463,7 @@ fn assistant_block( #[allow(clippy::too_many_arguments)] fn thinking_section( ix: usize, - reasoning: String, + reasoning: SharedString, expanded: bool, entity: &WeakEntity, md_style: &TextViewStyle, @@ -3466,7 +3522,7 @@ fn thinking_section( /// transcript stays honest about what happened to the history. fn summary_block( ix: usize, - text: String, + text: SharedString, expanded: bool, entity: &WeakEntity, md_style: &TextViewStyle, @@ -3527,7 +3583,7 @@ fn summary_block( /// turns read as distinct from the assistant's unlabeled content. Own /// selectable Markdown view. fn user_bubble( - text: String, + text: SharedString, id: String, md_style: &TextViewStyle, window: &mut Window, @@ -3558,7 +3614,7 @@ fn user_bubble( /// Thinking body: the reasoning Markdown view indented behind a left /// rail, rendered smaller/muted via ambient style inheritance. fn thinking_body( - text: String, + text: SharedString, id: String, md_style: &TextViewStyle, window: &mut Window, @@ -3608,7 +3664,7 @@ fn thinking_header_label() -> AnyElement { /// Markdown views coalesce changes behind their 200ms debounce), styled to /// match the committed blocks. Carets mark the growing end while the /// stream is active. -fn streaming_block(reasoning: String, content: String, streaming: bool) -> AnyElement { +fn streaming_block(reasoning: SharedString, content: SharedString, streaming: bool) -> AnyElement { let mut col = div().flex().flex_col().gap_2().w_full(); if reasoning.is_empty() && content.is_empty() { // Nothing streamed yet — a lone spinning loader, so the turn visibly @@ -3620,7 +3676,7 @@ fn streaming_block(reasoning: String, content: String, streaming: bool) -> AnyEl } if !reasoning.is_empty() { let text = if streaming && content.is_empty() { - format!("{reasoning}▍") + SharedString::from(format!("{reasoning}▍")) } else { reasoning }; @@ -3644,7 +3700,7 @@ fn streaming_block(reasoning: String, content: String, streaming: bool) -> AnyEl } if !content.is_empty() { let text = if streaming { - format!("{content}▍") + SharedString::from(format!("{content}▍")) } else { content }; diff --git a/crabport-ui/src/views/terminal.rs b/crabport-ui/src/views/terminal.rs index c5a7707..7cab51f 100644 --- a/crabport-ui/src/views/terminal.rs +++ b/crabport-ui/src/views/terminal.rs @@ -5,6 +5,7 @@ use std::sync::{ Arc, atomic::{AtomicBool, AtomicU32, Ordering}, }; +use std::time::Duration; use alacritty_terminal::{ grid::Dimensions, @@ -250,6 +251,11 @@ pub struct TerminalView { /// Index into `search_matches` of the currently-active (highlighted) /// match, or `None` if none is active. search_active: Option, + /// Generation counter for the debounced search recompute. Each keystroke + /// bumps it; the timer task that fires afterwards only runs the search if + /// the counter still matches, so a burst of typing produces one scan + /// instead of one per character. + search_debounce: u64, } /// Encode a key event into the kitty keyboard protocol's `CSI u` form, or one @@ -651,6 +657,7 @@ impl TerminalView { search_query: String::new(), search_matches: Vec::new(), search_active: None, + search_debounce: 0, } } @@ -680,6 +687,12 @@ impl TerminalView { // `Connected`, backend-event loop on error/close before that). let conn_recorded = Arc::new(AtomicBool::new(false)); + // The fade watcher below waits on its own receiver for the same + // wakeups the status listener consumes (each subscriber sees every + // broadcast). `try_broadcast` never blocks on a lagging receiver, so + // simply replacing the message is fine. + let session_wakeup = session.subscribe_wakeup(); + // Backend error/close events. let mut event_rx = session.subscribe_backend(); let overlay_c = overlay.clone(); @@ -976,6 +989,11 @@ impl TerminalView { } }); dirty_wk.store(true, Ordering::Release); + // Repaint now instead of waiting for the frame pump's next + // tick: this is the path every PTY/SSH byte takes, so the + // pump can stay slow while idle without adding latency to + // terminal output. + let _ = status_entity.update(cx, |_, cx| cx.notify()); } }) .detach(); @@ -989,11 +1007,18 @@ impl TerminalView { let overlay_fade = overlay.clone(); let dirty_fade = needs_repaint.clone(); let fade_entity = cx.entity().downgrade(); + // Event-driven rather than polled: every status change already + // broadcasts a session wakeup, so this parks on that channel + // instead of re-checking the flag every 50 ms. A connection that + // never fades out (a failed attempt, a session that stays + // connecting) therefore costs nothing at all, where the old loop + // kept waking every terminal tab forever. + let mut fade_rx = session_wakeup; cx.spawn(async move |_this, cx| { - loop { - smol::Timer::after(std::time::Duration::from_millis(50)).await; - if overlay_fade.lock().fade_out_started { - break; + while !overlay_fade.lock().fade_out_started { + if fade_rx.recv().await.is_err() { + // Backend gone; nothing left to fade. + return; } } smol::Timer::after(std::time::Duration::from_millis(600)).await; @@ -1031,8 +1056,18 @@ impl TerminalView { // `connection_overlay::render_connection_overlay` so the repaint // loop keeps ticking for exactly as long as the transition runs. const LOG_FADE_MS: u128 = 320; + // The 120 Hz cadence exists for animations (the spinner, log + // fades). Once nothing is animating the pump drops to a slow tick: + // it no longer needs to catch anything quickly, because every + // path that dirties the view calls `cx.notify()` itself (terminal + // data through the wakeup listener below, input handlers on the + // spot). Polling at full speed while idle cost every open tab 120 + // task wakeups and overlay locks per second, forever. + const ANIM_TICK: std::time::Duration = std::time::Duration::from_micros(8333); + const IDLE_TICK: std::time::Duration = std::time::Duration::from_millis(50); + let mut fast_tick = true; loop { - smol::Timer::after(std::time::Duration::from_micros(8333)).await; + smol::Timer::after(if fast_tick { ANIM_TICK } else { IDLE_TICK }).await; let ov = overlay_dirty_pump.lock(); // Fold the overlay-side dirty flag (set from non-gpui threads, // e.g. the SSH backend pushing a host-key prompt) into the @@ -1067,6 +1102,7 @@ impl TerminalView { if logs_animating { dirty_pump.store(true, Ordering::Release); } + fast_tick = spin || logs_animating; if dirty_pump.swap(false, Ordering::AcqRel) { if pump_entity.update(cx, |_, cx| cx.notify()).is_err() { break; @@ -1835,8 +1871,36 @@ impl TerminalView { /// Recompute matches for a new query, then select the first match /// closest to the current cursor position. + /// + /// Debounced: the scan walks the entire grid (scrollback included) under + /// the terminal lock, so running it on every keystroke both burns CPU and + /// stalls PTY parsing while it holds the lock. Typing fast now produces + /// one scan per pause instead of one per character; Enter flushes the + /// pending query immediately (see [`Self::flush_pending_search`]). fn on_search_query_changed(&mut self, query: &str, cx: &mut Context) { + const SEARCH_DEBOUNCE: Duration = Duration::from_millis(120); self.search_query = query.to_string(); + self.search_debounce = self.search_debounce.wrapping_add(1); + let generation = self.search_debounce; + let entity = cx.entity().downgrade(); + cx.spawn(async move |_this, cx| { + smol::Timer::after(SEARCH_DEBOUNCE).await; + let _ = entity.update(cx, |this, cx| { + if this.search_debounce != generation { + // Superseded by newer input. + return; + } + this.select_initial_match(cx); + }); + }) + .detach(); + // Repaint immediately so the query text shows even before the scan. + cx.notify(); + } + + /// Run the search for the current query and select the first match + /// closest to the cursor. + fn select_initial_match(&mut self, cx: &mut Context) { self.recompute_matches(); self.search_active = self.compute_initial_active(); if let Some(idx) = self.search_active { @@ -1846,6 +1910,16 @@ impl TerminalView { } } + /// Run any debounced search right now. Called before Enter-driven + /// navigation, so the user does not have to wait out the debounce for + /// their just-typed query to be searched. + fn flush_pending_search(&mut self, cx: &mut Context) { + // Bumping the counter invalidates the timer task that is still + // waiting out the debounce window. + self.search_debounce = self.search_debounce.wrapping_add(1); + self.select_initial_match(cx); + } + /// Re-run the search against the current terminal grid. Called when /// the query changes or when the grid is invalidated (resize, new /// output). Safe to call even when the search overlay is closed. @@ -1891,6 +1965,9 @@ impl TerminalView { /// Advance the active match in the given direction, wrapping around. pub fn advance_match(&mut self, dir: search::SearchDirection, cx: &mut Context) { if self.search_matches.is_empty() { + // Enter right after typing: the debounced scan has not run yet, + // so run it now rather than ignoring the key. + self.flush_pending_search(cx); return; } let len = self.search_matches.len(); @@ -1983,13 +2060,22 @@ impl Render for TerminalView { // window). Comparing the cached signature against the live config is // cheap, and lets every terminal tab pick up new font settings // without requiring an explicit cross-window notification. - { - let cfg = crabport_core::config::snapshot().appearance.terminal; - let family = cfg.effective_font_family().to_string(); - let size = cfg.effective_font_size(); - if self.applied_font_signature != Some((family.clone(), size)) { - self.reload_font_settings(cx); - } + // + // Reads through `config::with` rather than `config::snapshot()`: this + // runs on every painted frame, and cloning the whole config (AI + // provider list included) would churn hundreds of allocations a + // second. `reload_font_settings` takes the config lock itself, so it + // has to run after the borrow is released — hence the flag. + let font_changed = crabport_core::config::with(|cfg| { + let term = &cfg.appearance.terminal; + self.applied_font_signature + .as_ref() + .is_none_or(|(family, size)| { + family != term.effective_font_family() || *size != term.effective_font_size() + }) + }); + if font_changed { + self.reload_font_settings(cx); } // Lazily register focus/blur listeners (only once — a `&mut Window` @@ -2088,13 +2174,21 @@ impl Render for TerminalView { let ov = self.overlay.lock(); let overlay_visible = ov.is_visible(); let is_fading_out = ov.is_fading_out(); - let log_entries: Vec = ov.logs.clone(); + let is_remote = !self.remote_host.is_empty(); + // Only the overlay block below reads the log, and it renders only + // while the overlay is up (`is_remote` alone is not enough — a + // connected session hides it). Cloning the whole log every frame for + // a hidden overlay meant one `String` copy per entry, per frame, per + // tab, forever; skip it when nothing will paint it. + let log_entries: Vec = if is_remote || overlay_visible { + ov.logs.clone() + } else { + Vec::new() + }; let current_status = ov.status; let spinner_rotation_mrad = ov.spinner_rotation.load(Ordering::Relaxed); drop(ov); - let is_remote = !self.remote_host.is_empty(); - div() .id(ElementId::Name( format!("terminal-view-{}", self.count).into(), @@ -2417,6 +2511,57 @@ impl Render for TerminalView { )); let row_count = cache.rows_count; + + // Search highlights bucketed by viewport row, built + // once per frame. Scanning the whole match list inside + // the per-row loop made painting O(rows × matches) — + // with a common search term on a scrolled-back + // terminal that is tens of thousands of comparisons + // every frame. Each match row is now visited exactly + // once. Empty when no search is active, so the + // non-search path allocates nothing. + let search_rects_by_row: Vec> = + if search_matches_paint.is_empty() { + Vec::new() + } else { + let mut by_row: Vec> = + vec![Vec::new(); row_count]; + for (mi, m) in search_matches_paint.iter().enumerate() { + let color = search_match_color(Some(mi) == search_active_paint); + let single_row = m.start_line == m.end_line; + // Grid line → viewport row. + let vp_start = m.start_line + display_offset; + let vp_end = m.end_line + display_offset; + if vp_end < 0 || vp_start > row_count as i32 - 1 { + continue; + } + let first = vp_start.max(0) as usize; + let last = vp_end.min(row_count as i32 - 1).max(0) as usize; + for (vp_row, rects) in by_row + .iter_mut() + .enumerate() + .skip(first) + .take(last - first + 1) + { + let (lo, hi) = if single_row { + let lo = m.start_col.min(num_cols); + let hi = (m.end_col + 1).min(num_cols).max(lo + 1); + (lo, hi) + } else if vp_row as i32 == vp_start { + (m.start_col.min(num_cols), num_cols) + } else if vp_row as i32 == vp_end { + (0, (m.end_col + 1).min(num_cols)) + } else { + (0, num_cols) + }; + if hi > lo { + rects.push((lo, hi - lo, color)); + } + } + } + by_row + }; + for row_idx in 0..row_count { let y = bounds.origin.y + line_height * row_idx as f32; @@ -2514,42 +2659,14 @@ impl Render for TerminalView { // Search match highlights. Drawn after the cell // background layer but before the text so the text // remains visible on top of the match highlight. - // Each match is converted from grid line to - // viewport row via `display_offset`, the same as - // selection highlighting. + // The rects were bucketed by viewport row once + // before this loop, so each row only visits its + // own matches. // // The active match gets a brighter highlight so // the user can see which one they're on. - if !search_matches_paint.is_empty() { - let vp_row = row_idx as i32 - display_offset; - let mut search_rects: Vec<(usize, usize, Hsla)> = Vec::new(); - for (mi, m) in search_matches_paint.iter().enumerate() { - let is_active = Some(mi) == search_active_paint; - if m.start_line == m.end_line { - // Single-row match. - if vp_row == m.start_line { - let lo = m.start_col.min(num_cols); - let hi = (m.end_col + 1).min(num_cols).max(lo + 1); - let color = search_match_color(is_active); - search_rects.push((lo, hi - lo, color)); - } - } else { - // Multi-row match. - if vp_row == m.start_line { - let lo = m.start_col.min(num_cols); - let color = search_match_color(is_active); - search_rects.push((lo, num_cols - lo, color)); - } else if vp_row > m.start_line && vp_row < m.end_line { - let color = search_match_color(is_active); - search_rects.push((0, num_cols, color)); - } else if vp_row == m.end_line { - let hi = (m.end_col + 1).min(num_cols); - let color = search_match_color(is_active); - search_rects.push((0, hi, color)); - } - } - } - for (col, n, color) in search_rects { + if let Some(search_rects) = search_rects_by_row.get(row_idx) { + for &(col, n, color) in search_rects { if n == 0 { continue; } diff --git a/scripts/flat_profile.py b/scripts/flat_profile.py new file mode 100755 index 0000000..b4ce41b --- /dev/null +++ b/scripts/flat_profile.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +"""Turn a macOS `sample` report into a flat self-time profile. + +`sample` prints a call tree with per-node sample counts; that is hard to read +for "what is burning CPU". This walks the tree and prints, per function, the +samples where it was the *leaf* (its own time) — i.e. the classic flat profile. + +Usage: + flat_profile.py [top_n] + +Lines in the call graph look like: + + + 5073 start (in dyld) + 2840 [0x19aed4274] + + 5069 CFRunLoopRunSpecific (in CoreFoundation) + 588 [0x...] + +Depth is encoded by the length of the marker/space prefix before the count, so +a line is a child of the previous line with a strictly shorter prefix. +""" + +import re +import sys +from collections import Counter + +# prefix (markers + spaces) | count | frame text +LINE = re.compile(r"^(?P {4}[+!:| ]*?)(?P\d+) (?P.*)$") + +# `0x104db80c4` / `load address 0x… + 0x…` noise carries no signal. +ADDR = re.compile(r"\s*\[0x[0-9a-f]+\]\s*$") +LOAD = re.compile(r"load address 0x[0-9a-f]+ \+ 0x[0-9a-f]+") + + +def normalize(frame: str) -> str: + frame = ADDR.sub("", frame).strip() + frame = LOAD.sub("load address …", frame) + return frame + + +def parse(path: str): + """Yield (depth, samples, frame) for every node in the call graph.""" + in_graph = False + with open(path, "r", errors="replace") as handle: + for raw in handle: + line = raw.rstrip("\n") + if line.startswith("Call graph:"): + in_graph = True + continue + if not in_graph: + continue + if line.startswith("Total number in stack"): + break + match = LINE.match(line) + if not match: + continue + yield ( + len(match.group("prefix")), + int(match.group("count")), + normalize(match.group("frame")), + ) + + +def self_times(path: str) -> Counter: + """Samples attributed to each frame excluding its children's.""" + self_samples: Counter = Counter() + # (depth, samples, frame, children_samples) + stack = [] + for depth, samples, frame in parse(path): + while stack and stack[-1][0] >= depth: + pop_depth, pop_samples, pop_frame, children = stack.pop() + self_samples[pop_frame] += pop_samples - children + if stack: + stack[-1][3] += pop_samples + stack.append([depth, samples, frame, 0]) + while stack: + _depth, samples, frame, children = stack.pop() + self_samples[frame] += samples - children + if stack: + stack[-1][3] += samples + # A parent can legitimately have fewer self samples than its children + # (truncated branches); never report negative time. + return Counter({f: max(0, n) for f, n in self_samples.items() if n > 0}) + + +def main() -> int: + if len(sys.argv) < 2: + print(__doc__.strip()) + return 2 + path = sys.argv[1] + top_n = int(sys.argv[2]) if len(sys.argv) > 2 else 25 + + times = self_times(path) + total = sum(times.values()) + if total == 0: + print("no samples found — is this a `sample` report?") + return 1 + + print(f"total samples: {total}") + print(f"{'self%':>6} {'samples':>8} function") + for frame, count in times.most_common(top_n): + print(f"{100.0 * count / total:5.1f}% {count:8d} {frame}") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/profile.sh b/scripts/profile.sh new file mode 100755 index 0000000..e85fba2 --- /dev/null +++ b/scripts/profile.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# +# Stack-level profiling for CrabPort on macOS. +# +# The app ships stripped, so macOS's sampler can only show addresses inside it. +# This wraps `sample` (no extra installs, no sudo for your own process) around +# a profiling build, then reduces the call tree to a flat self-time table with +# scripts/flat_profile.py. +# +# Usage: +# scripts/profile.sh # profile the running CrabPort, 10s +# scripts/profile.sh 20 # sample for 20 seconds +# scripts/profile.sh --pid 1234 # attach to a specific process +# scripts/profile.sh --build # build --profile profiling first +# scripts/profile.sh --build --run # ...and launch it (opens a window) +# +# Typical workflow: +# 1. scripts/profile.sh --build --run (a profiling build appears) +# 2. reproduce the heavy scenario in that window +# 3. scripts/profile.sh --pid "$(pgrep -f 'target/profiling/CrabPort' | head -1)" + +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +seconds=10 +pid="" +build=0 +run=0 +out_dir="${TMPDIR:-/tmp}" + +while [[ $# -gt 0 ]]; do + case "$1" in + --pid) pid="$2"; shift 2 ;; + --build) build=1; shift ;; + --run) run=1; shift ;; + --out) out_dir="$2"; shift 2 ;; + -h|--help) sed -n '2,25p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; exit 0 ;; + ''|*[!0-9]*) echo "unknown argument: $1" >&2; exit 2 ;; + *) seconds="$1"; shift ;; + esac +done + +if [[ "$(uname)" != "Darwin" ]]; then + echo "this script uses macOS 'sample'; on Linux use 'perf record -p ' or samply" >&2 + exit 1 +fi + +if [[ $build -eq 1 ]]; then + echo "building --profile profiling (symbols kept)…" >&2 + cargo build --profile profiling --manifest-path "$repo_root/Cargo.toml" +fi + +if [[ $run -eq 1 ]]; then + binary="$repo_root/target/profiling/CrabPort" + [[ -x "$binary" ]] || { echo "no profiling build at $binary — pass --build" >&2; exit 1; } + echo "launching $binary" >&2 + "$binary" >/dev/null 2>&1 & + sleep 2 + pid="$!" +fi + +if [[ -z "$pid" ]]; then + pid="$(pgrep -f 'target/(profiling|release)/CrabPort|/CrabPort.app/Contents/MacOS/CrabPort' | head -1 || true)" + [[ -n "$pid" ]] || { echo "no CrabPort process found — start it, or pass --pid" >&2; exit 1; } +fi + +if ! ps -p "$pid" >/dev/null 2>&1; then + echo "pid $pid is not running" >&2 + exit 1 +fi + +report="$out_dir/crabport-sample-$(date +%Y%m%d-%H%M%S).txt" +echo "sampling pid $pid for ${seconds}s → $report" >&2 +sample "$pid" "$seconds" -mayDie -file "$report" >/dev/null + +echo +python3 "$repo_root/scripts/flat_profile.py" "$report" 25 +echo +echo "raw call tree: $report" >&2