mirror of
https://github.com/openai/codex.git
synced 2026-05-03 04:42:20 +03:00
Fix spinner/Esc interrupt when MCP startup completes mid-turn (#8661)
## **Problem** Codex’s TUI uses a single “task running” indicator (spinner + Esc interrupt hint) to communicate “the UI is busy”. In practice, “busy” can mean two different things: an agent turn is running, or MCP servers are still starting up. Without a clear contract, those lifecycles can interfere: startup completion can clear the spinner while a turn is still in progress, or the UI can appear idle while MCP is still booting. This is user-visible confusion during the most important moments (startup and the first turn), so it was worth making the contract explicit and guarding it. ## **Mental model** `ChatWidget` is the UI-side adapter for the `codex_core::protocol` event stream. It receives `EventMsg` events and updates two major UI surfaces: the transcript (history/streaming cells) and the bottom pane (composer + status indicator). The key concept after this change is that the bottom pane’s “task running” indicator is treated as **derived UI-busy state**, not “agent is running”. It is considered active while either: - an agent turn is in progress (`TurnStarted` → completion/abort), or - MCP startup is in progress (`McpStartupUpdate` → `McpStartupComplete`). Those lifecycles are tracked independently, and the bottom-pane indicator is defined as their union. ## **Non-goals** - This does not introduce separate UI indicators for “turn busy” vs “MCP busy”. - This does not change MCP startup behavior, ordering guarantees, or core protocol semantics. - This does not rework unrelated status/header rendering or transcript layout. ## **Tradeoffs** - The “one flag represents multiple lifecycles” approach remains lossy: it preserves correct “busy vs idle” semantics but cannot express *which* kind of busy is happening without further UI changes. - The design keeps complexity low by keeping a single derived boolean, rather than adding a more expressive bottom-pane state machine. That’s chosen because it matches existing UX and minimizes churn while fixing the confusion. ## **Architecture** - `codex-core` owns the actual lifecycles and emits `codex_core::protocol` events. - `ChatWidget` owns the UI interpretation of those lifecycles. It is responsible for keeping the bottom pane’s derived “busy” state consistent with the event stream, and for updating the status header when MCP progress updates arrive. - The bottom pane remains a dumb renderer of the single “task running” flag; it does not learn about MCP or agent turns directly. ## **Observability** - When working: the spinner/Esc hint stays visible during MCP startup and does not disappear mid-turn when `McpStartupComplete` arrives; startup status headers can update without clearing “busy” for an active turn. - When broken: you’ll see the spinner/hint flicker off while output is still streaming, or the UI appears idle while MCP startup status is still changing. ## **Tests** - Adds/strengthens a regression test that asserts MCP startup completion does not clear the “task running” indicator for an active turn (in both `tui` and `tui2` variants). - These tests prove the **contract** (“busy is the union of turn + startup”) at the UI boundary; they do not attempt to validate MCP startup ordering, real-world startup timing, or backend integration behavior. Fixes #7017 Signed-off-by: 2mawi2 <2mawi2@users.noreply.github.com> Co-authored-by: 2mawi2 <2mawi2@users.noreply.github.com> Co-authored-by: Josh McKinney <joshka@openai.com>
This commit is contained in:
@@ -14,7 +14,12 @@
|
||||
//! cache key is designed to change when the active cell mutates in place or when its transcript
|
||||
//! output is time-dependent so the overlay can refresh its cached tail without rebuilding it on
|
||||
//! every draw.
|
||||
|
||||
//!
|
||||
//! The bottom pane exposes a single "task running" indicator that drives the spinner and interrupt
|
||||
//! hints. This module treats that indicator as derived UI-busy state: it is set while an agent turn
|
||||
//! is in progress and while MCP server startup is in progress. Those lifecycles are tracked
|
||||
//! independently (`agent_turn_running` and `mcp_startup_status`) and synchronized via
|
||||
//! `update_task_running_state`.
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::VecDeque;
|
||||
@@ -330,6 +335,12 @@ pub(crate) enum ExternalEditorState {
|
||||
Active,
|
||||
}
|
||||
|
||||
/// Maintains the per-session UI state for the chat screen.
|
||||
///
|
||||
/// This type owns the state derived from a `codex_core::protocol` event stream (history cells,
|
||||
/// active streaming buffers, bottom-pane overlays, and transient status text). It is not
|
||||
/// responsible for running the agent itself; it only reflects progress by updating UI state and by
|
||||
/// sending `Op` requests back to codex-core.
|
||||
pub(crate) struct ChatWidget {
|
||||
app_event_tx: AppEventSender,
|
||||
codex_op_tx: UnboundedSender<Op>,
|
||||
@@ -364,6 +375,16 @@ pub(crate) struct ChatWidget {
|
||||
last_unified_wait: Option<UnifiedExecWaitState>,
|
||||
task_complete_pending: bool,
|
||||
unified_exec_processes: Vec<UnifiedExecProcessSummary>,
|
||||
/// Tracks whether codex-core currently considers an agent turn to be in progress.
|
||||
///
|
||||
/// This is kept separate from `mcp_startup_status` so that MCP startup progress (or completion)
|
||||
/// can update the status header without accidentally clearing the spinner for an active turn.
|
||||
agent_turn_running: bool,
|
||||
/// Tracks per-server MCP startup state while startup is in progress.
|
||||
///
|
||||
/// The map is `Some(_)` from the first `McpStartupUpdate` until `McpStartupComplete`, and the
|
||||
/// bottom pane is treated as "running" while this is populated, even if no agent turn is
|
||||
/// currently executing.
|
||||
mcp_startup_status: Option<HashMap<String, McpStartupStatus>>,
|
||||
// Queue of interruptive UI events deferred during an active write cycle
|
||||
interrupts: InterruptManager,
|
||||
@@ -457,6 +478,14 @@ fn create_initial_user_message(text: String, image_paths: Vec<PathBuf>) -> Optio
|
||||
}
|
||||
|
||||
impl ChatWidget {
|
||||
/// Synchronize the bottom-pane "task running" indicator with the current lifecycles.
|
||||
///
|
||||
/// The bottom pane only has one running flag, but this module treats it as a derived state of
|
||||
/// both the agent turn lifecycle and MCP startup lifecycle.
|
||||
fn update_task_running_state(&mut self) {
|
||||
self.bottom_pane
|
||||
.set_task_running(self.agent_turn_running || self.mcp_startup_status.is_some());
|
||||
}
|
||||
fn flush_answer_stream_with_separator(&mut self) {
|
||||
if let Some(mut controller) = self.stream_controller.take()
|
||||
&& let Some(cell) = controller.finalize()
|
||||
@@ -613,8 +642,9 @@ impl ChatWidget {
|
||||
// Raw reasoning uses the same flow as summarized reasoning
|
||||
|
||||
fn on_task_started(&mut self) {
|
||||
self.agent_turn_running = true;
|
||||
self.bottom_pane.clear_ctrl_c_quit_hint();
|
||||
self.bottom_pane.set_task_running(true);
|
||||
self.update_task_running_state();
|
||||
self.retry_status_header = None;
|
||||
self.bottom_pane.set_interrupt_hint_visible(true);
|
||||
self.set_status_header(String::from("Working"));
|
||||
@@ -628,7 +658,8 @@ impl ChatWidget {
|
||||
self.flush_answer_stream_with_separator();
|
||||
self.flush_wait_cell();
|
||||
// Mark task stopped and request redraw now that all content is in history.
|
||||
self.bottom_pane.set_task_running(false);
|
||||
self.agent_turn_running = false;
|
||||
self.update_task_running_state();
|
||||
self.running_commands.clear();
|
||||
self.suppressed_exec_calls.clear();
|
||||
self.last_unified_wait = None;
|
||||
@@ -755,12 +786,16 @@ impl ChatWidget {
|
||||
self.rate_limit_snapshot = None;
|
||||
}
|
||||
}
|
||||
/// Finalize any active exec as failed and stop/clear running UI state.
|
||||
/// Finalize any active exec as failed and stop/clear agent-turn UI state.
|
||||
///
|
||||
/// This does not clear MCP startup tracking, because MCP startup can overlap with turn cleanup
|
||||
/// and should continue to drive the bottom-pane running indicator while it is in progress.
|
||||
fn finalize_turn(&mut self) {
|
||||
// Ensure any spinner is replaced by a red ✗ and flushed into history.
|
||||
self.finalize_active_cell_as_failed();
|
||||
// Reset running state and clear streaming buffers.
|
||||
self.bottom_pane.set_task_running(false);
|
||||
self.agent_turn_running = false;
|
||||
self.update_task_running_state();
|
||||
self.running_commands.clear();
|
||||
self.suppressed_exec_calls.clear();
|
||||
self.last_unified_wait = None;
|
||||
@@ -789,7 +824,7 @@ impl ChatWidget {
|
||||
}
|
||||
status.insert(ev.server, ev.status);
|
||||
self.mcp_startup_status = Some(status);
|
||||
self.bottom_pane.set_task_running(true);
|
||||
self.update_task_running_state();
|
||||
if let Some(current) = &self.mcp_startup_status {
|
||||
let total = current.len();
|
||||
let mut starting: Vec<_> = current
|
||||
@@ -845,7 +880,7 @@ impl ChatWidget {
|
||||
}
|
||||
|
||||
self.mcp_startup_status = None;
|
||||
self.bottom_pane.set_task_running(false);
|
||||
self.update_task_running_state();
|
||||
self.maybe_send_next_queued_input();
|
||||
self.request_redraw();
|
||||
}
|
||||
@@ -1522,6 +1557,7 @@ impl ChatWidget {
|
||||
last_unified_wait: None,
|
||||
task_complete_pending: false,
|
||||
unified_exec_processes: Vec::new(),
|
||||
agent_turn_running: false,
|
||||
mcp_startup_status: None,
|
||||
interrupts: InterruptManager::new(),
|
||||
reasoning_buffer: String::new(),
|
||||
@@ -1612,6 +1648,7 @@ impl ChatWidget {
|
||||
last_unified_wait: None,
|
||||
task_complete_pending: false,
|
||||
unified_exec_processes: Vec::new(),
|
||||
agent_turn_running: false,
|
||||
mcp_startup_status: None,
|
||||
interrupts: InterruptManager::new(),
|
||||
reasoning_buffer: String::new(),
|
||||
|
||||
Reference in New Issue
Block a user