mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
c0ea566bb5
## 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.
134 lines
4.3 KiB
Rust
134 lines
4.3 KiB
Rust
//! Hook run lifecycle handling for `ChatWidget`.
|
|
//!
|
|
//! This module keeps active hook cells, hook timers, and hook completion output
|
|
//! together.
|
|
|
|
use super::*;
|
|
|
|
impl ChatWidget {
|
|
pub(super) fn on_hook_started(&mut self, run: codex_app_server_protocol::HookRunSummary) {
|
|
self.record_visible_turn_activity();
|
|
self.flush_answer_stream_with_separator();
|
|
self.flush_completed_hook_output();
|
|
match self.active_hook_cell.as_mut() {
|
|
Some(cell) => {
|
|
cell.start_run(run);
|
|
self.bump_active_cell_revision();
|
|
}
|
|
None => {
|
|
self.active_hook_cell = Some(history_cell::new_active_hook_cell(
|
|
run,
|
|
self.config.animations,
|
|
));
|
|
self.bump_active_cell_revision();
|
|
}
|
|
}
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(super) fn on_hook_completed(
|
|
&mut self,
|
|
completed: codex_app_server_protocol::HookRunSummary,
|
|
) {
|
|
let completed_existing_run = self
|
|
.active_hook_cell
|
|
.as_mut()
|
|
.map(|cell| cell.complete_run(completed.clone()))
|
|
.unwrap_or(false);
|
|
if completed_existing_run {
|
|
self.bump_active_cell_revision();
|
|
} else {
|
|
match self.active_hook_cell.as_mut() {
|
|
Some(cell) => {
|
|
cell.add_completed_run(completed);
|
|
self.bump_active_cell_revision();
|
|
}
|
|
None => {
|
|
let cell =
|
|
history_cell::new_completed_hook_cell(completed, self.config.animations);
|
|
if !cell.is_empty() {
|
|
self.active_hook_cell = Some(cell);
|
|
self.bump_active_cell_revision();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self.flush_completed_hook_output();
|
|
self.finish_active_hook_cell_if_idle();
|
|
self.request_redraw();
|
|
}
|
|
|
|
pub(super) fn flush_completed_hook_output(&mut self) {
|
|
let Some(completed_cell) = self
|
|
.active_hook_cell
|
|
.as_mut()
|
|
.and_then(HookCell::take_completed_persistent_runs)
|
|
else {
|
|
return;
|
|
};
|
|
let active_cell_is_empty = self
|
|
.active_hook_cell
|
|
.as_ref()
|
|
.is_some_and(HookCell::is_empty);
|
|
if active_cell_is_empty {
|
|
self.active_hook_cell = None;
|
|
}
|
|
self.bump_active_cell_revision();
|
|
self.transcript.needs_final_message_separator = true;
|
|
self.app_event_tx
|
|
.send(AppEvent::InsertHistoryCell(Box::new(completed_cell)));
|
|
}
|
|
|
|
pub(super) fn finish_active_hook_cell_if_idle(&mut self) {
|
|
let Some(cell) = self.active_hook_cell.as_ref() else {
|
|
return;
|
|
};
|
|
if cell.is_empty() {
|
|
self.active_hook_cell = None;
|
|
self.bump_active_cell_revision();
|
|
return;
|
|
}
|
|
if cell.should_flush()
|
|
&& let Some(cell) = self.active_hook_cell.take()
|
|
{
|
|
self.bump_active_cell_revision();
|
|
self.transcript.needs_final_message_separator = true;
|
|
self.app_event_tx
|
|
.send(AppEvent::InsertHistoryCell(Box::new(cell)));
|
|
}
|
|
}
|
|
|
|
pub(super) fn update_due_hook_visibility(&mut self) {
|
|
let Some(cell) = self.active_hook_cell.as_mut() else {
|
|
return;
|
|
};
|
|
let now = Instant::now();
|
|
if cell.advance_time(now) {
|
|
self.bump_active_cell_revision();
|
|
}
|
|
self.finish_active_hook_cell_if_idle();
|
|
}
|
|
|
|
pub(super) fn schedule_hook_timer_if_needed(&self) {
|
|
if self.config.animations
|
|
&& self
|
|
.active_hook_cell
|
|
.as_ref()
|
|
.is_some_and(HookCell::has_visible_running_run)
|
|
{
|
|
self.frame_requester
|
|
.schedule_frame_in(Duration::from_millis(50));
|
|
}
|
|
|
|
let Some(deadline) = self
|
|
.active_hook_cell
|
|
.as_ref()
|
|
.and_then(HookCell::next_timer_deadline)
|
|
else {
|
|
return;
|
|
};
|
|
let delay = deadline.saturating_duration_since(Instant::now());
|
|
self.frame_requester.schedule_frame_in(delay);
|
|
}
|
|
}
|