bctx 0.1.9

bctx CLI — intercept CLI commands and compress output for LLM coding agents
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
use anyhow::Result;
use std::collections::HashMap;
use std::path::PathBuf;
use weave::mesh::registry::FilterMesh;

// ── Domain tables ─────────────────────────────────────────────────────────────

/// Average compression ratio per program, derived from integration test data.
fn avg_compression_pct(program: &str) -> u64 {
    match program {
        "git" => 84,
        "cargo" => 79,
        "kubectl" => 68,
        "npm" | "pnpm" | "yarn" | "bun" => 71,
        "docker" | "docker-compose" => 65,
        "terraform" | "tofu" => 60,
        "aws" => 63,
        "go" => 72,
        "make" | "cmake" | "bazel" => 58,
        "pytest" | "jest" | "vitest" => 75,
        "ruff" | "mypy" | "eslint" | "clippy" => 77,
        "psql" | "mysql" | "prisma" => 62,
        "helm" | "ansible" => 60,
        _ => 55,
    }
}

/// Average tokens produced per invocation. Conservative estimates.
fn avg_tokens_per_run(program: &str) -> u64 {
    match program {
        "git" => 2_000,
        "cargo" => 6_000,
        "kubectl" => 3_000,
        "npm" | "pnpm" | "yarn" | "bun" => 4_000,
        "docker" | "docker-compose" => 3_500,
        "terraform" | "tofu" => 4_000,
        "aws" => 3_000,
        "go" => 3_500,
        "make" | "cmake" | "bazel" => 5_000,
        "pytest" | "jest" | "vitest" => 5_000,
        "ruff" | "mypy" | "eslint" | "clippy" => 2_500,
        "psql" | "mysql" | "prisma" => 2_000,
        _ => 1_500,
    }
}

/// Compute estimated missed tokens for `count` invocations of `program`.
pub fn missed_tokens_for(program: &str, count: u64) -> u64 {
    count * avg_tokens_per_run(program) * avg_compression_pct(program) / 100
}

// ── History parsing ───────────────────────────────────────────────────────────

/// Shell builtins and bctx itself — never report these as uncompressed.
pub fn is_builtin(program: &str) -> bool {
    matches!(
        program,
        "cd" | "ls"
            | "echo"
            | "export"
            | "source"
            | "."
            | "bctx"
            | "exit"
            | "history"
            | "alias"
            | "unset"
            | "set"
            | "eval"
            | "exec"
            | "pwd"
            | "type"
            | "which"
            | "man"
    )
}

/// Parse one raw history line into `(program, subcommand)`.
///
/// Returns `None` for blank lines, comments, builtins, or entries older than
/// `cutoff` (Unix seconds; only enforced when a timestamp is present).
pub fn parse_history_line(
    line: &str,
    is_zsh_extended: bool,
    cutoff: u64,
) -> Option<(String, String)> {
    let line = line.trim();
    if line.is_empty() || line.starts_with('#') {
        return None;
    }

    let (ts, cmd): (u64, &str) = if is_zsh_extended && line.starts_with(": ") {
        // `: 1715000000:0;git log --oneline -20`
        let semi = line.find(';')?;
        let ts_val: u64 = line[2..semi]
            .split(':')
            .next()
            .unwrap_or("0")
            .trim()
            .parse()
            .unwrap_or(0);
        (ts_val, line[semi + 1..].trim())
    } else if let Some(rest) = line.strip_prefix("- cmd: ") {
        // Fish: `- cmd: git log`
        (0, rest.trim())
    } else {
        // Plain bash history
        (0, line)
    };

    // Drop entries older than the cutoff (only when timestamp is known)
    if ts > 0 && ts < cutoff {
        return None;
    }

    let mut parts = cmd.split_whitespace();
    let program = parts.next()?;
    if is_builtin(program) {
        return None;
    }
    let sub = parts.next().unwrap_or("").to_string();
    Some((program.to_string(), sub))
}

/// Parse all lines from a history file content string.
pub fn parse_history_content(
    content: &str,
    is_zsh_extended: bool,
    cutoff: u64,
) -> Vec<(String, String)> {
    content
        .lines()
        .filter_map(|l| parse_history_line(l, is_zsh_extended, cutoff))
        .collect()
}

// ── FilterMesh classification ─────────────────────────────────────────────────

