Skip to content

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

feat(subagent_sessions): add document store for subagent sessions

Steven Enamakel8be1ab462407
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
index c0834d0010..e19299551a 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/mod.rs
@@ -1,5 +1,6 @@
mod ops;
mod store;
+mod store_documents;
mod types;
pub use ops::{
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
index b6533b5b8f..c90fdc6bf8 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
@@ -15,10 +15,16 @@ impl SubagentSessionStore {
}
pub fn load(&self) -> Result<Vec<DurableSubagentSession>, String> {
+ if let Some(docs) = super::store_documents::current().map_err(|err| format!("{err:#}"))? {
+ return docs.load().map_err(|err| format!("{err:#}"));
+ }
load_from_path(&self.path())
}
pub fn save(&self, sessions: &[DurableSubagentSession]) -> Result<(), String> {
+ if let Some(docs) = super::store_documents::current().map_err(|err| format!("{err:#}"))? {
+ return docs.save(sessions).map_err(|err| format!("{err:#}"));
+ }
save_to_path(&self.path(), sessions)
}
}
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
new file mode 100644
index 0000000000..8d95c4785d
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store_documents.rs
@@ -0,0 +1,71 @@
+//! Durable sub-agent sessions on the `tinystoragedrivers` document port.
+//!
+//! Used instead of `<workspace>/.openhuman/subagent_sessions.json` when the
+//! host configured a storage backend ([`crate::storage`]); `store.rs` picks it
+//! per call, so the sessions live under the acting agent's storage scope
+//! (`local` on a single-user host).
+//!
+//! # Layout
+//!
+//! | Collection | Document id | Holds |
+//! | --- | --- | --- |
+//! | `subagent_sessions` | `all` | `sessions`: the whole list the file held |
+//!
+//! The file store loads and saves the whole list, last writer wins; this keeps
+//! that contract so the callers in `ops.rs` are unchanged.
+
+use anyhow::Result;
+use serde_json::json;
+use tinystoragedrivers::{CollectionSpec, Precondition};
+
+use super::types::DurableSubagentSession;
+use crate::storage::documents::Repo;
+use crate::storage::DocumentStoreExt;
+
+const SESSIONS: &str = "subagent_sessions";
+const LIST_ID: &str = "all";
+const DOMAIN: &str = "subagent_sessions::store";
+
+fn collections() -> Vec<CollectionSpec> {
+ vec![CollectionSpec::new(SESSIONS)]
+}
+
+/// The document store for this call, when the host configured one.
+pub(super) fn current() -> Result<Option<Docs>> {
+ Ok(Repo::current(DOMAIN, collections)?.map(Docs))
+}
+
+/// The session list over one scoped document handle.
+pub(super) struct Docs(Repo);
+
+impl Docs {
+ #[cfg(test)]
+ pub(super) fn over(scoped: &crate::storage::ScopedStorage) -> Self {
+ Self(Repo::over(scoped, DOMAIN, collections))
+ }
+
+ pub(super) fn load(&self) -> Result<Vec<DurableSubagentSession>> {
+ let stored = self
+ .0
+ .run(|docs| async move { docs.get(SESSIONS, LIST_ID).await })?;
+ let Some(stored) = stored else {
+ return Ok(Vec::new());
+ };
+ let sessions = stored.doc.get("sessions").cloned().unwrap_or_default();
+ Ok(serde_json::from_value(sessions)?)
+ }
+
+ pub(super) fn save(&self, sessions: &[DurableSubagentSession]) -> Result<()> {
+ let doc = json!({ "sessions": sessions });
+ log::debug!("[subagent_sessions] document save count={}", sessions.len());
+ self.0.run(|docs| async move {
+ docs.put(SESSIONS, LIST_ID, doc, Precondition::None)
+ .await
+ .map(|_| ())
+ })
+ }
+}
+
+#[cfg(test)]
+#[path = "store_documents_tests.rs"]
+mod tests;