tokenix 0.65.4

Semantic search, symbol graphs, secrets scanning, output filters, and CLI hooks that save 60-90% LLM tokens
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
use crate::store::{list_all_project_logs, read_hook_log, read_hook_log_from_path, HookEvent};
use std::path::Path;

pub struct ModelPrice {
    pub name: &'static str,
    pub input_per_1m: f64,
    /// Output (completion) token rate. Used by `tokenix usage` for absolute spend.
    pub output_per_1m: f64,
    /// Cached-input read rate (typically a fraction of input).
    pub cache_read_per_1m: f64,
    /// Cache-write (creation) rate (typically a small premium over input).
    pub cache_write_per_1m: f64,
    pub reference: bool,
}

pub const PRICING_COLLECTED_AT: &str = "2026-06-11";

pub const MODELS: &[ModelPrice] = &[
    // Anthropic (source: platform.claude.com/docs/about-claude/pricing, collected 2026-06-11).
    // Anthropic convention: cache read = 0.1x input, cache write (5m) = 1.25x input.
    ModelPrice {
        name: "claude-haiku-4-5",
        input_per_1m: 1.00,
        output_per_1m: 5.00,
        cache_read_per_1m: 0.10,
        cache_write_per_1m: 1.25,
        reference: false,
    },
    ModelPrice {
        name: "claude-sonnet-4-6",
        input_per_1m: 3.00,
        output_per_1m: 15.00,
        cache_read_per_1m: 0.30,
        cache_write_per_1m: 3.75,
        reference: true,
    },
    ModelPrice {
        name: "claude-opus-4-8",
        input_per_1m: 5.00,
        output_per_1m: 25.00,
        cache_read_per_1m: 0.50,
        cache_write_per_1m: 6.25,
        reference: false,
    },
    ModelPrice {
        name: "claude-fable-5",
        input_per_1m: 10.00,
        output_per_1m: 50.00,
        cache_read_per_1m: 1.00,
        cache_write_per_1m: 12.50,
        reference: false,
    },
    // OpenAI (source: developers.openai.com/api/docs/pricing, collected 2026-06-11).
    // OpenAI has no separate cache-write; cached input is a discounted read (~0.25x).
    ModelPrice {
        name: "gpt-5.4-mini",
        input_per_1m: 0.75,
        output_per_1m: 3.00,
        cache_read_per_1m: 0.19,
        cache_write_per_1m: 0.75,
        reference: false,
    },
    ModelPrice {
        name: "gpt-5.4",
        input_per_1m: 2.50,
        output_per_1m: 10.00,
        cache_read_per_1m: 0.63,
        cache_write_per_1m: 2.50,
        reference: false,
    },
    ModelPrice {
        name: "gpt-5.5",
        input_per_1m: 5.00,
        output_per_1m: 20.00,
        cache_read_per_1m: 1.25,
        cache_write_per_1m: 5.00,
        reference: false,
    },
    // Google (source: ai.google.dev/gemini-api/docs/pricing, collected 2026-06-11).
    ModelPrice {
        name: "gemini-3.1-flash-lite",
        input_per_1m: 0.25,
        output_per_1m: 1.00,
        cache_read_per_1m: 0.06,
        cache_write_per_1m: 0.25,
        reference: false,
    },
    ModelPrice {
        name: "gemini-3.5-flash",
        input_per_1m: 1.50,
        output_per_1m: 6.00,
        cache_read_per_1m: 0.38,
        cache_write_per_1m: 1.50,
        reference: false,
    },
    ModelPrice {
        name: "gemini-3.1-pro-preview",
        input_per_1m: 2.00,
        output_per_1m: 8.00,
        cache_read_per_1m: 0.50,
        cache_write_per_1m: 2.00,
        reference: false,
    },
];

