leindex 1.9.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
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
use super::*;

/// Resolve cross-file call edges after all per-file PDGs have been merged.
///
/// During per-file PDG extraction, `extract_call_edges` can only resolve calls
/// to symbols defined in the same file (because the resolution maps are built
/// from that file's signatures alone). This function performs a second pass
/// over the merged PDG using ALL signatures from ALL files, adding call edges
/// that span file boundaries.
///
/// # Arguments
///
/// * `pdg` - The merged PDG containing nodes from all files
/// * `all_signatures` - Signatures from all files in the project
///
/// This function mutates the PDG in-place, adding new `Call` edges for
/// cross-file call relationships that were not resolved during per-file
/// extraction.
pub fn resolve_cross_file_call_edges(
    pdg: &mut ProgramDependenceGraph,
    all_signatures: &[SignatureInfo],
) {
    let owned: Vec<(Option<&str>, &SignatureInfo)> =
        all_signatures.iter().map(|sig| (None, sig)).collect();
    resolve_cross_file_call_edges_inner(pdg, &owned);
}

/// Resolve cross-file call edges with each signature bound to its source file.
///
/// Real indexing still knows which parse result produced each signature. Keeping
/// that ownership here prevents duplicate `qualified_name` definitions in
/// different files from receiving each other's calls.
pub fn resolve_cross_file_call_edges_for_files(
    pdg: &mut ProgramDependenceGraph,
    all_signatures: &[(String, SignatureInfo)],
) {
    let owned: Vec<(Option<&str>, &SignatureInfo)> = all_signatures
        .iter()
        .map(|(file_path, sig)| (Some(file_path.as_str()), sig))
        .collect();
    resolve_cross_file_call_edges_inner(pdg, &owned);
}

struct CrossFileCallIndexes {
    qname_to_node: HashMap<String, Vec<crate::graph::pdg::NodeId>>,
    qname_file_to_node: HashMap<(String, String), Vec<crate::graph::pdg::NodeId>>,
    exact_map: HashMap<String, Vec<crate::graph::pdg::NodeId>>,
    last_map: HashMap<String, Vec<crate::graph::pdg::NodeId>>,
    suffix_map: HashMap<String, Vec<crate::graph::pdg::NodeId>>,
}

fn build_cross_file_call_indexes(pdg: &ProgramDependenceGraph) -> CrossFileCallIndexes {
    let mut indexes = CrossFileCallIndexes {
        qname_to_node: HashMap::new(),
        qname_file_to_node: HashMap::new(),
        exact_map: HashMap::new(),
        last_map: HashMap::new(),
        suffix_map: HashMap::new(),
    };

    for nid in pdg.node_indices() {
        let Some(node) = pdg.get_node(nid) else {
            continue;
        };
        if node.node_type == NodeType::External {
            continue;
        }
        let Some(qname) = qualified_name_from_node(node) else {
            tracing::debug!(
                node_id = %node.id,
                file_path = %node.file_path,
                "Skipping node whose id does not start with its file_path prefix"
            );
            continue;
        };

        let is_module = node.node_type == NodeType::Module;
        if !is_module {
            indexes
                .qname_to_node
                .entry(qname.to_string())
                .or_default()
                .push(nid);
            indexes
                .qname_file_to_node
                .entry((qname.to_string(), node.file_path.to_string()))
                .or_default()
                .push(nid);
        } else {
            indexes.qname_to_node.entry(qname.to_string()).or_default();
        }

        let normalized = normalize_symbol(qname);
        let segments: Vec<&str> = normalized.split('.').filter(|s| !s.is_empty()).collect();
        indexes
            .exact_map
            .entry(normalized.clone())
            .or_default()
            .push(nid);
        if let Some(last) = segments.last() {
            indexes
                .last_map
                .entry(last.to_string())
                .or_default()
                .push(nid);
        }
        for len in 2..=3_usize.min(segments.len()) {
            indexes
                .suffix_map
                .entry(segments[segments.len() - len..].join("."))
                .or_default()
                .push(nid);
        }
        if !is_module {
            indexes
                .last_map
                .entry(node.name.to_string())
                .or_default()
                .push(nid);
        }
    }

    indexes
}

