arbor-graph 2.6.0

Graph schema and relationship tracking for Arbor
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
//! Cross-file symbol resolution.
//!
//! Maps Fully Qualified Names (FQNs) to graph nodes and resolves bare or
//! partially-qualified references against them.
//!
//! # Determinism
//!
//! Every lookup path in this module is order-independent. Candidate lists are
//! sorted by `(file, node index)` before any pick is made, and no decision ever
//! depends on `HashMap` iteration order — Rust seeds `RandomState` per process,
//! so iterating a map to choose a winner would make graph construction vary
//! between runs of the same binary on the same input.
//!
//! # Collisions
//!
//! An FQN can legitimately map to several nodes: `handler` in twelve route
//! files, `new` on forty structs, `process` in both `utils.py` and `helpers.py`.
//! The table keeps every entry and reports ambiguity to the caller rather than
//! silently overwriting, which would orphan the loser and produce a false
//! negative in blast radius.

use crate::graph::NodeId;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// One definition of a symbol.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SymbolEntry {
    pub id: NodeId,
    pub file: PathBuf,
}

/// How a reference was matched, ordered from most to least trustworthy.
///
/// The variant determines the confidence stamped on the resulting edge, so a
/// downstream consumer can tell a certain call from an educated guess.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Resolution {
    /// Exact FQN match with exactly one definition.
    Exact(NodeId),
    /// Matched a symbol defined in the referencing file.
    SameFile(NodeId),
    /// Suffix match with exactly one definition repo-wide.
    UniqueSuffix(NodeId),
    /// Several candidates; exactly one lives in the referencing file's directory.
    SameDir(NodeId),
    /// Several equally-plausible candidates, sorted deterministically.
    Ambiguous(Vec<NodeId>),
    /// No definition in this repository (external or stdlib).
    Unresolved,
}

impl Resolution {
    /// Confidence to stamp on an edge created from this resolution.
    ///
    /// `Ambiguous` is deliberately low rather than zero: the reference does
    /// point at *something* in the repo, we just cannot say which. Consumers
    /// that need certainty should filter on this value.
    pub fn confidence(&self) -> f32 {
        match self {
            Resolution::Exact(_) => 1.0,
            Resolution::SameFile(_) => 0.95,
            Resolution::UniqueSuffix(_) => 0.80,
            Resolution::SameDir(_) => 0.55,
            Resolution::Ambiguous(_) => 0.25,
            Resolution::Unresolved => 0.0,
        }
    }

    /// The single resolved node, if this resolution picked one.
    pub fn node(&self) -> Option<NodeId> {
        match self {
            Resolution::Exact(id)
            | Resolution::SameFile(id)
            | Resolution::UniqueSuffix(id)
            | Resolution::SameDir(id) => Some(*id),
            Resolution::Ambiguous(_) | Resolution::Unresolved => None,
        }
    }

    /// Every candidate this resolution considered plausible.
    pub fn candidates(&self) -> Vec<NodeId> {
        match self {
            Resolution::Ambiguous(ids) => ids.clone(),
            other => other.node().into_iter().collect(),
        }
    }

    pub fn is_resolved(&self) -> bool {
        !matches!(self, Resolution::Unresolved)
    }
}

/// A global symbol table for resolving cross-file references.
#[derive(Debug, Default, Clone)]
pub struct SymbolTable {
    /// FQN → every node defining it.
    by_fqn: HashMap<String, Vec<SymbolEntry>>,

    /// Segment-aligned suffix → FQNs ending with it.
    ///
    /// `pkg.Utils.helper` is indexed under `helper`, `Utils.helper`, and
    /// `pkg.Utils.helper`, so resolving a bare `helper` is an O(1) lookup
    /// instead of a scan over every FQN in the repository.
    by_suffix: HashMap<String, Vec<String>>,

    /// File → FQNs it defines.
    exports_by_file: HashMap<PathBuf, Vec<String>>,
}

impl SymbolTable {
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers a symbol definition.
    ///
    /// Repeated FQNs accumulate instead of overwriting.
    pub fn insert(&mut self, fqn: String, id: NodeId, file: PathBuf) {
        let entry = SymbolEntry {
            id,
            file: file.clone(),
        };

        let entries = self.by_fqn.entry(fqn.clone()).or_default();
        // Guard against the same definition being indexed twice (incremental
        // re-parse of an unchanged file).
        if !entries.contains(&entry) {
            entries.push(entry);
        }

        for suffix in segment_suffixes(&fqn) {
            let fqns = self.by_suffix.entry(suffix.to_string()).or_default();
            if !fqns.iter().any(|f| f == &fqn) {
                fqns.push(fqn.clone());
            }
        }

        let exports = self.exports_by_file.entry(file).or_default();
        if !exports.contains(&fqn) {
            exports.push(fqn);
        }
    }

