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-cli / tests / desk_required_startup_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 | //! sc#328 (Drake decision b, 2026-10-08): the approval desk is REQUIRED by |
| 17 | //! default. A real `aien daemon` process, started four ways: |
| 18 | //! D1 no desk key, switch unset: startup is refused before the socket |
| 19 | //! exists, the message names the key path and how to create it. |
| 20 | //! D2 the dev opt-out (`AIEN_COMPOSE_AUTHORIZE_REQUIRES_DESK=0`) in a |
| 21 | //! strict run (no AIEN_DEV_FALLBACK, as release qualification runs): |
| 22 | //! startup is refused. |
| 23 | //! D3 the dev opt-out in a dev run: the daemon serves and says so loudly |
| 24 | //! at startup ("Authorize MAC: OFF, DEV OPT-OUT"). |
| 25 | //! D4 a desk key created by `aien compose desk-key --create`: the daemon |
| 26 | //! serves with "Authorize MAC: on". |
| 27 | //! A helper whose check fails still stops its daemon (a `Daemon` guard |
| 28 | //! kills and reaps it on drop), so a failing run leaves nothing running. |
| 29 | //! No model and no composition library are needed (host CPU only); these run |
| 30 | //! in every build. |
| 31 | use std::os::unix::process::CommandExt; |
| 32 | use std::path::{Path, PathBuf}; |
| 33 | use std::process::{Child, Command, Stdio}; |
| 34 | use std::time::Duration; |
| 35 | |
| 36 | const SWITCH: &str = "AIEN_COMPOSE_AUTHORIZE_REQUIRES_DESK"; |
| 37 | |
| 38 | struct Rig { |
| 39 | _tmp: tempfile::TempDir, |
| 40 | root: PathBuf, |
| 41 | } |
| 42 | |
| 43 | impl Rig { |
| 44 | fn new() -> Rig { |
| 45 | // Short path: unix socket paths are limited to 108 bytes. |
| 46 | let tmp = tempfile::Builder::new() |
| 47 | .prefix("dk-") |
| 48 | .tempdir_in("/tmp") |
| 49 | .unwrap(); |
| 50 | let root = std::fs::canonicalize(tmp.path()).unwrap(); |
| 51 | for d in ["state", "home", "prov"] { |
| 52 | std::fs::create_dir_all(root.join(d)).unwrap(); |
| 53 | } |
| 54 | Rig { _tmp: tmp, root } |
| 55 | } |
| 56 | fn sock(&self) -> PathBuf { |
| 57 | self.root.join("s") |
| 58 | } |
| 59 | fn compose(&self) -> PathBuf { |
| 60 | self.root.join("compose") |
| 61 | } |
| 62 | fn log(&self) -> PathBuf { |
| 63 | self.root.join("daemon.log") |
| 64 | } |
| 65 | fn cmd(&self, dev: bool, switch: Option<&str>) -> Command { |
| 66 | let mut c = Command::new(env!("CARGO_BIN_EXE_aien-cli")); |
| 67 | c.current_dir(&self.root) |
| 68 | .env("HOME", self.root.join("home")) |
| 69 | .env("NO_COLOR", "1") |
| 70 | .env("AIEN_RUNTIME_SOCK", self.sock()) |
| 71 | .env("AIEN_RUNTIME_STATE_DIR", self.root.join("state")) |
| 72 | .env("AIEN_COMPOSE_DIR", self.compose()) |
| 73 | .env("AIEN_PROVENANCE_DIR", self.root.join("prov")) |
| 74 | .env("AIEN_REQUIRE_CHECKPOINT", "0"); |
| 75 | for k in [ |
| 76 | "AIEN_OMEGA_DIR", |
| 77 | "AIEN_OMEGA_GPU_LIB", |
| 78 | "AIEN_REQUIRE_BLACKWELL", |
| 79 | "AIEN_GPU_BACKEND", |
| 80 | "AIEN_MODEL_PATH", |
| 81 | "AIEN_TOKENIZER_PATH", |
| 82 | "AIEN_MODEL_DIR", |
| 83 | "AIEN_DEV_FALLBACK", |
| 84 | SWITCH, |
| 85 | ] { |
| 86 | c.env_remove(k); |
| 87 | } |
| 88 | if dev { |
| 89 | c.env("AIEN_DEV_FALLBACK", "1"); |
| 90 | } |
| 91 | if let Some(v) = switch { |
| 92 | c.env(SWITCH, v); |
| 93 | } |
| 94 | c |
| 95 | } |
| 96 | fn spawn(&self, dev: bool, switch: Option<&str>) -> Daemon { |
| 97 | let f = std::fs::File::create(self.log()).unwrap(); |
| 98 | let mut c = self.cmd(dev, switch); |
| 99 | c.arg("daemon") |
| 100 | .process_group(0) |
| 101 | .stdin(Stdio::null()) |
| 102 | .stdout(f.try_clone().unwrap()) |
| 103 | .stderr(f); |
| 104 | Daemon(c.spawn().expect("spawn daemon")) |
| 105 | } |
| 106 | fn text(&self) -> String { |
| 107 | std::fs::read_to_string(self.log()).unwrap_or_default() |
| 108 | } |
| 109 | |
| 110 | /// The daemon must exit on its own, refused, without ever binding the socket. |
| 111 | fn refused(&self, dev: bool, switch: Option<&str>) -> String { |
| 112 | let mut d = self.spawn(dev, switch); |
| 113 | for _ in 0..1200 { |
| 114 | if let Some(st) = d.0.try_wait().unwrap() { |
| 115 | let text = self.text(); |
| 116 | assert!( |
| 117 | !st.success(), |
| 118 | "a refused start must exit non-zero ({st:?}): {text}" |
| 119 | ); |
| 120 | assert!( |
| 121 | !self.sock().exists(), |
| 122 | "socket bound before the refusal: {text}" |
| 123 | ); |
| 124 | return text; |
| 125 | } |
| 126 | assert!( |
| 127 | !self.sock().exists(), |
| 128 | "the daemon bound its socket: {}", |
| 129 | self.text() |
| 130 | ); |
| 131 | std::thread::sleep(Duration::from_millis(50)); |
| 132 | } |
| 133 | panic!("the daemon did not refuse within 60 s: {}", self.text()); |
| 134 | } |
| 135 | |
| 136 | /// The daemon must serve; returns its startup log, then stops it. |
| 137 | fn serves(&self, dev: bool, switch: Option<&str>) -> String { |
| 138 | let mut d = self.spawn(dev, switch); |
| 139 | for _ in 0..1200 { |
| 140 | std::thread::sleep(Duration::from_millis(50)); |
| 141 | if let Some(st) = d.0.try_wait().unwrap() { |
| 142 | panic!("daemon exited early ({st:?}): {}", self.text()); |
| 143 | } |
| 144 | let text = self.text(); |
| 145 | if self.sock().exists() && text.contains("Authorize MAC:") { |
| 146 | return text; |
| 147 | } |
| 148 | } |
| 149 | panic!("the daemon did not come up in 60 s: {}", self.text()); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /// A spawned daemon, killed and reaped when dropped: on return and on a |
| 154 | /// failed assert alike, so a failing check never leaves it running. |
| 155 | struct Daemon(Child); |
| 156 | |
| 157 | impl Drop for Daemon { |
| 158 | fn drop(&mut self) { |
| 159 | let _ = self.0.kill(); |
| 160 | let _ = self.0.wait(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn key_path(compose: &Path) -> String { |
| 165 | compose.join("approval-desk.key").display().to_string() |
| 166 | } |
| 167 | |
| 168 | #[test] |
| 169 | fn d1_no_desk_key_refuses_startup() { |
| 170 | let r = Rig::new(); |
| 171 | for dev in [false, true] { |
| 172 | let text = r.refused(dev, None); |
| 173 | assert!(text.contains(&key_path(&r.compose())), "dev={dev}: {text}"); |
| 174 | assert!(text.contains("desk-key --create"), "dev={dev}: {text}"); |
| 175 | } |
| 176 | // "1" is the same requirement, spelled out. |
| 177 | let text = r.refused(false, Some("1")); |
| 178 | assert!(text.contains(&key_path(&r.compose())), "{text}"); |
| 179 | assert!( |
| 180 | !Path::new(&key_path(&r.compose())).exists(), |
| 181 | "startup must never mint a key" |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn d2_dev_opt_out_is_refused_in_a_strict_run() { |
| 187 | let r = Rig::new(); |
| 188 | if aien_inference_abi::strict::DEV_FALLBACK { |
| 189 | // A --features dev-fallback build is never strict: there the opt-out serves. |
| 190 | let text = r.serves(false, Some("0")); |
| 191 | assert!(text.contains("Authorize MAC: OFF, DEV OPT-OUT"), "{text}"); |
| 192 | return; |
| 193 | } |
| 194 | let text = r.refused(false, Some("0")); |
| 195 | assert!(text.contains(SWITCH), "{text}"); |
| 196 | assert!(text.contains("release qualification"), "{text}"); |
| 197 | assert!(text.contains("AIEN_DEV_FALLBACK"), "{text}"); |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn d3_dev_opt_out_starts_and_says_so() { |
| 202 | let r = Rig::new(); |
| 203 | let text = r.serves(true, Some("0")); |
| 204 | let line = text |
| 205 | .lines() |
| 206 | .find(|l| l.contains("Authorize MAC:")) |
| 207 | .unwrap_or_default(); |
| 208 | assert!(line.contains("Authorize MAC: OFF, DEV OPT-OUT"), "{line}"); |
| 209 | assert!( |
| 210 | line.contains("not valid for release qualification"), |
| 211 | "{line}" |
| 212 | ); |
| 213 | assert!( |
| 214 | !Path::new(&key_path(&r.compose())).exists(), |
| 215 | "startup must never mint a key" |
| 216 | ); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn d4_with_the_desk_key_the_daemon_serves_with_the_mac_on() { |
| 221 | let r = Rig::new(); |
| 222 | let o = r |
| 223 | .cmd(false, None) |
| 224 | .args(["compose", "desk-key", "--create", "1"]) |
| 225 | .output() |
| 226 | .unwrap(); |
| 227 | assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stdout)); |
| 228 | assert!(Path::new(&key_path(&r.compose())).exists()); |
| 229 | let text = r.serves(false, None); |
| 230 | let line = text |
| 231 | .lines() |
| 232 | .find(|l| l.contains("Authorize MAC:")) |
| 233 | .unwrap_or_default(); |
| 234 | assert!(line.starts_with("Authorize MAC: on"), "{line}"); |
| 235 | } |
| 236 | |
| 237 | /// Daemons still running on this socket (their environment names it). |
| 238 | fn daemons_on(sock: &Path) -> Vec<String> { |
| 239 | let want = format!("AIEN_RUNTIME_SOCK={}", sock.display()); |
| 240 | let mut pids = Vec::new(); |
| 241 | for e in std::fs::read_dir("/proc").unwrap().flatten() { |
| 242 | let pid = e.file_name().to_string_lossy().to_string(); |
| 243 | if !pid.chars().all(|c| c.is_ascii_digit()) { |
| 244 | continue; |
| 245 | } |
| 246 | if let Ok(env) = std::fs::read(e.path().join("environ")) { |
| 247 | if env.split(|b| *b == 0).any(|v| v == want.as_bytes()) { |
| 248 | pids.push(pid); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | pids |
| 253 | } |
| 254 | |
| 255 | /// A helper that fails must not leave its daemon running: here `refused` |
| 256 | /// meets a daemon that serves (the dev opt-out), so it panics. |
| 257 | #[test] |
| 258 | fn a_failed_check_stops_its_daemon() { |
| 259 | let r = Rig::new(); |
| 260 | let failed = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { |
| 261 | r.refused(true, Some("0")); |
| 262 | })); |
| 263 | assert!(failed.is_err(), "the daemon was expected to serve"); |
| 264 | let left = daemons_on(&r.sock()); |
| 265 | for pid in &left { |
| 266 | let _ = Command::new("kill").args(["-KILL", pid]).status(); |
| 267 | } |
| 268 | assert!( |
| 269 | left.is_empty(), |
| 270 | "the failed check left daemon(s) {left:?} running" |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | /// sc#414 defect 1: `aien start` itself (not just the daemon it spawns) refuses |
| 275 | /// without a desk key, in plain words, naming the exact command, and never |
| 276 | /// leaves a daemon or a socket behind. |
| 277 | #[test] |
| 278 | fn s1_aien_start_without_desk_key_refuses_with_the_command() { |
| 279 | let rig = Rig::new(); |
| 280 | let out = rig |
| 281 | .cmd(false, None) |
| 282 | .arg("start") |
| 283 | .stdin(Stdio::null()) |
| 284 | .output() |
| 285 | .expect("run aien start"); |
| 286 | let all = format!( |
| 287 | "{}{}", |
| 288 | String::from_utf8_lossy(&out.stdout), |
| 289 | String::from_utf8_lossy(&out.stderr) |
| 290 | ); |
| 291 | assert!(!out.status.success(), "start must refuse: {all}"); |
| 292 | assert!(all.contains("AIEN will not start yet"), "{all}"); |
| 293 | assert!(all.contains("aien compose desk-key --create 1"), "{all}"); |
| 294 | assert!(!rig.sock().exists(), "no daemon may be left: {all}"); |
| 295 | } |
| 296 | |
| 297 | /// sc#414 defect 2: `aien harness` refuses with a non-zero exit; it never |
| 298 | /// reports a pass, and never calls the non-existent `spark-harness check`. |
| 299 | #[test] |
| 300 | fn s2_aien_harness_refuses_not_shipped() { |
| 301 | let rig = Rig::new(); |
| 302 | let out = rig.cmd(false, None).arg("harness").output().unwrap(); |
| 303 | let all = format!( |
| 304 | "{}{}", |
| 305 | String::from_utf8_lossy(&out.stdout), |
| 306 | String::from_utf8_lossy(&out.stderr) |
| 307 | ); |
| 308 | assert!(!out.status.success(), "{all}"); |
| 309 | assert!(all.contains("not shipped in this release"), "{all}"); |
| 310 | } |