tokenix 0.22.0

Local semantic index CLI for LLM token optimization
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
use std::collections::HashMap;
use std::io::{self, BufRead, Write};
use std::path::Path;
use std::process::{Command, Stdio};

use anyhow::{bail, Result};
use colored::Colorize;

use crate::{filters, store};

struct CmdStats {
    base_cmd: String,
    count: usize,
    total_original: i64,
    total_saved: i64,
}

/// Base command for a Bash event. Prefers the stored `command` field (set at
/// log time); falls back to parsing legacy logs' input_preview, which only works
/// when the preview wasn't truncated before `tool_input.command`.
fn base_command(ev: &store::HookEvent) -> Option<String> {
    if !ev.command.is_empty() {
        return ev.command.split_whitespace().next().map(str::to_string);
    }
    extract_base_command(&ev.input_preview)
}

fn extract_base_command(input_preview: &str) -> Option<String> {
    let v: serde_json::Value = serde_json::from_str(input_preview).ok()?;
    let cmd = v["tool_input"]["command"].as_str()?;
    cmd.split_whitespace().next().map(str::to_string)
}

/// Reject command names that aren't plain executable identifiers. `base_cmd`
/// reaches `cmd /C <base_cmd> --help` (Windows shell metacharacter injection),
/// a `<base_cmd>.toml` filename (path traversal), and a git branch / PR head.
/// Allow only a leading alphanumeric followed by `[A-Za-z0-9._-]` — no spaces,
/// no `& | ; > < $ ` ( ) `, no path separators.
fn validate_command_name(cmd: &str) -> Result<()> {
    let mut chars = cmd.chars();
    let valid = matches!(chars.next(), Some(c) if c.is_ascii_alphanumeric())
        && chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '-'))
        && cmd.len() <= 64;
    if !valid {
        bail!(
            "refusing unsafe command name {cmd:?}: only [A-Za-z0-9._-] (≤64 chars, \
             alphanumeric start) are allowed for filter generation"
        );
    }
    Ok(())
}

fn format_num(n: i64) -> String {
    let s = n.to_string();
    let mut result = String::new();
    for (i, c) in s.chars().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.push(',');
        }
        result.push(c);
    }
    result.chars().rev().collect()
}

fn collect_stats(repo_root: &Path) -> Vec<CmdStats> {
    let events = store::read_hook_log(repo_root);
    let mut map: HashMap<String, CmdStats> = HashMap::new();
    for ev in events
        .iter()
        .filter(|e| e.tool == "Bash" && e.phase == "post")
    {
        if let Some(cmd) = base_command(ev) {
            let entry = map.entry(cmd.clone()).or_insert(CmdStats {
                base_cmd: cmd,
                count: 0,
                total_original: 0,
                total_saved: 0,
            });
            entry.count += 1;
            entry.total_original += ev.original_estimate;
            entry.total_saved += ev.saved_tokens;
        }
    }
    let mut stats: Vec<CmdStats> = map.into_values().collect();
    stats.sort_by_key(|s| -(s.total_original - s.total_saved));
    stats.truncate(20);
    stats
}

pub fn cmd_filter_list(repo_root: &Path) -> Result<()> {
    let stats = collect_stats(repo_root);
    print_stats_table(&stats);
    Ok(())
}

pub fn cmd_filter_active() -> Result<()> {
    let filters = filters::load_active_filters();
    if filters.is_empty() {
        println!("{}", "No active filters found.".yellow());
        return Ok(());
    }

    println!();
    println!("{}", "ACTIVE OUTPUT FILTERS".bold().underline());
    println!(
        "  {:<28} {:<8} {:<52} Description",
        "Name", "Source", "Match command"
    );
    println!("  {}", "-".repeat(118).bright_black());

    for f in filters {
        let desc = f.filter.description.unwrap_or_default();
        println!(
            "  {:<28} {:<8} {:<52} {}",
            truncate(&f.name, 28),
            f.source,
            truncate(&f.filter.match_command, 52),
            truncate(&desc, 42)
        );
    }
    println!();
    Ok(())
}

fn print_stats_table(stats: &[CmdStats]) {
    if stats.is_empty() {
        println!("No Bash hook events found. Run some commands to populate the log.");
        return;
    }
    println!("{}", "Top Bash commands by tokens wasted:".bold());
    println!(
        "{:<4} {:<18} {:>6} {:>15} {:>13}",
        "#", "Command", "Calls", "Tokens Wasted", "Tokens Saved"
    );
    println!("{}", "-".repeat(62));
    for (i, s) in stats.iter().enumerate() {
        println!(
            "{:<4} {:<18} {:>6} {:>15} {:>13}",
            i + 1,
            s.base_cmd,
            s.count,
            format_num(s.total_original - s.total_saved),
            format_num(s.total_saved),
        );
    }
}

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        return s.to_string();
    }
    let keep = max.saturating_sub(1);
    format!("{}~", s.chars().take(keep).collect::<String>())
}

