diff --git a/crates/deploy/src/app.rs b/crates/deploy/src/app.rs index 50afcb4c..7cab68d6 100644 --- a/crates/deploy/src/app.rs +++ b/crates/deploy/src/app.rs @@ -433,7 +433,7 @@ async fn verify(opts: UpOptions) -> i32 { "hashing both shards (and the archive, when it is a file) in full, in parallel; this renews the stamps", ); let engine = tokio::process::Command::new(paths.bin("leleka-inference")) - .args(config.verify_args()) + .args(config.verify_args(true)) .env(RUNTIME_TOKEN_ENV, token) .env_remove("HF_TOKEN") .stdin(std::process::Stdio::null()) diff --git a/crates/deploy/src/config.rs b/crates/deploy/src/config.rs index 52f6bfa9..53899b7c 100644 --- a/crates/deploy/src/config.rs +++ b/crates/deploy/src/config.rs @@ -219,7 +219,7 @@ impl RunConfig { /// Arguments of `leleka-inference --verify-only --verify-rehash`: both shards hashed in full and the stamps renewed. /// A file-backed archive is verified by the engine too; a sealed-memfd profile has no archive here (the /// supervisor-side hash of the archive file is `leleka verify`'s other half). - pub fn verify_args(&self) -> Vec { + pub fn verify_args(&self, rehash: bool) -> Vec { let paths = self.paths(); let mut args = self.engine_args(KarArg::File); // Keep the seven fixed pairs, drop the archive, pack and backend switches the verification does not use. @@ -232,9 +232,11 @@ impl RunConfig { paths.kar_provenance().display().to_string(), ]); } + args.push("--verify-only".into()); + if rehash { + args.push("--verify-rehash".into()); + } args.extend([ - "--verify-only".into(), - "--verify-rehash".into(), "--verify-stamps".into(), paths.engine_stamps().display().to_string(), ]); @@ -391,6 +393,49 @@ mod tests { assert_eq!(args[12], "--context-tokens"); } + #[test] + fn verify_arguments_keep_the_seven_pairs_and_choose_the_archive_by_profile() { + let server = config("rtx5090-32g"); + let args = server.verify_args(false); + assert_eq!(&args[..14], &server.engine_args(KarArg::Fd)[..14]); + let text = args.join(" "); + assert!( + text.ends_with("--verify-only --verify-stamps /var/lib/leleka/state/stamps/engine"), + "{text}" + ); + assert!( + !text.contains("--kar-archive") && !text.contains("--backend-mode"), + "{text}" + ); + assert!( + server + .verify_args(true) + .contains(&"--verify-rehash".to_owned()) + ); + let laptop = config("rtx3080-16g").verify_args(true).join(" "); + assert!( + laptop + .contains("--kar-archive-file /data/leleka/qwen-iq3-s-l2-v4.kar --kar-provenance"), + "{laptop}" + ); + } + + #[test] + fn stamps_are_in_the_engine_command_unless_switched_off() { + let mut c = config("rtx5090-32g"); + assert!( + c.engine_args(KarArg::Fd) + .join(" ") + .contains("--verify-stamps /var/lib/leleka/state/stamps/engine") + ); + c.verify_stamps = false; + assert!( + !c.engine_args(KarArg::Fd) + .join(" ") + .contains("--verify-stamps") + ); + } + #[test] fn laptop_uses_the_file_source_and_a_small_context() { let config = config("rtx3080-16g"); diff --git a/crates/deploy/src/profile.rs b/crates/deploy/src/profile.rs index 44d0d4fd..5ad7cc4a 100644 --- a/crates/deploy/src/profile.rs +++ b/crates/deploy/src/profile.rs @@ -15,6 +15,12 @@ pub const ARCHIVE_BYTES: u64 = 55_295_529_472; pub const ARCHIVE_SHA256_HEX: &str = "536d612ca60eab460cb978f51748fc6fa4e9d611d4601e777884e8b51385afa5"; +/// SHA-256 of the two pinned IQ3_S shards (`backend.rs` `SHARD0`, `SHARD1`). +pub const SHARD_SHA256_HEX: [&str; 2] = [ + "4c1eb2ceb4915e1192f4f386021897bde56a97f40a0bb78bb86465e0f7d2aca3", + "316b46f3a2dbd68c900f43136ab9449f9dcc3725dfd8c794847c204bc161e113", +]; + /// [`ARCHIVE_SHA256_HEX`] as bytes. pub fn archive_sha256() -> [u8; 32] { let mut out = [0u8; 32]; diff --git a/crates/deploy/src/supervisor.rs b/crates/deploy/src/supervisor.rs index 4d9bff40..db9f69d4 100644 --- a/crates/deploy/src/supervisor.rs +++ b/crates/deploy/src/supervisor.rs @@ -517,6 +517,45 @@ impl Supervisor { } } + /// Are both shards stamped as verified in their present state? (Then an engine start hashes neither.) + fn shard_stamps_current(&self) -> bool { + (0..2).all(|index| { + let digest = { + let mut out = [0u8; 32]; + hex::decode_to_slice(crate::profile::SHARD_SHA256_HEX[index], &mut out) + .is_ok() + .then_some(out) + }; + let identity = std::fs::File::open(self.paths.shard(index)) + .and_then(|f| f.metadata()) + .ok() + .and_then(|m| FileIdentity::of(&m)); + matches!((digest, identity), (Some(d), Some(id)) if self.stamps.trusts("gguf", &d, &id)) + }) + } + + /// While the supervisor fills the sealed memfd (reading 55 GB), a CPU-only `leleka-inference --verify-only` + /// hashes the two shards and records their stamps, so the engine that starts right after finds them verified. + /// The two reads overlap instead of following each other. Same verification, same refusals: a shard that does + /// not match its pin fails the start here, and a shard that changes before the engine opens it is hashed again + /// by the engine because the stamp no longer matches. + fn spawn_shard_preverify(&self) -> Option { + if !self.config.verify_stamps || self.shard_stamps_current() { + return None; + } + let program = self.paths.bin("leleka-inference"); + let args = self.config.verify_args(false); + match self.spawn("preverify", &program, &args, &BTreeMap::new(), None) { + Ok(child) => { + self.log.line( + "hashing the shards while the archive is copied (stamps are not current)", + ); + Some(child) + } + Err(_) => None, + } + } + async fn drive(&mut self, kar_fd: Option) -> Result<(), Flow> { // 1. the archive self.archive = match (kar_fd, self.config.profile.kar_source) { @@ -537,6 +576,7 @@ impl Supervisor { kar.display() )); let started = Instant::now(); + let mut preverify = self.spawn_shard_preverify(); // A stamp of the archive file (written after a start whose engine hashed the copy) lets the engine // skip the hash of the memfd; it applies only if the file is still in the stamped state before // AND after the copy. @@ -559,6 +599,27 @@ impl Supervisor { )); }, ); + if let Some(child) = preverify.as_mut() { + let waited = Instant::now(); + match child.wait().await { + Ok(status) if status.success() => self.log.line(&format!( + "shards verified ({:.1}s after the copy ended)", + waited.elapsed().as_secs_f32() + )), + Ok(status) => { + return fail( + 1, + format!( + "a shard does not match its pinned digest ({status}); see {}", + self.paths.logs().join("preverify.log").display() + ), + ); + } + Err(error) => { + return fail(1, format!("shard verification did not finish: {error}")); + } + } + } match sealed { Ok((file, identity)) => { self.log.line(&format!( diff --git a/crates/deploy/tests/recipe.rs b/crates/deploy/tests/recipe.rs index 65262808..a236b99c 100644 --- a/crates/deploy/tests/recipe.rs +++ b/crates/deploy/tests/recipe.rs @@ -123,6 +123,17 @@ fn the_pinned_archive_digest_of_the_stamps_is_the_one_the_engine_pins() { assert_eq!(leleka_deploy::profile::archive_sha256().len(), 32); } +#[test] +fn the_shard_pins_of_the_preverification_are_the_engines() { + let backend = read("crates/leleka-runtime/src/backend.rs"); + for digest in leleka_deploy::profile::SHARD_SHA256_HEX { + assert!( + backend.contains(&format!("sha256:{digest}")), + "{digest} is not pinned in backend.rs" + ); + } +} + #[cfg(unix)] #[test] fn install_script_is_valid_sh_and_verifies_before_it_runs() {