lean-ctx 3.9.19

Context Runtime for AI Agents with CCP. 79 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%.
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
//! Lightweight Solution Intelligence checks used by `ctx_optimize`.
//!
//! The analyzer intentionally favours explainable regex-based heuristics over a
//! complete parser. Findings are suggestions, not compiler diagnostics.

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

// Keep regexes compiled once, matching the static-regex convention used by the
// rest of the crate's lightweight analyzers.
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("valid static regex"))
    }};
}

/// One simplification opportunity identified in a source file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OptimizeFinding {
    pub line: usize,
    pub category: String,
    pub severity: String,
    pub current: String,
    pub recommended: String,
    pub loc_impact: i32,
}

/// Complete analysis result for one source file.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OptimizeReport {
    pub findings: Vec<OptimizeFinding>,
    pub summary: OptimizeSummary,
}

/// Aggregate counts for an [`OptimizeReport`].
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OptimizeSummary {
    pub total_findings: usize,
    pub estimated_loc_reduction: i32,
    pub by_category: HashMap<String, usize>,
}

/// Analyze `content` for simple, high-signal simplification opportunities.
pub fn analyze_file(content: &str, path: &str, project_root: &Path) -> OptimizeReport {
    let mut findings = Vec::new();
    findings.extend(detect_single_impl_traits(content));
    findings.extend(detect_single_product_factories(content));
    findings.extend(detect_stdlib_alternatives(content));
    findings.extend(detect_native_feature_replacements(content, project_root));
    findings.extend(detect_yagni(content));
    findings.extend(detect_oneline_candidates(content));
    findings.extend(detect_reuse_candidates(content, path, project_root));

    findings.sort_by(|left, right| {
        finding_score(right)
            .cmp(&finding_score(left))
            .then_with(|| left.line.cmp(&right.line))
            .then_with(|| left.category.cmp(&right.category))
    });
    findings.dedup_by(|left, right| {
        left.line == right.line
            && left.category == right.category
            && left.current == right.current
            && left.recommended == right.recommended
    });

    let mut by_category = HashMap::new();
    for finding in &findings {
        *by_category.entry(finding.category.clone()).or_insert(0) += 1;
    }

    OptimizeReport {
        summary: OptimizeSummary {
            total_findings: findings.len(),
            estimated_loc_reduction: findings.iter().map(|finding| finding.loc_impact).sum(),
            by_category,
        },
        findings,
    }
}

/// Detect Rust traits which currently exist only to abstract one implementation.
pub fn detect_single_impl_traits(content: &str) -> Vec<OptimizeFinding> {
    let trait_declarations =
        static_regex!(r"(?m)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?trait\s+([A-Za-z_][A-Za-z0-9_]*)\b");
    let trait_implementations = static_regex!(
        r"(?m)^[ \t]*impl(?:\s*<[^>\n]*>)?\s+([A-Za-z_][A-Za-z0-9_:]*)\s*(?:<[^>{}\n]*>)?\s+for\s+"
    );

    let implementations: HashMap<String, usize> = trait_implementations
        .captures_iter(content)
        .filter_map(|captures| {
            captures
                .get(1)
                .map(|name| last_path_segment(name.as_str()).to_string())
        })
        .fold(HashMap::new(), |mut counts, trait_name| {
            *counts.entry(trait_name).or_insert(0) += 1;
            counts
        });

    trait_declarations
        .captures_iter(content)
        .filter_map(|captures| {
            let trait_name = captures.get(1)?.as_str();
            (implementations.get(trait_name) == Some(&1)).then(|| {
                let start = captures.get(0).map_or(0, |m| m.start());
                OptimizeFinding {
                    line: line_number_at(content, start),
                    category: "single-impl-trait".into(),
                    severity: "medium".into(),
                    current: format!("Trait `{trait_name}` has exactly one implementation."),
                    recommended: format!(
                        "Use its concrete type directly; restore `{trait_name}` when a second implementation is needed."
                    ),
                    loc_impact: 4,
                }
            })
        })
        .collect()
}

