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 / approved_daemon_ipc_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 | //! #249 C: `ComposeApprovedProposal` over the daemon socket (the production |
| 17 | //! entry), against the real server: an authenticated approval commits; a |
| 18 | //! forged approver is refused with nothing appended; replay records cannot be |
| 19 | //! forged through `ComposeNote` (so a forgery can neither consume nor |
| 20 | //! validate an approval); after a daemon restart over the same compose home |
| 21 | //! every replay is refused and the same approval's retry gets the original |
| 22 | //! result back without a second run. One test per binary (it sets |
| 23 | //! AIEN_COMPOSE_DIR). |
| 24 | #[path = "support/home_guard.rs"] |
| 25 | mod home_guard; |
| 26 | |
| 27 | use aien_inference_abi::MockInferenceBackend; |
| 28 | use aien_kv_cache::create_shared_kv_manager; |
| 29 | use aien_omega_compose::hex; |
| 30 | use aien_runtime::approved::{approved_proposal_sha256, ApprovedProposal}; |
| 31 | use aien_runtime::approved_auth::{desk_key_path, DeskKey}; |
| 32 | use aien_runtime::client::AienRuntimeClient; |
| 33 | use aien_runtime::control::{ControlCommand, ControlResponse}; |
| 34 | use aien_runtime::server::AienRuntimeServer; |
| 35 | use aien_runtime::spine::AienRuntimeSpine; |
| 36 | use aien_scheduler::SchedulerConfig; |
| 37 | use serde_json::json; |
| 38 | use sha2::{Digest, Sha256}; |
| 39 | use std::path::Path; |
| 40 | use std::time::Duration; |
| 41 | |
| 42 | const PATH: &str = "NOTES.md"; |
| 43 | const CONTENT: &str = "approved through the daemon\n"; |
| 44 | |
| 45 | fn signed(dir: &Path, request: &str, approval: &str, approver: &str) -> ApprovedProposal { |
| 46 | let mut p = ApprovedProposal { |
| 47 | request_id: request.into(), |
| 48 | trace_id: format!("trace-{request}"), |
| 49 | approval_id: approval.into(), |
| 50 | approver: approver.into(), |
| 51 | path: PATH.into(), |
| 52 | content: CONTENT.into(), |
| 53 | approved_proposal_sha256: approved_proposal_sha256(PATH, CONTENT), |
| 54 | content_sha256: hex(&Sha256::digest(CONTENT.as_bytes())), |
| 55 | approval_mac: String::new(), |
| 56 | requirements: Some(String::new()), |
| 57 | requirements_mac: String::new(), |
| 58 | requirements_base: None, |
| 59 | }; |
| 60 | DeskKey::load(&desk_key_path(dir)) |
| 61 | .unwrap() |
| 62 | .seal(&mut p, &dir.parent().unwrap().join("ws")); |
| 63 | p |
| 64 | } |
| 65 | |
| 66 | async fn start( |
| 67 | socket: &Path, |
| 68 | ) -> ( |
| 69 | AienRuntimeClient, |
| 70 | tokio::task::JoinHandle<Result<(), String>>, |
| 71 | ) { |
| 72 | let cfg = SchedulerConfig { |
| 73 | max_batch_size: 8, |
| 74 | max_batch_tokens: 1024, |
| 75 | prefill_chunk_size: 64, |
| 76 | watermark_blocks: 4, |
| 77 | chunk_prefill: true, |
| 78 | max_prefill_tokens: 1024, |
| 79 | }; |
| 80 | let spine = AienRuntimeSpine::new(64, cfg, create_shared_kv_manager(256, 16)); |
| 81 | let server = AienRuntimeServer::new(spine, socket); |
| 82 | let handle = tokio::spawn(async move { server.run(MockInferenceBackend::new(1)).await }); |
| 83 | let client = AienRuntimeClient::new(socket); |
| 84 | for _ in 0..200 { |
| 85 | if client.is_alive().await { |
| 86 | return (client, handle); |
| 87 | } |
| 88 | tokio::time::sleep(Duration::from_millis(20)).await; |
| 89 | } |
| 90 | panic!("daemon did not come up"); |
| 91 | } |
| 92 | |
| 93 | async fn submit(c: &AienRuntimeClient, p: &ApprovedProposal, ws: &Path) -> ControlResponse { |
| 94 | c.send_command(ControlCommand::ComposeApprovedProposal { |
| 95 | proposal: p.clone(), |
| 96 | workspace: ws.display().to_string(), |
| 97 | }) |
| 98 | .await |
| 99 | .unwrap() |
| 100 | } |
| 101 | |
| 102 | async fn records(c: &AienRuntimeClient) -> u64 { |
| 103 | match c |
| 104 | .send_command(ControlCommand::ComposeRecall { |
| 105 | ids: vec![], |
| 106 | prefix: None, |
| 107 | }) |
| 108 | .await |
| 109 | .unwrap() |
| 110 | { |
| 111 | ControlResponse::ComposeRecalled(r) => r.records_total, |
| 112 | other => panic!("recall: {other:?}"), |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | async fn note(c: &AienRuntimeClient, kind: &str, text: serde_json::Value) -> ControlResponse { |
| 117 | c.send_command(ControlCommand::ComposeNote { |
| 118 | kind: kind.into(), |
| 119 | text: text.to_string(), |
| 120 | links: vec![], |
| 121 | }) |
| 122 | .await |
| 123 | .unwrap() |
| 124 | } |
| 125 | |
| 126 | fn refusal(r: ControlResponse) -> (String, String) { |
| 127 | match r { |
| 128 | ControlResponse::ComposeApprovedRefused(e) => (e.refused_by.clone(), e.name.clone()), |
| 129 | other => panic!("want a refusal, got {other:?}"), |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 134 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 135 | async fn approved_proposal_over_the_daemon_socket() { |
| 136 | let _home = home_guard::home_slot(); |
| 137 | let tmp = tempfile::tempdir().unwrap(); |
| 138 | let dir = tmp.path().join("compose"); |
| 139 | std::env::set_var("AIEN_COMPOSE_DIR", &dir); |
| 140 | let ws = tmp.path().join("ws"); |
| 141 | std::fs::create_dir_all(&ws).unwrap(); |
| 142 | DeskKey::create(&desk_key_path(&dir)).unwrap(); |
| 143 | let socket = tmp.path().join("runtime.sock"); |
| 144 | let (c, handle) = start(&socket).await; |
| 145 | |
| 146 | // The recall reply says whether the native library is linked (VAC M3a). |
| 147 | match c |
| 148 | .send_command(ControlCommand::ComposeRecall { |
| 149 | ids: vec![], |
| 150 | prefix: None, |
| 151 | }) |
| 152 | .await |
| 153 | .unwrap() |
| 154 | { |
| 155 | ControlResponse::ComposeRecalled(r) => { |
| 156 | assert_eq!(r.compose_native, aien_omega_compose::LINKED); |
| 157 | if aien_omega_compose::LINKED { |
| 158 | assert_eq!(r.omega_sha.len(), 40); |
| 159 | assert!(r.omega_sha.chars().all(|ch| ch.is_ascii_hexdigit())); |
| 160 | assert_eq!(r.omega_sha, aien_omega_compose::EXPECTED_OMEGA_SHA); |
| 161 | } else { |
| 162 | assert!(r.omega_sha.is_empty()); |
| 163 | } |
| 164 | } |
| 165 | other => panic!("recall: {other:?}"), |
| 166 | } |
| 167 | |
| 168 | // Before anything: forged approver (correct bytes, hashes and the MAC of |
| 169 | // the honest approval) is refused, nothing appended. |
| 170 | let good = signed(&dir, "req-1", "appr-1", "interplane-host"); |
| 171 | let mut forged = good.clone(); |
| 172 | forged.approver = "drake".into(); |
| 173 | let out = submit(&c, &forged, &ws).await; |
| 174 | assert_eq!( |
| 175 | refusal(out), |
| 176 | ("PROPOSAL_REFUSED".into(), "Unauthenticated".into()) |
| 177 | ); |
| 178 | if !aien_omega_compose::LINKED { |
| 179 | // Stub build: the authenticated proposal is refused at the claim. |
| 180 | let (by, _) = refusal(submit(&c, &good, &ws).await); |
| 181 | assert_eq!(by, "REPLAY_REFUSED"); |
| 182 | let _ = c.send_command(ControlCommand::Shutdown).await; |
| 183 | return; |
| 184 | } |
| 185 | // The forgery opened the home only to read nothing: no record written. |
| 186 | let n0 = records(&c).await; |
| 187 | |
| 188 | // Replay records cannot be forged through ComposeNote: neither a claim |
| 189 | // (which would consume req-2/appr-2) nor a commit. |
| 190 | for text in [ |
| 191 | json!({"approved_submission": "accepted", "approval_key": "0".repeat(64), |
| 192 | "request_id": "req-2", "approval_id": "appr-2", "trace_id": "trace-req-2", |
| 193 | "executor": {"pid": 1, "start": 1}}), |
| 194 | json!({"approved_submission": "committed", "claim": 1, |
| 195 | "evidence": {"compose_proposal_sha256": "x", "cx_promotion": 1, "cx_evidence": 1, "task": 1}}), |
| 196 | ] { |
| 197 | match note(&c, "effect", text).await { |
| 198 | ControlResponse::Error(e) => assert!(e.contains("approved_submission"), "{e}"), |
| 199 | other => panic!("forged replay record accepted: {other:?}"), |
| 200 | } |
| 201 | } |
| 202 | // Under another kind the field is plain text: written, but never read as a claim. |
| 203 | match note( |
| 204 | &c, |
| 205 | "constraint", |
| 206 | json!({"approved_submission": "accepted", "request_id": "req-2", "approval_id": "appr-2"}), |
| 207 | ) |
| 208 | .await |
| 209 | { |
| 210 | ControlResponse::ComposeNoted(_) => {} |
| 211 | other => panic!("constraint note: {other:?}"), |
| 212 | } |
| 213 | assert_eq!(records(&c).await, n0 + 1); |
| 214 | |
| 215 | // The authenticated approval commits through the production path. |
| 216 | let first = match submit(&c, &good, &ws).await { |
| 217 | ControlResponse::ComposeApprovedResult(r) => *r, |
| 218 | other => panic!("want COMMITTED, got {other:?}"), |
| 219 | }; |
| 220 | assert_eq!(first.state, "COMMITTED"); |
| 221 | assert_eq!( |
| 222 | ( |
| 223 | first.request_id.as_str(), |
| 224 | first.trace_id.as_str(), |
| 225 | first.approval_id.as_str() |
| 226 | ), |
| 227 | ("req-1", "trace-req-1", "appr-1") |
| 228 | ); |
| 229 | let t = first.task.as_ref().unwrap(); |
| 230 | assert!(t.committed && t.aegis_pass_mask & 1 == 1 && t.cx_promotion != 0); |
| 231 | assert_eq!(first.grant_links, vec![t.cx_promotion, t.cx_evidence]); |
| 232 | // The daemon wrote the one grant for this approval, confined to the workspace. |
| 233 | assert!(first.approved_grant.is_some()); |
| 234 | assert_eq!( |
| 235 | first.target.as_deref().map(Path::new), |
| 236 | Some(std::fs::canonicalize(&ws).unwrap().join(PATH).as_path()) |
| 237 | ); |
| 238 | assert!(!ws.join(PATH).exists(), "the command writes nothing"); |
| 239 | // The forged notes did not consume req-2/appr-2. |
| 240 | let second = signed(&dir, "req-2", "appr-2", "interplane-host"); |
| 241 | match submit(&c, &second, &ws).await { |
| 242 | ControlResponse::ComposeApprovedResult(r) => assert_eq!(r.state, "COMMITTED"), |
| 243 | other => panic!("req-2 must commit: {other:?}"), |
| 244 | } |
| 245 | // Replay in the same daemon. |
| 246 | let n1 = records(&c).await; |
| 247 | match submit(&c, &good, &ws).await { |
| 248 | ControlResponse::ComposeApprovedResult(r) => { |
| 249 | assert_eq!(r.state, "ALREADY_COMMITTED"); |
| 250 | assert!(r.task.is_none()); |
| 251 | } |
| 252 | other => panic!("{other:?}"), |
| 253 | } |
| 254 | assert_eq!( |
| 255 | refusal(submit(&c, &signed(&dir, "req-3", "appr-1", "interplane-host"), &ws).await), |
| 256 | ("REPLAY_REFUSED".into(), "AlreadyCommitted".into()) |
| 257 | ); |
| 258 | assert_eq!(records(&c).await, n1); |
| 259 | |
| 260 | // Daemon restart over the same compose home. |
| 261 | let _ = c.send_command(ControlCommand::Shutdown).await; |
| 262 | let _ = tokio::time::timeout(Duration::from_secs(5), handle).await; |
| 263 | let socket2 = tmp.path().join("runtime2.sock"); |
| 264 | let (c2, handle2) = start(&socket2).await; |
| 265 | let n2 = records(&c2).await; |
| 266 | match submit(&c2, &good, &ws).await { |
| 267 | ControlResponse::ComposeApprovedResult(r) => { |
| 268 | assert_eq!(r.state, "ALREADY_COMMITTED"); |
| 269 | assert_eq!(r.compose_proposal_sha256, first.compose_proposal_sha256); |
| 270 | assert_eq!(r.grant_links, first.grant_links); |
| 271 | } |
| 272 | other => panic!("{other:?}"), |
| 273 | } |
| 274 | for p in [ |
| 275 | signed(&dir, "req-1", "appr-9", "interplane-host"), |
| 276 | signed(&dir, "req-9", "appr-1", "interplane-host"), |
| 277 | signed(&dir, "req-2", "appr-2", "another-approver"), |
| 278 | ] { |
| 279 | let (by, name) = refusal(submit(&c2, &p, &ws).await); |
| 280 | assert_eq!( |
| 281 | (by.as_str(), name.as_str()), |
| 282 | ("REPLAY_REFUSED", "AlreadyCommitted") |
| 283 | ); |
| 284 | } |
| 285 | assert_eq!( |
| 286 | records(&c2).await, |
| 287 | n2, |
| 288 | "a replay after restart appended records" |
| 289 | ); |
| 290 | let _ = c2.send_command(ControlCommand::Shutdown).await; |
| 291 | let _ = tokio::time::timeout(Duration::from_secs(5), handle2).await; |
| 292 | } |
| 293 | |
| 294 | /// Compat: a recall reply from an older daemon (no native fields) still reads. |
| 295 | #[test] |
| 296 | fn old_recall_json_without_native_fields_deserializes() { |
| 297 | let old = r#"{"compose_dir":"/d","machine_id":"m","records_total":0,"host":[],"cited":[],"missing":[],"prefix":null,"prefix_digest":null}"#; |
| 298 | let r: aien_runtime::control::ComposeRecallReport = serde_json::from_str(old).unwrap(); |
| 299 | assert!(!r.compose_native); |
| 300 | assert!(r.omega_sha.is_empty()); |
| 301 | } |