Skip to content

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

feat(rpc): support storage-backed session store in saas mode

Steven Enamakela06f66340816
diff --git a/crates/openhuman-rpc/src/server/shims.rs b/crates/openhuman-rpc/src/server/shims.rs
index ab1907dda5..e6544db078 100644
--- a/crates/openhuman-rpc/src/server/shims.rs
+++ b/crates/openhuman-rpc/src/server/shims.rs
@@ -61,19 +61,35 @@ pub async fn run_server_headless(host: Option<&str>, port: Option<u16>) -> anyho
/// Runs a SaaS core: many users behind a trusted gateway, booted from the
/// operator's config file and refused unless its boot guard passes.
///
-/// The on-disk session store is installed before boot. It resolves the
-/// workspace of the context each call runs under, so every user agent keeps
-/// its sessions, transcripts and turn states in its own workspace.
+/// The session store is installed before boot. With no storage URL it is the
+/// on-disk store, which resolves the workspace of the context each call runs
+/// under, so every profile keeps its sessions, transcripts and turn states in
+/// its own workspace. With one (`OPENHUMAN_STORAGE_URL`, else the operator's
+/// `storage_url`) the backend is opened and installed — the profile registry
+/// and leases live there too — and its session store never recovers on open:
+/// recovery follows the profile lease instead.
+///
+/// On shutdown the leases of profiles nothing is using are released, so
+/// another node can take them at once.
pub async fn run_server_saas(
host: Option<&str>,
port: Option<u16>,
saas_config: &std::path::Path,
) -> anyhow::Result<()> {
let config = crate::core_host::core::runtime::SaasConfig::load(saas_config)?;
- crate::session_store::install();
+ let storage_url = config.resolved_storage_url();
+ log::info!(
+ "[rpc:saas] session store: {}",
+ if storage_url.is_some() { "storage backend" } else { "on-disk" }
+ );
+ crate::session_store::install_for_saas(storage_url).await?;
let runtime =
crate::core_host::core::runtime::saas::build(config, host.map(str::to_owned), port).await?;
- super::serve::serve(&runtime, None, None).await
+ let served = super::serve::serve(&runtime, None, None).await;
+ if let Some(profiles) = crate::core_host::profiles::host::host() {
+ profiles.release_idle_on_shutdown().await;
+ }
+ served
}
/// Like [`run_server`] but marks the instance as embedded.
diff --git a/crates/openhuman-rpc/src/session_store/mod.rs b/crates/openhuman-rpc/src/session_store/mod.rs
index b59a6ccabf..a2aaae2284 100644
--- a/crates/openhuman-rpc/src/session_store/mod.rs
+++ b/crates/openhuman-rpc/src/session_store/mod.rs
@@ -231,6 +231,31 @@ pub async fn install_for_url(url: Option<String>) -> anyhow::Result<()> {
/// When `url` cannot be parsed or opened.
pub async fn provider_for_url(
url: Option<String>,
+) -> anyhow::Result<Arc<dyn SessionStoreProvider>> {
+ provider_for_url_with(url, None).await
+}
+
+/// [`install_for_url`] for a SaaS core: a backend's session store never
+/// sweeps in-flight turns on open, whatever its driver. In SaaS mode recovery
+/// is lease-driven — a profile's turns are interrupted only when its lease is
+/// taken over from a holder that never released it — because another node
+/// may be running them. With no URL this is the classic on-disk store.
+///
+/// # Errors
+///
+/// When `url` cannot be parsed or opened.
+pub async fn install_for_saas(url: Option<String>) -> anyhow::Result<()> {
+ let provider = provider_for_url_with(url, Some(false)).await?;
+ log::debug!("[rpc:session_store] installing process-wide (SaaS, lease-driven recovery)");
+ crate::core_host::agent::session_store::install(provider);
+ Ok(())
+}
+
+/// [`provider_for_url`] with the recovery sweep made explicit: `None`
+/// recovers on open exactly when the driver is single-process.
+async fn provider_for_url_with(
+ url: Option<String>,
+ recover_on_open: Option<bool>,
) -> anyhow::Result<Arc<dyn SessionStoreProvider>> {
use anyhow::Context as _;
@@ -244,15 +269,16 @@ pub async fn provider_for_url(
let backend = crate::core_host::storage::open(&url)
.await
.context("opening the configured storage backend")?;
- let single_process = !crate::core_host::storage::driver_is_shared(backend.driver());
+ let recover = recover_on_open
+ .unwrap_or_else(|| !crate::core_host::storage::driver_is_shared(backend.driver()));
let provider = tinyagents_session::DriverSessionStores::new(Arc::clone(&backend))
.context("starting the session store bridge")?
- .recover_on_open(single_process);
+ .recover_on_open(recover);
// Only a fully working bridge makes the backend the process's storage.
crate::core_host::storage::install(backend);
log::info!(
"[rpc:session_store] opened the storage-backed session store \
- recover_on_open={single_process}"
+ recover_on_open={recover}"
);
Ok(Arc::new(provider))
}