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/memory/deletion.rs,crates/openhuman-core/src/memory/s

Steven Enamakel8a343d322b23
diff --git a/crates/openhuman-core/src/memory/deletion.rs b/crates/openhuman-core/src/memory/deletion.rs
index 7432e4bd73..e23230c784 100644
--- a/crates/openhuman-core/src/memory/deletion.rs
+++ b/crates/openhuman-core/src/memory/deletion.rs
@@ -38,7 +38,7 @@ struct FileStore {
impl ListStore<PendingDeletion> for FileStore {
fn load(&self) -> std::result::Result<Vec<PendingDeletion>, StoreError> {
- match super::state_store::load(&self.workspace_dir, STATE) {
+ match super::state_store::load_workspace(&self.workspace_dir, STATE) {
Ok(Some(pending)) => Ok(pending),
Ok(None) => Ok(Vec::new()),
Err(error) => {
@@ -50,10 +50,14 @@ impl ListStore<PendingDeletion> for FileStore {
fn save(&self, all: &[PendingDeletion]) -> std::result::Result<(), StoreError> {
if all.is_empty() {
- return super::state_store::remove(&self.workspace_dir, STATE)
+ return super::state_store::remove_workspace::<Vec<PendingDeletion>>(
+ &self.workspace_dir,
+ STATE,
+ )
.map_err(|error| StoreError(error.to_string()));
}
- super::state_store::save(&self.workspace_dir, STATE, &all)
+ let all = all.to_vec();
+ super::state_store::save_workspace(&self.workspace_dir, STATE, &all)
.map_err(|error| StoreError(error.to_string()))
}
}
diff --git a/crates/openhuman-core/src/memory/state_store.rs b/crates/openhuman-core/src/memory/state_store.rs
index 42ab339ccc..0b2fd660dd 100644
--- a/crates/openhuman-core/src/memory/state_store.rs
+++ b/crates/openhuman-core/src/memory/state_store.rs
@@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use serde::de::DeserializeOwned;
use serde::Serialize;
+use sha2::Digest;
use crate::storage::state_kv::StateKv;
@@ -37,6 +38,54 @@ fn open_quarantining<T: DeserializeOwned>(workspace_dir: &Path, name: &str) -> R
Ok(kv)
}
+fn workspace_scope(workspace_dir: &Path) -> Result<crate::storage::Scope> {
+ let path = std::fs::canonicalize(workspace_dir).unwrap_or_else(|_| workspace_dir.to_path_buf());
+ let digest = sha2::Sha256::digest(path.to_string_lossy().as_bytes());
+ let suffix = digest
+ .iter()
+ .take(12)
+ .map(|byte| format!("{byte:02x}"))
+ .collect::<String>();
+ crate::storage::scope_for_module(None, &format!("memory-workspace-{suffix}"))
+}
+
+fn open_workspace_quarantining<T: DeserializeOwned>(
+ workspace_dir: &Path,
+ name: &str,
+) -> Result<StateKv> {
+ let scope = workspace_scope(workspace_dir)?;
+ let kv = StateKv::open_in_scope(workspace_dir, &scope, OWNER, NS)?;
+ kv.import_file_quarantining::<T>(name, &legacy_path(workspace_dir, name))?;
+ Ok(kv)
+}
+
+/// A workspace-owned state value in a deterministic local module scope. Use
+/// this for queues that must remain readable after the ambient profile scope
+/// has ended, while remaining isolated from other workspaces on shared storage.
+pub(crate) fn load_workspace<T: DeserializeOwned + Send + 'static>(
+ workspace_dir: &Path,
+ name: &str,
+) -> Result<Option<T>> {
+ open_workspace_quarantining::<T>(workspace_dir, name)?.get(name)
+}
+
+/// Stores a workspace-owned state value in its deterministic local module scope.
+pub(crate) fn save_workspace<T>(workspace_dir: &Path, name: &str, value: &T) -> Result<()>
+where
+ T: Serialize + DeserializeOwned + Send + 'static,
+{
+ open_workspace_quarantining::<T>(workspace_dir, name)?.put(name, value)
+}
+
+/// Removes a workspace-owned state value from its deterministic local module scope.
+pub(crate) fn remove_workspace<T: DeserializeOwned + Send + 'static>(
+ workspace_dir: &Path,
+ name: &str,
+) -> Result<()> {
+ open_workspace_quarantining::<T>(workspace_dir, name)?.delete(name)?;
+ Ok(())
+}
+
/// [`open`] for a value a damaged legacy file must not reset.
fn open_strict(workspace_dir: &Path, name: &str) -> Result<StateKv> {
let kv = StateKv::open(workspace_dir, OWNER, NS)?;
diff --git a/crates/openhuman-core/src/memory/state_store_tests.rs b/crates/openhuman-core/src/memory/state_store_tests.rs
index c0842d4d30..69fa928c3d 100644
--- a/crates/openhuman-core/src/memory/state_store_tests.rs
+++ b/crates/openhuman-core/src/memory/state_store_tests.rs
@@ -41,6 +41,50 @@ async fn host_memory_state_adapters_use_the_state_kv_port() {
}
}
+#[test]
+fn workspace_scopes_keep_retry_queues_stable_and_isolated_on_shared_storage() {
+ let first = tempfile::tempdir().unwrap();
+ let second = tempfile::tempdir().unwrap();
+ let backend = crate::storage::MemoryStorage::new();
+ let first_scope = super::workspace_scope(first.path()).unwrap();
+ let second_scope = super::workspace_scope(second.path()).unwrap();
+ let first_storage = backend.for_scope(&first_scope).unwrap();
+ let second_storage = backend.for_scope(&second_scope).unwrap();
+ let first_state = crate::storage::state_kv::StateKv::over(
+ &first_storage,
+ "memory",
+ "state",
+ );
+
+ first_state
+ .put("pending_deletions", &vec!["thread-a".to_string()])
+ .unwrap();
+
+ let first_reopened = crate::storage::state_kv::StateKv::over(
+ &first_storage,
+ "memory",
+ "state",
+ );
+ let second_state = crate::storage::state_kv::StateKv::over(
+ &second_storage,
+ "memory",
+ "state",
+ );
+ assert_eq!(
+ first_reopened
+ .get::<Vec<String>>("pending_deletions")
+ .unwrap(),
+ Some(vec!["thread-a".to_string()])
+ );
+ assert!(
+ second_state
+ .get::<Vec<String>>("pending_deletions")
+ .unwrap()
+ .is_none(),
+ "a different workspace cannot read the queue"
+ );
+}
+
#[test]
fn state_values_do_not_leak_between_workspaces() {
let first = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/storage/state_kv.rs b/crates/openhuman-core/src/storage/state_kv.rs
index eb9554a5a0..1d04d1b61d 100644
--- a/crates/openhuman-core/src/storage/state_kv.rs
+++ b/crates/openhuman-core/src/storage/state_kv.rs
@@ -27,7 +27,7 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tinystoragedrivers::{
- CollectionSpec, DocumentStore, ErrorKind, Key, Kv, ScopedStorage, StorageError,
+ CollectionSpec, DocumentStore, ErrorKind, Key, Kv, Scope, ScopedStorage, StorageError,
};
use super::documents::Repo;
@@ -69,6 +69,23 @@ impl StateKv {
Ok(Self { repo, owner, ns })
}
+ /// The state of `workspace_dir` in an explicit scope on that workspace's
+ /// configured backend.
+ ///
+ /// # Errors
+ ///
+ /// When the backend cannot be opened or refuses `scope`.
+ pub fn open_in_scope(
+ workspace_dir: &Path,
+ scope: &Scope,
+ owner: &'static str,
+ ns: &'static str,
+ ) -> Result<Self> {
+ let backend = super::local::backend_for(workspace_dir)?;
+ let repo = Repo::on(&backend, scope, "storage::state_kv", collections)?;
+ Ok(Self { repo, owner, ns })
+ }
+
/// The state over an already scoped handle (tests, explicit scopes).
#[must_use]
pub fn over(scoped: &ScopedStorage, owner: &'static str, ns: &'static str) -> Self {