repotoire 0.3.47

Graph-powered code analysis CLI. 81 detectors for security, architecture, and code quality.
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
//! Root Cause Analyzer for cross-detector pattern recognition
//!
//! Identifies root causes of issues by analyzing relationships between
//! findings from multiple detectors. Enables prioritized refactoring by showing
//! that fixing one issue (e.g., god class) resolves many cascading issues.
//!
//! # The "God Class Cascade" Pattern
//!
//! ```text
//! GodClass → CircularDependency (imports everything)
//!          → FeatureEnvy (methods use external classes)
//!          → ShotgunSurgery (everyone imports it)
//!          → InappropriateIntimacy (bidirectional coupling)
//!          → CodeDuplication (copy-paste instead of import)
//! ```

use crate::models::{Finding, Severity};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use tracing::{debug, info};

/// Analysis result showing root cause and cascading issues
#[derive(Debug, Clone)]
pub struct RootCauseAnalysis {
    pub root_cause_finding: Finding,
    pub root_cause_type: String, // "god_class", "circular_dependency", etc.
    pub cascading_findings: Vec<Finding>,
    pub impact_score: f64,            // Higher = more impact if fixed
    pub estimated_resolved_count: i32,
    pub refactoring_priority: String, // LOW, MEDIUM, HIGH, CRITICAL
    pub suggested_approach: String,
}

/// Summary statistics of root cause analysis
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RootCauseSummary {
    pub total_root_causes: usize,
    pub total_cascading_issues: usize,
    pub root_causes_by_type: HashMap<String, usize>,
    pub average_impact_score: f64,
    pub high_priority_count: usize,
}

/// Detector names for categorization
const GOD_CLASS_DETECTOR: &str = "GodClassDetector";
const CIRCULAR_DEP_DETECTOR: &str = "CircularDependencyDetector";
const FEATURE_ENVY_DETECTOR: &str = "FeatureEnvyDetector";
const SHOTGUN_SURGERY_DETECTOR: &str = "ShotgunSurgeryDetector";
const INTIMACY_DETECTOR: &str = "InappropriateIntimacyDetector";
const MIDDLE_MAN_DETECTOR: &str = "MiddleManDetector";

/// Analyzes findings to identify root causes and cascading issues
///
/// Uses cross-detector patterns to find:
/// 1. God classes causing circular dependencies
/// 2. Feature envy caused by god classes
/// 3. Shotgun surgery linked to high-coupling classes
/// 4. Inappropriate intimacy from bidirectional god class dependencies
pub struct RootCauseAnalyzer {
    analyses: Vec<RootCauseAnalysis>,
}

impl Default for RootCauseAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

impl RootCauseAnalyzer {
    /// Create a new root cause analyzer
    pub fn new() -> Self {
        Self {
            analyses: Vec::new(),
        }
    }

    /// Analyze findings and enrich them with root cause information
    pub fn analyze(&mut self, findings: Vec<Finding>) -> Vec<Finding> {
        if findings.is_empty() {
            return findings;
        }

        // Group findings by detector
        let by_detector = self.group_by_detector(&findings);

        // Group findings by file
        let by_file = self.group_by_file(&findings);

        // Analyze god class cascade pattern
        self.analyze_god_class_cascade(&by_detector, &by_file);

        // Analyze circular dependency root causes
        self.analyze_circular_dep_causes(&by_detector, &by_file);

        // Enrich original findings with root cause info
        let enriched = self.enrich_findings(findings);

        info!(
            "RootCauseAnalyzer found {} root cause patterns affecting {} findings",
            self.analyses.len(),
            self.analyses
                .iter()
                .map(|a| a.estimated_resolved_count)
                .sum::<i32>()
        );

        enriched
    }