/// Look up pricing for a model id, matching by exact name then by prefix
/// (transcripts may carry suffixed ids like `claude-opus-4-8-20260101`).
pub fn price_for(model: &str) -> Option<&'static ModelPrice> {
    MODELS
        .iter()
        .find(|m| m.name == model)
        .or_else(|| MODELS.iter().find(|m| model.starts_with(m.name)))
        .or_else(|| {
            // Fall back to family match (e.g. "claude-opus" -> opus entry).
            MODELS.iter().find(|m| {
                let fam: String = m.name.split('-').take(2).collect::<Vec<_>>().join("-");
                !fam.is_empty() && model.starts_with(&fam)
            })
        })
}

/// Absolute USD cost of a single usage record, per token category.
pub fn usage_cost(
    price: &ModelPrice,
    input: u64,
    output: u64,
    cache_read: u64,
    cache_write: u64,
) -> f64 {
    (input as f64 * price.input_per_1m
        + output as f64 * price.output_per_1m
        + cache_read as f64 * price.cache_read_per_1m
        + cache_write as f64 * price.cache_write_per_1m)
        / 1_000_000.0
}

pub struct CostRow {
    pub model: &'static str,
    pub reference: bool,
    pub without_usd: f64,
    pub with_usd: f64,
    pub saved_usd: f64,
}

pub struct Economics {
    pub spend_usd: f64,
    pub saved_usd: f64,
    pub pct_of_spend: f64,
    pub total_tokens: u64,
    pub saved_tokens: i64,
}

/// Price measured savings against the user's OWN spend mix from transcripts,
/// not list-price hypotheticals. A weighted input-equivalent cost-per-token is
/// derived from the actual usage (output ≈ 5× input, cache write ≈ 1.25×,
/// cache read ≈ 0.1× — Anthropic's price ratios), then multiplied by the
/// measured saved tokens (which land on the input/cache side as tool
/// results). Returns None when no transcript usage is available.
///
/// The ratios are Anthropic's. `mix.cost_usd` itself comes from each record's
/// own model price, so the total spend is right for any provider, but the
/// weighting that splits it into a per-input-token rate is approximate when the
/// transcripts are dominated by a non-Anthropic model. The output is an
/// estimate and is presented as one.
pub fn economics(tokens_saved: i64) -> Option<Economics> {
    let mix = crate::usage::spend_mix()?;
    let units = mix.input as f64
        + 5.0 * mix.output as f64
        + 1.25 * mix.cache_write as f64
        + 0.1 * mix.cache_read as f64;
    if units <= 0.0 || mix.cost_usd <= 0.0 {
        return None;
    }
    let cpt = mix.cost_usd / units;
    let saved_usd = tokens_saved.max(0) as f64 * cpt;
    Some(Economics {
        spend_usd: mix.cost_usd,
        saved_usd,
        // Cap at 100%: this is "savings measured against what was actually
        // spent", and a long filtering history against a short billing window
        // could otherwise print a nonsensical 300%.
        pct_of_spend: (saved_usd / mix.cost_usd * 100.0).min(100.0),
        total_tokens: mix.input + mix.output + mix.cache_read + mix.cache_write,
        saved_tokens: tokens_saved.max(0),
    })
}

#[allow(dead_code)]
pub struct GainStats {
    pub total_calls: usize,
    pub intercepted: usize,
    pub passed: usize,
    pub tokens_saved: i64,
    pub tokens_used: i64,
    pub tokens_original: i64,
    pub pct_saved: f64,
    pub cost_rows: Vec<CostRow>,
    pub by_tool: Vec<(String, usize, i64)>,
    pub by_phase: Vec<(String, usize, i64)>,
    /// Top Bash commands compressed by the filter system, sorted by tokens saved.
    pub by_command: Vec<(String, usize, i64)>,
    /// Total semantic-search queries answered by the index (Read outlines + Grep).
    pub indexed_queries: usize,
    /// Commands that skipped filtering via the TOKENIX_DISABLED escape hatch.
    pub bypassed: usize,
    /// Per-session hook-call shape: sessions, mean/max calls per session, and
    /// within-session re-requests of the same tool+subject.
    pub sessions: SessionStats,
}

