Skip to content

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

refactor(spawn_async_subagent): extract refusal and accepted-response helpers

Steven Enamakela13d937a7fa9
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
index 5790546df8..634469f87d 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent.rs
@@ -290,6 +290,7 @@ impl SpawnAsyncSubagentTool {
include!("spawn_async_subagent_execute.rs");
include!("spawn_async_subagent_args.rs");
+include!("spawn_async_subagent_results.rs");
/// Settle a late registration refusal before an accepted response is built.
fn finish_spawn_registration(
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
index 70d72604a6..ab83fcb334 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_execute.rs
@@ -116,23 +116,12 @@ impl SpawnAsyncSubagentTool {
// reaching this guard — see the `has_delivery_thread` checks in `spawn_subagent.rs` and
// `dispatch.rs::dispatch_subagent`. Only a *direct* `spawn_async_subagent` call lands
// here.
- if parent_thread_id.is_none() {
- log::warn!(
- "[spawn_async_subagent] refusing fire-and-forget spawn with no delivery thread \
- parent={} requested={} — directing caller to synchronous delegation (flow node / \
- CLI / cron context, background result would be discarded)",
- parent.agent_definition_id,
- definition.id
- );
- return Ok(ToolResult::error(
- "spawn_async_subagent: no parent chat thread available to deliver the result \
- into (this looks like a flow node, CLI, or cron run rather than an interactive \
- chat turn). Fire-and-forget delegation has nowhere to land its result here and \
- the sub-agent's work would be silently discarded. Use synchronous delegation \
- instead: a `delegate_*` tool with `blocking: true` runs the sub-agent inline \
- and hands you its output in this turn. For parallel work, model it as \
- parallel flow nodes rather than background sub-agents.",
- ));
+ if let Some(refusal) = refuse_missing_delivery_thread(
+ parent_thread_id.as_deref(),
+ &parent.agent_definition_id,
+ &definition.id,
+ ) {
+ return Ok(refusal);
}
let store = SubagentSessionStore::new(parent.workspace_dir.clone());
let workspace_descriptor = tool_context
@@ -771,37 +760,14 @@ impl SpawnAsyncSubagentTool {
return Ok(ToolResult::error(error));
}
- let payload = async_subagent_ref_payload(
+ Ok(accepted_spawn_result(
&task_id,
&durable_session.subagent_session_id,
&definition.id,
worker_thread_id.as_deref(),
reusable.is_some(),
reuse_decision.as_str(),
- "running",
- &fleet,
- );
- let payload_json = match serde_json::to_string(&payload) {
- Ok(serialized) => {
- log::debug!(
- "[spawn_async_subagent] serialized async reference payload bytes={}",
- serialized.len()
- );
- serialized
- }
- Err(error) => {
- log::debug!(
- "[spawn_async_subagent] failed to serialize async reference payload: {}",
- error
- );
- "{}".to_string()
- }
- };
- log::debug!("[spawn_async_subagent] formatting accepted response");
- Ok(ToolResult::success(format_async_subagent_accepted(
- payload["agent_id"].as_str().unwrap_or("subagent"),
- &payload_json,
&fleet,
- )))
+ ))
}
}
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_results.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_results.rs
new file mode 100644
index 0000000000..9a14b9aa7d
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_results.rs
@@ -0,0 +1,68 @@
+fn refuse_missing_delivery_thread(
+ parent_thread_id: Option<&str>,
+ parent_agent_id: &str,
+ requested_agent_id: &str,
+) -> Option<ToolResult> {
+ if parent_thread_id.is_none() {
+ log::warn!(
+ "[spawn_async_subagent] refusing fire-and-forget spawn with no delivery thread \
+ parent={} requested={} — directing caller to synchronous delegation (flow node / \
+ CLI / cron context, background result would be discarded)",
+ parent_agent_id,
+ requested_agent_id
+ );
+ return Some(ToolResult::error(
+ "spawn_async_subagent: no parent chat thread available to deliver the result \
+ into (this looks like a flow node, CLI, or cron run rather than an interactive \
+ chat turn). Fire-and-forget delegation has nowhere to land its result here and \
+ the sub-agent's work would be silently discarded. Use synchronous delegation \
+ instead: a `delegate_*` tool with `blocking: true` runs the sub-agent inline \
+ and hands you its output in this turn. For parallel work, model it as \
+ parallel flow nodes rather than background sub-agents.",
+ ));
+ }
+ None
+}
+
+fn accepted_spawn_result(
+ task_id: &str,
+ session_id: &str,
+ agent_id: &str,
+ worker_thread_id: Option<&str>,
+ reused: bool,
+ reuse_decision: &str,
+ fleet: &FleetToolSet,
+) -> ToolResult {
+ let payload = async_subagent_ref_payload(
+ task_id,
+ session_id,
+ agent_id,
+ worker_thread_id,
+ reused,
+ reuse_decision,
+ "running",
+ fleet,
+ );
+ let payload_json = match serde_json::to_string(&payload) {
+ Ok(serialized) => {
+ log::debug!(
+ "[spawn_async_subagent] serialized async reference payload bytes={}",
+ serialized.len()
+ );
+ serialized
+ }
+ Err(error) => {
+ log::debug!(
+ "[spawn_async_subagent] failed to serialize async reference payload: {}",
+ error
+ );
+ "{}".to_string()
+ }
+ };
+ log::debug!("[spawn_async_subagent] formatting accepted response");
+ ToolResult::success(format_async_subagent_accepted(
+ payload["agent_id"].as_str().unwrap_or("subagent"),
+ &payload_json,
+ fleet,
+ ))
+}
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
index a4bd6cdea5..dcdfb73496 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/spawn_async_subagent_tests.rs
@@ -1,6 +1,6 @@
use super::*;
use crate::agent::harness::definition::AgentDefinitionRegistry;
-use crate::agent::harness::fork_context::{with_parent_context, ParentExecutionContext};
+use crate::agent::harness::fork_context::{ParentExecutionContext, with_parent_context};
use crate::agent::prompts::ToolCallFormat;
use crate::config::AgentConfig;
use std::collections::HashSet;
@@ -107,7 +107,7 @@ async fn late_registration_refusal_aborts_the_worker_and_settles_its_durable_ses
assert_eq!(persisted[0].status, DurableSubagentStatus::Failed);
assert_eq!(persisted[0].latest_error.as_deref(), Some(error.as_str()));
assert!(
- running_subagents::task_records(&workspace.path().to_path_buf()).is_empty(),
+ running_subagents::task_records(Some("late-parent")).is_empty(),
"refused registration must not create a Running task ledger row"
);
});
@@ -298,9 +298,10 @@ fn async_reference_matches_the_orchestrator_fleet_vocabulary() {
.iter()
.filter_map(|v| v.as_str())
.collect();
- assert!(next
- .iter()
- .any(|a| a.contains("delivered to you automatically")));
+ assert!(
+ next.iter()
+ .any(|a| a.contains("delivered to you automatically"))
+ );
let message = format_async_subagent_accepted("task_manager_agent", &serialized, &fleet);
let prose = message.split("[async_subagent_ref]").next().unwrap();
@@ -604,8 +605,10 @@ fn scoped_instance_advertises_exactly_the_allowlist() {
schema["properties"]["agent_id"]["enum"],
serde_json::json!(["agent_memory", "workflow_builder"])
);
- assert!(schema["properties"]["agent_id"]["description"]
- .as_str()
- .unwrap_or_default()
- .contains("only these are dispatchable"));
+ assert!(
+ schema["properties"]["agent_id"]["description"]
+ .as_str()
+ .unwrap_or_default()
+ .contains("only these are dispatchable")
+ );
}