    /// Resolves an exact FQN, but only when the definition is unambiguous.
    ///
    /// Returns `None` for a colliding FQN so the caller can fall back to
    /// context-aware resolution rather than picking a definition at random.
    pub fn resolve(&self, fqn: &str) -> Option<NodeId> {
        match self.by_fqn.get(fqn) {
            Some(entries) if entries.len() == 1 => Some(entries[0].id),
            _ => None,
        }
    }

    /// All nodes defining this exact FQN, in deterministic order.
    pub fn resolve_all(&self, fqn: &str) -> Vec<NodeId> {
        let Some(entries) = self.by_fqn.get(fqn) else {
            return Vec::new();
        };
        let mut entries: Vec<&SymbolEntry> = entries.iter().collect();
        sort_entries(&mut entries);
        entries.into_iter().map(|e| e.id).collect()
    }

    /// Returns all symbols exported by a file.
    pub fn get_file_exports(&self, file: &PathBuf) -> Option<&Vec<String>> {
        self.exports_by_file.get(file)
    }

    pub fn clear(&mut self) {
        self.by_fqn.clear();
        self.by_suffix.clear();
        self.exports_by_file.clear();
    }

    /// Resolves a reference, preferring locality and reporting ambiguity.
    ///
    /// Order:
    ///   1. Exact FQN, unique → `Exact`
    ///   2. Candidate gathering: exact-FQN entries plus every segment-aligned
    ///      suffix match, deduplicated and sorted
    ///   3. Single candidate → `Exact` / `UniqueSuffix`
    ///   4. Exactly one candidate in the referencing file → `SameFile`
    ///   5. Exactly one candidate in the referencing directory → `SameDir`
    ///   6. Otherwise → `Ambiguous`
    pub fn resolve_ref(&self, name: &str, context_file: &Path) -> Resolution {
        let exact = self.by_fqn.get(name);
        if let Some(entries) = exact {
            if entries.len() == 1 {
                return Resolution::Exact(entries[0].id);
            }
        }
        let had_exact = exact.is_some();

        let mut candidates: Vec<&SymbolEntry> = Vec::new();
        let mut seen: Vec<NodeId> = Vec::new();

        if let Some(entries) = exact {
            for e in entries {
                if !seen.contains(&e.id) {
                    seen.push(e.id);
                    candidates.push(e);
                }
            }
        }

        if let Some(fqns) = self.by_suffix.get(name) {
            // `by_suffix` values are insertion-ordered Vecs, but sort the FQN
            // list anyway so candidate order never depends on parse order.
            let mut fqns: Vec<&String> = fqns.iter().collect();
            fqns.sort();
            for fqn in fqns {
                if let Some(entries) = self.by_fqn.get(fqn) {
                    for e in entries {
                        if !seen.contains(&e.id) {
                            seen.push(e.id);
                            candidates.push(e);
                        }
                    }
                }
            }
        }

        if candidates.is_empty() {
            return Resolution::Unresolved;
        }

        sort_entries(&mut candidates);

        if candidates.len() == 1 {
            return if had_exact {
                Resolution::Exact(candidates[0].id)
            } else {
                Resolution::UniqueSuffix(candidates[0].id)
            };
        }

        let same_file: Vec<&SymbolEntry> = candidates
            .iter()
            .copied()
            .filter(|e| e.file == context_file)
            .collect();
        if same_file.len() == 1 {
            return Resolution::SameFile(same_file[0].id);
        }

        let context_dir = context_file.parent();
        let same_dir: Vec<&SymbolEntry> = candidates
            .iter()
            .copied()
            .filter(|e| e.file.parent() == context_dir)
            .collect();
        if same_dir.len() == 1 {
            return Resolution::SameDir(same_dir[0].id);
        }

        Resolution::Ambiguous(candidates.into_iter().map(|e| e.id).collect())
    }

    /// Back-compatible wrapper returning only a confidently-resolved node.
    pub fn resolve_with_context(&self, name: &str, context_file: &Path) -> Option<NodeId> {
        self.resolve_ref(name, context_file).node()
    }

    /// Number of distinct FQNs registered.
    pub fn len(&self) -> usize {
        self.by_fqn.len()
    }

    pub fn is_empty(&self) -> bool {
        self.by_fqn.is_empty()
    }
}