const COMMON_CALL_NAMES: &[&str] = &[
    "new",
    "clone",
    "name",
    "len",
    "get",
    "set",
    "add",
    "remove",
    "push",
    "pop",
    "iter",
    "next",
    "send",
    "recv",
    "read",
    "write",
    "open",
    "close",
    "start",
    "stop",
    "run",
    "exec",
    "call",
    "apply",
    "map",
    "filter",
    "fold",
    "collect",
    "into",
    "from",
    "to",
    "as",
    "is",
    "has",
    "can",
    "should",
    "will",
    "do",
    "done",
    "ok",
    "err",
    "some",
    "none",
    "true",
    "false",
    "nil",
    "null",
    "self",
    "super",
    "init",
    "free",
    "drop",
    "copy",
    "dup",
    "swap",
    "cmp",
    "eq",
    "ne",
    "lt",
    "le",
    "gt",
    "ge",
    "hash",
    "fmt",
    "dbg",
    "print",
    "println",
    "format",
    "parse",
    "unwrap",
    "expect",
    "ok_or",
    "ok_or_else",
    "map_err",
    "and_then",
    "or_else",
    "unwrap_or",
    "unwrap_or_else",
    "unwrap_or_default",
    "is_some",
    "is_none",
    "is_ok",
    "is_err",
    "as_ref",
    "as_mut",
    "as_str",
    "as_bytes",
    "trim",
    "split",
    "join",
    "replace",
    "contains",
    "starts_with",
    "ends_with",
    "find",
    "rfind",
    "matches",
    "to_string",
    "to_owned",
    "to_vec",
    "into_string",
    "into_bytes",
    "into_vec",
    "into_iter",
    "keys",
    "values",
    "entries",
    "execute",
    "handle",
    "process",
    "update",
    "create",
    "delete",
    "save",
    "load",
    "fetch",
    "build",
    "make",
    "spawn",
    "fork",
    "warn",
    "info",
    "error",
    "trace",
    "debug",
    "log",
    "json",
    "yaml",
    "toml",
    "xml",
    "html",
    "csv",
    "Ok",
    "Err",
    "Some",
    "None",
    "Result",
    "Option",
    "Vec",
    "Box",
    "Rc",
    "Arc",
    "Cell",
    "RefCell",
    "Mutex",
    "RwLock",
    "String",
    "str",
    "HashMap",
    "HashSet",
    "BTreeMap",
    "BTreeSet",
    "VecDeque",
    "LinkedList",
    "JsonRpcError",
    "Error",
    "Response",
    "Request",
    "Value",
    "serde",
    "serde_json",
    "display",
    "render",
];

/// Resolve call targets across all files.
///
/// This function intentionally fans out to ALL matching candidates rather than
/// selecting a unique callee. Connecting to every possible target ensures no
/// real call edge is missed — false-positive connectivity is preferable to
/// false-negative missed edges for a code-intelligence tool. Callers can filter
/// edges by confidence or traversal depth downstream.
///
/// Targets are deduplicated before return to prevent the same NodeId from
/// appearing multiple times through different candidate-resolution paths.
fn resolve_cross_file_call_targets(
    candidates: &[String],
    indexes: &CrossFileCallIndexes,
) -> Vec<crate::graph::pdg::NodeId> {
    let mut targets = Vec::new();
    let mut last_segment_fallback = None;

    for candidate in candidates {
        let normalized = normalize_symbol(candidate);
        let segments: Vec<&str> = normalized.split('.').filter(|s| !s.is_empty()).collect();
        if let Some(ids) = indexes.exact_map.get(&normalized) {
            targets.extend(ids);
        }
        for len in 2..=3_usize.min(segments.len()) {
            if let Some(ids) = indexes
                .suffix_map
                .get(&segments[segments.len() - len..].join("."))
            {
                targets.extend(ids);
            }
        }
        if segments.len() == 1 {
            if let Some(ids) = indexes.last_map.get(&normalized) {
                targets.extend(ids);
            }
        }
        if last_segment_fallback.is_none() {
            last_segment_fallback = segments.last().map(|segment| (*segment).to_string());
        }
    }

    if targets.is_empty() {
        if let Some(last) = last_segment_fallback {
            if last.len() > 2 && !COMMON_CALL_NAMES.contains(&last.as_str()) {
                if let Some(ids) = indexes.last_map.get(&last) {
                    targets.extend(ids);
                }
            }
        }
    }
    targets.sort_unstable();
    targets.dedup();
    targets
}

