lean-ctx 3.9.18

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
macro_rules! static_regex {
    ($pattern:expr_2021) => {{
        static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
        RE.get_or_init(|| {
            regex::Regex::new($pattern).expect(concat!("BUG: invalid static regex: ", $pattern))
        })
    }};
}

fn compiling_re() -> &'static regex::Regex {
    static_regex!(r"Compiling (\S+) v(\S+)")
}
fn checking_re() -> &'static regex::Regex {
    static_regex!(r"Checking (\S+) v(\S+)")
}
fn error_re() -> &'static regex::Regex {
    static_regex!(r"error\[E(\d+)\]: (.+)")
}
fn warning_re() -> &'static regex::Regex {
    static_regex!(r"warning(?:\[clippy::([^\]]+)\])?: (.+)")
}
fn generated_warnings_re() -> &'static regex::Regex {
    static_regex!(r"generated (\d+) warnings?")
}
fn generic_error_re() -> &'static regex::Regex {
    static_regex!(r"error(?:\[E\d+\])?: (.+)")
}
fn clippy_rule_re() -> &'static regex::Regex {
    static_regex!(r"clippy::([A-Za-z0-9_-]+)")
}
fn failed_test_re() -> &'static regex::Regex {
    static_regex!(r"^test (.+) \.\.\. FAILED$")
}
fn failed_test_header_re() -> &'static regex::Regex {
    static_regex!(r"^---- (.+) stdout ----$")
}
fn test_result_re() -> &'static regex::Regex {
    static_regex!(r"test result: (\w+)\. (\d+) passed; (\d+) failed; (\d+) ignored")
}
fn finished_re() -> &'static regex::Regex {
    static_regex!(r"Finished .+ in (\d+\.?\d*s)")
}

/// Compress output from a recognized Cargo subcommand.
pub fn compress(command: &str, output: &str) -> Option<String> {
    let args = command.strip_prefix("cargo ").unwrap_or(command);
    let subcmd = args.split_whitespace().next().unwrap_or("");
    match subcmd {
        "build" | "b" | "check" | "c" => Some(compress_build(output)),
        "test" | "t" | "nextest" => Some(compress_test(output)),
        "clippy" => Some(compress_clippy(output)),
        "clean" => Some(compress_clean(output)),
        "install" => Some(compress_install(output)),
        "add" => Some(compress_add(output)),
        "remove" | "rm" => Some(compress_remove(output)),
        "doc" | "d" => Some(compress_doc(output)),
        "tree" => Some(compress_tree(output)),
        "fmt" => Some(compress_fmt(output)),
        "update" | "up" => Some(compress_update(output)),
        "metadata" => Some(compress_metadata(output)),
        "run" | "r" => Some(compress_run(output)),
        "bench" => Some(compress_bench(output)),
        _ => None,
    }
}

fn compress_build(output: &str) -> String {
    let mut compiled = 0u32;
    let mut checked = 0u32;
    let mut errors = Vec::new();
    let mut time = String::new();

    for line in output.lines() {
        if compiling_re().is_match(line) {
            compiled += 1;
        } else if checking_re().is_match(line) {
            checked += 1;
        }
        if let Some(caps) = error_re().captures(line) {
            errors.push(format!("E{}: {}", &caps[1], &caps[2]));
        }
        if let Some(caps) = finished_re().captures(line) {
            time = caps[1].to_string();
        }
    }

    let mut parts = Vec::new();
    if compiled > 0 {
        parts.push(counted_crates("compiled", compiled));
    }
    if checked > 0 {
        parts.push(counted_crates("checked", checked));
    }
    if !errors.is_empty() {
        parts.push(format!("{} errors:", errors.len()));
        for e in &errors {
            parts.push(format!("  {e}"));
        }
    }
    let warning_groups = group_warnings(output);
    let warning_total = warning_total(output, &warning_groups);
    if warning_total > 0 {
        parts.push(format_warning_groups(&warning_groups, warning_total));
    }
    if !time.is_empty() {
        parts.push(format!("({time})"));
    }

    if parts.is_empty() {
        return "ok".to_string();
    }
    parts.join("\n")
}

