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 / spark-harness / tests / common / mod.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 | //! Shared fixtures for the Headstart M2 scheduler tests. Deterministic: |
| 17 | //! a fixed receipt clock and a manual monotonic scheduler clock. |
| 18 | #![allow(dead_code)] |
| 19 | |
| 20 | use serde_json::{json, Value}; |
| 21 | use spark_harness::completion::CompletionSubmission; |
| 22 | use spark_harness::readiness::{InterfaceSubmission, ProofClaim}; |
| 23 | use spark_harness::scheduler::{ |
| 24 | Clock, Effects, Event, EventKind, Outcome, ProvisionalConfig, ResourceLimits, ResourceRequest, |
| 25 | SchedError, Scheduler, |
| 26 | }; |
| 27 | use spark_harness::{HarnessEngine, WorkflowState}; |
| 28 | use std::fs; |
| 29 | use std::path::{Path, PathBuf}; |
| 30 | use std::sync::atomic::{AtomicU64, Ordering}; |
| 31 | use std::sync::Arc; |
| 32 | |
| 33 | pub const COMMIT: &str = "1111111111111111111111111111111111111111"; |
| 34 | |
| 35 | fn fixed_receipt_clock() -> u64 { |
| 36 | 1_760_000_000 |
| 37 | } |
| 38 | |
| 39 | /// Temp copy of the shipped definitions, removed on drop. |
| 40 | pub struct Fixture { |
| 41 | pub dir: PathBuf, |
| 42 | } |
| 43 | |
| 44 | impl Fixture { |
| 45 | pub fn new() -> Self { |
| 46 | let dir = std::env::temp_dir().join(format!("spark-harness-m2-{}", uuid::Uuid::new_v4())); |
| 47 | let src = Path::new(env!("CARGO_MANIFEST_DIR")); |
| 48 | fs::create_dir_all(dir.join("schemas")).unwrap(); |
| 49 | for f in ["workflows.json", "evals.json"] { |
| 50 | fs::copy(src.join(f), dir.join(f)).unwrap(); |
| 51 | } |
| 52 | for entry in fs::read_dir(src.join("schemas")).unwrap() { |
| 53 | let entry = entry.unwrap(); |
| 54 | fs::copy(entry.path(), dir.join("schemas").join(entry.file_name())).unwrap(); |
| 55 | } |
| 56 | Fixture { dir } |
| 57 | } |
| 58 | |
| 59 | pub fn engine(&self) -> HarnessEngine { |
| 60 | let mut e = HarnessEngine::load(&self.dir).expect("shipped definitions load"); |
| 61 | e.completion_clock = fixed_receipt_clock; |
| 62 | e |
| 63 | } |
| 64 | |
| 65 | pub fn journal(&self) -> PathBuf { |
| 66 | self.dir.join("sched").join("journal.jsonl") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl Drop for Fixture { |
| 71 | fn drop(&mut self) { |
| 72 | let _ = fs::remove_dir_all(&self.dir); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub fn clock(ticks: &Arc<AtomicU64>) -> Clock { |
| 77 | let t = Arc::clone(ticks); |
| 78 | Box::new(move || t.load(Ordering::SeqCst)) |
| 79 | } |
| 80 | |
| 81 | pub fn enabled() -> ProvisionalConfig { |
| 82 | ProvisionalConfig { |
| 83 | enabled: true, |
| 84 | queue_capacity: 8, |
| 85 | limits: ResourceLimits { |
| 86 | cpu_slots: 8, |
| 87 | memory_bytes: 1 << 30, |
| 88 | max_concurrency: 8, |
| 89 | }, |
| 90 | lease_ttl: 1_000, |
| 91 | dedup_window: 64, |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub fn req(cpu: u32) -> ResourceRequest { |
| 96 | ResourceRequest { |
| 97 | cpu_slots: cpu, |
| 98 | memory_bytes: 1 << 20, |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn engineering_fix_artifact(task: &str) -> Value { |
| 103 | json!({ |
| 104 | "bug": format!("Parser rejects valid token sequence in {task}"), |
| 105 | "reproduction_steps": ["Run parser test"], |
| 106 | "failure_evidence": "thread panicked at line 42", |
| 107 | "regression_test": "tests::test_parser", |
| 108 | "diff_summary": "Fixed token parser logic", |
| 109 | "retest_evidence": "All tests passing cleanly" |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | /// One scheduler plus the harness engine whose store it verifies against. |
| 114 | pub struct H { |
| 115 | pub fx: Fixture, |
| 116 | pub engine: HarnessEngine, |
| 117 | pub ticks: Arc<AtomicU64>, |
| 118 | pub sched: Scheduler, |
| 119 | n: u64, |
| 120 | } |
| 121 | |
| 122 | impl H { |
| 123 | pub fn new(cfg: ProvisionalConfig) -> Self { |
| 124 | let fx = Fixture::new(); |
| 125 | let engine = fx.engine(); |
| 126 | let ticks = Arc::new(AtomicU64::new(0)); |
| 127 | let sched = Scheduler::new(cfg, &engine.completion_store, clock(&ticks)).unwrap(); |
| 128 | H { |
| 129 | fx, |
| 130 | engine, |
| 131 | ticks, |
| 132 | sched, |
| 133 | n: 0, |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | pub fn journaled(cfg: ProvisionalConfig) -> Self { |
| 138 | let fx = Fixture::new(); |
| 139 | let engine = fx.engine(); |
| 140 | let ticks = Arc::new(AtomicU64::new(0)); |
| 141 | let (unaudited, report) = |
| 142 | Scheduler::open(cfg, &engine.completion_store, &fx.journal(), clock(&ticks)).unwrap(); |
| 143 | assert_eq!(report.entries, 0); |
| 144 | let (sched, revoked) = unaudited.audit().unwrap(); |
| 145 | assert!(revoked.is_empty()); |
| 146 | H { |
| 147 | fx, |
| 148 | engine, |
| 149 | ticks, |
| 150 | sched, |
| 151 | n: 0, |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /// Drop the scheduler (closing the journal), replay it from disk and |
| 156 | /// audit it. Returns the torn bytes dropped and the tasks the audit |
| 157 | /// revoked. |
| 158 | pub fn reopen(&mut self, cfg: ProvisionalConfig) -> Result<(usize, Vec<String>), SchedError> { |
| 159 | let placeholder = Scheduler::new( |
| 160 | cfg.clone(), |
| 161 | &self.engine.completion_store, |
| 162 | clock(&self.ticks), |
| 163 | )?; |
| 164 | drop(std::mem::replace(&mut self.sched, placeholder)); |
| 165 | let (unaudited, report) = Scheduler::open( |
| 166 | cfg, |
| 167 | &self.engine.completion_store, |
| 168 | &self.fx.journal(), |
| 169 | clock(&self.ticks), |
| 170 | )?; |
| 171 | let (sched, revoked) = unaudited.audit()?; |
| 172 | self.sched = sched; |
| 173 | Ok((report.torn_bytes, revoked)) |
| 174 | } |
| 175 | |
| 176 | /// Simulate a process restart: the new process's clock starts at 0. |
| 177 | pub fn restart(&mut self, cfg: ProvisionalConfig) -> Result<(usize, Vec<String>), SchedError> { |
| 178 | self.ticks.store(0, Ordering::SeqCst); |
| 179 | self.reopen(cfg) |
| 180 | } |
| 181 | |
| 182 | pub fn advance(&self, d: u64) { |
| 183 | self.ticks.fetch_add(d, Ordering::SeqCst); |
| 184 | } |
| 185 | |
| 186 | pub fn next_id(&mut self) -> String { |
| 187 | self.n += 1; |
| 188 | format!("ev-{}", self.n) |
| 189 | } |
| 190 | |
| 191 | /// Deliver with a fresh id; check invariants either way. |
| 192 | pub fn send(&mut self, kind: EventKind) -> Result<Outcome, SchedError> { |
| 193 | let id = self.next_id(); |
| 194 | let r = self.sched.deliver(Event { id, kind }); |
| 195 | self.sched.check_invariants().unwrap(); |
| 196 | r |
| 197 | } |
| 198 | |
| 199 | pub fn ok(&mut self, kind: EventKind) -> Effects { |
| 200 | self.advance(10); |
| 201 | match self.send(kind.clone()) { |
| 202 | Ok(Outcome::Applied(fx)) => fx, |
| 203 | other => panic!("event {kind:?} not applied: {other:?}"), |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | pub fn register(&mut self, task: &str, cpu: u32, producers: &[&str]) -> Effects { |
| 208 | self.ok(EventKind::RegisterTask { |
| 209 | task: task.into(), |
| 210 | resources: req(cpu), |
| 211 | producers: producers.iter().map(|p| p.to_string()).collect(), |
| 212 | }) |
| 213 | } |
| 214 | |
| 215 | pub fn edge(&mut self, producer: &str, consumer: &str) { |
| 216 | self.ok(EventKind::AddDependency { |
| 217 | producer: producer.into(), |
| 218 | consumer: consumer.into(), |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | pub fn generation(&self, task: &str) -> u64 { |
| 223 | self.sched.generation(task).unwrap() |
| 224 | } |
| 225 | |
| 226 | /// Mint an interface receipt in the harness store (not delivered). |
| 227 | pub fn mint_interface(&self, task: &str, version: u32) -> ProofClaim { |
| 228 | self.engine |
| 229 | .publish_interface(&InterfaceSubmission { |
| 230 | task_id: task.into(), |
| 231 | generation: self.generation(task), |
| 232 | source_commit: COMMIT.into(), |
| 233 | interface: json!({ "task": task, "api_version": version }), |
| 234 | }) |
| 235 | .unwrap() |
| 236 | .claim() |
| 237 | } |
| 238 | |
| 239 | pub fn publish(&mut self, task: &str, version: u32) -> (ProofClaim, Effects) { |
| 240 | let claim = self.mint_interface(task, version); |
| 241 | let generation = self.generation(task); |
| 242 | let fx = self.ok(EventKind::InterfacePublished { |
| 243 | task: task.into(), |
| 244 | generation, |
| 245 | claim: claim.clone(), |
| 246 | }); |
| 247 | (claim, fx) |
| 248 | } |
| 249 | |
| 250 | /// Run the M1 verified completion for the task's current generation. |
| 251 | pub fn mint_completion(&self, task: &str) -> ProofClaim { |
| 252 | let mut wf = WorkflowState { |
| 253 | id: format!("wf-{task}"), |
| 254 | workflow_type: "engineering_fix".into(), |
| 255 | current_state: "review".into(), |
| 256 | history: Vec::new(), |
| 257 | created_at: "2026-10-15T00:00:00Z".into(), |
| 258 | }; |
| 259 | let rec = self |
| 260 | .engine |
| 261 | .complete_verified_workflow( |
| 262 | &mut wf, |
| 263 | &CompletionSubmission { |
| 264 | task_id: task.into(), |
| 265 | generation: self.generation(task), |
| 266 | source_commit: COMMIT.into(), |
| 267 | artifact: engineering_fix_artifact(task), |
| 268 | }, |
| 269 | ) |
| 270 | .unwrap(); |
| 271 | ProofClaim { |
| 272 | receipt_id: rec.receipt_id, |
| 273 | commit: rec.source_commit, |
| 274 | digest: rec.artifact_digest, |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | pub fn complete(&mut self, task: &str) -> ProofClaim { |
| 279 | let claim = self.mint_completion(task); |
| 280 | let generation = self.generation(task); |
| 281 | self.ok(EventKind::CompletionVerified { |
| 282 | task: task.into(), |
| 283 | generation, |
| 284 | claim: claim.clone(), |
| 285 | }); |
| 286 | claim |
| 287 | } |
| 288 | |
| 289 | pub fn work_done(&mut self, task: &str) -> Effects { |
| 290 | let generation = self.generation(task); |
| 291 | self.ok(EventKind::WorkDone { |
| 292 | task: task.into(), |
| 293 | generation, |
| 294 | result_digest: format!("result-of-{task}"), |
| 295 | }) |
| 296 | } |
| 297 | |
| 298 | pub fn finalize(&mut self, task: &str) -> Effects { |
| 299 | let generation = self.generation(task); |
| 300 | self.ok(EventKind::Finalize { |
| 301 | task: task.into(), |
| 302 | generation, |
| 303 | }) |
| 304 | } |
| 305 | |
| 306 | /// Work done, finalize, then the M1 completion: the task becomes final |
| 307 | /// for its consumers. |
| 308 | pub fn finish(&mut self, task: &str) -> Effects { |
| 309 | self.work_done(task); |
| 310 | self.finalize(task); |
| 311 | let generation = self.generation(task); |
| 312 | let claim = self.mint_completion(task); |
| 313 | self.ok(EventKind::CompletionVerified { |
| 314 | task: task.into(), |
| 315 | generation, |
| 316 | claim, |
| 317 | }) |
| 318 | } |
| 319 | } |