leindex 2.1.1

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
//! Merge compact SCIP facts into the resident PDG.

use crate::graph::pdg::{Edge, EdgeMetadata, EdgeType, NodeId, ProgramDependenceGraph};
use crate::intel::scip_ingest::{CompactScipFacts, DefinitionFact};
use std::collections::{HashMap, HashSet};
use std::path::Path;

/// Accounting returned by one precision merge.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PrecisionReport {
    /// Number of SCIP definitions inspected.
    pub definitions_seen: usize,
    /// Number of definitions matched to canonical Tier-0 nodes.
    pub definitions_matched: usize,
    /// Definitions with no canonical Tier-0 match.
    pub definitions_unmatched: usize,
    /// Number of SCIP relationships inspected.
    pub relationships_seen: usize,
    /// Existing heuristic relationships upgraded to confidence 1.0.
    pub relationships_upgraded: usize,
    /// New precise relationships added to the PDG.
    pub relationships_added: usize,
    /// Relationships whose endpoint symbols could not be matched.
    pub relationships_unmatched: usize,
}

/// Merge compact SCIP facts into `pdg` without creating unmatched nodes.
pub fn merge_facts(pdg: &mut ProgramDependenceGraph, facts: &CompactScipFacts) -> PrecisionReport {
    let mut report = PrecisionReport {
        definitions_seen: facts.definitions.len(),
        relationships_seen: facts.relationships.len(),
        ..PrecisionReport::default()
    };

    // `files` identifies the complete file snapshot represented by the facts.
    // Without it, a missing definition cannot be distinguished from an omitted
    // fact, so leave marker lifecycle untouched for graph/storage to reconcile.
    clear_precision_markers_for_files(pdg, &facts.files);

    let mut symbol_nodes: HashMap<String, NodeId> = HashMap::new();

    for definition in &facts.definitions {
        if let Some(node_id) = match_definition(pdg, definition) {
            symbol_nodes.insert(definition.symbol.clone(), node_id);
            if let Some(node) = pdg.get_node(node_id) {
                pdg.mark_precision_symbol(node.id.clone());
            }
            report.definitions_matched += 1;
        } else {
            report.definitions_unmatched += 1;
        }
    }

    for relationship in &facts.relationships {
        let Some(&source) = symbol_nodes.get(&relationship.source) else {
            report.relationships_unmatched += 1;
            continue;
        };
        let Some(&target) = symbol_nodes.get(&relationship.target) else {
            report.relationships_unmatched += 1;
            continue;
        };
        let Some(edge_type) = relationship_edge_type(&relationship.kind) else {
            report.relationships_unmatched += 1;
            continue;
        };
        let mut upgraded = false;
        let edge_ids: Vec<_> = pdg
            .edge_indices()
            .filter(|&edge_id| {
                pdg.edge_endpoints(edge_id) == Some((source, target))
                    && pdg
                        .get_edge(edge_id)
                        .is_some_and(|edge| edge.edge_type == edge_type)
            })
            .collect();
        for edge_id in edge_ids {
            if let Some(edge) = pdg.get_edge_mut(edge_id) {
                edge.metadata.confidence = Some(1.0);
                upgraded = true;
            }
        }
        if upgraded {
            report.relationships_upgraded += 1;
        } else {
            pdg.add_edge(
                source,
                target,
                Edge {
                    edge_type,
                    metadata: EdgeMetadata::with_confidence(1.0),
                },
            );
            report.relationships_added += 1;
        }
    }

    report
}

fn relationship_edge_type(kind: &str) -> Option<EdgeType> {
    match kind {
        "call" => Some(EdgeType::Call),
        "inheritance" | "implements" | "extends" => Some(EdgeType::Inheritance),
        "type_of" | "type-definition" => Some(EdgeType::TypeOf),
        _ => None,
    }
}

