Files
codex/codex-rs/tui/src/app/exit_summary.rs
T
Benjamin Carlsson 02e745f3a0 Return to the command center while archiving a task (#61619)
## Why

Archiving the current task waits for the server response before returning to the command center, blocking navigation while the request is pending.

## What changed

- Run the archive request in the background after side chat and voice cleanup, hiding the task while it is pending.
- Preserve drafts and approvals until archiving succeeds. On failure, show an error and make the task visible again.
- Keep archive completions across reconnects and route dynamic tool replies through their original reply handles.
- Show `Archiving…` during side chat cleanup. On exit, wait up to five seconds for pending archives and report failures or unconfirmed outcomes.

## Testing

Add coverage for navigation before the archive response, success and failure handling across event channel replacement, preserved drafts, side chat cleanup failures, and exit warnings.

GitOrigin-RevId: 7b6e35cb11159f5be1d0b9fb2082dbd8c7826a94
2026-11-15 07:28:15 +00:00

170 lines
6.9 KiB
Rust

//! Exit output distinguishes a disconnected client from a stopped embedded server.
//! Persisted sessions offer a direct UUID command and an optional named picker hint.
use super::*;
use crate::RemoteAppServerEndpoint;
use crate::exec_command::escape_command;
use crate::status::remote_connection::sanitized_websocket_url;
/// A persisted thread that can be resumed after the TUI exits.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResumableThread {
pub thread_id: ThreadId,
pub thread_name: Option<String>,
}
/// Reconnection and stop guidance for a task owned by a persistent app server.
#[derive(Debug, Clone)]
pub struct DisconnectInfo {
/// CLI arguments identifying the server, without credential values.
pub command: Vec<String>,
pub stop_hint: String,
}
impl App {
pub(super) async fn finish_pending_archives(
&mut self,
deadline: tokio::time::Instant,
) -> Vec<String> {
let mut warnings = Vec::new();
for (thread_id, outcome) in self.agents_overview.pending_archives.drain() {
match tokio::time::timeout_at(deadline, outcome).await {
Ok(Ok(Ok(()))) => {}
Ok(Ok(Err(error))) => {
warnings.push(format!("Failed to archive thread {thread_id}: {error}"));
}
Ok(Err(_)) | Err(_) => {
warnings.push(format!("Archive of thread {thread_id} was not confirmed."));
}
}
}
warnings.sort();
warnings
}
pub(super) fn exit_info(&self, exit_reason: ExitReason) -> AppExitInfo {
let thread_id = self.chat_widget.thread_id().or(self.primary_thread_id);
let disconnect_info = thread_id.and_then(|_| {
let command = match &self.app_server_target {
AppServerTarget::Embedded | AppServerTarget::Cloud => return None,
AppServerTarget::LocalDaemon { .. } => vec!["codex".to_string()],
AppServerTarget::Remote { endpoint } => {
let address = match endpoint {
RemoteAppServerEndpoint::WebSocket { websocket_url, .. } => {
sanitized_websocket_url(websocket_url)
.and_then(|url| {
let host = url.host()?;
let port = url.port_or_known_default()?;
// The CLI requires an explicit port, including 80/443.
Some(format!("{}://{host}:{port}{}", url.scheme(), url.path()))
})
.unwrap_or_else(|| "<server-address>".to_string())
}
RemoteAppServerEndpoint::UnixSocket { socket_path } => {
format!("unix://{}", socket_path.display())
}
};
vec!["codex".to_string(), "--remote".to_string(), address]
}
};
let stop_hint = self
.keymap
.agents
.primary_hint("stop", &self.keymap.agents.stop)
.map_or_else(
|| "use the configured stop shortcut".to_string(),
|key| format!("press {}", key.display_label()),
);
Some(DisconnectInfo { command, stop_hint })
});
AppExitInfo {
token_usage: self.token_usage(),
thread_id,
resume_hint: resumable_thread(
thread_id,
self.chat_widget.thread_name(),
self.chat_widget.rollout_path().as_deref(),
),
disconnect_info,
archive_warnings: Vec::new(),
update_action: self.pending_update_action,
exit_reason,
}
}
}
impl AppExitInfo {
/// Format the terminal summary for both the CLI and standalone TUI binaries.
pub fn format_exit_messages(self, color_enabled: bool) -> Vec<String> {
let color_command = |command: String| {
if color_enabled {
format!("\u{1b}[36m{command}\u{1b}[39m")
} else {
command
}
};
let mut lines = self.archive_warnings;
if let Some(disconnect) = self.disconnect_info
&& let Some(thread_id) = self.thread_id
{
let turn_interrupted = matches!(self.exit_reason, ExitReason::TurnInterrupted);
let message = match self.exit_reason {
ExitReason::UserRequested => {
"Disconnected from this task. Any running work continues."
}
ExitReason::Fatal(_) => "Disconnected from this task. Work may still be running.",
ExitReason::TurnInterrupted => {
"Disconnected from this task. The current turn was stopped."
}
};
lines.push(message.to_string());
let mut resume_command = disconnect.command.clone();
resume_command.extend(["resume".to_string(), thread_id.to_string()]);
lines.push("To reconnect, run:".to_string());
lines.push(format!(
" {}",
color_command(escape_command(&resume_command)),
));
if !turn_interrupted {
let mut agents_command = disconnect.command;
agents_command.push("agents".to_string());
lines.push(format!(
"Stop the current turn: run {}, select this task, and {}.",
color_command(escape_command(&agents_command)),
disconnect.stop_hint,
));
}
if !self.token_usage.is_zero() {
let usage = self.token_usage.to_string();
lines.push(if turn_interrupted {
usage
} else {
usage.replacen("Token usage:", "Token usage so far:", /*count*/ 1)
});
}
return lines;
}
if !self.token_usage.is_zero() {
lines.push(self.token_usage.to_string());
}
if let Some(thread) = self.resume_hint {
lines.push("To continue this session, run:".to_string());
lines.push(format!(
" {}",
color_command(format!("codex resume {}", thread.thread_id)),
));
if let Some(thread_name) = thread.thread_name.filter(|name| !name.is_empty()) {
lines.push(format!(
"Or run {} and select {}.",
color_command("codex resume".to_string()),
color_command(thread_name),
));
}
} else if let Some(thread_id) = self.thread_id {
lines.push(format!("Session ID: {thread_id}"));
}
lines
}
}