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