/// Detect code which likely duplicates a standard-library facility.
pub fn detect_stdlib_alternatives(content: &str) -> Vec<OptimizeFinding> {
    let custom_map = static_regex!(
        r"(?m)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?struct\s+([A-Za-z_][A-Za-z0-9_]*(?:HashMap|Map|Dictionary|Lookup)[A-Za-z0-9_]*)\b"
    );
    let string_builder = static_regex!(r"\bString::new\s*\(\s*\)");
    let push_str = static_regex!(r"\.[ \t]*push_str\s*\(");
    let manual_sort =
        static_regex!(r"(?m)^[ \t]*(?:for|while)\b[\s\S]{0,800}?(?:swap\s*\(|\.swap\s*\()");
    let mut findings = Vec::new();

    for captures in custom_map.captures_iter(content) {
        let Some(name) = captures.get(1) else {
            continue;
        };
        let Some(full_match) = captures.get(0) else {
            continue;
        };
        push_unique(
            &mut findings,
            OptimizeFinding {
                line: line_number_at(content, full_match.start()),
                category: "stdlib-alternative".into(),
                severity: "medium".into(),
                current: format!("Custom collection `{}` resembles a map or lookup table.", name.as_str()),
                recommended: "Prefer `std::collections::HashMap` unless this type enforces a domain invariant.".to_string(),
                loc_impact: 8,
            },
        );
    }

    if string_builder.is_match(content) && push_str.find_iter(content).count() >= 2 {
        let start = string_builder
            .find(content)
            .map_or(0, |matched| matched.start());
        push_unique(
            &mut findings,
            OptimizeFinding {
                line: line_number_at(content, start),
                category: "stdlib-alternative".into(),
                severity: "low".into(),
                current: "A `String` is assembled with repeated `push_str` calls.".to_string(),
                recommended: "Use `join`, `format!`, or an iterator collected into `String` when it makes the construction clearer.".to_string(),
                loc_impact: 3,
            },
        );
    }

    if let Some(matched) = manual_sort.find(content) {
        push_unique(
            &mut findings,
            OptimizeFinding {
                line: line_number_at(content, matched.start()),
                category: "stdlib-alternative".into(),
                severity: "medium".into(),
                current: "A loop performs a manual element swap while sorting.".to_string(),
                recommended: "Use `slice::sort_by` or `slice::sort_unstable_by` instead of maintaining a sorting loop.".to_string(),
                loc_impact: 6,
            },
        );
    }

    findings
}

