From e286476f3f5baaaf7c1ec1c2e20c7eb26ca594e2 Mon Sep 17 00:00:00 2001 From: Channing Conger Date: Sat, 20 Jun 2026 00:22:40 +0000 Subject: [PATCH] code-mode: claim buffered completion once --- codex-rs/code-mode/src/cell_actor/tests.rs | 28 ++++++++++++++++++++++ codex-rs/code-mode/src/cell_actor/types.rs | 13 +++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/codex-rs/code-mode/src/cell_actor/tests.rs b/codex-rs/code-mode/src/cell_actor/tests.rs index 54c5daff7f..21e8f4e4e2 100644 --- a/codex-rs/code-mode/src/cell_actor/tests.rs +++ b/codex-rs/code-mode/src/cell_actor/tests.rs @@ -611,6 +611,34 @@ fn failed_completion_delivery_rebuffers_the_event() { assert_eq!(response_rx.try_recv(), Ok(Ok(event))); } +#[tokio::test] +async fn only_the_first_termination_claims_a_buffered_completion() { + let session_shutdown_token = CancellationToken::new(); + let cell_state = CellState::new(session_shutdown_token.child_token()); + let completion = CellEvent::Completed { + content_items: Vec::new(), + error_text: None, + }; + assert!(cell_state.commit_completion(completion.clone(), || {})); + assert!(matches!( + cell_state.deliver_completion(/*response_tx*/ None), + CompletionDelivery::Buffered + )); + + let first_termination = cell_state.request_termination(); + assert_eq!( + cell_state.request_termination().await, + Err(CellError::AlreadyTerminating) + ); + assert_eq!(first_termination.await, Ok(completion.clone())); + assert_eq!( + cell_state.finish_termination(CellEvent::Terminated { + content_items: Vec::new(), + }), + Some(completion) + ); +} + #[tokio::test] async fn session_shutdown_rejects_observation_before_the_actor_closes() { let session_shutdown_token = CancellationToken::new(); diff --git a/codex-rs/code-mode/src/cell_actor/types.rs b/codex-rs/code-mode/src/cell_actor/types.rs index 2f1bdcaaf0..c0e78ca0c2 100644 --- a/codex-rs/code-mode/src/cell_actor/types.rs +++ b/codex-rs/code-mode/src/cell_actor/types.rs @@ -115,6 +115,7 @@ enum CellPhase { response_tx: oneshot::Sender>, }, Completed(CellEvent), + CompletionClaimed(CellEvent), Tombstone, } @@ -167,10 +168,14 @@ impl CellState { Box::pin(async { Err(CellError::AlreadyTerminating) }) } CellPhase::Completed(event) => { - *phase = CellPhase::Completed(event.clone()); + *phase = CellPhase::CompletionClaimed(event.clone()); self.cancellation_token.cancel(); ready_event(event) } + CellPhase::CompletionClaimed(event) => { + *phase = CellPhase::CompletionClaimed(event); + Box::pin(async { Err(CellError::AlreadyTerminating) }) + } CellPhase::Tombstone => closed_event(), } } @@ -257,6 +262,11 @@ impl CellState { let _ = response_tx.send(Err(CellError::Closed)); ObservationDelivery::Closed } + CellPhase::CompletionClaimed(event) => { + *phase = CellPhase::CompletionClaimed(event); + let _ = response_tx.send(Err(CellError::Closed)); + ObservationDelivery::Closed + } CellPhase::Tombstone => { let _ = response_tx.send(Err(CellError::Closed)); ObservationDelivery::Closed @@ -276,6 +286,7 @@ impl CellState { Some(event) } CellPhase::Completed(completed_event) => Some(completed_event), + CellPhase::CompletionClaimed(completed_event) => Some(completed_event), CellPhase::Tombstone => None, }; self.cancellation_token.cancel();