feat(tui): restore output-free cancelled prompts (#25316)

## TL;DR

When you press Esc or Ctrl+C after sending a prompt but before any
output was rendering, it restores the last composer and the message.

## Summary

Cancelling a prompt immediately after submission should behave like
returning to edit that prompt, not like discarding the user's draft.
Today, pressing `Esc` or `Ctrl+C` before Codex responds leaves the
submitted prompt in the transcript and returns an empty composer,
forcing the user to recall or retype it.

When an interrupted turn has not produced substantive visible output,
restore its submitted prompt directly into the composer and roll back
that latest turn. This also covers the first prompt in a fresh thread,
before the TUI has retained a local user-history cell. The restored
draft keeps its text, image attachments, and active collaboration mode
so it can be edited and resubmitted in place.

Restoration is intentionally suppressed once the turn has produced
user-visible activity such as assistant output, tool work, hooks, or
patches. A transient thinking status does not make the prompt
ineligible. Rollback also rebuilds terminal scrollback from the retained
transcript cells so repeated cancellations and terminal resizes do not
duplicate history.

## How to Test

1. Start the TUI with `cargo run -p codex-cli --bin codex`.
2. In a fresh thread, submit the first prompt and press `Esc` before
Codex emits substantive output. Confirm that the prompt returns to the
composer for editing and its submitted transcript row is removed.
3. Repeat with `Ctrl+C`, then repeat after at least one completed turn.
Confirm the same behavior.
4. Submit a prompt, wait for assistant output or tool activity, then
cancel. Confirm that the transcript remains intact and the prompt is not
restored into the composer.
5. Cancel several output-free prompts and resize the terminal between
attempts. Confirm that the startup banner, tip, and transcript history
do not duplicate in scrollback.

Targeted tests:
- `just test -p codex-tui cancelled_turn_edit_restores_prompt`
- `just test -p codex-tui
output_free_interrupted_turn_requests_prompt_restore`
- `just test -p codex-tui
visible_output_prevents_cancelled_turn_prompt_restore`
- `just test -p codex-tui
thinking_status_keeps_cancelled_turn_prompt_restore_eligible`
- `just test -p codex-tui
patch_activity_prevents_cancelled_turn_prompt_restore`

The full `just test -p codex-tui` run completed with `2746` passing
tests and two unrelated existing guardian feature-flag failures. `just
argument-comment-lint` remains blocked locally by the existing Bazel
LLVM `compiler-rt` sanitizer-header glob failure; the touched Rust diff
was manually audited for positional literal comments.
This commit is contained in:
Felipe Coury authored and GitHub committed 2026-06-01 11:49:14 -03:00
1 parent 4eded02f52
commit c0ea566bb5
28 files changed
+329 -46

No files matched your search

+4
View File
@@ -324,8 +324,12 @@ impl App {
return Ok(AppRunControl::Exit(ExitReason::Fatal(message)));
}
AppEvent::CodexOp(op) => {
self.chat_widget.prepare_local_op_submission(&op);
self.submit_active_thread_op(app_server, op).await?;
}
AppEvent::RestoreCancelledTurn(prompt) => {
self.apply_cancelled_turn_edit(prompt);
}
AppEvent::AppendMessageHistoryEntry { thread_id, text } => {
self.append_message_history_entry(thread_id, text);
}
+29
View File
@@ -459,6 +459,35 @@ impl App {
Ok(terminal_width)
}
/// Rebuild scrollback after rollback removes transcript cells.
///
/// Unlike resize reflow, rollback must clear the terminal even when no cells remain. Otherwise
/// the cancelled user prompt stays visible in scrollback despite being removed from the source
/// transcript.
pub(super) fn rebuild_transcript_after_backtrack(&mut self, tui: &mut tui::Tui) -> Result<()> {
let terminal_width = tui.terminal.size()?.width;
let width = self.chat_widget.history_wrap_width(terminal_width);
let reflowed_lines = if self.transcript_cells.is_empty() {
self.reset_history_emission_state();
Vec::new()
} else {
self.render_transcript_lines_for_reflow(width).lines
};
tui.clear_pending_history_lines();
self.clear_terminal_for_resize_replay(tui)?;
self.deferred_history_lines.clear();
if !reflowed_lines.is_empty() {
tui.insert_history_hyperlink_lines_with_wrap_policy(
reflowed_lines,
self.history_line_wrap_policy(),
);
}
Ok(())
}
/// Render transcript cells for the current resize rebuild.
///
/// Rendering walks backward from the transcript tail so row-capped sessions avoid formatting the
@@ -0,0 +1,5 @@
---
source: tui/src/app/tests.rs
expression: app.chat_widget.composer_text_with_pending()
---
edit me
+55
View File
@@ -4637,6 +4637,61 @@ async fn backtrack_remote_image_only_selection_clears_existing_composer_draft()
assert_eq!(rollback_turns, Some(1));
}
#[tokio::test]
async fn cancelled_turn_edit_restores_prompt_and_rolls_back_latest_turn() {
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
app.transcript_cells = vec![Arc::new(UserHistoryCell {
message: "original".to_string(),
text_elements: Vec::new(),
local_image_paths: Vec::new(),
remote_image_urls: Vec::new(),
}) as Arc<dyn HistoryCell>];
let prompt = crate::chatwidget::UserMessage {
text: "edit me".to_string(),
local_images: Vec::new(),
remote_image_urls: vec!["https://example.com/edit.png".to_string()],
text_elements: Vec::new(),
mention_bindings: Vec::new(),
};
app.apply_cancelled_turn_edit(prompt);
assert_eq!(app.chat_widget.composer_text_with_pending(), "edit me");
assert_snapshot!(
"cancelled_turn_edit_restores_composer",
app.chat_widget.composer_text_with_pending()
);
assert_eq!(
app.chat_widget.remote_image_urls(),
vec!["https://example.com/edit.png".to_string()]
);
assert_matches!(op_rx.try_recv(), Ok(Op::ThreadRollback { num_turns: 1 }));
}
#[tokio::test]
async fn first_cancelled_turn_edit_restores_prompt_without_local_history() {
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
let prompt = crate::chatwidget::UserMessage {
text: "edit first prompt".to_string(),
local_images: Vec::new(),
remote_image_urls: vec!["https://example.com/edit.png".to_string()],
text_elements: Vec::new(),
mention_bindings: Vec::new(),
};
app.apply_cancelled_turn_edit(prompt);
assert_eq!(
app.chat_widget.composer_text_with_pending(),
"edit first prompt"
);
assert_eq!(
app.chat_widget.remote_image_urls(),
vec!["https://example.com/edit.png".to_string()]
);
assert_matches!(op_rx.try_recv(), Ok(Op::ThreadRollback { num_turns: 1 }));
}
#[tokio::test]
async fn backtrack_resubmit_preserves_data_image_urls_in_user_turn() {
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
+1 -1
View File
@@ -497,7 +497,7 @@ impl App {
op: &AppCommand,
) -> Result<bool> {
match op {
AppCommand::Interrupt => {
AppCommand::Interrupt { .. } => {
if let Some(turn_id) = self.active_turn_id_for_thread(thread_id).await {
app_server.turn_interrupt(thread_id, turn_id).await?;
} else {