Skip to main content

dbmd_core/
parser.rs

1//! `parser` — read and write db.md markdown files.
2//!
3//! Parses the YAML frontmatter block, the markdown body, wiki-links, standard
4//! markdown links, `##` sections, and the structured sections of the `DB.md`
5//! config file. Also the atomic writer that round-trips a file while
6//! preserving the operator-edited body verbatim and emitting frontmatter in
7//! canonical key order.
8//!
9//! Strict on required fields, lenient on unknowns: any frontmatter key the
10//! spec doesn't recognize is preserved in [`Frontmatter::extra`] as ambient
11//! context and round-tripped untouched.
12
13use std::collections::BTreeMap;
14use std::path::{Path, PathBuf};
15
16use chrono::{DateTime, FixedOffset};
17use serde_norway::{Mapping, Value};
18
19/// The two canonical layer folder names. A path is "content" / a wiki-link is
20/// "full-path" only when it resolves under one of these.
21const LAYER_DIRS: [&str; 2] = ["sources", "records"];
22
23/// Errors produced while parsing a markdown file or the `DB.md` config.
24#[derive(Debug, thiserror::Error)]
25pub enum ParseError {
26    /// The frontmatter block was not valid YAML. Maps to validate code
27    /// `FM_MALFORMED_YAML`.
28    #[error("malformed YAML frontmatter in {file}: {source}")]
29    MalformedYaml {
30        /// The file whose frontmatter failed to parse.
31        file: PathBuf,
32        /// The underlying YAML error.
33        source: serde_norway::Error,
34    },
35
36    /// The file has no `---`-delimited frontmatter block at its very start.
37    #[error("missing frontmatter block in {file}")]
38    MissingFrontmatter {
39        /// The offending file.
40        file: PathBuf,
41    },
42
43    /// A required field was absent. Maps to validate code `FM_MISSING_TYPE`
44    /// (for `type`) and the per-type required-field codes.
45    #[error("missing required field '{key}' in {file}")]
46    MissingField {
47        /// The file missing the field.
48        file: PathBuf,
49        /// The required key.
50        key: String,
51    },
52
53    /// A timestamp field was not ISO-8601 / RFC3339. Maps to `FM_BAD_TIMESTAMP`.
54    #[error("bad timestamp in field '{key}' of {file}: {value}")]
55    BadTimestamp {
56        /// The file.
57        file: PathBuf,
58        /// The frontmatter key.
59        key: String,
60        /// The unparseable value.
61        value: String,
62    },
63
64    /// An I/O error reading the file.
65    #[error(transparent)]
66    Io(#[from] std::io::Error),
67}
68
69/// The parsed YAML frontmatter of a db.md file.
70///
71/// The universal-contract fields are typed accessors; everything else lands in
72/// [`extra`](Frontmatter::extra) as ambient context (unknown-field passthrough)
73/// and is round-tripped verbatim. The atomic writer re-emits keys in canonical
74/// order: `type`, `id`, `created`, `updated`, `summary` first, then
75/// type-specific fields, then `status` / `tags`.
76#[derive(Debug, Clone, Default, PartialEq)]
77pub struct Frontmatter {
78    /// `type` — required on content files; the primary query key.
79    pub type_: Option<String>,
80    /// `meta-type` — records-only; the epistemic class `fact`/`operational`/
81    /// `conclusion`. Absent ⇒ `fact` (the effective default is applied by the
82    /// index/query layer for record-layer files; sources carry none).
83    pub meta_type: Option<String>,
84    /// `id` — optional; derived from the file path when absent.
85    pub id: Option<String>,
86    /// `created` — RFC3339; required and auto-set on content-file create.
87    pub created: Option<DateTime<FixedOffset>>,
88    /// `updated` — RFC3339; required and auto-maintained on content files.
89    pub updated: Option<DateTime<FixedOffset>>,
90    /// `summary` — the one-line catalog line; required on every content file.
91    pub summary: Option<String>,
92    /// `status` — optional lifecycle state.
93    pub status: Option<String>,
94    /// `tags` — optional flat list of short scalar labels.
95    pub tags: Vec<String>,
96    /// All other frontmatter keys (type-specific + custom), preserved verbatim
97    /// in insertion-stable sorted order. Wiki-link-valued fields keep their raw
98    /// YAML form here; [`Frontmatter::link_fields`] surfaces them as
99    /// [`WikiLink`]s.
100    pub extra: BTreeMap<String, Value>,
101}
102
103impl Frontmatter {
104    /// Parse a YAML frontmatter block (the text between the opening and closing
105    /// `---` fences, exclusive) into a [`Frontmatter`].
106    ///
107    /// Lenient on unknown keys (they go to [`extra`](Frontmatter::extra));
108    /// returns [`ParseError::MalformedYaml`] only on YAML that doesn't parse.
109    pub fn parse(yaml: &str, file: &Path) -> Result<Self, ParseError> {
110        // An empty (or whitespace-only) frontmatter block is a valid, empty
111        // mapping — not a YAML error.
112        let value: Value = if yaml.trim().is_empty() {
113            Value::Mapping(Mapping::new())
114        } else {
115            serde_norway::from_str(yaml).map_err(|source| ParseError::MalformedYaml {
116                file: file.to_path_buf(),
117                source,
118            })?
119        };
120
121        // Top-level frontmatter must be a mapping. A scalar or sequence at the
122        // top level is malformed for our purposes; surface it as such.
123        let map = match value {
124            Value::Mapping(m) => m,
125            Value::Null => Mapping::new(),
126            other => {
127                // serde_norway::Error has no public constructor, so let the
128                // deserializer decide: a value that coerces to a Mapping (e.g. a
129                // YAML-tagged mapping `!tag\n k: v`, where the tag is ambient) is
130                // accepted as that mapping; a genuine scalar or sequence top
131                // level fails to coerce and IS the malformed case. (Using a
132                // match here, not `expect_err`, avoids a panic on the
133                // tagged-mapping case, which deserializes to a Mapping just
134                // fine.)
135                match serde_norway::from_value::<Mapping>(other) {
136                    Ok(m) => m,
137                    Err(source) => {
138                        return Err(ParseError::MalformedYaml {
139                            file: file.to_path_buf(),
140                            source,
141                        });
142                    }
143                }
144            }
145        };
146
147        let mut fm = Frontmatter::default();
148        for (k, v) in map {
149            let key = match k.as_str() {
150                Some(s) => s.to_string(),
151                // Non-string keys (`2026:`, `true:`, `3.14:`) are unusual but
152                // valid YAML; per SPEC § "Unknown fields pass through" they must
153                // not be corrupted on re-emit. Stringify them through the YAML
154                // scalar emitter — `2026`, `true`, `3.14` — NOT the Rust `Debug`
155                // formatter (which produced `Number(2026)`, `Bool(true)`, …), so
156                // the key text survives. `extra` is `String`-keyed, so on the
157                // write side the key re-emits as a quoted-string key carrying that
158                // text (e.g. `'2026':`) — the type narrows from number to string,
159                // but the data is no longer destroyed and ordinary string keys are
160                // wholly unaffected.
161                None => yaml_scalar_key(&k),
162            };
163            match key.as_str() {
164                // Coerce scalar values rather than `v.as_str()` (which is None
165                // for Number/Bool/Null). A bare scalar that YAML reads as a
166                // non-string — `summary: 2026`, `id: 100`, `status: 0` — would
167                // otherwise be set to None AND dropped (it is a matched arm, so
168                // the raw value never reaches `extra`), and `to_yaml` then omits
169                // the None field, so `dbmd format` (read_file -> write_file)
170                // silently deletes the line from disk. `scalar_string` mirrors
171                // the coercion `validate`/`store` already apply to these fields,
172                // so a numeric/bool-looking scalar is preserved as its string
173                // form and round-trips instead of being destroyed.
174                //
175                // A sequence/mapping value on a universal key (`status: [a, b]`,
176                // a nested-mapping `summary:`) is NOT a valid scalar; rather than
177                // let the matched arm consume-and-drop it (silent data loss on
178                // the next re-emit), `scalar_string` returns None and we fall
179                // through to preserving the raw value in `extra` so `to_yaml`
180                // re-emits it verbatim. The universal accessors stay None (the
181                // value was never a valid scalar for that field), but the
182                // operator's bytes are never destroyed.
183                "type" => match scalar_string(&v) {
184                    Some(s) => fm.type_ = Some(s),
185                    None => {
186                        fm.extra.insert(key, v);
187                    }
188                },
189                "meta-type" => match scalar_string(&v) {
190                    Some(s) => fm.meta_type = Some(s),
191                    None => {
192                        fm.extra.insert(key, v);
193                    }
194                },
195                "id" => match scalar_string(&v) {
196                    Some(s) => fm.id = Some(s),
197                    None => {
198                        fm.extra.insert(key, v);
199                    }
200                },
201                "created" => fm.created = parse_timestamp(&v, "created", file)?,
202                "updated" => fm.updated = parse_timestamp(&v, "updated", file)?,
203                "summary" => match scalar_string(&v) {
204                    Some(s) => fm.summary = Some(s),
205                    None => {
206                        fm.extra.insert(key, v);
207                    }
208                },
209                "status" => match scalar_string(&v) {
210                    Some(s) => fm.status = Some(s),
211                    None => {
212                        fm.extra.insert(key, v);
213                    }
214                },
215                "tags" => match parse_tags_preserving(&v) {
216                    Ok(tags) => fm.tags = tags,
217                    // A `tags` value with a non-scalar item (`tags: [[vip]]`,
218                    // `tags: [a, [b]]`) is preserved verbatim in `extra` rather
219                    // than silently filtered down / erased on re-emit. The typed
220                    // `tags` vec stays empty (no valid scalar list was present),
221                    // so `to_yaml` won't ALSO emit a `tags:` from the vec.
222                    Err(raw) => {
223                        fm.extra.insert(key, raw);
224                    }
225                },
226                _ => {
227                    fm.extra.insert(key, v);
228                }
229            }
230        }
231        Ok(fm)
232    }
233
234    /// Serialize the frontmatter back to a YAML block (no `---` fences) in
235    /// canonical key order. Round-trips [`extra`](Frontmatter::extra) verbatim.
236    pub fn to_yaml(&self) -> String {
237        // Build an order-preserving mapping in canonical key order:
238        //   type, meta-type, id, created, updated, summary  (universal head)
239        //   <type-specific extra, BTreeMap-sorted>
240        //   status, tags                          (universal tail)
241        // serde_norway::Mapping preserves insertion order, so one serialize call
242        // emits the block in exactly this order with correct YAML quoting.
243        let mut map = Mapping::new();
244
245        if let Some(t) = &self.type_ {
246            map.insert(Value::String("type".into()), Value::String(t.clone()));
247        }
248        if let Some(mt) = &self.meta_type {
249            map.insert(Value::String("meta-type".into()), Value::String(mt.clone()));
250        }
251        if let Some(id) = &self.id {
252            map.insert(Value::String("id".into()), Value::String(id.clone()));
253        }
254        if let Some(created) = &self.created {
255            map.insert(
256                Value::String("created".into()),
257                Value::String(created.to_rfc3339()),
258            );
259        }
260        if let Some(updated) = &self.updated {
261            map.insert(
262                Value::String("updated".into()),
263                Value::String(updated.to_rfc3339()),
264            );
265        }
266        if let Some(summary) = &self.summary {
267            map.insert(
268                Value::String("summary".into()),
269                Value::String(summary.clone()),
270            );
271        }
272
273        // Type-specific + custom fields, in BTreeMap (sorted) order. Each value
274        // is canonicalized so a wiki-link round-trips to the form the writer and
275        // `dbmd validate` agree on — critically, the SPEC-canonical *unquoted*
276        // scalar `field: [[x]]` (which YAML parses to a nested `Seq[Seq[String]]`)
277        // is re-emitted as a quoted scalar `'[[x]]'` instead of the bracket-less
278        // block sequence `- - x` that a verbatim re-emit would produce and that
279        // destroys the link. See [`canonicalize_extra_value`].
280        for (k, v) in &self.extra {
281            map.insert(Value::String(k.clone()), canonicalize_extra_value(v));
282        }
283
284        if let Some(status) = &self.status {
285            map.insert(
286                Value::String("status".into()),
287                Value::String(status.clone()),
288            );
289        }
290        if !self.tags.is_empty() {
291            map.insert(
292                Value::String("tags".into()),
293                Value::Sequence(self.tags.iter().cloned().map(Value::String).collect()),
294            );
295        }
296
297        if map.is_empty() {
298            return String::new();
299        }
300        serde_norway::to_string(&Value::Mapping(map)).unwrap_or_default()
301    }
302
303    /// True if the file is content (under `sources/` or `records/`)
304    /// and not an `index.md`. Used by validate to decide which files require a
305    /// `summary`. Meta files (`DB.md`, `index.md`, `log.md`) return false.
306    pub fn is_content_file(path: &Path) -> bool {
307        // index.md is a meta file at every level, never content.
308        if path.file_name().and_then(|n| n.to_str()) == Some("index.md") {
309            return false;
310        }
311        // Content iff some path component is one of the two layer dirs. This
312        // works for both store-relative (`sources/emails/x.md`) and absolute
313        // (`/home/db/sources/emails/x.md`) paths. DB.md / log.md sit at the
314        // root, under no layer, so they fall through to false.
315        path.components().any(|c| {
316            c.as_os_str()
317                .to_str()
318                .is_some_and(|s| LAYER_DIRS.contains(&s))
319        })
320    }
321
322    /// Resolve the file's effective `id`: the explicit `id` field if present,
323    /// otherwise derived from the store-relative path (filename without `.md`).
324    pub fn effective_id(&self, store_relative_path: &Path) -> String {
325        if let Some(id) = &self.id {
326            if !id.is_empty() {
327                return id.clone();
328            }
329        }
330        // Derived id = filename without the `.md` extension.
331        store_relative_path
332            .file_stem()
333            .and_then(|s| s.to_str())
334            .unwrap_or_default()
335            .to_string()
336    }
337
338    /// The effective `meta-type` for a record: the declared value, or `fact`
339    /// when absent. Records only — sources carry no meta-type; callers apply
340    /// this only to record-layer files.
341    pub fn effective_meta_type(&self) -> &str {
342        self.meta_type.as_deref().unwrap_or("fact")
343    }
344
345    /// Read a single frontmatter key as a raw YAML [`Value`], looking in the
346    /// typed fields first and then [`extra`](Frontmatter::extra).
347    pub fn get(&self, key: &str) -> Option<Value> {
348        match key {
349            "type" => self.type_.clone().map(Value::String),
350            "meta-type" => self.meta_type.clone().map(Value::String),
351            "id" => self.id.clone().map(Value::String),
352            "created" => self.created.map(|d| Value::String(d.to_rfc3339())),
353            "updated" => self.updated.map(|d| Value::String(d.to_rfc3339())),
354            "summary" => self.summary.clone().map(Value::String),
355            "status" => self.status.clone().map(Value::String),
356            "tags" => {
357                if self.tags.is_empty() {
358                    None
359                } else {
360                    Some(Value::Sequence(
361                        self.tags.iter().cloned().map(Value::String).collect(),
362                    ))
363                }
364            }
365            _ => self.extra.get(key).cloned(),
366        }
367    }
368
369    /// Set a single frontmatter key from a string value, routing universal-
370    /// contract keys to their typed fields and everything else to
371    /// [`extra`](Frontmatter::extra). Used by `dbmd fm set`.
372    pub fn set(&mut self, key: &str, value: &str) -> Result<(), ParseError> {
373        match key {
374            "type" => self.type_ = Some(value.to_string()),
375            "meta-type" => self.meta_type = Some(value.to_string()),
376            "id" => self.id = Some(value.to_string()),
377            "created" => {
378                self.created = Some(parse_rfc3339(value, "created", Path::new("<fm set>"))?)
379            }
380            "updated" => {
381                self.updated = Some(parse_rfc3339(value, "updated", Path::new("<fm set>"))?)
382            }
383            "summary" => self.summary = Some(value.to_string()),
384            "status" => self.status = Some(value.to_string()),
385            "tags" => {
386                // Accept either a YAML flow list (`[a, b]`) or a single scalar
387                // tag. Anything that parses to a sequence becomes the tag list;
388                // otherwise the whole string is one tag.
389                self.tags = match serde_norway::from_str::<Value>(value) {
390                    Ok(Value::Sequence(seq)) => parse_tags(&Value::Sequence(seq)),
391                    _ => vec![value.to_string()],
392                };
393            }
394            _ => {
395                // A custom / type-specific field. The value is a scalar string by
396                // default, but the spec's list-valued link fields (e.g.
397                // `meeting.attendees`, SPEC § Linking) must serialize as a YAML
398                // block sequence of quoted wiki-links — never the flow-form string
399                // `"[[[a]], [[b]]]"`, which `dbmd validate` rejects as
400                // `WIKI_LINK_FLOW_FORM_LIST`. When the value parses as a YAML
401                // sequence whose every item is a clean single wiki-link, store the
402                // canonical sequence so `to_yaml` emits block form. Everything else
403                // — plain text, and a single inline `[[x]]` (which YAML reads as a
404                // nested `Seq[Seq[String]]`, not a list of link strings) — stays a
405                // verbatim scalar string, preserving the prior behavior.
406                let stored = parse_link_list_value(value)
407                    .unwrap_or_else(|| Value::String(value.to_string()));
408                self.extra.insert(key.to_string(), stored);
409            }
410        }
411        Ok(())
412    }
413
414    /// Extract every frontmatter field whose value is a wiki-link (scalar
415    /// inline form or a block-sequence list), pairing each with its key. The
416    /// validate engine checks these against `(link)` schema annotations.
417    pub fn link_fields(&self) -> Vec<(String, WikiLink)> {
418        let mut out = Vec::new();
419        // `summary` may carry navigational wiki-links (spec encourages it).
420        if let Some(summary) = &self.summary {
421            for link in extract_wiki_links(summary, Path::new("")) {
422                out.push(("summary".to_string(), link));
423            }
424        }
425        // Every type-specific / custom field: a scalar wiki-link or a list of
426        // wiki-links, in either the quoted (`"[[x]]"`) or the canonical unquoted
427        // (`[[x]]`) form. See [`links_in_field_value`] for the YAML shapes.
428        for (key, value) in &self.extra {
429            for link in links_in_field_value(value) {
430                out.push((key.clone(), link));
431            }
432        }
433        out
434    }
435}
436
437/// A wiki-link reference inside the store: `[[target]]` or `[[target|display]]`.
438///
439/// `target` is always recorded as written; [`is_full_path`](WikiLink::is_full_path)
440/// flags whether it's a full store-relative path (the doctrine) versus a
441/// short-form (a validation error).
442#[derive(Debug, Clone, PartialEq, Eq)]
443pub struct WikiLink {
444    /// The link target as written, without the `[[ ]]` and without `|display`.
445    pub target: String,
446    /// The optional `|display` text override.
447    pub display: Option<String>,
448    /// True when `target` is a full store-relative path (contains a `/` and
449    /// resolves under a known layer); false for short-form targets like
450    /// `sarah-chen` — which validate reports as `WIKI_LINK_SHORT_FORM`.
451    pub is_full_path: bool,
452    /// True when `target` carries a trailing `.md` extension — validate warns
453    /// `WIKI_LINK_HAS_EXTENSION`; the canonical writers emit the bare form.
454    pub has_md_extension: bool,
455    /// Where the link appears: `(file, line, col)`, 1-based line and column.
456    pub location: (PathBuf, u32, u32),
457}
458
459/// A standard markdown link `[text](url)` — an external reference, kept in a
460/// stream separate from [`WikiLink`] so external targets are visible to the
461/// toolkit without being conflated with in-store edges. Not graph-validated.
462#[derive(Debug, Clone, PartialEq, Eq)]
463pub struct MarkdownLink {
464    /// The link text inside `[ ]`.
465    pub text: String,
466    /// The URL or path inside `( )`.
467    pub url: String,
468    /// Where the link appears: `(file, line, col)`, 1-based.
469    pub location: (PathBuf, u32, u32),
470}
471
472/// A `##`/`###` section of a markdown body: the heading text plus the byte
473/// slice of the body it spans (heading line through the line before the next
474/// heading of equal-or-shallower depth).
475#[derive(Debug, Clone, PartialEq, Eq)]
476pub struct Section {
477    /// The heading text (without the leading `#`s).
478    pub heading: String,
479    /// Heading depth (number of leading `#`s).
480    pub level: u8,
481    /// The 1-based line where the heading appears.
482    pub line: u32,
483    /// The section body, from the heading line to the next sibling-or-shallower
484    /// heading (exclusive), as a slice of the original body.
485    pub body: String,
486}
487
488/// The parsed structured content of a store's `DB.md` config file.
489///
490/// All four parts are optional in the source; absent parts fall back to spec
491/// defaults. Produced by [`parse_db_md`].
492#[derive(Debug, Clone, Default, PartialEq)]
493pub struct Config {
494    /// Body of the `## Agent instructions` section — free-form prose passed to
495    /// the agent's system prompt.
496    pub agent_instructions: Option<String>,
497    /// `## Policies` → `### Frozen pages`: store-relative paths the toolkit
498    /// refuses to write (`POLICY_FROZEN_PAGE`).
499    pub frozen_pages: Vec<PathBuf>,
500    /// `## Policies` → `### Ignored types`: type names the curator never
501    /// synthesizes (still readable as ambient context).
502    pub ignored_types: Vec<String>,
503    /// `## Schemas` → one entry per `### <type>` sub-section.
504    pub schemas: BTreeMap<String, Schema>,
505    /// `## Folders` → optional per-folder display + description, surfaced in the
506    /// root + layer `index.md` rollups. Agent-authored; the tool never invents a
507    /// folder's description (absent ⇒ the rollup shows counts only). Keyed by the
508    /// store-relative, unix-slash folder path (e.g. `records/contacts`).
509    pub folders: BTreeMap<String, FolderMeta>,
510}
511
512/// Agent-authored display + description for one type-folder, declared in
513/// `DB.md ## Folders` and surfaced in the root/layer `index.md` rollups. Both
514/// fields are optional: `display` overrides the rollup's derived folder name
515/// (for casing the tool can't guess, e.g. acronyms like HubSpot); `description`
516/// is the one-line "what's in here" the rollup shows. The tool only ever
517/// *surfaces* these — it never composes a folder description from the folder's
518/// contents (that would be the tool inventing the curator's judgment).
519#[derive(Debug, Clone, Default, PartialEq, Eq)]
520pub struct FolderMeta {
521    /// Display-name override (absent ⇒ derived from the folder basename).
522    pub display: Option<String>,
523    /// One-line folder description shown in the rollup (absent ⇒ counts only).
524    pub description: Option<String>,
525}
526
527impl Config {
528    /// The `### Frozen pages` entry that matches a store-relative `target`, if
529    /// any. The **single** frozen-page matcher every write surface must funnel
530    /// through so the policy is enforced identically on `write` / `fm set` /
531    /// `fm init` / `link` / `rename` / `format`.
532    ///
533    /// Comparison is normalized so a policy line and a write target match
534    /// regardless of incidental spelling differences:
535    /// - `/` path separators on every OS,
536    /// - a single leading `./` dropped,
537    /// - a trailing `.md` dropped on **both** sides — `parse_db_md` stores
538    ///   frozen entries verbatim, so an operator who writes the natural
539    ///   extensionless spelling (`records/decisions/q1`) must protect the file
540    ///   (`records/decisions/q1.md`) exactly as the `.md` spelling does.
541    ///
542    /// Returns the matched config entry verbatim (its original spelling) so the
543    /// caller can name it in the `POLICY_FROZEN_PAGE` refusal.
544    pub fn frozen_match(&self, target: &Path) -> Option<PathBuf> {
545        let want = normalize_frozen_path(target);
546        self.frozen_pages
547            .iter()
548            .find(|frozen| {
549                let pat = normalize_frozen_path(frozen);
550                // A literal entry matches by exact normalized equality; an entry
551                // carrying a `*`/`**` glob matches by segment-wise glob so a
552                // pattern like `records/decisions/*` actually protects the
553                // concrete files under it instead of silently failing open.
554                if pat.contains('*') {
555                    frozen_glob_matches(&pat, &want)
556                } else {
557                    pat == want
558                }
559            })
560            .cloned()
561    }
562
563    /// True if `target` (store-relative) is a frozen page. Convenience wrapper
564    /// over [`Config::frozen_match`] for callers that only need presence.
565    pub fn is_frozen(&self, target: &Path) -> bool {
566        self.frozen_match(target).is_some()
567    }
568}
569
570/// Normalize a path for frozen-page comparison: `/` separators, a leading `./`
571/// or `/` dropped, and a trailing `.md` dropped. Both the policy entry and the
572/// write target pass through this before equality/glob, so the match is
573/// separator-, `./`-, leading-`/`-, and `.md`-insensitive. Without the leading
574/// `/` drop, an operator who wrote `/records/decisions/q1.md` normalized to a
575/// path that never equals the target's `records/decisions/q1`, silently failing
576/// the freeze OPEN.
577fn normalize_frozen_path(p: &Path) -> String {
578    use std::path::Component;
579    // Keep only the `Normal` path segments, dropping `RootDir`/`Prefix` (a
580    // leading `/` or drive prefix) and `CurDir` (`.`). This is what makes a
581    // leading-slash entry (`/records/decisions/q1.md`) normalize to the same
582    // `records/decisions/q1` as the store-relative target, instead of the
583    // doubled-`//` prefix `Path::components` + naive join produced — which never
584    // equalled the target and silently failed the freeze OPEN.
585    let unix: String = p
586        .components()
587        .filter_map(|c| match c {
588            Component::Normal(s) => s.to_str(),
589            _ => None,
590        })
591        .collect::<Vec<_>>()
592        .join("/");
593    unix.strip_suffix(".md").unwrap_or(&unix).to_string()
594}
595
596/// Match a normalized frozen-page glob `pat` against a normalized target `path`,
597/// segment by segment. `*` matches any run of characters *within a single path
598/// segment* (never crossing `/`); `**` as a whole segment matches zero or more
599/// whole segments. Both sides are already `normalize_frozen_path`-normalized, so
600/// this only deals with `/`-joined segment text. Keeps the substrate dependency-
601/// free (no glob crate) while making `records/decisions/*` actually freeze the
602/// files beneath it instead of failing open.
603fn frozen_glob_matches(pat: &str, path: &str) -> bool {
604    let pat_segs: Vec<&str> = pat.split('/').collect();
605    let path_segs: Vec<&str> = path.split('/').collect();
606    glob_segments(&pat_segs, &path_segs)
607}
608
609/// Recursive segment matcher for [`frozen_glob_matches`]. `**` consumes any
610/// number of path segments; every other pattern segment must match exactly one
611/// path segment (with `*` wildcards inside it).
612fn glob_segments(pat: &[&str], path: &[&str]) -> bool {
613    match pat.split_first() {
614        None => path.is_empty(),
615        Some((&"**", rest_pat)) => {
616            // `**` matches zero segments here, or one-or-more by consuming a path
617            // segment and recursing on the same `**`.
618            if glob_segments(rest_pat, path) {
619                return true;
620            }
621            !path.is_empty() && glob_segments(pat, &path[1..])
622        }
623        Some((&first_pat, rest_pat)) => match path.split_first() {
624            Some((&first_path, rest_path)) => {
625                glob_segment_text(first_pat, first_path) && glob_segments(rest_pat, rest_path)
626            }
627            None => false,
628        },
629    }
630}
631
632/// Match a single glob segment against a single path segment. `*` matches any
633/// run of characters within the segment; all other characters are literal.
634fn glob_segment_text(pat: &str, seg: &str) -> bool {
635    if !pat.contains('*') {
636        return pat == seg;
637    }
638    // Split on `*` into literal fragments. The first fragment must be a prefix,
639    // the last a suffix, and the middle fragments must appear in order.
640    let parts: Vec<&str> = pat.split('*').collect();
641    let mut pos = 0usize;
642    for (idx, part) in parts.iter().enumerate() {
643        if part.is_empty() {
644            continue;
645        }
646        if idx == 0 {
647            // Leading literal must be a prefix.
648            if !seg[pos..].starts_with(part) {
649                return false;
650            }
651            pos += part.len();
652        } else if idx == parts.len() - 1 {
653            // Trailing literal must be a suffix at or after the current cursor.
654            return seg[pos..].ends_with(part);
655        } else {
656            // Interior literal: find it at or after the cursor.
657            match seg[pos..].find(part) {
658                Some(off) => pos += off + part.len(),
659                None => return false,
660            }
661        }
662    }
663    true
664}
665
666/// A user-declared type schema parsed from a `DB.md` `### <type>` sub-section.
667/// The store's `## Schemas` is the **only** source of schema enforcement — the
668/// toolkit ships no built-in or implicit per-type schema (see SPEC § Schemas).
669#[derive(Debug, Clone, Default, PartialEq)]
670pub struct Schema {
671    /// One [`FieldSpec`] per bulleted field line, in source order.
672    pub fields: Vec<FieldSpec>,
673    /// `- unique: <field>[, <field> …]` directives — each inner vec is one
674    /// uniqueness constraint over the listed field(s) (compound when >1). Two
675    /// records of this type whose listed values collide warn as
676    /// `DUP_UNIQUE_KEY`.
677    pub unique_keys: Vec<Vec<String>>,
678    /// `- summary_template: <template>` directive — the `{field}` interpolation
679    /// pattern `dbmd fm init` / `dbmd write` use to compose a default `summary`
680    /// for this type. `None` falls back to the body's first paragraph.
681    pub summary_template: Option<String>,
682    /// `- shard: by-date | flat` directive — whether records of this type are
683    /// date-sharded on disk (`records/<type>/<YYYY>/<MM>/…`) or kept flat.
684    /// `None` = no directive declared, so the store's built-in default for the
685    /// type applies ([`crate::store::Store::type_shards`]); `Some(true)` forces
686    /// date-sharding (e.g. a custom event type the toolkit has no built-in for);
687    /// `Some(false)` forces flat. This is the v0.2 generic-model way to declare
688    /// sharding — the toolkit ships no implicit per-type behavior beyond the
689    /// example-type defaults.
690    pub shard: Option<bool>,
691}
692
693/// One field declaration inside a [`Schema`]: `- <name> (<modifiers>)`.
694///
695/// Modifiers are comma-separated inside the parens; this captures the
696/// recognized ones as typed fields and stashes anything unrecognized in
697/// [`unknown_modifiers`](FieldSpec::unknown_modifiers) (surfaced as `Info`).
698#[derive(Debug, Clone, Default, PartialEq)]
699pub struct FieldSpec {
700    /// The field name.
701    pub name: String,
702    /// `required` modifier present.
703    pub required: bool,
704    /// The shape modifier (`string`/`int`/`bool`/`date`/`email`/`currency`/
705    /// `url`), if any.
706    pub shape: Option<Shape>,
707    /// `link to <prefix>/` — the store-relative prefix a wiki-link target must
708    /// start with. The trailing slash is required in the source syntax.
709    pub link_prefix: Option<PathBuf>,
710    /// `default <value>` — the value written when the field is absent.
711    pub default: Option<Value>,
712    /// `enum: <v1>, <v2>, ...` — the allowed values (must be the last modifier
713    /// on the line because of its own commas).
714    pub enum_values: Option<Vec<String>>,
715    /// Any modifiers not in the recognized vocabulary, preserved verbatim;
716    /// validate surfaces these as `Info`, never errors.
717    pub unknown_modifiers: Vec<String>,
718}
719
720/// A recognized shape modifier for a schema field. Validate enforces the
721/// corresponding value shape (`SCHEMA_SHAPE_MISMATCH` on violation).
722#[derive(Debug, Clone, Copy, PartialEq, Eq)]
723pub enum Shape {
724    /// Any scalar string.
725    String,
726    /// Integer.
727    Int,
728    /// Boolean.
729    Bool,
730    /// RFC3339 / ISO-8601 date.
731    Date,
732    /// `<local>@<domain>` email address.
733    Email,
734    /// A currency amount.
735    Currency,
736    /// A URL.
737    Url,
738}
739
740/// The result of splitting a raw file into its frontmatter block and body.
741///
742/// `body` is the verbatim remainder after the closing `---` fence — the writer
743/// preserves it byte-for-byte so operator edits are never reflowed.
744#[derive(Debug, Clone, PartialEq, Eq)]
745pub struct ParsedFile {
746    /// The raw frontmatter YAML (between the fences, exclusive of them).
747    pub frontmatter_yaml: String,
748    /// The verbatim body (everything after the closing `---`).
749    pub body: String,
750}
751
752/// Split a file's full text into its frontmatter block and body. The
753/// frontmatter block must be the very first thing in the file, delimited by
754/// `---` on its own line at start and end. Returns
755/// [`ParseError::MissingFrontmatter`] if absent.
756pub fn split_frontmatter(text: &str, file: &Path) -> Result<ParsedFile, ParseError> {
757    // Tolerate a single leading UTF-8 BOM (U+FEFF) before the opening fence,
758    // matching `store::frontmatter_block` and `index::extract_frontmatter_block`
759    // which already strip it. Without this, a BOM-prefixed file (common from
760    // Windows / exported markdown dropped into `sources/`) gets walked and
761    // indexed by `dbmd index` yet hard-fails every write/edit surface that
762    // routes through `read_file` (`fm get/set`, `format`, `link`, `write`). The
763    // BOM is dropped from the emitted body so the canonical writer never carries
764    // it forward.
765    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
766
767    // The opening fence must be the very first line: `---`, no leading
768    // whitespace, nothing before it. Trailing whitespace on the fence line is
769    // tolerated via `trim_end()` (which strips spaces/tabs as well as CR/LF) so
770    // this matches `index::extract_frontmatter_block` and
771    // `validate::split_frontmatter`, both of which use `trim_end()`. Without this
772    // agreement a fence written `--- ` (a single trailing space — invisible in an
773    // editor, easily produced by hand edits or exporters) was indexed and
774    // validated clean by those scanners yet hard-failed every write/edit surface
775    // routed through `read_file` (`fm get/set`, `format`, `link`, `write`) — the
776    // same cross-scanner drift class already fixed for the UTF-8 BOM above.
777    let mut lines = text.split_inclusive('\n');
778    let first = lines.next().unwrap_or("");
779    if first.trim_end() != "---" {
780        return Err(ParseError::MissingFrontmatter {
781            file: file.to_path_buf(),
782        });
783    }
784
785    // Scan for the closing fence line. Track byte offsets so we can slice the
786    // YAML (between fences, exclusive) and the body (verbatim, after the
787    // closing fence's line terminator).
788    let opening_len = first.len();
789    let mut offset = opening_len;
790    for line in lines {
791        if line.trim_end() == "---" {
792            let yaml = &text[opening_len..offset];
793            let body_start = offset + line.len();
794            let body = &text[body_start..];
795            return Ok(ParsedFile {
796                frontmatter_yaml: yaml.to_string(),
797                body: body.to_string(),
798            });
799        }
800        offset += line.len();
801    }
802
803    // Opening fence present but no closing fence: malformed frontmatter block.
804    Err(ParseError::MissingFrontmatter {
805        file: file.to_path_buf(),
806    })
807}
808
809/// Read a file from disk and parse it into typed [`Frontmatter`] plus the
810/// verbatim body string.
811pub fn read_file(path: &Path) -> Result<(Frontmatter, String), ParseError> {
812    let text = std::fs::read_to_string(path)?;
813    let parsed = split_frontmatter(&text, path)?;
814    let fm = Frontmatter::parse(&parsed.frontmatter_yaml, path)?;
815    Ok((fm, parsed.body))
816}
817
818/// Atomically write a markdown file from frontmatter + body: emit the
819/// frontmatter in canonical key order, then the body verbatim, via a
820/// temp-file-rename so a reader never sees a half-written file. Preserves the
821/// operator-edited body exactly as given.
822pub fn write_file(path: &Path, frontmatter: &Frontmatter, body: &str) -> Result<(), ParseError> {
823    let contents = render_file(frontmatter, body);
824
825    // One durable, atomic write for all primary data (see `crate::fsx`):
826    // temp-file + fsync + rename + parent-fsync. Content records are primary
827    // data, so they get the durable path (unlike the rebuildable index).
828    crate::fsx::write_atomic(path, contents.as_bytes())?;
829    Ok(())
830}
831
832/// Atomically create a markdown file from frontmatter + body, refusing with
833/// [`std::io::ErrorKind::AlreadyExists`] if the destination already exists.
834///
835/// This is the create-new sibling of [`write_file`]: same canonical rendering
836/// and durable temp-file path, but backed by [`crate::fsx::write_atomic_new`] so
837/// two concurrent creators for the same path cannot both succeed.
838pub fn write_file_new(
839    path: &Path,
840    frontmatter: &Frontmatter,
841    body: &str,
842) -> Result<(), ParseError> {
843    let contents = render_file(frontmatter, body);
844    crate::fsx::write_atomic_new(path, contents.as_bytes())?;
845    Ok(())
846}
847
848fn render_file(frontmatter: &Frontmatter, body: &str) -> String {
849    let yaml = frontmatter.to_yaml();
850    // `to_yaml` already terminates each block with a newline. Compose the file
851    // as: opening fence, frontmatter YAML, closing fence, then body verbatim.
852    let mut contents = String::with_capacity(yaml.len() + body.len() + 8);
853    contents.push_str("---\n");
854    contents.push_str(&yaml);
855    contents.push_str("---\n");
856    contents.push_str(body);
857    contents
858}
859
860/// Extract every wiki-link from a body (and inline frontmatter), returning the
861/// structured [`WikiLink`] stream with short-form / `.md`-extension flags and
862/// `(file, line, col)` locations set.
863pub fn extract_wiki_links(body: &str, file: &Path) -> Vec<WikiLink> {
864    static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
865    let re = RE.get_or_init(|| {
866        // [[target]] or [[target|display]]; target/display exclude brackets and
867        // (for target) the `|` separator so nested forms don't over-match.
868        regex::Regex::new(r"\[\[([^\[\]|]+?)(?:\|([^\[\]]*))?\]\]").expect("valid wiki-link regex")
869    });
870
871    let mut out = Vec::new();
872    for (line_idx, line) in body.lines().enumerate() {
873        // Running (byte, char) cursor: derive each match's column in ONE linear
874        // pass over the line instead of recomputing it from the line start per
875        // match. `captures_iter` yields non-overlapping matches in increasing
876        // byte order, so advancing the char count by the gap since the previous
877        // match keeps the whole line O(line_len) rather than O(matches × len).
878        let mut cursor = ColCursor::new();
879        for caps in re.captures_iter(line) {
880            let whole = caps.get(0).expect("group 0 always present");
881            let col = cursor.column_at(line, whole.start());
882            let target = caps.get(1).map(|m| m.as_str()).unwrap_or("").to_string();
883            let display = caps.get(2).map(|m| m.as_str().to_string());
884            out.push(WikiLink {
885                is_full_path: target_is_full_path(&target),
886                has_md_extension: target_has_md_extension(&target),
887                target,
888                display,
889                location: (file.to_path_buf(), (line_idx as u32) + 1, col),
890            });
891        }
892    }
893    out
894}
895
896/// Extract every standard markdown link `[text](url)` from a body into a
897/// separate stream, kept distinct from wiki-links.
898pub fn extract_markdown_links(body: &str, file: &Path) -> Vec<MarkdownLink> {
899    static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
900    let re = RE.get_or_init(|| {
901        // [text](url). `text` excludes brackets so a wiki-link `[[x]]` (which
902        // has `]]`, not `](`) never matches; `url` excludes `)` and whitespace.
903        regex::Regex::new(r"\[([^\[\]]*)\]\(([^)\s]*)\)").expect("valid markdown-link regex")
904    });
905
906    let mut out = Vec::new();
907    for (line_idx, line) in body.lines().enumerate() {
908        // One linear column cursor per line (see `extract_wiki_links`): avoids the
909        // O(matches × line_len) recompute on a link-dense line.
910        let mut cursor = ColCursor::new();
911        for caps in re.captures_iter(line) {
912            let whole = caps.get(0).expect("group 0 always present");
913            let col = cursor.column_at(line, whole.start());
914            out.push(MarkdownLink {
915                text: caps.get(1).map(|m| m.as_str()).unwrap_or("").to_string(),
916                url: caps.get(2).map(|m| m.as_str()).unwrap_or("").to_string(),
917                location: (file.to_path_buf(), (line_idx as u32) + 1, col),
918            });
919        }
920    }
921    out
922}
923
924/// Detect the frontmatter wiki-link-list mis-encoding: a wiki-link *list*
925/// written so YAML parses it as nested sequences instead of a clean list of
926/// strings. Returns the offending keys so validate can emit
927/// `WIKI_LINK_FLOW_FORM_LIST`.
928///
929/// The subtlety is that `[[x]]` is YAML for "a list containing `[x]`", so the
930/// shapes nest:
931///
932/// - **Scalar inline** `company: [[records/x]]` → `Seq[ Seq[String] ]`
933///   (double-nested). This is the spec's scalar wiki-link form — NOT flagged.
934/// - **Flow list** `attendees: [[[a]], [[b]]]` → `Seq[ Seq[Seq[String]], … ]`
935///   (triple-nested). The list mis-encoding — flagged.
936/// - **Unquoted block list** (`- [[a]]` per line) → also triple-nested, so it
937///   is flagged too; the canonical list form must quote each item
938///   (`- "[[a]]"`), which parses to a clean `Seq[String, …]` and is NOT flagged.
939///
940/// So the discriminator is nesting depth: a *list* mis-encoding has at least one
941/// item that is itself a sequence-of-sequences, whereas a scalar inline link's
942/// single item is a sequence-of-scalars.
943pub fn detect_flow_form_link_lists(frontmatter_yaml: &str) -> Vec<String> {
944    let value: Value = match serde_norway::from_str(frontmatter_yaml) {
945        Ok(v) => v,
946        // Malformed YAML is FM_MALFORMED_YAML's job, not ours; report nothing.
947        Err(_) => return Vec::new(),
948    };
949    let Value::Mapping(map) = value else {
950        return Vec::new();
951    };
952
953    let mut out = Vec::new();
954    for (k, v) in &map {
955        if let Value::Sequence(items) = v {
956            // Triple-nesting: some outer item is a sequence that itself holds a
957            // sequence. Scalar inline `[[x]]` is only double-nested, so it
958            // never matches.
959            let is_link_list = items.iter().any(|item| match item {
960                Value::Sequence(inner) => inner.iter().any(|x| matches!(x, Value::Sequence(_))),
961                _ => false,
962            });
963            if is_link_list {
964                if let Some(key) = k.as_str() {
965                    out.push(key.to_string());
966                }
967            }
968        }
969    }
970    out
971}
972
973/// Extract the `##`/`###` sections of a markdown body into a flat list with
974/// body slices.
975pub fn extract_sections(body: &str) -> Vec<Section> {
976    // Keep each line's start so we can slice the body verbatim (exact newlines).
977    let lines: Vec<&str> = body.split_inclusive('\n').collect();
978
979    // First pass: classify heading levels (0 = not a heading), honoring fenced
980    // code blocks so a `## x` inside a ``` fence is not treated as a heading.
981    let mut levels: Vec<u8> = Vec::with_capacity(lines.len());
982    let mut fence: Option<(u8, usize)> = None;
983    for line in &lines {
984        let content = line.trim_end_matches(['\n', '\r']);
985        if let Some(f) = fence {
986            if is_closing_fence(content, f) {
987                fence = None;
988            }
989            levels.push(0);
990            continue;
991        }
992        if let Some(opened) = opening_fence(content) {
993            fence = Some(opened);
994            levels.push(0);
995            continue;
996        }
997        levels.push(heading_level(content));
998    }
999
1000    // Second pass: emit `##`+ headings; each section body runs from its heading
1001    // line to the next heading at an equal-or-shallower level (exclusive).
1002    let mut sections = Vec::new();
1003    for (i, &lvl) in levels.iter().enumerate() {
1004        if lvl < 2 {
1005            continue;
1006        }
1007        let heading_line = lines[i].trim_end_matches(['\n', '\r']);
1008        let heading = heading_text(heading_line, lvl);
1009
1010        let mut end = lines.len();
1011        for (j, &other) in levels.iter().enumerate().skip(i + 1) {
1012            if other != 0 && other <= lvl {
1013                end = j;
1014                break;
1015            }
1016        }
1017
1018        sections.push(Section {
1019            heading,
1020            level: lvl,
1021            line: (i + 1) as u32,
1022            body: lines[i..end].concat(),
1023        });
1024    }
1025    sections
1026}
1027
1028/// Extract the `##`/`###` sections of a **whole file** (frontmatter + body),
1029/// returning each [`Section`] with `line` numbered against the *source file*,
1030/// not the body.
1031///
1032/// [`extract_sections`] numbers headings 1-based within the body it is handed —
1033/// the right frame for callers that already track the frontmatter offset
1034/// (`validate` adds `fm_end_line`). But the single-file views (`dbmd sections`,
1035/// `dbmd outline`) present `Section::line` as a source line an agent can jump to;
1036/// because every db.md file opens with a frontmatter block, the body-relative
1037/// number is off by the block's length (`opening fence + frontmatter lines +
1038/// closing fence`) for every file. This helper does the offset once, in the
1039/// parser, so those surfaces report true file lines. A file with no leading
1040/// frontmatter block is treated as all-body (offset 0), so the function never
1041/// fails just because a file lacks frontmatter.
1042pub fn extract_sections_in_file(text: &str) -> Vec<Section> {
1043    // Tolerate a leading BOM the same way `split_frontmatter` does, so the line
1044    // count and the body slice agree with the read path.
1045    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
1046
1047    // Find the body and how many source lines precede it. The body begins right
1048    // after the closing fence; the number of lines consumed by the frontmatter
1049    // block (both fences + the YAML between) is the offset to add to each
1050    // body-relative heading line.
1051    let (body, offset) = match split_frontmatter(text, Path::new("<sections>")) {
1052        Ok(parsed) => {
1053            // Lines before the body = total lines in `text` minus lines in body.
1054            let total_lines = count_lines(text);
1055            let body_lines = count_lines(&parsed.body);
1056            (parsed.body, total_lines.saturating_sub(body_lines))
1057        }
1058        // No frontmatter block: the whole text is body, no offset.
1059        Err(_) => (text.to_string(), 0),
1060    };
1061
1062    let mut sections = extract_sections(&body);
1063    for s in &mut sections {
1064        s.line += offset;
1065    }
1066    sections
1067}
1068
1069/// Count the number of lines a string spans for line-number offsetting: one line
1070/// per `\n`, plus one more for a final line with no trailing newline. An empty
1071/// string is zero lines.
1072fn count_lines(s: &str) -> u32 {
1073    if s.is_empty() {
1074        return 0;
1075    }
1076    let newlines = s.bytes().filter(|&b| b == b'\n').count() as u32;
1077    if s.ends_with('\n') {
1078        newlines
1079    } else {
1080        newlines + 1
1081    }
1082}
1083
1084/// Parse a store's `DB.md` file into a [`Config`]: the `## Agent instructions`
1085/// prose, `## Policies` (`### Frozen pages` + `### Ignored types`), and
1086/// `## Schemas` (`### <type>` field-bullet blocks). Unrecognized sections are
1087/// ignored; absent sections leave their [`Config`] fields at default.
1088pub fn parse_db_md(text: &str, file: &Path) -> Result<Config, ParseError> {
1089    // The structured sections live in the body (after frontmatter). DB.md must
1090    // still start with a valid `---` block (`type: db-md`); if it's missing we
1091    // surface MissingFrontmatter like any other file.
1092    let parsed = split_frontmatter(text, file)?;
1093    let _frontmatter = Frontmatter::parse(&parsed.frontmatter_yaml, file)?;
1094    let sections = extract_sections(&parsed.body);
1095
1096    let mut config = Config::default();
1097    // Track which H2 region each H3 belongs to as we walk the flat list.
1098    let mut current_h2: Option<String> = None;
1099
1100    for section in &sections {
1101        match section.level {
1102            2 => {
1103                let name = section.heading.trim().to_ascii_lowercase();
1104                current_h2 = Some(name.clone());
1105                if name == "agent instructions" {
1106                    let prose = section_prose(&section.body);
1107                    if !prose.is_empty() {
1108                        config.agent_instructions = Some(prose);
1109                    }
1110                } else if name == "folders" {
1111                    // `## Folders` carries its bullets directly under the H2 (no
1112                    // `### <type>` sub-sections), like `## Agent instructions`.
1113                    for b in bullet_lines(&section.body) {
1114                        if let Some((path, meta)) = parse_folder_bullet(&b) {
1115                            config.folders.insert(path, meta);
1116                        }
1117                    }
1118                }
1119            }
1120            3 => {
1121                let h2 = current_h2.as_deref().unwrap_or("");
1122                let h3 = section.heading.trim().to_ascii_lowercase();
1123                match (h2, h3.as_str()) {
1124                    ("policies", "frozen pages") => {
1125                        config.frozen_pages = bullet_lines(&section.body)
1126                            .into_iter()
1127                            .map(|b| PathBuf::from(extract_path_bullet(&b)))
1128                            .collect();
1129                    }
1130                    ("policies", "ignored types") => {
1131                        config.ignored_types = bullet_lines(&section.body)
1132                            .into_iter()
1133                            .flat_map(|b| extract_type_list_bullet(&b))
1134                            .collect();
1135                    }
1136                    ("schemas", _) => {
1137                        // The H3 heading text (as written) is the type name.
1138                        let type_name = section.heading.trim().to_string();
1139                        let mut schema = Schema::default();
1140                        for b in bullet_lines(&section.body) {
1141                            match parse_schema_bullet(&b) {
1142                                SchemaBullet::Field(f) => schema.fields.push(f),
1143                                SchemaBullet::Unique(k) if !k.is_empty() => {
1144                                    schema.unique_keys.push(k)
1145                                }
1146                                SchemaBullet::SummaryTemplate(t) if !t.is_empty() => {
1147                                    schema.summary_template = Some(t)
1148                                }
1149                                SchemaBullet::Shard(Some(b)) => schema.shard = Some(b),
1150                                // Empty `unique:`/`summary_template:`, or a `shard:`
1151                                // with an unrecognized value — ignored.
1152                                SchemaBullet::Unique(_)
1153                                | SchemaBullet::SummaryTemplate(_)
1154                                | SchemaBullet::Shard(None) => {}
1155                            }
1156                        }
1157                        config.schemas.insert(type_name, schema);
1158                    }
1159                    _ => {}
1160                }
1161            }
1162            _ => {}
1163        }
1164    }
1165
1166    Ok(config)
1167}
1168
1169/// One parsed bullet inside a `### <type>` schema block: an ordinary field, or a
1170/// reserved directive (`unique:` / `summary_template:` / `shard:`). The names
1171/// `unique`, `summary_template`, and `shard` are reserved and cannot be used as
1172/// field names.
1173#[derive(Debug)]
1174enum SchemaBullet {
1175    /// An ordinary `- <name> (<modifiers>)` field.
1176    Field(FieldSpec),
1177    /// `- unique: <field>[, <field> …]` — a (possibly compound) uniqueness key.
1178    Unique(Vec<String>),
1179    /// `- summary_template: <template>` — the default-`summary` pattern.
1180    SummaryTemplate(String),
1181    /// `- shard: by-date | flat` — date-shard records of this type, or keep them
1182    /// flat. `None` = an unrecognized value, ignored like an unknown modifier.
1183    Shard(Option<bool>),
1184}
1185
1186/// Classify one `## Schemas` bullet as a directive or a field. The directive
1187/// forms are `- unique: a, b, …` and `- summary_template: …`; the keyword check
1188/// guards against false positives — a field like `- status (enum: a, b)` has a
1189/// `(` before any `:`, so its head isn't a bare reserved keyword and it parses
1190/// as a [`FieldSpec`].
1191fn parse_schema_bullet(bullet_line: &str) -> SchemaBullet {
1192    let line = bullet_line.trim();
1193    let line = line
1194        .strip_prefix("- ")
1195        .or_else(|| line.strip_prefix("* "))
1196        .or_else(|| line.strip_prefix("+ "))
1197        .or_else(|| line.strip_prefix('-'))
1198        .unwrap_or(line)
1199        .trim();
1200
1201    if let Some((head, rest)) = line.split_once(':') {
1202        match head.trim().to_ascii_lowercase().as_str() {
1203            "unique" => {
1204                let fields = rest
1205                    .split(',')
1206                    .map(|f| f.trim().to_string())
1207                    .filter(|f| !f.is_empty())
1208                    .collect();
1209                return SchemaBullet::Unique(fields);
1210            }
1211            "summary_template" => {
1212                return SchemaBullet::SummaryTemplate(rest.trim().to_string());
1213            }
1214            "shard" => {
1215                // `by-date` (synonyms: date/sharded/true) enables date-sharding;
1216                // `flat` (none/false) forces flat; anything else is ignored.
1217                let v = match rest.trim().to_ascii_lowercase().as_str() {
1218                    "by-date" | "date" | "sharded" | "true" => Some(true),
1219                    "flat" | "none" | "false" => Some(false),
1220                    _ => None,
1221                };
1222                return SchemaBullet::Shard(v);
1223            }
1224            _ => {}
1225        }
1226    }
1227
1228    SchemaBullet::Field(parse_field_spec(bullet_line))
1229}
1230
1231/// Parse one `## Folders` bullet — `- <path>[|<display>] — <description>` — into
1232/// the folder path (store-relative, unix-slash, no trailing slash) and its
1233/// [`FolderMeta`]. The optional `|<display>` overrides the rollup's derived
1234/// folder name (mirroring the wiki-link `|display` convention); the text after
1235/// the first em-dash (`—`), or ` - `, is the description. Backticks around the
1236/// path are tolerated (matching the `### Frozen pages` spelling). Returns `None`
1237/// for a bullet with no usable path.
1238fn parse_folder_bullet(bullet_line: &str) -> Option<(String, FolderMeta)> {
1239    let line = bullet_line.trim();
1240    let line = line
1241        .strip_prefix("- ")
1242        .or_else(|| line.strip_prefix("* "))
1243        .or_else(|| line.strip_prefix("+ "))
1244        .or_else(|| line.strip_prefix('-'))
1245        .unwrap_or(line)
1246        .trim();
1247
1248    // Split off the description at the first em-dash (preferred, matching the
1249    // rollup's own ` — ` separator) or a ` - ` fallback.
1250    let (pathspec, description) = match line.find('—') {
1251        Some(i) => (line[..i].trim(), Some(line[i + '—'.len_utf8()..].trim())),
1252        None => match line.find(" - ") {
1253            Some(i) => (line[..i].trim(), Some(line[i + 3..].trim())),
1254            None => (line, None),
1255        },
1256    };
1257
1258    // Optional `|display` override lives on the path side.
1259    let (path_raw, display) = match pathspec.split_once('|') {
1260        Some((p, d)) => (p.trim(), Some(d.trim())),
1261        None => (pathspec, None),
1262    };
1263
1264    // Normalize the path: drop surrounding backticks, a leading `./`, a trailing `/`.
1265    let path = path_raw.trim().trim_matches('`').trim();
1266    let path = path.strip_prefix("./").unwrap_or(path);
1267    let path = path.strip_suffix('/').unwrap_or(path).trim();
1268    if path.is_empty() {
1269        return None;
1270    }
1271
1272    let non_empty = |s: &str| {
1273        let t = s.trim();
1274        (!t.is_empty()).then(|| t.to_string())
1275    };
1276    Some((
1277        path.to_string(),
1278        FolderMeta {
1279            display: display.and_then(non_empty),
1280            description: description.and_then(non_empty),
1281        },
1282    ))
1283}
1284
1285/// Parse a single `## Schemas` field-bullet line — `- <name> (<modifiers>)` —
1286/// into a [`FieldSpec`], capturing recognized modifiers and stashing the rest
1287/// in [`FieldSpec::unknown_modifiers`].
1288pub fn parse_field_spec(bullet_line: &str) -> FieldSpec {
1289    // Strip the leading bullet marker (`- ` / `* ` / `+ `) and surrounding ws.
1290    let line = bullet_line.trim();
1291    let line = line
1292        .strip_prefix("- ")
1293        .or_else(|| line.strip_prefix("* "))
1294        .or_else(|| line.strip_prefix("+ "))
1295        .or_else(|| line.strip_prefix('-'))
1296        .unwrap_or(line)
1297        .trim();
1298
1299    // Split `<name> (<modifiers>)` — the canonical paren form — OR the natural
1300    // mis-spelling `<name>: <modifiers>` (colon instead of parens). The two
1301    // delimiters are interchangeable for the field head; whichever appears FIRST
1302    // wins, so a paren form whose modifiers contain a colon (`status (enum: a,
1303    // b)`) still parses by parens (the `(` precedes the `:`), while a bare
1304    // `title: string, required` parses by colon instead of being swallowed whole
1305    // into the field name with every modifier silently dropped.
1306    let paren = line.find('(');
1307    let colon = line.find(':');
1308    // Choose the head delimiter. The paren form wins when its `(` precedes any
1309    // `:` (so `status (enum: a, b)` parses by parens, the colon being inside the
1310    // modifiers); otherwise a `:` before the paren — or with no paren at all —
1311    // selects the colon form `<name>: <modifiers>`, the natural mis-spelling that
1312    // must NOT be swallowed whole into the field name with every modifier lost.
1313    let use_paren = matches!((paren, colon), (Some(p), c) if c.is_none_or(|c| p < c));
1314    let (name, modifiers) = if use_paren {
1315        let open = paren.expect("use_paren implies a paren");
1316        let name = line[..open].trim().to_string();
1317        let after = &line[open + 1..];
1318        let mods = match after.rfind(')') {
1319            Some(close) => &after[..close],
1320            None => after, // tolerate a missing close paren
1321        };
1322        (name, mods.trim())
1323    } else if let Some(c) = colon {
1324        // Colon form: everything after the first colon is the modifier list,
1325        // parsed identically to the parenthesized modifiers below.
1326        let name = line[..c].trim().to_string();
1327        (name, line[c + 1..].trim())
1328    } else {
1329        // Neither delimiter: a free-form optional field of any shape — name only.
1330        (line.to_string(), "")
1331    };
1332
1333    let mut spec = FieldSpec {
1334        name,
1335        ..FieldSpec::default()
1336    };
1337
1338    if modifiers.is_empty() {
1339        return spec;
1340    }
1341
1342    // Modifiers are comma-separated. `enum` and `default` are special: their own
1343    // values may contain commas, so each is a *greedy* clause that runs from its
1344    // keyword to the start of the next recognized greedy clause (or end of line).
1345    // This lets `default North America, EMEA fallback` keep its comma and lets a
1346    // `default …` written after an `enum …` still be recognized, instead of the
1347    // value being truncated at the first comma or absorbed into the enum list.
1348    let raw: Vec<&str> = modifiers.split(',').collect();
1349    let mut i = 0;
1350    while i < raw.len() {
1351        let token = raw[i].trim();
1352        if token.is_empty() {
1353            i += 1;
1354            continue;
1355        }
1356        let lower = token.to_ascii_lowercase();
1357
1358        if lower == "required" {
1359            spec.required = true;
1360            i += 1;
1361        } else if let Some(shape) = shape_from_str(&lower) {
1362            spec.shape = Some(shape);
1363            i += 1;
1364        } else if let Some(rest) = lower.strip_prefix("link to ") {
1365            // The trailing slash is required in the source; store the prefix
1366            // without it so `Path::starts_with` comparisons are clean.
1367            let prefix = token["link to ".len()..].trim().trim_end_matches('/');
1368            let _ = rest; // lowercase form only used for the keyword match
1369            spec.link_prefix = Some(PathBuf::from(prefix));
1370            i += 1;
1371        } else if token.len() >= "default ".len() && lower.starts_with("default ") {
1372            // Greedy `default <value>`: the value is this token (after the
1373            // keyword) plus every following comma-token up to the next greedy
1374            // clause, rejoined with the commas the split removed — so a comma
1375            // inside the default value is preserved. Original case is kept.
1376            let end = next_greedy_clause(&raw, i + 1);
1377            let mut value = token["default ".len()..].to_string();
1378            for tok in &raw[i + 1..end] {
1379                value.push(',');
1380                value.push_str(tok);
1381            }
1382            spec.default = Some(Value::String(value.trim().to_string()));
1383            i = end;
1384        } else if lower == "enum" || lower.starts_with("enum:") {
1385            // Greedy `enum` (bare `enum, a, b` or `enum: a, b`): the values run
1386            // from here to the next greedy clause (e.g. a trailing `default …`),
1387            // NOT unconditionally to end-of-line — so a `default` after `enum` is
1388            // parsed instead of swallowed as a bogus enum member.
1389            let end = next_greedy_clause(&raw, i + 1);
1390            // Rejoin this clause's tokens (trimmed so the `enum` head sits at the
1391            // start), drop the leading `enum`/`enum:` head, then re-split the
1392            // remainder into values.
1393            let joined = raw[i..end].join(",");
1394            let joined = joined.trim();
1395            let after_kw = match joined.find(':') {
1396                // `enum: a, b` — values follow the colon.
1397                Some(colon) => &joined[colon + 1..],
1398                // bare `enum, a, b` — values follow the keyword itself.
1399                None => joined.get("enum".len()..).unwrap_or(""),
1400            };
1401            let values: Vec<String> = after_kw
1402                .split(',')
1403                .map(|v| v.trim().to_string())
1404                .filter(|v| !v.is_empty())
1405                .collect();
1406            spec.enum_values = Some(values);
1407            i = end;
1408        } else {
1409            // Unrecognized modifier — captured verbatim, surfaced as Info.
1410            spec.unknown_modifiers.push(token.to_string());
1411            i += 1;
1412        }
1413    }
1414
1415    spec
1416}
1417
1418// ── Private helpers ─────────────────────────────────────────────────────────
1419
1420/// Parse a frontmatter timestamp value into a `DateTime<FixedOffset>`. A `null`
1421/// is treated as absent; anything else must be an RFC3339 string.
1422fn parse_timestamp(
1423    value: &Value,
1424    key: &str,
1425    file: &Path,
1426) -> Result<Option<DateTime<FixedOffset>>, ParseError> {
1427    match value {
1428        Value::Null => Ok(None),
1429        Value::String(s) => parse_rfc3339(s, key, file).map(Some),
1430        other => Err(ParseError::BadTimestamp {
1431            file: file.to_path_buf(),
1432            key: key.to_string(),
1433            value: format!("{other:?}"),
1434        }),
1435    }
1436}
1437
1438/// Parse an RFC3339 timestamp string, mapping failure to [`ParseError::BadTimestamp`].
1439fn parse_rfc3339(s: &str, key: &str, file: &Path) -> Result<DateTime<FixedOffset>, ParseError> {
1440    DateTime::parse_from_rfc3339(s.trim()).map_err(|_| ParseError::BadTimestamp {
1441        file: file.to_path_buf(),
1442        key: key.to_string(),
1443        value: s.to_string(),
1444    })
1445}
1446
1447/// Coerce a YAML scalar value to its string form for the universal-contract
1448/// fields (`type`/`id`/`summary`/`status`). Mirrors `validate::scalar_string`
1449/// and `store::yaml_scalar_string` so the four modules agree on one coercion
1450/// rule: a bare numeric/bool scalar (`id: 100`, `summary: 2026`, `status: 0`)
1451/// is preserved as its string form rather than being read as None and silently
1452/// dropped on the next `to_yaml` re-emit. Returns `None` only for genuinely
1453/// non-scalar values (sequences, mappings, null), which were never a valid
1454/// shape for these fields.
1455fn scalar_string(value: &Value) -> Option<String> {
1456    match value {
1457        Value::String(s) => Some(s.clone()),
1458        Value::Number(n) => Some(n.to_string()),
1459        Value::Bool(b) => Some(b.to_string()),
1460        _ => None,
1461    }
1462}
1463
1464/// Read a `tags` value into a flat `Vec<String>`. Accepts a sequence of scalars
1465/// (the canonical form) or a single scalar (coerced to a one-element list).
1466fn parse_tags(value: &Value) -> Vec<String> {
1467    match value {
1468        Value::Sequence(items) => items
1469            .iter()
1470            .filter_map(|v| match v {
1471                Value::String(s) => Some(s.clone()),
1472                Value::Number(n) => Some(n.to_string()),
1473                Value::Bool(b) => Some(b.to_string()),
1474                _ => None,
1475            })
1476            .collect(),
1477        Value::String(s) => vec![s.clone()],
1478        _ => Vec::new(),
1479    }
1480}
1481
1482/// Read a `tags` value into a flat `Vec<String>` **without losing data**: a
1483/// sequence of clean scalars (the canonical form) or a single scalar coerce to a
1484/// string list. Any other shape — a sequence with a non-scalar item
1485/// (`tags: [[vip]]` → `Seq[Seq[String]]`, `tags: [a, [b]]`), or a mapping — is
1486/// rejected as `Err(value.clone())` so the caller preserves the raw value in
1487/// `extra` rather than silently filtering items out / erasing the field on the
1488/// next re-emit. This is the `tags` analog of routing a non-scalar universal
1489/// value to pass-through instead of the destroy path.
1490fn parse_tags_preserving(value: &Value) -> Result<Vec<String>, Value> {
1491    match value {
1492        Value::Sequence(items) => {
1493            let mut out = Vec::with_capacity(items.len());
1494            for item in items {
1495                match item {
1496                    Value::String(s) => out.push(s.clone()),
1497                    Value::Number(n) => out.push(n.to_string()),
1498                    Value::Bool(b) => out.push(b.to_string()),
1499                    // A non-scalar item (nested sequence/mapping/null) means this
1500                    // is not a clean tag list; preserve the whole value verbatim.
1501                    _ => return Err(value.clone()),
1502                }
1503            }
1504            Ok(out)
1505        }
1506        Value::String(s) => Ok(vec![s.clone()]),
1507        Value::Number(n) => Ok(vec![n.to_string()]),
1508        Value::Bool(b) => Ok(vec![b.to_string()]),
1509        // A mapping / null `tags` value is not a list; preserve it verbatim.
1510        _ => Err(value.clone()),
1511    }
1512}
1513
1514/// Render a non-string YAML mapping key as the scalar text YAML would emit for
1515/// it (`2026`, `true`, `3.14`, …), so a numeric/bool/float frontmatter key
1516/// preserves its key *text* on round-trip instead of being rewritten to its Rust
1517/// `Debug` form (`Number(2026)`, `Bool(true)`, `'Null'`). The key re-emits as a
1518/// string-typed key carrying the original text (`'2026':`) — the type narrows to
1519/// string, but the operator's data is no longer corrupted, and ordinary string
1520/// keys are wholly unaffected. Falls back to `Debug` only for a key shape that
1521/// cannot be a scalar (a sequence/mapping key — not expressible in our
1522/// `String`-keyed `extra`), which never occurs in practice.
1523fn yaml_scalar_key(key: &Value) -> String {
1524    match key {
1525        Value::String(s) => s.clone(),
1526        Value::Number(n) => n.to_string(),
1527        Value::Bool(b) => b.to_string(),
1528        Value::Null => "null".to_string(),
1529        // Non-scalar key: not representable as a plain `extra` string key; keep
1530        // the defensive Debug form so nothing panics (unreachable in practice).
1531        other => format!("{other:?}"),
1532    }
1533}
1534
1535/// Parse a single `[[target|display]]` string into a [`WikiLink`] with no
1536/// location, or `None` if the string is not a bare wiki-link. Used for
1537/// frontmatter-valued links where there is no body position to report.
1538fn parse_wiki_link_str(s: &str) -> Option<WikiLink> {
1539    let s = s.trim();
1540    let inner = s.strip_prefix("[[")?.strip_suffix("]]")?;
1541    // Reject anything with further brackets (e.g. the nested flow-form item),
1542    // which is not a clean single wiki-link.
1543    if inner.contains('[') || inner.contains(']') {
1544        return None;
1545    }
1546    let (target, display) = match inner.split_once('|') {
1547        Some((t, d)) => (t.to_string(), Some(d.to_string())),
1548        None => (inner.to_string(), None),
1549    };
1550    Some(WikiLink {
1551        is_full_path: target_is_full_path(&target),
1552        has_md_extension: target_has_md_extension(&target),
1553        target,
1554        display,
1555        location: (PathBuf::new(), 0, 0),
1556    })
1557}
1558
1559/// Extract every wiki-link from a single frontmatter field value, accepting the
1560/// two canonical forms the spec defines (SPEC § Linking):
1561///
1562/// - a **scalar** wiki-link field, in either the quoted (`f: "[[x]]"`) or the
1563///   canonical unquoted inline (`f: [[x]]`) form, and
1564/// - a **list** field whose items are quoted wiki-link strings
1565///   (`- "[[x]]"`).
1566///
1567/// YAML eats the brackets of an unquoted `[[x]]`, leaving a flow-list-in-a-list,
1568/// so the parsed [`Value`] shapes are not what one would naively expect:
1569///
1570/// | source                         | parsed `Value`                     | here |
1571/// |--------------------------------|------------------------------------|------|
1572/// | `f: "[[x]]"`       (quoted)    | `String("[[x]]")`                  | link |
1573/// | `f: [[x]]`         (unquoted)  | `Seq[ Seq[String("x")] ]`          | link |
1574/// | `f:`\n`  - "[[x]]"`(quoted)    | `Seq[ String("[[x]]"), … ]`        | link |
1575/// | `f:`\n`  - [[x]]`  (unquoted)  | `Seq[ Seq[Seq[String("x")]], … ]`  | —    |
1576///
1577/// The last row — an *unquoted list* — parses identically to the flow-form list
1578/// `f: [[a], [b]]` and is a mis-encoding the canonical writer never emits;
1579/// `dbmd validate` reports it as `WIKI_LINK_FLOW_FORM_LIST` (see
1580/// [`detect_flow_form_link_lists`]). It is deliberately NOT surfaced here, so an
1581/// edge enumerator only ever sees the valid canonical forms.
1582///
1583/// The unquoted scalar (`Seq[Seq[String]]`, one element) is told apart from a
1584/// plain one-item flow list (`f: [x]` → `Seq[String]`, one fewer nesting level)
1585/// by [`unquoted_inline_link`] requiring its argument to be a `Sequence`.
1586fn links_in_field_value(value: &Value) -> Vec<WikiLink> {
1587    // Quoted scalar: `field: "[[x]]"`.
1588    if let Value::String(s) = value {
1589        return parse_wiki_link_str(s).into_iter().collect();
1590    }
1591    let Value::Sequence(items) = value else {
1592        return Vec::new();
1593    };
1594    // Unquoted scalar inline form `field: [[x]]` → `Seq[ Seq[String(x)] ]`.
1595    // (A quoted single-item list `["[[x]]"]` is `Seq[String]`, so its lone item
1596    // is a `String`, not a `Sequence`, and falls through to the list path below.)
1597    if items.len() == 1 {
1598        if let Some(link) = unquoted_inline_link(&items[0]) {
1599            return vec![link];
1600        }
1601    }
1602    // Otherwise a list of quoted wiki-link strings; non-string items (the
1603    // unquoted-list mis-encoding) are left for validate to flag.
1604    items
1605        .iter()
1606        .filter_map(|item| parse_wiki_link_str(item.as_str()?))
1607        .collect()
1608}
1609
1610/// Canonicalize one `extra` frontmatter value for emission by [`Frontmatter::to_yaml`].
1611///
1612/// The read path ([`Frontmatter::parse`]) stores every unknown key's raw parsed
1613/// [`Value`] verbatim, so a SPEC-canonical *unquoted* inline scalar wiki-link
1614/// (`company: [[records/companies/northstar]]`) lands in `extra` as the nested
1615/// shape YAML produces for it — `Seq[ Seq[String("records/companies/northstar")] ]`.
1616/// Re-emitting that verbatim yields the block sequence
1617///
1618/// ```text
1619/// company:
1620/// - - records/companies/northstar
1621/// ```
1622///
1623/// which has lost the `[[ ]]` brackets entirely: the link is destroyed, and every
1624/// reader (validate, graph, backlinks) stops seeing the edge. This normalizes such
1625/// a value back into the canonical emitted form before it is written:
1626///
1627/// - a **scalar** wiki-link (quoted `String("[[x]]")` or unquoted `Seq[Seq[String]]`,
1628///   one element) → a quoted scalar `Value::String("[[x]]")`, which serde_norway emits
1629///   inline as `'[[x]]'` — the form the finding confirms survives a round-trip and
1630///   that [`links_in_field_value`] reads back as the same scalar link;
1631/// - a **list** of wiki-links (in any spelling [`links_in_field_value`] accepts) →
1632///   a block `Value::Sequence` of quoted-link strings (`- "[[x]]"`), matching the
1633///   `set` write-in path and the canonical list form;
1634/// - everything else → returned verbatim (the common no-op for non-link values).
1635///
1636/// `|display` is preserved in both link branches. This is the single point that
1637/// keeps all three curator-loop writers (`format`, `fm set`, `link`) from
1638/// corrupting a pre-existing canonical link, since they all funnel through
1639/// `to_yaml`.
1640fn canonicalize_extra_value(value: &Value) -> Value {
1641    match value {
1642        // Scalar wiki-link, quoted form: `field: "[[x]]"` → `String("[[x]]")`.
1643        // Re-emit as a quoted scalar so it stays a string (never the brackets-as-
1644        // YAML nested sequence). Non-link strings are returned untouched.
1645        Value::String(s) => match parse_wiki_link_str(s) {
1646            Some(link) => Value::String(wiki_link_literal(&link)),
1647            None => value.clone(),
1648        },
1649        Value::Sequence(items) => {
1650            // Scalar wiki-link, unquoted inline form: `field: [[x]]` parses to a
1651            // one-element `Seq[ Seq[String(x)] ]`. Collapse back to the quoted
1652            // scalar string so the link is preserved rather than block-emitted.
1653            if items.len() == 1 {
1654                if let Some(link) = unquoted_inline_link(&items[0]) {
1655                    return Value::String(wiki_link_literal(&link));
1656                }
1657            }
1658            // List of wiki-links: re-emit as a block sequence of quoted-link
1659            // strings, the canonical list form `to_yaml` renders block-style and
1660            // `links_in_field_value` accepts. Only canonicalize when *every* item
1661            // is a clean single wiki-link; a list with any non-link item is left
1662            // verbatim so unrelated sequences (and the unquoted-list mis-encoding
1663            // validate flags) are untouched.
1664            let mut links = Vec::with_capacity(items.len());
1665            for item in items {
1666                match link_from_flow_list_item(item) {
1667                    Some(link) => links.push(link),
1668                    None => return value.clone(),
1669                }
1670            }
1671            if links.is_empty() {
1672                return value.clone();
1673            }
1674            Value::Sequence(
1675                links
1676                    .iter()
1677                    .map(|l| Value::String(wiki_link_literal(l)))
1678                    .collect(),
1679            )
1680        }
1681        // Mappings, scalars other than strings, nulls: nothing to canonicalize.
1682        _ => value.clone(),
1683    }
1684}
1685
1686/// Render a [`WikiLink`] back to its `[[target]]` / `[[target|display]]` literal,
1687/// the inner form the canonical writer emits and `links_in_field_value` accepts.
1688fn wiki_link_literal(link: &WikiLink) -> String {
1689    match &link.display {
1690        Some(d) => format!("[[{}|{}]]", link.target, d),
1691        None => format!("[[{}]]", link.target),
1692    }
1693}
1694
1695/// Recognize the inner token of an unquoted scalar `[[x]]`: after YAML strips the
1696/// outer brackets, the inner `[x]` is a single-element sequence `Seq[String(x)]`.
1697/// Reconstructs `[[x]]` (preserving any `|display`) and parses it, or returns
1698/// `None` when `v` is not that shape. Requiring a `Sequence` here is what keeps a
1699/// plain one-item flow list (`field: [x]` → `Seq[String]`, not `Seq[Seq[String]]`)
1700/// from being mistaken for a wiki-link.
1701fn unquoted_inline_link(v: &Value) -> Option<WikiLink> {
1702    let Value::Sequence(items) = v else {
1703        return None;
1704    };
1705    if items.len() != 1 {
1706        return None;
1707    }
1708    let s = items[0].as_str()?;
1709    // A clean unquoted wiki-link has no further brackets inside it.
1710    if s.contains('[') || s.contains(']') {
1711        return None;
1712    }
1713    parse_wiki_link_str(&format!("[[{s}]]"))
1714}
1715
1716/// Decide whether a `dbmd fm set` / `--fm` value string is a **list of
1717/// wiki-links** that should be stored as a YAML block sequence, returning the
1718/// canonical `Value::Sequence` of quoted-link strings when so.
1719///
1720/// The value path of every write surface stringifies its argument; without this
1721/// a required list-of-links field (`meeting.attendees`) was unwritable in valid
1722/// form — passing `[[[a]], [[b]]]` stored a single scalar string that mis-parses
1723/// and trips `WIKI_LINK_FLOW_FORM_LIST` / `WIKI_LINK_BROKEN`. This recognizes the
1724/// two list spellings an agent naturally types and normalizes both to the block
1725/// form the canonical writer emits and `dbmd validate` accepts:
1726///
1727/// - flow list of quoted links — `["[[a]]", "[[b]]"]`
1728/// - flow list of unquoted links — `[[[a]], [[b]]]` (YAML: `Seq[Seq[String], …]`)
1729///
1730/// Returns `None` (⇒ caller stores a verbatim scalar string) for everything that
1731/// is not unambiguously a list of clean wiki-links — plain text, a single inline
1732/// `[[x]]` (YAML reads it as a one-item `Seq[Seq[String]]`, kept scalar so it
1733/// renders inline), an empty list, or a list with any non-link item. A single
1734/// link must stay scalar; only genuine multi-item-or-explicit lists become
1735/// sequences, matching `links_in_field_value`'s acceptance rule so writer and
1736/// validator never disagree.
1737fn parse_link_list_value(value: &str) -> Option<Value> {
1738    let trimmed = value.trim();
1739    // Only a YAML *flow sequence* literal is a list candidate; anything not
1740    // wrapped in `[ … ]` is a scalar (a bare `[[x]]` is wrapped, and handled by
1741    // the single-inline-link guard below).
1742    if !(trimmed.starts_with('[') && trimmed.ends_with(']')) {
1743        return None;
1744    }
1745    let Ok(Value::Sequence(items)) = serde_norway::from_str::<Value>(trimmed) else {
1746        return None;
1747    };
1748    // A single inline `[[x]]` parses to `Seq[ Seq[String(x)] ]` (one item, itself
1749    // a sequence) — that is the unquoted *scalar* form, not a list. Keep it scalar
1750    // so it round-trips to the inline `field: [[x]]` rather than a one-item block
1751    // list. `links_in_field_value` reads it back as a scalar link either way.
1752    if items.len() == 1 && unquoted_inline_link(&items[0]).is_some() {
1753        return None;
1754    }
1755    // Every item must resolve to exactly one clean wiki-link, in any of the flow
1756    // spellings an agent types (see [`link_from_flow_list_item`]).
1757    let mut links = Vec::with_capacity(items.len());
1758    for item in &items {
1759        links.push(link_from_flow_list_item(item)?);
1760    }
1761    if links.is_empty() {
1762        return None;
1763    }
1764    // Normalize to a block sequence of quoted-link strings — the form `to_yaml`
1765    // renders block-style and `links_in_field_value` accepts. `|display` is
1766    // preserved.
1767    let normalized = links
1768        .iter()
1769        .map(|l| Value::String(wiki_link_literal(l)))
1770        .collect();
1771    Some(Value::Sequence(normalized))
1772}
1773
1774/// Recognize one clean wiki-link from a single **item** of a YAML flow sequence,
1775/// across the spellings an agent types for a list. After top-level flow parsing,
1776/// a list item arrives in one of:
1777///
1778/// - quoted — `"[[x]]"` ⇒ `String("[[x]]")`
1779/// - unquoted in a flow list — `[[x]]` inside `[…]` ⇒ `Seq[ Seq[String(x)] ]`
1780///   (one level deeper than a bare unquoted scalar, because the surrounding list
1781///   adds a wrapper); unwrap the single-element wrapper, then read the inline
1782///   `Seq[String(x)]` with [`unquoted_inline_link`].
1783///
1784/// Returns `None` for any item that is not exactly one clean wiki-link, so the
1785/// caller falls back to a scalar string and never fabricates a partial list.
1786fn link_from_flow_list_item(item: &Value) -> Option<WikiLink> {
1787    match item {
1788        Value::String(s) => parse_wiki_link_str(s),
1789        Value::Sequence(inner) => {
1790            // Unquoted list item `[[x]]` → `Seq[ Seq[String(x)] ]`: peel the lone
1791            // wrapper to expose the inline-link shape `Seq[String(x)]`.
1792            //
1793            // Only this triple-nested shape is a wiki-link. We deliberately do
1794            // NOT fall back to `unquoted_inline_link(item)` on the bare double
1795            // nesting `Seq[String(x)]` (a plain one-element string list `[x]`):
1796            // that fallback fabricated a wiki-link out of an ordinary nested
1797            // string list — `groups: [[alpha], [beta]]` (data `[["alpha"],
1798            // ["beta"]]`) was rewritten to `- '[[alpha]]'` / `- '[[beta]]'`,
1799            // silently changing the field's type and manufacturing short-form
1800            // links the tool then flags as `WIKI_LINK_SHORT_FORM`. An unknown
1801            // nested string list must pass through verbatim (SPEC § "Unknown
1802            // fields pass through").
1803            if inner.len() == 1 {
1804                if let Some(link) = unquoted_inline_link(&inner[0]) {
1805                    return Some(link);
1806                }
1807            }
1808            None
1809        }
1810        _ => None,
1811    }
1812}
1813
1814/// A target is a full store-relative path when its first path segment is one of
1815/// the three canonical layer dirs and at least one `/` separator follows. A
1816/// trailing `.md` does not affect this classification.
1817fn target_is_full_path(target: &str) -> bool {
1818    let target = target.trim();
1819    match target.split_once('/') {
1820        Some((head, _rest)) => LAYER_DIRS.contains(&head),
1821        None => false,
1822    }
1823}
1824
1825/// True when the target carries a trailing `.md` extension (validate warns
1826/// `WIKI_LINK_HAS_EXTENSION`).
1827fn target_has_md_extension(target: &str) -> bool {
1828    target.trim().ends_with(".md")
1829}
1830
1831/// A forward-only cursor that yields the 1-based character (Unicode scalar)
1832/// column of successive byte offsets within a single line in ONE linear pass.
1833///
1834/// The previous helper recomputed `line[..offset].chars().count()` from the line
1835/// start for every match, so a line with N matches cost O(N × line_len) — a
1836/// quadratic blowup on a link-dense line. Because regex matches arrive in
1837/// non-decreasing byte order, this cursor advances the char count only across the
1838/// gap since the last queried offset, giving O(line_len) total per line.
1839///
1840/// Offsets MUST be queried in non-decreasing order and must fall on UTF-8
1841/// character boundaries (regex match starts always do).
1842struct ColCursor {
1843    byte: usize,
1844    chars: u32,
1845}
1846
1847impl ColCursor {
1848    fn new() -> Self {
1849        ColCursor { byte: 0, chars: 0 }
1850    }
1851
1852    /// 1-based character column of `byte_offset` in `line`. `byte_offset` must be
1853    /// `>=` every previously queried offset (debug-asserted).
1854    fn column_at(&mut self, line: &str, byte_offset: usize) -> u32 {
1855        debug_assert!(byte_offset >= self.byte, "ColCursor queried out of order");
1856        self.chars += line[self.byte..byte_offset].chars().count() as u32;
1857        self.byte = byte_offset;
1858        self.chars + 1
1859    }
1860}
1861
1862/// Index of the first comma-token in `raw[from..]` that *starts a greedy
1863/// modifier clause* (`enum`, `enum:…`, or `default …`), or `raw.len()` when none
1864/// remain. Used to bound a greedy `default`/`enum` value so it stops at the next
1865/// such clause instead of either truncating at the first comma or swallowing a
1866/// following greedy clause whole.
1867fn next_greedy_clause(raw: &[&str], from: usize) -> usize {
1868    let mut j = from;
1869    while j < raw.len() {
1870        let lower = raw[j].trim().to_ascii_lowercase();
1871        if lower == "enum" || lower.starts_with("enum:") || lower.starts_with("default ") {
1872            return j;
1873        }
1874        j += 1;
1875    }
1876    raw.len()
1877}
1878
1879/// Map a lowercase shape keyword to its [`Shape`].
1880fn shape_from_str(s: &str) -> Option<Shape> {
1881    match s {
1882        "string" => Some(Shape::String),
1883        "int" => Some(Shape::Int),
1884        "bool" => Some(Shape::Bool),
1885        "date" => Some(Shape::Date),
1886        "email" => Some(Shape::Email),
1887        "currency" => Some(Shape::Currency),
1888        "url" => Some(Shape::Url),
1889        _ => None,
1890    }
1891}
1892
1893/// The ATX heading level of a line (number of leading `#`), or 0 if not a
1894/// heading. Up to three leading spaces (CommonMark), requires a space/tab (or
1895/// end-of-line) after the `#` run, caps the run at six.
1896fn heading_level(line: &str) -> u8 {
1897    let indent = line.len() - line.trim_start_matches(' ').len();
1898    if indent > 3 {
1899        return 0;
1900    }
1901    let rest = &line[indent..];
1902    let hashes = rest.len() - rest.trim_start_matches('#').len();
1903    if hashes == 0 || hashes > 6 {
1904        return 0;
1905    }
1906    let after = &rest[hashes..];
1907    if after.is_empty() || after.starts_with(' ') || after.starts_with('\t') {
1908        hashes as u8
1909    } else {
1910        0
1911    }
1912}
1913
1914/// The heading text after the `#` run, trimmed, with a trailing ATX *closing*
1915/// `#` sequence removed per CommonMark (`## Title ##` → `Title`).
1916///
1917/// CommonMark only treats a trailing run of `#` as a closing sequence when it is
1918/// **preceded by a space or tab** (or the content is empty). A `#` that abuts the
1919/// preceding word is literal heading text: `## C#` → `C#`, `## F#` → `F#`,
1920/// `## issue-123#` → `issue-123#`. The old unconditional `trim_end_matches('#')`
1921/// stripped those, corrupting `dbmd sections`/`outline` heading text and — via
1922/// `parse_db_md` using the heading verbatim as the schema type key — silently
1923/// binding a `### c#` schema to `type: c` instead of `type: c#`.
1924fn heading_text(line: &str, level: u8) -> String {
1925    let indent = line.len() - line.trim_start_matches(' ').len();
1926    let after_hashes = &line[indent + level as usize..];
1927    let trimmed = after_hashes.trim();
1928
1929    // Peel a trailing run of `#`. It is a closing sequence only if what precedes
1930    // it (within `trimmed`) is empty or ends in a space/tab; otherwise the `#`s
1931    // are literal content.
1932    let without_hashes = trimmed.trim_end_matches('#');
1933    if without_hashes.len() == trimmed.len() {
1934        // No trailing `#` at all.
1935        return trimmed.to_string();
1936    }
1937    if without_hashes.is_empty() || without_hashes.ends_with([' ', '\t']) {
1938        // A genuine closing sequence (`## Title ##`, `## ##`): drop it and the
1939        // whitespace before it.
1940        without_hashes.trim_end().to_string()
1941    } else {
1942        // The `#` run abuts content (`## C#`): keep it as literal heading text.
1943        trimmed.to_string()
1944    }
1945}
1946
1947/// If `line` opens a fenced code block, return `(fence byte, run length)`.
1948fn opening_fence(line: &str) -> Option<(u8, usize)> {
1949    let indent = line.len() - line.trim_start_matches(' ').len();
1950    if indent > 3 {
1951        return None;
1952    }
1953    let rest = &line[indent..];
1954    let byte = rest.bytes().next()?;
1955    if byte != b'`' && byte != b'~' {
1956        return None;
1957    }
1958    let run = rest.len() - rest.trim_start_matches(byte as char).len();
1959    if run < 3 {
1960        return None;
1961    }
1962    // A backtick fence's info string may not itself contain a backtick.
1963    if byte == b'`' && rest[run..].contains('`') {
1964        return None;
1965    }
1966    Some((byte, run))
1967}
1968
1969/// True if `line` closes the currently open fence: same char, run at least as
1970/// long, nothing but trailing whitespace after.
1971fn is_closing_fence(line: &str, fence: (u8, usize)) -> bool {
1972    let (byte, open_len) = fence;
1973    let indent = line.len() - line.trim_start_matches(' ').len();
1974    if indent > 3 {
1975        return false;
1976    }
1977    let rest = &line[indent..];
1978    let run = rest.len() - rest.trim_start_matches(byte as char).len();
1979    if run < open_len {
1980        return false;
1981    }
1982    rest[run..].trim().is_empty()
1983}
1984
1985/// The prose body of a section: everything after the heading line, trimmed.
1986fn section_prose(section_body: &str) -> String {
1987    match section_body.split_once('\n') {
1988        Some((_heading, rest)) => rest.trim().to_string(),
1989        None => String::new(),
1990    }
1991}
1992
1993/// The bullet lines (`-`/`*`/`+`) of a section body, excluding the heading
1994/// line, each returned with its leading whitespace trimmed.
1995fn bullet_lines(section_body: &str) -> Vec<String> {
1996    section_body
1997        .lines()
1998        .skip(1) // the heading line
1999        .map(str::trim)
2000        .filter(|l| l.starts_with("- ") || l.starts_with("* ") || l.starts_with("+ "))
2001        .map(|l| l.to_string())
2002        .collect()
2003}
2004
2005/// Cut a bullet's content at the first comment separator, returning only the
2006/// meaningful prefix. Recognizes the em-dash (` — `), en-dash (` – `), double-
2007/// hyphen (` -- `), and the plain single-ASCII-hyphen (` - `) spellings an
2008/// operator naturally types — without the single-hyphen form, a comment like
2009/// `records/decisions/q3.md - finalized` left the whole line (comment included)
2010/// as the frozen path, so the entry never matched and the freeze failed OPEN.
2011/// A store-relative path never contains a ` - ` (paths are `/`-joined, spaceless),
2012/// so this does not truncate legitimate path text.
2013fn strip_bullet_comment(content: &str) -> &str {
2014    let mut cut = content.len();
2015    for sep in [" — ", " -- ", " – ", " - "] {
2016        if let Some(idx) = content.find(sep) {
2017            cut = cut.min(idx);
2018        }
2019    }
2020    content[..cut].trim()
2021}
2022
2023/// Strip the leading bullet marker, returning the trimmed content after it.
2024fn bullet_content(bullet: &str) -> &str {
2025    let t = bullet.trim();
2026    t.strip_prefix("- ")
2027        .or_else(|| t.strip_prefix("* "))
2028        .or_else(|| t.strip_prefix("+ "))
2029        .unwrap_or(t)
2030        .trim()
2031}
2032
2033/// Extract a store-relative path from a Frozen-pages bullet. The path may be
2034/// wrapped in backticks and followed by an em-dash comment.
2035fn extract_path_bullet(bullet: &str) -> String {
2036    let content = bullet_content(bullet);
2037    // Prefer a backtick-delimited span if present.
2038    if let Some(start) = content.find('`') {
2039        if let Some(end_rel) = content[start + 1..].find('`') {
2040            return content[start + 1..start + 1 + end_rel].trim().to_string();
2041        }
2042    }
2043    // Otherwise take the text up to a comment separator, stripping quotes.
2044    strip_bullet_comment(content)
2045        .trim_matches('"')
2046        .trim_matches('\'')
2047        .trim()
2048        .to_string()
2049}
2050
2051/// Extract a comma-separated type list from an Ignored-types bullet, stripping
2052/// backticks/quotes and any trailing em-dash comment.
2053fn extract_type_list_bullet(bullet: &str) -> Vec<String> {
2054    let content = strip_bullet_comment(bullet_content(bullet));
2055    content
2056        .split(',')
2057        .map(|t| {
2058            t.trim()
2059                .trim_matches('`')
2060                .trim_matches('"')
2061                .trim_matches('\'')
2062                .trim()
2063                .to_string()
2064        })
2065        .filter(|t| !t.is_empty())
2066        .collect()
2067}
2068
2069#[cfg(test)]
2070mod tests {
2071    use super::*;
2072    use std::path::Path;
2073    use tempfile::tempdir;
2074
2075    // ── Config::frozen_match (the single write-surface policy matcher) ───────
2076
2077    #[test]
2078    fn frozen_match_is_md_insensitive_both_directions() {
2079        // A policy entry stored WITHOUT `.md` (the natural extensionless
2080        // spelling `parse_db_md` keeps verbatim) must still match a `.md`
2081        // write target — the regression every write surface had.
2082        let cfg = Config {
2083            frozen_pages: vec![PathBuf::from("records/decisions/q1")],
2084            ..Config::default()
2085        };
2086        assert_eq!(
2087            cfg.frozen_match(Path::new("records/decisions/q1.md")),
2088            Some(PathBuf::from("records/decisions/q1")),
2089            "extensionless policy entry must freeze the .md file"
2090        );
2091        assert!(cfg.is_frozen(Path::new("records/decisions/q1.md")));
2092
2093        // The symmetric case: a policy entry WITH `.md` matches a bare target.
2094        let cfg = Config {
2095            frozen_pages: vec![PathBuf::from("records/decisions/q1.md")],
2096            ..Config::default()
2097        };
2098        assert_eq!(
2099            cfg.frozen_match(Path::new("records/decisions/q1")),
2100            Some(PathBuf::from("records/decisions/q1.md")),
2101        );
2102        // And the same-spelling cases still match.
2103        assert!(cfg.is_frozen(Path::new("records/decisions/q1.md")));
2104    }
2105
2106    #[test]
2107    fn frozen_match_drops_leading_dot_slash() {
2108        let cfg = Config {
2109            frozen_pages: vec![PathBuf::from("records/decisions/q1.md")],
2110            ..Config::default()
2111        };
2112        assert!(cfg.is_frozen(Path::new("./records/decisions/q1.md")));
2113        assert!(cfg.is_frozen(Path::new("./records/decisions/q1")));
2114    }
2115
2116    #[test]
2117    fn frozen_match_returns_none_for_unlisted_and_prefix_paths() {
2118        let cfg = Config {
2119            frozen_pages: vec![PathBuf::from("records/decisions/q1")],
2120            ..Config::default()
2121        };
2122        assert!(cfg
2123            .frozen_match(Path::new("records/decisions/q2.md"))
2124            .is_none());
2125        // A prefix is not a match: `q1` must not freeze `q1-draft`.
2126        assert!(cfg
2127            .frozen_match(Path::new("records/decisions/q1-draft.md"))
2128            .is_none());
2129        assert!(!cfg.is_frozen(Path::new("records/decisions/q11.md")));
2130    }
2131
2132    // ── split_frontmatter ───────────────────────────────────────────────────
2133
2134    #[test]
2135    fn split_frontmatter_separates_yaml_and_verbatim_body() {
2136        let text = "---\ntype: contact\nsummary: x\n---\n# Heading\n\nBody line.\n";
2137        let p = split_frontmatter(text, Path::new("f.md")).unwrap();
2138        assert_eq!(p.frontmatter_yaml, "type: contact\nsummary: x\n");
2139        // Body is everything after the closing fence's newline, byte-for-byte.
2140        assert_eq!(p.body, "# Heading\n\nBody line.\n");
2141    }
2142
2143    #[test]
2144    fn split_frontmatter_preserves_body_without_trailing_newline() {
2145        let text = "---\ntype: x\n---\nno trailing newline";
2146        let p = split_frontmatter(text, Path::new("f.md")).unwrap();
2147        assert_eq!(p.body, "no trailing newline");
2148    }
2149
2150    #[test]
2151    fn split_frontmatter_empty_body_when_nothing_after_fence() {
2152        let text = "---\ntype: x\n---\n";
2153        let p = split_frontmatter(text, Path::new("f.md")).unwrap();
2154        assert_eq!(p.body, "");
2155    }
2156
2157    #[test]
2158    fn split_frontmatter_missing_opening_fence_errors() {
2159        let text = "# No frontmatter here\ntype: x\n";
2160        let err = split_frontmatter(text, Path::new("f.md")).unwrap_err();
2161        assert!(matches!(err, ParseError::MissingFrontmatter { .. }));
2162    }
2163
2164    #[test]
2165    fn split_frontmatter_leading_content_before_fence_rejected() {
2166        // The opening fence must be the very first line; a blank line first is
2167        // not allowed.
2168        let text = "\n---\ntype: x\n---\nbody";
2169        let err = split_frontmatter(text, Path::new("f.md")).unwrap_err();
2170        assert!(matches!(err, ParseError::MissingFrontmatter { .. }));
2171    }
2172
2173    #[test]
2174    fn split_frontmatter_unterminated_block_errors() {
2175        let text = "---\ntype: x\nsummary: y\n";
2176        let err = split_frontmatter(text, Path::new("f.md")).unwrap_err();
2177        assert!(matches!(err, ParseError::MissingFrontmatter { .. }));
2178    }
2179
2180    // ── Frontmatter::parse ───────────────────────────────────────────────────
2181
2182    #[test]
2183    fn parse_populates_typed_fields_and_routes_unknowns_to_extra() {
2184        let yaml = "type: contact\nid: sarah-chen\nsummary: Director of Ops\nstatus: active\ntags: [vip, renewal]\nemail: sarah@northstar.io\nrole: Director";
2185        let fm = Frontmatter::parse(yaml, Path::new("f.md")).unwrap();
2186        assert_eq!(fm.type_.as_deref(), Some("contact"));
2187        assert_eq!(fm.id.as_deref(), Some("sarah-chen"));
2188        assert_eq!(fm.summary.as_deref(), Some("Director of Ops"));
2189        assert_eq!(fm.status.as_deref(), Some("active"));
2190        assert_eq!(fm.tags, vec!["vip".to_string(), "renewal".to_string()]);
2191        // Type-specific fields are NOT promoted to typed slots.
2192        assert!(fm.type_.is_some() && !fm.extra.contains_key("type"));
2193        assert!(!fm.extra.contains_key("tags"));
2194        assert_eq!(
2195            fm.extra.get("email").and_then(|v| v.as_str()),
2196            Some("sarah@northstar.io")
2197        );
2198        assert_eq!(
2199            fm.extra.get("role").and_then(|v| v.as_str()),
2200            Some("Director")
2201        );
2202    }
2203
2204    #[test]
2205    fn parse_reads_rfc3339_timestamps() {
2206        let yaml =
2207            "type: email\ncreated: 2026-05-27T08:00:00-07:00\nupdated: 2026-05-28T09:30:00-07:00";
2208        let fm = Frontmatter::parse(yaml, Path::new("f.md")).unwrap();
2209        let created = fm.created.expect("created parsed");
2210        // -07:00 offset is 7 * 3600 seconds west.
2211        assert_eq!(created.offset().utc_minus_local(), 7 * 3600);
2212        assert_eq!(created.to_rfc3339(), "2026-05-27T08:00:00-07:00");
2213        assert!(fm.updated.is_some());
2214    }
2215
2216    #[test]
2217    fn parse_rejects_non_rfc3339_timestamp() {
2218        // A date-only value is not a full RFC3339 timestamp; created/updated
2219        // require the full form.
2220        let yaml = "type: email\ncreated: 2026-05-27";
2221        let err = Frontmatter::parse(yaml, Path::new("bad.md")).unwrap_err();
2222        match err {
2223            ParseError::BadTimestamp { key, value, .. } => {
2224                assert_eq!(key, "created");
2225                assert_eq!(value, "2026-05-27");
2226            }
2227            other => panic!("expected BadTimestamp, got {other:?}"),
2228        }
2229    }
2230
2231    #[test]
2232    fn parse_malformed_yaml_errors() {
2233        // Unclosed flow mapping is invalid YAML.
2234        let yaml = "type: contact\n  bad: : :\n- nope";
2235        let err = Frontmatter::parse(yaml, Path::new("bad.md")).unwrap_err();
2236        assert!(matches!(err, ParseError::MalformedYaml { .. }));
2237    }
2238
2239    #[test]
2240    fn frontmatter_with_yaml_tag_on_mapping_does_not_panic() {
2241        // Regression: a YAML tag on the top-level mapping made the old
2242        // `expect_err` path PANIC, because a tagged mapping deserializes to a
2243        // `Mapping` just fine. It must now be handled — accepted as the inner
2244        // mapping, never a panic.
2245        let fm = Frontmatter::parse("!mytag\ntype: contact\nsummary: hi\n", Path::new("x.md"))
2246            .expect("tagged-mapping frontmatter must parse, not panic");
2247        assert_eq!(fm.type_.as_deref(), Some("contact"));
2248        // A genuine scalar/sequence top level is still malformed (and still
2249        // doesn't panic).
2250        assert!(Frontmatter::parse("- a\n- b\n", Path::new("x.md")).is_err());
2251    }
2252
2253    #[test]
2254    fn parse_empty_block_is_empty_frontmatter() {
2255        let fm = Frontmatter::parse("", Path::new("f.md")).unwrap();
2256        assert_eq!(fm, Frontmatter::default());
2257    }
2258
2259    #[test]
2260    fn parse_scalar_top_level_is_malformed() {
2261        // A bare scalar at the top level is not a frontmatter mapping.
2262        let err = Frontmatter::parse("just a string", Path::new("f.md")).unwrap_err();
2263        assert!(matches!(err, ParseError::MalformedYaml { .. }));
2264    }
2265
2266    // ── to_yaml canonical order ──────────────────────────────────────────────
2267
2268    #[test]
2269    fn to_yaml_emits_canonical_key_order() {
2270        let mut fm = Frontmatter {
2271            type_: Some("contact".into()),
2272            id: Some("sarah-chen".into()),
2273            summary: Some("Director of Ops".into()),
2274            status: Some("active".into()),
2275            tags: vec!["vip".into()],
2276            created: Some(DateTime::parse_from_rfc3339("2026-05-27T08:00:00-07:00").unwrap()),
2277            updated: Some(DateTime::parse_from_rfc3339("2026-05-28T09:30:00-07:00").unwrap()),
2278            ..Default::default()
2279        };
2280        // Two type-specific fields, inserted in NON-alphabetical order to prove
2281        // the writer sorts them (BTreeMap) between the universal head and tail.
2282        fm.extra
2283            .insert("role".into(), Value::String("Director".into()));
2284        fm.extra.insert(
2285            "company".into(),
2286            Value::String("[[records/companies/northstar]]".into()),
2287        );
2288
2289        let yaml = fm.to_yaml();
2290        let keys: Vec<&str> = yaml
2291            .lines()
2292            .filter(|l| !l.starts_with(['-', ' ']) && l.contains(':'))
2293            .map(|l| l.split(':').next().unwrap())
2294            .collect();
2295        assert_eq!(
2296            keys,
2297            vec![
2298                "type", "id", "created", "updated", "summary", // universal head
2299                "company", "role",   // type-specific, sorted
2300                "status", // universal tail
2301                "tags",
2302            ],
2303            "canonical order violated; got:\n{yaml}"
2304        );
2305        // Timestamps round-trip as RFC3339 strings (YAML may quote them).
2306        assert!(
2307            yaml.contains("2026-05-27T08:00:00-07:00"),
2308            "created timestamp missing; got:\n{yaml}"
2309        );
2310        // The value re-parses to the same instant regardless of quoting.
2311        let reparsed = Frontmatter::parse(&yaml, Path::new("rt.md")).unwrap();
2312        assert_eq!(reparsed.created, fm.created);
2313        assert_eq!(reparsed.updated, fm.updated);
2314    }
2315
2316    #[test]
2317    fn to_yaml_omits_absent_optional_fields() {
2318        let fm = Frontmatter {
2319            type_: Some("note".into()),
2320            ..Default::default()
2321        };
2322        let yaml = fm.to_yaml();
2323        assert!(yaml.contains("type: note"));
2324        assert!(!yaml.contains("status"));
2325        assert!(!yaml.contains("tags"));
2326        assert!(!yaml.contains("summary"));
2327    }
2328
2329    // ── Regression: non-string scalar universal fields round-trip (finding #1) ─
2330
2331    #[test]
2332    fn regression_parse_preserves_non_string_scalar_universal_fields() {
2333        // A hand/externally-authored file whose universal fields are bare
2334        // scalars YAML reads as Number/Bool — `id: 100`, `summary: 2026`,
2335        // `status: 0`, `type: 42` — must be PRESERVED as their string form, not
2336        // read as None. Before the fix, `v.as_str()` returned None for these and
2337        // the matched arm discarded the value entirely (never reaching `extra`).
2338        let yaml = "type: 42\nid: 100\nsummary: 2026\nstatus: 0";
2339        let fm = Frontmatter::parse(yaml, Path::new("x.md")).unwrap();
2340        assert_eq!(fm.type_.as_deref(), Some("42"), "type scalar dropped");
2341        assert_eq!(fm.id.as_deref(), Some("100"), "id scalar dropped");
2342        assert_eq!(
2343            fm.summary.as_deref(),
2344            Some("2026"),
2345            "summary scalar dropped"
2346        );
2347        assert_eq!(fm.status.as_deref(), Some("0"), "status scalar dropped");
2348        // The values must surface through the public `get` accessor too.
2349        assert_eq!(
2350            fm.get("summary")
2351                .and_then(|v| v.as_str().map(str::to_string)),
2352            Some("2026".to_string())
2353        );
2354    }
2355
2356    #[test]
2357    fn regression_format_round_trip_does_not_delete_numeric_frontmatter() {
2358        // The exact finding-#1 trigger: `dbmd format` is read_file -> write_file.
2359        // A file whose `id`/`summary`/`status` are bare numeric scalars must
2360        // still carry those fields after the canonical re-emit. Before the fix,
2361        // the lines were silently deleted from disk (only `type` survived).
2362        let dir = tempdir().unwrap();
2363        let path = dir.path().join("x.md");
2364        let original = "---\ntype: contact\nid: 100\nsummary: 2026\nstatus: 0\n---\nbody\n";
2365        std::fs::write(&path, original).unwrap();
2366
2367        // Re-emit through the canonical writer, exactly as `dbmd format` does.
2368        let (fm, body) = read_file(&path).unwrap();
2369        write_file(&path, &fm, &body).unwrap();
2370
2371        let after = std::fs::read_to_string(&path).unwrap();
2372        // None of the four fields may vanish; they survive as string scalars.
2373        let reparsed = Frontmatter::parse(
2374            &split_frontmatter(&after, &path).unwrap().frontmatter_yaml,
2375            &path,
2376        )
2377        .unwrap();
2378        assert_eq!(reparsed.type_.as_deref(), Some("contact"));
2379        assert_eq!(reparsed.id.as_deref(), Some("100"), "id deleted by format");
2380        assert_eq!(
2381            reparsed.summary.as_deref(),
2382            Some("2026"),
2383            "summary deleted by format"
2384        );
2385        assert_eq!(
2386            reparsed.status.as_deref(),
2387            Some("0"),
2388            "status deleted by format"
2389        );
2390        // The body is preserved verbatim.
2391        assert_eq!(body, "body\n");
2392    }
2393
2394    // ── Regression: BOM-prefixed files parse like store/index (finding #19) ────
2395
2396    #[test]
2397    fn regression_split_frontmatter_tolerates_leading_utf8_bom() {
2398        // A BOM-prefixed file (EF BB BF + `---\n...`) is walked and indexed by
2399        // `dbmd index` (store/index strip the BOM) but, before the fix, every
2400        // write/edit surface routed through `read_file` hard-failed with
2401        // MissingFrontmatter. `split_frontmatter` must now strip a single leading
2402        // U+FEFF and emit a BOM-free body.
2403        let text = "\u{feff}---\ntype: note\nsummary: x\n---\nbody\n";
2404        let parsed = split_frontmatter(text, Path::new("note.md")).unwrap();
2405        assert_eq!(parsed.frontmatter_yaml, "type: note\nsummary: x\n");
2406        // Body never carries the BOM forward into the canonical writer.
2407        assert_eq!(parsed.body, "body\n");
2408        assert!(!parsed.body.starts_with('\u{feff}'));
2409    }
2410
2411    #[test]
2412    fn regression_read_file_parses_bom_prefixed_file() {
2413        // End-to-end through the same `read_file` path `dbmd fm get/set`,
2414        // `format`, `link`, and `write` use. Before the fix this returned
2415        // Err(MissingFrontmatter) on a file the catalog had already indexed.
2416        let dir = tempdir().unwrap();
2417        let path = dir.path().join("note.md");
2418        std::fs::write(&path, "\u{feff}---\ntype: note\nsummary: x\n---\nbody\n").unwrap();
2419
2420        let (fm, body) = read_file(&path).expect("BOM-prefixed file must parse");
2421        assert_eq!(fm.type_.as_deref(), Some("note"));
2422        assert_eq!(fm.summary.as_deref(), Some("x"));
2423        assert_eq!(body, "body\n");
2424    }
2425
2426    #[test]
2427    fn to_yaml_preserves_unquoted_scalar_wiki_link_round_trip() {
2428        // Regression (PRIMARY): the SPEC-canonical scalar wiki-link is the
2429        // *unquoted* inline `company: [[records/companies/northstar]]`
2430        // (SPEC § Linking, the worked `contact` example). YAML parses it to the
2431        // nested `Seq[Seq[String]]` shape and `parse` stores that verbatim in
2432        // `extra`. Before the fix, `to_yaml` re-emitted it block-style as
2433        //     company:
2434        //     - - records/companies/northstar
2435        // — the `[[ ]]` brackets GONE — so a no-op re-emit (`dbmd format`, and
2436        // any `fm set` / `link` write) silently destroyed the link.
2437        let yaml = "type: contact\ncompany: [[records/companies/northstar]]";
2438        let fm = Frontmatter::parse(yaml, Path::new("c.md")).unwrap();
2439        // Sanity: it really parsed as the nested sequence, not a string.
2440        assert!(fm.extra.get("company").and_then(|v| v.as_str()).is_none());
2441
2442        let out = fm.to_yaml();
2443        // The link must survive as a quoted inline scalar — brackets intact, and
2444        // never the bracket-less block sequence `- - records/...`.
2445        assert!(
2446            out.contains("[[records/companies/northstar]]"),
2447            "canonical writer dropped the wiki-link brackets; got:\n{out}"
2448        );
2449        assert!(
2450            !out.contains("- - "),
2451            "canonical writer emitted a nested block sequence (link corrupted); got:\n{out}"
2452        );
2453
2454        // And it round-trips: re-parsing the emitted YAML still surfaces exactly
2455        // one link with the right target (the edge graph/backlinks rely on).
2456        let reparsed = Frontmatter::parse(&out, Path::new("c.md")).unwrap();
2457        let fields = reparsed.link_fields();
2458        let links: Vec<(&str, &str, Option<&str>)> = fields
2459            .iter()
2460            .map(|(k, l)| (k.as_str(), l.target.as_str(), l.display.as_deref()))
2461            .collect();
2462        assert_eq!(
2463            links,
2464            vec![("company", "records/companies/northstar", None)]
2465        );
2466
2467        // A second re-emit is a fixed point — no progressive corruption across
2468        // repeated curator-loop writes.
2469        assert_eq!(
2470            reparsed.to_yaml(),
2471            out,
2472            "to_yaml is not idempotent on links"
2473        );
2474    }
2475
2476    #[test]
2477    fn to_yaml_preserves_unquoted_scalar_link_with_display() {
2478        // The `|display` segment must survive the unquoted-inline round-trip too.
2479        let yaml = "type: contact\ncompany: [[records/companies/northstar|Northstar]]";
2480        let fm = Frontmatter::parse(yaml, Path::new("c.md")).unwrap();
2481        let out = fm.to_yaml();
2482        assert!(
2483            out.contains("[[records/companies/northstar|Northstar]]"),
2484            "display segment lost on round-trip; got:\n{out}"
2485        );
2486        let reparsed = Frontmatter::parse(&out, Path::new("c.md")).unwrap();
2487        let f = reparsed.link_fields();
2488        assert_eq!(f.len(), 1);
2489        assert_eq!(f[0].1.target, "records/companies/northstar");
2490        assert_eq!(f[0].1.display.as_deref(), Some("Northstar"));
2491    }
2492
2493    #[test]
2494    fn to_yaml_does_not_mangle_link_list_or_plain_nested_sequence() {
2495        // A genuine quoted block list of links round-trips as a clean string
2496        // list — never collapsed to a scalar — and a plain nested sequence that
2497        // is NOT a wiki-link is left exactly as written (no false conversion).
2498        let yaml = "type: meeting\nattendees:\n  - \"[[records/contacts/elena]]\"\n  - \"[[records/contacts/sarah]]\"\nmatrix:\n  - - 1\n    - 2";
2499        let fm = Frontmatter::parse(yaml, Path::new("m.md")).unwrap();
2500        let out = fm.to_yaml();
2501
2502        // Both attendee links survive as quoted strings.
2503        assert!(out.contains("[[records/contacts/elena]]"), "got:\n{out}");
2504        assert!(out.contains("[[records/contacts/sarah]]"), "got:\n{out}");
2505
2506        let reparsed = Frontmatter::parse(&out, Path::new("m.md")).unwrap();
2507        let fields = reparsed.link_fields();
2508        let attendees: Vec<&str> = fields
2509            .iter()
2510            .filter(|(k, _)| k == "attendees")
2511            .map(|(_, l)| l.target.as_str())
2512            .collect();
2513        assert_eq!(
2514            attendees,
2515            vec!["records/contacts/elena", "records/contacts/sarah"]
2516        );
2517        // The non-link nested sequence is preserved verbatim, not touched.
2518        assert_eq!(reparsed.extra.get("matrix"), fm.extra.get("matrix"));
2519    }
2520
2521    // ── read_file / write_file round-trip ────────────────────────────────────
2522
2523    #[test]
2524    fn write_then_read_roundtrips_and_preserves_body_verbatim() {
2525        let dir = tempdir().unwrap();
2526        let path = dir.path().join("sources/emails/x.md");
2527        let body = "# Subject\n\nHello,\n\nSee [[records/contacts/sarah-chen]].\n";
2528        let mut fm = Frontmatter {
2529            type_: Some("email".into()),
2530            summary: Some("renewal note".into()),
2531            created: Some(DateTime::parse_from_rfc3339("2026-05-27T08:00:00-07:00").unwrap()),
2532            ..Default::default()
2533        };
2534        fm.extra
2535            .insert("from".into(), Value::String("elena@northstar.io".into()));
2536
2537        write_file(&path, &fm, body).unwrap();
2538
2539        let (read_fm, read_body) = read_file(&path).unwrap();
2540        assert_eq!(read_body, body, "body must be preserved byte-for-byte");
2541        assert_eq!(read_fm.type_.as_deref(), Some("email"));
2542        assert_eq!(read_fm.summary.as_deref(), Some("renewal note"));
2543        assert_eq!(
2544            read_fm.extra.get("from").and_then(|v| v.as_str()),
2545            Some("elena@northstar.io")
2546        );
2547        // The on-disk file starts with a fence and ends with the verbatim body.
2548        let raw = std::fs::read_to_string(&path).unwrap();
2549        assert!(raw.starts_with("---\n"));
2550        assert!(raw.ends_with(body));
2551    }
2552
2553    #[test]
2554    fn roundtrip_modify_summary_then_write_changes_only_summary() {
2555        let dir = tempdir().unwrap();
2556        let path = dir.path().join("records/contacts/sarah.md");
2557        let body = "Long-form operator notes about Sarah.\n";
2558        let fm = Frontmatter {
2559            type_: Some("contact".into()),
2560            summary: Some("old summary".into()),
2561            ..Default::default()
2562        };
2563        write_file(&path, &fm, body).unwrap();
2564
2565        // Read → modify summary → write back.
2566        let (mut fm2, body2) = read_file(&path).unwrap();
2567        fm2.summary = Some("new summary".into());
2568        write_file(&path, &fm2, &body2).unwrap();
2569
2570        let (fm3, body3) = read_file(&path).unwrap();
2571        assert_eq!(fm3.summary.as_deref(), Some("new summary"));
2572        assert_eq!(fm3.type_.as_deref(), Some("contact"));
2573        assert_eq!(body3, body, "body unchanged across the round-trip");
2574    }
2575
2576    #[test]
2577    fn roundtrip_preserves_handwritten_unquoted_scalar_wiki_link_on_disk() {
2578        // End-to-end analog of `dbmd format` on the verbatim SPEC worked example:
2579        // a hand-written file carrying the canonical UNQUOTED scalar link
2580        // `company: [[records/companies/northstar]]`, read from disk then written
2581        // back unchanged. Before the fix this no-op re-emit rewrote the on-disk
2582        // value to the bracket-less block sequence `company:\n- - records/...`,
2583        // and every reader (validate/graph/backlinks) then lost the edge.
2584        let dir = tempdir().unwrap();
2585        let path = dir.path().join("records/contacts/sarah-chen.md");
2586        let file = "---\ntype: contact\nid: sarah-chen\nsummary: Director of Ops\ncompany: [[records/companies/northstar]]\n---\n# Sarah Chen\n\nNotes.\n";
2587        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
2588        std::fs::write(&path, file).unwrap();
2589
2590        // Read → write back unchanged (the canonical no-op re-emit).
2591        let (fm, body) = read_file(&path).unwrap();
2592        write_file(&path, &fm, &body).unwrap();
2593
2594        // On-disk bytes still carry the bracketed link, never `- - records/...`.
2595        let raw = std::fs::read_to_string(&path).unwrap();
2596        assert!(
2597            raw.contains("[[records/companies/northstar]]"),
2598            "on-disk wiki-link brackets were destroyed; got:\n{raw}"
2599        );
2600        assert!(
2601            !raw.contains("- - "),
2602            "on-disk value became a nested block sequence; got:\n{raw}"
2603        );
2604
2605        // And the edge is still readable after the round-trip.
2606        let (fm2, _) = read_file(&path).unwrap();
2607        let fields = fm2.link_fields();
2608        let links: Vec<(&str, &str)> = fields
2609            .iter()
2610            .map(|(k, l)| (k.as_str(), l.target.as_str()))
2611            .collect();
2612        assert_eq!(links, vec![("company", "records/companies/northstar")]);
2613    }
2614
2615    #[test]
2616    fn write_file_does_not_leave_temp_files_behind() {
2617        let dir = tempdir().unwrap();
2618        let path = dir.path().join("records/x.md");
2619        let fm = Frontmatter {
2620            type_: Some("note".into()),
2621            ..Default::default()
2622        };
2623        write_file(&path, &fm, "body\n").unwrap();
2624        // The directory should contain only the target file, no `.x.md.tmp.*`.
2625        let entries: Vec<String> = std::fs::read_dir(path.parent().unwrap())
2626            .unwrap()
2627            .map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
2628            .collect();
2629        assert_eq!(entries, vec!["x.md".to_string()]);
2630    }
2631
2632    // ── is_content_file ──────────────────────────────────────────────────────
2633
2634    #[test]
2635    fn is_content_file_recognizes_layers_and_excludes_meta() {
2636        assert!(Frontmatter::is_content_file(Path::new(
2637            "sources/emails/2026-05-22.md"
2638        )));
2639        assert!(Frontmatter::is_content_file(Path::new(
2640            "records/contacts/sarah-chen.md"
2641        )));
2642        // A synthesis profile the agent authored lives under `records/` (the
2643        // old `wiki/` layer is gone, so a `wiki/...` path is NOT content).
2644        assert!(Frontmatter::is_content_file(Path::new(
2645            "records/profiles/sarah-chen.md"
2646        )));
2647        assert!(!Frontmatter::is_content_file(Path::new(
2648            "wiki/people/sarah-chen.md"
2649        )));
2650        // Absolute paths under a layer are still content.
2651        assert!(Frontmatter::is_content_file(Path::new(
2652            "/home/db/records/companies/northstar.md"
2653        )));
2654        // index.md at any level is meta.
2655        assert!(!Frontmatter::is_content_file(Path::new(
2656            "records/contacts/index.md"
2657        )));
2658        assert!(!Frontmatter::is_content_file(Path::new("index.md")));
2659        // Root meta files.
2660        assert!(!Frontmatter::is_content_file(Path::new("DB.md")));
2661        assert!(!Frontmatter::is_content_file(Path::new("log.md")));
2662    }
2663
2664    // ── effective_id ─────────────────────────────────────────────────────────
2665
2666    #[test]
2667    fn effective_id_prefers_explicit_then_derives_from_path() {
2668        let with_id = Frontmatter {
2669            id: Some("explicit-id".into()),
2670            ..Default::default()
2671        };
2672        assert_eq!(
2673            with_id.effective_id(Path::new("records/profiles/sarah-chen.md")),
2674            "explicit-id"
2675        );
2676        let no_id = Frontmatter::default();
2677        assert_eq!(
2678            no_id.effective_id(Path::new("records/profiles/sarah-chen.md")),
2679            "sarah-chen"
2680        );
2681    }
2682
2683    // ── get / set ────────────────────────────────────────────────────────────
2684
2685    #[test]
2686    fn set_routes_universal_and_custom_keys() {
2687        let mut fm = Frontmatter::default();
2688        fm.set("type", "contact").unwrap();
2689        fm.set("summary", "hi").unwrap();
2690        fm.set("company", "[[records/companies/northstar]]")
2691            .unwrap();
2692        assert_eq!(fm.type_.as_deref(), Some("contact"));
2693        assert_eq!(fm.summary.as_deref(), Some("hi"));
2694        // Custom key landed in extra, not a typed slot.
2695        assert_eq!(
2696            fm.extra.get("company").and_then(|v| v.as_str()),
2697            Some("[[records/companies/northstar]]")
2698        );
2699        // get reads from both typed fields and extra.
2700        assert_eq!(
2701            fm.get("type").and_then(|v| v.as_str().map(String::from)),
2702            Some("contact".into())
2703        );
2704        assert_eq!(
2705            fm.get("company").and_then(|v| v.as_str().map(String::from)),
2706            Some("[[records/companies/northstar]]".into())
2707        );
2708        assert!(fm.get("nonexistent").is_none());
2709    }
2710
2711    #[test]
2712    fn set_timestamp_validates_rfc3339() {
2713        let mut fm = Frontmatter::default();
2714        fm.set("created", "2026-05-27T08:00:00-07:00").unwrap();
2715        assert!(fm.created.is_some());
2716        let err = fm.set("updated", "not-a-date").unwrap_err();
2717        assert!(matches!(err, ParseError::BadTimestamp { .. }));
2718    }
2719
2720    // ── extract_wiki_links ───────────────────────────────────────────────────
2721
2722    #[test]
2723    fn extract_wiki_links_flags_full_path_short_form_and_extension() {
2724        let body = "See [[records/contacts/sarah-chen]] and [[sarah-chen]].\nAlso [[records/profiles/sarah-chen.md|Sarah]].\n";
2725        let links = extract_wiki_links(body, Path::new("doc.md"));
2726        assert_eq!(links.len(), 3);
2727
2728        // Full path, no extension, no display.
2729        assert_eq!(links[0].target, "records/contacts/sarah-chen");
2730        assert!(links[0].is_full_path);
2731        assert!(!links[0].has_md_extension);
2732        assert_eq!(links[0].display, None);
2733        assert_eq!(links[0].location.1, 1, "first link on line 1");
2734
2735        // Short form: not a full path.
2736        assert_eq!(links[1].target, "sarah-chen");
2737        assert!(!links[1].is_full_path, "bare target is short-form");
2738
2739        // Full path WITH .md extension and a display override on line 2.
2740        assert_eq!(links[2].target, "records/profiles/sarah-chen.md");
2741        assert!(links[2].is_full_path);
2742        assert!(links[2].has_md_extension);
2743        assert_eq!(links[2].display.as_deref(), Some("Sarah"));
2744        assert_eq!(links[2].location.1, 2);
2745    }
2746
2747    #[test]
2748    fn extract_wiki_links_reports_1_based_column_counting_chars() {
2749        // A multi-byte prefix (é is 2 bytes) must not skew the char column.
2750        let body = "café [[records/x/y]]";
2751        let links = extract_wiki_links(body, Path::new("d.md"));
2752        assert_eq!(links.len(), 1);
2753        // "café " is 5 chars, so the `[[` starts at char column 6 (1-based).
2754        assert_eq!(links[0].location.2, 6);
2755    }
2756
2757    #[test]
2758    fn extract_wiki_links_columns_are_correct_for_multiple_links_on_one_line() {
2759        // Locks the single-pass column cursor (the O(n²)→O(n) fix): each `[[`
2760        // reports the right 1-based CHAR column even with multi-byte prefixes and
2761        // several links per line.
2762        let body = "café [[a]] · [[records/x/y]] end";
2763        let links = extract_wiki_links(body, Path::new("d.md"));
2764        assert_eq!(links.len(), 2);
2765        // "café " = 5 chars → first `[[` at col 6.
2766        assert_eq!(links[0].location.2, 6);
2767        // "café [[a]] · " = 5 + 5 (`[[a]]`) + 3 (` · `, `·` is 1 char) = 13 chars
2768        // → second `[[` at col 14.
2769        assert_eq!(links[1].location.2, 14);
2770    }
2771
2772    #[test]
2773    fn extract_wiki_links_ignores_a_lone_path_without_brackets() {
2774        let links = extract_wiki_links(
2775            "records/contacts/sarah-chen is not a link",
2776            Path::new("d.md"),
2777        );
2778        assert!(links.is_empty());
2779    }
2780
2781    // ── extract_markdown_links ───────────────────────────────────────────────
2782
2783    #[test]
2784    fn extract_markdown_links_captures_external_and_not_wiki_links() {
2785        let body =
2786            "See [the thread](https://x.com/a) and [[records/contacts/sarah-chen]] internally.\n";
2787        let md = extract_markdown_links(body, Path::new("d.md"));
2788        assert_eq!(
2789            md.len(),
2790            1,
2791            "wiki-link must not be captured as a markdown link"
2792        );
2793        assert_eq!(md[0].text, "the thread");
2794        assert_eq!(md[0].url, "https://x.com/a");
2795        assert_eq!(md[0].location.1, 1);
2796
2797        // And the wiki-link extractor must not pick up the markdown link.
2798        let wl = extract_wiki_links(body, Path::new("d.md"));
2799        assert_eq!(wl.len(), 1);
2800        assert_eq!(wl[0].target, "records/contacts/sarah-chen");
2801    }
2802
2803    // ── link_fields ──────────────────────────────────────────────────────────
2804
2805    #[test]
2806    fn link_fields_extracts_scalar_list_and_summary_links() {
2807        // The canonical list form quotes each item so YAML parses it as clean
2808        // strings; a scalar field may be quoted OR written in the canonical
2809        // unquoted inline form `company: [[x]]` (SPEC § Linking).
2810        let yaml = "type: meeting\nsummary: with [[records/contacts/elena]]\ncompany: \"[[records/companies/northstar]]\"\nattendees:\n  - \"[[records/contacts/elena]]\"\n  - \"[[records/contacts/sarah]]\"\nnotes: just plain text";
2811        let fm = Frontmatter::parse(yaml, Path::new("m.md")).unwrap();
2812        // Sanity: company really did parse as a scalar string here.
2813        assert!(fm.extra.get("company").and_then(|v| v.as_str()).is_some());
2814        let fields = fm.link_fields();
2815
2816        // company (scalar) once, with the right target.
2817        let company: Vec<&str> = fields
2818            .iter()
2819            .filter(|(k, _)| k == "company")
2820            .map(|(_, l)| l.target.as_str())
2821            .collect();
2822        assert_eq!(company, vec!["records/companies/northstar"]);
2823        // attendees (block list) twice.
2824        let attendees: Vec<&str> = fields
2825            .iter()
2826            .filter(|(k, _)| k == "attendees")
2827            .map(|(_, l)| l.target.as_str())
2828            .collect();
2829        assert_eq!(
2830            attendees,
2831            vec!["records/contacts/elena", "records/contacts/sarah"]
2832        );
2833        // summary link surfaced.
2834        assert_eq!(fields.iter().filter(|(k, _)| k == "summary").count(), 1);
2835        // Plain-text field is not a link.
2836        assert_eq!(fields.iter().filter(|(k, _)| k == "notes").count(), 0);
2837    }
2838
2839    #[test]
2840    fn link_fields_surfaces_canonical_unquoted_scalar_link() {
2841        // Regression: the canonical scalar wiki-link form is the *unquoted*
2842        // inline `company: [[records/companies/northstar]]` (SPEC § Linking).
2843        // YAML parses `[[x]]` as a flow-list-in-a-list (`Seq[Seq[String]]`), so
2844        // a naive `as_str()`-only walk drops it. link_fields() must still
2845        // surface exactly one link with the correct target.
2846        let yaml = "type: meeting\ncompany: [[records/companies/northstar]]";
2847        let fm = Frontmatter::parse(yaml, Path::new("m.md")).unwrap();
2848        // Sanity: it really did parse as the nested sequence form, NOT a string.
2849        assert!(fm.extra.get("company").and_then(|v| v.as_str()).is_none());
2850
2851        let fields = fm.link_fields();
2852        let links: Vec<(&str, &str, Option<&str>)> = fields
2853            .iter()
2854            .map(|(k, l)| (k.as_str(), l.target.as_str(), l.display.as_deref()))
2855            .collect();
2856        assert_eq!(
2857            links,
2858            vec![("company", "records/companies/northstar", None)]
2859        );
2860
2861        // The `|display` segment survives the unquoted inline form too.
2862        let fm2 = Frontmatter::parse(
2863            "type: meeting\ncompany: [[records/companies/northstar|Northstar]]",
2864            Path::new("m.md"),
2865        )
2866        .unwrap();
2867        let f2 = fm2.link_fields();
2868        assert_eq!(f2.len(), 1);
2869        assert_eq!(f2[0].0, "company");
2870        assert_eq!(f2[0].1.target, "records/companies/northstar");
2871        assert_eq!(f2[0].1.display.as_deref(), Some("Northstar"));
2872    }
2873
2874    #[test]
2875    fn link_fields_ignores_plain_one_item_flow_list() {
2876        // A plain one-item flow list `aliases: [foo]` parses to `Seq[String]`
2877        // — one nesting level shallower than an unquoted `[[foo]]` — and must
2878        // NOT be mistaken for a wiki-link.
2879        let yaml = "type: contact\naliases: [foo]";
2880        let fm = Frontmatter::parse(yaml, Path::new("c.md")).unwrap();
2881        assert_eq!(fm.link_fields(), Vec::new());
2882    }
2883
2884    // ── detect_flow_form_link_lists ──────────────────────────────────────────
2885
2886    #[test]
2887    fn detect_flow_form_flags_list_misencodings_not_scalars() {
2888        // The flow-form list mis-encoding (triple-nested) IS flagged; a scalar
2889        // inline wiki-link (double-nested) is NOT.
2890        let bad = "attendees: [[[records/x]], [[records/y]]]\nscalar_inline: [[records/z]]";
2891        let flagged = detect_flow_form_link_lists(bad);
2892        assert_eq!(flagged, vec!["attendees".to_string()]);
2893
2894        // An UNquoted block list is also a mis-encoding (parses triple-nested).
2895        let unquoted_block = "attendees:\n  - [[records/x]]\n  - [[records/y]]";
2896        assert_eq!(
2897            detect_flow_form_link_lists(unquoted_block),
2898            vec!["attendees".to_string()]
2899        );
2900
2901        // The canonical QUOTED block form parses to clean strings — NOT flagged.
2902        let good = "attendees:\n  - \"[[records/x]]\"\n  - \"[[records/y]]\"";
2903        assert!(detect_flow_form_link_lists(good).is_empty());
2904
2905        // A plain scalar list of strings is not flagged.
2906        let plain = "tags: [a, b, c]";
2907        assert!(detect_flow_form_link_lists(plain).is_empty());
2908    }
2909
2910    // ── extract_sections ─────────────────────────────────────────────────────
2911
2912    #[test]
2913    fn extract_sections_levels_nesting_and_boundaries() {
2914        let body = "intro text\n## First\nalpha\n### Sub\nbeta\n## Second\ngamma\n";
2915        let secs = extract_sections(body);
2916        let headings: Vec<(&str, u8)> =
2917            secs.iter().map(|s| (s.heading.as_str(), s.level)).collect();
2918        assert_eq!(headings, vec![("First", 2), ("Sub", 3), ("Second", 2)]);
2919
2920        // "First" (H2) body extends through its H3 child, stopping at "Second".
2921        let first = &secs[0];
2922        assert!(first.body.contains("alpha"));
2923        assert!(first.body.contains("### Sub"));
2924        assert!(first.body.contains("beta"));
2925        assert!(!first.body.contains("Second"));
2926
2927        // "Sub" (H3) stops at the next equal-or-shallower heading ("Second").
2928        let sub = &secs[1];
2929        assert!(sub.body.contains("beta"));
2930        assert!(!sub.body.contains("gamma"));
2931
2932        // 1-based line numbers within the body.
2933        assert_eq!(first.line, 2);
2934        assert_eq!(secs[2].line, 6);
2935    }
2936
2937    #[test]
2938    fn extract_sections_ignores_headings_in_fenced_code() {
2939        let body = "## Real\n```\n## Fake heading in code\n```\nafter\n";
2940        let secs = extract_sections(body);
2941        assert_eq!(secs.len(), 1);
2942        assert_eq!(secs[0].heading, "Real");
2943        // The fenced "## Fake" is part of Real's body, not its own section.
2944        assert!(secs[0].body.contains("## Fake heading in code"));
2945    }
2946
2947    // ── parse_field_spec ─────────────────────────────────────────────────────
2948
2949    #[test]
2950    fn parse_field_spec_required_and_shape() {
2951        let f = parse_field_spec("- email (required, email)");
2952        assert_eq!(f.name, "email");
2953        assert!(f.required);
2954        assert_eq!(f.shape, Some(Shape::Email));
2955        assert!(f.unknown_modifiers.is_empty());
2956    }
2957
2958    #[test]
2959    fn parse_field_spec_link_prefix_strips_trailing_slash() {
2960        let f = parse_field_spec("- company (required, link to records/companies/)");
2961        assert!(f.required);
2962        assert_eq!(f.link_prefix, Some(PathBuf::from("records/companies")));
2963        assert_eq!(f.shape, None);
2964    }
2965
2966    #[test]
2967    fn parse_field_spec_default_preserves_case_and_value() {
2968        let f = parse_field_spec("- currency (default USD)");
2969        assert_eq!(f.name, "currency");
2970        assert_eq!(f.default, Some(Value::String("USD".into())));
2971    }
2972
2973    #[test]
2974    fn parse_field_spec_enum_captures_comma_list_as_last_modifier() {
2975        let f = parse_field_spec("- status (required, enum: open, closed, pending)");
2976        assert!(f.required);
2977        assert_eq!(
2978            f.enum_values,
2979            Some(vec![
2980                "open".to_string(),
2981                "closed".to_string(),
2982                "pending".to_string()
2983            ])
2984        );
2985    }
2986
2987    #[test]
2988    fn parse_field_spec_bare_enum_keyword_is_not_itself_a_value() {
2989        // `enum` with no colon: the values are the remaining tokens; the keyword
2990        // itself must NOT leak in as an allowed value.
2991        let f = parse_field_spec("- status (required, enum, open, closed)");
2992        assert!(f.required);
2993        assert_eq!(
2994            f.enum_values,
2995            Some(vec!["open".to_string(), "closed".to_string()])
2996        );
2997    }
2998
2999    #[test]
3000    fn parse_field_spec_unknown_modifier_is_captured_not_errored() {
3001        let f = parse_field_spec("- weird (required, frobnicate, string)");
3002        assert!(f.required);
3003        assert_eq!(f.shape, Some(Shape::String));
3004        assert_eq!(f.unknown_modifiers, vec!["frobnicate".to_string()]);
3005    }
3006
3007    #[test]
3008    fn parse_field_spec_no_parens_is_freeform_optional() {
3009        let f = parse_field_spec("- nickname");
3010        assert_eq!(f.name, "nickname");
3011        assert!(!f.required);
3012        assert_eq!(f.shape, None);
3013        assert!(f.link_prefix.is_none());
3014        assert!(f.enum_values.is_none());
3015        assert!(f.unknown_modifiers.is_empty());
3016    }
3017
3018    // ── parse_schema_bullet (directives) ─────────────────────────────────────
3019
3020    #[test]
3021    fn schema_bullet_unique_single_field() {
3022        match parse_schema_bullet("- unique: email") {
3023            SchemaBullet::Unique(fields) => assert_eq!(fields, vec!["email".to_string()]),
3024            other => panic!("expected Unique, got {other:?}"),
3025        }
3026    }
3027
3028    #[test]
3029    fn schema_bullet_unique_compound_trims_and_splits() {
3030        match parse_schema_bullet("- unique: date, amount , vendor") {
3031            SchemaBullet::Unique(fields) => assert_eq!(
3032                fields,
3033                vec![
3034                    "date".to_string(),
3035                    "amount".to_string(),
3036                    "vendor".to_string()
3037                ]
3038            ),
3039            other => panic!("expected Unique, got {other:?}"),
3040        }
3041    }
3042
3043    #[test]
3044    fn schema_bullet_summary_template_keeps_braces_and_inner_colons() {
3045        match parse_schema_bullet("- summary_template: {role} at {company} (x: y)") {
3046            SchemaBullet::SummaryTemplate(t) => assert_eq!(t, "{role} at {company} (x: y)"),
3047            other => panic!("expected SummaryTemplate, got {other:?}"),
3048        }
3049    }
3050
3051    #[test]
3052    fn schema_bullet_field_with_enum_modifier_is_not_a_directive() {
3053        // A field whose modifiers contain a colon (`enum:`) parses as a field, not
3054        // a directive — its head has a `(` before any `:`.
3055        match parse_schema_bullet("- status (enum: open, closed)") {
3056            SchemaBullet::Field(f) => {
3057                assert_eq!(f.name, "status");
3058                assert_eq!(
3059                    f.enum_values,
3060                    Some(vec!["open".to_string(), "closed".to_string()])
3061                );
3062            }
3063            other => panic!("expected Field, got {other:?}"),
3064        }
3065    }
3066
3067    #[test]
3068    fn parse_db_md_schema_captures_unique_and_summary_template() {
3069        let db = "---\ntype: db-md\nscope: x\nowner: y\n---\n\n## Schemas\n\n### contact\n- email (required, email)\n- unique: email\n- summary_template: {role} at {company}\n";
3070        let config = parse_db_md(db, Path::new("DB.md")).unwrap();
3071        let s = config.schemas.get("contact").expect("contact schema");
3072        assert_eq!(s.fields.len(), 1, "directives are not parsed as fields");
3073        assert_eq!(s.unique_keys, vec![vec!["email".to_string()]]);
3074        assert_eq!(s.summary_template.as_deref(), Some("{role} at {company}"));
3075    }
3076
3077    #[test]
3078    fn schema_bullet_shard_directive_parses_values() {
3079        assert!(matches!(
3080            parse_schema_bullet("- shard: by-date"),
3081            SchemaBullet::Shard(Some(true))
3082        ));
3083        assert!(matches!(
3084            parse_schema_bullet("- shard: flat"),
3085            SchemaBullet::Shard(Some(false))
3086        ));
3087        // An unrecognized value is ignored (None), like an unknown modifier.
3088        assert!(matches!(
3089            parse_schema_bullet("- shard: weekly"),
3090            SchemaBullet::Shard(None)
3091        ));
3092        // A field whose name has a `(` before any `:` is still a field — the same
3093        // guard that keeps `- status (enum: a, b)` a field, not a directive.
3094        assert!(matches!(
3095            parse_schema_bullet("- shardiness (string)"),
3096            SchemaBullet::Field(_)
3097        ));
3098    }
3099
3100    #[test]
3101    fn parse_db_md_schema_captures_shard_directive() {
3102        let db = "---\ntype: db-md\nscope: x\nowner: y\n---\n\n## Schemas\n\n### shipment\n- carrier (string)\n- shard: by-date\n\n### contact\n- shard: flat\n";
3103        let config = parse_db_md(db, Path::new("DB.md")).unwrap();
3104        let shipment = config.schemas.get("shipment").expect("shipment schema");
3105        assert_eq!(shipment.shard, Some(true));
3106        assert_eq!(
3107            shipment.fields.len(),
3108            1,
3109            "`shard:` is a directive, not a field"
3110        );
3111        assert_eq!(config.schemas.get("contact").unwrap().shard, Some(false));
3112    }
3113
3114    // ── parse_db_md ──────────────────────────────────────────────────────────
3115
3116    const CANONICAL_DB_MD: &str = "---\ntype: db-md\nscope: company\nowner: Sarah Chen\n---\n\n# Acme operations knowledge base\n\nCompany-scale institutional memory for Acme.\n\n## Agent instructions\n\nPrioritize creating `contact` records from new-sender emails. Use British English.\n\n## Policies\n\n### Frozen pages\n- `records/decisions/2026-q1-strategy.md` — finalized, do not modify.\n- `records/synthesis/2026-annual-plan.md` — signed-off plan.\n\n### Ignored types\n- `test`, `temp` — read but never synthesize.\n\n## Schemas\n\n### contact\n- name (required)\n- email (required, email)\n- company (required, link to records/companies/)\n- role (string)\n\n### expense\n- date (required, date)\n- amount (required)\n- currency (default USD)\n";
3117
3118    #[test]
3119    fn parse_db_md_extracts_all_canonical_sections() {
3120        let config = parse_db_md(CANONICAL_DB_MD, Path::new("DB.md")).unwrap();
3121
3122        // Agent instructions: free-form prose, heading line stripped.
3123        let ai = config
3124            .agent_instructions
3125            .expect("agent instructions present");
3126        assert!(ai.starts_with("Prioritize creating"));
3127        assert!(!ai.contains("## Agent instructions"));
3128
3129        // Frozen pages: paths extracted from backticked bullets, comments dropped.
3130        assert_eq!(
3131            config.frozen_pages,
3132            vec![
3133                PathBuf::from("records/decisions/2026-q1-strategy.md"),
3134                PathBuf::from("records/synthesis/2026-annual-plan.md"),
3135            ]
3136        );
3137
3138        // Ignored types: comma list, backticks/comment stripped.
3139        assert_eq!(
3140            config.ignored_types,
3141            vec!["test".to_string(), "temp".to_string()]
3142        );
3143
3144        // Schemas: two types, each with its fields in source order.
3145        assert_eq!(config.schemas.len(), 2);
3146        let contact = config.schemas.get("contact").expect("contact schema");
3147        let names: Vec<&str> = contact.fields.iter().map(|f| f.name.as_str()).collect();
3148        assert_eq!(names, vec!["name", "email", "company", "role"]);
3149        assert!(contact.fields[0].required); // name
3150        assert_eq!(contact.fields[1].shape, Some(Shape::Email)); // email
3151        assert_eq!(
3152            contact.fields[2].link_prefix,
3153            Some(PathBuf::from("records/companies"))
3154        ); // company
3155
3156        let expense = config.schemas.get("expense").expect("expense schema");
3157        let cur = expense
3158            .fields
3159            .iter()
3160            .find(|f| f.name == "currency")
3161            .unwrap();
3162        assert_eq!(cur.default, Some(Value::String("USD".into())));
3163    }
3164
3165    #[test]
3166    fn parse_db_md_handles_malformed_and_unknown_modifiers() {
3167        // corpus-b shape: a `## Schemas` section with a malformed bullet, an
3168        // unknown modifier, and bullets that appear with NO `### <type>`
3169        // heading (so they belong to no schema and are dropped).
3170        let text = "---\ntype: db-md\n---\n\n## Schemas\n- orphan (required)\n\n### ticket\n- priority (required, mystery, enum: low, high)\n- broken (\n";
3171        let config = parse_db_md(text, Path::new("DB.md")).unwrap();
3172
3173        // The orphan bullet under `## Schemas` with no `### type` heading is not
3174        // captured as a schema.
3175        assert_eq!(config.schemas.len(), 1);
3176        let ticket = config.schemas.get("ticket").expect("ticket schema");
3177        assert_eq!(ticket.fields.len(), 2);
3178
3179        let priority = &ticket.fields[0];
3180        assert!(priority.required);
3181        assert_eq!(priority.unknown_modifiers, vec!["mystery".to_string()]);
3182        assert_eq!(
3183            priority.enum_values,
3184            Some(vec!["low".to_string(), "high".to_string()])
3185        );
3186
3187        // A bullet with an unclosed paren still yields a usable name.
3188        let broken = &ticket.fields[1];
3189        assert_eq!(broken.name, "broken");
3190    }
3191
3192    #[test]
3193    fn parse_db_md_missing_frontmatter_errors() {
3194        let text = "# No frontmatter\n\n## Agent instructions\nhi\n";
3195        let err = parse_db_md(text, Path::new("DB.md")).unwrap_err();
3196        assert!(matches!(err, ParseError::MissingFrontmatter { .. }));
3197    }
3198
3199    #[test]
3200    fn parse_db_md_absent_sections_default_empty() {
3201        let text = "---\ntype: db-md\n---\n\n# Title only\n";
3202        let config = parse_db_md(text, Path::new("DB.md")).unwrap();
3203        assert_eq!(config, Config::default());
3204    }
3205
3206    // ── fm set / --fm list-valued link fields (meeting.attendees & friends) ──
3207
3208    /// `Frontmatter::set` is the value path every write surface (`fm set`,
3209    /// `write --fm`) funnels through. A list-of-wiki-links value (the SPEC's
3210    /// `meeting.attendees` shape) must serialize as a YAML **block sequence** of
3211    /// quoted links — readable back by [`links_in_field_value`] and accepted by
3212    /// `dbmd validate` — never the flow-form scalar string that trips
3213    /// `WIKI_LINK_FLOW_FORM_LIST`. Both the unquoted (`[[[a]], [[b]]]`) and
3214    /// quoted (`["[[a]]", "[[b]]"]`) spellings an agent types must normalize.
3215    #[test]
3216    fn set_list_of_wiki_links_becomes_block_sequence_both_spellings() {
3217        for value in [
3218            "[[[records/contacts/a]], [[records/contacts/b]]]",
3219            r#"["[[records/contacts/a]]", "[[records/contacts/b]]"]"#,
3220        ] {
3221            let mut fm = Frontmatter::default();
3222            fm.set("attendees", value).unwrap();
3223
3224            // Stored as a 2-element sequence of clean quoted links.
3225            let stored = fm.extra.get("attendees").expect("attendees set");
3226            let Value::Sequence(items) = stored else {
3227                panic!("attendees must be a Sequence, got {stored:?} for input {value}");
3228            };
3229            assert_eq!(items.len(), 2, "input {value}");
3230            assert_eq!(items[0], Value::String("[[records/contacts/a]]".into()));
3231            assert_eq!(items[1], Value::String("[[records/contacts/b]]".into()));
3232
3233            // The edge enumerator reads exactly the two links back (no stray
3234            // bracket targets, the flow-form-string symptom).
3235            let links: Vec<_> = links_in_field_value(stored)
3236                .into_iter()
3237                .map(|l| l.target)
3238                .collect();
3239            assert_eq!(
3240                links,
3241                vec!["records/contacts/a", "records/contacts/b"],
3242                "input {value}"
3243            );
3244
3245            // And the canonical writer renders it block-style, not as a scalar.
3246            let yaml = fm.to_yaml();
3247            assert!(
3248                yaml.contains("attendees:\n"),
3249                "expected block list in:\n{yaml}"
3250            );
3251            assert!(
3252                !yaml.contains("attendees: '[["),
3253                "must not be a flow-form scalar string in:\n{yaml}"
3254            );
3255        }
3256    }
3257
3258    /// A *single* inline wiki-link stays a scalar string (renders inline
3259    /// `field: [[x]]`), and a single link must never be widened to a one-item
3260    /// list — preserving the common `contact.company` / `expense.vendor` shape.
3261    #[test]
3262    fn set_single_inline_wiki_link_stays_scalar() {
3263        let mut fm = Frontmatter::default();
3264        fm.set("company", "[[records/companies/tideform]]").unwrap();
3265        assert_eq!(
3266            fm.extra.get("company"),
3267            Some(&Value::String("[[records/companies/tideform]]".into())),
3268        );
3269        // Still recognized as one link.
3270        let links: Vec<_> = links_in_field_value(fm.extra.get("company").unwrap())
3271            .into_iter()
3272            .map(|l| l.target)
3273            .collect();
3274        assert_eq!(links, vec!["records/companies/tideform"]);
3275    }
3276
3277    /// Plain text and a non-link flow list are left as verbatim scalar strings —
3278    /// the list normalization only triggers when every item is a clean wiki-link.
3279    #[test]
3280    fn set_non_link_values_stay_scalar_strings() {
3281        let mut fm = Frontmatter::default();
3282        fm.set("location", "Video call (remote)").unwrap();
3283        assert_eq!(
3284            fm.extra.get("location"),
3285            Some(&Value::String("Video call (remote)".into())),
3286        );
3287
3288        // A flow list whose items are NOT wiki-links must not be reinterpreted as
3289        // a link sequence; it stays the scalar string the agent passed.
3290        fm.set("note", "[draft, wip]").unwrap();
3291        assert_eq!(
3292            fm.extra.get("note"),
3293            Some(&Value::String("[draft, wip]".into()))
3294        );
3295    }
3296
3297    // ── Regression: non-string YAML keys round-trip (no Rust Debug corruption) ─
3298
3299    #[test]
3300    fn regression_non_string_yaml_keys_keep_their_text_on_round_trip() {
3301        // A numeric/bool/null/float frontmatter key is valid YAML and must NOT be
3302        // rewritten to its Rust `Debug` form (`Number(2026)`, `Bool(true)`,
3303        // `'Null'`). After the fix the key text survives (the key narrows to a
3304        // string-typed key, but the operator's data is no longer corrupted).
3305        let yaml = "type: note\n2026: planning notes\ntrue: yes-key\n3.14: f\n";
3306        let fm = Frontmatter::parse(yaml, Path::new("x.md")).unwrap();
3307        // Keys are stored as their scalar text, not the Debug string.
3308        assert!(fm.extra.contains_key("2026"), "numeric key text lost");
3309        assert!(fm.extra.contains_key("true"), "bool key text lost");
3310        assert!(fm.extra.contains_key("3.14"), "float key text lost");
3311        assert!(!fm.extra.keys().any(|k| k.starts_with("Number(")));
3312        assert!(!fm.extra.keys().any(|k| k.starts_with("Bool(")));
3313
3314        // And a re-emit never produces the Debug forms on disk.
3315        let out = fm.to_yaml();
3316        assert!(!out.contains("Number("), "Debug-form key emitted:\n{out}");
3317        assert!(!out.contains("Bool("), "Debug-form key emitted:\n{out}");
3318        // The key text is still present (quoted, since it now reads as a string).
3319        assert!(out.contains("2026"), "numeric key dropped:\n{out}");
3320        assert!(out.contains("planning notes"), "value dropped:\n{out}");
3321    }
3322
3323    // ── Regression: universal-key sequence/mapping values are preserved (#2) ───
3324
3325    #[test]
3326    fn regression_universal_key_non_scalar_value_is_preserved_not_deleted() {
3327        // A universal key carrying a sequence/mapping (`status: [active, draft]`)
3328        // is not a valid scalar for that field. Before the fix, the matched arm
3329        // consumed-and-dropped it (scalar_string -> None) and `to_yaml` then
3330        // omitted the field — `dbmd format` silently DELETED it. It must now pass
3331        // through `extra` and re-emit verbatim.
3332        let yaml = "type: note\nstatus:\n  - active\n  - draft\nsummary:\n  a: 1\n  b: 2\n";
3333        let fm = Frontmatter::parse(yaml, Path::new("x.md")).unwrap();
3334        // The typed accessors stay None (no valid scalar), but the data lives in
3335        // extra so nothing is lost.
3336        assert!(fm.status.is_none());
3337        assert!(fm.summary.is_none());
3338        assert!(fm.extra.contains_key("status"), "status value destroyed");
3339        assert!(fm.extra.contains_key("summary"), "summary value destroyed");
3340
3341        // A re-emit keeps both fields' data on disk.
3342        let out = fm.to_yaml();
3343        assert!(out.contains("status"), "status deleted on re-emit:\n{out}");
3344        assert!(out.contains("active"), "status items deleted:\n{out}");
3345        assert!(
3346            out.contains("summary"),
3347            "summary deleted on re-emit:\n{out}"
3348        );
3349
3350        // Round-trips as a fixed point — repeated curator-loop writes don't lose
3351        // the data.
3352        let reparsed = Frontmatter::parse(&out, Path::new("x.md")).unwrap();
3353        assert!(reparsed.extra.contains_key("status"));
3354        assert!(reparsed.extra.contains_key("summary"));
3355    }
3356
3357    // ── Regression: non-scalar tags items don't erase the tags field (#5) ──────
3358
3359    #[test]
3360    fn regression_non_scalar_tags_value_is_preserved_not_erased() {
3361        // `tags: [[vip]]` (an authoring slip — wiki-link brackets around a tag)
3362        // parses to a nested sequence; before the fix `parse_tags` filtered the
3363        // non-scalar item out and `to_yaml` then omitted the now-empty tags vec,
3364        // silently DELETING the tags line. It must now survive the re-emit (the
3365        // key data is preserved; the field is never dropped).
3366        let yaml = "type: note\ntags: [[vip]]\n";
3367        let fm = Frontmatter::parse(yaml, Path::new("x.md")).unwrap();
3368        // The typed tags vec is empty (no clean scalar list), but the raw value
3369        // is preserved in extra so nothing is destroyed.
3370        assert!(fm.tags.is_empty());
3371        assert!(fm.extra.contains_key("tags"), "tags value destroyed");
3372
3373        let out = fm.to_yaml();
3374        assert!(out.contains("tags"), "tags deleted on re-emit:\n{out}");
3375        // The `vip` text survives on disk in some form (never erased).
3376        assert!(out.contains("vip"), "tag content erased:\n{out}");
3377
3378        // A clean tag list still parses to the typed vec (not regressed).
3379        let clean =
3380            Frontmatter::parse("type: note\ntags: [vip, renewal]\n", Path::new("x.md")).unwrap();
3381        assert_eq!(clean.tags, vec!["vip".to_string(), "renewal".to_string()]);
3382        assert!(!clean.extra.contains_key("tags"));
3383    }
3384
3385    // ── Regression: plain nested string lists are NOT fabricated into links (#3) ─
3386
3387    #[test]
3388    fn regression_plain_nested_string_list_is_not_turned_into_wiki_links() {
3389        // `groups: [[alpha], [beta]]` is the data [["alpha"],["beta"]] — an
3390        // unknown nested string list that must pass through verbatim. Before the
3391        // fix, canonicalize_extra_value fabricated `- '[[alpha]]'` / `- '[[beta]]'`
3392        // (short-form links the tool then flagged), changing the field's type.
3393        let yaml = "type: note\ngroups: [[alpha], [beta]]\n";
3394        let fm = Frontmatter::parse(yaml, Path::new("x.md")).unwrap();
3395        let before = fm.extra.get("groups").cloned();
3396
3397        let out = fm.to_yaml();
3398        // No fabricated wiki-link brackets in the emitted YAML.
3399        assert!(!out.contains("[[alpha]]"), "fabricated a wiki-link:\n{out}");
3400        assert!(!out.contains("[[beta]]"), "fabricated a wiki-link:\n{out}");
3401
3402        // The value is unchanged across the canonical re-emit.
3403        let reparsed = Frontmatter::parse(&out, Path::new("x.md")).unwrap();
3404        assert_eq!(
3405            reparsed.extra.get("groups"),
3406            before.as_ref(),
3407            "nested string list mutated by canonicalize_extra_value"
3408        );
3409        // And it surfaces no links.
3410        assert!(reparsed.link_fields().is_empty());
3411    }
3412
3413    // ── Regression: fence-line trailing whitespace is tolerated (#4) ───────────
3414
3415    #[test]
3416    fn regression_split_frontmatter_tolerates_trailing_whitespace_on_fences() {
3417        // A fence written `--- ` (trailing space — invisible in editors) is
3418        // indexed/validated clean by index.rs/validate.rs (both use `trim_end()`)
3419        // but, before the fix, hard-failed every read/edit surface routed through
3420        // `split_frontmatter`. All three must now agree.
3421        let text = "--- \ntype: note\nsummary: x\n---\t\nbody\n";
3422        let parsed = split_frontmatter(text, Path::new("f.md")).unwrap();
3423        assert_eq!(parsed.frontmatter_yaml, "type: note\nsummary: x\n");
3424        assert_eq!(parsed.body, "body\n");
3425
3426        // End to end through read_file's parse.
3427        let fm = Frontmatter::parse(&parsed.frontmatter_yaml, Path::new("f.md")).unwrap();
3428        assert_eq!(fm.type_.as_deref(), Some("note"));
3429    }
3430
3431    // ── Regression: CommonMark trailing-'#' heading rule (#6) ──────────────────
3432
3433    #[test]
3434    fn regression_heading_text_keeps_abutting_hash_drops_closing_sequence() {
3435        // `## C#` → `C#` (the `#` abuts content, not a closing sequence).
3436        assert_eq!(heading_text("## C#", 2), "C#");
3437        assert_eq!(heading_text("## F#", 2), "F#");
3438        assert_eq!(heading_text("## issue-123#", 2), "issue-123#");
3439        // A genuine ATX closing sequence (space before the `#` run) is dropped.
3440        assert_eq!(heading_text("## Title ##", 2), "Title");
3441        assert_eq!(heading_text("## Title #", 2), "Title");
3442        // All-hashes content collapses to empty.
3443        assert_eq!(heading_text("## ##", 2), "");
3444        // No trailing hashes — unchanged.
3445        assert_eq!(heading_text("## Plain", 2), "Plain");
3446    }
3447
3448    #[test]
3449    fn regression_extract_sections_keeps_csharp_heading_and_schema_type_binds() {
3450        // `dbmd sections` must report `C#`, not `C`.
3451        let secs = extract_sections("## C#\nbody\n");
3452        assert_eq!(secs.len(), 1);
3453        assert_eq!(secs[0].heading, "C#");
3454
3455        // And a `### c#` schema must register under `c#`, not `c`.
3456        let db = "---\ntype: db-md\n---\n\n## Schemas\n\n### c#\n- name (required)\n";
3457        let config = parse_db_md(db, Path::new("DB.md")).unwrap();
3458        assert!(
3459            config.schemas.contains_key("c#"),
3460            "schema bound to wrong key"
3461        );
3462        assert!(!config.schemas.contains_key("c"));
3463    }
3464
3465    // ── Regression: section line numbers offset by the frontmatter block (#7) ──
3466
3467    #[test]
3468    fn regression_extract_sections_in_file_reports_source_line_numbers() {
3469        // A heading on file line 6 (after a 4-line frontmatter block + 1 body
3470        // line) must be reported as L6, not the body-relative L2.
3471        let text = "---\ntype: note\nsummary: x\n---\nbody line\n## Heading\nmore\n";
3472        let secs = extract_sections_in_file(text);
3473        assert_eq!(secs.len(), 1);
3474        assert_eq!(secs[0].heading, "Heading");
3475        assert_eq!(secs[0].line, 6, "section line not offset by frontmatter");
3476
3477        // The body-relative helper is unchanged (validate relies on that frame).
3478        let body_secs = extract_sections("body line\n## Heading\nmore\n");
3479        assert_eq!(body_secs[0].line, 2);
3480
3481        // No frontmatter: whole text is body, no offset.
3482        let plain = extract_sections_in_file("## Top\nx\n## Next\n");
3483        assert_eq!(plain[0].line, 1);
3484        assert_eq!(plain[1].line, 3);
3485    }
3486
3487    // ── Regression: colon-form schema field bullet parses modifiers (#8) ───────
3488
3489    #[test]
3490    fn regression_colon_form_field_bullet_parses_modifiers() {
3491        // `- title: string, required` is the natural mis-spelling of
3492        // `- title (string, required)`; before the fix the whole text became the
3493        // field name and every modifier was silently lost.
3494        let f = parse_field_spec("- title: string, required");
3495        assert_eq!(f.name, "title");
3496        assert!(f.required, "required modifier lost on colon-form");
3497        assert_eq!(f.shape, Some(Shape::String));
3498
3499        // Through the schema-bullet classifier (the real path), it is a Field.
3500        match parse_schema_bullet("- title: string, required") {
3501            SchemaBullet::Field(f) => {
3502                assert_eq!(f.name, "title");
3503                assert!(f.required);
3504                assert_eq!(f.shape, Some(Shape::String));
3505            }
3506            other => panic!("expected Field, got {other:?}"),
3507        }
3508
3509        // A paren form whose modifiers contain a colon still parses by parens.
3510        let g = parse_field_spec("- status (enum: open, closed)");
3511        assert_eq!(g.name, "status");
3512        assert_eq!(
3513            g.enum_values,
3514            Some(vec!["open".to_string(), "closed".to_string()])
3515        );
3516    }
3517
3518    // ── Regression: comma inside a `default` value is preserved (#9) ───────────
3519
3520    #[test]
3521    fn regression_default_value_preserves_internal_commas() {
3522        let f = parse_field_spec("- title (default Director, Operations)");
3523        assert_eq!(
3524            f.default,
3525            Some(Value::String("Director, Operations".into())),
3526            "comma-bearing default truncated"
3527        );
3528
3529        let g = parse_field_spec("- region (default North America, EMEA fallback)");
3530        assert_eq!(
3531            g.default,
3532            Some(Value::String("North America, EMEA fallback".into()))
3533        );
3534
3535        // A single-token default still works (no regression).
3536        let h = parse_field_spec("- currency (default USD)");
3537        assert_eq!(h.default, Some(Value::String("USD".into())));
3538    }
3539
3540    // ── Regression: a `default` after `enum` is parsed, not swallowed (#10) ────
3541
3542    #[test]
3543    fn regression_default_after_enum_is_parsed_not_an_enum_member() {
3544        let f = parse_field_spec("- status (enum: open, closed, default open)");
3545        assert_eq!(
3546            f.enum_values,
3547            Some(vec!["open".to_string(), "closed".to_string()]),
3548            "`default open` leaked into the enum list"
3549        );
3550        assert_eq!(
3551            f.default,
3552            Some(Value::String("open".into())),
3553            "default after enum was dropped"
3554        );
3555
3556        // The bare `enum` keyword form, with a trailing default.
3557        let g = parse_field_spec("- status (enum, open, closed, default open)");
3558        assert_eq!(
3559            g.enum_values,
3560            Some(vec!["open".to_string(), "closed".to_string()])
3561        );
3562        assert_eq!(g.default, Some(Value::String("open".into())));
3563    }
3564
3565    // ── Regression: frozen-page policy does not fail open (#11) ────────────────
3566
3567    #[test]
3568    fn regression_frozen_match_handles_leading_slash() {
3569        let cfg = Config {
3570            frozen_pages: vec![PathBuf::from("/records/decisions/q1.md")],
3571            ..Config::default()
3572        };
3573        assert!(
3574            cfg.is_frozen(Path::new("records/decisions/q1.md")),
3575            "leading-slash entry failed open"
3576        );
3577        assert!(cfg.is_frozen(Path::new("records/decisions/q1")));
3578    }
3579
3580    #[test]
3581    fn regression_frozen_match_supports_globs() {
3582        let cfg = Config {
3583            frozen_pages: vec![PathBuf::from("records/decisions/*")],
3584            ..Config::default()
3585        };
3586        assert!(
3587            cfg.is_frozen(Path::new("records/decisions/q1.md")),
3588            "glob entry failed to protect a concrete file"
3589        );
3590        assert!(cfg.is_frozen(Path::new("records/decisions/q2.md")));
3591        // The glob does not cross a `/` segment.
3592        assert!(!cfg.is_frozen(Path::new("records/decisions/sub/q1.md")));
3593        // `**` crosses segments.
3594        let deep = Config {
3595            frozen_pages: vec![PathBuf::from("records/**")],
3596            ..Config::default()
3597        };
3598        assert!(deep.is_frozen(Path::new("records/decisions/sub/q1.md")));
3599        assert!(deep.is_frozen(Path::new("records/x.md")));
3600        assert!(!deep.is_frozen(Path::new("sources/x.md")));
3601        // A `*.md`-style intra-segment glob.
3602        let suffix = Config {
3603            frozen_pages: vec![PathBuf::from("records/decisions/q*")],
3604            ..Config::default()
3605        };
3606        assert!(suffix.is_frozen(Path::new("records/decisions/q1.md")));
3607        assert!(!suffix.is_frozen(Path::new("records/decisions/draft.md")));
3608    }
3609
3610    #[test]
3611    fn regression_frozen_entry_single_hyphen_comment_is_stripped() {
3612        // `records/decisions/q3.md - finalized` (single ASCII hyphen comment, no
3613        // backticks): the comment must be stripped so the entry is just the path.
3614        let path = extract_path_bullet("- records/decisions/q3.md - finalized");
3615        assert_eq!(path, "records/decisions/q3.md");
3616
3617        // End to end: such a bullet freezes the file.
3618        let cfg = Config {
3619            frozen_pages: vec![PathBuf::from(extract_path_bullet(
3620                "- records/decisions/q3.md - finalized",
3621            ))],
3622            ..Config::default()
3623        };
3624        assert!(
3625            cfg.is_frozen(Path::new("records/decisions/q3.md")),
3626            "single-hyphen-comment entry failed open"
3627        );
3628    }
3629}