From 92db992ea9d9eb995a1a84682eb23e8c35f11fca Mon Sep 17 00:00:00 2001 From: Orual Date: Mon, 3 Aug 2026 07:42:24 -0400 Subject: [PATCH] Fail closed on missing authored evidence Require derivation groups and native oracle projections for every present fixture. Keep differential comparison mandatory so absent authored evidence cannot report pass. Epic: PM-86 Task: PM-78 --- crates/polymodel-ldraw-core/src/resolver.rs | 0 .../polymodel-ldraw-testkit/src/inventory.rs | 21 +-- crates/polymodel-ldraw-testkit/src/schema.rs | 154 ++++++++++++++++++ tools/ldraw-compat-harness/src/main.rs | 19 ++- 4 files changed, 167 insertions(+), 27 deletions(-) delete mode 100644 crates/polymodel-ldraw-core/src/resolver.rs diff --git a/crates/polymodel-ldraw-core/src/resolver.rs b/crates/polymodel-ldraw-core/src/resolver.rs deleted file mode 100644 index e69de29..0000000 diff --git a/crates/polymodel-ldraw-testkit/src/inventory.rs b/crates/polymodel-ldraw-testkit/src/inventory.rs index 428f0e1..6bf4569 100644 --- a/crates/polymodel-ldraw-testkit/src/inventory.rs +++ b/crates/polymodel-ldraw-testkit/src/inventory.rs @@ -1,6 +1,6 @@ use crate::{ AuthorityClass, ExpectedOutcome, ExpectedRecord, FixtureCase, Profile, Provenance, - SCHEMA_VERSION, hex_digest, parse_expected, + SCHEMA_VERSION, hex_digest, parse_expected, validate_expected_evidence, }; use serde::Deserialize; use std::collections::BTreeSet; @@ -448,24 +448,7 @@ pub fn validate_inventory(fixtures: &[FixtureCase]) -> Result<(), String> { if fixture.expected.canonical.provenance_id != fixture.id { return Err(format!("canonical provenance link mismatch {}", fixture.id)); } - for derivation in &fixture.expected.derivations { - if derivation.field.is_empty() - || derivation.source.is_empty() - || derivation.revision.is_empty() - || derivation.locator.is_empty() - || derivation.basis.is_empty() - { - return Err(format!("incomplete derivation {}", fixture.id)); - } - if derivation.authority == AuthorityClass::Observation - && derivation.decision.as_deref().unwrap_or("").is_empty() - { - return Err(format!( - "observation derivation without decision {}", - fixture.id - )); - } - } + validate_expected_evidence(&fixture.expected, fixture.id)?; } Ok(()) } diff --git a/crates/polymodel-ldraw-testkit/src/schema.rs b/crates/polymodel-ldraw-testkit/src/schema.rs index f671322..4e43b66 100644 --- a/crates/polymodel-ldraw-testkit/src/schema.rs +++ b/crates/polymodel-ldraw-testkit/src/schema.rs @@ -255,3 +255,157 @@ pub fn unique_strings(values: impl IntoIterator) -> bool { let values = values.into_iter().collect::>(); values.len() == values.iter().collect::>().len() } + +const REQUIRED_DERIVATION_GROUPS: &[(&str, &[&str])] = &[ + ("bfc", &["bfc"]), + ("bounds/transforms", &["bounds", "transform", "reflection"]), + ("graph/cache/instances", &["graph", "cache", "instance"]), + ("colours", &["colour", "color"]), + ("texmap", &["texmap"]), + ("diagnostics/profile", &["diagnostic", "profile", "outcome"]), + ("limits", &["limit"]), + ("steps", &["step"]), +]; + +fn derivation_group(field: &str) -> Option<&'static str> { + let field = field.to_ascii_lowercase(); + REQUIRED_DERIVATION_GROUPS + .iter() + .find(|(_, markers)| markers.iter().any(|marker| field.contains(marker))) + .map(|(group, _)| *group) +} + +fn validate_oracle_projection( + projection: &OracleProjection, + fixture_id: &str, +) -> Result<(), String> { + match (projection.status.as_str(), projection.error.is_some()) { + ("ok", false) | ("error", true) => {} + (status, has_error) => { + return Err(format!( + "invalid oracle projection status/error shape {fixture_id}: status={status:?}, error={has_error}" + )); + } + } + let mut names = BTreeSet::new(); + for (index, model) in projection.models.iter().enumerate() { + if model.name.is_empty() { + return Err(format!("oracle model has empty name {fixture_id}")); + } + if model.model_index != index as u32 { + return Err(format!( + "oracle model indices are not sequential {fixture_id}" + )); + } + if !names.insert(&model.name) { + return Err(format!( + "duplicate oracle model name {fixture_id}: {}", + model.name + )); + } + if normalize_certification(&model.certify).is_none() { + return Err(format!( + "invalid oracle model certification {fixture_id}: {}", + model.certify + )); + } + } + Ok(()) +} + +pub fn validate_expected_evidence( + expected: &ExpectedRecord, + fixture_id: &str, +) -> Result<(), String> { + if expected.derivations.is_empty() { + return Err(format!("missing derivation evidence {fixture_id}")); + } + + let mut groups = BTreeSet::new(); + for derivation in &expected.derivations { + if derivation.field.trim().is_empty() + || derivation.source.trim().is_empty() + || derivation.revision.trim().is_empty() + || derivation.locator.trim().is_empty() + || derivation.basis.trim().is_empty() + { + return Err(format!("incomplete derivation evidence {fixture_id}")); + } + let group = derivation_group(&derivation.field) + .ok_or_else(|| format!("unknown derivation field {} {fixture_id}", derivation.field))?; + groups.insert(group); + if derivation.authority == AuthorityClass::Observation + && derivation + .decision + .as_deref() + .unwrap_or("") + .trim() + .is_empty() + { + return Err(format!( + "observation derivation without decision {fixture_id}" + )); + } + } + for (group, _) in REQUIRED_DERIVATION_GROUPS { + if !groups.contains(group) { + return Err(format!("missing derivation group {group} {fixture_id}")); + } + } + + let projection = expected + .oracle_projection + .as_ref() + .ok_or_else(|| format!("missing oracle projection {fixture_id}"))?; + validate_oracle_projection(projection, fixture_id) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn expected_evidence_rejects_missing_derivations_before_oracle_data() { + let expected = ExpectedRecord { + outcome: ExpectedOutcome::Accepted, + profile: Profile::Strict, + canonical: serde_json::from_str( + r#"{ + "schema_version":"ldraw-canonical-v1", + "provenance_id":"fixture", + "syntax":[], + "semantic":{ + "canonical_path":"fixture.ldr", + "root_identity":"root", + "target_identity":"target", + "cache_identity":"cache", + "bfc_state":"unknown", + "colour_state":16, + "steps":[], + "limits":[], + "texmap_events":[] + }, + "scene":{ + "model_id":"model", + "instance_ids":[], + "instances":[], + "bounds":null, + "reflection":false, + "triangles":0, + "quads":0, + "lines":0, + "conditional_lines":0 + }, + "diagnostics":[] + }"#, + ) + .unwrap(), + derivations: Vec::new(), + oracle_projection: None, + }; + assert_eq!( + validate_expected_evidence(&expected, "fixture").unwrap_err(), + "missing derivation evidence fixture" + ); + } +} diff --git a/tools/ldraw-compat-harness/src/main.rs b/tools/ldraw-compat-harness/src/main.rs index 4235922..3a3b187 100644 --- a/tools/ldraw-compat-harness/src/main.rs +++ b/tools/ldraw-compat-harness/src/main.rs @@ -333,14 +333,17 @@ fn differential() -> Result { let path = root.join("crates/polymodel-ldraw-testkit").join(fixture.relative_path); let actual = run_oracle(&binary, &path, ORACLE_TIMEOUT)?; let actual_projection = projection(actual); - if let Some(expected_projection) = &fixture.expected.oracle_projection { - polymodel_ldraw_testkit::compare_oracle_projection( - expected_projection, - &actual_projection, - fixture.id, - ) - .map_err(|error| format!("{} (source: {}; oracle revision: {}; schema: {})", error, fixture.provenance.id, LD_PARSE_REVISION, ORACLE_SCHEMA_VERSION))?; - } + let expected_projection = fixture + .expected + .oracle_projection + .as_ref() + .ok_or_else(|| format!("missing authored oracle projection: {}", fixture.id))?; + polymodel_ldraw_testkit::compare_oracle_projection( + expected_projection, + &actual_projection, + fixture.id, + ) + .map_err(|error| format!("{} (source: {}; oracle revision: {}; schema: {})", error, fixture.provenance.id, LD_PARSE_REVISION, ORACLE_SCHEMA_VERSION))?; } serde_json::to_string_pretty(&Report { schema_version: SCHEMA_VERSION, -- knotmirror 0.0.1