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/spark-rsi-mv/blob/main/src/canary/identity.rs # spark-rsi-mv Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger 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 `spark-rsi-mv / src / canary / identity.rs` 164 lines ยท 5555 bytes Raw Blame History 1 //! The bound candidate identity chain: source patch digest, evaluated tree digest, built 2 //! executable digest, generation id. The supervisor launches only the executable whose digest 3 //! is in the chain, every observation carries that executable digest, and the quota validator 4 //! compares the executable digest, never the patch digest. 5 6 use super::{canonical_json, domain_digest, is_hex_digest, sha256_hex, IDENTITY_VERSION}; 7 use crate::isolation::ArtifactManifest; 8 use serde::{Deserialize, Serialize}; 9 use std::path::Path; 10 11 pub const IDENTITY_DOMAIN: &str = "spark-rsi/candidate-identity/v1"; 12 13 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] 14 #[serde(deny_unknown_fields)] 15 pub struct CandidateIdentity { 16 pub identity_version: u16, 17 /// SHA-256 of the proposal's patch bytes. 18 pub patch_digest: String, 19 /// Digest of the evaluated tree (`ArtifactManifest::compute_source_digest`). 20 pub tree_digest: String, 21 /// SHA-256 of the executable file the supervisor launches. 22 pub executable_digest: String, 23 pub generation_id: String, 24 } 25 26 impl CandidateIdentity { 27 pub fn new( 28 patch_digest: &str, 29 tree_digest: &str, 30 executable_digest: &str, 31 generation_id: &str, 32 ) -> Self { 33 Self { 34 identity_version: IDENTITY_VERSION, 35 patch_digest: patch_digest.to_string(), 36 tree_digest: tree_digest.to_string(), 37 executable_digest: executable_digest.to_string(), 38 generation_id: generation_id.to_string(), 39 } 40 } 41 42 pub fn validate(&self) -> Result<(), String> { 43 if self.identity_version != IDENTITY_VERSION { 44 return Err(format!( 45 "candidate identity version {} is not supported (expected {})", 46 self.identity_version, IDENTITY_VERSION 47 )); 48 } 49 for (name, value) in [ 50 ("patch_digest", &self.patch_digest), 51 ("tree_digest", &self.tree_digest), 52 ("executable_digest", &self.executable_digest), 53 ] { 54 if !is_hex_digest(value) { 55 return Err(format!("candidate identity {} is not a hex SHA-256", name)); 56 } 57 } 58 if self.generation_id.trim().is_empty() { 59 return Err("candidate identity generation id is required".to_string()); 60 } 61 Ok(()) 62 } 63 64 pub fn canonical_bytes(&self) -> Vec { 65 canonical_json(self) 66 } 67 68 /// One digest binding the whole chain, for ledger commitments and reports. 69 pub fn binding_digest(&self) -> String { 70 domain_digest( 71 IDENTITY_DOMAIN, 72 &[ 73 self.patch_digest.as_bytes(), 74 self.tree_digest.as_bytes(), 75 self.executable_digest.as_bytes(), 76 self.generation_id.as_bytes(), 77 ], 78 ) 79 } 80 81 /// Re-hashes the file at `path` and refuses it unless it is the executable in the chain. 82 /// Call this right before launching. 83 pub fn verify_executable(&self, path: &Path) -> Result<(), String> { 84 let actual = digest_file(path)?; 85 if actual != self.executable_digest { 86 return Err(format!( 87 "executable {:?} hashes to {}, not the candidate's {}; refusing to launch", 88 path, actual, self.executable_digest 89 )); 90 } 91 Ok(()) 92 } 93 } 94 95 pub fn digest_bytes(bytes: &[u8]) -> String { 96 sha256_hex(bytes) 97 } 98 99 pub fn digest_file(path: &Path) -> Result { 100 let bytes = 101 std::fs::read(path).map_err(|e| format!("cannot read {:?} for hashing: {}", path, e))?; 102 Ok(sha256_hex(&bytes)) 103 } 104 105 /// Digest of a source tree, skipping `target`, `.git` and `node_modules`. 106 pub fn tree_digest(root: &Path) -> Result { 107 ArtifactManifest::compute_source_digest(root) 108 } 109 110 #[cfg(test)] 111 mod tests { 112 use super::*; 113 114 fn sample() -> CandidateIdentity { 115 CandidateIdentity::new( 116 &sha256_hex(b"patch"), 117 &sha256_hex(b"tree"), 118 &sha256_hex(b"exe"), 119 "gen-0001", 120 ) 121 } 122 123 #[test] 124 fn binding_digest_covers_every_link() { 125 let a = sample(); 126 let mut b = sample(); 127 b.executable_digest = sha256_hex(b"other exe"); 128 assert_ne!(a.binding_digest(), b.binding_digest()); 129 let mut c = sample(); 130 c.generation_id = "gen-0002".into(); 131 assert_ne!(a.binding_digest(), c.binding_digest()); 132 assert!(a.validate().is_ok()); 133 } 134 135 #[test] 136 fn executable_verification_refuses_other_bytes() { 137 let tmp = tempfile::tempdir().unwrap(); 138 let exe = tmp.path().join("worker"); 139 std::fs::write(&exe, b"built bytes").unwrap(); 140 let id = CandidateIdentity::new( 141 &sha256_hex(b"patch"), 142 &sha256_hex(b"tree"), 143 &digest_file(&exe).unwrap(), 144 "gen-0001", 145 ); 146 assert!(id.verify_executable(&exe).is_ok()); 147 std::fs::write(&exe, b"swapped bytes").unwrap(); 148 assert!(id 149 .verify_executable(&exe) 150 .unwrap_err() 151 .contains("refusing to launch")); 152 assert!(id.verify_executable(&tmp.path().join("missing")).is_err()); 153 } 154 155 #[test] 156 fn validation_requires_hex_digests_and_a_generation() { 157 let mut bad = sample(); 158 bad.patch_digest = "not hex".into(); 159 assert!(bad.validate().is_err()); 160 let mut empty = sample(); 161 empty.generation_id = " ".into(); 162 assert!(empty.validate().is_err()); 163 } 164 }