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 / aien-campaign-verifier / src / bundle.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 | //! Bundle integrity and binding to the frozen spec (SPEC.md Section 10). |
| 17 | |
| 18 | use serde::Deserialize; |
| 19 | use sha2::{Digest, Sha256}; |
| 20 | use std::collections::BTreeSet; |
| 21 | use std::fs; |
| 22 | use std::path::Path; |
| 23 | |
| 24 | pub const SUMS_FILE: &str = "sha256sums.txt"; |
| 25 | pub const CAMPAIGN_ID: &str = "c1"; |
| 26 | |
| 27 | /// Subset of `manifest.json` the verifier binds against. Unknown fields are ignored. |
| 28 | #[derive(Debug, Clone, Deserialize)] |
| 29 | pub struct Manifest { |
| 30 | pub campaign_id: String, |
| 31 | pub acceptance_sha256: String, |
| 32 | pub placement_sha256: String, |
| 33 | pub kv_contract_sha256: String, |
| 34 | pub code_under_test_commit: String, |
| 35 | pub dirty_tree: bool, |
| 36 | pub kv_profile: String, |
| 37 | } |
| 38 | |
| 39 | pub fn sha256_file(path: &Path) -> Result<String, String> { |
| 40 | let bytes = fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?; |
| 41 | Ok(Sha256::digest(&bytes) |
| 42 | .iter() |
| 43 | .map(|b| format!("{b:02x}")) |
| 44 | .collect()) |
| 45 | } |
| 46 | |
| 47 | /// Every file in the bundle is listed in `sha256sums.txt` and matches its digest. |
| 48 | pub fn check_sha256sums(bundle: &Path) -> Vec<String> { |
| 49 | let sums = match fs::read_to_string(bundle.join(SUMS_FILE)) { |
| 50 | Ok(s) => s, |
| 51 | Err(e) => return vec![format!("{SUMS_FILE}: {e}")], |
| 52 | }; |
| 53 | |
| 54 | let mut v = Vec::new(); |
| 55 | let mut listed = BTreeSet::new(); |
| 56 | for (n, line) in sums.lines().enumerate() { |
| 57 | let line = line.trim(); |
| 58 | if line.is_empty() { |
| 59 | continue; |
| 60 | } |
| 61 | let Some((digest, rel)) = line.split_once(char::is_whitespace) else { |
| 62 | v.push(format!("{SUMS_FILE}:{}: malformed line", n + 1)); |
| 63 | continue; |
| 64 | }; |
| 65 | let rel = rel.trim_start().trim_start_matches('*'); |
| 66 | if rel.starts_with('/') || rel.split('/').any(|c| c == "..") { |
| 67 | v.push(format!("{SUMS_FILE}:{}: path escapes bundle: {rel}", n + 1)); |
| 68 | continue; |
| 69 | } |
| 70 | listed.insert(rel.to_string()); |
| 71 | match sha256_file(&bundle.join(rel)) { |
| 72 | Ok(actual) if actual == digest.to_ascii_lowercase() => {} |
| 73 | Ok(_) => v.push(format!("{rel}: digest mismatch")), |
| 74 | Err(e) => v.push(e), |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | match list_files(bundle) { |
| 79 | Ok(files) => { |
| 80 | for f in files { |
| 81 | if f != SUMS_FILE && !listed.contains(&f) { |
| 82 | v.push(format!("{f}: not covered by {SUMS_FILE}")); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | Err(e) => v.push(e), |
| 87 | } |
| 88 | v |
| 89 | } |
| 90 | |
| 91 | /// The manifest names this campaign, a clean tree, and the exact spec files the verifier holds. |
| 92 | pub fn check_manifest_against_spec(m: &Manifest, spec_dir: &Path) -> Vec<String> { |
| 93 | let mut v = Vec::new(); |
| 94 | if m.campaign_id != CAMPAIGN_ID { |
| 95 | v.push(format!( |
| 96 | "campaign_id {} is not {CAMPAIGN_ID}", |
| 97 | m.campaign_id |
| 98 | )); |
| 99 | } |
| 100 | if m.dirty_tree { |
| 101 | v.push("code under test was built from a dirty tree".into()); |
| 102 | } |
| 103 | let commit = &m.code_under_test_commit; |
| 104 | if commit.len() != 40 || !commit.chars().all(|c| c.is_ascii_hexdigit()) { |
| 105 | v.push(format!( |
| 106 | "code_under_test_commit {commit:?} is not a full SHA-1" |
| 107 | )); |
| 108 | } |
| 109 | let bound = [ |
| 110 | ("acceptance.toml", &m.acceptance_sha256), |
| 111 | ("placement.toml", &m.placement_sha256), |
| 112 | ("kv_contract.toml", &m.kv_contract_sha256), |
| 113 | ]; |
| 114 | for (file, recorded) in bound { |
| 115 | match sha256_file(&spec_dir.join(file)) { |
| 116 | Ok(actual) if actual == recorded.to_ascii_lowercase() => {} |
| 117 | Ok(actual) => v.push(format!( |
| 118 | "{file}: bundle recorded {recorded}, frozen spec is {actual}" |
| 119 | )), |
| 120 | Err(e) => v.push(e), |
| 121 | } |
| 122 | } |
| 123 | v |
| 124 | } |
| 125 | |
| 126 | fn list_files(root: &Path) -> Result<Vec<String>, String> { |
| 127 | let mut out = Vec::new(); |
| 128 | let mut stack = vec![root.to_path_buf()]; |
| 129 | while let Some(dir) = stack.pop() { |
| 130 | let entries = fs::read_dir(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; |
| 131 | for entry in entries { |
| 132 | let path = entry.map_err(|e| format!("{}: {e}", dir.display()))?.path(); |
| 133 | if path.is_dir() { |
| 134 | stack.push(path); |
| 135 | } else if let Ok(rel) = path.strip_prefix(root) { |
| 136 | let rel: Vec<String> = rel |
| 137 | .components() |
| 138 | .map(|c| c.as_os_str().to_string_lossy().into_owned()) |
| 139 | .collect(); |
| 140 | out.push(rel.join("/")); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | out.sort(); |
| 145 | Ok(out) |
| 146 | } |