Files
codex/codex-rs/analytics/src/guardian_v2.rs
T
marksteinbrick-oai 9c60cb0a27 Include classifier turn IDs in Guardian v2 analytics (#62125)
## Why

Guardian v2 classification events need the classifier's turn ID to
correlate outcomes with the corresponding Responses requests.

## What changed

Assign one turn ID per classification and reuse it across request retries.
Once request preparation succeeds, include the same ID from
`client_metadata.turn_id` as `classifier_turn_id` in the classification event.

## Testing

Extend the scoped tool approval integration test to check that the event's
classifier turn ID matches the request metadata.

GitOrigin-RevId: b3ed4bea12cdc06d0b742775f34f296e9c4de376
2026-11-17 23:15:04 +00:00

53 lines
1.6 KiB
Rust

//! Guardian V2 classifier and fast-decision facts, enriched by the existing reducer.
//! Payloads contain attribution and bounded outcomes, never prompts or tool arguments.
use crate::events::CodexAppServerClientMetadata;
use crate::events::CodexRuntimeMetadata;
use codex_protocol::protocol::ThreadSource;
use serde::Serialize;
#[derive(Serialize)]
pub struct GuardianV2Event {
pub thread_id: String,
pub turn_id: String,
pub item_id: Option<String>,
pub model: Option<String>,
pub occurred_at_ms: u64,
#[serde(flatten)]
pub kind: GuardianV2EventKind,
}
#[derive(Serialize)]
#[serde(untagged)]
pub enum GuardianV2EventKind {
Classification {
guardian_context_mode: &'static str,
outcome: &'static str,
risk_level: Option<&'static str>,
duration_ms: u64,
/// Responses `client_metadata.turn_id` reserved for this classifier, shared across retries.
classifier_turn_id: Option<String>,
},
FastDecision {
decision: &'static str,
},
}
#[derive(Serialize)]
pub(crate) struct GuardianV2EventRequest {
pub(crate) event_type: &'static str,
pub(crate) event_params: GuardianV2EventParams,
}
#[derive(Serialize)]
pub(crate) struct GuardianV2EventParams {
pub(crate) session_id: String,
pub(crate) app_server_client: CodexAppServerClientMetadata,
pub(crate) runtime: CodexRuntimeMetadata,
pub(crate) thread_source: Option<ThreadSource>,
pub(crate) subagent_source: Option<String>,
pub(crate) parent_thread_id: Option<String>,
#[serde(flatten)]
pub(crate) guardian_v2: GuardianV2Event,
}