Preserve queued agent messages when a parent turn is interrupted (#62348)

## Why

A parent turn can acknowledge a child agent's final reply while a model
request is in flight. If the parent is interrupted before consuming the
queued reply, turn cleanup can discard it.

## What changed

Record queued agent messages in conversation history during interruption
and flush the rollout before emitting `TurnAborted`, even when the
interruption marker is disabled.

## Testing

Add a regression test that interrupts a parent while a child's final
reply is queued. The test checks that the reply is persisted when
`TurnAborted` arrives and appears exactly once in the next model request.

GitOrigin-RevId: 5bd828c4101942fa4fe7ba5040355a8244f02018
This commit is contained in:
jif
2026-11-18 19:39:41 +00:00
committed by copyberry
parent 0e011c0092
commit 9d8ca1df71
3 changed files with 152 additions and 11 deletions
+40 -11
View File
@@ -1176,24 +1176,23 @@ impl Session {
turn_context: &TurnContext,
cause: Option<&CodexErrorInfo>,
) {
let Some(marker) = interrupted_turn_history_marker(
if let Some(marker) = interrupted_turn_history_marker(
InterruptedTurnHistoryMarker::from_config_and_version(
turn_context.config.as_ref(),
turn_context.multi_agent_version,
),
cause,
) else {
return;
};
self.record_conversation_items(
turn_context,
turn_context.model_info(),
std::slice::from_ref(&marker),
)
.await;
) {
self.record_conversation_items(
turn_context,
turn_context.model_info(),
std::slice::from_ref(&marker),
)
.await;
}
// Clients may re-read the rollout as soon as they receive the terminal event.
if let Err(err) = self.flush_rollout().await {
warn!("failed to flush interrupted-turn marker before terminal event: {err}");
warn!("failed to flush interrupted-turn history before terminal event: {err}");
}
}
@@ -1270,6 +1269,36 @@ impl Session {
.await;
if reason == TurnAbortReason::Interrupted {
// Injection acknowledges queueing. Preserve accepted agent mail before
// teardown discards pending input and the host observes TurnAborted.
let mut pending_mail = Vec::new();
let mut remaining_input = Vec::new();
for input in self
.input_queue
.take_pending_input_for_turn_state(turn_state)
.await
{
match input {
TurnInput::ResponseItem(item)
if matches!(item.item, ResponseItem::AgentMessage { .. }) =>
{
pending_mail.push(item);
}
input => remaining_input.push(input),
}
}
// Existing cleanup requeues native terminal results from this input.
self.input_queue
.extend_pending_input_for_turn_state(turn_state, remaining_input)
.await;
if !pending_mail.is_empty() {
self.record_annotated_conversation_items(
&task.turn_context,
&task.turn_context.capture_current_model_info(),
pending_mail,
)
.await;
}
self.record_interrupted_turn_history(
&task.turn_context,
error