debtmap 0.17.0

Code complexity and technical debt analyzer
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
//! Risk assessment and testing priority analysis.
//!
//! This module calculates risk scores for functions based on complexity,
//! test coverage, and contextual factors. It provides prioritized testing
//! recommendations with ROI estimates to guide test investment decisions.
//!
//! # Risk Factors
//!
//! - **Complexity**: Cyclomatic and cognitive complexity
//! - **Coverage**: Test coverage percentage and gaps
//! - **Context**: Critical paths, change frequency, dependencies
//!
//! # Key Components
//!
//! - **RiskAnalyzer**: Main entry point for risk calculation
//! - **Coverage analysis**: LCOV parsing and coverage gap detection
//! - **ROI calculation**: Estimate testing effort vs. risk reduction
//! - **Context aggregation**: Combine multiple risk signals

pub mod context;
pub mod correlation;
pub mod coverage_gap;
pub mod coverage_index;
pub mod delegation;
pub mod effects;
pub mod evidence;
pub mod evidence_calculator;
pub mod function_name_matching;
pub mod insights;
pub mod lcov;
pub mod path_normalization;
pub mod priority;
pub mod roi;
pub mod strategy;
pub mod thresholds;

use crate::core::ComplexityMetrics;
use chrono::{DateTime, Utc};
use im::Vector;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FunctionRisk {
    pub file: PathBuf,
    pub function_name: String,
    pub line_range: (usize, usize),
    pub cyclomatic_complexity: u32,
    pub cognitive_complexity: u32,
    pub coverage_percentage: Option<f64>,
    pub risk_score: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contextual_risk: Option<context::ContextualRisk>,
    pub test_effort: TestEffort,
    pub risk_category: RiskCategory,
    pub is_test_function: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum RiskCategory {
    Critical,   // High complexity (>15), low coverage (<30%)
    High,       // High complexity (>10), moderate coverage (<60%)
    Medium,     // Moderate complexity (>5), low coverage (<50%)
    Low,        // Low complexity or high coverage
    WellTested, // High complexity with high coverage (good examples)
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestEffort {
    pub estimated_difficulty: Difficulty,
    pub cognitive_load: u32,
    pub branch_count: u32,
    pub recommended_test_cases: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub enum Difficulty {
    Trivial,     // Cognitive < 5
    Simple,      // Cognitive 5-10
    Moderate,    // Cognitive 10-20
    Complex,     // Cognitive 20-40
    VeryComplex, // Cognitive > 40
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RiskInsight {
    pub top_risks: Vector<FunctionRisk>,
    pub risk_reduction_opportunities: Vector<TestingRecommendation>,
    pub codebase_risk_score: f64,
    pub complexity_coverage_correlation: Option<f64>,
    pub risk_distribution: RiskDistribution,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TestingRecommendation {
    pub function: String,
    pub file: PathBuf,
    pub line: usize,
    pub current_risk: f64,
    pub potential_risk_reduction: f64,
    pub test_effort_estimate: TestEffort,
    pub rationale: String,
    pub roi: Option<f64>,
    pub dependencies: Vec<String>,
    pub dependents: Vec<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RiskDistribution {
    pub critical_count: usize,
    pub high_count: usize,
    pub medium_count: usize,
    pub low_count: usize,
    pub well_tested_count: usize,
    pub total_functions: usize,
}

use self::context::{AnalysisTarget, ContextAggregator, ContextualRisk};
use self::strategy::{EnhancedRiskStrategy, RiskCalculator, RiskContext};
use std::sync::Arc;

pub struct RiskAnalyzer {
    strategy: Box<dyn RiskCalculator>,
    debt_score: Option<f64>,
    debt_threshold: Option<f64>,
    context_aggregator: Option<Arc<ContextAggregator>>,
    /// Reference time for deterministic analysis (Spec 214)
    pub reference_time: DateTime<Utc>,
}

impl Clone for RiskAnalyzer {
    /// Clone the risk analyzer, preserving context aggregator.
    ///
    /// The context aggregator is wrapped in Arc, so cloning is cheap (just
    /// an atomic reference count increment) and preserves the shared cache.
    fn clone(&self) -> Self {
        Self {
            strategy: self.strategy.box_clone(),
            debt_score: self.debt_score,
            debt_threshold: self.debt_threshold,
            context_aggregator: self.context_aggregator.clone(), // Arc::clone is cheap!
            reference_time: self.reference_time,
        }
    }
}

impl Default for RiskAnalyzer {
    fn default() -> Self {
        Self {
            strategy: Box::new(EnhancedRiskStrategy::default()),
            debt_score: None,
            debt_threshold: None,
            context_aggregator: None,
            reference_time: Utc::now(),
        }
    }
}

impl RiskAnalyzer {
    /// Set the reference time for analysis (for determinism)
    pub fn with_reference_time(mut self, time: DateTime<Utc>) -> Self {
        self.reference_time = time;
        self
    }
    pub fn with_debt_context(mut self, debt_score: f64, debt_threshold: f64) -> Self {
        self.debt_score = Some(debt_score);
        self.debt_threshold = Some(debt_threshold);
        self
    }

    pub fn with_context_aggregator(mut self, aggregator: ContextAggregator) -> Self {
        self.context_aggregator = Some(Arc::new(aggregator));
        self
    }

    pub fn has_context(&self) -> bool {
        self.context_aggregator.is_some()
    }

    pub fn analyze_function(
        &self,
        file: PathBuf,
        function_name: String,
        line_range: (usize, usize),
        complexity: &ComplexityMetrics,
        coverage: Option<f64>,
        is_test: bool,
    ) -> FunctionRisk {
        let context = RiskContext {
            file,
            function_name,
            line_range,
            complexity: complexity.clone(),
            coverage,
            debt_score: self.debt_score,
            debt_threshold: self.debt_threshold,
            is_test,
            is_recognized_pattern: false,
            pattern_type: None,
            pattern_confidence: 0.0,
        };

        self.strategy.calculate(&context)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn analyze_function_with_context(
        &self,
        file: PathBuf,
        function_name: String,
        line_range: (usize, usize),
        complexity: &ComplexityMetrics,
        coverage: Option<f64>,
        is_test: bool,
        root_path: PathBuf,
    ) -> (FunctionRisk, Option<ContextualRisk>) {
        let mut base_risk = self.analyze_function(
            file.clone(),
            function_name.clone(),
            line_range,
            complexity,
            coverage,
            is_test,
        );

        let contextual_risk = if let Some(ref aggregator) = self.context_aggregator {
            let target = AnalysisTarget {
                root_path,
                file_path: file,
                function_name: function_name.clone(),
                line_range,
                reference_time: self.reference_time,
            };

            let context_map = aggregator.analyze(&target);
            let ctx_risk = ContextualRisk::new(base_risk.risk_score, &context_map);

            // Update the FunctionRisk with contextual data
            base_risk.contextual_risk = Some(ctx_risk.clone());
            base_risk.risk_score = ctx_risk.contextual_risk;

            // Verbose logging for context contributions
            if log::log_enabled!(log::Level::Debug) {
                log::debug!(
                    "Context analysis for {}::{}: base_risk={:.1}, contextual_risk={:.1}, multiplier={:.2}x",
                    base_risk.file.display(),
                    function_name,
                    ctx_risk.base_risk,
                    ctx_risk.contextual_risk,
                    ctx_risk.contextual_risk / ctx_risk.base_risk.max(0.1)
                );

                for context in &ctx_risk.contexts {
                    log::debug!(
                        "  └─ {}: contribution={:.2}, weight={:.1}, impact=+{:.1}",
                        context.provider,
                        context.contribution,
                        context.weight,
                        context.contribution * context.weight
                    );
                }
            }

            Some(ctx_risk)
        } else {
            None
        };

        (base_risk, contextual_risk)
    }

    pub fn calculate_risk_score(
        &self,
        cyclomatic: u32,
        cognitive: u32,
        coverage: Option<f64>,
    ) -> f64 {
        let context = RiskContext {
            file: PathBuf::new(),
            function_name: String::new(),
            line_range: (0, 0),
            complexity: ComplexityMetrics {
                functions: vec![],
                cyclomatic_complexity: cyclomatic,
                cognitive_complexity: cognitive,
            },
            coverage,
            debt_score: self.debt_score,
            debt_threshold: self.debt_threshold,
            is_test: false,
            is_recognized_pattern: false,
            pattern_type: None,
            pattern_confidence: 0.0,
        };

        self.strategy.calculate_risk_score(&context)
    }

    pub fn calculate_risk_reduction(
        &self,
        current_risk: f64,
        complexity: u32,
        target_coverage: f64,
    ) -> f64 {
        self.strategy
            .calculate_risk_reduction(current_risk, complexity, target_coverage)
    }

    /// Analyze file-level contextual risk for god objects.
    ///
    /// This method specifically handles file-level analysis where there is no
    /// specific function being analyzed. It's designed for god objects where
    /// the entire file represents the technical debt unit.
    ///
    /// # Arguments
    /// * `file_path` - Path to the file being analyzed
    /// * `base_risk` - Base risk score for the god object (from god object scoring)
    /// * `root_path` - Project root path
    ///
    /// # Returns
    /// `Some(ContextualRisk)` if context analysis is enabled, `None` otherwise
    pub fn analyze_file_context(
        &self,
        file_path: PathBuf,
        base_risk: f64,
        root_path: PathBuf,
    ) -> Option<ContextualRisk> {
        let aggregator = self.context_aggregator.as_ref()?;

        let target = AnalysisTarget {
            root_path,
            file_path,
            function_name: String::new(), // Empty for file-level analysis
            line_range: (0, 0),           // Not applicable for file-level
            reference_time: self.reference_time,
        };

        let context_map = aggregator.analyze(&target);
        Some(ContextualRisk::new(base_risk, &context_map))
    }
}

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

    #[test]
    fn test_risk_analyzer_clone_preserves_context() {
        let aggregator = ContextAggregator::new();

        let analyzer = RiskAnalyzer::default().with_context_aggregator(aggregator);

        let cloned = analyzer.clone();

        assert!(cloned.has_context());
    }

    /// Stress test: analyze many functions with context to detect stack overflow.
    /// This simulates what happens when running `debtmap analyze --context`
    /// on a large codebase like debtmap itself (~4000 functions).
    #[test]
    #[ignore] // Takes >2 minutes - run with `cargo test -- --ignored`
    fn test_analyze_many_functions_with_context_no_stack_overflow() {
        use crate::core::ComplexityMetrics;
        use crate::priority::call_graph::CallGraph;
        use crate::risk::context::critical_path::{
            CriticalPathAnalyzer, CriticalPathProvider, EntryPoint, EntryType,
        };
        use crate::risk::context::dependency::{DependencyGraph, DependencyRiskProvider};

        // Build a realistic call graph
        let mut call_graph = CallGraph::new();
        for i in 0..2000 {
            let caller = format!("func_{}", i);
            let callee = format!("func_{}", i + 1);
            call_graph.add_edge_by_name(caller, callee, PathBuf::from("src/lib.rs"));
        }

        // Create critical path analyzer with entry point
        let mut cp_analyzer = CriticalPathAnalyzer::new();
        cp_analyzer.call_graph = call_graph;
        cp_analyzer.entry_points.push_back(EntryPoint {
            function_name: "func_0".to_string(),
            file_path: PathBuf::from("src/main.rs"),
            entry_type: EntryType::Main,
            is_user_facing: true,
        });

        // Build aggregator with providers
        let aggregator = ContextAggregator::new()
            .with_provider(Box::new(CriticalPathProvider::new(cp_analyzer)))
            .with_provider(Box::new(
                DependencyRiskProvider::new(DependencyGraph::new()),
            ));

        // Create risk analyzer with context
        let analyzer = RiskAnalyzer::default().with_context_aggregator(aggregator);

        // Analyze many functions - this is what crashes in production
        for i in 0..500 {
            let complexity = ComplexityMetrics {
                functions: vec![],
                cyclomatic_complexity: 10,
                cognitive_complexity: 15,
            };

            let (_risk, contextual) = analyzer.analyze_function_with_context(
                PathBuf::from(format!("src/module_{}.rs", i % 50)),
                format!("func_{}", i),
                (1, 50),
                &complexity,
                Some(0.75),
                false,
                PathBuf::from("/project"),
            );

            // Verify we got context
            if i < 100 {
                // First 100 should be cache misses
                assert!(
                    contextual.is_some(),
                    "Should get contextual risk for function {}",
                    i
                );
            }
        }
    }

    /// Test file-level context analysis (for god objects) at scale
    #[test]
    fn test_analyze_file_context_many_files_no_stack_overflow() {
        use crate::risk::context::critical_path::{
            CriticalPathAnalyzer, CriticalPathProvider, EntryPoint, EntryType,
        };
        use crate::risk::context::dependency::{DependencyGraph, DependencyRiskProvider};

        // Create aggregator
        let mut cp_analyzer = CriticalPathAnalyzer::new();
        cp_analyzer.entry_points.push_back(EntryPoint {
            function_name: "main".to_string(),
            file_path: PathBuf::from("src/main.rs"),
            entry_type: EntryType::Main,
            is_user_facing: true,
        });

        let aggregator = ContextAggregator::new()
            .with_provider(Box::new(CriticalPathProvider::new(cp_analyzer)))
            .with_provider(Box::new(
                DependencyRiskProvider::new(DependencyGraph::new()),
            ));

        let analyzer = RiskAnalyzer::default().with_context_aggregator(aggregator);

        // Analyze many files - simulates god object analysis
        for i in 0..200 {
            let result = analyzer.analyze_file_context(
                PathBuf::from(format!("src/large_file_{}.rs", i)),
                40.0,
                PathBuf::from("/project"),
            );

            assert!(result.is_some(), "Should get context for file {}", i);
        }
    }

    /// Minimal mock provider for testing
    struct MockProvider {
        name: &'static str,
    }

    impl context::ContextProvider for MockProvider {
        fn name(&self) -> &str {
            self.name
        }

        fn gather(&self, _target: &context::AnalysisTarget) -> anyhow::Result<context::Context> {
            Ok(context::Context {
                provider: self.name.to_string(),
                weight: 1.0,
                contribution: 0.5,
                details: context::ContextDetails::Historical {
                    change_frequency: 0.1,
                    bug_density: 0.05,
                    age_days: 100,
                    author_count: 3,
                    total_commits: 3,
                    bug_fix_count: 0,
                },
            })
        }

        fn weight(&self) -> f64 {
            1.0
        }

        fn explain(&self, _context: &context::Context) -> String {
            "mock".to_string()
        }
    }

    /// Test with 3 providers (matching production: critical_path, dependency, git_history)
    #[test]
    fn test_three_providers_many_iterations() {
        // Build aggregator with 3 mock providers
        let aggregator = ContextAggregator::new()
            .with_provider(Box::new(MockProvider {
                name: "critical_path",
            }))
            .with_provider(Box::new(MockProvider {
                name: "dependency_risk",
            }))
            .with_provider(Box::new(MockProvider {
                name: "git_history",
            }));

        let analyzer = RiskAnalyzer::default().with_context_aggregator(aggregator);

        // Run many iterations - each should be independent
        for i in 0..5000 {
            let result = analyzer.analyze_file_context(
                PathBuf::from(format!("src/file_{}.rs", i)),
                40.0,
                PathBuf::from("/project"),
            );

            assert!(result.is_some(), "Iteration {} should succeed", i);
        }
    }

    /// Test parallel execution with rayon - this is closer to production behavior
    #[test]
    fn test_parallel_context_analysis_with_rayon() {
        use rayon::prelude::*;

        // Build aggregator with 3 mock providers
        let aggregator = ContextAggregator::new()
            .with_provider(Box::new(MockProvider {
                name: "critical_path",
            }))
            .with_provider(Box::new(MockProvider {
                name: "dependency_risk",
            }))
            .with_provider(Box::new(MockProvider {
                name: "git_history",
            }));

        let analyzer = RiskAnalyzer::default().with_context_aggregator(aggregator);

        // Run in parallel - this uses rayon's thread pool with smaller stacks
        let results: Vec<_> = (0..5000)
            .into_par_iter()
            .map(|i| {
                analyzer.analyze_file_context(
                    PathBuf::from(format!("src/file_{}.rs", i)),
                    40.0,
                    PathBuf::from("/project"),
                )
            })
            .collect();

        assert_eq!(results.len(), 5000);
        assert!(results.iter().all(|r| r.is_some()));
    }
}