codescout 0.15.0

High-performance coding agent toolkit MCP server
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
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
//! Command type detection and smart output summarization.
//!
//! Detects whether a command is a test runner, a build tool, or something else,
//! then produces a structured summary appropriate for that command type.

use regex::Regex;
use serde_json::{json, Value};
use std::sync::OnceLock;

// ---------------------------------------------------------------------------
// Thresholds
// ---------------------------------------------------------------------------

/// Inline line cap for buffer-only queries (e.g. `grep/sed @cmd_xxx`).
/// Kept separate from the summarization threshold so "when to buffer" and "how much
/// to return from a buffer query" can be tuned independently.
pub(crate) const BUFFER_QUERY_INLINE_CAP: usize = 100;

/// Number of lines to keep from the top in generic summaries.
const HEAD_LINES: usize = 20;

/// Number of lines to keep from the bottom in generic summaries.
const TAIL_LINES: usize = 10;

// ---------------------------------------------------------------------------
// CommandType
// ---------------------------------------------------------------------------

/// Broad category of the command being run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommandType {
    Test,
    Build,
    Generic,
}

// ---------------------------------------------------------------------------
// Regex patterns (compiled once via OnceLock)
// ---------------------------------------------------------------------------

fn test_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        Regex::new(
            r"(?x)
            (?:^|\s|/)
            (?:
                cargo\s+test
              | pytest
              | npm\s+test
              | npx\s+jest
              | jest
              | go\s+test
              | mvn\s+test
              | gradle\s+test
            )
            (?:\s|$)",
        )
        .expect("test regex")
    })
}

fn build_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        Regex::new(
            r"(?x)
            (?:^|\s|/)
            (?:
                cargo\s+(?:build|clippy|check)
              | npm\s+run\s+build
              | make(?:\s|$)
              | tsc(?:\s|$)
              | gcc(?:\s|$)
              | g\+\+(?:\s|$)
              | clang(?:\s|$)
              | javac(?:\s|$)
              | go\s+build
            )",
        )
        .expect("build regex")
    })
}

fn cargo_test_result_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| {
        Regex::new(r"(\d+)\s+passed;\s+(\d+)\s+failed;\s+(\d+)\s+ignored")
            .expect("cargo test regex")
    })
}

fn rust_error_code_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"^error\[E\d+\]").expect("rust error regex"))
}

fn warning_re() -> &'static Regex {
    static RE: OnceLock<Regex> = OnceLock::new();
    RE.get_or_init(|| Regex::new(r"^warning(\[.+\])?:").expect("warning regex"))
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Classify a command string into Test, Build, or Generic.
pub fn detect_command_type(command: &str) -> CommandType {
    // Test takes priority over build (e.g. "cargo test" is Test, not Build).
    if test_re().is_match(command) {
        CommandType::Test
    } else if build_re().is_match(command) {
        CommandType::Build
    } else {
        CommandType::Generic
    }
}

/// Returns the byte offset of the `|` pipe character that separates a command
/// from a terminal filter stage (grep, head, tail, sed, awk, cut, wc, sort,
/// uniq, tr, rg, egrep, fgrep).
///
/// Returns `None` if the command has no pipe, or if the last pipe stage is not
/// a known terminal filter. Pipe characters inside quoted strings are ignored.
pub fn detect_terminal_filter(cmd: &str) -> Option<usize> {
    const TERMINAL_FILTERS: &[&str] = &[
        "grep", "egrep", "fgrep", "rg", "head", "tail", "sed", "awk", "cut", "wc", "sort", "uniq",
        "tr",
    ];

    let mut last_pipe: Option<usize> = None;
    let mut in_single = false;
    let mut in_double = false;
    let mut escape_next = false;

    for (i, ch) in cmd.char_indices() {
        if escape_next {
            escape_next = false;
            continue;
        }
        match ch {
            '\\' if !in_single => escape_next = true,
            '\'' if !in_double => in_single = !in_single,
            '"' if !in_single => in_double = !in_double,
            '|' if !in_single && !in_double => {
                // Skip `||` (logical OR) — not a pipeline pipe.
                let next_char = cmd[i + 1..].chars().next();
                let prev_char = if i > 0 { cmd[..i].chars().last() } else { None };
                if next_char != Some('|') && prev_char != Some('|') {
                    last_pipe = Some(i);
                }
            }
            _ => {}
        }
    }

    let pipe_pos = last_pipe?;

    // Extract the first token of the stage after the pipe
    let after_pipe = cmd[pipe_pos + 1..].trim_start();
    let token = after_pipe
        .split(|c: char| c.is_whitespace())
        .next()
        .unwrap_or("");

    // Strip any path prefix so "/usr/bin/grep" matches "grep"
    let name = token.rsplit('/').next().unwrap_or(token);

    if TERMINAL_FILTERS.contains(&name) {
        Some(pipe_pos)
    } else {
        None
    }
}

