axon-lang 2.11.0

AXON — the formal cognitive language: a deterministic, proof-carrying AI runtime. Native Rust lexer/parser/type-checker/IR generator (re-exported from axon-frontend) plus the runtime: typed channels (π-calculus mobility, capability extrusion), algebraic effects via Free Monad CPS handlers, lease kernel + reconcile loop, the Epistemic Security Kernel, Trust Types, Proof-Carrying Code (independently verifiable proof objects), and the closed-catalog extension mechanism. Crate publishes as `axon-lang`; library import is `use axon::*` so existing call sites keep working unchanged.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Trace Analytics — aggregate statistics across multiple execution traces.
//!
//! Loads one or more `.trace.json` files and computes:
//!   - Latency percentiles (p50, p95, p99, mean, min, max)
//!   - Token usage (total, mean, per-unit, per-step)
//!   - Anchor breach rate and top breached anchors
//!   - Error rate and retry rate
//!   - Step frequency distribution
//!
//! Usage:
//!   axon stats trace1.json trace2.json ...   — aggregate stats
//!   axon stats *.trace.json --json           — structured JSON output
//!
//! Exit codes:
//!   0 — stats computed successfully
//!   2 — I/O or parse error

use std::collections::HashMap;
use std::io::IsTerminal;

use crate::replay;

// ── Analytics structures ────────────────────────────────────────────────

/// Aggregate analytics across one or more traces.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TraceAnalytics {
    pub trace_count: usize,
    pub latency: LatencyStats,
    pub tokens: TokenStats,
    pub anchors: AnchorStats,
    pub errors: ErrorStats,
    pub steps: StepFrequency,
}

/// Latency statistics with percentiles.
#[derive(Debug, Clone, serde::Serialize)]
pub struct LatencyStats {
    pub unit_count: usize,
    pub p50_ms: u64,
    pub p95_ms: u64,
    pub p99_ms: u64,
    pub mean_ms: u64,
    pub min_ms: u64,
    pub max_ms: u64,
}

/// Token usage statistics.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TokenStats {
    pub total_input: u64,
    pub total_output: u64,
    pub total: u64,
    pub mean_input_per_unit: u64,
    pub mean_output_per_unit: u64,
    pub mean_total_per_unit: u64,
    pub unit_count: usize,
}

/// Anchor pass/breach statistics.
#[derive(Debug, Clone, serde::Serialize)]
pub struct AnchorStats {
    pub total_checks: usize,
    pub total_passes: usize,
    pub total_breaches: usize,
    pub pass_rate: f64,
    pub breach_rate: f64,
    pub top_breaches: Vec<AnchorBreachEntry>,
}

/// A single anchor breach frequency entry.
#[derive(Debug, Clone, serde::Serialize)]
pub struct AnchorBreachEntry {
    pub anchor_name: String,
    pub breach_count: usize,
}

/// Error and retry statistics.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ErrorStats {
    pub total_steps: usize,
    pub total_errors: usize,
    pub total_retries: usize,
    pub error_rate: f64,
    pub retry_rate: f64,
}

/// Step name frequency distribution.
#[derive(Debug, Clone, serde::Serialize)]
pub struct StepFrequency {
    pub unique_steps: usize,
    pub top_steps: Vec<StepFreqEntry>,
}

/// A single step frequency entry.
#[derive(Debug, Clone, serde::Serialize)]
pub struct StepFreqEntry {
    pub step_name: String,
    pub count: usize,
}

// ── Computation ─────────────────────────────────────────────────────────

