diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 9525907f32..d59a8669db 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -1917,9 +1917,7 @@ impl ChatWidget { self.clear_unified_exec_processes(); } // Core clears pending_input before emitting TurnAborted, so any unacknowledged steers - // still tracked here can no longer be committed for this turn. - self.pending_steers.clear(); - self.refresh_pending_input_preview(); + // still tracked here must be restored locally instead of waiting for a later commit. if reason != TurnAbortReason::ReviewEnded { self.add_to_history(history_cell::new_error_event( @@ -1927,22 +1925,23 @@ impl ChatWidget { )); } - if let Some(combined) = self.drain_queued_messages_for_restore() { + if let Some(combined) = self.drain_pending_messages_for_restore() { self.restore_user_message_to_composer(combined); } + self.refresh_pending_input_preview(); self.request_redraw(); } - /// Merge queued drafts (plus the current composer state) into a single message for restore. + /// Merge pending steers, queued drafts, and the current composer state into a single message. /// - /// Each queued draft numbers attachments from `[Image #1]`. When we concatenate drafts, we - /// must renumber placeholders in a stable order so the merged attachment list stays aligned - /// with the labels embedded in text. This helper drains the queue, remaps placeholders, and - /// fixes text element byte ranges as content is appended. Returns `None` when there is nothing - /// to restore. - fn drain_queued_messages_for_restore(&mut self) -> Option { - if self.queued_user_messages.is_empty() { + /// Each pending message numbers attachments from `[Image #1]` relative to its own remote + /// images. When we concatenate multiple messages after interrupt, we must renumber local-image + /// placeholders in a stable order and rebase text element byte ranges so the restored composer + /// state stays aligned with the merged attachment list. Returns `None` when there is nothing to + /// restore. + fn drain_pending_messages_for_restore(&mut self) -> Option { + if self.pending_steers.is_empty() && self.queued_user_messages.is_empty() { return None; } @@ -1954,7 +1953,34 @@ impl ChatWidget { mention_bindings: self.bottom_pane.composer_mention_bindings(), }; - let mut to_merge: Vec = self.queued_user_messages.drain(..).collect(); + let mut to_merge: Vec = self + .pending_steers + .drain(..) + .map(|steer| { + let RenderedUserMessageEvent { + message, + remote_image_urls, + local_images, + text_elements, + } = steer; + let remote_image_count = remote_image_urls.len(); + UserMessage { + text: message, + text_elements, + local_images: local_images + .into_iter() + .enumerate() + .map(|(idx, path)| LocalImageAttachment { + placeholder: local_image_label_text(remote_image_count + idx + 1), + path, + }) + .collect(), + remote_image_urls, + mention_bindings: Vec::new(), + } + }) + .collect(); + to_merge.extend(self.queued_user_messages.drain(..)); if !existing_message.text.is_empty() || !existing_message.local_images.is_empty() || !existing_message.remote_image_urls.is_empty() diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index b9aef9ca2f..0ad6a47738 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -3761,11 +3761,15 @@ async fn steer_enter_during_final_stream_preserves_follow_up_prompts_in_order() } #[tokio::test] -async fn manual_interrupt_clears_pending_steers() { +async fn manual_interrupt_restores_pending_steers_to_composer() { 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.on_agent_message_delta( + "Final answer line +" + .to_string(), + ); chat.bottom_pane.set_composer_text( "queued while streaming".to_string(), @@ -3790,7 +3794,7 @@ async fn manual_interrupt_clears_pending_steers() { chat.on_interrupted_turn(TurnAbortReason::Interrupted); assert!(chat.pending_steers.is_empty()); - assert_eq!(chat.bottom_pane.composer_text(), ""); + assert_eq!(chat.bottom_pane.composer_text(), "queued while streaming"); assert_no_submit_op(&mut op_rx); let inserted = drain_insert_history(&mut rx); @@ -3801,6 +3805,48 @@ async fn manual_interrupt_clears_pending_steers() { ); } +#[tokio::test] +async fn manual_interrupt_restores_pending_steers_before_queued_messages() { + 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 +" + .to_string(), + ); + + chat.bottom_pane + .set_composer_text("pending steer".to_string(), Vec::new(), Vec::new()); + chat.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + chat.queued_user_messages + .push_back(UserMessage::from("queued draft".to_string())); + chat.refresh_pending_input_preview(); + + match next_submit_op(&mut op_rx) { + Op::UserTurn { items, .. } => assert_eq!( + items, + vec![UserInput::Text { + text: "pending steer".to_string(), + text_elements: Vec::new(), + }] + ), + other => panic!("expected Op::UserTurn, got {other:?}"), + } + assert!(drain_insert_history(&mut rx).is_empty()); + + chat.on_interrupted_turn(TurnAbortReason::Interrupted); + + assert!(chat.pending_steers.is_empty()); + assert!(chat.queued_user_messages.is_empty()); + assert_eq!( + chat.bottom_pane.composer_text(), + "pending steer +queued draft" + ); + assert_no_submit_op(&mut op_rx); +} + #[tokio::test] async fn task_complete_renders_unacknowledged_pending_steer() { let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(None).await;