Skip to content

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

feat(embed): add storage backend seam to runtime builder

Steven Enamakel5b6ad7a2824a
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index f9663bba8d..0a890efd7e 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -173,6 +173,9 @@ pub mod seams {
pub use openhuman_core::core::all::{ControllerExtension, DomainGroup};
pub use openhuman_core::core::server_launcher::{ServeRequest, ServerLauncher};
pub use openhuman_core::security::SecurityPolicy;
+ pub use openhuman_core::storage::StorageBackend;
+
+ pub use crate::runtime::StorageSource;
}
/// The session store port: what a host implements to keep every agent's
diff --git a/crates/openhuman-embed/src/runtime/build.rs b/crates/openhuman-embed/src/runtime/build.rs
index 5fba574cf9..c8fb92ca4e 100644
--- a/crates/openhuman-embed/src/runtime/build.rs
+++ b/crates/openhuman-embed/src/runtime/build.rs
@@ -130,9 +130,12 @@ impl RuntimeBuilder {
// build cannot undo (the stored API key, the host memory engine), so
// a refused controller extension leaves neither behind. Restorable
// seams are undone by this guard if the boot below fails.
- let mut seams = std::mem::take(&mut self.seams)
- .install()
+ let mut host_seams = std::mem::take(&mut self.seams);
+ host_seams
+ .open_storage()
+ .await
.map_err(RuntimeError::Invalid)?;
+ let mut seams = host_seams.install().map_err(RuntimeError::Invalid)?;
// Before `CoreBuilder::build()`: the scheduler gate reads the credential
// store exactly once, at boot, to decide whether it is signed in.
diff --git a/crates/openhuman-embed/src/runtime/run.rs b/crates/openhuman-embed/src/runtime/run.rs
index 94ef82e7ea..ae335dcffd 100644
--- a/crates/openhuman-embed/src/runtime/run.rs
+++ b/crates/openhuman-embed/src/runtime/run.rs
@@ -52,7 +52,11 @@ impl RuntimeBuilder {
each subcommand installs the policy its config describes"
);
}
- let seams = std::mem::take(&mut self.seams)
+ let mut host_seams = std::mem::take(&mut self.seams);
+ host_seams
+ .open_storage_blocking()
+ .map_err(|error| anyhow::Error::new(RuntimeError::Invalid(error)))?;
+ let seams = host_seams
.install()
.map_err(|error| anyhow::Error::new(RuntimeError::Invalid(error)))?;
seams.persist();
diff --git a/crates/openhuman-embed/src/runtime/seams.rs b/crates/openhuman-embed/src/runtime/seams.rs
index 4218ef4b6a..263c672585 100644
--- a/crates/openhuman-embed/src/runtime/seams.rs
+++ b/crates/openhuman-embed/src/runtime/seams.rs
@@ -13,6 +13,7 @@
//! | [`post_turn_hook`](RuntimeBuilder::post_turn_hook) / [`tool_hook`](RuntimeBuilder::tool_hook) | before boot, replacing a same-named hook | newest first, while ours still holds the name: ours removed, a replaced same-named hook restored |
//! | [`server_launcher`](RuntimeBuilder::server_launcher) | before boot | kept: first install wins for the process |
//! | [`live_policy`](RuntimeBuilder::live_policy) | after boot | kept: the next boot (or a config reload) installs its own |
+//! | [`storage`](RuntimeBuilder::storage) | before boot | the previous backend is restored (or cleared), if ours still holds the slot |
//!
//! The live policy goes in *after* boot because the core's bootstrap installs
//! one derived from the config; installing earlier would be overwritten. A
@@ -31,10 +32,50 @@ use openhuman_core::agent::tinyagents::discovery::{
use openhuman_core::core::all::{register_controller_extension, ControllerExtension};
use openhuman_core::core::server_launcher::{install_server_launcher, ServerLauncher};
use openhuman_core::security::SecurityPolicy;
+use openhuman_core::storage::StorageBackend;
use tinytools::ToolRanker;
use super::RuntimeBuilder;
+/// Where a runtime's storage backend (the `tinystoragedrivers` ports the
+/// core's `storage` domain reads) comes from.
+#[derive(Clone)]
+pub enum StorageSource {
+ /// A storage URL (`sqlite://…`, `memory`, `mongodb://…`), opened when the
+ /// runtime is built. A URL that cannot be opened fails the build.
+ Url(String),
+ /// A backend the host already opened.
+ Backend(Arc<dyn StorageBackend>),
+}
+
+impl std::fmt::Debug for StorageSource {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ // A URL can carry credentials: never print it.
+ match self {
+ Self::Url(_) => f.write_str("StorageSource::Url(<redacted>)"),
+ Self::Backend(backend) => write!(f, "StorageSource::Backend({})", backend.driver()),
+ }
+ }
+}
+
+impl From<&str> for StorageSource {
+ fn from(url: &str) -> Self {
+ Self::Url(url.to_string())
+ }
+}
+
+impl From<String> for StorageSource {
+ fn from(url: String) -> Self {
+ Self::Url(url)
+ }
+}
+
+impl From<Arc<dyn StorageBackend>> for StorageSource {
+ fn from(backend: Arc<dyn StorageBackend>) -> Self {
+ Self::Backend(backend)
+ }
+}
+
/// The seam options a builder collected, not yet installed.
#[derive(Default)]
pub(crate) struct HostSeams {
@@ -44,9 +85,40 @@ pub(crate) struct HostSeams {
pub(crate) tool_hooks: Vec<Arc<dyn ToolHook>>,
pub(crate) server_launcher: Option<ServerLauncher>,
pub(crate) live_policy: Option<Arc<SecurityPolicy>>,
+ pub(crate) storage: Option<StorageSource>,
}
impl HostSeams {
+ /// Open a [`StorageSource::Url`] into a backend, so [`install`](Self::install)
+ /// can stay synchronous. A no-op for any other source.
+ ///
+ /// # Errors
+ ///
+ /// The URL could not be parsed or opened.
+ pub(crate) async fn open_storage(&mut self) -> Result<(), String> {
+ if let Some(StorageSource::Url(url)) = &self.storage {
+ let backend = openhuman_core::storage::open(url)
+ .await
+ .map_err(|error| format!("opening the storage backend: {error}"))?;
+ self.storage = Some(StorageSource::Backend(backend));
+ }
+ Ok(())
+ }
+
+ /// [`open_storage`](Self::open_storage) from synchronous code, on the
+ /// core's dedicated storage runtime thread so the backend outlives the call.
+ pub(crate) fn open_storage_blocking(&mut self) -> Result<(), String> {
+ let Some(StorageSource::Url(url)) = self.storage.take() else {
+ return Ok(());
+ };
+ let backend = openhuman_core::storage::block_on(async move {
+ openhuman_core::storage::open(&url).await
+ })
+ .map_err(|error| format!("opening the storage backend: {error}"))?;
+ self.storage = Some(StorageSource::Backend(backend));
+ Ok(())
+ }
+
/// Install every pre-boot seam and return the guard that undoes the
/// restorable ones.
///
@@ -122,7 +194,24 @@ impl HostSeams {
})
.collect();
+ let storage = match self.storage {
+ Some(StorageSource::Backend(backend)) => {
+ let previous = openhuman_core::storage::install(Arc::clone(&backend));
+ log::debug!(
+ "[embed][seams] storage backend installed driver={} replaced={}",
+ backend.driver(),
+ previous.is_some()
+ );
+ Some((backend, previous))
+ }
+ Some(StorageSource::Url(_)) => {
+ return Err("storage url was not opened before install".to_string());
+ }
+ None => None,
+ };
+
Ok(InstalledSeams {
+ storage,
ranker,
post_turn_hooks,
tool_hooks,
@@ -135,6 +224,9 @@ impl HostSeams {
/// A ranker we installed, and what held the slot before it.
type InstalledRanker = (Arc<dyn ToolRanker>, Option<Arc<dyn ToolRanker>>);
+/// A storage backend we installed, and what held the slot before it.
+type InstalledStorage = (Arc<dyn StorageBackend>, Option<Arc<dyn StorageBackend>>);
+
/// A hook we installed under `name`, and what held that name before it.
struct InstalledHook<H: ?Sized> {
name: String,
@@ -145,6 +237,7 @@ struct InstalledHook<H: ?Sized> {
/// Seams a runtime installed. Dropping it restores the restorable ones; see
/// the module docs.
pub(crate) struct InstalledSeams {
+ storage: Option<InstalledStorage>,
ranker: Option<InstalledRanker>,
/// Our hooks, in install order, each with the same-named hook it replaced.
post_turn_hooks: Vec<InstalledHook<dyn PostTurnHook>>,
@@ -190,6 +283,23 @@ impl Drop for InstalledSeams {
if !self.restore {
return;
}
+ if let Some((ours, previous)) = self.storage.take() {
+ let still_ours = openhuman_core::storage::installed()
+ .is_some_and(|now| Arc::ptr_eq(&now, &ours));
+ if still_ours {
+ match previous {
+ Some(previous) => {
+ openhuman_core::storage::install(previous);
+ }
+ None => {
+ openhuman_core::storage::clear();
+ }
+ }
+ log::debug!("[embed][seams] storage backend restored");
+ } else {
+ log::debug!("[embed][seams] storage backend replaced since build; left alone");
+ }
+ }
if let Some((ours, previous)) = self.ranker.take() {
let still_ours = installed_tool_ranker().is_some_and(|now| Arc::ptr_eq(&now, &ours));
if still_ours {
@@ -279,6 +389,19 @@ impl RuntimeBuilder {
self
}
+ /// The storage backend the core's `storage` domain reads (approvals,
+ /// secrets, documents, per-agent scopes): a URL opened at build time, or a
+ /// backend the host opened. Installed before boot and, when the runtime
+ /// drops, the previous backend is restored (or cleared) if ours still holds
+ /// the slot.
+ ///
+ /// This is only the backend. Conversations move onto it with
+ /// [`session_store`](Self::session_store).
+ pub fn storage(mut self, source: impl Into<StorageSource>) -> Self {
+ self.seams.storage = Some(source.into());
+ self
+ }
+
/// Replace the live security policy the core installs at boot. Applied
/// after boot; a later autonomy-settings reload rebuilds it from config.
pub fn live_policy(mut self, policy: Arc<SecurityPolicy>) -> Self {
diff --git a/crates/openhuman-tinyhumans/src/runtime.rs b/crates/openhuman-tinyhumans/src/runtime.rs
index 7fbe569cc6..dde8e0b5fa 100644
--- a/crates/openhuman-tinyhumans/src/runtime.rs
+++ b/crates/openhuman-tinyhumans/src/runtime.rs
@@ -203,6 +203,10 @@ impl RuntimeBuilder {
/// See [`openhuman_embed::RuntimeBuilder::server_launcher`].
server_launcher(launcher: ServerLauncher)
);
+ forward!(
+ /// See [`openhuman_embed::RuntimeBuilder::storage`].
+ storage(source: openhuman_embed::seams::StorageSource)
+ );
forward!(
/// See [`openhuman_embed::RuntimeBuilder::live_policy`].
live_policy(policy: Arc<SecurityPolicy>)