Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

measure drag performance after session setup (#3451)

kt01fa4dc44f63
diff --git a/crates/pa-tui/src/interactive.rs b/crates/pa-tui/src/interactive.rs
index c210ecb16c..6cfa6c161e 100644
--- a/crates/pa-tui/src/interactive.rs
+++ b/crates/pa-tui/src/interactive.rs
@@ -486,6 +486,7 @@ enum UiInput {
timeout_ms: u64,
},
ScrollTop,
+ Timestamp(std::sync::mpsc::Sender<(Instant, usize)>),
Resize,
HeadlessDone,
}
diff --git a/crates/pa-tui/src/interactive/headless.rs b/crates/pa-tui/src/interactive/headless.rs
index 20cc1cbd57..47e1362911 100644
--- a/crates/pa-tui/src/interactive/headless.rs
+++ b/crates/pa-tui/src/interactive/headless.rs
@@ -63,6 +63,12 @@ pub enum HeadlessStep {
/// A raw mouse sequence: decoded by the same parser the terminal's SGR reports flow
/// through, so the verifier drives the wheel dispatch with byte-identical sequences.
Mouse(String),
+ /// Record when the UI consumes this step, after the preceding headless step's frame,
+ /// with the renderer's headless render invocation count at that moment. Bracket input
+ /// sequences without including attach, initial rendering, or teardown: the render count
+ /// witnesses renders that the frame capture's plain-text dedupe cannot express (a
+ /// selection restyle changes styles, not text).
+ Timestamp(std::sync::mpsc::Sender<(std::time::Instant, usize)>),
/// One raw key event: the verifier's window into the selector/picker surfaces (arrows, escape),
/// which typed text cannot express.
Key(crossterm::event::KeyEvent),
diff --git a/crates/pa-tui/src/interactive/onboarding.rs b/crates/pa-tui/src/interactive/onboarding.rs
index 3580a05f29..321b11046e 100644
--- a/crates/pa-tui/src/interactive/onboarding.rs
+++ b/crates/pa-tui/src/interactive/onboarding.rs
@@ -222,6 +222,12 @@ async fn drive_onboarding_pane(
// The headless plan completed while the pane owned the channel: mark the run loop's
// flag (the pane keeps driving until the channel closes or a decision ends it).
UiInput::HeadlessDone => *drive.headless_done = true,
+ UiInput::Timestamp(_) => {
+ if let Some(task) = flow.take() {
+ task.end().await;
+ }
+ anyhow::bail!("headless timing markers require an attached session");
+ }
// The plan's render barriers (pane-scoped): a condition that already holds pops
// immediately; a pending one arms and holds the input batch behind it until a later
// frame satisfies it or the deadline pops (the timeout proceeds silently — the
@@ -696,3 +702,73 @@ fn warn_onboarding_persist_failure(
});
session.dirty = true;
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[tokio::test]
+ async fn rejected_timing_marker_ends_active_login_flow() {
+ let (auth_tx, mut auth_rx) = mpsc::unbounded_channel();
+ let panel = crate::auth_panel::AuthPanelHandle::new(auth_tx);
+ let cancel = panel.cancel_signal();
+ let flow_cancel = cancel.clone();
+ let (done_tx, mut done_rx) = tokio::sync::oneshot::channel();
+ let flow = OnboardingFlowTask::spawn(
+ async move {
+ while !flow_cancel.cancelled() {
+ tokio::task::yield_now().await;
+ }
+ done_tx.send(()).expect("flow completion receiver");
+ crate::provider_auth::ProviderAuthOutcome::Cancelled
+ },
+ cancel.clone(),
+ );
+ let (ui_tx, mut ui_rx) = mpsc::unbounded_channel();
+ let (timing_tx, _timing_rx) = std::sync::mpsc::channel();
+ ui_tx
+ .send(UiInput::Timestamp(timing_tx))
+ .expect("queued marker");
+ let mut view = AgentView::new(crate::theme::Theme::builtin(
+ "prime",
+ crate::theme::ColorMode::TrueColor,
+ ));
+ let mut renderer = Renderer::Headless {
+ width: 80,
+ height: 24,
+ frames: Vec::new(),
+ renders: 0,
+ };
+ let exit_guard = ExitGuard::new();
+ let mut headless_done = false;
+ let mut drive = PaneDrive {
+ ui_rx: &mut ui_rx,
+ renderer: &mut renderer,
+ exit_guard: &exit_guard,
+ keybindings: KeybindingsManager::new(),
+ auth_panel_rx: &mut auth_rx,
+ headless_done: &mut headless_done,
+ };
+ let mut osc_sink = crate::clipboard::OscSink::Buffer(Vec::new());
+ let error = tokio::time::timeout(
+ Duration::from_secs(5),
+ drive_onboarding_pane(
+ &mut view,
+ &mut drive,
+ crate::onboarding::OnboardingScreen::new(),
+ Some(flow),
+ &mut osc_sink,
+ ),
+ )
+ .await
+ .expect("pane exits promptly")
+ .err()
+ .expect("onboarding rejects session timing markers");
+ assert_eq!(
+ error.to_string(),
+ "headless timing markers require an attached session"
+ );
+ assert!(cancel.cancelled());
+ assert_eq!(done_rx.try_recv(), Ok(()));
+ }
+}
diff --git a/crates/pa-tui/src/interactive/render.rs b/crates/pa-tui/src/interactive/render.rs
index b8923286f4..7446b9e83b 100644
--- a/crates/pa-tui/src/interactive/render.rs
+++ b/crates/pa-tui/src/interactive/render.rs
@@ -94,6 +94,10 @@ pub(super) enum Renderer {
width: u16,
height: u16,
frames: Vec<String>,
+ /// Every headless render invocation so far. The frame capture dedupes on plain text,
+ /// so restyle-only renders add no frame; this count is the style-blind render witness
+ /// timing markers report.
+ renders: usize,
},
}
@@ -287,6 +291,11 @@ impl Renderer {
}
}
}
+ HeadlessStep::Timestamp(sender) => {
+ if ui_tx.send(UiInput::Timestamp(sender)).is_err() {
+ return;
+ }
+ }
HeadlessStep::Key(key) => {
if ui_tx.send(UiInput::Key(key)).is_err() {
return;
@@ -300,6 +309,7 @@ impl Renderer {
width: plan.width,
height: plan.height,
frames: Vec::new(),
+ renders: 0,
})
}
}
@@ -400,16 +410,27 @@ impl Renderer {
width,
height,
frames,
+ renders,
} = self
else {
return;
};
let text = crate::app::render_frame_text(view, *width, *height).join("\n");
+ *renders += 1;
if frames.last().map(String::as_str) != Some(text.as_str()) {
frames.push(text);
}
}
+ /// The headless render invocation count (0 on a terminal): timing markers carry it as the
+ /// witness for renders the deduped frame capture cannot express.
+ pub(super) fn headless_renders(&self) -> usize {
+ match self {
+ Renderer::Headless { renders, .. } => *renders,
+ Renderer::Terminal { .. } => 0,
+ }
+ }
+
/// The headless capture's frames (None on a terminal): the render barriers wait on these.
pub(super) fn headless_frames(&self) -> Option<&[String]> {
match self {
@@ -424,11 +445,13 @@ impl Renderer {
width,
height,
frames,
+ renders,
} = self
else {
return;
};
let text = crate::app::render_frame_text(view, *width, *height).join("\n");
+ *renders += 1;
if std::env::var("PA_TUI_DEBUG_EVENTS").is_ok() {
eprintln!(
"[tui-frame] len={} has_second={} has_again={}",
diff --git a/crates/pa-tui/src/interactive/run.rs b/crates/pa-tui/src/interactive/run.rs
index 5b7e37f0b1..52b168f76b 100644
--- a/crates/pa-tui/src/interactive/run.rs
+++ b/crates/pa-tui/src/interactive/run.rs
@@ -972,6 +972,14 @@ async fn run_interactive_surface(
}
}
} else if let Some(input) = pending.pop_front() {
+ // Timing markers observe the previous frame without dirtying the view or
+ // adding a select/render iteration to the measured input sequence. The render
+ // count rides along: the frame capture dedupes on plain text, so restyle-only
+ // renders need a separate witness.
+ if let UiInput::Timestamp(sender) = input {
+ sender.send((Instant::now(), renderer.headless_renders()))?;
+ continue;
+ }
session.dirty = true;
match input {
UiInput::Key(key) => {
@@ -1122,6 +1130,7 @@ async fn run_interactive_surface(
session.run_traces_login(&mut view);
}
}
+ UiInput::Timestamp(_) => unreachable!("timing marker handled above"),
UiInput::HeadlessDone => headless_done = true,
UiInput::WaitRender { .. } | UiInput::WaitGone { .. } => {
unreachable!("render barrier handled above")
diff --git a/crates/pa-tui/tests/drag_select_perf.rs b/crates/pa-tui/tests/drag_select_perf.rs
index 0163a4d6ed..ca83899c1a 100644
--- a/crates/pa-tui/tests/drag_select_perf.rs
+++ b/crates/pa-tui/tests/drag_select_perf.rs
@@ -1,8 +1,9 @@
//! Headless perf verifier for mouse drag-selection: the per-frame cost of a drag is independent of
//! the session size (a drag frame restyles the visible cached rows' selection diff, never the
-//! transcript geometry). The budget compares a 100-drag burst vs. a no-drag baseline and a small
-//! session; each session size samples several (baseline, drag) pairs, and the budget compares
-//! the least load-contaminated sample of each size (see `drag_excess`).
+//! transcript geometry). The budget measures a 100-drag burst after the initial viewport is
+//! rendered, excluding cold attach/setup and teardown on both session sizes; each session size
+//! samples several marker-bracketed drag runs, and the budget compares the least
+//! load-contaminated sample of each size (see `drag_cost`).
#![cfg(unix)]
// Casts: structurally bounded terminal-layout arithmetic; guarded conversions add panic paths.
#![allow(
@@ -224,16 +225,12 @@ fn options(socket: PathBuf) -> InteractiveOptions {
}
}
-fn run_plan(
- messages: usize,
- steps: Vec<HeadlessStep>,
-) -> (Vec<String>, Vec<String>, std::time::Duration) {
+fn run_plan(messages: usize, steps: Vec<HeadlessStep>) -> (Vec<String>, Vec<String>) {
let _guard = run_lock();
let dir = tempfile::TempDir::new().expect("temp dir");
let socket = dir.path().join("tui.sock");
let supervisor = MockSupervisor::bind(&socket);
let handle = std::thread::spawn(move || supervisor.serve(messages));
- let started = std::time::Instant::now();
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
@@ -246,71 +243,123 @@ fn run_plan(
let outcome = runtime
.block_on(run_interactive(options(socket), UiMode::Headless(plan)))
.expect("interactive run");
- let elapsed = started.elapsed();
let _ = handle.join();
- (outcome.frames, outcome.copies, elapsed)
+ (outcome.frames, outcome.copies)
}
/// The scroll-paused drag burst: mount the window at the transcript top, then press-drag-release
/// across the first rows. The chat opens directly into content (the operator's 2026-09-26
/// zero-shift directive), so the first user message's text row is row 3 (SGR 0-based 2).
-fn drag_burst() -> Vec<HeadlessStep> {
+fn drag_burst(
+ timing: Option<std::sync::mpsc::Sender<(std::time::Instant, usize)>>,
+) -> Vec<HeadlessStep> {
let mut steps = vec![HeadlessStep::ScrollTop];
+ if let Some(sender) = &timing {
+ steps.push(HeadlessStep::Timestamp(sender.clone()));
+ }
steps.push(HeadlessStep::Mouse(press(3, 3)));
for index in 0..100 {
steps.push(HeadlessStep::Mouse(drag(3 + (index % 40), 3 + (index % 6))));
}
steps.push(HeadlessStep::Mouse(release(43, 8)));
+ if let Some(sender) = timing {
+ steps.push(HeadlessStep::Timestamp(sender));
+ }
steps
}
-/// One no-drag baseline of the same session shape: the drag burst's excess over this is the
-/// selection path's own cost.
-fn baseline() -> Vec<HeadlessStep> {
- vec![HeadlessStep::ScrollTop]
+fn timed_drag(messages: usize) -> (Vec<String>, Vec<String>, Duration, usize) {
+ let (timing_tx, timing_rx) = std::sync::mpsc::channel();
+ let (frames, copies) = run_plan(messages, drag_burst(Some(timing_tx)));
+ let markers: Vec<_> = timing_rx.try_iter().collect();
+ let [(started, first_render), (finished, last_render)]: [(std::time::Instant, usize); 2] =
+ markers
+ .try_into()
+ .expect("the rendered drag burst records both timing boundaries");
+ let elapsed = finished
+ .checked_duration_since(started)
+ .expect("timing markers arrive in monotonic order");
+ (frames, copies, elapsed, last_render - first_render)
}
-/// How many (baseline, drag) pairs each session size samples after its discarded warm-up pair.
-const MEASURED_PAIRS: usize = 3;
+#[test]
+fn timing_checkpoints_preserve_rendered_frames_and_copies() {
+ let expected = run_plan(40, drag_burst(None));
+ let (frames, copies, ..) = timed_drag(40);
+ assert_eq!((frames, copies), expected);
+}
-/// One session size's drag-burst cost: the minimum excess across `MEASURED_PAIRS` measured
-/// (baseline, drag) pairs, preceded by one discarded warm-up pair.
+/// How many marker-bracketed drag runs each session size samples after its discarded warm-up run.
+const MEASURED_RUNS: usize = 3;
+
+/// One session size's drag-burst cost: the minimum marker-bracketed drag interval across
+/// `MEASURED_RUNS` measured runs, preceded by one discarded warm-up run. Every measured run
+/// must first prove it rendered the full burst and copied the same selection — the minimum is
+/// taken only over validated runs, so an incomplete run can never win it.
///
-/// A single pair's excess is the true drag-burst cost plus the pair's differential wall-clock
-/// noise, and the large session's ~40MB attach makes that differential spike hundreds of ms to
-/// seconds on loaded shared runners — the CI flake class (identical failures on pure main and
-/// on PRs with no drag-path commits). A one-pair budget flakes on the load spike, not the drag.
-/// The minimum over several pairs rejects the positive spikes; its residual error is a negative
-/// differential bounded by one baseline run's own noise, an order of magnitude below the signal
-/// the guard exists to catch. The guard stays real: a session-size regression inflates every
-/// pair (a burst that resolves the transcript once per frame adds seconds to each), so the
-/// minimum still trips the bounds.
-fn drag_excess(messages: usize) -> (Duration, Vec<Duration>, String) {
- // The warm-up pair primes the page cache and allocator arenas (the process's first run
+/// The interval's boundaries are timing markers consumed on the UI loop: it covers press, every
+/// drag frame, release/copy and its final frame, and excludes the separately variable cold
+/// attach/initial viewport work and teardown. Wall-clock noise on a loaded shared runner only
+/// ever adds time, so the minimum over several runs is the robust estimate of the drag's own
+/// cost. The guard stays real: a session-size regression inflates every run (a burst that
+/// resolves the transcript once per frame adds seconds to each), so the minimum still trips the
+/// bounds.
+fn drag_cost(messages: usize) -> (Duration, Vec<Duration>, String) {
+ // The warm-up run primes the page cache and allocator arenas (the process's first run
// reads about 2x its steady state); its numbers are discarded.
- let _ = run_plan(messages, baseline());
- let _ = run_plan(messages, drag_burst());
+ let _ = run_plan(messages, drag_burst(None));
- let mut excesses = Vec::with_capacity(MEASURED_PAIRS);
+ let mut intervals = Vec::with_capacity(MEASURED_RUNS);
let mut extracted = String::new();
- for pair in 0..MEASURED_PAIRS {
- let (_, _, baseline) = run_plan(messages, baseline());
- let (frames, copies, drag) = run_plan(messages, drag_burst());
- // The headless capture renders per change: the drag frames flowed.
- assert!(!frames.is_empty(), "the drag burst rendered frames");
+ for run in 0..MEASURED_RUNS {
+ let (frames, copies, drag, renders) = timed_drag(messages);
+ // The plain-text frame capture dedupes and strips styles, so a selection restyle adds
+ // no frame: the markers carry the render invocation count as the drag-render witness.
+ // Press, every drag, and release each render once, so fewer than 102 renders between
+ // the markers means the burst was skipped or coalesced, not measured.
+ assert!(
+ !frames.is_empty(),
+ "the headless capture recorded the run's frames"
+ );
+ assert!(
+ renders >= 102,
+ "the measured run rendered {renders} times — press, 100 drags, and release must \
+ each render individually"
+ );
assert_eq!(copies.len(), 1, "each drag burst copies once");
+ let text = copies.concat();
+ // The fixed coordinates pin the expected copy: the press (row 3, col 3) anchors the
+ // selection in message 0's wrapped block ("row 0 lorem ipsum ..."), the drag rows
+ // extend it down through the block's wrapped rows, and the release never crosses
+ // into message 1 ("answer 1 ..."). A one-row copy, or one leaking the next message,
+ // means the drag never extended the selection — the run is not measuring a drag.
+ assert!(
+ text.starts_with("row 0 lorem"),
+ "the selection anchored at the pressed row: {:?}",
+ &text[..text.len().min(40)]
+ );
+ assert!(
+ text.lines().count() > 1,
+ "the selection extended beyond the pressed row: {:?}",
+ &text[..text.len().min(80)]
+ );
assert!(
- copies[0].starts_with("row 0"),
- "the copy reads the pressed row: {:?}",
- &copies[0][..copies[0].len().min(40)]
+ !text.contains("answer 1"),
+ "the drag stayed inside the pressed message's block: {:?}",
+ &text[..text.len().min(80)]
);
- if pair == 0 {
- extracted = copies.concat();
+ if run == 0 {
+ extracted = text;
+ } else {
+ assert_eq!(
+ text, extracted,
+ "every measured run copies the same selection"
+ );
}
- excesses.push(drag.saturating_sub(baseline));
+ intervals.push(drag);
}
- let best = *excesses.iter().min().expect("at least one measured pair");
- (best, excesses, extracted)
+ let best = *intervals.iter().min().expect("at least one measured run");
+ (best, intervals, extracted)
}
#[test]
@@ -318,8 +367,8 @@ fn drag_select_frame_cost_is_independent_of_session_size() {
let small = 40usize;
let large = 4000usize;
- let (small_excess, small_samples, small_copy) = drag_excess(small);
- let (large_excess, large_samples, large_copy) = drag_excess(large);
+ let (small_cost, small_samples, small_copy) = drag_cost(small);
+ let (large_cost, large_samples, large_copy) = drag_cost(large);
// The copies read the same text both sizes: the drag extracts the spanned rows through the
// same coordinates on either session.
@@ -328,17 +377,17 @@ fn drag_select_frame_cost_is_independent_of_session_size() {
"the large session drags the same rows"
);
- // The drag path's own cost (burst minus baseline, minimum over the sampled pairs) stays in
- // the same band on either session: no per-frame geometry resolve scales it with the
+ // The drag path's own cost (marker-bracketed interval, minimum over the sampled runs) stays
+ // in the same band on either session: no per-frame geometry resolve scales it with the
// transcript.
assert!(
- large_excess < small_excess + Duration::from_millis(250),
- "the large session's drag excess (best of {large_samples:?}) must stay within \
+ large_cost < small_cost + Duration::from_millis(250),
+ "the large session's drag cost (best of {large_samples:?}) must stay within \
250ms of the small session's (best of {small_samples:?})"
);
assert!(
- large_excess < Duration::from_millis(500),
- "100 drag frames on a ~40MB session cost {large_excess:?} \
+ large_cost < Duration::from_millis(500),
+ "100 drag frames on a ~40MB session cost {large_cost:?} \
(samples {large_samples:?}) — the per-frame cost is not session-size independent \
(the pre-fix release alone resolved the full geometry once per copy)"
);