Track Guardian reviewer turn and tool analytics (#41023)

## Why

Trusted Guardian reviewer sessions can emit events without an app-server listener, leaving their turns and tool usage absent from analytics.

## What changed

- Translate Guardian session lifecycle and canonical tool item events into analytics notifications while excluding messages, reasoning, and review content.
- Preserve Guardian lineage during prewarming even when inherited client metadata is not yet available, without emitting an incomplete thread initialization event.
- Limit direct session tracking to trusted Guardian children so resumed threads that have an app-server listener do not report duplicate events.

## Testing

- Cover completed, failed, and interrupted Guardian turns, tool counts and timing, lineage, content exclusion, prewarmed lineage, and resumed-thread deduplication.

GitOrigin-RevId: 70ace5f7ed30a7e3b6a9d6097c8b2a6a326dc4f7
This commit is contained in:
marksteinbrick-oai authored and copyberry committed 2026-08-27 02:19:10 +00:00
1 parent 81e180044d
commit 307ce6cda9
10 files changed
+467 -28

No files matched your search

+106
View File
@@ -36,6 +36,7 @@ use crate::facts::TurnTokenUsageFact;
use crate::now_unix_millis;
use crate::reducer::AnalyticsReducer;
use crate::reducer::MAX_PLUGIN_MEASUREMENTS_PER_BATCH;
use crate::reducer::tracked_tool_item_id;
use crate::reducer::valid_plugin_measurement_identifier;
use crate::reducer::valid_plugin_measurement_row;
use codex_app_server_protocol::ClientRequest;
@@ -48,14 +49,24 @@ use codex_app_server_protocol::RequestId;
use codex_app_server_protocol::ServerNotification;
use codex_app_server_protocol::ServerRequest;
use codex_app_server_protocol::ServerResponse;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnCompletedNotification;
use codex_app_server_protocol::TurnError;
use codex_app_server_protocol::TurnItemsView;
use codex_app_server_protocol::TurnStartedNotification;
use codex_app_server_protocol::TurnStatus;
use codex_app_server_protocol::item_event_to_server_notification;
use codex_login::AuthManager;
use codex_login::CodexAuth;
use codex_login::default_client::create_client;
use codex_plugin::PluginId;
use codex_plugin::PluginTelemetryMetadata;
use codex_protocol::ThreadId;
use codex_protocol::items::CollabAgentToolCallItem;
use codex_protocol::items::CollabAgentToolCallStatus;
use codex_protocol::items::TurnItem;
use codex_protocol::protocol::Event;
use codex_protocol::protocol::EventMsg;
use codex_protocol::request_permissions::RequestPermissionsResponse;
use std::collections::HashSet;
use std::path::PathBuf;
@@ -335,6 +346,13 @@ impl AnalyticsEventsClient {
));
}
/// Logs Guardian turn and tool events that bypass the app-server listener.
pub fn track_guardian_session_event(&self, thread_id: ThreadId, event: &Event) {
if let Some(notification) = session_event_to_analytics_notification(thread_id, event) {
self.track_notification(&notification);
}
}
pub fn track_collab_tool_call(
&self,
turn_id: String,
@@ -710,6 +728,94 @@ impl AnalyticsEventsClient {
}
}
fn session_event_to_analytics_notification(
thread_id: ThreadId,
event: &Event,
) -> Option<ServerNotification> {
let notification = match &event.msg {
EventMsg::ItemStarted(started) => item_event_to_server_notification(
event.msg.clone(),
&started.thread_id.to_string(),
&started.turn_id,
),
EventMsg::ItemCompleted(completed) => item_event_to_server_notification(
event.msg.clone(),
&completed.thread_id.to_string(),
&completed.turn_id,
),
EventMsg::TurnStarted(started) => {
ServerNotification::TurnStarted(TurnStartedNotification {
thread_id: thread_id.to_string(),
turn: Turn {
started_at: started.started_at,
..analytics_turn(&started.turn_id, TurnStatus::InProgress)
},
})
}
EventMsg::TurnComplete(completed) => {
let error = completed.error.as_ref().map(|error| TurnError {
message: String::new(),
codex_error_info: error.codex_error_info.clone().map(Into::into),
additional_details: None,
misalignment: None,
});
let status = if error.is_some() {
TurnStatus::Failed
} else {
TurnStatus::Completed
};
ServerNotification::TurnCompleted(TurnCompletedNotification {
thread_id: thread_id.to_string(),
turn: Turn {
error,
started_at: completed.started_at,
completed_at: completed.completed_at,
duration_ms: completed.duration_ms,
..analytics_turn(&completed.turn_id, status)
},
})
}
EventMsg::TurnAborted(aborted) => {
ServerNotification::TurnCompleted(TurnCompletedNotification {
thread_id: thread_id.to_string(),
turn: Turn {
started_at: aborted.started_at,
completed_at: aborted.completed_at,
duration_ms: aborted.duration_ms,
..analytics_turn(
aborted.turn_id.as_deref().unwrap_or(&event.id),
TurnStatus::Interrupted,
)
},
})
}
// Legacy tool events accompany canonical items. Messages, reasoning, and review
// content must not enter the analytics queue.
_ => return None,
};
match &notification {
ServerNotification::ItemStarted(ItemStartedNotification { item, .. })
| ServerNotification::ItemCompleted(ItemCompletedNotification { item, .. }) => {
tracked_tool_item_id(item)?;
}
_ => {}
}
Some(notification)
}
fn analytics_turn(turn_id: &str, status: TurnStatus) -> Turn {
Turn {
id: turn_id.to_string(),
items: Vec::new(),
items_view: TurnItemsView::NotLoaded,
status,
error: None,
started_at: None,
completed_at: None,
duration_ms: None,
}
}
async fn send_track_events(
auth_manager: &AuthManager,
destination: &AnalyticsEventsDestination,