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 / provenance_link_live_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 | //! Provenance link (arch#162), live: the real `aien daemon` process with |
| 17 | //! SmolLM2-1.7B on the CPU reference backend, and the real `aien compose` |
| 18 | //! CLI. `propose` -> `authorize` -> `execute`, then every record of the |
| 19 | //! commit is read back from the ledger: each names the generation record of |
| 20 | //! the model's proposal, whose `model_sha256` is the sha256 of the weights |
| 21 | //! file the daemon loaded (hashed here, independently, from the file). |
| 22 | //! |
| 23 | //! IGNORED, like recovery_matrix_test: it needs a prebuilt compose-linked CPU |
| 24 | //! binary and the model (minutes of CPU inference), so it cannot run in the |
| 25 | //! default suite. Build once, then run: |
| 26 | //! |
| 27 | //! ```text |
| 28 | //! AIEN_OMEGA_COMPOSE_LIB=<librx_compose.a> cargo build --release -p aien-cli |
| 29 | //! AIEN_BIN=target/release/aien-cli cargo test -p aien-cli --test provenance_link_live_test \ |
| 30 | //! -- --ignored --nocapture |
| 31 | //! ``` |
| 32 | //! |
| 33 | //! `AIEN_PROPOSER_MODEL` overrides the model directory |
| 34 | //! (default `$HOME/models/SmolLM2-1.7B-Instruct-31b70e2e869a`). The daemon |
| 35 | //! must report the CPU reference backend or the test stops (no GPU use). |
| 36 | use serde_json::Value; |
| 37 | use sha2::{Digest, Sha256}; |
| 38 | use std::path::{Path, PathBuf}; |
| 39 | use std::process::{Child, Command, Stdio}; |
| 40 | use std::time::Duration; |
| 41 | |
| 42 | fn file_sha256(p: &Path) -> String { |
| 43 | use std::io::Read; |
| 44 | let mut f = std::fs::File::open(p).unwrap(); |
| 45 | let mut h = Sha256::new(); |
| 46 | let mut buf = vec![0u8; 1 << 20]; |
| 47 | loop { |
| 48 | let n = f.read(&mut buf).unwrap(); |
| 49 | if n == 0 { |
| 50 | break; |
| 51 | } |
| 52 | h.update(&buf[..n]); |
| 53 | } |
| 54 | aien_omega_compose::hex(&h.finalize()) |
| 55 | } |
| 56 | |
| 57 | struct Rig { |
| 58 | bin: PathBuf, |
| 59 | root: PathBuf, |
| 60 | _tmp: tempfile::TempDir, |
| 61 | } |
| 62 | |
| 63 | impl Rig { |
| 64 | fn env(&self, c: &mut Command) { |
| 65 | c.current_dir(&self.root) |
| 66 | .env("HOME", self.root.join("home")) |
| 67 | .env("NO_COLOR", "1") |
| 68 | .env("AIEN_RUNTIME_SOCK", self.root.join("s")) |
| 69 | .env("AIEN_RUNTIME_STATE_DIR", self.root.join("state")) |
| 70 | .env("AIEN_COMPOSE_DIR", self.root.join("compose")) |
| 71 | .env("AIEN_PROVENANCE_DIR", self.root.join("prov")) |
| 72 | .env("AIEN_REQUIRE_CHECKPOINT", "1"); |
| 73 | for k in [ |
| 74 | "AIEN_OMEGA_DIR", |
| 75 | "AIEN_OMEGA_GPU_LIB", |
| 76 | "AIEN_REQUIRE_BLACKWELL", |
| 77 | "AIEN_GPU_BACKEND", |
| 78 | "AIEN_MODEL_DIR", |
| 79 | "AIEN_ALLEN_SUBJECT", |
| 80 | "AIEN_ALLEN_ADOPT", |
| 81 | "AIEN_COMPOSE_AUTHORIZE_REQUIRES_DESK", |
| 82 | ] { |
| 83 | c.env_remove(k); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn cli(&self, args: &[&str]) -> Value { |
| 88 | let mut c = Command::new(&self.bin); |
| 89 | c.arg("compose").args(args); |
| 90 | self.env(&mut c); |
| 91 | let o = c.output().expect("run aien compose"); |
| 92 | let out = String::from_utf8_lossy(&o.stdout).to_string(); |
| 93 | out.lines() |
| 94 | .rev() |
| 95 | .find_map(|l| serde_json::from_str::<Value>(l).ok()) |
| 96 | .unwrap_or_else(|| { |
| 97 | panic!( |
| 98 | "no JSON from {args:?}: {out} {}", |
| 99 | String::from_utf8_lossy(&o.stderr) |
| 100 | ) |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | /// Text of one ledger record, digest-verified. |
| 105 | fn record(&self, id: u64) -> Value { |
| 106 | let v = self.cli(&["recall", "--ids", &id.to_string()]); |
| 107 | let c = &v["recall"]["cited"][0]; |
| 108 | assert_eq!(c["verified"], true, "record {id}: {v}"); |
| 109 | serde_json::from_str(c["text"].as_str().expect("record text")).unwrap() |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | struct Daemon(Child); |
| 114 | |
| 115 | impl Drop for Daemon { |
| 116 | fn drop(&mut self) { |
| 117 | let _ = self.0.kill(); |
| 118 | let _ = self.0.wait(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | #[ignore = "live: needs AIEN_BIN (compose-linked CPU build) and SmolLM2 on disk; minutes of CPU"] |
| 124 | fn a_live_model_commit_names_its_generation_record_and_weights_digest() { |
| 125 | let bin = PathBuf::from( |
| 126 | std::env::var("AIEN_BIN").expect("AIEN_BIN: the compose-linked CPU aien-cli"), |
| 127 | ); |
| 128 | let model_dir = std::env::var("AIEN_PROPOSER_MODEL") |
| 129 | .map(PathBuf::from) |
| 130 | .unwrap_or_else(|_| { |
| 131 | PathBuf::from(std::env::var("HOME").unwrap()) |
| 132 | .join("models/SmolLM2-1.7B-Instruct-31b70e2e869a") |
| 133 | }); |
| 134 | let weights = model_dir.join("model.safetensors"); |
| 135 | let tmp = tempfile::Builder::new() |
| 136 | .prefix("pl-") |
| 137 | .tempdir_in("/tmp") |
| 138 | .unwrap(); |
| 139 | let root = std::fs::canonicalize(tmp.path()).unwrap(); |
| 140 | for d in ["ws", "state", "home", "prov"] { |
| 141 | std::fs::create_dir_all(root.join(d)).unwrap(); |
| 142 | } |
| 143 | // sc#328: the daemon requires the approval desk by default. |
| 144 | aien_runtime::approved_auth::DeskKey::create(&aien_runtime::approved_auth::desk_key_path( |
| 145 | &root.join("compose"), |
| 146 | )) |
| 147 | .unwrap(); |
| 148 | let rig = Rig { |
| 149 | bin: bin.clone(), |
| 150 | root: root.clone(), |
| 151 | _tmp: tmp, |
| 152 | }; |
| 153 | let log = root.join("daemon.log"); |
| 154 | let f = std::fs::File::create(&log).unwrap(); |
| 155 | let mut c = Command::new(&bin); |
| 156 | c.arg("daemon") |
| 157 | .env("AIEN_MODEL_PATH", &weights) |
| 158 | .env("AIEN_TOKENIZER_PATH", model_dir.join("tokenizer.json")) |
| 159 | .env("AIEN_COMPOSE_EDIT_BUDGET_MS", "599000") |
| 160 | .env("AIEN_COMPOSE_DOC_BUDGET_MS", "599000") |
| 161 | .stdin(Stdio::null()) |
| 162 | .stdout(f.try_clone().unwrap()) |
| 163 | .stderr(f); |
| 164 | rig.env(&mut c); |
| 165 | let mut daemon = Daemon(c.spawn().expect("spawn daemon")); |
| 166 | let mut up = false; |
| 167 | for _ in 0..2400 { |
| 168 | std::thread::sleep(Duration::from_millis(500)); |
| 169 | assert!( |
| 170 | daemon.0.try_wait().unwrap().is_none(), |
| 171 | "daemon exited: {}", |
| 172 | std::fs::read_to_string(&log).unwrap_or_default() |
| 173 | ); |
| 174 | let t = std::fs::read_to_string(&log).unwrap_or_default(); |
| 175 | if root.join("s").exists() && t.contains("Replay reconcile:") { |
| 176 | let backend = t.lines().find(|l| l.contains("Backend:")).unwrap_or(""); |
| 177 | assert!( |
| 178 | backend.contains("CPU-reference"), |
| 179 | "not the CPU backend: {backend}" |
| 180 | ); |
| 181 | up = true; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | assert!(up, "daemon did not come up in 20 min"); |
| 186 | |
| 187 | let ws = root.join("ws"); |
| 188 | let report_file = root.join("propose.json"); |
| 189 | let propose = rig.cli(&[ |
| 190 | "propose", |
| 191 | "--goal", |
| 192 | "Write garden.md in the workspace, a garden plan.", |
| 193 | "--workspace", |
| 194 | ws.to_str().unwrap(), |
| 195 | ]); |
| 196 | std::fs::write(&report_file, propose.to_string()).unwrap(); |
| 197 | let report = &propose["report"]; |
| 198 | assert_eq!( |
| 199 | report["committed"], true, |
| 200 | "the model's proposal did not commit: {propose}" |
| 201 | ); |
| 202 | let gen_id = report["generation_record"] |
| 203 | .as_u64() |
| 204 | .expect("report names the generation record"); |
| 205 | let commit = report["compose_commit"].as_u64().unwrap(); |
| 206 | let auth = rig.cli(&[ |
| 207 | "authorize", |
| 208 | "--report", |
| 209 | report_file.to_str().unwrap(), |
| 210 | "--workspace", |
| 211 | ws.to_str().unwrap(), |
| 212 | "--approver", |
| 213 | "drake", |
| 214 | "--desk", |
| 215 | "1", |
| 216 | ]); |
| 217 | assert_eq!(auth["ok"], true, "{auth}"); |
| 218 | let grant = auth["authorization"]["id"].as_u64().unwrap(); |
| 219 | let exec = rig.cli(&[ |
| 220 | "execute", |
| 221 | "--report", |
| 222 | report_file.to_str().unwrap(), |
| 223 | "--workspace", |
| 224 | ws.to_str().unwrap(), |
| 225 | "--authorization", |
| 226 | &grant.to_string(), |
| 227 | ]); |
| 228 | assert_eq!(exec["state"], "DONE", "{exec}"); |
| 229 | let effects = rig.cli(&["effects"]); |
| 230 | let intents = effects["ledger"]["intents"].as_array().expect("intents"); |
| 231 | assert_eq!(intents.len(), 1); |
| 232 | let intent = intents[0]["intent"].as_u64().unwrap(); |
| 233 | let ack = intents[0]["state_record"].as_u64().unwrap(); |
| 234 | |
| 235 | let want_model = file_sha256(&weights); |
| 236 | let want_tok = file_sha256(&model_dir.join("tokenizer.json")); |
| 237 | for (name, id) in [ |
| 238 | ("compose_commit", commit), |
| 239 | ("grant", grant), |
| 240 | ("intent", intent), |
| 241 | ("ack", ack), |
| 242 | ] { |
| 243 | let r = rig.record(id); |
| 244 | assert_eq!(r["provenance"]["generation_record"], gen_id, "{name}: {r}"); |
| 245 | assert_eq!(r["provenance"]["allen_agent"], "none", "{name}: {r}"); |
| 246 | } |
| 247 | let g = rig.record(gen_id); |
| 248 | assert_eq!(g["generation"], 1); |
| 249 | assert_eq!(g["origin"], "compose_proposal"); |
| 250 | assert_eq!( |
| 251 | g["model_sha256"], want_model, |
| 252 | "the digest of the loaded weights file" |
| 253 | ); |
| 254 | assert_eq!(g["tokenizer_sha256"], want_tok); |
| 255 | assert_eq!(g["task"], report["task"]); |
| 256 | let attempt = report["proposal_attempts"] |
| 257 | .as_array() |
| 258 | .unwrap() |
| 259 | .iter() |
| 260 | .find(|a| a["outcome"] == "parsed") |
| 261 | .expect("the parsed attempt"); |
| 262 | assert_eq!(g["attempt"], attempt["attempt"]); |
| 263 | assert_eq!(g["prompt_ids_sha256"], attempt["prompt_ids_sha256"]); |
| 264 | assert_eq!(g["output_text_sha256"], attempt["text_sha256"]); |
| 265 | println!( |
| 266 | "LIVE_PROVENANCE generation_record={gen_id} model_sha256={want_model} commit={commit} grant={grant} intent={intent} ack={ack}" |
| 267 | ); |
| 268 | } |