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 / aien-scheduler / tests / dual_decision_golden.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 | //! Before/after proof for the `serve.admit_preempt` tap (DUAL-3a). |
| 17 | //! |
| 18 | //! `build_scheduled_batch` was split into a thin wrapper and a traced body so |
| 19 | //! an observer can be shown the decision. This test uses only APIs that exist |
| 20 | //! on `main` before that change: it runs the pre-registered workload set with |
| 21 | //! no observer and compares the sha256 of the canonical trace of every |
| 22 | //! decision, per scenario, against |
| 23 | //! `tests/fixtures/dual_serve_admit_preempt.golden.json`. |
| 24 | //! |
| 25 | //! The fixture was produced on the base commit (recorded in the evidence |
| 26 | //! receipt) by running this same file there with |
| 27 | //! `DUAL_WRITE_GOLDEN=1 cargo test -p aien-scheduler --test dual_decision_golden`. |
| 28 | //! On the candidate commit the test compares; it never writes unless asked. |
| 29 | |
| 30 | mod dual_common; |
| 31 | |
| 32 | use dual_common::{build, canonical_json, run, scenarios, ScenarioTrace}; |
| 33 | use sha2::{Digest, Sha256}; |
| 34 | use std::collections::BTreeMap; |
| 35 | use std::path::PathBuf; |
| 36 | |
| 37 | fn fixture_path() -> PathBuf { |
| 38 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 39 | .join("tests/fixtures/dual_serve_admit_preempt.golden.json") |
| 40 | } |
| 41 | |
| 42 | fn all_traces() -> Vec<ScenarioTrace> { |
| 43 | scenarios() |
| 44 | .iter() |
| 45 | .map(|s| { |
| 46 | let (mut scheduler, kv) = build(s); |
| 47 | run(s, &mut scheduler, &kv) |
| 48 | }) |
| 49 | .collect() |
| 50 | } |
| 51 | |
| 52 | fn digests(traces: &[ScenarioTrace]) -> BTreeMap<String, String> { |
| 53 | traces |
| 54 | .iter() |
| 55 | .map(|t| { |
| 56 | let hex = Sha256::digest(canonical_json(t)) |
| 57 | .iter() |
| 58 | .map(|b| format!("{b:02x}")) |
| 59 | .collect::<String>(); |
| 60 | (t.name.clone(), format!("sha256:{hex}")) |
| 61 | }) |
| 62 | .collect() |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn decisions_match_golden_fixture_from_base_commit() { |
| 67 | let now = digests(&all_traces()); |
| 68 | let path = fixture_path(); |
| 69 | |
| 70 | if std::env::var_os("DUAL_WRITE_GOLDEN").is_some() { |
| 71 | let bytes = serde_json::to_vec_pretty(&now).expect("serialize"); |
| 72 | std::fs::write(&path, &bytes).expect("write golden fixture"); |
| 73 | eprintln!("wrote {} ({} bytes)", path.display(), bytes.len()); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | let golden = std::fs::read(&path).unwrap_or_else(|e| { |
| 78 | panic!( |
| 79 | "missing golden fixture {}: {e}. Produce it on the base commit with DUAL_WRITE_GOLDEN=1", |
| 80 | path.display() |
| 81 | ) |
| 82 | }); |
| 83 | let golden: BTreeMap<String, String> = serde_json::from_slice(&golden).expect("parse golden"); |
| 84 | assert_eq!( |
| 85 | now.keys().collect::<Vec<_>>(), |
| 86 | golden.keys().collect::<Vec<_>>(), |
| 87 | "scenario set differs from the golden fixture" |
| 88 | ); |
| 89 | for (name, digest) in &now { |
| 90 | assert_eq!( |
| 91 | digest, &golden[name], |
| 92 | "scenario {name} decides differently from the base commit" |
| 93 | ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | #[test] |
| 98 | fn workload_is_deterministic_run_to_run() { |
| 99 | let a = all_traces(); |
| 100 | let b = all_traces(); |
| 101 | assert!(canonical_json(&a) == canonical_json(&b)); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn drained_scenarios_leak_nothing() { |
| 106 | for s in scenarios() { |
| 107 | let (mut scheduler, kv) = build(&s); |
| 108 | let trace = run(&s, &mut scheduler, &kv); |
| 109 | if s.expect_drained { |
| 110 | assert_eq!( |
| 111 | trace.final_kv.allocated_blocks, 0, |
| 112 | "{}: KV blocks held", |
| 113 | s.name |
| 114 | ); |
| 115 | assert_eq!(trace.final_kv.active_tables, 0, "{}: tables left", s.name); |
| 116 | assert!(trace.final_kv.active_blocks.is_empty(), "{}", s.name); |
| 117 | assert_eq!(trace.final_scheduler.running, 0, "{}", s.name); |
| 118 | assert_eq!(trace.final_scheduler.waiting, 0, "{}", s.name); |
| 119 | assert_eq!(trace.final_scheduler.preempted, 0, "{}", s.name); |
| 120 | assert_eq!(trace.final_scheduler.arena_active, 0, "{}", s.name); |
| 121 | } else { |
| 122 | // The refused request stays queued and never allocated. |
| 123 | assert_eq!(trace.final_kv.allocated_blocks, 0, "{}", s.name); |
| 124 | assert!( |
| 125 | trace.steps.iter().any(|st| st.error.is_some()), |
| 126 | "{}: expected a refusal", |
| 127 | s.name |
| 128 | ); |
| 129 | } |
| 130 | } |
| 131 | } |