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 / src / session.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 | //! Clarification and rewind over one bundle, without recomputing anything. |
| 17 | //! |
| 18 | //! "Explain that", "explain step 4", "go one level deeper", "where did that come |
| 19 | //! from?", "show the evidence", "explain it technically / simply", "give me an |
| 20 | //! analogy", "what are you uncertain about?", "what else was considered?" all read |
| 21 | //! the same immutable bundle. |
| 22 | |
| 23 | use crate::bundle::ExplanationBundle; |
| 24 | use crate::contract::{ExplainError, Relation}; |
| 25 | use crate::render::{render, status_words, Audience, ExplanationRequest, Mode, MAX_LEVEL}; |
| 26 | use std::fmt::Write; |
| 27 | |
| 28 | /// A conversation about one explanation. |
| 29 | pub struct ExplanationSession { |
| 30 | bundle: ExplanationBundle, |
| 31 | level: u8, |
| 32 | audience: Audience, |
| 33 | } |
| 34 | |
| 35 | impl ExplanationSession { |
| 36 | pub fn new(bundle: ExplanationBundle, audience: Audience) -> Self { |
| 37 | Self { |
| 38 | bundle, |
| 39 | level: 0, |
| 40 | audience, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | pub fn bundle(&self) -> &ExplanationBundle { |
| 45 | &self.bundle |
| 46 | } |
| 47 | |
| 48 | pub fn level(&self) -> u8 { |
| 49 | self.level |
| 50 | } |
| 51 | |
| 52 | fn req(&self, mode: Mode, level: u8) -> ExplanationRequest { |
| 53 | ExplanationRequest { |
| 54 | mode, |
| 55 | level: Some(level), |
| 56 | audience: self.audience, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// "Explain that": the conclusion only (level 0). |
| 61 | pub fn explain(&mut self) -> String { |
| 62 | self.level = 0; |
| 63 | render(&self.bundle, &self.req(Mode::StepByStep, 0)) |
| 64 | } |
| 65 | |
| 66 | /// "Go one level deeper." |
| 67 | pub fn deeper(&mut self) -> String { |
| 68 | self.level = (self.level + 1).min(MAX_LEVEL); |
| 69 | render(&self.bundle, &self.req(Mode::StepByStep, self.level)) |
| 70 | } |
| 71 | |
| 72 | /// "Back up one level." |
| 73 | pub fn shallower(&mut self) -> String { |
| 74 | self.level = self.level.saturating_sub(1); |
| 75 | render(&self.bundle, &self.req(Mode::StepByStep, self.level)) |
| 76 | } |
| 77 | |
| 78 | /// "Explain step n": the step, what it rests on, and what rests on it. |
| 79 | pub fn step(&self, n: u32) -> Result<String, ExplainError> { |
| 80 | let s = self.bundle.step(n).ok_or(ExplainError::StepOutOfRange(n))?; |
| 81 | let mut out = format!( |
| 82 | "Step {}: {} [{}]\n", |
| 83 | s.index, |
| 84 | s.statement, |
| 85 | status_words(s.status) |
| 86 | ); |
| 87 | if s.supported_by.is_empty() { |
| 88 | if s.grounding { |
| 89 | out.push_str("It is a starting point: recorded directly, not derived.\n"); |
| 90 | } else { |
| 91 | out.push_str("Nothing earlier on the path supports it.\n"); |
| 92 | } |
| 93 | } else { |
| 94 | for l in &s.supported_by { |
| 95 | let p = self.bundle.step(l.step).expect("support index valid"); |
| 96 | let verb = if l.relation == Relation::References { |
| 97 | "It refers to (weak link, not a recorded one)" |
| 98 | } else { |
| 99 | "It rests on" |
| 100 | }; |
| 101 | let _ = writeln!( |
| 102 | out, |
| 103 | "{verb} step {} ({}): {}", |
| 104 | p.index, |
| 105 | l.relation.name(), |
| 106 | p.statement |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | for later in &self.bundle.steps { |
| 111 | if later.supported_by.iter().any(|l| l.step == s.index) { |
| 112 | let _ = writeln!(out, "Step {} rests on it.", later.index); |
| 113 | } |
| 114 | } |
| 115 | for u in self |
| 116 | .bundle |
| 117 | .uncertainties |
| 118 | .iter() |
| 119 | .filter(|u| u.node_id == s.node_id) |
| 120 | { |
| 121 | let _ = writeln!(out, "Caveat: {}", u.detail); |
| 122 | } |
| 123 | Ok(out) |
| 124 | } |
| 125 | |
| 126 | /// "Where did that claim come from?" |
| 127 | pub fn source(&self, n: u32) -> Result<String, ExplainError> { |
| 128 | let s = self.bundle.step(n).ok_or(ExplainError::StepOutOfRange(n))?; |
| 129 | Ok(format!( |
| 130 | "Step {} comes from {} ({}), reference {}{}; node {}; status {}.\n", |
| 131 | s.index, |
| 132 | s.source.system, |
| 133 | s.source.artifact, |
| 134 | s.source.reference, |
| 135 | s.source |
| 136 | .digest |
| 137 | .as_ref() |
| 138 | .map(|d| format!(", digest {d}")) |
| 139 | .unwrap_or_default(), |
| 140 | s.node_id, |
| 141 | s.status.name() |
| 142 | )) |
| 143 | } |
| 144 | |
| 145 | /// "Show the evidence." |
| 146 | pub fn evidence(&self) -> String { |
| 147 | let mut out = String::new(); |
| 148 | if self.bundle.evidence.is_empty() { |
| 149 | out.push_str("No digests, receipts or measurements are attached to this path.\n"); |
| 150 | } |
| 151 | for e in &self.bundle.evidence { |
| 152 | let _ = write!( |
| 153 | out, |
| 154 | "Step {}: {} {} {}", |
| 155 | e.step, e.source.system, e.source.artifact, e.source.reference |
| 156 | ); |
| 157 | if let Some(d) = &e.source.digest { |
| 158 | let _ = write!(out, " digest {d}"); |
| 159 | } |
| 160 | out.push('\n'); |
| 161 | for m in &e.measurements { |
| 162 | let _ = writeln!(out, " {} = {} {}", m.name, m.value, m.unit); |
| 163 | } |
| 164 | } |
| 165 | out |
| 166 | } |
| 167 | |
| 168 | pub fn technical(&self) -> String { |
| 169 | render(&self.bundle, &self.req(Mode::Technical, MAX_LEVEL)) |
| 170 | } |
| 171 | |
| 172 | pub fn simple(&self) -> String { |
| 173 | render( |
| 174 | &self.bundle, |
| 175 | &ExplanationRequest { |
| 176 | mode: Mode::Summary, |
| 177 | level: Some(1), |
| 178 | audience: Audience::General, |
| 179 | }, |
| 180 | ) |
| 181 | } |
| 182 | |
| 183 | pub fn analogy(&self) -> String { |
| 184 | render(&self.bundle, &self.req(Mode::Analogy, 2)) |
| 185 | } |
| 186 | |
| 187 | /// "What are you uncertain about?" |
| 188 | pub fn uncertainties(&self) -> String { |
| 189 | if self.bundle.uncertainties.is_empty() && self.bundle.contradictions.is_empty() { |
| 190 | return "No uncertainty is recorded on this path.\n".into(); |
| 191 | } |
| 192 | let mut out = String::new(); |
| 193 | for u in &self.bundle.uncertainties { |
| 194 | let _ = writeln!(out, "- {}: {}", u.kind.label(), u.detail); |
| 195 | } |
| 196 | for c in &self.bundle.contradictions { |
| 197 | let _ = writeln!(out, "- conflict: {}", c.statement); |
| 198 | } |
| 199 | out |
| 200 | } |
| 201 | |
| 202 | /// "What other explanations were considered?" Only explicitly recorded ones. |
| 203 | pub fn alternatives(&self) -> String { |
| 204 | if self.bundle.alternatives.is_empty() { |
| 205 | return "No alternative explanations are recorded in the evidence.\n".into(); |
| 206 | } |
| 207 | let mut out = String::new(); |
| 208 | for a in &self.bundle.alternatives { |
| 209 | let _ = writeln!(out, "- {}", a.statement); |
| 210 | } |
| 211 | out |
| 212 | } |
| 213 | } |