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