/// Detect abstractions whose present form is unlikely to justify their cost.
pub fn detect_yagni(content: &str) -> Vec<OptimizeFinding> {
    let empty_trait = static_regex!(
        r"(?ms)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?trait\s+([A-Za-z_][A-Za-z0-9_]*)\s*\{\s*\}"
    );
    let private_abstraction =
        static_regex!(r"(?m)^[ \t]*(?:struct|enum|trait)\s+([A-Za-z_][A-Za-z0-9_]*)\b");
    let function_parameters = static_regex!(
        r"(?ms)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+([A-Za-z_][A-Za-z0-9_]*)[^\n{]*\(([^)]*)\)"
    );
    let mut findings = Vec::new();
    let mut empty_trait_names = HashSet::new();

    for captures in empty_trait.captures_iter(content) {
        let (Some(name), Some(full_match)) = (captures.get(1), captures.get(0)) else {
            continue;
        };
        empty_trait_names.insert(name.as_str().to_string());
        push_unique(
            &mut findings,
            OptimizeFinding {
                line: line_number_at(content, full_match.start()),
                category: "yagni".into(),
                severity: "low".into(),
                current: format!(
                    "Marker trait `{}` has no behaviour or bounds.",
                    name.as_str()
                ),
                recommended: "Remove it until it expresses a real capability or constraint."
                    .to_string(),
                loc_impact: 2,
            },
        );
    }

    for captures in private_abstraction.captures_iter(content) {
        let (Some(name), Some(full_match)) = (captures.get(1), captures.get(0)) else {
            continue;
        };
        if empty_trait_names.contains(name.as_str())
            || count_identifier_occurrences(content, name.as_str()) > 1
        {
            continue;
        }
        push_unique(
            &mut findings,
            OptimizeFinding {
                line: line_number_at(content, full_match.start()),
                category: "yagni".into(),
                severity: "low".into(),
                current: format!(
                    "Private abstraction `{}` is never referenced after its declaration.",
                    name.as_str()
                ),
                recommended:
                    "Remove the unused abstraction, or add it only with its first concrete use."
                        .to_string(),
                loc_impact: 2,
            },
        );
    }

    for captures in function_parameters.captures_iter(content) {
        let (Some(name), Some(parameters), Some(full_match)) =
            (captures.get(1), captures.get(2), captures.get(0))
        else {
            continue;
        };
        let parameter_count = parameters
            .as_str()
            .split(',')
            .map(str::trim)
            .filter(|parameter| !parameter.is_empty())
            .count();
        if parameter_count < 6 {
            continue;
        }
        push_unique(
            &mut findings,
            OptimizeFinding {
                line: line_number_at(content, full_match.start()),
                category: "yagni".into(),
                severity: "low".into(),
                current: format!(
                    "Function `{}` accepts {parameter_count} parameters.",
                    name.as_str()
                ),
                recommended:
                    "Group parameters that change together into a focused options or domain type."
                        .to_string(),
                loc_impact: 3,
            },
        );
    }

    findings
}

/// Detect multi-line Rust functions whose body is already a single expression.
pub fn detect_oneline_candidates(content: &str) -> Vec<OptimizeFinding> {
    let single_expression_function = static_regex!(
        r"(?ms)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+([A-Za-z_][A-Za-z0-9_]*)(?:<[^>{}]*>)?\s*\([^{};]*\)\s*(?:->\s*[^\n{]+)?\{\s*\n\s*([^{}\n]+?)\s*\n\s*\}"
    );

    single_expression_function
        .captures_iter(content)
        .filter_map(|captures| {
            let (Some(name), Some(body), Some(full_match)) =
                (captures.get(1), captures.get(2), captures.get(0))
            else {
                return None;
            };
            let expression = body.as_str().trim();
            let is_expression = !expression.ends_with(';')
                && !expression.starts_with("let ")
                && !expression.starts_with("return ")
                && !expression.starts_with("if ")
                && !expression.starts_with("match ")
                && !expression.starts_with("//");
            is_expression.then(|| OptimizeFinding {
                line: line_number_at(content, full_match.start()),
                category: "oneline-candidate".into(),
                severity: "low".into(),
                current: format!(
                    "Function `{}` uses a multi-line single-expression body.",
                    name.as_str()
                ),
                recommended: format!(
                    "Collapse `{}` to a one-line expression body if that matches nearby style.",
                    name.as_str()
                ),
                loc_impact: 2,
            })
        })
        .collect()
}

