Skip to content

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

refactor(storage): improve state kv key generation and logic

Steven Enamakelf74c8368165f
diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rs
index 0a06de43d2..0117fc0d70 100644
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@ -55,7 +55,7 @@ pub use scope::{
};
use std::future::Future;
-use std::sync::{Arc, LazyLock, Mutex, OnceLock, RwLock, mpsc};
+use std::sync::{mpsc, Arc, LazyLock, Mutex, OnceLock, RwLock};
/// SQLite bindings re-exported by the storage driver crate, for the two
/// consumers that open a SQLite file that is not a storage backend: the
diff --git a/crates/openhuman-core/src/storage/state_kv.rs b/crates/openhuman-core/src/storage/state_kv.rs
index 3da6fee484..899c5ab246 100644
--- a/crates/openhuman-core/src/storage/state_kv.rs
+++ b/crates/openhuman-core/src/storage/state_kv.rs
@@ -26,9 +26,9 @@ use anyhow::{Context, Result};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
+use sha2::Digest;
use tinystoragedrivers::{CollectionSpec, Cursor, ErrorKind, Precondition, Scope, StorageError};
use tinystoragedrivers_api::KvStore;
-use sha2::Digest;
use super::documents::Repo;
use super::import_state;
@@ -94,11 +94,18 @@ impl StateKv {
fn prefix(&self) -> String {
let namespace = format!("{}\0{}", self.owner, self.ns);
- format!("openhuman-state:{}:", hex::encode(sha2::Sha256::digest(namespace.as_bytes())))
+ format!(
+ "openhuman-state:{}:",
+ hex::encode(sha2::Sha256::digest(namespace.as_bytes()))
+ )
}
fn id(&self, name: &str) -> Result<String, StorageError> {
- Ok(format!("{}{}", self.prefix(), hex::encode(sha2::Sha256::digest(name.as_bytes()))))
+ Ok(format!(
+ "{}{}",
+ self.prefix(),
+ hex::encode(sha2::Sha256::digest(name.as_bytes()))
+ ))
}
/// The value stored under `name`.
@@ -143,13 +150,12 @@ impl StateKv {
/// A storage error.
pub fn delete(&self, name: &str) -> Result<bool> {
let (this, name) = (self.clone(), name.to_owned());
- self.repo
- .run_storage(move |storage| async move {
- match storage.kv().get(&this.id(&name)?).await? {
- Some(current) => storage.kv().delete(&current.id, current.unchanged()).await,
- None => Ok(false),
- }
- })
+ self.repo.run_storage(move |storage| async move {
+ match storage.kv().get(&this.id(&name)?).await? {
+ Some(current) => storage.kv().delete(&current.id, current.unchanged()).await,
+ None => Ok(false),
+ }
+ })
}
/// Every `(name, value)` whose name starts with `prefix`, in name order.
@@ -199,7 +205,11 @@ impl StateKv {
let this = self.clone();
let name = name.to_owned();
self.repo.run_storage(move |storage| async move {
- match storage.kv().cas(&this.id(&name)?, body, Precondition::Absent).await {
+ match storage
+ .kv()
+ .cas(&this.id(&name)?, body, Precondition::Absent)
+ .await
+ {
Ok(_) => Ok(true),
Err(error) if error.kind() == ErrorKind::Conflict => Ok(false),
Err(error) => Err(error),
@@ -255,7 +265,9 @@ impl StateKv {
Err(error) => return Err(error),
}
}
- Err(StorageError::conflict("state update exceeded its retry budget"))
+ Err(StorageError::conflict(
+ "state update exceeded its retry budget",
+ ))
})
}
@@ -272,10 +284,7 @@ impl StateKv {
fn decode_entry(&self, storage_key: &str, value: Value) -> Result<KvEntry, StorageError> {
let entry: KvEntry = serde_json::from_value(value)
.map_err(|error| StorageError::invalid_input(error.to_string()))?;
- if entry.owner != self.owner
- || entry.ns != self.ns
- || self.id(&entry.key)? != storage_key
- {
+ if entry.owner != self.owner || entry.ns != self.ns || self.id(&entry.key)? != storage_key {
return Err(StorageError::invalid_input(
"state KV identity envelope does not match its storage prefix",
));
diff --git a/crates/openhuman-core/src/storage/state_kv_tests.rs b/crates/openhuman-core/src/storage/state_kv_tests.rs
index a31d6b9a35..dc276ef089 100644
--- a/crates/openhuman-core/src/storage/state_kv_tests.rs
+++ b/crates/openhuman-core/src/storage/state_kv_tests.rs
@@ -64,8 +64,15 @@ fn put_get_list_delete_round_trip() {
fn scopes_do_not_see_each_others_state() {
let backend: std::sync::Arc<dyn StorageBackend> =
std::sync::Arc::new(tinystoragedrivers_testkit::MemoryBackend::new());
- let alice = StateKv::on_backend(&backend, &Scope::new("alice").unwrap(), "test_owner", "state").unwrap();
- let bob = StateKv::on_backend(&backend, &Scope::new("bob").unwrap(), "test_owner", "state").unwrap();
+ let alice = StateKv::on_backend(
+ &backend,
+ &Scope::new("alice").unwrap(),
+ "test_owner",
+ "state",
+ )
+ .unwrap();
+ let bob =
+ StateKv::on_backend(&backend, &Scope::new("bob").unwrap(), "test_owner", "state").unwrap();
alice.put("x", &1u32).unwrap();
assert!(bob.get::<u32>("x").unwrap().is_none());
}
@@ -233,7 +240,8 @@ fn typed_quarantine_validation_is_cached_per_backend_scope_name_and_type() {
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(tinystoragedrivers_testkit::MemoryBackend::new());
+ let backend: std::sync::Arc<dyn StorageBackend> =
+ std::sync::Arc::new(tinystoragedrivers_testkit::MemoryBackend::new());
let local = StateKv::on_backend(&backend, &Scope::local(), "test_owner", "state").unwrap();
local
.import_file_quarantining::<CountedA>("first", &first_legacy)