From 7a253076fef8058adb7964fdd521df80663cf39d Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Wed, 4 Feb 2026 11:47:10 -0800 Subject: [PATCH] Persist pending input user events (#10656) - Persist user-message events for mid-turn injected input by emitting user message turn items when pending input is recorded. --- codex-rs/core/src/codex.rs | 28 +++++++++++++++++----- codex-rs/core/tests/suite/pending_input.rs | 5 ++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 5368b8b06..b1d2fdf45 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -3579,19 +3579,35 @@ pub(crate) async fn run_turn( // Note that pending_input would be something like a message the user // submitted through the UI while the model was running. Though the UI // may support this, the model might not. - let pending_input = sess + let pending_response_items = sess .get_pending_input() .await .into_iter() .map(ResponseItem::from) .collect::>(); + if !pending_response_items.is_empty() { + for response_item in pending_response_items { + if let Some(TurnItem::UserMessage(user_message)) = parse_turn_item(&response_item) { + // todo(aibrahim): move pending input to be UserInput only to keep TextElements. context: https://github.com/openai/codex/pull/10656#discussion_r2765522480 + sess.record_user_prompt_and_emit_turn_item( + turn_context.as_ref(), + &user_message.content, + response_item, + ) + .await; + } else { + sess.record_conversation_items( + &turn_context, + std::slice::from_ref(&response_item), + ) + .await; + } + } + } + // Construct the input that we will send to the model. - let sampling_request_input: Vec = { - sess.record_conversation_items(&turn_context, &pending_input) - .await; - sess.clone_history().await.for_prompt() - }; + let sampling_request_input: Vec = { sess.clone_history().await.for_prompt() }; let sampling_request_input_messages = sampling_request_input .iter() diff --git a/codex-rs/core/tests/suite/pending_input.rs b/codex-rs/core/tests/suite/pending_input.rs index ad662d901..1fcff4da3 100644 --- a/codex-rs/core/tests/suite/pending_input.rs +++ b/codex-rs/core/tests/suite/pending_input.rs @@ -125,6 +125,11 @@ async fn injected_user_input_triggers_follow_up_request_with_deltas() { let _ = gate_completed_tx.send(()); + let _ = wait_for_event(&codex, |event| { + matches!(event, EventMsg::UserMessage(message) if message.message == "second prompt") + }) + .await; + wait_for_event(&codex, |event| matches!(event, EventMsg::TurnComplete(_))).await; let requests = server.requests().await;