/// Hook calls per session and within-session re-requests — the measurable share
/// of the "+13.8% turns" mechanism by which compression savings invert
/// (arXiv 2607.12161): a compressed observation can make the agent re-request
/// the same file or command, adding turns instead of saving them. Sessions are
/// inferred from time gaps in the hook log (no session id is stored);
/// `re_requests` counts a (tool, subject) pair requested more than once in one
/// session, where `subject` is the parsed command for Bash and the file path
/// for Read/Grep. This is a diagnostic, not a causal Δ: without a holdout
/// control group no claim of a *caused* turn change is possible.
#[derive(Debug, Default, Clone, Copy)]
pub struct SessionStats {
    pub sessions: usize,
    pub calls_per_session: f64,
    pub calls_per_session_max: usize,
    pub re_requests: usize,
}

/// A gap longer than this between consecutive hook events starts a new session.
const SESSION_GAP_SECS: f64 = 30.0 * 60.0;

pub fn session_stats(events: &[HookEvent]) -> SessionStats {
    let mut sorted: Vec<&HookEvent> = events.iter().collect();
    sorted.sort_by(|a, b| a.ts.total_cmp(&b.ts));
    let mut stats = SessionStats::default();
    let mut seen: std::collections::HashSet<(String, String)> = std::collections::HashSet::new();
    let mut cur = 0usize;
    let mut prev_ts: Option<f64> = None;
    for e in &sorted {
        if prev_ts.is_some_and(|p| e.ts - p > SESSION_GAP_SECS) {
            stats.sessions += 1;
            stats.calls_per_session_max = stats.calls_per_session_max.max(cur);
            cur = 0;
            seen.clear();
        }
        cur += 1;
        let subject = if e.command.is_empty() {
            e.input_preview.clone()
        } else {
            e.command.clone()
        };
        if !seen.insert((e.tool.clone(), subject)) {
            stats.re_requests += 1;
        }
        prev_ts = Some(e.ts);
    }
    if cur > 0 {
        stats.sessions += 1;
        stats.calls_per_session_max = stats.calls_per_session_max.max(cur);
    }
    stats.calls_per_session = if stats.sessions > 0 {
        sorted.len() as f64 / stats.sessions as f64
    } else {
        0.0
    };
    stats
}

/// Aggregate stats across all projects, with per-project breakdown.
pub struct GlobalGainStats {
    pub aggregate: GainStats,
    /// (label, tokens_saved, total_calls, intercepted) per project, desc by saved.
    pub projects: Vec<(String, i64, usize, usize)>,
}

/// Collapse a full Bash command line to a readable base label for the BY COMMAND
/// rollup. Keeps the leading executable + subcommand words and stops at the first
/// flag, redirect, shell operator, or argument carrying special characters — so
/// `pnpm run test --coverage 2>&1 | tail` → `pnpm run test`, and the many
/// `cargo test … | …` variants collapse into one `cargo test` bucket instead of
/// cluttering the footer with full argument strings.
pub(crate) fn command_label(cmd: &str) -> String {
    let mut parts: Vec<&str> = Vec::new();
    for tok in cmd.split_whitespace() {
        let stop = tok.starts_with('-') || tok.chars().any(|c| "|&;<>=\"'/\\`$()".contains(c));
        if stop {
            break;
        }
        parts.push(tok);
        if parts.len() >= 4 {
            break;
        }
    }
    if parts.is_empty() {
        // e.g. `VAR=x cmd` — fall back to the first token so the bucket isn't empty.
        cmd.split_whitespace().next().unwrap_or(cmd).to_string()
    } else {
        parts.join(" ")
    }
}

