Skip to main content

memstead_schema/
loader.rs

1//! Schema loader — reads on-disk schema directories (or in-memory YAML) into
2//! validated `Schema` values with `edge_weights` resolved.
3//!
4//! Three validation layers coordinate here:
5//! 1. Structural (serde + `deny_unknown_fields`) — handled by the deserialize.
6//! 2. Semantic (this module) — cross-field rules listed in `SchemaLoadError`.
7//! 3. Editor (JSON Schemas) — generated by `emit_json_schemas`, consumed by
8//!    schema authors via `# yaml-language-server: $schema=...`.
9
10use std::collections::{HashMap, HashSet};
11use std::path::{Path, PathBuf};
12use std::sync::Arc;
13
14use indexmap::IndexMap;
15use thiserror::Error;
16
17use crate::base_metadata;
18use crate::manifest::SchemaManifest;
19use crate::schema::Schema;
20use crate::types::TypeDefinition;
21
22#[derive(Debug, Error)]
23pub enum SchemaLoadError {
24    #[error("i/o error reading {}: {source}", .path.display())]
25    Io {
26        path: PathBuf,
27        #[source]
28        source: std::io::Error,
29    },
30
31    #[error("failed to parse manifest {}: {source}", .path.display())]
32    ParseManifest {
33        path: PathBuf,
34        #[source]
35        source: serde_yaml_ng::Error,
36    },
37
38    #[error("failed to parse type file {}: {source}", .path.display())]
39    ParseType {
40        path: PathBuf,
41        #[source]
42        source: serde_yaml_ng::Error,
43    },
44
45    #[error("invalid version '{value}': must be semver (e.g. 1.0.0)")]
46    InvalidVersion { value: String },
47
48    #[error("invalid schema name '{value}': {reason}")]
49    InvalidName { value: String, reason: &'static str },
50
51    #[error(
52        "schema type file mismatch — declared in manifest: [{}], found in types/: [{}]",
53        declared.join(", "),
54        found.join(", ")
55    )]
56    TypeFileMismatch {
57        declared: Vec<String>,
58        found: Vec<String>,
59    },
60
61    #[error(
62        "type file '{file}.yaml' has `name: {declared}` — filename and `name` field must match"
63    )]
64    TypeNameMismatch { file: String, declared: String },
65    /// The retired `propagating_relationships:` key was used in an
66    /// authoring context (a schema package loaded from a directory —
67    /// workspace `.memstead/schemas/`, `schema install`, `schema
68    /// validate`). The key's name promised propagation the engine
69    /// never performed; its only effect — refusing self-loops on the
70    /// listed rel-types — now lives under `no_self_loop_relationships`.
71    /// Sealed content (built-ins, installed refs) keeps loading with
72    /// the old key translated; only authoring refuses, so the fix is
73    /// one mechanical key rename per schema.
74    #[error(
75        "type '{type_name}': `propagating_relationships` was renamed — its only effect is \
76         refusing self-loops on the listed rel-types, so the key is now \
77         `no_self_loop_relationships` (optional; empty lists can simply be deleted). \
78         Rename the key and retry."
79    )]
80    PropagatingRelationshipsRenamed { type_name: String },
81
82    #[error(
83        "type '{type_name}' declares the retired `examples:` list — it was never \
84         validated nor served and is replaced by the engine-validated `exemplar:` \
85         (one canonical entity: title, metadata, sections, relations with \
86         placeholder targets). Move the material into `exemplar:` and retry."
87    )]
88    ExamplesRetired { type_name: String },
89
90    /// An exemplar relation entry used the retired authoring spelling
91    /// in an authoring context. Exemplars are authored in the mutation
92    /// vocabulary so what an agent copies from the served schema is
93    /// exactly what the write gate accepts. Sealed content keeps
94    /// loading with the old keys translated; only authoring refuses,
95    /// so the fix is one mechanical key rename per relation entry.
96    #[error(
97        "type '{type_name}': exemplar relation entries speak the mutation vocabulary — \
98         rename `to:` to `target:` and `type:` to `rel_type:`, then retry. (Sealed \
99         packages with the old spelling keep loading; only authoring refuses.)"
100    )]
101    ExemplarRelationSpellingRetired { type_name: String },
102
103    /// An exemplar relation entry is missing `target:` or `rel_type:`
104    /// after legacy translation — the entry cannot name an edge.
105    #[error(
106        "type '{type_name}': an exemplar relation entry must carry both `target:` \
107         (bare placeholder slug) and `rel_type:` (declared relationship name)."
108    )]
109    ExemplarRelationIncomplete { type_name: String },
110
111    /// The retired `optional:` metadata-field key was used in an
112    /// authoring context. The polarity flipped (first-author-path
113    /// plan 07): a field is optional unless it declares
114    /// `required: true` — the same rule sections follow. Sealed
115    /// content keeps loading with the old key inverted; only
116    /// authoring refuses, so the fix is one mechanical edit.
117    #[error(
118        "type '{type_name}' metadata field '{field}' declares the retired `optional:` key — \
119         fields are optional unless they declare `required: true`. Fix: delete `optional: true`; \
120         replace `optional: false` with `required: true`. Then retry."
121    )]
122    OptionalRetired { type_name: String, field: String },
123
124    /// A `due:` declaration referencing fields the type does not have
125    /// in the required shapes. `offender` names the bad reference,
126    /// `reason` states the shape rule it violates — the due axis is
127    /// spec (an external declaration), so its references validate at
128    /// load with the loader's usual recovery quality.
129    #[error("type '{type_name}' due axis is invalid: {reason} — offending name: '{offender}'")]
130    InvalidDueAxis {
131        type_name: String,
132        offender: String,
133        reason: String,
134    },
135
136    #[error("schema relationship vocabulary must include a '_default' definition")]
137    MissingDefaultWeight,
138
139    #[error("duplicate relationship definition: '{name}'")]
140    DuplicateRelationship { name: String },
141
142    #[error(
143        "type '{type_name}' references relationship '{relationship}' in field '{field}' — not declared in schema. Available: [{}]. {}",
144        available.join(", "),
145        format_suggestion(relationship, available)
146    )]
147    UndeclaredRelationship {
148        type_name: String,
149        field: &'static str,
150        relationship: String,
151        available: Vec<String>,
152    },
153
154    #[error(
155        "type '{type_name}' must have exactly one section with `catch_all: true` (found {count})"
156    )]
157    CatchAllViolation { type_name: String, count: usize },
158
159    #[error(
160        "type '{type_name}' field '{field}' references unknown key '{reference}' — not a section or metadata field"
161    )]
162    UnknownFieldReference {
163        type_name: String,
164        field: &'static str,
165        reference: String,
166    },
167
168    #[error(
169        "type '{type_name}' constraint ({kind}) is invalid: {reason} — offending name: '{offender}'"
170    )]
171    InvalidConstraint {
172        type_name: String,
173        kind: &'static str,
174        offender: String,
175        reason: String,
176    },
177
178    #[error("relationships.acyclic_sets is invalid: {reason} — offending entry: '{offender}'")]
179    InvalidAcyclicSet { offender: String, reason: String },
180
181    #[error("relationships.labelling is invalid: {reason} — offending name: '{offender}'")]
182    InvalidLabelling { offender: String, reason: String },
183
184    #[error(
185        "type '{type_name}' section '{section}' format declaration is invalid: {}",
186        problems.join("; ")
187    )]
188    InvalidSectionFormat {
189        type_name: String,
190        section: String,
191        /// EVERY problem of the section's declaration — the loader
192        /// names all offenders, never the first only, so one repair
193        /// pass fixes the schema.
194        problems: Vec<String>,
195    },
196
197    #[error(
198        "type '{type_name}' metadata field '{field}' default '{default}' is not listed in enum_values: [{}]",
199        allowed.join(", ")
200    )]
201    DefaultValueNotInEnum {
202        type_name: String,
203        field: String,
204        default: String,
205        allowed: Vec<String>,
206    },
207
208    #[error(
209        "type '{type_name}' redeclares engine-implicit metadata key '{field}' — remove it from the YAML; the loader injects it automatically"
210    )]
211    RedeclaredBaseField { type_name: String, field: String },
212
213    #[error(
214        "relationship '{relationship}' field '{field}' references unknown type '{reference}'. Declared types: [{}]. {}",
215        declared.join(", "),
216        format_suggestion(reference, declared)
217    )]
218    UndeclaredRelationshipType {
219        relationship: String,
220        field: &'static str,
221        reference: String,
222        declared: Vec<String>,
223    },
224
225    /// Schema declares a regular section or metadata field whose key
226    /// collides with an engine-invariant key. The reserved set covers
227    /// section key `relationships` (the parser's auto-managed
228    /// `## Relationships` section) and the metadata identity/
229    /// discriminator triple `type` / `mem` / `id`
230    /// ([`reserved_metadata_field_keys`]). Sections refuse at load;
231    /// metadata keys refuse on the install/strict-validation path
232    /// ([`check_reserved_metadata_keys`]) so sealed schemas keep
233    /// booting.
234    #[error(
235        "type '{type_name}' declares {kind} with reserved key '{offending_key}' — reserved keys: [{}]",
236        reserved_keys.join(", ")
237    )]
238    ReservedSchemaKey {
239        type_name: String,
240        kind: &'static str,
241        offending_key: String,
242        reserved_keys: Vec<String>,
243    },
244
245    /// A `cross_mem_relationships:` entry's `to_schema:` field is
246    /// not a bare schema name. Cross-mem eligibility is name-based —
247    /// versioned (`software@1.0.0`) and range (`software@^1.0`) forms
248    /// are refused so a version component can never silently re-enter
249    /// the eligibility path.
250    #[error(
251        "cross_mem_relationships[].to_schema '{value}' {reason} — expected a bare schema name (e.g. 'software', not 'software@1.0.0')"
252    )]
253    InvalidCrossMemToSchema { value: String, reason: String },
254
255    /// Two `cross_mem_relationships:` entries declare the same
256    /// `to_schema:`. A schema declares each target-schema at most once
257    /// — the second entry would otherwise silently shadow or split
258    /// the vocabulary.
259    #[error("cross_mem_relationships declares duplicate to_schema '{to_schema}'")]
260    DuplicateCrossMemToSchema { to_schema: String },
261
262    /// A sealed package's file list carries no `schema.yaml`. Only
263    /// reachable through [`load_sealed_package`] — the directory
264    /// loader surfaces a missing manifest as [`Self::Io`] instead.
265    #[error("sealed schema package carries no schema.yaml")]
266    SealedPackageMissingManifest,
267
268    /// A `to_schema: "*"` wildcard entry in a schema that declares no
269    /// `alias_target_rel_type`. The wildcard is BOUND to the
270    /// alias-synthesised rel-type — a schema that has not opted into
271    /// alias synthesis has made no decision the wildcard could extend.
272    #[error(
273        "cross_mem_relationships declares to_schema '*' but the schema declares no \
274         alias_target_rel_type — the wildcard is bound to the alias-synthesised rel-type; \
275         declare alias_target_rel_type, or name each destination schema explicitly"
276    )]
277    CrossMemWildcardWithoutAliasTarget,
278
279    /// A `to_schema: "*"` wildcard entry declaring a rel-type other
280    /// than the schema's `alias_target_rel_type`. Hand-authored
281    /// structural edges keep requiring a per-destination-schema
282    /// declaration — the wildcard only extends the soft, auto-emitted
283    /// alias references the author already permitted.
284    #[error(
285        "cross_mem_relationships[to_schema='*'] declares rel-type '{rel_type}', but the \
286         wildcard is bound to the schema's alias_target_rel_type '{alias_target}' — \
287         hand-authored structural edges need a per-destination-schema declaration"
288    )]
289    CrossMemWildcardNonAliasRelType {
290        rel_type: String,
291        alias_target: String,
292    },
293
294    /// A `cross_mem_relationships[].definitions[*].source_types` entry
295    /// references a type name not declared in the source schema's
296    /// `types` list. Source types belong to the source schema's
297    /// namespace; unknown names raise this error at load time.
298    /// (Target types are accepted as opaque strings — they belong to
299    /// the target schema's namespace, which is not in scope here.)
300    #[error(
301        "cross_mem_relationships[to_schema='{to_schema}'].definitions[name='{relationship}'].source_types references unknown type '{reference}'. Declared types: [{}]. {}",
302        declared.join(", "),
303        format_suggestion(reference, declared)
304    )]
305    UndeclaredCrossMemSourceType {
306        to_schema: String,
307        relationship: String,
308        reference: String,
309        declared: Vec<String>,
310    },
311
312    /// The schema's `alias_target_rel_type:` pointer names a rel-type
313    /// not declared in `relationships.definitions`. Surfaces at
314    /// schema-load time so the alias-synthesis pass can trust the
315    /// pointer is resolvable at every later mutation call.
316    #[error(
317        "schema '{schema}' alias_target_rel_type '{target}' is not declared in relationships. Declared: [{}]. {}",
318        declared.join(", "),
319        format_suggestion(target, declared)
320    )]
321    AliasTargetRelTypeNotDeclared {
322        schema: String,
323        target: String,
324        declared: Vec<String>,
325    },
326
327    /// One or more declared section headings do not derive back to their
328    /// declared keys (`derive_section_key(heading) != key`), so content
329    /// written under the heading could never be parsed back into the
330    /// section — it would silently fork into a second heading or fall
331    /// through to the catch-all. Raised by
332    /// [`check_section_heading_roundtrip`] on the authoring/installation
333    /// path only; a schema already sealed into a mem-repo keeps loading
334    /// and surfaces the condition through health instead.
335    #[error(
336        "schema declares section heading(s) that cannot round-trip to their key(s): {}. \
337         Fix: make each heading derive to its key — lowercasing the heading and replacing \
338         spaces with underscores must yield the key exactly (key `current_state` ⇒ heading \
339         `Current State`)",
340        format_heading_violations(violations)
341    )]
342    SectionHeadingMismatch {
343        violations: Vec<HeadingKeyViolation>,
344    },
345
346    /// Two or more independent semantic violations found in one load
347    /// pass. The loader accumulates every violation it can prove on
348    /// successfully parsed structure and refuses once, so the author
349    /// fixes the whole set in one edit instead of one violation per
350    /// validate round. Structural failures (a manifest that does not
351    /// parse, a declared-vs-found type-file mismatch) still
352    /// short-circuit — everything downstream of them would be noise
353    /// derived from a value that does not exist. A single violation is
354    /// returned bare, never as a one-element list.
355    #[error(
356        "schema has {} violations:\n{}",
357        errors.len(),
358        format_multiple(errors)
359    )]
360    Multiple { errors: Vec<SchemaLoadError> },
361}
362
363fn format_multiple(errors: &[SchemaLoadError]) -> String {
364    errors
365        .iter()
366        .enumerate()
367        .map(|(i, e)| format!("  {}. {e}", i + 1))
368        .collect::<Vec<_>>()
369        .join("\n")
370}
371
372/// Fold an accumulated violation list into one error: a single
373/// violation stays bare (the common case keeps today's message shape),
374/// several wrap in [`SchemaLoadError::Multiple`]. Callers guarantee
375/// the list is non-empty.
376fn collapse(mut errors: Vec<SchemaLoadError>) -> SchemaLoadError {
377    debug_assert!(!errors.is_empty());
378    if errors.len() == 1 {
379        errors.remove(0)
380    } else {
381        SchemaLoadError::Multiple { errors }
382    }
383}
384
385/// One `(type, key, heading, derived_key)` tuple in a
386/// [`SchemaLoadError::SectionHeadingMismatch`] refusal.
387#[derive(Debug, Clone, PartialEq, Eq)]
388pub struct HeadingKeyViolation {
389    pub type_name: String,
390    pub key: String,
391    pub heading: String,
392    pub derived_key: String,
393}
394
395fn format_heading_violations(violations: &[HeadingKeyViolation]) -> String {
396    violations
397        .iter()
398        .map(|v| {
399            format!(
400                "type '{}' section key '{}' has heading '{}' (derives to '{}')",
401                v.type_name, v.key, v.heading, v.derived_key
402            )
403        })
404        .collect::<Vec<_>>()
405        .join("; ")
406}
407
408/// Refuse a schema in which any declared section's heading does not
409/// derive back to that section's declared key. Collects **every**
410/// offending `(type, key, heading, derived_key)` tuple — a schema with
411/// one good and one bad section is refused whole, and the author sees
412/// the complete list in one round.
413///
414/// Installation-path gate only: callers are the schema-authoring
415/// surfaces (CLI `schema validate` / `schema install`, the engine's
416/// `install_schema` primitive). Boot and sealed-schema loads must NOT
417/// call this — a schema already sealed on `__MEMSTEAD` that violates
418/// the rule keeps loading, and the violation surfaces as a health
419/// finding, never as a boot failure.
420pub fn check_section_heading_roundtrip(schema: &Schema) -> Result<(), SchemaLoadError> {
421    let mut violations = Vec::new();
422    // Deterministic report order: sort type names (Schema.types is a
423    // HashMap); sections keep declaration order within a type.
424    let mut type_names: Vec<&String> = schema.types.keys().collect();
425    type_names.sort();
426    for type_name in type_names {
427        let t = &schema.types[type_name];
428        for s in &t.sections {
429            let derived_key = crate::types::derive_section_key(&s.heading);
430            if derived_key != s.key {
431                violations.push(HeadingKeyViolation {
432                    type_name: type_name.clone(),
433                    key: s.key.clone(),
434                    heading: s.heading.clone(),
435                    derived_key,
436                });
437            }
438        }
439    }
440    if violations.is_empty() {
441        Ok(())
442    } else {
443        Err(SchemaLoadError::SectionHeadingMismatch { violations })
444    }
445}
446
447/// Engine-invariant section keys reserved against schema use. The
448/// parser's auto-managed `## Relationships` section is the only entry
449/// today.
450pub fn reserved_section_keys() -> &'static [&'static str] {
451    &["relationships"]
452}
453
454/// Engine-invariant metadata-field keys reserved against schema use —
455/// the entity's identity/discriminator triple. `type` is the engine-set
456/// frontmatter discriminator; `mem` and `id` are the entity's
457/// structural identity, owned by the engine's id grammar and mount
458/// routing. One reservation, one behaviour: no installable schema may
459/// declare any of them (see [`check_reserved_metadata_keys`]), and the
460/// engine write paths refuse them as caller-supplied metadata.
461pub fn reserved_metadata_field_keys() -> &'static [&'static str] {
462    &["type", "mem", "id"]
463}
464
465/// Author/install-path check: refuse a schema whose types **declare**
466/// an engine-reserved metadata-field key (`type` / `mem` / `id`).
467/// Reads the raw pre-merge declaration list the loader records
468/// (`TypeDefinition::declared_metadata_keys`) so the engine-injected
469/// base fields never false-positive.
470///
471/// Same posture as [`check_section_heading_roundtrip`]: install and
472/// strict validation call this and refuse; boot and sealed-schema
473/// loads must NOT — a schema already sealed that violates the rule
474/// keeps loading (refusing at boot would brick the workspace), and the
475/// engine's write-path refusals keep the reserved keys unwritable
476/// regardless.
477pub fn check_reserved_metadata_keys(schema: &crate::Schema) -> Result<(), SchemaLoadError> {
478    for td in schema.types.values() {
479        for key in &td.declared_metadata_keys {
480            if reserved_metadata_field_keys().contains(&key.as_str()) {
481                return Err(SchemaLoadError::ReservedSchemaKey {
482                    type_name: td.name.clone(),
483                    kind: "metadata_field",
484                    offending_key: key.clone(),
485                    reserved_keys: reserved_metadata_field_keys()
486                        .iter()
487                        .map(|s| s.to_string())
488                        .collect(),
489                });
490            }
491        }
492    }
493    Ok(())
494}
495
496fn format_suggestion(needle: &str, candidates: &[String]) -> String {
497    let mut best: Option<(usize, &String)> = None;
498    for cand in candidates {
499        let d = strsim::levenshtein(needle, cand);
500        match best {
501            Some((bd, _)) if bd <= d => {}
502            _ => best = Some((d, cand)),
503        }
504    }
505    match best {
506        Some((d, cand)) if d > 0 && d <= needle.len().saturating_add(3) => {
507            format!("Did you mean '{cand}'?")
508        }
509        _ => String::new(),
510    }
511}
512
513/// Load a schema from a directory containing `schema.yaml` and `types/*.yaml`.
514pub fn load_schema_from_dir(path: &Path) -> Result<Schema, SchemaLoadError> {
515    let manifest_path = path.join("schema.yaml");
516    let manifest_text =
517        std::fs::read_to_string(&manifest_path).map_err(|e| SchemaLoadError::Io {
518            path: manifest_path.clone(),
519            source: e,
520        })?;
521
522    let types_dir = path.join("types");
523    let mut type_files: Vec<(String, String)> = Vec::new();
524    if types_dir.is_dir() {
525        let entries = std::fs::read_dir(&types_dir).map_err(|e| SchemaLoadError::Io {
526            path: types_dir.clone(),
527            source: e,
528        })?;
529        for entry in entries {
530            let entry = entry.map_err(|e| SchemaLoadError::Io {
531                path: types_dir.clone(),
532                source: e,
533            })?;
534            let p = entry.path();
535            if p.extension().and_then(|s| s.to_str()) != Some("yaml") {
536                continue;
537            }
538            let Some(stem) = p.file_stem().and_then(|s| s.to_str()).map(str::to_owned) else {
539                continue;
540            };
541            let contents = std::fs::read_to_string(&p).map_err(|e| SchemaLoadError::Io {
542                path: p.clone(),
543                source: e,
544            })?;
545            type_files.push((stem, contents));
546        }
547    }
548    // read_dir order is filesystem-dependent; accumulated violations
549    // must report in a stable order across runs on the same input.
550    type_files.sort_by(|a, b| a.0.cmp(&b.0));
551
552    load_with_context(
553        &manifest_text,
554        &type_files,
555        Some(&manifest_path),
556        Some(&types_dir),
557        // Authoring context — the author writes the current language.
558        MetadataPolarityFormat::RequiredOptIn,
559    )
560}
561
562/// The metadata-field polarity generation of a sealed package —
563/// decided by the presence of the package's format marker
564/// (`schema-format.json`), never by heuristics over the document body.
565///
566/// Under the pre-flip language an absent `required`/`optional` key
567/// meant **required**; under the current language absence means
568/// **optional**. The two are syntactically indistinguishable, so an
569/// unmarked sealed package reads with [`Self::Legacy`] semantics —
570/// its effective behaviour conserved — while packages sealed from
571/// this change on carry the marker and read as
572/// [`Self::RequiredOptIn`]. Directory (authoring) loads are always
573/// `RequiredOptIn`: the author writes against the current language.
574#[derive(Debug, Clone, Copy, PartialEq, Eq)]
575pub enum MetadataPolarityFormat {
576    /// Pre-flip sealed content: an absent key means required.
577    Legacy,
578    /// Current language: an absent key means optional.
579    RequiredOptIn,
580}
581
582/// The format-marker file name sealed alongside a schema package on
583/// the genuinely sealed surfaces — the `__MEMSTEAD` ref, published
584/// `.mem` archives (the export carries it, the archive validator
585/// admits it, the archive loader honors it), and new builtin version
586/// directories. Presence ⇒ [`MetadataPolarityFormat::RequiredOptIn`];
587/// absence ⇒ legacy. Content is informative JSON; presence is the
588/// contract.
589///
590/// Directory loads (`load_schema_from_dir`: workspace
591/// `.memstead/schemas/`, cache extractions, `schema validate` /
592/// `install`) are the AUTHORING tier and never consult the marker —
593/// they always read the current language, with the retired
594/// `optional:` key refusing loudly. A pre-flip directory package
595/// relying on absent-key-means-required flips soft (fields become
596/// optional — admits more, refuses nothing); that is the fail-soft
597/// direction by design, with `health_required_fields` and
598/// constraints as the data-quality backstop.
599pub const SCHEMA_FORMAT_MARKER_FILE: &str = "schema-format.json";
600
601/// The marker file's canonical content.
602pub const SCHEMA_FORMAT_MARKER_CONTENT: &str = "{\"metadata_polarity\":\"required-opt-in\"}\n";
603
604/// Append the format marker to a package's file list if absent.
605///
606/// **For content verified current-language only.** The marker is a
607/// generation claim, and this helper cannot verify one — a legacy
608/// package stamped here would flip every bare field from required to
609/// optional the moment the sealed copy is read. The one place that
610/// KNOWS a package is current is the authoring/install resolver (a
611/// directory source just validated under the current language, where
612/// the retired `optional:` key refuses loudly); sealed and builtin
613/// sources carry their generation as-found and are never stamped —
614/// absence IS the legacy claim, and inventing a marker over it is the
615/// silent-flip defect this contract exists to prevent.
616pub fn with_format_marker(mut files: Vec<(String, Vec<u8>)>) -> Vec<(String, Vec<u8>)> {
617    if !files
618        .iter()
619        .any(|(rel, _)| rel == SCHEMA_FORMAT_MARKER_FILE)
620    {
621        files.push((
622            SCHEMA_FORMAT_MARKER_FILE.to_string(),
623            SCHEMA_FORMAT_MARKER_CONTENT.as_bytes().to_vec(),
624        ));
625    }
626    files
627}
628
629/// Load a schema from in-memory YAML strings — **legacy sealed
630/// semantics** (an absent required/optional key means required).
631/// Use [`load_schema_from_memory_with_format`] when the caller knows
632/// the package's format generation from its marker.
633///
634/// `types_yamls` is a slice of `(filename_stem, contents)` tuples — the stems
635/// must match `manifest.types` exactly.
636pub fn load_schema_from_memory(
637    manifest_yaml: &str,
638    types_yamls: &[(String, String)],
639) -> Result<Schema, SchemaLoadError> {
640    load_with_context(
641        manifest_yaml,
642        types_yamls,
643        None,
644        None,
645        MetadataPolarityFormat::Legacy,
646    )
647}
648
649/// Load a **sealed** schema package from its `(relative-path, bytes)`
650/// file list — the shape every sealed surface carries: a published
651/// `.mem` archive's `.memstead/schema/` tree, the git-branch
652/// `__MEMSTEAD:schemas/<name>@<version>/` tree, a built-in version
653/// directory. Paths are package-relative (`schema.yaml`,
654/// `types/<stem>.yaml`, `schema-format.json`); anything else is
655/// ignored.
656///
657/// One function so the surfaces that *admit* a sealed package (the
658/// archive validator) and the surfaces that later *read it back* (the
659/// local schema source the install stages into) can never disagree —
660/// a package valid to publish stays valid to install. The metadata
661/// polarity comes from the package's own format marker, and keys
662/// retired after the package was sealed read with their written
663/// meaning instead of refusing.
664///
665/// Authoring content takes [`load_schema_from_dir`] instead, which
666/// refuses retired keys loudly so the author can act.
667pub fn load_sealed_package(files: &[(String, Vec<u8>)]) -> Result<Schema, SchemaLoadError> {
668    let mut manifest: Option<String> = None;
669    let mut types: Vec<(String, String)> = Vec::new();
670    let mut marked = false;
671    for (rel, bytes) in files {
672        if rel == "schema.yaml" {
673            manifest = Some(String::from_utf8_lossy(bytes).into_owned());
674        } else if rel == SCHEMA_FORMAT_MARKER_FILE {
675            marked = true;
676        } else if let Some(stem) = rel
677            .strip_prefix("types/")
678            .and_then(|f| f.strip_suffix(".yaml"))
679        {
680            types.push((
681                stem.to_string(),
682                String::from_utf8_lossy(bytes).into_owned(),
683            ));
684        }
685    }
686    let manifest = manifest.ok_or(SchemaLoadError::SealedPackageMissingManifest)?;
687    // Deterministic order regardless of how the caller enumerated the
688    // package — accumulated violations must report the same way twice.
689    types.sort_by(|a, b| a.0.cmp(&b.0));
690    let format = if marked {
691        MetadataPolarityFormat::RequiredOptIn
692    } else {
693        MetadataPolarityFormat::Legacy
694    };
695    load_with_context(&manifest, &types, None, None, format)
696}
697
698/// Would this package content pass the AUTHORING tier under the
699/// current language? Same strictness as [`load_schema_from_dir`] —
700/// retired keys (`propagating_relationships`, `examples:`, `optional:`)
701/// refuse loudly — but over in-memory content, so a caller holding a
702/// sealed package's files (git-branch `__MEMSTEAD:schemas/` tree,
703/// folder seal, archive) can ask whether the content is still
704/// re-authorable/installable without materialising a directory. The
705/// tolerant sealed read ([`load_sealed_package`]) is unaffected; this
706/// is a pure diagnosis — the health rot axis is the consumer.
707pub fn check_package_reauthorable(
708    manifest_yaml: &str,
709    types_yamls: &[(String, String)],
710) -> Result<(), SchemaLoadError> {
711    load_authoring_package_from_memory(manifest_yaml, types_yamls).map(|_| ())
712}
713
714/// Load in-memory package content under the AUTHORING tier — the same
715/// strictness as [`load_schema_from_dir`] (retired keys refuse, the
716/// current metadata polarity), without a directory. The schema comes
717/// back for callers that need to compare what the strict read resolves
718/// (the migrate verb's faithfulness check); [`check_package_reauthorable`]
719/// is the yes/no form.
720pub fn load_authoring_package_from_memory(
721    manifest_yaml: &str,
722    types_yamls: &[(String, String)],
723) -> Result<Schema, SchemaLoadError> {
724    // The `types_dir: Some(..)` context is what selects the authoring-
725    // strict legacy-key gates; the path itself only labels error
726    // messages.
727    let strict_context = Path::new("<authoring package>");
728    load_with_context(
729        manifest_yaml,
730        types_yamls,
731        Some(strict_context),
732        Some(strict_context),
733        MetadataPolarityFormat::RequiredOptIn,
734    )
735}
736
737/// Load a schema from in-memory YAML strings with an explicit
738/// metadata-polarity format generation (from the sealed package's
739/// format marker).
740pub fn load_schema_from_memory_with_format(
741    manifest_yaml: &str,
742    types_yamls: &[(String, String)],
743    format: MetadataPolarityFormat,
744) -> Result<Schema, SchemaLoadError> {
745    load_with_context(manifest_yaml, types_yamls, None, None, format)
746}
747
748fn load_with_context(
749    manifest_yaml: &str,
750    types_yamls: &[(String, String)],
751    manifest_path: Option<&Path>,
752    types_dir: Option<&Path>,
753    format: MetadataPolarityFormat,
754) -> Result<Schema, SchemaLoadError> {
755    // Semantic-violation accumulator. Every check operating on
756    // successfully parsed structure pushes here instead of returning,
757    // so the author sees the complete violation set in one refusal;
758    // only structural failures short-circuit (see
759    // [`SchemaLoadError::Multiple`]). Order is deterministic:
760    // manifest checks in declaration order, then type files in
761    // `types_yamls` order (sorted by stem when loaded from a
762    // directory).
763    let mut errors: Vec<SchemaLoadError> = Vec::new();
764
765    let mut manifest: SchemaManifest =
766        serde_yaml_ng::from_str(manifest_yaml).map_err(|e| SchemaLoadError::ParseManifest {
767            path: manifest_path
768                .map(Path::to_path_buf)
769                .unwrap_or_else(|| PathBuf::from("<memory>")),
770            source: e,
771        })?;
772
773    if let Err(e) = validate_name(&manifest.name) {
774        errors.push(e);
775    }
776
777    // `None` only ever coexists with a non-empty accumulator, so the
778    // `Schema` construction at the bottom (reached only when the
779    // accumulator is empty) can unwrap.
780    let version = match semver::Version::parse(&manifest.version) {
781        Ok(v) => Some(v),
782        Err(_) => {
783            errors.push(SchemaLoadError::InvalidVersion {
784                value: manifest.version.clone(),
785            });
786            None
787        }
788    };
789
790    // Relationship vocabulary: unique names + _default present
791    let mut rel_names: HashSet<String> = HashSet::new();
792    for def in &manifest.relationships.definitions {
793        if !rel_names.insert(def.name.clone()) {
794            errors.push(SchemaLoadError::DuplicateRelationship {
795                name: def.name.clone(),
796            });
797        }
798    }
799    if !rel_names.contains("_default") {
800        errors.push(SchemaLoadError::MissingDefaultWeight);
801    }
802    let available_rels: Vec<String> = manifest
803        .relationships
804        .definitions
805        .iter()
806        .map(|d| d.name.clone())
807        .collect();
808
809    // Validate that the schema-level alias_target_rel_type pointer (if
810    // set) names a declared rel-type. The synthesis pass later relies
811    // on this invariant — running it as a load-time check keeps the
812    // mutation path's hot loop free of resolution failures.
813    if let Some(target) = &manifest.alias_target_rel_type
814        && !rel_names.contains(target)
815    {
816        let mut declared = available_rels.clone();
817        declared.sort();
818        errors.push(SchemaLoadError::AliasTargetRelTypeNotDeclared {
819            schema: manifest.name.clone(),
820            target: target.clone(),
821            declared,
822        });
823    }
824
825    // Acyclicity sets: each set names two or more DECLARED rel-types,
826    // and a rel-type appears in at most one set across all sets
827    // (overlapping sets have no coherent refusal message; a duplicate
828    // inside one set is the same defect). A single-member set is the
829    // per-definition `acyclic` flag's job and refuses here.
830    let mut acyclic_set_member_seen: HashSet<&str> = HashSet::new();
831    for set in &manifest.relationships.acyclic_sets {
832        if set.len() < 2 {
833            errors.push(SchemaLoadError::InvalidAcyclicSet {
834                offender: set.join(", "),
835                reason: "a set needs at least two rel-types (a single member is the \
836                         per-definition `acyclic` flag)"
837                    .to_string(),
838            });
839        }
840        for name in set {
841            if !rel_names.contains(name.as_str()) {
842                errors.push(SchemaLoadError::InvalidAcyclicSet {
843                    offender: name.clone(),
844                    reason: "names no declared relationship".to_string(),
845                });
846            }
847            if !acyclic_set_member_seen.insert(name.as_str()) {
848                errors.push(SchemaLoadError::InvalidAcyclicSet {
849                    offender: name.clone(),
850                    reason: "a rel-type may appear in at most one acyclicity set".to_string(),
851                });
852            }
853        }
854    }
855
856    // Labelling declaration: `attack` names at least one declared
857    // rel-type; a `support` block names declared rel-types too (its
858    // `terminal_types` need every type loaded and are checked in the
859    // schema-level pass; its `direction` is a closed enum).
860    if let Some(lab) = &manifest.relationships.labelling {
861        if lab.attack.is_empty() {
862            errors.push(SchemaLoadError::InvalidLabelling {
863                offender: "(empty)".to_string(),
864                reason: "`labelling.attack` must name at least one rel-type".to_string(),
865            });
866        }
867        for name in &lab.attack {
868            if !rel_names.contains(name.as_str()) {
869                errors.push(SchemaLoadError::InvalidLabelling {
870                    offender: name.clone(),
871                    reason: "`labelling.attack` entry names no declared relationship".to_string(),
872                });
873            }
874        }
875        if let Some(sup) = &lab.support {
876            if sup.relationships.is_empty() {
877                errors.push(SchemaLoadError::InvalidLabelling {
878                    offender: "(empty)".to_string(),
879                    reason: "`labelling.support.relationships` must name at least one rel-type"
880                        .to_string(),
881                });
882            }
883            for name in &sup.relationships {
884                if !rel_names.contains(name.as_str()) {
885                    errors.push(SchemaLoadError::InvalidLabelling {
886                        offender: name.clone(),
887                        reason: "`labelling.support.relationships` entry names no declared \
888                                 relationship"
889                            .to_string(),
890                    });
891                }
892            }
893        }
894    }
895
896    // Option C coupling — auto-force `manual_authoring: forbidden` on
897    // the rel-type named by `alias_target_rel_type`. Schemas setting
898    // the pointer opt the named rel-type out of explicit authoring;
899    // the only path to a relation of that rel-type is via the
900    // alias-synthesis pass that emits one per body wiki-link. This
901    // closes the explicit/synthesised coexistence question: with the
902    // coupling in place, edges of the pointer rel-type are always
903    // engine-emitted, so `EdgeSource::BodyLink` is unambiguous and
904    // GC can drop pointer-rel-type relations without risking
905    // explicit-author data.
906    //
907    // The coupling is silent — a schema that writes
908    // `manual_authoring: allow` (or `warn`) on the named rel-type
909    // gets overridden to `forbidden` at load. The override is the
910    // schema-strictness contract; explicit `allow`/`warn` on the
911    // pointer rel-type is meaningless under the design and would
912    // surprise the validator at runtime, so the loader corrects it
913    // here.
914    if let Some(pointer) = manifest.alias_target_rel_type.clone() {
915        for def in &mut manifest.relationships.definitions {
916            if def.name == pointer {
917                def.manual_authoring = crate::manifest::ManualAuthoring::Forbidden;
918            }
919        }
920    }
921
922    // Cross-check `source_types` / `target_types` on each relationship
923    // definition against the manifest's declared type list. Unknown
924    // names raise `UndeclaredRelationshipType` with a "did you mean"
925    // suggestion — the schema-author equivalent of `INVALID_REL_SHAPE`.
926    for def in &manifest.relationships.definitions {
927        for t in &def.source_types {
928            if !manifest.types.iter().any(|d| d == t) {
929                errors.push(SchemaLoadError::UndeclaredRelationshipType {
930                    relationship: def.name.clone(),
931                    field: "source_types",
932                    reference: t.clone(),
933                    declared: manifest.types.clone(),
934                });
935            }
936        }
937        for t in &def.target_types {
938            if !manifest.types.iter().any(|d| d == t) {
939                errors.push(SchemaLoadError::UndeclaredRelationshipType {
940                    relationship: def.name.clone(),
941                    field: "target_types",
942                    reference: t.clone(),
943                    declared: manifest.types.clone(),
944                });
945            }
946        }
947    }
948
949    // Cross-mem relationships: validate `to_schema` is a bare schema
950    // name (cross-mem eligibility is name-based — a version suffix or
951    // range refuses), refuse duplicate target schemas, and cross-check
952    // `source_types` against the source schema's types. `target_types`
953    // are accepted as opaque strings — they belong to the target
954    // schema's namespace, which is out of scope at source-schema load
955    // time. The target schema may not even be present in the workspace
956    // when the source schema loads (and cross-mem declarations
957    // targeting absent schemas are legitimate for portable library
958    // schemas).
959    let mut seen_to_schemas: HashSet<String> = HashSet::new();
960    for entry in &manifest.cross_mem_relationships {
961        if entry.to_schema == "*" {
962            // Wildcard destination — bound to the alias-synthesised
963            // rel-type. The binding IS the safety argument: the author
964            // already permitted soft auto-emitted references of that
965            // type; the wildcard extends that decision across the mem
966            // boundary and introduces no new permission. Structural
967            // rel-types stay per-destination-schema.
968            match manifest.alias_target_rel_type.as_deref() {
969                None => errors.push(SchemaLoadError::CrossMemWildcardWithoutAliasTarget),
970                Some(alias) => {
971                    for def in &entry.definitions {
972                        if def.name != alias {
973                            errors.push(SchemaLoadError::CrossMemWildcardNonAliasRelType {
974                                rel_type: def.name.clone(),
975                                alias_target: alias.to_string(),
976                            });
977                        }
978                    }
979                }
980            }
981        } else if entry.to_schema.contains('@') {
982            errors.push(SchemaLoadError::InvalidCrossMemToSchema {
983                value: entry.to_schema.clone(),
984                reason: "must not carry a version or range".into(),
985            });
986        } else if let Err(reason) = name_shape(&entry.to_schema) {
987            errors.push(SchemaLoadError::InvalidCrossMemToSchema {
988                value: entry.to_schema.clone(),
989                reason: reason.into(),
990            });
991        }
992        if !seen_to_schemas.insert(entry.to_schema.clone()) {
993            errors.push(SchemaLoadError::DuplicateCrossMemToSchema {
994                to_schema: entry.to_schema.clone(),
995            });
996        }
997        for def in &entry.definitions {
998            for t in &def.source_types {
999                if !manifest.types.iter().any(|d| d == t) {
1000                    errors.push(SchemaLoadError::UndeclaredCrossMemSourceType {
1001                        to_schema: entry.to_schema.clone(),
1002                        relationship: def.name.clone(),
1003                        reference: t.clone(),
1004                        declared: manifest.types.clone(),
1005                    });
1006                }
1007            }
1008        }
1009    }
1010
1011    // Type file cross-check: declared vs found, set equality (order-insensitive)
1012    let mut found_stems: Vec<String> = types_yamls.iter().map(|(s, _)| s.clone()).collect();
1013    found_stems.sort();
1014    let mut declared = manifest.types.clone();
1015    declared.sort();
1016    if found_stems != declared {
1017        // Structural: the per-type pass below would run against a file
1018        // set the manifest never described. Refuse now, together with
1019        // every manifest-level violation already proven.
1020        errors.push(SchemaLoadError::TypeFileMismatch {
1021            declared,
1022            found: found_stems,
1023        });
1024        return Err(collapse(errors));
1025    }
1026
1027    // Per-type defaults map for edge_weights resolution
1028    let defaults: IndexMap<String, f32> = manifest
1029        .relationships
1030        .definitions
1031        .iter()
1032        .map(|d| (d.name.clone(), d.default_weight))
1033        .collect();
1034
1035    let mut types_map: HashMap<String, Arc<TypeDefinition>> = HashMap::new();
1036    let mut had_type_parse_failure = false;
1037
1038    for (stem, text) in types_yamls {
1039        let type_path = types_dir
1040            .map(|d| d.join(format!("{stem}.yaml")))
1041            .unwrap_or_else(|| PathBuf::from(format!("<memory>/{stem}.yaml")));
1042
1043        let mut td: TypeDefinition = match serde_yaml_ng::from_str(text) {
1044            Ok(td) => td,
1045            Err(e) => {
1046                // A type file that does not parse cannot be checked
1047                // semantically — record the parse failure, keep
1048                // checking the other files, and skip the cross-type
1049                // pass below (the missing type would make it noise).
1050                errors.push(SchemaLoadError::ParseType {
1051                    path: type_path.clone(),
1052                    source: e,
1053                });
1054                had_type_parse_failure = true;
1055                continue;
1056            }
1057        };
1058
1059        if td.name != *stem {
1060            errors.push(SchemaLoadError::TypeNameMismatch {
1061                file: stem.clone(),
1062                declared: td.name.clone(),
1063            });
1064        }
1065
1066        // Legacy-key gate: authoring contexts (loaded from a
1067        // directory — `types_dir` is `Some`) refuse the retired
1068        // `propagating_relationships` key with the rename error;
1069        // sealed contexts (in-memory: built-ins, installed refs)
1070        // translate it so shipped content keeps loading (install-time
1071        // strict, sealed-tolerant — the section-heading round-trip
1072        // doctrine).
1073        if let Some(legacy) = td.legacy_propagating_relationships.take() {
1074            if types_dir.is_some() {
1075                errors.push(SchemaLoadError::PropagatingRelationshipsRenamed {
1076                    type_name: td.name.clone(),
1077                });
1078            } else if td.no_self_loop_relationships.is_empty() {
1079                td.no_self_loop_relationships = legacy;
1080            }
1081        }
1082
1083        // Retired `examples:` list (agent-trust plan 09): dead
1084        // vocabulary — never validated, never served. Authoring
1085        // contexts refuse with the pointer at `exemplar:`; sealed
1086        // contexts tolerate and drop (nothing consumed it, so
1087        // dropping is lossless).
1088        if td.legacy_examples.take().is_some() && types_dir.is_some() {
1089            errors.push(SchemaLoadError::ExamplesRetired {
1090                type_name: td.name.clone(),
1091            });
1092        }
1093
1094        // Exemplar relation spelling (consistency-sweep 05-front-door/08
1095        // rider): entries are authored in the mutation vocabulary
1096        // (`target:` / `rel_type:`) so the served exemplar round-trips
1097        // into `memstead_create` unchanged. Authoring contexts refuse
1098        // the retired `to:` / `type:` spelling with the rename pointer;
1099        // sealed contexts translate it so every shipped version keeps
1100        // loading. After the gate, both resolved keys are guaranteed
1101        // present on every loaded schema.
1102        if let Some(ex) = td.exemplar.as_mut() {
1103            let mut retired_spelling = false;
1104            let mut incomplete = false;
1105            for rel in &mut ex.relations {
1106                let legacy_to = rel.legacy_to.take();
1107                let legacy_type = rel.legacy_type.take();
1108                if legacy_to.is_some() || legacy_type.is_some() {
1109                    if types_dir.is_some() {
1110                        retired_spelling = true;
1111                        continue;
1112                    }
1113                    if rel.target.is_none() {
1114                        rel.target = legacy_to;
1115                    }
1116                    if rel.rel_type.is_none() {
1117                        rel.rel_type = legacy_type;
1118                    }
1119                }
1120                if rel.target.is_none() || rel.rel_type.is_none() {
1121                    incomplete = true;
1122                }
1123            }
1124            if retired_spelling {
1125                errors.push(SchemaLoadError::ExemplarRelationSpellingRetired {
1126                    type_name: td.name.clone(),
1127                });
1128            }
1129            if incomplete {
1130                errors.push(SchemaLoadError::ExemplarRelationIncomplete {
1131                    type_name: td.name.clone(),
1132                });
1133            }
1134        }
1135
1136        // Metadata-required polarity (first-author-path plan 07):
1137        // authoring refuses the retired `optional:` key naming the
1138        // inversion; sealed content inverts it. An absent key resolves
1139        // by the package's format generation — legacy sealed content
1140        // reads absence as required (its written meaning), everything
1141        // else as optional.
1142        for field in &mut td.metadata_fields {
1143            // Current-language contexts (directory/authoring loads and
1144            // install validation, both RequiredOptIn) refuse the
1145            // retired key; only Legacy sealed loads invert silently.
1146            if matches!(format, MetadataPolarityFormat::RequiredOptIn)
1147                && field.legacy_optional.is_some()
1148            {
1149                errors.push(SchemaLoadError::OptionalRetired {
1150                    type_name: td.name.clone(),
1151                    field: field.key.clone(),
1152                });
1153            }
1154            field.required_resolved = match (field.required, field.legacy_optional.take()) {
1155                (Some(required), _) => required,
1156                (None, Some(optional)) => !optional,
1157                (None, None) => matches!(format, MetadataPolarityFormat::Legacy),
1158            };
1159        }
1160
1161        // Record the raw author-declared metadata keys BEFORE the
1162        // base-metadata merge, so the install-path reserved-key check
1163        // ([`check_reserved_metadata_keys`]) can tell a declared
1164        // `type`/`mem`/`id` from the engine-injected base fields. The
1165        // check itself deliberately does NOT run here: this loader
1166        // serves boot and sealed-schema reads too, and a schema sealed
1167        // before the reservation widened must keep loading (heading-
1168        // round-trip posture — refusal fires on the authoring/install
1169        // path, never at boot).
1170        td.declared_metadata_keys = td.metadata_fields.iter().map(|f| f.key.clone()).collect();
1171
1172        // Reject redeclarations of remaining engine-implicit base metadata
1173        // (`created_date`, `last_modified`, `tags`). The reserved `type`
1174        // case is excluded here — it refuses with the typed reserved-key
1175        // error on the install path instead.
1176        for field in &td.metadata_fields {
1177            if base_metadata::is_base_key(&field.key)
1178                && !reserved_metadata_field_keys().contains(&field.key.as_str())
1179            {
1180                errors.push(SchemaLoadError::RedeclaredBaseField {
1181                    type_name: td.name.clone(),
1182                    field: field.key.clone(),
1183                });
1184            }
1185        }
1186
1187        // Merge base metadata around the type-declared fields. Canonical
1188        // order: type, created_date, last_modified, <declared>, tags.
1189        let mut merged = base_metadata::prefix_fields();
1190        merged.append(&mut td.metadata_fields);
1191        merged.extend(base_metadata::suffix_fields());
1192        td.metadata_fields = merged;
1193
1194        compile_section_formats(&mut td);
1195        validate_type(&td, &rel_names, &available_rels, &mut errors);
1196
1197        // Resolve edge_weights: start with schema defaults, apply overrides.
1198        let mut weights = defaults.clone();
1199        for (k, v) in &td.edge_weight_overrides {
1200            weights.insert(k.clone(), *v);
1201        }
1202        td.edge_weights = weights;
1203
1204        types_map.insert(stem.clone(), Arc::new(td));
1205    }
1206
1207    // Schema-level constraint pass — checks that need every type
1208    // loaded. `enum_from_neighbour.section` names a section on the
1209    // *reached* entity, whose type this schema cannot pin statically;
1210    // requiring the key to exist on at least one declared type catches
1211    // the typo class without over-constraining the endpoint. Skipped
1212    // when a type file failed to parse — the missing type's sections
1213    // would make the existence check report noise.
1214    if !had_type_parse_failure {
1215        let all_section_keys: HashSet<&str> = types_map
1216            .values()
1217            .flat_map(|t| t.sections.iter().map(|s| s.key.as_str()))
1218            .collect();
1219        // Deterministic report order: sort type names (types_map is a
1220        // HashMap); constraints keep declaration order within a type.
1221        let mut type_names: Vec<&String> = types_map.keys().collect();
1222        type_names.sort();
1223        for type_name in type_names {
1224            let td = &types_map[type_name];
1225            for c in &td.constraints {
1226                if let crate::types::ConstraintDef::EnumFromNeighbour { section, .. } = c
1227                    && !all_section_keys.contains(section.as_str())
1228                {
1229                    errors.push(SchemaLoadError::InvalidConstraint {
1230                        type_name: td.name.clone(),
1231                        kind: "enum_from_neighbour",
1232                        offender: section.clone(),
1233                        reason: "`section` names a section key no type of this schema declares"
1234                            .to_string(),
1235                    });
1236                }
1237            }
1238            // `must_reach.terminal_types` name types of this schema —
1239            // checkable only once every type is loaded.
1240            for ob in &td.must_reach {
1241                for t in &ob.terminal_types {
1242                    if !types_map.contains_key(t.as_str()) {
1243                        errors.push(SchemaLoadError::InvalidConstraint {
1244                            type_name: td.name.clone(),
1245                            kind: "must_reach",
1246                            offender: t.clone(),
1247                            reason: "`terminal_types` entry names no type of this schema"
1248                                .to_string(),
1249                        });
1250                    }
1251                }
1252            }
1253            // A signal's neighbour pair reads the COUNTERPART entity,
1254            // whose type this schema cannot pin statically: the field
1255            // must be declared with `enum_values` on at least one type
1256            // of the schema, and the value must be a member on at
1257            // least one such declaring type.
1258            for sig in &td.signals {
1259                if let (Some(field), Some(value)) = (&sig.neighbour_field, &sig.neighbour_value) {
1260                    let declaring: Vec<&crate::types::MetadataFieldDef> = types_map
1261                        .values()
1262                        .flat_map(|t| t.metadata_fields.iter())
1263                        .filter(|f| f.key == *field && f.enum_values.is_some())
1264                        .collect();
1265                    if declaring.is_empty() {
1266                        errors.push(SchemaLoadError::InvalidConstraint {
1267                            type_name: td.name.clone(),
1268                            kind: "signal",
1269                            offender: field.clone(),
1270                            reason: "`neighbour_field` is declared with `enum_values` on no \
1271                                     type of this schema"
1272                                .to_string(),
1273                        });
1274                    } else if !declaring.iter().any(|f| {
1275                        f.enum_values
1276                            .as_ref()
1277                            .is_some_and(|allowed| allowed.contains(value))
1278                    }) {
1279                        errors.push(SchemaLoadError::InvalidConstraint {
1280                            type_name: td.name.clone(),
1281                            kind: "signal",
1282                            offender: value.clone(),
1283                            reason: format!(
1284                                "`neighbour_value` is outside `{field}`'s enum_values on every \
1285                                 declaring type"
1286                            ),
1287                        });
1288                    }
1289                }
1290            }
1291        }
1292    }
1293
1294    // `labelling.support.terminal_types` name types of this schema —
1295    // checkable only once every type is loaded (mirrors the
1296    // `must_reach` pass; skipped on type-parse failure like the rest
1297    // of the schema-level pass).
1298    if !had_type_parse_failure
1299        && let Some(lab) = &manifest.relationships.labelling
1300        && let Some(sup) = &lab.support
1301    {
1302        for t in &sup.terminal_types {
1303            if !types_map.contains_key(t.as_str()) {
1304                errors.push(SchemaLoadError::InvalidLabelling {
1305                    offender: t.clone(),
1306                    reason: "`labelling.support.terminal_types` entry names no type of this \
1307                             schema"
1308                        .to_string(),
1309                });
1310            }
1311        }
1312    }
1313
1314    if !errors.is_empty() {
1315        return Err(collapse(errors));
1316    }
1317
1318    Ok(Schema {
1319        manifest,
1320        version: version.expect("version parse failure would have accumulated an error"),
1321        types: types_map,
1322    })
1323}
1324
1325fn validate_name(name: &str) -> Result<(), SchemaLoadError> {
1326    name_shape(name).map_err(|reason| SchemaLoadError::InvalidName {
1327        value: name.into(),
1328        reason,
1329    })
1330}
1331
1332/// Author-time access to the schema-name shape rule — the same check
1333/// the loader runs on a manifest's `name:`. Exposed so scaffolding
1334/// tooling (`memstead schema new`) can refuse a bad name up front with
1335/// the loader's own reason string instead of a drifting copy of the
1336/// grammar.
1337pub fn validate_schema_name(name: &str) -> Result<(), &'static str> {
1338    name_shape(name)
1339}
1340
1341/// Shared shape rule for schema names — the manifest's own `name:` and
1342/// every `cross_mem_relationships[].to_schema` follow the same
1343/// grammar; the two callers wrap violations in their field-specific
1344/// error variants.
1345fn name_shape(name: &str) -> Result<(), &'static str> {
1346    if name.is_empty() {
1347        return Err("must not be empty");
1348    }
1349    let mut chars = name.chars();
1350    let first = chars.next().unwrap();
1351    if !first.is_ascii_lowercase() {
1352        return Err("must start with a lowercase letter");
1353    }
1354    for c in chars {
1355        if !(c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-') {
1356            return Err("must contain only lowercase letters, digits, and hyphens");
1357        }
1358    }
1359    Ok(())
1360}
1361
1362/// Validate and compile the section-format declarations of a type
1363/// (plan 08). LENIENT at load: problems are recorded on the section
1364/// (`format_problems`) instead of refusing, so a sealed schema
1365/// carrying a bad declaration keeps loading — install and strict
1366/// validation refuse via [`check_section_formats`], and a defective
1367/// declaration is never enforced (`compiled_content` stays `None`). A
1368/// valid `content` expression is compiled once and cached.
1369fn compile_section_formats(td: &mut TypeDefinition) {
1370    use crate::content_expr::ContentExpr;
1371    for section in &mut td.sections {
1372        // A non-default severity only deserializes from an explicit
1373        // declaration, so a lone `format_severity: warn` without
1374        // `content` is detectable — and would otherwise load and be
1375        // silently ignored (`format_severity: block` alone equals the
1376        // default and is inherently a no-op).
1377        let declares_any = section.content.is_some()
1378            || section.item_pattern.is_some()
1379            || section.table.is_some()
1380            || section.example.is_some()
1381            || section.format_severity != crate::types::ConstraintSeverity::Block;
1382        if !declares_any {
1383            continue;
1384        }
1385        let mut problems: Vec<String> = Vec::new();
1386
1387        let compiled = match &section.content {
1388            None => {
1389                problems.push(
1390                    "`item_pattern` / `table` / `example` require a `content` declaration"
1391                        .to_string(),
1392                );
1393                None
1394            }
1395            Some(expr_src) => match ContentExpr::parse(expr_src) {
1396                Ok(expr) => Some(expr),
1397                Err(e) => {
1398                    problems.push(format!("`content` is invalid: {e}"));
1399                    None
1400                }
1401            },
1402        };
1403
1404        if let Some(pattern) = &section.item_pattern {
1405            if let Err(e) = regex::Regex::new(pattern) {
1406                problems.push(format!("`item_pattern` is not a valid regex: {e}"));
1407            }
1408            if let Some(expr) = &compiled {
1409                let names = expr.mentioned_names();
1410                let has_list = names.contains(&"list");
1411                let has_paragraph = names.contains(&"paragraph");
1412                if has_list == has_paragraph {
1413                    problems.push(
1414                        "`item_pattern` requires a `content` expression containing exactly                          one of `list` / `paragraph` (tables use `column_patterns`)"
1415                            .to_string(),
1416                    );
1417                }
1418            }
1419        }
1420
1421        if let Some(table) = &section.table {
1422            if let Some(expr) = &compiled
1423                && !expr.mentioned_names().contains(&"table")
1424            {
1425                problems.push(
1426                    "`table` block is only legal when `content` contains `table`".to_string(),
1427                );
1428            }
1429            if table.columns.is_empty() {
1430                problems.push("`table.columns` must name at least one column".to_string());
1431            }
1432            for (column, pattern) in &table.column_patterns {
1433                if !table.columns.contains(column) {
1434                    problems.push(format!(
1435                        "`column_patterns` names '{column}', which is not in `columns`"
1436                    ));
1437                }
1438                if let Err(e) = regex::Regex::new(pattern) {
1439                    problems.push(format!(
1440                        "`column_patterns.{column}` is not a valid regex: {e}"
1441                    ));
1442                }
1443            }
1444        }
1445
1446        if problems.is_empty() {
1447            section.compiled_content = compiled;
1448        } else {
1449            section.format_problems = problems;
1450        }
1451    }
1452}
1453
1454/// Refuse a schema whose section-format declarations are defective —
1455/// the install / strict-validation half of the loader-honesty rule.
1456/// Same posture as [`check_reserved_metadata_keys`]: install and
1457/// strict validation call this and refuse (naming EVERY problem of
1458/// the first defective section); boot and sealed-schema loads must
1459/// NOT — the recorded `format_problems` surface as health findings
1460/// instead, and the defective declaration is never enforced.
1461pub fn check_section_formats(schema: &crate::Schema) -> Result<(), SchemaLoadError> {
1462    // Aggregate EVERY defective section across every type — the
1463    // refusal names all offenders, never the first only. `type_name`
1464    // / `section` carry the first offender; entries beyond it are
1465    // prefixed with their own type/section inside `problems`.
1466    let mut first: Option<(String, String)> = None;
1467    let mut problems: Vec<String> = Vec::new();
1468    for td in schema.types.values() {
1469        for section in &td.sections {
1470            if section.format_problems.is_empty() {
1471                continue;
1472            }
1473            if first.is_none() {
1474                first = Some((td.name.clone(), section.key.clone()));
1475                problems.extend(section.format_problems.iter().cloned());
1476            } else {
1477                problems.extend(
1478                    section
1479                        .format_problems
1480                        .iter()
1481                        .map(|p| format!("[{}.{}] {p}", td.name, section.key)),
1482                );
1483            }
1484        }
1485    }
1486    match first {
1487        Some((type_name, section)) => Err(SchemaLoadError::InvalidSectionFormat {
1488            type_name,
1489            section,
1490            problems,
1491        }),
1492        None => Ok(()),
1493    }
1494}
1495
1496fn validate_type(
1497    td: &TypeDefinition,
1498    rel_names: &HashSet<String>,
1499    available_rels: &[String],
1500    errors: &mut Vec<SchemaLoadError>,
1501) {
1502    // Reserved-section check. Section key `relationships` collides
1503    // with the parser's auto-managed `## Relationships` section.
1504    // Domain conventions (`identity`, `purpose`, ...) are NOT reserved.
1505    // The reserved-metadata-key check runs earlier in
1506    // `load_with_context` against the *raw* author-declared field list
1507    // so the base-metadata merge doesn't false-positive against
1508    // engine-injected keys.
1509    for section in &td.sections {
1510        if reserved_section_keys().contains(&section.key.as_str()) {
1511            errors.push(SchemaLoadError::ReservedSchemaKey {
1512                type_name: td.name.clone(),
1513                kind: "section",
1514                offending_key: section.key.clone(),
1515                reserved_keys: reserved_section_keys()
1516                    .iter()
1517                    .map(|s| s.to_string())
1518                    .collect(),
1519            });
1520        }
1521    }
1522
1523    if let Err(e) = check_rel(
1524        &td.name,
1525        "hierarchy_relationship",
1526        &td.hierarchy_relationship,
1527        rel_names,
1528        available_rels,
1529    ) {
1530        errors.push(e);
1531    }
1532    for r in &td.no_self_loop_relationships {
1533        if let Err(e) = check_rel(
1534            &td.name,
1535            "no_self_loop_relationships",
1536            r,
1537            rel_names,
1538            available_rels,
1539        ) {
1540            errors.push(e);
1541        }
1542    }
1543    for r in td.edge_weight_overrides.keys() {
1544        if let Err(e) = check_rel(
1545            &td.name,
1546            "edge_weight_overrides",
1547            r,
1548            rel_names,
1549            available_rels,
1550        ) {
1551            errors.push(e);
1552        }
1553    }
1554    for block in &td.required_outgoing {
1555        for r in &block.relationships {
1556            if let Err(e) = check_rel(&td.name, "required_outgoing", r, rel_names, available_rels) {
1557                errors.push(e);
1558            }
1559        }
1560        // Conditional blocks: `when_field` / `when_value` travel as a
1561        // pair, the field must be a declared metadata field carrying
1562        // `enum_values`, and the value must be a member. Stricter than
1563        // `requires_when` (which tolerates non-enum trigger fields):
1564        // an edge obligation armed by a free-text value would never
1565        // fire predictably, so the loader refuses instead of loading a
1566        // dead condition.
1567        match (&block.when_field, &block.when_value) {
1568            (None, None) => {}
1569            (Some(f), None) => {
1570                errors.push(SchemaLoadError::InvalidConstraint {
1571                    type_name: td.name.clone(),
1572                    kind: "required_outgoing",
1573                    offender: f.clone(),
1574                    reason: "`when_field` requires `when_value` alongside it".to_string(),
1575                });
1576            }
1577            (None, Some(v)) => {
1578                errors.push(SchemaLoadError::InvalidConstraint {
1579                    type_name: td.name.clone(),
1580                    kind: "required_outgoing",
1581                    offender: v.clone(),
1582                    reason: "`when_value` requires `when_field` alongside it".to_string(),
1583                });
1584            }
1585            (Some(f), Some(v)) => match td.metadata_fields.iter().find(|mf| mf.key == *f) {
1586                None => {
1587                    errors.push(SchemaLoadError::InvalidConstraint {
1588                        type_name: td.name.clone(),
1589                        kind: "required_outgoing",
1590                        offender: f.clone(),
1591                        reason: "`when_field` names no metadata field of this type".to_string(),
1592                    });
1593                }
1594                Some(when_def) => match &when_def.enum_values {
1595                    None => {
1596                        errors.push(SchemaLoadError::InvalidConstraint {
1597                            type_name: td.name.clone(),
1598                            kind: "required_outgoing",
1599                            offender: f.clone(),
1600                            reason: format!(
1601                                "`when_field` must name a metadata field with `enum_values`; `{f}` declares none"
1602                            ),
1603                        });
1604                    }
1605                    Some(allowed) if !allowed.contains(v) => {
1606                        errors.push(SchemaLoadError::InvalidConstraint {
1607                            type_name: td.name.clone(),
1608                            kind: "required_outgoing",
1609                            offender: v.clone(),
1610                            reason: format!(
1611                                "`when_value` is not in `{f}`'s enum_values [{}]",
1612                                allowed.join(", ")
1613                            ),
1614                        });
1615                    }
1616                    Some(_) => {}
1617                },
1618            },
1619        }
1620    }
1621
1622    // Reachability obligations. Per-type checks here; `terminal_types`
1623    // needs every type loaded and is checked in the schema-level pass.
1624    for ob in &td.must_reach {
1625        if ob.relationships.is_empty() {
1626            errors.push(SchemaLoadError::InvalidConstraint {
1627                type_name: td.name.clone(),
1628                kind: "must_reach",
1629                offender: "(empty)".to_string(),
1630                reason: "`relationships` must name at least one relationship".to_string(),
1631            });
1632        }
1633        for r in &ob.relationships {
1634            if let Err(e) = check_rel(&td.name, "must_reach", r, rel_names, available_rels) {
1635                errors.push(e);
1636            }
1637        }
1638        if ob.terminal_types.is_empty() {
1639            errors.push(SchemaLoadError::InvalidConstraint {
1640                type_name: td.name.clone(),
1641                kind: "must_reach",
1642                offender: "(empty)".to_string(),
1643                reason: "`terminal_types` must name at least one type".to_string(),
1644            });
1645        }
1646        if ob.max_depth == Some(0) {
1647            errors.push(SchemaLoadError::InvalidConstraint {
1648                type_name: td.name.clone(),
1649                kind: "must_reach",
1650                offender: "0".to_string(),
1651                reason: "`max_depth` must be at least 1 — nothing is reachable in zero hops"
1652                    .to_string(),
1653            });
1654        }
1655        if ob.severity == crate::types::ConstraintSeverity::Block {
1656            // A transitive property is established by writes on OTHER
1657            // entities, so a write-time refusal would punish the wrong
1658            // mutation — refuse the promise rather than load-and-
1659            // downgrade (same posture as status_propagation).
1660            errors.push(SchemaLoadError::InvalidConstraint {
1661                type_name: td.name.clone(),
1662                kind: "must_reach",
1663                offender: "block".to_string(),
1664                reason: "must_reach is always warn-tier — a reachability gap is created by \
1665                         writes on other entities, so no single write can be refused for it"
1666                    .to_string(),
1667            });
1668        }
1669    }
1670
1671    // Aggregate signals. Per-type checks here; the neighbour pair's
1672    // cross-type validation needs every type loaded and runs in the
1673    // schema-level pass.
1674    let mut signal_names_seen: HashSet<&str> = HashSet::new();
1675    for sig in &td.signals {
1676        let name_ok = sig
1677            .name
1678            .chars()
1679            .next()
1680            .is_some_and(|c| c.is_ascii_lowercase())
1681            && sig
1682                .name
1683                .chars()
1684                .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_');
1685        if !name_ok {
1686            errors.push(SchemaLoadError::InvalidConstraint {
1687                type_name: td.name.clone(),
1688                kind: "signal",
1689                offender: sig.name.clone(),
1690                reason: "`name` must match [a-z][a-z0-9_]*".to_string(),
1691            });
1692        }
1693        if !signal_names_seen.insert(sig.name.as_str()) {
1694            errors.push(SchemaLoadError::InvalidConstraint {
1695                type_name: td.name.clone(),
1696                kind: "signal",
1697                offender: sig.name.clone(),
1698                reason: "duplicate signal name on this type".to_string(),
1699            });
1700        }
1701        if sig.relationships.is_empty() {
1702            errors.push(SchemaLoadError::InvalidConstraint {
1703                type_name: td.name.clone(),
1704                kind: "signal",
1705                offender: sig.name.clone(),
1706                reason: "`relationships` must name at least one relationship".to_string(),
1707            });
1708        }
1709        for r in &sig.relationships {
1710            if let Err(e) = check_rel(&td.name, "signals", r, rel_names, available_rels) {
1711                errors.push(e);
1712            }
1713        }
1714        if sig.thresholds.is_empty() {
1715            errors.push(SchemaLoadError::InvalidConstraint {
1716                type_name: td.name.clone(),
1717                kind: "signal",
1718                offender: sig.name.clone(),
1719                reason: "`thresholds` must declare at least one step".to_string(),
1720            });
1721        }
1722        for pair in sig.thresholds.windows(2) {
1723            if pair[1].at_least <= pair[0].at_least {
1724                errors.push(SchemaLoadError::InvalidConstraint {
1725                    type_name: td.name.clone(),
1726                    kind: "signal",
1727                    offender: pair[1].at_least.to_string(),
1728                    reason: "`thresholds` must have strictly increasing `at_least` values"
1729                        .to_string(),
1730                });
1731            }
1732        }
1733        match (&sig.neighbour_field, &sig.neighbour_value) {
1734            (None, None) | (Some(_), Some(_)) => {}
1735            (Some(f), None) => {
1736                errors.push(SchemaLoadError::InvalidConstraint {
1737                    type_name: td.name.clone(),
1738                    kind: "signal",
1739                    offender: f.clone(),
1740                    reason: "`neighbour_field` requires `neighbour_value` alongside it".to_string(),
1741                });
1742            }
1743            (None, Some(v)) => {
1744                errors.push(SchemaLoadError::InvalidConstraint {
1745                    type_name: td.name.clone(),
1746                    kind: "signal",
1747                    offender: v.clone(),
1748                    reason: "`neighbour_value` requires `neighbour_field` alongside it".to_string(),
1749                });
1750            }
1751        }
1752    }
1753
1754    // Constraint vocabulary (loader honesty: a malformed declaration
1755    // refuses with a typed error naming the offender — never
1756    // load-and-ignore).
1757    let field_keys: std::collections::HashSet<&str> =
1758        td.metadata_fields.iter().map(|f| f.key.as_str()).collect();
1759    let section_keys: std::collections::HashSet<&str> =
1760        td.sections.iter().map(|sec| sec.key.as_str()).collect();
1761    for c in &td.constraints {
1762        match c {
1763            crate::types::ConstraintDef::RequiresWhen {
1764                field,
1765                when_field,
1766                when_value,
1767                ..
1768            } => {
1769                if !field_keys.contains(field.as_str()) && !section_keys.contains(field.as_str()) {
1770                    errors.push(SchemaLoadError::InvalidConstraint {
1771                        type_name: td.name.clone(),
1772                        kind: "requires_when",
1773                        offender: field.clone(),
1774                        reason: "`field` names neither a metadata field nor a section of this type"
1775                            .to_string(),
1776                    });
1777                }
1778                let Some(when_def) = td.metadata_fields.iter().find(|f| f.key == *when_field)
1779                else {
1780                    errors.push(SchemaLoadError::InvalidConstraint {
1781                        type_name: td.name.clone(),
1782                        kind: "requires_when",
1783                        offender: when_field.clone(),
1784                        reason: "`when_field` names no metadata field of this type".to_string(),
1785                    });
1786                    continue;
1787                };
1788                if let Some(allowed) = &when_def.enum_values
1789                    && !allowed.contains(when_value)
1790                {
1791                    errors.push(SchemaLoadError::InvalidConstraint {
1792                        type_name: td.name.clone(),
1793                        kind: "requires_when",
1794                        offender: when_value.clone(),
1795                        reason: format!(
1796                            "`when_value` is not in `{when_field}`'s enum_values [{}]",
1797                            allowed.join(", ")
1798                        ),
1799                    });
1800                }
1801            }
1802            crate::types::ConstraintDef::Unique { fields, .. } => {
1803                if fields.is_empty() {
1804                    errors.push(SchemaLoadError::InvalidConstraint {
1805                        type_name: td.name.clone(),
1806                        kind: "unique",
1807                        offender: "(empty)".to_string(),
1808                        reason: "`fields` must name at least one metadata field".to_string(),
1809                    });
1810                }
1811                for f in fields {
1812                    if !field_keys.contains(f.as_str()) {
1813                        errors.push(SchemaLoadError::InvalidConstraint {
1814                            type_name: td.name.clone(),
1815                            kind: "unique",
1816                            offender: f.clone(),
1817                            reason: "`fields` entry names no metadata field of this type"
1818                                .to_string(),
1819                        });
1820                    }
1821                }
1822            }
1823            crate::types::ConstraintDef::EnumFromNeighbour {
1824                field, rel_type, ..
1825            } => {
1826                if !field_keys.contains(field.as_str()) {
1827                    errors.push(SchemaLoadError::InvalidConstraint {
1828                        type_name: td.name.clone(),
1829                        kind: "enum_from_neighbour",
1830                        offender: field.clone(),
1831                        reason: "`field` names no metadata field of this type".to_string(),
1832                    });
1833                }
1834                if !rel_names.contains(rel_type) {
1835                    errors.push(SchemaLoadError::InvalidConstraint {
1836                        type_name: td.name.clone(),
1837                        kind: "enum_from_neighbour",
1838                        offender: rel_type.clone(),
1839                        reason: "`rel_type` is not in the schema's relationship vocabulary"
1840                            .to_string(),
1841                    });
1842                }
1843                // `section` names a key on the *reached* type, which
1844                // this per-type pass cannot see — the schema-level
1845                // pass after all types load checks it.
1846            }
1847            crate::types::ConstraintDef::TransitionRequiresChecks {
1848                field,
1849                to_value,
1850                relationships,
1851                ..
1852            } => {
1853                match td.metadata_fields.iter().find(|f| f.key == *field) {
1854                    None => {
1855                        errors.push(SchemaLoadError::InvalidConstraint {
1856                            type_name: td.name.clone(),
1857                            kind: "transition_requires_checks",
1858                            offender: field.clone(),
1859                            reason: "`field` names no metadata field of this type".to_string(),
1860                        });
1861                    }
1862                    Some(field_def) => {
1863                        if let Some(allowed) = &field_def.enum_values
1864                            && !allowed.contains(to_value)
1865                        {
1866                            errors.push(SchemaLoadError::InvalidConstraint {
1867                                type_name: td.name.clone(),
1868                                kind: "transition_requires_checks",
1869                                offender: to_value.clone(),
1870                                reason: format!(
1871                                    "`to_value` is not in `{field}`'s enum_values [{}]",
1872                                    allowed.join(", ")
1873                                ),
1874                            });
1875                        }
1876                    }
1877                }
1878                if relationships.is_empty() {
1879                    errors.push(SchemaLoadError::InvalidConstraint {
1880                        type_name: td.name.clone(),
1881                        kind: "transition_requires_checks",
1882                        offender: "(empty)".to_string(),
1883                        reason: "`relationships` must name at least one declared relationship"
1884                            .to_string(),
1885                    });
1886                }
1887                for rel in relationships {
1888                    if !rel_names.contains(rel) {
1889                        errors.push(SchemaLoadError::InvalidConstraint {
1890                            type_name: td.name.clone(),
1891                            kind: "transition_requires_checks",
1892                            offender: rel.clone(),
1893                            reason: "`relationships` entry is not in the schema's relationship \
1894                                     vocabulary"
1895                                .to_string(),
1896                        });
1897                    }
1898                }
1899            }
1900            crate::types::ConstraintDef::StatusPropagation {
1901                field,
1902                value,
1903                rel_type,
1904                rel_types,
1905                severity,
1906                ..
1907            } => {
1908                match td.metadata_fields.iter().find(|f| f.key == *field) {
1909                    None => {
1910                        errors.push(SchemaLoadError::InvalidConstraint {
1911                            type_name: td.name.clone(),
1912                            kind: "status_propagation",
1913                            offender: field.clone(),
1914                            reason: "`field` names no metadata field of this type".to_string(),
1915                        });
1916                    }
1917                    Some(field_def) => {
1918                        if let Some(allowed) = &field_def.enum_values
1919                            && !allowed.contains(value)
1920                        {
1921                            errors.push(SchemaLoadError::InvalidConstraint {
1922                                type_name: td.name.clone(),
1923                                kind: "status_propagation",
1924                                offender: value.clone(),
1925                                reason: format!(
1926                                    "`value` is not in `{field}`'s enum_values [{}]",
1927                                    allowed.join(", ")
1928                                ),
1929                            });
1930                        }
1931                    }
1932                }
1933                // Exactly one of `rel_type` / `rel_types`; every named
1934                // member must be declared; an empty set is a defect.
1935                match (rel_type, rel_types) {
1936                    (Some(_), Some(_)) => {
1937                        errors.push(SchemaLoadError::InvalidConstraint {
1938                            type_name: td.name.clone(),
1939                            kind: "status_propagation",
1940                            offender: "rel_type".to_string(),
1941                            reason: "declare `rel_type` or `rel_types`, not both".to_string(),
1942                        });
1943                    }
1944                    (None, None) => {
1945                        errors.push(SchemaLoadError::InvalidConstraint {
1946                            type_name: td.name.clone(),
1947                            kind: "status_propagation",
1948                            offender: "(missing)".to_string(),
1949                            reason: "one of `rel_type` / `rel_types` is required".to_string(),
1950                        });
1951                    }
1952                    (Some(single), None) => {
1953                        if !rel_names.contains(single) {
1954                            errors.push(SchemaLoadError::InvalidConstraint {
1955                                type_name: td.name.clone(),
1956                                kind: "status_propagation",
1957                                offender: single.clone(),
1958                                reason: "`rel_type` is not in the schema's relationship vocabulary"
1959                                    .to_string(),
1960                            });
1961                        }
1962                    }
1963                    (None, Some(set)) => {
1964                        if set.is_empty() {
1965                            errors.push(SchemaLoadError::InvalidConstraint {
1966                                type_name: td.name.clone(),
1967                                kind: "status_propagation",
1968                                offender: "(empty)".to_string(),
1969                                reason: "`rel_types` must name at least one relationship"
1970                                    .to_string(),
1971                            });
1972                        }
1973                        for name in set {
1974                            if !rel_names.contains(name) {
1975                                errors.push(SchemaLoadError::InvalidConstraint {
1976                                    type_name: td.name.clone(),
1977                                    kind: "status_propagation",
1978                                    offender: name.clone(),
1979                                    reason: "`rel_types` entry is not in the schema's \
1980                                             relationship vocabulary"
1981                                        .to_string(),
1982                                });
1983                            }
1984                        }
1985                    }
1986                }
1987                if *severity == crate::types::ConstraintSeverity::Block {
1988                    // Propagation can never refuse a write (the taint
1989                    // arises from the ancestor's later change), so a
1990                    // `block` declaration would be a promise the
1991                    // engine will not keep — refuse it rather than
1992                    // load-and-downgrade.
1993                    errors.push(SchemaLoadError::InvalidConstraint {
1994                        type_name: td.name.clone(),
1995                        kind: "status_propagation",
1996                        offender: "block".to_string(),
1997                        reason: "status_propagation is always warn-tier — a parent falling after \
1998                                 the child was written cannot retroactively make the child's \
1999                                 write illegal"
2000                            .to_string(),
2001                    });
2002                }
2003            }
2004        }
2005    }
2006
2007    // Due axis (first-author-path plan 08): the declaration's
2008    // references must exist on this type in the declared shapes.
2009    if let Some(due) = &td.due {
2010        match td.metadata_fields.iter().find(|f| f.key == due.date_field) {
2011            None => errors.push(SchemaLoadError::InvalidDueAxis {
2012                type_name: td.name.clone(),
2013                offender: due.date_field.clone(),
2014                reason: "`date_field` names no metadata field of this type".to_string(),
2015            }),
2016            Some(f) if f.field_type != crate::types::FieldType::Date => {
2017                errors.push(SchemaLoadError::InvalidDueAxis {
2018                    type_name: td.name.clone(),
2019                    offender: due.date_field.clone(),
2020                    reason: "`date_field` must name a date-typed metadata field".to_string(),
2021                })
2022            }
2023            Some(_) => {}
2024        }
2025        match td
2026            .metadata_fields
2027            .iter()
2028            .find(|f| f.key == due.status_field)
2029        {
2030            None => errors.push(SchemaLoadError::InvalidDueAxis {
2031                type_name: td.name.clone(),
2032                offender: due.status_field.clone(),
2033                reason: "`status_field` names no metadata field of this type".to_string(),
2034            }),
2035            Some(f) => match &f.enum_values {
2036                None => errors.push(SchemaLoadError::InvalidDueAxis {
2037                    type_name: td.name.clone(),
2038                    offender: due.status_field.clone(),
2039                    reason: "`status_field` must name an enum-typed metadata field \
2040                             (declare enum_values)"
2041                        .to_string(),
2042                }),
2043                Some(allowed) => {
2044                    for v in &due.open_values {
2045                        if !allowed.contains(v) {
2046                            errors.push(SchemaLoadError::InvalidDueAxis {
2047                                type_name: td.name.clone(),
2048                                offender: v.clone(),
2049                                reason: format!(
2050                                    "`open_values` entry is not in `{}`'s enum_values [{}]",
2051                                    due.status_field,
2052                                    allowed.join(", ")
2053                                ),
2054                            });
2055                        }
2056                    }
2057                }
2058            },
2059        }
2060        if due.open_values.is_empty() {
2061            errors.push(SchemaLoadError::InvalidDueAxis {
2062                type_name: td.name.clone(),
2063                offender: "(empty)".to_string(),
2064                reason: "`open_values` must name at least one open status value".to_string(),
2065            });
2066        }
2067        if let Some(lead) = &due.lead_section
2068            && !td.sections.iter().any(|s| s.key == *lead)
2069        {
2070            errors.push(SchemaLoadError::InvalidDueAxis {
2071                type_name: td.name.clone(),
2072                offender: lead.clone(),
2073                reason: "`lead_section` names no section of this type".to_string(),
2074            });
2075        }
2076    }
2077
2078    // Exactly one catch_all section
2079    let catch_all_count = td.sections.iter().filter(|s| s.catch_all).count();
2080    if catch_all_count != 1 {
2081        errors.push(SchemaLoadError::CatchAllViolation {
2082            type_name: td.name.clone(),
2083            count: catch_all_count,
2084        });
2085    }
2086
2087    // Field-reference integrity
2088    let section_keys: HashSet<&str> = td.sections.iter().map(|s| s.key.as_str()).collect();
2089    let meta_keys: HashSet<&str> = td.metadata_fields.iter().map(|m| m.key.as_str()).collect();
2090
2091    for f in &td.text_fields {
2092        // text_fields point at section content — not metadata.
2093        if !section_keys.contains(f.as_str()) {
2094            errors.push(SchemaLoadError::UnknownFieldReference {
2095                type_name: td.name.clone(),
2096                field: "text_fields",
2097                reference: f.clone(),
2098            });
2099        }
2100    }
2101    for f in &td.health_required_fields {
2102        if !section_keys.contains(f.as_str()) && !meta_keys.contains(f.as_str()) {
2103            errors.push(SchemaLoadError::UnknownFieldReference {
2104                type_name: td.name.clone(),
2105                field: "health_required_fields",
2106                reference: f.clone(),
2107            });
2108        }
2109    }
2110    for f in &td.updatable_fields {
2111        // `title` is the entity's filename-derived title — always updatable.
2112        if f == "title" {
2113            continue;
2114        }
2115        if !section_keys.contains(f.as_str()) && !meta_keys.contains(f.as_str()) {
2116            errors.push(SchemaLoadError::UnknownFieldReference {
2117                type_name: td.name.clone(),
2118                field: "updatable_fields",
2119                reference: f.clone(),
2120            });
2121        }
2122    }
2123
2124    // Metadata default_value must be a member of enum_values when both present.
2125    for m in &td.metadata_fields {
2126        if let (Some(default), Some(allowed)) = (m.default_value.as_ref(), m.enum_values.as_ref())
2127            && !allowed.contains(default)
2128        {
2129            errors.push(SchemaLoadError::DefaultValueNotInEnum {
2130                type_name: td.name.clone(),
2131                field: m.key.clone(),
2132                default: default.clone(),
2133                allowed: allowed.clone(),
2134            });
2135        }
2136    }
2137}
2138
2139fn check_rel(
2140    type_name: &str,
2141    field: &'static str,
2142    relationship: &str,
2143    rel_names: &HashSet<String>,
2144    available: &[String],
2145) -> Result<(), SchemaLoadError> {
2146    if rel_names.contains(relationship) {
2147        return Ok(());
2148    }
2149    Err(SchemaLoadError::UndeclaredRelationship {
2150        type_name: type_name.into(),
2151        field,
2152        relationship: relationship.into(),
2153        available: available.to_vec(),
2154    })
2155}