Skip to main content

rto_graph/
workspace.rs

1//! A **workspace**: many per-repo graphs served by one process (ADR-0008).
2//!
3//! Each Roteiro graph is per-repo — a small `SQLite` store at
4//! `<repo>/.git/roteiro/graph.db`. The expensive resource a server holds is the
5//! *model*, not the graphs, so one process can hold the model once and answer
6//! questions about **any** registered repo by opening that repo's store on
7//! demand and caching it. A [`Workspace`] is that registry + on-demand,
8//! cached store resolver; the tool surfaces (MCP and the `/v1` model server)
9//! call [`Workspace::with_store`] with an optional `project` selector.
10//!
11//! Single-repo serving is just a workspace with one project (see
12//! [`Workspace::single`]), so the default `serve` path is unchanged.
13//!
14//! The registry can be **reloaded** in place ([`Workspace::reload_from`]) so a
15//! long-lived server can pick up added/removed repos without a restart (a SIGHUP
16//! trigger); already-open stores for still-present projects keep their warm
17//! connections, and dropped projects are evicted. An optional first-open hook
18//! ([`Workspace::with_on_open`], `serve --sync-on-access`) (re)builds a project's
19//! graph the first time it is queried.
20
21use std::collections::{BTreeMap, HashMap};
22use std::path::{Path, PathBuf};
23use std::sync::{Arc, Mutex};
24
25use crate::git::{GitError, Repo};
26use crate::model::Node;
27use crate::store::{Store, StoreError};
28
29/// A failure resolving or opening a project's graph.
30#[derive(Debug, thiserror::Error)]
31pub enum WorkspaceError {
32    /// A call named a project the workspace does not know.
33    #[error("no project named `{name}` (known: {known})")]
34    UnknownProject {
35        /// The requested name.
36        name: String,
37        /// Comma-separated list of known project names.
38        known: String,
39    },
40    /// A call omitted `project` but the workspace has no single default (it holds
41    /// several projects), so the selection is ambiguous.
42    #[error("this server hosts several projects ({known}); name one with `project`")]
43    AmbiguousProject {
44        /// Comma-separated list of known project names.
45        known: String,
46    },
47    /// The workspace is registered but empty (no repos resolved).
48    #[error("no projects registered")]
49    Empty,
50    /// A selector named a workspace the [`WorkspaceSet`] does not know.
51    #[error("no workspace named `{name}` (known: {known})")]
52    UnknownWorkspace {
53        /// The requested workspace name.
54        name: String,
55        /// Comma-separated list of known workspace names.
56        known: String,
57    },
58    /// A selection omitted a name but the [`WorkspaceSet`] holds several
59    /// workspaces, so the choice is ambiguous.
60    #[error("several workspaces configured ({known}); select one with `--workspace-name`")]
61    AmbiguousWorkspace {
62        /// Comma-separated list of known workspace names.
63        known: String,
64    },
65    /// Reading a workspace root directory during repo discovery failed.
66    #[error("reading workspace root `{}`: {msg}", .root.display())]
67    Discover {
68        /// The root directory that could not be read.
69        root: PathBuf,
70        /// The underlying I/O error message.
71        msg: String,
72    },
73    /// A cross-repo target was not a project-qualified key (`<project>::<key>`).
74    #[error("`{key}` is not a project-qualified key (expected `<project>::<key>`)")]
75    Unqualified {
76        /// The malformed key.
77        key: String,
78    },
79    /// The project's graph store does not exist yet — its repo has not been
80    /// synced (`roteiro sync`).
81    #[error("project `{name}` has no graph yet — run `roteiro sync` in {}", .path.display())]
82    NoGraph {
83        /// The project name.
84        name: String,
85        /// The repo directory whose graph is missing.
86        path: PathBuf,
87    },
88    /// The on-open hook (`serve --sync-on-access`) failed to prepare a project's
89    /// graph before it was first served.
90    #[error("failed to prepare project `{name}` on first access: {msg}")]
91    Prepare {
92        /// The project name.
93        name: String,
94        /// The hook's error message.
95        msg: String,
96    },
97    /// A store lock was poisoned by a panic in another thread.
98    #[error("store lock poisoned")]
99    Poisoned,
100    /// Discovering the repo for a registered path failed.
101    #[error(transparent)]
102    Git(#[from] GitError),
103    /// Opening the project's store failed.
104    #[error(transparent)]
105    Store(#[from] StoreError),
106}
107
108/// Where a project's store comes from: a `graph.db` to open on demand, or an
109/// already-open store (the single-repo default and tests).
110#[derive(Clone)]
111enum Source {
112    /// Open this `graph.db` path on first use, for the repository whose working
113    /// tree is rooted at `root`.
114    ///
115    /// `root` is *carried* rather than derived from `db`, because a
116    /// repository's own configuration governs how it is scanned, whoever is
117    /// asking ([`Workspace::project_root`]) — and the "repo dir is the store's
118    /// grandparent" shortcut is wrong for a **linked worktree**, whose git dir
119    /// is `<main>/.git/worktrees/<name>`, not `<repo>/.git`. [`build_registry`]
120    /// already holds the true working-tree root, so it is recorded here instead
121    /// of guessed later. `None` where the caller supplied only a `graph.db`
122    /// path ([`Workspace::from_named_dbs`]).
123    Path {
124        /// The `graph.db` to open.
125        db: PathBuf,
126        /// The repository's working-tree root, when known.
127        root: Option<PathBuf>,
128    },
129    /// A pre-opened store, shared directly.
130    Open(Arc<Mutex<Store>>),
131}
132
133/// The registry plus the open-store cache, behind one lock. Held only briefly —
134/// to look up a source or (un)cache a handle — never across a graph query, which
135/// runs on the returned per-store `Mutex` after this lock is released.
136struct Inner {
137    /// Project name → its store source, in stable name order.
138    projects: BTreeMap<String, Source>,
139    /// The project used when a call omits `project` (the sole project, if there
140    /// is exactly one; otherwise `None` and a bare call is ambiguous).
141    default: Option<String>,
142    /// Opened stores, cached by project name, tagged with the [`Source`] they
143    /// were opened from. `Store` is `!Sync` (it holds a rusqlite connection), so
144    /// each is behind its own `Mutex`. The tag lets a reload keep a warm
145    /// connection only when the project still maps to the *same* source, and
146    /// never serve a handle for a repo the name no longer points at.
147    cache: HashMap<String, (Source, Arc<Mutex<Store>>)>,
148}
149
150/// Whether two sources denote the same store: the same `graph.db` path, or the
151/// very same pre-opened handle. The `graph.db` path *is* the store's identity,
152/// so the recorded working-tree root does not enter the comparison.
153fn source_eq(a: &Source, b: &Source) -> bool {
154    match (a, b) {
155        (Source::Path { db: x, .. }, Source::Path { db: y, .. }) => x == y,
156        (Source::Open(x), Source::Open(y)) => Arc::ptr_eq(x, y),
157        _ => false,
158    }
159}
160
161/// A hook run against a project's `graph.db` path the first time it is opened —
162/// used by `serve --sync-on-access` to (re)build a stale or missing graph before
163/// it is served (ADR-0008). Returns a human-readable error on failure.
164pub type OnOpen = Arc<dyn Fn(&Path) -> Result<(), String> + Send + Sync>;
165
166/// A named set of per-repo graphs, each opened on demand and cached. Cheap to
167/// hold: the stores are small `SQLite` files opened lazily; the caller (a server)
168/// holds the one expensive model. The registry is reloadable in place.
169pub struct Workspace {
170    inner: Mutex<Inner>,
171    /// Optional first-open hook (`serve --sync-on-access`): run against a
172    /// project's `graph.db` path before it is opened, to sync it on demand.
173    on_open: Option<OnOpen>,
174}
175
176impl Workspace {
177    /// A single-project workspace over an already-open `store`, named `name`.
178    /// This is the single-repo `serve` default and the test constructor; a bare
179    /// (no-`project`) call resolves to it. Not reloadable (no repo paths).
180    #[must_use]
181    pub fn single(name: impl Into<String>, store: Store) -> Self {
182        let name = name.into();
183        let mut projects = BTreeMap::new();
184        projects.insert(name.clone(), Source::Open(Arc::new(Mutex::new(store))));
185        Self {
186            inner: Mutex::new(Inner {
187                projects,
188                default: Some(name),
189                cache: HashMap::new(),
190            }),
191            on_open: None,
192        }
193    }
194
195    /// A workspace over several already-open stores, one per named project — the
196    /// in-memory counterpart of [`Workspace::from_repo_paths`] (which opens each
197    /// project's `graph.db` from disk lazily). Used for multi-repo serving of
198    /// pre-built stores and for tests. With exactly one project it becomes the
199    /// default (as [`Workspace::single`]); with several, a bare (no-`project`)
200    /// call is ambiguous. Not reloadable (no repo paths).
201    #[must_use]
202    pub fn from_stores<I, S>(stores: I) -> Self
203    where
204        I: IntoIterator<Item = (S, Store)>,
205        S: Into<String>,
206    {
207        let mut projects = BTreeMap::new();
208        for (name, store) in stores {
209            // Dedupe like `from_repo_paths` (`-2`, `-3`, …) so two stores sharing a
210            // base name both survive instead of the second silently overwriting the
211            // first (which would drop a project).
212            let name = dedupe_name(&projects, name.into());
213            projects.insert(name, Source::Open(Arc::new(Mutex::new(store))));
214        }
215        // Mirror `from_repo_paths`: a lone project is the default; several are
216        // ambiguous until a call names one.
217        let default = if projects.len() == 1 {
218            projects.keys().next().cloned()
219        } else {
220            None
221        };
222        Self {
223            inner: Mutex::new(Inner {
224                projects,
225                default,
226                cache: HashMap::new(),
227            }),
228            on_open: None,
229        }
230    }
231
232    /// Build a workspace from repo directories: each is `git`-discovered, named
233    /// after its working-tree directory (collisions get a `-2`, `-3`, … suffix),
234    /// and its `graph.db` opened lazily. With exactly one repo, that repo is the
235    /// default project.
236    ///
237    /// # Errors
238    /// [`WorkspaceError::Git`] if a path is not inside a git repository, or
239    /// [`WorkspaceError::Empty`] if `paths` resolves to no repos.
240    pub fn from_repo_paths<I, P>(paths: I) -> Result<Self, WorkspaceError>
241    where
242        I: IntoIterator<Item = P>,
243        P: AsRef<Path>,
244    {
245        let (projects, default) = build_registry(paths)?;
246        Ok(Self {
247            inner: Mutex::new(Inner {
248                projects,
249                default,
250                cache: HashMap::new(),
251            }),
252            on_open: None,
253        })
254    }
255
256    /// Build a workspace from explicit `(project name, graph.db path)` pairs,
257    /// **without** git discovery — used where the names and store locations are
258    /// already known ([`WorkspaceSet`] construction re-uses the CLI's discovery
259    /// upstream, and tests build synthetic registries). Names are taken verbatim
260    /// (deduplicate before calling if a collision is possible); with exactly one
261    /// pair, that project is the default.
262    #[must_use]
263    pub fn from_named_dbs<I>(dbs: I) -> Self
264    where
265        I: IntoIterator<Item = (String, PathBuf)>,
266    {
267        let projects: BTreeMap<String, Source> = dbs
268            .into_iter()
269            .map(|(n, db)| (n, Source::Path { db, root: None }))
270            .collect();
271        let default = (projects.len() == 1)
272            .then(|| projects.keys().next().cloned())
273            .flatten();
274        Self {
275            inner: Mutex::new(Inner {
276                projects,
277                default,
278                cache: HashMap::new(),
279            }),
280            on_open: None,
281        }
282    }
283
284    /// The `graph.db` paths of the workspace's lazily-opened (`Path`) projects, in
285    /// stable name order. Pre-opened (`single`) projects carry no path and are
286    /// omitted. Used by [`WorkspaceSet::containing`] to find which workspace holds
287    /// a given repo.
288    #[must_use]
289    pub fn member_dbs(&self) -> Vec<PathBuf> {
290        self.lock()
291            .map(|i| {
292                i.projects
293                    .values()
294                    .filter_map(|s| match s {
295                        Source::Path { db, .. } => Some(db.clone()),
296                        Source::Open(_) => None,
297                    })
298                    .collect()
299            })
300            .unwrap_or_default()
301    }
302
303    /// The **working-tree root** of `project`'s repository, resolving `project`
304    /// the same way [`Workspace::with_store`] does (so `None` means the default
305    /// project).
306    ///
307    /// This exists so a caller can read *that repository's own* configuration
308    /// rather than the invoking process's. The rule, following ADR-0009's
309    /// per-repo `[[links]]` resolution: **a repository's own config governs how
310    /// it is scanned, whoever is asking.** Without it, a server started in repo
311    /// A answers questions about repo B using A's settings — and B's own
312    /// `[debt] ignore` never applies, so the API and B's CLI disagree about B.
313    ///
314    /// Returns `Ok(None)` when the project's store was handed over pre-opened
315    /// ([`Workspace::single`] / [`Workspace::from_stores`]) or registered by
316    /// `graph.db` path alone ([`Workspace::from_named_dbs`]): there is no
317    /// repository on disk to consult, and the caller falls back to its own
318    /// configuration.
319    ///
320    /// # Errors
321    /// [`WorkspaceError::UnknownProject`] / [`WorkspaceError::AmbiguousProject`]
322    /// as [`Workspace::resolve`], or [`WorkspaceError::Poisoned`].
323    pub fn project_root(&self, project: Option<&str>) -> Result<Option<PathBuf>, WorkspaceError> {
324        let name = self.resolve(project)?;
325        let inner = self.lock()?;
326        Ok(match inner.projects.get(&name) {
327            Some(Source::Path { root, .. }) => root.clone(),
328            _ => None,
329        })
330    }
331
332    /// Set a first-open hook (`serve --sync-on-access`): before a project's store
333    /// is opened for the first time, `hook` is run against its `graph.db` path to
334    /// (re)build it. Applies to lazily-opened `Path` projects; a pre-opened
335    /// `single` store is already loaded, so the hook does not fire for it.
336    #[must_use]
337    pub fn with_on_open(mut self, hook: OnOpen) -> Self {
338        self.on_open = Some(hook);
339        self
340    }
341
342    /// Rebuild the registry from a fresh set of repo `paths`: added repos become
343    /// available, removed ones are dropped (and their cached store evicted), and
344    /// still-present ones keep their warm connection. Returns the new project
345    /// names. Use this to reload a running server (e.g. on SIGHUP) without a
346    /// restart. A single-project pre-opened workspace ([`Workspace::single`]) has
347    /// no repo paths, so reloading it simply replaces it with the given repos.
348    ///
349    /// # Errors
350    /// As [`Workspace::from_repo_paths`].
351    pub fn reload_from<I, P>(&self, paths: I) -> Result<Vec<String>, WorkspaceError>
352    where
353        I: IntoIterator<Item = P>,
354        P: AsRef<Path>,
355    {
356        // Build the new registry outside the lock (discovery does git I/O).
357        let (projects, default) = build_registry(paths)?;
358        let names: Vec<String> = projects.keys().cloned().collect();
359        let mut inner = self.lock()?;
360        // Keep a warm connection only where the project still maps to the *same*
361        // source; drop it if the name is gone or now points at a different
362        // `graph.db` (or was a pre-opened `single` store), so a query never hits
363        // the wrong repo.
364        inner
365            .cache
366            .retain(|name, (src, _)| projects.get(name).is_some_and(|new| source_eq(new, src)));
367        inner.projects = projects;
368        inner.default = default;
369        Ok(names)
370    }
371
372    /// The registered project names, in stable order.
373    #[must_use]
374    pub fn names(&self) -> Vec<String> {
375        self.lock()
376            .map(|i| i.projects.keys().cloned().collect())
377            .unwrap_or_default()
378    }
379
380    /// Whether the workspace holds more than one project (so `project` selection
381    /// is meaningful to expose to callers/tools).
382    #[must_use]
383    pub fn is_multi(&self) -> bool {
384        self.lock().is_ok_and(|i| i.projects.len() > 1)
385    }
386
387    /// Resolve `project` (or the default) to a concrete project name.
388    ///
389    /// # Errors
390    /// [`WorkspaceError::UnknownProject`] if named but absent,
391    /// [`WorkspaceError::AmbiguousProject`] if omitted with several projects, or
392    /// [`WorkspaceError::Empty`] if there are none.
393    pub fn resolve(&self, project: Option<&str>) -> Result<String, WorkspaceError> {
394        let inner = self.lock()?;
395        match project {
396            Some(name) if inner.projects.contains_key(name) => Ok(name.to_owned()),
397            Some(name) => Err(WorkspaceError::UnknownProject {
398                name: name.to_owned(),
399                known: keys(&inner.projects),
400            }),
401            None => inner.default.clone().ok_or_else(|| {
402                if inner.projects.is_empty() {
403                    WorkspaceError::Empty
404                } else {
405                    WorkspaceError::AmbiguousProject {
406                        known: keys(&inner.projects),
407                    }
408                }
409            }),
410        }
411    }
412
413    /// Run `f` with the resolved project's store (opened and cached on first
414    /// use). The store lock is held only for `f`, never across an `.await`.
415    ///
416    /// # Errors
417    /// As [`Workspace::resolve`], plus [`WorkspaceError::NoGraph`] if the store
418    /// file is absent, [`WorkspaceError::Store`] on open failure, or
419    /// [`WorkspaceError::Poisoned`] if a lock was poisoned.
420    pub fn with_store<R>(
421        &self,
422        project: Option<&str>,
423        f: impl FnOnce(&Store) -> R,
424    ) -> Result<R, WorkspaceError> {
425        let name = self.resolve(project)?;
426        let handle = self.handle(&name)?;
427        let store = handle.lock().map_err(|_| WorkspaceError::Poisoned)?;
428        Ok(f(&store))
429    }
430
431    /// Like [`Workspace::with_store`], but hands `f` a **mutable** store so it can
432    /// persist into the graph (e.g. [`Store::apply_import_layer`]). The store lock
433    /// is held only for `f`, never across an `.await`. Backs the explorer's
434    /// `links/write` endpoint, which materialises the inferred cross-repo links into
435    /// a spoke's graph as a durable import layer.
436    ///
437    /// # Errors
438    /// As [`Workspace::with_store`].
439    pub fn with_store_mut<R>(
440        &self,
441        project: Option<&str>,
442        f: impl FnOnce(&mut Store) -> R,
443    ) -> Result<R, WorkspaceError> {
444        let name = self.resolve(project)?;
445        let handle = self.handle(&name)?;
446        let mut store = handle.lock().map_err(|_| WorkspaceError::Poisoned)?;
447        Ok(f(&mut store))
448    }
449
450    /// Resolve a **project-qualified** key `"<project>::<key>"` to its node across
451    /// the workspace, opening the target project on demand (ADR-0009). `Ok(None)`
452    /// means the key is well-formed and the project exists but the node does not —
453    /// i.e. **cross-repo drift** (a removed or renamed target). Errors distinguish
454    /// the other failure modes so a caller can report them precisely:
455    /// [`WorkspaceError::Unqualified`] (not in `<project>::<key>` form),
456    /// [`WorkspaceError::UnknownProject`] (target repo not in the workspace),
457    /// [`WorkspaceError::NoGraph`] (target repo unsynced).
458    ///
459    /// # Errors
460    /// As above, plus [`WorkspaceError::Store`] / [`WorkspaceError::Poisoned`].
461    pub fn resolve_qualified(&self, qualified: &str) -> Result<Option<Node>, WorkspaceError> {
462        let (project, key) =
463            parse_qualified(qualified).ok_or_else(|| WorkspaceError::Unqualified {
464                key: qualified.to_owned(),
465            })?;
466        let key = key.to_owned();
467        self.with_store(Some(project), move |s| s.get_node(&key))?
468            .map_err(WorkspaceError::from)
469    }
470
471    /// Follow an **external-ref** placeholder node to the real node it stands for,
472    /// resolving its project-qualified target across the workspace (ADR-0009). An
473    /// external-ref lives in a spoke's store as a local stand-in for a node in the
474    /// hub's store (see [`crate::external_ref_node`]); this walks it through to the
475    /// hub. `Ok(None)` means either `node` is not an external-ref, or its target no
476    /// longer resolves — cross-repo drift (a removed or renamed hub key). Errors
477    /// distinguish the other failure modes, as [`Workspace::resolve_qualified`].
478    ///
479    /// # Errors
480    /// As [`Workspace::resolve_qualified`].
481    pub fn follow_external_ref(&self, node: &Node) -> Result<Option<Node>, WorkspaceError> {
482        match crate::external_ref_target(node) {
483            Some(qualified) => self.resolve_qualified(&qualified),
484            None => Ok(None),
485        }
486    }
487
488    /// Follow a **project-qualified** cross-repo target to the most specific
489    /// *definition* it names — the follow-the-link hop that turns a click on a
490    /// spoke's app-key target into a jump to the hub node that defines it.
491    ///
492    /// [`Workspace::resolve_qualified`] lands on the raw hub node a spoke points
493    /// at, which for a config override is the hub's `config_key` node (e.g.
494    /// `cfgkey:config.toml#serve.addr`), *not* the Rust struct that declares the
495    /// setting. This method adds the net-new **`config_key` → struct bridge**: when
496    /// the resolved node is a config key whose dotted path maps — with confidence —
497    /// to exactly one hub struct and one of its named fields, it returns that
498    /// struct as the jump target ([`Follow::StructField`], carrying the matched
499    /// field name). Otherwise it returns the resolved node unchanged
500    /// ([`Follow::Node`]) — a config key we could not bridge, or any non-config
501    /// target (e.g. an authored `[[links]]` that already points at a symbol). A
502    /// well-formed target whose node is gone is [`Follow::Drift`].
503    ///
504    /// The bridge is deliberately conservative (see [`bridge_config_key`]): it
505    /// fires only on a *unique* match of both an independent section→struct-name
506    /// signal and a field-presence signal, so it never jumps to a **wrong** node —
507    /// an ambiguous or unmatched key falls back to the config-key node.
508    ///
509    /// # Errors
510    /// As [`Workspace::resolve_qualified`] (a well-formed but unhosted / unsynced
511    /// target project still errors; a resolved-but-missing node is `Drift`).
512    pub fn follow_definition(&self, qualified: &str) -> Result<Follow, WorkspaceError> {
513        let (project, key) =
514            parse_qualified(qualified).ok_or_else(|| WorkspaceError::Unqualified {
515                key: qualified.to_owned(),
516            })?;
517        let key = key.to_owned();
518        self.with_store(Some(project), move |store| -> Result<Follow, StoreError> {
519            let Some(node) = store.get_node(&key)? else {
520                return Ok(Follow::Drift);
521            };
522            // Only a config-key node needs bridging; anything else the spoke points
523            // at is already a definition-level target. Compare against the stable
524            // token via `as_str()` — no allocation to build a throwaway `NodeKind`.
525            if node.kind.as_str() == crate::config_keys::KIND {
526                match bridge_config_key(store, &node)? {
527                    Some((target, field)) => Ok(Follow::StructField {
528                        node: target,
529                        field,
530                    }),
531                    None => Ok(Follow::Node { node }),
532                }
533            } else {
534                Ok(Follow::Node { node })
535            }
536        })?
537        .map_err(WorkspaceError::from)
538    }
539
540    /// Lock the inner state, mapping a poisoned lock to [`WorkspaceError::Poisoned`].
541    fn lock(&self) -> Result<std::sync::MutexGuard<'_, Inner>, WorkspaceError> {
542        self.inner.lock().map_err(|_| WorkspaceError::Poisoned)
543    }
544
545    /// Get (opening + caching on first use) the shared store handle for `name`.
546    /// Opens `graph.db` **outside** the registry lock so a first-touch open never
547    /// blocks other projects' queries.
548    fn handle(&self, name: &str) -> Result<Arc<Mutex<Store>>, WorkspaceError> {
549        // Fast path and pre-opened sources resolve under a single short lock.
550        let (db, root) = {
551            let mut inner = self.lock()?;
552            if let Some((_, handle)) = inner.cache.get(name) {
553                return Ok(handle.clone());
554            }
555            match inner.projects.get(name) {
556                Some(Source::Open(handle)) => {
557                    let handle = handle.clone();
558                    inner.cache.insert(
559                        name.to_owned(),
560                        (Source::Open(handle.clone()), handle.clone()),
561                    );
562                    return Ok(handle);
563                }
564                Some(Source::Path { db, root }) => (db.clone(), root.clone()),
565                None => {
566                    return Err(WorkspaceError::UnknownProject {
567                        name: name.to_owned(),
568                        known: keys(&inner.projects),
569                    });
570                }
571            }
572        };
573        // `serve --sync-on-access`: (re)build this project's graph before opening
574        // it, so a stale or never-synced repo is prepared on first touch. Runs
575        // outside the registry lock (it does extraction I/O).
576        if let Some(on_open) = &self.on_open {
577            on_open(&db).map_err(|msg| WorkspaceError::Prepare {
578                name: name.to_owned(),
579                msg,
580            })?;
581        }
582        if !db.exists() {
583            return Err(WorkspaceError::NoGraph {
584                name: name.to_owned(),
585                // The repo dir is the store's grandparent (`…/.git/roteiro`).
586                path: db
587                    .parent()
588                    .and_then(Path::parent)
589                    .and_then(Path::parent)
590                    .unwrap_or(&db)
591                    .to_path_buf(),
592            });
593        }
594        let handle = Arc::new(Mutex::new(Store::open(&db)?));
595        let opened = Source::Path {
596            db: db.clone(),
597            root,
598        };
599        let mut inner = self.lock()?;
600        // Another thread may have opened it while we were; prefer the existing.
601        if let Some((_, existing)) = inner.cache.get(name) {
602            return Ok(existing.clone());
603        }
604        // Only cache if the registry still maps this name to the DB we opened —
605        // a concurrent `reload_from` may have remapped or removed it. If so,
606        // return the freshly-opened handle for this call (the caller resolved
607        // before the reload) but do not cache a now-stale mapping.
608        if inner
609            .projects
610            .get(name)
611            .is_some_and(|current| source_eq(current, &opened))
612        {
613            inner
614                .cache
615                .insert(name.to_owned(), (opened, handle.clone()));
616        }
617        Ok(handle)
618    }
619}
620
621/// Comma-separated project names (for error messages).
622fn keys(projects: &BTreeMap<String, Source>) -> String {
623    projects.keys().cloned().collect::<Vec<_>>().join(", ")
624}
625
626/// Split a **project-qualified** key `"<project>::<key>"` into `(project, key)`,
627/// or `None` if it carries no `::` separator (a bare, within-repo key). A project
628/// name never contains `::`; a bare key may itself contain single colons (e.g.
629/// `sym:rust:…`), so only the **first** double-colon separates the project
630/// (ADR-0009).
631#[must_use]
632pub fn parse_qualified(key: &str) -> Option<(&str, &str)> {
633    key.split_once("::")
634        .filter(|(project, bare)| !project.is_empty() && !bare.is_empty())
635}
636
637/// The outcome of [`Workspace::follow_definition`]: where a cross-repo follow-hop
638/// lands.
639#[derive(Debug, Clone, PartialEq, Eq)]
640pub enum Follow {
641    /// Bridged past a `config_key` node to the hub **struct** that declares the
642    /// setting, carrying the specific named field that matched (e.g. the
643    /// `ServeConfig` struct for `serve.addr`, `field = "addr"`). The `node` is the
644    /// real struct node, so a caller can center it in the hub graph.
645    StructField {
646        /// The defining struct node (`sym:rust:<file>#<Struct>`).
647        node: Node,
648        /// The struct field the dotted key resolved to (its declared identifier).
649        field: String,
650    },
651    /// The resolved target node itself, unbridged — a `config_key` we could not map
652    /// to a struct with confidence (the safe fallback), or any non-config target a
653    /// spoke points straight at.
654    Node {
655        /// The resolved hub node.
656        node: Node,
657    },
658    /// The target is well-formed but its node is gone — cross-repo drift.
659    Drift,
660}
661
662/// Bridge a hub **`config_key`** node to the Rust **struct** that declares it, plus
663/// the specific field matched — the net-new step behind [`Workspace::follow_definition`].
664///
665/// The mapping from a dotted config key (`serve.addr`) to a defining Rust field is
666/// not recorded anywhere in the graph (the extractor models structs as nodes but
667/// not their fields as nodes, and a field's *type* is not captured), so this is a
668/// **resolve-time join** over two independent, deterministic signals — and it only
669/// bridges when they agree on exactly one struct:
670///
671/// 1. **section → struct name.** The dotted key's head segment (`serve`) must name
672///    the struct: its lower-cased name, with a trailing `Config` stripped, equals
673///    the section (`ServeConfig` → `serve`; a bare `Serve` also matches). See
674///    [`struct_matches_section`].
675/// 2. **field presence.** The struct must actually declare a field whose
676///    normalised name equals the key's leaf (`addr`, or `tls_cert` for
677///    `serve.tls_cert`) — read from the struct's `meta.fields`. See
678///    [`struct_field_matching`].
679///
680/// Requiring a **unique** `(struct, field)` hit is the correctness rule: a key that
681/// matches zero structs (no such section, or the field isn't declared) or more than
682/// one (genuinely ambiguous) returns `None`, and the caller falls back to the
683/// config-key node rather than risk jumping to a wrong definition.
684///
685/// Known limits (documented, deliberate): a single-segment key (no section, e.g.
686/// `port`) is never bridged; a key nested past one level (`serve.tls.cert` where
687/// `tls` is a sub-struct) won't match a flat field and falls back; and a struct
688/// whose name doesn't follow the `<Section>Config` convention won't be found. All
689/// three degrade to the existing config-key target — never to a wrong one.
690fn bridge_config_key(store: &Store, cfg_node: &Node) -> Result<Option<(Node, String)>, StoreError> {
691    // The dotted key: authoritative from `meta.key`, falling back to the node name
692    // (both are the dotted path in practice — see config-key extraction).
693    let dotted = cfg_node
694        .meta
695        .get("key")
696        .and_then(serde_json::Value::as_str)
697        .unwrap_or(cfg_node.name.as_str());
698    let Some((section, leaf)) = split_section_field(dotted) else {
699        return Ok(None);
700    };
701    let leaf_norm = crate::config_keys::normalize(leaf);
702    if leaf_norm.is_empty() {
703        return Ok(None);
704    }
705
706    // Fetch only the CANDIDATE struct(s) for this section by name, rather than
707    // loading and JSON-decoding every `struct` node in the graph on each hop
708    // (a latency spike on a large hub). `section_struct_names` yields the exact
709    // lower-cased names `struct_matches_section` would accept, so this narrows the
710    // scan without changing the bridging semantics; `struct_matches_section` is
711    // still applied below as the authoritative check.
712    let mut candidates: Vec<Node> = Vec::new();
713    for name in section_struct_names(section) {
714        candidates.extend(store.nodes_by_kind_named(&crate::NodeKind::Struct, &name)?);
715    }
716
717    let mut hits = candidates
718        .into_iter()
719        .filter(|s| struct_matches_section(&s.name, section))
720        .filter_map(|s| struct_field_matching(&s, &leaf_norm).map(|field| (s, field)));
721
722    match (hits.next(), hits.next()) {
723        // Exactly one confident match → bridge to it.
724        (Some(one), None) => Ok(Some(one)),
725        // Zero or ambiguous (>1) → fall back to the config-key node.
726        _ => Ok(None),
727    }
728}
729
730/// Split a dotted config key into `(section, leaf)` on its **first** separator:
731/// `serve.addr` → `("serve", "addr")`, `serve.tls_cert` → `("serve", "tls_cert")`.
732/// A single-segment key (`port`) has no section to identify a struct by, so it is
733/// `None` (never bridged).
734fn split_section_field(dotted: &str) -> Option<(&str, &str)> {
735    dotted
736        .split_once('.')
737        .filter(|(section, leaf)| !section.is_empty() && !leaf.is_empty())
738}
739
740/// The section's canonical form for name-matching: normalised, separators removed
741/// (`serve` → `serve`, `serve_mode` → `servemode`). Empty when the section carries
742/// no alphanumerics.
743fn section_key(section: &str) -> String {
744    crate::config_keys::normalize(section).replace('.', "")
745}
746
747/// The lower-cased struct names a config `section` can map to — exactly the names
748/// [`struct_matches_section`] accepts: `serve` → `["serve", "serveconfig"]`. Used
749/// to fetch just the candidate struct(s) by name instead of scanning them all
750/// (kept in lock-step with [`struct_matches_section`], which remains the check).
751fn section_struct_names(section: &str) -> Vec<String> {
752    let want = section_key(section);
753    if want.is_empty() {
754        return Vec::new();
755    }
756    let with_config = format!("{want}config");
757    vec![want, with_config]
758}
759
760/// Whether a struct `name` is the one a config `section` maps to: its lower-cased
761/// name with a trailing `config` stripped equals the section (case- and
762/// separator-insensitive). `ServeConfig`/`Serve` both match section `serve`;
763/// `ServeSettings` does not (so an unrelated struct is never bridged to).
764fn struct_matches_section(name: &str, section: &str) -> bool {
765    let lname = name.to_ascii_lowercase();
766    let base = lname.strip_suffix("config").unwrap_or(&lname);
767    let want = section_key(section);
768    !want.is_empty() && base == want
769}
770
771/// The struct field whose normalised identifier equals `leaf_norm`, read from the
772/// struct node's `meta.fields` (see extraction). Returns the field's original
773/// declared name (for display), or `None` when the struct declares no such field.
774fn struct_field_matching(struct_node: &Node, leaf_norm: &str) -> Option<String> {
775    struct_node
776        .meta
777        .get("fields")?
778        .as_array()?
779        .iter()
780        .filter_map(serde_json::Value::as_str)
781        .find(|field| crate::config_keys::normalize(field) == leaf_norm)
782        .map(ToOwned::to_owned)
783}
784
785/// Discover repos at `paths` into a `(name → Source, default)` registry: each
786/// path is git-discovered, named after its working-tree directory (deduped), and
787/// mapped to a lazily-opened `graph.db`. Exactly one repo ⇒ it is the default.
788type Registry = (BTreeMap<String, Source>, Option<String>);
789fn build_registry<I, P>(paths: I) -> Result<Registry, WorkspaceError>
790where
791    I: IntoIterator<Item = P>,
792    P: AsRef<Path>,
793{
794    let mut projects: BTreeMap<String, Source> = BTreeMap::new();
795    let mut seen_dbs: std::collections::HashSet<PathBuf> = std::collections::HashSet::new();
796    for path in paths {
797        let repo = Repo::discover(path.as_ref())?;
798        let db = repo.git_dir().join("roteiro").join("graph.db");
799        // De-duplicate the same repo reached via different paths (O(1) lookup, so
800        // discovery stays linear even on a big workspace and every reload).
801        if !seen_dbs.insert(db.clone()) {
802            continue;
803        }
804        let base = repo
805            .workdir()
806            .and_then(Path::file_name)
807            .map_or_else(|| "repo".to_owned(), |s| s.to_string_lossy().into_owned());
808        let name = dedupe_name(&projects, base);
809        projects.insert(
810            name,
811            Source::Path {
812                db,
813                // The repository's own root, so its own config can be read later.
814                root: repo.workdir().map(Path::to_path_buf),
815            },
816        );
817    }
818    if projects.is_empty() {
819        return Err(WorkspaceError::Empty);
820    }
821    let default = if projects.len() == 1 {
822        projects.keys().next().cloned()
823    } else {
824        None
825    };
826    Ok((projects, default))
827}
828
829/// Make `base` unique against the names already in `projects`, appending
830/// `-2`, `-3`, … on collision.
831fn dedupe_name(projects: &BTreeMap<String, Source>, base: String) -> String {
832    if !projects.contains_key(&base) {
833        return base;
834    }
835    let mut n = 2u32;
836    loop {
837        let candidate = format!("{base}-{n}");
838        if !projects.contains_key(&candidate) {
839            return candidate;
840        }
841        n += 1;
842    }
843}
844
845/// Shallow git-repo discovery under `root`: the root itself if it is a repo, plus
846/// each immediate subdirectory that is one, in sorted order. Shallow by design — a
847/// code directory holding sibling checkouts is the common case, and a deep scan
848/// would be slow and surprising. Shared by the CLI's workspace collection and
849/// [`WorkspaceSet`] / config resolution, so the membership rule lives in one place.
850///
851/// A repo is any directory containing a `.git` entry (a directory in a normal
852/// clone, a file in worktrees and submodules), so existence — not `is_dir` — is
853/// tested.
854///
855/// # Errors
856/// [`WorkspaceError::Discover`] if `root` cannot be read.
857pub fn discover_repos_under(root: &Path) -> Result<Vec<PathBuf>, WorkspaceError> {
858    let is_repo = |dir: &Path| dir.join(".git").exists();
859    let mut repos = Vec::new();
860    if is_repo(root) {
861        repos.push(root.to_path_buf());
862    }
863    let entries = std::fs::read_dir(root).map_err(|e| WorkspaceError::Discover {
864        root: root.to_path_buf(),
865        msg: e.to_string(),
866    })?;
867    let mut children: Vec<PathBuf> = entries
868        .filter_map(Result::ok)
869        .map(|e| e.path())
870        .filter(|p| p.is_dir() && is_repo(p))
871        .collect();
872    children.sort();
873    repos.extend(children);
874    Ok(repos)
875}
876
877/// A workspace group after config normalisation ([`crate::WorkspaceSet`] input): a
878/// name, its member `roots`/`repos` (unexpanded — discovered when the set is
879/// built), and whether its repos are cross-**linked** (served as one multi-repo
880/// graph) or **standalone** (each its own single-repo graph, no cross-repo links).
881///
882/// A `linked = false` (standalone) group denotes **exactly one** single-repo graph:
883/// the config normaliser emits one such group per discovered repo, and
884/// [`WorkspaceSet::from_resolved`] upholds the invariant by materialising a
885/// standalone group as a one-repo [`Workspace`] per member — a standalone group can
886/// never collapse several repos into one unlinked multi-repo graph.
887#[derive(Debug, Clone, PartialEq, Eq)]
888pub struct ResolvedWorkspace {
889    /// The workspace name (the `--workspace-name` selector).
890    pub name: String,
891    /// Directories to scan for member repos (as `[workspace] roots`).
892    pub roots: Vec<String>,
893    /// Explicit member repo paths, in addition to anything under `roots`.
894    pub repos: Vec<String>,
895    /// `true` ⇒ the repos form one linked graph; `false` ⇒ **standalone**: each
896    /// member repo is its own single-repo graph (no cross-repo links).
897    pub linked: bool,
898}
899
900/// One entry in a [`WorkspaceSet`]: a built [`Workspace`] plus whether its member
901/// repos are cross-linked. The workspace is held behind an `Arc` so an
902/// already-shared workspace (e.g. the one a `serve` process holds for its model
903/// tools and MCP router) can be wrapped into a set without re-opening its stores
904/// ([`WorkspaceSet::from_single`]).
905struct WorkspaceEntry {
906    /// The per-group workspace (one repo for a standalone singleton, several for a
907    /// linked group).
908    workspace: Arc<Workspace>,
909    /// Whether the group's repos are cross-linked.
910    linked: bool,
911}
912
913/// An install's **many** named workspaces: linked groups (multi-repo graphs) and
914/// standalone singletons (one-repo graphs), keyed by name in stable order (ADR-0008
915/// multi-workspace). The outer layer over [`Workspace`]: it selects *which*
916/// workspace a command operates on, then hands back that `Workspace` to resolve
917/// projects within it. Built from normalised config ([`WorkspaceSet::from_resolved`])
918/// so the `serve`/`links` selection logic is shared.
919pub struct WorkspaceSet {
920    /// Workspace name → its entry, in stable (`BTreeMap`) name order.
921    entries: BTreeMap<String, WorkspaceEntry>,
922    /// The workspace used when a selection omits a name (the sole workspace, if
923    /// there is exactly one; otherwise `None` and a bare selection is ambiguous).
924    default: Option<String>,
925}
926
927impl WorkspaceSet {
928    /// Assemble a set from pre-built named workspaces — the shared core of
929    /// [`WorkspaceSet::from_resolved`] and the test constructor. With exactly one
930    /// entry, that workspace is the default (a bare selection resolves to it).
931    #[must_use]
932    pub fn from_workspaces<I>(entries: I) -> Self
933    where
934        I: IntoIterator<Item = (String, Workspace, bool)>,
935    {
936        let entries: BTreeMap<String, WorkspaceEntry> = entries
937            .into_iter()
938            .map(|(name, workspace, linked)| {
939                (
940                    name,
941                    WorkspaceEntry {
942                        workspace: Arc::new(workspace),
943                        linked,
944                    },
945                )
946            })
947            .collect();
948        let default = (entries.len() == 1)
949            .then(|| entries.keys().next().cloned())
950            .flatten();
951        Self { entries, default }
952    }
953
954    /// Wrap an already-built [`Workspace`] (shared via `Arc`) as a one-entry set
955    /// under `name`, with `linked` recording whether that workspace is a
956    /// cross-linked multi-repo group. Used where a single `Workspace` is served as
957    /// the whole set — e.g. `roteiro serve` merges the read-only graph API over the
958    /// one workspace it already holds for its model tools and MCP router, so the
959    /// API's flat routes resolve to it as the sole (default) workspace. The store
960    /// handles are shared, never re-opened.
961    #[must_use]
962    pub fn from_single(name: impl Into<String>, workspace: Arc<Workspace>, linked: bool) -> Self {
963        let name = name.into();
964        let mut entries = BTreeMap::new();
965        entries.insert(name.clone(), WorkspaceEntry { workspace, linked });
966        Self {
967            entries,
968            default: Some(name),
969        }
970    }
971
972    /// Build a set from normalised config groups: each group's `roots`/`repos` are
973    /// discovered into member repo paths and opened as [`Workspace`]s. A **linked**
974    /// group becomes one multi-repo graph. A **standalone** (`linked = false`) group
975    /// becomes one single-repo graph **per member repo** — the invariant that a
976    /// standalone workspace is exactly one repo is upheld *here*, by splitting, so a
977    /// hand-built group can never collapse several repos into one unlinked multi-repo
978    /// graph (the config normaliser already emits standalone as per-repo singletons,
979    /// so in practice each such group has exactly one repo and the split is a no-op).
980    /// On a split, the extra members take a `-2`/`-3` suffix off the group name. A
981    /// group that resolves to **no** repos is skipped, so a stale root never aborts
982    /// the whole set.
983    ///
984    /// # Errors
985    /// [`WorkspaceError::Discover`] if a group's root cannot be read, or
986    /// [`WorkspaceError::Git`] if an explicit repo path is not inside a git repo.
987    pub fn from_resolved(resolved: Vec<ResolvedWorkspace>) -> Result<Self, WorkspaceError> {
988        let mut entries: BTreeMap<String, WorkspaceEntry> = BTreeMap::new();
989        for rw in resolved {
990            let mut paths: Vec<PathBuf> = Vec::new();
991            for root in &rw.roots {
992                paths.extend(discover_repos_under(Path::new(root))?);
993            }
994            for repo in &rw.repos {
995                paths.push(PathBuf::from(repo));
996            }
997            if paths.is_empty() {
998                // A group naming nothing (e.g. a `roots` dir with no repos) is
999                // simply absent rather than an error.
1000                continue;
1001            }
1002            if rw.linked {
1003                let workspace = Workspace::from_repo_paths(&paths)?;
1004                entries.insert(
1005                    rw.name.clone(),
1006                    WorkspaceEntry {
1007                        workspace: Arc::new(workspace),
1008                        linked: true,
1009                    },
1010                );
1011            } else {
1012                // Standalone: one single-repo graph per member, enforcing the
1013                // `linked = false` ⇒ exactly-one-repo invariant structurally (the
1014                // config normaliser already emits one repo per group, so this is a
1015                // no-op split there; it only matters if a group is hand-built).
1016                for (i, path) in paths.iter().enumerate() {
1017                    let workspace = Workspace::from_repo_paths([path])?;
1018                    let name = if i == 0 {
1019                        rw.name.clone()
1020                    } else {
1021                        format!("{}-{}", rw.name, i + 1)
1022                    };
1023                    entries.insert(
1024                        name,
1025                        WorkspaceEntry {
1026                            workspace: Arc::new(workspace),
1027                            linked: false,
1028                        },
1029                    );
1030                }
1031            }
1032        }
1033        let default = (entries.len() == 1)
1034            .then(|| entries.keys().next().cloned())
1035            .flatten();
1036        Ok(Self { entries, default })
1037    }
1038
1039    /// The configured workspace names, in stable order.
1040    #[must_use]
1041    pub fn names(&self) -> Vec<String> {
1042        self.entries.keys().cloned().collect()
1043    }
1044
1045    /// Each configured workspace as a `(name, shared handle)` pair, in stable name
1046    /// order. The `Arc<Workspace>` is the very handle the set holds, so a caller can
1047    /// build a **per-workspace** view — e.g. a tool registry confined to one
1048    /// workspace's projects — over the same lazily-opened stores, never re-opening
1049    /// them. Used by `serve` to scope the workspace-level Ask to the selected
1050    /// workspace (ADR-0008), mirroring how [`WorkspaceSet::select`] scopes the
1051    /// read-only `/v1/graph/workspaces/{ws}/…` routes.
1052    #[must_use]
1053    pub fn workspace_handles(&self) -> Vec<(String, Arc<Workspace>)> {
1054        self.entries
1055            .iter()
1056            .map(|(name, entry)| (name.clone(), entry.workspace.clone()))
1057            .collect()
1058    }
1059
1060    /// Whether workspace `name` is linked (`Some(true)`), standalone
1061    /// (`Some(false)`), or unknown (`None`).
1062    #[must_use]
1063    pub fn linked(&self, name: &str) -> Option<bool> {
1064        self.entries.get(name).map(|e| e.linked)
1065    }
1066
1067    /// Select a workspace by `name`, or the default when `name` is `None`.
1068    ///
1069    /// # Errors
1070    /// [`WorkspaceError::UnknownWorkspace`] if named but absent,
1071    /// [`WorkspaceError::AmbiguousWorkspace`] if omitted with several configured,
1072    /// or [`WorkspaceError::Empty`] if none are configured.
1073    pub fn select(&self, name: Option<&str>) -> Result<&Workspace, WorkspaceError> {
1074        if let Some(n) = name {
1075            return self
1076                .entries
1077                .get(n)
1078                .map(|e| e.workspace.as_ref())
1079                .ok_or_else(|| WorkspaceError::UnknownWorkspace {
1080                    name: n.to_owned(),
1081                    known: self.known(),
1082                });
1083        }
1084        // No name given: the sole workspace, else ambiguous / empty.
1085        let name = self.default.as_ref().ok_or_else(|| {
1086            if self.entries.is_empty() {
1087                WorkspaceError::Empty
1088            } else {
1089                WorkspaceError::AmbiguousWorkspace {
1090                    known: self.known(),
1091                }
1092            }
1093        })?;
1094        Ok(self.entries[name].workspace.as_ref())
1095    }
1096
1097    /// The **name** of the workspace [`WorkspaceSet::select`] resolves for `name`:
1098    /// the given name when present (and valid), else the sole/default workspace's
1099    /// name. Same resolution and errors as `select`, but returns the concrete name
1100    /// — so a caller (e.g. the `/follow` endpoint) can report which workspace it
1101    /// actually resolved in, even on a flat route where the default was implicit.
1102    ///
1103    /// # Errors
1104    /// As [`WorkspaceSet::select`].
1105    pub fn select_name(&self, name: Option<&str>) -> Result<&str, WorkspaceError> {
1106        if let Some(n) = name {
1107            return self
1108                .entries
1109                .get_key_value(n)
1110                .map(|(k, _)| k.as_str())
1111                .ok_or_else(|| WorkspaceError::UnknownWorkspace {
1112                    name: n.to_owned(),
1113                    known: self.known(),
1114                });
1115        }
1116        self.default.as_deref().ok_or_else(|| {
1117            if self.entries.is_empty() {
1118                WorkspaceError::Empty
1119            } else {
1120                WorkspaceError::AmbiguousWorkspace {
1121                    known: self.known(),
1122                }
1123            }
1124        })
1125    }
1126
1127    /// The name of the workspace whose member repos include the repo whose graph is
1128    /// `cwd_repo_db` (`<repo>/.git/roteiro/graph.db`), or `None` if no workspace
1129    /// contains it. Used to default `--workspace-name` to the workspace the current
1130    /// directory belongs to.
1131    #[must_use]
1132    pub fn containing(&self, cwd_repo_db: &Path) -> Option<&str> {
1133        self.entries.iter().find_map(|(name, e)| {
1134            e.workspace
1135                .member_dbs()
1136                .iter()
1137                .any(|db| db == cwd_repo_db)
1138                .then_some(name.as_str())
1139        })
1140    }
1141
1142    /// Comma-separated workspace names (for error messages).
1143    fn known(&self) -> String {
1144        self.entries.keys().cloned().collect::<Vec<_>>().join(", ")
1145    }
1146}
1147
1148#[cfg(test)]
1149mod tests {
1150    use super::*;
1151    use crate::store::Store;
1152
1153    fn store() -> Store {
1154        Store::open_in_memory().expect("in-memory store")
1155    }
1156
1157    #[test]
1158    fn single_project_is_the_default_and_resolves_bare() {
1159        let ws = Workspace::single("myrepo", store());
1160        assert_eq!(ws.names(), vec!["myrepo".to_owned()]);
1161        assert!(!ws.is_multi());
1162        // A bare call resolves to the sole project.
1163        assert_eq!(ws.resolve(None).unwrap(), "myrepo");
1164        // Naming it explicitly works too.
1165        assert_eq!(ws.resolve(Some("myrepo")).unwrap(), "myrepo");
1166        // with_store hands over the store.
1167        let n = ws.with_store(None, |s| s.node_count().unwrap()).unwrap();
1168        assert_eq!(n, 0);
1169    }
1170
1171    #[test]
1172    fn from_stores_dedupes_colliding_names() {
1173        // Two stores sharing the base name `repo` must both survive: the second
1174        // is suffixed `repo-2` (like `from_repo_paths`), never dropped.
1175        let ws = Workspace::from_stores([("repo", store()), ("repo", store())]);
1176        let mut names = ws.names();
1177        names.sort();
1178        assert_eq!(names, vec!["repo".to_owned(), "repo-2".to_owned()]);
1179        assert!(ws.is_multi());
1180    }
1181
1182    #[test]
1183    fn unknown_project_is_an_error_naming_the_known_ones() {
1184        let ws = Workspace::single("a", store());
1185        let err = ws.resolve(Some("b")).unwrap_err();
1186        assert!(matches!(err, WorkspaceError::UnknownProject { .. }));
1187        assert!(err.to_string().contains("known: a"));
1188    }
1189
1190    #[test]
1191    fn cached_store_handle_is_reused() {
1192        let ws = Workspace::single("a", store());
1193        // Two accesses return the same underlying handle (cache hit).
1194        ws.with_store(None, |s| s.node_count().unwrap()).unwrap();
1195        let again = ws.handle("a").unwrap();
1196        // The handle is held by both the cache and this local, so ≥ 2.
1197        assert!(Arc::strong_count(&again) >= 2);
1198    }
1199
1200    #[test]
1201    fn parse_qualified_splits_on_the_first_double_colon_only() {
1202        // Bare keys carry single colons; only `::` separates the project.
1203        assert_eq!(
1204            parse_qualified("app::sym:rust:a.rs#B"),
1205            Some(("app", "sym:rust:a.rs#B"))
1206        );
1207        assert_eq!(parse_qualified("app::file:x"), Some(("app", "file:x")));
1208        // Not qualified / malformed.
1209        assert_eq!(parse_qualified("sym:rust:a.rs#B"), None);
1210        assert_eq!(parse_qualified("::x"), None);
1211        assert_eq!(parse_qualified("app::"), None);
1212    }
1213
1214    #[test]
1215    fn resolve_qualified_finds_drift_and_bad_targets() {
1216        use crate::model::{Node, NodeKind};
1217        let mut s = store();
1218        s.apply_factset(&crate::model::FactSet::new().with_node(Node::new(
1219            "file:cfg.rs",
1220            NodeKind::File,
1221            "cfg.rs",
1222        )))
1223        .unwrap();
1224        let ws = Workspace::single("app", s);
1225
1226        // Resolves an existing node in the named project.
1227        let hit = ws.resolve_qualified("app::file:cfg.rs").unwrap();
1228        assert_eq!(hit.map(|n| n.key), Some("file:cfg.rs".to_owned()));
1229        // Well-formed but absent → drift (Ok(None)).
1230        assert!(ws.resolve_qualified("app::file:gone.rs").unwrap().is_none());
1231        // Unknown target project → an error the caller reports as drift.
1232        assert!(matches!(
1233            ws.resolve_qualified("ghost::file:x").unwrap_err(),
1234            WorkspaceError::UnknownProject { .. }
1235        ));
1236        // Not project-qualified at all.
1237        assert!(matches!(
1238            ws.resolve_qualified("file:cfg.rs").unwrap_err(),
1239            WorkspaceError::Unqualified { .. }
1240        ));
1241    }
1242
1243    #[test]
1244    fn follow_external_ref_walks_a_placeholder_to_its_target() {
1245        use crate::links::external_ref_node;
1246        use crate::model::{Node, NodeKind};
1247        let mut s = store();
1248        // A real target node, plus a placeholder standing in for it (as it would
1249        // live in a spoke store pointing back at this project).
1250        s.apply_factset(&crate::model::FactSet::new().with_node(Node::new(
1251            "file:cfg.rs",
1252            NodeKind::File,
1253            "cfg.rs",
1254        )))
1255        .unwrap();
1256        let ws = Workspace::single("app", s);
1257
1258        // Following the placeholder resolves the qualified target to the real node.
1259        let placeholder = external_ref_node("app::file:cfg.rs");
1260        let hit = ws.follow_external_ref(&placeholder).unwrap();
1261        assert_eq!(hit.map(|n| n.key), Some("file:cfg.rs".to_owned()));
1262
1263        // A placeholder for a removed target is drift (Ok(None)), not an error.
1264        let gone = external_ref_node("app::file:gone.rs");
1265        assert!(ws.follow_external_ref(&gone).unwrap().is_none());
1266
1267        // A plain (non-external-ref) node is simply not followed.
1268        let plain = Node::new("file:cfg.rs", NodeKind::File, "cfg.rs");
1269        assert!(ws.follow_external_ref(&plain).unwrap().is_none());
1270    }
1271
1272    // -- follow-the-link hop: config_key → struct bridge ------------------
1273
1274    /// A config-key node as extraction emits it: key `cfgkey:<file>#<dotted>`,
1275    /// name the dotted key, `meta { key, value }`.
1276    fn cfg_node(dotted: &str) -> crate::model::Node {
1277        use crate::model::{Node, NodeKind};
1278        let mut n = Node::new(
1279            format!("cfgkey:config.toml#{dotted}"),
1280            NodeKind::Other("config_key".to_owned()),
1281            dotted,
1282        );
1283        n.meta = serde_json::json!({ "key": dotted, "value": "x" });
1284        n
1285    }
1286
1287    /// A struct node as extraction emits it, carrying its declared field names in
1288    /// `meta.fields` (the bridge's join signal).
1289    fn struct_node(name: &str, fields: &[&str]) -> crate::model::Node {
1290        use crate::model::{Node, NodeKind};
1291        let mut n = Node::new(format!("sym:rust:config.rs#{name}"), NodeKind::Struct, name);
1292        n.meta = serde_json::json!({ "fields": fields });
1293        n
1294    }
1295
1296    /// Build a hub with a `ServeConfig`/`addr` struct field AND its `serve.addr`
1297    /// config key — plus decoys — so the bridge's confidence rules are exercised.
1298    fn bridge_hub() -> Workspace {
1299        use crate::model::FactSet;
1300        let mut s = store();
1301        s.apply_factset(
1302            &FactSet::new()
1303                .with_node(struct_node("ServeConfig", &["addr", "tools", "tls_cert"]))
1304                .with_node(struct_node("ModelsConfig", &["embedding", "generative"]))
1305                .with_node(cfg_node("serve.addr"))
1306                .with_node(cfg_node("serve.tls_cert"))
1307                .with_node(cfg_node("serve.ghost")) // resolves, but no such field
1308                .with_node(cfg_node("mystery.addr")) // no struct for section `mystery`
1309                .with_node(cfg_node("port")), // single-segment: no section
1310        )
1311        .unwrap();
1312        Workspace::single("hub", s)
1313    }
1314
1315    #[test]
1316    fn follow_bridges_config_key_to_its_defining_struct_field() {
1317        let ws = bridge_hub();
1318        // `serve.addr` bridges to the `ServeConfig` struct, field `addr`.
1319        match ws
1320            .follow_definition("hub::cfgkey:config.toml#serve.addr")
1321            .unwrap()
1322        {
1323            Follow::StructField { node, field } => {
1324                assert_eq!(node.key, "sym:rust:config.rs#ServeConfig");
1325                assert_eq!(field, "addr");
1326            }
1327            other => panic!("expected a struct-field bridge, got {other:?}"),
1328        }
1329        // Separator-insensitive on the leaf: `serve.tls_cert` → field `tls_cert`.
1330        match ws
1331            .follow_definition("hub::cfgkey:config.toml#serve.tls_cert")
1332            .unwrap()
1333        {
1334            Follow::StructField { node, field } => {
1335                assert_eq!(node.key, "sym:rust:config.rs#ServeConfig");
1336                assert_eq!(field, "tls_cert");
1337            }
1338            other => panic!("expected a struct-field bridge, got {other:?}"),
1339        }
1340    }
1341
1342    #[test]
1343    fn follow_falls_back_to_config_key_when_not_confidently_bridgeable() {
1344        let ws = bridge_hub();
1345        // Section matches a struct, but the struct has no such field → fall back.
1346        let ghost = ws
1347            .follow_definition("hub::cfgkey:config.toml#serve.ghost")
1348            .unwrap();
1349        assert!(
1350            matches!(&ghost, Follow::Node { node } if node.name == "serve.ghost"),
1351            "unmatched field falls back to the config_key node, got {ghost:?}"
1352        );
1353        // No struct maps to section `mystery` → fall back.
1354        let mystery = ws
1355            .follow_definition("hub::cfgkey:config.toml#mystery.addr")
1356            .unwrap();
1357        assert!(matches!(&mystery, Follow::Node { node } if node.name == "mystery.addr"));
1358        // A single-segment key names no section → never bridged.
1359        let port = ws
1360            .follow_definition("hub::cfgkey:config.toml#port")
1361            .unwrap();
1362        assert!(matches!(&port, Follow::Node { node } if node.name == "port"));
1363    }
1364
1365    #[test]
1366    fn follow_does_not_bridge_on_ambiguity() {
1367        use crate::model::FactSet;
1368        // TWO structs both map to section `serve` and both declare `addr` — a
1369        // genuinely ambiguous mapping must fall back, never guess a wrong node.
1370        let mut s = store();
1371        s.apply_factset(
1372            &FactSet::new()
1373                .with_node(struct_node("ServeConfig", &["addr"]))
1374                .with_node(struct_node("Serve", &["addr"])) // also matches `serve`
1375                .with_node(cfg_node("serve.addr")),
1376        )
1377        .unwrap();
1378        let ws = Workspace::single("hub", s);
1379        let out = ws
1380            .follow_definition("hub::cfgkey:config.toml#serve.addr")
1381            .unwrap();
1382        assert!(
1383            matches!(&out, Follow::Node { node } if node.name == "serve.addr"),
1384            "ambiguous (two matching structs) falls back, got {out:?}"
1385        );
1386    }
1387
1388    #[test]
1389    fn follow_narrow_lookup_ignores_unrelated_structs_with_the_same_field() {
1390        use crate::model::FactSet;
1391        // The name-narrowed struct lookup must return exactly what a full scan
1392        // would: an unrelated struct that happens to declare `addr` is NOT the
1393        // `serve` section's struct, so `serve.addr` still bridges only to
1394        // `ServeConfig` — proving the narrowing preserves bridging semantics.
1395        let mut s = store();
1396        s.apply_factset(
1397            &FactSet::new()
1398                .with_node(struct_node("ServeConfig", &["addr"]))
1399                .with_node(struct_node("Unrelated", &["addr"]))
1400                .with_node(struct_node("Widget", &["addr", "size"]))
1401                .with_node(struct_node("ModelsConfig", &["embedding"]))
1402                .with_node(cfg_node("serve.addr")),
1403        )
1404        .unwrap();
1405        let ws = Workspace::single("hub", s);
1406        match ws
1407            .follow_definition("hub::cfgkey:config.toml#serve.addr")
1408            .unwrap()
1409        {
1410            Follow::StructField { node, field } => {
1411                assert_eq!(node.key, "sym:rust:config.rs#ServeConfig");
1412                assert_eq!(field, "addr");
1413            }
1414            other => panic!("expected a struct-field bridge to ServeConfig, got {other:?}"),
1415        }
1416    }
1417
1418    #[test]
1419    fn follow_reports_drift_and_passes_through_non_config_targets() {
1420        use crate::model::{FactSet, Node, NodeKind};
1421        let mut s = store();
1422        s.apply_factset(&FactSet::new().with_node(Node::new(
1423            "sym:rust:a.rs#Thing",
1424            NodeKind::Struct,
1425            "Thing",
1426        )))
1427        .unwrap();
1428        let ws = Workspace::single("hub", s);
1429        // A well-formed target whose node is gone → drift.
1430        assert_eq!(
1431            ws.follow_definition("hub::cfgkey:config.toml#gone")
1432                .unwrap(),
1433            Follow::Drift
1434        );
1435        // A spoke pointing straight at a symbol (an authored link, not a config
1436        // key) passes the node through unbridged.
1437        match ws.follow_definition("hub::sym:rust:a.rs#Thing").unwrap() {
1438            Follow::Node { node } => assert_eq!(node.key, "sym:rust:a.rs#Thing"),
1439            other => panic!("expected pass-through, got {other:?}"),
1440        }
1441    }
1442
1443    #[test]
1444    fn workspace_set_select_single_ambiguous_and_unknown() {
1445        // One workspace ⇒ the default; a bare or named select both resolve to it.
1446        let one = WorkspaceSet::from_workspaces([(
1447            "only".to_owned(),
1448            Workspace::single("only", store()),
1449            true,
1450        )]);
1451        assert_eq!(one.names(), vec!["only".to_owned()]);
1452        assert_eq!(one.linked("only"), Some(true));
1453        assert!(one.linked("nope").is_none());
1454        assert!(one.select(None).is_ok());
1455        assert!(one.select(Some("only")).is_ok());
1456        assert!(matches!(
1457            one.select(Some("ghost")),
1458            Err(WorkspaceError::UnknownWorkspace { .. })
1459        ));
1460
1461        // Several workspaces ⇒ a bare select is ambiguous (listing the names), a
1462        // named select works, and an unknown name errors.
1463        let many = WorkspaceSet::from_workspaces([
1464            ("api".to_owned(), Workspace::single("api", store()), true),
1465            ("web".to_owned(), Workspace::single("web", store()), false),
1466        ]);
1467        assert_eq!(many.names(), vec!["api".to_owned(), "web".to_owned()]);
1468        assert_eq!(many.linked("web"), Some(false));
1469        // (`select` yields `&Workspace`, which isn't `Debug`, so match the error
1470        // out rather than `unwrap_err`.)
1471        let Err(err) = many.select(None) else {
1472            panic!("a bare select over several workspaces must be ambiguous");
1473        };
1474        assert!(matches!(err, WorkspaceError::AmbiguousWorkspace { .. }));
1475        assert!(err.to_string().contains("api"));
1476        assert!(err.to_string().contains("web"));
1477        assert!(many.select(Some("web")).is_ok());
1478        assert!(matches!(
1479            many.select(Some("ghost")),
1480            Err(WorkspaceError::UnknownWorkspace { .. })
1481        ));
1482
1483        // No workspaces ⇒ a bare select reports the empty set.
1484        let none = WorkspaceSet::from_workspaces(std::iter::empty());
1485        assert!(matches!(none.select(None), Err(WorkspaceError::Empty)));
1486    }
1487
1488    #[test]
1489    fn workspace_set_containing_finds_the_owning_workspace_by_db_path() {
1490        // Build two workspaces from explicit (name, graph.db) pairs — no git needed
1491        // — so `containing` can match a repo's db against each workspace's members.
1492        let api_db = PathBuf::from("/ws/api/svc/.git/roteiro/graph.db");
1493        let web_db = PathBuf::from("/ws/web/app/.git/roteiro/graph.db");
1494        let set = WorkspaceSet::from_workspaces([
1495            (
1496                "api".to_owned(),
1497                Workspace::from_named_dbs([("svc".to_owned(), api_db.clone())]),
1498                true,
1499            ),
1500            (
1501                "web".to_owned(),
1502                Workspace::from_named_dbs([("app".to_owned(), web_db.clone())]),
1503                false,
1504            ),
1505        ]);
1506        assert_eq!(set.containing(&api_db), Some("api"));
1507        assert_eq!(set.containing(&web_db), Some("web"));
1508        // A db in no workspace matches nothing.
1509        assert_eq!(
1510            set.containing(Path::new("/elsewhere/.git/roteiro/graph.db")),
1511            None
1512        );
1513    }
1514}