Skip to content

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

feat(session-store): install storage-backed session store when configured

Steven Enamakelbc6dc016b216
diff --git a/crates/openhuman-rpc/Cargo.toml b/crates/openhuman-rpc/Cargo.toml
index 0571b80609..c7bba86139 100644
--- a/crates/openhuman-rpc/Cargo.toml
+++ b/crates/openhuman-rpc/Cargo.toml
@@ -38,7 +38,7 @@ server = [
# `session_raw/` / `session_db/` / `tinyagents_store/` layout behind
# TinyAgents' session store port. On with `server`; the TUI, which runs the
# core without the server, turns it on by itself.
-session-store = ["dep:tinyagents-session", "dep:tinyagents-harness", "dep:chrono", "dep:anyhow"]
+session-store = ["dep:tinyagents-session", "dep:tinyagents-harness", "dep:chrono", "dep:anyhow", "tinyagents-session/storage-drivers"]
# Forwarded so the server's Sentry-routing tests run against a core that
# reports to Sentry.
crash-reporting = ["openhuman-core/crash-reporting"]
diff --git a/crates/openhuman-rpc/src/server/shims.rs b/crates/openhuman-rpc/src/server/shims.rs
index 466e677963..d7f7c3338f 100644
--- a/crates/openhuman-rpc/src/server/shims.rs
+++ b/crates/openhuman-rpc/src/server/shims.rs
@@ -152,9 +152,9 @@ async fn run_server_with_services(
}
// The desktop app and the CLI keep conversations in the classic on-disk
- // layout; the core itself carries no storage. Installed before boot so
- // its recovery sweep runs.
- crate::session_store::install();
+ // layout unless a storage URL is configured; the core itself carries no
+ // storage. Installed before boot so its recovery sweep runs.
+ crate::session_store::install_for_host().await?;
let runtime = builder.build().await?;
super::serve::serve(&runtime, ready_tx, shutdown_token).await
}
diff --git a/crates/openhuman-rpc/src/session_store/mod.rs b/crates/openhuman-rpc/src/session_store/mod.rs
index 9434cab661..7e951c5639 100644
--- a/crates/openhuman-rpc/src/session_store/mod.rs
+++ b/crates/openhuman-rpc/src/session_store/mod.rs
@@ -139,6 +139,61 @@ pub fn install() {
)));
}
+/// Installs the session store the host's configuration asks for, before the
+/// core boots.
+///
+/// With no storage URL (`OPENHUMAN_STORAGE_URL`, else `[storage] url`) this is
+/// [`install`]: the classic on-disk layout, unchanged. With one, the backend is
+/// opened, made the process's storage backend
+/// ([`openhuman_core::storage::install`]), and TinyAgents'
+/// `DriverSessionStores` is installed over it: every agent's transcripts, turn
+/// states, records and journal in that backend, one storage scope per agent.
+///
+/// A single-process backend (SQLite, memory, files) also interrupts an
+/// agent's in-flight turns the first time it is opened, since only an earlier
+/// process can have left them. MongoDB does not: several processes may share
+/// that database, and another one may own those turns.
+///
+/// # Errors
+///
+/// When a URL is configured but cannot be parsed or opened. A deployment that
+/// asked for a backend must not quietly fall back to local files.
+pub async fn install_for_host() -> anyhow::Result<()> {
+ use anyhow::Context as _;
+
+ let url = match std::env::var(openhuman_core::storage::STORAGE_URL_VAR) {
+ Ok(url) if !url.trim().is_empty() => Some(url.trim().to_string()),
+ _ => match openhuman_core::config::ops::loader::load_config_with_timeout().await {
+ Ok(config) => openhuman_core::storage::configured_url(&config),
+ Err(error) => {
+ tracing::warn!(
+ "[session_store] config unavailable ({error}); keeping the on-disk layout"
+ );
+ None
+ }
+ },
+ };
+ let Some(url) = url else {
+ install();
+ return Ok(());
+ };
+ let backend = openhuman_core::storage::open(&url)
+ .await
+ .context("opening the configured storage backend")?;
+ openhuman_core::storage::install(Arc::clone(&backend));
+ let single_process = backend.driver() != "mongodb";
+ let provider = tinyagents_session::DriverSessionStores::new(backend)
+ .context("starting the session store bridge")?
+ .recover_on_open(single_process);
+ tracing::info!(
+ target: "openhuman_rpc::session_store",
+ recover_on_open = single_process,
+ "[session_store] installed the storage-backed session store"
+ );
+ openhuman_core::agent::session_store::install(Arc::new(provider));
+ Ok(())
+}
+
#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;
diff --git a/crates/openhuman-tui/src/runner.rs b/crates/openhuman-tui/src/runner.rs
index a95d526224..ac178d7ec9 100644
--- a/crates/openhuman-tui/src/runner.rs
+++ b/crates/openhuman-tui/src/runner.rs
@@ -177,7 +177,7 @@ async fn async_main(
// In-process core: full domains (channel.web_chat needs DomainGroup::Channels,
// so harness() is not enough), no RPC transport, no background services.
// Conversations in the classic on-disk layout, as the desktop keeps them.
- openhuman_rpc::session_store::install();
+ openhuman_rpc::session_store::install_for_host().await?;
let runtime = Arc::new(
CoreBuilder::new(HostKind::detect_standalone())
.domains(DomainSet::full())