pub fn cmd_filter_generate(command: Option<String>, repo_root: &Path) -> Result<()> {
    let base_cmd = match command {
        Some(c) => c,
        None => {
            let stats = collect_stats(repo_root);
            print_stats_table(&stats);
            if stats.is_empty() {
                return Ok(());
            }
            print!("\nSelect command to generate filter (1-{}): ", stats.len());
            io::stdout().flush()?;
            let mut line = String::new();
            io::stdin().lock().read_line(&mut line)?;
            let idx: usize = line.trim().parse().unwrap_or(0);
            if idx == 0 || idx > stats.len() {
                bail!("invalid selection");
            }
            stats[idx - 1].base_cmd.clone()
        }
    };

    // Security gate: base_cmd reaches a shell, a filename, and git/PR args.
    validate_command_name(&base_cmd)?;

    // Get sample output by running `<cmd> --help`
    println!(
        "\n{} running `{} --help` for sample output...",
        "".cyan(),
        base_cmd
    );
    let sample = run_command_sample(&base_cmd);

    // Show preview and let user confirm or replace
    let sample = preview_and_confirm_sample(&base_cmd, sample)?;
    if sample.is_empty() {
        return Ok(());
    }

    // Detect available AI CLIs
    let clis = detect_ai_clis();
    if clis.is_empty() {
        bail!(
            "No AI CLI found. Install one of: claude (Claude Code), gemini, codex\n\
             Claude Code: https://claude.ai/code"
        );
    }

    let chosen_cli = if clis.len() == 1 {
        println!("Using AI CLI: {}", clis[0].0.green());
        clis[0].clone()
    } else {
        println!("\nAvailable AI CLIs:");
        for (i, (name, _)) in clis.iter().enumerate() {
            println!("  [{}] {}", i + 1, name);
        }
        print!("Select CLI (1-{}): ", clis.len());
        io::stdout().flush()?;
        let mut line = String::new();
        io::stdin().lock().read_line(&mut line)?;
        let idx: usize = line.trim().parse::<usize>().unwrap_or(1).saturating_sub(1);
        clis.get(idx).cloned().unwrap_or_else(|| clis[0].clone())
    };

    // Build and send prompt
    println!(
        "{} asking {} to generate filter...",
        "".cyan(),
        chosen_cli.0.green()
    );
    let prompt = filters::build_filter_prompt(&base_cmd, &sample);
    let toml_output = invoke_ai_cli(&chosen_cli.0, &chosen_cli.1, &prompt)?;
    let toml_clean = extract_toml_from_response(&toml_output);

    // Show only the extracted TOML (not AI prose)
    println!("\n{}", "Generated filter:".bold());
    println!("{}", "".repeat(60));
    println!("{}", toml_clean.cyan());
    println!("{}", "".repeat(60));

    if toml::from_str::<toml::Value>(&toml_clean).is_err() {
        println!("{} TOML is invalid — edit before saving.", "".yellow());
        println!("  Raw AI response saved to stderr for reference.");
        eprintln!("\n--- raw AI response ---\n{}\n---", toml_output.trim());
    }

    // Confirm save
    print!("\nSave to ~/.tokenix/filters/{}.toml? [Y/n]: ", base_cmd);
    io::stdout().flush()?;
    let mut ans = String::new();
    io::stdin().lock().read_line(&mut ans)?;
    if ans.trim().eq_ignore_ascii_case("n") {
        println!("Discarded.");
        return Ok(());
    }

    let dir = filters::filters_dir();
    std::fs::create_dir_all(&dir)?;
    let path = dir.join(format!("{}.toml", base_cmd));
    std::fs::write(&path, toml_clean.trim())?;
    println!("{} Saved to {}", "".green(), path.display());

    // Offer PR contribution
    print!("\nContribute this filter to tokenix? [y/N]: ");
    io::stdout().flush()?;
    let mut ans = String::new();
    io::stdin().lock().read_line(&mut ans)?;
    if ans.trim().eq_ignore_ascii_case("y") {
        contribute_filter(&base_cmd, toml_clean.trim());
    }

    Ok(())
}