/// Sorts entries by `(file, node index)` for a total, parse-order-independent order.
fn sort_entries(entries: &mut [&SymbolEntry]) {
    entries.sort_by(|a, b| {
        a.file
            .cmp(&b.file)
            .then_with(|| a.id.index().cmp(&b.id.index()))
    });
}

/// Every segment-aligned suffix of an FQN, including the FQN itself.
///
/// `a.b.c` → `["a.b.c", "b.c", "c"]`; `a::b::c` → `["a::b::c", "b::c", "c"]`.
///
/// Only positions immediately after a `.` or `:` qualify, so `helper` never
/// matches the tail of `my_helper`. A suffix that would itself start with a
/// separator (the middle of Rust's `::`) is skipped.
fn segment_suffixes(fqn: &str) -> Vec<&str> {
    let mut out = vec![fqn];
    let bytes = fqn.as_bytes();

    for (i, c) in fqn.char_indices().skip(1) {
        if c == '.' || c == ':' {
            continue;
        }
        // `i` is a char boundary, so `bytes[i - 1]` is either the whole
        // previous char (ASCII) or a UTF-8 continuation byte (>= 0x80),
        // which can never equal `.` or `:`.
        let prev = bytes[i - 1];
        if prev == b'.' || prev == b':' {
            out.push(&fqn[i..]);
        }
    }

    out
}

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

    fn nid(n: usize) -> NodeId {
        petgraph::graph::NodeIndex::new(n)
    }

    #[test]
    fn insert_resolve() {
        let mut table = SymbolTable::new();
        let path = PathBuf::from("main.rs");
        table.insert("main::foo".to_string(), nid(1), path.clone());

        assert_eq!(table.resolve("main::foo"), Some(nid(1)));
        assert_eq!(table.resolve("main::bar"), None);
        assert_eq!(table.get_file_exports(&path).unwrap(), &vec!["main::foo"]);
    }

    #[test]
    fn exact_match_from_any_context() {
        let mut table = SymbolTable::new();
        table.insert(
            "pkg.utils.helper".to_string(),
            nid(1),
            PathBuf::from("src/utils.rs"),
        );

        let r = table.resolve_ref("pkg.utils.helper", Path::new("other/file.rs"));
        assert_eq!(r, Resolution::Exact(nid(1)));
        assert_eq!(r.confidence(), 1.0);
    }

    #[test]
    fn unique_suffix_match() {
        let mut table = SymbolTable::new();
        table.insert(
            "pkg.utils.helper".to_string(),
            nid(1),
            PathBuf::from("src/utils.rs"),
        );

        let r = table.resolve_ref("helper", Path::new("other/file.rs"));
        assert_eq!(r, Resolution::UniqueSuffix(nid(1)));
    }

    #[test]
    fn multi_segment_suffix_match() {
        let mut table = SymbolTable::new();
        table.insert(
            "pkg.Utils.helper".to_string(),
            nid(1),
            PathBuf::from("src/utils.rs"),
        );

        let r = table.resolve_ref("Utils.helper", Path::new("other/file.rs"));
        assert_eq!(r, Resolution::UniqueSuffix(nid(1)));
    }

    #[test]
    fn suffix_must_be_segment_aligned() {
        let mut table = SymbolTable::new();
        table.insert(
            "pkg.my_helper".to_string(),
            nid(1),
            PathBuf::from("src/utils.rs"),
        );

        // `helper` is a character-suffix of `my_helper` but not a segment.
        assert_eq!(
            table.resolve_ref("helper", Path::new("other/file.rs")),
            Resolution::Unresolved
        );
    }

    #[test]
    fn ambiguous_reports_all_candidates() {
        let mut table = SymbolTable::new();
        table.insert(
            "pkg.a.helper".to_string(),
            nid(1),
            PathBuf::from("src/a/mod.rs"),
        );
        table.insert(
            "pkg.b.helper".to_string(),
            nid(2),
            PathBuf::from("src/b/mod.rs"),
        );

        let r = table.resolve_ref("helper", Path::new("src/c/caller.rs"));
        assert_eq!(r, Resolution::Ambiguous(vec![nid(1), nid(2)]));
        assert!(r.node().is_none());
        assert_eq!(r.candidates().len(), 2);
    }

    #[test]
    fn locality_preference_same_dir() {
        let mut table = SymbolTable::new();
        table.insert(
            "pkg.a.helper".to_string(),
            nid(1),
            PathBuf::from("src/a/mod.rs"),
        );
        table.insert(
            "pkg.b.helper".to_string(),
            nid(2),
            PathBuf::from("src/b/mod.rs"),
        );

        assert_eq!(
            table.resolve_ref("helper", Path::new("src/a/caller.rs")),
            Resolution::SameDir(nid(1))
        );
        assert_eq!(
            table.resolve_ref("helper", Path::new("src/b/caller.rs")),
            Resolution::SameDir(nid(2))
        );
    }

    #[test]
    fn same_file_beats_same_dir() {
        let mut table = SymbolTable::new();
        table.insert("A.run".to_string(), nid(1), PathBuf::from("src/a.rs"));
        table.insert("B.run".to_string(), nid(2), PathBuf::from("src/b.rs"));

        // Both live in `src/`, so same-dir cannot disambiguate — same-file must.
        assert_eq!(
            table.resolve_ref("run", Path::new("src/a.rs")),
            Resolution::SameFile(nid(1))
        );
    }

    #[test]
    fn colliding_fqn_keeps_both_definitions() {
        let mut table = SymbolTable::new();
        table.insert("process".to_string(), nid(1), PathBuf::from("src/utils.py"));
        table.insert(
            "process".to_string(),
            nid(2),
            PathBuf::from("src/helpers.py"),
        );

        // The old table overwrote here, orphaning one node entirely.
        assert_eq!(table.resolve_all("process"), vec![nid(2), nid(1)]);
        assert_eq!(
            table.resolve("process"),
            None,
            "collision is not unambiguous"
        );

        let r = table.resolve_ref("process", Path::new("src/other.py"));
        assert_eq!(r, Resolution::Ambiguous(vec![nid(2), nid(1)]));
    }

    #[test]
    fn duplicate_insert_is_idempotent() {
        let mut table = SymbolTable::new();
        table.insert("a.b".to_string(), nid(1), PathBuf::from("x.rs"));
        table.insert("a.b".to_string(), nid(1), PathBuf::from("x.rs"));

        assert_eq!(table.resolve_all("a.b"), vec![nid(1)]);
        assert_eq!(
            table
                .get_file_exports(&PathBuf::from("x.rs"))
                .unwrap()
                .len(),
            1
        );
    }

    #[test]
    fn resolution_confidence_is_ordered() {
        assert!(Resolution::Exact(nid(0)).confidence() > Resolution::SameFile(nid(0)).confidence());
        assert!(
            Resolution::SameFile(nid(0)).confidence()
                > Resolution::UniqueSuffix(nid(0)).confidence()
        );
        assert!(
            Resolution::UniqueSuffix(nid(0)).confidence()
                > Resolution::SameDir(nid(0)).confidence()
        );
        assert!(
            Resolution::SameDir(nid(0)).confidence() > Resolution::Ambiguous(vec![]).confidence()
        );
        assert_eq!(Resolution::Unresolved.confidence(), 0.0);
    }

    #[test]
    fn resolution_is_order_independent() {
        // Same definitions inserted in opposite orders must resolve identically.
        let mut a = SymbolTable::new();
        a.insert("p.x.run".into(), nid(1), PathBuf::from("src/x/mod.rs"));
        a.insert("p.y.run".into(), nid(2), PathBuf::from("src/y/mod.rs"));
        a.insert("p.z.run".into(), nid(3), PathBuf::from("src/z/mod.rs"));

        let mut b = SymbolTable::new();
        b.insert("p.z.run".into(), nid(3), PathBuf::from("src/z/mod.rs"));
        b.insert("p.y.run".into(), nid(2), PathBuf::from("src/y/mod.rs"));
        b.insert("p.x.run".into(), nid(1), PathBuf::from("src/x/mod.rs"));

        let ctx = Path::new("src/other/caller.rs");
        assert_eq!(a.resolve_ref("run", ctx), b.resolve_ref("run", ctx));
    }

    #[test]
    fn segment_suffixes_shapes() {
        assert_eq!(segment_suffixes("a.b.c"), vec!["a.b.c", "b.c", "c"]);
        assert_eq!(segment_suffixes("a::b::c"), vec!["a::b::c", "b::c", "c"]);
        assert_eq!(segment_suffixes("solo"), vec!["solo"]);
    }

    #[test]
    fn unicode_fqn_does_not_panic() {
        let mut table = SymbolTable::new();
        table.insert("módulo.función".to_string(), nid(1), PathBuf::from("a.py"));
        assert_eq!(
            table.resolve_ref("función", Path::new("b.py")),
            Resolution::UniqueSuffix(nid(1))
        );
    }
}