Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/storage/state_kv_tests.rs

Steven Enamakelb49bd70e78c1
diff --git a/crates/openhuman-core/src/storage/state_kv_tests.rs b/crates/openhuman-core/src/storage/state_kv_tests.rs
index 63ca0345f2..8a13a096ff 100644
--- a/crates/openhuman-core/src/storage/state_kv_tests.rs
+++ b/crates/openhuman-core/src/storage/state_kv_tests.rs
@@ -1,6 +1,36 @@
use super::*;
use crate::storage::{MemoryStorage, Scope, StorageBackend};
use std::collections::BTreeMap;
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+static COUNTED_A_DESERIALIZATIONS: AtomicUsize = AtomicUsize::new(0);
+static COUNTED_B_DESERIALIZATIONS: AtomicUsize = AtomicUsize::new(0);
+
+struct CountedA;
+
+impl<'de> serde::Deserialize<'de> for CountedA {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let _: serde_json::Value = serde::Deserialize::deserialize(deserializer)?;
+ COUNTED_A_DESERIALIZATIONS.fetch_add(1, Ordering::SeqCst);
+ Ok(Self)
+ }
+}
+
+struct CountedB;
+
+impl<'de> serde::Deserialize<'de> for CountedB {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ let _: serde_json::Value = serde::Deserialize::deserialize(deserializer)?;
+ COUNTED_B_DESERIALIZATIONS.fetch_add(1, Ordering::SeqCst);
+ Ok(Self)
+ }
+}
fn state(storage: &MemoryStorage, scope: &str) -> StateKv {
StateKv::over(
@@ -163,3 +193,64 @@ fn typed_import_keeps_valid_legacy_file_and_does_not_resurrect_deleted_value() {
assert!(kv.get::<BTreeMap<String, u32>>("valid").unwrap().is_none());
assert_eq!(std::fs::read(legacy).unwrap(), bytes);
}
+
+#[test]
+fn typed_quarantine_validation_is_cached_per_backend_scope_name_and_type() {
+ COUNTED_A_DESERIALIZATIONS.store(0, Ordering::SeqCst);
+ COUNTED_B_DESERIALIZATIONS.store(0, Ordering::SeqCst);
+ let dir = tempfile::tempdir().unwrap();
+ let first_legacy = dir.path().join("first.json");
+ let second_legacy = dir.path().join("second.json");
+ std::fs::write(&first_legacy, r#"{"ok":true}"#).unwrap();
+ std::fs::write(&second_legacy, r#"{"ok":true}"#).unwrap();
+
+ let backend: std::sync::Arc<dyn StorageBackend> =
+ std::sync::Arc::new(MemoryStorage::new());
+ let local = StateKv::on_backend(&backend, &Scope::local(), "test_owner", "state").unwrap();
+ local
+ .import_file_quarantining::<CountedA>("first", &first_legacy)
+ .unwrap();
+ let first_count = COUNTED_A_DESERIALIZATIONS.load(Ordering::SeqCst);
+ assert!(first_count > 0, "the legacy value was validated on first import");
+
+ local
+ .import_file_quarantining::<CountedA>("first", &first_legacy)
+ .unwrap();
+ assert_eq!(
+ COUNTED_A_DESERIALIZATIONS.load(Ordering::SeqCst),
+ first_count,
+ "reopening a valid legacy file does not reparse it"
+ );
+
+ local
+ .import_file_quarantining::<CountedA>("second", &second_legacy)
+ .unwrap();
+ assert!(
+ COUNTED_A_DESERIALIZATIONS.load(Ordering::SeqCst) > first_count,
+ "another state name gets its own validation"
+ );
+
+ let another_scope = StateKv::on_backend(
+ &backend,
+ &Scope::new("another").unwrap(),
+ "test_owner",
+ "state",
+ )
+ .unwrap();
+ let before_other_scope = COUNTED_A_DESERIALIZATIONS.load(Ordering::SeqCst);
+ another_scope
+ .import_file_quarantining::<CountedA>("first", &first_legacy)
+ .unwrap();
+ assert!(
+ COUNTED_A_DESERIALIZATIONS.load(Ordering::SeqCst) > before_other_scope,
+ "the same state name in another scope is validated independently"
+ );
+
+ local
+ .import_file_quarantining::<CountedB>("first", &first_legacy)
+ .unwrap();
+ assert!(
+ COUNTED_B_DESERIALIZATIONS.load(Ordering::SeqCst) > 0,
+ "a different requested value type gets its own validation"
+ );
+}