fn compress_test(output: &str) -> String {
    let mut failed_tests = Vec::new();
    let mut passed = 0u32;
    let mut failed = 0u32;
    let mut skipped = 0u32;
    let mut time = String::new();
    let mut compile_lines = Vec::new();
    let mut passed_names: Vec<String> = Vec::new();
    let mut in_test_phase = false;

    for line in output.lines() {
        if let Some(caps) = test_result_re().captures(line) {
            passed += caps[2].parse::<u32>().unwrap_or_default();
            failed += caps[3].parse::<u32>().unwrap_or_default();
            skipped += caps[4].parse::<u32>().unwrap_or_default();
            in_test_phase = true;
        } else if line.trim_start().starts_with("running ") {
            in_test_phase = true;
        } else if !in_test_phase {
            compile_lines.push(line);
        }
        let trimmed = line.trim();
        if trimmed.starts_with("test ") && trimmed.ends_with("... ok") {
            if let Some(name) = trimmed
                .strip_prefix("test ")
                .and_then(|r| r.strip_suffix(" ... ok"))
            {
                passed_names.push(name.to_string());
            }
        }
        if let Some(caps) = failed_test_re().captures(trimmed) {
            failed_tests.push(caps[1].to_string());
        } else if let Some(caps) = failed_test_header_re().captures(trimmed) {
            failed_tests.push(caps[1].to_string());
        }
        if let Some(caps) = finished_re().captures(line) {
            time = caps[1].to_string();
        }
    }

    let mut parts = Vec::new();
    let compile_summary = compile_phase_summary(&compile_lines.join("\n"));
    if !compile_summary.is_empty() {
        parts.push(format!("[{compile_summary}]"));
    }
    if passed > 0 || failed > 0 || skipped > 0 {
        let mut result = format!("cargo test: {passed} passed, {failed} failed");
        if skipped > 0 {
            result.push_str(&format!(", {skipped} skipped"));
        }
        failed_tests.sort_unstable();
        failed_tests.dedup();
        if !failed_tests.is_empty() {
            let shown = failed_tests.iter().take(5).cloned().collect::<Vec<_>>();
            let suffix = if failed_tests.len() > shown.len() {
                format!(", ... +{} more", failed_tests.len() - shown.len())
            } else {
                String::new()
            };
            result.push_str(&format!(" ({}){suffix}", shown.join(", ")));
        }
        if failed_tests.is_empty() && !passed_names.is_empty() {
            let total = passed_names.len();
            let shown: Vec<_> = passed_names.iter().take(5).cloned().collect();
            let suffix = if total > 5 {
                format!(" ...+{} more", total - 5)
            } else {
                String::new()
            };
            result.push_str(&format!(
                "
  ran: {}{suffix}",
                shown.join(", ")
            ));
        }
        parts.push(result);
    }
    if !time.is_empty() {
        parts.push(format!("({time})"));
    }

    if parts.is_empty() {
        return "ok".to_string();
    }
    parts.join("\n")
}

fn compress_clippy(output: &str) -> String {
    let errors = group_clippy_errors(output);
    let warnings = group_warnings(output);
    let warning_total = warning_total(output, &warnings);

    let mut parts = Vec::new();
    if !errors.is_empty() {
        let error_total = errors.iter().map(|(_, count)| count).sum();
        parts.push(format_rule_groups(
            &errors,
            error_total,
            "error",
            "errors",
            "rules",
        ));
    }
    if warning_total > 0 {
        parts.push(format_rule_groups(
            &warnings,
            warning_total,
            "warning",
            "warnings",
            "rules",
        ));
    }

    if parts.is_empty() {
        return "clean".to_string();
    }
    parts.join("\n")
}

