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 disk

Steven Enamakel83b3651d0661
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index a6185ac152..7770b587a7 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -20,6 +20,21 @@ pub use tinyagents_session::port::{AgentStores, SessionStoreProvider};
static PROVIDER: LazyLock<RwLock<Option<Arc<dyn SessionStoreProvider>>>> =
LazyLock::new(|| RwLock::new(None));
+tokio::task_local! {
+ /// A provider for one task tree only, ahead of the process-wide one; how
+ /// tests in a shared binary use a store without touching each other.
+ static SCOPED: Arc<dyn SessionStoreProvider>;
+}
+
+/// Runs `future` with `provider` as the session store, for that task only.
+/// Tasks it spawns do not inherit it.
+pub async fn scope<F: std::future::Future>(
+ provider: Arc<dyn SessionStoreProvider>,
+ future: F,
+) -> F::Output {
+ SCOPED.scope(provider, future).await
+}
+
/// Routes every agent's session state through `provider`, replacing any
/// earlier one.
pub fn install(provider: Arc<dyn SessionStoreProvider>) {
@@ -44,22 +59,22 @@ pub fn clear() -> bool {
had
}
-/// The installed provider, if any.
+/// The provider in effect: the task's [`scope`]d one, else the installed
+/// one, if any.
#[must_use]
pub fn installed() -> Option<Arc<dyn SessionStoreProvider>> {
- PROVIDER
- .read()
- .unwrap_or_else(PoisonError::into_inner)
- .clone()
+ SCOPED.try_with(Arc::clone).ok().or_else(|| {
+ PROVIDER
+ .read()
+ .unwrap_or_else(PoisonError::into_inner)
+ .clone()
+ })
}
-/// Whether a host session store is installed.
+/// Whether a host session store is in effect.
#[must_use]
pub fn is_installed() -> bool {
- PROVIDER
- .read()
- .unwrap_or_else(PoisonError::into_inner)
- .is_some()
+ installed().is_some()
}
/// `agent_id`'s stores from the installed provider, or `None` when the core
diff --git a/crates/openhuman-core/src/agent/session_store/mod_tests.rs b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
new file mode 100644
index 0000000000..f459804a68
--- /dev/null
+++ b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
@@ -0,0 +1,33 @@
+use super::*;
+use tinyagents_session::port::InMemorySessionStores;
+
+#[tokio::test]
+async fn a_scoped_provider_serves_only_its_own_task() {
+ let provider: Arc<dyn SessionStoreProvider> = Arc::new(InMemorySessionStores::new());
+ let inside = scope(provider.clone(), async {
+ assert!(is_installed());
+ for_agent("u1").map(|stores| stores.transcripts.destination_key())
+ })
+ .await;
+ assert_eq!(
+ inside,
+ Some(provider.for_agent("u1").transcripts.destination_key())
+ );
+}
+
+/// The process-wide slot, exercised by one test so no other test in this
+/// binary sees it set.
+#[test]
+fn installing_routes_every_agent_until_cleared() {
+ assert!(!clear(), "nothing installed yet");
+ let provider: Arc<dyn SessionStoreProvider> = Arc::new(InMemorySessionStores::new());
+ install(provider.clone());
+ assert!(is_installed());
+ let stores = for_agent(DEFAULT_AGENT).expect("installed");
+ assert_eq!(
+ stores.transcripts.destination_key(),
+ provider.for_agent(DEFAULT_AGENT).transcripts.destination_key()
+ );
+ assert!(clear());
+ assert!(for_agent("u1").is_none());
+}