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 / deployment.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 | //! Host-produced deployment record `<home>.allen-deployments`: which model |
| 17 | //! artifacts served which logical agent, in order. Append-only JSON lines, |
| 18 | //! each line hash-chained to the previous one. Digests come from hashing |
| 19 | //! files on the host; model-supplied identity never enters (arch#150 s4.4). |
| 20 | //! The subject object is never written here or by a model swap: a swap or a |
| 21 | //! rollback appends a line and nothing else. |
| 22 | //! |
| 23 | //! `placeholder: true` marks synthetic digests used by tests. Such a line |
| 24 | //! does NOT establish real model-swap continuity. |
| 25 | use crate::{hex, unhex32}; |
| 26 | use serde::{Deserialize, Serialize}; |
| 27 | use sha2::{Digest, Sha256}; |
| 28 | use std::io::{Read, Write}; |
| 29 | use std::path::{Path, PathBuf}; |
| 30 | |
| 31 | pub const GENESIS_PREV: &str = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 32 | |
| 33 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 34 | pub struct Entry { |
| 35 | pub seq: u64, |
| 36 | pub prev: String, |
| 37 | /// Logical agent id (hex) this deployment serves. |
| 38 | pub agent: String, |
| 39 | pub model_sha256: String, |
| 40 | pub config_sha256: String, |
| 41 | pub candidate_id: String, |
| 42 | pub executable_sha256: String, |
| 43 | pub revision: String, |
| 44 | pub placeholder: bool, |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 48 | struct Line { |
| 49 | entry: Entry, |
| 50 | sha256: String, |
| 51 | } |
| 52 | |
| 53 | #[derive(Debug, Clone)] |
| 54 | pub struct Input { |
| 55 | pub model_sha256: String, |
| 56 | pub config_sha256: String, |
| 57 | pub candidate_id: String, |
| 58 | pub executable_sha256: String, |
| 59 | pub revision: String, |
| 60 | pub placeholder: bool, |
| 61 | } |
| 62 | |
| 63 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 64 | pub enum DeployRefusal { |
| 65 | Damaged(String), |
| 66 | ChainBroken(u64), |
| 67 | ForgedIdentity(u64), |
| 68 | ArtifactChanged { |
| 69 | seq: u64, |
| 70 | recorded: String, |
| 71 | actual: String, |
| 72 | }, |
| 73 | Io(String), |
| 74 | } |
| 75 | |
| 76 | impl std::fmt::Display for DeployRefusal { |
| 77 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 78 | match self { |
| 79 | Self::Damaged(w) => write!(f, "deployment record damaged: {w}"), |
| 80 | Self::ChainBroken(s) => write!(f, "deployment chain broken at line {s}"), |
| 81 | Self::ForgedIdentity(s) => { |
| 82 | write!(f, "deployment line {s} names another logical agent") |
| 83 | } |
| 84 | Self::ArtifactChanged { |
| 85 | seq, |
| 86 | recorded, |
| 87 | actual, |
| 88 | } => write!( |
| 89 | f, |
| 90 | "artifact bytes changed since deployment {seq}: recorded {recorded}, now {actual}" |
| 91 | ), |
| 92 | Self::Io(w) => write!(f, "deployment record io: {w}"), |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | pub fn deployments_path(home: &Path) -> PathBuf { |
| 98 | let mut n = home.as_os_str().to_owned(); |
| 99 | n.push(".allen-deployments"); |
| 100 | PathBuf::from(n) |
| 101 | } |
| 102 | |
| 103 | fn entry_hash(e: &Entry) -> String { |
| 104 | hex(&Sha256::digest( |
| 105 | serde_json::to_string(e) |
| 106 | .expect("entry serializes") |
| 107 | .as_bytes(), |
| 108 | )) |
| 109 | } |
| 110 | |
| 111 | /// sha256 of a file, hex: the host-side way to name a model artifact. |
| 112 | pub fn sha256_file_hex(path: &Path) -> std::io::Result<String> { |
| 113 | let mut f = std::fs::File::open(path)?; |
| 114 | let mut h = Sha256::new(); |
| 115 | let mut buf = vec![0u8; 1 << 20]; |
| 116 | loop { |
| 117 | let n = f.read(&mut buf)?; |
| 118 | if n == 0 { |
| 119 | break; |
| 120 | } |
| 121 | h.update(&buf[..n]); |
| 122 | } |
| 123 | Ok(hex(&h.finalize())) |
| 124 | } |
| 125 | |
| 126 | /// Read and verify the whole record for `agent` (hex). Empty/missing = no history. |
| 127 | pub fn verify(path: &Path, agent: &[u8; 32]) -> Result<Vec<Entry>, DeployRefusal> { |
| 128 | let text = match std::fs::read_to_string(path) { |
| 129 | Ok(t) => t, |
| 130 | Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()), |
| 131 | Err(e) => return Err(DeployRefusal::Damaged(e.to_string())), |
| 132 | }; |
| 133 | let agent_hex = hex(agent); |
| 134 | let mut out: Vec<Entry> = Vec::new(); |
| 135 | for (i, raw) in text.lines().enumerate() { |
| 136 | let n = i as u64 + 1; |
| 137 | let line: Line = serde_json::from_str(raw) |
| 138 | .map_err(|e| DeployRefusal::Damaged(format!("line {n}: {e}")))?; |
| 139 | let e = line.entry; |
| 140 | if entry_hash(&e) != line.sha256 || e.seq != n { |
| 141 | return Err(DeployRefusal::ChainBroken(n)); |
| 142 | } |
| 143 | let want_prev = out.last().map_or(GENESIS_PREV.to_string(), entry_hash); |
| 144 | if e.prev != want_prev { |
| 145 | return Err(DeployRefusal::ChainBroken(n)); |
| 146 | } |
| 147 | if unhex32(&e.agent).is_none() || e.agent != agent_hex { |
| 148 | return Err(DeployRefusal::ForgedIdentity(n)); |
| 149 | } |
| 150 | out.push(e); |
| 151 | } |
| 152 | if !text.is_empty() && !text.ends_with('\n') { |
| 153 | return Err(DeployRefusal::Damaged("truncated final line".into())); |
| 154 | } |
| 155 | Ok(out) |
| 156 | } |
| 157 | |
| 158 | /// Append one deployment line (verifies the existing record first). |
| 159 | pub fn append(path: &Path, agent: &[u8; 32], input: Input) -> Result<Entry, DeployRefusal> { |
| 160 | let have = verify(path, agent)?; |
| 161 | let e = Entry { |
| 162 | seq: have.len() as u64 + 1, |
| 163 | prev: have.last().map_or(GENESIS_PREV.to_string(), entry_hash), |
| 164 | agent: hex(agent), |
| 165 | model_sha256: input.model_sha256, |
| 166 | config_sha256: input.config_sha256, |
| 167 | candidate_id: input.candidate_id, |
| 168 | executable_sha256: input.executable_sha256, |
| 169 | revision: input.revision, |
| 170 | placeholder: input.placeholder, |
| 171 | }; |
| 172 | let line = Line { |
| 173 | sha256: entry_hash(&e), |
| 174 | entry: e.clone(), |
| 175 | }; |
| 176 | let io = |e: std::io::Error| DeployRefusal::Io(e.to_string()); |
| 177 | let mut f = std::fs::OpenOptions::new() |
| 178 | .create(true) |
| 179 | .append(true) |
| 180 | .open(path) |
| 181 | .map_err(io)?; |
| 182 | let mut s = serde_json::to_string(&line).expect("line serializes"); |
| 183 | s.push('\n'); |
| 184 | f.write_all(s.as_bytes()).map_err(io)?; |
| 185 | f.sync_all().map_err(io)?; |
| 186 | Ok(e) |
| 187 | } |
| 188 | |
| 189 | /// Does the model file on disk still match deployment `e`? |
| 190 | pub fn check_artifact(e: &Entry, file: &Path) -> Result<(), DeployRefusal> { |
| 191 | let actual = sha256_file_hex(file).map_err(|x| DeployRefusal::Io(x.to_string()))?; |
| 192 | if actual != e.model_sha256 { |
| 193 | return Err(DeployRefusal::ArtifactChanged { |
| 194 | seq: e.seq, |
| 195 | recorded: e.model_sha256.clone(), |
| 196 | actual, |
| 197 | }); |
| 198 | } |
| 199 | Ok(()) |
| 200 | } |