fn group_warnings(output: &str) -> Vec<(String, u32)> {
    let mut counts = std::collections::BTreeMap::new();
    for line in output.lines() {
        let Some(caps) = warning_re().captures(line.trim()) else {
            continue;
        };
        let message = caps.get(2).map_or("", |capture| capture.as_str());
        if message.contains("generated ") {
            continue;
        }
        let rule = caps
            .get(1)
            .map(|capture| normalize_rule(capture.as_str()))
            .unwrap_or_else(|| message_prefix(message));
        *counts.entry(rule).or_insert(0) += 1;
    }
    let mut groups: Vec<_> = counts.into_iter().collect();
    groups.sort_unstable_by_key(|(name, count)| (std::cmp::Reverse(*count), name.clone()));
    groups
}

fn group_clippy_errors(output: &str) -> Vec<(String, u32)> {
    let mut rules = Vec::new();
    let mut last_error = None;
    for line in output.lines() {
        let trimmed = line.trim();
        if let Some(caps) = generic_error_re().captures(trimmed) {
            let message = caps.get(1).map_or("", |capture| capture.as_str());
            let rule = clippy_rule_re()
                .captures(trimmed)
                .and_then(|rule_caps| rule_caps.get(1))
                .map_or_else(
                    || message_prefix(message),
                    |capture| normalize_rule(capture.as_str()),
                );
            rules.push(rule);
            last_error = Some(rules.len() - 1);
        } else if let Some(index) = last_error
            && let Some(caps) = clippy_rule_re().captures(trimmed)
            && let Some(rule) = caps.get(1)
        {
            rules[index] = normalize_rule(rule.as_str());
        }
    }
    group_named_rules(rules)
}

fn group_named_rules(rules: Vec<String>) -> Vec<(String, u32)> {
    let mut counts = std::collections::BTreeMap::new();
    for rule in rules {
        *counts.entry(rule).or_insert(0) += 1;
    }
    let mut groups: Vec<_> = counts.into_iter().collect();
    groups.sort_unstable_by_key(|(name, count)| (std::cmp::Reverse(*count), name.clone()));
    groups
}

fn format_warning_groups(groups: &[(String, u32)], total: u32) -> String {
    format_rule_groups(groups, total, "warning", "warnings", "others")
}

fn format_rule_groups(
    groups: &[(String, u32)],
    total: u32,
    singular: &str,
    plural: &str,
    remainder_label: &str,
) -> String {
    let noun = if total == 1 { singular } else { plural };
    let shown = groups
        .iter()
        .take(5)
        .map(|(rule, count)| format!("{rule} ×{count}"))
        .collect::<Vec<_>>();
    let remainder = groups.len().saturating_sub(shown.len());
    let suffix = if remainder > 0 {
        format!(", +{remainder} {remainder_label}")
    } else {
        String::new()
    };
    format!("{total} {noun} ({}){suffix}", shown.join(", "))
}

fn warning_total(output: &str, groups: &[(String, u32)]) -> u32 {
    let generated = output
        .lines()
        .filter_map(|line| generated_warnings_re().captures(line))
        .filter_map(|caps| caps[1].parse::<u32>().ok())
        .sum();
    if generated == 0 {
        groups.iter().map(|(_, count)| count).sum()
    } else {
        generated
    }
}

fn compile_phase_summary(output: &str) -> String {
    let compiled = output
        .lines()
        .filter(|line| compiling_re().is_match(line))
        .count() as u32;
    let checked = output
        .lines()
        .filter(|line| checking_re().is_match(line))
        .count() as u32;
    let groups = group_warnings(output);
    let warnings = warning_total(output, &groups);
    let mut parts = Vec::new();
    if compiled > 0 {
        parts.push(counted_crates("compiled", compiled));
    }
    if checked > 0 {
        parts.push(counted_crates("checked", checked));
    }
    if warnings > 0 {
        let noun = if warnings == 1 { "warning" } else { "warnings" };
        parts.push(format!("{warnings} {noun}"));
    }
    parts.join(", ")
}

