Omit the turn-aborted marker when a turn goes to sleep (#65341)

## Why

When a turn wakes after a planned sleep, a synthetic `<turn_aborted>`
message makes the previous turn look interrupted to the model.

## What changed

Skip the model-facing marker for `InterruptIfNoPendingInput`. Continue
flushing the rollout before emitting `TurnAborted` so clients can read
the latest history when they receive the event.

## Testing

Extend the abort lifecycle test to check that the wake-up request
contains the prior agent message and omits the `<turn_aborted>` marker.

GitOrigin-RevId: fd5deb7f926551fa15edd4bab7d83c81842e4270
This commit is contained in:
Owen Lin
2026-12-02 21:50:29 +00:00
committed by copyberry
parent 2b282f5c01
commit cffa2fddad
4 changed files with 46 additions and 17 deletions
@@ -23,6 +23,8 @@ mod tests;
impl CodexThread {
/// Interrupts the named active turn unless it has queued input.
/// Emits `TurnAborted` without adding a synthetic `<turn_aborted>` message
/// to the next model request.
/// Returns the decision before joining cancellation, so tool callbacks can await it.
/// Like [`Op::Interrupt`], this does not notify idle contributors; the caller owns wakeup.
pub async fn interrupt_if_no_pending_input(&self, turn_id: &str) -> CodexResult<bool> {
@@ -103,6 +105,7 @@ impl Session {
active_turn,
TurnAbortReason::Interrupted,
/*error*/ None,
/*record_interrupted_turn_marker*/ false,
std::future::ready(()),
)
.await;
+35 -12
View File
@@ -709,6 +709,7 @@ impl Session {
reason.clone(),
&active_turn,
/*error*/ None,
/*record_interrupted_turn_marker*/ true,
std::future::ready(()),
)
.await;
@@ -753,8 +754,14 @@ impl Session {
return false;
};
self.finish_turn_abort(active_turn, reason, error, publication)
.await;
self.finish_turn_abort(
active_turn,
reason,
error,
/*record_interrupted_turn_marker*/ true,
publication,
)
.await;
true
}
@@ -782,12 +789,20 @@ impl Session {
mut active_turn: ActiveTurn,
reason: TurnAbortReason,
error: Option<ErrorEvent>,
record_interrupted_turn_marker: bool,
publication: impl Future<Output = ()>,
) {
let task = active_turn.task.take();
if let Some(task) = task {
self.handle_task_abort(task, reason.clone(), &active_turn, error, publication)
.await;
self.handle_task_abort(
task,
reason.clone(),
&active_turn,
error,
record_interrupted_turn_marker,
publication,
)
.await;
} else {
publication.await;
}
@@ -936,6 +951,7 @@ impl Session {
if sampling_interrupted || stop_cause.is_some() {
self.record_interrupted_turn_history(&turn_context, stop_cause.as_ref())
.await;
self.flush_rollout_before_terminal_event().await;
}
drop(finish_recording);
// A retry on the first error must start a new turn with the denied turn's input in history.
@@ -1211,9 +1227,12 @@ impl Session {
)
.await;
}
}
async fn flush_rollout_before_terminal_event(&self) {
// 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 history before terminal event: {err}");
warn!("failed to flush rollout before terminal event: {err}");
}
}
@@ -1223,6 +1242,7 @@ impl Session {
reason: TurnAbortReason,
active_turn: &ActiveTurn,
error: Option<ErrorEvent>,
record_interrupted_turn_marker: bool,
publication: impl Future<Output = ()>,
) {
let turn_state = active_turn.turn_state.as_ref();
@@ -1340,13 +1360,16 @@ impl Session {
self.input_queue
.extend_pending_input_for_turn_state(turn_state, remaining_input)
.await;
self.record_interrupted_turn_history(
&task.turn_context,
error
.as_ref()
.and_then(|error| error.codex_error_info.as_ref()),
)
.await;
if record_interrupted_turn_marker {
self.record_interrupted_turn_history(
&task.turn_context,
error
.as_ref()
.and_then(|error| error.codex_error_info.as_ref()),
)
.await;
}
self.flush_rollout_before_terminal_event().await;
drop(finish_recording);
run_turn_interrupt_hooks(self, &task.turn_context, turn_state).await;
}
+5 -3
View File
@@ -168,9 +168,11 @@ async fn abort_lifecycle_finishes_before_terminal_event(
if let Some(wake) = wake {
wait_for_event(&thread, |event| matches!(event, EventMsg::TurnStarted(_))).await;
wait_for_event(&thread, |event| matches!(event, EventMsg::TurnComplete(_))).await;
assert_eq!(
wake.single_request().inputs_of_type("agent_message").len(),
1
let wake_request = wake.single_request();
assert_eq!(wake_request.inputs_of_type("agent_message").len(), 1);
assert!(
!wake_request.body_contains_text("<turn_aborted>"),
"a planned sleep should not look interrupted to the waking model"
);
}
if !matches!(operation, AbortOperation::Shutdown) {
+3 -2
View File
@@ -703,8 +703,9 @@ pub enum Op {
/// Interrupt a spawned agent, respecting its configured sampling boundary.
InterruptAgent,
/// Interrupt the named turn only if no input is queued for it.
/// The decision is acknowledged before cancellation finishes.
/// Interrupt the named turn only if no input is queued for it. This emits `TurnAborted`
/// without adding a synthetic `<turn_aborted>` message to the next model request. The
/// decision is acknowledged before cancellation finishes.
#[debug("InterruptIfNoPendingInput {{ turn_id: {turn_id:?} }}")]
InterruptIfNoPendingInput {
turn_id: String,