Refresh the subagent picker in the background (#35693)

## Why

Opening the subagent picker could wait on thread metadata and live event-store locks, delaying terminal input. Its cached entries could also omit descendants that were not observed in the current TUI session.

## What changed

- Render the picker immediately from cached navigation state and refresh root descendants asynchronously with `thread/list`.
- Merge discovered descendants into an open picker while preserving its selection and live status, and coalesce concurrent refresh requests.
- Ignore refresh responses from a previous session and avoid blocking on busy event stores.

## Testing

- Extend session lifecycle coverage for nonblocking picker opens, refresh coalescing, descendant discovery, selection preservation, and status updates.
- Add coverage for rejecting a refresh response after navigation state is cleared.

GitOrigin-RevId: 7eae7f696f8d2d323a47b3fd2800ea1c1cdbf2a0
This commit is contained in:
Eric Traut authored and copyberry committed 2026-07-28 00:38:45 +00:00
1 parent 63682c4e1a
commit f029bb795c
10 files changed
+415 -37

No files matched your search

+63 -24
View File
@@ -4,6 +4,7 @@
//! resuming/forking saved sessions, replacing ChatWidget instances, and maintaining the agent picker
//! cache used for multi-agent navigation.
use super::agent_picker::AGENT_PICKER_VIEW_ID;
use super::*;
use crate::app_server_session::source_agent_path;
use crate::app_server_session::thread_blocks_direct_input;
@@ -26,7 +27,11 @@ pub(super) struct LoadedSubagentBackfill {
impl App {
pub(super) async fn open_agent_picker(&mut self, app_server: &mut AppServerSession) {
let backfill = self.backfill_loaded_subagent_threads(app_server).await;
let backfill = if self.primary_thread_id.is_none() {
self.backfill_loaded_subagent_threads(app_server).await
} else {
LoadedSubagentBackfill::default()
};
// V2 subagents are identified by canonical paths observed from activity events or loaded
// thread metadata. A buffered active turn is positive liveness evidence; a completed
// snapshot is terminal evidence. An empty store does not clear a successful spawn hint.
@@ -40,22 +45,23 @@ impl App {
if let Some(channel) = self.thread_event_channels.get(&thread_id)
&& channel.attachment() == ThreadEventAttachment::Live
{
let (has_active_turn, has_terminal_snapshot) = {
let store = channel.store.lock().await;
(
store.active_turn_id().is_some(),
store
.turns
.last()
.is_some_and(|turn| !matches!(turn.status, TurnStatus::InProgress)),
)
let Ok(store) = channel.store.try_lock() else {
continue;
};
let has_active_turn = store.active_turn_id().is_some();
let has_terminal_snapshot = store
.turns
.last()
.is_some_and(|turn| !matches!(turn.status, TurnStatus::InProgress));
drop(store);
if has_active_turn {
self.agent_navigation.mark_running(thread_id);
} else if has_terminal_snapshot {
self.agent_navigation.mark_stopped(thread_id);
}
} else if !backfill.refreshed_thread_ids.contains(&thread_id) {
} else if self.primary_thread_id.is_none()
&& !backfill.refreshed_thread_ids.contains(&thread_id)
{
self.refresh_agent_picker_thread_liveness(app_server, thread_id)
.await;
}
@@ -76,10 +82,16 @@ impl App {
let mut entries = Vec::new();
for (thread_id, agent_path) in running_threads {
let preview = if let Some(channel) = self.thread_event_channels.get(&thread_id) {
let store = channel.store.lock().await;
super::agent_status_feed::AgentStatusThreadPreview::from_store(
agent_path, &store,
)
match channel.store.try_lock() {
Ok(store) => {
super::agent_status_feed::AgentStatusThreadPreview::from_store(
agent_path, &store,
)
}
Err(_) => {
super::agent_status_feed::AgentStatusThreadPreview::empty(agent_path)
}
}
} else {
super::agent_status_feed::AgentStatusThreadPreview::empty(agent_path)
};
@@ -105,11 +117,14 @@ impl App {
{
continue;
}
if !self
.refresh_agent_picker_thread_liveness(app_server, thread_id)
.await
{
continue;
if self.primary_thread_id.is_none() {
self.refresh_agent_picker_thread_liveness(app_server, thread_id)
.await;
} else if self.agent_navigation.get(&thread_id).is_none() {
self.upsert_agent_picker_thread(
thread_id, /*agent_nickname*/ None, /*agent_role*/ None,
/*is_closed*/ false,
);
}
}
@@ -117,6 +132,9 @@ impl App {
.agent_navigation
.has_non_primary_thread(self.primary_thread_id);
if !self.config.features.enabled(Feature::Collab) && !has_non_primary_agent_thread {
if let Some(primary_thread_id) = self.primary_thread_id {
self.refresh_agent_picker_threads(app_server, primary_thread_id);
}
self.chat_widget.open_multi_agent_enable_prompt();
return;
}
@@ -127,14 +145,34 @@ impl App {
return;
}
let mut initial_selected_idx = None;
let selected = self
.chat_widget
.selected_index_for_present_view(AGENT_PICKER_VIEW_ID);
let params = self.agent_picker_selection_view_params(selected);
if !self
.chat_widget
.replace_selection_view_if_present(AGENT_PICKER_VIEW_ID, params)
{
let params = self.agent_picker_selection_view_params(selected);
self.chat_widget.show_selection_view(params);
}
if let Some(primary_thread_id) = self.primary_thread_id {
self.refresh_agent_picker_threads(app_server, primary_thread_id);
}
}
pub(super) fn agent_picker_selection_view_params(
&self,
selected: Option<usize>,
) -> SelectionViewParams {
let mut initial_selected_idx = selected;
let items: Vec<SelectionItem> = self
.agent_navigation
.ordered_threads()
.into_iter()
.enumerate()
.map(|(idx, (thread_id, entry))| {
if self.active_thread_id == Some(thread_id) {
if initial_selected_idx.is_none() && self.active_thread_id == Some(thread_id) {
initial_selected_idx = Some(idx);
}
let id = thread_id;
@@ -168,14 +206,15 @@ impl App {
})
.collect();
self.chat_widget.show_selection_view(SelectionViewParams {
SelectionViewParams {
view_id: Some(AGENT_PICKER_VIEW_ID),
title: Some("Subagents".to_string()),
subtitle: Some(AgentNavigationState::picker_subtitle()),
footer_hint: Some(standard_popup_hint_line()),
items,
initial_selected_idx,
..Default::default()
});
}
}
pub(super) fn is_terminal_thread_read_error(err: &color_eyre::Report) -> bool {