A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
Open autocomplete dropdowns while a reply streams (#3379)
Sebastian Müllere99b901388ad
diff --git a/.changes/fix-tui-quiet-tick-stream-starvation.md b/.changes/fix-tui-quiet-tick-stream-starvation.mdnew file mode 100644index 0000000000..7262f7870f--- /dev/null+++ b/.changes/fix-tui-quiet-tick-stream-starvation.md@@ -0,0 +1 @@+- The `/` and `@` autocomplete dropdowns now open, and drag auto-scroll keeps scrolling, while a reply streams, instead of waiting for the stream to pause.diff --git a/crates/pa-tui/src/interactive/run.rs b/crates/pa-tui/src/interactive/run.rsindex 62dcbda7a3..5b7e37f0b1 100644--- a/crates/pa-tui/src/interactive/run.rs+++ b/crates/pa-tui/src/interactive/run.rs@@ -19,6 +19,8 @@ const HEADLESS_SETTLE_TIMEOUT_MS: u64 = 60_000;const MIN_RENDER_INTERVAL: Duration = Duration::from_millis(16);/// The spinner's wall-clock cadence, not the render rate.const SPINNER_INTERVAL_MS: u128 = 80;+/// The input-idle window the quiet tick waits out before materializing parked work.+const QUIET_TICK_INTERVAL: Duration = Duration::from_millis(50);/// The animating loader's next phase boundary, the wake the select needs/// while a quiet turn waits out its stream: TS `Loader`'s `setInterval`@@ -779,6 +781,7 @@ async fn run_interactive_surface(// is armed while a dirty frame waits out the interval.let mut last_render_at: Option<Instant> = None;let mut render_deadline: Option<Instant> = None;+ let mut quiet_tick_deadline: Option<Instant> = None;let mut anim_started: Option<Instant> = None;// The spinner phase painted by the last frame (`usize::MAX` before the first): a quiet turn// only dirties when the 80ms phase advances.@@ -1282,6 +1285,14 @@ async fn run_interactive_surface(// iteration. Terminal runs never arm it (`headless_done`// exists only on the headless harness).let settle_recheck_wanted = headless_done && headless_settle_pending;+ // The window anchors at the first iteration that sees pending work: another arm's+ // wake must not restart it, or a streaming reply starves the parked request.+ quiet_tick_deadline = if autocomplete_pending || auto_scroll_armed || settle_recheck_wanted+ {+ quiet_tick_deadline.or_else(|| Some(Instant::now() + QUIET_TICK_INTERVAL))+ } else {+ None+ };tokio::select! {maybe_event = async {// A closed channel's recv() resolves None instantly and forever; while the@@ -1492,6 +1503,11 @@ async fn run_interactive_surface(}} => {if let Some(input) = maybe_input {+ // A keystroke restarts the window: the rest of a typed burst (a+ // command plus its Enter) applies before a parked request materializes.+ if matches!(input, UiInput::Key(_) | UiInput::Paste(_)) {+ quiet_tick_deadline = None;+ }pending.push_back(input);}}@@ -1835,11 +1851,14 @@ async fn run_interactive_surface(// autocomplete request (suggestions resolve asynchronously after the keystroke// batch, so the dropdown opens only once typing pauses) or an armed selection// auto-scroll. An idle surface parks this arm.- if !(autocomplete_pending || auto_scroll_armed || settle_recheck_wanted) {- std::future::pending::<()>().await;+ match quiet_tick_deadline {+ Some(deadline) => {+ tokio::time::sleep_until(tokio::time::Instant::from_std(deadline)).await;+ }+ None => std::future::pending::<()>().await,}- tokio::time::sleep(Duration::from_millis(50)).await;} => {+ quiet_tick_deadline = None;session.materialize_editor_autocomplete(&mut view);session.selection_auto_scroll_tick(&mut view);}diff --git a/crates/pa-tui/tests/quiet_tick_busy_stream_headless.rs b/crates/pa-tui/tests/quiet_tick_busy_stream_headless.rsnew file mode 100644index 0000000000..52fdf764ed--- /dev/null+++ b/crates/pa-tui/tests/quiet_tick_busy_stream_headless.rs@@ -0,0 +1,256 @@+//! Headless e2e for the quiet tick under a busy stream: one loop-local deadline that stream+//! wakes cannot restart materializes a parked autocomplete request mid-reply, while a+//! keystroke still restarts the window, so a typed command plus Enter submits as typed.+#![cfg(unix)]++use std::io::{BufRead, BufReader, Write};+use std::os::unix::net::{UnixListener, UnixStream};+use std::path::PathBuf;+use std::time::Duration;++use pa_tui::interactive::{+ run_interactive, HeadlessPlan, HeadlessStep, InteractiveOptions, ModelSelection,+ SessionSelection, UiMode,+};+use serde_json::{json, Value};++/// The stream's cadence: intervals shorter than the tick's 50ms window, so stream wakes alone+/// keep a per-iteration sleep from completing.+const STREAM_INTERVAL_MS: u64 = 20;+/// Deltas past the render barriers' budget, so a starved tick cannot wait out the stream.+const STREAM_DELTAS: usize = 130;+const BARRIER_TIMEOUT_MS: u64 = 1200;++/// Serve one client: attach answers with the empty-session snapshot and spawns the streaming+/// reply; every other command gets one success response.+fn serve(listener: &UnixListener) {+ let (stream, _) = listener.accept().expect("accept");+ let mut writer = stream.try_clone().expect("clone mock socket");+ let mut reader = BufReader::new(stream);++ write_json(+ &mut writer,+ &json!({+ "type": "daemon_hello",+ "protocol": { "name": "prime-agent.daemon", "version": 7 },+ "serverCapabilities": [],+ "clientId": "mock",+ }),+ );++ let mut line = String::new();+ loop {+ line.clear();+ match reader.read_line(&mut line) {+ Ok(0) | Err(_) => break,+ Ok(_) => {}+ }+ let Ok(envelope) = serde_json::from_str::<Value>(line.trim()) else {+ continue;+ };+ let id = envelope.get("id").and_then(Value::as_str).unwrap_or("");+ let command_type = envelope+ .get("command")+ .and_then(|command| command.get("type"))+ .and_then(Value::as_str)+ .unwrap_or_default()+ .to_string();+ match command_type.as_str() {+ "attach" => {+ write_json(&mut writer, &attach_data(id));+ // The reply streams on its own handle while this loop keeps answering.+ let stream_writer = writer.try_clone().expect("clone stream socket");+ std::thread::spawn(move || stream_reply(stream_writer));+ }+ // create, get_commands, get_session_stats, detach: one success covers all.+ _ => {+ write_json(+ &mut writer,+ &json!({+ "type": "response",+ "id": id,+ "command": command_type,+ "success": true,+ "data": {+ "activeSessionId": "s1",+ "id": "s1",+ "sessionId": "sess-1",+ "sessionFile": "/tmp/sess-1.jsonl",+ },+ }),+ );+ }+ }+ }+}++/// Stream one reply: a turn start, assistant deltas at the stream cadence, then the turn end.+fn stream_reply(mut writer: UnixStream) {+ let event = |payload: Value| {+ json!({+ "type": "session_event",+ "activeSessionId": "s1",+ "event": payload,+ })+ };+ write_json(&mut writer, &event(json!({ "type": "turn_start" })));+ for delta in 0..STREAM_DELTAS {+ std::thread::sleep(Duration::from_millis(STREAM_INTERVAL_MS));+ write_json(+ &mut writer,+ &event(json!({+ "type": "message_update",+ "message": {+ "role": "assistant",+ "content": [{ "type": "text", "text": format!("reply delta {delta}") }],+ },+ "assistantMessageEvent": { "type": "text_delta", "delta": " more text" },+ })),+ );+ }+ write_json(&mut writer, &event(json!({ "type": "turn_end" })));+}++fn write_json(writer: &mut UnixStream, value: &Value) {+ let mut line = serde_json::to_string(value).expect("serialize mock frame");+ line.push('\n');+ writer.write_all(line.as_bytes()).expect("write mock frame");+ writer.flush().expect("flush mock frame");+}++/// The attach result: one empty, quiet session.+fn attach_data(id: &str) -> Value {+ json!({+ "type": "response",+ "id": id,+ "command": "attach",+ "success": true,+ "data": {+ "activeSessionId": "s1",+ "snapshot": {+ "state": {+ "sessionId": "sess-1",+ "sessionName": "busy stream session",+ "isStreaming": false,+ },+ "messages": [],+ },+ },+ })+}++fn options(socket: PathBuf) -> InteractiveOptions {+ InteractiveOptions {+ models: None,+ socket_path: socket,+ cwd: PathBuf::from("/tmp"),+ session_dir: None,+ script_path: None,+ model_selection: ModelSelection::default(),+ model_catalog: Vec::new(),+ model_configured_providers: std::collections::HashSet::default(),+ model_recent_models: Vec::new(),+ default_thinking_level: None,+ no_session: false,+ session: SessionSelection::New,+ initial_message: None,+ show_images: true,+ fullscreen_mouse: false,+ theme: "prime".to_string(),+ code_block_indent: " ".to_string(),+ tree_filter_mode: String::new(),+ branch_summary_skip_prompt: false,+ version: "0.0.0".to_string(),+ onboarding: None,+ telemetry_disabled: None,+ client_auth: None,+ traces: None,+ provider_auth: None,+ update_commands: None,+ telemetry: None,+ keybindings: pa_tui::keybindings::KeybindingsManager::new(),+ session_rlm_depth: None,+ prompt_stash: std::sync::Arc::default(),+ session_has_children: false,+ restore_dock_focus: false,+ client_settings: None,+ }+}++/// Run one plan against the mock and return the rendered frames.+fn run_plan(steps: Vec<HeadlessStep>) -> Vec<String> {+ // Scrub the ambient TMUX variable: its startup notice must not reach the frames.+ std::env::remove_var("TMUX");+ let dir = tempfile::TempDir::new().expect("temp dir");+ let socket = dir.path().join("tui.sock");+ let listener = UnixListener::bind(&socket).expect("bind mock socket");+ let handle = std::thread::spawn(move || serve(&listener));+ let runtime = tokio::runtime::Builder::new_multi_thread()+ .enable_all()+ .build()+ .expect("tokio runtime");+ let plan = HeadlessPlan {+ steps,+ width: 100,+ height: 30,+ };+ let outcome = runtime+ .block_on(run_interactive(options(socket), UiMode::Headless(plan)))+ .expect("interactive run");+ handle.join().expect("mock supervisor finished");+ outcome.frames+}++/// A parked autocomplete request materializes while the reply streams: the second barrier+/// releases only on the dropdown's selected row, and its budget is shorter than the stream,+/// so a tick that waits for the stream to pause times the barrier out instead.+#[test]+fn the_quiet_tick_materializes_autocomplete_while_a_reply_streams() {+ let frames = run_plan(vec![+ // The `/` must land mid-stream: the barrier waits for a rendered delta first.+ HeadlessStep::WaitRender {+ needle: "reply delta".to_string(),+ timeout_ms: BARRIER_TIMEOUT_MS,+ },+ HeadlessStep::Type("/".to_string()),+ HeadlessStep::WaitRender {+ needle: "\u{203a} settings".to_string(),+ timeout_ms: BARRIER_TIMEOUT_MS,+ },+ ]);+ let all = frames.join("\n");+ assert!(+ !all.contains("timed out waiting"),+ "a render barrier timed out: the tick starved while the stream ran:\n{all}"+ );+}++/// A keystroke restarts the window: a typed command plus its Enter in one burst submits as+/// typed, instead of a mid-burst dropdown's Enter completing the command into its args.+#[test]+fn a_typed_command_burst_submits_as_typed() {+ let mut steps = vec![+ // The burst lands mid-stream, like a fast typist over a busy turn.+ HeadlessStep::WaitRender {+ needle: "reply delta".to_string(),+ timeout_ms: BARRIER_TIMEOUT_MS,+ },+ ];+ // The keys land 15ms apart: each gap stays far inside the 50ms window, their sum+ // crosses it, so a window anchored at the first key closes mid-burst.+ for key in ["/", "n", "a", "m", "e"] {+ steps.push(HeadlessStep::Type(key.to_string()));+ steps.push(HeadlessStep::WaitMs(15));+ }+ // The Enter rides with the burst's tail, the typed-ahead submit path.+ steps.push(HeadlessStep::Type("\r".to_string()));+ steps.push(HeadlessStep::WaitRender {+ needle: "Session name: busy stream session".to_string(),+ timeout_ms: BARRIER_TIMEOUT_MS,+ });+ let all = run_plan(steps).join("\n");+ assert!(+ all.contains("Session name: busy stream session"),+ "the burst's Enter completed a mid-burst dropdown instead of submitting /name:\n{all}"+ );+}