leindex 1.6.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! Impact analysis for edit changes

use crate::edit::ResolvedEditChange;
use crate::graph::pdg::{NodeId, NodeType, TraversalConfig};
use crate::graph::ProgramDependenceGraph;
use crate::validation::ValidationError;
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::Arc;

/// Location in source code
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Location {
    /// Line number (1-indexed)
    pub line: usize,
    /// Column number (1-indexed, in bytes)
    pub column: usize,
}

impl std::fmt::Display for Location {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.line, self.column)
    }
}

impl Location {
    /// Create a new location
    pub fn new(line: usize, column: usize) -> Self {
        Self { line, column }
    }
}

/// Risk level of an impact
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum RiskLevel {
    /// Low risk - local changes only
    Low = 0,
    /// Medium risk - affects same module
    Medium = 1,
    /// High risk - affects multiple modules
    High = 2,
    /// Critical risk - affects public API
    Critical = 3,
}

impl RiskLevel {
    /// Get a description of this risk level
    pub fn description(&self) -> &str {
        match self {
            Self::Low => "Local changes only",
            Self::Medium => "Affects same module",
            Self::High => "Affects multiple modules",
            Self::Critical => "Affects public API",
        }
    }

    /// Get the color code for terminal output
    pub fn color_code(&self) -> &str {
        match self {
            Self::Low => "\x1b[32m",      // Green
            Self::Medium => "\x1b[33m",   // Yellow
            Self::High => "\x1b[31m",     // Red
            Self::Critical => "\x1b[35m", // Magenta
        }
    }
}

impl std::fmt::Display for RiskLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

/// Impact analysis report
#[derive(Debug, Clone)]
pub struct ImpactReport {
    /// Risk level of the change
    pub risk_level: RiskLevel,
    /// Number of affected nodes
    pub affected_nodes: usize,
    /// Files affected by the change
    pub affected_files: Vec<PathBuf>,
    /// Public APIs affected
    pub affected_apis: Vec<String>,
    /// Detailed description
    pub description: String,
}

impl ImpactReport {
    /// Create a new impact report
    pub fn new(
        risk_level: RiskLevel,
        affected_nodes: usize,
        affected_files: Vec<PathBuf>,
        affected_apis: Vec<String>,
    ) -> Self {
        let description = format!(
            "Risk: {}, Affected nodes: {}, Files: {}, APIs: {}",
            risk_level,
            affected_nodes,
            affected_files.len(),
            affected_apis.len()
        );

        Self {
            risk_level,
            affected_nodes,
            affected_files,
            affected_apis,
            description,
        }
    }

    /// Create a minimal (low risk) impact report
    pub fn minimal() -> Self {
        Self::new(RiskLevel::Low, 0, vec![], vec![])
    }
}

/// Impact analyzer using PDG
#[derive(Clone)]
pub struct ImpactAnalyzer {
    /// PDG for impact analysis
    pdg: Arc<ProgramDependenceGraph>,
}

impl ImpactAnalyzer {
    /// Create a new impact analyzer
    pub fn new(pdg: Arc<ProgramDependenceGraph>) -> Self {
        Self { pdg }
    }

    /// Analyze impact for edit changes
    ///
    /// # Arguments
    /// * `changes` - Edit changes to analyze
    ///
    /// # Returns
    /// Impact report with analysis results
    pub fn analyze_impact(
        &self,
        changes: &[ResolvedEditChange],
    ) -> Result<ImpactReport, ValidationError> {
        if changes.is_empty() {
            return Ok(ImpactReport::minimal());
        }

        let mut affected_files = HashSet::new();
        let mut affected_apis = HashSet::new();
        let mut total_affected_nodes = 0;

        for change in changes {
            // Find nodes in the changed file
            let file_path = change.file_path.to_string_lossy().to_string();
            let nodes_in_file = self.pdg.nodes_in_file(&file_path);

            // For each node, calculate forward and backward impact
            let mut affected = HashSet::new();

            for node_id in &nodes_in_file {
                // Forward impact (nodes that depend on this)
                let forward = self
                    .pdg
                    .forward_impact(*node_id, &TraversalConfig::for_impact_analysis());
                affected.extend(forward);

                // Backward impact (nodes this depends on)
                let backward = self
                    .pdg
                    .backward_impact(*node_id, &TraversalConfig::for_impact_analysis());
                affected.extend(backward);

                affected.insert(*node_id);
            }

            total_affected_nodes += affected.len();

            // Collect affected files
            for node_id in &affected {
                if let Some(node) = self.pdg.get_node(*node_id) {
                    affected_files.insert(PathBuf::from(&*node.file_path));

                    // Check if this is a public API
                    if matches!(
                        node.node_type,
                        NodeType::Function | NodeType::Method | NodeType::Class
                    ) {
                        affected_apis.insert(node.name.clone());
                    }
                }
            }

            // Always include the changed file
            affected_files.insert(change.file_path.clone());
        }

        // Calculate risk level
        let risk_level =
            self.calculate_risk_level(total_affected_nodes, &affected_files, &affected_apis);

        Ok(ImpactReport::new(
            risk_level,
            total_affected_nodes,
            affected_files.into_iter().collect(),
            affected_apis.into_iter().collect(),
        ))
    }