fn cross_file_type_target(
    call_target: &str,
    indexes: &CrossFileCallIndexes,
) -> Option<crate::graph::pdg::NodeId> {
    let callee_name = normalize_symbol(call_target);
    let (scoped_prefix, _) = callee_name.rsplit_once('.')?;
    let bare_type = scoped_prefix.rsplit('.').next().unwrap_or(scoped_prefix);
    bare_type
        .chars()
        .next()
        .is_some_and(|c| c.is_uppercase())
        .then_some(())?;
    indexes
        .qname_to_node
        .get(scoped_prefix)
        .and_then(|ids| ids.first())
        .or_else(|| indexes.last_map.get(bare_type).and_then(|ids| ids.first()))
        .copied()
}

fn existing_call_edges(
    pdg: &ProgramDependenceGraph,
) -> HashSet<(crate::graph::pdg::NodeId, crate::graph::pdg::NodeId)> {
    pdg.edge_indices()
        .filter(|&edge_idx| {
            pdg.get_edge(edge_idx)
                .is_some_and(|edge| edge.edge_type == EdgeType::Call)
        })
        .filter_map(|edge_idx| pdg.edge_endpoints(edge_idx))
        .collect()
}

fn queue_cross_file_call_edge(
    caller_id: crate::graph::pdg::NodeId,
    target_id: crate::graph::pdg::NodeId,
    existing_edges: &mut HashSet<(crate::graph::pdg::NodeId, crate::graph::pdg::NodeId)>,
    new_edges: &mut Vec<(crate::graph::pdg::NodeId, crate::graph::pdg::NodeId)>,
) {
    if caller_id != target_id && existing_edges.insert((caller_id, target_id)) {
        new_edges.push((caller_id, target_id));
    }
}

fn resolve_cross_file_call_edges_inner(
    pdg: &mut ProgramDependenceGraph,
    all_signatures: &[(Option<&str>, &SignatureInfo)],
) {
    let indexes = build_cross_file_call_indexes(pdg);
    let mut existing_edges = existing_call_edges(pdg);

    let mut new_edges = Vec::new();

    for (source_file, sig) in all_signatures {
        let alias_map = import_alias_map(&sig.imports);
        // Real indexing passes the source file for each signature. Use it to
        // bind duplicate qualified names to their owning PDG node; the legacy
        // no-file API falls back to all matching definitions.
        let caller_ids = if let Some(source_file) = source_file {
            match indexes
                .qname_file_to_node
                .get(&(sig.qualified_name.clone(), (*source_file).to_string()))
            {
                Some(ids) if !ids.is_empty() => ids.clone(),
                _ => continue,
            }
        } else {
            match indexes.qname_to_node.get(&sig.qualified_name) {
                Some(ids) if !ids.is_empty() => ids.clone(),
                _ => continue,
            }
        };

        for caller_id in caller_ids {
            let caller_ns = caller_namespace(&sig.qualified_name);

            for call_target in &sig.calls {
                let candidates =
                    ordered_resolution_candidates(call_target, &alias_map, caller_ns.as_deref());

                let targets = resolve_cross_file_call_targets(&candidates, &indexes);

                for target_id in targets {
                    queue_cross_file_call_edge(
                        caller_id,
                        target_id,
                        &mut existing_edges,
                        &mut new_edges,
                    );
                }
                if let Some(target_id) = cross_file_type_target(call_target, &indexes) {
                    queue_cross_file_call_edge(
                        caller_id,
                        target_id,
                        &mut existing_edges,
                        &mut new_edges,
                    );
                }
            }
        }
    }

    if !new_edges.is_empty() {
        tracing::debug!(
            "Cross-file call edge resolution: added {} new edges",
            new_edges.len()
        );
        pdg.add_call_edges(new_edges);
    }
}

