mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
emit failed lifecycle mutations
This commit is contained in:
@@ -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<String>,
|
||||
}
|
||||
|
||||
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<ThreadHistoryMutation> {
|
||||
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<ThreadHistoryMutation> {
|
||||
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<ThreadHistoryMutation> {
|
||||
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 }
|
||||
}
|
||||
|
||||
@@ -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<ThreadHistoryMutation>) -> Vec<serde_json::Value> {
|
||||
mutations
|
||||
.into_iter()
|
||||
|
||||
Reference in New Issue
Block a user