Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-runtime/tests/effects_test.rs # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-runtime / tests / effects_test.rs` 309 lines ยท 11285 bytes Raw Blame History 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 //! NEXT-PHASE-2: durable effect intents, ack, reconcile, stop/resume and 17 //! revoke through the real compose home (ACCEPTANCE-v2 section 2). 18 //! 19 //! With librx_compose.a linked this drives the bridge exactly as the daemon 20 //! does (no socket); in a stub build it only checks the refusal. 21 #[path = "support/home_guard.rs"] 22 mod home_guard; 23 24 use aien_omega_compose::hex; 25 use aien_runtime::compose::{ComposeBridge, ComposeProposer, Generation}; 26 use aien_runtime::control::{ 27 ComposeControlReport, ComposeNoteReport, ComposeReconcileReport, ControlResponse, 28 ReconcileDeclare, 29 }; 30 use aien_runtime::effects::{self, IntentRequest, Ledger}; 31 use serde_json::{json, Value}; 32 use sha2::{Digest, Sha256}; 33 use std::path::Path; 34 use std::sync::Arc; 35 36 const PROPOSAL: &str = "filename: NOTES.md\nkeep every change inside the workspace\n"; 37 const CONTENT: &str = "keep every change inside the workspace\n"; 38 39 fn proposer() -> ComposeProposer { 40 Arc::new(|_prompt: &str, _limit: std::time::Duration| { 41 Ok(Generation { 42 text: PROPOSAL.to_string(), 43 tokens: 8, 44 finish_reason: Some("eos".into()), 45 ..Default::default() 46 }) 47 }) 48 } 49 50 fn sha(b: &[u8]) -> String { 51 hex(&Sha256::digest(b)) 52 } 53 54 fn noted(r: ControlResponse) -> ComposeNoteReport { 55 match r { 56 ControlResponse::ComposeNoted(n) => n, 57 other => panic!("expected ComposeNoted, got {other:?}"), 58 } 59 } 60 61 fn refused(r: ControlResponse, name: &str) { 62 match r { 63 ControlResponse::Error(e) => assert!( 64 e.starts_with(&format!("EFFECT_REFUSED {name}:")), 65 "want {name}, got {e}" 66 ), 67 other => panic!("want EFFECT_REFUSED {name}, got {other:?}"), 68 } 69 } 70 71 fn reconciled(r: ControlResponse) -> ComposeReconcileReport { 72 match r { 73 ControlResponse::ComposeReconciled(r) => *r, 74 other => panic!("expected ComposeReconciled, got {other:?}"), 75 } 76 } 77 78 fn controlled(r: ControlResponse) -> ComposeControlReport { 79 match r { 80 ControlResponse::ComposeControlled(r) => *r, 81 other => panic!("expected ComposeControlled, got {other:?}"), 82 } 83 } 84 85 fn ledger(b: &ComposeBridge) -> Ledger { 86 match b.recall(&[], None) { 87 ControlResponse::ComposeRecalled(r) => Ledger::from_records(&r.host).unwrap(), 88 other => panic!("recall: {other:?}"), 89 } 90 } 91 92 fn state(b: &ComposeBridge, record: u64) -> String { 93 match b.recall(&[record], None) { 94 ControlResponse::ComposeRecalled(r) => { 95 let t = r.cited[0].text.clone().unwrap(); 96 serde_json::from_str::(&t).unwrap()["state"] 97 .as_str() 98 .unwrap() 99 .to_string() 100 } 101 other => panic!("recall: {other:?}"), 102 } 103 } 104 105 struct Fixture { 106 promotion: std::cell::Cell, 107 target: String, 108 psha: String, 109 csha: String, 110 } 111 112 impl Fixture { 113 fn grant(&self, b: &ComposeBridge) -> u64 { 114 // sovereign-core #261: the daemon mints the grant from its own commit record. 115 noted(effects::mint_grant( 116 b, 117 &effects::MintRequest { 118 cx_promotion: self.promotion.get(), 119 proposal_sha256: self.psha.clone(), 120 workspace: Path::new(&self.target) 121 .parent() 122 .unwrap() 123 .display() 124 .to_string(), 125 approver: "drake".into(), 126 constraints: vec![], 127 }, 128 )) 129 .id 130 } 131 132 /// `alive`: this test process is the executor; otherwise a dead one. 133 fn req(&self, a: u64, alive: bool) -> IntentRequest { 134 let (pid, start) = if alive { 135 effects::self_executor() 136 } else { 137 (0, 0) 138 }; 139 IntentRequest { 140 authorization: a, 141 proposal_sha256: self.psha.clone(), 142 path: "NOTES.md".into(), 143 target: self.target.clone(), 144 content_sha256: self.csha.clone(), 145 executor_pid: pid, 146 executor_start: start, 147 } 148 } 149 } 150 151 #[test] 152 fn effect_intents_are_at_most_once_or_unresolved() { 153 let _home = home_guard::home_slot(); 154 let tmp = tempfile::tempdir().unwrap(); 155 let home = tmp.path().join("compose"); 156 let ws = tmp.path().join("ws"); 157 std::fs::create_dir_all(&ws).unwrap(); 158 let ws = std::fs::canonicalize(&ws).unwrap(); 159 let b = ComposeBridge::new(home.clone(), proposer(), "test:fixed-proposer") 160 .with_authorize_requires_desk(false); 161 let run = b.run_task("write the note", ws.to_str().unwrap()); 162 if !aien_omega_compose::LINKED { 163 assert!(matches!(run, ControlResponse::Error(_))); 164 return; 165 } 166 let report = match run { 167 ControlResponse::ComposeTaskResult(r) => *r, 168 other => panic!("run: {other:?}"), 169 }; 170 assert!(report.committed, "{report:?}"); 171 let f = Fixture { 172 promotion: std::cell::Cell::new(report.cx_promotion), 173 target: ws.join("NOTES.md").display().to_string(), 174 psha: sha(PROPOSAL.as_bytes()), 175 csha: sha(CONTENT.as_bytes()), 176 }; 177 178 // 1. Grant, intent, write, ack: DONE once; the grant is then spent. 179 let a1 = f.grant(&b); 180 let i1 = noted(effects::open_intent(&b, &f.req(a1, true))).id; 181 refused( 182 effects::open_intent(&b, &f.req(a1, true)), 183 "ReconciliationRequired", 184 ); 185 let r = reconciled(effects::reconcile(&b, None, "test")); 186 assert_eq!(r.outcomes.len(), 1); 187 assert!( 188 r.outcomes[0].record.is_none(), 189 "live executor is never decided" 190 ); 191 std::fs::write(&f.target, CONTENT).unwrap(); 192 let k1 = noted(effects::ack(&b, i1, &json!({"bytes": CONTENT.len()}))); 193 assert_eq!(state(&b, k1.id), "DONE"); 194 refused(effects::open_intent(&b, &f.req(a1, true)), "AlreadySpent"); 195 assert!(matches!( 196 effects::ack(&b, i1, &json!({})), 197 ControlResponse::Error(_) 198 )); 199 let c = controlled(effects::control(&b, "revoke", "drake", Some(a1))); 200 assert_eq!((c.revoked, c.recorded), (Some(false), None)); 201 202 // sovereign-core #261: a grant that settled DONE closes its commit (one 203 // committed proposal, at most one DONE effect); new content needs a new compose. 204 refused_mint(&f, &b); 205 // 2. Executor died after the intent, before the write: NOT_DONE. 206 // (Target removed first: a grant against the content itself would read 207 // DONE, ACCEPTANCE-v2 2.2 checks the content digest first. It also lets the 208 // second compose create NOTES.md again: since #288 a new-document compose 209 // never overwrites an existing file.) 210 std::fs::remove_file(&f.target).unwrap(); 211 match b.run_task("write the note again", ws.to_str().unwrap()) { 212 ControlResponse::ComposeTaskResult(r) => { 213 assert!(r.committed, "second compose: {r:?}"); 214 f.promotion.set(r.cx_promotion) 215 } 216 other => panic!("second compose: {other:?}"), 217 } 218 let a2 = f.grant(&b); 219 let i2 = noted(effects::open_intent(&b, &f.req(a2, false))).id; 220 let r = reconciled(effects::reconcile(&b, None, "test")); 221 let o = r.outcomes.iter().find(|o| o.intent == i2).unwrap(); 222 assert_eq!(o.state, "NOT_DONE"); 223 assert!(o.record.is_some()); 224 refused(effects::open_intent(&b, &f.req(a2, true)), "AlreadySpent"); 225 226 // 3. Ambiguous world: UNRESOLVED once per digest, then an operator decides. 227 let a3 = f.grant(&b); 228 let i3 = noted(effects::open_intent(&b, &f.req(a3, false))).id; 229 std::fs::write(&f.target, b"someone else\n").unwrap(); 230 let r = reconciled(effects::reconcile(&b, None, "test")); 231 assert_eq!(r.outcomes[0].state, "UNRESOLVED"); 232 assert!(r.outcomes[0].record.is_some()); 233 let r = reconciled(effects::reconcile(&b, None, "test")); 234 assert!( 235 r.outcomes[0].record.is_none(), 236 "same digest is not recorded twice" 237 ); 238 refused( 239 effects::open_intent(&b, &f.req(a3, true)), 240 "ReconciliationRequired", 241 ); 242 let d = ReconcileDeclare { 243 intent: i3, 244 state: "not_done".into(), 245 approver: "drake".into(), 246 }; 247 let r = reconciled(effects::reconcile(&b, Some(&d), "test")); 248 assert_eq!( 249 (r.outcomes[0].state.as_str(), r.outcomes[0].note.as_str()), 250 ("NOT_DONE", "operator:drake") 251 ); 252 assert!(matches!( 253 effects::reconcile(&b, Some(&d), "test"), 254 ControlResponse::Error(_) 255 )); 256 assert_eq!(std::fs::read(&f.target).unwrap(), b"someone else\n"); 257 258 // 4. Stale world, revoke, stop and resume. 259 let a4 = f.grant(&b); 260 std::fs::write(&f.target, b"changed after the grant\n").unwrap(); 261 refused(effects::open_intent(&b, &f.req(a4, true)), "Stale"); 262 let a5 = f.grant(&b); 263 let c = controlled(effects::control(&b, "revoke", "drake", Some(a5))); 264 assert_eq!(c.revoked, Some(true)); 265 refused(effects::open_intent(&b, &f.req(a5, true)), "Revoked"); 266 let a6 = f.grant(&b); 267 controlled(effects::control(&b, "stop", "drake", None)); 268 refused(effects::open_intent(&b, &f.req(a6, true)), "Stopped"); 269 assert!(matches!( 270 effects::control(&b, "resume", " ", None), 271 ControlResponse::Error(_) 272 )); 273 controlled(effects::control(&b, "resume", "drake", None)); 274 refused(effects::open_intent(&b, &f.req(a6, true)), "Stale"); 275 let a7 = f.grant(&b); 276 let i7 = noted(effects::open_intent(&b, &f.req(a7, true))).id; 277 std::fs::write(&f.target, CONTENT).unwrap(); 278 assert_eq!( 279 state(&b, noted(effects::ack(&b, i7, &json!({}))).id), 280 "DONE" 281 ); 282 283 // 5. Restart: the ledger is rebuilt from the journal, nothing is open, 284 // and the forged-record guard holds. 285 let before = ledger(&b).view(); 286 drop(b); 287 let b = ComposeBridge::new(home, proposer(), "test:fixed-proposer") 288 .with_authorize_requires_desk(false); 289 let line = effects::reconcile_at_start(&b); 290 assert!(line.starts_with("Reconcile: checked 0"), "{line}"); 291 assert_eq!(ledger(&b).view(), before); 292 assert!(effects::check_reserved_note("effect", r#"{"phase":"ack","intent":1}"#).is_err()); 293 } 294 295 fn refused_mint(f: &Fixture, b: &ComposeBridge) { 296 match effects::mint_grant( 297 b, 298 &effects::MintRequest { 299 cx_promotion: f.promotion.get(), 300 proposal_sha256: f.psha.clone(), 301 workspace: Path::new(&f.target).parent().unwrap().display().to_string(), 302 approver: "drake".into(), 303 constraints: vec![], 304 }, 305 ) { 306 ControlResponse::Error(e) => assert!(e.contains("settled DONE"), "{e}"), 307 other => panic!("ATTACK SUCCEEDED (second grant after DONE): {other:?}"), 308 } 309 }