Skip to main content

phi_telemetry/
types.rs

1//! Structured metrics types for Agent observability.
2//!
3//! These types were moved from agent-base to phi-telemetry so that agent-base
4//! remains a pure runtime kernel with no knowledge of metrics.
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::collections::HashMap;
9
10// ── Outcome enums ──
11
12/// Outcome of a single turn (one LLM interaction).
13#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(rename_all = "snake_case")]
15pub enum TurnOutcome {
16    /// Turn completed successfully (text response, no tool calls).
17    Completed,
18    /// Turn ended with tool calls (agent will loop back for another turn).
19    ToolCalls,
20    /// Turn ended with an error.
21    Error,
22    /// Turn hit the max-turns safety limit.
23    MaxTurns,
24    /// User cancelled this turn.
25    Cancelled,
26}
27
28/// Outcome of a session (the entire conversation).
29#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "snake_case")]
31pub enum SessionOutcome {
32    /// Session completed normally.
33    Completed,
34    /// Session ended with an unrecoverable error.
35    Failed,
36    /// User cancelled the session.
37    Cancelled,
38    /// Max turns exceeded (safety limit).
39    MaxTurns,
40}
41
42// ── TurnMetrics ──
43
44/// Per-turn metrics — one per LLM interaction.
45#[derive(Clone, Debug, Serialize, Deserialize)]
46pub struct TurnMetrics {
47    // Timing
48    pub turn_number: u32,
49    pub started_at: String, // ISO8601
50    pub duration_ms: u64,
51
52    // Latency breakdown
53    pub time_to_first_token_ms: u64,
54    pub llm_duration_ms: u64,
55    pub tool_duration_ms: u64,
56
57    // LLM
58    pub llm_calls: u32,
59    pub input_tokens: u64,
60    pub output_tokens: u64,
61    pub model: String,
62
63    // Tool
64    pub tool_call_count: u32,
65    pub tools_used: Vec<String>,
66    pub tool_success: u32,
67    pub tool_failed: u32,
68
69    // Result
70    pub outcome: TurnOutcome,
71    pub text_length: u64,
72    pub error_message: Option<String>,
73    pub has_thinking: bool,
74
75    // Meta events
76    #[serde(default)]
77    pub plan_updates: u32,
78    #[serde(default)]
79    pub approval_count: u32,
80
81    // User input (truncated to 80 chars)
82    pub user_input: String,
83
84    // Business extension
85    #[serde(default)]
86    pub custom: Value,
87}
88
89impl TurnMetrics {
90    /// Create a new TurnMetrics with defaults (custom = {}).
91    pub fn new(
92        turn_number: u32,
93        started_at: String,
94        duration_ms: u64,
95        model: String,
96        user_input: String,
97        outcome: TurnOutcome,
98    ) -> Self {
99        Self {
100            turn_number,
101            started_at,
102            duration_ms,
103            time_to_first_token_ms: 0,
104            llm_duration_ms: 0,
105            tool_duration_ms: 0,
106            llm_calls: 1,
107            input_tokens: 0,
108            output_tokens: 0,
109            model,
110            tool_call_count: 0,
111            tools_used: Vec::new(),
112            tool_success: 0,
113            tool_failed: 0,
114            outcome,
115            text_length: 0,
116            error_message: None,
117            has_thinking: false,
118            plan_updates: 0,
119            approval_count: 0,
120            user_input: truncate_str(&user_input, 80),
121            custom: Value::Object(serde_json::Map::new()),
122        }
123    }
124}
125
126// ── SessionMetrics ──
127
128/// Accumulated session metrics. Written incrementally to `session_metrics.json`
129/// at the end of each turn.
130#[derive(Clone, Debug, Serialize, Deserialize)]
131pub struct SessionMetrics {
132    // Identity
133    pub session_id: String,
134    #[serde(default)]
135    pub node_id: String,
136    pub created_at: String,
137
138    // LLM summary
139    pub model: String,
140    pub total_input_tokens: u64,
141    pub total_output_tokens: u64,
142    pub estimated_cost: f64,
143
144    // Characters (always available, even without API token support)
145    #[serde(default)]
146    pub total_chars: u64,
147
148    // Tool summary
149    pub total_tool_calls: u32,
150    pub tool_breakdown: HashMap<String, u32>,
151    pub tool_fail_rate: f64,
152    /// Running total of failed tool calls.
153    #[serde(default)]
154    pub total_failed: u32,
155
156    // Timing
157    pub total_duration_ms: u64,
158    pub total_llm_ms: u64,
159    pub total_tool_ms: u64,
160    pub total_turns: u32,
161    pub avg_turn_ms: u64,
162    pub p50_turn_ms: u64,
163    pub p95_turn_ms: u64,
164    pub p99_turn_ms: u64,
165
166    // Outcome
167    pub outcome: SessionOutcome,
168    pub error_count: u32,
169
170    // Meta events
171    #[serde(default)]
172    pub total_plan_updates: u32,
173    #[serde(default)]
174    pub total_approvals: u32,
175
176    // Business extension
177    #[serde(default)]
178    pub custom: Value,
179
180    // Multi-agent reservation (Phase 1: always None / "default")
181    #[serde(default, skip_serializing_if = "Option::is_none")]
182    pub parent_session_id: Option<String>,
183    #[serde(default = "default_session_type")]
184    pub session_type: String,
185
186    // Per-turn details
187    pub turns: Vec<TurnMetrics>,
188}
189
190fn default_session_type() -> String {
191    "default".to_string()
192}
193
194impl SessionMetrics {
195    /// Create a new session metrics accumulator (empty turns).
196    pub fn new(session_id: String, node_id: String, model: String) -> Self {
197        Self {
198            session_id,
199            node_id,
200            created_at: chrono::Utc::now().to_rfc3339(),
201            model,
202            total_input_tokens: 0,
203            total_output_tokens: 0,
204            estimated_cost: 0.0,
205            total_chars: 0,
206            total_tool_calls: 0,
207            tool_breakdown: HashMap::new(),
208            tool_fail_rate: 0.0,
209            total_failed: 0,
210            total_duration_ms: 0,
211            total_llm_ms: 0,
212            total_tool_ms: 0,
213            total_turns: 0,
214            avg_turn_ms: 0,
215            p50_turn_ms: 0,
216            p95_turn_ms: 0,
217            p99_turn_ms: 0,
218            outcome: SessionOutcome::Completed,
219            error_count: 0,
220            total_plan_updates: 0,
221            total_approvals: 0,
222            custom: Value::Object(serde_json::Map::new()),
223            parent_session_id: None,
224            session_type: "default".to_string(),
225            turns: Vec::new(),
226        }
227    }
228
229    /// Append a turn and recompute session-level aggregates.
230    pub fn append_turn(&mut self, turn: TurnMetrics) {
231        self.total_turns += 1;
232        self.total_input_tokens += turn.input_tokens;
233        self.total_output_tokens += turn.output_tokens;
234        self.total_chars += turn.text_length;
235        self.total_duration_ms += turn.duration_ms;
236        self.total_llm_ms += turn.llm_duration_ms;
237        self.total_tool_ms += turn.tool_duration_ms;
238        self.total_tool_calls += turn.tool_call_count;
239
240        // Tool breakdown
241        for tool_name in &turn.tools_used {
242            *self.tool_breakdown.entry(tool_name.clone()).or_insert(0) += 1;
243        }
244
245        // Tool fail rate (incremental — O(1) per turn)
246        self.total_failed += turn.tool_failed;
247        if self.total_tool_calls > 0 {
248            self.tool_fail_rate = self.total_failed as f64 / self.total_tool_calls as f64;
249        }
250
251        // Error count
252        if matches!(turn.outcome, TurnOutcome::Error) {
253            self.error_count += 1;
254        }
255
256        // Meta events
257        self.total_plan_updates += turn.plan_updates;
258        self.total_approvals += turn.approval_count;
259
260        // Average turn duration
261        self.avg_turn_ms = self.total_duration_ms / self.total_turns as u64;
262
263        // Update model to the most frequently used one
264        {
265            let mut model_counts: HashMap<String, u32> = HashMap::new();
266            for t in &self.turns {
267                if !t.model.is_empty() {
268                    *model_counts.entry(t.model.clone()).or_insert(0) += 1;
269                }
270            }
271            if !turn.model.is_empty() {
272                *model_counts.entry(turn.model.clone()).or_insert(0) += 1;
273            }
274            if let Some(top_model) = model_counts
275                .into_iter()
276                .max_by_key(|(_, count)| *count)
277                .map(|(m, _)| m)
278            {
279                self.model = top_model;
280            }
281        }
282
283        // Percentiles
284        self.turns.push(turn);
285        self.recompute_percentiles();
286    }
287
288    /// Recompute P50/P95/P99 from stored turn durations.
289    fn recompute_percentiles(&mut self) {
290        if self.turns.is_empty() {
291            self.p50_turn_ms = 0;
292            self.p95_turn_ms = 0;
293            self.p99_turn_ms = 0;
294            return;
295        }
296
297        let mut durations: Vec<u64> = self.turns.iter().map(|t| t.duration_ms).collect();
298        durations.sort_unstable();
299
300        self.p50_turn_ms = percentile_from_sorted(&durations, 50.0);
301        self.p95_turn_ms = percentile_from_sorted(&durations, 95.0);
302        self.p99_turn_ms = percentile_from_sorted(&durations, 99.0);
303    }
304
305    /// Finalize the session — recompute percentiles and set outcome.
306    pub fn finalize(&mut self, outcome: SessionOutcome) {
307        self.outcome = outcome;
308        self.recompute_percentiles();
309    }
310}
311
312// ── Summary for CLI listing ──
313
314/// Lightweight summary returned by `list_all()`.
315#[derive(Clone, Debug, Serialize, Deserialize)]
316pub struct SessionSummary {
317    pub session_id: String,
318    pub node_id: String,
319    pub created_at: String,
320    pub model: String,
321    pub total_turns: u32,
322    pub total_chars: u64,
323    pub outcome: SessionOutcome,
324    /// Product name from custom field, if set (e.g. "phi-bard").
325    pub product: Option<String>,
326}
327
328// ── Helpers ──
329
330/// Compute the `p`-th percentile from an already-sorted slice of values.
331pub(crate) fn percentile_from_sorted(sorted: &[u64], p: f64) -> u64 {
332    if sorted.is_empty() {
333        return 0;
334    }
335    let n = sorted.len() as f64;
336    let idx = ((p / 100.0) * (n - 1.0)).round() as usize;
337    sorted[idx.min(sorted.len() - 1)]
338}
339
340/// Truncate a string to `max_chars` characters, appending "..." if truncated.
341pub(crate) fn truncate_str(s: &str, max_chars: usize) -> String {
342    if s.chars().count() > max_chars {
343        let truncated: String = s.chars().take(max_chars).collect();
344        format!("{}...", truncated)
345    } else {
346        s.to_string()
347    }
348}
349
350// ── Conversion from agent-base ──
351
352/// Convert an agent-base `RunOutcome` to a turn-level `TurnOutcome`.
353/// Checks `tools_used` to distinguish `ToolCalls` from `Completed`.
354pub fn run_outcome_to_turn_outcome(
355    outcome: &agent_base::RunOutcome,
356    tools_used: &[String],
357) -> TurnOutcome {
358    match outcome {
359        agent_base::RunOutcome::Completed => {
360            if tools_used.is_empty() {
361                TurnOutcome::Completed
362            } else {
363                TurnOutcome::ToolCalls
364            }
365        }
366        agent_base::RunOutcome::Failed { .. } => TurnOutcome::Error,
367        agent_base::RunOutcome::Cancelled => TurnOutcome::Cancelled,
368        agent_base::RunOutcome::MaxTurnsExceeded { .. } => TurnOutcome::MaxTurns,
369    }
370}
371
372/// Convert agent-base `RunOutcome` to `SessionOutcome`.
373pub fn run_outcome_to_session_outcome(outcome: &agent_base::RunOutcome) -> SessionOutcome {
374    match outcome {
375        agent_base::RunOutcome::Completed => SessionOutcome::Completed,
376        agent_base::RunOutcome::Failed { .. } => SessionOutcome::Failed,
377        agent_base::RunOutcome::Cancelled => SessionOutcome::Cancelled,
378        agent_base::RunOutcome::MaxTurnsExceeded { .. } => SessionOutcome::MaxTurns,
379    }
380}
381
382#[cfg(test)]
383mod tests {
384    use super::*;
385
386    #[test]
387    fn test_truncate_str() {
388        assert_eq!(truncate_str("hello", 10), "hello");
389        assert_eq!(
390            truncate_str("hello world this is long", 10),
391            "hello worl..."
392        );
393        assert_eq!(truncate_str("你好世界测试文本", 4), "你好世界...");
394    }
395
396    #[test]
397    fn test_percentile_from_sorted() {
398        let data = vec![10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
399        assert_eq!(percentile_from_sorted(&data, 50.0), 60);
400        assert_eq!(percentile_from_sorted(&data, 95.0), 100);
401        assert_eq!(percentile_from_sorted(&data, 0.0), 10);
402        assert_eq!(percentile_from_sorted(&data, 100.0), 100);
403    }
404
405    #[test]
406    fn test_percentile_empty() {
407        assert_eq!(percentile_from_sorted(&[], 50.0), 0);
408    }
409
410    #[test]
411    fn test_session_metrics_new() {
412        let m = SessionMetrics::new(
413            "20260729_test".to_string(),
414            "".to_string(),
415            "claude-sonnet".to_string(),
416        );
417        assert_eq!(m.session_id, "20260729_test");
418        assert_eq!(m.node_id, "");
419        assert_eq!(m.model, "claude-sonnet");
420        assert_eq!(m.total_turns, 0);
421        assert_eq!(m.parent_session_id, None);
422        assert_eq!(m.session_type, "default");
423        assert!(m.turns.is_empty());
424    }
425
426    #[test]
427    fn test_session_metrics_append_turn() {
428        let mut m = SessionMetrics::new(
429            "test".to_string(),
430            "".to_string(),
431            "claude-sonnet".to_string(),
432        );
433        let turn = TurnMetrics {
434            turn_number: 1,
435            started_at: "2026-07-29T00:00:00Z".to_string(),
436            duration_ms: 1000,
437            time_to_first_token_ms: 200,
438            llm_duration_ms: 800,
439            tool_duration_ms: 100,
440            llm_calls: 1,
441            input_tokens: 500,
442            output_tokens: 300,
443            model: "claude-sonnet".to_string(),
444            tool_call_count: 1,
445            tools_used: vec!["shell".to_string()],
446            tool_success: 1,
447            tool_failed: 0,
448            outcome: TurnOutcome::ToolCalls,
449            text_length: 200,
450            error_message: None,
451            has_thinking: true,
452            plan_updates: 0,
453            approval_count: 0,
454            user_input: "test input".to_string(),
455            custom: Value::Object(serde_json::Map::new()),
456        };
457        m.append_turn(turn);
458        assert_eq!(m.total_turns, 1);
459        assert_eq!(m.total_input_tokens, 500);
460        assert_eq!(m.total_output_tokens, 300);
461        assert_eq!(m.total_duration_ms, 1000);
462        assert_eq!(m.total_tool_calls, 1);
463        assert_eq!(m.tool_breakdown.get("shell"), Some(&1));
464        assert_eq!(m.tool_fail_rate, 0.0);
465        assert_eq!(m.avg_turn_ms, 1000);
466        assert_eq!(m.p50_turn_ms, 1000);
467    }
468
469    #[test]
470    fn test_session_metrics_append_multiple_turns() {
471        let mut m = SessionMetrics::new(
472            "test".to_string(),
473            "".to_string(),
474            "claude-sonnet".to_string(),
475        );
476        for i in 1..=5 {
477            let turn = TurnMetrics {
478                turn_number: i,
479                started_at: "2026-07-29T00:00:00Z".to_string(),
480                duration_ms: i as u64 * 1000,
481                time_to_first_token_ms: 200,
482                llm_duration_ms: 800,
483                tool_duration_ms: 100,
484                llm_calls: 1,
485                input_tokens: 500,
486                output_tokens: 300,
487                model: "claude-sonnet".to_string(),
488                tool_call_count: 1,
489                tools_used: vec!["shell".to_string()],
490                tool_success: 1,
491                tool_failed: 0,
492                outcome: TurnOutcome::ToolCalls,
493                text_length: 200,
494                error_message: None,
495                has_thinking: true,
496                plan_updates: 0,
497                approval_count: 0,
498                user_input: "test".to_string(),
499                custom: Value::Object(serde_json::Map::new()),
500            };
501            m.append_turn(turn);
502        }
503        assert_eq!(m.total_turns, 5);
504        assert_eq!(m.total_duration_ms, 15000);
505        assert_eq!(m.avg_turn_ms, 3000);
506        assert_eq!(m.p50_turn_ms, 3000);
507    }
508
509    #[test]
510    fn test_session_metrics_finalize() {
511        let mut m = SessionMetrics::new(
512            "test".to_string(),
513            "".to_string(),
514            "claude-sonnet".to_string(),
515        );
516        m.finalize(SessionOutcome::Completed);
517        assert_eq!(m.outcome, SessionOutcome::Completed);
518    }
519
520    #[test]
521    fn test_session_metrics_json_roundtrip() {
522        let m = SessionMetrics::new(
523            "20260729_test".to_string(),
524            "node-1".to_string(),
525            "claude-sonnet".to_string(),
526        );
527        let json = serde_json::to_string(&m).unwrap();
528        let m2: SessionMetrics = serde_json::from_str(&json).unwrap();
529        assert_eq!(m2.session_id, "20260729_test");
530        assert_eq!(m2.node_id, "node-1");
531        assert_eq!(m2.parent_session_id, None);
532        assert_eq!(m2.session_type, "default");
533    }
534
535    #[test]
536    fn test_session_metrics_backward_compat() {
537        let old_json = r#"{
538            "session_id": "test",
539            "node_id": "",
540            "created_at": "2026-07-29T00:00:00Z",
541            "model": "claude-sonnet",
542            "total_input_tokens": 0,
543            "total_output_tokens": 0,
544            "estimated_cost": 0.0,
545            "total_chars": 0,
546            "total_tool_calls": 0,
547            "tool_breakdown": {},
548            "tool_fail_rate": 0.0,
549            "total_duration_ms": 0,
550            "total_llm_ms": 0,
551            "total_tool_ms": 0,
552            "total_turns": 0,
553            "avg_turn_ms": 0,
554            "p50_turn_ms": 0,
555            "p95_turn_ms": 0,
556            "p99_turn_ms": 0,
557            "outcome": "completed",
558            "error_count": 0,
559            "custom": {},
560            "turns": []
561        }"#;
562        let m: SessionMetrics = serde_json::from_str(old_json).unwrap();
563        assert_eq!(m.parent_session_id, None);
564        assert_eq!(m.session_type, "default");
565    }
566
567    #[test]
568    fn test_turn_metrics_custom_default() {
569        let turn = TurnMetrics::new(
570            1,
571            "2026-07-29T00:00:00Z".to_string(),
572            1000,
573            "claude-sonnet".to_string(),
574            "hello world".to_string(),
575            TurnOutcome::Completed,
576        );
577        assert_eq!(turn.custom, Value::Object(serde_json::Map::new()));
578        assert_eq!(turn.user_input, "hello world");
579        let turn_long = TurnMetrics::new(
580            1,
581            "2026-07-29T00:00:00Z".to_string(),
582            1000,
583            "claude-sonnet".to_string(),
584            "this is a very long user input that should definitely be truncated at eighty characters because that's the max we allow for storage".to_string(),
585            TurnOutcome::Completed,
586        );
587        assert!(turn_long.user_input.chars().count() <= 83);
588        assert!(turn_long.user_input.ends_with("..."));
589    }
590
591    #[test]
592    fn test_run_outcome_conversion() {
593        use agent_base::RunOutcome;
594
595        // Completed with no tools → TurnOutcome::Completed
596        assert_eq!(
597            run_outcome_to_turn_outcome(&RunOutcome::Completed, &[]),
598            TurnOutcome::Completed
599        );
600
601        // Completed with tools → TurnOutcome::ToolCalls
602        assert_eq!(
603            run_outcome_to_turn_outcome(&RunOutcome::Completed, &["shell".to_string()]),
604            TurnOutcome::ToolCalls
605        );
606
607        // Failed → Error
608        assert_eq!(
609            run_outcome_to_turn_outcome(
610                &RunOutcome::Failed {
611                    error: "oops".to_string()
612                },
613                &[]
614            ),
615            TurnOutcome::Error
616        );
617
618        // Cancelled → Cancelled
619        assert_eq!(
620            run_outcome_to_turn_outcome(&RunOutcome::Cancelled, &[]),
621            TurnOutcome::Cancelled
622        );
623    }
624}