Skip to main content

rto_render/
okf.rs

1//! Render the graph as an **Open Knowledge Format** bundle (issue #663).
2//!
3//! OKF v0.2 is Google Cloud's vendor-neutral specification for the "LLM wiki"
4//! pattern: a directory of markdown concept documents carrying YAML frontmatter,
5//! reserved `index.md` and `log.md` files, and plain markdown links between
6//! concepts. The whole specification fits on a page, and its only hard
7//! requirement is that every concept document carries a non-empty `type`.
8//!
9//! <https://github.com/GoogleCloudPlatform/open-knowledge-format/blob/main/SPEC.md>
10//!
11//! # Why this replaced the Obsidian vault
12//!
13//! The vault was **one-way**: Roteiro wrote it, nothing read it back, and no
14//! tool but Obsidian could consume it. An open format with named consumers earns
15//! the same machinery better. Two concrete gains beyond that:
16//!
17//! - **The hierarchy retires a class of bug.** The vault flattened every note
18//!   into one directory and appended a hash to each filename, because
19//!   case-insensitive filesystems fold names that differ only in case — a defect
20//!   that once cost this repository 104 notes of 8,144. OKF nests concepts in
21//!   directories, so the collision the hash existed to survive does not arise.
22//! - **Provenance stops being decoration.** Obsidian had nowhere to put it but a
23//!   tag. OKF has a trust model, and it is the one Roteiro already computes.
24//!
25//! # The provenance mapping, which is the point
26//!
27//! Most producers will emit `type` and little else. Roteiro's authored/derived/
28//! inferred distinction lands exactly on OKF's trust tiers (§5.3), which
29//! consumers derive from `verified`:
30//!
31//! | [`Provenance`] | frontmatter | tier |
32//! | --- | --- | --- |
33//! | `Authored` — ADR and blueprint prose | `verified: [{ by: human:<id> }]` | human-reviewed |
34//! | `Derived` — deterministic tree-sitter extraction | `verified: [{ by: roteiro/<version> }]` | machine-confirmed |
35//! | `Inferred` — heuristic, carries a confidence | `generated:` alone | unverified |
36//!
37//! `Derived` is **machine-confirmed rather than unverified** on purpose: it is
38//! reproduced deterministically from the AST at a known commit, so a consumer can
39//! re-derive it. `Inferred` is a similarity judgement with a confidence score and
40//! gets no `verified` key, because claiming otherwise would launder a guess into
41//! a confirmation — the distinction the whole graph exists to keep.
42//!
43//! §7 makes the `human:` prefix load-bearing: it is the only thing that
44//! separates human-reviewed from machine-confirmed, and producers **MUST** use it
45//! for hand-authored content. Roteiro knows which nodes those are, and resolves
46//! *which person* per document — the author of the commit that last changed that
47//! document's path. Naming one author for the whole repository would record a
48//! review that person never did, on every ADR at once.
49//!
50//! # One deliberate divergence
51//!
52//! §11 says consumers **MUST NOT** reject a bundle for broken cross-links.
53//! Roteiro treats a broken authored link as drift and fails a gate over it. Both
54//! are right — the specification asks consumers to be liberal; Roteiro is a
55//! producer that guarantees more than it must. A Roteiro bundle should not
56//! contain a broken link, and `roteiro check` is the reason.
57
58pub mod read;
59
60use std::collections::BTreeMap;
61use std::fmt::Write as _;
62
63use rto_graph::{Explanation, NodeSummary, Provenance};
64
65/// The specification version this renderer targets, written into the bundle
66/// root's `index.md` as `okf_version` (§10 — the one place frontmatter is
67/// permitted in an index).
68pub const OKF_VERSION: &str = "0.2";
69
70/// The reserved filename for a directory listing (§8).
71pub const INDEX_FILE: &str = "index.md";
72
73/// The reserved filename for a change log (§9).
74pub const LOG_FILE: &str = "log.md";
75
76/// The namespace a cross-repo placeholder node's key carries (ADR-0009).
77///
78/// Spelled once here and checked against the graph's own writer by
79/// `the_placeholder_prefix_is_the_graphs`, so the two cannot drift into
80/// disagreeing about what a placeholder key looks like.
81const EXTREF_PREFIX: &str = "extref:";
82
83/// One rendered file in the bundle: a bundle-relative path and its content.
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct BundleFile {
86    /// Path relative to the bundle root, always `/`-separated.
87    pub path: String,
88    /// The file's full text, including any frontmatter block.
89    pub content: String,
90}
91
92/// Who produced or confirmed a concept, in the actor form §7 requires.
93///
94/// The three shapes are not interchangeable: a consumer classifying trust keys
95/// off the `human:` prefix, so using the wrong one silently moves a concept
96/// between tiers.
97///
98/// # Deliberately exhaustive
99///
100/// This is deliberately not `#[non_exhaustive]`, though these crates are
101/// published and a fourth variant would therefore be a breaking change. **The
102/// set is closed by the specification, not by us**: §7 defines exactly these
103/// three forms, and a
104/// fourth appearing means OKF changed. When that happens a caller matching on
105/// this enum *should* stop compiling, because a new actor form is a decision
106/// about trust that must be looked at rather than absorbed by a wildcard arm.
107///
108/// `#[non_exhaustive]` would buy version-compatibility at the price of making
109/// that change silent — which is the opposite of what the trust model needs.
110#[derive(Debug, Clone, PartialEq, Eq)]
111pub enum Actor {
112    /// A person: `human:<id>`. The only form that yields the human-reviewed tier.
113    Human(String),
114    /// A tool, as `<producer>/<version>`.
115    Tool(String, String),
116    /// An automated process: `process:<id>`.
117    Process(String),
118}
119
120impl Actor {
121    /// The wire form, exactly as §7 specifies it.
122    #[must_use]
123    pub fn as_token(&self) -> String {
124        match self {
125            Self::Human(id) => format!("human:{id}"),
126            Self::Tool(producer, version) => format!("{producer}/{version}"),
127            Self::Process(id) => format!("process:{id}"),
128        }
129    }
130}
131
132/// How a concept came to exist, rendered into `generated` / `verified`.
133#[derive(Debug, Clone, PartialEq, Eq)]
134pub struct Origin {
135    /// The actor that produced the concept.
136    pub by: Actor,
137    /// When, as an ISO 8601 instant.
138    pub at: String,
139    /// Whether this origin also *confirms* the concept.
140    ///
141    /// `Authored` and `Derived` do; `Inferred` does not. See the module doc — a
142    /// heuristic that claimed confirmation would launder a guess.
143    pub confirms: bool,
144}
145
146/// A concept document's frontmatter.
147///
148/// Only [`Self::type_`] is required by the specification; every other field is
149/// omitted entirely when absent rather than written empty, because §11 tells
150/// consumers not to reject a document for a missing optional field and an empty
151/// string is a different claim from silence.
152#[derive(Debug, Clone, Default, PartialEq, Eq)]
153pub struct Frontmatter {
154    /// `type` — the one required key. Named with a trailing underscore because
155    /// `type` is a Rust keyword; it is written as `type`.
156    pub type_: String,
157    /// `title` — human-readable display name.
158    pub title: Option<String>,
159    /// `description` — a single-sentence summary.
160    pub description: Option<String>,
161    /// `resource` — canonical URI for the underlying asset.
162    pub resource: Option<String>,
163    /// `tags` — categorisation strings.
164    pub tags: Vec<String>,
165    /// `status` — `draft` | `stable` | `deprecated`.
166    pub status: Option<String>,
167    /// The origin, split into `generated` and `verified` on render.
168    pub origin: Option<Origin>,
169    /// `sources` — where the concept derives from, each with a `resource`.
170    pub sources: Vec<String>,
171}
172
173/// Quote a scalar for YAML, always, and escape everything a double-quoted scalar
174/// cannot hold raw.
175///
176/// Quoting is unconditional rather than clever: a value that looks like a number,
177/// a date, `yes`, `no`, `null` or `~` changes type under a YAML parser when
178/// written bare, and a concept `type` of `no` becoming the boolean `false` is
179/// exactly the failure that makes a bundle non-conformant while looking fine.
180///
181/// # Control characters, because the values are not ours
182///
183/// Every scalar here comes from somewhere a person can put anything: a git author
184/// name, a document heading, a node key derived from a path. A raw newline inside
185/// a quoted scalar does not merely look wrong — YAML folds it, so the value
186/// changes; and a line of the injected text starting at column 0 with `key:` on
187/// it ends the scalar and becomes a *sibling key*. That is frontmatter injection,
188/// and in a document whose frontmatter decides a trust tier it is the one that
189/// matters: a `verified:` block forged from inside a title.
190///
191/// So `\`, `"`, and every C0 control (plus DEL) are escaped — the common three by
192/// name, the rest as `\uXXXX`, which YAML 1.2 §7.3.1 defines for exactly this.
193fn yaml_scalar(s: &str) -> String {
194    let mut out = String::with_capacity(s.len() + 2);
195    out.push('"');
196    for ch in s.chars() {
197        match ch {
198            '\\' => out.push_str("\\\\"),
199            '"' => out.push_str("\\\""),
200            '\n' => out.push_str("\\n"),
201            '\r' => out.push_str("\\r"),
202            '\t' => out.push_str("\\t"),
203            // C0 and DEL. `\uXXXX` is the general escape, used for everything
204            // without a shorter name so nothing reaches the file raw.
205            c if c.is_control() => {
206                let _ = write!(out, "\\u{:04x}", u32::from(c));
207            }
208            c => out.push(c),
209        }
210    }
211    out.push('"');
212    out
213}
214
215impl Frontmatter {
216    /// Render the frontmatter block, `---` fences included.
217    #[must_use]
218    pub fn render(&self) -> String {
219        let mut out = String::from("---\n");
220        let _ = writeln!(out, "type: {}", yaml_scalar(&self.type_));
221        for (key, value) in [
222            ("title", self.title.as_deref()),
223            ("description", self.description.as_deref()),
224            ("resource", self.resource.as_deref()),
225            ("status", self.status.as_deref()),
226        ] {
227            if let Some(v) = value {
228                let _ = writeln!(out, "{key}: {}", yaml_scalar(v));
229            }
230        }
231        if !self.tags.is_empty() {
232            out.push_str("tags:\n");
233            for t in &self.tags {
234                let _ = writeln!(out, "  - {}", yaml_scalar(t));
235            }
236        }
237        if let Some(origin) = &self.origin {
238            // `generated` always: it records production, which happened whether or
239            // not anyone confirmed the result.
240            let _ = writeln!(
241                out,
242                "generated:\n  by: {}\n  at: {}",
243                yaml_scalar(&origin.by.as_token()),
244                yaml_scalar(&origin.at)
245            );
246            // `verified` only when the origin confirms. Its **absence** is the
247            // unverified tier, so writing an empty list here would claim a
248            // confirmation nobody made.
249            if origin.confirms {
250                let _ = writeln!(
251                    out,
252                    "verified:\n  - by: {}\n    at: {}",
253                    yaml_scalar(&origin.by.as_token()),
254                    yaml_scalar(&origin.at)
255                );
256            }
257        }
258        if !self.sources.is_empty() {
259            out.push_str("sources:\n");
260            for s in &self.sources {
261                let _ = writeln!(out, "  - resource: {}", yaml_scalar(s));
262            }
263        }
264        out.push_str("---\n");
265        out
266    }
267}
268
269/// The bundle directory a node kind belongs in.
270///
271/// Grouping by kind is what gives the bundle its hierarchy, and with it a
272/// meaningful per-directory `index.md`. Code symbols share one directory rather
273/// than splitting `fn` from `struct`, because a reader looking for a symbol does
274/// not know which it is.
275#[must_use]
276pub fn section_for(kind: &str) -> &'static str {
277    match kind {
278        "adr" | "adr_section" => "decisions",
279        "blueprint" => "blueprints",
280        "doc" => "docs",
281        "file" => "files",
282        "marker" => "debt",
283        _ => "symbols",
284    }
285}
286
287/// The longest slug a filename may carry, before any disambiguating suffix.
288///
289/// `NAME_MAX` is 255 bytes on Linux and macOS. Real keys reach it: rendering this
290/// repository failed with `File name too long (os error 63)` on a symbol key,
291/// **after** writing part of the bundle — a unit test over short fixtures could
292/// not have found it, and did not. The headroom below covers the `-` plus an
293/// eight-character digest plus `.md`.
294const MAX_SLUG: usize = 200;
295
296/// Slug a node key into a filename that is safe on every filesystem and stable
297/// across renders.
298///
299/// Unlike the vault this replaces, the result does **not** need a hash appended:
300/// concepts live in per-kind directories, so the cross-kind collisions the vault
301/// hashed around cannot occur here. Two keys that still slug identically within
302/// one directory are disambiguated by the caller, which can see the whole set.
303#[must_use]
304pub fn slug(key: &str) -> String {
305    let mut out = String::with_capacity(key.len());
306    let mut last_dash = false;
307    for ch in key.chars() {
308        if ch.is_ascii_alphanumeric() {
309            out.push(ch.to_ascii_lowercase());
310            last_dash = false;
311        } else if !last_dash && !out.is_empty() {
312            out.push('-');
313            last_dash = true;
314        }
315    }
316    let trimmed = out.trim_end_matches('-').to_owned();
317    if trimmed.is_empty() {
318        return "concept".to_owned();
319    }
320    if trimmed.len() <= MAX_SLUG {
321        return trimmed;
322    }
323    // Truncation can *create* a collision that the full keys did not have — two
324    // long keys sharing a prefix become one name — so a shortened slug always
325    // carries a digest of the whole key. Cutting on a char boundary is free here
326    // because every retained character is ASCII.
327    let keep = MAX_SLUG - 9;
328    format!("{}-{}", &trimmed[..keep], short_digest(key))
329}
330
331/// The bundle-relative path a node takes in a single-project bundle whose slug
332/// did not collide, always beginning with `/` so it can be used as a link target
333/// verbatim (§6 — absolute, bundle-relative).
334///
335/// **Provisional, not authoritative.** [`assemble`] overwrites it, because the
336/// real path also carries the workspace member's directory and a disambiguating
337/// digest when two keys slug alike — neither of which is visible from one node.
338/// Resolving a *link* with this function is the bug it exists to make obvious:
339/// use the placement [`assemble`] passes to [`render_concept`].
340#[must_use]
341pub fn concept_path(node: &NodeSummary) -> String {
342    format!("/{}/{}.md", section_for(&node.kind), slug(&node.key))
343}
344
345/// Map a graph provenance onto an OKF origin.
346///
347/// See the module documentation for why `Derived` confirms and `Inferred` does
348/// not. `tool` is the producing tool's actor, used for everything a machine
349/// produced; `human` is the authored content's confirmer, which the caller
350/// resolves from the commit that introduced it.
351///
352/// # An imported concept re-emits the peer's own origin and does not come here
353///
354/// A fact imported from another repository's bundle (`external-*`, issue #706)
355/// keeps the `generated`/`verified` block **that bundle carried**, recovered by
356/// [`read::peer_origin`] and preferred by the caller. That is what stops the
357/// round trip from re-tiering it: the peer's `verified: [{ by: human:alice }]`
358/// goes back out naming Alice, so the next consumer learns who confirmed it
359/// instead of being told this graph did.
360///
361/// The external arms below are the **fallback** for a concept whose bundle
362/// recorded no origin at all. They confirm only when the caller supplies an
363/// actor, on exactly `Authored`'s existing rule — an unknown confirmer yields no
364/// confirmation rather than the wrong one. Naming `tool` as the confirmer would
365/// be this graph vouching for a peer's fact on the strength of having read it.
366/// The cost is honest and one-directional: an unattributed external concept
367/// renders *unverified*, understating a claim rather than inventing one.
368#[must_use]
369pub fn origin_for(prov: Provenance, at: &str, tool: &Actor, human: Option<&Actor>) -> Origin {
370    match prov {
371        // Authored prose is confirmed by the person who wrote it. Falling back to
372        // the tool when the author is unknown would move the concept from
373        // human-reviewed to machine-confirmed, so an unknown author yields no
374        // confirmation at all rather than the wrong one.
375        //
376        // Both **external** confirming tiers join this arm, including
377        // `ExternalDerived` — which is the one place the imported tiers do not
378        // simply follow their local namesake, and the difference is the reason
379        // the tier is carried rather than the variant flattened. `Derived`
380        // confirms below because *a consumer can re-derive it from the same
381        // commit*; a consumer of **our** bundle cannot re-derive a peer's fact,
382        // having neither their tree nor their extractor. What survives an import
383        // is the peer's claim, and a claim needs a claimant's name on it to
384        // confirm anything — which is exactly `Authored`'s rule, so it is
385        // `Authored`'s arm.
386        Provenance::Authored | Provenance::ExternalDerived | Provenance::ExternalAuthored => {
387            match human {
388                Some(actor) => Origin {
389                    by: actor.clone(),
390                    at: at.to_owned(),
391                    confirms: true,
392                },
393                None => Origin {
394                    by: tool.clone(),
395                    at: at.to_owned(),
396                    confirms: false,
397                },
398            }
399        }
400        // Deterministic extraction: a consumer can re-derive it from the same
401        // commit and get the same answer, which is what machine-confirmed means.
402        Provenance::Derived => Origin {
403            by: tool.clone(),
404            at: at.to_owned(),
405            confirms: true,
406        },
407        // A similarity judgement carrying a confidence. Unverified, and honestly
408        // so — and a peer's guess, or anything taken at *acknowledge* rather than
409        // *trust*, is unverified for the same reason.
410        Provenance::Inferred | Provenance::ExternalInferred => Origin {
411            by: tool.clone(),
412            at: at.to_owned(),
413            confirms: false,
414        },
415    }
416}
417
418/// Render one node as an OKF concept document.
419///
420/// `body` is the node's prose when it has any. Relationships become plain
421/// markdown links under a heading, which is how §6 says a relationship is
422/// asserted — the link carries the relationship, and the surrounding prose says
423/// what kind it is.
424#[must_use]
425pub fn render_concept(
426    ex: &Explanation,
427    fm: &Frontmatter,
428    body: Option<&str>,
429    resolve: &dyn Fn(&str) -> Option<String>,
430) -> BundleFile {
431    let mut content = fm.render();
432    content.push('\n');
433    let text = body.map(str::trim).filter(|t| !t.is_empty());
434    // A document that opens with its own `#` heading keeps it. Writing the title
435    // above it would give the concept two H1s saying nearly the same thing, and
436    // the document's own is the better one — it is what its author wrote.
437    let body_leads_with_heading = text.is_some_and(|t| t.starts_with("# "));
438    if !body_leads_with_heading {
439        let _ = writeln!(
440            content,
441            "# {}\n",
442            fm.title.as_deref().unwrap_or(&ex.node.name)
443        );
444    }
445    if let Some(text) = text {
446        content.push_str(text);
447        content.push_str("\n\n");
448    }
449
450    // Group by edge kind so the prose above each list can name the relationship.
451    let mut groups: BTreeMap<&str, Vec<String>> = BTreeMap::new();
452    for (edge, direction) in ex
453        .outgoing
454        .iter()
455        .map(|e| (e, "→"))
456        .chain(ex.incoming.iter().map(|e| (e, "←")))
457    {
458        if let Some(target) = resolve(&edge.node) {
459            let label = edge.node.rsplit(':').next().unwrap_or(&edge.node);
460            let confidence = edge
461                .confidence
462                .map(|c| format!(" (confidence {c:.2})"))
463                .unwrap_or_default();
464            groups
465                .entry(edge.kind.as_str())
466                .or_default()
467                .push(format!("* {direction} [{label}]({target}){confidence}"));
468        }
469    }
470    if !groups.is_empty() {
471        content.push_str("## Relationships\n\n");
472        for (kind, mut links) in groups {
473            links.sort();
474            links.dedup();
475            let _ = writeln!(content, "### {kind}\n");
476            for link in links {
477                let _ = writeln!(content, "{link}");
478            }
479            content.push('\n');
480        }
481    }
482
483    BundleFile {
484        path: concept_path(&ex.node),
485        content,
486    }
487}
488
489/// One entry in a directory listing.
490#[derive(Debug, Clone, PartialEq, Eq)]
491pub struct IndexEntry {
492    /// Display title.
493    pub title: String,
494    /// Link target, bundle-relative.
495    pub target: String,
496    /// Short description, taken from the concept's own frontmatter (§8 SHOULD).
497    pub description: Option<String>,
498}
499
500/// Render a directory `index.md` (§8).
501///
502/// Deliberately **no frontmatter**: §8 permits it only in the bundle root, and a
503/// stray block in a nested index would make the file a malformed concept rather
504/// than a valid listing.
505#[must_use]
506pub fn render_index(heading: &str, entries: &[IndexEntry]) -> String {
507    let mut out = format!("# {heading}\n\n");
508    for e in entries {
509        let desc = e
510            .description
511            .as_deref()
512            .map(|d| format!(" - {d}"))
513            .unwrap_or_default();
514        let _ = writeln!(out, "* [{}]({}){desc}", e.title, e.target);
515    }
516    out
517}
518
519/// Render the bundle-root `index.md`, the one index that carries frontmatter.
520#[must_use]
521pub fn render_root_index(heading: &str, entries: &[IndexEntry]) -> String {
522    let mut out = format!("---\nokf_version: {}\n---\n\n", yaml_scalar(OKF_VERSION));
523    out.push_str(&render_index(heading, entries));
524    out
525}
526
527/// One dated group of log entries.
528#[derive(Debug, Clone, PartialEq, Eq)]
529pub struct LogDay {
530    /// ISO 8601 `YYYY-MM-DD`. §9 requires this exact form for date headings.
531    pub date: String,
532    /// The day's entries, each already prefixed with its kind (`**Update**: …`).
533    pub entries: Vec<String>,
534}
535
536/// Render `log.md` (§9): dated groups, newest first.
537#[must_use]
538pub fn render_log(heading: &str, days: &[LogDay]) -> String {
539    let mut out = format!("# {heading}\n\n");
540    for day in days {
541        let _ = writeln!(out, "## {}\n", day.date);
542        for entry in &day.entries {
543            let _ = writeln!(out, "* {entry}");
544        }
545        out.push('\n');
546    }
547    out
548}
549
550/// A concept ready to be written: its node, its frontmatter, and its prose.
551pub struct Concept<'a> {
552    /// The graph node and its neighbourhood.
553    pub explanation: &'a Explanation,
554    /// The frontmatter to render.
555    pub frontmatter: Frontmatter,
556    /// The node's prose body, when it has one.
557    pub body: Option<String>,
558    /// The workspace member this concept came from, for a bundle spanning several
559    /// repositories (ADR-0009). `None` for a single project.
560    ///
561    /// Nesting by member is what stops two repositories' `file:README.md` landing
562    /// on one path. The vault this replaces solved the same problem by qualifying
563    /// the *key* and hashing the filename, because it had one flat directory to
564    /// work with; a bundle has directories, so the structure carries it.
565    pub member: Option<String>,
566}
567
568/// One directory's concepts, each with the path [`assemble`]'s first pass gave
569/// it — the intermediate the second pass renders from.
570struct Placed<'a> {
571    /// The workspace member these concepts came from, when the bundle spans one.
572    /// Also the scope a link resolves in: the same key in two members is two
573    /// concepts.
574    member: Option<String>,
575    /// The bundle-relative directory: `<member>/<section>`, or `<section>` alone.
576    dir: String,
577    /// Each concept and the bundle-relative path it will be written to.
578    concepts: Vec<(Concept<'a>, String)>,
579}
580
581/// Assemble a whole bundle: every concept, an `index.md` for each section
582/// directory, and the bundle-root `index.md` carrying `okf_version`.
583///
584/// A workspace **member's** directory carries no index of its own: it is a
585/// container for that member's sections, and the root index links straight
586/// through to `<member>/<section>`, so nothing is unreachable without one.
587/// `an_index_lists_a_section_and_a_member_directory_is_a_container` pins that,
588/// because the layout is documented in `docs/OKF_BUNDLE.md` and a bundle that
589/// grew member indexes would make that page wrong without failing anything.
590///
591/// # Collisions are resolved here, and only here
592///
593/// [`slug`] can map two different keys onto one filename. The Obsidian vault this
594/// replaces appended a hash to **every** note to survive that, because it wrote
595/// one flat directory on filesystems that fold case — and it still lost 104 notes
596/// of 8,144 before the hash existed. Nesting by kind removes most of the pressure,
597/// but not all of it, so the remaining collisions are settled where the whole set
598/// is visible rather than by a per-name rule that cannot see its neighbours.
599///
600/// A colliding name gets a short digest of its key appended. The **first** name in
601/// key order keeps the bare slug, so a bundle re-rendered from an unchanged graph
602/// is byte-identical: the disambiguation depends on the set, and the set is sorted.
603///
604/// Comparison is case-**insensitive** on purpose. `Foo` and `foo` are one file on
605/// macOS and Windows, and a bundle that wrote both would silently lose one — which
606/// is exactly how the vault lost notes.
607///
608/// # Links are resolved against the placement, not re-derived from the key
609///
610/// Which is why this happens in two passes. A concept's path depends on the whole
611/// set — the member directory it nests under, and whether its slug collided — so
612/// *any* rule that turns a key into a path on its own is guessing. The first pass
613/// places every concept and records `key -> path`; the second renders, resolving
614/// each relationship through that map. A key the map does not hold is not in the
615/// bundle, and its link is dropped rather than written as a path that does not
616/// exist.
617///
618/// The map is scoped **per member**: `file:README.md` is a different concept in
619/// each repository of a workspace, so a link from one member's concept resolves
620/// inside that member.
621#[must_use]
622pub fn assemble(concepts: Vec<Concept<'_>>, title: &str, log: &[LogDay]) -> Vec<BundleFile> {
623    // Group by section, in key order, so both the output and the disambiguation
624    // are deterministic.
625    let mut by_section: BTreeMap<(Option<String>, &'static str), Vec<Concept<'_>>> =
626        BTreeMap::new();
627    let mut ordered = concepts;
628    ordered.sort_by(|a, b| a.explanation.node.key.cmp(&b.explanation.node.key));
629    for c in ordered {
630        by_section
631            .entry((c.member.clone(), section_for(&c.explanation.node.kind)))
632            .or_default()
633            .push(c);
634    }
635
636    // Pass one: place every concept. Nothing is rendered yet, because a link
637    // written now could only guess at a path this pass is still deciding.
638    let mut placed: Vec<Placed<'_>> = Vec::new();
639    let mut index: BTreeMap<Option<String>, BTreeMap<String, String>> = BTreeMap::new();
640
641    for ((member, section), members) in by_section {
642        // `/<member>/<section>/` in a workspace, `/<section>/` on its own.
643        let dir = member
644            .as_deref()
645            .map_or_else(|| section.to_owned(), |m| format!("{}/{section}", slug(m)));
646        let mut taken: BTreeMap<String, usize> = BTreeMap::new();
647        let mut concepts: Vec<(Concept<'_>, String)> = Vec::with_capacity(members.len());
648        let member_index = index.entry(member.clone()).or_default();
649
650        for c in members {
651            // Case folding is already handled: `slug` lowercases, so no two slugs
652            // can differ by case alone and this comparison needs no folding of its
653            // own. An earlier version folded again here and read as the guard
654            // against case-insensitive filesystems — it was a no-op, and removing
655            // it changed no test, which is how the redundancy was found.
656            let base = slug(&c.explanation.node.key);
657            let name = match taken.get(&base) {
658                None => base.clone(),
659                Some(_) => format!("{base}-{}", short_digest(&c.explanation.node.key)),
660            };
661            *taken.entry(base).or_insert(0) += 1;
662
663            let path = format!("/{dir}/{name}.md");
664            member_index.insert(c.explanation.node.key.clone(), path.clone());
665            concepts.push((c, path));
666        }
667        placed.push(Placed {
668            member,
669            dir,
670            concepts,
671        });
672    }
673
674    let mut files = Vec::new();
675    let mut sections: Vec<IndexEntry> = Vec::new();
676
677    // Pass two: render, resolving every link through the placement above.
678    for section in placed {
679        let member_index = index.get(&section.member);
680        let dir = &section.dir;
681        let mut entries: Vec<IndexEntry> = Vec::with_capacity(section.concepts.len());
682
683        for (c, path) in &section.concepts {
684            let title = c
685                .frontmatter
686                .title
687                .clone()
688                .unwrap_or_else(|| c.explanation.node.name.clone());
689            entries.push(IndexEntry {
690                title,
691                target: path.clone(),
692                description: c.frontmatter.description.clone(),
693            });
694            let mut file =
695                render_concept(c.explanation, &c.frontmatter, c.body.as_deref(), &|key| {
696                    // A cross-repo reference names a concept that is *in this
697                    // bundle*, one member over. Following the placeholder's own
698                    // key would land the reader on the stub standing in for it
699                    // (see `cross_member_target`), which is a worse answer than
700                    // the one the bundle already contains.
701                    cross_member_target(&index, key)
702                        .or_else(|| member_index.and_then(|m| m.get(key)).cloned())
703                });
704            file.path.clone_from(path);
705            files.push(file);
706        }
707
708        files.push(BundleFile {
709            path: format!("/{dir}/{INDEX_FILE}"),
710            content: render_index(dir, &entries),
711        });
712        sections.push(IndexEntry {
713            title: dir.clone(),
714            target: format!("/{dir}/{INDEX_FILE}"),
715            description: Some(format!("{} concept(s)", section.concepts.len())),
716        });
717    }
718
719    if !log.is_empty() {
720        files.push(BundleFile {
721            path: format!("/{LOG_FILE}"),
722            content: render_log("Update Log", log),
723        });
724    }
725    files.push(BundleFile {
726        path: format!("/{INDEX_FILE}"),
727        content: render_root_index(title, &sections),
728    });
729    files.sort_by(|a, b| a.path.cmp(&b.path));
730    files
731}
732
733/// Where a **cross-repo reference** actually points, when the member it names is
734/// in this same bundle.
735///
736/// A workspace graph records a reference into another repository as an
737/// `extref:<project>::<key>` placeholder node in the *referring* member
738/// (ADR-0009): a stub standing in for a concept that member cannot see. But a
739/// workspace **bundle** contains that other member, so the concept the reference
740/// is about is right there — and linking to the stub instead would send a reader
741/// to a document whose entire content is that it is not the document they wanted.
742///
743/// Both spellings reach the same place: the placeholder node's key
744/// (`extref:<project>::<key>`, what an edge actually points at) and a bare
745/// project-qualified key. What counts as *qualified* is
746/// [`rto_graph::parse_qualified`]'s decision, not a second `::` rule invented
747/// here — the keys were produced by that rule, so the bundle must not disagree
748/// with it about where the project name ends.
749///
750/// `None` unless every part holds: the key parses as qualified, it names a member
751/// of **this** bundle, and that member really has the concept. The caller falls
752/// back to the member-scoped lookup then — which yields the placeholder, a file
753/// that exists — because a stub in the bundle beats a link to nothing.
754fn cross_member_target(
755    index: &BTreeMap<Option<String>, BTreeMap<String, String>>,
756    key: &str,
757) -> Option<String> {
758    let qualified = key.strip_prefix(EXTREF_PREFIX).unwrap_or(key);
759    let (project, bare) = rto_graph::parse_qualified(qualified)?;
760    // The membership test is what makes reading a bare key this way safe: a
761    // symbol key containing `::` splits too, but its left half is never a
762    // workspace member's name.
763    index.get(&Some(project.to_owned()))?.get(bare).cloned()
764}
765
766/// A short, stable digest of a key, for disambiguating a collided slug.
767///
768/// FNV-1a rather than a cryptographic hash: this is a filename disambiguator, not
769/// a security boundary, and it must stay identical across renders and platforms.
770///
771/// The **low 32 bits**, masked rather than sliced off the hex rendering. An
772/// earlier version wrote `format!("{h:08x}")[..8]`, which is a string operation
773/// wearing a number's clothes: `{:08x}` pads to 8 but does not truncate, so a
774/// hash above `2^32` renders 9 to 16 digits and the slice then takes a *high*
775/// window whose offset moves with the magnitude. The entropy is 32 bits either
776/// way, so no collision was ever more likely — but which 32 bits you got depended
777/// on how large the hash happened to be, and a filename rule nobody can state in
778/// one sentence is a filename rule waiting to be got wrong.
779fn short_digest(key: &str) -> String {
780    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
781    for b in key.as_bytes() {
782        h ^= u64::from(*b);
783        h = h.wrapping_mul(0x0000_0100_0000_01b3);
784    }
785    // Masked to 32 bits, so `{:08x}` renders exactly eight digits and no cast is
786    // needed to say so. `MAX_SLUG`'s headroom is written against that eight.
787    format!("{:08x}", h & 0xffff_ffff)
788}
789
790#[cfg(test)]
791mod tests {
792    use super::*;
793
794    fn node(key: &str, kind: &str, name: &str) -> NodeSummary {
795        NodeSummary {
796            key: key.to_owned(),
797            kind: kind.to_owned(),
798            name: name.to_owned(),
799            path: None,
800            lang: None,
801        }
802    }
803
804    fn explanation(key: &str, kind: &str, name: &str) -> Explanation {
805        Explanation {
806            schema: rto_graph::SCHEMA,
807            node: node(key, kind, name),
808            meta: serde_json::Value::Null,
809            outgoing: Vec::new(),
810            incoming: Vec::new(),
811        }
812    }
813
814    fn concept<'a>(ex: &'a Explanation, type_: &str) -> Concept<'a> {
815        Concept {
816            explanation: ex,
817            frontmatter: Frontmatter {
818                type_: type_.to_owned(),
819                ..Frontmatter::default()
820            },
821            body: None,
822            member: None,
823        }
824    }
825
826    fn edge(to: &str) -> rto_graph::EdgeRef {
827        rto_graph::EdgeRef {
828            kind: "references".to_owned(),
829            provenance: "authored",
830            confidence: None,
831            node: to.to_owned(),
832        }
833    }
834
835    /// Every `](/…)` link in an emitted bundle, as `(containing file, target)`.
836    fn internal_links(files: &[BundleFile]) -> Vec<(String, String)> {
837        let mut out = Vec::new();
838        for f in files {
839            let mut rest = f.content.as_str();
840            while let Some(open) = rest.find("](/") {
841                rest = &rest[open + 2..];
842                let Some(close) = rest.find(')') else { break };
843                out.push((f.path.clone(), rest[..close].to_owned()));
844                rest = &rest[close..];
845            }
846        }
847        out
848    }
849
850    /// **Every internal link points at a file the bundle actually contains.**
851    ///
852    /// The conformance test above cannot make this assertion, and would not have
853    /// caught its failure: §11 tells consumers they **MUST NOT** reject a bundle
854    /// for a broken cross-link, so a bundle full of them is still conformant. It
855    /// is still wrong, and this repository promises better (ADR-0021).
856    ///
857    /// Three ways a link target can differ from a key's own slug, all present in
858    /// the fixture because a resolver that re-derives the path from the key gets
859    /// each of them wrong:
860    ///
861    /// 1. a **workspace member** prefixes the directory;
862    /// 2. a **collided slug** takes a digest suffix;
863    /// 3. a node whose **kind and key disagree** about the section —
864    ///    `blueprint_section` keys begin `blueprint:` but the concept files under
865    ///    `symbols`, which is how 43 links broke in a real render of this
866    ///    repository.
867    #[test]
868    fn every_emitted_link_resolves_to_a_file_that_exists() {
869        // (3) key says `blueprint:`, kind says `blueprint_section` → `symbols`.
870        let section = {
871            let mut ex = explanation(
872                "blueprint:docs/blueprint/roteiro.md#1-crate-placement",
873                "blueprint_section",
874                "1 · Crate placement",
875            );
876            ex.outgoing = vec![edge("blueprint:docs/blueprint/roteiro.md")];
877            ex
878        };
879        let plan = {
880            let mut ex = explanation(
881                "blueprint:docs/blueprint/roteiro.md",
882                "blueprint",
883                "roteiro.md",
884            );
885            // (2) both collision partners, and the section above.
886            ex.outgoing = vec![
887                edge("blueprint:docs/blueprint/roteiro.md#1-crate-placement"),
888                edge("sym:rust:a/b.rs#Thing"),
889                edge("sym:rust:a-b.rs#thing"),
890            ];
891            ex
892        };
893        let thing_a = explanation("sym:rust:a/b.rs#Thing", "fn", "Thing");
894        let thing_b = explanation("sym:rust:a-b.rs#thing", "fn", "thing");
895        assert_eq!(
896            slug(&thing_a.node.key),
897            slug(&thing_b.node.key),
898            "the fixture must actually collide, or the digest suffix is never exercised"
899        );
900
901        // (1) everything nests under one workspace member.
902        let concepts: Vec<Concept<'_>> = [
903            (&section, "blueprint_section"),
904            (&plan, "blueprint"),
905            (&thing_a, "fn"),
906            (&thing_b, "fn"),
907        ]
908        .into_iter()
909        .map(|(ex, type_)| {
910            let mut c = concept(ex, type_);
911            c.member = Some("Alpha".to_owned());
912            c
913        })
914        .collect();
915
916        let files = assemble(concepts, "Workspace", &[]);
917        let emitted: std::collections::BTreeSet<&str> =
918            files.iter().map(|f| f.path.as_str()).collect();
919
920        // The fixture is load-bearing only if the placement really did all three
921        // things. Asserted before the links, so a fixture that stopped exercising
922        // one of them fails here rather than passing vacuously below.
923        assert!(
924            emitted
925                .iter()
926                .all(|p| *p == "/index.md" || p.starts_with("/alpha/")),
927            "every concept must nest under its member: {emitted:?}"
928        );
929        assert!(
930            emitted.contains("/alpha/symbols/sym-rust-a-b-rs-thing.md"),
931            "the first collision partner keeps the bare slug: {emitted:?}"
932        );
933        assert!(
934            emitted
935                .iter()
936                .any(|p| p.starts_with("/alpha/symbols/sym-rust-a-b-rs-thing-")),
937            "the second takes a digest suffix: {emitted:?}"
938        );
939        assert!(
940            emitted.contains(
941                "/alpha/symbols/blueprint-docs-blueprint-roteiro-md-1-crate-placement.md"
942            ),
943            "a `blueprint_section` files under `symbols`, not under its key's \
944             `blueprints`: {emitted:?}"
945        );
946
947        let links = internal_links(&files);
948        // A resolver that drops what it cannot place satisfies the loop below by
949        // emitting nothing, so count first: 4 relationship links (one per edge),
950        // 4 concept entries across the two directory indexes, and 2 directory
951        // entries in the root index.
952        assert_eq!(links.len(), 4 + 4 + 2, "{links:?}");
953
954        for (from, target) in &links {
955            assert!(
956                emitted.contains(target.as_str()),
957                "{from} links to {target}, which the bundle does not contain: {emitted:?}"
958            );
959        }
960
961        // Existence is not enough, and this is the half that is easy to miss: two
962        // concepts whose slugs collided are *different files*, so a resolver that
963        // re-derives the bare slug sends both links to whichever one kept it. That
964        // target exists, so the loop above passes while the link points at the
965        // wrong concept — silently wrong rather than broken. `plan` has three
966        // distinct edge targets and must therefore emit three distinct paths.
967        let plan_path = "/alpha/blueprints/blueprint-docs-blueprint-roteiro-md.md";
968        let from_plan: std::collections::BTreeSet<&str> = links
969            .iter()
970            .filter(|(from, _)| from == plan_path)
971            .map(|(_, target)| target.as_str())
972            .collect();
973        assert_eq!(
974            from_plan.len(),
975            plan.outgoing.len(),
976            "{plan_path} has {} edges to distinct concepts but links to {} file(s): {from_plan:?}",
977            plan.outgoing.len(),
978            from_plan.len()
979        );
980    }
981
982    /// Every file the bundle emits satisfies §11's conformance criteria.
983    ///
984    /// Asserted over the *emitted set* rather than over the renderer, because the
985    /// specification is a statement about a bundle and a per-function test cannot
986    /// make it.
987    #[test]
988    fn every_emitted_bundle_is_conformant() {
989        let a = explanation("adr:0001#decision", "adr", "ADR-0001");
990        let b = explanation("sym:rust:src/main.rs#greet", "fn", "greet");
991        let files = assemble(
992            vec![concept(&a, "adr"), concept(&b, "fn")],
993            "Roteiro",
994            &[LogDay {
995                date: "2026-08-28".into(),
996                entries: vec!["**Update**: rebuilt.".into()],
997            }],
998        );
999
1000        for f in &files {
1001            let reserved = f.path.ends_with(INDEX_FILE) || f.path.ends_with(LOG_FILE);
1002            if reserved {
1003                continue;
1004            }
1005            // §11.1 — a parseable frontmatter block, and §11.2 a non-empty `type`.
1006            assert!(
1007                f.content.starts_with("---\n"),
1008                "{} opens with no frontmatter block",
1009                f.path
1010            );
1011            let end = f.content[4..]
1012                .find("\n---\n")
1013                .expect("frontmatter must terminate");
1014            let block = &f.content[4..4 + end];
1015            assert!(
1016                block
1017                    .lines()
1018                    .any(|l| l.starts_with("type: ") && l.len() > 8),
1019                "{} carries no non-empty `type`: {block}",
1020                f.path
1021            );
1022        }
1023
1024        // §8 — a nested index carries no frontmatter; only the root may.
1025        let nested = files
1026            .iter()
1027            .find(|f| f.path == "/decisions/index.md")
1028            .expect("a per-directory index");
1029        assert!(!nested.content.starts_with("---"), "{}", nested.content);
1030        let root = files
1031            .iter()
1032            .find(|f| f.path == "/index.md")
1033            .expect("a root index");
1034        assert!(
1035            root.content.contains("okf_version: \"0.2\""),
1036            "{}",
1037            root.content
1038        );
1039    }
1040
1041    /// A document that brings its own heading is not given a second one.
1042    #[test]
1043    fn a_body_with_its_own_heading_is_not_double_titled() {
1044        let ex = explanation("adr:0010", "adr", "ADR-0010");
1045        let fm = Frontmatter {
1046            type_: "adr".into(),
1047            title: Some("Explorer web app".into()),
1048            ..Frontmatter::default()
1049        };
1050        let with = render_concept(
1051            &ex,
1052            &fm,
1053            Some("# ADR-0010: Explorer web app\n\nBody."),
1054            &|_| None,
1055        );
1056        let h1s = |c: &str| c.lines().filter(|l| l.starts_with("# ")).count();
1057        assert_eq!(h1s(&with.content), 1, "exactly one H1: {}", with.content);
1058        assert!(with.content.contains("# ADR-0010: Explorer web app"));
1059        assert!(
1060            !with.content.contains("# Explorer web app\n\n# ADR-0010"),
1061            "the frontmatter title must not be stacked above the document's own"
1062        );
1063
1064        // A body with no heading still gets one, or the concept has no title at all.
1065        let without = render_concept(&ex, &fm, Some("Just prose."), &|_| None);
1066        assert!(
1067            without.content.contains("# Explorer web app"),
1068            "a headingless body still gets the title: {}",
1069            without.content
1070        );
1071        assert_eq!(h1s(&without.content), 1);
1072    }
1073
1074    /// Two members' identically-named concepts do not collide.
1075    ///
1076    /// Every repository has a `README.md`, so `file:README.md` is the same key in
1077    /// each — the case the vault this replaces had to qualify keys and hash
1078    /// filenames to survive, because it wrote one flat directory. Nesting by
1079    /// member carries it structurally instead, and the assertion is again the one
1080    /// whose failure was invisible: **both concepts are written**.
1081    #[test]
1082    fn two_members_sharing_a_key_both_survive() {
1083        let a = explanation("file:README.md", "file", "README.md");
1084        let b = explanation("file:README.md", "file", "README.md");
1085        let mut ca = concept(&a, "file");
1086        ca.member = Some("app".to_owned());
1087        let mut cb = concept(&b, "file");
1088        cb.member = Some("lib".to_owned());
1089
1090        let files = assemble(vec![ca, cb], "Workspace", &[]);
1091        let concepts: Vec<&BundleFile> = files
1092            .iter()
1093            .filter(|f| !f.path.ends_with(INDEX_FILE) && !f.path.ends_with(LOG_FILE))
1094            .collect();
1095        assert_eq!(concepts.len(), 2, "both members' README must be written");
1096        assert!(
1097            concepts.iter().any(|f| f.path.starts_with("/app/")),
1098            "one under its member: {:?}",
1099            concepts.iter().map(|f| &f.path).collect::<Vec<_>>()
1100        );
1101        assert!(concepts.iter().any(|f| f.path.starts_with("/lib/")));
1102    }
1103
1104    /// The prefix this module strips is the one the graph writes.
1105    ///
1106    /// Two crates spelling a key namespace independently is how a resolver stops
1107    /// recognising the keys it is given, silently — the link would simply stop
1108    /// crossing, and every target still exists, so nothing else would notice.
1109    #[test]
1110    fn the_placeholder_prefix_is_the_graphs() {
1111        assert_eq!(rto_graph::external_ref_key(""), EXTREF_PREFIX);
1112    }
1113
1114    /// **A cross-repo reference links to the other member's concept, not to the
1115    /// stub standing in for it.**
1116    ///
1117    /// A workspace graph records a reference into another repository as an
1118    /// `extref:<project>::<key>` placeholder in the *referring* member, because
1119    /// that member cannot see the target. A workspace **bundle** can: the other
1120    /// member is in it. Resolving the placeholder's own key — which is what a
1121    /// member-scoped lookup does — produces a link that works and teaches nothing,
1122    /// landing the reader on a document whose whole content is that it is not the
1123    /// document they wanted. An existence check cannot see that, which is why the
1124    /// assertion is the *destination* rather than that a link resolved.
1125    #[test]
1126    fn a_cross_repo_reference_reaches_the_other_members_concept() {
1127        // `app` has the real concept.
1128        let real = explanation("file:README.md", "file", "README.md");
1129        // `deploy` holds the placeholder, and a document that references it.
1130        let stub = explanation(
1131            "extref:app::file:README.md",
1132            "external_ref",
1133            "app::file:README.md",
1134        );
1135        let referrer = {
1136            let mut ex = explanation("doc:deploy.md", "doc", "deploy.md");
1137            ex.outgoing = vec![edge("extref:app::file:README.md")];
1138            ex
1139        };
1140
1141        let member = |ex, type_, name: &str| {
1142            let mut c = concept(ex, type_);
1143            c.member = Some(name.to_owned());
1144            c
1145        };
1146        let files = assemble(
1147            vec![
1148                member(&real, "file", "app"),
1149                member(&stub, "external_ref", "deploy"),
1150                member(&referrer, "doc", "deploy"),
1151            ],
1152            "Workspace",
1153            &[],
1154        );
1155
1156        let emitted: std::collections::BTreeSet<&str> =
1157            files.iter().map(|f| f.path.as_str()).collect();
1158        let target = "/app/files/file-readme-md.md";
1159        assert!(
1160            emitted.contains(target),
1161            "the fixture must place the real concept: {emitted:?}"
1162        );
1163        // The stub is still written — it is a concept of `deploy`'s graph — and
1164        // links must simply not prefer it.
1165        let stub_path = "/deploy/symbols/extref-app-file-readme-md.md";
1166        assert!(
1167            emitted.contains(stub_path),
1168            "the placeholder must still be a concept: {emitted:?}"
1169        );
1170
1171        let links = internal_links(&files);
1172        let targets: Vec<&str> = links
1173            .iter()
1174            .filter(|(from, _)| from == "/deploy/docs/doc-deploy-md.md")
1175            .map(|(_, t)| t.as_str())
1176            .collect();
1177        assert_eq!(
1178            targets,
1179            vec![target],
1180            "the reference must reach `app`'s concept rather than `deploy`'s stub"
1181        );
1182    }
1183
1184    /// **An `index.md` lists a section; a workspace member's directory is a
1185    /// container and has none.**
1186    ///
1187    /// §8 makes an index *optional* in any directory, so a missing one is not a
1188    /// conformance failure and no conformance check will ever mention it. What it
1189    /// is instead is a documented layout — `docs/OKF_BUNDLE.md`, ADR-0021 and the
1190    /// site each tell a reader which directories carry one — and prose the
1191    /// renderer can contradict without failing anything is how all three came to
1192    /// over-claim "each directory carries an `index.md`". Pinning the exact set
1193    /// means a bundle that grows member indexes has to move those pages with it.
1194    ///
1195    /// The member directory is not a dead end without one: the root index links
1196    /// straight through to `<member>/<section>`, which the second half asserts,
1197    /// because "no index here" is only defensible while nothing needs it.
1198    #[test]
1199    fn an_index_lists_a_section_and_a_member_directory_is_a_container() {
1200        let readme = explanation("file:README.md", "file", "README.md");
1201        let thing = explanation("sym:rust:a.rs#thing", "fn", "thing");
1202
1203        let member = |ex, type_, name: &str| {
1204            let mut c = concept(ex, type_);
1205            c.member = Some(name.to_owned());
1206            c
1207        };
1208        let files = assemble(
1209            vec![
1210                member(&readme, "file", "app"),
1211                member(&thing, "fn", "deploy"),
1212            ],
1213            "Workspace",
1214            &[],
1215        );
1216
1217        let emitted: std::collections::BTreeSet<&str> =
1218            files.iter().map(|f| f.path.as_str()).collect();
1219        // The fixture is load-bearing only if it really made two members whose
1220        // sections differ, so that is asserted before the set below — which an
1221        // empty bundle would otherwise satisfy by containing only a root index.
1222        assert!(
1223            emitted.contains("/app/files/file-readme-md.md")
1224                && emitted.contains("/deploy/symbols/sym-rust-a-rs-thing.md"),
1225            "the fixture must place a concept in each member: {emitted:?}"
1226        );
1227
1228        // `/{INDEX_FILE}` rather than the bare name: a concept whose slug ends
1229        // `-index` would otherwise be counted as a directory listing.
1230        let index_suffix = format!("/{INDEX_FILE}");
1231        let indexes: Vec<&str> = files
1232            .iter()
1233            .map(|f| f.path.as_str())
1234            .filter(|p| p.ends_with(&index_suffix))
1235            .collect();
1236        assert_eq!(
1237            indexes,
1238            vec![
1239                "/app/files/index.md",
1240                "/deploy/symbols/index.md",
1241                "/index.md"
1242            ],
1243            "the bundle root and every section directory carry an index, and a \
1244             member directory carries none"
1245        );
1246
1247        let from_root: Vec<String> = internal_links(&files)
1248            .into_iter()
1249            .filter(|(from, _)| from == "/index.md")
1250            .map(|(_, target)| target)
1251            .collect();
1252        assert_eq!(
1253            from_root,
1254            vec![
1255                "/app/files/index.md".to_owned(),
1256                "/deploy/symbols/index.md".to_owned()
1257            ],
1258            "the root index must reach each section directly, since the member \
1259             directory between them carries no index of its own"
1260        );
1261    }
1262
1263    /// A key longer than the filesystem allows is truncated, and truncation does
1264    /// not merge two concepts into one.
1265    ///
1266    /// Found by *running* the renderer over this repository, not by a unit test:
1267    /// it failed with `File name too long (os error 63)` after writing part of
1268    /// the bundle. Short fixtures cannot reach this, which is why the earlier
1269    /// tests were all green while the real render was broken.
1270    #[test]
1271    fn an_overlong_key_is_truncated_without_colliding() {
1272        let long = "sym:rust:".to_owned() + &"a".repeat(400);
1273        // Same 400-character prefix, different tails: truncation alone would
1274        // merge them.
1275        let a = format!("{long}#one");
1276        let b = format!("{long}#two");
1277
1278        assert!(
1279            slug(&a).len() <= MAX_SLUG,
1280            "slug must fit: {}",
1281            slug(&a).len()
1282        );
1283        assert!(slug(&b).len() <= MAX_SLUG);
1284        assert_ne!(
1285            slug(&a),
1286            slug(&b),
1287            "two keys sharing a truncated prefix must not slug to one name"
1288        );
1289        // And the cap leaves room for `.md` plus a disambiguating suffix inside
1290        // NAME_MAX (255).
1291        assert!(slug(&a).len() + ".md".len() + 9 <= 255);
1292    }
1293
1294    #[test]
1295    fn colliding_slugs_do_not_lose_a_concept() {
1296        // Different keys, identical slug. (Case is not a separate hazard here:
1297        // `slug` lowercases, so a case-only difference cannot survive into a
1298        // filename at all.)
1299        let a = explanation("sym:rust:a/b.rs#Thing", "fn", "Thing");
1300        let b = explanation("sym:rust:a-b.rs#thing", "fn", "thing");
1301        assert_eq!(
1302            slug(&a.node.key).to_ascii_lowercase(),
1303            slug(&b.node.key).to_ascii_lowercase(),
1304            "fixture must actually collide, or this test proves nothing"
1305        );
1306
1307        let files = assemble(vec![concept(&a, "fn"), concept(&b, "fn")], "T", &[]);
1308        let concepts: Vec<&BundleFile> = files
1309            .iter()
1310            .filter(|f| !f.path.ends_with(INDEX_FILE) && !f.path.ends_with(LOG_FILE))
1311            .collect();
1312        assert_eq!(concepts.len(), 2, "both concepts must be written");
1313
1314        let paths: std::collections::BTreeSet<String> = concepts
1315            .iter()
1316            .map(|f| f.path.to_ascii_lowercase())
1317            .collect();
1318        assert_eq!(
1319            paths.len(),
1320            2,
1321            "and to distinct files even when case is folded: {paths:?}"
1322        );
1323    }
1324
1325    /// The same graph renders to the same bytes, whatever order it arrives in.
1326    ///
1327    /// Both fixtures are in the **same section** on purpose. An earlier version
1328    /// used an `adr` and a `fn`, which land in different directories — so each
1329    /// section held one member, ordering within a section was never exercised,
1330    /// and deleting the sort changed nothing. The test passed and guarded nothing.
1331    #[test]
1332    fn assembly_is_deterministic() {
1333        let a = explanation("sym:rust:a.rs#a", "fn", "a");
1334        let b = explanation("sym:rust:z.rs#z", "fn", "z");
1335        assert_eq!(
1336            section_for(&a.node.kind),
1337            section_for(&b.node.kind),
1338            "the fixtures must share a section, or ordering is not under test"
1339        );
1340        let once = assemble(vec![concept(&a, "fn"), concept(&b, "fn")], "T", &[]);
1341        let twice = assemble(vec![concept(&b, "fn"), concept(&a, "fn")], "T", &[]);
1342        assert_eq!(once, twice, "input order must not change the bundle");
1343    }
1344
1345    fn tool() -> Actor {
1346        Actor::Tool("roteiro".into(), "4.0.0".into())
1347    }
1348
1349    #[test]
1350    fn the_only_required_field_is_type() {
1351        let fm = Frontmatter {
1352            type_: "adr".into(),
1353            ..Frontmatter::default()
1354        };
1355        let rendered = fm.render();
1356        assert_eq!(rendered, "---\ntype: \"adr\"\n---\n");
1357    }
1358
1359    #[test]
1360    fn actors_use_the_forms_the_spec_requires() {
1361        assert_eq!(Actor::Human("pixie79".into()).as_token(), "human:pixie79");
1362        assert_eq!(tool().as_token(), "roteiro/4.0.0");
1363        assert_eq!(
1364            Actor::Process("nightly".into()).as_token(),
1365            "process:nightly"
1366        );
1367    }
1368
1369    /// The trust tiers of §5.3, asserted through the rendered frontmatter rather
1370    /// than through `Origin`, because the tier is what a consumer derives.
1371    #[test]
1372    fn provenance_maps_onto_the_trust_tiers() {
1373        let human = Actor::Human("pixie79".into());
1374        let at = "2026-08-28T10:00:00Z";
1375
1376        let authored = origin_for(Provenance::Authored, at, &tool(), Some(&human));
1377        let fm = Frontmatter {
1378            type_: "adr".into(),
1379            origin: Some(authored),
1380            ..Frontmatter::default()
1381        };
1382        let rendered = fm.render();
1383        assert!(
1384            rendered.contains("verified:") && rendered.contains("human:pixie79"),
1385            "authored prose is human-reviewed: {rendered}"
1386        );
1387
1388        let derived = origin_for(Provenance::Derived, at, &tool(), Some(&human));
1389        let fm = Frontmatter {
1390            type_: "fn".into(),
1391            origin: Some(derived),
1392            ..Frontmatter::default()
1393        };
1394        let rendered = fm.render();
1395        assert!(
1396            rendered.contains("verified:"),
1397            "deterministic extraction is machine-confirmed: {rendered}"
1398        );
1399        assert!(
1400            !rendered.contains("human:"),
1401            "but it is not human-reviewed — the prefix is the only thing that \
1402             separates the tiers: {rendered}"
1403        );
1404
1405        let inferred = origin_for(Provenance::Inferred, at, &tool(), Some(&human));
1406        let fm = Frontmatter {
1407            type_: "fn".into(),
1408            origin: Some(inferred),
1409            ..Frontmatter::default()
1410        };
1411        let rendered = fm.render();
1412        assert!(
1413            rendered.contains("generated:"),
1414            "a heuristic still records that it was produced: {rendered}"
1415        );
1416        assert!(
1417            !rendered.contains("verified:"),
1418            "but claims no confirmation — absence *is* the unverified tier, so an \
1419             empty list here would launder a guess: {rendered}"
1420        );
1421    }
1422
1423    /// An authored node whose author is unknown must not silently become
1424    /// machine-confirmed.
1425    #[test]
1426    fn an_authored_node_with_no_known_human_claims_nothing() {
1427        let o = origin_for(Provenance::Authored, "2026-08-28T10:00:00Z", &tool(), None);
1428        assert!(
1429            !o.confirms,
1430            "falling back to the tool would move the concept between trust tiers"
1431        );
1432    }
1433
1434    #[test]
1435    fn scalars_are_quoted_so_yaml_cannot_retype_them() {
1436        // `no`, `12:30` and `1.0` all change type when written bare.
1437        for raw in ["no", "yes", "null", "~", "12:30", "1.0", "on"] {
1438            let fm = Frontmatter {
1439                type_: raw.into(),
1440                ..Frontmatter::default()
1441            };
1442            assert_eq!(fm.render(), format!("---\ntype: \"{raw}\"\n---\n"));
1443        }
1444    }
1445
1446    /// **A value cannot break out of its own scalar.**
1447    ///
1448    /// Every scalar here comes from somewhere a person can put anything — a git
1449    /// author name, a heading, a key derived from a path. A raw newline does not
1450    /// merely make the YAML ugly: the text after it starts a new line at column
1451    /// 0, so `verified:` written inside a *title* becomes a sibling key of the
1452    /// title, and this bundle's frontmatter is what a consumer derives a trust
1453    /// tier from (§5.3). Forging `verified` is the whole attack.
1454    ///
1455    /// Asserted as *the injected key never begins a line*, not merely as "the
1456    /// output contains `\\n`": a rendering that escaped the newline but left the
1457    /// text somewhere else would satisfy the weaker check.
1458    #[test]
1459    fn a_scalar_cannot_forge_a_sibling_key() {
1460        let forged = "Innocent Title\"\nverified:\n  - by: \"human:someone-else";
1461        let fm = Frontmatter {
1462            type_: "adr".into(),
1463            title: Some(forged.to_owned()),
1464            ..Frontmatter::default()
1465        };
1466        let rendered = fm.render();
1467
1468        assert!(
1469            !rendered.lines().any(|l| l.starts_with("verified:")),
1470            "a title must not be able to open a `verified` block: {rendered}"
1471        );
1472        // Exactly three lines of frontmatter — the fences and one `type`, one
1473        // `title`. A forged key would add its own.
1474        assert_eq!(
1475            rendered.lines().count(),
1476            4,
1477            "the block must hold two keys and two fences: {rendered}"
1478        );
1479        assert!(
1480            rendered.contains("\\n"),
1481            "the newline is escaped: {rendered}"
1482        );
1483
1484        // The control characters a quoted scalar cannot hold raw, each replaced
1485        // by an escape rather than written through.
1486        for (raw, escaped) in [
1487            ("a\nb", "\\n"),
1488            ("a\rb", "\\r"),
1489            ("a\tb", "\\t"),
1490            ("a\u{0}b", "\\u0000"),
1491            ("a\u{7}b", "\\u0007"),
1492            ("a\u{1b}b", "\\u001b"),
1493            ("a\u{7f}b", "\\u007f"),
1494        ] {
1495            let out = yaml_scalar(raw);
1496            assert!(out.contains(escaped), "{raw:?} -> {out}");
1497            assert!(
1498                !out.chars().any(char::is_control),
1499                "no control character may survive into the file: {out:?}"
1500            );
1501        }
1502    }
1503
1504    #[test]
1505    fn a_nested_index_carries_no_frontmatter_but_the_root_does() {
1506        let entries = [IndexEntry {
1507            title: "ADR-0001".into(),
1508            target: "/decisions/adr-0001.md".into(),
1509            description: Some("The founding decision.".into()),
1510        }];
1511        let nested = render_index("Decisions", &entries);
1512        assert!(
1513            !nested.starts_with("---"),
1514            "§8 permits frontmatter only in the bundle root: {nested}"
1515        );
1516        assert!(nested.contains("* [ADR-0001](/decisions/adr-0001.md) - The founding decision."));
1517
1518        let root = render_root_index("Bundle", &entries);
1519        assert!(
1520            root.starts_with("---\nokf_version: \"0.2\"\n---\n"),
1521            "{root}"
1522        );
1523    }
1524
1525    #[test]
1526    fn log_days_use_iso_8601_headings() {
1527        let log = render_log(
1528            "Update Log",
1529            &[LogDay {
1530                date: "2026-08-28".into(),
1531                entries: vec!["**Update**: rebuilt from `74fad8f`.".into()],
1532            }],
1533        );
1534        assert!(log.contains("## 2026-08-28\n"), "{log}");
1535        assert!(
1536            log.contains("* **Update**: rebuilt from `74fad8f`."),
1537            "{log}"
1538        );
1539    }
1540
1541    #[test]
1542    fn concepts_are_grouped_into_per_kind_directories() {
1543        assert_eq!(section_for("adr"), "decisions");
1544        assert_eq!(section_for("adr_section"), "decisions");
1545        assert_eq!(section_for("blueprint"), "blueprints");
1546        assert_eq!(section_for("file"), "files");
1547        assert_eq!(section_for("marker"), "debt");
1548        // Every code symbol shares one directory: a reader looking for `greet`
1549        // does not know whether it is a fn, a struct or a trait.
1550        assert_eq!(section_for("fn"), "symbols");
1551        assert_eq!(section_for("struct"), "symbols");
1552        assert_eq!(section_for("trait"), "symbols");
1553    }
1554
1555    #[test]
1556    fn slugs_are_stable_and_filesystem_safe() {
1557        assert_eq!(
1558            slug("sym:rust:src/main.rs#greet"),
1559            "sym-rust-src-main-rs-greet"
1560        );
1561        assert_eq!(slug("adr:0001#decision"), "adr-0001-decision");
1562        // No trailing separator, no empty result, no run of dashes.
1563        assert_eq!(slug("a//b"), "a-b");
1564        assert_eq!(slug("trailing///"), "trailing");
1565        assert_eq!(slug("###"), "concept");
1566    }
1567
1568    /// The digest is **always eight lowercase hex digits**, whatever the key.
1569    ///
1570    /// [`MAX_SLUG`]'s headroom is written against that eight — `slug` reserves
1571    /// `MAX_SLUG - 9` for a truncated name so the dash, the digest and `.md` fit
1572    /// inside `NAME_MAX`. A digest that could be wider would silently spend that
1573    /// reservation and put the failure back where it was found: a render dying on
1574    /// `File name too long` after writing part of the bundle.
1575    ///
1576    /// Nothing about the width is visible at the call sites, which is why it is
1577    /// asserted here rather than inferred from them.
1578    #[test]
1579    fn the_digest_is_always_eight_hex_digits() {
1580        // Long, empty, unicode, and enough varied keys to reach hashes on both
1581        // sides of 2^32 — the boundary the previous rendering was sensitive to.
1582        let mut keys: Vec<String> = vec![
1583            String::new(),
1584            "a".into(),
1585            "sym:rust:src/main.rs#greet".into(),
1586            "ünïcødé::key".into(),
1587            "x".repeat(4096),
1588        ];
1589        keys.extend((0..512).map(|i| format!("sym:rust:crates/a/src/b{i}.rs#Thing{i}")));
1590
1591        for key in &keys {
1592            let digest = short_digest(key);
1593            assert_eq!(digest.len(), 8, "{key:?} -> {digest}");
1594            assert!(
1595                digest
1596                    .chars()
1597                    .all(|c| c.is_ascii_digit() || ('a'..='f').contains(&c)),
1598                "lowercase hex only: {key:?} -> {digest}"
1599            );
1600        }
1601        // Stable across calls: the disambiguation must not move between renders.
1602        assert_eq!(short_digest("adr:0001"), short_digest("adr:0001"));
1603    }
1604}