Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/aien-proof/src/fingerprint.rs # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / aien-proof / src / fingerprint.rs` 272 lines ยท 9097 bytes Raw Blame History 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 //! Job fingerprints. Paths are hashed relative to the job's base directory, so 17 //! the same code in two different worktrees produces the same key and shares 18 //! one stamp. Inside a git checkout, files git ignores (build output such as a 19 //! compiled `.so` dropped into a source dir) are left out, exactly as git would. 20 21 use std::ffi::OsStr; 22 use std::fs; 23 use std::io; 24 use std::os::unix::ffi::OsStrExt; 25 use std::path::{Component, Path, PathBuf}; 26 use std::process::Command; 27 28 /// Directory names never hashed: build output (Cargo `target`, oracle `.lake`), git 29 /// internals, live crumb scents. 30 const SKIP: &[&str] = &["target", ".git", ".crumb.local", ".lake"]; 31 32 fn field(h: &mut blake3::Hasher, bytes: &[u8]) { 33 h.update(&(bytes.len() as u64).to_be_bytes()); 34 h.update(bytes); 35 } 36 37 fn collect(path: &Path, out: &mut Vec) -> io::Result<()> { 38 let meta = fs::metadata(path)?; 39 if meta.is_dir() { 40 for entry in fs::read_dir(path)? { 41 let entry = entry?; 42 let name = entry.file_name(); 43 if SKIP.iter().any(|s| name == *s) { 44 continue; 45 } 46 collect(&entry.path(), out)?; 47 } 48 } else if meta.is_file() { 49 out.push(path.to_path_buf()); 50 } 51 Ok(()) 52 } 53 54 /// Tracked plus untracked-but-not-ignored files under `inputs`, or `None` when 55 /// `base` is not a git checkout. 56 fn git_files(base: &Path, inputs: &[&PathBuf]) -> Option> { 57 if inputs.is_empty() { 58 return Some(Vec::new()); 59 } 60 let out = Command::new("git") 61 .arg("-C") 62 .arg(base) 63 .args([ 64 "ls-files", 65 "-z", 66 "--cached", 67 "--others", 68 "--exclude-standard", 69 "--", 70 ]) 71 .args(inputs) 72 .output() 73 .ok()?; 74 if !out.status.success() { 75 return None; 76 } 77 let files = out 78 .stdout 79 .split(|b| *b == 0) 80 .filter(|s| !s.is_empty()) 81 .map(|s| base.join(OsStr::from_bytes(s))) 82 .filter(|p| { 83 !p.components() 84 .any(|c| SKIP.iter().any(|s| c.as_os_str() == *s)) 85 }) 86 .collect(); 87 Some(files) 88 } 89 90 fn inside(input: &Path) -> bool { 91 input.is_relative() && !input.components().any(|c| c == Component::ParentDir) 92 } 93 94 /// Feed the sorted file set under `inputs` (paths and content hashes) into `h`. 95 fn hash_inputs(h: &mut blake3::Hasher, base: &Path, inputs: &[PathBuf]) -> io::Result<()> { 96 // Inputs inside the checkout go through git's view; anything outside it 97 // (sibling repos reached by `../` path dependencies) is walked directly. 98 let (local, outside): (Vec<&PathBuf>, Vec<&PathBuf>) = inputs.iter().partition(|p| inside(p)); 99 let mut files = Vec::new(); 100 match git_files(base, &local) { 101 Some(tracked) => files.extend(tracked), 102 None => { 103 for input in &local { 104 collect(&base.join(input), &mut files)?; 105 } 106 } 107 } 108 for input in &outside { 109 collect(&base.join(input), &mut files)?; 110 } 111 // Git lists a symlinked directory (e.g. a crate linked in from a sibling 112 // repo) as one entry; hash what it points at so edits there void the stamp. 113 let mut expanded = Vec::with_capacity(files.len()); 114 for file in files { 115 if fs::metadata(&file).is_ok_and(|m| m.is_dir()) { 116 collect(&file, &mut expanded)?; 117 } else { 118 expanded.push(file); 119 } 120 } 121 let mut files = expanded; 122 files.sort(); 123 files.dedup(); 124 h.update(&(files.len() as u64).to_be_bytes()); 125 for file in &files { 126 let rel = file.strip_prefix(base).unwrap_or(file); 127 field(h, rel.to_string_lossy().as_bytes()); 128 match fs::read(file) { 129 Ok(data) => field(h, blake3::hash(&data).as_bytes()), 130 // Tracked in git but deleted in this worktree. 131 Err(e) if e.kind() == io::ErrorKind::NotFound => field(h, b""), 132 Err(e) => return Err(e), 133 } 134 } 135 Ok(()) 136 } 137 138 pub fn job_key( 139 base: &Path, 140 job: &str, 141 cmd: &[String], 142 toolchain: &str, 143 inputs: &[PathBuf], 144 ) -> io::Result { 145 let mut h = blake3::Hasher::new(); 146 h.update(b"AIEN_PROOF_JOB_V2"); 147 field(&mut h, job.as_bytes()); 148 h.update(&(cmd.len() as u64).to_be_bytes()); 149 for arg in cmd { 150 field(&mut h, arg.as_bytes()); 151 } 152 field(&mut h, toolchain.as_bytes()); 153 hash_inputs(&mut h, base, inputs)?; 154 Ok(h.finalize().to_hex().to_string()) 155 } 156 157 /// Digest of the file set under `inputs` alone (no command, no toolchain). 158 /// Receipts use it to name one slice of a job's inputs, such as a formal 159 /// model or the implementation file a proof covers, with the same file rules 160 /// the job fingerprint uses. 161 pub fn inputs_digest(base: &Path, inputs: &[PathBuf]) -> io::Result { 162 let mut h = blake3::Hasher::new(); 163 h.update(b"AIEN_PROOF_INPUTS_V1"); 164 hash_inputs(&mut h, base, inputs)?; 165 Ok(h.finalize().to_hex().to_string()) 166 } 167 168 #[cfg(test)] 169 mod tests { 170 use super::*; 171 use crate::testutil::temp_dir; 172 173 fn tree(root: &Path) { 174 fs::create_dir_all(root.join("src")).unwrap(); 175 fs::create_dir_all(root.join("src/target")).unwrap(); 176 fs::write(root.join("src/lib.rs"), "pub fn a() {}").unwrap(); 177 fs::write(root.join("src/target/junk.o"), "junk").unwrap(); 178 } 179 180 fn key(root: &Path) -> String { 181 job_key( 182 root, 183 "t", 184 &["cargo".into(), "test".into()], 185 "rustc 1", 186 &[PathBuf::from("src")], 187 ) 188 .unwrap() 189 } 190 191 #[test] 192 fn same_code_in_two_worktrees_shares_a_key() { 193 let (a, b) = (temp_dir("fp-a"), temp_dir("fp-b")); 194 tree(&a); 195 tree(&b); 196 assert_eq!(key(&a), key(&b)); 197 } 198 199 #[test] 200 fn content_change_changes_key_and_build_output_does_not() { 201 let root = temp_dir("fp-c"); 202 tree(&root); 203 let before = key(&root); 204 fs::write(root.join("src/target/junk.o"), "different junk").unwrap(); 205 assert_eq!(before, key(&root)); 206 fs::write(root.join("src/lib.rs"), "pub fn b() {}").unwrap(); 207 assert_ne!(before, key(&root)); 208 } 209 210 #[test] 211 fn git_ignored_files_do_not_change_the_key() { 212 let root = temp_dir("fp-git"); 213 tree(&root); 214 fs::write(root.join(".gitignore"), "*.so\n").unwrap(); 215 let git = |args: &[&str]| { 216 assert!(Command::new("git") 217 .arg("-C") 218 .arg(&root) 219 .args(args) 220 .output() 221 .unwrap() 222 .status 223 .success()); 224 }; 225 git(&["init", "-q"]); 226 git(&["add", "."]); 227 fs::write(root.join("src/libkernel.so"), "build 1").unwrap(); 228 let before = key(&root); 229 fs::write(root.join("src/libkernel.so"), "build 2").unwrap(); 230 assert_eq!(before, key(&root)); 231 // Untracked source that git does not ignore still counts. 232 fs::write(root.join("src/new.rs"), "pub fn n() {}").unwrap(); 233 assert_ne!(before, key(&root)); 234 } 235 236 #[test] 237 fn edits_behind_a_tracked_directory_symlink_change_the_key() { 238 let (root, linked) = (temp_dir("fp-link"), temp_dir("fp-link-target")); 239 fs::create_dir_all(&root).unwrap(); 240 fs::write(linked.join("lib.rs"), "pub fn a() {}").unwrap(); 241 std::os::unix::fs::symlink(&linked, root.join("src")).unwrap(); 242 for args in [&["init", "-q"][..], &["add", "."][..]] { 243 assert!(Command::new("git") 244 .arg("-C") 245 .arg(&root) 246 .args(args) 247 .output() 248 .unwrap() 249 .status 250 .success()); 251 } 252 let before = key(&root); 253 fs::write(linked.join("lib.rs"), "pub fn b() {}").unwrap(); 254 assert_ne!(before, key(&root)); 255 } 256 257 #[test] 258 fn command_and_toolchain_are_part_of_the_key() { 259 let root = temp_dir("fp-d"); 260 tree(&root); 261 let inputs = [PathBuf::from("src")]; 262 let base = job_key(&root, "t", &["x".into()], "rustc 1", &inputs).unwrap(); 263 assert_ne!( 264 base, 265 job_key(&root, "t", &["y".into()], "rustc 1", &inputs).unwrap() 266 ); 267 assert_ne!( 268 base, 269 job_key(&root, "t", &["x".into()], "rustc 2", &inputs).unwrap() 270 ); 271 } 272 }