    /// Calculate risk level based on impact metrics
    fn calculate_risk_level(
        &self,
        affected_nodes: usize,
        affected_files: &HashSet<PathBuf>,
        affected_apis: &HashSet<String>,
    ) -> RiskLevel {
        // If public APIs are affected, it's at least high risk
        if !affected_apis.is_empty() {
            // Check if any of the affected APIs are in a public location
            // (e.g., not in test files, not in private modules)
            for api in affected_apis {
                if self.is_public_api(api) {
                    return RiskLevel::Critical;
                }
            }
            return RiskLevel::High;
        }

        // If multiple files are affected, it's high risk
        if affected_files.len() > 3 {
            return RiskLevel::High;
        }

        // If multiple files are affected, it's medium risk
        if affected_files.len() > 1 {
            return RiskLevel::Medium;
        }

        // If many nodes are affected in the same file, it's medium risk
        if affected_nodes > 10 {
            return RiskLevel::Medium;
        }

        // Default to low risk
        RiskLevel::Low
    }

    /// Check if an API is public (exported)
    fn is_public_api(&self, api_name: &str) -> bool {
        // Check if the API is in a public location
        if let Some(node_id) = self.pdg.find_by_symbol(api_name) {
            if let Some(node) = self.pdg.get_node(node_id) {
                // Not in a test file
                if !node.file_path.contains("test") && !node.file_path.contains("spec") {
                    // Not in a private/internal module
                    return !node.file_path.contains("internal")
                        && !node.file_path.contains("private");
                }
            }
        }
        false
    }

    /// Get the forward impact (nodes reachable from a node)
    pub fn get_forward_impact(&self, node_id: NodeId) -> Vec<NodeId> {
        self.pdg
            .forward_impact(node_id, &TraversalConfig::for_impact_analysis())
    }

    /// Get the backward impact (nodes that can reach a node)
    pub fn get_backward_impact(&self, node_id: NodeId) -> Vec<NodeId> {
        self.pdg
            .backward_impact(node_id, &TraversalConfig::for_impact_analysis())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::pdg::{EdgeMetadata, EdgeType};
    use crate::graph::Node;

    #[test]
    fn test_location_new() {
        let loc = Location::new(10, 5);
        assert_eq!(loc.line, 10);
        assert_eq!(loc.column, 5);
    }

    #[test]
    fn test_location_display() {
        let loc = Location::new(10, 5);
        assert_eq!(loc.to_string(), "10:5");
    }

    #[test]
    fn test_risk_level_ordering() {
        assert!(RiskLevel::Low < RiskLevel::Medium);
        assert!(RiskLevel::Medium < RiskLevel::High);
        assert!(RiskLevel::High < RiskLevel::Critical);
    }

    #[test]
    fn test_risk_level_description() {
        assert_eq!(RiskLevel::Low.description(), "Local changes only");
        assert_eq!(RiskLevel::Medium.description(), "Affects same module");
        assert_eq!(RiskLevel::High.description(), "Affects multiple modules");
        assert_eq!(RiskLevel::Critical.description(), "Affects public API");
    }

    #[test]
    fn test_risk_level_display() {
        assert_eq!(RiskLevel::Low.to_string(), "Local changes only");
        assert_eq!(RiskLevel::Critical.to_string(), "Affects public API");
    }

    #[test]
    fn test_risk_level_equality() {
        assert_eq!(RiskLevel::Low, RiskLevel::Low);
        assert_ne!(RiskLevel::Low, RiskLevel::High);
    }

    #[test]
    fn test_impact_report_new() {
        let report = ImpactReport::new(
            RiskLevel::Low,
            5,
            vec![PathBuf::from("test.py")],
            vec!["my_func".to_string()],
        );

        assert_eq!(report.risk_level, RiskLevel::Low);
        assert_eq!(report.affected_nodes, 5);
        assert_eq!(report.affected_files.len(), 1);
        assert_eq!(report.affected_apis.len(), 1);
    }

    #[test]
    fn test_impact_report_minimal() {
        let report = ImpactReport::minimal();
        assert_eq!(report.risk_level, RiskLevel::Low);
        assert_eq!(report.affected_nodes, 0);
        assert!(report.affected_files.is_empty());
        assert!(report.affected_apis.is_empty());
    }

    #[test]
    fn test_impact_analyzer_new() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let _analyzer = ImpactAnalyzer::new(pdg);
        // Just verify it was created
        assert!(true);
    }

