diff --git a/codex-rs/core/src/agent/control.rs b/codex-rs/core/src/agent/control.rs index 7083081dc..bc978d3ba 100644 --- a/codex-rs/core/src/agent/control.rs +++ b/codex-rs/core/src/agent/control.rs @@ -85,7 +85,6 @@ impl AgentControl { result } - #[allow(dead_code)] // Will be used for collab tools. /// Fetch the last known status for `agent_id`, returning `NotFound` when unavailable. pub(crate) async fn get_status(&self, agent_id: ThreadId) -> AgentStatus { let Ok(state) = self.upgrade() else { diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index f533e608f..01dc99d91 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -235,6 +235,15 @@ impl ThreadManager { self.state.threads.write().await.remove(thread_id) } + /// Closes all threads open in this ThreadManager + pub async fn remove_and_close_all_threads(&self) -> CodexResult<()> { + for thread in self.state.threads.read().await.values() { + thread.submit(Op::Shutdown).await?; + } + self.state.threads.write().await.clear(); + Ok(()) + } + /// Fork an existing thread by taking messages up to the given position (not including /// the message at the given position) and starting a new thread with identical /// configuration (unless overridden by the caller's `config`). The new thread will have diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index b256ccd20..211975c27 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -672,6 +672,9 @@ impl App { let summary = session_summary(self.chat_widget.token_usage(), self.chat_widget.thread_id()); self.shutdown_current_thread().await; + if let Err(err) = self.server.remove_and_close_all_threads().await { + tracing::warn!(error = %err, "failed to close all threads"); + } let init = crate::chatwidget::ChatWidgetInit { config: self.config.clone(), frame_requester: tui.frame_requester(), diff --git a/codex-rs/tui2/src/app.rs b/codex-rs/tui2/src/app.rs index e6f7fd5f4..de2fe2001 100644 --- a/codex-rs/tui2/src/app.rs +++ b/codex-rs/tui2/src/app.rs @@ -1447,6 +1447,9 @@ impl App { self.chat_widget.conversation_id(), ); self.shutdown_current_conversation().await; + if let Err(err) = self.server.remove_and_close_all_threads().await { + tracing::warn!(error = %err, "failed to close all threads"); + } let init = crate::chatwidget::ChatWidgetInit { config: self.config.clone(), frame_requester: tui.frame_requester(),