From 6adf62f75c4c085acee21f55a867f0da4899c16a Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sat, 6 Jun 2026 16:53:25 -0700 Subject: [PATCH] codex: address PR review feedback (#26754) --- codex-rs/tui/src/app/event_dispatch.rs | 4 ++-- codex-rs/tui/src/app/side.rs | 22 ++++++++++++++++------ codex-rs/tui/src/app/side_server.rs | 22 ++++++++++++++++++---- codex-rs/tui/src/app/tests.rs | 18 ++++++++++++++++++ codex-rs/tui/src/app_event.rs | 6 +++++- 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs index 96aa836099..42e7aabf98 100644 --- a/codex-rs/tui/src/app/event_dispatch.rs +++ b/codex-rs/tui/src/app/event_dispatch.rs @@ -1723,8 +1723,8 @@ impl App { self.handle_start_side(app_server, parent_thread_id, user_message) .await; } - AppEvent::SideThreadPrepared(result) => { - self.handle_side_thread_prepared(tui, app_server, result) + AppEvent::SideThreadPrepared { request_id, result } => { + self.handle_side_thread_prepared(tui, app_server, request_id, result) .await?; } AppEvent::OpenSkillsList => { diff --git a/codex-rs/tui/src/app/side.rs b/codex-rs/tui/src/app/side.rs index 5752ee990c..be1d2db1c5 100644 --- a/codex-rs/tui/src/app/side.rs +++ b/codex-rs/tui/src/app/side.rs @@ -197,6 +197,7 @@ pub(super) struct SideThreadState { } pub(super) struct PendingSideStart { + pub(super) request_id: Uuid, pub(super) parent_thread_id: ThreadId, pub(super) user_message: Option, } @@ -501,6 +502,11 @@ impl App { self.restore_side_user_message(user_message); } + pub(super) fn take_pending_side_start(&mut self, request_id: Uuid) -> Option { + self.pending_side_start + .take_if(|pending| pending.request_id == request_id) + } + pub(super) fn install_side_thread_snapshot( store: &mut ThreadEventStore, mut session: ThreadSessionState, @@ -561,7 +567,9 @@ impl App { self.refresh_in_memory_config_from_disk_best_effort("starting a side conversation") .await; + let request_id = Uuid::new_v4(); self.pending_side_start = Some(PendingSideStart { + request_id, parent_thread_id, user_message, }); @@ -583,7 +591,7 @@ impl App { ) .await .map_err(|err| format!("{err:#}")); - app_event_tx.send(AppEvent::SideThreadPrepared(result)); + app_event_tx.send(AppEvent::SideThreadPrepared { request_id, result }); }); } @@ -591,25 +599,27 @@ impl App { &mut self, tui: &mut tui::Tui, app_server: &mut AppServerSession, + request_id: Uuid, result: std::result::Result, ) -> Result<()> { let Some(PendingSideStart { parent_thread_id, mut user_message, - }) = self.pending_side_start.take() + .. + }) = self.take_pending_side_start(request_id) else { if let Ok(started) = result { - // Esc and session teardown only cancel the UI transition; close a child that - // finishes after its pending state has been abandoned. let thread_id = started.session.thread_id; + // ThreadStarted may have created local state before this preparation was canceled. + self.discard_thread_local_state(thread_id).await; let request_handle = app_server.request_handle(); tokio::spawn(async move { if let Err(err) = - super::side_server::unsubscribe_side_thread(request_handle, thread_id).await + super::side_server::cleanup_side_thread(request_handle, thread_id).await { tracing::warn!( thread_id = %thread_id, - "failed to unsubscribe abandoned side thread: {err}" + "failed to clean up abandoned side thread: {err}" ); } }); diff --git a/codex-rs/tui/src/app/side_server.rs b/codex-rs/tui/src/app/side_server.rs index a703b71f08..8b6dd357c2 100644 --- a/codex-rs/tui/src/app/side_server.rs +++ b/codex-rs/tui/src/app/side_server.rs @@ -5,6 +5,8 @@ use codex_app_server_protocol::ThreadInjectItemsParams; use codex_app_server_protocol::ThreadInjectItemsResponse; use codex_app_server_protocol::ThreadUnsubscribeParams; use codex_app_server_protocol::ThreadUnsubscribeResponse; +use codex_app_server_protocol::TurnInterruptParams; +use codex_app_server_protocol::TurnInterruptResponse; pub(super) async fn prepare_side_thread( request_handle: AppServerRequestHandle, @@ -38,7 +40,7 @@ pub(super) async fn prepare_side_thread( .await; if let Err(err) = inject_result { // The caller only receives fully prepared threads, so clean up this partial fork here. - if let Err(cleanup_err) = unsubscribe_side_thread(request_handle, child_thread_id).await { + if let Err(cleanup_err) = cleanup_side_thread(request_handle, child_thread_id).await { tracing::warn!( thread_id = %child_thread_id, "failed to clean up side thread after inject failure: {cleanup_err}" @@ -49,11 +51,21 @@ pub(super) async fn prepare_side_thread( Ok(started) } -pub(super) async fn unsubscribe_side_thread( +pub(super) async fn cleanup_side_thread( request_handle: AppServerRequestHandle, thread_id: ThreadId, ) -> Result<()> { - request_handle + let interrupt_result = request_handle + .request_typed::(ClientRequest::TurnInterrupt { + request_id: RequestId::String(format!("side-thread-interrupt-{}", Uuid::new_v4())), + params: TurnInterruptParams { + thread_id: thread_id.to_string(), + turn_id: String::new(), + }, + }) + .await + .wrap_err("turn/interrupt failed while cleaning up TUI side thread"); + let unsubscribe_result = request_handle .request_typed::(ClientRequest::ThreadUnsubscribe { request_id: RequestId::String(format!("side-thread-unsubscribe-{}", Uuid::new_v4())), params: ThreadUnsubscribeParams { @@ -62,5 +74,7 @@ pub(super) async fn unsubscribe_side_thread( }) .await .wrap_err("thread/unsubscribe failed in TUI") - .map(drop) + .map(drop); + interrupt_result?; + unsubscribe_result } diff --git a/codex-rs/tui/src/app/tests.rs b/codex-rs/tui/src/app/tests.rs index 760014bbb9..e70866edaf 100644 --- a/codex-rs/tui/src/app/tests.rs +++ b/codex-rs/tui/src/app/tests.rs @@ -3386,6 +3386,7 @@ async fn side_thread_ignores_global_mcp_startup_notifications() { async fn cancelling_pending_side_start_restores_message() { let mut app = make_test_app().await; app.pending_side_start = Some(PendingSideStart { + request_id: Uuid::new_v4(), parent_thread_id: ThreadId::new(), user_message: Some(crate::chatwidget::UserMessage::from("side question")), }); @@ -3400,6 +3401,23 @@ async fn cancelling_pending_side_start_restores_message() { ); } +#[tokio::test] +async fn side_completion_only_consumes_its_own_pending_start() { + let mut app = make_test_app().await; + let current_request_id = Uuid::new_v4(); + app.pending_side_start = Some(PendingSideStart { + request_id: current_request_id, + parent_thread_id: ThreadId::new(), + user_message: None, + }); + + assert!(app.take_pending_side_start(Uuid::new_v4()).is_none()); + assert_eq!( + app.pending_side_start.as_ref().unwrap().request_id, + current_request_id + ); +} + #[tokio::test] async fn side_discard_selection_keeps_current_side_thread() { let mut app = make_test_app().await; diff --git a/codex-rs/tui/src/app_event.rs b/codex-rs/tui/src/app_event.rs index 648221221a..3f5c28bdde 100644 --- a/codex-rs/tui/src/app_event.rs +++ b/codex-rs/tui/src/app_event.rs @@ -48,6 +48,7 @@ use codex_protocol::models::ActivePermissionProfile; use codex_protocol::openai_models::ReasoningEffort; use codex_realtime_webrtc::RealtimeWebrtcEvent; use codex_realtime_webrtc::RealtimeWebrtcSessionHandle; +use uuid::Uuid; use crate::history_cell::HistoryCell; @@ -147,7 +148,10 @@ pub(crate) enum AppEvent { user_message: Option, }, /// Finish preparing a transient side conversation off the TUI event loop. - SideThreadPrepared(Result), + SideThreadPrepared { + request_id: Uuid, + result: Result, + }, /// Submit an op to the specified thread, regardless of current focus. SubmitThreadOp {