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 Enamakel353caaf92a1a
diff --git a/crates/openhuman-core/src/agent/session_store/mod.rs b/crates/openhuman-core/src/agent/session_store/mod.rs
index 7770b587a7..14b2f118f5 100644
--- a/crates/openhuman-core/src/agent/session_store/mod.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod.rs
@@ -84,6 +84,25 @@ pub fn for_agent(agent_id: &str) -> Option<AgentStores> {
installed().map(|provider| provider.for_agent(agent_id))
}
+/// The stores of the agent the current [`CoreContext`] works for — the one
+/// it was derived for ([`CoreContext::session_agent`]), else
+/// [`DEFAULT_AGENT`] — or `None` when the core keeps the on-disk layout.
+///
+/// For code that has a workspace path but no agent id of its own (goals,
+/// todos, the turn journal): under an embedded agent's context it lands in
+/// that agent's stores.
+///
+/// [`CoreContext`]: crate::core::runtime::CoreContext
+/// [`CoreContext::session_agent`]: crate::core::runtime::CoreContext::session_agent
+#[must_use]
+pub fn current() -> Option<AgentStores> {
+ let provider = installed()?;
+ let agent = crate::core::runtime::CoreContext::current()
+ .and_then(|context| context.session_agent().map(str::to_owned))
+ .unwrap_or_else(|| DEFAULT_AGENT.to_string());
+ Some(provider.for_agent(&agent))
+}
+
/// The agent whose stores a turn without a definition id uses. Root turns of
/// the desktop app have no per-user agent; a single shared bucket keeps them
/// together, as the shared workspace always did.
diff --git a/crates/openhuman-core/src/agent/session_store/mod_tests.rs b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
index 3991bde3a8..71a55d125a 100644
--- a/crates/openhuman-core/src/agent/session_store/mod_tests.rs
+++ b/crates/openhuman-core/src/agent/session_store/mod_tests.rs
@@ -14,3 +14,18 @@ async fn a_scoped_provider_serves_only_its_own_task() {
Some(provider.for_agent("u1").transcripts.destination_key())
);
}
+
+#[tokio::test]
+async fn work_outside_an_agent_context_uses_the_default_agent() {
+ let provider: Arc<dyn SessionStoreProvider> = Arc::new(InMemorySessionStores::new());
+ let key = scope(provider.clone(), async {
+ current().map(|stores| stores.transcripts.destination_key())
+ })
+ .await;
+ // No agent context in a unit test: the shared default bucket.
+ assert_eq!(
+ key,
+ Some(provider.for_agent(DEFAULT_AGENT).transcripts.destination_key())
+ );
+ assert!(current().is_none() || is_installed());
+}