mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Harden TUI subagent navigation (#39088)
## What changed - Use `/subagents` consistently for the subagent picker and status UI, removing the `/agent` alias. - Rejoin already-loaded subagent threads without overriding their existing settings. - Route notifications and approval requests only to the active thread or its known subagents, discard unrelated startup events, and avoid replaying resolved approvals. ## Testing - Add coverage for filtering buffered startup events, dropping resolved approvals, preserving owned subagent approvals, and resuming threads without setting overrides. GitOrigin-RevId: b4e597f7cd4756faccf31d8b964e7a172f5ab0eb
This commit is contained in:
1 parent
fc6268ad38
commit
0c14c73471
19 files changed
+401
-46
No files matched your search
@@ -1,7 +1,7 @@
|
||||
//! Multi-agent picker navigation and labeling state for the TUI app.
|
||||
//!
|
||||
//! This module exists to keep the pure parts of multi-agent navigation out of [`crate::app::App`].
|
||||
//! It owns the stable spawn-order cache used by the `/agent` picker, keyboard next/previous
|
||||
//! It owns the stable spawn-order cache used by the `/subagents` picker, keyboard next/previous
|
||||
//! navigation, and the contextual footer label for the thread currently being watched.
|
||||
//!
|
||||
//! Responsibilities here are intentionally narrow:
|
||||
@@ -230,7 +230,7 @@ impl AgentNavigationState {
|
||||
///
|
||||
/// This is reserved for entries that were only discovered opportunistically and never became
|
||||
/// replayable local threads. Keeping those around after the backend confirms they are gone
|
||||
/// would leave ghost rows in `/agent`.
|
||||
/// would leave ghost rows in `/subagents`.
|
||||
pub(crate) fn remove(&mut self, thread_id: ThreadId) {
|
||||
self.threads.remove(&thread_id);
|
||||
self.order.retain(|candidate| *candidate != thread_id);
|
||||
@@ -361,7 +361,7 @@ impl AgentNavigationState {
|
||||
)
|
||||
}
|
||||
|
||||
/// Builds the `/agent` picker subtitle from the same canonical bindings used by key handling.
|
||||
/// Builds the `/subagents` picker subtitle from the same canonical bindings used by key handling.
|
||||
///
|
||||
/// Keeping this text derived from the actual shortcut helpers prevents the picker copy from
|
||||
/// drifting if the bindings ever change on one platform.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Bounded, best-effort previews for the v2 `/agent` status output.
|
||||
//! Bounded, best-effort previews for the v2 `/subagents` status output.
|
||||
|
||||
use super::ThreadBufferedEvent;
|
||||
use super::ThreadEventStore;
|
||||
@@ -32,7 +32,7 @@ impl AgentStatusHistoryCell {
|
||||
impl HistoryCell for AgentStatusHistoryCell {
|
||||
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
|
||||
let mut lines: Vec<Line<'static>> = vec![
|
||||
"/agent".magenta().into(),
|
||||
"/subagents".magenta().into(),
|
||||
"Sub-agents running".bold().into(),
|
||||
"".into(),
|
||||
];
|
||||
|
||||
@@ -54,7 +54,7 @@ fn agent_status_uses_bounded_buffered_activity() {
|
||||
.join("\n");
|
||||
|
||||
insta::assert_snapshot!(rendered, @r###"
|
||||
/agent
|
||||
/subagents
|
||||
Sub-agents running
|
||||
|
||||
• `/root/reviewer`
|
||||
@@ -102,7 +102,7 @@ fn agent_status_uses_reasoning_summaries_only() {
|
||||
.join("\n");
|
||||
|
||||
insta::assert_snapshot!(rendered, @r###"
|
||||
/agent
|
||||
/subagents
|
||||
Sub-agents running
|
||||
|
||||
• `/root/reviewer`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! App-server event stream handling for the TUI app.
|
||||
|
||||
use super::App;
|
||||
use super::ThreadBufferedEvent;
|
||||
use super::app_server_event_targets::ServerNotificationThreadTarget;
|
||||
use super::app_server_event_targets::server_notification_thread_target;
|
||||
use super::app_server_event_targets::server_request_thread_id;
|
||||
@@ -11,9 +12,15 @@ use crate::app_server_session::AppServerSession;
|
||||
use crate::app_server_session::status_account_display_from_auth_mode;
|
||||
use codex_app_server_client::AppServerEvent;
|
||||
use codex_app_server_protocol::AuthMode;
|
||||
use codex_app_server_protocol::ClientRequest;
|
||||
use codex_app_server_protocol::RateLimitReachedType;
|
||||
use codex_app_server_protocol::RequestId;
|
||||
use codex_app_server_protocol::ServerNotification;
|
||||
use codex_app_server_protocol::ServerRequest;
|
||||
use codex_app_server_protocol::SessionSource;
|
||||
use codex_app_server_protocol::ThreadReadParams;
|
||||
use codex_app_server_protocol::ThreadReadResponse;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
|
||||
impl App {
|
||||
pub(super) fn refresh_mcp_startup_expected_servers_from_config(&mut self) {
|
||||
@@ -81,6 +88,10 @@ impl App {
|
||||
) {
|
||||
match ¬ification {
|
||||
ServerNotification::ServerRequestResolved(notification) => {
|
||||
self.pending_primary_events.retain(|event| {
|
||||
!matches!(event, ThreadBufferedEvent::Request(request)
|
||||
if request.id() == ¬ification.request_id)
|
||||
});
|
||||
if let Some(request) = self
|
||||
.pending_app_server_requests
|
||||
.resolve_notification(¬ification.request_id)
|
||||
@@ -179,6 +190,31 @@ impl App {
|
||||
|
||||
match server_notification_thread_target(¬ification) {
|
||||
ServerNotificationThreadTarget::Thread(thread_id) => {
|
||||
if self.primary_thread_id.is_none() && !self.pending_startup_thread_start {
|
||||
return;
|
||||
}
|
||||
if self.primary_thread_id.is_some()
|
||||
&& self.primary_thread_id != Some(thread_id)
|
||||
&& !self.thread_event_channels.contains_key(&thread_id)
|
||||
&& self.agent_navigation.get(&thread_id).is_none()
|
||||
&& !self.side_threads.contains_key(&thread_id)
|
||||
&& !matches!(¬ification, ServerNotification::McpServerStatusUpdated(_))
|
||||
&& !matches!(
|
||||
¬ification,
|
||||
ServerNotification::ThreadStarted(started)
|
||||
if matches!(
|
||||
&started.thread.source,
|
||||
SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id,
|
||||
..
|
||||
}) if self.primary_thread_id == Some(*parent_thread_id)
|
||||
|| self.thread_event_channels.contains_key(parent_thread_id)
|
||||
|| self.agent_navigation.get(parent_thread_id).is_some()
|
||||
)
|
||||
)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let result = if self.primary_thread_id == Some(thread_id)
|
||||
|| self.primary_thread_id.is_none()
|
||||
{
|
||||
@@ -232,6 +268,52 @@ impl App {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if thread_id.is_some()
|
||||
&& self.primary_thread_id.is_none()
|
||||
&& self.pending_startup_thread_start
|
||||
{
|
||||
self.pending_primary_events
|
||||
.push_back(ThreadBufferedEvent::Request(Box::new(request)));
|
||||
return;
|
||||
}
|
||||
if thread_id.is_some()
|
||||
&& self.primary_thread_id.is_none()
|
||||
&& !self.pending_startup_thread_start
|
||||
{
|
||||
return;
|
||||
}
|
||||
if let Some(thread_id) = thread_id
|
||||
&& self.primary_thread_id.is_some()
|
||||
&& self.primary_thread_id != Some(thread_id)
|
||||
&& !self.thread_event_channels.contains_key(&thread_id)
|
||||
&& self.agent_navigation.get(&thread_id).is_none()
|
||||
&& !self.side_threads.contains_key(&thread_id)
|
||||
{
|
||||
let thread = app_server_client
|
||||
.request_handle()
|
||||
.request_typed::<ThreadReadResponse>(ClientRequest::ThreadRead {
|
||||
request_id: RequestId::String(format!("subagent-approval-{thread_id}")),
|
||||
params: ThreadReadParams {
|
||||
thread_id: thread_id.to_string(),
|
||||
include_turns: false,
|
||||
},
|
||||
})
|
||||
.await;
|
||||
if !matches!(
|
||||
thread,
|
||||
Ok(ThreadReadResponse { thread })
|
||||
if matches!(thread.source,
|
||||
SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id,
|
||||
..
|
||||
}) if self.primary_thread_id == Some(parent_thread_id)
|
||||
|| self.thread_event_channels.contains_key(&parent_thread_id)
|
||||
|| self.agent_navigation.get(&parent_thread_id).is_some()
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(unsupported) = self
|
||||
.pending_app_server_requests
|
||||
|
||||
@@ -7,6 +7,9 @@
|
||||
use std::io;
|
||||
|
||||
use super::agent_picker::AGENT_PICKER_VIEW_ID;
|
||||
use super::app_server_event_targets::ServerNotificationThreadTarget;
|
||||
use super::app_server_event_targets::server_notification_thread_target;
|
||||
use super::app_server_event_targets::server_request_thread_id;
|
||||
use super::*;
|
||||
use crate::app_server_session::source_agent_path;
|
||||
use crate::app_server_session::thread_blocks_direct_input;
|
||||
@@ -357,8 +360,8 @@ impl App {
|
||||
/// TUI has not cached a local event channel yet.
|
||||
///
|
||||
/// Resume-time backfill intentionally avoids creating empty placeholder channels, because those
|
||||
/// placeholders make stale `/agent` entries open blank transcripts. When a user later selects a
|
||||
/// still-live discovered thread, attach it on demand with a real resumed snapshot.
|
||||
/// placeholders make stale `/subagents` entries open blank transcripts. When a user later
|
||||
/// selects a still-live discovered thread, attach it on demand with a real resumed snapshot.
|
||||
pub(super) async fn attach_live_thread_for_selection(
|
||||
&mut self,
|
||||
app_server: &mut AppServerSession,
|
||||
@@ -369,7 +372,11 @@ impl App {
|
||||
}
|
||||
|
||||
let (session, turns, live_attached) = match app_server
|
||||
.resume_thread(self.config.clone(), thread_id, self.resume_model_settings())
|
||||
.resume_thread(
|
||||
self.config.clone(),
|
||||
thread_id,
|
||||
crate::app_server_session::ResumeModelSettings::PreserveExistingThread,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(started) => {
|
||||
@@ -628,8 +635,48 @@ impl App {
|
||||
.set_queue_submissions_until_session_configured(/*queue*/ false);
|
||||
match result {
|
||||
Ok(started) => {
|
||||
let thread_id = started.session.thread_id;
|
||||
self.pending_primary_events.retain(|event| match event {
|
||||
ThreadBufferedEvent::Notification(notification) => matches!(
|
||||
server_notification_thread_target(notification),
|
||||
ServerNotificationThreadTarget::Thread(event_thread_id)
|
||||
if event_thread_id == thread_id
|
||||
),
|
||||
ThreadBufferedEvent::Request(request) => {
|
||||
server_request_thread_id(request) == Some(thread_id)
|
||||
}
|
||||
ThreadBufferedEvent::HistoryEntryResponse(_)
|
||||
| ThreadBufferedEvent::FeedbackSubmission(_) => true,
|
||||
});
|
||||
self.pending_app_server_requests.clear();
|
||||
let mut unsupported_requests = Vec::new();
|
||||
for event in &self.pending_primary_events {
|
||||
if let ThreadBufferedEvent::Request(request) = event
|
||||
&& let Some(unsupported) = self
|
||||
.pending_app_server_requests
|
||||
.note_server_request(request)
|
||||
{
|
||||
unsupported_requests.push(unsupported);
|
||||
}
|
||||
}
|
||||
for unsupported in unsupported_requests {
|
||||
self.pending_primary_events.retain(|event| {
|
||||
!matches!(event, ThreadBufferedEvent::Request(request)
|
||||
if request.id() == &unsupported.request_id)
|
||||
});
|
||||
if let Err(error) = self
|
||||
.reject_app_server_request(
|
||||
app_server,
|
||||
unsupported.request_id,
|
||||
unsupported.message,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::warn!("{error}");
|
||||
}
|
||||
}
|
||||
if started.blocks_direct_input {
|
||||
self.mark_primary_thread_parent_owned(started.session.thread_id);
|
||||
self.mark_primary_thread_parent_owned(thread_id);
|
||||
}
|
||||
self.enqueue_primary_thread_session(started.session, started.turns)
|
||||
.await?;
|
||||
@@ -773,7 +820,7 @@ impl App {
|
||||
/// Fetches all loaded threads from the app server and registers descendants of the primary
|
||||
/// thread in the navigation cache and chat widget metadata.
|
||||
///
|
||||
/// Called when opening the `/agent` picker and after resuming a thread so that the picker and
|
||||
/// Called when opening the `/subagents` picker and after resuming a thread so that the picker and
|
||||
/// keyboard navigation are pre-populated even if the TUI did not witness the original spawn
|
||||
/// events. Fresh and forked threads cannot have pre-existing descendants.
|
||||
///
|
||||
|
||||
@@ -1723,13 +1723,13 @@ async fn open_agent_picker_preserves_running_hints_until_observed_completion() -
|
||||
let event = app_event_rx.try_recv().expect("agent status history cell");
|
||||
if let AppEvent::InsertHistoryCell(cell) = event {
|
||||
let rendered = lines_to_single_string(&cell.display_lines(/*width*/ 80));
|
||||
if rendered.contains("/agent") {
|
||||
if rendered.contains("/subagents") {
|
||||
break rendered;
|
||||
}
|
||||
}
|
||||
};
|
||||
assert_snapshot!(status, @r###"
|
||||
/agent
|
||||
/subagents
|
||||
Sub-agents running
|
||||
|
||||
• `/root/child`
|
||||
|
||||
@@ -3,6 +3,7 @@ use crate::app_event_sender::AppEventSender;
|
||||
use crate::bottom_pane::BottomPane;
|
||||
use crate::bottom_pane::BottomPaneParams;
|
||||
use crate::tui::FrameRequester;
|
||||
use app_test_support::create_fake_parented_rollout_with_source;
|
||||
use codex_app_server_protocol::ToolRequestUserInputOption;
|
||||
use codex_app_server_protocol::ToolRequestUserInputQuestion;
|
||||
use crossterm::event::KeyCode;
|
||||
@@ -844,6 +845,195 @@ async fn startup_thread_started_submits_queued_startup_input() {
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_thread_started_discards_another_threads_buffered_events() {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
app.pending_startup_thread_start = true;
|
||||
let other_thread_id = ThreadId::new();
|
||||
app.enqueue_primary_thread_notification(ServerNotification::McpServerStatusUpdated(
|
||||
McpServerStatusUpdatedNotification {
|
||||
thread_id: Some(other_thread_id.to_string()),
|
||||
name: "other".to_string(),
|
||||
status: McpServerStartupState::Starting,
|
||||
error: None,
|
||||
failure_reason: None,
|
||||
},
|
||||
))
|
||||
.await
|
||||
.expect("foreign notification should be buffered");
|
||||
|
||||
let request = ServerRequest::CommandExecutionRequestApproval {
|
||||
request_id: AppServerRequestId::Integer(1),
|
||||
params: CommandExecutionRequestApprovalParams {
|
||||
thread_id: other_thread_id.to_string(),
|
||||
turn_id: "turn-1".to_string(),
|
||||
item_id: "item-1".to_string(),
|
||||
started_at_ms: 0,
|
||||
approval_id: None,
|
||||
environment_id: None,
|
||||
reason: None,
|
||||
network_approval_context: None,
|
||||
command: None,
|
||||
cwd: None,
|
||||
command_actions: None,
|
||||
additional_permissions: None,
|
||||
proposed_execpolicy_amendment: None,
|
||||
proposed_network_policy_amendments: None,
|
||||
available_decisions: None,
|
||||
},
|
||||
};
|
||||
app.pending_app_server_requests
|
||||
.note_server_request(&request);
|
||||
app.enqueue_primary_thread_request(request)
|
||||
.await
|
||||
.expect("foreign request should be buffered");
|
||||
|
||||
let mut app_server = Box::pin(crate::start_embedded_app_server_for_picker(
|
||||
app.chat_widget.config_ref(),
|
||||
))
|
||||
.await
|
||||
.expect("embedded app server");
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
codex_app_server_client::AppServerEvent::ServerRequest(Box::new(
|
||||
ServerRequest::CurrentTimeRead {
|
||||
request_id: AppServerRequestId::Integer(2),
|
||||
params: codex_app_server_protocol::CurrentTimeReadParams {
|
||||
thread_id: other_thread_id.to_string(),
|
||||
},
|
||||
},
|
||||
)),
|
||||
)
|
||||
.await;
|
||||
assert!(app.pending_primary_events.iter().any(|event| {
|
||||
matches!(event, ThreadBufferedEvent::Request(request)
|
||||
if matches!(request.as_ref(), ServerRequest::CurrentTimeRead { .. }))
|
||||
}));
|
||||
let thread_id = ThreadId::new();
|
||||
app.handle_startup_thread_started(
|
||||
&mut app_server,
|
||||
Ok(AppServerStartedThread {
|
||||
session: test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
turns: Vec::new(),
|
||||
blocks_direct_input: false,
|
||||
}),
|
||||
)
|
||||
.await
|
||||
.expect("startup thread should attach");
|
||||
|
||||
assert!(app.pending_primary_events.is_empty());
|
||||
assert!(
|
||||
app.active_thread_rx
|
||||
.as_mut()
|
||||
.is_some_and(|rx| rx.try_recv().is_err())
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_thread_started_does_not_replay_resolved_approval() -> Result<()> {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
app.pending_startup_thread_start = true;
|
||||
let app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
let thread_id = ThreadId::new();
|
||||
let request = exec_approval_request(thread_id, "turn-1", "item-1", /*approval_id*/ None);
|
||||
let request_id = request.id().clone();
|
||||
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
codex_app_server_client::AppServerEvent::ServerRequest(Box::new(request.clone())),
|
||||
)
|
||||
.await;
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
codex_app_server_client::AppServerEvent::ServerNotification(Box::new(
|
||||
ServerNotification::ServerRequestResolved(
|
||||
codex_app_server_protocol::ServerRequestResolvedNotification {
|
||||
thread_id: thread_id.to_string(),
|
||||
request_id,
|
||||
},
|
||||
),
|
||||
)),
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut app_server = app_server;
|
||||
app.handle_startup_thread_started(
|
||||
&mut app_server,
|
||||
Ok(AppServerStartedThread {
|
||||
session: test_thread_session(thread_id, test_path_buf("/tmp/project")),
|
||||
turns: Vec::new(),
|
||||
blocks_direct_input: false,
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert!(
|
||||
!app.pending_app_server_requests
|
||||
.contains_server_request(&request)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn owned_subagent_approval_before_thread_started_is_preserved() -> Result<()> {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
let codex_home = tempdir()?;
|
||||
app.config.codex_home = codex_home.path().to_path_buf().abs();
|
||||
app.config.sqlite = codex_state::SqliteConfig::new_for_testing(codex_home.path().abs());
|
||||
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
|
||||
let parent = app_server.start_thread(&app.config).await?;
|
||||
let parent_thread_id = parent.session.thread_id;
|
||||
app.enqueue_primary_thread_session(parent.session, parent.turns)
|
||||
.await?;
|
||||
let child_thread_id = ThreadId::from_string(
|
||||
&create_fake_parented_rollout_with_source(
|
||||
codex_home.path(),
|
||||
"2026-01-01T00-00-01",
|
||||
"2026-01-01T00:00:01Z",
|
||||
"child task",
|
||||
Some(app.config.model_provider_id.as_str()),
|
||||
/*git_info*/ None,
|
||||
RolloutSessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id,
|
||||
depth: 1,
|
||||
agent_path: None,
|
||||
agent_nickname: None,
|
||||
agent_role: None,
|
||||
}),
|
||||
parent_thread_id.into(),
|
||||
parent_thread_id,
|
||||
)
|
||||
.expect("create child rollout"),
|
||||
)?;
|
||||
app_server
|
||||
.resume_thread(
|
||||
app.config.clone(),
|
||||
child_thread_id,
|
||||
crate::app_server_session::ResumeModelSettings::RestoreFromThread,
|
||||
)
|
||||
.await?;
|
||||
let request = exec_approval_request(
|
||||
child_thread_id,
|
||||
"turn-1",
|
||||
"item-1",
|
||||
/*approval_id*/ None,
|
||||
);
|
||||
|
||||
app.handle_app_server_event(
|
||||
&app_server,
|
||||
codex_app_server_client::AppServerEvent::ServerRequest(Box::new(request.clone())),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(
|
||||
app.pending_app_server_requests
|
||||
.contains_server_request(&request)
|
||||
);
|
||||
assert!(app.thread_event_channels.contains_key(&child_thread_id));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn startup_thread_start_failure_returns_error() {
|
||||
let (mut app, _app_event_rx, _op_rx) = make_test_app_with_channels().await;
|
||||
|
||||
@@ -1355,7 +1355,11 @@ impl App {
|
||||
}
|
||||
|
||||
match app_server
|
||||
.resume_thread(self.config.clone(), thread_id, self.resume_model_settings())
|
||||
.resume_thread(
|
||||
self.config.clone(),
|
||||
thread_id,
|
||||
crate::app_server_session::ResumeModelSettings::PreserveExistingThread,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(started) => {
|
||||
@@ -1407,7 +1411,7 @@ impl App {
|
||||
.retain(ThreadEventStore::event_survives_session_refresh);
|
||||
}
|
||||
|
||||
/// Opens the `/agent` picker after refreshing cached labels for known threads.
|
||||
/// Opens the `/subagents` picker after refreshing cached labels for known threads.
|
||||
///
|
||||
/// The picker state is derived from long-lived thread channels plus best-effort metadata
|
||||
/// refreshes from the backend. Refresh failures are treated as "thread is only inspectable by
|
||||
|
||||
Reference in new issue
Block a user