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-allen/tests/identity_test.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-allen / tests / identity_test.rs` 381 lines ยท 11602 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 //! Restart, corruption, rollback, machine change, negative control. 17 mod support; 18 use aien_allen::binding::{self, pin_path}; 19 use aien_allen::hex; 20 use aien_allen::resolve::{resolve, Context, Refusal, SubjectSource}; 21 use aien_allen::subject_v0::object_id; 22 use support::*; 23 24 const M1: [u8; 32] = [1; 32]; 25 const M2: [u8; 32] = [2; 32]; 26 27 fn ctx(f: &Fx, m: [u8; 32]) -> Context { 28 Context { 29 machine_id: m, 30 lineage: Some(f.cortex), 31 } 32 } 33 34 struct Rig { 35 _t: tempfile::TempDir, 36 home: std::path::PathBuf, 37 subj: std::path::PathBuf, 38 f: Fx, 39 } 40 41 fn rig_head(n: u64) -> Rig { 42 let t = tempfile::tempdir().unwrap(); 43 let f = fx("a"); 44 let c = chain(&f, n); 45 let subj = write_head(&t.path().join("subject.bin"), c.last().unwrap()); 46 Rig { 47 home: t.path().join("compose"), 48 subj, 49 f, 50 _t: t, 51 } 52 } 53 54 fn adopt(r: &Rig) -> aien_allen::Resolved { 55 resolve( 56 &SubjectSource::Head(r.subj.clone()), 57 &pin_path(&r.home), 58 &ctx(&r.f, M1), 59 Some(&hex(&r.f.agent)), 60 ) 61 .unwrap() 62 } 63 64 fn again(r: &Rig, m: [u8; 32]) -> Result { 65 resolve( 66 &SubjectSource::Head(r.subj.clone()), 67 &pin_path(&r.home), 68 &ctx(&r.f, m), 69 None, 70 ) 71 } 72 73 #[test] 74 fn restart_preserves_identity() { 75 let r = rig_head(2); 76 let first = adopt(&r); 77 assert!(first.adopted); 78 let pin_bytes = std::fs::read(pin_path(&r.home)).unwrap(); 79 // "Restart": nothing in memory, everything from files. 80 let second = again(&r, M1).unwrap(); 81 assert!(!second.adopted); 82 assert_eq!(first.agent, second.agent); 83 assert_eq!(first.head_id, second.head_id); 84 assert_eq!(first.root, second.root); 85 assert_eq!(pin_bytes, std::fs::read(pin_path(&r.home)).unwrap()); 86 } 87 88 #[test] 89 fn negative_control_nothing_is_ever_minted() { 90 let r = rig_head(1); 91 // No subject file at all, even with a correct adopt value. 92 let missing = r._t.path().join("nope.bin"); 93 let e = resolve( 94 &SubjectSource::Head(missing), 95 &pin_path(&r.home), 96 &ctx(&r.f, M1), 97 Some(&hex(&r.f.agent)), 98 ) 99 .unwrap_err(); 100 assert!(matches!(e, Refusal::Absent(_)), "{e}"); 101 assert!(!pin_path(&r.home).exists(), "no pin written"); 102 // An empty subject file. 103 std::fs::write(&r.subj, b"").unwrap(); 104 assert!(matches!(again(&r, M1), Err(Refusal::Absent(_)))); 105 // An empty directory. 106 let d = r._t.path().join("d"); 107 std::fs::create_dir(&d).unwrap(); 108 let e = resolve( 109 &SubjectSource::Dir(d.clone()), 110 &pin_path(&r.home), 111 &ctx(&r.f, M1), 112 None, 113 ) 114 .unwrap_err(); 115 assert!(matches!(e, Refusal::Absent(_))); 116 assert_eq!( 117 std::fs::read_dir(&d).unwrap().count(), 118 0, 119 "directory untouched" 120 ); 121 assert!(!pin_path(&r.home).exists()); 122 } 123 124 #[test] 125 fn no_pin_means_refusal_not_adoption() { 126 let r = rig_head(1); 127 assert_eq!(again(&r, M1).unwrap_err(), Refusal::PinAbsent); 128 assert!(!pin_path(&r.home).exists()); 129 } 130 131 #[test] 132 fn adopt_rules() { 133 let r = rig_head(1); 134 let src = SubjectSource::Head(r.subj.clone()); 135 let p = pin_path(&r.home); 136 let c = ctx(&r.f, M1); 137 // wrong agent value 138 assert_eq!( 139 resolve(&src, &p, &c, Some(&hex(&h32("other")))).unwrap_err(), 140 Refusal::AdoptMismatch 141 ); 142 assert_eq!( 143 resolve(&src, &p, &c, Some("zz")).unwrap_err(), 144 Refusal::AdoptBadValue 145 ); 146 assert!(!p.exists()); 147 adopt(&r); 148 // adopt variable refused once a pin exists 149 assert_eq!( 150 resolve(&src, &p, &c, Some(&hex(&r.f.agent))).unwrap_err(), 151 Refusal::AdoptRefusedPinExists 152 ); 153 } 154 155 #[test] 156 fn corrupted_pin_is_refused_and_untouched() { 157 let r = rig_head(1); 158 adopt(&r); 159 let p = pin_path(&r.home); 160 let good = std::fs::read(&p).unwrap(); 161 for i in [0usize, 9, 20, 100, 115, 160, 230] { 162 let mut bad = good.clone(); 163 bad[i] ^= 1; 164 std::fs::write(&p, &bad).unwrap(); 165 assert!( 166 matches!(again(&r, M1), Err(Refusal::PinDamaged(_))), 167 "byte {i}" 168 ); 169 assert_eq!(std::fs::read(&p).unwrap(), bad, "pin not repaired"); 170 } 171 // truncated and extended 172 std::fs::write(&p, &good[..100]).unwrap(); 173 assert!(matches!(again(&r, M1), Err(Refusal::PinDamaged(_)))); 174 let mut long = good.clone(); 175 long.push(0); 176 std::fs::write(&p, &long).unwrap(); 177 assert!(matches!(again(&r, M1), Err(Refusal::PinDamaged(_)))); 178 assert!(binding::read(&p).is_err()); 179 } 180 181 #[test] 182 fn corrupted_or_truncated_subject_is_refused() { 183 let r = rig_head(1); 184 adopt(&r); 185 let good = std::fs::read(&r.subj).unwrap(); 186 for i in [0usize, 9, 20, 60, 130, 200, good.len() - 1] { 187 let mut bad = good.clone(); 188 bad[i] ^= 1; 189 std::fs::write(&r.subj, &bad).unwrap(); 190 assert!(again(&r, M1).is_err(), "flip at {i} must be refused"); 191 } 192 std::fs::write(&r.subj, &good[..good.len() - 8]).unwrap(); 193 assert!(matches!(again(&r, M1), Err(Refusal::Corrupt(_)))); 194 let mut long = good.clone(); 195 long.push(0); 196 std::fs::write(&r.subj, &long).unwrap(); 197 assert!(matches!(again(&r, M1), Err(Refusal::Corrupt(_)))); 198 } 199 200 #[test] 201 fn rolled_back_head_is_refused_and_same_sequence_other_object_too() { 202 let r = rig_head(3); 203 adopt(&r); 204 let c = chain(&r.f, 3); 205 std::fs::write(&r.subj, &c[1]).unwrap(); // sequence 2 < pinned 3 206 assert_eq!( 207 again(&r, M1).unwrap_err(), 208 Refusal::RolledBack { 209 pinned: 3, 210 found: 2 211 } 212 ); 213 // Same sequence, different bytes (different payload). 214 let other = encode(&r.f, 3, object_id(&c[1]), [7, 2000]); 215 std::fs::write(&r.subj, &other).unwrap(); 216 assert_eq!(again(&r, M1).unwrap_err(), Refusal::SameSequenceDifferentId); 217 } 218 219 #[test] 220 fn legitimate_successor_advances_the_pin() { 221 let r = rig_head(2); 222 adopt(&r); 223 let c = chain(&r.f, 3); 224 std::fs::write(&r.subj, &c[2]).unwrap(); 225 let res = again(&r, M1).unwrap(); 226 assert_eq!(res.head_seq, 3); 227 assert_eq!( 228 binding::read(&pin_path(&r.home)).unwrap().unwrap().head_seq, 229 3 230 ); 231 } 232 233 #[test] 234 fn machine_change_is_refused_never_rebound() { 235 let r = rig_head(1); 236 adopt(&r); 237 let before = std::fs::read(pin_path(&r.home)).unwrap(); 238 assert_eq!(again(&r, M2).unwrap_err(), Refusal::MachineChanged); 239 assert_eq!( 240 before, 241 std::fs::read(pin_path(&r.home)).unwrap(), 242 "pin not rebound" 243 ); 244 // identity (the subject file) is untouched by the machine id 245 assert!(again(&r, M1).is_ok()); 246 } 247 248 #[test] 249 fn lineage_rules() { 250 let r = rig_head(1); 251 let src = SubjectSource::Head(r.subj.clone()); 252 let a = Some(hex(&r.f.agent)); 253 let mut c = ctx(&r.f, M1); 254 c.lineage = Some(h32("another journal")); 255 assert_eq!( 256 resolve(&src, &pin_path(&r.home), &c, a.as_deref()).unwrap_err(), 257 Refusal::LineageMismatch 258 ); 259 c.lineage = None; 260 assert_eq!( 261 resolve(&src, &pin_path(&r.home), &c, a.as_deref()).unwrap_err(), 262 Refusal::LineageMissing 263 ); 264 assert!(!pin_path(&r.home).exists()); 265 } 266 267 #[test] 268 fn foreign_subject_against_pin_is_refused() { 269 let r = rig_head(1); 270 adopt(&r); 271 let g = fx("b"); 272 let o = chain(&g, 1); 273 std::fs::write(&r.subj, &o[0]).unwrap(); 274 let c = Context { 275 machine_id: M1, 276 lineage: Some(g.cortex), 277 }; 278 let e = resolve( 279 &SubjectSource::Head(r.subj.clone()), 280 &pin_path(&r.home), 281 &c, 282 None, 283 ) 284 .unwrap_err(); 285 assert!(matches!(e, Refusal::ForeignIdentity(_)), "{e}"); 286 } 287 288 #[test] 289 fn directory_mode_chain_rows() { 290 let t = tempfile::tempdir().unwrap(); 291 let f = fx("a"); 292 let c = chain(&f, 3); 293 let home = t.path().join("compose"); 294 let d = t.path().join("subj"); 295 write_dir(&d, &c); 296 let src = SubjectSource::Dir(d.clone()); 297 let cx = ctx(&f, M1); 298 let ok = resolve(&src, &pin_path(&home), &cx, Some(&hex(&f.agent))).unwrap(); 299 assert!(ok.chain_verified); 300 assert_eq!(ok.head_seq, 3); 301 // forked: a second object at sequence 3 302 let fork = encode(&f, 3, object_id(&c[1]), [7, 9]); 303 write_dir(&d, std::slice::from_ref(&fork)); 304 assert_eq!( 305 resolve(&src, &pin_path(&home), &cx, None).unwrap_err(), 306 Refusal::Forked(3) 307 ); 308 std::fs::remove_file(d.join(format!("{}.bin", hex(&object_id(&fork))))).unwrap(); 309 // gap: remove sequence 2 310 std::fs::remove_file(d.join(format!("{}.bin", hex(&object_id(&c[1]))))).unwrap(); 311 assert_eq!( 312 resolve(&src, &pin_path(&home), &cx, None).unwrap_err(), 313 Refusal::Gap(2) 314 ); 315 write_dir(&d, std::slice::from_ref(&c[1])); 316 // changed bytes under an id name 317 let name = d.join(format!("{}.bin", hex(&object_id(&c[0])))); 318 let mut bad = c[0].clone(); 319 bad[100] ^= 1; 320 std::fs::write(&name, &bad).unwrap(); 321 assert!(matches!( 322 resolve(&src, &pin_path(&home), &cx, None), 323 Err(Refusal::Corrupt(_)) 324 )); 325 std::fs::write(&name, &c[0]).unwrap(); 326 // link broken: sequence 2 naming a wrong previous 327 let wrong = encode(&f, 2, h32("nonsense"), [7, 1000]); 328 let d2 = t.path().join("subj2"); 329 write_dir(&d2, &[c[0].clone(), wrong, c[2].clone()]); 330 assert!(matches!( 331 resolve(&SubjectSource::Dir(d2), &pin_path(&home), &cx, None), 332 Err(Refusal::Corrupt(_)) 333 )); 334 // foreign object mixed into the chain 335 let g = fx("b"); 336 let alien = encode(&g, 2, object_id(&c[0]), [7, 1000]); 337 let d3 = t.path().join("subj3"); 338 write_dir(&d3, &[c[0].clone(), alien]); 339 assert!(matches!( 340 resolve(&SubjectSource::Dir(d3), &pin_path(&home), &cx, None), 341 Err(Refusal::ForeignIdentity(_)) 342 )); 343 } 344 345 #[test] 346 fn directory_mode_rejects_a_chain_that_forks_from_the_pin() { 347 let t = tempfile::tempdir().unwrap(); 348 let f = fx("a"); 349 let c = chain(&f, 2); 350 let home = t.path().join("compose"); 351 let d = t.path().join("subj"); 352 write_dir(&d, &c); 353 let cx = ctx(&f, M1); 354 resolve( 355 &SubjectSource::Dir(d.clone()), 356 &pin_path(&home), 357 &cx, 358 Some(&hex(&f.agent)), 359 ) 360 .unwrap(); 361 // A different history with the same agent: seq 2 differs, seq 3 added. 362 let alt2 = encode(&f, 2, object_id(&c[0]), [7, 5555]); 363 let alt3 = encode(&f, 3, object_id(&alt2), [7, 1000]); 364 let d2 = t.path().join("alt"); 365 write_dir(&d2, &[c[0].clone(), alt2, alt3]); 366 assert_eq!( 367 resolve(&SubjectSource::Dir(d2), &pin_path(&home), &cx, None).unwrap_err(), 368 Refusal::ForkedFromPin 369 ); 370 } 371 372 #[test] 373 fn known_object_ids_are_stable() { 374 // Same bytes, same id, on any machine, under any model (ADR 0018 s2.2). 375 let f = fx("a"); 376 let o = encode(&f, 1, [0; 32], [7, 1000]); 377 assert_eq!(object_id(&o), object_id(&o.clone())); 378 let mut o2 = o.clone(); 379 o2[100] ^= 1; 380 assert_ne!(object_id(&o), object_id(&o2)); 381 }