The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(storage): add scoped storage helpers and sync bridge
Steven Enamakele7ea429f175a
diff --git a/crates/openhuman-core/Cargo.toml b/crates/openhuman-core/Cargo.tomlindex daedb2d542..2cf4e2bc3f 100644--- a/crates/openhuman-core/Cargo.toml+++ b/crates/openhuman-core/Cargo.toml@@ -152,7 +152,7 @@ tinyagents-registry = { path = "../../vendor/tinyagents/crates/tinyagents-registtinyagents-session = { path = "../../vendor/tinyagents/crates/tinyagents-session", features = ["storage-drivers"] }# The storage ports and the URL-to-backend facade (`storage` domain). No driver# by default: the `storage-*` features below add them.-tinystoragedrivers = { path = "../../vendor/tinyagents/vendor/tinystoragedrivers/crates/tinystoragedrivers", default-features = false }+tinystoragedrivers = { path = "../../vendor/tinyagents/vendor/tinystoragedrivers/crates/tinystoragedrivers", default-features = false, features = ["blocking"] }tinyagents-runtime = { path = "../../vendor/tinyagents/crates/tinyagents-runtime" }tinyagents-orchestration = { path = "../../vendor/tinyagents/crates/tinyagents-orchestration" }tinyinference-core = { path = "../../vendor/tinyagents/vendor/tinyinference/crates/tinyinference-core" }diff --git a/crates/openhuman-core/src/storage/mod.rs b/crates/openhuman-core/src/storage/mod.rsindex a603f9a17c..891f5b92de 100644--- a/crates/openhuman-core/src/storage/mod.rs+++ b/crates/openhuman-core/src/storage/mod.rs@@ -18,10 +18,12 @@//! driver the build does not carry fails at [`open`] naming the feature, so a//! misconfigured deployment stops at boot instead of at its first write.-use std::sync::{Arc, LazyLock, RwLock};+use std::future::Future;+use std::sync::{Arc, LazyLock, OnceLock, RwLock};pub use tinystoragedrivers::{- Scope, ScopedStorage, StorageBackend, StorageConfig as StorageUrl, StorageError,+ Blocking, DocumentStore, DocumentStoreExt, Scope, ScopedStorage, StorageBackend,+ StorageConfig as StorageUrl, StorageError,};use crate::config::schema::storage::redact_url;@@ -91,6 +93,73 @@ pub fn clear() -> bool {.is_some()}+/// The storage scope of the current call: the acting agent's+/// ([`scope_for_agent`]) when the dispatch carries one+/// (`CoreContext::session_agent`), else [`Scope::local`] — except in SaaS+/// mode, where a call with no acting agent is refused rather than given a+/// bucket every user would share.+///+/// # Errors+///+/// In SaaS mode, when the current context names no agent.+pub fn current_scope() -> Result<Scope, StorageError> {+ let agent = crate::core::runtime::CoreContext::current()+ .and_then(|context| context.session_agent().map(str::to_string));+ scope_from(agent.as_deref(), crate::core::runtime::mode::is_saas())+}++/// [`current_scope`] with its two inputs made explicit, so the rule is+/// testable without a booted context or a locked mode.+///+/// # Errors+///+/// When `saas` and there is no `agent`.+pub fn scope_from(agent: Option<&str>, saas: bool) -> Result<Scope, StorageError> {+ match agent {+ Some(agent) => Ok(scope_for_agent(agent)),+ None if saas => Err(StorageError::invalid_input(+ "no acting agent in SaaS mode; refusing a shared storage scope",+ )),+ None => Ok(Scope::local()),+ }+}++/// The installed backend bound to [`current_scope`], or `None` when the host+/// configured no backend (the classic on-disk layout).+///+/// # Errors+///+/// When the scope cannot be resolved ([`current_scope`]) or the backend+/// refuses it.+pub fn current_scoped() -> Result<Option<ScopedStorage>, StorageError> {+ installed()+ .map(|backend| backend.for_scope(¤t_scope()?))+ .transpose()+}++/// Runs `future` to completion from synchronous code, on one dedicated+/// runtime thread shared by every caller in the process.+///+/// For domain stores whose API is synchronous (most of the core's), so they+/// can call the async storage ports without `block_in_place` — which would+/// panic on a current-thread runtime.+///+/// # Errors+///+/// When the bridge cannot start, or `future` called back into it.+pub fn block_on<T, F>(future: F) -> Result<T, StorageError>+where+ F: Future<Output = Result<T, StorageError>> + Send + 'static,+ T: Send + 'static,+{+ static BRIDGE: OnceLock<Result<Blocking, String>> = OnceLock::new();+ let bridge = BRIDGE+ .get_or_init(|| Blocking::new().map_err(|error| error.to_string()))+ .as_ref()+ .map_err(|error| StorageError::backend(error.clone()))?;+ bridge.run(future)?+}+/// The storage scope agent `agent_id`'s records live under — the same/// mapping the session store uses, so every domain agrees on it.pub fn scope_for_agent(agent_id: &str) -> Scope {diff --git a/crates/openhuman-core/src/storage/mod_tests.rs b/crates/openhuman-core/src/storage/mod_tests.rsindex a9535025e4..764b2ec2b0 100644--- a/crates/openhuman-core/src/storage/mod_tests.rs+++ b/crates/openhuman-core/src/storage/mod_tests.rs@@ -57,3 +57,28 @@ fn agent_scopes_match_the_session_store() {.as_str().starts_with("sha256:"));}++#[test]+fn the_scope_follows_the_acting_agent_and_fails_closed_in_saas() {+ assert_eq!(scope_from(Some("agent-7"), false).unwrap().as_str(), "agent-7");+ assert_eq!(scope_from(Some("agent-7"), true).unwrap().as_str(), "agent-7");+ assert!(scope_from(None, false).unwrap().is_local());+ assert!(scope_from(None, true).is_err(), "SaaS never falls back to a shared scope");+}++#[test]+fn block_on_runs_a_future_from_sync_code() {+ let answer = block_on(async { Ok::<_, StorageError>(42) }).unwrap();+ assert_eq!(answer, 42);+ let error = block_on(async { Err::<(), _>(StorageError::conflict("raced")) }).unwrap_err();+ assert_eq!(error.message(), "raced");+}++#[tokio::test(flavor = "multi_thread")]+async fn block_on_works_inside_a_runtime_too() {+ let answer = tokio::task::spawn_blocking(|| block_on(async { Ok::<_, StorageError>(7) }))+ .await+ .unwrap()+ .unwrap();+ assert_eq!(answer, 7);+}