repotoire 0.8.0

Graph-powered code analysis CLI. 110 detectors for security, architecture, bus factor, and code quality.
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
//! Influential code detector using PageRank
//!
//! Uses pre-computed PageRank from graph primitives to identify truly important
//! code components based on transitive dependency weight. Enhanced with function
//! context for smarter role-aware detection.

use crate::detectors::base::{Detector, DetectorConfig};
use crate::detectors::function_context::FunctionRole;
use crate::models::{Finding, LineRange, Severity};
use anyhow::Result;
use tracing::debug;

/// Detects influential code using PageRank from graph primitives.
///
/// PageRank measures the importance of a function based on how many
/// other components depend on it (and how important those dependents are).
/// Unlike simple fan-in, PageRank accounts for transitive influence.
///
/// Uses FunctionContext to make smarter decisions:
/// - Utilities: High influence is expected, only flag if also complex
/// - Hubs: Genuinely important, flag with appropriate severity
/// - Test functions: Skipped entirely
pub struct InfluentialCodeDetector {
    config: DetectorConfig,
    /// Complexity threshold for flagging as complex
    high_complexity_threshold: u32,
    /// Lines of code threshold for being "large"
    high_loc_threshold: u32,
    /// PageRank percentile threshold (0.0-1.0) for flagging as influential.
    /// Functions above this percentile are considered influential.
    pagerank_percentile_threshold: f64,
}

impl InfluentialCodeDetector {
    /// Create a new detector with default config
    pub fn new() -> Self {
        Self {
            config: DetectorConfig::new(),
            high_complexity_threshold: 15,
            high_loc_threshold: 100,
            pagerank_percentile_threshold: 0.90,
        }
    }

    /// Create with custom config
    pub fn with_config(config: DetectorConfig) -> Self {
        let pagerank_percentile_threshold: f64 =
            config.get_option_or("pagerank_percentile_threshold", 90) as f64 / 100.0;
        Self {
            high_complexity_threshold: config.get_option_or("high_complexity_threshold", 15),
            high_loc_threshold: config.get_option_or("high_loc_threshold", 100),
            pagerank_percentile_threshold,
            config,
        }
    }

    /// Calculate severity based on metrics and function role.
    ///
    /// `pagerank_pct` is the node's percentile position (0.0..1.0).
    fn calculate_severity(
        &self,
        pagerank_pct: f64,
        complexity: usize,
        loc: usize,
        role: FunctionRole,
    ) -> Severity {
        // Base severity from PageRank percentile + complexity
        let base_severity = if pagerank_pct >= 0.99 && complexity >= 20 {
            Severity::High
        } else if pagerank_pct >= 0.95
            && (complexity >= self.high_complexity_threshold as usize
                || loc >= self.high_loc_threshold as usize)
        {
            Severity::Medium
        } else {
            Severity::Low
        };

        // Adjust based on function role
        match role {
            FunctionRole::Utility => {
                // Utilities are expected to be influential
                // Only flag if complexity is problematic
                if complexity < self.high_complexity_threshold as usize * 2 {
                    Severity::Low
                } else {
                    base_severity.min(Severity::Medium)
                }
            }
            FunctionRole::Hub => {
                // Hubs are genuinely important - keep severity
                base_severity
            }
            FunctionRole::EntryPoint => {
                // Entry points are expected to be influential
                base_severity.min(Severity::Medium)
            }
            FunctionRole::Test => Severity::Low,
            _ => base_severity,
        }
    }

