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-allen / src / binding.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 | //! The host pin record `<home>.allen-binding`: what this machine resolved |
| 17 | //! last. It is sovereign-core's own record, NOT part of the ALLEN subject |
| 18 | //! (ADR 0035 I3: the subject holds no machine id). The machine id is its own |
| 19 | //! field; there is no model field and no KV field. |
| 20 | //! |
| 21 | //! Layout (248 bytes, little-endian): "AIENALB1" | u32 version 1 | u32 0 | |
| 22 | //! agent 32 | root 32 | head object id 32 | u64 head sequence | lineage 32 | |
| 23 | //! machine id 32 | provenance 32 | sha256 of the first 216 bytes. |
| 24 | //! Limits (NOT_PROVED): the sha256 is unkeyed, so a pin deleted and |
| 25 | //! re-adopted, or the pin and the subject rolled back together, are not |
| 26 | //! detected. |
| 27 | use sha2::{Digest, Sha256}; |
| 28 | use std::io::Write; |
| 29 | use std::path::{Path, PathBuf}; |
| 30 | |
| 31 | pub const PIN_LEN: usize = 248; |
| 32 | const MAGIC: &[u8; 8] = b"AIENALB1"; |
| 33 | const VERSION: u32 = 1; |
| 34 | |
| 35 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 36 | pub struct Pin { |
| 37 | pub agent: [u8; 32], |
| 38 | pub root: [u8; 32], |
| 39 | pub head_id: [u8; 32], |
| 40 | pub head_seq: u64, |
| 41 | pub lineage: [u8; 32], |
| 42 | pub machine_id: [u8; 32], |
| 43 | pub provenance: [u8; 32], |
| 44 | } |
| 45 | |
| 46 | /// `<home>.allen-binding`, beside `<home>.machine-root` and `<home>.cortex-mark`. |
| 47 | pub fn pin_path(home: &Path) -> PathBuf { |
| 48 | let mut n = home.as_os_str().to_owned(); |
| 49 | n.push(".allen-binding"); |
| 50 | PathBuf::from(n) |
| 51 | } |
| 52 | |
| 53 | impl Pin { |
| 54 | pub fn encode(&self) -> [u8; PIN_LEN] { |
| 55 | let mut b = [0u8; PIN_LEN]; |
| 56 | b[..8].copy_from_slice(MAGIC); |
| 57 | b[8..12].copy_from_slice(&VERSION.to_le_bytes()); |
| 58 | b[16..48].copy_from_slice(&self.agent); |
| 59 | b[48..80].copy_from_slice(&self.root); |
| 60 | b[80..112].copy_from_slice(&self.head_id); |
| 61 | b[112..120].copy_from_slice(&self.head_seq.to_le_bytes()); |
| 62 | b[120..152].copy_from_slice(&self.lineage); |
| 63 | b[152..184].copy_from_slice(&self.machine_id); |
| 64 | b[184..216].copy_from_slice(&self.provenance); |
| 65 | let h = Sha256::digest(&b[..216]); |
| 66 | b[216..].copy_from_slice(&h); |
| 67 | b |
| 68 | } |
| 69 | |
| 70 | pub fn decode(b: &[u8]) -> Result<Pin, String> { |
| 71 | if b.len() != PIN_LEN { |
| 72 | return Err(format!("{} bytes, expected {PIN_LEN}", b.len())); |
| 73 | } |
| 74 | if &b[..8] != MAGIC { |
| 75 | return Err("bad magic".into()); |
| 76 | } |
| 77 | let v = u32::from_le_bytes(b[8..12].try_into().expect("4")); |
| 78 | if v != VERSION || b[12..16] != [0u8; 4] { |
| 79 | return Err(format!("unknown version {v}")); |
| 80 | } |
| 81 | if Sha256::digest(&b[..216]).as_slice() != &b[216..] { |
| 82 | return Err("checksum mismatch".into()); |
| 83 | } |
| 84 | let f = |a: usize| -> [u8; 32] { b[a..a + 32].try_into().expect("32") }; |
| 85 | Ok(Pin { |
| 86 | agent: f(16), |
| 87 | root: f(48), |
| 88 | head_id: f(80), |
| 89 | head_seq: u64::from_le_bytes(b[112..120].try_into().expect("8")), |
| 90 | lineage: f(120), |
| 91 | machine_id: f(152), |
| 92 | provenance: f(184), |
| 93 | }) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /// `Ok(None)` when no pin file exists; `Err` when it exists and is damaged. |
| 98 | pub fn read(path: &Path) -> Result<Option<Pin>, String> { |
| 99 | match std::fs::read(path) { |
| 100 | Ok(b) => Pin::decode(&b).map(Some), |
| 101 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), |
| 102 | Err(e) => Err(format!("read {}: {e}", path.display())), |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// Atomic write: temp file, fsync, rename, fsync of the directory. |
| 107 | pub fn write_atomic(path: &Path, pin: &Pin) -> Result<(), String> { |
| 108 | let mut tmp = path.as_os_str().to_owned(); |
| 109 | tmp.push(".tmp"); |
| 110 | let tmp = PathBuf::from(tmp); |
| 111 | let werr = |what: &str, e: std::io::Error| format!("{what} {}: {e}", path.display()); |
| 112 | if let Some(p) = path.parent() { |
| 113 | std::fs::create_dir_all(p).map_err(|e| werr("create dir for", e))?; |
| 114 | } |
| 115 | let mut f = std::fs::File::create(&tmp).map_err(|e| werr("create temp for", e))?; |
| 116 | f.write_all(&pin.encode()).map_err(|e| werr("write", e))?; |
| 117 | f.sync_all().map_err(|e| werr("sync", e))?; |
| 118 | drop(f); |
| 119 | std::fs::rename(&tmp, path).map_err(|e| werr("rename onto", e))?; |
| 120 | if let Some(p) = path.parent() { |
| 121 | std::fs::File::open(p) |
| 122 | .and_then(|d| d.sync_all()) |
| 123 | .map_err(|e| werr("sync dir of", e))?; |
| 124 | } |
| 125 | Ok(()) |
| 126 | } |