From cffa2fddadf6768693712c2c1d722d4d67c4dcab Mon Sep 17 00:00:00 2001 From: Owen Lin Date: Wed, 2 Dec 2026 21:31:31 +0000 Subject: [PATCH] Omit the turn-aborted marker when a turn goes to sleep (#65341) ## Why When a turn wakes after a planned sleep, a synthetic `` 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 `` marker. GitOrigin-RevId: fd5deb7f926551fa15edd4bab7d83c81842e4270 --- .../src/session/extension_interruption.rs | 3 ++ codex-rs/core/src/tasks/mod.rs | 47 ++++++++++++++----- codex-rs/core/tests/suite/abort_lifecycle.rs | 8 ++-- codex-rs/protocol/src/protocol.rs | 5 +- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/codex-rs/core/src/session/extension_interruption.rs b/codex-rs/core/src/session/extension_interruption.rs index dfc5200d46..335bb5dcf2 100644 --- a/codex-rs/core/src/session/extension_interruption.rs +++ b/codex-rs/core/src/session/extension_interruption.rs @@ -23,6 +23,8 @@ mod tests; impl CodexThread { /// Interrupts the named active turn unless it has queued input. + /// Emits `TurnAborted` without adding a synthetic `` 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 { @@ -103,6 +105,7 @@ impl Session { active_turn, TurnAbortReason::Interrupted, /*error*/ None, + /*record_interrupted_turn_marker*/ false, std::future::ready(()), ) .await; diff --git a/codex-rs/core/src/tasks/mod.rs b/codex-rs/core/src/tasks/mod.rs index 1b7dba7cce..8e9ca62b5d 100644 --- a/codex-rs/core/src/tasks/mod.rs +++ b/codex-rs/core/src/tasks/mod.rs @@ -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, + record_interrupted_turn_marker: bool, publication: impl Future, ) { 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, + record_interrupted_turn_marker: bool, publication: impl Future, ) { 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; } diff --git a/codex-rs/core/tests/suite/abort_lifecycle.rs b/codex-rs/core/tests/suite/abort_lifecycle.rs index c9d6c6be19..a00582fd71 100644 --- a/codex-rs/core/tests/suite/abort_lifecycle.rs +++ b/codex-rs/core/tests/suite/abort_lifecycle.rs @@ -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(""), + "a planned sleep should not look interrupted to the waking model" ); } if !matches!(operation, AbortOperation::Shutdown) { diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index cd3bf3fc3b..13f8c1c1c0 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -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 `` message to the next model request. The + /// decision is acknowledged before cancellation finishes. #[debug("InterruptIfNoPendingInput {{ turn_id: {turn_id:?} }}")] InterruptIfNoPendingInput { turn_id: String,