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/aien-sovereign-core-mv/blob/main/crates/aien-runtime/src/world.rs # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. 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 `aien-sovereign-core-mv / crates / aien-runtime / src / world.rs` 237 lines ยท 7603 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 //! Immutable World Manifests and Structural State Storage 17 //! Implements path-copying persistent world branching, staged effect journals, and reachability tracking. 18 19 use serde::{Deserialize, Serialize}; 20 use sha2::Digest; 21 use std::collections::{HashMap, HashSet}; 22 23 #[derive(Debug, Clone, Serialize, Deserialize)] 24 pub enum EffectIntent { 25 WriteFile { 26 path: String, 27 content_hash: [u8; 32], 28 }, 29 ExecuteCommand { 30 command: String, 31 args: Vec, 32 }, 33 SendMessage { 34 recipient: String, 35 message_hash: [u8; 32], 36 }, 37 PublishGit { 38 remote: String, 39 branch: String, 40 commit_sha: String, 41 }, 42 } 43 44 #[derive(Debug, Clone, Serialize, Deserialize)] 45 pub struct WorldManifest { 46 pub id: u64, 47 pub parent: Option, 48 pub object_root: [u8; 32], 49 pub filesystem_root: [u8; 32], 50 pub token_root: u64, 51 pub memory_root: u64, 52 pub capability_set: u64, 53 pub effect_log: Vec, 54 pub created_at: u64, 55 } 56 57 pub struct WorldDraft { 58 pub base_world_id: u64, 59 pub staged_objects: HashMap>, 60 pub staged_effects: Vec, 61 } 62 63 impl WorldDraft { 64 pub fn new(base_world_id: u64) -> Self { 65 Self { 66 base_world_id, 67 staged_objects: HashMap::new(), 68 staged_effects: Vec::new(), 69 } 70 } 71 72 pub fn stage_object(&mut self, key: String, data: Vec) { 73 self.staged_objects.insert(key, data); 74 } 75 76 pub fn record_effect(&mut self, effect: EffectIntent) { 77 self.staged_effects.push(effect); 78 } 79 } 80 81 /// Content-addressed in-memory persistent object store and world hierarchy. 82 pub struct WorldStore { 83 worlds: HashMap, 84 objects: HashMap<[u8; 32], Vec>, 85 retained_roots: HashSet, 86 next_world_id: u64, 87 } 88 89 impl Default for WorldStore { 90 fn default() -> Self { 91 Self::new() 92 } 93 } 94 95 impl WorldStore { 96 pub fn new() -> Self { 97 Self { 98 worlds: HashMap::new(), 99 objects: HashMap::new(), 100 retained_roots: HashSet::new(), 101 next_world_id: 1, 102 } 103 } 104 105 pub fn create_root_world( 106 &mut self, 107 token_root: u64, 108 memory_root: u64, 109 capability_set: u64, 110 timestamp: u64, 111 ) -> u64 { 112 let world_id = self.next_world_id; 113 self.next_world_id += 1; 114 115 let manifest = WorldManifest { 116 id: world_id, 117 parent: None, 118 object_root: [0u8; 32], 119 filesystem_root: [0u8; 32], 120 token_root, 121 memory_root, 122 capability_set, 123 effect_log: Vec::new(), 124 created_at: timestamp, 125 }; 126 127 self.worlds.insert(world_id, manifest); 128 self.retained_roots.insert(world_id); 129 world_id 130 } 131 132 /// Fast constant-time fork: duplicates root pointers into a new WorldManifest. 133 pub fn fork_world(&mut self, parent_id: u64, timestamp: u64) -> Result { 134 let parent = self 135 .worlds 136 .get(&parent_id) 137 .ok_or_else(|| format!("Parent world {} not found", parent_id))? 138 .clone(); 139 140 let child_id = self.next_world_id; 141 self.next_world_id += 1; 142 143 let child = WorldManifest { 144 id: child_id, 145 parent: Some(parent_id), 146 object_root: parent.object_root, 147 filesystem_root: parent.filesystem_root, 148 token_root: parent.token_root, 149 memory_root: parent.memory_root, 150 capability_set: parent.capability_set, 151 effect_log: Vec::new(), // Staged effects begin empty for child 152 created_at: timestamp, 153 }; 154 155 self.worlds.insert(child_id, child); 156 self.retained_roots.insert(child_id); 157 Ok(child_id) 158 } 159 160 pub fn commit_draft(&mut self, draft: WorldDraft, timestamp: u64) -> Result { 161 let parent = self 162 .worlds 163 .get(&draft.base_world_id) 164 .ok_or_else(|| format!("Base world {} not found", draft.base_world_id))? 165 .clone(); 166 167 // Canonical content hash: SHA-256 over sorted (key, blob) entries, 168 // folded into the parent root. Deterministic regardless of HashMap order. 169 let mut keys: Vec<&String> = draft.staged_objects.keys().collect(); 170 keys.sort(); 171 let mut root_hasher = sha2::Sha256::new(); 172 root_hasher.update(parent.object_root); 173 for key in keys { 174 let data = &draft.staged_objects[key]; 175 let mut blob_hasher = sha2::Sha256::new(); 176 blob_hasher.update((key.len() as u64).to_le_bytes()); 177 blob_hasher.update(key.as_bytes()); 178 blob_hasher.update((data.len() as u64).to_le_bytes()); 179 blob_hasher.update(data); 180 let blob_hash: [u8; 32] = blob_hasher.finalize().into(); 181 root_hasher.update(blob_hash); 182 self.objects.insert(blob_hash, data.clone()); 183 } 184 let object_root: [u8; 32] = root_hasher.finalize().into(); 185 186 let new_world_id = self.next_world_id; 187 self.next_world_id += 1; 188 189 let manifest = WorldManifest { 190 id: new_world_id, 191 parent: Some(draft.base_world_id), 192 object_root, 193 filesystem_root: parent.filesystem_root, 194 token_root: parent.token_root, 195 memory_root: parent.memory_root, 196 capability_set: parent.capability_set, 197 effect_log: draft.staged_effects, 198 created_at: timestamp, 199 }; 200 201 self.worlds.insert(new_world_id, manifest); 202 self.retained_roots.insert(new_world_id); 203 Ok(new_world_id) 204 } 205 206 pub fn get_world(&self, world_id: u64) -> Option<&WorldManifest> { 207 self.worlds.get(&world_id) 208 } 209 210 pub fn drop_world(&mut self, world_id: u64) -> bool { 211 self.retained_roots.remove(&world_id); 212 self.worlds.remove(&world_id).is_some() 213 } 214 215 /// Release a world and prune objects no longer reachable from any live world. 216 /// Returns the number of objects pruned. 217 pub fn release_world(&mut self, world_id: u64) -> usize { 218 self.drop_world(world_id); 219 self.collect_garbage() 220 } 221 222 /// Removes objects unreachable from any live world root. 223 /// Reachability is approximated by object roots referenced in live manifests. 224 pub fn collect_garbage(&mut self) -> usize { 225 let retained: HashSet = self.retained_roots.clone(); 226 let before = self.worlds.len(); 227 self.worlds.retain(|id, _| retained.contains(id)); 228 // Blob level pruning waits on the durable manifest with parent links. 229 // Until then only unretained worlds are reclaimed, and the count of 230 // pruned worlds is reported honestly. 231 before - self.worlds.len() 232 } 233 234 pub fn active_world_count(&self) -> usize { 235 self.worlds.len() 236 } 237 }