fn counted_crates(action: &str, count: u32) -> String {
    let noun = if count == 1 { "crate" } else { "crates" };
    format!("{action} {count} {noun}")
}

fn message_prefix(message: &str) -> String {
    message
        .split_whitespace()
        .next()
        .map(normalize_rule)
        .unwrap_or_else(|| "unknown".to_string())
}

fn normalize_rule(rule: &str) -> String {
    rule.trim_matches('`').replace('-', "_")
}

fn compress_clean(output: &str) -> String {
    output
        .lines()
        .find_map(|line| line.trim().strip_prefix("Removed "))
        .map_or_else(
            || "cleaned".to_string(),
            |removed| {
                format!(
                    "removed {}",
                    removed.split_once(',').map_or(removed, |(files, _)| files)
                )
            },
        )
}

fn compress_install(output: &str) -> String {
    summarize_dependency_action(output, "Installed ", "installed")
}

fn compress_add(output: &str) -> String {
    summarize_dependency_action(output, "Adding ", "added")
}

fn compress_remove(output: &str) -> String {
    summarize_dependency_action(output, "Removing ", "removed")
}

fn summarize_dependency_action(output: &str, prefix: &str, action: &str) -> String {
    output
        .lines()
        .find_map(|line| line.trim().strip_prefix(prefix))
        .and_then(|dependency| dependency.split_whitespace().next())
        .map_or_else(
            || action.to_string(),
            |dependency| format!("{action} {dependency}"),
        )
}

fn compress_doc(output: &str) -> String {
    let mut crate_count = 0u32;
    let mut warnings = 0u32;
    let mut time = String::new();

    for line in output.lines() {
        if line.contains("Documenting ") || compiling_re().is_match(line) {
            crate_count += 1;
        }
        if warning_re().is_match(line) && !line.contains("generated") {
            warnings += 1;
        }
        if let Some(caps) = finished_re().captures(line) {
            time = caps[1].to_string();
        }
    }

    let mut parts = Vec::new();
    if crate_count > 0 {
        parts.push(format!("documented {crate_count} crates"));
    }
    if warnings > 0 {
        parts.push(format!("{warnings} warnings"));
    }
    if !time.is_empty() {
        parts.push(format!("({time})"));
    }
    if parts.is_empty() {
        "ok".to_string()
    } else {
        parts.join("\n")
    }
}

fn compress_tree(output: &str) -> String {
    let lines: Vec<&str> = output.lines().collect();
    if lines.len() <= 20 {
        return output.to_string();
    }

    let direct: Vec<&str> = lines
        .iter()
        .filter(|l| !l.starts_with(' ') || l.starts_with("├── ") || l.starts_with("└── "))
        .copied()
        .collect();

    if direct.is_empty() {
        let shown = &lines[..20.min(lines.len())];
        return format!(
            "{}\n... ({} more lines)",
            shown.join("\n"),
            lines.len() - 20
        );
    }

    format!(
        "{} direct deps ({} total lines):\n{}",
        direct.len(),
        lines.len(),
        direct.join("\n")
    )
}

fn compress_fmt(output: &str) -> String {
    let trimmed = output.trim();
    if trimmed.is_empty() {
        return "ok (formatted)".to_string();
    }

    let diffs: Vec<&str> = trimmed
        .lines()
        .filter(|l| l.starts_with("Diff in ") || l.starts_with("  --> "))
        .collect();

    if !diffs.is_empty() {
        return format!("{} formatting issues:\n{}", diffs.len(), diffs.join("\n"));
    }

    let lines: Vec<&str> = trimmed.lines().filter(|l| !l.trim().is_empty()).collect();
    if lines.len() <= 5 {
        lines.join("\n")
    } else {
        format!(
            "{}\n... ({} more lines)",
            lines[..5].join("\n"),
            lines.len() - 5
        )
    }
}