/// Compute aggregate analytics from a set of parsed traces.
pub fn compute_analytics(traces: &[replay::ReplayTrace]) -> TraceAnalytics {
    let mut durations: Vec<u64> = Vec::new();
    let mut total_input: u64 = 0;
    let mut total_output: u64 = 0;
    let mut total_passes: usize = 0;
    let mut total_breaches: usize = 0;
    let mut total_steps: usize = 0;
    let mut total_errors: usize = 0;
    let mut total_retries: usize = 0;
    let mut breach_counts: HashMap<String, usize> = HashMap::new();
    let mut step_counts: HashMap<String, usize> = HashMap::new();

    for trace in traces {
        for unit in &trace.units {
            durations.push(unit.duration_ms);
            total_input += unit.total_input_tokens;
            total_output += unit.total_output_tokens;

            for step in &unit.steps {
                total_steps += 1;
                *step_counts.entry(step.name.clone()).or_insert(0) += 1;

                if !step.success {
                    total_errors += 1;
                }
                if step.was_retried {
                    total_retries += 1;
                }

                for anchor in &step.anchor_results {
                    if anchor.passed {
                        total_passes += 1;
                    } else {
                        total_breaches += 1;
                        *breach_counts.entry(anchor.anchor_name.clone()).or_insert(0) += 1;
                    }
                }
            }
        }
    }

    let latency = compute_latency(&durations);
    let unit_count = durations.len();

    let tokens = TokenStats {
        total_input,
        total_output,
        total: total_input + total_output,
        mean_input_per_unit: if unit_count > 0 { total_input / unit_count as u64 } else { 0 },
        mean_output_per_unit: if unit_count > 0 { total_output / unit_count as u64 } else { 0 },
        mean_total_per_unit: if unit_count > 0 { (total_input + total_output) / unit_count as u64 } else { 0 },
        unit_count,
    };

    let total_checks = total_passes + total_breaches;
    let anchors = AnchorStats {
        total_checks,
        total_passes,
        total_breaches,
        pass_rate: if total_checks > 0 { total_passes as f64 / total_checks as f64 } else { 1.0 },
        breach_rate: if total_checks > 0 { total_breaches as f64 / total_checks as f64 } else { 0.0 },
        top_breaches: top_breaches(&breach_counts, 10),
    };

    let errors = ErrorStats {
        total_steps,
        total_errors,
        total_retries,
        error_rate: if total_steps > 0 { total_errors as f64 / total_steps as f64 } else { 0.0 },
        retry_rate: if total_steps > 0 { total_retries as f64 / total_steps as f64 } else { 0.0 },
    };

    let steps = compute_step_frequency(&step_counts, 10);

    TraceAnalytics {
        trace_count: traces.len(),
        latency,
        tokens,
        anchors,
        errors,
        steps,
    }
}

fn compute_latency(durations: &[u64]) -> LatencyStats {
    if durations.is_empty() {
        return LatencyStats {
            unit_count: 0,
            p50_ms: 0,
            p95_ms: 0,
            p99_ms: 0,
            mean_ms: 0,
            min_ms: 0,
            max_ms: 0,
        };
    }

    let mut sorted = durations.to_vec();
    sorted.sort();
    let n = sorted.len();

    LatencyStats {
        unit_count: n,
        p50_ms: percentile(&sorted, 50.0),
        p95_ms: percentile(&sorted, 95.0),
        p99_ms: percentile(&sorted, 99.0),
        mean_ms: sorted.iter().sum::<u64>() / n as u64,
        min_ms: sorted[0],
        max_ms: sorted[n - 1],
    }
}

/// Compute a percentile from a sorted slice using nearest-rank method.
fn percentile(sorted: &[u64], pct: f64) -> u64 {
    if sorted.is_empty() {
        return 0;
    }
    let rank = (pct / 100.0 * sorted.len() as f64).ceil() as usize;
    let idx = rank.min(sorted.len()).saturating_sub(1);
    sorted[idx]
}

fn top_breaches(counts: &HashMap<String, usize>, limit: usize) -> Vec<AnchorBreachEntry> {
    let mut entries: Vec<AnchorBreachEntry> = counts
        .iter()
        .map(|(name, &count)| AnchorBreachEntry {
            anchor_name: name.clone(),
            breach_count: count,
        })
        .collect();
    entries.sort_by(|a, b| b.breach_count.cmp(&a.breach_count));
    entries.truncate(limit);
    entries
}

