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/crumbs/src/mixer.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 / crumbs / src / mixer.rs` 350 lines ยท 12597 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 //! The population mixer (sealed side). 17 //! 18 //! Crumbs are drawn from five secret populations with configurable weights 19 //! (initial v1 schedule 55/20/10/10/5, loaded from data, not hard-coded 20 //! behaviour). The mix adapts to evidence: as capabilities are demonstrated, 21 //! weight moves from acquisition to composition. Within a population the 22 //! mixer balances new capabilities, retention retests, previously failed 23 //! capabilities and random draws. Consecutive draws never repeat a family or 24 //! a capability, so curriculum order carries no ontology. 25 //! 26 //! The learner never sees a population, a family, or the draw reason. 27 28 use crate::gen::rng::Rng; 29 use crate::gen::{registry, FamilySpec}; 30 use crate::provenance::clean_room_check; 31 use crate::sealed::{CapabilityId, Population, SourceFamily}; 32 use serde::{Deserialize, Serialize}; 33 use std::collections::{BTreeMap, HashMap}; 34 35 #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] 36 pub struct MixerConfig { 37 /// Percent weights per population. Need not sum to 100. 38 pub populations: BTreeMap, 39 /// Within the fundamental population: weights for new / retention / uncertain / random. 40 pub select_new: u32, 41 pub select_retention: u32, 42 pub select_uncertain: u32, 43 pub select_random: u32, 44 /// Fraction (percent) of acquisition weight moved to composition when every capability is demonstrated. 45 pub adapt_pct: u32, 46 /// Only families that pass the clean-room policy. 47 pub clean_room: bool, 48 /// Restrict capability-bearing families to these capability pool indices (None = all). 49 pub capability_subset: Option>, 50 /// Restrict composition families to these family keys (None = all ready ones). 51 pub composition_keys: Option>, 52 } 53 54 impl Default for MixerConfig { 55 fn default() -> Self { 56 let mut populations = BTreeMap::new(); 57 populations.insert(Population::Fundamental, 55); 58 populations.insert(Population::Composition, 20); 59 populations.insert(Population::Ambiguous, 10); 60 populations.insert(Population::RabbitHole, 10); 61 populations.insert(Population::Frontier, 5); 62 MixerConfig { 63 populations, 64 select_new: 40, 65 select_retention: 20, 66 select_uncertain: 20, 67 select_random: 20, 68 adapt_pct: 50, 69 clean_room: false, 70 capability_subset: None, 71 composition_keys: None, 72 } 73 } 74 } 75 76 impl MixerConfig { 77 pub fn from_json(s: &str) -> Result { 78 serde_json::from_str(s) 79 } 80 } 81 82 /// Evidence about one capability, updated from sealed evaluations. 83 #[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)] 84 pub struct CapabilityState { 85 pub attempts: u32, 86 pub verified: u32, 87 pub last_verified: bool, 88 } 89 90 impl CapabilityState { 91 pub fn demonstrated(&self) -> bool { 92 self.verified > 0 && self.last_verified 93 } 94 } 95 96 #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] 97 pub enum DrawReason { 98 NewCapability, 99 Retention, 100 Uncertain, 101 Random, 102 Composition, 103 Fallback, 104 } 105 106 #[derive(Clone, Debug, Serialize, Deserialize)] 107 pub struct Draw { 108 pub seq: u64, 109 pub family_id: u32, 110 pub seed: u64, 111 pub population: Population, 112 pub reason: DrawReason, 113 } 114 115 pub struct Mixer { 116 pub cfg: MixerConfig, 117 rng: Rng, 118 run_seed: u64, 119 seq: u64, 120 pub caps: HashMap, 121 last_family: Option, 122 last_caps: Vec, 123 /// Draws whose population had nothing eligible (empty, or only back-to-back repeats) and were redistributed. 124 pub redistributed_draws: BTreeMap, 125 } 126 127 fn population_of(f: &FamilySpec) -> Option { 128 Some(match f.source_family { 129 SourceFamily::Capability => Population::Fundamental, 130 SourceFamily::Composition => Population::Composition, 131 SourceFamily::Ambiguous => Population::Ambiguous, 132 SourceFamily::RabbitHole => Population::RabbitHole, 133 SourceFamily::Frontier => Population::Frontier, 134 SourceFamily::CleanRoomPhysics => return None, 135 }) 136 } 137 138 impl Mixer { 139 pub fn new(cfg: MixerConfig, run_seed: u64) -> Self { 140 Mixer { 141 cfg, 142 rng: Rng::named("mixer", &[run_seed]), 143 run_seed, 144 seq: 0, 145 caps: HashMap::new(), 146 last_family: None, 147 last_caps: vec![], 148 redistributed_draws: BTreeMap::new(), 149 } 150 } 151 152 fn family_caps(f: &FamilySpec) -> Vec { 153 let pool = crate::gen::capability_pool(); 154 f.params.caps.iter().map(|&c| pool[c as usize].id).collect() 155 } 156 157 fn eligible(&self, f: &FamilySpec) -> bool { 158 if self.cfg.clean_room && clean_room_check(&f.provenance).is_err() { 159 return false; 160 } 161 if let Some(sub) = &self.cfg.capability_subset { 162 if !f.params.caps.is_empty() && !f.params.caps.iter().all(|c| sub.contains(c)) { 163 return false; 164 } 165 } 166 if f.source_family == SourceFamily::Composition { 167 if let Some(keys) = &self.cfg.composition_keys { 168 if !keys.contains(&f.key) { 169 return false; 170 } 171 } 172 } 173 true 174 } 175 176 fn adjacent(&self, f: &FamilySpec) -> bool { 177 if self.last_family == Some(f.id) { 178 return true; 179 } 180 let caps = Self::family_caps(f); 181 caps.iter().any(|c| self.last_caps.contains(c)) 182 } 183 184 /// Current population weights after evidence-driven adaptation. 185 pub fn weights(&self) -> BTreeMap { 186 let mut w = self.cfg.populations.clone(); 187 let pool = crate::gen::capability_pool(); 188 let tracked: Vec<_> = pool 189 .iter() 190 .filter(|c| { 191 self.cfg 192 .capability_subset 193 .as_ref() 194 .is_none_or(|s| s.contains(&c.index)) 195 }) 196 .collect(); 197 if !tracked.is_empty() { 198 let demo = tracked 199 .iter() 200 .filter(|c| self.caps.get(&c.id).is_some_and(|s| s.demonstrated())) 201 .count(); 202 let fund = *w.get(&Population::Fundamental).unwrap_or(&0); 203 let moved = fund as u64 * self.cfg.adapt_pct as u64 * demo as u64 204 / (100 * tracked.len() as u64); 205 *w.entry(Population::Fundamental).or_insert(0) -= moved as u32; 206 *w.entry(Population::Composition).or_insert(0) += moved as u32; 207 } 208 w 209 } 210 211 fn pick_weighted(&mut self, items: &[(T, u32)]) -> Option { 212 let total: u64 = items.iter().map(|x| x.1 as u64).sum(); 213 if total == 0 { 214 return None; 215 } 216 let mut r = self.rng.below(total); 217 for &(t, w) in items { 218 if r < w as u64 { 219 return Some(t); 220 } 221 r -= w as u64; 222 } 223 None 224 } 225 226 fn candidates(&self, pop: Population) -> Vec<&'static FamilySpec> { 227 registry() 228 .families 229 .iter() 230 .filter(|f| population_of(f) == Some(pop) && self.eligible(f)) 231 .collect() 232 } 233 234 fn cap_state(&self, id: &CapabilityId) -> CapabilityState { 235 self.caps.get(id).copied().unwrap_or_default() 236 } 237 238 /// Choose a family within a population, or None if the population is empty. 239 fn choose_in(&mut self, pop: Population) -> Option<(&'static FamilySpec, DrawReason)> { 240 let all = self.candidates(pop); 241 if all.is_empty() { 242 return None; 243 } 244 let pools: Vec<(DrawReason, Vec<&'static FamilySpec>)> = match pop { 245 Population::Fundamental => { 246 let with_cap = 247 |pred: &dyn Fn(CapabilityState) -> bool| -> Vec<&'static FamilySpec> { 248 all.iter() 249 .copied() 250 .filter(|f| { 251 f.params.caps.len() == 1 252 && pred(self.cap_state(&Self::family_caps(f)[0])) 253 }) 254 .collect() 255 }; 256 vec![ 257 (DrawReason::NewCapability, with_cap(&|s| s.attempts == 0)), 258 (DrawReason::Retention, with_cap(&|s| s.demonstrated())), 259 ( 260 DrawReason::Uncertain, 261 with_cap(&|s| s.attempts > 0 && !s.demonstrated()), 262 ), 263 (DrawReason::Random, all.clone()), 264 ] 265 } 266 Population::Composition => { 267 let ready: Vec<_> = all 268 .iter() 269 .copied() 270 .filter(|f| { 271 Self::family_caps(f) 272 .iter() 273 .all(|c| self.cap_state(c).demonstrated()) 274 }) 275 .collect(); 276 vec![(DrawReason::Composition, ready)] 277 } 278 _ => vec![(DrawReason::Random, all.clone())], 279 }; 280 // Never repeat a family or a capability back to back. 281 let pools: Vec<(DrawReason, Vec<&'static FamilySpec>)> = pools 282 .into_iter() 283 .map(|(r, v)| (r, v.into_iter().filter(|f| !self.adjacent(f)).collect())) 284 .collect(); 285 let weights = [ 286 self.cfg.select_new, 287 self.cfg.select_retention, 288 self.cfg.select_uncertain, 289 self.cfg.select_random, 290 ]; 291 let options: Vec<(usize, u32)> = pools 292 .iter() 293 .enumerate() 294 .filter(|(_, (_, v))| !v.is_empty()) 295 .map(|(i, _)| (i, if pools.len() == 4 { weights[i] } else { 1 })) 296 .collect(); 297 let i = self.pick_weighted(&options)?; 298 let (reason, fams) = &pools[i]; 299 let f = fams[self.rng.below(fams.len() as u64) as usize]; 300 Some((f, *reason)) 301 } 302 303 pub fn draw(&mut self) -> Draw { 304 let seq = self.seq; 305 self.seq += 1; 306 let weights: Vec<(Population, u32)> = self.weights().into_iter().collect(); 307 let mut pop = self 308 .pick_weighted(&weights) 309 .unwrap_or(Population::Fundamental); 310 let mut chosen = self.choose_in(pop); 311 if chosen.is_none() { 312 // Empty population (e.g. no v1 frontier sources, or nothing ready to 313 // compose): record it and redistribute over the populations that can draw. 314 *self.redistributed_draws.entry(pop).or_insert(0) += 1; 315 let rest: Vec<(Population, u32)> = 316 weights.iter().copied().filter(|(p, _)| *p != pop).collect(); 317 for _ in 0..8 { 318 pop = self.pick_weighted(&rest).unwrap_or(Population::Fundamental); 319 chosen = self.choose_in(pop); 320 if chosen.is_some() { 321 break; 322 } 323 } 324 } 325 let (f, reason) = chosen.unwrap_or_else(|| (®istry().families[0], DrawReason::Fallback)); 326 self.last_family = Some(f.id); 327 self.last_caps = Self::family_caps(f); 328 let seed = Rng::named("draw-seed", &[self.run_seed, seq]).next_u64(); 329 Draw { 330 seq, 331 family_id: f.id, 332 seed, 333 population: pop, 334 reason, 335 } 336 } 337 338 /// Feed back a sealed verdict for a drawn family. 339 pub fn record(&mut self, family: &FamilySpec, verified: bool) { 340 if family.params.caps.len() == 1 { 341 let id = Self::family_caps(family)[0]; 342 let s = self.caps.entry(id).or_default(); 343 s.attempts += 1; 344 if verified { 345 s.verified += 1; 346 } 347 s.last_verified = verified; 348 } 349 } 350 }