Skip to content

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

fix(subagent_sessions): keep owner scope on the session store

Steven Enamakela9fef2898fb0
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 b05dcff1f6..a0f9f2f9a7 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/store.rs
@@ -4,7 +4,35 @@ use super::types::{DurableSubagentSession, SubagentSessionStore};
impl SubagentSessionStore {
pub fn new(workspace_dir: PathBuf) -> Self {
- Self { workspace_dir }
+ Self {
+ workspace_dir,
+ context: None,
+ }
+ }
+
+ /// Opens the session store under the dispatch's resolved storage scope.
+ /// Keep the context on the handle because these synchronous compatibility
+ /// operations can run after an async task-local scope has been lost.
+ pub fn new_in_context(
+ ctx: &crate::core::runtime::DispatchCtx,
+ workspace_dir: PathBuf,
+ ) -> Result<Self, String> {
+ let context = ctx
+ .resolved()
+ .ok_or_else(|| "subagent session store has no resolved owner scope".to_owned())?;
+ Ok(Self {
+ workspace_dir,
+ context: Some(context),
+ })
+ }
+
+ fn in_scope<T>(&self, f: impl FnOnce() -> T) -> T {
+ match &self.context {
+ Some(context) => {
+ crate::core::runtime::CoreContext::sync_scope(Arc::clone(context), f)
+ }
+ None => f(),
+ }
}
/// The pre-documents `<workspace>/.openhuman/subagent_sessions.json`.
@@ -44,17 +72,21 @@ impl SubagentSessionStore {
}
pub fn load(&self) -> Result<Vec<DurableSubagentSession>, String> {
- let docs = super::store_documents::current(&self.workspace_dir)
- .map_err(|err| format!("{err:#}"))?;
- self.import_legacy(&docs)?;
- docs.load().map_err(|err| format!("{err:#}"))
+ self.in_scope(|| {
+ let docs = super::store_documents::current(&self.workspace_dir)
+ .map_err(|err| format!("{err:#}"))?;
+ self.import_legacy(&docs)?;
+ docs.load().map_err(|err| format!("{err:#}"))
+ })
}
pub fn save(&self, sessions: &[DurableSubagentSession]) -> Result<(), String> {
- let docs = super::store_documents::current(&self.workspace_dir)
- .map_err(|err| format!("{err:#}"))?;
- self.import_legacy(&docs)?;
- docs.save(sessions).map_err(|err| format!("{err:#}"))
+ self.in_scope(|| {
+ let docs = super::store_documents::current(&self.workspace_dir)
+ .map_err(|err| format!("{err:#}"))?;
+ self.import_legacy(&docs)?;
+ docs.save(sessions).map_err(|err| format!("{err:#}"))
+ })
}
pub fn update_session<F>(
@@ -65,11 +97,13 @@ impl SubagentSessionStore {
where
F: FnMut(&mut DurableSubagentSession, String) + Send + 'static,
{
- let docs = super::store_documents::current(&self.workspace_dir)
- .map_err(|err| format!("{err:#}"))?;
- self.import_legacy(&docs)?;
- docs.update_session(subagent_session_id, update)
- .map_err(|err| format!("{err:#}"))
+ self.in_scope(|| {
+ let docs = super::store_documents::current(&self.workspace_dir)
+ .map_err(|err| format!("{err:#}"))?;
+ self.import_legacy(&docs)?;
+ docs.update_session(subagent_session_id, update)
+ .map_err(|err| format!("{err:#}"))
+ })
}
pub fn update_session_if<F>(
@@ -80,10 +114,14 @@ impl SubagentSessionStore {
where
F: FnMut(&mut DurableSubagentSession, String) -> bool + Send + 'static,
{
- let docs = super::store_documents::current(&self.workspace_dir)
- .map_err(|err| format!("{err:#}"))?;
- self.import_legacy(&docs)?;
- docs.update_session_if(subagent_session_id, update)
- .map_err(|err| format!("{err:#}"))
+ self.in_scope(|| {
+ let docs = super::store_documents::current(&self.workspace_dir)
+ .map_err(|err| format!("{err:#}"))?;
+ self.import_legacy(&docs)?;
+ docs.update_session_if(subagent_session_id, update)
+ .map_err(|err| format!("{err:#}"))
+ })
}
}
+
+use std::sync::Arc;
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs
index fea3ce7610..f394770fa9 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/types.rs
@@ -158,6 +158,7 @@ pub struct SubagentSessionUpsert {
#[derive(Debug, Clone)]
pub struct SubagentSessionStore {
pub workspace_dir: PathBuf,
+ pub(crate) context: Option<std::sync::Arc<crate::core::runtime::CoreContext>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_host_impl.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_host_impl.rs
index 2fc0359758..6285dafb9c 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_host_impl.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_host_impl.rs
@@ -89,7 +89,14 @@ impl SpawnSubagentTool {
.or(run_context.thread_id.as_deref())
.map(str::to_owned);
- let store = SubagentSessionStore::new(parent.workspace_dir.clone());
+ let store = match SubagentSessionStore::new_in_context(&dispatch, parent.workspace_dir.clone()) {
+ Ok(store) => store,
+ Err(error) => {
+ return Ok(ToolResult::error(format!(
+ "spawn_subagent: failed to open the caller-scoped session store: {error}"
+ )));
+ }
+ };
let workspace_descriptor = tool_context
.and_then(|ctx| ctx.workspace().cloned())
.or_else(|| run_context.workspace.clone());