Skip to main content

lean_ctx/core/
graph_provider.rs

1use std::path::Path;
2use std::sync::atomic::{AtomicBool, Ordering};
3
4use super::graph_index::{self, ProjectIndex};
5use super::property_graph::served::GraphGate;
6use super::property_graph::{CodeGraph, GraphSource};
7
8static GRAPH_BUILD_TRIGGERED: AtomicBool = AtomicBool::new(false);
9
10#[derive(Debug, Clone)]
11pub struct SymbolInfo {
12    pub name: String,
13    pub file: String,
14    pub kind: String,
15    pub start_line: usize,
16    pub end_line: usize,
17    pub is_exported: bool,
18}
19
20/// Normalize a property-graph edge-kind string back to the graph_index spelling
21/// when materializing a [`ProjectIndex`]. The mirror maps graph_index `import` →
22/// `EdgeKind::Imports`, which serializes as the plural `imports`; the legacy
23/// dependency consumers (`graph_index` BFS, the facade's index-backed
24/// `dependencies`/`dependents`, `ctx_graph`) filter on the singular `import`, so
25/// reverse exactly that one rename. All other kinds already share their spelling
26/// across both stores (#696 C1).
27fn index_edge_kind(pg_kind: &str) -> String {
28    match pg_kind {
29        "imports" => "import".to_string(),
30        other => other.to_string(),
31    }
32}
33
34/// Absolute distance from a symbol's `start` line to a handle's hint `line`.
35/// A missing hint contributes zero distance, so handles without an `@Lline`
36/// suffix neither help nor hurt the tiebreak (all candidates rank equal here).
37fn line_distance(start: usize, target: Option<usize>) -> usize {
38    match target {
39        Some(t) => start.abs_diff(t),
40        None => 0,
41    }
42}
43
44/// Convert a property-graph symbol [`Node`] into a backend-agnostic
45/// [`SymbolInfo`], recovering the precise source `kind` and export flag from the
46/// node metadata (the `Node` itself only carries a coarse `NodeKind`). Single
47/// source of truth for the three facade methods that surface PG symbols, so a
48/// materialized `ProjectIndex` is lossless (#696 C1).
49fn symbol_info_from_node(n: super::property_graph::Node) -> SymbolInfo {
50    let (meta_kind, meta_exported) =
51        super::property_graph::parse_symbol_metadata(n.metadata.as_deref());
52    SymbolInfo {
53        kind: meta_kind.unwrap_or_else(|| n.kind.as_str().to_string()),
54        is_exported: meta_exported.unwrap_or(true),
55        name: n.name,
56        file: n.file_path,
57        start_line: n.line_start.unwrap_or(0),
58        end_line: n.line_end.unwrap_or(0),
59    }
60}
61
62#[derive(Debug, Clone)]
63pub struct EdgeInfo {
64    pub from: String,
65    pub to: String,
66    pub kind: String,
67    pub weight: f64,
68}
69
70#[derive(Debug, Clone)]
71pub struct FileInfo {
72    pub path: String,
73    pub hash: String,
74    pub language: String,
75    pub line_count: usize,
76    pub token_count: usize,
77    pub exports: Vec<String>,
78    pub summary: String,
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum GraphProviderSource {
83    PropertyGraph,
84    GraphIndex,
85}
86
87// One provider lives for one call on the stack; boxing the graph would add an
88// allocation per open and touch every construction site for no gain.
89#[allow(clippy::large_enum_variant)]
90pub enum GraphProvider {
91    PropertyGraph(CodeGraph),
92    GraphIndex(ProjectIndex),
93}
94
95pub struct OpenGraphProvider {
96    pub source: GraphProviderSource,
97    pub provider: GraphProvider,
98}
99
100/// The file an edge end names: a file path, or the file of a
101/// `path::symbol` key (a file path never contains `::`).
102fn edge_file(end: &str) -> &str {
103    end.split_once("::").map_or(end, |(file, _)| file)
104}
105
106/// The tool-facing surface. Every file, symbol and edge it returns has passed
107/// the serve-time source check (`property_graph::served`, G04): the property
108/// graph carries its gate for the request that opened it, the index backend
109/// gets one per call. The `*_unchecked` accessors below are the raw store
110/// reads and stay private to this facade.
111impl GraphProvider {
112    fn index_gate(&self) -> Option<GraphGate> {
113        match self {
114            GraphProvider::PropertyGraph(_) => None,
115            GraphProvider::GraphIndex(i) => Some(GraphGate::for_root(&i.project_root)),
116        }
117    }
118
119    fn file_served(&self, gate: Option<&GraphGate>, path: &str) -> bool {
120        match self {
121            GraphProvider::PropertyGraph(g) => g.serves_file(path),
122            GraphProvider::GraphIndex(_) => gate.is_none_or(|gate| gate.file(path)),
123        }
124    }
125
126    fn symbol_served(&self, gate: Option<&GraphGate>, path: &str, name: &str) -> bool {
127        match self {
128            GraphProvider::PropertyGraph(g) => g.serves_symbol(path, name),
129            GraphProvider::GraphIndex(_) => gate.is_none_or(|gate| gate.symbol(path, name)),
130        }
131    }
132
133    /// `paths` found for `anchor`: nothing when the anchor itself may not be
134    /// served, otherwise the paths that may.
135    fn served_from(&self, anchor: Option<&str>, paths: Vec<String>) -> Vec<String> {
136        let gate = self.index_gate();
137        if anchor.is_some_and(|anchor| !self.file_served(gate.as_ref(), anchor)) {
138            return Vec::new();
139        }
140        paths
141            .into_iter()
142            .filter(|path| self.file_served(gate.as_ref(), path))
143            .collect()
144    }
145
146    fn served_symbols(&self, symbols: Vec<SymbolInfo>) -> Vec<SymbolInfo> {
147        let gate = self.index_gate();
148        symbols
149            .into_iter()
150            .filter(|symbol| self.symbol_served(gate.as_ref(), &symbol.file, &symbol.name))
151            .collect()
152    }
153
154    pub fn dependencies(&self, file_path: &str) -> Vec<String> {
155        self.served_from(Some(file_path), self.dependencies_unchecked(file_path))
156    }
157
158    pub fn dependents(&self, file_path: &str) -> Vec<String> {
159        self.served_from(Some(file_path), self.dependents_unchecked(file_path))
160    }
161
162    /// Files reached from `file_path` within `depth`. The walk itself runs
163    /// over served files only: nothing is reached through a file that is not
164    /// served now (`property_graph::queries::impact_analysis`,
165    /// `ProjectIndex::get_related_served`).
166    pub fn related(&self, file_path: &str, depth: usize) -> Vec<String> {
167        let gate = self.index_gate();
168        let served = |path: &str| self.file_served(gate.as_ref(), path);
169        if !served(file_path) {
170            return Vec::new();
171        }
172        match self {
173            GraphProvider::PropertyGraph(g) => g
174                .impact_analysis(file_path, depth)
175                .map(|r| r.affected_files)
176                .unwrap_or_default(),
177            GraphProvider::GraphIndex(i) => i.get_related_served(file_path, depth, &served),
178        }
179    }
180
181    /// The file graph a computation over the whole graph may use — community
182    /// detection, a ranking: the files served now and the edges between two
183    /// of them, decided by one gate (`GraphGate::ranking_files`; under
184    /// protection a graph too large to decide in full yields nothing).
185    pub(crate) fn ranking_graph(&self) -> (Vec<String>, Vec<EdgeInfo>) {
186        let mut files = self.file_paths_unchecked();
187        files.sort();
188        files.dedup();
189        let edges = self.edges_unchecked();
190        let pairs: Vec<(String, String)> = edges
191            .iter()
192            .map(|e| (edge_file(&e.from).to_owned(), edge_file(&e.to).to_owned()))
193            .collect();
194        let served = match self {
195            GraphProvider::PropertyGraph(g) => g.rankable_files(files.clone(), &pairs, &[]),
196            GraphProvider::GraphIndex(i) => {
197                GraphGate::for_root(&i.project_root).ranking_files(files.clone(), &pairs, &[])
198            }
199        };
200        files.retain(|file| served.contains(file));
201        let edges = edges
202            .into_iter()
203            .filter(|e| served.contains(edge_file(&e.from)) && served.contains(edge_file(&e.to)))
204            .collect();
205        (files, edges)
206    }
207
208    pub fn file_paths(&self) -> Vec<String> {
209        self.served_from(None, self.file_paths_unchecked())
210    }
211
212    pub fn find_symbols(
213        &self,
214        name: &str,
215        file_filter: Option<&str>,
216        kind_filter: Option<&str>,
217    ) -> Vec<SymbolInfo> {
218        self.served_symbols(self.find_symbols_unchecked(name, file_filter, kind_filter))
219    }
220
221    /// Every symbol with its file + line span (unfiltered by name).
222    pub fn all_symbols(&self) -> Vec<SymbolInfo> {
223        self.served_symbols(self.all_symbols_unchecked())
224    }
225
226    pub fn get_symbol(&self, key: &str) -> Option<SymbolInfo> {
227        let gate = self.index_gate();
228        self.get_symbol_unchecked(key)
229            .filter(|symbol| self.symbol_served(gate.as_ref(), &symbol.file, &symbol.name))
230    }
231
232    pub fn edges(&self) -> Vec<EdgeInfo> {
233        let gate = self.index_gate();
234        let served = |end: &str| self.file_served(gate.as_ref(), edge_file(end));
235        self.edges_unchecked()
236            .into_iter()
237            .filter(|edge| served(edge.from.as_str()) && served(edge.to.as_str()))
238            .collect()
239    }
240
241    pub fn get_file_entry(&self, path: &str) -> Option<FileInfo> {
242        let gate = self.index_gate();
243        self.file_served(gate.as_ref(), path)
244            .then(|| self.get_file_entry_unchecked(path))
245            .flatten()
246    }
247
248    /// Scored related files using multi-edge weights (unscored deps and
249    /// dependents for the index backend). Only served neighbours are ranked,
250    /// and `limit` is applied after the check: a file that is not served now
251    /// takes no slot from one that is.
252    pub fn related_files_scored(&self, file_path: &str, limit: usize) -> Vec<(String, f64)> {
253        let gate = self.index_gate();
254        self.related_files_scored_with(gate.as_ref(), file_path, limit)
255    }
256
257    /// [`Self::related_files_scored`] under the caller's request gate: the
258    /// opened property graph carries its own; the index backend has none and
259    /// is decided by the gate the caller creates once per request, so each
260    /// file is decided once and one bound is spent.
261    pub(crate) fn related_files_scored_with(
262        &self,
263        gate: Option<&GraphGate>,
264        file_path: &str,
265        limit: usize,
266    ) -> Vec<(String, f64)> {
267        match self {
268            GraphProvider::PropertyGraph(g) => {
269                g.related_files(file_path, limit).unwrap_or_default()
270            }
271            GraphProvider::GraphIndex(_) => {
272                let served = |path: &str| self.file_served(gate, path);
273                let mut result: Vec<(String, f64)> = Vec::new();
274                if !served(file_path) {
275                    return result;
276                }
277                let neighbours = self
278                    .dependencies_unchecked(file_path)
279                    .into_iter()
280                    .map(|dep| (dep, 1.0))
281                    .chain(
282                        self.dependents_unchecked(file_path)
283                            .into_iter()
284                            .map(|dep| (dep, 0.5)),
285                    );
286                for (dep, score) in neighbours {
287                    if result.len() >= limit {
288                        break;
289                    }
290                    if !result.iter().any(|(path, _)| *path == dep) && served(&dep) {
291                        result.push((dep, score));
292                    }
293                }
294                result
295            }
296        }
297    }
298
299    pub fn node_count(&self) -> Option<usize> {
300        match self {
301            GraphProvider::PropertyGraph(g) => g.node_count().ok(),
302            GraphProvider::GraphIndex(i) => Some(i.file_count()),
303        }
304    }
305
306    pub fn edge_count(&self) -> Option<usize> {
307        match self {
308            GraphProvider::PropertyGraph(g) => g.edge_count().ok(),
309            GraphProvider::GraphIndex(i) => Some(i.edge_count()),
310        }
311    }
312
313    fn dependencies_unchecked(&self, file_path: &str) -> Vec<String> {
314        match self {
315            GraphProvider::PropertyGraph(g) => g.dependencies(file_path).unwrap_or_default(),
316            GraphProvider::GraphIndex(i) => i
317                .edges
318                .iter()
319                .filter(|e| e.kind == "import" && e.from == file_path)
320                .map(|e| e.to.clone())
321                .collect(),
322        }
323    }
324
325    fn dependents_unchecked(&self, file_path: &str) -> Vec<String> {
326        match self {
327            GraphProvider::PropertyGraph(g) => g.dependents(file_path).unwrap_or_default(),
328            GraphProvider::GraphIndex(i) => i
329                .edges
330                .iter()
331                .filter(|e| e.kind == "import" && e.to == file_path)
332                .map(|e| e.from.clone())
333                .collect(),
334        }
335    }
336
337    fn file_paths_unchecked(&self) -> Vec<String> {
338        match self {
339            GraphProvider::PropertyGraph(g) => g.file_catalog_paths().unwrap_or_default(),
340            GraphProvider::GraphIndex(i) => {
341                let mut paths: Vec<String> = i.files.keys().cloned().collect();
342                paths.sort();
343                paths
344            }
345        }
346    }
347
348    pub fn file_count(&self) -> usize {
349        match self {
350            GraphProvider::PropertyGraph(g) => g.file_catalog_count().unwrap_or(0),
351            GraphProvider::GraphIndex(i) => i.files.len(),
352        }
353    }
354
355    pub fn symbol_count(&self) -> usize {
356        match self {
357            GraphProvider::PropertyGraph(g) => g.symbol_count().unwrap_or(0),
358            GraphProvider::GraphIndex(i) => i.symbols.len(),
359        }
360    }
361
362    fn find_symbols_unchecked(
363        &self,
364        name: &str,
365        file_filter: Option<&str>,
366        kind_filter: Option<&str>,
367    ) -> Vec<SymbolInfo> {
368        match self {
369            GraphProvider::PropertyGraph(g) => g
370                // Kind is filtered AFTER mapping, not in SQL: the PG `nodes.kind`
371                // column is the coarse `NodeKind` (every code symbol is `"symbol"`),
372                // while the precise source kind (`struct`, `fn`, …) lives in metadata
373                // and is recovered by `symbol_info_from_node`. Passing `kind_filter`
374                // to the SQL query matched it against `"symbol"` and dropped every
375                // hit — an exported struct queried with kind="struct" came back empty
376                // (#889). ponytail: the SQL LIMIT 100 caps candidates pre-kind-filter,
377                // fine until a name substring has >100 cross-kind matches.
378                .find_symbols(name, file_filter, None)
379                .unwrap_or_default()
380                .into_iter()
381                .map(symbol_info_from_node)
382                .filter(|s| kind_filter.is_none_or(|k| s.kind == k))
383                .collect(),
384            GraphProvider::GraphIndex(i) => {
385                let name_lower = name.to_lowercase();
386                // Same order and cap as the property graph's SQL (path, line):
387                // a cap over hash order would pick a different 100 per process.
388                let mut hits: Vec<_> = i
389                    .symbols
390                    .values()
391                    .filter(|s| s.name.to_lowercase().contains(&name_lower))
392                    .filter(|s| file_filter.is_none_or(|f| s.file.contains(f)))
393                    .filter(|s| kind_filter.is_none_or(|k| s.kind == k))
394                    .collect();
395                hits.sort_by(|a, b| {
396                    (a.file.as_str(), a.start_line, a.name.as_str()).cmp(&(
397                        b.file.as_str(),
398                        b.start_line,
399                        b.name.as_str(),
400                    ))
401                });
402                hits.into_iter()
403                    .take(100)
404                    .map(|s| SymbolInfo {
405                        name: s.name.clone(),
406                        file: s.file.clone(),
407                        kind: s.kind.clone(),
408                        start_line: s.start_line,
409                        end_line: s.end_line,
410                        is_exported: s.is_exported,
411                    })
412                    .collect()
413            }
414        }
415    }
416
417    /// Every symbol with its file + line span (unfiltered). Backend-agnostic
418    /// equivalent of iterating `ProjectIndex::symbols` — used by the call-graph
419    /// builder to attribute call sites to their enclosing symbol.
420    fn all_symbols_unchecked(&self) -> Vec<SymbolInfo> {
421        match self {
422            GraphProvider::PropertyGraph(g) => g
423                .all_symbols()
424                .unwrap_or_default()
425                .into_iter()
426                .map(symbol_info_from_node)
427                .collect(),
428            GraphProvider::GraphIndex(i) => i
429                .symbols
430                .values()
431                .map(|s| SymbolInfo {
432                    name: s.name.clone(),
433                    file: s.file.clone(),
434                    kind: s.kind.clone(),
435                    start_line: s.start_line,
436                    end_line: s.end_line,
437                    is_exported: s.is_exported,
438                })
439                .collect(),
440        }
441    }
442
443    fn get_symbol_unchecked(&self, key: &str) -> Option<SymbolInfo> {
444        match self {
445            GraphProvider::PropertyGraph(g) => {
446                // Keys are `rel_path::sym_name` (graph_index, see `mod.rs`). A
447                // file path never contains `::`, but a symbol name does for trait
448                // impls (`std::fmt::Display for T`). Split on the FIRST `::` so
449                // those names round-trip — `rsplitn` mangled them (#682.3).
450                let parts: Vec<&str> = key.splitn(2, "::").collect();
451                if parts.len() != 2 {
452                    return None;
453                }
454                let (file_path, sym_name) = (parts[0], parts[1]);
455                g.get_node_by_symbol(sym_name, file_path)
456                    .ok()
457                    .flatten()
458                    .map(symbol_info_from_node)
459            }
460            GraphProvider::GraphIndex(i) => i.get_symbol(key).map(|s| SymbolInfo {
461                name: s.name.clone(),
462                file: s.file.clone(),
463                kind: s.kind.clone(),
464                start_line: s.start_line,
465                end_line: s.end_line,
466                is_exported: s.is_exported,
467            }),
468        }
469    }
470
471    /// Resolve a stable `SymbolHandle` to
472    /// its current [`SymbolInfo`], robust to line drift. Resolution order:
473    ///
474    /// 1. Exact `(path, name)` — the unique `{file}::{name}` index key, so the
475    ///    common case is a single `O(1)` lookup that ignores the `@Lline` hint
476    ///    entirely (the symbol may have moved since the handle was emitted).
477    /// 2. Otherwise, same-file candidates whose name matches exactly or by its
478    ///    unqualified tail (`Config::load` ↔ `load`), ranked by exact-name
479    ///    first, then nearest line to the handle hint, then lowest line, then
480    ///    name — a total, deterministic order (#498).
481    ///
482    /// Returns `None` only when no symbol in that file plausibly matches. The
483    /// `@Lline` is never a hard requirement, so this is strictly more robust
484    /// than a brittle line-only reference.
485    pub fn find_symbol_by_handle(
486        &self,
487        handle: &crate::core::handle::SymbolHandle,
488    ) -> Option<SymbolInfo> {
489        let key = format!("{}::{}", handle.path, handle.name);
490        if let Some(sym) = self.get_symbol(&key) {
491            return Some(sym);
492        }
493
494        let tail = handle
495            .name
496            .rsplit("::")
497            .next()
498            .unwrap_or(handle.name.as_str());
499        let mut candidates: Vec<SymbolInfo> = self
500            .all_symbols()
501            .into_iter()
502            .filter(|s| s.file == handle.path)
503            .filter(|s| s.name == handle.name || s.name.rsplit("::").next() == Some(tail))
504            .collect();
505        if candidates.is_empty() {
506            return None;
507        }
508        candidates.sort_by(|a, b| {
509            let exact_a = u8::from(a.name != handle.name);
510            let exact_b = u8::from(b.name != handle.name);
511            exact_a
512                .cmp(&exact_b)
513                .then_with(|| {
514                    line_distance(a.start_line, handle.line)
515                        .cmp(&line_distance(b.start_line, handle.line))
516                })
517                .then_with(|| a.start_line.cmp(&b.start_line))
518                .then_with(|| a.name.cmp(&b.name))
519        });
520        candidates.into_iter().next()
521    }
522
523    fn edges_unchecked(&self) -> Vec<EdgeInfo> {
524        match self {
525            // Normalize the raw property-graph edge-kind vocabulary back to the
526            // graph_index vocabulary every consumer speaks (notably `imports` →
527            // `import`, queried by impact/overview via `edges_by_kind("import")`).
528            // Without this the facade leaks `EdgeKind::as_str()` plurals and PG
529            // vs legacy backends would answer the same query differently (#696).
530            GraphProvider::PropertyGraph(g) => g
531                .all_edges_flat()
532                .unwrap_or_default()
533                .into_iter()
534                .map(|(from, to, kind, weight)| EdgeInfo {
535                    from,
536                    to,
537                    kind: index_edge_kind(&kind),
538                    weight,
539                })
540                .collect(),
541            GraphProvider::GraphIndex(i) => i
542                .edges
543                .iter()
544                .map(|e| EdgeInfo {
545                    from: e.from.clone(),
546                    to: e.to.clone(),
547                    kind: e.kind.clone(),
548                    weight: e.weight as f64,
549                })
550                .collect(),
551        }
552    }
553
554    pub fn edges_by_kind(&self, kind: &str) -> Vec<EdgeInfo> {
555        self.edges()
556            .into_iter()
557            .filter(|e| e.kind == kind)
558            .collect()
559    }
560
561    /// Every catalogued file as [`FileInfo`]. Backend-agnostic equivalent of
562    /// iterating `ProjectIndex::files` — used by stats/bootstrap consumers that
563    /// need per-file language + token counts, not just paths.
564    pub fn file_entries(&self) -> Vec<FileInfo> {
565        match self {
566            GraphProvider::PropertyGraph(_) => self
567                .file_paths()
568                .into_iter()
569                .filter_map(|p| self.get_file_entry_unchecked(&p))
570                .collect(),
571            GraphProvider::GraphIndex(i) => {
572                let gate = self.index_gate();
573                i.files
574                    .values()
575                    .filter(|e| self.file_served(gate.as_ref(), &e.path))
576                    .map(|e| FileInfo {
577                        path: e.path.clone(),
578                        hash: e.hash.clone(),
579                        language: e.language.clone(),
580                        line_count: e.line_count,
581                        token_count: e.token_count,
582                        exports: e.exports.clone(),
583                        summary: e.summary.clone(),
584                    })
585                    .collect()
586            }
587        }
588    }
589
590    fn get_file_entry_unchecked(&self, path: &str) -> Option<FileInfo> {
591        match self {
592            GraphProvider::PropertyGraph(g) => {
593                g.get_file_catalog(path).ok().flatten().map(|e| FileInfo {
594                    path: e.path,
595                    hash: e.hash,
596                    language: e.language,
597                    line_count: e.line_count,
598                    token_count: e.token_count,
599                    exports: e.exports,
600                    summary: e.summary,
601                })
602            }
603            GraphProvider::GraphIndex(i) => i.files.get(path).map(|e| FileInfo {
604                path: e.path.clone(),
605                hash: e.hash.clone(),
606                language: e.language.clone(),
607                line_count: e.line_count,
608                token_count: e.token_count,
609                exports: e.exports.clone(),
610                summary: e.summary.clone(),
611            }),
612        }
613    }
614
615    pub fn last_scan(&self) -> String {
616        match self {
617            GraphProvider::PropertyGraph(_) => String::new(),
618            GraphProvider::GraphIndex(i) => i.last_scan.clone(),
619        }
620    }
621
622    /// Reconstruct a full [`ProjectIndex`] from this provider — the inverse of
623    /// the graph_index→PG mirror
624    /// ([`populate_from_project_index`](super::property_graph::populate_from_project_index)).
625    /// Lets the
626    /// remaining legacy `ProjectIndex` consumers be sourced from the
627    /// PropertyGraph (parity-proven lossless, #682.3) so the redundant JSON
628    /// store can be retired (#696 phase C). For the GraphIndex backend it clones
629    /// the index it already holds.
630    pub fn materialize_project_index(&self, project_root: &str) -> ProjectIndex {
631        if let GraphProvider::GraphIndex(i) = self {
632            // The index it already holds, without what may not be served now.
633            let gate = self.index_gate();
634            let served = |path: &str| self.file_served(gate.as_ref(), path);
635            let mut index = i.clone();
636            index.files.retain(|path, _| served(path.as_str()));
637            index
638                .symbols
639                .retain(|_, symbol| self.symbol_served(gate.as_ref(), &symbol.file, &symbol.name));
640            index
641                .edges
642                .retain(|edge| served(edge_file(&edge.from)) && served(edge_file(&edge.to)));
643            index.rebuild_interner();
644            return index;
645        }
646        let mut idx = ProjectIndex::new(project_root);
647        // Stamp `last_scan` from the graph's build time (graph.meta.json) so the
648        // TTL staleness check reflects the real build age, not this
649        // materialization moment (#696 C4). Content-based staleness still keys
650        // off the meta file's mtime independently.
651        if let Some(meta) = super::property_graph::load_meta(project_root)
652            && let Ok(dt) = lean_ctx_time::OffsetDateTime::parse_from_rfc3339(&meta.built_at)
653        {
654            idx.last_scan = dt.to_local().format("%Y-%m-%d %H:%M:%S").to_string();
655        }
656        for f in self.file_entries() {
657            idx.files.insert(
658                f.path.clone(),
659                graph_index::FileEntry {
660                    path: f.path,
661                    hash: f.hash,
662                    language: f.language,
663                    line_count: f.line_count,
664                    token_count: f.token_count,
665                    exports: f.exports,
666                    summary: f.summary,
667                },
668            );
669        }
670        for s in self.all_symbols() {
671            let key = format!("{}::{}", s.file, s.name);
672            idx.symbols.insert(
673                key,
674                graph_index::SymbolEntry {
675                    file: s.file,
676                    name: s.name,
677                    kind: s.kind,
678                    start_line: s.start_line,
679                    end_line: s.end_line,
680                    is_exported: s.is_exported,
681                },
682            );
683        }
684        for e in self.edges() {
685            // `edges()` already normalizes PG kinds to the graph_index
686            // vocabulary, so `e.kind` is ready to store verbatim.
687            idx.edges.push(graph_index::IndexEdge {
688                from: e.from,
689                to: e.to,
690                kind: e.kind,
691                weight: e.weight as f32,
692            });
693        }
694        idx.rebuild_interner();
695        idx
696    }
697
698    pub fn index_dir(project_root: &str) -> Option<std::path::PathBuf> {
699        graph_index::ProjectIndex::index_dir(project_root)
700    }
701}
702
703/// Open whichever graph is already populated on disk, **without** triggering any
704/// build, plus a flag telling the caller the property graph still wants a
705/// (re)build. Prefers a fully-populated PropertyGraph and falls back to the
706/// in-memory graph_index extractor while the PG is not yet populated (first run
707/// or just after a rebuild), flagging `needs_build` so the caller can warm the
708/// PG for the next call (#696 phase D: the `legacy` backend escape hatch was
709/// retired once PG-only persistence proved lossless in #682.3).
710fn open_existing(project_root: &str) -> (Option<OpenGraphProvider>, bool) {
711    let t0 = std::time::Instant::now();
712
713    let mut pg_provider = None;
714    let mut pg_populated = false;
715    // A graph built by an older engine may hold edges the current engine no
716    // longer derives (e.g. guessed `calls`, engine 6): never serve it — fall
717    // back to the index extractor and let the flagged rebuild replace it.
718    let pg_current = !crate::core::property_graph::engine_outdated(project_root);
719    if pg_current && let Ok(pg) = CodeGraph::open(project_root) {
720        let nodes = pg.node_count().unwrap_or(0);
721        let edges = pg.edge_count().unwrap_or(0);
722        let file_cat = pg.file_catalog_count().unwrap_or(0);
723        pg_populated = nodes > 0 && edges > 0 && file_cat > 0;
724        if pg_populated {
725            log_source_selection(GraphProviderSource::PropertyGraph, nodes, edges, t0);
726            return (
727                Some(OpenGraphProvider {
728                    source: GraphProviderSource::PropertyGraph,
729                    provider: GraphProvider::PropertyGraph(pg),
730                }),
731                false,
732            );
733        }
734        if nodes > 0 && file_cat > 0 {
735            pg_provider = Some(pg);
736        }
737    }
738
739    // PG is not fully populated: a (re)build would help the next call.
740    let needs_build = !pg_populated;
741
742    if let Some(idx) = super::index_orchestrator::try_load_graph_index(project_root) {
743        let files = idx.files.len();
744        let edges = idx.edges.len();
745        if !idx.edges.is_empty() || !idx.files.is_empty() {
746            log_source_selection(GraphProviderSource::GraphIndex, files, edges, t0);
747            return (
748                Some(OpenGraphProvider {
749                    source: GraphProviderSource::GraphIndex,
750                    provider: GraphProvider::GraphIndex(idx),
751                }),
752                needs_build,
753            );
754        }
755    }
756
757    if let Some(pg) = pg_provider {
758        let nodes = pg.node_count().unwrap_or(0);
759        log_source_selection(GraphProviderSource::PropertyGraph, nodes, 0, t0);
760        return (
761            Some(OpenGraphProvider {
762                source: GraphProviderSource::PropertyGraph,
763                provider: GraphProvider::PropertyGraph(pg),
764            }),
765            needs_build,
766        );
767    }
768
769    (None, needs_build)
770}
771
772/// Open an already-built graph, kicking off a one-shot background build when the
773/// property graph is not fully populated so the *next* call is fast. Returns
774/// `None` on this call when nothing is ready yet. Best-effort callers
775/// (dashboards, context gate, stats, `ctx_graph`) use this; callers that need a
776/// graph *right now* use [`open_or_build`], which builds synchronously instead.
777pub fn open_best_effort(project_root: &str) -> Option<OpenGraphProvider> {
778    let (existing, needs_build) = open_existing(project_root);
779    if needs_build {
780        trigger_lazy_graph_build(project_root);
781    }
782    existing
783}
784
785/// Hot-path open for optional enrichment (post-read hints, silent preload):
786/// only a fully populated property graph, opened from SQLite. Never parses the
787/// JSON index and never builds synchronously — when the graph is not ready it
788/// schedules the background build and returns `None`, so the caller simply
789/// skips its optional work. A read must not wait seconds (or a 30 s build) for
790/// a hint it may not even show.
791pub fn open_ready(project_root: &str) -> Option<OpenGraphProvider> {
792    let ready = if crate::core::property_graph::engine_outdated(project_root) {
793        None
794    } else {
795        // Nodes and catalog, not edges: a built graph of a project without
796        // imports (a single file) legitimately has none.
797        CodeGraph::open(project_root).ok().filter(|pg| {
798            pg.node_count().unwrap_or(0) > 0 && pg.file_catalog_count().unwrap_or(0) > 0
799        })
800    };
801    if let Some(pg) = ready {
802        Some(OpenGraphProvider {
803            source: GraphProviderSource::PropertyGraph,
804            provider: GraphProvider::PropertyGraph(pg),
805        })
806    } else {
807        trigger_lazy_graph_build(project_root);
808        None
809    }
810}
811
812fn log_source_selection(
813    source: GraphProviderSource,
814    nodes: usize,
815    edges: usize,
816    start: std::time::Instant,
817) {
818    let elapsed_ms = start.elapsed().as_millis();
819    if std::env::var("LCTX_DEBUG").is_ok() {
820        eprintln!(
821            "[graph_provider] source={source:?} nodes={nodes} edges={edges} resolve_ms={elapsed_ms}"
822        );
823    }
824    let _ = (source, nodes, edges, elapsed_ms);
825}
826
827/// Triggers a background graph build once per process when the graph is empty.
828fn trigger_lazy_graph_build(project_root: &str) {
829    // Unit tests rewrite the process-global `LEAN_CTX_DATA_DIR` per test (each uses
830    // its own tempdir). A detached, fire-and-forget build thread reads that global
831    // mid-flight and runs concurrently with test bodies that are otherwise
832    // serialized on `test_env_lock` — a source of graph-state concurrency, and the
833    // root of an intermittent macOS-only flake where a freshly-built index appeared
834    // empty to the asserting test. `open_or_build` has a synchronous fallback that
835    // fully covers tests, so skip the background build under `cfg!(test)`. Production
836    // (and integration tests, which run the lib normally) are unaffected.
837    if cfg!(test) {
838        return;
839    }
840    // Preserve the cheap post-trigger fast path, but do not claim the latch
841    // until the root has been validated as an actual project.
842    if GRAPH_BUILD_TRIGGERED.load(Ordering::SeqCst) {
843        return;
844    }
845    let root = Path::new(project_root);
846    // Both probes are TCC-guarded (#356): a non-existent/non-dir path has no
847    // markers, and a launchd-standalone process never stats under ~/Documents.
848    let is_project = crate::core::pathutil::has_project_marker(root)
849        || crate::core::pathutil::has_multi_repo_children(root);
850    if !is_project {
851        return;
852    }
853    if GRAPH_BUILD_TRIGGERED.swap(true, Ordering::SeqCst) {
854        return;
855    }
856    // #682.2: build via the same reliable worker that builds the JSON index
857    // (which now mirrors into PG, backend-gated), instead of a dedicated
858    // fire-and-forget thread that silently died in short-lived processes.
859    super::index_orchestrator::ensure_all_background(project_root);
860}
861
862/// Build the property graph from the proven graph_index extractor (#682.1).
863///
864/// Loads the current [`ProjectIndex`] (or scans if absent) and mirrors it into
865/// the SQLite store — files + `file_catalog`, symbols, and structural edges —
866/// then stamps `graph.meta.json`. Sourcing PG from the mature extractor
867/// guarantees PG ⊇ graph_index (so a later backend flip cannot lose data) and
868/// populates the `file_catalog` that the `pg_populated` gate requires.
869///
870/// Synchronous and self-contained in `core` (no `tools` dependency), so callers
871/// can build reliably without the fire-and-forget caveat of the lazy trigger.
872pub fn build_property_graph(project_root: &str) -> anyhow::Result<()> {
873    let index = super::index_orchestrator::try_load_graph_index(project_root)
874        .filter(|i| !i.files.is_empty())
875        .unwrap_or_else(|| graph_index::scan_with_content_cache(project_root).0);
876    super::property_graph::mirror_index(project_root, &index)
877}
878
879/// Maximum time `open_or_build` will wait for a synchronous graph build before
880/// falling back to "graph unavailable" (#1018). Prevents blocking-pool
881/// exhaustion on large repos with cold indices, especially on Windows with AV.
882const BUILD_TIMEOUT_SECS: u64 = 30;
883
884/// Serialization gate: only one synchronous graph build at a time. Concurrent
885/// callers that cannot acquire the gate return immediately with `None` rather
886/// than queueing (which would exhaust the blocking pool). The background indexer
887/// picks up the build for them asynchronously.
888static BUILD_GATE: std::sync::LazyLock<std::sync::Mutex<()>> =
889    std::sync::LazyLock::new(|| std::sync::Mutex::new(()));
890
891pub fn open_or_build(project_root: &str) -> Option<OpenGraphProvider> {
892    if let (Some(p), _) = open_existing(project_root) {
893        return Some(p);
894    }
895
896    // Tests require the documented byte-stable result even when the parallel
897    // suite is building another fixture. Production remains non-blocking.
898    let _gate = if cfg!(test) {
899        BUILD_GATE
900            .lock()
901            .unwrap_or_else(std::sync::PoisonError::into_inner)
902    } else {
903        let Ok(gate) = BUILD_GATE.try_lock() else {
904            tracing::info!(
905                "open_or_build: another build in progress for {project_root}; returning None"
906            );
907            trigger_lazy_graph_build(project_root);
908            return None;
909        };
910        gate
911    };
912
913    // Run the potentially-slow `load_or_build` under a timeout thread so we
914    // don't block the calling tool handler for unbounded time (#1018).
915    let root_owned = project_root.to_string();
916    let (tx, rx) = std::sync::mpsc::sync_channel(1);
917    std::thread::spawn(move || {
918        let idx = super::graph_index::load_or_build(&root_owned);
919        let _ = tx.send(idx);
920    });
921
922    let timeout = std::time::Duration::from_secs(BUILD_TIMEOUT_SECS);
923    match rx.recv_timeout(timeout) {
924        Ok(idx) if !idx.files.is_empty() => Some(OpenGraphProvider {
925            source: GraphProviderSource::GraphIndex,
926            provider: GraphProvider::GraphIndex(idx),
927        }),
928        Ok(_) => None,
929        Err(_) => {
930            tracing::warn!(
931                "open_or_build: graph build timed out after {BUILD_TIMEOUT_SECS}s for {project_root}; \
932                 triggering background build"
933            );
934            trigger_lazy_graph_build(project_root);
935            None
936        }
937    }
938}
939#[cfg(test)]
940mod tests {
941    use super::*;
942
943    #[test]
944    fn best_effort_prefers_graph_index_when_property_graph_empty() {
945        let _lock = crate::core::data_dir::test_env_lock();
946        let tmp = tempfile::tempdir().expect("tempdir");
947        let data = tmp.path().join("data");
948        std::fs::create_dir_all(&data).expect("mkdir data");
949        crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.to_string_lossy().to_string());
950
951        let project_root = tmp.path().join("proj");
952        std::fs::create_dir_all(&project_root).expect("mkdir proj");
953        let root = project_root.to_string_lossy().to_string();
954        // The index serves a file only while it exists in the project.
955        std::fs::create_dir_all(project_root.join("src")).expect("mkdir src");
956        std::fs::write(project_root.join("src/main.rs"), "fn main() {}\n").expect("source");
957
958        let mut idx = ProjectIndex::new(&root);
959        idx.files.insert(
960            "src/main.rs".to_string(),
961            super::super::graph_index::FileEntry {
962                path: "src/main.rs".to_string(),
963                hash: "h".to_string(),
964                language: "rs".to_string(),
965                line_count: 1,
966                token_count: 1,
967                exports: vec![],
968                summary: String::new(),
969            },
970        );
971        idx.save().expect("save index");
972
973        let open = open_best_effort(&root).expect("open");
974        assert_eq!(open.source, GraphProviderSource::GraphIndex);
975
976        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
977    }
978
979    #[test]
980    fn best_effort_none_when_no_graphs() {
981        let _lock = crate::core::data_dir::test_env_lock();
982        let tmp = tempfile::tempdir().expect("tempdir");
983        let data = tmp.path().join("data");
984        std::fs::create_dir_all(&data).expect("mkdir data");
985        crate::test_env::set_var("LEAN_CTX_DATA_DIR", data.to_string_lossy().to_string());
986
987        let project_root = tmp.path().join("proj");
988        std::fs::create_dir_all(&project_root).expect("mkdir proj");
989        let root = project_root.to_string_lossy().to_string();
990
991        let open = open_best_effort(&root);
992        assert!(open.is_none());
993
994        crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
995    }
996
997    fn handle_provider() -> (GraphProvider, tempfile::TempDir) {
998        let mut idx = ProjectIndex::new("/test");
999        for (key, file, name, kind, start, end) in [
1000            (
1001                "src/lib.rs::Config",
1002                "src/lib.rs",
1003                "Config",
1004                "struct",
1005                5usize,
1006                20usize,
1007            ),
1008            (
1009                "src/lib.rs::Config::load",
1010                "src/lib.rs",
1011                "Config::load",
1012                "method",
1013                22,
1014                35,
1015            ),
1016            ("src/main.rs::main", "src/main.rs", "main", "fn", 1, 10),
1017        ] {
1018            idx.symbols.insert(
1019                key.to_string(),
1020                graph_index::SymbolEntry {
1021                    file: file.to_string(),
1022                    name: name.to_string(),
1023                    kind: kind.to_string(),
1024                    start_line: start,
1025                    end_line: end,
1026                    is_exported: true,
1027                },
1028            );
1029        }
1030        let (idx, files) = idx.on_disk(&[]);
1031        (GraphProvider::GraphIndex(idx), files)
1032    }
1033
1034    #[test]
1035    fn handle_resolves_exact_file_and_name() {
1036        let (gp, _files) = handle_provider();
1037        let h = crate::core::handle::SymbolHandle::new("src/lib.rs", "Config::load", 22);
1038        let sym = gp.find_symbol_by_handle(&h).expect("resolves");
1039        assert_eq!(sym.name, "Config::load");
1040        assert_eq!(sym.start_line, 22);
1041    }
1042
1043    #[test]
1044    fn handle_resolves_after_line_drift() {
1045        // Same (path, name) but a stale, drifted line — must still resolve to
1046        // the current symbol at its real line (robust beyond line-only refs).
1047        let (gp, _files) = handle_provider();
1048        let h = crate::core::handle::SymbolHandle::new("src/lib.rs", "Config::load", 999);
1049        let sym = gp
1050            .find_symbol_by_handle(&h)
1051            .expect("resolves despite drift");
1052        assert_eq!(sym.start_line, 22);
1053    }
1054
1055    #[test]
1056    fn handle_resolves_by_unqualified_tail() {
1057        // Handle carries only the unqualified tail `load`; the exact key misses,
1058        // so the same-file tail match + line tiebreak recovers `Config::load`.
1059        let (gp, _files) = handle_provider();
1060        let h = crate::core::handle::SymbolHandle::new("src/lib.rs", "load", 22);
1061        let sym = gp.find_symbol_by_handle(&h).expect("resolves by tail");
1062        assert_eq!(sym.name, "Config::load");
1063    }
1064
1065    #[test]
1066    fn handle_unknown_file_returns_none() {
1067        let (gp, _files) = handle_provider();
1068        let h = crate::core::handle::SymbolHandle::new("src/nope.rs", "Config::load", 22);
1069        assert!(gp.find_symbol_by_handle(&h).is_none());
1070    }
1071
1072    #[test]
1073    fn pg_kind_filter_matches_precise_metadata_kind() {
1074        // #889: a PG symbol's coarse `nodes.kind` is always `"symbol"`; the
1075        // source kind (`struct`) lives in metadata. Filtering by kind="struct"
1076        // must still find it (previously matched the coarse column → empty).
1077        use super::super::property_graph::{Node, NodeKind};
1078
1079        let pg = CodeGraph::open_in_memory().unwrap();
1080        pg.upsert_node(
1081            &Node::symbol("State", "simplex/simplex.go", NodeKind::Symbol)
1082                .with_lines(129, 140)
1083                .with_metadata(r#"{"kind":"struct","exported":true}"#),
1084        )
1085        .unwrap();
1086        let gp = GraphProvider::PropertyGraph(pg);
1087
1088        let hits = gp.find_symbols("State", None, Some("struct"));
1089        assert_eq!(
1090            hits.len(),
1091            1,
1092            "kind=struct must resolve the exported struct"
1093        );
1094        assert_eq!(hits[0].kind, "struct");
1095        assert!(hits[0].is_exported);
1096
1097        // A non-matching kind still filters it out.
1098        assert!(gp.find_symbols("State", None, Some("fn")).is_empty());
1099    }
1100
1101    #[test]
1102    fn parity_dependencies_both_stores_agree() {
1103        use super::super::graph_index::{FileEntry, IndexEdge};
1104        use super::super::property_graph::{Edge, EdgeKind, Node};
1105
1106        let pg = CodeGraph::open_in_memory().unwrap();
1107        let a_id = pg.upsert_node(&Node::file("src/a.rs")).unwrap();
1108        let b_id = pg.upsert_node(&Node::file("src/b.rs")).unwrap();
1109        let c_id = pg.upsert_node(&Node::file("src/c.rs")).unwrap();
1110        pg.upsert_edge(&Edge::new(a_id, b_id, EdgeKind::Imports))
1111            .unwrap();
1112        pg.upsert_edge(&Edge::new(a_id, c_id, EdgeKind::Imports))
1113            .unwrap();
1114
1115        let mut idx = ProjectIndex::new("/test");
1116        for name in &["src/a.rs", "src/b.rs", "src/c.rs"] {
1117            idx.files.insert(
1118                name.to_string(),
1119                FileEntry {
1120                    path: name.to_string(),
1121                    hash: "h".into(),
1122                    language: "rs".into(),
1123                    line_count: 1,
1124                    token_count: 1,
1125                    exports: vec![],
1126                    summary: String::new(),
1127                },
1128            );
1129        }
1130        idx.edges.push(IndexEdge {
1131            from: "src/a.rs".into(),
1132            to: "src/b.rs".into(),
1133            kind: "import".into(),
1134            weight: 1.0,
1135        });
1136        idx.edges.push(IndexEdge {
1137            from: "src/a.rs".into(),
1138            to: "src/c.rs".into(),
1139            kind: "import".into(),
1140            weight: 1.0,
1141        });
1142
1143        let pg_deps = GraphProvider::PropertyGraph(pg);
1144        let (idx, _files) = idx.on_disk(&[]);
1145        let gi_deps = GraphProvider::GraphIndex(idx);
1146
1147        let mut pg_result = pg_deps.dependencies("src/a.rs");
1148        let mut gi_result = gi_deps.dependencies("src/a.rs");
1149        pg_result.sort();
1150        gi_result.sort();
1151
1152        assert_eq!(
1153            pg_result, gi_result,
1154            "Import edges must match between PG and GraphIndex"
1155        );
1156
1157        let mut pg_dependents = pg_deps.dependents("src/b.rs");
1158        let mut gi_dependents = gi_deps.dependents("src/b.rs");
1159        pg_dependents.sort();
1160        gi_dependents.sort();
1161        assert_eq!(
1162            pg_dependents, gi_dependents,
1163            "Dependents must match between PG and GraphIndex"
1164        );
1165    }
1166
1167    /// Round-trip guard for #696 C1: graph_index → PG (mirror) → graph_index
1168    /// (materialize) must preserve files (full catalog), symbols (incl. the
1169    /// precise `kind` + `is_exported`, which live in node metadata) and import
1170    /// edges. Proves the PropertyGraph can be the sole source for the remaining
1171    /// legacy `ProjectIndex` consumers — the prerequisite for retiring the JSON
1172    /// store.
1173    #[test]
1174    fn materialize_project_index_round_trips_losslessly() {
1175        use super::super::graph_index::{FileEntry, IndexEdge, SymbolEntry};
1176        use super::super::property_graph::populate_from_project_index;
1177
1178        let mut a = ProjectIndex::new("/test");
1179        a.files.insert(
1180            "src/a.rs".to_string(),
1181            FileEntry {
1182                path: "src/a.rs".to_string(),
1183                hash: "hash-a".to_string(),
1184                language: "rs".to_string(),
1185                line_count: 42,
1186                token_count: 137,
1187                exports: vec!["Foo".to_string()],
1188                summary: "module a".to_string(),
1189            },
1190        );
1191        a.files.insert(
1192            "src/b.rs".to_string(),
1193            FileEntry {
1194                path: "src/b.rs".to_string(),
1195                hash: "hash-b".to_string(),
1196                language: "rs".to_string(),
1197                line_count: 7,
1198                token_count: 19,
1199                exports: vec![],
1200                summary: String::new(),
1201            },
1202        );
1203        // Two symbols with DIFFERENT kinds and export flags — the fields most at
1204        // risk of being flattened by the coarse property-graph `NodeKind`.
1205        a.symbols.insert(
1206            "src/a.rs::Foo".to_string(),
1207            SymbolEntry {
1208                file: "src/a.rs".to_string(),
1209                name: "Foo".to_string(),
1210                kind: "struct".to_string(),
1211                start_line: 1,
1212                end_line: 9,
1213                is_exported: true,
1214            },
1215        );
1216        a.symbols.insert(
1217            "src/b.rs::helper".to_string(),
1218            SymbolEntry {
1219                file: "src/b.rs".to_string(),
1220                name: "helper".to_string(),
1221                kind: "function".to_string(),
1222                start_line: 3,
1223                end_line: 6,
1224                is_exported: false,
1225            },
1226        );
1227        a.edges.push(IndexEdge {
1228            from: "src/b.rs".to_string(),
1229            to: "src/a.rs".to_string(),
1230            kind: "import".to_string(),
1231            weight: 1.0,
1232        });
1233
1234        let pg = CodeGraph::open_in_memory().unwrap();
1235        populate_from_project_index(&pg, &a).unwrap();
1236        let provider = GraphProvider::PropertyGraph(pg);
1237        let b = provider.materialize_project_index("/test");
1238
1239        // Files: inventory + every catalog field survive.
1240        let mut a_files: Vec<&String> = a.files.keys().collect();
1241        let mut b_files: Vec<&String> = b.files.keys().collect();
1242        a_files.sort();
1243        b_files.sort();
1244        assert_eq!(a_files, b_files, "file inventory must round-trip");
1245        for (path, fa) in &a.files {
1246            let fb = b.files.get(path).expect("file present after round trip");
1247            assert_eq!(fa.hash, fb.hash, "hash {path}");
1248            assert_eq!(fa.language, fb.language, "language {path}");
1249            assert_eq!(fa.line_count, fb.line_count, "line_count {path}");
1250            assert_eq!(fa.token_count, fb.token_count, "token_count {path}");
1251            assert_eq!(fa.exports, fb.exports, "exports {path}");
1252            assert_eq!(fa.summary, fb.summary, "summary {path}");
1253        }
1254
1255        // Symbols: keys + spans + the metadata-carried kind/export flag survive.
1256        let mut a_syms: Vec<&String> = a.symbols.keys().collect();
1257        let mut b_syms: Vec<&String> = b.symbols.keys().collect();
1258        a_syms.sort();
1259        b_syms.sort();
1260        assert_eq!(a_syms, b_syms, "symbol table must round-trip");
1261        for (key, sa) in &a.symbols {
1262            let sb = b.symbols.get(key).expect("symbol present after round trip");
1263            assert_eq!(sa.name, sb.name, "name {key}");
1264            assert_eq!(sa.file, sb.file, "file {key}");
1265            assert_eq!(sa.kind, sb.kind, "kind {key}");
1266            assert_eq!(sa.start_line, sb.start_line, "start_line {key}");
1267            assert_eq!(sa.end_line, sb.end_line, "end_line {key}");
1268            assert_eq!(sa.is_exported, sb.is_exported, "is_exported {key}");
1269        }
1270
1271        // Structural edges: the import edge survives (PG may enrich, never lose).
1272        assert!(
1273            b.edges
1274                .iter()
1275                .any(|e| e.from == "src/b.rs" && e.to == "src/a.rs" && e.kind == "import"),
1276            "import edge must round-trip; got {:?}",
1277            b.edges
1278        );
1279    }
1280}