Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/aienos-agent-state/src/history.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / aienos-agent-state / src / history.rs` 128 lines ยท 3711 bytes Raw Blame History 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 //! Append-only TokenHistory log with cryptographic rolling hash chains. 17 18 use aienos_kernel::crypto::sha256; 19 use serde::{Deserialize, Serialize}; 20 21 /// Record of an individual token and its cryptographic chain state. 22 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] 23 pub struct TokenRecord { 24 pub sequence_idx: u64, 25 pub token_id: u32, 26 pub token_hash: [u8; 32], 27 } 28 29 /// Append-only log of token sequence IDs & hashes. 30 #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] 31 pub struct TokenHistory { 32 records: Vec, 33 rolling_hash: [u8; 32], 34 } 35 36 impl Default for TokenHistory { 37 fn default() -> Self { 38 Self::new() 39 } 40 } 41 42 impl TokenHistory { 43 /// Initialize an empty token history with genesis rolling hash. 44 pub fn new() -> Self { 45 let genesis_hash = sha256::hash(b"AIENOS_TOKEN_GENESIS_v1"); 46 Self { 47 records: Vec::new(), 48 rolling_hash: genesis_hash, 49 } 50 } 51 52 /// Number of tokens in history. 53 pub fn len(&self) -> usize { 54 self.records.len() 55 } 56 57 /// Check if history is empty. 58 pub fn is_empty(&self) -> bool { 59 self.records.is_empty() 60 } 61 62 /// Current cumulative rolling hash. 63 pub fn rolling_hash(&self) -> [u8; 32] { 64 self.rolling_hash 65 } 66 67 /// Slice of all token records. 68 pub fn records(&self) -> &[TokenRecord] { 69 &self.records 70 } 71 72 /// Extract raw token IDs. 73 pub fn tokens(&self) -> Vec { 74 self.records.iter().map(|r| r.token_id).collect() 75 } 76 77 /// Append a single token to the history log, updating rolling cryptographic hash. 78 pub fn append(&mut self, token_id: u32) { 79 let seq = self.records.len() as u64; 80 let mut hasher = sha256::Sha256::new(); 81 hasher.update(&self.rolling_hash); 82 hasher.update(&seq.to_be_bytes()); 83 hasher.update(&token_id.to_be_bytes()); 84 let next_hash = hasher.finalize(); 85 86 self.records.push(TokenRecord { 87 sequence_idx: seq, 88 token_id, 89 token_hash: next_hash, 90 }); 91 self.rolling_hash = next_hash; 92 } 93 94 /// Append a batch of tokens sequentially. 95 pub fn append_tokens(&mut self, tokens: &[u32]) { 96 for &t in tokens { 97 self.append(t); 98 } 99 } 100 101 /// Fork this token history into a new independent branch starting from this state. 102 pub fn fork(&self) -> Self { 103 self.clone() 104 } 105 } 106 107 #[cfg(test)] 108 mod tests { 109 use super::*; 110 111 #[test] 112 fn test_token_history_rolling_hash() { 113 let mut h1 = TokenHistory::new(); 114 h1.append_tokens(&[101, 202, 303]); 115 116 let mut h2 = TokenHistory::new(); 117 h2.append_tokens(&[101, 202, 303]); 118 119 assert_eq!(h1.len(), 3); 120 assert_eq!(h1.rolling_hash(), h2.rolling_hash()); 121 assert_eq!(h1.tokens(), vec![101, 202, 303]); 122 123 // Appending different tokens must diverge hashes 124 let mut h3 = h1.fork(); 125 h3.append(404); 126 assert_ne!(h1.rolling_hash(), h3.rolling_hash()); 127 } 128 }