Skip to content

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

feat(session_store): persist agent sessions to workspace storage

Steven Enamakeleae4b2e17c8d
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index fea1131feb..179b9f7e5a 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -26,7 +26,7 @@ mod workspace;
pub use rebound::{rebind_all, ReboundSessionStores};
pub use workspace::{
current_documents_for, ledger_for, provider_for_workspace, release_workspace_defaults,
- require_ledger, status_store_for, stores_for, stores_or_ephemeral, with_ledger, with_ledger_async,
+ require_ledger, status_store_for, stores_for, stores_or_ephemeral, with_ledger, with_ledger_async, WorkspaceSessionStores,
};
static PROVIDER: LazyLock<RwLock<Option<Arc<dyn SessionStoreProvider>>>> =
diff --git a/crates/openhuman-core/src/agent/session_store/workspace.rs b/crates/openhuman-core/src/agent/session_store/workspace.rs
index 06d3e3c0f7..09e4228068 100644
--- a/crates/openhuman-core/src/agent/session_store/workspace.rs
+++ b/crates/openhuman-core/src/agent/session_store/workspace.rs
@@ -54,7 +54,7 @@ pub fn provider_for_workspace(workspace_dir: &Path) -> Arc<dyn SessionStoreProvi
}
/// The provider over `workspace_dir`'s default backend, installed or not.
-fn fallback_provider(workspace_dir: &Path) -> Arc<dyn SessionStoreProvider> {
+pub(super) fn fallback_provider(workspace_dir: &Path) -> Arc<dyn SessionStoreProvider> {
let backend = match crate::storage::local::backend_for(workspace_dir) {
Ok(backend) => backend,
Err(error) => {
@@ -253,3 +253,57 @@ where
let ledger = require_ledger(workspace_dir)?;
op(ledger).await
}
+
+/// A session store that resolves the workspace of the calling context on every
+/// use and keeps that workspace's sessions in its own default backend
+/// ([`crate::storage::local::backend_for`]).
+///
+/// For the one host with no single backend to install: a SaaS core with no
+/// storage URL, where every profile has a workspace of its own and so a
+/// database of its own. Every other host installs
+/// [`ReboundSessionStores`](super::ReboundSessionStores) over its backend.
+#[derive(Debug, Default, Clone, Copy)]
+pub struct WorkspaceSessionStores;
+
+impl WorkspaceSessionStores {
+ fn current() -> Arc<dyn SessionStoreProvider> {
+ fallback_provider(&super::context_workspace_dir())
+ }
+}
+
+impl SessionStoreProvider for WorkspaceSessionStores {
+ fn for_agent(&self, agent_id: &str) -> AgentStores {
+ Self::current().for_agent(agent_id)
+ }
+
+ fn try_for_agent(
+ &self,
+ agent_id: &str,
+ ) -> Result<AgentStores, tinystoragedrivers::StorageError> {
+ Self::current().try_for_agent(agent_id)
+ }
+
+ fn run_ledger_for_agent(
+ &self,
+ agent_id: &str,
+ ) -> LedgerResult<Option<Arc<dyn RunLedgerStore>>> {
+ Self::current().run_ledger_for_agent(agent_id)
+ }
+
+ fn conversations_for_agent(
+ &self,
+ agent_id: &str,
+ ) -> tinyagents_session::threads::ConversationResult<
+ Option<Arc<dyn tinyagents_session::threads::ConversationPersistence>>,
+ > {
+ Self::current().conversations_for_agent(agent_id)
+ }
+
+ fn recover(&self) -> anyhow::Result<()> {
+ Self::current().recover()
+ }
+
+ fn destination_key(&self) -> Option<String> {
+ Self::current().destination_key()
+ }
+}
diff --git a/crates/openhuman-core/src/storage/local.rs b/crates/openhuman-core/src/storage/local.rs
index e7c81f05e8..128568ab93 100644
--- a/crates/openhuman-core/src/storage/local.rs
+++ b/crates/openhuman-core/src/storage/local.rs
@@ -9,9 +9,8 @@
//! process. A different workspace is a different database, which is also what
//! isolates tests that use a temp directory each.
//!
-//! A SaaS process never gets this fallback: it has no workspace-local state,
-//! and its hosts refuse to start without an explicit backend, so an
-//! uninstalled call there is an error, not a bucket other tenants could share.
+//! A SaaS core with no storage URL lands here too: each profile's workspace is
+//! its own database, so nothing is shared between tenants.
//!
//! The fallback imports nothing. The legacy per-domain files are read by
//! [`super::import`] when a backend is installed at boot (and again when the
@@ -63,11 +62,6 @@ pub fn backend_for(workspace_dir: &Path) -> Result<Arc<dyn StorageBackend>> {
if let Some(backend) = installed() {
return Ok(backend);
}
- if crate::core::runtime::mode::is_saas() {
- return Err(anyhow!(
- "SaaS mode needs an explicit storage backend (OPENHUMAN_STORAGE_URL)"
- ));
- }
let mut opened = WORKSPACES.lock().unwrap_or_else(PoisonError::into_inner);
if let Some(backend) = opened.get(workspace_dir) {
return Ok(Arc::clone(backend));