fn preview_and_confirm_sample(cmd: &str, sample: String) -> Result<String> {
    let preview_lines: Vec<&str> = sample.lines().take(30).collect();
    println!(
        "\n{} (first 30 lines):",
        format!("Sample output for `{}`", cmd).bold()
    );
    println!("{}", "".repeat(60));
    for line in &preview_lines {
        println!("{}", line);
    }
    let total = sample.lines().count();
    if total > 30 {
        println!("{}", format!("  ... ({} more lines)", total - 30).dimmed());
    }
    println!("{}", "".repeat(60));

    print!("\n[U]se this sample  [P]aste your own  [Q]uit: ");
    io::stdout().flush()?;
    let mut ans = String::new();
    io::stdin().lock().read_line(&mut ans)?;
    match ans.trim().to_lowercase().as_str() {
        "u" | "" => Ok(sample),
        "p" => {
            println!(
                "Paste your sample output, then enter a line with just a single dot (.) to finish:"
            );
            let mut pasted = String::new();
            let stdin = io::stdin();
            for line in stdin.lock().lines() {
                let line = line?;
                if line.trim() == "." {
                    break;
                }
                pasted.push_str(&line);
                pasted.push('\n');
            }
            Ok(pasted)
        }
        _ => Ok(String::new()),
    }
}

fn run_command_sample(cmd: &str) -> String {
    let output = if cfg!(windows) {
        Command::new("cmd")
            .args(["/C", cmd, "--help"])
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
    } else {
        Command::new(cmd)
            .arg("--help")
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
    };

    match output {
        Ok(o) => {
            let stdout = String::from_utf8_lossy(&o.stdout);
            let stderr = String::from_utf8_lossy(&o.stderr);
            let combined = if stdout.is_empty() {
                stderr.to_string()
            } else {
                stdout.to_string()
            };
            // Cap at 150 lines
            combined.lines().take(150).collect::<Vec<_>>().join("\n")
        }
        Err(_) => format!("(could not run `{} --help`)", cmd),
    }
}

/// Returns Vec of (name, invoke_flag) for detected, working AI CLIs.
fn detect_ai_clis() -> Vec<(String, String)> {
    // flag: how to pass the prompt as an argument
    let candidates = [("claude", "-p"), ("gemini", "-p"), ("codex", "-p")];
    let mut found = Vec::new();
    for (name, flag) in candidates {
        if is_cli_available(name) {
            found.push((name.to_string(), flag.to_string()));
        }
    }
    found
}

