From 1b794987d94b78ddf8a2c6cdc13939de2c6f0897 Mon Sep 17 00:00:00 2001 From: Libby Date: Thu, 12 Nov 2026 14:24:42 +0000 Subject: [PATCH] cultcache-rs: pin load_soa's depth refusal and the write gate after a duplicate-index or retype pull Co-Authored-By: Claude Sonnet 5.5 --- packages/cultcache-rs/tests/union_parity.rs | 17 +++++++ packages/cultcache-rs/tests/unique_index.rs | 50 ++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/cultcache-rs/tests/union_parity.rs b/packages/cultcache-rs/tests/union_parity.rs index bc3aba6d..5dbe0e45 100644 --- a/packages/cultcache-rs/tests/union_parity.rs +++ b/packages/cultcache-rs/tests/union_parity.rs @@ -619,6 +619,23 @@ fn depth_is_refused_at_load() { assert!(cache.snapshot().is_empty()); } +// load_soa is a load too: a table holding a payload nested past the bound is refused with the typed depth refusal, the +// prior view is kept, and a tail behind the depth is still the depth refusal. +#[test] +fn load_soa_refuses_a_payload_nested_past_the_bound() { + let mut deep = vec![0x91u8; MAX_PAYLOAD_DEPTH + 1]; + deep.push(0xc0); + let mut cache = CultCache::new(); + cache.register_entry_type::().unwrap(); + cache.load_soa(CultCacheSoaTable::from_envelopes(vec![envelope("kept", &vector("union-surface.msgpack"))])).unwrap(); + let before = cache.snapshot(); + for payload in [deep.clone(), [deep.clone(), vec![0xc0]].concat()] { + let error = cache.load_soa(CultCacheSoaTable::from_envelopes(vec![envelope("k", &payload)])).unwrap_err(); + assert_eq!(error.downcast_ref::(), Some(&TOO_DEEP), "{error:#}"); + assert_eq!(cache.snapshot(), before, "a refused load keeps the prior view"); + } +} + // A cache whose load was refused refuses every write and removal and leaves the store as it was: a // later write would otherwise persist the empty image over the records it never read. #[test] diff --git a/packages/cultcache-rs/tests/unique_index.rs b/packages/cultcache-rs/tests/unique_index.rs index 193ba6fb..58b97293 100644 --- a/packages/cultcache-rs/tests/unique_index.rs +++ b/packages/cultcache-rs/tests/unique_index.rs @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf}; use cultcache_rs::{ - CultCache, CultCacheEnvelope, CultCacheSoaTable, DatabaseEntry, SingleFileMessagePackBackingStore, + CultCache, CultCacheEnvelope, CultCacheLoadRefused, CultCacheSoaTable, DatabaseEntry, SingleFileMessagePackBackingStore, }; use serde::Deserialize; @@ -419,6 +419,54 @@ fn a_load_of_a_duplicate_image_is_refused_and_keeps_the_prior_view() { soa.put("item:y", &unique("K")).unwrap(); } +// A pull refused for a duplicate unique-index value, or for one key held under two types in two stores, closes the same +// write gate a payload refusal does: the cache holds no image of its stores, so a write would lay the empty image over them. +#[test] +fn a_pull_refused_for_a_duplicate_index_or_a_retype_closes_every_write() { + fn assert_gated(cache: &mut CultCache, path: &Path, door_label: &str) { + let before = std::fs::read(path).unwrap(); + let errors = [ + ("put", cache.put("item:new", &unique("N")).unwrap_err()), + ("put_raw_envelope", cache.put_raw_envelope(cache.prepare_entry("item:new", &unique("N")).unwrap().0).unwrap_err()), + ("delete", cache.delete::("item:keep").unwrap_err()), + ]; + for (door, error) in errors { + assert!(error.downcast_ref::().is_some(), "{door_label} {door}: {error:#}"); + } + assert_eq!(std::fs::read(path).unwrap(), before, "{door_label}: the store changed"); + } + + // Duplicate index value found in the stored image. + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("dup.cc"); + let mut cache = cache_at(&path); + cache.put("item:keep", &unique("K")).unwrap(); + let mut twin = twin_cache_at(&path); + twin.put("item:b", &Twin { sku: "S".into(), category: "c".into() }).unwrap(); + twin.put("item:a", &Twin { sku: "S".into(), category: "c".into() }).unwrap(); + assert!(error_text(cache.pull_all_backing_stores()).contains("both hold unique index 'Sku'")); + assert_gated(&mut cache, &path, "duplicate index"); + + // One key held under two types by two stores. The cache pulled cleanly first, so only the refused pull can close the gate. + let (first, second) = (dir.path().join("first.cc"), dir.path().join("second.cc")); + let mut cache = CultCache::new(); + cache.register_entry_type::().unwrap(); + cache.register_entry_type::().unwrap(); + cache.register_entry_type::().unwrap(); + cache.add_backing_store(SingleFileMessagePackBackingStore::new(&first), [Plain::TYPE]).unwrap(); + cache.add_backing_store(SingleFileMessagePackBackingStore::new(&second), [Other::TYPE]).unwrap(); + cache.pull_all_backing_stores().unwrap(); + cache.put("k", &Plain { value: "p".into() }).unwrap(); + let mut seed = CultCache::new(); + seed.register_entry_type::().unwrap(); + seed.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&second)).unwrap(); + seed.pull_all_backing_stores().unwrap(); + seed.put("k", &Other { sku: "o".into() }).unwrap(); + assert!(error_text(cache.pull_all_backing_stores()).contains("\"k\"")); + assert_gated(&mut cache, &first, "retype"); + assert_eq!(cache.get::("k").unwrap().unwrap().value, "p", "a refused pull keeps the prior view"); +} + #[test] fn a_multi_index_store_with_a_duplicate_on_the_second_alias_refuses_to_open() { let dir = tempfile::tempdir().unwrap();