From 0b6fb310e34c805f3613d2410ddfa8551cb95fb4 Mon Sep 17 00:00:00 2001 From: Charles Cunningham Date: Thu, 19 Mar 2026 15:09:47 -0700 Subject: [PATCH] tui: requeue leftover pending steers on turn complete Co-authored-by: Codex --- codex-rs/tui/src/chatwidget.rs | 13 ++ codex-rs/tui/src/chatwidget/tests.rs | 114 ++++++++++++++++++ codex-rs/tui_app_server/src/chatwidget.rs | 13 ++ .../tui_app_server/src/chatwidget/tests.rs | 114 ++++++++++++++++++ 4 files changed, 254 insertions(+) diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 6183aacd87..76d6dc1fa4 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1790,6 +1790,19 @@ impl ChatWidget { self.request_redraw(); let had_pending_steers = !self.pending_steers.is_empty(); + if had_pending_steers { + // Some tasks, such as `/compact`, can finish without ever committing steer input back + // through core. When that happens, preserve FIFO by converting the leftover steers + // into normal queued follow-up turns ahead of any tab-queued drafts. + let pending_steers: Vec = self + .pending_steers + .drain(..) + .map(|pending| pending.user_message) + .collect(); + for user_message in pending_steers.into_iter().rev() { + self.queued_user_messages.push_front(user_message); + } + } self.refresh_pending_input_preview(); if !from_replay && self.queued_user_messages.is_empty() && !had_pending_steers { diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index cc91dce6f2..7716824567 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -4122,6 +4122,120 @@ async fn steer_enter_uses_pending_steers_while_final_answer_stream_is_active() { assert!(lines_to_single_string(&inserted[0]).contains("queued while streaming")); } +#[tokio::test] +async fn turn_complete_requeues_unconsumed_pending_steers_before_queued_drafts() { + let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await; + chat.thread_id = Some(ThreadId::new()); + chat.on_task_started(); + + chat.bottom_pane + .set_composer_text("first pending steer".to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "first pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + + chat.bottom_pane + .set_composer_text("second pending steer".to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "second pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + + chat.queued_user_messages + .push_back(UserMessage::from("queued draft".to_string())); + chat.refresh_pending_input_preview(); + assert!(drain_insert_history(&mut rx).is_empty()); + + chat.handle_codex_event(Event { + id: "turn-1".into(), + msg: EventMsg::TurnComplete(TurnCompleteEvent { + turn_id: "turn-1".to_string(), + last_agent_message: None, + }), + }); + + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "first pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected requeued pending steer to submit first, got {other:?}"), + } + assert!(chat.pending_steers.is_empty()); + assert_eq!( + chat.queued_user_message_texts(), + vec![ + "second pending steer".to_string(), + "queued draft".to_string() + ] + ); +} + +#[tokio::test] +async fn turn_complete_requeues_pending_steer_after_final_message_streaming() { + let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await; + chat.thread_id = Some(ThreadId::new()); + chat.on_task_started(); + chat.on_agent_message_delta("Final answer line\n".to_string()); + + chat.bottom_pane.set_composer_text( + "queued while streaming".to_string(), + Vec::new(), + Vec::new(), + ); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "queued while streaming".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + assert!(drain_insert_history(&mut rx).is_empty()); + + chat.handle_codex_event(Event { + id: "turn-1".into(), + msg: EventMsg::TurnComplete(TurnCompleteEvent { + turn_id: "turn-1".to_string(), + last_agent_message: None, + }), + }); + + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "queued while streaming".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected requeued streamed steer to submit, got {other:?}"), + } + assert!(chat.pending_steers.is_empty()); + assert!(chat.queued_user_messages.is_empty()); +} + #[tokio::test] async fn failed_pending_steer_submit_does_not_add_pending_preview() { let (mut chat, mut rx, op_rx) = make_chatwidget_manual(None).await; diff --git a/codex-rs/tui_app_server/src/chatwidget.rs b/codex-rs/tui_app_server/src/chatwidget.rs index b233527faf..4fe26b6f51 100644 --- a/codex-rs/tui_app_server/src/chatwidget.rs +++ b/codex-rs/tui_app_server/src/chatwidget.rs @@ -2150,6 +2150,19 @@ impl ChatWidget { self.request_redraw(); let had_pending_steers = !self.pending_steers.is_empty(); + if had_pending_steers { + // Some tasks, such as `/compact`, can finish without ever committing steer input back + // through core. When that happens, preserve FIFO by converting the leftover steers + // into normal queued follow-up turns ahead of any tab-queued drafts. + let pending_steers: Vec = self + .pending_steers + .drain(..) + .map(|pending| pending.user_message) + .collect(); + for user_message in pending_steers.into_iter().rev() { + self.queued_user_messages.push_front(user_message); + } + } self.refresh_pending_input_preview(); if !from_replay && self.queued_user_messages.is_empty() && !had_pending_steers { diff --git a/codex-rs/tui_app_server/src/chatwidget/tests.rs b/codex-rs/tui_app_server/src/chatwidget/tests.rs index 2b14dac16d..8648cda438 100644 --- a/codex-rs/tui_app_server/src/chatwidget/tests.rs +++ b/codex-rs/tui_app_server/src/chatwidget/tests.rs @@ -4126,6 +4126,120 @@ async fn steer_enter_uses_pending_steers_while_final_answer_stream_is_active() { assert!(lines_to_single_string(&inserted[0]).contains("queued while streaming")); } +#[tokio::test] +async fn turn_complete_requeues_unconsumed_pending_steers_before_queued_drafts() { + let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await; + chat.thread_id = Some(ThreadId::new()); + chat.on_task_started(); + + chat.bottom_pane + .set_composer_text("first pending steer".to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "first pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + + chat.bottom_pane + .set_composer_text("second pending steer".to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "second pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + + chat.queued_user_messages + .push_back(UserMessage::from("queued draft".to_string())); + chat.refresh_pending_input_preview(); + assert!(drain_insert_history(&mut rx).is_empty()); + + chat.handle_codex_event(Event { + id: "turn-1".into(), + msg: EventMsg::TurnComplete(TurnCompleteEvent { + turn_id: "turn-1".to_string(), + last_agent_message: None, + }), + }); + + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "first pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected requeued pending steer to submit first, got {other:?}"), + } + assert!(chat.pending_steers.is_empty()); + assert_eq!( + chat.queued_user_message_texts(), + vec![ + "second pending steer".to_string(), + "queued draft".to_string() + ] + ); +} + +#[tokio::test] +async fn turn_complete_requeues_pending_steer_after_final_message_streaming() { + let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await; + chat.thread_id = Some(ThreadId::new()); + chat.on_task_started(); + chat.on_agent_message_delta("Final answer line\n".to_string()); + + chat.bottom_pane.set_composer_text( + "queued while streaming".to_string(), + Vec::new(), + Vec::new(), + ); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "queued while streaming".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + assert!(drain_insert_history(&mut rx).is_empty()); + + chat.handle_codex_event(Event { + id: "turn-1".into(), + msg: EventMsg::TurnComplete(TurnCompleteEvent { + turn_id: "turn-1".to_string(), + last_agent_message: None, + }), + }); + + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "queued while streaming".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected requeued streamed steer to submit, got {other:?}"), + } + assert!(chat.pending_steers.is_empty()); + assert!(chat.queued_user_messages.is_empty()); +} + #[tokio::test] async fn failed_pending_steer_submit_does_not_add_pending_preview() { let (mut chat, mut rx, op_rx) = make_chatwidget_manual(None).await;