/// Detect repeated three-line code sequences in the current file or nearby project sources.
pub fn detect_reuse_candidates(
    content: &str,
    path: &str,
    project_root: &Path,
) -> Vec<OptimizeFinding> {
    let current_windows = code_windows(content);
    let mut findings = Vec::new();
    let mut first_occurrence = HashMap::new();
    let mut reported = HashSet::new();

    for (line, window) in &current_windows {
        if let Some(first_line) = first_occurrence.get(window) {
            if *line >= *first_line + 3 && reported.insert(window.clone()) {
                push_unique(
                    &mut findings,
                    OptimizeFinding {
                        line: *line,
                        category: "reuse-candidate".into(),
                        severity: "low".into(),
                        current: format!("Three-line sequence duplicates code beginning at line {first_line}."),
                        recommended: "Extract the shared sequence into a small helper with a domain-focused name.".to_string(),
                        loc_impact: 3,
                    },
                );
            }
        } else {
            first_occurrence.insert(window.clone(), *line);
        }
    }

    if first_occurrence.is_empty() {
        return findings;
    }

    let target_path = normalized_target_path(path, project_root);
    for source_path in project_source_files(project_root, &target_path) {
        let Ok(other_content) = fs::read_to_string(&source_path) else {
            continue;
        };
        for (line, window) in code_windows(&other_content) {
            let Some(current_line) = first_occurrence.get(&window) else {
                continue;
            };
            if !reported.insert(window) {
                continue;
            }
            let source_label = source_path
                .strip_prefix(project_root)
                .unwrap_or(&source_path)
                .display();
            push_unique(
                &mut findings,
                OptimizeFinding {
                    line: *current_line,
                    category: "reuse-candidate".into(),
                    severity: "low".into(),
                    current: format!(
                        "Three-line sequence is also present near line {line} in `{source_label}`."
                    ),
                    recommended: "Extract a shared helper only if both call sites have the same domain meaning.".to_string(),
                    loc_impact: 3,
                },
            );
        }
    }

    findings
}

/// Render a human-readable optimization report.
pub fn detect_single_product_factories(content: &str) -> Vec<OptimizeFinding> {
    let factory = static_regex!(
        r"(?m)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?(?:struct|enum)\s+([A-Za-z_][A-Za-z0-9_]*(?:Factory|Builder|Creator))\b"
    );
    let return_type = static_regex!(
        r"(?m)^[ \t]*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?fn\s+[A-Za-z_][A-Za-z0-9_]*[^\n{]*->\s*([A-Za-z_][A-Za-z0-9_:]*)"
    );

    factory
        .captures_iter(content)
        .filter_map(|captures| {
            let name = captures.get(1)?.as_str();
            let declaration = captures.get(0)?;
            let impl_start = content.find(&format!("impl {name}"))?;
            let impl_end = content[impl_start + 1..]
                .find("\nimpl ")
                .map_or(content.len(), |offset| impl_start + 1 + offset);
            let products: HashSet<_> = return_type
                .captures_iter(&content[impl_start..impl_end])
                .filter_map(|return_capture| return_capture.get(1))
                .map(|product| product.as_str())
                .filter(|product| !matches!(*product, "Self" | "Result" | "Option"))
                .collect();

            (products.len() == 1).then(|| {
                let product = products.into_iter().next().unwrap_or_default();
                OptimizeFinding {
                    line: line_number_at(content, declaration.start()),
                    category: "single-product-factory".to_string(),
                    severity: "low".to_string(),
                    current: format!("'{name}' only constructs '{product}'."),
                    recommended: format!(
                        "Construct '{product}' directly; retain '{name}' when it supports multiple products."
                    ),
                    loc_impact: 4,
                }
            })
        })
        .collect()
}

pub fn detect_native_feature_replacements(
    content: &str,
    project_root: &Path,
) -> Vec<OptimizeFinding> {
    let Ok(manifest) = fs::read_to_string(project_root.join("Cargo.toml")) else {
        return Vec::new();
    };
    let replacements = [
        (
            "once_cell",
            "once_cell::",
            "std::sync::OnceLock or std::sync::LazyLock",
        ),
        ("lazy_static", "lazy_static!", "std::sync::LazyLock"),
    ];

    replacements
        .iter()
        .filter_map(|(dependency, usage, native)| {
            let declared = manifest.lines().any(|line| {
                let line = line.trim_start();
                line.starts_with(dependency)
                    && matches!(
                        line.as_bytes().get(dependency.len()),
                        Some(b' ' | b'\t' | b'=')
                    )
            });
            let offset = content.find(usage)?;
            declared.then(|| OptimizeFinding {
                line: line_number_at(content, offset),
                category: "native-feature-replacement".to_string(),
                severity: "medium".to_string(),
                current: format!("'{dependency}' is declared and '{usage}' is used in this file."),
                recommended: format!(
                    "Use '{native}' when its standard-library behavior covers this use case."
                ),
                loc_impact: 1,
            })
        })
        .collect()
}