/// Probe the CLI by running `--version` — filters out stale shims in PATH.
fn is_cli_available(name: &str) -> bool {
    let ok = if cfg!(windows) {
        Command::new("cmd")
            .args(["/C", name, "--version"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
    } else {
        Command::new(name)
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
    };
    ok.map(|s| s.success()).unwrap_or(false)
}

pub fn is_gh_available() -> bool {
    is_cli_available("gh")
}

fn invoke_ai_cli(name: &str, flag: &str, prompt: &str) -> Result<String> {
    // On Windows, CLIs are often .cmd/.bat wrappers — must invoke via cmd /C.
    // Rust's Command API passes args directly without shell interpretation,
    // so special chars in prompt are safe.
    let mut cmd = if cfg!(windows) {
        let mut c = Command::new("cmd");
        c.args(["/C", name, flag, prompt]);
        c
    } else {
        let mut c = Command::new(name);
        c.args([flag, prompt]);
        c
    };
    let child = cmd
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
        .map_err(|e| anyhow::anyhow!("failed to start {}: {}", name, e))?;

    let output = child.wait_with_output()?;
    if output.stdout.is_empty() {
        bail!("{} returned no output", name);
    }
    Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

/// Extract TOML from AI response — handles prose + fences, partial fences, bare TOML.
fn extract_toml_from_response(s: &str) -> String {
    // 1. Find ```toml...``` block anywhere (AI often wraps in markdown)
    if let Some(start) = s.find("```toml") {
        let after = &s[start + 7..];
        let body_start = after.find('\n').map(|i| i + 1).unwrap_or(0);
        let body = &after[body_start..];
        let end = body.find("```").unwrap_or(body.len());
        return body[..end].trim().to_string();
    }

    // 2. Find ``` block that contains a [filters. section
    if let Some(start) = s.find("```\n") {
        let after = &s[start + 4..];
        let end = after.find("```").unwrap_or(after.len());
        let candidate = after[..end].trim().to_string();
        if candidate.contains("[filters.") {
            return candidate;
        }
    }

    // 3. Find bare [filters. section — skip any leading prose
    if let Some(start) = s.find("[filters.") {
        return s[start..].trim().to_string();
    }

    s.trim().to_string()
}

fn contribute_filter(cmd: &str, toml_content: &str) {
    if !is_gh_available() {
        println!("{} gh CLI not found — manual steps:", "".yellow());
        print_contribution_instructions(cmd, toml_content);
        return;
    }
    if let Err(e) = create_pr(cmd, toml_content) {
        println!("{} PR failed: {} — manual steps:", "".yellow(), e);
        print_contribution_instructions(cmd, toml_content);
    }
}

fn create_pr(cmd: &str, toml_content: &str) -> Result<()> {
    let tmp = std::env::temp_dir().join(format!("tokenix-filter-{}", cmd));
    if tmp.exists() {
        std::fs::remove_dir_all(&tmp)?;
    }
    std::fs::create_dir_all(&tmp)?;

    println!("{} forking juninmd/tokenix...", "".cyan());
    gh_run(&["repo", "fork", "juninmd/tokenix", "--clone"], &tmp)?;

    let repo = tmp.join("tokenix");
    let branch = format!("filter-{}", cmd);

    git_run(&["-C", repo.to_str().unwrap(), "checkout", "-b", &branch])?;

    let filters_dir = repo.join("filters");
    std::fs::create_dir_all(&filters_dir)?;
    std::fs::write(filters_dir.join(format!("{}.toml", cmd)), toml_content)?;

    git_run(&[
        "-C",
        repo.to_str().unwrap(),
        "add",
        &format!("filters/{}.toml", cmd),
    ])?;
    git_run(&[
        "-C",
        repo.to_str().unwrap(),
        "commit",
        "-m",
        &format!("filter: add {} filter", cmd),
    ])?;
    git_run(&["-C", repo.to_str().unwrap(), "push", "origin", &branch])?;

    println!("{} creating PR...", "".cyan());
    let title = format!("filter: add {} filter", cmd);
    let body = format!(
        "New community filter for `{cmd}`.\n\nGenerated by `tokenix filter generate {cmd}`.\n\n```toml\n{toml_content}\n```\n"
    );
    gh_run(
        &[
            "pr",
            "create",
            "--repo",
            "juninmd/tokenix",
            "--title",
            &title,
            "--body",
            &body,
            "--base",
            "main",
            "--head",
            &branch,
        ],
        &repo,
    )?;

    println!(
        "{} PR created at github.com/juninmd/tokenix/pulls",
        "".green()
    );
    let _ = std::fs::remove_dir_all(&tmp);
    Ok(())
}

/// Run a `gh` subcommand, optionally in a working directory.
fn gh_run(args: &[&str], cwd: &std::path::Path) -> Result<()> {
    let ok = if cfg!(windows) {
        let mut full = vec!["/C", "gh"];
        full.extend_from_slice(args);
        Command::new("cmd").args(&full).current_dir(cwd).status()?
    } else {
        Command::new("gh").args(args).current_dir(cwd).status()?
    };
    if ok.success() {
        Ok(())
    } else {
        bail!("gh {:?} failed", args)
    }
}

/// Run a `git` subcommand (no working-dir needed; uses -C flag instead).
fn git_run(args: &[&str]) -> Result<()> {
    let ok = if cfg!(windows) {
        let mut full = vec!["/C", "git"];
        full.extend_from_slice(args);
        Command::new("cmd").args(&full).status()?
    } else {
        Command::new("git").args(args).status()?
    };
    if ok.success() {
        Ok(())
    } else {
        bail!("git {:?} failed", args)
    }
}

fn print_contribution_instructions(cmd: &str, toml_content: &str) {
    println!("  1. Fork https://github.com/juninmd/tokenix");
    println!("  2. Create file: filters/{}.toml", cmd);
    println!("{}", "".repeat(60));
    println!("{}", toml_content);
    println!("{}", "".repeat(60));
    println!("  3. PR title: \"filter: add {} filter\"", cmd);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validate_command_name_accepts_real_commands() {
        for ok in [
            "cargo",
            "npm",
            "git",
            "uv",
            "docker-compose",
            "go.test",
            "a",
        ] {
            assert!(validate_command_name(ok).is_ok(), "{ok} should be allowed");
        }
    }

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

    #[test]
    fn base_command_prefers_stored_command_field() {
        // The real Claude payload front-loads session_id/cwd, so input_preview is
        // truncated before tool_input.command — the stored field must win.
        let truncated = r#"{"session_id":"abc","transcript_path":"x","cwd":"y","#;
        assert_eq!(
            base_command(&ev("cargo build --release", truncated)),
            Some("cargo".to_string())
        );
    }

    #[test]
    fn base_command_falls_back_to_legacy_preview() {
        let legacy = r#"{"tool_input":{"command":"git status"}}"#;
        assert_eq!(base_command(&ev("", legacy)), Some("git".to_string()));
    }

    #[test]
    fn validate_command_name_rejects_injection_and_traversal() {
        for bad in [
            "cargo & calc",     // command chaining
            "foo|bar",          // pipe
            "rm;ls",            // semicolon
            "$(whoami)",        // substitution
            "../../etc/passwd", // path traversal
            "a/b",              // path separator
            "-rf",              // leading dash (flag injection)
            "",                 // empty
            "foo bar",          // space
        ] {
            assert!(
                validate_command_name(bad).is_err(),
                "{bad:?} should be rejected"
            );
        }
    }
}