mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Prevent accidental TUI interrupts after a steer is accepted (#63784)
## Why A pending steering message offers a shortcut to interrupt the running turn and send the message immediately. If Codex accepts the message before the terminal redraws, pressing the shortcut while that hint is still visible can interrupt the turn even though the message has already been accepted. ## What changed Track whether the interrupt-and-send hint appeared in the last terminal frame. If Codex has since accepted the last pending steer for the running turn, ignore one interrupt press and its key repeats and briefly show “Message received. Ignored one interrupt.” A fresh press can still interrupt the turn. If the hint was never drawn or a redraw has removed it, the first press interrupts as usual. ## Testing Add regression coverage in both TUI rendering modes for an interrupt before and after redraw, a hint that was never displayed, held-key repeats, and an image submission in progress. GitOrigin-RevId: 8be3bd51c623af44c108987fe0c2148eeb4515c7
This commit is contained in:
@@ -1615,13 +1615,14 @@ impl App {
|
||||
self.maybe_run_resize_reflow(tui, screen_size)?;
|
||||
}
|
||||
let hud = self.workspace_hud(tui, screen_size.into());
|
||||
self.chat_widget.begin_steer_hint_draw();
|
||||
let mut rendered_area = Rect::default();
|
||||
self.with_chat_widget_frame(screen_size.width, |desired_height, chat_widget| {
|
||||
let desired_height = if dashboard_visible {
|
||||
screen_size.height
|
||||
} else {
|
||||
desired_height
|
||||
};
|
||||
let mut rendered_area = Rect::default();
|
||||
tui.draw_with_resize_reflow(desired_height, screen_size, hud, |frame| {
|
||||
let area = frame.area();
|
||||
rendered_area = area;
|
||||
@@ -1634,9 +1635,10 @@ impl App {
|
||||
frame.set_cursor_style(chat_widget.cursor_style(area));
|
||||
frame.set_cursor_position((x, y));
|
||||
}
|
||||
})?;
|
||||
Ok(rendered_area)
|
||||
})
|
||||
})
|
||||
})?;
|
||||
self.chat_widget.finish_steer_hint_draw();
|
||||
Ok(rendered_area)
|
||||
}
|
||||
|
||||
fn with_chat_widget_frame<T>(
|
||||
|
||||
@@ -364,6 +364,7 @@ impl App {
|
||||
}
|
||||
prepared.floating_shortcuts = hud.is_some();
|
||||
let mut painted = PaintedConversation::default();
|
||||
self.chat_widget.begin_steer_hint_draw();
|
||||
tui.draw_with_shortcut_hud(screen_size.height, hud, |frame| {
|
||||
if let Some(backdrop) = backdrop {
|
||||
let viewport = frame.area();
|
||||
@@ -658,6 +659,7 @@ impl App {
|
||||
}
|
||||
self.paint_centered_dialog(frame);
|
||||
})?;
|
||||
self.chat_widget.finish_steer_hint_draw();
|
||||
// A shortcut HUD is painted after the conversation and can move over a stationary pointer.
|
||||
if hover_mouse != self.hover_pointer(tui, tui.link_hover.mouse) {
|
||||
frames.schedule_frame();
|
||||
@@ -1278,6 +1280,8 @@ impl App {
|
||||
// Keys owned by the transcript and non-text keys still need fresh geometry immediately.
|
||||
if let TuiEvent::Key(key) = event
|
||||
&& key.kind != KeyEventKind::Release
|
||||
// Route this key against the hint it arrived on, before refreshing geometry.
|
||||
&& !self.chat_widget.interrupts_acknowledged_steer(*key)
|
||||
&& (!self.chat_widget.is_in_paste_burst()
|
||||
|| self.transcript_view.is_search_editing()
|
||||
|| self.transcript_view.is_activity_focused()
|
||||
|
||||
@@ -530,3 +530,109 @@ async fn unsupported_legacy_permissions_are_shown_without_exiting() -> Result<()
|
||||
app_server.shutdown().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn interrupt_after_steer_commit_follows_the_displayed_preview() -> Result<()> {
|
||||
for owned in [false, true] {
|
||||
for (show_preview, redraw, prepare_image) in [
|
||||
(true, false, false),
|
||||
(true, false, true),
|
||||
(true, true, false),
|
||||
(false, false, false),
|
||||
] {
|
||||
let (mut app, mut events, mut ops) = make_test_app_with_channels().await;
|
||||
let id = ThreadId::new();
|
||||
let session = test_thread_session(id, app.config.cwd.to_path_buf());
|
||||
app.chat_widget.handle_thread_session_quiet(session);
|
||||
app.chat_widget.handle_server_notification(
|
||||
turn_started_notification(id, "turn"),
|
||||
/*replay_kind*/ None,
|
||||
);
|
||||
let mut server =
|
||||
Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
tui.set_owned_screen(owned)?;
|
||||
let size = ratatui::layout::Size::new(/*width*/ 80, /*height*/ 24);
|
||||
tui.terminal.resize(size)?;
|
||||
|
||||
app.chat_widget.apply_external_edit("Focus on setup".into());
|
||||
app.chat_widget.handle_key_event(KeyCode::Enter.into());
|
||||
let (client_id, content) = std::iter::from_fn(|| ops.try_recv().ok())
|
||||
.find_map(|op| match op {
|
||||
Op::UserTurn {
|
||||
client_user_message_id,
|
||||
items,
|
||||
..
|
||||
} => Some((client_user_message_id, items)),
|
||||
_ => None,
|
||||
})
|
||||
.expect("submitted steer");
|
||||
if prepare_image {
|
||||
app.chat_widget.snapshot_local_images = true;
|
||||
let path = app.config.codex_home.join("image.png");
|
||||
image::RgbImage::new(/*width*/ 2, /*height*/ 2).save(&path)?;
|
||||
app.chat_widget
|
||||
.attach_image(path.into(), ClipboardPasteContext::Composer);
|
||||
app.chat_widget.handle_key_event(KeyCode::Enter.into());
|
||||
}
|
||||
if show_preview {
|
||||
app.render_chat_widget_frame(&mut tui, size)?;
|
||||
}
|
||||
app.chat_widget.handle_server_notification(
|
||||
ServerNotification::ItemCompleted(
|
||||
codex_app_server_protocol::ItemCompletedNotification::new(
|
||||
id.to_string(),
|
||||
"turn".into(),
|
||||
ThreadItemPayload::UserMessage {
|
||||
id: "user".into(),
|
||||
client_id: Some(client_id),
|
||||
content,
|
||||
},
|
||||
/*started_at_ms*/ None,
|
||||
/*completed_at_ms*/ 0,
|
||||
),
|
||||
),
|
||||
/*replay_kind*/ None,
|
||||
);
|
||||
if redraw {
|
||||
app.render_chat_widget_frame(&mut tui, size)?;
|
||||
}
|
||||
let esc = KeyEvent::from(KeyCode::Esc);
|
||||
let mut interrupted = || {
|
||||
std::iter::from_fn(|| events.try_recv().ok())
|
||||
.chain(std::iter::from_fn(|| ops.try_recv().ok()).map(AppEvent::CodexOp))
|
||||
.any(|event| matches!(event, AppEvent::CodexOp(Op::Interrupt)))
|
||||
};
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(esc))
|
||||
.await?;
|
||||
if show_preview && !redraw {
|
||||
let repeat = KeyEvent {
|
||||
kind: crossterm::event::KeyEventKind::Repeat,
|
||||
..esc
|
||||
};
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(repeat))
|
||||
.await?;
|
||||
assert!(!interrupted());
|
||||
assert!(app.chat_widget.queued_user_message_texts().is_empty());
|
||||
if !owned {
|
||||
let pane = crate::chatwidget::tests::helpers::render_bottom_popup(
|
||||
&app.chat_widget,
|
||||
/*width*/ 80,
|
||||
);
|
||||
insta::allow_duplicates! {
|
||||
insta::assert_snapshot!(pane.lines().last().unwrap().trim(), @"Message received. Ignored one interrupt.");
|
||||
}
|
||||
}
|
||||
app.handle_tui_event(&mut tui, &mut server, TuiEvent::Key(esc))
|
||||
.await?;
|
||||
}
|
||||
assert!(interrupted());
|
||||
if prepare_image {
|
||||
assert_eq!(app.chat_widget.queued_user_message_texts(), ["[Image #1]"]);
|
||||
}
|
||||
server.shutdown().await?;
|
||||
tui.set_owned_screen(/*owned*/ false)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1890,6 +1890,10 @@ impl BottomPane {
|
||||
self.is_task_running
|
||||
}
|
||||
|
||||
pub(crate) fn take_painted_steer_hint(&self) -> bool {
|
||||
self.pending_input_preview.painted_steer_hint.take()
|
||||
}
|
||||
|
||||
pub(crate) fn should_interrupt_running_task(&self, key_event: KeyEvent) -> bool {
|
||||
let is_agent_command = self
|
||||
.composer_text()
|
||||
|
||||
@@ -21,6 +21,7 @@ use crate::wrapping::adaptive_wrap_lines;
|
||||
/// queued user inputs to pop back into the composer. The displayed binding is
|
||||
/// configurable via [`set_edit_binding`](Self::set_edit_binding).
|
||||
pub(crate) struct PendingInputPreview {
|
||||
pub(super) painted_steer_hint: std::cell::Cell<bool>,
|
||||
pub pending_steers: Vec<String>,
|
||||
pub rejected_steers: Vec<String>,
|
||||
pub queued_messages: Vec<String>,
|
||||
@@ -40,6 +41,7 @@ const PREVIEW_LINE_LIMIT: usize = 3;
|
||||
impl PendingInputPreview {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
painted_steer_hint: Default::default(),
|
||||
pending_steers: Vec::new(),
|
||||
rejected_steers: Vec::new(),
|
||||
queued_messages: Vec::new(),
|
||||
@@ -82,7 +84,7 @@ impl PendingInputPreview {
|
||||
));
|
||||
}
|
||||
|
||||
fn as_renderable(&self, width: u16, questions: QuestionPresence) -> Box<dyn Renderable> {
|
||||
fn layout(&self, width: u16, questions: QuestionPresence) -> (HyperlinkRows, Option<usize>) {
|
||||
let has_questions = matches!(questions, QuestionPresence::Present);
|
||||
if (self.pending_steers.is_empty()
|
||||
&& self.rejected_steers.is_empty()
|
||||
@@ -90,10 +92,11 @@ impl PendingInputPreview {
|
||||
&& !has_questions)
|
||||
|| width < 4
|
||||
{
|
||||
return Box::new(());
|
||||
return (HyperlinkRows::from(Vec::new()), None);
|
||||
}
|
||||
|
||||
let mut lines = vec![];
|
||||
let mut hint_height = None;
|
||||
|
||||
if !self.pending_steers.is_empty() {
|
||||
let mut header = vec!["Messages to be submitted after next tool call".into()];
|
||||
@@ -104,6 +107,8 @@ impl PendingInputPreview {
|
||||
}
|
||||
Self::push_section_header(&mut lines, width, Line::from(header));
|
||||
|
||||
hint_height = self.interrupt_binding.map(|_| lines.len());
|
||||
|
||||
for steer in &self.pending_steers {
|
||||
let wrapped = wrap_preview_lines(
|
||||
steer
|
||||
@@ -176,7 +181,14 @@ impl PendingInputPreview {
|
||||
lines.push(HyperlinkLine::new(hint));
|
||||
}
|
||||
|
||||
Box::new(HyperlinkRows::from(lines))
|
||||
(HyperlinkRows::from(lines), hint_height)
|
||||
}
|
||||
|
||||
fn render_preview(&self, area: Rect, buf: &mut Buffer, questions: QuestionPresence) {
|
||||
let (rows, hint_height) = self.layout(area.width, questions);
|
||||
rows.render(area, buf);
|
||||
self.painted_steer_hint
|
||||
.set(hint_height.is_some_and(|height| height <= usize::from(area.height)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,12 +232,12 @@ impl Renderable for PendingInputPreview {
|
||||
return;
|
||||
}
|
||||
|
||||
self.as_renderable(area.width, QuestionPresence::Absent)
|
||||
.render(area, buf);
|
||||
self.render_preview(area, buf, QuestionPresence::Absent);
|
||||
}
|
||||
|
||||
fn desired_height(&self, width: u16) -> u16 {
|
||||
self.as_renderable(width, QuestionPresence::Absent)
|
||||
self.layout(width, QuestionPresence::Absent)
|
||||
.0
|
||||
.desired_height(width)
|
||||
}
|
||||
}
|
||||
@@ -235,14 +247,13 @@ pub(super) struct PendingInputPreviewContent<'a>(pub(super) &'a PendingInputPrev
|
||||
|
||||
impl Renderable for PendingInputPreviewContent<'_> {
|
||||
fn render(&self, area: Rect, buf: &mut Buffer) {
|
||||
self.0
|
||||
.as_renderable(area.width, QuestionPresence::Present)
|
||||
.render(area, buf);
|
||||
self.0.render_preview(area, buf, QuestionPresence::Present);
|
||||
}
|
||||
|
||||
fn desired_height(&self, width: u16) -> u16 {
|
||||
self.0
|
||||
.as_renderable(width, QuestionPresence::Present)
|
||||
.layout(width, QuestionPresence::Present)
|
||||
.0
|
||||
.desired_height(width)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -421,6 +421,7 @@ mod usage_history;
|
||||
use self::transcript::TranscriptState;
|
||||
mod turn_lifecycle;
|
||||
mod turn_runtime;
|
||||
use self::turn_lifecycle::SteerHint;
|
||||
use self::turn_lifecycle::TurnLifecycleState;
|
||||
mod usage;
|
||||
mod user_messages;
|
||||
@@ -1416,6 +1417,14 @@ impl ChatWidget {
|
||||
}),
|
||||
};
|
||||
if let Some(pending) = matching.and_then(|index| steers.remove(index)) {
|
||||
if steers.is_empty()
|
||||
&& self.turn_lifecycle.steer_hint == SteerHint::Pending
|
||||
&& self.turn_lifecycle.agent_turn_running
|
||||
&& self.last_turn_id() == Some(turn_id)
|
||||
&& !self.review.is_review_mode
|
||||
{
|
||||
self.turn_lifecycle.steer_hint = SteerHint::Acknowledged;
|
||||
}
|
||||
self.refresh_pending_input_preview();
|
||||
if !already_rendered {
|
||||
if !pending.optimistically_rendered {
|
||||
|
||||
@@ -178,6 +178,33 @@ impl ChatWidget {
|
||||
.then_some((cycle, environment))
|
||||
}
|
||||
|
||||
pub(crate) fn interrupts_acknowledged_steer(&self, key: KeyEvent) -> bool {
|
||||
self.turn_lifecycle.steer_hint == SteerHint::Acknowledged
|
||||
&& self.bottom_pane.should_interrupt_running_task(key)
|
||||
}
|
||||
|
||||
fn ignore_late_interrupt(
|
||||
&mut self,
|
||||
key_event: KeyEvent,
|
||||
activation_key: Option<KeyBinding>,
|
||||
) -> bool {
|
||||
if !self.interrupts_acknowledged_steer(key_event) {
|
||||
return false;
|
||||
}
|
||||
self.turn_lifecycle.steer_hint = SteerHint::Hidden;
|
||||
let key = activation_key.unwrap_or_else(|| KeyBinding::from_event(key_event));
|
||||
self.suppress_current_view_key_until_release(
|
||||
key,
|
||||
Instant::now(),
|
||||
ActivationKeySuppression::ReportedRepeats,
|
||||
);
|
||||
self.bottom_pane.show_footer_flash(
|
||||
"Message received. Ignored one interrupt.".dim().into(),
|
||||
Duration::from_secs(3),
|
||||
);
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn handle_key_event_with_activation_key(
|
||||
&mut self,
|
||||
key_event: KeyEvent,
|
||||
@@ -256,6 +283,9 @@ impl ChatWidget {
|
||||
&& self.pending_image_submission.is_some()
|
||||
{
|
||||
if self.is_cancellable_work_active() {
|
||||
if self.ignore_late_interrupt(key_event, activation_key) {
|
||||
return KeyEventAction::None;
|
||||
}
|
||||
self.requeue_image_submission();
|
||||
self.input_queue.recovered_queue = true;
|
||||
if self.submit_op(AppCommand::interrupt()) {
|
||||
@@ -381,6 +411,9 @@ impl ChatWidget {
|
||||
}
|
||||
_ => {
|
||||
let had_modal_or_popup = !self.bottom_pane.no_modal_or_popup_active();
|
||||
if self.ignore_late_interrupt(key_event, activation_key) {
|
||||
return KeyEventAction::None;
|
||||
}
|
||||
let should_pause_active_goal =
|
||||
self.bottom_pane.should_interrupt_running_task(key_event);
|
||||
let input_result = self
|
||||
|
||||
@@ -446,6 +446,20 @@ impl ChatWidget {
|
||||
(!lines.is_empty()).then_some(lines)
|
||||
}
|
||||
|
||||
/// Discard offscreen renders. Only a successful terminal draw changes key routing.
|
||||
pub(crate) fn begin_steer_hint_draw(&self) {
|
||||
self.bottom_pane.take_painted_steer_hint();
|
||||
}
|
||||
|
||||
pub(crate) fn finish_steer_hint_draw(&mut self) {
|
||||
self.turn_lifecycle.steer_hint =
|
||||
if self.bottom_pane.take_painted_steer_hint() && self.no_modal_or_popup_active() {
|
||||
SteerHint::Pending
|
||||
} else {
|
||||
SteerHint::Hidden
|
||||
};
|
||||
}
|
||||
|
||||
pub(crate) fn note_rendered_width(&self, width: u16) {
|
||||
self.last_rendered_width.set(Some(width));
|
||||
}
|
||||
|
||||
@@ -6,12 +6,21 @@ use std::time::Instant;
|
||||
|
||||
use codex_utils_sleep_inhibitor::SleepInhibitor;
|
||||
|
||||
/// State of the interrupt-and-send hint in the last terminal frame.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub(super) enum SteerHint {
|
||||
Hidden,
|
||||
Pending,
|
||||
Acknowledged,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(super) struct TurnLifecycleState {
|
||||
pub(super) sleep_inhibitor: SleepInhibitor,
|
||||
/// Tracks whether codex-core currently considers an agent turn to be in progress.
|
||||
pub(super) agent_turn_running: bool,
|
||||
pub(super) last_turn_id: Option<String>,
|
||||
pub(super) steer_hint: SteerHint,
|
||||
/// A live or restored turn makes the parent available for side-chat creation.
|
||||
pub(super) has_turn_history: bool,
|
||||
/// Cloud confirmed admission to a turn whose live start we have yet to process.
|
||||
@@ -29,6 +38,7 @@ impl TurnLifecycleState {
|
||||
sleep_inhibitor: SleepInhibitor::new(prevent_idle_sleep),
|
||||
agent_turn_running: false,
|
||||
last_turn_id: None,
|
||||
steer_hint: SteerHint::Hidden,
|
||||
has_turn_history: false,
|
||||
admitted_turn_id: None,
|
||||
budget_limited_turn_ids: HashSet::new(),
|
||||
@@ -39,12 +49,14 @@ impl TurnLifecycleState {
|
||||
}
|
||||
|
||||
pub(super) fn start(&mut self, now: Instant) {
|
||||
self.steer_hint = SteerHint::Hidden;
|
||||
self.agent_turn_running = true;
|
||||
self.goal_status_active_turn_started_at = Some(now);
|
||||
self.sleep_inhibitor.set_turn_running(/*turn_running*/ true);
|
||||
}
|
||||
|
||||
pub(super) fn finish(&mut self) {
|
||||
self.steer_hint = SteerHint::Hidden;
|
||||
self.agent_turn_running = false;
|
||||
self.goal_status_active_turn_started_at = None;
|
||||
self.sleep_inhibitor
|
||||
@@ -56,6 +68,7 @@ impl TurnLifecycleState {
|
||||
}
|
||||
|
||||
pub(super) fn restore_running(&mut self, running: bool, now: Instant) {
|
||||
self.steer_hint = SteerHint::Hidden;
|
||||
self.agent_turn_running = running;
|
||||
self.goal_status_active_turn_started_at = running.then_some(now);
|
||||
self.sleep_inhibitor.set_turn_running(running);
|
||||
|
||||
Reference in New Issue
Block a user