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_crash_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 B crash boundaries of the approved-proposal path, with a real process |
| 17 | //! crash: a child process (this test binary, re-run on the hidden helper |
| 18 | //! test) submits one approved proposal and aborts at a fault-hold point; the |
| 19 | //! parent then opens the same compose home as a restarted daemon would |
| 20 | //! (start-up replay reconcile) and retries. |
| 21 | //! |
| 22 | //! approved_after_claim boundary 1: accepted, dies before compose |
| 23 | //! approved_after_compose boundary 2: the World committed, dies before the |
| 24 | //! replay commit record |
| 25 | //! approved_after_commit boundary 3: the replay claim committed, dies |
| 26 | //! before the daemon's grant record |
| 27 | //! |
| 28 | //! Needs the `fault-hold` feature (test builds only): |
| 29 | //! `cargo test -p aien-runtime --features fault-hold --test approved_crash_test`. |
| 30 | #![cfg(feature = "fault-hold")] |
| 31 | use aien_omega_compose::hex; |
| 32 | use aien_runtime::approved::{ApprovedProposal, ProposerHook}; |
| 33 | use aien_runtime::approved_auth::{desk_key_path, DeskKey}; |
| 34 | use aien_runtime::approved_replay; |
| 35 | use aien_runtime::compose::{ComposeBridge, ComposeProposer}; |
| 36 | use aien_runtime::control::{ComposeRecallReport, ControlResponse}; |
| 37 | use aien_runtime::effects::Ledger; |
| 38 | use sha2::{Digest, Sha256}; |
| 39 | use std::path::Path; |
| 40 | use std::sync::Arc; |
| 41 | |
| 42 | const PATH: &str = "NOTES.md"; |
| 43 | const CONTENT: &str = "approved by the INTERPLANE desk\n"; |
| 44 | |
| 45 | fn never() -> ComposeProposer { |
| 46 | Arc::new(|_: &str, _: std::time::Duration| panic!("model proposer ran on the hook path")) |
| 47 | } |
| 48 | |
| 49 | fn sha(b: &[u8]) -> String { |
| 50 | hex(&Sha256::digest(b)) |
| 51 | } |
| 52 | |
| 53 | fn proposal(b: &ComposeBridge) -> ApprovedProposal { |
| 54 | let mut p = ApprovedProposal { |
| 55 | request_id: "req-crash".into(), |
| 56 | trace_id: "trace-crash".into(), |
| 57 | approval_id: "appr-crash".into(), |
| 58 | approver: "interplane-host".into(), |
| 59 | path: PATH.into(), |
| 60 | content: CONTENT.into(), |
| 61 | approved_proposal_sha256: aien_runtime::approved::approved_proposal_sha256(PATH, CONTENT), |
| 62 | content_sha256: sha(CONTENT.as_bytes()), |
| 63 | approval_mac: String::new(), |
| 64 | requirements: Some(String::new()), |
| 65 | requirements_mac: String::new(), |
| 66 | requirements_base: None, |
| 67 | }; |
| 68 | DeskKey::load(&desk_key_path(b.dir())) |
| 69 | .unwrap() |
| 70 | .seal(&mut p, &b.dir().parent().unwrap().join("ws")); |
| 71 | p |
| 72 | } |
| 73 | |
| 74 | fn recalled(b: &ComposeBridge) -> ComposeRecallReport { |
| 75 | match b.recall(&[], None) { |
| 76 | ControlResponse::ComposeRecalled(r) => *r, |
| 77 | other => panic!("recall: {other:?}"), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Hidden helper: the child. Does nothing unless the parent set the env. |
| 82 | #[test] |
| 83 | #[ignore] |
| 84 | fn crash_child() { |
| 85 | let (Ok(dir), Ok(ws)) = (std::env::var("CRASH_DIR"), std::env::var("CRASH_WS")) else { |
| 86 | return; |
| 87 | }; |
| 88 | let b = Arc::new(ComposeBridge::new(dir.into(), never(), "t")); |
| 89 | let hook = ProposerHook::new(b.clone()); |
| 90 | let out = hook.submit(&proposal(&b), &ws); |
| 91 | // Reaching here means the hold point did not fire. |
| 92 | panic!("child was not stopped at its hold point: {out:?}"); |
| 93 | } |
| 94 | |
| 95 | fn run_child(hold: &str, dir: &Path, ws: &Path) { |
| 96 | let st = std::process::Command::new(std::env::current_exe().unwrap()) |
| 97 | .args([ |
| 98 | "--exact", |
| 99 | "crash_child", |
| 100 | "--ignored", |
| 101 | "--nocapture", |
| 102 | "--test-threads=1", |
| 103 | ]) |
| 104 | .env("AIEN_FAULT_HOLD", hold) |
| 105 | .env("CRASH_DIR", dir) |
| 106 | .env("CRASH_WS", ws) |
| 107 | .stdout(std::process::Stdio::null()) |
| 108 | .stderr(std::process::Stdio::null()) |
| 109 | .status() |
| 110 | .unwrap(); |
| 111 | use std::os::unix::process::ExitStatusExt; |
| 112 | assert_eq!( |
| 113 | st.signal(), |
| 114 | Some(libc_sigabrt()), |
| 115 | "child must abort at {hold}: {st:?}" |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | fn libc_sigabrt() -> i32 { |
| 120 | 6 |
| 121 | } |
| 122 | |
| 123 | /// Replay claims (`accepted` records) in the journal. |
| 124 | fn claims(r: &ComposeRecallReport) -> usize { |
| 125 | r.host |
| 126 | .iter() |
| 127 | .filter(|h| { |
| 128 | h.text |
| 129 | .as_deref() |
| 130 | .is_some_and(|t| t.contains("\"approved_submission\":\"accepted\"")) |
| 131 | }) |
| 132 | .count() |
| 133 | } |
| 134 | |
| 135 | static HOMES: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 136 | |
| 137 | fn boundary(hold: &str, want_reconciled: &str, want_refusal: Option<&str>) { |
| 138 | let _turn = HOMES.lock().unwrap_or_else(|e| e.into_inner()); |
| 139 | let tmp = tempfile::tempdir().unwrap(); |
| 140 | let ws = tmp.path().join("ws"); |
| 141 | std::fs::create_dir_all(&ws).unwrap(); |
| 142 | let dir = tmp.path().join("compose"); |
| 143 | DeskKey::create(&desk_key_path(&dir)).unwrap(); |
| 144 | run_child(hold, &dir, &ws); |
| 145 | |
| 146 | // The restarted daemon: a fresh bridge, start-up reconcile first. |
| 147 | let b = Arc::new(ComposeBridge::new(dir.clone(), never(), "t")); |
| 148 | let line = approved_replay::reconcile_at_start(&b).unwrap(); |
| 149 | assert!(line.contains(want_reconciled), "{line}"); |
| 150 | let before = recalled(&b); |
| 151 | assert_eq!(claims(&before), 1, "the child claimed once"); |
| 152 | // Any compose run appends goal, candidate, evidence and promotion |
| 153 | // records: an unchanged total proves nothing ran again. |
| 154 | let total = before.records_total; |
| 155 | |
| 156 | // Retry of the same approval and replays under either id: refused, never run. |
| 157 | let hook = ProposerHook::new(b.clone()); |
| 158 | let p = proposal(&b); |
| 159 | match (hook.submit(&p, ws.to_str().unwrap()), want_refusal) { |
| 160 | (Err(e), Some(want)) => { |
| 161 | assert_eq!(e.name, want, "{e}"); |
| 162 | assert_eq!(e.refused_by, "REPLAY_REFUSED"); |
| 163 | } |
| 164 | // Committed before the crash: the same approval gets the original |
| 165 | // result back, and no grant (the daemon never wrote one). |
| 166 | (Ok(r), None) => { |
| 167 | assert_eq!(r.state, "ALREADY_COMMITTED"); |
| 168 | assert!(r.task.is_none() && r.approved_grant.is_none()); |
| 169 | } |
| 170 | (other, want) => panic!("want {want:?}, got {other:?}"), |
| 171 | } |
| 172 | let want_refusal = want_refusal.unwrap_or("AlreadyCommitted"); |
| 173 | let mut q = p.clone(); |
| 174 | q.request_id = "req-crash-2".into(); |
| 175 | DeskKey::load(&desk_key_path(&dir)) |
| 176 | .unwrap() |
| 177 | .seal(&mut q, &ws); |
| 178 | assert_eq!( |
| 179 | hook.submit(&q, ws.to_str().unwrap()).unwrap_err().name, |
| 180 | want_refusal |
| 181 | ); |
| 182 | let after = recalled(&b); |
| 183 | assert_eq!( |
| 184 | after.records_total, total, |
| 185 | "a refused retry appended records" |
| 186 | ); |
| 187 | // No effect: no grant, no intent, nothing on disk. |
| 188 | let l = Ledger::from_records(&after.host).unwrap(); |
| 189 | assert!(l.grants.is_empty() && l.intents.is_empty()); |
| 190 | assert!(!ws.join(PATH).exists()); |
| 191 | } |
| 192 | |
| 193 | /// Boundary 1: accepted, the process dies before compose. After restart the |
| 194 | /// claim is NOT_EXECUTED and the approval stays consumed (a new approval is |
| 195 | /// needed); nothing ran. |
| 196 | #[test] |
| 197 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 198 | fn crash_after_claim_is_consumed_never_run() { |
| 199 | if !aien_omega_compose::LINKED { |
| 200 | return; |
| 201 | } |
| 202 | boundary( |
| 203 | "approved_after_claim", |
| 204 | "1 -> NOT_EXECUTED", |
| 205 | Some("AlreadyConsumed"), |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | /// Boundary 2: the World committed, the process dies before the replay commit |
| 210 | /// record. After restart the claim is UNCERTAIN (recorded), every retry is |
| 211 | /// refused: no second World commit, no effect. |
| 212 | #[test] |
| 213 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 214 | fn crash_after_compose_is_uncertain_never_rerun() { |
| 215 | if !aien_omega_compose::LINKED { |
| 216 | return; |
| 217 | } |
| 218 | boundary( |
| 219 | "approved_after_compose", |
| 220 | "1 -> UNCERTAIN", |
| 221 | Some("Uncertain"), |
| 222 | ); |
| 223 | } |
| 224 | |
| 225 | /// Boundary 3: the replay claim committed, the process dies before the |
| 226 | /// daemon's grant is written. After restart the approval is spent and no |
| 227 | /// grant exists, so no effect is possible: a new approval is needed. |
| 228 | #[test] |
| 229 | #[cfg_attr(not(compose_linked), ignore = "needs librx_compose.a: stub build")] |
| 230 | fn crash_before_grant_leaves_no_effect_possible() { |
| 231 | if !aien_omega_compose::LINKED { |
| 232 | return; |
| 233 | } |
| 234 | boundary("approved_after_commit", "", None); |
| 235 | } |