mirror of
https://github.com/openai/codex.git
synced 2026-11-22 23:00:42 +00:00
codex: address PR review feedback (#16803)
Preserve buffered assistant text across non-message item completions so delayed assistant items can still replay early deltas.
This commit is contained in:
@@ -7416,6 +7416,8 @@ async fn try_run_sampling_request(
|
||||
match event {
|
||||
ResponseEvent::Created => {}
|
||||
ResponseEvent::OutputItemDone(item) => {
|
||||
let completed_assistant_message =
|
||||
matches!(&item, ResponseItem::Message { role, .. } if role == "assistant");
|
||||
let previously_active_item = active_item.take();
|
||||
if let Some(previous) = previously_active_item.as_ref()
|
||||
&& matches!(previous, TurnItem::AgentMessage(_))
|
||||
@@ -7473,10 +7475,11 @@ async fn try_run_sampling_request(
|
||||
let output_result = handle_output_item_done(&mut ctx, item, previously_active_item)
|
||||
.instrument(handle_responses)
|
||||
.await?;
|
||||
if !pending_output_text_delta.is_empty() {
|
||||
// If an item completed while this buffer is still non-empty, those bytes could
|
||||
// not be matched to an assistant item. Clear them so they don't leak into the
|
||||
// next item.
|
||||
if completed_assistant_message && !pending_output_text_delta.is_empty() {
|
||||
// If an assistant message completed while this buffer is still non-empty,
|
||||
// those bytes could not be matched to that message item. Clear them so they
|
||||
// don't leak into a later assistant item, but keep the buffer across
|
||||
// reasoning/tool completions so a delayed message item can still claim it.
|
||||
warn!(
|
||||
buffered_len = pending_output_text_delta.len(),
|
||||
"dropping buffered output text deltas after item completion"
|
||||
|
||||
@@ -1131,6 +1131,76 @@ async fn output_text_delta_before_output_item_added_is_buffered() -> anyhow::Res
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn buffered_output_text_survives_intervening_reasoning_item_done() -> anyhow::Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = start_mock_server().await;
|
||||
let TestCodex {
|
||||
codex,
|
||||
session_configured,
|
||||
..
|
||||
} = test_codex().build(&server).await?;
|
||||
|
||||
let stream = sse(vec![
|
||||
ev_response_created("resp-1"),
|
||||
ev_output_text_delta("Hello "),
|
||||
ev_reasoning_item("reasoning-1", &["thinking"], &[]),
|
||||
ev_message_item_added("msg-1", ""),
|
||||
ev_output_text_delta("world"),
|
||||
ev_assistant_message("msg-1", "Hello world"),
|
||||
ev_completed("resp-1"),
|
||||
]);
|
||||
mount_sse_once(&server, stream).await;
|
||||
|
||||
codex
|
||||
.submit(Op::UserTurn {
|
||||
items: vec![UserInput::Text {
|
||||
text: "hello".into(),
|
||||
text_elements: Vec::new(),
|
||||
}],
|
||||
final_output_json_schema: None,
|
||||
cwd: std::env::current_dir()?,
|
||||
approval_policy: codex_protocol::protocol::AskForApproval::Never,
|
||||
approvals_reviewer: None,
|
||||
sandbox_policy: codex_protocol::protocol::SandboxPolicy::DangerFullAccess,
|
||||
model: session_configured.model.clone(),
|
||||
effort: None,
|
||||
summary: None,
|
||||
service_tier: None,
|
||||
collaboration_mode: None,
|
||||
personality: None,
|
||||
})
|
||||
.await?;
|
||||
|
||||
let mut agent_deltas = Vec::new();
|
||||
let mut completed = None;
|
||||
loop {
|
||||
match wait_for_event(&codex, |_| true).await {
|
||||
EventMsg::AgentMessageContentDelta(event) => agent_deltas.push(event.delta),
|
||||
EventMsg::ItemCompleted(ItemCompletedEvent {
|
||||
item: TurnItem::AgentMessage(item),
|
||||
..
|
||||
}) => completed = Some(item),
|
||||
EventMsg::TurnComplete(_) => break,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(agent_deltas.concat(), "Hello world");
|
||||
let completed_text: String = completed
|
||||
.expect("assistant item completion should be emitted")
|
||||
.content
|
||||
.iter()
|
||||
.map(|entry| match entry {
|
||||
AgentMessageContent::Text { text } => text.as_str(),
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(completed_text, "Hello world");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn orphan_reasoning_summary_events_do_not_break_completed_reasoning_item()
|
||||
-> anyhow::Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user