    /// Group findings by detector name
    fn group_by_detector<'a>(&self, findings: &'a [Finding]) -> HashMap<&'a str, Vec<&'a Finding>> {
        let mut grouped: HashMap<&str, Vec<&Finding>> = HashMap::new();
        for finding in findings {
            grouped
                .entry(finding.detector.as_str())
                .or_default()
                .push(finding);
        }
        grouped
    }

    /// Group findings by affected file
    fn group_by_file<'a>(&self, findings: &'a [Finding]) -> HashMap<String, Vec<&'a Finding>> {
        let mut grouped: HashMap<String, Vec<&Finding>> = HashMap::new();
        for finding in findings {
            for file_path in &finding.affected_files {
                let path_str = file_path.to_string_lossy().to_string();
                grouped.entry(path_str).or_default().push(finding);
            }
        }
        grouped
    }

    /// Identify god classes that cause cascading issues
    fn analyze_god_class_cascade(
        &mut self,
        by_detector: &HashMap<&str, Vec<&Finding>>,
        by_file: &HashMap<String, Vec<&Finding>>,
    ) {
        let god_classes = by_detector.get(GOD_CLASS_DETECTOR).cloned().unwrap_or_default();

        for god_class in god_classes {
            let mut cascading = Vec::new();
            let god_class_files: HashSet<String> = god_class
                .affected_files
                .iter()
                .map(|p| p.to_string_lossy().to_string())
                .collect();

            // Check for circular dependencies involving this god class
            if let Some(circ_deps) = by_detector.get(CIRCULAR_DEP_DETECTOR) {
                for circ_dep in circ_deps {
                    let circ_files: HashSet<String> = circ_dep
                        .affected_files
                        .iter()
                        .map(|p| p.to_string_lossy().to_string())
                        .collect();
                    if !god_class_files.is_disjoint(&circ_files) {
                        cascading.push((*circ_dep).clone());
                    }
                }
            }

            // Check for shotgun surgery (god class is widely used)
            if let Some(shotguns) = by_detector.get(SHOTGUN_SURGERY_DETECTOR) {
                for shotgun in shotguns {
                    let shotgun_files: HashSet<String> = shotgun
                        .affected_files
                        .iter()
                        .map(|p| p.to_string_lossy().to_string())
                        .collect();
                    if !god_class_files.is_disjoint(&shotgun_files) {
                        cascading.push((*shotgun).clone());
                    }
                }
            }

            // Check for inappropriate intimacy
            if let Some(intimacies) = by_detector.get(INTIMACY_DETECTOR) {
                for intimacy in intimacies {
                    let intimacy_files: HashSet<String> = intimacy
                        .affected_files
                        .iter()
                        .map(|p| p.to_string_lossy().to_string())
                        .collect();
                    if !god_class_files.is_disjoint(&intimacy_files) {
                        cascading.push((*intimacy).clone());
                    }
                }
            }

            // Check for findings in same files
            for file_path in &god_class_files {
                if let Some(file_findings) = by_file.get(file_path) {
                    for finding in file_findings {
                        if finding.id != god_class.id
                            && !cascading.iter().any(|c| c.id == finding.id)
                        {
                            // Only add if it's a related detector type
                            let related_detectors = [
                                CIRCULAR_DEP_DETECTOR,
                                FEATURE_ENVY_DETECTOR,
                                SHOTGUN_SURGERY_DETECTOR,
                                INTIMACY_DETECTOR,
                                MIDDLE_MAN_DETECTOR,
                            ];
                            if related_detectors.contains(&finding.detector.as_str()) {
                                cascading.push((*finding).clone());
                            }
                        }
                    }
                }
            }

            if !cascading.is_empty() {
                // Calculate impact score
                let impact = self.calculate_impact_score(god_class, &cascading);
                let priority = self.calculate_priority(god_class, &cascading);

                let analysis = RootCauseAnalysis {
                    root_cause_finding: god_class.clone(),
                    root_cause_type: "god_class".to_string(),
                    cascading_findings: cascading.clone(),
                    impact_score: impact,
                    estimated_resolved_count: (cascading.len() + 1) as i32,
                    refactoring_priority: priority,
                    suggested_approach: self.suggest_god_class_refactoring(god_class, &cascading),
                };
                self.analyses.push(analysis);
            }
        }
    }

    /// Identify root causes of circular dependencies
    fn analyze_circular_dep_causes(
        &mut self,
        by_detector: &HashMap<&str, Vec<&Finding>>,
        _by_file: &HashMap<String, Vec<&Finding>>,
    ) {
        let circular_deps = by_detector
            .get(CIRCULAR_DEP_DETECTOR)
            .cloned()
            .unwrap_or_default();

        // Collect files already identified as god class root causes
        let god_class_files: HashSet<String> = self
            .analyses
            .iter()
            .filter(|a| a.root_cause_type == "god_class")
            .flat_map(|a| {
                a.root_cause_finding
                    .affected_files
                    .iter()
                    .map(|p| p.to_string_lossy().to_string())
            })
            .collect();

        for circ_dep in circular_deps {
            // Skip if already linked to a god class
            let circ_files: HashSet<String> = circ_dep
                .affected_files
                .iter()
                .map(|p| p.to_string_lossy().to_string())
                .collect();
            if !god_class_files.is_disjoint(&circ_files) {
                continue;
            }

            // Check for inappropriate intimacy as root cause
            let mut cascading = Vec::new();
            if let Some(intimacies) = by_detector.get(INTIMACY_DETECTOR) {
                for intimacy in intimacies {
                    let intimacy_files: HashSet<String> = intimacy
                        .affected_files
                        .iter()
                        .map(|p| p.to_string_lossy().to_string())
                        .collect();
                    if !circ_files.is_disjoint(&intimacy_files) {
                        cascading.push((*intimacy).clone());
                    }
                }
            }

            if !cascading.is_empty() {
                // Circular dep is the root cause, intimacy is related
                let impact = self.calculate_impact_score(circ_dep, &cascading);
                let priority = self.calculate_priority(circ_dep, &cascading);

                let analysis = RootCauseAnalysis {
                    root_cause_finding: circ_dep.clone(),
                    root_cause_type: "circular_dependency".to_string(),
                    cascading_findings: cascading.clone(),
                    impact_score: impact,
                    estimated_resolved_count: (cascading.len() + 1) as i32,
                    refactoring_priority: priority,
                    suggested_approach: self.suggest_circular_dep_refactoring(circ_dep),
                };
                self.analyses.push(analysis);
            }
        }
    }

    /// Calculate impact score for fixing the root cause
    fn calculate_impact_score(&self, root_cause: &Finding, cascading: &[Finding]) -> f64 {
        let severity_scores: HashMap<Severity, f64> = [
            (Severity::Critical, 4.0),
            (Severity::High, 3.0),
            (Severity::Medium, 2.0),
            (Severity::Low, 1.0),
            (Severity::Info, 0.5),
        ]
        .into_iter()
        .collect();

        let base_score = severity_scores
            .get(&root_cause.severity)
            .copied()
            .unwrap_or(1.0);

        // Add score for each cascading issue
        let cascade_score: f64 = cascading
            .iter()
            .map(|f| severity_scores.get(&f.severity).copied().unwrap_or(1.0) * 0.5)
            .sum();

        // Bonus for number of cascading issues
        let count_bonus = (cascading.len() as f64 * 0.3).min(2.0);

        let total = base_score + cascade_score + count_bonus;

        // Normalize to 0-10 scale
        total.min(10.0)
    }

    /// Calculate refactoring priority
    fn calculate_priority(&self, root_cause: &Finding, cascading: &[Finding]) -> String {
        // Count high-severity cascading issues
        let critical_count = cascading
            .iter()
            .filter(|f| f.severity == Severity::Critical)
            .count();
        let high_count = cascading
            .iter()
            .filter(|f| f.severity == Severity::High)
            .count();

        if root_cause.severity == Severity::Critical || critical_count >= 1 {
            "CRITICAL".to_string()
        } else if root_cause.severity == Severity::High || high_count >= 2 {
            "HIGH".to_string()
        } else if cascading.len() >= 3 {
            "HIGH".to_string()
        } else if !cascading.is_empty() {
            "MEDIUM".to_string()
        } else {
            "LOW".to_string()
        }
    }

    /// Generate refactoring suggestion for god class root cause
    fn suggest_god_class_refactoring(&self, god_class: &Finding, cascading: &[Finding]) -> String {
        let class_name = god_class
            .title
            .split(':')
            .last()
            .unwrap_or("the class")
            .trim();

        // Count cascading issue types
        let has_circular = cascading
            .iter()
            .any(|f| f.detector == CIRCULAR_DEP_DETECTOR);
        let has_shotgun = cascading
            .iter()
            .any(|f| f.detector == SHOTGUN_SURGERY_DETECTOR);

        let mut suggestions = vec![format!(
            "ROOT CAUSE: God class '{}' is causing {} cascading issues.\n",
            class_name,
            cascading.len()
        )];
        suggestions.push("RECOMMENDED REFACTORING APPROACH:\n".to_string());

        let mut step = 1;
        if has_circular {
            suggestions.push(format!(
                "  {}. Extract interfaces to break circular dependencies\n",
                step
            ));
            step += 1;
        }

        suggestions.push(format!(
            "  {}. Split into focused classes by responsibility:\n\
                  - Group related methods (look at shared field access)\n\
                  - Extract each group into a dedicated class\n",
            step
        ));
        step += 1;

        if has_shotgun {
            suggestions.push(format!(
                "  {}. Create a facade to limit external coupling\n",
                step
            ));
        }

        suggestions.push(format!(
            "\nEXPECTED RESULT: Fixing '{}' will resolve ~{} related issues.",
            class_name,
            cascading.len()
        ));

        suggestions.join("")
    }

    /// Generate refactoring suggestion for circular dependency root cause
    fn suggest_circular_dep_refactoring(&self, circ_dep: &Finding) -> String {
        let cycle_length = circ_dep.affected_files.len();

        let mut suggestions = vec!["ROOT CAUSE: Circular dependency creating tight coupling.\n".to_string()];
        suggestions.push("RECOMMENDED REFACTORING APPROACH:\n".to_string());

        if cycle_length <= 3 {
            suggestions.push(
                "  1. Consider merging tightly coupled modules\n\
                   2. Or extract shared types to a common module\n\
                   3. Use TYPE_CHECKING for type-only imports\n"
                    .to_string(),
            );
        } else {
            suggestions.push(
                "  1. Identify the module with most incoming imports\n\
                   2. Extract its dependencies into interface module\n\
                   3. Apply Dependency Inversion Principle\n\
                   4. Consider using dependency injection\n"
                    .to_string(),
            );
        }

        suggestions.join("")
    }

    /// Enrich findings with root cause analysis information
    fn enrich_findings(&self, mut findings: Vec<Finding>) -> Vec<Finding> {
        // Build lookup of finding ID to analysis
        let mut root_cause_ids: HashMap<&str, &RootCauseAnalysis> = HashMap::new();
        let mut cascading_ids: HashMap<&str, &RootCauseAnalysis> = HashMap::new();

        for analysis in &self.analyses {
            root_cause_ids.insert(&analysis.root_cause_finding.id, analysis);
            for cascading in &analysis.cascading_findings {
                cascading_ids.insert(&cascading.id, analysis);
            }
        }

        // Enrich each finding
        for finding in &mut findings {
            // Check if this is a root cause
            if let Some(analysis) = root_cause_ids.get(finding.id.as_str()) {
                // Update description with root cause info
                finding.description = format!(
                    "{}\n\n\
                     📍 **ROOT CAUSE ANALYSIS**\n\
                     - Type: {}\n\
                     - Impact Score: {:.1}\n\
                     - Cascading Issues: {}\n\
                     - Priority: {}",
                    finding.description,
                    analysis.root_cause_type,
                    analysis.impact_score,
                    analysis.cascading_findings.len(),
                    analysis.refactoring_priority
                );

                // Update suggested fix with root cause approach
                if !analysis.suggested_approach.is_empty() {
                    finding.suggested_fix = Some(analysis.suggested_approach.clone());
                }
            }
            // Check if this is caused by a root cause
            else if let Some(analysis) = cascading_ids.get(finding.id.as_str()) {
                let root_name = analysis
                    .root_cause_finding
                    .title
                    .split(':')
                    .last()
                    .unwrap_or("unknown")
                    .trim();

                // Add note about root cause to description
                let root_note = if analysis.root_cause_type == "god_class" {
                    format!(
                        "\n\n📍 ROOT CAUSE: This issue is linked to god class '{}'. \
                         Fixing the god class may resolve this issue.",
                        root_name
                    )
                } else {
                    format!(
                        "\n\n📍 ROOT CAUSE: This issue is linked to {}. \
                         Fixing the root cause may resolve this issue.",
                        analysis.root_cause_type.replace('_', " ")
                    )
                };

                finding.description = format!("{}{}", finding.description, root_note);
            }
        }

        findings
    }

    /// Get all root cause analyses
    pub fn get_analyses(&self) -> &[RootCauseAnalysis] {
        &self.analyses
    }

    /// Get summary statistics of root cause analysis
    pub fn get_summary(&self) -> RootCauseSummary {
        let total_root_causes = self.analyses.len();
        let total_cascading: usize = self
            .analyses
            .iter()
            .map(|a| a.cascading_findings.len())
            .sum();

        let mut by_type: HashMap<String, usize> = HashMap::new();
        for analysis in &self.analyses {
            *by_type.entry(analysis.root_cause_type.clone()).or_insert(0) += 1;
        }

        let avg_impact = if total_root_causes > 0 {
            self.analyses.iter().map(|a| a.impact_score).sum::<f64>() / total_root_causes as f64
        } else {
            0.0
        };

        let high_priority_count = self
            .analyses
            .iter()
            .filter(|a| a.refactoring_priority == "HIGH" || a.refactoring_priority == "CRITICAL")
            .count();

        RootCauseSummary {
            total_root_causes,
            total_cascading_issues: total_cascading,
            root_causes_by_type: by_type,
            average_impact_score: (avg_impact * 100.0).round() / 100.0,
            high_priority_count,
        }
    }
}

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

    fn create_test_finding(id: &str, detector: &str, severity: Severity, file: &str) -> Finding {
        Finding {
            id: id.to_string(),
            detector: detector.to_string(),
            severity,
            title: format!("Test: {}", detector),
            description: "Test description".to_string(),
            affected_files: vec![PathBuf::from(file)],
            line_start: Some(10),
            line_end: Some(20),
            suggested_fix: Some("Fix it".to_string()),
            estimated_effort: None,
            category: None,
            cwe_id: None,
            why_it_matters: None,
            ..Default::default()
        }
    }

    #[test]
    fn test_empty_findings() {
        let mut analyzer = RootCauseAnalyzer::new();
        let result = analyzer.analyze(vec![]);
        assert!(result.is_empty());
        assert!(analyzer.get_analyses().is_empty());
    }

    #[test]
    fn test_god_class_cascade() {
        let mut analyzer = RootCauseAnalyzer::new();
        let findings = vec![
            create_test_finding("1", GOD_CLASS_DETECTOR, Severity::High, "core/god.py"),
            create_test_finding(
                "2",
                CIRCULAR_DEP_DETECTOR,
                Severity::Medium,
                "core/god.py",
            ),
            create_test_finding("3", INTIMACY_DETECTOR, Severity::Medium, "core/god.py"),
        ];

        let enriched = analyzer.analyze(findings);

        assert_eq!(analyzer.get_analyses().len(), 1);
        let analysis = &analyzer.get_analyses()[0];
        assert_eq!(analysis.root_cause_type, "god_class");
        assert_eq!(analysis.cascading_findings.len(), 2);

        // Check enrichment
        let god_class = enriched.iter().find(|f| f.id == "1").unwrap();
        assert!(god_class.description.contains("ROOT CAUSE ANALYSIS"));
    }

    #[test]
    fn test_impact_score() {
        let analyzer = RootCauseAnalyzer::new();
        let root = create_test_finding("1", GOD_CLASS_DETECTOR, Severity::High, "test.py");
        let cascading = vec![
            create_test_finding("2", CIRCULAR_DEP_DETECTOR, Severity::Medium, "test.py"),
            create_test_finding("3", INTIMACY_DETECTOR, Severity::Low, "test.py"),
        ];

        let score = analyzer.calculate_impact_score(&root, &cascading);
        assert!(score > 0.0);
        assert!(score <= 10.0);
    }

    #[test]
    fn test_priority_calculation() {
        let analyzer = RootCauseAnalyzer::new();

        // Critical root cause
        let root = create_test_finding("1", GOD_CLASS_DETECTOR, Severity::Critical, "test.py");
        assert_eq!(analyzer.calculate_priority(&root, &[]), "CRITICAL");

        // High with many cascading
        let root = create_test_finding("2", GOD_CLASS_DETECTOR, Severity::Medium, "test.py");
        let cascading = vec![
            create_test_finding("3", CIRCULAR_DEP_DETECTOR, Severity::Low, "test.py"),
            create_test_finding("4", INTIMACY_DETECTOR, Severity::Low, "test.py"),
            create_test_finding("5", SHOTGUN_SURGERY_DETECTOR, Severity::Low, "test.py"),
        ];
        assert_eq!(analyzer.calculate_priority(&root, &cascading), "HIGH");
    }

    #[test]
    fn test_summary() {
        let mut analyzer = RootCauseAnalyzer::new();
        let findings = vec![
            create_test_finding("1", GOD_CLASS_DETECTOR, Severity::High, "test.py"),
            create_test_finding("2", CIRCULAR_DEP_DETECTOR, Severity::Medium, "test.py"),
        ];

        analyzer.analyze(findings);
        let summary = analyzer.get_summary();

        assert_eq!(summary.total_root_causes, 1);
        assert!(summary.root_causes_by_type.contains_key("god_class"));
    }
}