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-closure / src / digest.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 | //! Source digest of a component directory. |
| 17 | //! |
| 18 | //! BLAKE3 over every regular file under the directory, in byte-sorted |
| 19 | //! relative-path order, skipping `target`, `.git`, `closure.toml` and |
| 20 | //! `closure.lock` (the manifest records the digest, so it cannot be part of |
| 21 | //! it). Each file contributes `u64be(len(path)) path u64be(len(bytes)) bytes` |
| 22 | //! under the domain `AIEN_CLOSURE_SOURCE_V1`. Symlinks and other non-regular |
| 23 | //! files are an error, never silently skipped. |
| 24 | |
| 25 | use std::fs; |
| 26 | use std::path::{Path, PathBuf}; |
| 27 | |
| 28 | pub const DOMAIN: &[u8] = b"AIEN_CLOSURE_SOURCE_V1"; |
| 29 | const SKIP_DIRS: &[&str] = &["target", ".git"]; |
| 30 | const SKIP_FILES: &[&str] = &["closure.toml", "closure.lock"]; |
| 31 | |
| 32 | fn collect(base: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<(), String> { |
| 33 | let entries = fs::read_dir(dir).map_err(|e| format!("cannot read {}: {e}", dir.display()))?; |
| 34 | for entry in entries { |
| 35 | let entry = entry.map_err(|e| e.to_string())?; |
| 36 | let path = entry.path(); |
| 37 | let name = entry.file_name().to_string_lossy().into_owned(); |
| 38 | let ty = entry.file_type().map_err(|e| e.to_string())?; |
| 39 | if ty.is_dir() { |
| 40 | if SKIP_DIRS.contains(&name.as_str()) { |
| 41 | continue; |
| 42 | } |
| 43 | collect(base, &path, out)?; |
| 44 | } else if ty.is_file() { |
| 45 | if dir == base && SKIP_FILES.contains(&name.as_str()) { |
| 46 | continue; |
| 47 | } |
| 48 | out.push(path); |
| 49 | } else { |
| 50 | return Err(format!( |
| 51 | "non-regular file in component source: {}", |
| 52 | path.display() |
| 53 | )); |
| 54 | } |
| 55 | } |
| 56 | Ok(()) |
| 57 | } |
| 58 | |
| 59 | /// Returns the lowercase hex digest (64 digits). |
| 60 | pub fn source_digest(dir: &Path) -> Result<String, String> { |
| 61 | let mut files = Vec::new(); |
| 62 | collect(dir, dir, &mut files)?; |
| 63 | let mut keyed: Vec<(String, PathBuf)> = files |
| 64 | .into_iter() |
| 65 | .map(|p| { |
| 66 | let rel = p |
| 67 | .strip_prefix(dir) |
| 68 | .unwrap_or(&p) |
| 69 | .to_string_lossy() |
| 70 | .replace('\\', "/"); |
| 71 | (rel, p) |
| 72 | }) |
| 73 | .collect(); |
| 74 | keyed.sort_by(|a, b| a.0.as_bytes().cmp(b.0.as_bytes())); |
| 75 | let mut h = blake3::Hasher::new(); |
| 76 | h.update(DOMAIN); |
| 77 | for (rel, path) in keyed { |
| 78 | let bytes = fs::read(&path).map_err(|e| format!("cannot read {}: {e}", path.display()))?; |
| 79 | h.update(&(rel.len() as u64).to_be_bytes()); |
| 80 | h.update(rel.as_bytes()); |
| 81 | h.update(&(bytes.len() as u64).to_be_bytes()); |
| 82 | h.update(&bytes); |
| 83 | } |
| 84 | Ok(h.finalize().to_hex().to_string()) |
| 85 | } |