/// Count how many times each program appeared in `entries` AND is supported
/// by the FilterMesh. Unsupported programs are silently ignored.
pub fn count_supported(entries: &[(String, String)], mesh: &FilterMesh) -> HashMap<String, u64> {
    let mut counts: HashMap<String, u64> = HashMap::new();
    for (program, sub) in entries {
        let args: Vec<String> = if sub.is_empty() {
            vec![]
        } else {
            vec![sub.clone()]
        };
        if mesh.find(program, &args).is_some() {
            *counts.entry(program.clone()).or_insert(0) += 1;
        }
    }
    counts
}

// ── Ranking ───────────────────────────────────────────────────────────────────

/// Build a ranked list `(program, run_count, missed_tokens)` sorted by
/// missed_tokens descending. Capped at top 10.
pub fn rank_missed(counts: HashMap<String, u64>) -> Vec<(String, u64, u64)> {
    let mut ranked: Vec<(String, u64, u64)> = counts
        .into_iter()
        .map(|(prog, count)| {
            let missed = missed_tokens_for(&prog, count);
            (prog, count, missed)
        })
        .collect();
    ranked.sort_by_key(|b| std::cmp::Reverse(b.2));
    ranked.truncate(10);
    ranked
}

// ── I/O ───────────────────────────────────────────────────────────────────────

fn read_shell_history(days: u32) -> Vec<(String, String)> {
    let home = std::env::var("HOME").unwrap_or_default();
    let cutoff = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
        .saturating_sub(days as u64 * 86_400);

    let sources: Vec<(PathBuf, bool)> = vec![
        (PathBuf::from(&home).join(".zsh_history"), true),
        (PathBuf::from(&home).join(".bash_history"), false),
        (
            PathBuf::from(&home).join(".local/share/fish/fish_history"),
            false,
        ),
    ];

    let mut entries = Vec::new();
    for (path, is_zsh_extended) in sources {
        let Ok(content) = std::fs::read_to_string(&path) else {
            continue;
        };
        entries.extend(parse_history_content(&content, is_zsh_extended, cutoff));
    }
    entries
}

pub fn handle(days: u32) -> Result<()> {
    let mesh = FilterMesh::new();
    let history = read_shell_history(days);

    if history.is_empty() {
        println!();
        println!("  No shell history found.");
        println!("  Checked: ~/.zsh_history  ~/.bash_history  ~/.local/share/fish/fish_history");
        println!();
        return Ok(());
    }

    let counts = count_supported(&history, &mesh);

    if counts.is_empty() {
        println!();
        println!("  No uncompressed commands found in the last {days} days.");
        println!("  Run `bctx init` to wire the shell hook and start saving tokens.");
        println!();
        return Ok(());
    }

    let ranked = rank_missed(counts);
    let total_missed: u64 = ranked.iter().map(|(_, _, t)| t).sum();
    let cost = total_missed as f64 * 0.000_015;
    let max_missed = ranked.iter().map(|(_, _, t)| *t).max().unwrap_or(1);

    let sep = "  ─────────────────────────────────────────────────────";
    println!();
    println!("  UNCOMPRESSED COMMANDS  (last {days} days)");
    println!("{sep}");
    for (prog, count, missed_tokens) in &ranked {
        let bar = savings_bar(*missed_tokens, max_missed, 14);
        println!(
            "  {:<12}  {}  ran {:>4}×  ·  est. missed  ~{}",
            prog,
            bar,
            count,
            fmt_tokens(*missed_tokens),
        );
    }
    println!("{sep}");
    println!(
        "  Total estimated missed savings: ~{} tokens (~${:.2})",
        fmt_tokens(total_missed),
        cost,
    );
    println!();
    println!("  These commands ran without bctx interception.");
    println!("  Run `bctx init` to wire the shell hook and capture them automatically.");
    println!();
    Ok(())
}

// ── Formatting helpers ────────────────────────────────────────────────────────

fn fmt_tokens(n: u64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.0}K", n as f64 / 1_000.0)
    } else {
        n.to_string()
    }
}