fn stats_from_events(events: Vec<HookEvent>) -> GainStats {
    let intercepted_events: Vec<_> = events
        .iter()
        .filter(|e| e.action == "intercepted")
        .collect();
    let passed_events: Vec<_> = events.iter().filter(|e| e.action == "pass").collect();

    let bypassed = events.iter().filter(|e| e.action == "bypassed").count();
    let tokens_saved: i64 = intercepted_events.iter().map(|e| e.saved_tokens).sum();

    // Rate over every run that was actually measured, not only the ones that
    // won. A pass-through carries `original_estimate == actual_tokens` when the
    // payload was measured (a command that ran through `tokenix run` and did not
    // shrink) and zeros otherwise, so including them widens the denominator by
    // exactly the runs that saved nothing — the ones a "60-90% saved" headline
    // has to account for.
    let measured = intercepted_events.iter().chain(passed_events.iter());
    let tokens_used: i64 = measured.clone().map(|e| e.actual_tokens).sum();
    let tokens_original: i64 = measured.map(|e| e.original_estimate).sum();

    let pct_saved = if tokens_original > 0 {
        (tokens_saved as f64 / tokens_original as f64) * 100.0
    } else {
        0.0
    };

    let cost_rows = MODELS
        .iter()
        .map(|m| {
            let rate = m.input_per_1m / 1_000_000.0;
            let without_usd = tokens_original as f64 * rate;
            let with_usd = tokens_used as f64 * rate;
            CostRow {
                model: m.name,
                reference: m.reference,
                without_usd,
                with_usd,
                saved_usd: without_usd - with_usd,
            }
        })
        .collect();

    let mut by_tool_map: std::collections::HashMap<String, (usize, i64)> =
        std::collections::HashMap::new();
    for e in &intercepted_events {
        let entry = by_tool_map.entry(e.tool.clone()).or_default();
        entry.0 += 1;
        entry.1 += e.saved_tokens;
    }
    let mut by_tool: Vec<(String, usize, i64)> = by_tool_map
        .into_iter()
        .map(|(k, (c, s))| (k, c, s))
        .collect();
    by_tool.sort_by_key(|row| std::cmp::Reverse(row.2));

    let mut by_phase_map: std::collections::HashMap<String, (usize, i64)> =
        std::collections::HashMap::new();
    for e in &intercepted_events {
        let entry = by_phase_map.entry(e.phase.clone()).or_default();
        entry.0 += 1;
        entry.1 += e.saved_tokens;
    }
    let mut by_phase: Vec<(String, usize, i64)> = by_phase_map
        .into_iter()
        .map(|(k, (c, s))| (k, c, s))
        .collect();
    by_phase.sort_by(|a, b| a.0.cmp(&b.0));

    // Bash commands compressed by the filter system (command field non-empty).
    let mut by_cmd_map: std::collections::HashMap<String, (usize, i64)> =
        std::collections::HashMap::new();
    for e in &intercepted_events {
        if !e.command.is_empty() {
            let entry = by_cmd_map.entry(command_label(&e.command)).or_default();
            entry.0 += 1;
            entry.1 += e.saved_tokens;
        }
    }
    let mut by_command: Vec<(String, usize, i64)> = by_cmd_map
        .into_iter()
        .map(|(k, (c, s))| (k, c, s))
        .collect();
    by_command.sort_by_key(|row| std::cmp::Reverse(row.2));

    // Count semantic-index intercepts (Read/Grep — events without a command).
    let indexed_queries = intercepted_events
        .iter()
        .filter(|e| e.command.is_empty())
        .count();

    GainStats {
        total_calls: events.len(),
        intercepted: intercepted_events.len(),
        passed: passed_events.len(),
        tokens_saved,
        tokens_used,
        tokens_original,
        pct_saved,
        cost_rows,
        by_tool,
        by_phase,
        by_command,
        indexed_queries,
        bypassed,
        sessions: session_stats(&events),
    }
}

pub fn compute_gain(repo_root: &Path) -> GainStats {
    stats_from_events(read_hook_log(repo_root))
}

