Preserve streamed assistant message order in retained context (#47584)

## Why

A user can reply to a streamed question before its completed message is recorded. Assigning the assistant message's order at completion can place the reply before the question in retained context.

## What changed

Reserve an assistant message's order when its source stream starts in thread-owned Guardian context, then reuse that reservation when recording the message. Reserve completion-only messages before publishing their text, and give text and plan output from the same source message a single order.

## Testing

Add streaming regression coverage that accepts a reply before the question completes and verifies question-before-reply ordering both live and after resume. Cover server-provided and generated message IDs, plan output, and text combined with a plan.

GitOrigin-RevId: cb67e7459c804df7beb0841908244202448c655a
This commit is contained in:
felixxia-oai
2026-09-23 16:16:29 +00:00
committed by copyberry
parent 29aa1df62e
commit 876ce590e1
4 changed files with 208 additions and 3 deletions
+15 -2
View File
@@ -3623,6 +3623,9 @@ impl Session {
.current_time_reminder
.note_recorded_items(&response_items);
if self.guardian_context_mode == GuardianContextMode::ThreadOwned {
let pending_orders = turn_context
.extension_data
.get::<retained_context::PendingAssistantMessageOrders>();
for envelope in &mut items {
if envelope
.metadata
@@ -3635,12 +3638,22 @@ impl Session {
|| matches!(&envelope.item, ResponseItem::FunctionCall { .. })
|| crate::context::is_user_authorization_message(&envelope.item)
{
// Preserve accepted input order; assign delivery order to other originals.
let message_order = pending_orders.as_ref().and_then(|orders| {
orders
.0
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.remove(envelope.item.id()?.as_str())
});
// Preserve input acceptance and source-message start order.
// Synthetic messages still receive their order here.
envelope
.metadata
.get_or_insert_default()
.user_input_order
.get_or_insert_with(|| state.history.reserve_input_order());
.get_or_insert_with(|| {
message_order.unwrap_or_else(|| state.history.reserve_input_order())
});
}
}
}
+42 -1
View File
@@ -1,13 +1,54 @@
//! Orders retained inputs and records host-verified facts at the context checkpoint boundary.
//! Orders retained inputs at acceptance and assistant messages at source-stream start.
use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::PoisonError;
use crate::context::GuardianContextMode;
use codex_history::RetainedContextEvent;
use codex_history::RolloutItem;
use codex_protocol::models::ResponseItem;
use super::Session;
use super::TurnContext;
use super::thread_settings;
/// Only unrecorded starts need a reservation; abandoned entries expire with the turn.
#[derive(Default)]
pub(super) struct PendingAssistantMessageOrders(pub(super) Mutex<HashMap<String, u64>>);
impl Session {
/// Reserve before deriving display items, including plans, from the source message.
/// Completion-only responses use the same path before publishing their text.
pub(super) async fn reserve_assistant_message_order(
&self,
turn_context: &TurnContext,
item: &ResponseItem,
) {
if self.guardian_context_mode == GuardianContextMode::ThreadOwned
&& let ResponseItem::Message {
id: Some(id), role, ..
} = item
&& role == "assistant"
{
let mut state = self.state.lock().await;
if !state
.history
.raw_items()
.any(|item| item.id().is_some_and(|recorded_id| recorded_id == id))
{
turn_context
.extension_data
.get_or_init(PendingAssistantMessageOrders::default)
.0
.lock()
.unwrap_or_else(PoisonError::into_inner)
.entry(id.to_string())
.or_insert_with(|| state.history.reserve_input_order());
}
}
}
/// Legacy mode neither reserves a sequence nor takes the session-state lock.
pub(crate) async fn reserve_user_input_order(&self) -> Option<u64> {
if self.guardian_context_mode == GuardianContextMode::Legacy {
+4
View File
@@ -2603,6 +2603,8 @@ async fn try_run_sampling_request(
}
ResponseEvent::OutputItemDone(mut item) => {
assign_missing_streamed_response_item_id(&mut item, active_item.as_ref());
sess.reserve_assistant_message_order(&turn_context, &item)
.await;
if analytics_tool_call_ids.len() < MAX_ANALYTICS_TOOL_CALL_IDS_PER_RESPONSE {
let call_id = match &item {
ResponseItem::FunctionCall { call_id, .. }
@@ -2715,6 +2717,8 @@ async fn try_run_sampling_request(
}
ResponseEvent::OutputItemAdded(mut item) => {
assign_missing_streamed_response_item_id(&mut item, /*active_item*/ None);
sess.reserve_assistant_message_order(&turn_context, &item)
.await;
if let ResponseItem::CustomToolCall {
call_id,
name,