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
aien-sovereign-core-mv / crates / crumbs / src / promotion.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 | //! The promotion ladder (sealed side). |
| 17 | //! |
| 18 | //! One solved crumb never makes an operation universal. An operation climbs: |
| 19 | //! |
| 20 | //! ```text |
| 21 | //! candidate operation sub-chain of an accepted solution |
| 22 | //! -> held-out verified part of a solution the sealed verifier accepted |
| 23 | //! -> corroborated supports >= min_support distinct crumbs |
| 24 | //! -> survived adversarial every support passed a non-empty adversarial tier, |
| 25 | //! and rejected-submission exposures stay bounded |
| 26 | //! -> generalized every support passed a non-empty extrapolation tier |
| 27 | //! -> admitted sent to the learner's library with an explicit scope |
| 28 | //! ``` |
| 29 | //! |
| 30 | //! Scope is recorded, not assumed: `scope_bits` is the widest input magnitude |
| 31 | //! (in bits) over which a supporting solution was verified. A learner may only |
| 32 | //! apply an admitted operation to crumbs inside that scope. |
| 33 | |
| 34 | use crate::canon::Enc; |
| 35 | use crate::digest::{digest, Digest, Domain}; |
| 36 | use crate::program::Program; |
| 37 | use crate::sealed::{HeldoutTier, SealedCrumb}; |
| 38 | use crate::verify::{EvalClass, SealedEvaluation}; |
| 39 | use serde::{Deserialize, Serialize}; |
| 40 | use std::collections::BTreeMap; |
| 41 | |
| 42 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] |
| 43 | pub enum Stage { |
| 44 | Candidate = 1, |
| 45 | HeldoutVerified = 3, |
| 46 | Corroborated = 4, |
| 47 | SurvivedAdversarial = 5, |
| 48 | Generalized = 6, |
| 49 | Admitted = 7, |
| 50 | Suspended = 9, |
| 51 | } |
| 52 | |
| 53 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 54 | pub struct PromotionConfig { |
| 55 | pub min_len: usize, |
| 56 | pub max_len: usize, |
| 57 | pub min_support: usize, |
| 58 | /// Suspend when exposures in rejected submissions exceed this percent of supports. |
| 59 | pub max_exposure_pct: u32, |
| 60 | } |
| 61 | |
| 62 | impl Default for PromotionConfig { |
| 63 | fn default() -> Self { |
| 64 | PromotionConfig { |
| 65 | min_len: 2, |
| 66 | max_len: 6, |
| 67 | min_support: 2, |
| 68 | max_exposure_pct: 200, |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 74 | pub struct Support { |
| 75 | pub seq: u64, |
| 76 | pub crumb: Digest, |
| 77 | pub family_id: u32, |
| 78 | pub evaluation: Digest, |
| 79 | pub adversarial_total: u32, |
| 80 | pub extrapolation_total: u32, |
| 81 | pub max_input_bits: u8, |
| 82 | } |
| 83 | |
| 84 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 85 | pub struct OpRecord { |
| 86 | pub behavior: Digest, |
| 87 | pub program: Program, |
| 88 | pub stage: Stage, |
| 89 | pub supports: Vec<Support>, |
| 90 | pub exposures: Vec<Digest>, |
| 91 | pub scope_bits: u8, |
| 92 | pub first_seen_seq: u64, |
| 93 | pub op_ref: Option<u32>, |
| 94 | pub history: Vec<(Stage, u64)>, |
| 95 | } |
| 96 | |
| 97 | impl OpRecord { |
| 98 | pub fn distinct_crumbs(&self) -> usize { |
| 99 | let mut v: Vec<_> = self.supports.iter().map(|s| s.crumb).collect(); |
| 100 | v.sort(); |
| 101 | v.dedup(); |
| 102 | v.len() |
| 103 | } |
| 104 | |
| 105 | /// Commitment to the operation, its evidence and its scope. |
| 106 | pub fn promotion_digest(&self) -> Digest { |
| 107 | let mut e = Enc::new(); |
| 108 | e.blob(&self.program.to_bytes()) |
| 109 | .u8(self.stage as u8) |
| 110 | .u8(self.scope_bits); |
| 111 | e.u32(self.supports.len() as u32); |
| 112 | for s in &self.supports { |
| 113 | e.digest(&s.crumb).digest(&s.evaluation); |
| 114 | } |
| 115 | e.u32(self.exposures.len() as u32); |
| 116 | for x in &self.exposures { |
| 117 | e.digest(x); |
| 118 | } |
| 119 | digest(Domain::Promotion, &e.finish()) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// A newly admitted operation, to be sent to a learner that may reuse it. |
| 124 | #[derive(Clone, Debug, Serialize, Deserialize)] |
| 125 | pub struct Admission { |
| 126 | pub op_ref: u32, |
| 127 | pub program: Program, |
| 128 | pub scope_bits: u8, |
| 129 | pub promotion_digest: Digest, |
| 130 | } |
| 131 | |
| 132 | #[derive(Default, Serialize, Deserialize)] |
| 133 | pub struct Ladder { |
| 134 | pub cfg: PromotionConfig, |
| 135 | pub ops: BTreeMap<Digest, OpRecord>, |
| 136 | next_ref: u32, |
| 137 | } |
| 138 | |
| 139 | fn bits(x: u64) -> u8 { |
| 140 | (64 - x.leading_zeros()) as u8 |
| 141 | } |
| 142 | |
| 143 | impl Ladder { |
| 144 | pub fn new(cfg: PromotionConfig) -> Self { |
| 145 | Ladder { |
| 146 | cfg, |
| 147 | ops: BTreeMap::new(), |
| 148 | next_ref: 1, |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | fn candidates_of(&self, program: &Program) -> Vec<Program> { |
| 153 | let mut out = program.sub_chains(self.cfg.min_len, self.cfg.max_len); |
| 154 | out.sort(); |
| 155 | out.dedup(); |
| 156 | out |
| 157 | } |
| 158 | |
| 159 | /// An accepted solution: every sub-chain is (evidence for) a candidate operation. |
| 160 | pub fn observe_accept( |
| 161 | &mut self, |
| 162 | seq: u64, |
| 163 | sealed: &SealedCrumb, |
| 164 | eval: &SealedEvaluation, |
| 165 | program: &Program, |
| 166 | ) { |
| 167 | if !matches!(eval.class, EvalClass::Verified) { |
| 168 | return; // ambiguous acceptances are not evidence for reuse |
| 169 | } |
| 170 | let max_in = HeldoutTier::ALL |
| 171 | .iter() |
| 172 | .flat_map(|t| sealed.heldouts.tier(*t)) |
| 173 | .map(|ex| ex.input[0]) |
| 174 | .max() |
| 175 | .unwrap_or(0); |
| 176 | for cand in self.candidates_of(program) { |
| 177 | let b = cand.behavior(); |
| 178 | let rec = self.ops.entry(b).or_insert_with(|| OpRecord { |
| 179 | behavior: b, |
| 180 | program: cand.clone(), |
| 181 | stage: Stage::Candidate, |
| 182 | supports: vec![], |
| 183 | exposures: vec![], |
| 184 | scope_bits: 0, |
| 185 | first_seen_seq: seq, |
| 186 | op_ref: None, |
| 187 | history: vec![(Stage::Candidate, seq)], |
| 188 | }); |
| 189 | if cand.len() < rec.program.len() { |
| 190 | rec.program = cand.clone(); |
| 191 | } |
| 192 | if rec.supports.iter().any(|s| s.crumb == eval.crumb_digest) { |
| 193 | continue; |
| 194 | } |
| 195 | rec.supports.push(Support { |
| 196 | seq, |
| 197 | crumb: eval.crumb_digest, |
| 198 | family_id: sealed.family_id, |
| 199 | evaluation: eval.digest(), |
| 200 | adversarial_total: eval.adversarial.total, |
| 201 | extrapolation_total: eval.extrapolation.total, |
| 202 | max_input_bits: bits(max_in), |
| 203 | }); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /// A submission that fit the visible examples but failed held-outs. |
| 208 | pub fn observe_reject(&mut self, eval: &SealedEvaluation, program: &Program) { |
| 209 | if !matches!(eval.class, EvalClass::FalsifiedHidden) { |
| 210 | return; |
| 211 | } |
| 212 | for cand in self.candidates_of(program) { |
| 213 | if let Some(rec) = self.ops.get_mut(&cand.behavior()) { |
| 214 | rec.exposures.push(eval.digest()); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Climb every operation as far as its evidence allows. Returns operations |
| 220 | /// that reached `Admitted` in this call. |
| 221 | pub fn advance(&mut self, seq: u64) -> Vec<Admission> { |
| 222 | let mut admitted = Vec::new(); |
| 223 | let cfg = self.cfg.clone(); |
| 224 | for rec in self.ops.values_mut() { |
| 225 | if rec.stage == Stage::Admitted || rec.stage == Stage::Suspended { |
| 226 | continue; |
| 227 | } |
| 228 | let supports = rec.distinct_crumbs(); |
| 229 | if supports > 0 && rec.exposures.len() * 100 > supports * cfg.max_exposure_pct as usize |
| 230 | { |
| 231 | rec.stage = Stage::Suspended; |
| 232 | rec.history.push((Stage::Suspended, seq)); |
| 233 | continue; |
| 234 | } |
| 235 | let mut next = Stage::Candidate; |
| 236 | if supports >= 1 { |
| 237 | next = Stage::HeldoutVerified; |
| 238 | } |
| 239 | if next == Stage::HeldoutVerified && supports >= cfg.min_support { |
| 240 | next = Stage::Corroborated; |
| 241 | } |
| 242 | if next == Stage::Corroborated && rec.supports.iter().all(|s| s.adversarial_total > 0) { |
| 243 | next = Stage::SurvivedAdversarial; |
| 244 | } |
| 245 | if next == Stage::SurvivedAdversarial |
| 246 | && rec.supports.iter().all(|s| s.extrapolation_total > 0) |
| 247 | { |
| 248 | next = Stage::Generalized; |
| 249 | } |
| 250 | if next == Stage::Generalized { |
| 251 | next = Stage::Admitted; |
| 252 | } |
| 253 | // Record every rung crossed, in order. |
| 254 | for st in [ |
| 255 | Stage::HeldoutVerified, |
| 256 | Stage::Corroborated, |
| 257 | Stage::SurvivedAdversarial, |
| 258 | Stage::Generalized, |
| 259 | Stage::Admitted, |
| 260 | ] { |
| 261 | if st > rec.stage && st <= next { |
| 262 | rec.history.push((st, seq)); |
| 263 | } |
| 264 | } |
| 265 | if next > rec.stage { |
| 266 | rec.stage = next; |
| 267 | } |
| 268 | rec.scope_bits = rec |
| 269 | .supports |
| 270 | .iter() |
| 271 | .map(|s| s.max_input_bits) |
| 272 | .max() |
| 273 | .unwrap_or(0); |
| 274 | if rec.stage == Stage::Admitted && rec.op_ref.is_none() { |
| 275 | let r = self.next_ref; |
| 276 | self.next_ref += 1; |
| 277 | rec.op_ref = Some(r); |
| 278 | admitted.push(Admission { |
| 279 | op_ref: r, |
| 280 | program: rec.program.clone(), |
| 281 | scope_bits: rec.scope_bits, |
| 282 | promotion_digest: rec.promotion_digest(), |
| 283 | }); |
| 284 | } |
| 285 | } |
| 286 | admitted |
| 287 | } |
| 288 | |
| 289 | pub fn by_ref(&self, op_ref: u32) -> Option<&OpRecord> { |
| 290 | self.ops.values().find(|r| r.op_ref == Some(op_ref)) |
| 291 | } |
| 292 | |
| 293 | pub fn count(&self, stage: Stage) -> usize { |
| 294 | self.ops.values().filter(|r| r.stage == stage).count() |
| 295 | } |
| 296 | } |