brokk-rtk 0.42.4

Rust Token Killer - High-performance CLI proxy to minimize LLM token consumption
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
use anyhow::Result;
use std::io::Read;

use crate::core::stream::RAW_CAP;
use crate::core::truncate::{CAP_LIST, CAP_WARNINGS};

const MAX_PIPE_MATCHES: usize = CAP_WARNINGS;
const MAX_PIPE_FILES: usize = CAP_WARNINGS;
const MAX_PIPE_DIRS: usize = CAP_LIST;

pub fn resolve_filter(name: &str) -> Option<fn(&str) -> String> {
    match name {
        "cargo-test" | "cargo" => Some(crate::cmds::rust::cargo_cmd::filter_cargo_test),
        "pytest" => Some(crate::cmds::python::pytest_cmd::filter_pytest_output),
        "go-test" => Some(go_test_wrapper),
        "go-build" => Some(crate::cmds::go::go_cmd::filter_go_build),
        "tsc" => Some(crate::cmds::js::tsc_cmd::filter_tsc_output),
        "vitest" => Some(vitest_wrapper),
        "grep" | "rg" => Some(grep_wrapper),
        "find" | "fd" => Some(find_wrapper),
        "git-log" => Some(git_log_wrapper),
        "git-diff" => Some(git_diff_wrapper),
        "git-status" => Some(git_status_wrapper),
        "log" => Some(crate::cmds::system::log_cmd::run_stdin_str),
        "mypy" => Some(crate::cmds::python::mypy_cmd::filter_mypy_output),
        "ruff-check" => Some(crate::cmds::python::ruff_cmd::filter_ruff_check_json),
        "ruff-format" => Some(crate::cmds::python::ruff_cmd::filter_ruff_format),
        "prettier" => Some(crate::cmds::js::prettier_cmd::filter_prettier_output),
        _ => None,
    }
}

fn go_test_wrapper(input: &str) -> String {
    crate::cmds::go::go_cmd::filter_go_test_json(input)
}

fn git_status_wrapper(input: &str) -> String {
    crate::cmds::git::git::format_status_output(input)
}

fn git_log_wrapper(input: &str) -> String {
    crate::cmds::git::git::filter_log_output(input, 50, false, false)
}

fn git_diff_wrapper(input: &str) -> String {
    crate::cmds::git::git::compact_diff(input, 200)
}

fn vitest_wrapper(input: &str) -> String {
    use crate::cmds::js::vitest_cmd::VitestParser;
    use crate::parser::{FormatMode, OutputParser, TokenFormatter};
    let result = VitestParser::parse(input);
    match result {
        crate::parser::ParseResult::Full(data) => data.format(FormatMode::Compact),
        crate::parser::ParseResult::Degraded(data, _) => data.format(FormatMode::Compact),
        crate::parser::ParseResult::Passthrough(raw) => raw,
    }
}

fn grep_wrapper(input: &str) -> String {
    use std::collections::HashMap;

    let mut by_file: HashMap<&str, Vec<(&str, &str)>> = HashMap::new();
    let mut total = 0;

    for line in input.lines() {
        let parts: Vec<&str> = line.splitn(3, ':').collect();
        if parts.len() == 3 {
            if let Ok(_line_num) = parts[1].parse::<usize>() {
                total += 1;
                by_file.entry(parts[0]).or_default().push((parts[1], parts[2]));
            }
        }
    }

    if total == 0 {
        return input.to_string();
    }

    let mut out = format!("{} matches in {}F:\n\n", total, by_file.len());
    let mut files: Vec<_> = by_file.iter().collect();
    files.sort_by_key(|(f, _)| *f);

    for (file, matches) in files {
        out.push_str(&format!("[file] {} ({}):\n", file, matches.len()));
        for (line_num, content) in matches.iter().take(MAX_PIPE_MATCHES) {
            out.push_str(&format!("  {:>4}: {}\n", line_num, content.trim()));
        }
        if matches.len() > MAX_PIPE_MATCHES {
            out.push_str(&format!("  +{}\n", matches.len() - MAX_PIPE_MATCHES));
        }
        out.push('\n');
    }

    out
}

