tokenix 0.37.0

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
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,
    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)
    ModelPrice {
        name: "claude-haiku-4-5",
        input_per_1m: 1.00,
        reference: false,
    },
    ModelPrice {
        name: "claude-sonnet-4-6",
        input_per_1m: 3.00,
        reference: true,
    },
    ModelPrice {
        name: "claude-opus-4-8",
        input_per_1m: 5.00,
        reference: false,
    },
    ModelPrice {
        name: "claude-fable-5",
        input_per_1m: 10.00,
        reference: false,
    },
    // OpenAI (source: developers.openai.com/api/docs/pricing, collected 2026-06-11)
    ModelPrice {
        name: "gpt-5.4-mini",
        input_per_1m: 0.75,
        reference: false,
    },
    ModelPrice {
        name: "gpt-5.4",
        input_per_1m: 2.50,
        reference: false,
    },
    ModelPrice {
        name: "gpt-5.5",
        input_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,
        reference: false,
    },
    ModelPrice {
        name: "gemini-3.5-flash",
        input_per_1m: 1.50,
        reference: false,
    },
    ModelPrice {
        name: "gemini-3.1-pro-preview",
        input_per_1m: 2.00,
        reference: false,
    },
];

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

#[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)>,
    /// Tokens saved by semantic-index intercepts (Read/Grep replaced with
    /// outlines/query results — events with no command), and their call count.
    pub index_saved: i64,
    pub index_calls: usize,
    /// Tokens saved by command output filters (Bash compression — events
    /// carrying the executed command), and their call count.
    pub filter_saved: i64,
    pub filter_calls: usize,
}

/// 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.
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 tokens_saved: i64 = intercepted_events.iter().map(|e| e.saved_tokens).sum();
    let tokens_used: i64 = intercepted_events.iter().map(|e| e.actual_tokens).sum();
    let tokens_original: i64 = intercepted_events.iter().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));

    // Source split: events carrying a command come from the Bash filter
    // pipeline; everything else is a semantic-index intercept (Read/Grep).
    // Pre-phase Bash events are rewrite markers (saved is always 0 there —
    // the compression is logged separately at execution time), so they are
    // excluded from the filter call count to avoid counting a command twice.
    let (mut index_saved, mut index_calls) = (0i64, 0usize);
    let (mut filter_saved, mut filter_calls) = (0i64, 0usize);
    for e in &intercepted_events {
        if e.command.is_empty() {
            index_saved += e.saved_tokens;
            index_calls += 1;
        } else if e.phase != "pre" {
            filter_saved += e.saved_tokens;
            filter_calls += 1;
        }
    }

    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,
        index_saved,
        index_calls,
        filter_saved,
        filter_calls,
    }
}

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 p = std::env::temp_dir().join("tokenix_test_gain").join(format!(
            "{}_{}",
            sub,
            std::process::id()
        ));
        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 a semantic-index intercept.
        assert_eq!(stats.index_saved, 100);
        assert_eq!(stats.index_calls, 1);
        assert_eq!(stats.filter_saved, 0);
        assert_eq!(stats.filter_calls, 0);

        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.index_saved, 300);
        assert_eq!(stats.index_calls, 1);
        assert_eq!(stats.filter_saved, 200);
        assert_eq!(stats.filter_calls, 1);
        assert_eq!(stats.tokens_saved, 500);

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