fn compress_update(output: &str) -> String {
    let mut updated = Vec::new();
    let mut unchanged = 0u32;

    for line in output.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("Updating ") || trimmed.starts_with("    Updating ") {
            updated.push(trimmed.trim_start_matches("    ").to_string());
        } else if trimmed.starts_with("Unchanged ") || trimmed.contains("Unchanged") {
            unchanged += 1;
        }
    }

    if updated.is_empty() && unchanged == 0 {
        let lines: Vec<&str> = output.lines().filter(|l| !l.trim().is_empty()).collect();
        if lines.is_empty() {
            return "ok (up-to-date)".to_string();
        }
        if lines.len() <= 5 {
            return lines.join("\n");
        }
        return format!(
            "{}\n... ({} more lines)",
            lines[..5].join("\n"),
            lines.len() - 5
        );
    }

    let mut parts = Vec::new();
    if !updated.is_empty() {
        parts.push(format!("{} updated:", updated.len()));
        for u in updated.iter().take(15) {
            parts.push(format!("  {u}"));
        }
        if updated.len() > 15 {
            parts.push(format!("  ... +{} more", updated.len() - 15));
        }
    }
    if unchanged > 0 {
        parts.push(format!("{unchanged} unchanged"));
    }
    parts.join("\n")
}

fn compress_run(output: &str) -> String {
    let mut program_lines = Vec::new();
    let mut compiling = 0u32;
    let mut time = String::new();

    for line in output.lines() {
        let trimmed = line.trim();
        if compiling_re().is_match(trimmed) || trimmed.starts_with("Compiling ") {
            compiling += 1;
            continue;
        }
        if trimmed.starts_with("Downloading ")
            || trimmed.starts_with("Downloaded ")
            || trimmed.starts_with("Blocking waiting")
            || trimmed.starts_with("Locking ")
        {
            continue;
        }
        if trimmed.starts_with("Running `") || trimmed.starts_with("Running ") {
            continue;
        }
        if let Some(caps) = finished_re().captures(trimmed) {
            time = caps[1].to_string();
            continue;
        }
        program_lines.push(line);
    }

    let mut result = String::new();
    if compiling > 0 {
        result.push_str(&format!("(compiled {compiling} crates"));
        if !time.is_empty() {
            result.push_str(&format!(", {time}"));
        }
        result.push_str(")\n");
    }

    if program_lines.len() <= 50 {
        result.push_str(&program_lines.join("\n"));
    } else {
        result.push_str(&program_lines[..25].join("\n"));
        result.push_str(&format!(
            "\n... ({} lines omitted)\n",
            program_lines.len() - 50
        ));
        result.push_str(&program_lines[program_lines.len() - 25..].join("\n"));
    }

    if result.trim().is_empty() {
        return "ok".to_string();
    }
    result
}

