From 0bd53ac07802d808a8e3a8e4bfe269016c652e5e Mon Sep 17 00:00:00 2001 From: Ahmed Ibrahim Date: Tue, 15 Jul 2025 16:14:43 -0700 Subject: [PATCH] fixing tests --- codex-rs/core/src/client.rs | 6 ++++- codex-rs/core/src/codex.rs | 42 +++++++++++++++++++++++++------ codex-rs/core/tests/cli_stream.rs | 8 ++++-- codex-rs/exec/src/lib.rs | 1 + 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index b88c91b57f..6d54270a29 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -197,6 +197,10 @@ impl ModelClient { } } } + + pub fn streaming_enabled(&self) -> bool { + self.config.streaming_enabled + } } #[derive(Debug, Deserialize, Serialize)] @@ -316,7 +320,7 @@ where // duplicated `output` array embedded in the `response.completed` // payload. That produced two concrete issues: // 1. No real‑time streaming – the user only saw output after the - // entire turn had finished, which broke the “typing” UX and + // entire turn had finished, which broke the "typing" UX and // made long‑running turns look stalled. // 2. Duplicate `function_call_output` items – both the // individual *and* the completed array were forwarded, which diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 1d52ef64a0..bb035e6790 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -1122,12 +1122,14 @@ async fn try_run_turn( let mut stream = sess.client.clone().stream(&prompt).await?; let mut output = Vec::new(); + // Patch: buffer for non-streaming mode + let mut assistant_message_buf = String::new(); + let streaming_enabled = sess.client.streaming_enabled(); while let Some(event) = stream.next().await { let event = event?; match event { ResponseEvent::Created => { let mut state = sess.state.lock().unwrap(); - // We successfully created a new response and ensured that all pending calls were included so we can clear the pending call ids. state.pending_call_ids.clear(); } ResponseEvent::OutputItemDone(item) => { @@ -1140,23 +1142,37 @@ async fn try_run_turn( _ => None, }; if let Some(call_id) = call_id { - // We just got a new call id so we need to make sure to respond to it in the next turn. let mut state = sess.state.lock().unwrap(); state.pending_call_ids.insert(call_id.clone()); } + // Patch: buffer assistant message text if streaming is disabled + if !streaming_enabled { + if let ResponseItem::Message { role, content } = &item { + if role == "assistant" { + for c in content { + if let ContentItem::OutputText { text } = c { + assistant_message_buf.push_str(text); + } + } + } + } + } let response = match &item { ResponseItem::Message { .. } | ResponseItem::Reasoning { .. } => None, _ => handle_response_item(sess, sub_id, item.clone()).await?, }; - output.push(ProcessedResponseItem { item, response }); } ResponseEvent::OutputTextDelta(text) => { - let event = Event { - id: sub_id.to_string(), - msg: EventMsg::AgentMessageDelta(AgentMessageEvent { message: text }), - }; - sess.tx_event.send(event).await.ok(); + if streaming_enabled { + let event = Event { + id: sub_id.to_string(), + msg: EventMsg::AgentMessageDelta(AgentMessageEvent { message: text }), + }; + sess.tx_event.send(event).await.ok(); + } else { + assistant_message_buf.push_str(&text); + } } ResponseEvent::ReasoningSummaryDelta(text) => { let event = Event { @@ -1169,6 +1185,16 @@ async fn try_run_turn( response_id, token_usage, } => { + // Patch: emit full message if we buffered deltas + if !streaming_enabled && !assistant_message_buf.is_empty() { + let event = Event { + id: sub_id.to_string(), + msg: EventMsg::AgentMessage(AgentMessageEvent { + message: assistant_message_buf.clone(), + }), + }; + sess.tx_event.send(event).await.ok(); + } if let Some(token_usage) = token_usage { sess.tx_event .send(Event { diff --git a/codex-rs/core/tests/cli_stream.rs b/codex-rs/core/tests/cli_stream.rs index df3fedfd48..fcfb5274dc 100644 --- a/codex-rs/core/tests/cli_stream.rs +++ b/codex-rs/core/tests/cli_stream.rs @@ -58,6 +58,8 @@ async fn chat_mode_stream_cli() { .arg(&provider_override) .arg("-c") .arg("model_provider=\"mock\"") + .arg("-c") + .arg("streaming=false") .arg("-C") .arg(env!("CARGO_MANIFEST_DIR")) .arg("hello?"); @@ -71,8 +73,8 @@ async fn chat_mode_stream_cli() { println!("Stderr:\n{}", String::from_utf8_lossy(&output.stderr)); assert!(output.status.success()); let stdout = String::from_utf8_lossy(&output.stdout); - assert!(stdout.contains("hi")); - assert_eq!(stdout.matches("hi").count(), 1); + let hi_lines = stdout.lines().filter(|line| line.trim() == "hi").count(); + assert_eq!(hi_lines, 1, "Expected exactly one line with 'hi'"); server.verify().await; } @@ -104,6 +106,8 @@ async fn responses_api_stream_cli() { .arg("--") .arg("exec") .arg("--skip-git-repo-check") + .arg("-c") + .arg("streaming=false") .arg("-C") .arg(env!("CARGO_MANIFEST_DIR")) .arg("hello?"); diff --git a/codex-rs/exec/src/lib.rs b/codex-rs/exec/src/lib.rs index 71f19d2b9a..49ae23437a 100644 --- a/codex-rs/exec/src/lib.rs +++ b/codex-rs/exec/src/lib.rs @@ -115,6 +115,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option) -> any }; let config = Config::load_with_cli_overrides(cli_kv_overrides, overrides)?; + println!("[DEBUG] streaming_enabled: {}", config.streaming_enabled); let mut event_processor = EventProcessor::create_with_ansi( stdout_with_ansi, !config.hide_agent_reasoning,