From 348a54d86ccb2f5b5b4749d947154e3890bece4a Mon Sep 17 00:00:00 2001 From: bromano Date: Fri, 5 Jun 2026 12:25:42 -0700 Subject: [PATCH] Report turn event dispatch timing --- .../analytics/src/analytics_client_tests.rs | 18 +++++++++++++ codex-rs/analytics/src/events.rs | 3 +++ codex-rs/analytics/src/facts.rs | 3 +++ codex-rs/analytics/src/reducer.rs | 6 +++++ codex-rs/core/src/session/mod.rs | 4 +++ codex-rs/core/src/tasks/mod.rs | 14 ++++++++-- codex-rs/core/src/turn_timing.rs | 27 +++++++++++++++++++ codex-rs/core/src/turn_timing_tests.rs | 6 +++++ 8 files changed, 79 insertions(+), 2 deletions(-) diff --git a/codex-rs/analytics/src/analytics_client_tests.rs b/codex-rs/analytics/src/analytics_client_tests.rs index 920e75a7f0..64d5a90c03 100644 --- a/codex-rs/analytics/src/analytics_client_tests.rs +++ b/codex-rs/analytics/src/analytics_client_tests.rs @@ -410,6 +410,9 @@ fn sample_turn_profile() -> TurnProfile { sampling_retry_delay_ms: 50, request_user_input_count: 1, request_user_input_wait_ms: 250, + event_dispatch_count: 3, + event_dispatch_duration_ms: 75, + final_rollout_flush_duration_ms: 25, } } @@ -3339,6 +3342,9 @@ fn turn_event_serializes_expected_shape() { sampling_retry_delay_ms: 50, request_user_input_count: 1, request_user_input_wait_ms: 250, + event_dispatch_count: 3, + event_dispatch_duration_ms: 75, + final_rollout_flush_duration_ms: 25, duration_ms: Some(1234), started_at: Some(455), completed_at: Some(456), @@ -3415,6 +3421,9 @@ fn turn_event_serializes_expected_shape() { "sampling_retry_delay_ms": 50, "request_user_input_count": 1, "request_user_input_wait_ms": 250, + "event_dispatch_count": 3, + "event_dispatch_duration_ms": 75, + "final_rollout_flush_duration_ms": 25, "duration_ms": 1234, "started_at": 455, "completed_at": 456 @@ -3732,6 +3741,15 @@ async fn turn_lifecycle_emits_turn_event() { assert_eq!(payload["event_params"]["started_at"], json!(455)); assert_eq!(payload["event_params"]["completed_at"], json!(456)); assert_eq!(payload["event_params"]["duration_ms"], json!(1234)); + assert_eq!(payload["event_params"]["event_dispatch_count"], json!(3)); + assert_eq!( + payload["event_params"]["event_dispatch_duration_ms"], + json!(75) + ); + assert_eq!( + payload["event_params"]["final_rollout_flush_duration_ms"], + json!(25) + ); assert_eq!(payload["event_params"]["input_tokens"], json!(123)); assert_eq!(payload["event_params"]["cached_input_tokens"], json!(45)); assert_eq!(payload["event_params"]["output_tokens"], json!(140)); diff --git a/codex-rs/analytics/src/events.rs b/codex-rs/analytics/src/events.rs index b2ccf3f7ea..325a76838a 100644 --- a/codex-rs/analytics/src/events.rs +++ b/codex-rs/analytics/src/events.rs @@ -827,6 +827,9 @@ pub(crate) struct CodexTurnEventParams { pub(crate) sampling_retry_delay_ms: u64, pub(crate) request_user_input_count: u32, pub(crate) request_user_input_wait_ms: u64, + pub(crate) event_dispatch_count: u32, + pub(crate) event_dispatch_duration_ms: u64, + pub(crate) final_rollout_flush_duration_ms: u64, pub(crate) duration_ms: Option, pub(crate) started_at: Option, pub(crate) completed_at: Option, diff --git a/codex-rs/analytics/src/facts.rs b/codex-rs/analytics/src/facts.rs index db4a1174e7..3314abbcaa 100644 --- a/codex-rs/analytics/src/facts.rs +++ b/codex-rs/analytics/src/facts.rs @@ -116,6 +116,9 @@ pub struct TurnProfile { pub sampling_retry_delay_ms: u64, pub request_user_input_count: u32, pub request_user_input_wait_ms: u64, + pub event_dispatch_count: u32, + pub event_dispatch_duration_ms: u64, + pub final_rollout_flush_duration_ms: u64, } #[derive(Clone)] diff --git a/codex-rs/analytics/src/reducer.rs b/codex-rs/analytics/src/reducer.rs index dea94d5ee8..2854d6852a 100644 --- a/codex-rs/analytics/src/reducer.rs +++ b/codex-rs/analytics/src/reducer.rs @@ -2436,6 +2436,9 @@ fn codex_turn_event_params( sampling_retry_delay_ms, request_user_input_count, request_user_input_wait_ms, + event_dispatch_count, + event_dispatch_duration_ms, + final_rollout_flush_duration_ms, } = profile; let token_usage = turn_state.token_usage.clone(); let codex_error = turn_state.codex_error.as_ref(); @@ -2509,6 +2512,9 @@ fn codex_turn_event_params( sampling_retry_delay_ms, request_user_input_count, request_user_input_wait_ms, + event_dispatch_count, + event_dispatch_duration_ms, + final_rollout_flush_duration_ms, duration_ms: completed.duration_ms, started_at, completed_at: Some(completed.completed_at), diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 2658501c32..dfcf682c46 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -1658,6 +1658,7 @@ impl Session { /// Persist the event to rollout and send it to clients. pub(crate) async fn send_event(&self, turn_context: &TurnContext, msg: EventMsg) { + let dispatch_started_at = Instant::now(); let legacy_source = msg.clone(); self.services .rollout_thread_trace @@ -1685,6 +1686,9 @@ impl Session { }; self.send_event_raw(legacy_event).await; } + turn_context + .turn_timing_state + .record_event_dispatch(dispatch_started_at.elapsed()); } /// Forwards terminal turn events from spawned MultiAgentV2 children to their direct parent. diff --git a/codex-rs/core/src/tasks/mod.rs b/codex-rs/core/src/tasks/mod.rs index 70466c9c2f..0e755bd393 100644 --- a/codex-rs/core/src/tasks/mod.rs +++ b/codex-rs/core/src/tasks/mod.rs @@ -407,7 +407,12 @@ impl Session { ) .await; let sess = session_ctx.clone_session(); - if let Err(err) = sess.flush_rollout().await { + let flush_started_at = Instant::now(); + let flush_result = sess.flush_rollout().await; + ctx_for_finish + .turn_timing_state + .record_final_rollout_flush(flush_started_at.elapsed()); + if let Err(err) = flush_result { warn!("failed to flush rollout before completing turn: {err}"); sess.send_event( ctx_for_finish.as_ref(), @@ -865,7 +870,12 @@ impl Session { .await; // Ensure the marker is durably visible before emitting TurnAborted: some clients // synchronously re-read the rollout on receipt of the abort event. - if let Err(err) = self.flush_rollout().await { + let flush_started_at = Instant::now(); + let flush_result = self.flush_rollout().await; + task.turn_context + .turn_timing_state + .record_final_rollout_flush(flush_started_at.elapsed()); + if let Err(err) = flush_result { warn!("failed to flush interrupted-turn marker before emitting TurnAborted: {err}"); } } diff --git a/codex-rs/core/src/turn_timing.rs b/codex-rs/core/src/turn_timing.rs index 54ade652fd..1de0e4a9b4 100644 --- a/codex-rs/core/src/turn_timing.rs +++ b/codex-rs/core/src/turn_timing.rs @@ -69,6 +69,9 @@ struct TurnProfileState { sampling_retry_delay: Duration, request_user_input_count: u32, request_user_input_wait: Duration, + event_dispatch_count: u32, + event_dispatch_duration: Duration, + final_rollout_flush_duration: Duration, completed_profile: Option, } @@ -139,6 +142,14 @@ impl TurnTimingState { .record_request_user_input_wait(duration); } + pub(crate) fn record_event_dispatch(&self, duration: Duration) { + self.profile_state().record_event_dispatch(duration); + } + + pub(crate) fn record_final_rollout_flush(&self, duration: Duration) { + self.profile_state().record_final_rollout_flush(duration); + } + pub(crate) fn begin_tool_blocking(self: &Arc) -> TurnProfileTimingGuard { let active = self.profile_state().begin_tool_blocking(Instant::now()); TurnProfileTimingGuard { @@ -239,6 +250,19 @@ impl TurnProfileState { } } + fn record_event_dispatch(&mut self, duration: Duration) { + if self.completed_profile.is_none() && self.started_at.is_some() { + self.event_dispatch_count = self.event_dispatch_count.saturating_add(1); + self.event_dispatch_duration = self.event_dispatch_duration.saturating_add(duration); + } + } + + fn record_final_rollout_flush(&mut self, duration: Duration) { + if self.completed_profile.is_none() && self.started_at.is_some() { + self.final_rollout_flush_duration = duration; + } + } + fn begin_tool_blocking(&mut self, now: Instant) -> bool { if self.completed_profile.is_some() || self.started_at.is_none() @@ -296,6 +320,9 @@ impl TurnProfileState { sampling_retry_delay_ms: duration_to_u64_ms(self.sampling_retry_delay), request_user_input_count: self.request_user_input_count, request_user_input_wait_ms: duration_to_u64_ms(self.request_user_input_wait), + event_dispatch_count: self.event_dispatch_count, + event_dispatch_duration_ms: duration_to_u64_ms(self.event_dispatch_duration), + final_rollout_flush_duration_ms: duration_to_u64_ms(self.final_rollout_flush_duration), }; let total_ms = self .started_at diff --git a/codex-rs/core/src/turn_timing_tests.rs b/codex-rs/core/src/turn_timing_tests.rs index bad1920a0f..f97389ad6b 100644 --- a/codex-rs/core/src/turn_timing_tests.rs +++ b/codex-rs/core/src/turn_timing_tests.rs @@ -169,6 +169,9 @@ fn turn_profile_breaks_down_sampling_blocking_and_retry_overhead() { ); state.record_sampling_retry(Duration::from_millis(50)); state.record_request_user_input_wait(Duration::from_millis(250)); + state.record_event_dispatch(Duration::from_millis(75)); + state.record_event_dispatch(Duration::from_millis(25)); + state.record_final_rollout_flush(Duration::from_millis(40)); let _ = state.begin_sampling(started_at + Duration::from_millis(1_000)); state.end_phase( started_at + Duration::from_millis(1_200), @@ -188,6 +191,9 @@ fn turn_profile_breaks_down_sampling_blocking_and_retry_overhead() { sampling_retry_delay_ms: 50, request_user_input_count: 1, request_user_input_wait_ms: 250, + event_dispatch_count: 2, + event_dispatch_duration_ms: 100, + final_rollout_flush_duration_ms: 40, } ); }