fn clear_precision_markers_for_files(pdg: &mut ProgramDependenceGraph, files: &[String]) {
    if files.is_empty() || pdg.precision_symbols().is_empty() {
        return;
    }

    let affected_node_ids: HashSet<String> = pdg
        .node_indices()
        .filter_map(|node_id| {
            let node = pdg.get_node(node_id)?;
            files
                .iter()
                .any(|file_path| same_path(&node.file_path, file_path))
                .then(|| node.id.clone())
        })
        .collect();
    pdg.retain_precision_symbols(|node_id| !affected_node_ids.contains(node_id));
}

fn match_definition(pdg: &ProgramDependenceGraph, definition: &DefinitionFact) -> Option<NodeId> {
    let candidates = pdg.nodes_in_file(&definition.file_path);
    let candidates = if candidates.is_empty() {
        pdg.node_indices()
            .filter(|&node_id| {
                pdg.get_node(node_id)
                    .is_some_and(|node| same_path(&node.file_path, &definition.file_path))
            })
            .collect()
    } else {
        candidates
    };

    // A source range is the authoritative identity when it is available. Do
    // not make a range match compete with the weaker name fallback: generated
    // graphs can contain duplicate names (and, defensively, duplicate ranges).
    if let Some(range) = definition.byte_range {
        if let Some(node_id) = candidates.iter().copied().find(|&node_id| {
            pdg.get_node(node_id)
                .is_some_and(|node| node.byte_range == range)
        }) {
            return Some(node_id);
        }
    }

    // SCIP display names are often only the short name. A qualified-name
    // fallback is therefore useful, but it must never silently select the
    // first overloaded candidate. Keep the set of matching node IDs and only
    // accept the fallback when exactly one candidate remains.
    let name_matches: HashSet<NodeId> = candidates
        .into_iter()
        .filter(|&node_id| {
            pdg.get_node(node_id).is_some_and(|node| {
                matches_name_or_qualified_name(node, &definition.qualified_name)
            })
        })
        .collect();
    (name_matches.len() == 1).then(|| name_matches.into_iter().next().unwrap())
}

fn matches_name_or_qualified_name(node: &crate::graph::pdg::Node, qualified_name: &str) -> bool {
    // A short display name is a valid fallback only when the SCIP value is
    // short too. A qualified SCIP name must match the node's qualified name,
    // otherwise two namespaces containing the same short name are ambiguous.
    if node.name == qualified_name {
        return true;
    }
    if qualified_name.is_empty() {
        return false;
    }

    // Duplicate qualified names are suffixed with `@start..end` by PDG
    // extraction. Remove that suffix before comparing the canonical qname.
    let id_without_range = node
        .id
        .split_once('@')
        .map_or(node.id.as_str(), |(id, _)| id);
    if id_without_range.ends_with(&format!(":{qualified_name}")) {
        return true;
    }

    // A qname's final component is useful only for a short SCIP display name;
    // this is deliberately not used when `qualified_name` contains a scope.
    let is_short_name = !qualified_name.contains("::") && !qualified_name.contains('.');
    is_short_name
        && id_without_range
            .rsplit_once(':')
            .is_some_and(|(_, short_name)| short_name == qualified_name)
}

