Skip to content

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

Rust

select the best match when searching the agents view (#3289)

kt35b8727a75f1
diff --git a/crates/pa-tui/src/agents_view.rs b/crates/pa-tui/src/agents_view.rs
index 7623929c6e..49aadc5b3f 100644
--- a/crates/pa-tui/src/agents_view.rs
+++ b/crates/pa-tui/src/agents_view.rs
@@ -473,6 +473,10 @@ struct AgentsViewMode {
/// user move cancels the wait. A scoped view never lists the anchor
/// (the scope root is excluded), so the first-row default stands there.
anchor_selection_pending: bool,
+ /// The selection a cleared search returns to: the row selected when
+ /// the query went non-empty. A move while searching drops it, so the
+ /// clear keeps the user's pick.
+ search_return: Option<(String, SelectionKey)>,
/// First ctrl+c shows the exit hint; the second exits.
exit_armed: bool,
/// The double-Ctrl+C force-quit guard (the run's shared instance is
@@ -635,6 +639,7 @@ impl AgentsViewMode {
selected_identity,
selected_key,
anchor_selection_pending,
+ search_return: None,
exit_armed: false,
exit_guard: crate::exit_guard::ExitGuard::new(),
pulse: 0,
diff --git a/crates/pa-tui/src/agents_view/data_input.rs b/crates/pa-tui/src/agents_view/data_input.rs
index b762d9e20b..bc32f54860 100644
--- a/crates/pa-tui/src/agents_view/data_input.rs
+++ b/crates/pa-tui/src/agents_view/data_input.rs
@@ -6,7 +6,7 @@
use super::{
build_rows, compute_rollups, filter_empty_sessions, filter_unified_sessions,
parse_search_query, reconcile_unified_sessions, resolve_selection, scope_ancestors, scope_root,
- scope_to_subtree, AgentsViewMode, AgentsViewScope, Composer, OpenedRow, PathBuf,
+ scope_to_subtree, AgentsViewMode, AgentsViewRow, AgentsViewScope, Composer, OpenedRow, PathBuf,
PressedMouseClick, RowKind, ScopeRoot, SelectionEdge, SelectionKey, SessionSelection, Value,
ANCHOR_LOADING_HINT,
};
@@ -237,6 +237,7 @@ impl AgentsViewMode {
/// which must never override it.
pub(super) fn move_selection(&mut self, delta: isize) {
self.anchor_selection_pending = false;
+ self.search_return = None;
self.clear_anchor_loading_hint();
let selectable: Vec<usize> = self
.rows
@@ -269,6 +270,7 @@ impl AgentsViewMode {
/// roster rebuild resolves the selection back onto the landed row.
pub(super) fn move_selection_to(&mut self, edge: SelectionEdge) {
self.anchor_selection_pending = false;
+ self.search_return = None;
self.clear_anchor_loading_hint();
let selectable: Vec<usize> = self
.rows
@@ -358,6 +360,14 @@ impl AgentsViewMode {
self.end_anchor_wait();
}
+ /// One search edit (TS `queryChanged`). The rebuilt list selects its
+ /// first row, the top-ranked hit, instead of following the previous
+ /// row (a lower-ranked hit, or an index clamped onto the last hit when
+ /// that row was filtered out). The first keystroke remembers the
+ /// selected session, and the edit that clears the query returns to it
+ /// while it is still listed, else to the top. Searching is an explicit
+ /// choice, so it ends the entry anchor's wait (TS `syncSelectedRowState`).
+ ///
/// TS `rearmSavedSearchFetch`: a terminal saved-catalog failure re-arms
/// on the next query change. The loop owns the client, so the mode only
/// records the intent; `take_saved_fetch_rearm` hands it to the loop
@@ -365,8 +375,26 @@ impl AgentsViewMode {
/// concurrent `list_saved_sessions` scans (whose completions can
/// arrive out of order) never race a stale failure over a newer
/// success.
- pub(super) fn note_query_changed(&mut self) {
+ pub(super) fn query_changed(&mut self, was_empty: bool) {
self.saved_query_rearm = self.saved_fetch_failed;
+ self.end_anchor_wait();
+ if was_empty {
+ self.search_return = self
+ .selected_identity
+ .clone()
+ .zip(self.selected_key.clone());
+ }
+ self.rebuild_rows();
+ if !self.query.is_empty() {
+ self.selected = self
+ .rows
+ .iter()
+ .position(AgentsViewRow::selectable)
+ .unwrap_or(0);
+ } else if let Some((identity, key)) = self.search_return.take() {
+ self.selected = resolve_selection(&self.rows, 0, Some(&identity), Some(&key));
+ }
+ self.sync_selected_row_state();
}
/// The selected row's stop-or-delete arming target (the confirm's
@@ -636,8 +664,7 @@ impl AgentsViewMode {
if self.keybindings.matches(key, "app.input.clear") {
if !self.query.is_empty() {
self.query.clear();
- self.note_query_changed();
- self.rebuild_rows();
+ self.query_changed(false);
} else if self.scope_active {
self.open_scope_root(false);
} else {
@@ -661,9 +688,8 @@ impl AgentsViewMode {
// A no-op edit on an empty query changes nothing: the
// re-arm's expensive retry must not fire behind it.
if self.query.pop().is_some() {
- self.note_query_changed();
+ self.query_changed(false);
}
- self.rebuild_rows();
return;
}
if self
@@ -672,9 +698,8 @@ impl AgentsViewMode {
{
if !self.query.is_empty() {
self.query.clear();
- self.note_query_changed();
+ self.query_changed(false);
}
- self.rebuild_rows();
return;
}
// The printable decode the editor uses (`decode_printable`):
@@ -682,9 +707,9 @@ impl AgentsViewMode {
// raw space there), and the shift+letter ids decode to their
// characters.
if let Some(text) = crate::editor::decode_printable(key) {
+ let was_empty = self.query.is_empty();
self.query.push_str(&text);
- self.note_query_changed();
- self.rebuild_rows();
+ self.query_changed(was_empty);
}
}
@@ -810,6 +835,7 @@ impl AgentsViewMode {
self.exit_armed = false;
self.pending_delete = None;
self.selected = *index;
+ self.search_return = None;
// A click is an explicit user choice like a direction key: it
// ends the entry anchor's wait, so the open below targets the
// clicked row, never the loading hint.
diff --git a/crates/pa-tui/src/agents_view/tests.rs b/crates/pa-tui/src/agents_view/tests.rs
index 992a0cde1c..9b19d4c4e6 100644
--- a/crates/pa-tui/src/agents_view/tests.rs
+++ b/crates/pa-tui/src/agents_view/tests.rs
@@ -22,6 +22,7 @@ mod render_pulse;
mod reply;
mod running_lines;
mod saved_catalog;
+mod search_selection;
mod selection_churn;
/// One idle row under test plus a holder row that keeps the selection,
diff --git a/crates/pa-tui/src/agents_view/tests/entry_anchor.rs b/crates/pa-tui/src/agents_view/tests/entry_anchor.rs
index a644a897ff..c4085d79b4 100644
--- a/crates/pa-tui/src/agents_view/tests/entry_anchor.rs
+++ b/crates/pa-tui/src/agents_view/tests/entry_anchor.rs
@@ -191,13 +191,13 @@ fn a_saved_catalog_failure_settles_the_anchor_wait() {
#[test]
fn a_failed_fetch_rearms_once_per_query_change() {
let mut mode = mode_with_anchor(None, Vec::new());
- mode.note_query_changed();
+ mode.query_changed(true);
assert!(
!mode.take_saved_fetch_rearm(),
"a healthy fetch never re-arms"
);
mode.saved_fetch_failed = true;
- mode.note_query_changed();
+ mode.query_changed(true);
assert!(
mode.take_saved_fetch_rearm(),
"the query change after a failure re-arms the fetch"
@@ -208,19 +208,19 @@ fn a_failed_fetch_rearms_once_per_query_change() {
);
// The arm consumed the failure flag: no second concurrent retry
// until the in-flight one fails again.
- mode.note_query_changed();
+ mode.query_changed(true);
assert!(
!mode.take_saved_fetch_rearm(),
"the retry in flight is the only one"
);
mode.saved_fetch_failed = true;
- mode.note_query_changed();
+ mode.query_changed(true);
assert!(
mode.take_saved_fetch_rearm(),
"a new terminal failure re-arms again"
);
mode.saved_fetch_failed = false;
- mode.note_query_changed();
+ mode.query_changed(true);
assert!(!mode.take_saved_fetch_rearm());
}
diff --git a/crates/pa-tui/src/agents_view/tests/search_selection.rs b/crates/pa-tui/src/agents_view/tests/search_selection.rs
new file mode 100644
index 0000000000..f245e40d9c
--- /dev/null
+++ b/crates/pa-tui/src/agents_view/tests/search_selection.rs
@@ -0,0 +1,157 @@
+//! The selection under search edits: every query change lands on the
+//! top-ranked hit, and clearing the query returns to the session the
+//! selection sat on before the search began.
+
+use super::*;
+
+/// Six idle top-level sessions, newest first; three of them match
+/// "gateway", and the exact-name match ranks first.
+fn search_roster() -> Vec<serde_json::Value> {
+ let names = [
+ "write docs",
+ "gateway worker",
+ "fix login bug",
+ "deploy the gateway now",
+ "refactor tests",
+ "gateway",
+ ];
+ names
+ .iter()
+ .enumerate()
+ .map(|(index, name)| {
+ let n = index + 1;
+ roster_entry(
+ &format!("s{n}"),
+ "idle",
+ &serde_json::json!({
+ "sessionId": format!("s{n}"), "lifecycle": "live",
+ "activeSessionId": format!("s{n}-live"),
+ "sessionFile": format!("/x/s{n}.jsonl"),
+ "runtimeKind": "top-level",
+ "sessionName": name,
+ "messageCount": 2,
+ "rlmDepth": 0,
+ "lastActivityAt": format!("2025-01-{:02}T00:00:00.000Z", 7 - n),
+ }),
+ )
+ })
+ .collect()
+}
+
+fn type_query(mode: &mut AgentsViewMode, text: &str) {
+ for ch in text.chars() {
+ mode.handle_key(&ch.to_string());
+ }
+}
+
+fn selected_title(mode: &AgentsViewMode) -> &str {
+ &mode.rows[mode.selected].title
+}
+
+fn first_selectable(mode: &AgentsViewMode) -> usize {
+ mode.rows
+ .iter()
+ .position(AgentsViewRow::selectable)
+ .expect("a selectable row")
+}
+
+/// The reported bug: with the selection at the bottom of the list,
+/// typing a query clamped the selection onto the LAST hit. Every
+/// keystroke lands on the first row, the top-ranked hit.
+#[test]
+fn typing_a_query_selects_the_top_hit() {
+ let mut mode = fresh_mode(search_roster());
+ mode.handle_key("end");
+ assert_eq!(selected_title(&mode), "gateway");
+ mode.handle_key("up");
+ assert_eq!(selected_title(&mode), "refactor tests");
+ for ch in "gateway".chars() {
+ mode.handle_key(&ch.to_string());
+ assert_eq!(
+ mode.selected,
+ first_selectable(&mode),
+ "after {:?} the selection sits on the first hit",
+ mode.query
+ );
+ }
+ assert_eq!(mode.rows.len(), 3);
+ assert_eq!(selected_title(&mode), "gateway");
+ assert_eq!(
+ mode.selected_identity.as_deref(),
+ Some(mode.rows[mode.selected].identity.as_str()),
+ "the carried identity follows the top hit"
+ );
+}
+
+/// A selected session that is itself a lower-ranked hit does not hold
+/// the selection: the query change still lands on the top hit, and so
+/// does deleting a character while the query stays non-empty.
+#[test]
+fn a_matching_selection_still_moves_to_the_top_hit() {
+ let mut mode = fresh_mode(search_roster());
+ mode.handle_key("down");
+ assert_eq!(selected_title(&mode), "gateway worker");
+ type_query(&mut mode, "gateway");
+ assert_eq!(selected_title(&mode), "gateway");
+ mode.handle_key("down");
+ assert_ne!(mode.selected, first_selectable(&mode));
+ mode.handle_key("backspace");
+ assert_eq!(mode.query, "gatewa");
+ assert_eq!(mode.selected, first_selectable(&mode));
+}
+
+/// Clearing the query (backspace to empty, ctrl+u, or escape) returns
+/// the selection to the session it sat on before the search began.
+#[test]
+fn clearing_the_query_restores_the_pre_search_selection() {
+ for clear in ["backspace", "ctrl+u", "escape"] {
+ let mut mode = fresh_mode(search_roster());
+ mode.handle_key("down");
+ mode.handle_key("down");
+ assert_eq!(selected_title(&mode), "fix login bug");
+ type_query(&mut mode, "gate");
+ assert_eq!(mode.selected, first_selectable(&mode));
+ if clear == "backspace" {
+ for _ in 0..4 {
+ mode.handle_key("backspace");
+ }
+ } else {
+ mode.handle_key(clear);
+ }
+ assert!(mode.query.is_empty(), "{clear} clears the query");
+ assert_eq!(selected_title(&mode), "fix login bug", "{clear}");
+ assert_eq!(
+ mode.selected_identity.as_deref(),
+ Some(mode.rows[mode.selected].identity.as_str()),
+ );
+ }
+}
+
+/// A row the user picks while searching is their selection: clearing
+/// the query keeps it instead of returning to the pre-search row.
+#[test]
+fn a_pick_made_while_searching_survives_the_clear() {
+ let mut mode = fresh_mode(search_roster());
+ type_query(&mut mode, "gateway");
+ mode.handle_key("down");
+ let picked = selected_title(&mode).to_string();
+ assert_ne!(picked, "gateway");
+ mode.handle_key("ctrl+u");
+ assert_eq!(selected_title(&mode), picked);
+}
+
+/// The pre-search session left while the query was up: the clear lands
+/// on the top of the list.
+#[test]
+fn clearing_after_the_pre_search_session_left_selects_the_top() {
+ let mut mode = fresh_mode(search_roster());
+ mode.handle_key("end");
+ mode.handle_key("up");
+ assert_eq!(selected_title(&mode), "refactor tests");
+ type_query(&mut mode, "gate");
+ mode.apply_roster_update(Vec::new(), vec!["s5".to_string()], false);
+ mode.handle_key("escape");
+ assert!(mode.query.is_empty());
+ assert_eq!(mode.selected, first_selectable(&mode));
+ assert_eq!(selected_title(&mode), "write docs");
+}