fn find_wrapper(input: &str) -> String {
    use std::collections::HashMap;

    let paths: Vec<&str> = input.lines().filter(|l| !l.trim().is_empty()).collect();

    if paths.is_empty() {
        return input.to_string();
    }

    let mut by_dir: HashMap<&str, Vec<&str>> = HashMap::new();

    for path in &paths {
        let dir = match path.rfind('/') {
            Some(pos) => &path[..pos],
            None => ".",
        };
        let name = match path.rfind('/') {
            Some(pos) => &path[pos + 1..],
            None => path,
        };
        by_dir.entry(dir).or_default().push(name);
    }

    let mut out = format!("{} files in {} dirs:\n\n", paths.len(), by_dir.len());
    let mut dirs: Vec<_> = by_dir.iter().collect();
    dirs.sort_by_key(|(d, _)| *d);

    for (dir, files) in dirs.iter().take(MAX_PIPE_DIRS) {
        out.push_str(&format!("{}/  ({})\n", dir, files.len()));
        for f in files.iter().take(MAX_PIPE_FILES) {
            out.push_str(&format!("  {}\n", f));
        }
        if files.len() > MAX_PIPE_FILES {
            out.push_str(&format!("  +{}\n", files.len() - MAX_PIPE_FILES));
        }
    }

    if dirs.len() > MAX_PIPE_DIRS {
        out.push_str(&format!("\n+{} more dirs\n", dirs.len() - MAX_PIPE_DIRS));
    }

    out
}

pub fn auto_detect_filter(input: &str) -> fn(&str) -> String {
    let end = input.len().min(1024);
    // Avoid panic: byte 1024 may fall inside a multi-byte UTF-8 char
    let end = input.floor_char_boundary(end);
    let first_1k = &input[..end];

    if first_1k.contains("test result:") && first_1k.contains("passed;") {
        return crate::cmds::rust::cargo_cmd::filter_cargo_test;
    }

    if first_1k.contains("=== test session starts") {
        return crate::cmds::python::pytest_cmd::filter_pytest_output;
    }

    let first_trimmed = first_1k.trim_start();
    if first_trimmed.starts_with('{') && first_1k.contains("\"Action\"") {
        return go_test_wrapper;
    }

    if first_1k.contains(": error:") && first_1k.contains(".py:") {
        return crate::cmds::python::mypy_cmd::filter_mypy_output;
    }

    // grep/rg: lines matching file:number:content
    if first_1k
        .lines()
        .take(5)
        .filter(|l| !l.trim().is_empty())
        .any(|l| {
            let parts: Vec<_> = l.splitn(3, ':').collect();
            parts.len() == 3 && parts[1].parse::<usize>().is_ok()
        })
    {
        return grep_wrapper;
    }

    if first_1k.contains("\"testResults\"") || first_1k.contains("\"numTotalTests\"") {
        return vitest_wrapper;
    }

    // find/fd: all non-empty lines look like file paths, minimum 3 lines
    let path_like_lines: usize = first_1k
        .lines()
        .filter(|l| {
            let t = l.trim();
            !t.is_empty()
                && !t.contains(':')
                && (t.starts_with('.') || t.starts_with('/') || t.contains('/'))
        })
        .count();
    let nonempty_lines: usize = first_1k.lines().filter(|l| !l.trim().is_empty()).count();
    if nonempty_lines >= 3 && path_like_lines == nonempty_lines {
        return find_wrapper;
    }

    identity_filter
}

fn identity_filter(input: &str) -> String {
    input.to_string()
}

fn apply_filter(filter_fn: fn(&str) -> String, input: &str) -> String {
    std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| filter_fn(input)))
        .unwrap_or_else(|_| {
            eprintln!("[rtk] warning: filter panicked — passing through raw output");
            input.to_string()
        })
}

