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 / verify.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 generic verifier contract (sealed side). |
| 17 | //! |
| 18 | //! A learner's candidate arrives as a CPG1 `Program`. The verifier executes it |
| 19 | //! with this crate's own interpreter against the visible examples and the |
| 20 | //! three sealed held-out tiers, and produces a `SealedEvaluation`. The learner |
| 21 | //! receives exactly one byte derived from it (`learner_verdict`). |
| 22 | //! |
| 23 | //! Visible interpolation is never verification: a candidate is accepted only |
| 24 | //! if it also predicts every sealed held-out. |
| 25 | |
| 26 | use crate::canon::Enc; |
| 27 | use crate::digest::{digest, Digest, Domain}; |
| 28 | use crate::program::Program; |
| 29 | use crate::sealed::{AdversarialClass, DecoyStatus, HeldoutTier, SealedCrumb}; |
| 30 | use crate::visible::{lane_mask, Encoding, VisibleCrumb}; |
| 31 | use serde::{Deserialize, Serialize}; |
| 32 | |
| 33 | pub const VERIFIER_VERSION: &str = "crumbs-verifier/1.0.0"; |
| 34 | |
| 35 | /// Identity of the verification rules and interpreter. |
| 36 | pub fn verifier_digest() -> Digest { |
| 37 | let mut e = Enc::new(); |
| 38 | e.str(VERIFIER_VERSION).str(crate::program::std_interpreter_id()).str("accept=visible_ok&&all_heldout_tiers_pass;noise=mismatch_subset_of_sealed_noisy_indices"); |
| 39 | digest(Domain::Verifier, &e.finish()) |
| 40 | } |
| 41 | |
| 42 | /// The single byte a learner is told. |
| 43 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 44 | #[repr(u8)] |
| 45 | pub enum Verdict { |
| 46 | Accept = 1, |
| 47 | Reject = 2, |
| 48 | BudgetExhausted = 3, |
| 49 | } |
| 50 | |
| 51 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 52 | pub enum EvalClass { |
| 53 | /// Fits the visible examples and predicts every held-out. |
| 54 | Verified, |
| 55 | /// Does not even explain the visible examples. |
| 56 | FalsifiedVisible, |
| 57 | /// Explains the visible examples, fails held-outs: a recorded useful failure. |
| 58 | FalsifiedHidden, |
| 59 | /// Predicts the held-outs, but the visible evidence could not have |
| 60 | /// established it (insufficient-evidence crumb). |
| 61 | AmbiguousUnderdetermined, |
| 62 | /// The crumb's shape is outside CPG1 v1 (output arity != 1). |
| 63 | Unsupported, |
| 64 | } |
| 65 | |
| 66 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 67 | pub struct TierResult { |
| 68 | pub passed: u32, |
| 69 | pub total: u32, |
| 70 | } |
| 71 | |
| 72 | impl TierResult { |
| 73 | pub fn ok(&self) -> bool { |
| 74 | self.passed == self.total |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// Machine-readable evidence for one evaluation. |
| 79 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 80 | pub struct SealedEvaluation { |
| 81 | pub class: EvalClass, |
| 82 | pub verdict: Verdict, |
| 83 | pub visible_matches: u32, |
| 84 | pub visible_total: u32, |
| 85 | pub interpolation: TierResult, |
| 86 | pub extrapolation: TierResult, |
| 87 | pub adversarial: TierResult, |
| 88 | /// The visible evidence admitted rival explanations (sealed alternatives exist). |
| 89 | pub underdetermined: bool, |
| 90 | /// Insufficient-evidence crumbs only: did the learner report >= 2 rival fits? |
| 91 | pub ambiguity_recognized: Option<bool>, |
| 92 | /// Index of the sealed alternative this candidate behaves like on the held-outs. |
| 93 | pub matches_alternative: Option<u32>, |
| 94 | pub learner_hypotheses: u8, |
| 95 | pub robust_submission: bool, |
| 96 | pub crumb_digest: Digest, |
| 97 | pub sealed_digest: Digest, |
| 98 | pub generator_instance_digest: Digest, |
| 99 | pub program_digest: Digest, |
| 100 | pub visible_set_digest: Digest, |
| 101 | pub heldout_set_digest: Digest, |
| 102 | pub verifier_digest: Digest, |
| 103 | pub program_steps: u32, |
| 104 | pub evaluation_ops: u64, |
| 105 | } |
| 106 | |
| 107 | impl SealedEvaluation { |
| 108 | pub fn tier(&self, t: HeldoutTier) -> TierResult { |
| 109 | match t { |
| 110 | HeldoutTier::Interpolation => self.interpolation, |
| 111 | HeldoutTier::Extrapolation => self.extrapolation, |
| 112 | HeldoutTier::Adversarial => self.adversarial, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub fn to_bytes(&self) -> Vec<u8> { |
| 117 | let mut e = Enc::new(); |
| 118 | e.bytes(b"CEV1").u8(self.class as u8).u8(self.verdict as u8); |
| 119 | e.u32(self.visible_matches).u32(self.visible_total); |
| 120 | for t in [self.interpolation, self.extrapolation, self.adversarial] { |
| 121 | e.u32(t.passed).u32(t.total); |
| 122 | } |
| 123 | e.u8(self.underdetermined as u8); |
| 124 | e.u8(match self.ambiguity_recognized { |
| 125 | None => 0, |
| 126 | Some(false) => 1, |
| 127 | Some(true) => 2, |
| 128 | }); |
| 129 | e.u32(self.matches_alternative.unwrap_or(u32::MAX)); |
| 130 | e.u8(self.learner_hypotheses) |
| 131 | .u8(self.robust_submission as u8); |
| 132 | for d in [ |
| 133 | &self.crumb_digest, |
| 134 | &self.sealed_digest, |
| 135 | &self.generator_instance_digest, |
| 136 | &self.program_digest, |
| 137 | &self.visible_set_digest, |
| 138 | &self.heldout_set_digest, |
| 139 | &self.verifier_digest, |
| 140 | ] { |
| 141 | e.digest(d); |
| 142 | } |
| 143 | e.u32(self.program_steps).u64(self.evaluation_ops); |
| 144 | e.finish() |
| 145 | } |
| 146 | |
| 147 | /// Evidence receipt id of this evaluation. |
| 148 | pub fn digest(&self) -> Digest { |
| 149 | digest(Domain::Evaluation, &self.to_bytes()) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | fn out_mask(v: &VisibleCrumb) -> u64 { |
| 154 | match v.encoding { |
| 155 | Encoding::DecimalUtf8 => u64::MAX, |
| 156 | Encoding::RawLe => lane_mask(v.out_lane_bytes), |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | pub fn evaluate( |
| 161 | visible: &VisibleCrumb, |
| 162 | sealed: &SealedCrumb, |
| 163 | cand: &Program, |
| 164 | learner_hypotheses: u8, |
| 165 | robust_submission: bool, |
| 166 | ) -> SealedEvaluation { |
| 167 | let mask = out_mask(visible); |
| 168 | let mut ops: u64 = 0; |
| 169 | let steps = cand.len() as u64; |
| 170 | let mut run = |x: u64| { |
| 171 | ops += steps; |
| 172 | cand.run(x) & mask |
| 173 | }; |
| 174 | |
| 175 | let supported = visible.out_arity == 1; |
| 176 | let values = visible.values(); |
| 177 | let mut visible_matches = 0u32; |
| 178 | let mut mismatched = Vec::new(); |
| 179 | if supported { |
| 180 | for (i, (inp, out)) in values.iter().enumerate() { |
| 181 | if run(inp[0]) == out[0] { |
| 182 | visible_matches += 1; |
| 183 | } else { |
| 184 | mismatched.push(i as u32); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | let visible_ok = supported && mismatched.iter().all(|i| sealed.noisy_visible.contains(i)); |
| 189 | |
| 190 | let mut tiers = [TierResult { |
| 191 | passed: 0, |
| 192 | total: 0, |
| 193 | }; 3]; |
| 194 | for (k, t) in HeldoutTier::ALL.iter().enumerate() { |
| 195 | for ex in sealed.heldouts.tier(*t) { |
| 196 | tiers[k].total += 1; |
| 197 | if supported && run(ex.input[0]) == ex.output[0] { |
| 198 | tiers[k].passed += 1; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | let hidden_ok = tiers.iter().all(|t| t.ok()); |
| 203 | |
| 204 | let insufficient = matches!( |
| 205 | sealed.decoy_status, |
| 206 | DecoyStatus::RabbitHole(AdversarialClass::InsufficientEvidence) |
| 207 | ); |
| 208 | let class = if !supported { |
| 209 | EvalClass::Unsupported |
| 210 | } else if !visible_ok { |
| 211 | EvalClass::FalsifiedVisible |
| 212 | } else if !hidden_ok { |
| 213 | EvalClass::FalsifiedHidden |
| 214 | } else if insufficient { |
| 215 | EvalClass::AmbiguousUnderdetermined |
| 216 | } else { |
| 217 | EvalClass::Verified |
| 218 | }; |
| 219 | let verdict = if matches!( |
| 220 | class, |
| 221 | EvalClass::Verified | EvalClass::AmbiguousUnderdetermined |
| 222 | ) { |
| 223 | Verdict::Accept |
| 224 | } else { |
| 225 | Verdict::Reject |
| 226 | }; |
| 227 | |
| 228 | let matches_alternative = if supported { |
| 229 | sealed.alternatives.iter().position(|alt| { |
| 230 | HeldoutTier::ALL |
| 231 | .iter() |
| 232 | .flat_map(|t| sealed.heldouts.tier(*t)) |
| 233 | .all(|ex| alt.run(ex.input[0]) & mask == cand.run(ex.input[0]) & mask) |
| 234 | }) |
| 235 | } else { |
| 236 | None |
| 237 | }; |
| 238 | |
| 239 | SealedEvaluation { |
| 240 | class, |
| 241 | verdict, |
| 242 | visible_matches, |
| 243 | visible_total: values.len() as u32, |
| 244 | interpolation: tiers[0], |
| 245 | extrapolation: tiers[1], |
| 246 | adversarial: tiers[2], |
| 247 | underdetermined: !sealed.alternatives.is_empty(), |
| 248 | ambiguity_recognized: if insufficient { |
| 249 | Some(learner_hypotheses >= 2) |
| 250 | } else { |
| 251 | None |
| 252 | }, |
| 253 | matches_alternative: matches_alternative.map(|i| i as u32), |
| 254 | learner_hypotheses, |
| 255 | robust_submission, |
| 256 | crumb_digest: visible.digest(), |
| 257 | sealed_digest: sealed.digest(), |
| 258 | generator_instance_digest: sealed.generator_instance_digest, |
| 259 | program_digest: cand.digest(), |
| 260 | visible_set_digest: visible.visible_set_digest(), |
| 261 | heldout_set_digest: sealed.heldouts.digest(), |
| 262 | verifier_digest: verifier_digest(), |
| 263 | program_steps: cand.len() as u32, |
| 264 | evaluation_ops: ops, |
| 265 | } |
| 266 | } |