Skip to main content

prov_graph/graph/
census.rs

1//! The census types, the spanning-tree walker that fills them in, and the
2//! reachability views built over the result. See the module doc at
3//! [`crate::graph`] for why the census is ground truth.
4
5use std::collections::{BTreeMap, BTreeSet};
6use std::fmt;
7use std::ops::Range;
8use std::path::{Path, PathBuf};
9
10use super::Graph;
11use crate::error::Result;
12use crate::fs::ReadStorage;
13use crate::identity::{self, Id};
14use crate::index::IdIndex;
15use crate::link::{self, Link};
16use crate::title::{self, TitleIndex, TitleMatch};
17
18use super::Target;
19
20/// Where in a document a forward link is written — a frontmatter relation
21/// field or a body wikilink. Carried by every link-resolution finding
22/// (`prov`'s `Finding`, derived in `validate` — see
23/// [`StructuralFact`]) and every [`CensusEntry`] so a report can point at the
24/// exact site.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub enum LinkSite {
27    /// A frontmatter relation field, by name (e.g. `contents`, `links`).
28    Relation(String),
29    /// A `[[…]]` wikilink in the body, at this byte span.
30    Body(Range<usize>),
31}
32
33impl fmt::Display for LinkSite {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            LinkSite::Relation(name) => f.write_str(name),
37            LinkSite::Body(_) => f.write_str("body"),
38        }
39    }
40}
41
42/// How a forward link resolves against the workspace. Path and id forms stay
43/// distinct on purpose: the registry owns id resolution (location-independent,
44/// stable across moves), while a path is checked against the on-disk name — so
45/// a caller can tell which links a rename must rewrite (paths) from which it
46/// must leave alone (ids).
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum Resolution {
49    /// A path target that resolves to an existing file (exact name).
50    Path(PathBuf),
51    /// A path target that only matches case-insensitively; `got` is the target
52    /// as resolved, `actual` the exact on-disk name.
53    CaseMismatch { got: PathBuf, actual: String },
54    /// A path target with nothing on disk.
55    Broken,
56    /// A `prov:<id>` target the registry resolves to the live path `to`.
57    Id { id: Id, to: PathBuf },
58    /// A well-formed `prov:<id>` target with no live registry entry;
59    /// `tombstoned` separates "deleted" from "never issued here" (§4 hazard).
60    DanglingId { id: Id, tombstoned: bool },
61    /// A `prov:<id>` target failing its check character — a typo.
62    MalformedId,
63    /// A nominal (alias) target several documents claim — unresolvable.
64    /// `candidates` are the sharers, sorted.
65    AmbiguousAlias {
66        name: String,
67        candidates: Vec<PathBuf>,
68    },
69    /// A URL / mail address — off-workspace, never resolved or rewritten.
70    External,
71    /// An `id:<workspace>/<id>` target naming a document in another workspace.
72    ///
73    /// A clean resolution, not a finding: prov holds no map from a workspace
74    /// name to a location (see
75    /// [`Target::Foreign`]), so it has no
76    /// evidence either way about whether the target exists. Reporting a link it
77    /// cannot check as broken would be a false positive every host would then
78    /// have to suppress — and a `check` that must be filtered is one nobody
79    /// reads. The id is deliberately **not** check-verified: the foreign
80    /// workspace owns its id space and need not be a prov workspace.
81    Foreign { workspace: String, id: Id },
82}
83
84impl Resolution {
85    /// The workspace path this link reaches, if it resolves to one (by path or
86    /// through the registry) — what the spanning walk descends into and what a
87    /// backlink map keys on. `None` for broken, dangling, malformed, external.
88    pub fn resolved_path(&self) -> Option<&PathBuf> {
89        match self {
90            Resolution::Path(p)
91            | Resolution::CaseMismatch { got: p, .. }
92            | Resolution::Id { to: p, .. } => Some(p),
93            _ => None,
94        }
95    }
96}
97
98/// One forward link as found in a document: where it is written and how it
99/// resolves. The unit of the
100/// [`census`](Graph::census).
101#[derive(Debug, Clone, PartialEq, Eq)]
102pub struct CensusEntry {
103    /// The document that declares the link (workspace-relative).
104    pub source: PathBuf,
105    /// Where in `source` the link is written.
106    pub site: LinkSite,
107    /// The target exactly as written (bare — the `[label](…)` wrapper stripped).
108    pub target_text: String,
109    /// The display label the link carried, when written `[label](target)` /
110    /// `[[target|label]]` — `None` for a bare target. Kept so a caller can check
111    /// a label against the target's current title (stale-label detection) without
112    /// re-reading the source.
113    pub label: Option<String>,
114    /// How the target resolves.
115    pub resolution: Resolution,
116}
117
118/// An inbound reference to a document, as discovered by the census: which
119/// document links here ([`source`](Backlink::source)), where in it
120/// ([`site`](Backlink::site)), and whether the link is by stable id (survives
121/// moves) or by path (rewritten on a move). The inverse of a forward
122/// [`CensusEntry`] — the marquee payoff of the identity layer (DESIGN §6).
123#[derive(Debug, Clone, PartialEq, Eq)]
124pub struct Backlink {
125    /// The document that links to the target.
126    pub source: PathBuf,
127    /// Where in `source` the link is written.
128    pub site: LinkSite,
129    /// `true` when the link is a `prov:<id>` reference (location-independent),
130    /// `false` when it is a path.
131    pub by_id: bool,
132}
133
134enum NameMatch {
135    Exact,
136    CaseOnly(String),
137    None,
138}
139
140/// A structural observation the walk makes as it traverses — not a verdict,
141/// just what it saw: a document that would not load, a self-stored id
142/// disagreeing with (or absent from) the registry, a spanning edge that
143/// revisits an already-reached node, a spanning child whose inverse field
144/// does not point back, or a `content` pointer that failed to resolve.
145///
146/// These are facts about *traversal state* — they need the queue, the
147/// visited set, the inverse lookup — so only the walk can raise them; a
148/// single [`CensusEntry`]'s [`Resolution`] is not enough (that half of the
149/// story is `validate`'s [`CensusEntry`]-keyed
150/// `prov`'s `validate` instead, since a resolution *is* already the
151/// fact). `validate::check` turns each variant here into the
152/// `prov`'s `Finding` that names it — one to one, since
153/// the walk already knows exactly what happened and there is nothing left to
154/// infer. Keeping the enum here rather than importing `Finding` is what
155/// keeps `graph` a pure "plain text → walkable graph" layer: it reports what
156/// it found, never how that should be judged.
157#[derive(Debug, Clone, PartialEq, Eq)]
158pub enum StructuralFact {
159    /// A document that exists but could not be read or parsed.
160    Unreadable { doc: PathBuf, error: String },
161    /// A document's self-stored `id` frontmatter disagrees with the registry
162    /// (or claims an id the registry hands to a different document).
163    /// `registry` is `None` when the registry has no record of the path at
164    /// all under this id.
165    IdMismatch {
166        doc: PathBuf,
167        frontmatter: Id,
168        registry: Option<Id>,
169    },
170    /// A document carries a self-stored `id` the registry has no record of.
171    UnregisteredId { doc: PathBuf, frontmatter: Id },
172    /// A stamping workspace's registered document does not carry its own
173    /// `id` frontmatter.
174    UnstampedId { doc: PathBuf, registry: Id },
175    /// A spanning target already reached by the walk — a cycle or a second
176    /// parent.
177    DuplicateContainment { doc: PathBuf, target: String },
178    /// A spanning child whose inverse field does not link back to `doc`.
179    MissingInverse {
180        doc: PathBuf,
181        child: PathBuf,
182        inverse: String,
183    },
184    /// A `content` pointer resolving only case-insensitively.
185    CaseMismatch {
186        doc: PathBuf,
187        site: LinkSite,
188        target: String,
189        actual: String,
190    },
191    /// A `content` pointer resolving to nothing on disk.
192    BrokenLink {
193        doc: PathBuf,
194        site: LinkSite,
195        target: String,
196    },
197    /// A node declaring both `content` and `manifest` — a sidecar for one
198    /// payload and for a whole directory at once. The two are mutually
199    /// exclusive ([`crate::manifest`]), and neither reading is safe to pick.
200    ManifestConflict { doc: PathBuf },
201}
202
203/// The result of one spanning-tree
204/// [`walk`](Graph::census): the forward-link census,
205/// the structural facts observed from traversal state, and the prose body
206/// files reached through separated nodes' `content` pointers (tracked for
207/// the orphan check, deliberately absent from the census).
208pub struct Walk {
209    pub census: Vec<CensusEntry>,
210    pub facts: Vec<StructuralFact>,
211    pub content_bodies: Vec<PathBuf>,
212}
213
214/// The set of workspace-relative paths a walk from `start` reaches: `start`
215/// itself, every path a census link resolves to (any relation, a body wikilink,
216/// or an id through the registry), and every `content` target.
217///
218/// A **case-mismatched** link counts its *actual* on-disk file as reached, so a
219/// file is never both case-mismatched and orphaned. Prose bodies (and attachment
220/// payloads) arrive through `content_bodies` rather than the census, because a
221/// `content` pointer is not a graph edge — but it does reach a file, which is
222/// what every caller here cares about.
223///
224/// The one definition of "reachable" that the orphan check, the fixity pass, the
225/// vocabulary pass, and the history capture set all share (DESIGN §8).
226pub fn reachable_set(
227    start: &Path,
228    census: &[CensusEntry],
229    content_bodies: &[PathBuf],
230) -> BTreeSet<PathBuf> {
231    let mut reachable: BTreeSet<PathBuf> = BTreeSet::new();
232    reachable.insert(link::normalize(start));
233    reachable.extend(content_bodies.iter().cloned());
234    for entry in census {
235        match &entry.resolution {
236            Resolution::Path(p) | Resolution::Id { to: p, .. } => {
237                reachable.insert(p.clone());
238            }
239            Resolution::CaseMismatch { got, actual } => {
240                reachable.insert(got.with_file_name(actual));
241            }
242            _ => {}
243        }
244    }
245    reachable
246}
247
248impl<FS: ReadStorage, Ix: IdIndex> Graph<FS, Ix> {
249    /// [`reachable_set`], minus any **shadowed attachment payload**
250    /// (`attach --opaque`) — the population a pass may parse *as a document*.
251    ///
252    /// A shadowed payload is still reachable (it must not be reported as an
253    /// orphan, and it is still fixity-checked *through its sidecar*), but its
254    /// bytes are an exhibit prov promised never to interpret. That is the same
255    /// bound [`is_shadowed_payload`](Graph::is_shadowed_payload) already
256    /// holds the flat title and id scans to; this is its reachability-walk
257    /// counterpart, for `prov`'s `vocabulary_findings` and
258    /// `prov`'s `fixity_findings` — the two passes that load
259    /// every reachable path and read its frontmatter.
260    ///
261    /// The listing `is_shadowed_payload` needs is built the same way
262    /// `prov`'s `orphans` builds one: the direct children of every
263    /// directory the reachable set occupies, so a shadow check costs a set
264    /// lookup per candidate extension rather than a stat.
265    pub async fn reachable_documents(
266        &self,
267        start: &Path,
268        census: &[CensusEntry],
269        content_bodies: &[PathBuf],
270    ) -> Result<BTreeSet<PathBuf>> {
271        let reachable = reachable_set(start, census, content_bodies);
272        let reached_dirs = Self::reached_dirs(&reachable);
273        let listing: BTreeSet<PathBuf> = self
274            .direct_child_files(&reached_dirs)
275            .await?
276            .into_iter()
277            .collect();
278        let mut documents = BTreeSet::new();
279        for path in reachable {
280            if !self.is_shadowed_payload(&path, &listing).await {
281                documents.insert(path);
282            }
283        }
284        Ok(documents)
285    }
286
287    /// Every file the workspace reaches from `start` that actually exists on
288    /// disk — [`reachable_set`] over a fresh walk, filtered to real files.
289    ///
290    /// This is §8's bounded walk expressed as a *file set* rather than a findings
291    /// list: the same population `check` validates. `prov`'s `Workspace::history_capture` captures
292    /// it (minus prov's two byte-parking stores) precisely so that an event is a
293    /// consistent cut across everything the workspace considers its own.
294    ///
295    /// `prov`'s `Workspace::history_capture`: `prov`'s `Workspace::history_capture`
296    pub async fn reachable_files(&self, start: impl AsRef<Path>) -> Result<BTreeSet<PathBuf>> {
297        self.reachable_files_within(start, &[]).await
298    }
299
300    /// [`reachable_files`](Self::reachable_files), told which directories are
301    /// parked — see [`title_index_scoped`](Self::title_index_scoped).
302    pub async fn reachable_files_within(
303        &self,
304        start: impl AsRef<Path>,
305        parked: &[PathBuf],
306    ) -> Result<BTreeSet<PathBuf>> {
307        let start = link::normalize(start);
308        let Walk {
309            census,
310            content_bodies,
311            ..
312        } = self.walk(&start, parked).await?;
313        let mut files = BTreeSet::new();
314        for path in reachable_set(&start, &census, &content_bodies) {
315            if self.fs().try_exists(&self.root().join(&path)).await? {
316                files.insert(path);
317            }
318        }
319        Ok(files)
320    }
321
322    /// Take a census of every forward link reachable from `start`: one
323    /// [`CensusEntry`] per frontmatter relation edge *and* per body `[[…]]`
324    /// wikilink, each carrying its [`LinkSite`] and [`Resolution`].
325    ///
326    /// This is the one traversal the backlink map, the integrity findings, and
327    /// (via `mutate`) inbound-rename maintenance are all views over. Because it
328    /// is read from the documents, it is ground truth: a stored backlink index
329    /// heals *toward* the census, never the reverse.
330    pub async fn census(&self, start: impl AsRef<Path>) -> Result<Vec<CensusEntry>> {
331        self.census_within(start, &[]).await
332    }
333
334    /// [`census`](Self::census), told which directories are parked — see
335    /// [`title_index_scoped`](Self::title_index_scoped).
336    pub async fn census_within(
337        &self,
338        start: impl AsRef<Path>,
339        parked: &[PathBuf],
340    ) -> Result<Vec<CensusEntry>> {
341        Ok(self.walk(start.as_ref(), parked).await?.census)
342    }
343
344    /// The backlink map for the workspace reachable from `start`: every resolved
345    /// target to the inbound references ([`Backlink`]s) that reach it, path- and
346    /// id-form alike. This is the census inverted — recomputed from the
347    /// documents, so it is always fresh (the Route-N "reconcile-on-load": no
348    /// stored index to drift). Each target's backlinks are sorted by source.
349    pub async fn backlinks(
350        &self,
351        start: impl AsRef<Path>,
352    ) -> Result<BTreeMap<PathBuf, Vec<Backlink>>> {
353        Ok(invert(self.census(start).await?))
354    }
355
356    /// The inbound references to a single `target` (workspace-relative) reachable
357    /// from `start`, sorted by source. The focused form of
358    /// [`backlinks`](Self::backlinks) for "who links here?".
359    pub async fn backlinks_to(
360        &self,
361        start: impl AsRef<Path>,
362        target: impl AsRef<Path>,
363    ) -> Result<Vec<Backlink>> {
364        Ok(inbound(self.census(start).await?, target.as_ref()))
365    }
366
367    /// The shared spanning-tree walk: gathers the forward-link census and the
368    /// structural facts ([`StructuralFact`], which depend on traversal state,
369    /// not on a single link's resolution) in one pass. Frontmatter edges may
370    /// be spanning and so drive descent, the single-parent check, and the
371    /// inverse check; body wikilinks are always overlay references —
372    /// censused, never spanning.
373    ///
374    /// "One pass" describes what it *reports*, not how many times it opens a
375    /// file: descent reads each document, the inverse check reads every spanning
376    /// child again to see whether it points back, and a workspace using
377    /// `[[alias]]` links pays a third read per document for the title index.
378    /// Three reads of everything, for one walk. So the walk opens a scope of its
379    /// own rather than waiting to be given one — a caller with no interest in
380    /// memos still gets a walk that reads each document once, and a caller that
381    /// already opened one (`check`, a `mutate` verb) nests inside it and keeps
382    /// everything the walk read.
383    pub async fn walk(&self, start: &Path, parked: &[PathBuf]) -> Result<Walk> {
384        let _scope = self.read_scope();
385        let mut census = Vec::new();
386        let mut structural = Vec::new();
387        // Prose bodies reached through a separated node's `content` pointer.
388        // Kept out of the census (not a graph edge), but tracked so the orphan
389        // check does not mistake a linked body file for an unlinked one.
390        let mut content_bodies = Vec::new();
391        let mut visited = BTreeSet::new();
392        let mut queue = vec![link::normalize(start)];
393
394        // The nominal-resolution index, built lazily — only if a `[[alias]]` link
395        // is actually encountered. A path/id workspace never scans (which, at the
396        // root of a larger repo, would read every file under `target/`, vendored
397        // trees, and the rest — the reported multi-second `tree`/`check`).
398        let mut titles: Option<TitleIndex> = None;
399
400        let spanning = self.relations().spanning_relation().map(str::to_owned);
401        let inverse = spanning.as_deref().and_then(|s| {
402            self.relations()
403                .relations()
404                .iter()
405                .find(|r| r.name == s)
406                .and_then(|r| r.inverse.clone())
407        });
408
409        while let Some(path) = queue.pop() {
410            if !visited.insert(path.clone()) {
411                continue;
412            }
413            let doc = match self.load(&path).await {
414                Ok((_, doc)) => doc,
415                Err(e) => {
416                    structural.push(StructuralFact::Unreadable {
417                        doc: path,
418                        error: e.to_string(),
419                    });
420                    continue;
421                }
422            };
423            let meta = fig::Value::from(&doc.meta);
424
425            // Reconcile a self-stored `id` against the registry (frontmatter
426            // storage, DESIGN §5). Three outcomes when a document carries its own
427            // `id`: the registry agrees (nothing to do); the registry records a
428            // *different* id for this path, or hands this id to another document
429            // (`IdMismatch` — a drift); or the registry has never heard of the id
430            // (`UnregisteredId` — the shadow got ahead of the cache).
431            if let Some(fm) = meta.get("id").and_then(fig::Value::as_str)
432                && !fm.trim().is_empty()
433            {
434                let fm = Id(fm.trim().to_string());
435                match self.index().id_for_path(&path) {
436                    Some(reg) if reg != fm => structural.push(StructuralFact::IdMismatch {
437                        doc: path.clone(),
438                        frontmatter: fm,
439                        registry: Some(reg),
440                    }),
441                    Some(_) => {} // the registry agrees with the frontmatter
442                    None => match self.index().resolve(&fm) {
443                        // The id is live, but points at a *different* document.
444                        Some(other) if other != path => {
445                            structural.push(StructuralFact::IdMismatch {
446                                doc: path.clone(),
447                                frontmatter: fm,
448                                registry: None,
449                            })
450                        }
451                        // resolve == this path but no reverse entry: consistent.
452                        Some(_) => {}
453                        // The registry has no record of this id at all.
454                        None => structural.push(StructuralFact::UnregisteredId {
455                            doc: path.clone(),
456                            frontmatter: fm,
457                        }),
458                    },
459                }
460            } else if self.id_storage().stamps_frontmatter()
461                && let Some(reg) = self.index().id_for_path(&path)
462            {
463                // The other direction: a stamping workspace expects every
464                // registered document to carry its own id, and this one does not
465                // (a workspace converted from registry-only storage, or an `id`
466                // stripped out of band). The registry is the authority — the id
467                // is already live and linked to — so the repair writes it down.
468                structural.push(StructuralFact::UnstampedId {
469                    doc: path.clone(),
470                    registry: reg,
471                });
472            }
473
474            // Frontmatter relation edges — the only links that can be spanning.
475            for edge in self.relations().edges(&meta) {
476                // Parse once: `link.target` is the bare target (any `[label](…)`
477                // stripped), which is what both the census and findings record.
478                let link = Link::parse(&edge.target);
479                if titles.is_none() && title::is_alias_shaped(&link.target) {
480                    titles = Some(self.title_index_scoped(start, parked).await?);
481                }
482                let resolution = self.resolve_forward(&path, &link, titles.as_ref()).await;
483
484                if Some(edge.relation.as_str()) == spanning.as_deref()
485                    && let Some(resolved) = resolution.resolved_path().cloned()
486                {
487                    // Single-parent check, inverse check, descent.
488                    if visited.contains(&resolved) || queue.contains(&resolved) {
489                        structural.push(StructuralFact::DuplicateContainment {
490                            doc: path.clone(),
491                            target: link.target.clone(),
492                        });
493                    } else {
494                        if let Some(inverse) = inverse.as_deref()
495                            && let Ok((_, child_doc)) = self.load(&resolved).await
496                            && child_doc.has_meta()
497                        {
498                            let child_meta = fig::Value::from(&child_doc.meta);
499                            let inverse_targets = child_meta
500                                .get(inverse)
501                                .map(crate::meta::link_strings)
502                                .unwrap_or_default();
503                            // Build the title index if a nominal inverse link needs it.
504                            if titles.is_none()
505                                && inverse_targets
506                                    .iter()
507                                    .any(|t| title::is_alias_shaped(&Link::parse(t).target))
508                            {
509                                titles = Some(self.title_index_scoped(start, parked).await?);
510                            }
511                            let points_back = inverse_targets.iter().any(|t| {
512                                self.resolve_link_with(&resolved, &Link::parse(t), titles.as_ref())
513                                    == Target::Path(path.clone())
514                            });
515                            if !points_back {
516                                structural.push(StructuralFact::MissingInverse {
517                                    doc: path.clone(),
518                                    child: resolved.clone(),
519                                    inverse: inverse.to_string(),
520                                });
521                            }
522                        }
523                        queue.push(resolved);
524                    }
525                }
526
527                census.push(CensusEntry {
528                    source: path.clone(),
529                    site: LinkSite::Relation(edge.relation),
530                    label: link.label,
531                    target_text: link.target,
532                    resolution,
533                });
534            }
535
536            // Body links — `[[wikilinks]]` and markdown/djot `[t](a)` links
537            // alike — overlay references, censused but never spanning.
538            for body_link in link::scan_body_links(&path, &doc.body) {
539                let wl = body_link.link;
540                if titles.is_none() && title::is_alias_shaped(&wl.target) {
541                    titles = Some(self.title_index_scoped(start, parked).await?);
542                }
543                let resolution = self.resolve_forward(&path, &wl, titles.as_ref()).await;
544                census.push(CensusEntry {
545                    source: path.clone(),
546                    site: LinkSite::Body(body_link.span),
547                    label: wl.label,
548                    target_text: wl.target,
549                    resolution,
550                });
551            }
552
553            // A separated document's `content` must resolve to an existing body
554            // file. Validated here (not a graph edge, so kept out of the census).
555            if let Some(content) = doc.content_attr() {
556                let target = link::resolve(&path, content);
557                let site = LinkSite::Relation("content".to_string());
558                match self.exact_name(&target).await {
559                    NameMatch::Exact => content_bodies.push(target),
560                    NameMatch::CaseOnly(actual) => {
561                        // The linked body exists under a different case: record its
562                        // real name as reached (so it is not also an orphan), and
563                        // still flag the portability hazard.
564                        content_bodies.push(target.with_file_name(&actual));
565                        structural.push(StructuralFact::CaseMismatch {
566                            doc: path.clone(),
567                            site,
568                            target: content.to_string(),
569                            actual,
570                        });
571                    }
572                    NameMatch::None => structural.push(StructuralFact::BrokenLink {
573                        doc: path.clone(),
574                        site,
575                        target: content.to_string(),
576                    }),
577                }
578            }
579
580            // A manifest node's `manifest` must resolve to an existing document,
581            // the same way and for the same reason: it is not a graph edge (the
582            // manifest is machinery, carrying no `part_of` and no id), but it
583            // does reach a file, so the orphan pass must count it as reached.
584            //
585            // The rows *inside* it reach files too, and deliberately do not
586            // arrive here. A covered file is opaque bytes — never a content
587            // document, so never an orphan candidate — and adding ten thousand
588            // of them to every walk's reachable set would make a photo archive
589            // pay for a check none of those files can fail. What the manifest
590            // promises about them is `check`'s manifest pass, once, not the
591            // census's, per document.
592            if let Some(manifest) = doc.manifest_attr() {
593                if doc.content_attr().is_some() {
594                    structural.push(StructuralFact::ManifestConflict { doc: path.clone() });
595                }
596                let target = link::resolve(&path, manifest);
597                let site = LinkSite::Relation(crate::manifest::MANIFEST_KEY.to_string());
598                match self.exact_name(&target).await {
599                    NameMatch::Exact => content_bodies.push(target),
600                    NameMatch::CaseOnly(actual) => {
601                        content_bodies.push(target.with_file_name(&actual));
602                        structural.push(StructuralFact::CaseMismatch {
603                            doc: path.clone(),
604                            site,
605                            target: manifest.to_string(),
606                            actual,
607                        });
608                    }
609                    NameMatch::None => structural.push(StructuralFact::BrokenLink {
610                        doc: path.clone(),
611                        site,
612                        target: manifest.to_string(),
613                    }),
614                }
615            }
616        }
617        Ok(Walk {
618            census,
619            facts: structural,
620            content_bodies,
621        })
622    }
623
624    /// Resolve one forward link (declared in the document at `source`) into a
625    /// [`Resolution`]. A path target is checked against the on-disk name; an
626    /// `id:<id>` target resolves through the registry and stays an id-form
627    /// resolution; an `id:<workspace>/<id>` target naming another workspace
628    /// stops at [`Resolution::Foreign`]; a nominal (`[[My File]]`) target
629    /// resolves through `titles` — `Unique` to the on-disk path, `Ambiguous` to
630    /// [`Resolution::AmbiguousAlias`], `Unknown` falling through to a path (so a
631    /// nominal link to nothing reports as `Broken`, like any dead link).
632    async fn resolve_forward(
633        &self,
634        source: &Path,
635        link: &Link,
636        titles: Option<&TitleIndex>,
637    ) -> Resolution {
638        if link.is_external() {
639            return Resolution::External;
640        }
641        // Mirrors `Workspace::resolve_link_with`: a reference qualified with
642        // this workspace's own name is local, any other qualifier is foreign,
643        // and a malformed `id:` body is a broken id rather than a filename that
644        // happens to contain a colon.
645        let local_id = match link.id_ref() {
646            Some(crate::link::IdRef::Local(id)) => Some(id),
647            Some(crate::link::IdRef::Foreign { workspace, id }) => {
648                if self.workspace_id().is_empty() || workspace != self.workspace_id() {
649                    return Resolution::Foreign { workspace, id };
650                }
651                Some(id)
652            }
653            Some(crate::link::IdRef::Malformed) => return Resolution::MalformedId,
654            None => None,
655        };
656        if let Some(id) = local_id {
657            if !identity::verify(id.as_str()) {
658                return Resolution::MalformedId;
659            }
660            return match self.index().resolve(&id) {
661                Some(path) => Resolution::Id {
662                    id,
663                    to: link::normalize(path),
664                },
665                None => Resolution::DanglingId {
666                    tombstoned: self.index().is_known(&id),
667                    id,
668                },
669            };
670        }
671        // Only a nominal link needs the title index; the caller builds it lazily
672        // the first time one appears, so `titles` is `Some` here whenever it is
673        // consulted. If absent, fall through to path resolution.
674        if let Some(titles) = titles.filter(|_| title::is_alias_shaped(&link.target)) {
675            match titles.resolve(&link.target) {
676                TitleMatch::Unique(path) => {
677                    return match self.exact_name(&path).await {
678                        NameMatch::Exact => Resolution::Path(path),
679                        NameMatch::CaseOnly(actual) => {
680                            Resolution::CaseMismatch { got: path, actual }
681                        }
682                        NameMatch::None => Resolution::Broken,
683                    };
684                }
685                TitleMatch::Ambiguous(candidates) => {
686                    return Resolution::AmbiguousAlias {
687                        name: link.target.clone(),
688                        candidates,
689                    };
690                }
691                TitleMatch::Unknown => {}
692            }
693        }
694        let resolved = link::resolve(source, &link.target);
695        match self.exact_name(&resolved).await {
696            NameMatch::Exact => Resolution::Path(resolved),
697            NameMatch::CaseOnly(actual) => Resolution::CaseMismatch {
698                got: resolved,
699                actual,
700            },
701            NameMatch::None => Resolution::Broken,
702        }
703    }
704
705    /// How `path`'s final component matches its parent directory's listing:
706    /// exactly, only case-insensitively (the portability hazard), or not at all.
707    async fn exact_name(&self, path: &Path) -> NameMatch {
708        let full = self.root().join(path);
709        let (Some(parent), Some(name)) = (full.parent(), full.file_name()) else {
710            return NameMatch::None;
711        };
712        let Ok(entries) = self.fs().read_dir(parent).await else {
713            return NameMatch::None;
714        };
715        let mut case_only = None;
716        for entry in entries {
717            let Some(entry_name) = entry.file_name() else {
718                continue;
719            };
720            if entry_name == name {
721                return NameMatch::Exact;
722            }
723            if entry_name.eq_ignore_ascii_case(name) {
724                case_only = Some(entry_name.to_string_lossy().into_owned());
725            }
726        }
727        match case_only {
728            Some(actual) => NameMatch::CaseOnly(actual),
729            None => NameMatch::None,
730        }
731    }
732}
733
734// These tests use YAML frontmatter fixtures, so they run under the `yaml` feature.
735#[cfg(all(test, feature = "yaml"))]
736mod tests {
737    use super::*;
738    use crate::exec::block_on;
739    use crate::fs::StdFs;
740    use crate::graph::ReadSettings;
741    use crate::index::NoIndex;
742
743    fn write(dir: &Path, rel: &str, text: &str) {
744        let p = dir.join(rel);
745        std::fs::create_dir_all(p.parent().unwrap()).unwrap();
746        std::fs::write(p, text).unwrap();
747    }
748
749    fn tempdir(tag: &str) -> PathBuf {
750        let dir = std::env::temp_dir().join(format!("prov-census-{tag}-{}", std::process::id()));
751        let _ = std::fs::remove_dir_all(&dir);
752        std::fs::create_dir_all(&dir).unwrap();
753        dir
754    }
755
756    #[test]
757    fn census_covers_frontmatter_edges_and_body_wikilinks() {
758        let dir = tempdir("census");
759        write(
760            &dir,
761            "index.md",
762            "---\ncontents:\n- a.md\n---\nBody links [[a.md]] and [[gone.md]].\n",
763        );
764        write(&dir, "a.md", "---\npart_of: index.md\n---\n");
765        let ws = Graph::new(StdFs, &dir, NoIndex, ReadSettings::default());
766        let census = block_on(ws.census("index.md")).unwrap();
767
768        // The frontmatter `contents` edge, resolving to the existing file.
769        assert!(
770            census.iter().any(
771                |e| matches!(&e.site, LinkSite::Relation(r) if r == "contents")
772                    && matches!(&e.resolution, Resolution::Path(p) if p == &PathBuf::from("a.md"))
773            ),
774            "{census:?}"
775        );
776        // The body wikilink to the same file — sited in the body, resolving.
777        assert!(
778            census.iter().any(|e| matches!(e.site, LinkSite::Body(_))
779                && e.target_text == "a.md"
780                && matches!(&e.resolution, Resolution::Path(_))),
781            "{census:?}"
782        );
783        // The body wikilink to a missing file — a Broken resolution.
784        assert!(
785            census
786                .iter()
787                .any(|e| e.target_text == "gone.md" && matches!(e.resolution, Resolution::Broken)),
788            "{census:?}"
789        );
790    }
791
792    #[test]
793    fn backlinks_invert_the_census_across_relations_and_body() {
794        let dir = tempdir("backlinks");
795        write(&dir, "index.md", "---\ncontents:\n- a.md\n- b.md\n---\n");
796        write(&dir, "a.md", "---\npart_of: index.md\n---\n");
797        write(
798            &dir,
799            "b.md",
800            "---\npart_of: index.md\nlinks:\n- a.md\n---\nSee [[a.md]] again.\n",
801        );
802        let ws = Graph::new(StdFs, &dir, NoIndex, ReadSettings::default());
803
804        // Who links to a.md? index.md (contents), b.md (links), b.md (body).
805        let to_a = block_on(ws.backlinks_to("index.md", "a.md")).unwrap();
806        assert_eq!(to_a.len(), 3, "{to_a:?}");
807        assert!(
808            to_a.iter().any(|bl| bl.source == Path::new("index.md")
809                && matches!(&bl.site, LinkSite::Relation(r) if r == "contents")),
810            "{to_a:?}"
811        );
812        assert!(
813            to_a.iter().any(|bl| bl.source == Path::new("b.md")
814                && matches!(&bl.site, LinkSite::Relation(r) if r == "links")),
815            "{to_a:?}"
816        );
817        assert!(
818            to_a.iter()
819                .any(|bl| bl.source == Path::new("b.md") && matches!(bl.site, LinkSite::Body(_))),
820            "{to_a:?}"
821        );
822        // All path-form (this workspace has no registry / id links).
823        assert!(to_a.iter().all(|bl| !bl.by_id), "{to_a:?}");
824
825        // The full map keys targets by path; a.md is one of them.
826        let map = block_on(ws.backlinks("index.md")).unwrap();
827        assert_eq!(map[&PathBuf::from("a.md")].len(), 3);
828    }
829}
830
831/// Invert a census into a backlink map: every resolved target to the inbound
832/// references that reach it, each target's sorted by source.
833///
834/// A free function over an already-taken census, rather than a method that takes
835/// one, because the caller who has to bound the walk — `prov`, which knows where
836/// it parks its own bytes — has already done the walking. Taking the census as
837/// an argument is what lets the bounded and unbounded callers share this.
838pub fn invert(census: Vec<CensusEntry>) -> BTreeMap<PathBuf, Vec<Backlink>> {
839    let mut map: BTreeMap<PathBuf, Vec<Backlink>> = BTreeMap::new();
840    for entry in census {
841        let by_id = matches!(entry.resolution, Resolution::Id { .. });
842        let Some(target) = entry.resolution.resolved_path().cloned() else {
843            continue;
844        };
845        map.entry(target).or_default().push(Backlink {
846            source: entry.source,
847            site: entry.site,
848            by_id,
849        });
850    }
851    for links in map.values_mut() {
852        links.sort_by(|a, b| a.source.cmp(&b.source).then(a.by_id.cmp(&b.by_id)));
853    }
854    map
855}
856
857/// The inbound references to one `target` within an already-taken census,
858/// sorted by source — [`invert`] focused on a single entry.
859pub fn inbound(census: Vec<CensusEntry>, target: &Path) -> Vec<Backlink> {
860    let target = link::normalize(target);
861    let mut links: Vec<Backlink> = census
862        .into_iter()
863        .filter(|entry| entry.resolution.resolved_path() == Some(&target))
864        .map(|entry| {
865            let by_id = matches!(entry.resolution, Resolution::Id { .. });
866            Backlink {
867                source: entry.source,
868                site: entry.site,
869                by_id,
870            }
871        })
872        .collect();
873    links.sort_by(|a, b| a.source.cmp(&b.source).then(a.by_id.cmp(&b.by_id)));
874    links
875}