fn compress_bench(output: &str) -> String {
    let mut compiling = 0u32;
    let mut bench_results = Vec::new();
    let mut time = String::new();
    let mut errors = Vec::new();

    for line in output.lines() {
        let trimmed = line.trim();
        if compiling_re().is_match(trimmed) || trimmed.starts_with("Compiling ") {
            compiling += 1;
            continue;
        }
        if trimmed.starts_with("Downloading ")
            || trimmed.starts_with("Downloaded ")
            || trimmed.starts_with("Blocking waiting")
            || trimmed.starts_with("Locking ")
        {
            continue;
        }
        if trimmed.starts_with("Benchmarking ")
            || trimmed.starts_with("Gnuplot ")
            || trimmed.starts_with("Collecting ")
            || trimmed.starts_with("Warming up")
            || trimmed.starts_with("Analyzing ")
        {
            continue;
        }
        if trimmed.starts_with("Running ") && trimmed.contains("target") {
            continue;
        }
        if let Some(caps) = finished_re().captures(trimmed) {
            time = caps[1].to_string();
            continue;
        }
        if let Some(caps) = error_re().captures(trimmed) {
            errors.push(format!("E{}: {}", &caps[1], &caps[2]));
            continue;
        }
        if trimmed.starts_with("test ") && trimmed.contains("bench:") {
            bench_results.push(trimmed.to_string());
            continue;
        }
        if trimmed.contains("time:") || trimmed.contains("thrpt:") {
            bench_results.push(trimmed.to_string());
            continue;
        }
        if let Some(caps) = test_result_re().captures(trimmed) {
            bench_results.push(format!(
                "{}: {} pass, {} fail, {} skip",
                &caps[1], &caps[2], &caps[3], &caps[4]
            ));
        }
    }

    let mut parts = Vec::new();

    if !errors.is_empty() {
        parts.push(format!("{} errors:", errors.len()));
        for e in &errors {
            parts.push(format!("  {e}"));
        }
        return parts.join("\n");
    }

    if compiling > 0 {
        let mut header = format!("compiled {compiling} crates");
        if !time.is_empty() {
            header.push_str(&format!(" ({time})"));
        }
        parts.push(header);
    }

    if bench_results.is_empty() {
        parts.push("no benchmark results captured".to_string());
    } else {
        parts.push(format!("{} benchmarks:", bench_results.len()));
        for b in &bench_results {
            parts.push(format!("  {b}"));
        }
    }

    if parts.is_empty() {
        return "ok".to_string();
    }
    parts.join("\n")
}