pub fn run(filter_name: Option<&str>, passthrough: bool) -> Result<()> {
    if passthrough {
        std::io::copy(&mut std::io::stdin(), &mut std::io::stdout())
            .map_err(|e| anyhow::anyhow!("Failed to relay stdin: {}", e))?;
        return Ok(());
    }

    let mut buf = String::new();
    std::io::stdin()
        .take((RAW_CAP + 1) as u64)
        .read_to_string(&mut buf)
        .map_err(|e| anyhow::anyhow!("Failed to read stdin: {}", e))?;
    if buf.len() > RAW_CAP {
        anyhow::bail!("stdin exceeds {} byte limit", RAW_CAP);
    }

    let filter_fn = match filter_name {
        Some(name) => resolve_filter(name).ok_or_else(|| {
            anyhow::anyhow!(
                "Unknown filter '{}'. Available: cargo-test, pytest, go-test, go-build, \
                 tsc, vitest, grep, rg, find, fd, git-log, git-diff, git-status, \
                 log, mypy, ruff-check, ruff-format, prettier",
                name
            )
        })?,
        None => auto_detect_filter(&buf),
    };

    let output = apply_filter(filter_fn, &buf);
    print!("{}", output);
    Ok(())
}

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

    #[test]
    fn test_resolve_filter_cargo_test() {
        let f = resolve_filter("cargo-test").expect("cargo-test filter must exist");
        let out = f("test result: ok. 5 passed; 0 failed");
        assert!(out.contains("passed") || out.contains("PASS"), "out={}", out);
    }

    #[test]
    fn test_resolve_filter_cargo_alias() {
        assert!(resolve_filter("cargo").is_some());
    }

    #[test]
    fn test_resolve_filter_grep() {
        let f = resolve_filter("grep").expect("grep filter must exist");
        let input = "src/main.rs:42:fn main() {\nsrc/lib.rs:10:pub fn helper() {}\n";
        let out = f(input);
        assert!(
            out.contains("main.rs") || out.contains("matches"),
            "out={}",
            out
        );
    }

    #[test]
    fn test_resolve_filter_rg_alias() {
        assert!(resolve_filter("rg").is_some());
    }

    #[test]
    fn test_resolve_filter_pytest() {
        assert!(resolve_filter("pytest").is_some());
    }

    #[test]
    fn test_resolve_filter_go_test() {
        assert!(resolve_filter("go-test").is_some());
    }

    #[test]
    fn test_resolve_filter_tsc() {
        assert!(resolve_filter("tsc").is_some());
    }

    #[test]
    fn test_resolve_filter_vitest() {
        assert!(resolve_filter("vitest").is_some());
    }

    #[test]
    fn test_resolve_filter_git_log() {
        assert!(resolve_filter("git-log").is_some());
    }

    #[test]
    fn test_resolve_filter_git_diff() {
        assert!(resolve_filter("git-diff").is_some());
    }

    #[test]
    fn test_resolve_filter_git_status() {
        assert!(resolve_filter("git-status").is_some());
    }

    #[test]
    fn test_resolve_filter_log() {
        assert!(resolve_filter("log").is_some());
    }

    #[test]
    fn test_resolve_filter_unknown_returns_none() {
        assert!(resolve_filter("nonexistent-filter").is_none());
    }

    #[test]
    fn test_auto_detect_cargo_test() {
        let input = "test result: ok. 5 passed; 0 failed; 0 ignored; 0 measured\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(!out.is_empty());
    }

    #[test]
    fn test_auto_detect_pytest() {
        let input = "=== test session starts ===\ncollected 3 items\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(!out.is_empty());
    }

    #[test]
    fn test_auto_detect_grep_format() {
        let input = "src/main.rs:42:fn main() {\nsrc/lib.rs:10:pub fn helper() {}\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(!out.is_empty());
    }

    #[test]
    fn test_auto_detect_go_test_ndjson() {
        let input = r#"{"Time":"2024-01-01T00:00:00Z","Action":"run","Package":"example/pkg"}
{"Time":"2024-01-01T00:00:01Z","Action":"pass","Package":"example/pkg","Elapsed":0.5}
"#;
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(!out.is_empty());
    }

    #[test]
    fn test_auto_detect_unknown_returns_identity() {
        let input = "some random text that doesn't match any filter pattern\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert_eq!(out, input);
    }

    #[test]
    fn test_git_log_wrapper() {
        let input = "abc1234 Fix bug in parser (2 days ago) <alice>\n\
                     def5678 Add new feature (3 days ago) <bob>\n";
        let out = git_log_wrapper(input);
        assert!(!out.is_empty());
    }

    #[test]
    fn test_git_diff_wrapper() {
        let input = "diff --git a/src/main.rs b/src/main.rs\n\
                     --- a/src/main.rs\n\
                     +++ b/src/main.rs\n\
                     @@ -1,3 +1,4 @@\n\
                     +// new comment\n\
                      fn main() {}\n";
        let out = git_diff_wrapper(input);
        assert!(!out.is_empty());
    }

    #[test]
    fn test_resolve_filter_find() {
        let f = resolve_filter("find").expect("find filter must exist");
        let input = "./src/main.rs\n./src/lib.rs\n./tests/foo.rs\n";
        let out = f(input);
        assert!(out.contains("3 files"), "out={}", out);
    }

    #[test]
    fn test_resolve_filter_fd_alias() {
        assert!(resolve_filter("fd").is_some());
    }

    #[test]
    fn test_auto_detect_find_paths() {
        let input = "./src/main.rs\n./src/lib.rs\n./src/cmd/mod.rs\n./tests/foo.rs\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(out.contains("4 files"), "out={}", out);
    }

    #[test]
    fn test_auto_detect_find_absolute_paths() {
        let input = "/home/user/src/main.rs\n/home/user/src/lib.rs\n/home/user/tests/foo.rs\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(out.contains("3 files"), "out={}", out);
    }

    #[test]
    fn test_auto_detect_find_not_triggered_for_few_lines() {
        let input = "./src/main.rs\n./src/lib.rs\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert_eq!(out, input);
    }

    #[test]
    fn test_auto_detect_find_not_triggered_for_grep_output() {
        let input = "src/main.rs:42:fn main() {\nsrc/lib.rs:10:pub fn helper() {}\nsrc/a.rs:1:x\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(
            !out.contains("files"),
            "should not trigger find filter: out={}",
            out
        );
    }

    #[test]
    fn test_auto_detect_empty_input_is_identity() {
        let f = auto_detect_filter("");
        let out = f("");
        assert_eq!(out, "");
    }

    #[test]
    fn test_auto_detect_multibyte_at_1024_boundary() {
        // Build input where byte 1024 falls inside a multi-byte char (é = 2 bytes)
        let mut input = "a".repeat(1023);
        input.push('é'); // 2-byte char starting at byte 1023, ends at 1025
        let f = auto_detect_filter(&input);
        let out = f(&input);
        assert_eq!(out, input);
    }

    #[test]
    fn test_auto_detect_single_line_unknown() {
        let input = "hello world\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert_eq!(out, input);
    }

    #[test]
    fn test_resolve_filter_go_build() {
        assert!(resolve_filter("go-build").is_some());
    }

    #[test]
    fn test_resolve_filter_mypy() {
        assert!(resolve_filter("mypy").is_some());
    }

    #[test]
    fn test_resolve_filter_ruff_check() {
        assert!(resolve_filter("ruff-check").is_some());
    }

    #[test]
    fn test_resolve_filter_ruff_format() {
        assert!(resolve_filter("ruff-format").is_some());
    }

    #[test]
    fn test_resolve_filter_prettier() {
        assert!(resolve_filter("prettier").is_some());
    }

    #[test]
    fn test_panicking_filter_returns_passthrough() {
        fn panicking_filter(_input: &str) -> String {
            panic!("filter bug");
        }
        let input = "some output\n";
        let result = super::apply_filter(panicking_filter, input);
        assert_eq!(result, input);
    }

    fn count_tokens(s: &str) -> usize {
        s.split_whitespace().count()
    }

    #[test]
    fn test_grep_wrapper_token_savings() {
        // Realistic rg output: 200 matches across 10 files (20 per file → 10 shown + truncation)
        let mut input = String::new();
        for file_idx in 1..=10 {
            for line in 1..=20 {
                input.push_str(&format!(
                    "src/cmds/module{}/handler.rs:{}:    let result = process_request(ctx, &payload).await?;\n",
                    file_idx, line * 10
                ));
            }
        }
        let output = grep_wrapper(&input);
        let savings = 100.0 - (count_tokens(&output) as f64 / count_tokens(&input) as f64 * 100.0);
        assert!(
            savings >= 40.0, // TODO: grep pipe filter below 60% target — improve grouping
            "grep filter: expected ≥40% savings, got {:.1}% (in={}, out={})",
            savings, count_tokens(&input), count_tokens(&output)
        );
    }

    #[test]
    fn test_find_wrapper_token_savings() {
        // Realistic find output: 500 files across 30 dirs (20-dir cap + 10-file cap both trigger)
        let mut input = String::new();
        for dir in 1..=30 {
            for file in 1..=17 {
                input.push_str(&format!(
                    "./src/components/feature{}/sub_{}/component_{}.tsx\n",
                    dir, dir, file
                ));
            }
        }
        let output = find_wrapper(&input);
        let savings = 100.0 - (count_tokens(&output) as f64 / count_tokens(&input) as f64 * 100.0);
        assert!(
            savings >= 40.0, // TODO: find pipe filter below 60% target — improve grouping
            "find filter: expected ≥40% savings, got {:.1}% (in={}, out={})",
            savings, count_tokens(&input), count_tokens(&output)
        );
    }

    #[test]
    fn test_auto_detect_mypy_output() {
        let input = "src/app.py:42: error: Argument 1 has incompatible type [arg-type]\n\
                     src/utils.py:10: error: Missing return statement [return]\n\
                     Found 2 errors in 2 files\n";
        let f = auto_detect_filter(input);
        let out = f(input);
        assert!(!out.is_empty());
    }
}