mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Discard TUI typeahead after prompt rewind and fork (#63193)
## Why Rewinding to edit an earlier prompt restores it in the composer. If a second rewind gesture is already buffered, its Enter can submit the restored prompt before the user edits it. [Prompt rewind behavior](https://github.com/openai/codex/pull/45845) ## What changed Pause terminal input as soon as a rewind is queued. Drain buffered input before showing the restored prompt, and use the same drain after forks from a locked thread or the agents overview. Retry in bounded batches while input is still pending, deliver buffered focus changes in order, and recheck terminal size when the drain finishes. ## Testing Extend PTY coverage to verify that repeated rewind keystrokes and a trailing Escape leave the restored prompt ready for a fresh edit and submission. Cover delayed paste completion, focus event ordering, and terminal resize during a drain. GitOrigin-RevId: 65ba4f343892ed3ba2eeb120172f66bfbebe120f
This commit is contained in:
@@ -30,6 +30,28 @@ use codex_app_server_protocol::WindowsSandboxSetupMode;
|
||||
pub(super) const SHUTDOWN_FIRST_EXIT_TIMEOUT: Duration = Duration::from_secs(/*secs*/ 2);
|
||||
|
||||
impl App {
|
||||
pub(super) fn finish_thread_input_drain(
|
||||
&mut self,
|
||||
tui: &mut tui::Tui,
|
||||
transition: ThreadTransition,
|
||||
) {
|
||||
if tui
|
||||
.discard_pending_input_after_thread_transition()
|
||||
.is_pending()
|
||||
{
|
||||
self.pending_input_drain = Some(transition);
|
||||
return;
|
||||
}
|
||||
self.pending_input_drain = None;
|
||||
if let ThreadTransition::PromptReverted(thread_id) = transition
|
||||
&& self.chat_widget.thread_id() == Some(thread_id)
|
||||
{
|
||||
self.chat_widget
|
||||
.set_queue_autosend_suppressed(/*suppressed*/ false);
|
||||
self.chat_widget.emit_prompt_edit_thread_event();
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn handle_event<'a>(
|
||||
&'a mut self,
|
||||
tui: &'a mut tui::Tui,
|
||||
@@ -59,7 +81,10 @@ impl App {
|
||||
} else {
|
||||
event
|
||||
};
|
||||
// Release the shortcut's input guard even when a fork is rejected below.
|
||||
// Release queued input guards even when the transition is rejected below.
|
||||
if matches!(event, AppEvent::RevertSessionForPromptEdit { .. }) {
|
||||
self.backtrack.revert_queued = false;
|
||||
}
|
||||
if matches!(event, AppEvent::ForkCurrentSession { .. }) {
|
||||
self.chat_widget.fork_in_progress = false;
|
||||
}
|
||||
@@ -884,10 +909,7 @@ impl App {
|
||||
}
|
||||
if from_locked_thread || from_agents_overview {
|
||||
// Repeated fork shortcuts must not act on the resulting view.
|
||||
if let Err(err) = tui.discard_pending_input_before_interactive_screen() {
|
||||
tracing::warn!(%err, "failed to discard input after forking");
|
||||
}
|
||||
tui.schedule_screen_size_recheck(Duration::ZERO);
|
||||
self.finish_thread_input_drain(tui, ThreadTransition::DiscardTypeahead);
|
||||
}
|
||||
} else {
|
||||
self.chat_widget.add_error_message(
|
||||
@@ -925,12 +947,12 @@ impl App {
|
||||
self.chat_widget.add_error_message(
|
||||
"Wait for permissions to update before editing this prompt.".into(),
|
||||
);
|
||||
tui.frame_requester().schedule_frame();
|
||||
self.finish_thread_input_drain(tui, ThreadTransition::DiscardTypeahead);
|
||||
return Ok(AppRunControl::Continue);
|
||||
}
|
||||
let Some(index) = self.transcript_cells.iter().position(|cell| Arc::ptr_eq(cell, &selected_cell)) else {
|
||||
self.restore_backtrack_prompt_after_revert_error(prompt, "the selected prompt is no longer visible");
|
||||
tui.frame_requester().schedule_frame();
|
||||
self.finish_thread_input_drain(tui, ThreadTransition::DiscardTypeahead);
|
||||
return Ok(AppRunControl::Continue);
|
||||
};
|
||||
let nth_user_message = crate::app_backtrack::user_count(&self.transcript_cells[..index]);
|
||||
@@ -995,7 +1017,7 @@ impl App {
|
||||
Ok(selection) => selection,
|
||||
Err(err) => {
|
||||
self.restore_backtrack_prompt_after_revert_error(prompt, err);
|
||||
tui.frame_requester().schedule_frame();
|
||||
self.finish_thread_input_drain(tui, ThreadTransition::DiscardTypeahead);
|
||||
return Ok(AppRunControl::Continue);
|
||||
}
|
||||
};
|
||||
@@ -1007,7 +1029,7 @@ impl App {
|
||||
if matches!(&err, codex_app_server_client::TypedRequestError::Server { source, .. }
|
||||
if matches!(source.code, -32602..=-32600)) {
|
||||
self.restore_backtrack_prompt_after_revert_error(prompt, err);
|
||||
tui.frame_requester().schedule_frame();
|
||||
self.finish_thread_input_drain(tui, ThreadTransition::DiscardTypeahead);
|
||||
return Ok(AppRunControl::Continue);
|
||||
}
|
||||
self.chat_widget.restore_user_message_to_composer(prompt);
|
||||
@@ -1090,9 +1112,9 @@ impl App {
|
||||
self.last_thread_usage_status_cell = None;
|
||||
self.pending_thread_usage_history_refresh = false;
|
||||
self.backtrack_render_pending = !tui.is_owned_screen();
|
||||
self.chat_widget.set_queue_autosend_suppressed(/*suppressed*/ false);
|
||||
self.chat_widget.emit_prompt_edit_thread_event();
|
||||
tui.frame_requester().schedule_frame();
|
||||
// Input was blocked through the transcript reset. A queued Enter must
|
||||
// not submit the prompt we just restored for editing.
|
||||
self.finish_thread_input_drain(tui, ThreadTransition::PromptReverted(thread_id));
|
||||
}
|
||||
}
|
||||
AppEvent::BeginInitialHistoryReplayBuffer => {
|
||||
|
||||
@@ -423,6 +423,7 @@ impl App {
|
||||
self.app_server_backend_id = Some(session.backend_id());
|
||||
*app_event_rx = rx;
|
||||
self.pending_thread_switch_resets = 0;
|
||||
self.backtrack.revert_queued = false;
|
||||
for (parent, pane) in &mut self.sidebars {
|
||||
pane.focus_document_request = None;
|
||||
pane.agents.pending_link = None;
|
||||
|
||||
@@ -179,9 +179,11 @@ impl App {
|
||||
tui: &tui::Tui,
|
||||
app_event_rx: &mpsc::UnboundedReceiver<AppEvent>,
|
||||
) -> bool {
|
||||
self.pending_thread_switch_resets == 0
|
||||
self.pending_input_drain.is_none()
|
||||
&& self.pending_thread_switch_resets == 0
|
||||
&& (app_event_rx.is_empty()
|
||||
|| (!self.config.features.enabled(Feature::TuiMultiBackend)
|
||||
|| (!self.backtrack.revert_queued
|
||||
&& !self.config.features.enabled(Feature::TuiMultiBackend)
|
||||
&& !self.side_retention_enabled(tui)
|
||||
&& self
|
||||
.current_displayed_thread_id()
|
||||
@@ -196,7 +198,8 @@ impl App {
|
||||
tui: &mut tui::Tui,
|
||||
app_event_rx: &mpsc::UnboundedReceiver<AppEvent>,
|
||||
) -> Result<()> {
|
||||
if self.awaiting_side_replay(tui)
|
||||
if self.pending_input_drain.is_some()
|
||||
|| self.awaiting_side_replay(tui)
|
||||
|| (tui.is_owned_screen()
|
||||
&& !app_event_rx.is_empty()
|
||||
&& !self.chat_widget.has_active_view())
|
||||
@@ -1086,6 +1089,7 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
transcript_reflow: TranscriptReflowState::default(),
|
||||
initial_history_replay_buffer: None,
|
||||
pending_thread_switch_resets: 0,
|
||||
pending_input_drain: None,
|
||||
scrollback_has_older_history: false,
|
||||
commit_animation: None,
|
||||
status_line_invalid_items_warned: status_line_invalid_items_warned.clone(),
|
||||
@@ -1601,6 +1605,11 @@ See the Codex keymap documentation for supported actions and examples."
|
||||
cloud_inventory_poll.reset();
|
||||
}
|
||||
let control = select! {
|
||||
// Leave the previous frame visible and let other app work run between batches.
|
||||
Some(transition) = std::future::ready(app.pending_input_drain), if app.pending_input_drain.is_some() => {
|
||||
app.finish_thread_input_drain(tui, transition);
|
||||
AppRunControl::Continue
|
||||
}
|
||||
result = async {
|
||||
match app.daemon_restart.as_mut() {
|
||||
Some(task) => task.await,
|
||||
|
||||
@@ -100,6 +100,7 @@ pub(crate) async fn make_test_app() -> Box<App> {
|
||||
transcript_reflow: TranscriptReflowState::default(),
|
||||
initial_history_replay_buffer: None,
|
||||
pending_thread_switch_resets: 0,
|
||||
pending_input_drain: None,
|
||||
scrollback_has_older_history: false,
|
||||
enhanced_keys_supported: false,
|
||||
keymap: crate::keymap::RuntimeKeymap::defaults(),
|
||||
|
||||
@@ -9245,6 +9245,7 @@ async fn make_test_app() -> Box<App> {
|
||||
transcript_reflow: TranscriptReflowState::default(),
|
||||
initial_history_replay_buffer: None,
|
||||
pending_thread_switch_resets: 0,
|
||||
pending_input_drain: None,
|
||||
scrollback_has_older_history: false,
|
||||
enhanced_keys_supported: false,
|
||||
keymap: crate::keymap::RuntimeKeymap::defaults(),
|
||||
@@ -9394,6 +9395,7 @@ pub(super) async fn make_test_app_with_channels() -> (
|
||||
transcript_reflow: TranscriptReflowState::default(),
|
||||
initial_history_replay_buffer: None,
|
||||
pending_thread_switch_resets: 0,
|
||||
pending_input_drain: None,
|
||||
scrollback_has_older_history: false,
|
||||
enhanced_keys_supported: false,
|
||||
keymap: crate::keymap::RuntimeKeymap::defaults(),
|
||||
|
||||
@@ -858,6 +858,25 @@ async fn unattended_modals_survive_unfinished_terminal_input() -> Result<()> {
|
||||
let mut tui = crate::tui::test_support::make_test_tui()?;
|
||||
let mut app_server =
|
||||
crate::start_embedded_app_server_for_picker(app.chat_widget.config_ref()).await?;
|
||||
// A rewind must not advertise an editable prompt while typeahead is still draining.
|
||||
crossterm::event::buffer_input(b"\x1b[200~unfinished")?;
|
||||
let transition = ThreadTransition::PromptReverted(thread_id);
|
||||
app.finish_thread_input_drain(&mut tui, transition);
|
||||
assert!(!app.can_read_terminal_after_thread_switch(&tui, &app_event_rx));
|
||||
assert!(
|
||||
!std::iter::from_fn(|| app_event_rx.try_recv().ok())
|
||||
.any(|event| matches!(event, AppEvent::InsertHistoryCell(_))),
|
||||
"premature rewind notice"
|
||||
);
|
||||
crossterm::event::buffer_input(b"\x1b[201~\r")?;
|
||||
app.finish_thread_input_drain(&mut tui, transition);
|
||||
assert!(
|
||||
std::iter::from_fn(|| app_event_rx.try_recv().ok())
|
||||
.any(|event| matches!(event, AppEvent::InsertHistoryCell(_))),
|
||||
"missing rewind notice"
|
||||
);
|
||||
assert!(app.can_read_terminal_after_thread_switch(&tui, &app_event_rx));
|
||||
|
||||
crossterm::event::buffer_input(b"\x1b]10;")?;
|
||||
let request = exec_approval_request(thread_id, "turn-1", "call-1", /*approval_id*/ None);
|
||||
let _ = app
|
||||
|
||||
Reference in New Issue
Block a user