Skip to content

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

refactor(orchestration): extract reusable driver session lookup for spawn

Steven Enamakel5b8713b7f72b
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
index 5c9929ca64..4c1e52eb7c 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -455,18 +455,18 @@ pub(crate) fn stop_for_thread(
continue;
};
let scope = ParentJobScope::new(owner_scope.workspace.clone(), parent_thread_id);
- let matching_status = jobs
- .list_for_parent_scope(&owner, &scope)
- .ok()
- .and_then(|jobs| {
- jobs.into_iter().find_map(|job| {
- (job.extensions
- .get("task_id")
- .and_then(serde_json::Value::as_str)
- == Some(task_id.as_str()))
- .then_some(job.status)
- })
- });
+ let matching_status =
+ jobs.list_for_parent_scope(&owner, &scope)
+ .ok()
+ .and_then(|jobs| {
+ jobs.into_iter().find_map(|job| {
+ (job.extensions
+ .get("task_id")
+ .and_then(serde_json::Value::as_str)
+ == Some(task_id.as_str()))
+ .then_some(job.status)
+ })
+ });
if matching_status.is_some_and(|status| {
matches!(
status,
@@ -475,9 +475,10 @@ pub(crate) fn stop_for_thread(
| SubAgentJobStatus::AwaitingInput
)
}) {
- task_ids.push(task_id);
- if let Some((_, paused_tasks)) =
- stopped_scopes.iter_mut().find(|(stopped, _)| stopped == &scope)
+ task_ids.push(task_id.clone());
+ if let Some((_, paused_tasks)) = stopped_scopes
+ .iter_mut()
+ .find(|(stopped, _)| stopped == &scope)
{
if matching_status == Some(SubAgentJobStatus::AwaitingInput) {
paused_tasks.push(task_id);
@@ -496,7 +497,7 @@ pub(crate) fn stop_for_thread(
}
for (scope, paused_task_ids) in stopped_scopes {
match jobs.cancel_for_thread(&scope) {
- Ok(()) => {
+ Ok(_) => {
for task_id in paused_task_ids {
crate::agent::orchestration::completion_owners::finish_driver_task(
ctx, &task_id,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_driver_lookup.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_driver_lookup.rs
index 08a5e2e46c..1aa7e2b201 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_driver_lookup.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_subagent_driver_lookup.rs
@@ -37,6 +37,57 @@ async fn find_reusable_driver_session(
)))
}
+async fn reusable_driver_session_for_spawn(
+ driver: &std::sync::Arc<dyn tinyagents_orchestration::subagent::SubagentSessionStore>,
+ host_store: &SubagentSessionStore,
+ ctx: &DispatchCtx,
+ workspace_dir: &std::path::Path,
+ selector: &SubagentSessionSelector,
+ force_fresh: bool,
+) -> Result<Option<DurableSubagentSession>, String> {
+ let lookup = find_reusable_driver_session(driver, ctx, workspace_dir, selector).await;
+ if force_fresh {
+ match lookup {
+ Ok(Some(session)) => {
+ close_reusable_driver_session(
+ driver,
+ host_store,
+ ctx,
+ workspace_dir,
+ &session,
+ &selector.parent_session,
+ selector.parent_thread_id.as_deref(),
+ )
+ .await
+ ?;
+ }
+ Ok(None) => {}
+ Err(error) => log::warn!(
+ "[subagent_reuse] fresh lookup failed parent_thread_id={} agent_id={} task_key={} error={}",
+ selector.parent_thread_id.as_deref().unwrap_or("none"),
+ selector.agent_id,
+ selector.task_key,
+ error
+ ),
+ }
+ Ok(None)
+ } else {
+ match lookup {
+ Ok(session) => Ok(session),
+ Err(error) => {
+ log::warn!(
+ "[subagent_reuse] lookup failed parent_thread_id={} agent_id={} task_key={} error={}",
+ selector.parent_thread_id.as_deref().unwrap_or("none"),
+ selector.agent_id,
+ selector.task_key,
+ error
+ );
+ Ok(None)
+ }
+ }
+ }
+}
+
async fn upsert_driver_session(
driver: &std::sync::Arc<dyn tinyagents_orchestration::subagent::SubagentSessionStore>,
ctx: &DispatchCtx,
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 a1193e9064..6dcc2035fe 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
@@ -158,63 +158,18 @@ impl SpawnSubagentTool {
task_key: task_key.clone(),
};
- let reusable = if force_fresh {
- match find_reusable_driver_session(
- &driver_store,
- &dispatch,
- &parent.workspace_dir,
- &selector,
- )
- .await
- {
- Ok(Some(session)) => {
- if let Err(error) = close_reusable_driver_session(
- &driver_store,
- &store,
- &dispatch,
- &parent.workspace_dir,
- &session,
- &parent_session,
- parent_thread_id.as_deref(),
- )
- .await
- {
- return Ok(ToolResult::error(format!("spawn_subagent: {error}")));
- }
- }
- Ok(None) => {}
- Err(err) => {
- log::warn!(
- "[subagent_reuse] fresh lookup failed parent_thread_id={} agent_id={} task_key={} error={}",
- parent_thread_id.as_deref().unwrap_or("none"),
- definition.id,
- task_key,
- err
- );
- }
- }
- None
- } else {
- match find_reusable_driver_session(
- &driver_store,
- &dispatch,
- &parent.workspace_dir,
- &selector,
- )
- .await
- {
- Ok(session) => session,
- Err(err) => {
- log::warn!(
- "[subagent_reuse] lookup failed parent_thread_id={} agent_id={} task_key={} error={}",
- parent_thread_id.as_deref().unwrap_or("none"),
- definition.id,
- task_key,
- err
- );
- None
- }
- }
+ let reusable = match reusable_driver_session_for_spawn(
+ &driver_store,
+ &store,
+ &dispatch,
+ &parent.workspace_dir,
+ &selector,
+ force_fresh,
+ )
+ .await
+ {
+ Ok(reusable) => reusable,
+ Err(error) => return Ok(ToolResult::error(format!("spawn_subagent: {error}"))),
};
let reuse_decision = subagent_sessions::reuse_decision(reusable.as_ref(), force_fresh);
let follow_up_prompt = reusable_follow_up_message(&prompt, context.as_deref());
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_paused_cancel_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_paused_cancel_tests.rs
index 7eddf6c0ee..de8bb0a52e 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_paused_cancel_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_paused_cancel_tests.rs
@@ -300,8 +300,8 @@ async fn stopping_a_paused_driver_releases_its_active_owner() {
parent_session,
thread_id,
);
- let jobs = (*crate::core::runtime::slot_for::<SubAgentJobRegistry>(&live.data.dispatch_ctx()))
- .clone();
+ let jobs =
+ (*crate::core::runtime::slot_for::<SubAgentJobRegistry>(&live.data.dispatch_ctx())).clone();
let result = SpawnSubagentTool::new()
.execute_with_live_parent_context(
json!({
@@ -343,7 +343,11 @@ async fn stopping_a_paused_driver_releases_its_active_owner() {
assert_eq!(
jobs.list()
.into_iter()
- .find(|job| job.extensions.get("task_id").and_then(serde_json::Value::as_str) == Some(task_id.as_str()))
+ .find(|job| job
+ .extensions
+ .get("task_id")
+ .and_then(serde_json::Value::as_str)
+ == Some(task_id.as_str()))
.map(|job| job.status),
Some(SubAgentJobStatus::Cancelled)
);
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs
index 53e5e160a4..a45372c5d1 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_tests.rs
@@ -616,10 +616,10 @@ impl ChatModel<()> for LoopingToolModel {
mod driver_reuse_tests;
#[path = "tools_e2e_driver_tests.rs"]
mod driver_tests;
-#[path = "tools_e2e_thread_delete_tests.rs"]
-mod thread_delete_tests;
#[path = "tools_e2e_profile_cancel_tests.rs"]
mod profile_cancel_tests;
+#[path = "tools_e2e_thread_delete_tests.rs"]
+mod thread_delete_tests;
#[path = "tools_e2e_continuation_tests.rs"]
mod continuation_tests;
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_delete_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_delete_tests.rs
index 5c317a2398..fb1f52dfc4 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_delete_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_delete_tests.rs
@@ -399,4 +399,3 @@ async fn thread_delete_keeps_completions_when_driver_task_scope_cannot_be_resolv
"stale-driver-task",
);
}
-