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 / visible.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 | //! Crumb v1 learner-visible contract. |
| 17 | //! |
| 18 | //! The visible side is an ordered list of opaque `INPUT_BYTES -> OUTPUT_BYTES` |
| 19 | //! examples plus the minimum structure needed to parse them. It has no field |
| 20 | //! that could carry a name, topic, family, difficulty, population, decoy flag, |
| 21 | //! provenance, explanation, solution or held-out example. |
| 22 | //! |
| 23 | //! Canonical bytes (little-endian): |
| 24 | //! |
| 25 | //! ```text |
| 26 | //! "CRB1" magic |
| 27 | //! schema_version u16 = 1 |
| 28 | //! encoding u8 1 = decimal UTF-8 lanes, 2 = raw little-endian lanes |
| 29 | //! flags u8 bit0 = budget present; other bits must be 0 |
| 30 | //! in_arity u8 lanes per input (1..=8) |
| 31 | //! out_arity u8 lanes per output (1..=8) |
| 32 | //! in_lane_bytes u8 raw: 1,2,4,8; decimal: 0 |
| 33 | //! out_lane_bytes u8 raw: 1,2,4,8; decimal: 0 |
| 34 | //! n u32 examples (1..=64) |
| 35 | //! n x (u32 len, input bytes, u32 len, output bytes) |
| 36 | //! [max_candidates u32, max_depth u32, max_oracle_queries u32, max_program_ops u32] |
| 37 | //! ``` |
| 38 | //! |
| 39 | //! Decimal lanes are canonical base-10 u64 values (no sign, no leading zeros) |
| 40 | //! separated by one ASCII space. `CrumbDigest` = BLAKE3 over exactly these |
| 41 | //! bytes, so it covers the schema version and nothing else. |
| 42 | |
| 43 | use crate::canon::{Dec, DecodeError, Enc}; |
| 44 | use crate::digest::{digest, CrumbDigest, Digest, Domain}; |
| 45 | |
| 46 | pub const MAGIC: &[u8; 4] = b"CRB1"; |
| 47 | pub const SCHEMA_VERSION: u16 = 1; |
| 48 | pub const MAX_ARITY: u8 = 8; |
| 49 | pub const MAX_EXAMPLES: usize = 64; |
| 50 | const FLAG_BUDGET: u8 = 1; |
| 51 | |
| 52 | #[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 53 | pub enum Encoding { |
| 54 | DecimalUtf8 = 1, |
| 55 | RawLe = 2, |
| 56 | } |
| 57 | |
| 58 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)] |
| 59 | pub struct Budget { |
| 60 | pub max_candidates: u32, |
| 61 | pub max_depth: u32, |
| 62 | pub max_oracle_queries: u32, |
| 63 | pub max_program_ops: u32, |
| 64 | } |
| 65 | |
| 66 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 67 | pub struct Example { |
| 68 | pub input: Vec<u8>, |
| 69 | pub output: Vec<u8>, |
| 70 | } |
| 71 | |
| 72 | /// The only problem representation a learner may receive. |
| 73 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 74 | pub struct VisibleCrumb { |
| 75 | pub encoding: Encoding, |
| 76 | pub in_arity: u8, |
| 77 | pub out_arity: u8, |
| 78 | pub in_lane_bytes: u8, |
| 79 | pub out_lane_bytes: u8, |
| 80 | pub examples: Vec<Example>, |
| 81 | pub budget: Option<Budget>, |
| 82 | } |
| 83 | |
| 84 | /// Visible-side errors are bare codes: they cannot carry crumb content. |
| 85 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 86 | pub enum VisibleError { |
| 87 | Magic, |
| 88 | Version, |
| 89 | Shape, |
| 90 | Lane, |
| 91 | Length, |
| 92 | NonCanonical, |
| 93 | } |
| 94 | |
| 95 | impl std::fmt::Display for VisibleError { |
| 96 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 97 | write!(f, "visible crumb rejected: {self:?}") |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | impl std::error::Error for VisibleError {} |
| 102 | |
| 103 | impl From<DecodeError> for VisibleError { |
| 104 | fn from(_: DecodeError) -> Self { |
| 105 | VisibleError::Length |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn lane_bytes_ok(encoding: Encoding, b: u8) -> bool { |
| 110 | match encoding { |
| 111 | Encoding::DecimalUtf8 => b == 0, |
| 112 | Encoding::RawLe => matches!(b, 1 | 2 | 4 | 8), |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub fn lane_mask(lane_bytes: u8) -> u64 { |
| 117 | if lane_bytes == 0 || lane_bytes >= 8 { |
| 118 | u64::MAX |
| 119 | } else { |
| 120 | (1u64 << (8 * lane_bytes as u32)) - 1 |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// Encode lane values into example bytes. |
| 125 | pub fn encode_lanes(encoding: Encoding, lane_bytes: u8, lanes: &[u64]) -> Vec<u8> { |
| 126 | match encoding { |
| 127 | Encoding::DecimalUtf8 => lanes |
| 128 | .iter() |
| 129 | .map(|v| v.to_string()) |
| 130 | .collect::<Vec<_>>() |
| 131 | .join(" ") |
| 132 | .into_bytes(), |
| 133 | Encoding::RawLe => { |
| 134 | let mut out = Vec::with_capacity(lanes.len() * lane_bytes as usize); |
| 135 | for v in lanes { |
| 136 | out.extend_from_slice(&v.to_le_bytes()[..lane_bytes as usize]); |
| 137 | } |
| 138 | out |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /// Strictly parse example bytes into lane values. |
| 144 | pub fn decode_lanes( |
| 145 | encoding: Encoding, |
| 146 | lane_bytes: u8, |
| 147 | arity: u8, |
| 148 | bytes: &[u8], |
| 149 | ) -> Result<Vec<u64>, VisibleError> { |
| 150 | match encoding { |
| 151 | Encoding::DecimalUtf8 => { |
| 152 | let text = std::str::from_utf8(bytes).map_err(|_| VisibleError::Lane)?; |
| 153 | let parts: Vec<&str> = text.split(' ').collect(); |
| 154 | if parts.len() != arity as usize { |
| 155 | return Err(VisibleError::Lane); |
| 156 | } |
| 157 | let mut out = Vec::with_capacity(parts.len()); |
| 158 | for p in parts { |
| 159 | if p.is_empty() |
| 160 | || !p.bytes().all(|c| c.is_ascii_digit()) |
| 161 | || (p.len() > 1 && p.starts_with('0')) |
| 162 | { |
| 163 | return Err(VisibleError::NonCanonical); |
| 164 | } |
| 165 | out.push(p.parse::<u64>().map_err(|_| VisibleError::Lane)?); |
| 166 | } |
| 167 | Ok(out) |
| 168 | } |
| 169 | Encoding::RawLe => { |
| 170 | let lb = lane_bytes as usize; |
| 171 | if bytes.len() != lb * arity as usize { |
| 172 | return Err(VisibleError::Lane); |
| 173 | } |
| 174 | Ok(bytes |
| 175 | .chunks(lb) |
| 176 | .map(|c| { |
| 177 | let mut b = [0u8; 8]; |
| 178 | b[..lb].copy_from_slice(c); |
| 179 | u64::from_le_bytes(b) |
| 180 | }) |
| 181 | .collect()) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | impl VisibleCrumb { |
| 187 | /// Build a crumb from lane values; values are masked to the lane width. |
| 188 | pub fn from_values( |
| 189 | encoding: Encoding, |
| 190 | in_lane_bytes: u8, |
| 191 | out_lane_bytes: u8, |
| 192 | pairs: &[(Vec<u64>, Vec<u64>)], |
| 193 | budget: Option<Budget>, |
| 194 | ) -> Result<Self, VisibleError> { |
| 195 | if pairs.is_empty() || pairs.len() > MAX_EXAMPLES { |
| 196 | return Err(VisibleError::Length); |
| 197 | } |
| 198 | let in_arity = pairs[0].0.len(); |
| 199 | let out_arity = pairs[0].1.len(); |
| 200 | if in_arity == 0 |
| 201 | || out_arity == 0 |
| 202 | || in_arity > MAX_ARITY as usize |
| 203 | || out_arity > MAX_ARITY as usize |
| 204 | { |
| 205 | return Err(VisibleError::Shape); |
| 206 | } |
| 207 | if !lane_bytes_ok(encoding, in_lane_bytes) || !lane_bytes_ok(encoding, out_lane_bytes) { |
| 208 | return Err(VisibleError::Shape); |
| 209 | } |
| 210 | let (im, om) = (lane_mask(in_lane_bytes), lane_mask(out_lane_bytes)); |
| 211 | let mut examples = Vec::with_capacity(pairs.len()); |
| 212 | for (i, o) in pairs { |
| 213 | if i.len() != in_arity || o.len() != out_arity { |
| 214 | return Err(VisibleError::Shape); |
| 215 | } |
| 216 | let i: Vec<u64> = i.iter().map(|v| v & im).collect(); |
| 217 | let o: Vec<u64> = o.iter().map(|v| v & om).collect(); |
| 218 | examples.push(Example { |
| 219 | input: encode_lanes(encoding, in_lane_bytes, &i), |
| 220 | output: encode_lanes(encoding, out_lane_bytes, &o), |
| 221 | }); |
| 222 | } |
| 223 | Ok(VisibleCrumb { |
| 224 | encoding, |
| 225 | in_arity: in_arity as u8, |
| 226 | out_arity: out_arity as u8, |
| 227 | in_lane_bytes, |
| 228 | out_lane_bytes, |
| 229 | examples, |
| 230 | budget, |
| 231 | }) |
| 232 | } |
| 233 | |
| 234 | pub fn to_bytes(&self) -> Vec<u8> { |
| 235 | let mut e = Enc::new(); |
| 236 | e.bytes(MAGIC).u16(SCHEMA_VERSION).u8(self.encoding as u8); |
| 237 | e.u8(if self.budget.is_some() { |
| 238 | FLAG_BUDGET |
| 239 | } else { |
| 240 | 0 |
| 241 | }); |
| 242 | e.u8(self.in_arity) |
| 243 | .u8(self.out_arity) |
| 244 | .u8(self.in_lane_bytes) |
| 245 | .u8(self.out_lane_bytes); |
| 246 | e.u32(self.examples.len() as u32); |
| 247 | for ex in &self.examples { |
| 248 | e.blob(&ex.input).blob(&ex.output); |
| 249 | } |
| 250 | if let Some(b) = &self.budget { |
| 251 | e.u32(b.max_candidates) |
| 252 | .u32(b.max_depth) |
| 253 | .u32(b.max_oracle_queries) |
| 254 | .u32(b.max_program_ops); |
| 255 | } |
| 256 | e.finish() |
| 257 | } |
| 258 | |
| 259 | /// Strict decode: the bytes must be exactly what `to_bytes` would produce. |
| 260 | pub fn from_bytes(bytes: &[u8]) -> Result<Self, VisibleError> { |
| 261 | let mut d = Dec::new(bytes); |
| 262 | if d.take(4)? != MAGIC { |
| 263 | return Err(VisibleError::Magic); |
| 264 | } |
| 265 | if d.u16()? != SCHEMA_VERSION { |
| 266 | return Err(VisibleError::Version); |
| 267 | } |
| 268 | let encoding = match d.u8()? { |
| 269 | 1 => Encoding::DecimalUtf8, |
| 270 | 2 => Encoding::RawLe, |
| 271 | _ => return Err(VisibleError::Shape), |
| 272 | }; |
| 273 | let flags = d.u8()?; |
| 274 | if flags & !FLAG_BUDGET != 0 { |
| 275 | return Err(VisibleError::NonCanonical); |
| 276 | } |
| 277 | let (in_arity, out_arity, in_lb, out_lb) = (d.u8()?, d.u8()?, d.u8()?, d.u8()?); |
| 278 | if !(1..=MAX_ARITY).contains(&in_arity) || !(1..=MAX_ARITY).contains(&out_arity) { |
| 279 | return Err(VisibleError::Shape); |
| 280 | } |
| 281 | if !lane_bytes_ok(encoding, in_lb) || !lane_bytes_ok(encoding, out_lb) { |
| 282 | return Err(VisibleError::Shape); |
| 283 | } |
| 284 | let n = d.u32()? as usize; |
| 285 | if n == 0 || n > MAX_EXAMPLES { |
| 286 | return Err(VisibleError::Length); |
| 287 | } |
| 288 | let mut examples = Vec::with_capacity(n); |
| 289 | for _ in 0..n { |
| 290 | let input = d.blob(8 * 21)?.to_vec(); |
| 291 | let output = d.blob(8 * 21)?.to_vec(); |
| 292 | decode_lanes(encoding, in_lb, in_arity, &input)?; |
| 293 | decode_lanes(encoding, out_lb, out_arity, &output)?; |
| 294 | examples.push(Example { input, output }); |
| 295 | } |
| 296 | let budget = if flags & FLAG_BUDGET != 0 { |
| 297 | Some(Budget { |
| 298 | max_candidates: d.u32()?, |
| 299 | max_depth: d.u32()?, |
| 300 | max_oracle_queries: d.u32()?, |
| 301 | max_program_ops: d.u32()?, |
| 302 | }) |
| 303 | } else { |
| 304 | None |
| 305 | }; |
| 306 | d.finish()?; |
| 307 | let c = VisibleCrumb { |
| 308 | encoding, |
| 309 | in_arity, |
| 310 | out_arity, |
| 311 | in_lane_bytes: in_lb, |
| 312 | out_lane_bytes: out_lb, |
| 313 | examples, |
| 314 | budget, |
| 315 | }; |
| 316 | if c.to_bytes() != bytes { |
| 317 | return Err(VisibleError::NonCanonical); |
| 318 | } |
| 319 | Ok(c) |
| 320 | } |
| 321 | |
| 322 | pub fn digest(&self) -> CrumbDigest { |
| 323 | digest(Domain::CrumbDigest, &self.to_bytes()) |
| 324 | } |
| 325 | |
| 326 | /// Digest of the example list alone (the "visible-set digest" in evidence). |
| 327 | pub fn visible_set_digest(&self) -> Digest { |
| 328 | let mut e = Enc::new(); |
| 329 | e.u32(self.examples.len() as u32); |
| 330 | for ex in &self.examples { |
| 331 | e.blob(&ex.input).blob(&ex.output); |
| 332 | } |
| 333 | digest(Domain::VisibleSet, &e.finish()) |
| 334 | } |
| 335 | |
| 336 | /// Parsed lane values of every example. |
| 337 | pub fn values(&self) -> Vec<(Vec<u64>, Vec<u64>)> { |
| 338 | self.examples |
| 339 | .iter() |
| 340 | .map(|ex| { |
| 341 | ( |
| 342 | decode_lanes(self.encoding, self.in_lane_bytes, self.in_arity, &ex.input) |
| 343 | .expect("validated"), |
| 344 | decode_lanes( |
| 345 | self.encoding, |
| 346 | self.out_lane_bytes, |
| 347 | self.out_arity, |
| 348 | &ex.output, |
| 349 | ) |
| 350 | .expect("validated"), |
| 351 | ) |
| 352 | }) |
| 353 | .collect() |
| 354 | } |
| 355 | } |