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-c1-tree/src/page.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-c1-tree / src / page.rs` 123 lines · 4071 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 //! Physical KV Page representation for C1 Copy-On-Write memory engine (§10, §28). 17 18 use aienos_kernel::crypto::sha256; 19 use core::fmt; 20 use serde::{Deserialize, Serialize}; 21 22 /// Number of tokens stored per physical KV page frame. 23 pub const PAGE_CAPACITY_TOKENS: usize = 64; 24 25 /// Simulated embedding / KV tensor bytes per token slot (e.g. 128 f32 elements = 512 bytes). 26 pub const KV_ELEMENTS_PER_TOKEN: usize = 128; 27 pub const PAGE_KV_BYTES: usize = PAGE_CAPACITY_TOKENS * KV_ELEMENTS_PER_TOKEN * 4; 28 29 /// Physical KV Page frame identifier. 30 #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] 31 pub struct PageId(pub u64); 32 33 impl fmt::Debug for PageId { 34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 35 write!(f, "PageId(0x{:08x})", self.0) 36 } 37 } 38 39 impl fmt::Display for PageId { 40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 41 write!(f, "0x{:08x}", self.0) 42 } 43 } 44 45 /// Physical Key-Value Page holding prefix cache on bare-metal accelerator memory. 46 #[derive(Clone, Debug, PartialEq)] 47 pub struct PhysicalKvPage { 48 pub page_id: PageId, 49 pub tokens: [u32; PAGE_CAPACITY_TOKENS], 50 pub len: usize, 51 pub ref_count: usize, 52 pub kv_cache: Vec, 53 pub checksum: [u8; 32], 54 } 55 56 impl PhysicalKvPage { 57 /// Allocate an empty physical page frame. 58 pub fn new(page_id: PageId) -> Self { 59 Self { 60 page_id, 61 tokens: [0u32; PAGE_CAPACITY_TOKENS], 62 len: 0, 63 ref_count: 1, 64 kv_cache: vec![0.0; PAGE_CAPACITY_TOKENS * KV_ELEMENTS_PER_TOKEN], 65 checksum: [0u8; 32], 66 } 67 } 68 69 /// Check if page has reached its maximum token capacity. 70 pub fn is_full(&self) -> bool { 71 self.len >= PAGE_CAPACITY_TOKENS 72 } 73 74 /// Explicit block ownership (ADR 0005): shared immutable block. 75 pub fn is_shared(&self) -> bool { 76 self.ref_count > 1 77 } 78 79 /// Explicit block ownership (ADR 0005): private mutable block. 80 pub fn is_private(&self) -> bool { 81 self.ref_count == 1 82 } 83 84 /// Append a token and update its physical KV cache slot and cryptographic checksum. 85 pub fn append(&mut self, token_id: u32) -> bool { 86 if self.is_full() { 87 return false; 88 } 89 90 let idx = self.len; 91 self.tokens[idx] = token_id; 92 93 // Deterministically synthesize KV cache slot from token ID 94 let base = idx * KV_ELEMENTS_PER_TOKEN; 95 for i in 0..KV_ELEMENTS_PER_TOKEN { 96 self.kv_cache[base + i] = ((token_id as f32) * 0.01) + (i as f32 * 0.001); 97 } 98 99 self.len += 1; 100 self.recompute_checksum(); 101 true 102 } 103 104 /// Recompute SHA-256 checksum over page tokens and physical KV data. 105 pub fn recompute_checksum(&mut self) { 106 let mut hasher = sha256::Sha256::new(); 107 hasher.update(&self.page_id.0.to_be_bytes()); 108 for i in 0..self.len { 109 hasher.update(&self.tokens[i].to_be_bytes()); 110 } 111 self.checksum = hasher.finalize(); 112 } 113 114 /// Verify cryptographic integrity of physical KV page against SHA-256 checksum. 115 pub fn verify_checksum(&self) -> bool { 116 let mut hasher = sha256::Sha256::new(); 117 hasher.update(&self.page_id.0.to_be_bytes()); 118 for i in 0..self.len { 119 hasher.update(&self.tokens[i].to_be_bytes()); 120 } 121 hasher.finalize() == self.checksum 122 } 123 }