Avoid redundant TUI subagent metadata requests (#34234)

## What changed

- Skip loaded-subagent backfills for fresh and forked threads, which cannot have pre-existing descendants.
- Keep the backfill after resuming a thread so agent navigation is populated immediately.
- Reuse thread status collected during backfill when opening the agent picker, avoiding duplicate `thread/read` requests while preserving liveness from live event channels.

## Testing

- Add a WebSocket-backed session lifecycle test that records app-server requests across fork, fresh-session, resume, and agent-picker flows.

GitOrigin-RevId: 604742b3cb495da12c97908d7a0ffa4ebc6a6da3
This commit is contained in:
Charlie Marsh authored and copyberry committed 2026-07-19 21:39:58 +00:00
1 parent a97ae65362
commit 678157acaa
9 files changed
+380 -21

No files matched your search

+13
View File
@@ -17,6 +17,7 @@
use crate::app_server_session::thread_blocks_direct_input;
use codex_app_server_protocol::SessionSource;
use codex_app_server_protocol::Thread;
use codex_app_server_protocol::ThreadStatus;
use codex_protocol::ThreadId;
use codex_protocol::protocol::SubAgentSource;
use std::collections::HashMap;
@@ -31,6 +32,8 @@ pub(crate) struct LoadedSubagentThread {
pub(crate) agent_role: Option<String>,
pub(crate) agent_path: Option<String>,
pub(crate) blocks_direct_input: bool,
pub(crate) is_running: bool,
pub(crate) is_closed: bool,
}
/// Walks the spawn tree rooted at `primary_thread_id` and returns every descendant subagent.
@@ -87,6 +90,8 @@ pub(crate) fn find_loaded_subagent_threads_for_primary(
.remove(&thread_id)
.map(|thread| LoadedSubagentThread {
blocks_direct_input: thread_blocks_direct_input(&thread),
is_running: matches!(&thread.status, ThreadStatus::Active { .. }),
is_closed: matches!(&thread.status, ThreadStatus::NotLoaded),
thread_id,
agent_nickname: thread.agent_nickname,
agent_role: thread.agent_role,
@@ -196,6 +201,9 @@ mod tests {
child.agent_nickname = Some("Scout".to_string());
child.agent_role = Some("explorer".to_string());
child.can_accept_direct_input = Some(true);
child.status = ThreadStatus::Active {
active_flags: Vec::new(),
};
let mut grandchild = test_thread(
grandchild_thread_id,
@@ -204,6 +212,7 @@ mod tests {
grandchild.agent_nickname = Some("Atlas".to_string());
grandchild.agent_role = Some("worker".to_string());
grandchild.can_accept_direct_input = Some(false);
grandchild.status = ThreadStatus::NotLoaded;
let unrelated_child = test_thread(
unrelated_child_id,
thread_spawn_source(unrelated_parent_id, /*depth*/ 1, "Other", "researcher"),
@@ -228,6 +237,8 @@ mod tests {
agent_nickname: Some("Scout".to_string()),
agent_role: Some("explorer".to_string()),
agent_path: None,
is_running: true,
is_closed: false,
},
LoadedSubagentThread {
blocks_direct_input: true,
@@ -235,6 +246,8 @@ mod tests {
agent_nickname: Some("Atlas".to_string()),
agent_role: Some("worker".to_string()),
agent_path: None,
is_running: false,
is_closed: true,
},
]
);