fn existing_typed_edges(
    pdg: &ProgramDependenceGraph,
) -> HashSet<(
    crate::graph::pdg::NodeId,
    crate::graph::pdg::NodeId,
    EdgeType,
)> {
    pdg.edge_indices()
        .filter_map(|edge_id| {
            let edge = pdg.get_edge(edge_id)?;
            let (from, to) = pdg.edge_endpoints(edge_id)?;
            Some((from, to, edge.edge_type.clone()))
        })
        .collect()
}

/// Resolve source-level value/state channels after all per-file PDGs merge.
///
/// Per-file extraction already emits local flow edges. This pass connects the
/// same facts to definitions in other files, using exact normalized qualified
/// names rather than a bare-name fallback. The exact-key contract prevents a
/// fact targeting `module.target` from attaching to an unrelated `target` in
/// another file; callers that need broader alias/type inference must add that
/// evidence explicitly rather than widening this bounded resolver.
// ponytail: syntax/name matching keeps indexing bounded; add type/alias analysis
// only when measured flow misses justify its cost.
pub fn resolve_cross_file_flow_edges_for_files(
    pdg: &mut ProgramDependenceGraph,
    all_signatures: &[(String, SignatureInfo)],
) {
    use crate::graph::pdg::{EdgeType, NodeId};

    let mut by_qname: HashMap<String, Vec<NodeId>> = HashMap::new();
    let mut by_file_qname: HashMap<(String, String), Vec<NodeId>> = HashMap::new();

    for nid in pdg.node_indices() {
        let Some(node) = pdg.get_node(nid) else {
            continue;
        };
        if node.node_type == NodeType::External {
            continue;
        }
        let Some(qname) = qualified_name_from_node(node) else {
            continue;
        };
        let normalized = normalize_symbol(qname);
        by_qname.entry(normalized.clone()).or_default().push(nid);
        by_file_qname
            .entry((normalized.clone(), node.file_path.to_string()))
            .or_default()
            .push(nid);
    }

    let mut existing = existing_typed_edges(pdg);

    let mut added = 0usize;
    for (source_file, sig) in all_signatures {
        let normalized_sig = normalize_symbol(&sig.qualified_name);
        let caller_ids = by_file_qname
            .get(&(normalized_sig.clone(), source_file.clone()))
            .cloned()
            .or_else(|| by_qname.get(&normalized_sig).cloned())
            .unwrap_or_default();
        if caller_ids.is_empty() {
            continue;
        }

        for fact in &sig.flow_facts {
            let (edge_type, target_label) = match fact.channel {
                FlowChannel::Argument => (EdgeType::DataDependency, fact.target.as_str()),
                FlowChannel::StateRead | FlowChannel::StateWrite => {
                    (EdgeType::StateTransition, fact.target.as_str())
                }
                FlowChannel::ReturnValue if fact.target != "return" => {
                    (EdgeType::DataDependency, fact.target.as_str())
                }
                _ => continue,
            };
            let targets = resolve_cross_file_flow_targets(target_label, &by_qname);
            for caller_id in &caller_ids {
                for target_id in &targets {
                    if caller_id == target_id
                        || !existing.insert((*caller_id, *target_id, edge_type.clone()))
                    {
                        continue;
                    }
                    let mut metadata = EdgeMetadata::with_variable(fact.source.clone());
                    metadata.channel = Some(flow_channel_name(&fact.channel));
                    metadata.position = fact.position;
                    pdg.add_edge(
                        *caller_id,
                        *target_id,
                        Edge {
                            edge_type: edge_type.clone(),
                            metadata,
                        },
                    );
                    added += 1;
                }
            }
        }
    }

    if added > 0 {
        tracing::debug!("Cross-file flow edge resolution: added {added} edges");
    }
}

fn resolve_cross_file_flow_targets(
    target: &str,
    by_qname: &HashMap<String, Vec<crate::graph::pdg::NodeId>>,
) -> Vec<crate::graph::pdg::NodeId> {
    let normalized = normalize_symbol(target);
    by_qname.get(&normalized).cloned().unwrap_or_default()
}