/// Returns `true` when the combined output is large enough to benefit from
/// summarization rather than raw output.
pub fn needs_summary(stdout: &str, stderr: &str) -> bool {
    (stdout.len() + stderr.len()) / 4 > crate::tools::MAX_INLINE_TOKENS
}

/// Produce a structured summary of test-runner output.
///
/// Parses cargo-test-style result lines, sums across multiple test binaries,
/// and extracts failure details.
pub fn summarize_test_output(stdout: &str, stderr: &str, exit_code: i32) -> Value {
    let mut passed: u64 = 0;
    let mut failed: u64 = 0;
    let mut ignored: u64 = 0;

    let combined = if stderr.is_empty() {
        stdout.to_string()
    } else {
        format!("{stdout}\n{stderr}")
    };

    let re = cargo_test_result_re();
    for line in combined.lines() {
        if let Some(caps) = re.captures(line) {
            passed += caps[1].parse::<u64>().unwrap_or(0);
            failed += caps[2].parse::<u64>().unwrap_or(0);
            ignored += caps[3].parse::<u64>().unwrap_or(0);
        }
    }

    let failures = extract_test_failures(&combined);

    let mut result = json!({
        "type": "test",
        "exit_code": exit_code,
        "passed": passed,
    });

    if failed > 0 {
        result["failed"] = json!(failed);
    }
    if ignored > 0 {
        result["ignored"] = json!(ignored);
    }
    if let Some(f) = failures {
        result["failures"] = Value::String(f);
    }

    result
}

/// Produce a structured summary of compiler / build-tool output.
///
/// Counts errors (with error codes) and warnings, and extracts the first
/// error block for quick diagnosis.
pub fn summarize_build_output(stdout: &str, stderr: &str, exit_code: i32) -> Value {
    let combined = if stderr.is_empty() {
        stdout.to_string()
    } else if stdout.is_empty() {
        stderr.to_string()
    } else {
        format!("{stdout}\n{stderr}")
    };

    let mut errors: u64 = 0;
    let mut warnings: u64 = 0;
    let mut first_error: Option<String> = None;

    let err_re = rust_error_code_re();
    let warn_re = warning_re();
    let lines: Vec<&str> = combined.lines().collect();
    for (i, line) in lines.iter().enumerate() {
        if err_re.is_match(line) {
            errors += 1;
            if first_error.is_none() {
                first_error = Some(extract_error_block(&lines, i));
            }
        } else if warn_re.is_match(line) {
            warnings += 1;
        }
    }

    let mut result = json!({
        "type": "build",
        "exit_code": exit_code,
    });

    if errors > 0 {
        result["errors"] = json!(errors);
    }
    if warnings > 0 {
        result["warnings"] = json!(warnings);
    }
    if let Some(err) = first_error {
        result["first_error"] = Value::String(err);
    }

    result
}