    #[test]
    fn test_analyze_impact_empty_changes() {
        let pdg = Arc::new(ProgramDependenceGraph::new());
        let analyzer = ImpactAnalyzer::new(pdg);
        let changes: &[ResolvedEditChange] = &[];
        let report = analyzer.analyze_impact(changes).unwrap();
        assert_eq!(report.risk_level, RiskLevel::Low);
        assert_eq!(report.affected_nodes, 0);
    }

    #[test]
    fn test_analyze_impact_single_change() {
        let mut pdg = ProgramDependenceGraph::new();

        // Add a node
        let node = Node {
            id: "my_func".to_string(),
            node_type: NodeType::Function,
            name: "my_func".to_string(),
            file_path: Arc::from("test.py"),
            byte_range: (0, 100),
            complexity: 1,
            language: "python".to_string(),
        };
        pdg.add_node(node);

        let analyzer = ImpactAnalyzer::new(Arc::new(pdg));

        let change = ResolvedEditChange::new(
            PathBuf::from("test.py"),
            "old content".to_string(),
            "new content".to_string(),
        );

        let report = analyzer.analyze_impact(&[change]).unwrap();
        assert!(!report.affected_files.is_empty());
    }

    #[test]
    fn test_analyze_impact_with_dependencies() {
        let mut pdg = ProgramDependenceGraph::new();

        // Create a dependency chain: func_a -> func_b -> func_c
        let node_a = pdg.add_node(Node {
            id: "func_a".to_string(),
            node_type: NodeType::Function,
            name: "func_a".to_string(),
            file_path: Arc::from("a.py"),
            byte_range: (0, 100),
            complexity: 1,
            language: "python".to_string(),
        });

        let node_b = pdg.add_node(Node {
            id: "func_b".to_string(),
            node_type: NodeType::Function,
            name: "func_b".to_string(),
            file_path: Arc::from("b.py"),
            byte_range: (0, 100),
            complexity: 1,
            language: "python".to_string(),
        });

        let node_c = pdg.add_node(Node {
            id: "func_c".to_string(),
            node_type: NodeType::Function,
            name: "func_c".to_string(),
            file_path: Arc::from("c.py"),
            byte_range: (0, 100),
            complexity: 1,
            language: "python".to_string(),
        });

        // Add edges: a calls b, b calls c
        pdg.add_edge(
            node_a,
            node_b,
            crate::graph::Edge {
                edge_type: EdgeType::Call,
                metadata: EdgeMetadata {
                    call_count: None,
                    confidence: None,
                    variable_name: None,
                },
            },
        );
        pdg.add_edge(
            node_b,
            node_c,
            crate::graph::Edge {
                edge_type: EdgeType::Call,
                metadata: EdgeMetadata {
                    call_count: None,
                    confidence: None,
                    variable_name: None,
                },
            },
        );

        let analyzer = ImpactAnalyzer::new(Arc::new(pdg));

        // Change to func_c should show impact
        let change =
            ResolvedEditChange::new(PathBuf::from("c.py"), "old".to_string(), "new".to_string());

        let report = analyzer.analyze_impact(&[change]).unwrap();
        // func_c is in c.py, and func_b and func_a depend on it
        assert!(report.affected_nodes >= 1);
    }

    #[test]
    fn test_is_public_api() {
        let mut pdg = ProgramDependenceGraph::new();

        let node = Node {
            id: "public_func".to_string(),
            node_type: NodeType::Function,
            name: "public_func".to_string(),
            file_path: Arc::from("src/lib.rs"),
            byte_range: (0, 100),
            complexity: 1,
            language: "rust".to_string(),
        };
        pdg.add_node(node);

        let analyzer = ImpactAnalyzer::new(Arc::new(pdg));
        assert!(analyzer.is_public_api("public_func"));
    }

    #[test]
    fn test_is_public_api_test_file() {
        let mut pdg = ProgramDependenceGraph::new();

        let node = Node {
            id: "test_func".to_string(),
            node_type: NodeType::Function,
            name: "test_func".to_string(),
            file_path: Arc::from("tests/test_lib.rs"),
            byte_range: (0, 100),
            complexity: 1,
            language: "rust".to_string(),
        };
        pdg.add_node(node);

        let analyzer = ImpactAnalyzer::new(Arc::new(pdg));
        assert!(!analyzer.is_public_api("test_func"));
    }

    #[test]
    fn test_is_public_api_internal_module() {
        let mut pdg = ProgramDependenceGraph::new();

        let node = Node {
            id: "internal_func".to_string(),
            node_type: NodeType::Function,
            name: "internal_func".to_string(),
            file_path: Arc::from("src/internal/mod.rs"),
            byte_range: (0, 100),
            complexity: 1,
            language: "rust".to_string(),
        };
        pdg.add_node(node);

        let analyzer = ImpactAnalyzer::new(Arc::new(pdg));
        assert!(!analyzer.is_public_api("internal_func"));
    }
}