Skip to content

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

Merge branch 'storage-drivers-host' into storage-approvals

Steven Enamakel28f9411c2d87
diff --cc crates/openhuman-core/src/storage/mod.rs
index 84fa4819a2,0e8f26f4ea..c4299297bc
--- a/crates/openhuman-core/src/storage/mod.rs
+++ b/crates/openhuman-core/src/storage/mod.rs
@@@ -109,76 -84,29 +109,92 @@@ pub fn installed() -> Option<Arc<dyn St
/// Removes the installed backend; returns whether there was one.
pub fn clear() -> bool {
- BACKEND
- .write()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .take()
- .is_some()
+ BACKEND.clear()
+}
+
+/// 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(&current_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)?
}
+ /// Whether a backend with this driver name may be shared by several
+ /// processes at once. A MongoDB database can be; SQLite files, the memory
+ /// driver and plain files belong to the one process that opened them.
+ ///
+ /// Boot-time recovery (interrupting in-flight turns, reaping orphaned runs)
+ /// is only sound on a backend no other process can be writing to.
+ pub fn driver_is_shared(driver: &str) -> bool {
+ driver == "mongodb"
+ }
+
+ /// Whether the installed backend may be shared with other processes; `false`
+ /// when none is installed. See [`driver_is_shared`].
+ pub fn installed_is_shared() -> bool {
+ installed().is_some_and(|backend| driver_is_shared(backend.driver()))
+ }
+
/// 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 --cc crates/openhuman-core/src/storage/mod_tests.rs
index 55db3f3915,c9e2b8e1c1..f6c173b6ef
--- a/crates/openhuman-core/src/storage/mod_tests.rs
+++ b/crates/openhuman-core/src/storage/mod_tests.rs
@@@ -63,36 -58,10 +63,44 @@@ fn agent_scopes_match_the_session_store
.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);
+}
++
+ #[test]
+ fn only_mongodb_is_shared_between_processes() {
+ assert!(driver_is_shared("mongodb"));
+ for driver in ["sqlite", "memory", "file"] {
+ assert!(!driver_is_shared(driver), "{driver}");
+ }
+ }