diff --git a/crates/weksa/src/run.rs b/crates/weksa/src/run.rs index de6d846..46670c8 100644 --- a/crates/weksa/src/run.rs +++ b/crates/weksa/src/run.rs @@ -2,7 +2,11 @@ //! and the one writer of both (`RunStores::create` and, for rounds, `round`). //! //! A run pins its corpus and entity store files by canonical path and FNV-1a 64 -//! digest of their bytes. Every round reopens them only from that record and +//! digest of their bytes. It works on its own copy of the inventory: +//! `RunStores::create` copies the caller's inventory file to +//! `/inventory.cc` and pins the digest of the copy; the caller's path is +//! kept as provenance and never opened again, and `open` refuses a head file +//! whose digest differs. Every round reopens them only from that record and //! refuses on drift, and every round record carries the digests. Nothing inside //! a process whose writer controls the code can stop an item faked into a //! scratch store; what this makes true is that a run over a scratch store is a @@ -46,6 +50,9 @@ pub struct RunRecord { /// Canonical absolute path of the corpus store. #[cultcache(key = 1)] pub corpus: String, + /// Canonical absolute path of the inventory store the run was opened over: + /// provenance only. `create` copies that file to `/inventory.cc` and + /// the run reads nothing at this path afterwards. #[cultcache(key = 2)] pub inventory: String, #[cultcache(key = 3)] @@ -67,6 +74,9 @@ pub struct RunRecord { pub embedder: String, #[cultcache(key = 10)] pub chrf_version: String, + /// 16 hex of the bytes `create` copied to `/inventory.cc`. + #[cultcache(key = 11)] + pub inventory_digest: String, } /// Where a round refused: the first stage that did. @@ -276,6 +286,8 @@ pub enum DriftOf { Lowerer, Model, Embedder, + /// The run's own head inventory file. + Inventory, } /// A run's failure. Display and Debug name a kind, never a path, key, label or @@ -340,10 +352,17 @@ pub struct RunStores { entities: EntityStore, } -fn file_digest(path: &str) -> Option { +/// The run's own copy of the inventory, made at `create`. +const INVENTORY_FILE: &str = "inventory.cc"; + +fn file_digest_at(path: &Path) -> Option { std::fs::read(path).ok().map(|b| hex16(fnv1a64(&b))) } +fn file_digest(path: &str) -> Option { + file_digest_at(Path::new(path)) +} + fn canonical(path: &Path) -> Result { std::fs::canonicalize(path) .map_err(|e| RunError::Io(e.kind()))? @@ -403,10 +422,15 @@ impl RunStores { model: model.to_string(), embedder: embedder.to_string(), chrf_version: CHRF_VERSION.to_string(), + inventory_digest: String::new(), }; + // The run works on its own copy; the caller's store is never opened again. + let head = root.join(INVENTORY_FILE); + std::fs::copy(&record.inventory, &head).map_err(|e| RunError::Io(e.kind()))?; let record = RunRecord { corpus_digest: file_digest(&record.corpus).ok_or(RunError::Store)?, entity_digest: file_digest(&record.entities).ok_or(RunError::Store)?, + inventory_digest: file_digest_at(&head).ok_or(RunError::Store)?, ..record }; cache @@ -435,6 +459,9 @@ impl RunStores { if file_digest(&record.entities).as_ref() != Some(&record.entity_digest) { return Err(RunError::Drift(DriftOf::Entities)); } + if file_digest_at(&root.join(INVENTORY_FILE)).as_ref() != Some(&record.inventory_digest) { + return Err(RunError::Drift(DriftOf::Inventory)); + } Self::with(root, cache, record) } @@ -443,7 +470,7 @@ impl RunStores { Ok(Self { root: root.to_path_buf(), corpus: CorpusStore::open(Path::new(&record.corpus)).map_err(store)?, - inventory: InventoryStore::open(Path::new(&record.inventory)).map_err(store)?, + inventory: InventoryStore::open(&root.join(INVENTORY_FILE)).map_err(store)?, entities: EntityStore::open(Path::new(&record.entities)).map_err(store)?, cache, record, diff --git a/crates/weksa/tests/golf_loop.rs b/crates/weksa/tests/golf_loop.rs index 6631861..c2eec2b 100644 --- a/crates/weksa/tests/golf_loop.rs +++ b/crates/weksa/tests/golf_loop.rs @@ -120,6 +120,10 @@ fn open_checks_each_pin_by_name() { drift(&|r| r.entity_digest = "0".repeat(16)), Some(RunError::Drift(DriftOf::Entities)) ); + assert_eq!( + drift(&|r| r.inventory_digest = "0".repeat(16)), + Some(RunError::Drift(DriftOf::Inventory)) + ); assert_eq!(drift(&|_| {}), None, "the unchanged record opens"); // An entity put after the pin is another entity store. @@ -132,6 +136,146 @@ fn open_checks_each_pin_by_name() { ); } +#[test] +fn a_run_works_on_its_own_copy_of_the_inventory() { + let w = world(); + let dir = w.dir.path().to_path_buf(); + let root = dir.join("run"); + let (c, i, e) = paths(&dir); + let source_bytes = std::fs::read(&i).unwrap(); + let mut run = RunStores::create( + &root, + "t", + RunPaths { + corpus: &c, + inventory: &i, + entities: &e, + }, + "fake-model", + "bag", + ) + .unwrap(); + + // The copy is byte for byte the source, and the record pins its digest. + let copy = root.join("inventory.cc"); + assert_eq!(std::fs::read(©).unwrap(), source_bytes); + assert_eq!(run.record().inventory_digest, hex(&i)); + assert_eq!(run.record().inventory_digest, hex(©)); + assert_eq!( + run.record().inventory, + std::fs::canonicalize(&i).unwrap().to_str().unwrap(), + "the source path is provenance" + ); + let rev = run.inventory().inventory().unwrap().latest().unwrap().n; + + // A revision written to the source after open is a revision the run never + // reads, and it does not drift the run. + let mut source = weksa::InventoryStore::open(&i).unwrap(); + source + .admit_revision( + weksa::InventoryRevision { + n: rev + 1, + parent: Some(rev), + changes: vec![], + frozen: false, + }, + vec![], + ) + .unwrap(); + assert_ne!(hex(&i), run.record().inventory_digest); + run = RunStores::open(&root).unwrap(); + assert_eq!( + run.inventory().inventory().unwrap().latest().unwrap().n, + rev, + "the run reads its copy" + ); + assert_eq!(std::fs::read(©).unwrap(), source_bytes); + + // Even with the source gone the run opens. + std::fs::remove_file(&i).unwrap(); + assert!(RunStores::open(&root).is_ok()); + + // A revision appended to the copy is drift. + let mut head = weksa::InventoryStore::open(©).unwrap(); + head.admit_revision( + weksa::InventoryRevision { + n: rev + 1, + parent: Some(rev), + changes: vec![], + frozen: false, + }, + vec![], + ) + .unwrap(); + assert_eq!( + RunStores::open(&root).err(), + Some(RunError::Drift(DriftOf::Inventory)) + ); +} + +/// `weksa.loop_run` as it was before the run kept its own inventory copy: keys +/// 0 to 10. +#[derive(Clone, cultcache_rs::DatabaseEntry)] +#[cultcache(type = "weksa.loop_run")] +struct LegacyRun { + #[cultcache(key = 0)] + label: String, + #[cultcache(key = 1)] + corpus: String, + #[cultcache(key = 2)] + inventory: String, + #[cultcache(key = 3)] + entities: String, + #[cultcache(key = 4)] + corpus_digest: String, + #[cultcache(key = 5)] + entity_digest: String, + #[cultcache(key = 6)] + prompt_digest: String, + #[cultcache(key = 7)] + lowerer_digest: String, + #[cultcache(key = 8)] + model: String, + #[cultcache(key = 9)] + embedder: String, + #[cultcache(key = 10)] + chrf_version: String, +} + +#[test] +fn a_run_from_before_the_copy_does_not_open() { + // A run.cc without key 11 is a scratch run off git: its open fails Store. + let w = world(); + let dir = w.dir.path().to_path_buf(); + let current = run_over(&dir).record().clone(); + let root = dir.join("run"); + std::fs::remove_file(root.join("run.cc")).unwrap(); + let mut cache = CultCache::new(); + cache.register_entry_type::().unwrap(); + cache + .add_generic_backing_store(SingleFileMessagePackBackingStore::new(root.join("run.cc"))) + .unwrap(); + cache + .put( + "run:t", + &LegacyRun { + label: current.label, + corpus: current.corpus, + inventory: current.inventory, + entities: current.entities, + corpus_digest: current.corpus_digest, + entity_digest: current.entity_digest, + prompt_digest: current.prompt_digest, + lowerer_digest: current.lowerer_digest, + model: current.model, + embedder: current.embedder, + chrf_version: current.chrf_version, + }, + ) + .unwrap(); + assert_eq!(RunStores::open(&root).err(), Some(RunError::Store)); +} + #[test] fn source_digests_cover_their_files() { let src = Path::new(env!("CARGO_MANIFEST_DIR")).join("src"); diff --git a/schemas/cultnet/weksa.loop_run.schema.json b/schemas/cultnet/weksa.loop_run.schema.json index b04b8b4..69bb0a4 100644 --- a/schemas/cultnet/weksa.loop_run.schema.json +++ b/schemas/cultnet/weksa.loop_run.schema.json @@ -26,6 +26,11 @@ "type": "string" }, "inventory": { + "description": "Canonical absolute path of the inventory store the run was opened over:\nprovenance only. `create` copies that file to `/inventory.cc` and\nthe run reads nothing at this path afterwards.", + "type": "string" + }, + "inventory_digest": { + "description": "16 hex of the bytes `create` copied to `/inventory.cc`.", "type": "string" }, "label": { @@ -53,6 +58,7 @@ "lowerer_digest", "model", "embedder", - "chrf_version" + "chrf_version", + "inventory_digest" ] }