fn savings_bar(value: u64, max: u64, width: usize) -> String {
    use std::io::IsTerminal;
    let filled = if max == 0 {
        0
    } else {
        ((value as usize) * width / (max as usize)).min(width)
    };
    let empty = width - filled;
    let blocks = "".repeat(filled);
    let shade = "".repeat(empty);
    if std::io::stdout().is_terminal() {
        format!("\x1b[33m{blocks}\x1b[2m{shade}\x1b[0m")
    } else {
        format!("{blocks}{shade}")
    }
}

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

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

    // ── fmt_tokens ────────────────────────────────────────────────────────────

    #[test]
    fn fmt_tokens_millions() {
        assert_eq!(fmt_tokens(2_400_000), "2.4M");
    }

    #[test]
    fn fmt_tokens_thousands() {
        assert_eq!(fmt_tokens(94_000), "94K");
    }

    #[test]
    fn fmt_tokens_small() {
        assert_eq!(fmt_tokens(0), "0");
        assert_eq!(fmt_tokens(42), "42");
        assert_eq!(fmt_tokens(999), "999");
    }

    // ── savings_bar ───────────────────────────────────────────────────────────

    #[test]
    fn savings_bar_full() {
        let b = savings_bar(16, 16, 8);
        assert!(b.contains("████████"));
    }

    #[test]
    fn savings_bar_zero_max_no_panic() {
        let b = savings_bar(0, 0, 8);
        assert!(b.contains(""));
    }

    #[test]
    fn savings_bar_half() {
        let b = savings_bar(4, 8, 8);
        assert!(b.contains("████"));
        assert!(b.contains("░░░░"));
    }

    // ── avg_compression_pct ───────────────────────────────────────────────────

    #[test]
    fn avg_compression_pct_known() {
        assert_eq!(avg_compression_pct("git"), 84);
        assert_eq!(avg_compression_pct("cargo"), 79);
        assert_eq!(avg_compression_pct("kubectl"), 68);
    }

    #[test]
    fn avg_compression_pct_fallback() {
        assert_eq!(avg_compression_pct("some_unknown_tool"), 55);
    }

    // ── is_builtin ────────────────────────────────────────────────────────────

    #[test]
    fn builtins_are_filtered() {
        for b in &["cd", "ls", "echo", "export", "source", "exit", "bctx"] {
            assert!(is_builtin(b), "{b} should be a builtin");
        }
    }

    #[test]
    fn supported_programs_are_not_builtins() {
        for p in &["git", "cargo", "kubectl", "npm", "docker", "terraform"] {
            assert!(!is_builtin(p), "{p} should not be a builtin");
        }
    }

    // ── parse_history_line ────────────────────────────────────────────────────

    #[test]
    fn parse_plain_bash_line() {
        let result = parse_history_line("git log --oneline -20", false, 0);
        assert_eq!(result, Some(("git".into(), "log".into())));
    }

    #[test]
    fn parse_plain_bash_no_subcommand() {
        let result = parse_history_line("docker", false, 0);
        assert_eq!(result, Some(("docker".into(), "".into())));
    }

    #[test]
    fn parse_zsh_extended_line() {
        let result = parse_history_line(": 1715000000:0;cargo build --release", true, 0);
        assert_eq!(result, Some(("cargo".into(), "build".into())));
    }

    #[test]
    fn parse_zsh_extended_old_entry_filtered_by_cutoff() {
        // timestamp 1000 < cutoff 9999999999 → should be dropped
        let result = parse_history_line(": 1000:0;git status", true, 9_999_999_999);
        assert_eq!(result, None);
    }

    #[test]
    fn parse_zsh_extended_recent_entry_kept() {
        // timestamp far in the future relative to cutoff 0
        let result = parse_history_line(": 9999999999:0;git status", true, 0);
        assert_eq!(result, Some(("git".into(), "status".into())));
    }

    #[test]
    fn parse_fish_line() {
        let result = parse_history_line("- cmd: kubectl get pods", false, 0);
        assert_eq!(result, Some(("kubectl".into(), "get".into())));
    }

    #[test]
    fn parse_empty_line_returns_none() {
        assert_eq!(parse_history_line("", false, 0), None);
        assert_eq!(parse_history_line("   ", false, 0), None);
    }

    #[test]
    fn parse_comment_line_returns_none() {
        assert_eq!(parse_history_line("# this is a comment", false, 0), None);
    }

    #[test]
    fn parse_builtin_returns_none() {
        assert_eq!(parse_history_line("cd /home/user", false, 0), None);
        assert_eq!(parse_history_line("bctx git log", false, 0), None);
        assert_eq!(parse_history_line("echo hello", false, 0), None);
    }

    // ── parse_history_content ─────────────────────────────────────────────────

    #[test]
    fn parse_history_content_bash_multiline() {
        let content = "git log\ncargo test\ncd /tmp\ngit status\n";
        let result = parse_history_content(content, false, 0);
        assert_eq!(result.len(), 3); // cd filtered
        assert!(result.iter().any(|(p, _)| p == "git"));
        assert!(result.iter().any(|(p, _)| p == "cargo"));
        assert!(!result.iter().any(|(p, _)| p == "cd"));
    }

    #[test]
    fn parse_history_content_zsh_filters_old_entries() {
        let content = ": 100:0;git log\n: 9999999999:0;cargo build\n";
        let result = parse_history_content(content, true, 1_000_000);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "cargo");
    }

    #[test]
    fn parse_history_content_skips_blank_and_comments() {
        let content = "\n# comment\ngit status\n\n";
        let result = parse_history_content(content, false, 0);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].0, "git");
    }

    // ── count_supported ───────────────────────────────────────────────────────

    #[test]
    fn count_supported_ignores_unknown_programs() {
        let mesh = FilterMesh::default_mesh();
        let entries = vec![
            ("my_obscure_tool_xyz".into(), "".into()),
            ("another_unknown_999".into(), "run".into()),
        ];
        let counts = count_supported(&entries, &mesh);
        assert!(counts.is_empty(), "unknown tools must not appear in counts");
    }

    #[test]
    fn count_supported_counts_git_occurrences() {
        let mesh = FilterMesh::default_mesh();
        let entries = vec![
            ("git".into(), "log".into()),
            ("git".into(), "status".into()),
            ("git".into(), "diff".into()),
        ];
        let counts = count_supported(&entries, &mesh);
        assert_eq!(counts.get("git"), Some(&3));
    }

    #[test]
    fn count_supported_counts_multiple_programs() {
        let mesh = FilterMesh::default_mesh();
        let entries = vec![
            ("git".into(), "log".into()),
            ("cargo".into(), "build".into()),
            ("git".into(), "status".into()),
        ];
        let counts = count_supported(&entries, &mesh);
        assert_eq!(counts.get("git"), Some(&2));
        assert_eq!(counts.get("cargo"), Some(&1));
    }

    #[test]
    fn count_supported_mixed_known_and_unknown() {
        let mesh = FilterMesh::default_mesh();
        let entries = vec![
            ("git".into(), "log".into()),
            ("totally_fake_tool_xyz".into(), "run".into()),
            ("cargo".into(), "test".into()),
        ];
        let counts = count_supported(&entries, &mesh);
        assert_eq!(counts.len(), 2);
        assert!(!counts.contains_key("totally_fake_tool_xyz"));
    }

    // ── rank_missed ───────────────────────────────────────────────────────────

    #[test]
    fn rank_missed_sorted_by_tokens_descending() {
        let mut counts = HashMap::new();
        counts.insert("git".into(), 1u64); // 2000 × 84% = 1680
        counts.insert("cargo".into(), 1u64); // 6000 × 79% = 4740
        let ranked = rank_missed(counts);
        assert_eq!(ranked[0].0, "cargo");
        assert_eq!(ranked[1].0, "git");
    }

    #[test]
    fn rank_missed_capped_at_ten() {
        let counts: HashMap<String, u64> = (0..15).map(|i| (format!("tool_{i}"), 1u64)).collect();
        let ranked = rank_missed(counts);
        assert!(ranked.len() <= 10);
    }

    #[test]
    fn rank_missed_token_estimate_correct() {
        let mut counts = HashMap::new();
        counts.insert("git".into(), 2u64); // 2 × 2000 × 84% = 3360
        let ranked = rank_missed(counts);
        assert_eq!(ranked[0].2, 3_360);
    }

    // ── missed_tokens_for ─────────────────────────────────────────────────────

    #[test]
    fn missed_tokens_for_git_single_run() {
        // 1 × 2000 tokens × 84% = 1680
        assert_eq!(missed_tokens_for("git", 1), 1_680);
    }

    #[test]
    fn missed_tokens_for_cargo_multiple_runs() {
        // 3 × 6000 × 79% = 14220
        assert_eq!(missed_tokens_for("cargo", 3), 14_220);
    }

    #[test]
    fn missed_tokens_for_zero_runs() {
        assert_eq!(missed_tokens_for("git", 0), 0);
    }
}