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-cortex / src / record.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 | //! Epistemic records capturing knowledge with immutable provenance and evidence hashes. |
| 17 | |
| 18 | use crate::status::EpistemicStatus; |
| 19 | use aienos_kernel::crypto::sha256; |
| 20 | use core::fmt; |
| 21 | use serde::{Deserialize, Serialize}; |
| 22 | |
| 23 | /// Epistemic Record preserving knowledge with verifiable provenance. |
| 24 | #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 25 | pub struct EpistemicRecord { |
| 26 | pub id: [u8; 16], |
| 27 | pub status: EpistemicStatus, |
| 28 | pub statement: String, |
| 29 | pub provenance_source: String, |
| 30 | pub evidence_hash: [u8; 32], |
| 31 | pub created_at_utc: u64, |
| 32 | pub confidence: f32, |
| 33 | pub verified_by: Option<String>, |
| 34 | pub causal_parents: Vec<[u8; 16]>, |
| 35 | } |
| 36 | |
| 37 | impl EpistemicRecord { |
| 38 | /// Create a new record with calculated deterministic evidence hash. |
| 39 | #[allow(clippy::too_many_arguments)] |
| 40 | pub fn new( |
| 41 | status: EpistemicStatus, |
| 42 | statement: impl Into<String>, |
| 43 | provenance_source: impl Into<String>, |
| 44 | evidence_payload: &[u8], |
| 45 | created_at_utc: u64, |
| 46 | confidence: f32, |
| 47 | verified_by: Option<String>, |
| 48 | causal_parents: Vec<[u8; 16]>, |
| 49 | ) -> Self { |
| 50 | let stmt = statement.into(); |
| 51 | let prov = provenance_source.into(); |
| 52 | |
| 53 | // Calculate cryptographic evidence hash: SHA-256(status || statement || provenance || payload) |
| 54 | let mut hasher = sha256::Sha256::new(); |
| 55 | hasher.update(&[status as u8]); |
| 56 | hasher.update(stmt.as_bytes()); |
| 57 | hasher.update(prov.as_bytes()); |
| 58 | hasher.update(evidence_payload); |
| 59 | for parent in &causal_parents { |
| 60 | hasher.update(parent); |
| 61 | } |
| 62 | let evidence_hash = hasher.finalize(); |
| 63 | |
| 64 | // Deterministic ID derived from evidence hash + timestamp |
| 65 | let mut id_hasher = sha256::Sha256::new(); |
| 66 | id_hasher.update(&evidence_hash); |
| 67 | id_hasher.update(&created_at_utc.to_be_bytes()); |
| 68 | let full_id = id_hasher.finalize(); |
| 69 | let mut id = [0u8; 16]; |
| 70 | id.copy_from_slice(&full_id[..16]); |
| 71 | |
| 72 | Self { |
| 73 | id, |
| 74 | status, |
| 75 | statement: stmt, |
| 76 | provenance_source: prov, |
| 77 | evidence_hash, |
| 78 | created_at_utc, |
| 79 | confidence: confidence.clamp(0.0, 1.0), |
| 80 | verified_by, |
| 81 | causal_parents, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// Hex-formatted string of record ID. |
| 86 | pub fn id_hex(&self) -> String { |
| 87 | self.id.iter().map(|b| format!("{:02x}", b)).collect() |
| 88 | } |
| 89 | |
| 90 | /// Hex-formatted string of evidence hash. |
| 91 | pub fn hash_hex(&self) -> String { |
| 92 | self.evidence_hash |
| 93 | .iter() |
| 94 | .map(|b| format!("{:02x}", b)) |
| 95 | .collect() |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl fmt::Display for EpistemicRecord { |
| 100 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 101 | write!( |
| 102 | f, |
| 103 | "[{:?}] ({}) \"{}\" (conf: {:.2}, prov: {})", |
| 104 | self.status, |
| 105 | &self.id_hex()[..8], |
| 106 | self.statement, |
| 107 | self.confidence, |
| 108 | self.provenance_source |
| 109 | ) |
| 110 | } |
| 111 | } |