mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
Defer queue-only mail after a retained final answer (#62639)
## Why A WebSocket response can still complete with a final answer while Codex drains it after a mailbox interrupt. Treating the interrupt alone as a reason to resample reopens the turn even when the pending mail can wait. ## What changed Recheck whether pending mail requires preemption after the drain. When the drain retains a final answer, queue-only mail waits for the next turn's sampling boundary. User steering can still reopen the current turn. ## Testing Add WebSocket regression cases for queue-only mail after both empty and nonempty final answers, and for user steering received during the drain. GitOrigin-RevId: 914b51f2de1afc91311667cc05718507717aaaa0
This commit is contained in:
@@ -3256,9 +3256,16 @@ async fn try_run_sampling_request(
|
||||
if resampling.update_usage_boundary {
|
||||
sess.state.lock().await.history.reset_last_usage_boundary();
|
||||
}
|
||||
// A completed final retained during the drain can defer queue-only mail.
|
||||
// Recheck that boundary instead of reopening it solely because we interrupted.
|
||||
let mailbox_preempted = *draining_for_mailbox
|
||||
&& sess
|
||||
.input_queue
|
||||
.should_preempt_sampling_for_mail(&sess.active_turn, &turn_context.sub_id)
|
||||
.await;
|
||||
break Ok(SamplingRequestResult {
|
||||
needs_follow_up,
|
||||
mailbox_preempted: *draining_for_mailbox,
|
||||
needs_follow_up: needs_follow_up || mailbox_preempted,
|
||||
mailbox_preempted,
|
||||
last_agent_message,
|
||||
});
|
||||
}
|
||||
@@ -3581,7 +3588,6 @@ async fn try_run_sampling_request(
|
||||
}
|
||||
// Keep the response ID and terminal decision. Later tools receive a denial.
|
||||
*draining_for_mailbox = true;
|
||||
needs_follow_up = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -333,6 +333,120 @@ async fn mailbox_preemption_drains_websocket(scenario: MailboxDrain) -> anyhow::
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test_case(false, "The answer is ready."; "queue_only_mail_waits_for_next_turn")]
|
||||
#[test_case(false, ""; "empty_final_defers_queue_only_mail")]
|
||||
#[test_case(true, "The answer is ready."; "user_steering_still_reopens_turn")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn mailbox_preemption_retains_final_without_reopening_for_queue_only_mail(
|
||||
steer_during_drain: bool,
|
||||
final_text: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
core_test_support::skip_if_no_network!(Ok(()));
|
||||
|
||||
let boundary = responses::ev_reasoning_item("boundary", &["Preparing the answer"], &[]);
|
||||
let acknowledgement = json!({"type": "response.interrupt.accepted", "response_id": "first"});
|
||||
let mut exchanges = mailbox_initial_responses(&boundary);
|
||||
let mut final_answer = responses::ev_assistant_message("retained-final", final_text);
|
||||
final_answer["item"]["phase"] = json!("final_answer");
|
||||
exchanges.push(vec![
|
||||
acknowledgement.clone(),
|
||||
final_answer,
|
||||
responses::ev_completed("first"),
|
||||
]);
|
||||
if !steer_during_drain {
|
||||
// Mail carried into a new user turn is delivered at that turn's next
|
||||
// sampling boundary, rather than ahead of the initial user input.
|
||||
exchanges.push(vec![
|
||||
responses::ev_response_created("next-boundary"),
|
||||
responses::ev_reasoning_item("next-reasoning", &["Reading the new request"], &[]),
|
||||
responses::ev_completed("next-boundary"),
|
||||
]);
|
||||
}
|
||||
exchanges.push(vec![
|
||||
responses::ev_response_created("next"),
|
||||
responses::ev_assistant_message("next-final", "The new request is answered."),
|
||||
responses::ev_completed("next"),
|
||||
]);
|
||||
let server = responses::start_websocket_server(vec![exchanges]).await;
|
||||
let release = server.gate_response_event(&boundary);
|
||||
let release_drain = steer_during_drain.then(|| server.gate_response_event(&acknowledgement));
|
||||
let test = start_mailbox_preemption(&server, release).await?;
|
||||
let next_prompt = "Use both worker updates.";
|
||||
if let Some(release_drain) = release_drain {
|
||||
server
|
||||
.wait_for_request(/*connection_index*/ 0, /*request_index*/ 2)
|
||||
.await;
|
||||
let submission = test
|
||||
.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![super::text(next_prompt)]))
|
||||
.await?;
|
||||
assert!(matches!(submission, TurnInputSubmission::Steered { .. }));
|
||||
release_drain.send(()).expect("release retained final");
|
||||
}
|
||||
let mut completed_messages = Vec::new();
|
||||
wait_for_event(&test.codex, |event| {
|
||||
assert!(
|
||||
!matches!(event, EventMsg::Error(_) | EventMsg::StreamError(_)),
|
||||
"{event:?}"
|
||||
);
|
||||
if let EventMsg::ItemCompleted(item) = event
|
||||
&& let codex_protocol::items::TurnItem::AgentMessage(message) = &item.item
|
||||
{
|
||||
completed_messages.push(message.id.clone());
|
||||
}
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
|
||||
if steer_during_drain {
|
||||
assert_eq!(completed_messages, vec!["retained-final", "next-final"]);
|
||||
} else {
|
||||
assert_eq!(completed_messages, vec!["retained-final"]);
|
||||
assert_eq!(server.single_connection().len(), 3);
|
||||
test.codex
|
||||
.start_or_steer_turn(TurnInputRequest::user_input(vec![super::text(next_prompt)]))
|
||||
.await?;
|
||||
wait_for_event(&test.codex, |event| {
|
||||
matches!(event, EventMsg::TurnComplete(_))
|
||||
})
|
||||
.await;
|
||||
}
|
||||
let requests = server.single_connection();
|
||||
assert_eq!(requests.len(), if steer_during_drain { 4 } else { 5 });
|
||||
assert_eq!(
|
||||
requests[2].body_json(),
|
||||
json!({
|
||||
"type": "response.interrupt", "response_id": "first", "mode": "discard_partial_items",
|
||||
}),
|
||||
);
|
||||
let input = responses::strip_response_item_ids_from_json(responses::strip_metadata_from_json(
|
||||
requests.last().expect("mail delivery request").body_json()["input"].clone(),
|
||||
));
|
||||
let input = input.as_array().expect("next request input");
|
||||
assert_eq!(
|
||||
input
|
||||
.iter()
|
||||
.filter(|item| item["type"] == "agent_message")
|
||||
.cloned()
|
||||
.collect::<Vec<_>>(),
|
||||
expected_mailbox_input(),
|
||||
);
|
||||
let first_next_input = requests[3].body_json()["input"].clone();
|
||||
assert!(
|
||||
first_next_input
|
||||
.as_array()
|
||||
.expect("next user input")
|
||||
.iter()
|
||||
.any(|item| {
|
||||
item["role"] == "user"
|
||||
&& item["content"] == json!([{"type": "input_text", "text": next_prompt}])
|
||||
})
|
||||
);
|
||||
test.codex.shutdown_and_wait().await?;
|
||||
server.shutdown().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test_case(2; "disconnect_before_interrupt")]
|
||||
#[test_case(3; "disconnect_after_interrupt")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
|
||||
Reference in New Issue
Block a user