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-agent-state / src / lineage.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 | //! Agent lineage provenance tracking and Cortex epistemic references. |
| 17 | |
| 18 | use crate::id::{LogicalAgentId, LogicalBranchId}; |
| 19 | use serde::{Deserialize, Serialize}; |
| 20 | |
| 21 | /// Provenance lineage tree path tracing back to Genesis root agent. |
| 22 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 23 | pub struct Lineage { |
| 24 | pub root_agent: LogicalAgentId, |
| 25 | pub ancestor_branches: Vec<LogicalBranchId>, |
| 26 | pub depth: usize, |
| 27 | } |
| 28 | |
| 29 | impl Lineage { |
| 30 | /// Create root lineage for an agent. |
| 31 | pub fn new_root(root_agent: LogicalAgentId) -> Self { |
| 32 | Self { |
| 33 | root_agent, |
| 34 | ancestor_branches: Vec::new(), |
| 35 | depth: 0, |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /// Extend lineage with a new descendant branch. |
| 40 | pub fn extend(&self, branch_id: LogicalBranchId) -> Self { |
| 41 | let mut ancestors = self.ancestor_branches.clone(); |
| 42 | ancestors.push(branch_id); |
| 43 | Self { |
| 44 | root_agent: self.root_agent, |
| 45 | ancestor_branches: ancestors, |
| 46 | depth: self.depth + 1, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Check if this branch is in the lineage. |
| 51 | pub fn contains(&self, branch_id: &LogicalBranchId) -> bool { |
| 52 | self.ancestor_branches.contains(branch_id) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// Set of Cortex observation/fact record UUIDs referenced by a reasoning branch. |
| 57 | #[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] |
| 58 | pub struct EpistemicRefs { |
| 59 | record_ids: Vec<[u8; 16]>, |
| 60 | } |
| 61 | |
| 62 | impl EpistemicRefs { |
| 63 | /// Create empty epistemic references set. |
| 64 | pub fn new() -> Self { |
| 65 | Self { |
| 66 | record_ids: Vec::new(), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// Insert a Cortex record reference UUID. |
| 71 | pub fn insert(&mut self, record_id: [u8; 16]) { |
| 72 | if !self.record_ids.contains(&record_id) { |
| 73 | self.record_ids.push(record_id); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// Check if a Cortex record is referenced. |
| 78 | pub fn contains(&self, record_id: &[u8; 16]) -> bool { |
| 79 | self.record_ids.contains(record_id) |
| 80 | } |
| 81 | |
| 82 | /// Access all referenced record UUIDs. |
| 83 | pub fn records(&self) -> &[[u8; 16]] { |
| 84 | &self.record_ids |
| 85 | } |
| 86 | |
| 87 | /// Count of referenced epistemic records. |
| 88 | pub fn len(&self) -> usize { |
| 89 | self.record_ids.len() |
| 90 | } |
| 91 | |
| 92 | /// Check if empty. |
| 93 | pub fn is_empty(&self) -> bool { |
| 94 | self.record_ids.is_empty() |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #[cfg(test)] |
| 99 | mod tests { |
| 100 | use super::*; |
| 101 | |
| 102 | #[test] |
| 103 | fn test_lineage_extension() { |
| 104 | let agent = LogicalAgentId::from_seed("lineage-test"); |
| 105 | let root = Lineage::new_root(agent); |
| 106 | assert_eq!(root.depth, 0); |
| 107 | |
| 108 | let b1 = LogicalBranchId::derive(LogicalBranchId::root_branch(agent), 1); |
| 109 | let l1 = root.extend(b1); |
| 110 | assert_eq!(l1.depth, 1); |
| 111 | assert!(l1.contains(&b1)); |
| 112 | |
| 113 | let b2 = LogicalBranchId::derive(b1, 2); |
| 114 | let l2 = l1.extend(b2); |
| 115 | assert_eq!(l2.depth, 2); |
| 116 | assert!(l2.contains(&b1)); |
| 117 | assert!(l2.contains(&b2)); |
| 118 | } |
| 119 | } |