aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / crumbs / tests / visible_conformance.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! CRB1 shared conformance: the Rust reference reader against the |
| 17 | //! language-neutral corpus in aien-protocols (specs/crumb-visible). |
| 18 | //! |
| 19 | //! Every vector listed in `expected.txt` is decoded with |
| 20 | //! `VisibleCrumb::from_bytes`; the outcome is printed in the contract's line |
| 21 | //! format and compared byte for byte. Refusals are compared by the contract's |
| 22 | //! numbered code, mapped here at the edge so no Rust type crosses the contract. |
| 23 | //! |
| 24 | //! Corpus location: `$CRB1_VECTORS`, else the sibling checkout |
| 25 | //! `../aien-protocols/specs/crumb-visible/vectors` next to this repository. |
| 26 | //! When the corpus is absent the test reports NOT_RUN, unless |
| 27 | //! `CRB1_REQUIRE=1` (set by the dedicated CI job), in which case it fails. |
| 28 | |
| 29 | use crumbs::visible::{VisibleCrumb, VisibleError}; |
| 30 | use std::path::PathBuf; |
| 31 | |
| 32 | /// Contract refusal codes (CRUMB_READER_CONTRACT.md, section "Refusal codes"). |
| 33 | fn contract_code(e: VisibleError) -> i32 { |
| 34 | match e { |
| 35 | VisibleError::Magic => -1, |
| 36 | VisibleError::Version => -2, |
| 37 | VisibleError::Shape => -3, |
| 38 | VisibleError::Lane => -4, |
| 39 | VisibleError::Length => -5, |
| 40 | VisibleError::NonCanonical => -6, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /// Contract outcome line (without the leading file name). |
| 45 | fn outcome(bytes: &[u8]) -> String { |
| 46 | let c = match VisibleCrumb::from_bytes(bytes) { |
| 47 | Ok(c) => c, |
| 48 | Err(e) => return format!("refuse {}", contract_code(e)), |
| 49 | }; |
| 50 | let mut line = format!( |
| 51 | "ok enc={} in={}x{} out={}x{} n={}", |
| 52 | c.encoding as u8, |
| 53 | c.in_arity, |
| 54 | c.in_lane_bytes, |
| 55 | c.out_arity, |
| 56 | c.out_lane_bytes, |
| 57 | c.examples.len() |
| 58 | ); |
| 59 | if let Some(b) = c.budget { |
| 60 | line.push_str(&format!( |
| 61 | " budget={},{},{},{}", |
| 62 | b.max_candidates, b.max_depth, b.max_oracle_queries, b.max_program_ops |
| 63 | )); |
| 64 | } |
| 65 | for (i, o) in c.values() { |
| 66 | line.push_str(" |"); |
| 67 | for v in i { |
| 68 | line.push_str(&format!(" {v}")); |
| 69 | } |
| 70 | line.push_str(" ->"); |
| 71 | for v in o { |
| 72 | line.push_str(&format!(" {v}")); |
| 73 | } |
| 74 | } |
| 75 | line |
| 76 | } |
| 77 | |
| 78 | fn corpus_dir() -> PathBuf { |
| 79 | match std::env::var_os("CRB1_VECTORS") { |
| 80 | Some(d) => PathBuf::from(d), |
| 81 | None => PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 82 | .join("../../../aien-protocols/specs/crumb-visible/vectors"), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | #[test] |
| 87 | fn crumb_visible_v1_conformance() { |
| 88 | let dir = corpus_dir(); |
| 89 | let manifest = dir.join("expected.txt"); |
| 90 | let Ok(text) = std::fs::read_to_string(&manifest) else { |
| 91 | let required = std::env::var("CRB1_REQUIRE").is_ok_and(|v| v == "1"); |
| 92 | println!( |
| 93 | "CRUMB_VISIBLE_V1_CONFORMANCE impl=rust NOT_RUN (no corpus at {})", |
| 94 | manifest.display() |
| 95 | ); |
| 96 | assert!(!required, "CRB1_REQUIRE=1 but the corpus is missing"); |
| 97 | return; |
| 98 | }; |
| 99 | let (mut pass, mut fail) = (0usize, 0usize); |
| 100 | for line in text.lines() { |
| 101 | if line.is_empty() || line.starts_with('#') { |
| 102 | continue; |
| 103 | } |
| 104 | let (name, want) = line |
| 105 | .split_once(' ') |
| 106 | .expect("manifest line: <file> <outcome>"); |
| 107 | let bytes = std::fs::read(dir.join(name)).expect("vector file listed in manifest"); |
| 108 | // Determinism: two decodes of the same bytes give the same line. |
| 109 | let got = outcome(&bytes); |
| 110 | assert_eq!(got, outcome(&bytes), "non-deterministic outcome for {name}"); |
| 111 | if got == want { |
| 112 | pass += 1; |
| 113 | } else { |
| 114 | fail += 1; |
| 115 | println!("MISMATCH {name}\n want: {want}\n got: {got}"); |
| 116 | } |
| 117 | } |
| 118 | println!("CRUMB_VISIBLE_V1_CONFORMANCE impl=rust pass={pass} fail={fail}"); |
| 119 | assert!(pass > 0, "empty corpus"); |
| 120 | assert_eq!(fail, 0); |
| 121 | } |