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 / curriculum.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 | //! CRUMB_CURRICULUM_PASS, CRUMB_NO_CATEGORY_LEAK_PASS, CLEAN_ROOM_PROVENANCE_PASS, |
| 17 | //! CRUMB_OPERATION_PROMOTION_PASS (ladder unit), ledger chain, and the trace |
| 18 | //! corpus gates that do not need a real learner (CRUMB_TRACE_PASS, |
| 19 | //! CRUMB_TRAINING_SAMPLE_PASS, NO_COT_STORAGE_PASS with the probe learner). |
| 20 | |
| 21 | use crumbs::audit::find_labels; |
| 22 | use crumbs::digest::{CrumbOccurrenceId, Digest}; |
| 23 | use crumbs::gen::{self, registry}; |
| 24 | use crumbs::ledger::{CrumblineRecord, Ledger, LedgerError}; |
| 25 | use crumbs::mixer::{Mixer, MixerConfig}; |
| 26 | use crumbs::program::{Op, Program}; |
| 27 | use crumbs::promotion::{Ladder, PromotionConfig, Stage}; |
| 28 | use crumbs::provenance::{ |
| 29 | clean_room_check, CleanRoomViolation, ContaminationClass, Lane, Provenance, |
| 30 | }; |
| 31 | use crumbs::sealed::Population; |
| 32 | use crumbs::session::{Session, SessionConfig}; |
| 33 | use crumbs::trace::{self, verify_chain, SAMPLE_RECORD_BYTES, TRACE_RECORD_BYTES}; |
| 34 | use crumbs::verify::evaluate; |
| 35 | use std::collections::BTreeMap; |
| 36 | |
| 37 | const PROBE: &str = env!("CARGO_BIN_EXE_crumbs-probe-learner"); |
| 38 | |
| 39 | #[test] |
| 40 | fn crumb_curriculum_pass() { |
| 41 | // The v1 schedule is data: it loads from the shipped config file. |
| 42 | let cfg = |
| 43 | MixerConfig::from_json(include_str!("../config/mixer.v1.json")).expect("config parses"); |
| 44 | assert_eq!(cfg, MixerConfig::default()); |
| 45 | let w: Vec<u32> = Population::ALL.iter().map(|p| cfg.populations[p]).collect(); |
| 46 | assert_eq!(w, vec![55, 20, 10, 10, 5]); |
| 47 | |
| 48 | let mut m = Mixer::new(cfg.clone(), 7); |
| 49 | let mut counts: BTreeMap<Population, u32> = BTreeMap::new(); |
| 50 | let mut last: Option<u32> = None; |
| 51 | for _ in 0..3000 { |
| 52 | let d = m.draw(); |
| 53 | *counts.entry(d.population).or_insert(0) += 1; |
| 54 | assert_ne!( |
| 55 | Some(d.family_id), |
| 56 | last, |
| 57 | "the same family was drawn twice in a row" |
| 58 | ); |
| 59 | last = Some(d.family_id); |
| 60 | } |
| 61 | // No frontier sources exist in v1: its share is redistributed and recorded. |
| 62 | assert!( |
| 63 | m.redistributed_draws |
| 64 | .get(&Population::Frontier) |
| 65 | .copied() |
| 66 | .unwrap_or(0) |
| 67 | > 0 |
| 68 | ); |
| 69 | let fund = counts[&Population::Fundamental] as f64 / 3000.0; |
| 70 | assert!((0.45..0.75).contains(&fund), "fundamental share {fund}"); |
| 71 | |
| 72 | // Adaptation: demonstrating capabilities moves weight to composition. |
| 73 | let before = m.weights(); |
| 74 | for f in registry() |
| 75 | .families |
| 76 | .iter() |
| 77 | .filter(|f| f.params.caps.len() == 1) |
| 78 | { |
| 79 | m.record(f, true); |
| 80 | } |
| 81 | let after = m.weights(); |
| 82 | assert!(after[&Population::Fundamental] < before[&Population::Fundamental]); |
| 83 | assert!(after[&Population::Composition] > before[&Population::Composition]); |
| 84 | |
| 85 | // Determinism: same seed, same draws. |
| 86 | let (mut a, mut b) = (Mixer::new(cfg.clone(), 42), Mixer::new(cfg, 42)); |
| 87 | for _ in 0..200 { |
| 88 | let (x, y) = (a.draw(), b.draw()); |
| 89 | assert_eq!((x.family_id, x.seed), (y.family_id, y.seed)); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /// Curriculum order carries no ontology: the learner-visible stream of a long |
| 94 | /// run contains no population, family or label bytes, and consecutive draws |
| 95 | /// never share a family or a capability. |
| 96 | #[test] |
| 97 | fn crumb_no_category_leak_pass() { |
| 98 | let mut m = Mixer::new(MixerConfig::default(), 3); |
| 99 | let mut stream = Vec::new(); |
| 100 | let mut prev_caps: Vec<u16> = vec![]; |
| 101 | for _ in 0..500 { |
| 102 | let d = m.draw(); |
| 103 | let f = registry().get(d.family_id).unwrap(); |
| 104 | assert!( |
| 105 | prev_caps.iter().all(|c| !f.params.caps.contains(c)), |
| 106 | "consecutive crumbs share a capability" |
| 107 | ); |
| 108 | prev_caps = f.params.caps.clone(); |
| 109 | stream.extend(gen::generate(f, d.seed).visible.to_bytes()); |
| 110 | } |
| 111 | assert!(find_labels(&stream).is_empty()); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn clean_room_provenance_pass() { |
| 116 | // Every registered family declares a contamination class. |
| 117 | for f in ®istry().families { |
| 118 | let _ = f.provenance.class; |
| 119 | } |
| 120 | // The forbidden list. |
| 121 | assert_eq!( |
| 122 | clean_room_check(&Provenance::human_theory()), |
| 123 | Err(CleanRoomViolation::HumanTheoryDerived) |
| 124 | ); |
| 125 | for (class, want) in [ |
| 126 | ( |
| 127 | ContaminationClass::FrontierDerived, |
| 128 | CleanRoomViolation::FrontierDerived, |
| 129 | ), |
| 130 | ( |
| 131 | ContaminationClass::RealWorldMeasured, |
| 132 | CleanRoomViolation::RealWorldMeasured, |
| 133 | ), |
| 134 | ] { |
| 135 | let p = Provenance { |
| 136 | class, |
| 137 | uses_human_constants: false, |
| 138 | uses_human_ontology: false, |
| 139 | }; |
| 140 | assert_eq!(clean_room_check(&p), Err(want)); |
| 141 | } |
| 142 | let consts = Provenance { |
| 143 | uses_human_constants: true, |
| 144 | ..Provenance::synthetic() |
| 145 | }; |
| 146 | assert_eq!( |
| 147 | clean_room_check(&consts), |
| 148 | Err(CleanRoomViolation::HumanConstants) |
| 149 | ); |
| 150 | let onto = Provenance { |
| 151 | uses_human_ontology: true, |
| 152 | ..Provenance::synthetic() |
| 153 | }; |
| 154 | assert_eq!( |
| 155 | clean_room_check(&onto), |
| 156 | Err(CleanRoomViolation::HumanOntology) |
| 157 | ); |
| 158 | assert!(clean_room_check(&Provenance::synthetic()).is_ok()); |
| 159 | assert!(clean_room_check(&Provenance::adversarial()).is_ok()); |
| 160 | |
| 161 | // The clean-room ledger partition refuses forbidden records outright. |
| 162 | let mut clean = Ledger::new(Lane::CleanRoom, "t"); |
| 163 | let rec = |seq| CrumblineRecord { |
| 164 | seq, |
| 165 | lane: Lane::Main, |
| 166 | occurrence: CrumbOccurrenceId::new(), |
| 167 | condition: "t".into(), |
| 168 | population: Population::Fundamental, |
| 169 | crumb_digest: Digest::ZERO, |
| 170 | generator_instance_digest: Digest::ZERO, |
| 171 | sealed_digest: Digest::ZERO, |
| 172 | verifier_digest: Digest::ZERO, |
| 173 | learner_digest: Digest::ZERO, |
| 174 | evaluations: vec![], |
| 175 | trace_stream_digest: Digest::ZERO, |
| 176 | promotions: vec![], |
| 177 | prev_root: Digest::ZERO, |
| 178 | root: Digest::ZERO, |
| 179 | }; |
| 180 | assert_eq!( |
| 181 | clean.append(rec(0), &Provenance::human_theory()), |
| 182 | Err(LedgerError::CleanRoom( |
| 183 | CleanRoomViolation::HumanTheoryDerived |
| 184 | )) |
| 185 | ); |
| 186 | assert!(clean.append(rec(0), &Provenance::synthetic()).is_ok()); |
| 187 | assert_eq!(clean.records[0].lane, Lane::CleanRoom); |
| 188 | |
| 189 | // A clean-room mixer never draws a forbidden family. |
| 190 | let mut m = Mixer::new( |
| 191 | MixerConfig { |
| 192 | clean_room: true, |
| 193 | ..MixerConfig::default() |
| 194 | }, |
| 195 | 11, |
| 196 | ); |
| 197 | for _ in 0..1000 { |
| 198 | let f = registry().get(m.draw().family_id).unwrap(); |
| 199 | assert!( |
| 200 | clean_room_check(&f.provenance).is_ok(), |
| 201 | "{} entered the clean room", |
| 202 | f.key |
| 203 | ); |
| 204 | } |
| 205 | // Contamination classes never appear in learner bytes (checked for every family in boundary.rs). |
| 206 | } |
| 207 | |
| 208 | #[test] |
| 209 | fn ledger_chain_detects_tampering() { |
| 210 | let mut l = Ledger::new(Lane::Main, "run"); |
| 211 | for s in 0..5 { |
| 212 | let r = CrumblineRecord { |
| 213 | seq: s, |
| 214 | lane: Lane::Main, |
| 215 | occurrence: CrumbOccurrenceId::new(), |
| 216 | condition: "c".into(), |
| 217 | population: Population::Composition, |
| 218 | crumb_digest: Digest([s as u8; 32]), |
| 219 | generator_instance_digest: Digest::ZERO, |
| 220 | sealed_digest: Digest::ZERO, |
| 221 | verifier_digest: Digest::ZERO, |
| 222 | learner_digest: Digest::ZERO, |
| 223 | evaluations: vec![], |
| 224 | trace_stream_digest: Digest::ZERO, |
| 225 | promotions: vec![], |
| 226 | prev_root: Digest::ZERO, |
| 227 | root: Digest::ZERO, |
| 228 | }; |
| 229 | l.append(r, &Provenance::synthetic()).unwrap(); |
| 230 | } |
| 231 | assert_eq!(Ledger::verify(l.genesis, &l.records), Ok(l.head)); |
| 232 | let mut forged = l.records.clone(); |
| 233 | forged[2].crumb_digest = Digest([9; 32]); |
| 234 | assert_eq!(Ledger::verify(l.genesis, &forged), Err(LedgerError::Chain)); |
| 235 | } |
| 236 | |
| 237 | /// One solved crumb never admits an operation; two distinct supporting crumbs |
| 238 | /// with adversarial and extrapolation evidence do, with an explicit scope. |
| 239 | #[test] |
| 240 | fn crumb_operation_promotion_pass() { |
| 241 | let fam = registry().by_key("capability-03").unwrap(); |
| 242 | let mut ladder = Ladder::new(PromotionConfig::default()); |
| 243 | let g1 = gen::generate(fam, 1); |
| 244 | let crumbs::sealed::KnownSolution::Known(truth) = g1.sealed.known_solution.clone() else { |
| 245 | panic!() |
| 246 | }; |
| 247 | let e1 = evaluate(&g1.visible, &g1.sealed, &truth, 1, false); |
| 248 | ladder.observe_accept(0, &g1.sealed, &e1, &truth); |
| 249 | assert!( |
| 250 | ladder.advance(0).is_empty(), |
| 251 | "one crumb must not admit anything" |
| 252 | ); |
| 253 | let rec = ladder.ops.values().next().unwrap(); |
| 254 | assert_eq!(rec.stage, Stage::HeldoutVerified); |
| 255 | // The same crumb again is not new evidence. |
| 256 | ladder.observe_accept(1, &g1.sealed, &e1, &truth); |
| 257 | assert!(ladder.advance(1).is_empty()); |
| 258 | let g2 = gen::generate(fam, 2); |
| 259 | let e2 = evaluate(&g2.visible, &g2.sealed, &truth, 1, false); |
| 260 | ladder.observe_accept(2, &g2.sealed, &e2, &truth); |
| 261 | let adm = ladder.advance(2); |
| 262 | assert_eq!(adm.len(), 1); |
| 263 | assert_eq!(adm[0].program, truth); |
| 264 | assert!(adm[0].scope_bits > 0 && adm[0].scope_bits <= 64); |
| 265 | let rec = ladder.by_ref(adm[0].op_ref).unwrap(); |
| 266 | let stages: Vec<Stage> = rec.history.iter().map(|h| h.0).collect(); |
| 267 | assert_eq!( |
| 268 | stages, |
| 269 | vec![ |
| 270 | Stage::Candidate, |
| 271 | Stage::HeldoutVerified, |
| 272 | Stage::Corroborated, |
| 273 | Stage::SurvivedAdversarial, |
| 274 | Stage::Generalized, |
| 275 | Stage::Admitted |
| 276 | ] |
| 277 | ); |
| 278 | // Too many held-out-falsified exposures suspend a candidate instead of promoting it. |
| 279 | let mut l2 = Ladder::new(PromotionConfig { |
| 280 | max_exposure_pct: 50, |
| 281 | ..PromotionConfig::default() |
| 282 | }); |
| 283 | let decoy = gen::generate(registry().by_key("decoy-simple-rule-w12").unwrap(), 4); |
| 284 | let alt = decoy.sealed.alternatives[0].clone(); |
| 285 | let ea = evaluate(&decoy.visible, &decoy.sealed, &alt, 1, false); |
| 286 | let gcap = gen::generate(registry().by_key("chain-basic-l2-w16").unwrap(), 5); |
| 287 | let crumbs::sealed::KnownSolution::Known(cap) = gcap.sealed.known_solution.clone() else { |
| 288 | panic!() |
| 289 | }; |
| 290 | let _ = Program::of(&[(Op::Mul, 2)]); |
| 291 | let ec = evaluate(&gcap.visible, &gcap.sealed, &cap, 1, false); |
| 292 | l2.observe_accept(0, &gcap.sealed, &ec, &cap); |
| 293 | l2.observe_reject(&ea, &cap); |
| 294 | l2.observe_reject(&ea, &cap); |
| 295 | assert!(l2.advance(1).is_empty()); |
| 296 | assert_eq!( |
| 297 | l2.ops[&cap.behavior()].stage, |
| 298 | Stage::Suspended, |
| 299 | "exposed operation must be suspended, not promoted" |
| 300 | ); |
| 301 | let _ = alt; |
| 302 | } |
| 303 | |
| 304 | /// Trace corpus through a real (probe) learner session: chained records, |
| 305 | /// numbers only, hidden results only on SUBMIT records, samples on solved crumbs. |
| 306 | #[test] |
| 307 | fn crumb_trace_and_sample_pass() { |
| 308 | let dir = std::env::temp_dir().join(format!("crumbs-trace-{}", std::process::id())); |
| 309 | let _ = std::fs::remove_dir_all(&dir); |
| 310 | let cfg = SessionConfig { |
| 311 | condition: "test".into(), |
| 312 | learner: PROBE.into(), |
| 313 | run_id: "trace-test".into(), |
| 314 | search: Default::default(), |
| 315 | library_enabled: false, |
| 316 | promotion: PromotionConfig::default(), |
| 317 | record_transcript: false, |
| 318 | out_dir: Some(dir.clone()), |
| 319 | }; |
| 320 | // The probe reports no search events; the trace still records its submissions. |
| 321 | let mut s = Session::start(cfg).unwrap(); |
| 322 | let fam = registry().by_key("chain-basic-l1-w16").unwrap(); |
| 323 | let o = s.present(fam, 3, Population::Fundamental, "t").unwrap(); |
| 324 | assert!(o.accepted); |
| 325 | s.finish().unwrap(); |
| 326 | let tr = std::fs::read(dir.join("trace.ctr")).unwrap(); |
| 327 | let smp = std::fs::read(dir.join("samples.cts")).unwrap_or_default(); |
| 328 | assert_eq!(tr.len() % TRACE_RECORD_BYTES, 0); |
| 329 | assert_eq!(smp.len() % SAMPLE_RECORD_BYTES, 0); |
| 330 | assert!(verify_chain(&tr, TRACE_RECORD_BYTES, crumbs::digest::Domain::TraceRecord).is_some()); |
| 331 | let mut tampered = tr.clone(); |
| 332 | if !tampered.is_empty() { |
| 333 | tampered[40] ^= 1; |
| 334 | assert!(verify_chain( |
| 335 | &tampered, |
| 336 | TRACE_RECORD_BYTES, |
| 337 | crumbs::digest::Domain::TraceRecord |
| 338 | ) |
| 339 | .is_none()); |
| 340 | } |
| 341 | assert!(find_labels(&tr).is_empty() && find_labels(&smp).is_empty()); |
| 342 | let _ = trace::bank_digest(&[]); |
| 343 | } |