/// Produce a head+tail summary for generic command output.
///
/// If stdout fits within HEAD_LINES + TAIL_LINES, it is returned verbatim.
/// Otherwise the middle is replaced with an "N lines omitted" marker.
pub fn summarize_generic(stdout: &str, stderr: &str, exit_code: i32) -> Value {
    let stdout_lines: Vec<&str> = stdout.lines().collect();
    let total_stdout_lines = stdout_lines.len();

    let summarized_stdout = if total_stdout_lines > HEAD_LINES + TAIL_LINES {
        let head: Vec<&str> = stdout_lines[..HEAD_LINES].to_vec();
        let tail: Vec<&str> = stdout_lines[total_stdout_lines - TAIL_LINES..].to_vec();
        let omitted = total_stdout_lines - HEAD_LINES - TAIL_LINES;
        format!(
            "{}\n--- {} lines omitted ---\n{}",
            head.join("\n"),
            omitted,
            tail.join("\n")
        )
    } else {
        stdout.to_string()
    };

    let mut result = json!({
        "type": "generic",
        "exit_code": exit_code,
    });

    if !summarized_stdout.is_empty() {
        result["stdout"] = Value::String(summarized_stdout);
    }
    if !stderr.is_empty() {
        result["stderr"] = Value::String(stderr.to_string());
    }

    result
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

pub fn count_lines(s: &str) -> usize {
    if s.is_empty() {
        0
    } else {
        s.lines().count()
    }
}

/// Truncate `text` to at most `max_lines` lines.
///
/// Returns `(truncated_text, lines_shown, lines_total)`.
/// When `lines_total <= max_lines`, `text` is returned unchanged and
/// `lines_shown == lines_total`.
#[allow(dead_code)]
pub(crate) fn truncate_lines(text: &str, max_lines: usize) -> (String, usize, usize) {
    let total = count_lines(text);
    if total <= max_lines {
        return (text.to_string(), total, total);
    }
    let truncated = text.lines().take(max_lines).collect::<Vec<_>>().join("\n");
    (truncated, max_lines, total)
}

/// Truncate `text` to at most `max_lines` lines **and** at most `max_bytes` bytes.
///
/// Both limits are applied; whichever is more restrictive wins. Truncation
/// always occurs on a line boundary so output is never split mid-line.
///
/// Returns `(content, lines_shown, total_lines)`.
pub(crate) fn truncate_lines_and_bytes(
    text: &str,
    max_lines: usize,
    max_bytes: usize,
) -> (String, usize, usize) {
    let total = count_lines(text);
    let mut result = String::new();
    let mut lines_shown = 0;

    for line in text.lines().take(max_lines) {
        // Each line adds the line itself plus a '\n' separator (except the first).
        let needed = if result.is_empty() {
            line.len()
        } else {
            line.len() + 1 // +1 for '\n'
        };
        if result.len() + needed > max_bytes {
            break;
        }
        if !result.is_empty() {
            result.push('\n');
        }
        result.push_str(line);
        lines_shown += 1;
    }

    (result, lines_shown, total)
}

/// Extract text between the first `failures:` section markers in cargo test output.
fn extract_test_failures(output: &str) -> Option<String> {
    // Cargo test outputs failures between two "failures:" markers.
    // The first "failures:" is followed by stdout of failing tests.
    // The second "failures:" is followed by test names.
    let lines: Vec<&str> = output.lines().collect();
    let mut start = None;
    let mut end = None;

    for (i, line) in lines.iter().enumerate() {
        if line.trim() == "failures:" {
            if start.is_none() {
                start = Some(i);
            } else {
                end = Some(i);
                break;
            }
        }
    }

    // If we found at least one "failures:" marker, collect everything after it
    // up to the second marker (or end of output).
    if let Some(s) = start {
        let e = end.unwrap_or(lines.len());
        // Include from start through the second failures block
        let section: Vec<&str> = if let Some(end_idx) = end {
            // Find the end of the second failures block (next "test result:" line or EOF)
            let block_end = lines[end_idx..]
                .iter()
                .position(|l| l.starts_with("test result:"))
                .map(|p| end_idx + p)
                .unwrap_or(lines.len());
            lines[s..block_end].to_vec()
        } else {
            lines[s..e].to_vec()
        };

        let text = section.join("\n").trim().to_string();
        if text.is_empty() {
            None
        } else {
            Some(text)
        }
    } else {
        None
    }
}

/// Extract an error block: the error line plus continuation lines until
/// the next blank line or next error/warning.
fn extract_error_block(lines: &[&str], start: usize) -> String {
    let err_re = rust_error_code_re();
    let warn_re = warning_re();
    let mut block = vec![lines[start]];
    for line in &lines[start + 1..] {
        // Stop at blank lines or next top-level diagnostic
        if line.is_empty()
            || err_re.is_match(line)
            || warn_re.is_match(line)
            || line.starts_with("error:")
        {
            break;
        }
        block.push(line);
    }
    block.join("\n")
}

/// Strip ANSI CSI escape sequences from `s` (e.g. `\x1b[32m`, `\x1b[0m`).
///
/// Call this on buffered command output before byte-budget calculations. ANSI
/// codes are opaque to LLMs and inflate byte lengths — on ANSI-colored log
/// files a single grep match line can be several KB of escape codes around a
/// few dozen bytes of visible text, silently exhausting the byte budget and
/// causing `stdout_shown=0`.
///
/// Only CSI sequences (`ESC '['` … final ASCII letter) are removed. Other
/// escape types (rare in log files) pass through unchanged.
pub(crate) fn strip_ansi_codes(s: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let mut chars = s.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '\x1b' {
            match chars.peek().copied() {
                Some('[') => {
                    chars.next(); // consume '['
                    for c in chars.by_ref() {
                        if c.is_ascii_alphabetic() {
                            break; // final byte consumed
                        }
                    }
                }
                _ => {
                    // Non-CSI escape — preserve; rare in structured log output.
                    result.push(ch);
                }
            }
        } else {
            result.push(ch);
        }
    }
    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- detect_command_type --

    #[test]
    fn strip_ansi_codes_removes_color_sequences() {
        let input = "\x1b[32mINFO\x1b[0m some message";
        assert_eq!(strip_ansi_codes(input), "INFO some message");
    }

    #[test]
    fn strip_ansi_codes_removes_256_color_sequences() {
        // 256-color and bold sequences have multiple parameters separated by ';'
        let input = "\x1b[1;38;5;220mWARN\x1b[0m something";
        assert_eq!(strip_ansi_codes(input), "WARN something");
    }

    #[test]
    fn strip_ansi_codes_plain_text_unchanged() {
        let input = "no escape codes here\nline two";
        assert_eq!(strip_ansi_codes(input), input);
    }

    #[test]
    fn strip_ansi_codes_preserves_newlines() {
        let input = "\x1b[32mline1\x1b[0m\nline2\n\x1b[31mline3\x1b[0m";
        assert_eq!(strip_ansi_codes(input), "line1\nline2\nline3");
    }

    #[test]
    fn strip_ansi_codes_non_csi_escape_preserved() {
        // ESC followed by non-'[' is not a CSI sequence — preserved as-is
        let input = "\x1b=hello";
        assert_eq!(strip_ansi_codes(input), "\x1b=hello");
    }

    #[test]
    fn strip_ansi_codes_empty_string() {
        assert_eq!(strip_ansi_codes(""), "");
    }

    #[test]
    fn detect_test_command() {
        assert_eq!(detect_command_type("cargo test"), CommandType::Test);
        assert_eq!(
            detect_command_type("cargo test --release"),
            CommandType::Test
        );
        assert_eq!(detect_command_type("pytest tests/"), CommandType::Test);
        assert_eq!(detect_command_type("npm test"), CommandType::Test);
        assert_eq!(detect_command_type("npx jest"), CommandType::Test);
        assert_eq!(detect_command_type("go test ./..."), CommandType::Test);
    }

    #[test]
    fn detect_build_command() {
        assert_eq!(detect_command_type("cargo build"), CommandType::Build);
        assert_eq!(
            detect_command_type("cargo clippy -- -D warnings"),
            CommandType::Build
        );
        assert_eq!(detect_command_type("npm run build"), CommandType::Build);
        assert_eq!(detect_command_type("make"), CommandType::Build);
        assert_eq!(detect_command_type("tsc"), CommandType::Build);
        assert_eq!(detect_command_type("gcc main.c"), CommandType::Build);
    }

    #[test]
    fn detect_generic_command() {
        assert_eq!(detect_command_type("echo hello"), CommandType::Generic);
        assert_eq!(detect_command_type("ls -la"), CommandType::Generic);
        assert_eq!(detect_command_type("cat file.txt"), CommandType::Generic);
    }

    // -- needs_summary --

    #[test]
    fn short_output_not_summarized() {
        assert!(!needs_summary("hello\nworld\n", ""));
    }

    #[test]
    fn long_output_needs_summary() {
        // Generate output exceeding MAX_INLINE_TOKENS * 4 bytes (~10KB)
        let stdout: String = (1..=3000).map(|i| format!("line {}\n", i)).collect();
        assert!(needs_summary(&stdout, ""));
    }

    // -- summarize_test_output --

    #[test]
    fn summarize_cargo_test_all_pass() {
        let stdout = "running 5 tests\ntest a ... ok\ntest b ... ok\ntest c ... ok\ntest d ... ok\ntest e ... ok\n\ntest result: ok. 5 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.02s\n";
        let summary = summarize_test_output(stdout, "", 0);
        assert_eq!(summary["passed"], 5);
        assert!(
            summary.get("failed").is_none(),
            "failed:0 should be omitted"
        );
        assert!(
            summary.get("ignored").is_none(),
            "ignored:0 should be omitted"
        );
        assert!(summary.get("failures").is_none() || summary["failures"].is_null());
    }

    #[test]
    fn summarize_cargo_test_with_failures() {
        let stdout = "running 3 tests\ntest ok_test ... ok\ntest failing_test ... FAILED\ntest another ... ok\n\nfailures:\n\n---- failing_test stdout ----\nthread 'failing_test' panicked at 'assertion failed'\n\nfailures:\n    failing_test\n\ntest result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out\n";
        let summary = summarize_test_output(stdout, "", 1);
        assert_eq!(summary["passed"], 2);
        assert_eq!(summary["failed"], 1);
        let failures = summary["failures"].as_str().unwrap();
        assert!(failures.contains("failing_test"));
    }

    #[test]
    fn summarize_cargo_test_multiple_binaries() {
        let stdout = "\
running 3 tests
test a ... ok
test b ... ok
test c ... ok

test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 2 tests
test d ... ok
test e ... FAILED

failures:

---- e stdout ----
assertion failed

failures:
    e

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
";
        let summary = summarize_test_output(stdout, "", 1);
        // Sums across both binaries
        assert_eq!(summary["passed"], 4);
        assert_eq!(summary["failed"], 1);
    }

    // -- summarize_build_output --

    #[test]
    fn summarize_build_errors() {
        let stderr = "error[E0308]: mismatched types\n --> src/main.rs:5:20\n  |\n5 |     let x: String = 42;\n  |                     ^^ expected `String`, found integer\n\nwarning: unused variable: `y`\n --> src/main.rs:3:9\n  |\n3 |     let y = 1;\n  |         ^ help: consider prefixing with an underscore: `_y`\n\nerror: aborting due to 1 previous error; 1 warning emitted\n";
        let summary = summarize_build_output("", stderr, 1);
        assert_eq!(summary["errors"], 1); // only error[E...], not "error: aborting"
        assert_eq!(summary["warnings"], 1);
        assert!(summary["first_error"].as_str().unwrap().contains("E0308"));
    }

    #[test]
    fn summarize_build_no_errors() {
        let stderr = "warning: unused variable: `x`\n --> src/main.rs:2:9\n";
        let summary = summarize_build_output("", stderr, 0);
        assert!(
            summary.get("errors").is_none(),
            "errors:0 should be omitted"
        );
        assert_eq!(summary["warnings"], 1);
        assert!(summary.get("first_error").is_none() || summary["first_error"].is_null());
    }

    // -- summarize_generic --

    #[test]
    fn summarize_generic_head_tail() {
        let lines: String = (1..=100).map(|i| format!("line {}\n", i)).collect();
        let summary = summarize_generic(&lines, "", 0);
        let output = summary["stdout"].as_str().unwrap();
        assert!(output.contains("line 1"));
        assert!(output.contains("line 20"));
        assert!(output.contains("lines omitted"));
        assert!(output.contains("line 100"));
    }

    #[test]
    fn summarize_generic_short_output_verbatim() {
        let stdout = "line 1\nline 2\nline 3\n";
        let summary = summarize_generic(stdout, "", 0);
        let output = summary["stdout"].as_str().unwrap();
        assert_eq!(output, stdout);
        assert!(!output.contains("omitted"));
    }

    #[test]
    fn summarize_generic_includes_stderr() {
        let summary = summarize_generic("out\n", "err\n", 1);
        assert!(summary.get("stderr").is_some());
        assert_eq!(summary["stderr"].as_str().unwrap(), "err\n");
    }

    #[test]
    fn summarize_generic_omits_empty_stderr() {
        let summary = summarize_generic("out\n", "", 0);
        assert!(summary.get("stderr").is_none() || summary["stderr"].is_null());
    }

    #[test]
    fn summarize_generic_omits_empty_stdout() {
        let summary = summarize_generic("", "err\n", 1);
        assert!(summary.get("stdout").is_none() || summary["stdout"].is_null());
        assert_eq!(summary["stderr"].as_str().unwrap(), "err\n");
    }

    // -- helpers --

    #[test]
    fn count_lines_empty() {
        assert_eq!(count_lines(""), 0);
    }

    #[test]
    fn count_lines_normal() {
        assert_eq!(count_lines("a\nb\nc"), 3);
    }

    #[test]
    fn truncate_lines_short_returns_unchanged() {
        let text = "a\nb\nc";
        let (out, shown, total) = truncate_lines(text, 10);
        assert_eq!(out, text);
        assert_eq!(shown, 3);
        assert_eq!(total, 3);
    }

    #[test]
    fn truncate_lines_exact_limit_not_truncated() {
        let text: String = (1..=5)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let (out, shown, total) = truncate_lines(&text, 5);
        assert_eq!(shown, 5);
        assert_eq!(total, 5);
        assert_eq!(out, text);
    }

    #[test]
    fn truncate_lines_long_truncates_correctly() {
        let text: String = (1..=10)
            .map(|i| format!("line {i}"))
            .collect::<Vec<_>>()
            .join("\n");
        let (out, shown, total) = truncate_lines(&text, 3);
        assert_eq!(shown, 3);
        assert_eq!(total, 10);
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines.len(), 3);
        assert_eq!(lines[0], "line 1");
        assert_eq!(lines[2], "line 3");
    }

    #[test]
    fn truncate_lines_empty_string() {
        let (out, shown, total) = truncate_lines("", 10);
        assert_eq!(out, "");
        assert_eq!(shown, 0);
        assert_eq!(total, 0);
    }

    #[test]
    fn truncate_lines_and_bytes_line_limit_wins() {
        // 3 lines, byte budget is generous — line limit (2) wins.
        let text = "aaa\nbbb\nccc";
        let (out, shown, total) = truncate_lines_and_bytes(text, 2, 1000);
        assert_eq!(out, "aaa\nbbb");
        assert_eq!(shown, 2);
        assert_eq!(total, 3);
    }

    #[test]
    fn truncate_lines_and_bytes_byte_limit_wins() {
        // 5 short lines, byte budget forces truncation after the 2nd line.
        // "aaa\nbbb" = 7 bytes; adding "\nccc" = 11 bytes — over the 8-byte budget.
        let text = "aaa\nbbb\nccc\nddd\neee";
        let (out, shown, total) = truncate_lines_and_bytes(text, 10, 8);
        assert_eq!(out, "aaa\nbbb");
        assert_eq!(shown, 2);
        assert_eq!(total, 5);
    }

    #[test]
    fn truncate_lines_and_bytes_both_fit() {
        let text = "a\nb\nc";
        let (out, shown, total) = truncate_lines_and_bytes(text, 10, 1000);
        assert_eq!(out, text);
        assert_eq!(shown, 3);
        assert_eq!(total, 3);
    }

    #[test]
    fn truncate_lines_and_bytes_long_lines_stay_under_byte_budget() {
        // Simulate log output: 200-char lines, budget = 10_000 bytes (TOOL_OUTPUT_BUFFER_THRESHOLD).
        let long_line = "x".repeat(200);
        let text: String = (0..100).map(|_| format!("{long_line}\n")).collect();
        let (out, shown, _total) = truncate_lines_and_bytes(&text, 100, 10_000);
        // Must fit within budget.
        assert!(
            out.len() <= 10_000,
            "output {} bytes exceeds budget",
            out.len()
        );
        // Should have shown fewer than 100 lines (200-char lines can't all fit in 10KB).
        assert!(shown < 100, "expected byte truncation, shown={shown}");
    }

    // -- detect_terminal_filter --

    #[test]
    fn terminal_filter_grep() {
        let pos = detect_terminal_filter("cargo build 2>&1 | grep error");
        assert!(pos.is_some());
    }

    #[test]
    fn terminal_filter_head() {
        let pos = detect_terminal_filter("cat big_file.log | head -20");
        assert!(pos.is_some());
    }

    #[test]
    fn terminal_filter_tail() {
        let pos = detect_terminal_filter("journalctl | tail -100");
        assert!(pos.is_some());
    }

    #[test]
    fn terminal_filter_no_pipe() {
        assert!(detect_terminal_filter("cargo build").is_none());
    }

    #[test]
    fn terminal_filter_non_filter_pipe() {
        // Second stage is not a known filter
        assert!(detect_terminal_filter("cat file | cargo install").is_none());
    }

    #[test]
    fn terminal_filter_quoted_pipe_ignored() {
        // Pipe inside quotes is not a real pipe
        assert!(detect_terminal_filter("echo 'foo | bar'").is_none());
    }

    #[test]
    fn terminal_filter_nested_filters_last_wins() {
        // cmd | sed | grep  →  finds the grep (last) pipe
        let cmd = "cat file | sed 's/x/y/' | grep foo";
        let pos = detect_terminal_filter(cmd);
        assert!(pos.is_some());
        // The position should be the second pipe (before grep), not the first
        let pipe_pos = pos.unwrap();
        assert!(cmd[pipe_pos + 1..].trim_start().starts_with("grep"));
    }

    #[test]
    fn terminal_filter_returns_pipe_position() {
        let cmd = "cargo build | grep error";
        let pos = detect_terminal_filter(cmd).unwrap();
        // Character at pipe_pos should be '|'
        assert_eq!(&cmd[pos..pos + 1], "|");
    }

    #[test]
    fn terminal_filter_logical_or_not_a_pipe() {
        // || is logical OR, not a pipeline — should not trigger tee injection
        assert!(detect_terminal_filter("ls || grep foo").is_none());
    }

    #[test]
    fn terminal_filter_path_prefixed_filter() {
        // /usr/bin/grep should be recognized as grep
        let pos = detect_terminal_filter("ls | /usr/bin/grep foo");
        assert!(pos.is_some());
    }

    #[test]
    fn bash_stderr_pipe_not_treated_as_filter() {
        // |& is bash's stderr pipe — the & becomes part of the filter name
        // ("&grep"), which doesn't match any known filter, so correctly returns None
        assert!(detect_terminal_filter("cmd |& grep foo").is_none());
    }
}