From d8212aaa011ef1b55e7d59bfdfaa23bb8a340096 Mon Sep 17 00:00:00 2001 From: cflmy Date: Wed, 25 Nov 2026 09:46:04 +0800 Subject: [PATCH] loader: bind Analysis to the captured manifest and lock bytes Read marqdo.yaml and marqdo.lock exactly once while loading, parse those bytes, and record sha256 digests on LoadedProject. This removes the post-preparation lock re-read (and its silent empty digest on IO failure) and lets Analysis name the exact static snapshot that selected the program. Add a regression test that editing the manifest after load cannot change the captured digest. --- crates/cli/src/loader.rs | 90 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/crates/cli/src/loader.rs b/crates/cli/src/loader.rs index c0c0d17..0cc63b0 100644 --- a/crates/cli/src/loader.rs +++ b/crates/cli/src/loader.rs @@ -119,6 +119,14 @@ fn source_digest(source: &str) -> String { hex::encode(hasher.finalize()) } +/// `sha256:` of exact bytes, the wire form used by Analysis snapshots. +fn bytes_digest(bytes: &[u8]) -> String { + use sha2::{Digest, Sha256}; + let mut hasher = Sha256::new(); + hasher.update(bytes); + format!("sha256:{}", hex::encode(hasher.finalize())) +} + /// A resolved source location inside a loaded file. pub struct LocatedSource<'a> { /// The owning file. @@ -167,6 +175,13 @@ pub struct LoadedProject { pub manifest: Option, /// Verified lock file, when present (lock v4). pub lock: Option, + /// SHA-256 of the exact `marqdo.lock` bytes captured while loading, or an + /// empty string when the project has no lock. Analysis must never re-read + /// the file: a later edit cannot change the snapshot the program used. + pub lock_digest: String, + /// SHA-256 of the exact `marqdo.yaml` bytes captured while loading, or an + /// empty string when there is no manifest. + pub manifest_digest: String, /// Resolved `marqdo.yaml.extensions` activation sources (static paths only). pub extension_sources: Vec, } @@ -251,19 +266,33 @@ pub fn load(entry: &Path) -> Result { .map(|parent| parent.to_path_buf()) .unwrap_or_else(|| PathBuf::from(".")), }; - let manifest = if root.join(MANIFEST_FILE).is_file() { - Some(Manifest::load(&root.join(MANIFEST_FILE))?) + // Read each snapshot input exactly once. The digest recorded in Analysis + // is computed from these bytes, not from a second read after preparation. + let manifest_path = root.join(MANIFEST_FILE); + let (manifest, manifest_digest) = if manifest_path.is_file() { + let bytes = std::fs::read(&manifest_path) + .map_err(|error| format!("cannot read {MANIFEST_FILE}: {error}"))?; + let yaml = String::from_utf8(bytes.clone()) + .map_err(|error| format!("{MANIFEST_FILE} is not valid UTF-8: {error}"))?; + let manifest = Manifest::from_yaml(&yaml) + .map_err(|error| format!("{MANIFEST_FILE}: {error}"))?; + (Some(manifest), bytes_digest(&bytes)) } else { - None + (None, String::new()) }; - let lock = if root.join(LOCK_FILE).is_file() { - let lock = LockFile::load(&root.join(LOCK_FILE)) + let lock_path = root.join(LOCK_FILE); + let (lock, lock_digest) = if lock_path.is_file() { + let bytes = std::fs::read(&lock_path) + .map_err(|error| format!("cannot read {LOCK_FILE}: {error}"))?; + let yaml = String::from_utf8(bytes.clone()) + .map_err(|error| format!("{LOCK_FILE} is not valid UTF-8: {error}"))?; + let lock = LockFile::from_yaml(&yaml) .map_err(|error| format!("{LOCK_FILE}: {error}"))?; if let Some(manifest) = &manifest { lock.validate_root(manifest) .map_err(|error| format!("{LOCK_FILE}: {error}"))?; } - Some(lock) + (Some(lock), bytes_digest(&bytes)) } else { if manifest .as_ref() @@ -275,7 +304,7 @@ pub fn load(entry: &Path) -> Result { .to_string(), ); } - None + (None, String::new()) }; let resolver = PackageResolver::new(root.clone(), manifest.clone(), lock.clone()); let entry_module = resolver.resolve_entry(&entry)?; @@ -326,6 +355,8 @@ pub fn load(entry: &Path) -> Result { official_aliases, manifest, lock, + lock_digest, + manifest_digest, extension_sources, }) } @@ -683,3 +714,48 @@ fn collect_identifiers_expr(expr: &Expr, names: &mut std::collections::BTreeSet< Expr::Int(_) | Expr::Float(_) | Expr::Text(_) | Expr::Bool(_) | Expr::None => {} } } + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicU32, Ordering}; + + static COUNTER: AtomicU32 = AtomicU32::new(0); + + fn temp_project(files: &[(&str, &str)]) -> PathBuf { + let id = COUNTER.fetch_add(1, Ordering::Relaxed); + let root = std::env::temp_dir().join(format!( + "marqdo-loader-test-{}-{id}", + std::process::id() + )); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + for (name, source) in files { + std::fs::write(root.join(name), source).unwrap(); + } + root + } + + #[test] + fn project_snapshot_digests_are_captured_once() { + let root = temp_project(&[ + ("marqdo.yaml", "project:\n name: snap\n version: 0.1.0\n"), + ("index.mq.md", "## @exec main\n\n`return 1`\n"), + ]); + let entry = root.join("index.mq.md"); + let project = load(&entry).expect("load project"); + let manifest_bytes = std::fs::read(root.join("marqdo.yaml")).unwrap(); + assert_eq!(project.manifest_digest, bytes_digest(&manifest_bytes)); + assert!(project.lock_digest.is_empty()); + + // The snapshot is what the program used; editing the manifest after + // loading cannot change the recorded digest. + std::fs::write( + root.join("marqdo.yaml"), + "project:\n name: snap\n version: 0.2.0\n", + ) + .unwrap(); + assert_eq!(project.manifest_digest, bytes_digest(&manifest_bytes)); + let _ = std::fs::remove_dir_all(&root); + } +}