fn compute_step_frequency(counts: &HashMap<String, usize>, limit: usize) -> StepFrequency {
    let mut entries: Vec<StepFreqEntry> = counts
        .iter()
        .map(|(name, &count)| StepFreqEntry {
            step_name: name.clone(),
            count,
        })
        .collect();
    entries.sort_by(|a, b| b.count.cmp(&a.count));
    let unique_steps = entries.len();
    entries.truncate(limit);

    StepFrequency {
        unique_steps,
        top_steps: entries,
    }
}

// ── CLI entry point ─────────────────────────────────────────────────────

/// Load trace files and compute aggregate analytics.
/// Format: "text" (default), "json", "prometheus", "csv".
/// Returns exit code: 0 = success, 2 = error.
pub fn run_stats(files: &[String], format: &str) -> i32 {
    if files.is_empty() {
        eprintln!("error: no trace files provided");
        return 2;
    }

    let mut traces: Vec<replay::ReplayTrace> = Vec::new();
    let mut errors = 0;

    for path in files {
        match std::fs::read_to_string(path) {
            Ok(content) => {
                match serde_json::from_str::<serde_json::Value>(&content) {
                    Ok(data) => {
                        traces.push(replay::parse_trace(&data));
                    }
                    Err(e) => {
                        eprintln!("error: failed to parse {}: {}", path, e);
                        errors += 1;
                    }
                }
            }
            Err(e) => {
                eprintln!("error: failed to read {}: {}", path, e);
                errors += 1;
            }
        }
    }

    if traces.is_empty() {
        eprintln!("error: no valid traces loaded ({} errors)", errors);
        return 2;
    }

    let analytics = compute_analytics(&traces);

    match format {
        "json" => println!("{}", serde_json::to_string_pretty(&analytics).unwrap()),
        "prometheus" => print!("{}", crate::trace_export::to_prometheus(&analytics)),
        "csv" => print!("{}", crate::trace_export::to_csv(&analytics)),
        _ => print_analytics(&analytics, errors),
    }

    0
}

// ── Human-readable output ───────────────────────────────────────────────

fn print_analytics(a: &TraceAnalytics, load_errors: usize) {
    let use_color = std::io::stdout().is_terminal();

    let bold = if use_color { "\x1b[1m" } else { "" };
    let cyan = if use_color { "\x1b[36m" } else { "" };
    let yellow = if use_color { "\x1b[33m" } else { "" };
    let red = if use_color { "\x1b[31m" } else { "" };
    let green = if use_color { "\x1b[32m" } else { "" };
    let reset = if use_color { "\x1b[0m" } else { "" };

    println!("{}═══ AXON Trace Analytics ═══{}", bold, reset);
    println!();

    // Overview
    println!("{}Traces:{} {}", cyan, reset, a.trace_count);
    if load_errors > 0 {
        println!("{}Load errors:{} {}", red, reset, load_errors);
    }
    println!("{}Units:{} {}", cyan, reset, a.latency.unit_count);
    println!("{}Steps:{} {}", cyan, reset, a.errors.total_steps);
    println!();

    // Latency
    println!("{}── Latency ──{}", bold, reset);
    if a.latency.unit_count > 0 {
        println!("  p50:  {} ms", a.latency.p50_ms);
        println!("  p95:  {} ms", a.latency.p95_ms);
        println!("  p99:  {} ms", a.latency.p99_ms);
        println!("  mean: {} ms", a.latency.mean_ms);
        println!("  min:  {} ms", a.latency.min_ms);
        println!("  max:  {} ms", a.latency.max_ms);
    } else {
        println!("  (no latency data)");
    }
    println!();

    // Tokens
    println!("{}── Tokens ──{}", bold, reset);
    println!("  total input:    {}", a.tokens.total_input);
    println!("  total output:   {}", a.tokens.total_output);
    println!("  total:          {}", a.tokens.total);
    if a.tokens.unit_count > 0 {
        println!("  mean/unit:      {} in + {} out", a.tokens.mean_input_per_unit, a.tokens.mean_output_per_unit);
    }
    println!();

    // Anchors
    println!("{}── Anchors ──{}", bold, reset);
    if a.anchors.total_checks > 0 {
        println!("  checks:    {}", a.anchors.total_checks);
        println!("  {}passes:    {}{} ({:.1}%)", green, a.anchors.total_passes, reset, a.anchors.pass_rate * 100.0);
        println!("  {}breaches:  {}{} ({:.1}%)", red, a.anchors.total_breaches, reset, a.anchors.breach_rate * 100.0);
        if !a.anchors.top_breaches.is_empty() {
            println!("  top breaches:");
            for b in &a.anchors.top_breaches {
                println!("    {}× {}{}{}", b.breach_count, yellow, b.anchor_name, reset);
            }
        }
    } else {
        println!("  (no anchor data)");
    }
    println!();

    // Errors
    println!("{}── Errors ──{}", bold, reset);
    println!("  errors:  {} / {} steps ({:.1}%)", a.errors.total_errors, a.errors.total_steps, a.errors.error_rate * 100.0);
    println!("  retries: {} ({:.1}%)", a.errors.total_retries, a.errors.retry_rate * 100.0);
    println!();

    // Step frequency
    println!("{}── Step Frequency ──{}", bold, reset);
    println!("  unique steps: {}", a.steps.unique_steps);
    if !a.steps.top_steps.is_empty() {
        for s in &a.steps.top_steps {
            println!("    {}× {}", s.count, s.step_name);
        }
    }
}

// ── Tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::replay::{ReplayTrace, TraceMeta, ReplayUnit, ReplayStep, AnchorEvent, ReplaySummary};

    fn make_meta() -> TraceMeta {
        TraceMeta {
            source: "test.axon".into(),
            backend: "anthropic".into(),
            tool_mode: "stub".into(),
            axon_version: "1.0.0".into(),
            mode: "stub".into(),
        }
    }

    fn make_step(name: &str, success: bool, retried: bool, anchors: Vec<AnchorEvent>) -> ReplayStep {
        ReplayStep {
            name: name.into(),
            event_type: "step_complete".into(),
            output: format!("{} output", name),
            success,
            anchor_results: anchors,
            was_retried: retried,
        }
    }

    fn make_unit(flow: &str, duration_ms: u64, input_tokens: u64, output_tokens: u64, steps: Vec<ReplayStep>) -> ReplayUnit {
        ReplayUnit {
            flow_name: flow.into(),
            steps,
            duration_ms,
            total_input_tokens: input_tokens,
            total_output_tokens: output_tokens,
            anchor_breaches: 0,
        }
    }

    fn make_trace(units: Vec<ReplayUnit>) -> ReplayTrace {
        let total_steps = units.iter().map(|u| u.steps.len()).sum();
        let total_input: u64 = units.iter().map(|u| u.total_input_tokens).sum();
        let total_output: u64 = units.iter().map(|u| u.total_output_tokens).sum();
        ReplayTrace {
            meta: make_meta(),
            units,
            summary: ReplaySummary {
                total_units: 0,
                total_steps,
                total_anchor_passes: 0,
                total_anchor_breaches: 0,
                total_retries: 0,
                total_errors: 0,
                total_input_tokens: total_input,
                total_output_tokens: total_output,
            },
        }
    }

    fn anchor(name: &str, passed: bool) -> AnchorEvent {
        AnchorEvent { anchor_name: name.into(), passed, detail: String::new() }
    }

    #[test]
    fn percentile_basic() {
        // 10 values: 10,20,30,...,100
        let data: Vec<u64> = (1..=10).map(|x| x * 10).collect();
        assert_eq!(percentile(&data, 50.0), 50);
        assert_eq!(percentile(&data, 95.0), 100);
        assert_eq!(percentile(&data, 99.0), 100);
        assert_eq!(percentile(&data, 0.0), 10); // ceil(0) = 0, saturating_sub → 0 → first element
    }

    #[test]
    fn percentile_single_value() {
        assert_eq!(percentile(&[42], 50.0), 42);
        assert_eq!(percentile(&[42], 99.0), 42);
    }

    #[test]
    fn percentile_empty() {
        assert_eq!(percentile(&[], 50.0), 0);
    }

    #[test]
    fn latency_stats_computed() {
        let t1 = make_trace(vec![
            make_unit("F", 100, 0, 0, vec![make_step("S1", true, false, vec![])]),
            make_unit("F", 200, 0, 0, vec![make_step("S2", true, false, vec![])]),
        ]);
        let t2 = make_trace(vec![
            make_unit("F", 150, 0, 0, vec![make_step("S1", true, false, vec![])]),
        ]);

        let a = compute_analytics(&[t1, t2]);
        assert_eq!(a.latency.unit_count, 3);
        assert_eq!(a.latency.min_ms, 100);
        assert_eq!(a.latency.max_ms, 200);
        assert_eq!(a.latency.mean_ms, 150); // (100+200+150)/3
    }

    #[test]
    fn token_stats_aggregated() {
        let t = make_trace(vec![
            make_unit("F1", 0, 100, 50, vec![make_step("S", true, false, vec![])]),
            make_unit("F2", 0, 200, 80, vec![make_step("S", true, false, vec![])]),
        ]);

        let a = compute_analytics(&[t]);
        assert_eq!(a.tokens.total_input, 300);
        assert_eq!(a.tokens.total_output, 130);
        assert_eq!(a.tokens.total, 430);
        assert_eq!(a.tokens.mean_input_per_unit, 150);
        assert_eq!(a.tokens.mean_output_per_unit, 65);
        assert_eq!(a.tokens.unit_count, 2);
    }

    #[test]
    fn anchor_stats_computed() {
        let t = make_trace(vec![
            make_unit("F", 0, 0, 0, vec![
                make_step("S1", true, false, vec![
                    anchor("SafeOutput", true),
                    anchor("NoHallucination", false),
                ]),
                make_step("S2", true, false, vec![
                    anchor("SafeOutput", true),
                    anchor("NoHallucination", false),
                    anchor("FactualOnly", false),
                ]),
            ]),
        ]);

        let a = compute_analytics(&[t]);
        assert_eq!(a.anchors.total_checks, 5);
        assert_eq!(a.anchors.total_passes, 2);
        assert_eq!(a.anchors.total_breaches, 3);
        assert!((a.anchors.pass_rate - 0.4).abs() < 0.01);
        assert!((a.anchors.breach_rate - 0.6).abs() < 0.01);

        // Top breaches sorted by count
        assert_eq!(a.anchors.top_breaches.len(), 2);
        assert_eq!(a.anchors.top_breaches[0].anchor_name, "NoHallucination");
        assert_eq!(a.anchors.top_breaches[0].breach_count, 2);
        assert_eq!(a.anchors.top_breaches[1].anchor_name, "FactualOnly");
        assert_eq!(a.anchors.top_breaches[1].breach_count, 1);
    }

    #[test]
    fn error_and_retry_stats() {
        let t = make_trace(vec![
            make_unit("F", 0, 0, 0, vec![
                make_step("S1", true, false, vec![]),
                make_step("S2", false, true, vec![]),  // error + retried
                make_step("S3", true, true, vec![]),   // success but was retried
                make_step("S4", false, false, vec![]), // error, no retry
            ]),
        ]);

        let a = compute_analytics(&[t]);
        assert_eq!(a.errors.total_steps, 4);
        assert_eq!(a.errors.total_errors, 2);
        assert_eq!(a.errors.total_retries, 2);
        assert!((a.errors.error_rate - 0.5).abs() < 0.01);
        assert!((a.errors.retry_rate - 0.5).abs() < 0.01);
    }

    #[test]
    fn step_frequency_distribution() {
        let t = make_trace(vec![
            make_unit("F1", 0, 0, 0, vec![
                make_step("Analyze", true, false, vec![]),
                make_step("Summarize", true, false, vec![]),
            ]),
            make_unit("F2", 0, 0, 0, vec![
                make_step("Analyze", true, false, vec![]),
                make_step("Generate", true, false, vec![]),
                make_step("Analyze", true, false, vec![]),
            ]),
        ]);

        let a = compute_analytics(&[t]);
        assert_eq!(a.steps.unique_steps, 3);
        assert_eq!(a.steps.top_steps[0].step_name, "Analyze");
        assert_eq!(a.steps.top_steps[0].count, 3);
    }

    #[test]
    fn empty_traces() {
        let a = compute_analytics(&[]);
        assert_eq!(a.trace_count, 0);
        assert_eq!(a.latency.unit_count, 0);
        assert_eq!(a.latency.p50_ms, 0);
        assert_eq!(a.tokens.total, 0);
        assert!((a.anchors.pass_rate - 1.0).abs() < 0.01); // No checks → 100% pass
        assert!((a.anchors.breach_rate - 0.0).abs() < 0.01);
    }

    #[test]
    fn multiple_traces_aggregate() {
        let t1 = make_trace(vec![
            make_unit("F", 100, 50, 20, vec![make_step("A", true, false, vec![])]),
        ]);
        let t2 = make_trace(vec![
            make_unit("F", 200, 70, 30, vec![make_step("B", true, false, vec![])]),
        ]);

        let a = compute_analytics(&[t1, t2]);
        assert_eq!(a.trace_count, 2);
        assert_eq!(a.latency.unit_count, 2);
        assert_eq!(a.tokens.total_input, 120);
        assert_eq!(a.tokens.total_output, 50);
        assert_eq!(a.errors.total_steps, 2);
        assert_eq!(a.steps.unique_steps, 2);
    }

    #[test]
    fn no_anchor_data_defaults() {
        let t = make_trace(vec![
            make_unit("F", 100, 0, 0, vec![make_step("S", true, false, vec![])]),
        ]);

        let a = compute_analytics(&[t]);
        assert_eq!(a.anchors.total_checks, 0);
        assert!((a.anchors.pass_rate - 1.0).abs() < 0.01);
        assert!(a.anchors.top_breaches.is_empty());
    }

    #[test]
    fn analytics_serializes_to_json() {
        let t = make_trace(vec![
            make_unit("F", 100, 50, 20, vec![
                make_step("S", true, false, vec![anchor("Safe", true)]),
            ]),
        ]);

        let a = compute_analytics(&[t]);
        let json = serde_json::to_value(&a).unwrap();
        assert_eq!(json["trace_count"], 1);
        assert!(json["latency"]["p50_ms"].is_number());
        assert!(json["tokens"]["total"].is_number());
        assert!(json["anchors"]["pass_rate"].is_number());
        assert!(json["errors"]["error_rate"].is_number());
        assert!(json["steps"]["unique_steps"].is_number());
    }

    #[test]
    fn run_stats_no_files_returns_error() {
        assert_eq!(run_stats(&[], "text"), 2);
    }

    #[test]
    fn run_stats_missing_file_returns_error() {
        let files = vec!["nonexistent_trace_file.json".to_string()];
        assert_eq!(run_stats(&files, "text"), 2);
    }

    #[test]
    fn run_stats_valid_trace_json() {
        let tmp = std::env::temp_dir().join("axon_stats_test.trace.json");
        let data = serde_json::json!({
            "_meta": { "source": "t.axon", "backend": "anthropic", "tool_mode": "stub", "axon_version": "1.0.0", "mode": "stub" },
            "events": [
                { "event": "unit_start", "unit": "F", "step": "", "detail": "" },
                { "event": "step_complete", "unit": "F", "step": "S", "detail": "ok" },
                { "event": "unit_complete", "unit": "F", "step": "", "detail": "" },
            ]
        });
        std::fs::write(&tmp, serde_json::to_string(&data).unwrap()).unwrap();

        let files = vec![tmp.to_str().unwrap().to_string()];
        assert_eq!(run_stats(&files, "json"), 0);

        let _ = std::fs::remove_file(tmp);
    }
}