fn finding_score(finding: &OptimizeFinding) -> i32 {
    let severity = match finding.severity.as_str() {
        "critical" => 400,
        "high" => 300,
        "medium" => 200,
        _ => 100,
    };
    severity + finding.loc_impact.max(0)
}

pub fn format_report_text(report: &OptimizeReport, path: &str) -> String {
    let mut out = format!(
        "Solution Intelligence: {path}\n\n{} finding(s); estimated reduction: {} LOC\n",
        report.summary.total_findings, report.summary.estimated_loc_reduction
    );

    if report.findings.is_empty() {
        out.push_str("\nNo simplification opportunities detected.\n");
        return out;
    }

    for finding in &report.findings {
        out.push_str(&format!(
            "\nLine {} [{}] {}\n  Current: {}\n  Recommended: {}\n  Estimated impact: -{} LOC\n",
            finding.line,
            finding.severity,
            finding.category,
            finding.current,
            finding.recommended,
            finding.loc_impact
        ));
    }

    let mut categories: Vec<_> = report.summary.by_category.iter().collect();
    categories.sort_by(|left, right| left.0.cmp(right.0));
    out.push_str("\nBy category:\n");
    for (category, count) in categories {
        out.push_str(&format!("  {category}: {count}\n"));
    }
    out
}

/// Render a machine-readable optimization report.
pub fn format_report_json(report: &OptimizeReport, path: &str) -> Value {
    json!({
        "path": path,
        "findings": &report.findings,
        "summary": &report.summary,
    })
}

fn push_unique(findings: &mut Vec<OptimizeFinding>, candidate: OptimizeFinding) {
    if !findings.iter().any(|finding| {
        finding.line == candidate.line
            && finding.category == candidate.category
            && finding.current == candidate.current
    }) {
        findings.push(candidate);
    }
}

fn line_number_at(content: &str, byte_offset: usize) -> usize {
    content[..byte_offset]
        .bytes()
        .filter(|byte| *byte == b'\n')
        .count()
        + 1
}

fn last_path_segment(name: &str) -> &str {
    name.rsplit("::").next().unwrap_or(name)
}

fn count_identifier_occurrences(content: &str, identifier: &str) -> usize {
    let mut count = 0;
    let mut offset = 0;
    while let Some(found_at) = content[offset..].find(identifier) {
        let start = offset + found_at;
        let end = start + identifier.len();
        let before_is_ident = content[..start]
            .chars()
            .next_back()
            .is_some_and(is_identifier_character);
        let after_is_ident = content[end..]
            .chars()
            .next()
            .is_some_and(is_identifier_character);
        if !before_is_ident && !after_is_ident {
            count += 1;
        }
        offset = end;
    }
    count
}

fn is_identifier_character(character: char) -> bool {
    character.is_ascii_alphanumeric() || character == '_'
}

fn code_windows(content: &str) -> Vec<(usize, String)> {
    let comment_or_blank = static_regex!(r"^\s*(?://|#|/\*|\*|\*/|$)");
    let whitespace = static_regex!(r"\s+");
    let lines: Vec<_> = content
        .lines()
        .enumerate()
        .filter(|(_, line)| !comment_or_blank.is_match(line))
        .map(|(index, line)| {
            (
                index + 1,
                whitespace.replace_all(line.trim(), " ").into_owned(),
            )
        })
        .collect();

    lines
        .windows(3)
        .filter_map(|window| {
            let text = format!("{}\n{}\n{}", window[0].1, window[1].1, window[2].1);
            if text.len() >= 30 {
                Some((window[0].0, text))
            } else {
                None
            }
        })
        .collect()
}

