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 / explain.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 | //! Behavioural tests for the explanation bridge (brief section 20). |
| 17 | |
| 18 | use aienos_agent_state::{AgentStateAbi, AgentStateManager, LogicalAgentId}; |
| 19 | use aienos_cortex::{CortexStore, EpistemicRecord, EpistemicStatus}; |
| 20 | use aienos_explain::adapters::{ |
| 21 | branch_graph, branch_graph_for_record, cortex_graph_for_record, cortex_graph_from, |
| 22 | cortex_node_id, store_from_journal, |
| 23 | }; |
| 24 | use aienos_explain::bundle::{Role, UncertaintyKind}; |
| 25 | use aienos_explain::contract::{ |
| 26 | EdgeDoc, EvidenceGraph, NodeDoc, NodeKind, Relation, SourceRef, Status, |
| 27 | }; |
| 28 | use aienos_explain::render::status_words; |
| 29 | use aienos_explain::{ |
| 30 | build_bundle, render, Audience, ExplanationBundle, ExplanationRequest, ExplanationSession, |
| 31 | Limits, Mode, |
| 32 | }; |
| 33 | use std::collections::HashMap; |
| 34 | |
| 35 | fn n(id: &str, kind: NodeKind, status: Status, parents: &[(&str, Relation)]) -> NodeDoc { |
| 36 | NodeDoc { |
| 37 | id: id.into(), |
| 38 | kind: kind.code(), |
| 39 | status: status.code(), |
| 40 | statement: format!("statement of {id}"), |
| 41 | source: SourceRef { |
| 42 | system: "test".into(), |
| 43 | artifact: "Node".into(), |
| 44 | reference: id.into(), |
| 45 | digest: None, |
| 46 | }, |
| 47 | confidence_permille: None, |
| 48 | confidence_basis: None, |
| 49 | recorded_at_utc: None, |
| 50 | parents: parents |
| 51 | .iter() |
| 52 | .map(|(t, r)| EdgeDoc { |
| 53 | to: (*t).into(), |
| 54 | relation: r.code(), |
| 55 | }) |
| 56 | .collect(), |
| 57 | measurements: vec![], |
| 58 | missing: vec![], |
| 59 | notes: vec![], |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn graph(nodes: Vec<NodeDoc>) -> EvidenceGraph { |
| 64 | let mut g = EvidenceGraph::new("test"); |
| 65 | g.nodes = nodes; |
| 66 | g |
| 67 | } |
| 68 | |
| 69 | fn ids(b: &ExplanationBundle) -> Vec<&str> { |
| 70 | b.steps.iter().map(|s| s.node_id.as_str()).collect() |
| 71 | } |
| 72 | |
| 73 | fn has(b: &ExplanationBundle, k: UncertaintyKind) -> bool { |
| 74 | b.uncertainties.iter().any(|u| u.kind == k) |
| 75 | } |
| 76 | |
| 77 | const SUP: Relation = Relation::SupportedBy; |
| 78 | const REC: NodeKind = NodeKind::CortexRecord; |
| 79 | |
| 80 | #[test] |
| 81 | fn linear_chain_is_ordered_parents_first() { |
| 82 | let mut c = CortexStore::new(); |
| 83 | let obs = c.record_observation("allocator queue depth 900", "telemetry:kv", b"900", 10); |
| 84 | let i1 = c |
| 85 | .record_inference( |
| 86 | "allocator is saturated", |
| 87 | "rule:queue>512", |
| 88 | vec![obs.id], |
| 89 | 0.9, |
| 90 | 20, |
| 91 | ) |
| 92 | .unwrap(); |
| 93 | let i2 = c |
| 94 | .record_inference( |
| 95 | "p95 latency is allocator bound", |
| 96 | "rule:join", |
| 97 | vec![i1.id], |
| 98 | 0.8, |
| 99 | 30, |
| 100 | ) |
| 101 | .unwrap(); |
| 102 | let g = cortex_graph_for_record(&c, &i2.id, Limits::default()).unwrap(); |
| 103 | let b = build_bundle(&g, &cortex_node_id(&i2.id), Limits::default()).unwrap(); |
| 104 | assert_eq!( |
| 105 | ids(&b), |
| 106 | vec![ |
| 107 | cortex_node_id(&obs.id), |
| 108 | cortex_node_id(&i1.id), |
| 109 | cortex_node_id(&i2.id) |
| 110 | ] |
| 111 | ); |
| 112 | assert_eq!(b.conclusion().node_id, cortex_node_id(&i2.id)); |
| 113 | assert_eq!(b.steps[1].supported_by[0].step, 1); |
| 114 | assert_eq!(b.steps[2].supported_by[0].step, 2); |
| 115 | assert!(b.steps[0].grounding); |
| 116 | assert_eq!(b.steps[0].role, Role::Observed); |
| 117 | assert!(!has(&b, UncertaintyKind::MissingParent)); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn branching_support_is_deterministic_regardless_of_input_order() { |
| 122 | let nodes = vec![ |
| 123 | n( |
| 124 | "c", |
| 125 | REC, |
| 126 | Status::Inference, |
| 127 | &[("z", SUP), ("a", SUP), ("m", SUP)], |
| 128 | ), |
| 129 | n("z", REC, Status::DirectObservation, &[]), |
| 130 | n("a", REC, Status::VerifiedFact, &[]), |
| 131 | n("m", REC, Status::OperatorDecision, &[]), |
| 132 | ]; |
| 133 | let b1 = build_bundle(&graph(nodes.clone()), "c", Limits::default()).unwrap(); |
| 134 | let mut rev = nodes; |
| 135 | rev.reverse(); |
| 136 | let b2 = build_bundle(&graph(rev), "c", Limits::default()).unwrap(); |
| 137 | assert_eq!(b1.steps, b2.steps); |
| 138 | // Observed (a, z) before Decision (m), ids break ties, conclusion last. |
| 139 | assert_eq!(ids(&b1), vec!["a", "z", "m", "c"]); |
| 140 | } |
| 141 | |
| 142 | #[test] |
| 143 | fn shared_parent_appears_once() { |
| 144 | let nodes = vec![ |
| 145 | n("concl", REC, Status::Inference, &[("i1", SUP), ("i2", SUP)]), |
| 146 | n("i1", REC, Status::Inference, &[("obs", SUP)]), |
| 147 | n("i2", REC, Status::Inference, &[("obs", SUP)]), |
| 148 | n("obs", REC, Status::DirectObservation, &[]), |
| 149 | ]; |
| 150 | let mut g = graph(nodes); |
| 151 | g.nodes[3].source.digest = Some("ab".repeat(32)); |
| 152 | let b = build_bundle(&g, "concl", Limits::default()).unwrap(); |
| 153 | assert_eq!(b.steps.len(), 4); |
| 154 | assert_eq!(ids(&b).iter().filter(|i| **i == "obs").count(), 1); |
| 155 | assert_eq!(b.evidence.len(), 1); |
| 156 | assert_eq!(b.steps[0].node_id, "obs"); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn cycle_terminates_and_is_reported() { |
| 161 | let nodes = vec![ |
| 162 | n("A", REC, Status::Inference, &[("B", SUP)]), |
| 163 | n("B", REC, Status::Inference, &[("C", SUP)]), |
| 164 | n("C", REC, Status::Inference, &[("A", SUP)]), |
| 165 | ]; |
| 166 | let b = build_bundle(&graph(nodes), "A", Limits::default()).unwrap(); |
| 167 | assert_eq!(b.steps.len(), 3); |
| 168 | assert_eq!(b.conclusion().node_id, "A"); |
| 169 | let cyc: Vec<_> = b |
| 170 | .uncertainties |
| 171 | .iter() |
| 172 | .filter(|u| u.kind == UncertaintyKind::Cycle) |
| 173 | .collect(); |
| 174 | assert_eq!(cyc.len(), 1); |
| 175 | assert_eq!(cyc[0].node_id, "C"); |
| 176 | assert_eq!(cyc[0].related.as_deref(), Some("A")); |
| 177 | let text = render(&b, &ExplanationRequest::new(Mode::StepByStep)); |
| 178 | assert!(text.contains("circular")); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn cycle_through_imported_cortex_journal_terminates() { |
| 183 | // Cortex refuses cycles on write, but an imported journal is not re-verified. |
| 184 | let mk = |tag: u8, parents: Vec<[u8; 16]>| { |
| 185 | let mut r = EpistemicRecord::new( |
| 186 | EpistemicStatus::Inference, |
| 187 | format!("claim {tag}"), |
| 188 | "test", |
| 189 | &[tag], |
| 190 | tag as u64, |
| 191 | 0.5, |
| 192 | None, |
| 193 | parents, |
| 194 | ); |
| 195 | r.id = [tag; 16]; |
| 196 | r |
| 197 | }; |
| 198 | let a = mk(1, vec![[2; 16]]); |
| 199 | let bb = mk(2, vec![[3; 16]]); |
| 200 | let c = mk(3, vec![[1; 16]]); |
| 201 | let map: HashMap<[u8; 16], EpistemicRecord> = |
| 202 | [a.clone(), bb, c].into_iter().map(|r| (r.id, r)).collect(); |
| 203 | let g = cortex_graph_from(&[a.id], |id| map.get(id), &[], Limits::default()); |
| 204 | let b = build_bundle(&g, &cortex_node_id(&a.id), Limits::default()).unwrap(); |
| 205 | assert_eq!(b.steps.len(), 3); |
| 206 | assert!(has(&b, UncertaintyKind::Cycle)); |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn missing_parent_stays_missing() { |
| 211 | let nodes = vec![ |
| 212 | n( |
| 213 | "concl", |
| 214 | REC, |
| 215 | Status::Inference, |
| 216 | &[("obs", SUP), ("ghost", SUP)], |
| 217 | ), |
| 218 | n("obs", REC, Status::DirectObservation, &[]), |
| 219 | ]; |
| 220 | let b = build_bundle(&graph(nodes), "concl", Limits::default()).unwrap(); |
| 221 | assert_eq!( |
| 222 | ids(&b), |
| 223 | vec!["obs", "concl"], |
| 224 | "no step is invented for the gap" |
| 225 | ); |
| 226 | let u = b |
| 227 | .uncertainties |
| 228 | .iter() |
| 229 | .find(|u| u.kind == UncertaintyKind::MissingParent) |
| 230 | .expect("gap reported"); |
| 231 | assert_eq!(u.related.as_deref(), Some("ghost")); |
| 232 | let text = render(&b, &ExplanationRequest::new(Mode::Summary)); |
| 233 | assert!(text.contains("no evidence for that link"), "{text}"); |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn unsupported_claim_is_flagged() { |
| 238 | let nodes = vec![n("lonely", REC, Status::Inference, &[])]; |
| 239 | let b = build_bundle(&graph(nodes), "lonely", Limits::default()).unwrap(); |
| 240 | assert!(has(&b, UncertaintyKind::Unsupported)); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn contradiction_remains_visible() { |
| 245 | let mut c = CortexStore::new(); |
| 246 | let o1 = c.record_observation("sensor A says 70C", "sensor:a", b"70", 1); |
| 247 | let o2 = c.record_observation("sensor B says 40C", "sensor:b", b"40", 2); |
| 248 | let _x = c |
| 249 | .flag_contradiction(o1.id, o2.id, "A and B disagree", 3) |
| 250 | .unwrap(); |
| 251 | let inf = c |
| 252 | .record_inference("die is hot", "rule:max", vec![o1.id], 0.7, 4) |
| 253 | .unwrap(); |
| 254 | let g = cortex_graph_for_record(&c, &inf.id, Limits::default()).unwrap(); |
| 255 | let b = build_bundle(&g, &cortex_node_id(&inf.id), Limits::default()).unwrap(); |
| 256 | assert_eq!(b.contradictions.len(), 1); |
| 257 | assert_eq!(b.contradictions[0].involves, vec![cortex_node_id(&o1.id)]); |
| 258 | assert!(has(&b, UncertaintyKind::ConflictingEvidence)); |
| 259 | // The contradiction is disclosure, never a support step. |
| 260 | assert_eq!(b.steps.len(), 2); |
| 261 | let text = render(&b, &ExplanationRequest::new(Mode::StepByStep)); |
| 262 | assert!(text.contains("Conflicts:")); |
| 263 | assert!(text.contains("A and B disagree")); |
| 264 | let s = render(&b, &ExplanationRequest::new(Mode::Summary)); |
| 265 | assert!(s.contains("Main uncertainty"), "{s}"); |
| 266 | assert!(s.contains("contradiction"), "{s}"); |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn hypothesis_is_never_rendered_as_fact() { |
| 271 | let mut c = CortexStore::new(); |
| 272 | let o = c.record_observation("cache misses up 40%", "perf:llc", b"40", 1); |
| 273 | let h = c |
| 274 | .record_hypothesis("prefetcher is mis-trained", "agent:rsi", vec![o.id], 2) |
| 275 | .unwrap(); |
| 276 | let i = c |
| 277 | .record_inference("retune prefetcher", "planner", vec![h.id], 0.6, 3) |
| 278 | .unwrap(); |
| 279 | let g = cortex_graph_for_record(&c, &i.id, Limits::default()).unwrap(); |
| 280 | let b = build_bundle(&g, &cortex_node_id(&i.id), Limits::default()).unwrap(); |
| 281 | let hs = b |
| 282 | .steps |
| 283 | .iter() |
| 284 | .find(|s| s.node_id == cortex_node_id(&h.id)) |
| 285 | .unwrap(); |
| 286 | assert_eq!(hs.status, Status::Hypothesis); |
| 287 | assert!(!hs.grounding); |
| 288 | assert!(has(&b, UncertaintyKind::HypothesisNotFalsified)); |
| 289 | for mode in [ |
| 290 | Mode::Summary, |
| 291 | Mode::StepByStep, |
| 292 | Mode::Technical, |
| 293 | Mode::Analogy, |
| 294 | ] { |
| 295 | for aud in [Audience::General, Audience::Developer] { |
| 296 | let t = render( |
| 297 | &b, |
| 298 | &ExplanationRequest { |
| 299 | mode, |
| 300 | level: Some(4), |
| 301 | audience: aud, |
| 302 | }, |
| 303 | ); |
| 304 | for line in t |
| 305 | .lines() |
| 306 | .filter(|l| l.contains("prefetcher is mis-trained")) |
| 307 | { |
| 308 | assert!(!line.contains("checked fact"), "{line}"); |
| 309 | assert!(!line.contains("VerifiedFact"), "{line}"); |
| 310 | assert!(!line.contains("observed]"), "{line}"); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | assert!(status_words(Status::Hypothesis).contains("not yet established")); |
| 315 | } |
| 316 | |
| 317 | fn branch_fixture() -> ( |
| 318 | CortexStore, |
| 319 | AgentStateManager, |
| 320 | aienos_agent_state::LogicalBranchId, |
| 321 | [u8; 16], |
| 322 | [u8; 16], |
| 323 | [u8; 16], |
| 324 | ) { |
| 325 | let mut c = CortexStore::new(); |
| 326 | let used = c.record_observation("link 3 dropped 2% packets", "nic:3", b"2", 1); |
| 327 | let derived = c |
| 328 | .record_inference("link 3 is flaky", "rule:loss>1", vec![used.id], 0.7, 2) |
| 329 | .unwrap(); |
| 330 | let unrelated = c |
| 331 | .record_verified_fact( |
| 332 | "firmware 1.2 is signed", |
| 333 | "boot", |
| 334 | "sha256", |
| 335 | b"sig", |
| 336 | vec![], |
| 337 | 3, |
| 338 | ) |
| 339 | .unwrap(); |
| 340 | let m = AgentStateManager::new(1); |
| 341 | let agent = LogicalAgentId::from_seed("explain-branch"); |
| 342 | let root = m.register_agent(agent, 1); |
| 343 | let child = m.fork_branch(root).unwrap(); |
| 344 | m.add_epistemic_ref(child, derived.id).unwrap(); |
| 345 | (c, m, child, used.id, derived.id, unrelated.id) |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn branch_explanation_uses_only_what_the_branch_depended_on() { |
| 350 | let (c, m, child, used, derived, unrelated) = branch_fixture(); |
| 351 | let br = m.get_branch(&child).unwrap(); |
| 352 | let g = branch_graph(&c, &br, Limits::default()); |
| 353 | let b = build_bundle(&g, &g.nodes[0].id, Limits::default()).unwrap(); |
| 354 | let got = ids(&b); |
| 355 | assert!(got.contains(&cortex_node_id(&used).as_str())); |
| 356 | assert!(got.contains(&cortex_node_id(&derived).as_str())); |
| 357 | assert!( |
| 358 | !got.contains(&cortex_node_id(&unrelated).as_str()), |
| 359 | "no opportunistic facts" |
| 360 | ); |
| 361 | assert_eq!(b.conclusion().kind, NodeKind::AgentBranch); |
| 362 | let d = b |
| 363 | .steps |
| 364 | .iter() |
| 365 | .find(|s| s.node_id == cortex_node_id(&derived)) |
| 366 | .unwrap(); |
| 367 | assert!(d.branch_referenced); |
| 368 | let u = b |
| 369 | .steps |
| 370 | .iter() |
| 371 | .find(|s| s.node_id == cortex_node_id(&used)) |
| 372 | .unwrap(); |
| 373 | assert!(!u.branch_referenced, "ancestry, not a direct reference"); |
| 374 | let err = branch_graph_for_record(&c, &br, &unrelated, Limits::default()).unwrap_err(); |
| 375 | assert_eq!(err.code(), 8); |
| 376 | assert!(branch_graph_for_record(&c, &br, &used, Limits::default()).is_ok()); |
| 377 | } |
| 378 | |
| 379 | #[test] |
| 380 | fn token_history_never_enters_the_explanation() { |
| 381 | let (c, m, child, ..) = branch_fixture(); |
| 382 | let before = branch_graph(&c, &m.get_branch(&child).unwrap(), Limits::default()); |
| 383 | m.append_tokens(child, &[11, 22, 33, 44]).unwrap(); |
| 384 | let after = branch_graph(&c, &m.get_branch(&child).unwrap(), Limits::default()); |
| 385 | assert_eq!( |
| 386 | before, after, |
| 387 | "explanations are built from records, not token streams" |
| 388 | ); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn analogy_is_isolated_from_evidence() { |
| 393 | let mut c = CortexStore::new(); |
| 394 | let o = c.record_observation("x", "s", b"x", 1); |
| 395 | let h = c.record_hypothesis("y", "agent", vec![o.id], 2).unwrap(); |
| 396 | let journal_before = c.export_journal().unwrap(); |
| 397 | let g = cortex_graph_for_record(&c, &h.id, Limits::default()).unwrap(); |
| 398 | let b = build_bundle(&g, &cortex_node_id(&h.id), Limits::default()).unwrap(); |
| 399 | let digest = b.bundle_digest.clone(); |
| 400 | let json_before = b.to_json(); |
| 401 | let a = render(&b, &ExplanationRequest::new(Mode::Analogy)); |
| 402 | assert!(a.contains("illustration only, not evidence")); |
| 403 | assert!(a.contains("The evidence itself:")); |
| 404 | // Deleting the analogy leaves a complete explanation. |
| 405 | let plain = render(&b, &ExplanationRequest::new(Mode::StepByStep)); |
| 406 | for s in &b.steps { |
| 407 | assert!(plain.contains(&s.statement)); |
| 408 | assert!(a.contains(&s.statement)); |
| 409 | } |
| 410 | // No mutation of the bundle or of Cortex; analogy text is not in the bundle. |
| 411 | assert_eq!(b.bundle_digest, digest); |
| 412 | assert_eq!(b.compute_digest(), digest); |
| 413 | assert_eq!(b.to_json(), json_before); |
| 414 | assert!(!json_before.contains("mechanic")); |
| 415 | assert_eq!(c.export_journal().unwrap(), journal_before); |
| 416 | assert_eq!(c.len(), 2); |
| 417 | } |
| 418 | |
| 419 | #[test] |
| 420 | fn identical_input_gives_identical_bundle_and_text() { |
| 421 | let mut c = CortexStore::new(); |
| 422 | let o = c.record_observation("o", "s", b"o", 1); |
| 423 | let i = c.record_inference("i", "m", vec![o.id], 0.5, 2).unwrap(); |
| 424 | let journal = c.export_journal().unwrap(); |
| 425 | let c2 = store_from_journal(&journal).unwrap(); |
| 426 | let mk = |s: &CortexStore| { |
| 427 | let g = cortex_graph_for_record(s, &i.id, Limits::default()).unwrap(); |
| 428 | build_bundle(&g, &cortex_node_id(&i.id), Limits::default()).unwrap() |
| 429 | }; |
| 430 | let (b1, b2) = (mk(&c), mk(&c2)); |
| 431 | assert_eq!(b1.to_json(), b2.to_json()); |
| 432 | assert_eq!(b1.bundle_digest, b2.bundle_digest); |
| 433 | for mode in [ |
| 434 | Mode::Summary, |
| 435 | Mode::StepByStep, |
| 436 | Mode::Technical, |
| 437 | Mode::Analogy, |
| 438 | ] { |
| 439 | let r = ExplanationRequest::new(mode); |
| 440 | assert_eq!(render(&b1, &r), render(&b2, &r)); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | #[test] |
| 445 | fn depth_and_node_limits_bound_the_walk() { |
| 446 | let mut nodes = vec![n("n0", REC, Status::DirectObservation, &[])]; |
| 447 | for k in 1..50 { |
| 448 | let p = format!("n{}", k - 1); |
| 449 | nodes.push(n( |
| 450 | &format!("n{k}"), |
| 451 | REC, |
| 452 | Status::Inference, |
| 453 | &[(p.as_str(), SUP)], |
| 454 | )); |
| 455 | } |
| 456 | let g = graph(nodes); |
| 457 | let lim = Limits { |
| 458 | max_depth: 3, |
| 459 | max_nodes: 256, |
| 460 | }; |
| 461 | let b = build_bundle(&g, "n49", lim).unwrap(); |
| 462 | assert_eq!(b.steps.len(), 4); |
| 463 | assert!(has(&b, UncertaintyKind::Truncated)); |
| 464 | // Wide fan-in: node limit holds. |
| 465 | let mut wide = vec![]; |
| 466 | let parents: Vec<String> = (0..3000).map(|k| format!("p{k}")).collect(); |
| 467 | let refs: Vec<(&str, Relation)> = parents.iter().map(|p| (p.as_str(), SUP)).collect(); |
| 468 | // Parents are capped per node by the contract; split over 3 intermediate nodes. |
| 469 | wide.push(n( |
| 470 | "top", |
| 471 | REC, |
| 472 | Status::Inference, |
| 473 | &[("m0", SUP), ("m1", SUP), ("m2", SUP)], |
| 474 | )); |
| 475 | for (j, chunk) in refs.chunks(1000).enumerate() { |
| 476 | wide.push(n(&format!("m{j}"), REC, Status::Inference, chunk)); |
| 477 | } |
| 478 | for p in &parents { |
| 479 | wide.push(n(p, REC, Status::DirectObservation, &[])); |
| 480 | } |
| 481 | let lim = Limits { |
| 482 | max_depth: 32, |
| 483 | max_nodes: 100, |
| 484 | }; |
| 485 | let b = build_bundle(&graph(wide), "top", lim).unwrap(); |
| 486 | assert!(b.steps.len() <= 100); |
| 487 | assert!(has(&b, UncertaintyKind::Truncated)); |
| 488 | // Limits are validated. |
| 489 | assert_eq!( |
| 490 | build_bundle( |
| 491 | &g, |
| 492 | "n1", |
| 493 | Limits { |
| 494 | max_depth: 0, |
| 495 | max_nodes: 1 |
| 496 | } |
| 497 | ) |
| 498 | .unwrap_err() |
| 499 | .code(), |
| 500 | 10 |
| 501 | ); |
| 502 | assert_eq!( |
| 503 | build_bundle( |
| 504 | &g, |
| 505 | "n1", |
| 506 | Limits { |
| 507 | max_depth: 1, |
| 508 | max_nodes: 1_000_000 |
| 509 | } |
| 510 | ) |
| 511 | .unwrap_err() |
| 512 | .code(), |
| 513 | 10 |
| 514 | ); |
| 515 | } |
| 516 | |
| 517 | #[test] |
| 518 | fn malformed_input_fails_safely() { |
| 519 | assert_eq!( |
| 520 | EvidenceGraph::from_json(b"{not json").unwrap_err().code(), |
| 521 | 1 |
| 522 | ); |
| 523 | let mut g = graph(vec![n("a", REC, Status::Inference, &[])]); |
| 524 | g.version = 2; |
| 525 | assert_eq!(g.validate().unwrap_err().code(), 2); |
| 526 | let mut g = graph(vec![n("a", REC, Status::Inference, &[])]); |
| 527 | g.nodes[0].status = 9; |
| 528 | assert_eq!( |
| 529 | build_bundle(&g, "a", Limits::default()).unwrap_err().code(), |
| 530 | 3 |
| 531 | ); |
| 532 | let g = graph(vec![ |
| 533 | n("a", REC, Status::Inference, &[]), |
| 534 | n("a", REC, Status::Inference, &[]), |
| 535 | ]); |
| 536 | assert_eq!( |
| 537 | build_bundle(&g, "a", Limits::default()).unwrap_err().code(), |
| 538 | 4 |
| 539 | ); |
| 540 | let g = graph(vec![n("a", REC, Status::Inference, &[])]); |
| 541 | assert_eq!( |
| 542 | build_bundle(&g, "zz", Limits::default()) |
| 543 | .unwrap_err() |
| 544 | .code(), |
| 545 | 5 |
| 546 | ); |
| 547 | let mut j = CortexStore::new(); |
| 548 | j.record_observation("o", "s", b"o", 1); |
| 549 | let mut bytes = j.export_journal().unwrap(); |
| 550 | let k = bytes.len() / 2; |
| 551 | bytes[k] ^= 1; |
| 552 | assert!(store_from_journal(&bytes).is_err()); |
| 553 | } |
| 554 | |
| 555 | #[test] |
| 556 | fn clarification_reads_the_same_bundle() { |
| 557 | let nodes = vec![ |
| 558 | n("concl", REC, Status::Inference, &[("i1", SUP)]), |
| 559 | n("i1", REC, Status::Inference, &[("obs", SUP)]), |
| 560 | n("obs", REC, Status::DirectObservation, &[]), |
| 561 | ]; |
| 562 | let b = build_bundle(&graph(nodes), "concl", Limits::default()).unwrap(); |
| 563 | let digest = b.bundle_digest.clone(); |
| 564 | let mut s = ExplanationSession::new(b, Audience::Operator); |
| 565 | let l0 = s.explain(); |
| 566 | assert!(l0.contains("statement of concl")); |
| 567 | assert!(!l0.contains("statement of obs")); |
| 568 | let l1 = s.deeper(); |
| 569 | assert!(l1.contains("statement of i1")); |
| 570 | assert!(!l1.contains("statement of obs")); |
| 571 | let l2 = s.deeper(); |
| 572 | assert!(l2.contains("statement of obs")); |
| 573 | assert_eq!(s.level(), 2); |
| 574 | assert!(s.step(2).unwrap().contains("rests on step 1")); |
| 575 | assert!(s.step(1).unwrap().contains("starting point")); |
| 576 | assert!(s.source(1).unwrap().contains("test (Node)")); |
| 577 | assert_eq!(s.step(9).unwrap_err().code(), 7); |
| 578 | assert!(s.technical().contains("step 1 id=obs")); |
| 579 | assert!(s.simple().contains("Conclusion:")); |
| 580 | assert!(s.analogy().contains("The evidence itself:")); |
| 581 | assert!(s.uncertainties().contains("inferred")); |
| 582 | assert!(s.alternatives().contains("No alternative")); |
| 583 | assert_eq!(s.bundle().bundle_digest, digest); |
| 584 | } |