fn same_path(left: &str, right: &str) -> bool {
    if left == right {
        return true;
    }
    let left = Path::new(left);
    let right = Path::new(right);
    left.ends_with(right) || right.ends_with(left)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::graph::pdg::{Node, NodeType};
    use crate::intel::scip_ingest::RelationshipFact;
    use std::sync::Arc;

    fn fixture() -> ProgramDependenceGraph {
        let mut pdg = ProgramDependenceGraph::new();
        pdg.add_node(Node {
            id: "src/lib.rs:Thing".to_string(),
            node_type: NodeType::Class,
            name: "Thing".to_string(),
            file_path: Arc::from("src/lib.rs"),
            byte_range: (0, 5),
            complexity: 1,
            language: "rust".to_string(),
        });
        pdg.add_node(Node {
            id: "src/lib.rs:value".to_string(),
            node_type: NodeType::Variable,
            name: "value".to_string(),
            file_path: Arc::from("src/lib.rs"),
            byte_range: (6, 11),
            complexity: 0,
            language: "rust".to_string(),
        });
        pdg
    }

    #[test]
    fn test_merge_marks_definition_and_adds_type_edge() {
        let mut pdg = fixture();
        let facts = CompactScipFacts {
            definitions: vec![
                DefinitionFact {
                    symbol: "thing".to_string(),
                    file_path: "src/lib.rs".to_string(),
                    byte_range: Some((0, 5)),
                    qualified_name: "Thing".to_string(),
                    signature: None,
                },
                DefinitionFact {
                    symbol: "value".to_string(),
                    file_path: "src/lib.rs".to_string(),
                    byte_range: Some((6, 11)),
                    qualified_name: "value".to_string(),
                    signature: None,
                },
            ],
            relationships: vec![RelationshipFact {
                source: "thing".to_string(),
                target: "value".to_string(),
                kind: "type_of".to_string(),
            }],
            ..CompactScipFacts::default()
        };
        let report = merge_facts(&mut pdg, &facts);
        assert_eq!(report.definitions_matched, 2);
        assert_eq!(report.relationships_added, 1);
        assert_eq!(pdg.edge_count(), 1);
        assert!(pdg.is_precision_symbol("src/lib.rs:Thing"));
    }

    #[test]
    fn test_merge_upgrades_existing_edge_without_duplicate() {
        let mut pdg = fixture();
        let source = pdg.find_by_id("src/lib.rs:Thing").unwrap();
        let target = pdg.find_by_id("src/lib.rs:value").unwrap();
        pdg.add_edge(
            source,
            target,
            Edge {
                edge_type: EdgeType::Call,
                metadata: EdgeMetadata::with_confidence(0.4),
            },
        );
        let facts = CompactScipFacts {
            definitions: vec![
                DefinitionFact {
                    symbol: "thing".to_string(),
                    file_path: "src/lib.rs".to_string(),
                    byte_range: Some((0, 5)),
                    qualified_name: "Thing".to_string(),
                    signature: None,
                },
                DefinitionFact {
                    symbol: "value".to_string(),
                    file_path: "src/lib.rs".to_string(),
                    byte_range: Some((6, 11)),
                    qualified_name: "value".to_string(),
                    signature: None,
                },
            ],
            relationships: vec![RelationshipFact {
                source: "thing".to_string(),
                target: "value".to_string(),
                kind: "call".to_string(),
            }],
            ..CompactScipFacts::default()
        };
        let report = merge_facts(&mut pdg, &facts);
        assert_eq!(report.relationships_upgraded, 1);
        assert_eq!(pdg.edge_count(), 1);
        let edge_id = pdg.edge_indices().next().unwrap();
        assert_eq!(
            pdg.get_edge(edge_id).unwrap().metadata.confidence,
            Some(1.0)
        );
    }

    #[test]
    fn test_merge_leaves_ambiguous_name_fallback_unmatched() {
        let mut pdg = ProgramDependenceGraph::new();
        for (id, byte_range) in [
            ("src/lib.rs:first:overloaded", (0, 10)),
            ("src/lib.rs:second:overloaded", (20, 30)),
        ] {
            pdg.add_node(Node {
                id: id.to_string(),
                node_type: NodeType::Function,
                name: "overloaded".to_string(),
                file_path: Arc::from("src/lib.rs"),
                byte_range,
                complexity: 1,
                language: "rust".to_string(),
            });
        }

        let facts = CompactScipFacts {
            definitions: vec![DefinitionFact {
                symbol: "overloaded".to_string(),
                file_path: "src/lib.rs".to_string(),
                byte_range: None,
                qualified_name: "overloaded".to_string(),
                signature: None,
            }],
            ..CompactScipFacts::default()
        };
        let report = merge_facts(&mut pdg, &facts);

        assert_eq!(report.definitions_matched, 0);
        assert_eq!(report.definitions_unmatched, 1);
        assert!(!pdg.is_precision_symbol("src/lib.rs:first:overloaded"));
        assert!(!pdg.is_precision_symbol("src/lib.rs:second:overloaded"));
    }

    #[test]
    fn test_merge_range_match_wins_for_ambiguous_name() {
        let mut pdg = ProgramDependenceGraph::new();
        for (id, byte_range) in [
            ("src/lib.rs:first:overloaded", (0, 10)),
            ("src/lib.rs:second:overloaded", (20, 30)),
        ] {
            pdg.add_node(Node {
                id: id.to_string(),
                node_type: NodeType::Function,
                name: "overloaded".to_string(),
                file_path: Arc::from("src/lib.rs"),
                byte_range,
                complexity: 1,
                language: "rust".to_string(),
            });
        }

        let facts = CompactScipFacts {
            definitions: vec![DefinitionFact {
                symbol: "overloaded".to_string(),
                file_path: "src/lib.rs".to_string(),
                byte_range: Some((20, 30)),
                qualified_name: "overloaded".to_string(),
                signature: None,
            }],
            ..CompactScipFacts::default()
        };
        let report = merge_facts(&mut pdg, &facts);

        assert_eq!(report.definitions_matched, 1);
        assert!(pdg.is_precision_symbol("src/lib.rs:second:overloaded"));
        assert!(!pdg.is_precision_symbol("src/lib.rs:first:overloaded"));
    }

    #[test]
    fn test_merge_qualified_name_fallback_requires_exact_scope() {
        let mut pdg = ProgramDependenceGraph::new();
        for (id, name, byte_range) in [
            ("src/lib.rs:alpha::run", "run", (0, 10)),
            ("src/lib.rs:beta::run", "run", (20, 30)),
        ] {
            pdg.add_node(Node {
                id: id.to_string(),
                node_type: NodeType::Function,
                name: name.to_string(),
                file_path: Arc::from("src/lib.rs"),
                byte_range,
                complexity: 1,
                language: "rust".to_string(),
            });
        }

        let facts = CompactScipFacts {
            definitions: vec![DefinitionFact {
                symbol: "qualified-run".to_string(),
                file_path: "src/lib.rs".to_string(),
                byte_range: None,
                qualified_name: "beta::run".to_string(),
                signature: None,
            }],
            ..CompactScipFacts::default()
        };
        let report = merge_facts(&mut pdg, &facts);

        assert_eq!(report.definitions_matched, 1);
        assert!(pdg.is_precision_symbol("src/lib.rs:beta::run"));
        assert!(!pdg.is_precision_symbol("src/lib.rs:alpha::run"));
    }

    #[test]
    fn test_merge_rerun_clears_markers_only_for_identified_files() {
        let mut pdg = fixture();
        let other_file_node = pdg.add_node(Node {
            id: "src/other.rs:Other".to_string(),
            node_type: NodeType::Class,
            name: "Other".to_string(),
            file_path: Arc::from("src/other.rs"),
            byte_range: (0, 5),
            complexity: 1,
            language: "rust".to_string(),
        });
        pdg.mark_precision_symbol("src/lib.rs:Thing");
        pdg.mark_precision_symbol("src/other.rs:Other");
        pdg.mark_precision_symbol("src/lib.rs:removed");

        let facts = CompactScipFacts {
            files: vec!["src/lib.rs".to_string()],
            ..CompactScipFacts::default()
        };
        merge_facts(&mut pdg, &facts);

        assert!(!pdg.is_precision_symbol("src/lib.rs:Thing"));
        assert!(pdg.is_precision_symbol("src/lib.rs:removed"));
        assert!(pdg.is_precision_symbol(&pdg.get_node(other_file_node).unwrap().id));
    }
}