    /// Create a finding
    fn create_finding(
        &self,
        name: &str,
        file_path: &str,
        range: LineRange,
        page_rank: f64,
        pagerank_pct: f64,
        fan_in: usize,
        complexity: usize,
        loc: usize,
        role: FunctionRole,
        betweenness: Option<f64>,
    ) -> Finding {
        let severity = self.calculate_severity(pagerank_pct, complexity, loc, role);

        let role_note = match role {
            FunctionRole::Utility => " (utility - high influence expected)",
            FunctionRole::Hub => " (architectural hub)",
            FunctionRole::EntryPoint => " (entry point)",
            _ => "",
        };

        let title = format!("Influential Code: {}{}", name, role_note);

        let mut description = format!(
            "Function '{}' has high transitive influence (PageRank p{:.0}) with \
            complexity {} and {} LOC. High-impact code.\n\n\
            **Metrics:**\n\
            - PageRank: {:.6} (p{:.0})\n\
            - Callers (fan-in): {}\n\
            - Complexity: {}\n\
            - Lines of code: {}",
            name,
            pagerank_pct * 100.0,
            complexity,
            loc,
            page_rank,
            pagerank_pct * 100.0,
            fan_in,
            complexity,
            loc
        );

        if let Some(b) = betweenness {
            description.push_str(&format!("\n- Betweenness centrality: {:.4}", b));
        }

        let suggested_fix = match role {
            FunctionRole::Utility => "This utility is influential but complex. Consider:\n\
                - Breaking into smaller, focused helpers\n\
                - Adding comprehensive tests"
                .to_string(),
            FunctionRole::Hub => "This is a critical hub. Consider:\n\
                - Ensuring comprehensive test coverage\n\
                - Adding monitoring and observability\n\
                - Documenting thoroughly"
                .to_string(),
            _ => {
                "Consider refactoring to reduce complexity while maintaining interface".to_string()
            }
        };

        Finding {
            id: String::new(),
            detector: "InfluentialCodeDetector".to_string(),
            severity,
            title,
            description,
            affected_files: vec![file_path.into()],
            line_start: Some(range.start),
            line_end: Some(range.end),
            suggested_fix: Some(suggested_fix),
            estimated_effort: Some("Large (4+ hours)".to_string()),
            category: Some("architecture".to_string()),
            cwe_id: None,
            why_it_matters: Some(
                "Changes to influential code have wide-reaching effects across the codebase."
                    .to_string(),
            ),
            ..Default::default()
        }
    }
}

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

impl Detector for InfluentialCodeDetector {
    fn name(&self) -> &'static str {
        "InfluentialCodeDetector"
    }

    fn description(&self) -> &'static str {
        "Detects influential code using PageRank analysis and function context"
    }

    fn category(&self) -> &'static str {
        "architecture"
    }

    fn config(&self) -> Option<&DetectorConfig> {
        Some(&self.config)
    }

    fn detect(
        &self,
        ctx: &crate::detectors::analysis_context::AnalysisContext,
    ) -> Result<Vec<Finding>> {
        let graph = ctx.graph;
        let contexts = &ctx.functions;
        let i = graph.interner();
        let mut findings = Vec::new();
        let func_idxs = graph.functions_idx();

        debug!(
            "InfluentialCodeDetector: analyzing {} functions with PageRank",
            func_idxs.len()
        );

        // Collect PageRank values for percentile computation
        let mut pagerank_values: Vec<f64> = func_idxs
            .iter()
            .map(|&idx| {
                graph
                    .primitives()
                    .page_rank
                    .get(&idx)
                    .copied()
                    .unwrap_or(0.0)
            })
            .collect();
        pagerank_values
            .sort_unstable_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let total = pagerank_values.len();
        if total == 0 {
            return Ok(findings);
        }

        // Helper: compute percentile rank for a given PageRank value
        let percentile_of = |pr: f64| -> f64 {
            if total <= 1 {
                return 1.0;
            }
            let rank = pagerank_values.partition_point(|&v| v < pr);
            rank as f64 / total as f64
        };

        for &func_idx in func_idxs {
            let Some(func) = graph.node_idx(func_idx) else {
                continue;
            };

            let fctx = contexts.get(func.qn(i));

            // Skip test functions
            if let Some(c) = fctx {
                if c.is_test || c.role == FunctionRole::Test {
                    continue;
                }
            }

            let page_rank = graph
                .primitives()
                .page_rank
                .get(&func_idx)
                .copied()
                .unwrap_or(0.0);
            let pagerank_pct = percentile_of(page_rank);

            // Skip functions below the PageRank percentile threshold
            if pagerank_pct < self.pagerank_percentile_threshold {
                continue;
            }

            let (fan_in, complexity, loc, role, betweenness) = if let Some(c) = fctx {
                (
                    c.in_degree,
                    c.complexity.unwrap_or(1) as usize,
                    c.loc as usize,
                    c.role,
                    Some(c.betweenness),
                )
            } else {
                let fan_in = graph.call_fan_in_idx(func_idx);
                let complexity = func.complexity_opt().unwrap_or(1) as usize;
                let loc = func.loc() as usize;
                let raw_b = graph
                    .primitives()
                    .betweenness
                    .get(&func_idx)
                    .copied()
                    .unwrap_or(0.0);
                let betweenness = if raw_b > 0.0 { Some(raw_b) } else { None };
                (fan_in, complexity, loc, FunctionRole::Unknown, betweenness)
            };

            // Role-aware filtering: still require complexity or size to flag
            let should_flag = match role {
                FunctionRole::Utility => {
                    // Utilities can have high PageRank. Only flag if complexity is extreme.
                    complexity >= self.high_complexity_threshold as usize * 2
                }
                FunctionRole::Hub => {
                    // Hubs are important - flag if complex or large
                    complexity >= self.high_complexity_threshold as usize
                        || loc >= self.high_loc_threshold as usize
                }
                FunctionRole::EntryPoint => {
                    // Entry points are expected to be influential. Only flag if very complex.
                    complexity >= self.high_complexity_threshold as usize * 2
                }
                FunctionRole::Test => false,
                FunctionRole::Leaf | FunctionRole::Orchestrator | FunctionRole::Unknown => {
                    // Default: require complexity or size
                    complexity >= self.high_complexity_threshold as usize
                        || loc >= self.high_loc_threshold as usize
                }
            };

            if should_flag {
                findings.push(self.create_finding(
                    func.node_name(i),
                    func.path(i),
                    LineRange::new(func.line_start, func.line_end),
                    page_rank,
                    pagerank_pct,
                    fan_in,
                    complexity,
                    loc,
                    role,
                    betweenness,
                ));
            }
        }

        debug!("InfluentialCodeDetector: found {} findings", findings.len());
        Ok(findings)
    }
}