fn normalized_target_path(path: &str, project_root: &Path) -> PathBuf {
    let path = Path::new(path);
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        project_root.join(path)
    }
}

fn project_source_files(project_root: &Path, target_path: &Path) -> Vec<PathBuf> {
    const MAX_FILES: usize = 256;
    const MAX_FILE_BYTES: u64 = 1_048_576;
    let mut sources = Vec::new();
    let mut directories = vec![project_root.to_path_buf()];

    while let Some(directory) = directories.pop() {
        let Ok(entries) = fs::read_dir(&directory) else {
            continue;
        };
        for entry in entries.flatten() {
            if sources.len() >= MAX_FILES {
                return sources;
            }
            let path = entry.path();
            let Ok(file_type) = entry.file_type() else {
                continue;
            };
            if file_type.is_dir() {
                if !should_skip_directory(&path) {
                    directories.push(path);
                }
                continue;
            }
            if !file_type.is_file() || path == target_path || !is_source_file(&path) {
                continue;
            }
            if entry
                .metadata()
                .map_or(true, |metadata| metadata.len() > MAX_FILE_BYTES)
            {
                continue;
            }
            sources.push(path);
        }
    }
    sources.sort();
    sources
}

fn should_skip_directory(path: &Path) -> bool {
    matches!(
        path.file_name().and_then(|name| name.to_str()),
        Some(".git" | "target" | "node_modules" | "vendor" | ".venv" | "dist" | "build")
    )
}

fn is_source_file(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|extension| extension.to_str()),
        Some(
            "rs" | "c"
                | "cc"
                | "cpp"
                | "h"
                | "hpp"
                | "go"
                | "java"
                | "js"
                | "jsx"
                | "kt"
                | "py"
                | "rb"
                | "swift"
                | "ts"
                | "tsx"
        )
    )
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::{
        analyze_file, detect_oneline_candidates, detect_single_impl_traits,
        detect_stdlib_alternatives, detect_yagni, format_report_json, format_report_text,
    };

    #[test]
    fn finds_trait_with_one_implementation() {
        let content = "trait Store { fn get(&self); }\nstruct Memory;\nimpl Store for Memory { fn get(&self) {} }\n";
        let findings = detect_single_impl_traits(content);

        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].category, "single-impl-trait");
        assert_eq!(findings[0].line, 1);
    }

    #[test]
    fn finds_standard_library_alternatives() {
        let content = "struct UserMap { entries: Vec<(String, String)> }\n\nfn render() {\n    let mut out = String::new();\n    out.push_str(\"a\");\n    out.push_str(\"b\");\n}\n";
        let findings = detect_stdlib_alternatives(content);

        assert!(
            findings
                .iter()
                .any(|finding| finding.current.contains("UserMap"))
        );
        assert!(
            findings
                .iter()
                .any(|finding| finding.current.contains("push_str"))
        );
    }

    #[test]
    fn finds_yagni_and_one_line_candidates() {
        let content = "trait Marker {}\n\nfn options(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) {}\n\nfn answer() -> u8 {\n    42\n}\n";

        assert_eq!(detect_yagni(content).len(), 2);
        assert_eq!(detect_oneline_candidates(content).len(), 1);
    }

    #[test]
    fn report_formats_text_and_json() {
        let content = "trait Store { fn get(&self); }\nstruct Memory;\nimpl Store for Memory { fn get(&self) {} }\n";
        let report = analyze_file(content, "src/store.rs", Path::new("."));

        assert!(format_report_text(&report, "src/store.rs").contains("Solution Intelligence"));
        let json = format_report_json(&report, "src/store.rs");
        assert_eq!(json["path"], "src/store.rs");
        assert_eq!(json["summary"]["total_findings"], 1);
    }
}