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 / src / scheduler / unit_tests.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 | //! Guards that no event sequence reaches (defense in depth), tested on a |
| 17 | //! hand-built state. |
| 18 | |
| 19 | use super::core::{Binding, BindingLevel, TaskEntry}; |
| 20 | use super::{ProvisionalConfig, ResourceRequest, SchedError, Scheduler}; |
| 21 | use crate::execution::TaskState; |
| 22 | use crate::readiness::{OperationClass, ProofClaim}; |
| 23 | use std::path::Path; |
| 24 | |
| 25 | fn req() -> ResourceRequest { |
| 26 | ResourceRequest { |
| 27 | cpu_slots: 1, |
| 28 | memory_bytes: 1, |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Producer `p` at generation 2; consumer `c` in `state`, still bound to |
| 33 | /// generation 1. Revocation always clears such a binding, so only a |
| 34 | /// corrupted state can carry one. |
| 35 | fn stale_binding(state: TaskState) -> Scheduler { |
| 36 | let cfg = ProvisionalConfig { |
| 37 | enabled: true, |
| 38 | ..ProvisionalConfig::default() |
| 39 | }; |
| 40 | let store = Path::new("/nonexistent/spark-harness-m2-unit-store"); |
| 41 | let mut s = Scheduler::new(cfg, store, Box::new(|| 100)).unwrap(); |
| 42 | s.core.graph.add_node("p").unwrap(); |
| 43 | s.core.graph.add_node("c").unwrap(); |
| 44 | s.core.graph.add_edge("p", "c").unwrap(); |
| 45 | let mut p = TaskEntry::new(req(), 0); |
| 46 | p.generation = 2; |
| 47 | let mut c = TaskEntry::new(req(), 0); |
| 48 | c.state = state; |
| 49 | c.lease_expires_at = Some(1_000); |
| 50 | c.bindings.insert( |
| 51 | "p".to_string(), |
| 52 | Binding { |
| 53 | generation: 1, |
| 54 | level: BindingLevel::Interface, |
| 55 | claim: ProofClaim { |
| 56 | receipt_id: "r".to_string(), |
| 57 | commit: "c".to_string(), |
| 58 | digest: "d".to_string(), |
| 59 | }, |
| 60 | }, |
| 61 | ); |
| 62 | s.core.tasks.insert("p".to_string(), p); |
| 63 | s.core.tasks.insert("c".to_string(), c); |
| 64 | s |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn binding_to_an_old_producer_generation_is_refused() { |
| 69 | let s = stale_binding(TaskState::Provisional); |
| 70 | let r = s.may_start("c", OperationClass::ReadOnlyAnalysis); |
| 71 | assert!(matches!(r, Err(SchedError::StaleBinding { .. })), "{r:?}"); |
| 72 | let s = stale_binding(TaskState::AwaitingFinalProof); |
| 73 | let r = s.may_finalize("c"); |
| 74 | assert!(matches!(r, Err(SchedError::StaleBinding { .. })), "{r:?}"); |
| 75 | } |
| 76 | |
| 77 | /// Measures the whole-state copy `deliver` makes per event, next to the |
| 78 | /// full in-memory deliver (copy + apply + dispatch scan) and one journal |
| 79 | /// append with fsync. Run with |
| 80 | /// `cargo test --release -p spark-harness --lib -- --ignored --nocapture` |
| 81 | /// (task count from `M2_COST_TASKS`, default 2000). |
| 82 | #[test] |
| 83 | #[ignore] |
| 84 | fn deliver_cost_is_small_next_to_the_journal_fsync() { |
| 85 | use super::{Event, EventKind}; |
| 86 | use std::time::Instant; |
| 87 | let tasks: usize = std::env::var("M2_COST_TASKS") |
| 88 | .ok() |
| 89 | .and_then(|v| v.parse().ok()) |
| 90 | .unwrap_or(2_000); |
| 91 | const N: u32 = 200; |
| 92 | let cfg = ProvisionalConfig { |
| 93 | enabled: true, |
| 94 | ..ProvisionalConfig::default() |
| 95 | }; |
| 96 | let store = Path::new("/nonexistent/spark-harness-m2-unit-store"); |
| 97 | let mut s = Scheduler::new(cfg, store, Box::new(|| 1)).unwrap(); |
| 98 | for i in 0..tasks { |
| 99 | let producers = if i == 0 { |
| 100 | vec![] |
| 101 | } else { |
| 102 | vec!["t0".to_string()] |
| 103 | }; |
| 104 | let kind = EventKind::RegisterTask { |
| 105 | task: format!("t{i}"), |
| 106 | resources: req(), |
| 107 | producers, |
| 108 | }; |
| 109 | s.deliver(Event { |
| 110 | id: format!("reg-{i}"), |
| 111 | kind, |
| 112 | }) |
| 113 | .unwrap(); |
| 114 | } |
| 115 | let start = Instant::now(); |
| 116 | for _ in 0..N { |
| 117 | std::hint::black_box(s.core.clone()); |
| 118 | } |
| 119 | let copy = start.elapsed() / N; |
| 120 | let start = Instant::now(); |
| 121 | for i in 0..N { |
| 122 | let ev = Event { |
| 123 | id: format!("tick-{i}"), |
| 124 | kind: EventKind::Tick, |
| 125 | }; |
| 126 | s.deliver(ev).unwrap(); |
| 127 | } |
| 128 | let deliver = start.elapsed() / N; |
| 129 | let dir = std::env::temp_dir().join(format!("spark-harness-m2-cost-{}", uuid::Uuid::new_v4())); |
| 130 | let header = serde_json::json!({ "format": "cost" }); |
| 131 | let mut j = crate::journal::Journal::open::<serde_json::Value>(&dir.join("j.jsonl"), &header) |
| 132 | .unwrap() |
| 133 | .journal; |
| 134 | let line = serde_json::json!({ "seq": 1, "id": "tick", "at": 1, "kind": { "type": "tick" } }); |
| 135 | let start = Instant::now(); |
| 136 | for _ in 0..N { |
| 137 | j.append(&line).unwrap(); |
| 138 | } |
| 139 | let fsync = start.elapsed() / N; |
| 140 | drop(j); |
| 141 | let _ = std::fs::remove_dir_all(&dir); |
| 142 | println!( |
| 143 | "M2_DELIVER_COST tasks={tasks} state_copy={copy:?} in_memory_deliver={deliver:?} \ |
| 144 | journal_append_fsync={fsync:?}" |
| 145 | ); |
| 146 | } |