impl crate::detectors::RegisteredDetector for InfluentialCodeDetector {
    fn create(init: &crate::detectors::DetectorInit) -> std::sync::Arc<dyn Detector> {
        std::sync::Arc::new(Self::with_config(
            init.config_for("InfluentialCodeDetector"),
        ))
    }
}

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

    #[test]
    fn test_severity_with_role() {
        let detector = InfluentialCodeDetector::new();

        // Utility with moderate complexity = Low (expected behavior)
        let sev = detector.calculate_severity(0.99, 10, 50, FunctionRole::Utility);
        assert_eq!(sev, Severity::Low);

        // Utility with extreme complexity = Medium (capped)
        let sev = detector.calculate_severity(0.99, 40, 200, FunctionRole::Utility);
        assert_eq!(sev, Severity::Medium);

        // Hub with high PageRank + high complexity = High
        let sev = detector.calculate_severity(0.99, 25, 100, FunctionRole::Hub);
        assert_eq!(sev, Severity::High);

        // Low PageRank percentile = Low severity regardless
        let sev = detector.calculate_severity(0.50, 25, 100, FunctionRole::Hub);
        assert_eq!(sev, Severity::Low);
    }

    #[test]
    fn test_severity_thresholds() {
        let detector = InfluentialCodeDetector::new();

        // p99 + complexity >= 20 => High
        assert_eq!(
            detector.calculate_severity(0.99, 20, 50, FunctionRole::Unknown),
            Severity::High
        );

        // p95 + complexity >= 15 => Medium
        assert_eq!(
            detector.calculate_severity(0.96, 15, 50, FunctionRole::Unknown),
            Severity::Medium
        );

        // p95 + low complexity => Low
        assert_eq!(
            detector.calculate_severity(0.96, 5, 50, FunctionRole::Unknown),
            Severity::Low
        );
    }

    #[test]
    fn test_default_percentile_threshold() {
        let detector = InfluentialCodeDetector::new();
        assert!((detector.pagerank_percentile_threshold - 0.90).abs() < f64::EPSILON);
    }
}