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 / provisional_journal.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 | //! Headstart M2 (#535): the scheduler journal survives restarts, ignores a |
| 17 | //! torn final line, refuses corruption, and replay never creates a |
| 18 | //! committable task without re-verification. |
| 19 | |
| 20 | mod common; |
| 21 | |
| 22 | use common::{enabled, H}; |
| 23 | use spark_harness::execution::TaskState; |
| 24 | use spark_harness::scheduler::{EventKind, ProvisionalConfig, SchedError, Scheduler}; |
| 25 | use std::fs::{self, OpenOptions}; |
| 26 | use std::io::Write; |
| 27 | |
| 28 | fn scenario(h: &mut H) { |
| 29 | h.register("a", 1, &[]); |
| 30 | h.register("b", 1, &["a"]); |
| 31 | h.register("c", 1, &["b"]); |
| 32 | h.publish("a", 1); |
| 33 | h.publish("b", 1); |
| 34 | h.advance(40); |
| 35 | h.work_done("c"); |
| 36 | h.publish("a", 2); // interface change: b and c revoked, re-dispatched |
| 37 | h.publish("b", 1); |
| 38 | h.finish("a"); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn replay_reproduces_state_and_trace_exactly() { |
| 43 | let mut h = H::journaled(enabled()); |
| 44 | scenario(&mut h); |
| 45 | let snap = h.sched.snapshot(); |
| 46 | let trace = h.sched.timing_trace(); |
| 47 | // Move the clock: replay must use journaled times, not the clock. |
| 48 | h.advance(500); |
| 49 | assert_eq!(h.reopen(enabled()).unwrap(), (0, vec![])); |
| 50 | assert_eq!(h.sched.snapshot(), snap); |
| 51 | assert_eq!(h.sched.timing_trace(), trace); |
| 52 | h.sched.check_invariants().unwrap(); |
| 53 | // The replayed scheduler keeps working and keeps journaling. |
| 54 | h.work_done("b"); |
| 55 | h.finalize("b"); |
| 56 | let snap2 = h.sched.snapshot(); |
| 57 | h.reopen(enabled()).unwrap(); |
| 58 | assert_eq!(h.sched.snapshot(), snap2); |
| 59 | assert_eq!(h.sched.state("b"), Some(TaskState::Committable)); |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn torn_last_line_is_ignored_and_truncated() { |
| 64 | let mut h = H::journaled(enabled()); |
| 65 | scenario(&mut h); |
| 66 | let snap = h.sched.snapshot(); |
| 67 | let path = h.fx.journal(); |
| 68 | let before = fs::read(&path).unwrap(); |
| 69 | // Simulate a crash mid-append of the next event. |
| 70 | let torn = br#"{"seq":999,"id":"ev-torn","at":1,"kind":{"type":"fin"#; |
| 71 | OpenOptions::new() |
| 72 | .append(true) |
| 73 | .open(&path) |
| 74 | .unwrap() |
| 75 | .write_all(torn) |
| 76 | .unwrap(); |
| 77 | assert_eq!(h.reopen(enabled()).unwrap().0, torn.len()); |
| 78 | assert_eq!(h.sched.snapshot(), snap); |
| 79 | assert_eq!(fs::read(&path).unwrap(), before, "torn tail truncated"); |
| 80 | // Next append starts on a clean line and replays. |
| 81 | h.work_done("b"); |
| 82 | let snap2 = h.sched.snapshot(); |
| 83 | assert_eq!(h.reopen(enabled()).unwrap().0, 0); |
| 84 | assert_eq!(h.sched.snapshot(), snap2); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn version_1_journal_is_refused() { |
| 89 | let mut h = H::journaled(enabled()); |
| 90 | scenario(&mut h); |
| 91 | let path = h.fx.journal(); |
| 92 | let text = fs::read_to_string(&path).unwrap(); |
| 93 | assert!(text.lines().next().unwrap().contains("\"version\":2")); |
| 94 | let old = text.replacen("\"version\":2", "\"version\":1", 1); |
| 95 | fs::write(&path, &old).unwrap(); |
| 96 | let r = h.reopen(enabled()); |
| 97 | assert!( |
| 98 | matches!(r, Err(SchedError::Journal(ref m)) if m.contains("header mismatch")), |
| 99 | "{r:?}" |
| 100 | ); |
| 101 | assert_eq!(fs::read_to_string(&path).unwrap(), old); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn refused_journal_is_not_truncated() { |
| 106 | let mut h = H::journaled(enabled()); |
| 107 | scenario(&mut h); |
| 108 | let path = h.fx.journal(); |
| 109 | OpenOptions::new() |
| 110 | .append(true) |
| 111 | .open(&path) |
| 112 | .unwrap() |
| 113 | .write_all(br#"{"seq":999,"id":"ev-torn""#) |
| 114 | .unwrap(); |
| 115 | let before = fs::read(&path).unwrap(); |
| 116 | let other = ProvisionalConfig { |
| 117 | lease_ttl: 7, |
| 118 | ..enabled() |
| 119 | }; |
| 120 | assert!(matches!(h.reopen(other), Err(SchedError::Journal(_)))); |
| 121 | assert_eq!( |
| 122 | fs::read(&path).unwrap(), |
| 123 | before, |
| 124 | "refused journal untouched" |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | #[test] |
| 129 | fn restart_rebases_the_clock_so_dead_leases_expire() { |
| 130 | let mut h = H::journaled(enabled()); |
| 131 | h.advance(1_000_000); // the old process ran for a long time |
| 132 | h.register("a", 2, &[]); |
| 133 | assert_eq!(h.sched.state("a"), Some(TaskState::Provisional)); |
| 134 | let lease_ends = h.sched.task("a").unwrap().lease_expires_at.unwrap(); |
| 135 | // The worker dies with the process. The new process's clock starts at 0. |
| 136 | h.restart(enabled()).unwrap(); |
| 137 | assert_eq!(h.sched.permits_in_use().cpu_in_use, 2); |
| 138 | let last = lease_ends - enabled().lease_ttl; |
| 139 | h.advance(enabled().lease_ttl - 11); |
| 140 | let fx = h.ok(EventKind::Tick); // at lease end - 1 |
| 141 | assert!(fx.aborted.is_empty(), "{fx:?}"); |
| 142 | let fx = h.ok(EventKind::Tick); |
| 143 | assert_eq!(fx.aborted, vec!["a"]); |
| 144 | assert_eq!(h.sched.permits_in_use().running, 0); |
| 145 | assert!(h.sched.timing_trace()["as_of"].as_u64().unwrap() > last); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn forged_finalize_in_the_journal_fails_replay() { |
| 150 | let mut h = H::journaled(enabled()); |
| 151 | h.register("a", 1, &[]); |
| 152 | h.register("b", 1, &["a"]); |
| 153 | h.publish("a", 1); |
| 154 | h.work_done("b"); |
| 155 | // a is not final, so no Finalize for b could have been journaled. |
| 156 | let path = h.fx.journal(); |
| 157 | let text = fs::read_to_string(&path).unwrap(); |
| 158 | let last: serde_json::Value = serde_json::from_str(text.lines().last().unwrap()).unwrap(); |
| 159 | let forged = serde_json::json!({ |
| 160 | "seq": last["seq"].as_u64().unwrap() + 1, |
| 161 | "id": "forged", |
| 162 | "at": last["at"], |
| 163 | "kind": { "type": "finalize", "task": "b", "generation": 1 }, |
| 164 | }); |
| 165 | let mut f = OpenOptions::new().append(true).open(&path).unwrap(); |
| 166 | writeln!(f, "{forged}").unwrap(); |
| 167 | drop(f); |
| 168 | let r = h.reopen(enabled()); |
| 169 | assert!(matches!(r, Err(SchedError::Journal(_))), "{r:?}"); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn corrupt_complete_line_fails_closed() { |
| 174 | let mut h = H::journaled(enabled()); |
| 175 | scenario(&mut h); |
| 176 | let path = h.fx.journal(); |
| 177 | let text = fs::read_to_string(&path).unwrap(); |
| 178 | let mut lines: Vec<&str> = text.lines().collect(); |
| 179 | lines[3] = "{\"not\":\"an event\"}"; |
| 180 | fs::write(&path, format!("{}\n", lines.join("\n"))).unwrap(); |
| 181 | let r = h.reopen(enabled()); |
| 182 | assert!(matches!(r, Err(SchedError::Journal(_))), "{r:?}"); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn reordered_or_duplicated_lines_fail_replay() { |
| 187 | let mut h = H::journaled(enabled()); |
| 188 | scenario(&mut h); |
| 189 | let path = h.fx.journal(); |
| 190 | let text = fs::read_to_string(&path).unwrap(); |
| 191 | let mut lines: Vec<&str> = text.lines().collect(); |
| 192 | let dup = lines[2]; |
| 193 | lines.insert(3, dup); |
| 194 | fs::write(&path, format!("{}\n", lines.join("\n"))).unwrap(); |
| 195 | let r = h.reopen(enabled()); |
| 196 | assert!(matches!(r, Err(SchedError::Journal(_))), "{r:?}"); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn config_mismatch_is_refused() { |
| 201 | let mut h = H::journaled(enabled()); |
| 202 | scenario(&mut h); |
| 203 | let other = ProvisionalConfig { |
| 204 | enabled: false, |
| 205 | ..enabled() |
| 206 | }; |
| 207 | let r = h.reopen(other); |
| 208 | assert!(matches!(r, Err(SchedError::Journal(_))), "{r:?}"); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn journal_has_a_single_writer() { |
| 213 | let h = H::journaled(enabled()); |
| 214 | let second = Scheduler::open( |
| 215 | enabled(), |
| 216 | &h.engine.completion_store, |
| 217 | &h.fx.journal(), |
| 218 | common::clock(&h.ticks), |
| 219 | ); |
| 220 | assert!(matches!(second, Err(SchedError::Journal(_)))); |
| 221 | } |
| 222 | |
| 223 | #[test] |
| 224 | fn replay_never_turns_provisional_work_committable() { |
| 225 | let mut h = H::journaled(enabled()); |
| 226 | h.register("a", 1, &[]); |
| 227 | h.register("b", 1, &["a"]); |
| 228 | h.publish("a", 1); |
| 229 | h.work_done("b"); |
| 230 | // Crash before a is final: b replays as AwaitingFinalProof, not more. |
| 231 | h.reopen(enabled()).unwrap(); |
| 232 | assert_eq!(h.sched.state("b"), Some(TaskState::AwaitingFinalProof)); |
| 233 | assert!(h.sched.may_finalize("b").is_err()); |
| 234 | assert!(h |
| 235 | .send(EventKind::Finalize { |
| 236 | task: "b".into(), |
| 237 | generation: h.generation("b") |
| 238 | }) |
| 239 | .is_err()); |
| 240 | assert!(h.sched.audit().unwrap().is_empty()); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn replayed_committable_task_is_reverified_against_the_store() { |
| 245 | let mut h = H::journaled(enabled()); |
| 246 | h.register("a", 1, &[]); |
| 247 | h.register("b", 1, &["a"]); |
| 248 | h.publish("a", 1); |
| 249 | h.work_done("b"); |
| 250 | h.finish("a"); |
| 251 | h.finalize("b"); |
| 252 | assert!(h.sched.may_finalize("b").is_ok()); |
| 253 | let a_completion = h.sched.task("a").unwrap().completion.clone().unwrap(); |
| 254 | // The store loses a's completion receipt while the scheduler is down. |
| 255 | fs::remove_file( |
| 256 | h.engine |
| 257 | .completion_store |
| 258 | .join("receipts") |
| 259 | .join(format!("{}.json", a_completion.receipt_id)), |
| 260 | ) |
| 261 | .unwrap(); |
| 262 | // Replay is pure, so a and b come back Committable; `open` hands out |
| 263 | // only an unaudited handle, and its audit revokes both before any event |
| 264 | // or dispatch can run on them. |
| 265 | let (_, revoked) = h.reopen(enabled()).unwrap(); |
| 266 | assert_eq!(revoked, vec!["a", "b"]); |
| 267 | assert_eq!(h.sched.state("b"), Some(TaskState::Blocked)); |
| 268 | // A consumer registered after the restart is not dispatched on a. |
| 269 | h.register("c", 1, &["a"]); |
| 270 | assert_eq!(h.sched.state("c"), Some(TaskState::Blocked)); |
| 271 | assert!(h.sched.may_finalize("b").is_err()); |
| 272 | // The revocation itself is journaled. |
| 273 | assert_eq!(h.reopen(enabled()).unwrap().1, Vec::<String>::new()); |
| 274 | assert_eq!(h.sched.state("b"), Some(TaskState::Blocked)); |
| 275 | } |