aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-explain / tests / rsi_and_cli.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 | //! Cross-repo fixture from spark-rsi (the RSI KV-cache cycle) and the CLI. |
| 17 | //! |
| 18 | //! The fixture is produced by spark-rsi `tests/test_explain_evidence_graph.rs`; both |
| 19 | //! repos pin the same sha256 so a contract drift on either side fails a test. |
| 20 | |
| 21 | use aienos_explain::bundle::{Role, UncertaintyKind}; |
| 22 | use aienos_explain::contract::{ |
| 23 | EdgeDoc, EvidenceGraph, NodeDoc, NodeKind, Relation, SourceRef, Status, |
| 24 | }; |
| 25 | use aienos_explain::{build_bundle, render, ExplanationRequest, Limits, Mode}; |
| 26 | use std::process::Command; |
| 27 | |
| 28 | const FIXTURE: &[u8] = include_bytes!("fixtures/rsi_kv_cycle.evidence.json"); |
| 29 | const FIXTURE_SHA256: &str = "7e9f1c1e09210fb3158cd001ecb6207d4d5c3fbd30996448c35c61dc900fc07d"; |
| 30 | const TARGET: &str = "rsi:conclusion:cycle-kv-0001"; |
| 31 | |
| 32 | fn hexs(b: &[u8]) -> String { |
| 33 | b.iter().map(|x| format!("{x:02x}")).collect() |
| 34 | } |
| 35 | |
| 36 | fn fixture() -> EvidenceGraph { |
| 37 | EvidenceGraph::from_json(FIXTURE).expect("fixture parses") |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn rsi_fixture_is_pinned() { |
| 42 | assert_eq!( |
| 43 | hexs(&aienos_crypto::sha256::hash(FIXTURE)), |
| 44 | FIXTURE_SHA256, |
| 45 | "fixture changed; regenerate in spark-rsi and update both pins" |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn rsi_cycle_explains_in_causal_order() { |
| 51 | let g = fixture(); |
| 52 | assert_eq!(g.producer, "spark-rsi/explain"); |
| 53 | let b = build_bundle(&g, TARGET, Limits::default()).expect("bundle"); |
| 54 | assert_eq!(b.conclusion().node_id, TARGET); |
| 55 | // Role order along the path never goes backwards for this cycle. |
| 56 | let roles: Vec<Role> = b.steps.iter().map(|s| s.role).collect(); |
| 57 | let first = |r: Role| roles.iter().position(|x| *x == r).expect("role present"); |
| 58 | assert!(first(Role::Observed) < first(Role::Analysis)); |
| 59 | assert!(first(Role::Analysis) < first(Role::Hypothesis)); |
| 60 | assert!(first(Role::Hypothesis) < first(Role::Prediction)); |
| 61 | assert!(first(Role::Prediction) < first(Role::Falsification)); |
| 62 | assert!(first(Role::Falsification) < first(Role::Evaluation)); |
| 63 | assert!(first(Role::Evaluation) < first(Role::Decision)); |
| 64 | assert_eq!(*roles.last().unwrap(), Role::Conclusion); |
| 65 | |
| 66 | let kinds: Vec<UncertaintyKind> = b.uncertainties.iter().map(|u| u.kind).collect(); |
| 67 | // Candidate -> hypothesis link is not recorded by RSI, so it stays visible. |
| 68 | assert!(kinds.contains(&UncertaintyKind::MissingLink)); |
| 69 | // The receipt does not evaluate the hypothesis directly. |
| 70 | assert!(kinds.contains(&UncertaintyKind::HypothesisNotFalsified)); |
| 71 | // Gate flags are outside the receipt digest: reported, status unknown. |
| 72 | assert!(kinds.contains(&UncertaintyKind::Unknown)); |
| 73 | assert_eq!(b.alternatives.len(), 3); |
| 74 | |
| 75 | let receipt = b |
| 76 | .steps |
| 77 | .iter() |
| 78 | .find(|s| s.kind == NodeKind::EvaluationReceipt && s.source.digest.is_some()) |
| 79 | .expect("receipt step"); |
| 80 | assert_eq!(receipt.status, Status::VerifiedFact); |
| 81 | let summary = b |
| 82 | .steps |
| 83 | .iter() |
| 84 | .find(|s| s.kind == NodeKind::EvaluationReceipt && s.source.digest.is_none()) |
| 85 | .expect("receipt summary step"); |
| 86 | assert_eq!(summary.status, Status::Unclassified); |
| 87 | // The hypothesis is never presented as fact. |
| 88 | let h = b |
| 89 | .steps |
| 90 | .iter() |
| 91 | .find(|s| s.kind == NodeKind::Hypothesis) |
| 92 | .expect("hypothesis"); |
| 93 | assert_eq!(h.status, Status::Hypothesis); |
| 94 | |
| 95 | let text = render(&b, &ExplanationRequest::new(Mode::StepByStep)); |
| 96 | for heading in [ |
| 97 | "Observed", |
| 98 | "System analysis", |
| 99 | "Hypothesis", |
| 100 | "Prediction", |
| 101 | "Falsification", |
| 102 | "Evaluation", |
| 103 | "Decision", |
| 104 | "Conclusion", |
| 105 | ] { |
| 106 | assert!(text.contains(heading), "missing heading {heading}:\n{text}"); |
| 107 | } |
| 108 | // Determinism across runs. |
| 109 | let again = build_bundle(&fixture(), TARGET, Limits::default()).unwrap(); |
| 110 | assert_eq!(b.bundle_digest, again.bundle_digest); |
| 111 | } |
| 112 | |
| 113 | fn node(id: &str, status: Status, parents: &[(&str, Relation)]) -> NodeDoc { |
| 114 | NodeDoc { |
| 115 | id: id.into(), |
| 116 | kind: NodeKind::CortexRecord.code(), |
| 117 | status: status.code(), |
| 118 | statement: format!("statement of {id}"), |
| 119 | source: SourceRef { |
| 120 | system: "test".into(), |
| 121 | artifact: "Node".into(), |
| 122 | reference: id.into(), |
| 123 | digest: None, |
| 124 | }, |
| 125 | confidence_permille: None, |
| 126 | confidence_basis: None, |
| 127 | recorded_at_utc: None, |
| 128 | parents: parents |
| 129 | .iter() |
| 130 | .map(|(to, r)| EdgeDoc { |
| 131 | to: (*to).into(), |
| 132 | relation: r.code(), |
| 133 | }) |
| 134 | .collect(), |
| 135 | measurements: Vec::new(), |
| 136 | missing: Vec::new(), |
| 137 | notes: Vec::new(), |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn contradiction_disclosed_in_both_directions() { |
| 143 | let mut g = EvidenceGraph::new("test"); |
| 144 | g.nodes = vec![ |
| 145 | node("t:obs", Status::DirectObservation, &[]), |
| 146 | // An ordinary record (not a Contradiction record) that contradicts the path. |
| 147 | node( |
| 148 | "t:rival", |
| 149 | Status::DirectObservation, |
| 150 | &[("t:obs", Relation::Contradicts)], |
| 151 | ), |
| 152 | // The target itself contradicts something off the path. |
| 153 | node( |
| 154 | "t:claim", |
| 155 | Status::Inference, |
| 156 | &[ |
| 157 | ("t:obs", Relation::SupportedBy), |
| 158 | ("t:other", Relation::Contradicts), |
| 159 | ], |
| 160 | ), |
| 161 | node("t:other", Status::DirectObservation, &[]), |
| 162 | ]; |
| 163 | let b = build_bundle(&g, "t:claim", Limits::default()).unwrap(); |
| 164 | let ids: Vec<&str> = b |
| 165 | .contradictions |
| 166 | .iter() |
| 167 | .map(|c| c.node_id.as_str()) |
| 168 | .collect(); |
| 169 | assert!(ids.contains(&"t:rival"), "{ids:?}"); |
| 170 | assert!(ids.contains(&"t:claim"), "{ids:?}"); |
| 171 | // Contradicting nodes are disclosed, never walked as support. |
| 172 | assert!(b |
| 173 | .steps |
| 174 | .iter() |
| 175 | .all(|s| s.node_id != "t:rival" && s.node_id != "t:other")); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn cli_explains_fixture() { |
| 180 | let path = concat!( |
| 181 | env!("CARGO_MANIFEST_DIR"), |
| 182 | "/tests/fixtures/rsi_kv_cycle.evidence.json" |
| 183 | ); |
| 184 | let bin = env!("CARGO_BIN_EXE_aienos-explain"); |
| 185 | let out = Command::new(bin) |
| 186 | .args(["explain", "--graph", path, "--artifact", TARGET, "--why"]) |
| 187 | .output() |
| 188 | .expect("run cli"); |
| 189 | assert!( |
| 190 | out.status.success(), |
| 191 | "{}", |
| 192 | String::from_utf8_lossy(&out.stderr) |
| 193 | ); |
| 194 | let text = String::from_utf8(out.stdout).unwrap(); |
| 195 | assert!(text.contains("Conclusion")); |
| 196 | let json = Command::new(bin) |
| 197 | .args(["explain", "--graph", path, "--artifact", TARGET, "--json"]) |
| 198 | .output() |
| 199 | .unwrap(); |
| 200 | assert!(json.status.success()); |
| 201 | assert!(String::from_utf8_lossy(&json.stdout).contains("bundle_digest")); |
| 202 | let bad = Command::new(bin) |
| 203 | .args(["explain", "--graph", path, "--artifact", "no:such"]) |
| 204 | .output() |
| 205 | .unwrap(); |
| 206 | assert_eq!( |
| 207 | bad.status.code(), |
| 208 | Some(15), |
| 209 | "TargetNotFound is code 5, exit 10+5" |
| 210 | ); |
| 211 | let usage = Command::new(bin).args(["explain"]).output().unwrap(); |
| 212 | assert_eq!(usage.status.code(), Some(2)); |
| 213 | } |