fn compress_metadata(output: &str) -> String {
    let parsed: Result<serde_json::Value, _> = serde_json::from_str(output);
    let Ok(json) = parsed else {
        let lines: Vec<&str> = output.lines().collect();
        if lines.len() <= 20 {
            return output.to_string();
        }
        return format!(
            "{}\n... ({} more lines, non-JSON metadata)",
            lines[..10].join("\n"),
            lines.len() - 10
        );
    };

    let mut parts = Vec::new();

    if let Some(workspace_members) = json.get("workspace_members").and_then(|v| v.as_array()) {
        parts.push(format!("workspace_members: {}", workspace_members.len()));
        for m in workspace_members.iter().take(20) {
            if let Some(s) = m.as_str() {
                let short = s.split(' ').take(2).collect::<Vec<_>>().join(" ");
                parts.push(format!("  {short}"));
            }
        }
        if workspace_members.len() > 20 {
            parts.push(format!("  ... +{} more", workspace_members.len() - 20));
        }
    }

    if let Some(target_dir) = json.get("target_directory").and_then(|v| v.as_str()) {
        parts.push(format!("target_directory: {target_dir}"));
    }

    if let Some(workspace_root) = json.get("workspace_root").and_then(|v| v.as_str()) {
        parts.push(format!("workspace_root: {workspace_root}"));
    }

    if let Some(packages) = json.get("packages").and_then(|v| v.as_array()) {
        parts.push(format!("packages: {}", packages.len()));
        for pkg in packages.iter().take(30) {
            let name = pkg.get("name").and_then(|v| v.as_str()).unwrap_or("?");
            let version = pkg.get("version").and_then(|v| v.as_str()).unwrap_or("?");
            let features: Vec<&str> = pkg
                .get("features")
                .and_then(|v| v.as_object())
                .map(|f| f.keys().map(std::string::String::as_str).collect())
                .unwrap_or_default();
            if features.is_empty() {
                parts.push(format!("  {name} v{version}"));
            } else {
                parts.push(format!(
                    "  {name} v{version} [features: {}]",
                    features.join(", ")
                ));
            }
        }
        if packages.len() > 30 {
            parts.push(format!("  ... +{} more", packages.len() - 30));
        }
    }

    if let Some(resolve) = json.get("resolve")
        && let Some(nodes) = resolve.get("nodes").and_then(|v| v.as_array())
    {
        let total_deps: usize = nodes
            .iter()
            .map(|n| {
                n.get("deps")
                    .and_then(|v| v.as_array())
                    .map_or(0, std::vec::Vec::len)
            })
            .sum();
        parts.push(format!(
            "resolve: {} nodes, {} dep edges",
            nodes.len(),
            total_deps
        ));
    }

    if parts.is_empty() {
        "cargo metadata: ok (empty)".to_string()
    } else {
        parts.join("\n")
    }
}

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

    #[test]
    fn cargo_build_success() {
        let output = "   Compiling lean-ctx v2.1.1\n    Finished release profile [optimized] target(s) in 30.5s";
        let result = compress("cargo build", output).unwrap();
        assert!(result.contains("compiled"), "should mention compilation");
        assert!(result.contains("30.5s"), "should include build time");
    }

    #[test]
    fn cargo_build_with_errors() {
        let output = "   Compiling lean-ctx v2.1.1\nerror[E0308]: mismatched types\n --> src/main.rs:10:5\n  |\n10|     1 + \"hello\"\n  |         ^^^^^^^ expected integer, found &str";
        let result = compress("cargo build", output).unwrap();
        assert!(result.contains("E0308"), "should contain error code");
    }

    #[test]
    fn cargo_test_success() {
        let output = "running 5 tests\ntest test_one ... ok\ntest test_two ... ok\ntest test_three ... ok\ntest test_four ... ok\ntest test_five ... ok\n\ntest result: ok. 5 passed; 0 failed; 0 ignored";
        let result = compress("cargo test", output).unwrap();
        assert!(result.contains("5 pass"), "should show passed count");
    }

    #[test]
    fn cargo_test_failure() {
        let output = "running 3 tests\ntest test_ok ... ok\ntest test_fail ... FAILED\ntest test_ok2 ... ok\n\ntest result: FAILED. 2 passed; 1 failed; 0 ignored";
        let result = compress("cargo test", output).unwrap();
        assert!(result.contains("1 failed"), "should indicate failure");
        assert!(result.contains("test_fail"), "should name failed test");
    }

    #[test]
    fn test_build_with_checking() {
        let output = "Compiling app v0.1.0\nChecking dep v1.0.0\nChecking dep-two v2.0.0\nFinished dev profile in 1.2s";
        let result = compress("cargo check", output).unwrap();
        assert!(result.contains("compiled 1 crate"));
        assert!(result.contains("checked 2 crates"));
    }

    #[test]
    fn test_build_with_warnings() {
        let warnings = ["warning: unused value"; 10].join("\n");
        let output =
            format!("Compiling app v0.1.0\n{warnings}\nwarning: app generated 10 warnings");
        let result = compress("cargo build", &output).unwrap();
        assert!(result.contains("10 warnings (unused ×10)"));
    }

    #[test]
    fn test_clippy_grouped() {
        let output = "warning[clippy::needless-borrow]: needless borrow\nwarning[clippy::unused-imports]: unused import\nwarning[clippy::dead-code]: dead code\nwarning[clippy::manual-map]: manual map\nwarning[clippy::map-clone]: map clone\nwarning[clippy::redundant-closure]: redundant closure";
        let result = compress("cargo clippy", output).unwrap();
        assert!(result.contains("6 warnings"));
        assert!(result.contains("needless_borrow ×1"));
        assert!(result.contains("+1 rules"));
    }

    #[test]
    fn test_test_with_compile_warnings() {
        let output = "Compiling app v0.1.0\nwarning: unused value\nrunning 2 tests\ntest one ... ok\ntest two ... ok\ntest result: ok. 2 passed; 0 failed; 0 ignored\nFinished test profile in 1.4s";
        let result = compress("cargo test", output).unwrap();
        assert!(result.contains("[compiled 1 crate, 1 warning]"));
        assert!(result.contains("cargo test: 2 passed, 0 failed"));
    }

    #[test]
    fn test_dispatch_precision() {
        assert!(
            compress(
                "cargo test",
                "test result: ok. 0 passed; 0 failed; 0 ignored"
            )
            .is_some()
        );
        assert!(compress("cargo latest", "latest version").is_none());
    }

    #[test]
    fn test_clean_handler() {
        let result = compress("cargo clean", "Removed 42 files, 12.3MiB total").unwrap();
        assert_eq!(result, "removed 42 files");
    }

    #[test]
    fn cargo_clippy_clean() {
        let output = "    Checking lean-ctx v2.1.1\n    Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.2s";
        let result = compress("cargo clippy", output).unwrap();
        assert!(result.contains("clean"), "clean clippy should say clean");
    }

    #[test]
    fn cargo_check_routes_to_build() {
        let output = "    Checking lean-ctx v2.1.1\n    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.1s";
        let result = compress("cargo check", output);
        assert!(
            result.is_some(),
            "cargo check should route to build compressor"
        );
    }

    #[test]
    fn cargo_metadata_json() {
        let json = r#"{
            "packages": [
                {"name": "lean-ctx", "version": "3.2.9", "features": {"tree-sitter": ["dep:tree-sitter"]}},
                {"name": "serde", "version": "1.0.200", "features": {"derive": ["serde_derive"]}}
            ],
            "workspace_members": ["lean-ctx 3.2.9 (path+file:///foo)"],
            "workspace_root": "/foo",
            "target_directory": "/foo/target",
            "resolve": {
                "nodes": [
                    {"id": "lean-ctx", "deps": [{"name": "serde"}]},
                    {"id": "serde", "deps": []}
                ]
            }
        }"#;
        let result = compress("cargo metadata", json).unwrap();
        assert!(
            result.contains("workspace_members: 1"),
            "should list workspace members"
        );
        assert!(result.contains("packages: 2"), "should list packages");
        assert!(
            result.contains("resolve: 2 nodes"),
            "should summarize resolve graph"
        );
        assert!(
            result.len() < json.len(),
            "compressed output should be shorter"
        );
    }

    #[test]
    fn cargo_run_strips_compilation() {
        let output = "   Compiling lean-ctx v2.1.1\n    Finished `dev` profile [unoptimized] target(s) in 5.2s\n     Running `target/debug/lean-ctx`\nHello, world!\nResult: 42";
        let result = compress("cargo run", output).unwrap();
        assert!(
            !result.contains("Running `target"),
            "should strip Running line"
        );
        assert!(
            result.contains("Hello, world!"),
            "should keep program output"
        );
        assert!(result.contains("compiled"), "should summarize compilation");
    }

    #[test]
    fn cargo_bench_keeps_results() {
        let output = "   Compiling lean-ctx v2.1.1\n    Finished `bench` profile [optimized] target(s) in 12.0s\n     Running benches/main.rs\ntest bench_parse  ... bench:     1,234 ns/iter (+/- 56)\ntest bench_render ... bench:     5,678 ns/iter (+/- 123)\n\ntest result: ok. 0 passed; 0 failed; 2 ignored";
        let result = compress("cargo bench", output).unwrap();
        assert!(result.contains("bench_parse"), "should keep bench results");
        assert!(result.contains("bench_render"), "should keep bench results");
        assert!(result.contains("compiled"), "should summarize compilation");
    }

    #[test]
    fn cargo_bench_with_criterion() {
        let output = "   Compiling bench-suite v0.1.0\nBenchmarking parser/parse_large\nCollecting 100 samples\nWarming up for 3.0000 s\nAnalyzing results...\nparser/parse_large      time:   [1.2345 ms 1.3000 ms 1.3500 ms]";
        let result = compress("cargo bench", output).unwrap();
        assert!(
            result.contains("time:"),
            "should keep criterion timing lines"
        );
        assert!(!result.contains("Collecting"), "should strip progress");
    }

    #[test]
    fn cargo_metadata_non_json() {
        let output = "error: `cargo metadata` exited with an error\nsome detailed error";
        let result = compress("cargo metadata", output).unwrap();
        assert!(
            result.contains("error"),
            "should pass through non-JSON output"
        );
    }
}