//! Command execution lifecycle handlers for `ChatWidget`. //! //! This module owns command start/output/completion rendering, including active //! exec-cell grouping and unified exec wait state. use super::*; use crate::exec_cell::CommandOutput; use crate::exec_cell::plain_output_text; impl ChatWidget { pub(super) fn flush_unified_exec_wait_streak(&mut self) { let Some(wait) = self.unified_exec_wait_streak.take() else { return; }; let cell = history_cell::new_unified_exec_interaction(wait.command_display, String::new()); if let Err(cell) = self.absorb_activity_detail(Box::new(cell)) { self.app_event_tx.send(AppEvent::InsertHistoryCell(cell)); } self.restore_reasoning_status_header(); } pub(super) fn on_command_execution_started( &mut self, item: ThreadItemPayload, render_source: ThreadItemRenderSource, ) { let ThreadItemPayload::CommandExecution { id, command, process_id, source, command_actions, .. } = &item else { return; }; let (_command, parsed_cmd) = command_execution_command_and_parsed(command, command_actions); self.flush_answer_stream_with_separator(); if is_unified_exec_source(*source) { if *source == ExecCommandSource::UnifiedExecStartup && !render_source.is_replay() { self.track_unified_exec_process_begin(id, process_id.as_deref(), command); } if !self.bottom_pane.is_task_running() { return; } // Unified exec may be parsed as Unknown; keep the working indicator visible regardless. self.bottom_pane.ensure_status_indicator(); if !is_standard_tool_call(&parsed_cmd) { return; } } self.defer_or_handle( item, InterruptManager::push_item_started, Self::handle_command_execution_started_now, ); } pub(super) fn on_exec_command_output_delta(&mut self, call_id: &str, delta: &str) { self.track_unified_exec_output_chunk(call_id, delta); if !self.bottom_pane.is_task_running() { return; } let Some(cell) = self .transcript .active_cell .as_mut() .and_then(|c| c.as_any_mut().downcast_mut::()) else { return; }; if cell.append_output(call_id, delta) { self.bump_active_cell_revision(); self.request_redraw(); } } pub(super) fn on_terminal_interaction(&mut self, process_id: String, stdin: String) { if !self.bottom_pane.is_task_running() { return; } let command_display = self .unified_exec_processes .iter() .find(|process| process.key == process_id) .map(|process| process.command_display.clone()); if stdin.is_empty() && command_display.is_none() { return; } self.flush_answer_stream_with_separator(); if stdin.is_empty() { // Empty stdin means we are polling for background output. // Surface this in the status indicator (single "waiting" surface) instead of // the transcript. Keep the header short so the interrupt hint remains visible. self.bottom_pane.ensure_status_indicator(); self.bottom_pane .set_interrupt_hint_visible(/*visible*/ true); self.status_state.terminal_title_status_kind = TerminalTitleStatusKind::WaitingForBackgroundTerminal; self.set_status( "Waiting for background terminal".to_string(), command_display.clone(), StatusDetailsCapitalization::Preserve, /*details_max_lines*/ 1, ); match &mut self.unified_exec_wait_streak { Some(wait) if wait.process_id == process_id => { wait.update_command_display(command_display); } Some(_) => { self.flush_unified_exec_wait_streak(); self.unified_exec_wait_streak = Some(UnifiedExecWaitStreak::new(process_id, command_display)); } None => { self.unified_exec_wait_streak = Some(UnifiedExecWaitStreak::new(process_id, command_display)); } } self.request_redraw(); } else { if self .unified_exec_wait_streak .as_ref() .is_some_and(|wait| wait.process_id == process_id) { self.flush_unified_exec_wait_streak(); } self.add_to_history(history_cell::new_unified_exec_interaction( command_display, stdin, )); } } pub(super) fn on_command_execution_completed(&mut self, item: ThreadItemPayload) { let ThreadItemPayload::CommandExecution { id, process_id, source, .. } = &item else { return; }; if is_unified_exec_source(*source) { if let Some(process_id) = process_id.as_deref() && self .unified_exec_wait_streak .as_ref() .is_some_and(|wait| wait.process_id == process_id) { self.flush_unified_exec_wait_streak(); } self.track_unified_exec_process_end(id, process_id.as_deref()); if !self.bottom_pane.is_task_running() { return; } } self.defer_or_handle( item, InterruptManager::push_item_completed, Self::handle_command_execution_completed_now, ); } pub(super) fn track_unified_exec_process_begin( &mut self, call_id: &str, process_id: Option<&str>, command: &str, ) { let key = process_id.unwrap_or(call_id).to_string(); let command = split_command_string(command); let command_display = strip_bash_lc_and_escape(&command); if let Some(existing) = self .unified_exec_processes .iter_mut() .find(|process| process.key == key) { existing.call_id = call_id.to_string(); existing.command_display = command_display; existing.recent_chunks.clear(); existing.pending = Default::default(); } else { self.unified_exec_processes.push(UnifiedExecProcessSummary { key, call_id: call_id.to_string(), command_display, recent_chunks: Vec::new(), pending: Default::default(), }); } self.sync_unified_exec_footer(); } pub(super) fn clear_background_terminals(&mut self) { self.pending_terminal_restore = None; self.unified_exec_processes.clear(); self.sync_unified_exec_footer(); } pub(crate) fn begin_background_terminal_restore(&mut self) -> uuid::Uuid { let request_id = uuid::Uuid::new_v4(); self.pending_terminal_restore = Some((request_id, HashSet::new())); request_id } pub(crate) fn restore_background_terminals( &mut self, request_id: uuid::Uuid, terminals: Vec, ) { let Some((_, exited)) = self .pending_terminal_restore .take_if(|(id, _)| *id == request_id) else { return; }; for terminal in terminals { if !exited.contains(&terminal.item_id) && !self .unified_exec_processes .iter() .any(|process| process.key == terminal.process_id) { self.unified_exec_processes.push(UnifiedExecProcessSummary { key: terminal.process_id, call_id: terminal.item_id, command_display: terminal.command, recent_chunks: Vec::new(), pending: Default::default(), }); } } self.sync_unified_exec_footer(); } pub(super) fn track_unified_exec_process_end( &mut self, call_id: &str, process_id: Option<&str>, ) { if let Some((_, exited)) = &mut self.pending_terminal_restore { exited.insert(call_id.to_owned()); } let key = process_id.unwrap_or(call_id); let before = self.unified_exec_processes.len(); self.unified_exec_processes .retain(|process| process.key != key); if self.unified_exec_processes.len() != before { self.sync_unified_exec_footer(); } } pub(super) fn sync_unified_exec_footer(&mut self) { let processes = self .unified_exec_processes .iter() .map(|process| process.command_display.clone()) .collect(); self.bottom_pane.set_unified_exec_processes(processes); } /// Record recent stdout/stderr lines for the unified exec footer. fn track_unified_exec_output_chunk(&mut self, call_id: &str, chunk: &str) { let Some(process) = self .unified_exec_processes .iter_mut() .find(|process| process.call_id == call_id) else { return; }; let limit = UnifiedExecProcessSummary::MAX_RECENT_CHUNKS; let insert_at = process.recent_chunks.len(); for line in process .pending .push_lines(chunk) .rev() .map(plain_output_text) .filter(|line| !line.is_empty()) .take(limit) { process.recent_chunks.insert(insert_at, line); } let excess = process.recent_chunks.len().saturating_sub(limit); process.recent_chunks.drain(..excess); } pub(crate) fn handle_command_execution_started_now(&mut self, item: ThreadItemPayload) { let ThreadItemPayload::CommandExecution { id, command, source, command_actions, .. } = item else { return; }; let (command, parsed_cmd) = command_execution_command_and_parsed(&command, &command_actions); // Ensure the status indicator is visible while the command runs. self.bottom_pane.ensure_status_indicator(); let parsed_cmd = self.annotate_skill_reads_in_parsed_cmd(parsed_cmd); self.running_commands.insert( id.clone(), RunningCommand { command: command.clone(), parsed_cmd: parsed_cmd.clone(), source, }, ); let is_wait_interaction = matches!(source, ExecCommandSource::UnifiedExecInteraction); let command_display = command.join(" "); let should_suppress_unified_wait = is_wait_interaction && self .last_unified_wait .as_ref() .is_some_and(|wait| wait.is_duplicate(&command_display)); if is_wait_interaction { self.last_unified_wait = Some(UnifiedExecWaitState::new(command_display)); } else { self.last_unified_wait = None; } if should_suppress_unified_wait { self.suppressed_exec_calls.insert(id); return; } if let Some(cell) = self .transcript .active_cell .as_mut() .and_then(|c| c.as_any_mut().downcast_mut::()) && cell.add_call( id.clone(), command.clone(), parsed_cmd.clone(), source, /*interaction_input*/ None, ) { self.bump_active_cell_revision(); } else { self.flush_active_cell(); self.transcript.active_cell = Some(Box::new(new_active_exec_command( id, command, parsed_cmd, source, /*interaction_input*/ None, self.local_settings.tui.animations && self.local_settings.tui.effects.progress, ))); self.bump_active_cell_revision(); } self.request_redraw(); } /// Finalizes an exec call while preserving the active exec cell grouping contract. /// /// Exec begin/end events usually pair through `running_commands`, but unified exec can emit an /// end event for a call that was never materialized as the current active `ExecCell` (for /// example, when another exploring group is still active). In that case we render the end as a /// standalone history entry instead of replacing or flushing the unrelated active exploring /// cell. If this method treated every unknown end as "complete the active cell", the UI could /// merge unrelated commands and hide still-running exploring work. pub(crate) fn handle_command_execution_completed_now(&mut self, item: ThreadItemPayload) { enum ExecEndTarget { // Normal case: the active exec cell already tracks this call id. ActiveTracked, // We have an active exec group, but it does not contain this call id. Render the end // as a standalone finalized history cell so the active group remains intact. OrphanHistoryWhileActiveExec, // No active exec cell can safely own this end; build a new cell from the end payload. NewCell, } let ThreadItemPayload::CommandExecution { id, command, process_id: _, source, status, command_actions, aggregated_output, exit_code, duration_ms, .. } = item else { return; }; let event_command = split_command_string(&command); let event_parsed = command_actions .into_iter() .map(codex_app_server_protocol::CommandAction::into_core) .collect(); let duration = Duration::from_millis(duration_ms.unwrap_or_default().max(0) as u64); let running = self.running_commands.remove(&id); if self.suppressed_exec_calls.remove(&id) { return; } let (command, parsed, source) = match running { Some(rc) => (rc.command, rc.parsed_cmd, rc.source), None => (event_command, event_parsed, source), }; let parsed = self.annotate_skill_reads_in_parsed_cmd(parsed); let is_user_shell = source == ExecCommandSource::UserShell; // Completion-only replay has no begin event to join adjacent exploration. Extend only // a finished, compatible group; an unrelated running group must retain orphan routing. if let Some(cell) = self .transcript .active_cell .as_mut() .and_then(|cell| cell.as_any_mut().downcast_mut::()) && !cell.is_active() && !cell.should_flush() && !cell.iter_calls().any(|call| call.call_id == id) { cell.add_call( id.clone(), command.clone(), parsed.clone(), source, /*interaction_input*/ None, ); } let end_target = match self.transcript.active_cell.as_ref() { Some(cell) => match cell.as_any().downcast_ref::() { Some(exec_cell) if exec_cell.iter_calls().any(|call| call.call_id == id) => { ExecEndTarget::ActiveTracked } Some(exec_cell) if exec_cell.is_active() => { ExecEndTarget::OrphanHistoryWhileActiveExec } None if cell.as_any().is::() || cell .as_any() .downcast_ref::() .is_some_and(history_cell::ComputerActivityCell::is_active) => { ExecEndTarget::OrphanHistoryWhileActiveExec } Some(_) | None => ExecEndTarget::NewCell, }, None => ExecEndTarget::NewCell, }; let output = CommandOutput::from_execution(status, exit_code, source, aggregated_output); match end_target { ExecEndTarget::ActiveTracked => { if let Some(cell) = self .transcript .active_cell .as_mut() .and_then(|c| c.as_any_mut().downcast_mut::()) { let completed = cell.complete_call(&id, output, duration); debug_assert!(completed, "active exec cell should contain {id}"); if cell.should_flush() { self.flush_active_cell(); } else { self.bump_active_cell_revision(); self.request_redraw(); } } } ExecEndTarget::OrphanHistoryWhileActiveExec => { let mut orphan = new_active_exec_command( id.clone(), command, parsed, source, /*interaction_input*/ None, self.local_settings.tui.animations && self.local_settings.tui.effects.progress, ); let completed = orphan.complete_call(&id, output, duration); debug_assert!(completed, "new orphan exec cell should contain {id}"); self.app_event_tx .send(AppEvent::InsertHistoryCell(Box::new(orphan))); self.request_redraw(); } ExecEndTarget::NewCell => { let mut cell = new_active_exec_command( id.clone(), command, parsed, source, /*interaction_input*/ None, self.local_settings.tui.animations && self.local_settings.tui.effects.progress, ); let completed = cell.complete_call(&id, output, duration); debug_assert!(completed, "new exec cell should contain {id}"); if let Some(active) = self .transcript .active_cell .as_mut() .and_then(|cell| cell.as_any_mut().downcast_mut::()) && !active.is_active() && active.is_exploring_cell() && cell.is_exploring_cell() { // Replayed commands have completion events without matching starts. active.group.calls.extend(cell.group.calls); self.bump_active_cell_revision(); self.request_redraw(); } else { self.flush_active_cell(); if cell.should_flush() { self.add_to_history(cell); } else { self.transcript.active_cell = Some(Box::new(cell)); self.bump_active_cell_revision(); self.request_redraw(); } } } } if is_user_shell { self.maybe_send_next_queued_input(); } } }