/// Aggregate gain stats across every project that has a hook log in `~/.tokenix/`.
pub fn compute_global_gain() -> GlobalGainStats {
    let project_logs = list_all_project_logs();

    let mut all_events: Vec<HookEvent> = Vec::new();
    let mut projects: Vec<(String, i64, usize, usize)> = Vec::new();

    for entry in &project_logs {
        let events = read_hook_log_from_path(&entry.log_path);
        let saved: i64 = events
            .iter()
            .filter(|e| e.action == "intercepted")
            .map(|e| e.saved_tokens)
            .sum();
        let total = events.len();
        let intercepted = events.iter().filter(|e| e.action == "intercepted").count();
        if total > 0 {
            projects.push((entry.label.clone(), saved, total, intercepted));
        }
        all_events.extend(events);
    }

    // Sort projects by tokens saved descending.
    projects.sort_by_key(|(_, saved, _, _)| std::cmp::Reverse(*saved));

    GlobalGainStats {
        aggregate: stats_from_events(all_events),
        projects,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::{log_hook_event, HookEvent};

    fn create_test_temp_dir(sub: &str) -> std::path::PathBuf {
        let unique = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos();
        let p = std::env::temp_dir().join("tokenix_test_gain").join(format!(
            "{}_{}_{}",
            sub,
            std::process::id(),
            unique
        ));
        let _ = std::fs::remove_dir_all(&p);
        let _ = std::fs::create_dir_all(&p);
        p
    }

    #[test]
    fn test_compute_gain_empty() {
        let temp_dir = create_test_temp_dir("empty");
        let stats = compute_gain(&temp_dir);
        assert_eq!(stats.total_calls, 0);
        assert_eq!(stats.intercepted, 0);
        assert_eq!(stats.passed, 0);
        assert_eq!(stats.tokens_saved, 0);
        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn test_compute_gain_with_events() {
        let temp_dir = create_test_temp_dir("events");

        let ev1 = HookEvent {
            ts: 1234567.0,
            tool: "Bash".to_string(),
            action: "intercepted".to_string(),
            phase: "post".to_string(),
            reason: "".to_string(),
            saved_tokens: 100,
            actual_tokens: 20,
            original_estimate: 120,
            input_preview: "".to_string(),
            command: "".to_string(),
        };
        let ev2 = HookEvent {
            ts: 1234568.0,
            tool: "Read".to_string(),
            action: "pass".to_string(),
            phase: "pre".to_string(),
            reason: "not intercepted".to_string(),
            saved_tokens: 0,
            actual_tokens: 0,
            original_estimate: 0,
            input_preview: "".to_string(),
            command: "".to_string(),
        };

        log_hook_event(&temp_dir, &ev1).unwrap();
        log_hook_event(&temp_dir, &ev2).unwrap();

        let stats = compute_gain(&temp_dir);
        assert_eq!(stats.total_calls, 2);
        assert_eq!(stats.intercepted, 1);
        assert_eq!(stats.passed, 1);
        assert_eq!(stats.tokens_saved, 100);
        assert_eq!(stats.tokens_used, 20);
        assert_eq!(stats.tokens_original, 120);
        assert!((stats.pct_saved - 83.333).abs() < 0.01);
        assert_eq!(stats.by_tool.len(), 1);
        assert_eq!(stats.by_tool[0], ("Bash".to_string(), 1, 100));
        assert_eq!(stats.by_phase.len(), 1);
        assert_eq!(stats.by_phase[0], ("post".to_string(), 1, 100));
        // ev1 carries no command → counted as an indexed query.
        assert_eq!(stats.indexed_queries, 1);
        // Two events one second apart → one session, two calls.
        assert_eq!(stats.sessions.sessions, 1);
        assert!((stats.sessions.calls_per_session - 2.0).abs() < 1e-9);

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn session_stats_splits_by_gap_and_counts_re_requests() {
        let mk = |ts: f64, tool: &str, preview: &str, command: &str| HookEvent {
            ts,
            tool: tool.to_string(),
            action: "pass".to_string(),
            phase: "pre".to_string(),
            reason: String::new(),
            saved_tokens: 0,
            actual_tokens: 0,
            original_estimate: 0,
            input_preview: preview.to_string(),
            command: command.to_string(),
        };
        let events = vec![
            // Session 1: reading the same file twice = one re-request.
            mk(0.0, "Read", "src/a.rs", ""),
            mk(60.0, "Read", "src/a.rs", ""),
            mk(120.0, "Bash", "", "cargo test"),
            // Session 2 (gap > 30 min): same command again — not a re-request,
            // it starts a fresh session.
            mk(3_700.0, "Bash", "", "cargo test"),
        ];
        let s = session_stats(&events);
        assert_eq!(s.sessions, 2);
        assert_eq!(s.calls_per_session_max, 3);
        assert!((s.calls_per_session - 2.0).abs() < 1e-9);
        assert_eq!(s.re_requests, 1, "only the repeated Read counts");
    }

    #[test]
    fn pct_saved_counts_runs_that_saved_nothing() {
        // The number people read as "tokenix saves X%". Measuring it only over
        // runs where compression already worked made it un-lowerable and wrong:
        // a command that went through `tokenix run` and came back the same size
        // is a real measurement and belongs in the denominator.
        let temp_dir = create_test_temp_dir("honest_pct");

        let win = HookEvent {
            ts: 1.0,
            tool: "Bash".to_string(),
            action: "intercepted".to_string(),
            phase: "ToolOutputCompressed".to_string(),
            reason: "compressed command output".to_string(),
            saved_tokens: 500,
            actual_tokens: 500,
            original_estimate: 1000,
            input_preview: "".to_string(),
            command: "cargo test".to_string(),
        };
        // Same payload size in and out: measured, not skipped.
        let no_win = HookEvent {
            ts: 2.0,
            tool: "Bash".to_string(),
            action: "pass".to_string(),
            phase: "ToolOutputCompressed".to_string(),
            reason: "no reduction available".to_string(),
            saved_tokens: 0,
            actual_tokens: 1000,
            original_estimate: 1000,
            input_preview: "".to_string(),
            command: "cargo build".to_string(),
        };

        log_hook_event(&temp_dir, &win).unwrap();
        log_hook_event(&temp_dir, &no_win).unwrap();

        let stats = compute_gain(&temp_dir);
        assert_eq!(stats.tokens_saved, 500);
        assert_eq!(
            stats.tokens_original, 2000,
            "both measured runs must be in the denominator"
        );
        assert_eq!(stats.tokens_used, 1500);
        assert!(
            (stats.pct_saved - 25.0).abs() < 0.01,
            "expected 500/2000 = 25%, got {}",
            stats.pct_saved
        );

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn test_source_split_index_vs_filters() {
        let temp_dir = create_test_temp_dir("source_split");

        let index_ev = HookEvent {
            ts: 1.0,
            tool: "Read".to_string(),
            action: "intercepted".to_string(),
            phase: "pre".to_string(),
            reason: "outline".to_string(),
            saved_tokens: 300,
            actual_tokens: 50,
            original_estimate: 350,
            input_preview: String::new(),
            command: String::new(),
        };
        let filter_ev = HookEvent {
            ts: 2.0,
            tool: "Bash".to_string(),
            action: "intercepted".to_string(),
            phase: "ToolOutputCompressed".to_string(),
            reason: "compressed command output".to_string(),
            saved_tokens: 200,
            actual_tokens: 30,
            original_estimate: 230,
            input_preview: String::new(),
            command: "cargo test".to_string(),
        };

        // Pre-phase rewrite marker for the same command: saved is 0 and the
        // event must not inflate the filter call count.
        let rewrite_marker = HookEvent {
            ts: 1.5,
            tool: "Bash".to_string(),
            action: "intercepted".to_string(),
            phase: "pre".to_string(),
            reason: "rewrote command to tokenix run".to_string(),
            saved_tokens: 0,
            actual_tokens: 0,
            original_estimate: 0,
            input_preview: String::new(),
            command: "cargo test".to_string(),
        };

        log_hook_event(&temp_dir, &index_ev).unwrap();
        log_hook_event(&temp_dir, &rewrite_marker).unwrap();
        log_hook_event(&temp_dir, &filter_ev).unwrap();

        let stats = compute_gain(&temp_dir);
        assert_eq!(stats.indexed_queries, 1);
        assert_eq!(stats.tokens_saved, 500);

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    fn intercepted(tool: &str, command: &str, original: i64, actual: i64) -> HookEvent {
        HookEvent {
            ts: 0.0,
            tool: tool.to_string(),
            action: "intercepted".to_string(),
            phase: "post".to_string(),
            reason: String::new(),
            saved_tokens: (original - actual).max(0),
            actual_tokens: actual,
            original_estimate: original,
            input_preview: String::new(),
            command: command.to_string(),
        }
    }

    #[test]
    fn cost_rows_price_savings_per_model() {
        let temp_dir = create_test_temp_dir("cost");
        // 1M original tokens compressed to 200k used → 800k saved.
        log_hook_event(
            &temp_dir,
            &intercepted("Bash", "cargo build", 1_000_000, 200_000),
        )
        .unwrap();

        let stats = compute_gain(&temp_dir);
        assert_eq!(stats.cost_rows.len(), MODELS.len());
        // Exactly one reference model (sonnet) drives the headline figure.
        assert_eq!(stats.cost_rows.iter().filter(|r| r.reference).count(), 1);

        let sonnet = stats
            .cost_rows
            .iter()
            .find(|r| r.model == "claude-sonnet-4-6")
            .expect("sonnet present");
        // $3 / 1M input tokens.
        assert!((sonnet.without_usd - 3.0).abs() < 1e-9);
        assert!((sonnet.with_usd - 0.6).abs() < 1e-9);
        assert!((sonnet.saved_usd - 2.4).abs() < 1e-9);
        // saved is always without - with, for every model.
        for r in &stats.cost_rows {
            assert!((r.saved_usd - (r.without_usd - r.with_usd)).abs() < 1e-9);
            assert!(r.saved_usd >= 0.0, "{} saved negative", r.model);
        }

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn price_for_matches_exact_and_suffixed_ids() {
        assert_eq!(
            price_for("claude-sonnet-4-6").map(|m| m.name),
            Some("claude-sonnet-4-6")
        );
        // Suffixed transcript ids resolve by prefix to the base model.
        assert_eq!(
            price_for("claude-opus-4-8-20260101").map(|m| m.name),
            Some("claude-opus-4-8")
        );
        assert!(price_for("totally-unknown-model").is_none());
    }

    #[test]
    fn usage_cost_sums_all_token_categories() {
        let p = price_for("claude-sonnet-4-6").unwrap();
        // 1M each of input/output/cache_read/cache_write at 3/15/0.30/3.75 per 1M.
        let cost = usage_cost(p, 1_000_000, 1_000_000, 1_000_000, 1_000_000);
        assert!((cost - (3.0 + 15.0 + 0.30 + 3.75)).abs() < 1e-9);
        // Zero usage costs nothing.
        assert_eq!(usage_cost(p, 0, 0, 0, 0), 0.0);
    }

    #[test]
    fn by_command_rollup_aggregates_same_base() {
        let temp_dir = create_test_temp_dir("rollup");
        // Two cargo-test variants collapse into one bucket; git status separate.
        log_hook_event(
            &temp_dir,
            &intercepted("Bash", "cargo test --all", 500, 100),
        )
        .unwrap();
        log_hook_event(
            &temp_dir,
            &intercepted("Bash", "cargo test -p core", 300, 50),
        )
        .unwrap();
        log_hook_event(&temp_dir, &intercepted("Bash", "git status -s", 80, 20)).unwrap();

        let stats = compute_gain(&temp_dir);
        let cargo = stats
            .by_command
            .iter()
            .find(|(c, _, _)| c == "cargo test")
            .expect("cargo test bucket");
        assert_eq!(cargo.1, 2, "two cargo invocations rolled up");
        assert_eq!(cargo.2, 400 + 250, "saved tokens summed");
        // Sorted by tokens saved desc → cargo (650) before git status (60).
        assert_eq!(stats.by_command[0].0, "cargo test");
        assert_eq!(stats.tokens_saved, 400 + 250 + 60);

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn pct_saved_zero_without_original_tokens() {
        let temp_dir = create_test_temp_dir("pctzero");
        // An intercept that recorded no original estimate must not divide by zero.
        log_hook_event(&temp_dir, &intercepted("Bash", "noisy", 0, 0)).unwrap();
        let stats = compute_gain(&temp_dir);
        assert_eq!(stats.pct_saved, 0.0);
        let _ = std::fs::remove_dir_all(&temp_dir);
    }
}