From 90d6594ba17b398e3fdcf873d10d4ba255975355 Mon Sep 17 00:00:00 2001 From: Tom Wiltzius Date: Fri, 5 Jun 2026 09:52:56 -0700 Subject: [PATCH] emit failed lifecycle mutations --- .../src/protocol/lifecycle_projection.rs | 137 +++++++++++------- .../protocol/lifecycle_projection_tests.rs | 41 ++++++ 2 files changed, 127 insertions(+), 51 deletions(-) diff --git a/codex-rs/app-server-protocol/src/protocol/lifecycle_projection.rs b/codex-rs/app-server-protocol/src/protocol/lifecycle_projection.rs index cff2580c3e..7ab8702939 100644 --- a/codex-rs/app-server-protocol/src/protocol/lifecycle_projection.rs +++ b/codex-rs/app-server-protocol/src/protocol/lifecycle_projection.rs @@ -11,7 +11,11 @@ use serde_json::json; mod tests; /// Observes append batches and emits only thread lifecycle mutations. -pub struct LifecycleProjectionObserver; +pub struct LifecycleProjectionObserver { + // TODO(wiltzius): Restore this from persisted lifecycle projection state once stores persist + // lifecycle mutations, instead of reconstructing it from only the current process's appends. + current_turn_id: Option, +} impl Default for LifecycleProjectionObserver { fn default() -> Self { @@ -21,7 +25,9 @@ impl Default for LifecycleProjectionObserver { impl LifecycleProjectionObserver { pub fn new() -> Self { - Self + Self { + current_turn_id: None, + } } pub fn observe_append( @@ -31,9 +37,86 @@ impl LifecycleProjectionObserver { ) -> Vec { source_rollout_items(persisted_rollout_items, projection_source_events) .iter() - .filter_map(lifecycle_mutation) + .filter_map(|rollout_item| self.lifecycle_mutation(rollout_item)) .collect() } + + fn lifecycle_mutation(&mut self, rollout_item: &RolloutItem) -> Option { + let RolloutItem::EventMsg(event) = rollout_item else { + return None; + }; + let (event_type, turn_id, payload) = match event { + EventMsg::TurnStarted(payload) => { + self.current_turn_id = Some(payload.turn_id.clone()); + ( + "turn.started", + Some(payload.turn_id.clone()), + json!({ + "startedAt": payload.started_at, + }), + ) + } + EventMsg::TurnComplete(payload) => { + self.clear_current_turn_if_matches(&payload.turn_id); + ( + "turn.completed", + Some(payload.turn_id.clone()), + json!({ + "completedAt": payload.completed_at, + "durationMs": payload.duration_ms, + }), + ) + } + EventMsg::TurnAborted(payload) => { + let turn_id = payload + .turn_id + .clone() + .or_else(|| self.current_turn_id.clone()); + if let Some(turn_id) = &turn_id { + self.clear_current_turn_if_matches(turn_id); + } + ( + "turn.cancelled", + turn_id, + json!({ + "reason": payload.reason, + "completedAt": payload.completed_at, + "durationMs": payload.duration_ms, + }), + ) + } + EventMsg::Error(payload) if payload.affects_turn_status() => ( + "turn.failed", + Some(self.current_turn_id.clone()?), + json!({ + "message": payload.message, + "codexErrorInfo": payload.codex_error_info, + }), + ), + EventMsg::ThreadRolledBack(payload) => ( + "thread.rolled_back", + None, + json!({ + "numTurns": payload.num_turns, + }), + ), + _ => return None, + }; + Some(ThreadHistoryMutation::Lifecycle(LifecycleMutation { + metadata: mutation_metadata(), + payload: json!({ + "eventType": event_type, + "turnId": turn_id, + "payload": payload, + }), + })) + } + + fn clear_current_turn_if_matches(&mut self, turn_id: &str) { + if self.current_turn_id.as_deref() == Some(turn_id) { + self.current_turn_id = None; + } + } } impl ThreadHistoryProjectionObserver for LifecycleProjectionObserver { @@ -50,54 +133,6 @@ impl ThreadHistoryProjectionObserver for LifecycleProjectionObserver { } } -fn lifecycle_mutation(rollout_item: &RolloutItem) -> Option { - let RolloutItem::EventMsg(event) = rollout_item else { - return None; - }; - let (event_type, turn_id, payload) = match event { - EventMsg::TurnStarted(payload) => ( - "turn.started", - Some(payload.turn_id.clone()), - json!({ - "startedAt": payload.started_at, - }), - ), - EventMsg::TurnComplete(payload) => ( - "turn.completed", - Some(payload.turn_id.clone()), - json!({ - "completedAt": payload.completed_at, - "durationMs": payload.duration_ms, - }), - ), - EventMsg::TurnAborted(payload) => ( - "turn.cancelled", - payload.turn_id.clone(), - json!({ - "reason": payload.reason, - "completedAt": payload.completed_at, - "durationMs": payload.duration_ms, - }), - ), - EventMsg::ThreadRolledBack(payload) => ( - "thread.rolled_back", - None, - json!({ - "numTurns": payload.num_turns, - }), - ), - _ => return None, - }; - Some(ThreadHistoryMutation::Lifecycle(LifecycleMutation { - metadata: mutation_metadata(), - payload: json!({ - "eventType": event_type, - "turnId": turn_id, - "payload": payload, - }), - })) -} - fn mutation_metadata() -> ThreadHistoryMutationMetadata { ThreadHistoryMutationMetadata { schema_version: 1 } } diff --git a/codex-rs/app-server-protocol/src/protocol/lifecycle_projection_tests.rs b/codex-rs/app-server-protocol/src/protocol/lifecycle_projection_tests.rs index e118685da8..ead748e1b5 100644 --- a/codex-rs/app-server-protocol/src/protocol/lifecycle_projection_tests.rs +++ b/codex-rs/app-server-protocol/src/protocol/lifecycle_projection_tests.rs @@ -1,4 +1,5 @@ use super::*; +use codex_protocol::protocol::ErrorEvent; use codex_protocol::protocol::EventMsg; use codex_protocol::protocol::RolloutItem; use codex_protocol::protocol::ThreadRolledBackEvent; @@ -69,6 +70,46 @@ fn emits_lifecycle_mutations_from_lifecycle_events_only() { ); } +#[test] +fn emits_turn_failed_for_turn_affecting_errors() { + let mut observer = LifecycleProjectionObserver::new(); + let persisted_rollout_items = vec![ + RolloutItem::EventMsg(EventMsg::TurnStarted(TurnStartedEvent { + turn_id: "turn-a".into(), + trace_id: None, + started_at: Some(10), + model_context_window: None, + collaboration_mode_kind: Default::default(), + })), + RolloutItem::EventMsg(EventMsg::Error(ErrorEvent { + message: "boom".into(), + codex_error_info: None, + })), + ]; + let mutations = observer.observe_append(&persisted_rollout_items, &[]); + + assert_eq!( + lifecycle_payloads(mutations), + vec![ + json!({ + "eventType": "turn.started", + "turnId": "turn-a", + "payload": { + "startedAt": 10, + }, + }), + json!({ + "eventType": "turn.failed", + "turnId": "turn-a", + "payload": { + "message": "boom", + "codexErrorInfo": null, + }, + }), + ] + ); +} + fn lifecycle_payloads(mutations: Vec) -> Vec { mutations .into_iter()