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-runtime / tests / allen_spine_test.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 | //! ALLEN identity gate wired into the compose-home open (compose/bridge.rs `allen_gate`). |
| 17 | //! |
| 18 | //! Refusals exit the process (merge-control condition b), so every case runs |
| 19 | //! the daemon open path in a CHILD process: this test binary re-executes |
| 20 | //! itself on `child_open` with the environment under test, and the parent |
| 21 | //! checks exit code and output. With the stub omega library (no |
| 22 | //! librx_compose.a) the compose home cannot open at all, so every case |
| 23 | //! reports NOT_RUN and returns; with `AIEN_OMEGA_COMPOSE_DIR` set they run. |
| 24 | #[path = "support/home_guard.rs"] |
| 25 | mod home_guard; |
| 26 | |
| 27 | #[path = "../../aien-allen/tests/support/mod.rs"] |
| 28 | mod support; |
| 29 | |
| 30 | use aien_allen::binding::{self, pin_path}; |
| 31 | use aien_allen::{hex, ENV_ADOPT, ENV_SUBJECT, EXIT_REFUSED, NOT_ENGAGED_LINE}; |
| 32 | use aien_omega_compose::{Compose, RootKind, LINKED}; |
| 33 | use aien_runtime::compose::{ComposeBridge, ComposeProposer, Generation}; |
| 34 | use std::path::{Path, PathBuf}; |
| 35 | use std::process::Command; |
| 36 | use std::sync::Arc; |
| 37 | |
| 38 | fn proposer() -> ComposeProposer { |
| 39 | Arc::new(|_p: &str, _l: std::time::Duration| { |
| 40 | Ok(Generation { |
| 41 | text: "filename: NOTES.md\nnote\n".into(), |
| 42 | tokens: 4, |
| 43 | finish_reason: Some("eos".into()), |
| 44 | ..Default::default() |
| 45 | }) |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | /// The child: open the home twice (as two daemon requests would) and note once. |
| 50 | #[test] |
| 51 | fn child_open() { |
| 52 | let Ok(home) = std::env::var("ALLEN_SPINE_CHILD_HOME") else { |
| 53 | return; // not the child |
| 54 | }; |
| 55 | let b = ComposeBridge::new(PathBuf::from(home), proposer(), "test"); |
| 56 | let r = b.note("constraint", "keep changes inside the workspace", &[]); |
| 57 | println!( |
| 58 | "CHILD_NOTE_OK={}", |
| 59 | matches!(r, aien_runtime::control::ControlResponse::ComposeNoted(_)) |
| 60 | ); |
| 61 | if !matches!(r, aien_runtime::control::ControlResponse::ComposeNoted(_)) { |
| 62 | println!("CHILD_NOTE_RESPONSE={r:?}"); |
| 63 | } |
| 64 | let r2 = b.recall(&[], None); |
| 65 | println!( |
| 66 | "CHILD_RECALL_OK={}", |
| 67 | matches!( |
| 68 | r2, |
| 69 | aien_runtime::control::ControlResponse::ComposeRecalled(_) |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | struct Out { |
| 75 | code: i32, |
| 76 | stdout: String, |
| 77 | stderr: String, |
| 78 | } |
| 79 | |
| 80 | fn run_child(home: &Path, env: &[(&str, &str)]) -> Out { |
| 81 | let mut c = Command::new(std::env::current_exe().unwrap()); |
| 82 | c.args(["--exact", "child_open", "--nocapture", "--test-threads=1"]) |
| 83 | .env("ALLEN_SPINE_CHILD_HOME", home) |
| 84 | .env_remove(ENV_SUBJECT) |
| 85 | .env_remove(ENV_ADOPT); |
| 86 | for (k, v) in env { |
| 87 | c.env(k, v); |
| 88 | } |
| 89 | let o = c.output().unwrap(); |
| 90 | Out { |
| 91 | code: o.status.code().unwrap_or(-1), |
| 92 | stdout: String::from_utf8_lossy(&o.stdout).into_owned(), |
| 93 | stderr: String::from_utf8_lossy(&o.stderr).into_owned(), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn skip() -> bool { |
| 98 | if !LINKED { |
| 99 | println!("NOT_RUN: stub omega build (no librx_compose.a); set AIEN_OMEGA_COMPOSE_DIR"); |
| 100 | return true; |
| 101 | } |
| 102 | false |
| 103 | } |
| 104 | |
| 105 | fn names(dir: &Path) -> Vec<String> { |
| 106 | let mut v: Vec<String> = std::fs::read_dir(dir) |
| 107 | .unwrap() |
| 108 | .map(|e| e.unwrap().file_name().to_string_lossy().into_owned()) |
| 109 | .collect(); |
| 110 | v.sort(); |
| 111 | v |
| 112 | } |
| 113 | |
| 114 | /// (machine id, digest of Cortex record 1) of a home the daemon already opened. |
| 115 | fn home_facts(home: &Path) -> ([u8; 32], [u8; 32]) { |
| 116 | let mut name = home.as_os_str().to_owned(); |
| 117 | name.push(".machine-root"); |
| 118 | let root = std::fs::read(PathBuf::from(name)).unwrap(); |
| 119 | let (mut c, info) = Compose::open(home, RootKind::Provisioned, &root, 0xA1E4_0001).unwrap(); |
| 120 | let d = c.record(1).unwrap().digest; |
| 121 | assert_ne!(d, [0u8; 32], "record 1 has a digest"); |
| 122 | (info.machine_id, d) |
| 123 | } |
| 124 | |
| 125 | fn subject_for(dir: &Path, lineage: [u8; 32]) -> (PathBuf, support::Fx) { |
| 126 | let mut f = support::fx("spine"); |
| 127 | f.cortex = lineage; |
| 128 | let c = support::chain(&f, 2); |
| 129 | ( |
| 130 | support::write_head(&dir.join("subject.bin"), c.last().unwrap()), |
| 131 | f, |
| 132 | ) |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn not_engaged_path_is_unchanged() { |
| 137 | if skip() { |
| 138 | return; |
| 139 | } |
| 140 | let t = tempfile::tempdir().unwrap(); |
| 141 | let home = t.path().join("compose"); |
| 142 | let o = run_child(&home, &[]); |
| 143 | assert_eq!(o.code, 0, "{}{}", o.stdout, o.stderr); |
| 144 | assert!(o.stdout.contains("CHILD_NOTE_OK=true") && o.stdout.contains("CHILD_RECALL_OK=true")); |
| 145 | // Exactly one not-engaged line even though the home opened more than once |
| 146 | // per process, and the line is the agreed text. |
| 147 | assert_eq!( |
| 148 | o.stdout.matches(NOT_ENGAGED_LINE).count(), |
| 149 | 1, |
| 150 | "{}", |
| 151 | o.stdout |
| 152 | ); |
| 153 | assert!(!o.stderr.contains("ALLEN"), "{}", o.stderr); |
| 154 | // No ALLEN artifact beside the home: the gate wrote and read nothing. |
| 155 | let beside = names(t.path()); |
| 156 | assert!(beside.iter().all(|n| !n.contains("allen")), "{beside:?}"); |
| 157 | // Reopening gives the same home and the same record count (second run adds |
| 158 | // one more note; nothing else). |
| 159 | let before = names(&home); |
| 160 | let o2 = run_child(&home, &[]); |
| 161 | assert_eq!(o2.code, 0); |
| 162 | assert_eq!(before, names(&home)); |
| 163 | } |
| 164 | |
| 165 | #[test] |
| 166 | fn engaged_without_pin_or_adopt_is_fatal_and_creates_nothing() { |
| 167 | let _home = home_guard::home_slot(); |
| 168 | if skip() { |
| 169 | return; |
| 170 | } |
| 171 | let t = tempfile::tempdir().unwrap(); |
| 172 | let home = t.path().join("compose"); |
| 173 | assert_eq!(run_child(&home, &[]).code, 0); // creates a journal |
| 174 | let (_, lin) = home_facts(&home); |
| 175 | let (subj, _) = subject_for(t.path(), lin); |
| 176 | let o = run_child(&home, &[(ENV_SUBJECT, subj.to_str().unwrap())]); |
| 177 | assert_eq!(o.code, EXIT_REFUSED, "{}{}", o.stdout, o.stderr); |
| 178 | assert!( |
| 179 | o.stderr.contains("FATAL ALLEN refused: PinAbsent"), |
| 180 | "{}", |
| 181 | o.stderr |
| 182 | ); |
| 183 | assert!(!pin_path(&home).exists(), "no silent adoption"); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn engaged_missing_subject_is_fatal_never_minted() { |
| 188 | if skip() { |
| 189 | return; |
| 190 | } |
| 191 | let t = tempfile::tempdir().unwrap(); |
| 192 | let home = t.path().join("compose"); |
| 193 | assert_eq!(run_child(&home, &[]).code, 0); |
| 194 | let missing = t.path().join("none.bin"); |
| 195 | let o = run_child(&home, &[(ENV_SUBJECT, missing.to_str().unwrap())]); |
| 196 | assert_eq!(o.code, EXIT_REFUSED); |
| 197 | assert!( |
| 198 | o.stderr.contains("FATAL ALLEN refused: Absent"), |
| 199 | "{}", |
| 200 | o.stderr |
| 201 | ); |
| 202 | assert!(!missing.exists() && !pin_path(&home).exists()); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn adopt_restart_machine_change_and_foreign_journal() { |
| 207 | let _home = home_guard::home_slot(); |
| 208 | if skip() { |
| 209 | return; |
| 210 | } |
| 211 | let t = tempfile::tempdir().unwrap(); |
| 212 | let home = t.path().join("compose"); |
| 213 | assert_eq!(run_child(&home, &[]).code, 0); |
| 214 | let (machine, lin) = home_facts(&home); |
| 215 | let (subj, f) = subject_for(t.path(), lin); |
| 216 | let s = subj.to_str().unwrap(); |
| 217 | // Wrong adopt value: fatal, no pin. |
| 218 | let wrong = hex(&support::h32("someone else")); |
| 219 | let o = run_child(&home, &[(ENV_SUBJECT, s), (ENV_ADOPT, &wrong)]); |
| 220 | assert_eq!(o.code, EXIT_REFUSED); |
| 221 | assert!(o.stderr.contains("AdoptMismatch"), "{}", o.stderr); |
| 222 | assert!(!pin_path(&home).exists()); |
| 223 | // Operator adoption with the right agent id: engaged, pin written. |
| 224 | let agent = hex(&f.agent); |
| 225 | let o = run_child(&home, &[(ENV_SUBJECT, s), (ENV_ADOPT, &agent)]); |
| 226 | assert_eq!(o.code, 0, "{}{}", o.stdout, o.stderr); |
| 227 | assert!(o.stdout.contains("ADOPTED"), "{}", o.stdout); |
| 228 | let pin = binding::read(&pin_path(&home)).unwrap().unwrap(); |
| 229 | assert_eq!(pin.agent, f.agent); |
| 230 | assert_eq!(pin.machine_id, machine); |
| 231 | // Restart without the adopt variable: same identity, pin unchanged. |
| 232 | let pin_bytes = std::fs::read(pin_path(&home)).unwrap(); |
| 233 | let o = run_child(&home, &[(ENV_SUBJECT, s)]); |
| 234 | assert_eq!(o.code, 0, "{}{}", o.stdout, o.stderr); |
| 235 | assert!(o.stdout.contains(&format!("agent={agent}"))); |
| 236 | assert_eq!(pin_bytes, std::fs::read(pin_path(&home)).unwrap()); |
| 237 | // The adopt variable is refused once a pin exists. |
| 238 | let o = run_child(&home, &[(ENV_SUBJECT, s), (ENV_ADOPT, &agent)]); |
| 239 | assert_eq!(o.code, EXIT_REFUSED); |
| 240 | assert!(o.stderr.contains("AdoptRefusedPinExists"), "{}", o.stderr); |
| 241 | // Corrupt the pin: fatal. |
| 242 | let mut bad = pin_bytes.clone(); |
| 243 | bad[20] ^= 1; |
| 244 | std::fs::write(pin_path(&home), &bad).unwrap(); |
| 245 | let o = run_child(&home, &[(ENV_SUBJECT, s)]); |
| 246 | assert_eq!(o.code, EXIT_REFUSED); |
| 247 | assert!(o.stderr.contains("PinDamaged"), "{}", o.stderr); |
| 248 | // Machine id differs from the pin: fatal, never rebound. |
| 249 | let mut other = pin.clone(); |
| 250 | other.machine_id = [0xEE; 32]; |
| 251 | binding::write_atomic(&pin_path(&home), &other).unwrap(); |
| 252 | let o = run_child(&home, &[(ENV_SUBJECT, s)]); |
| 253 | assert_eq!(o.code, EXIT_REFUSED); |
| 254 | assert!(o.stderr.contains("MachineChanged"), "{}", o.stderr); |
| 255 | assert_eq!( |
| 256 | binding::read(&pin_path(&home)).unwrap().unwrap(), |
| 257 | other, |
| 258 | "not rebound" |
| 259 | ); |
| 260 | // Foreign journal: restore the pin, point at another home's journal. |
| 261 | binding::write_atomic(&pin_path(&home), &pin).unwrap(); |
| 262 | let home2 = t.path().join("compose2"); |
| 263 | assert_eq!(run_child(&home2, &[]).code, 0); |
| 264 | let (_, lin2) = home_facts(&home2); |
| 265 | // Two fresh homes may share record-1 content; only test when they differ. |
| 266 | if lin2 != lin { |
| 267 | let o = run_child(&home2, &[(ENV_SUBJECT, s), (ENV_ADOPT, &agent)]); |
| 268 | assert_eq!(o.code, EXIT_REFUSED); |
| 269 | assert!(o.stderr.contains("LineageMismatch"), "{}", o.stderr); |
| 270 | assert!(!pin_path(&home2).exists()); |
| 271 | } else { |
| 272 | println!("NOT_RUN: foreign-journal row (two fresh journals have the same record 1)"); |
| 273 | } |
| 274 | } |