Skip to content

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

fix(orchestration): reject detached subagent spawns without a valid caller scope

Steven Enamakela5150102d964
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
index bc5db97e71..1597520bf3 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
@@ -71,7 +71,7 @@ pub(crate) use cancel::{
cancel_all, cancel_by_session_in_workspace, cancel_by_task, cancel_for_thread, stop_for_thread,
CancelledSubagent,
};
-pub(crate) use registry::{register, status_channel};
+pub(crate) use registry::{admit_spawn, register, status_channel, RegistrationError};
pub(crate) use resolve::{resume_ref_for_task_in_workspace, task_id_for_session_in_workspace};
pub(crate) use roster::active_subagents_context_block;
pub(crate) use steering::steer_control;
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
index 23834f4008..43e94c6eb8 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
@@ -65,6 +65,23 @@ fn caller_profile() -> Result<Option<String>, WaitError> {
Ok(profile)
}
+/// A refused detached spawn cannot be reported as accepted work.
+#[derive(Debug, thiserror::Error)]
+pub(crate) enum RegistrationError {
+ #[error("missing or mismatched tenant scope")]
+ Scope,
+ #[error("detached task registration failed: {0}")]
+ Registry(#[source] tinyagents_tasks::DetachedTaskRegistryError),
+}
+
+/// Check the actual caller before any reusable-session or worker-thread write.
+pub(crate) fn admit_spawn(
+ workspace: &std::path::Path,
+) -> Result<Option<String>, RegistrationError> {
+ admit_workspace(workspace).map_err(|_| RegistrationError::Scope)?;
+ caller_profile().map_err(|_| RegistrationError::Scope)
+}
+
/// Host ownership adapter over TinyAgents' opaque owner string.
pub(super) fn owner_key(parent_session: &str) -> Result<String, WaitError> {
let profile = caller_profile()?;
@@ -155,11 +172,14 @@ pub(crate) fn register(
run_queue: Arc<RunQueue<crate::agent::queued_turn::QueuedTurn>>,
abort: AbortHandle,
status: watch::Receiver<DetachedSubagentStatus>,
-) {
- let Ok(profile) = caller_profile() else {
- abort.abort();
- log::warn!("[running_subagents] refusing registration without a SaaS profile");
- return;
+) -> Result<(), RegistrationError> {
+ let profile = match admit_spawn(&workspace_dir) {
+ Ok(profile) => profile,
+ Err(error) => {
+ abort.abort();
+ log::warn!("[running_subagents] refusing registration without a SaaS profile");
+ return Err(error);
+ }
};
let owner = crate::core::runtime::tenant::tenant_key(
&crate::core::runtime::tenant::Tenant {
@@ -189,31 +209,13 @@ pub(crate) fn register(
crate::agent::orchestration::background_delivery::recover_on_boot(&workspace_dir);
}
- // Typed lifecycle ledger: record the spawn and mirror the child's terminal
- // status into the store via a lightweight watcher (issue #4249). Done before
- // the entry is moved into the map so the metadata is still in scope.
- record_spawned(
- &task_id,
- &agent_id,
- &parent_session,
- session_parent_prefix.as_deref(),
- subagent_session_id.as_deref(),
- &workspace_dir,
- parent_thread_id.as_deref(),
- );
- spawn_status_watcher(
- task_store_for_workspace(&workspace_dir),
- task_id.clone(),
- status.clone(),
- );
-
let metadata = RunningSubagentMetadata {
profile,
- parent_session,
- agent_id,
- subagent_session_id,
- workspace_dir,
- parent_thread_id,
+ parent_session: parent_session.clone(),
+ agent_id: agent_id.clone(),
+ subagent_session_id: subagent_session_id.clone(),
+ workspace_dir: workspace_dir.clone(),
+ parent_thread_id: parent_thread_id.clone(),
run_queue,
};
registry()
@@ -221,14 +223,32 @@ pub(crate) fn register(
TaskId::new(task_id.clone()),
owner,
metadata,
- status,
+ status.clone(),
// Cooperative cancellation is flipped before the registry invokes
// the hard abort. The child executor can adopt this token without
// changing the registry/control API.
CancellationToken::new(),
- abort,
+ abort.clone(),
)
- .expect("duplicate detached sub-agent task id");
+ .map_err(|error| {
+ abort.abort();
+ RegistrationError::Registry(error)
+ })?;
+ // Failed admission must not leave a Running lifecycle record or watcher.
+ record_spawned(
+ &task_id,
+ &agent_id,
+ &parent_session,
+ session_parent_prefix.as_deref(),
+ subagent_session_id.as_deref(),
+ &workspace_dir,
+ parent_thread_id.as_deref(),
+ );
+ spawn_status_watcher(
+ task_store_for_workspace(&workspace_dir),
+ task_id.clone(),
+ status,
+ );
log::debug!(
"[running_subagents] registered task_id={} live_entries={}",
task_id,
@@ -236,4 +256,5 @@ pub(crate) fn register(
.len()
.expect("detached task registry lock poisoned")
);
+ Ok(())
}
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs
index ef70c92b0d..de96a77e9c 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents_tenant_tests.rs
@@ -70,7 +70,8 @@ impl Worker {
queue.clone(),
abort.clone(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
Self {
task,
session,
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
index 0f17fbfb69..a0743eef20 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents_tests.rs
@@ -99,7 +99,8 @@ fn register_test_with_thread(
rq,
dummy_abort(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
tx
}
@@ -171,7 +172,8 @@ async fn task_id_for_session_enforces_parent_ownership() {
rq,
dummy_abort(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
assert_eq!(
task_id_for_session("subsess-1", "session-owner").unwrap(),
@@ -203,7 +205,8 @@ async fn snapshot_and_block_scope_to_parent_and_reflect_live_status() {
run_queue(),
dummy_abort(),
rx_a,
- );
+ )
+ .expect("fixture registration admitted");
let (tx_b, rx_b) = status_channel();
register(
"task-fleet-b".into(),
@@ -216,7 +219,8 @@ async fn snapshot_and_block_scope_to_parent_and_reflect_live_status() {
run_queue(),
dummy_abort(),
rx_b,
- );
+ )
+ .expect("fixture registration admitted");
// A worker owned by a different parent must not leak into the snapshot.
let (tx_other, rx_other) = status_channel();
register(
@@ -230,7 +234,8 @@ async fn snapshot_and_block_scope_to_parent_and_reflect_live_status() {
run_queue(),
dummy_abort(),
rx_other,
- );
+ )
+ .expect("fixture registration admitted");
// `b` pauses awaiting the user; `a` stays running.
tx_b.send(DetachedSubagentStatus::AwaitingUser {
@@ -372,7 +377,8 @@ async fn resume_ref_for_task_includes_resume_fields_and_enforces_ownership() {
run_queue(),
dummy_abort(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
let reference = resume_ref_for_task("task-resume", "session-owner").expect("resume reference");
assert_eq!(reference.task_id, "task-resume");
@@ -408,7 +414,8 @@ async fn task_id_for_session_prefers_live_task_over_terminal_task() {
run_queue(),
dummy_abort(),
old_rx,
- );
+ )
+ .expect("fixture registration admitted");
let _ = old_tx.send(DetachedSubagentStatus::Completed {
output: "old".into(),
iterations: 1,
@@ -425,7 +432,8 @@ async fn task_id_for_session_prefers_live_task_over_terminal_task() {
run_queue(),
dummy_abort(),
new_rx,
- );
+ )
+ .expect("fixture registration admitted");
assert_eq!(
task_id_for_session("subsess-live", "session-owner").unwrap(),
@@ -654,7 +662,8 @@ async fn stop_for_thread_aborts_the_threads_running_children() {
rq.clone(),
child.abort_handle(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
// Another thread's child must survive the stop.
let _other =
register_test_with_thread("task-stop-other", "session-stop", Some("thread-keep"), rq);
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents_wire_tests.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents_wire_tests.rs
index dfe023aa08..db3db1e3b8 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents_wire_tests.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents_wire_tests.rs
@@ -43,7 +43,8 @@ async fn roster_status_labels_are_literal() {
super::run_queue(),
super::dummy_abort(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
senders.push((id, tx));
}
for (id, tx) in &senders {
@@ -171,7 +172,8 @@ async fn spawn_ledger_record_has_literal_persisted_shape() {
super::run_queue(),
super::dummy_abort(),
rx,
- );
+ )
+ .expect("fixture registration admitted");
let text = std::fs::read_to_string(ws.path().join(".openhuman/orchestration_tasks.jsonl"))
.expect("ledger file at the product path");
let first: serde_json::Value =
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 2b68730791..5790546df8 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
@@ -291,6 +291,23 @@ impl SpawnAsyncSubagentTool {
include!("spawn_async_subagent_execute.rs");
include!("spawn_async_subagent_args.rs");
+/// Settle a late registration refusal before an accepted response is built.
+fn finish_spawn_registration(
+ registration: Result<(), running_subagents::RegistrationError>,
+ store: &SubagentSessionStore,
+ session_id: &str,
+ task_id: &str,
+) -> Result<(), String> {
+ if let Err(error) = registration {
+ let refusal = format!("spawn_async_subagent: registration refused: {error}");
+ subagent_sessions::mark_failed(store, session_id, task_id, refusal.clone()).map_err(
+ |failure| format!("{refusal}; failed to settle persisted session: {failure}"),
+ )?;
+ return Err(refusal);
+ }
+ Ok(())
+}
+
/// Format the user-facing acceptance text around a structured async sub-agent reference.
///
/// The wording follows what the parent can actually do: a parent without
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 aa42843e44..70d72604a6 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
@@ -46,6 +46,14 @@ impl SpawnAsyncSubagentTool {
}
};
+ // Preserve the actual caller's authority before any filesystem/session
+ // effects. A scoped refusal cannot become an accepted/running reference.
+ if let Err(error) = running_subagents::admit_spawn(&parent.workspace_dir) {
+ return Ok(ToolResult::error(format!(
+ "spawn_async_subagent: registration refused: {error}"
+ )));
+ }
+
let registry = match AgentDefinitionRegistry::current() {
Some(registry) => registry,
None => {
@@ -742,7 +750,7 @@ impl SpawnAsyncSubagentTool {
// Register *after* spawn so the AbortHandle is available. The task owns
// `status_tx`; this side holds `status_rx` for `wait_subagent`.
- running_subagents::register(
+ let registration = running_subagents::register(
task_id.clone(),
definition.id.clone(),
parent_session.clone(),
@@ -754,6 +762,14 @@ impl SpawnAsyncSubagentTool {
join.abort_handle(),
status_rx,
);
+ if let Err(error) = finish_spawn_registration(
+ registration,
+ &store,
+ &durable_session.subagent_session_id,
+ &task_id,
+ ) {
+ return Ok(ToolResult::error(error));
+ }
let payload = async_subagent_ref_payload(
&task_id,
diff --git a/crates/openhuman-core/src/core/runtime/saas.rs b/crates/openhuman-core/src/core/runtime/saas.rs
index 8186c7aac5..f4f7d5c125 100644
--- a/crates/openhuman-core/src/core/runtime/saas.rs
+++ b/crates/openhuman-core/src/core/runtime/saas.rs
@@ -384,6 +384,7 @@ impl DomainSet {
pub fn saas() -> Self {
Self {
operator: true,
+ agent: true,
threads: true,
channels: true,
memory: true,
diff --git a/crates/openhuman-core/src/profiles/host.rs b/crates/openhuman-core/src/profiles/host.rs
index e2bfcfef66..7f3da6df85 100644
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@ -116,6 +116,7 @@ impl std::fmt::Debug for ProfileHost {
/// methods on [`USER_METHODS`](super::surface::USER_METHODS) are reachable.
pub fn user_domains() -> DomainSet {
DomainSet {
+ agent: true,
threads: true,
channels: true,
memory: true,
diff --git a/crates/openhuman-core/src/profiles/surface.rs b/crates/openhuman-core/src/profiles/surface.rs
index 3f0e1056aa..ea49ec3940 100644
--- a/crates/openhuman-core/src/profiles/surface.rs
+++ b/crates/openhuman-core/src/profiles/surface.rs
@@ -24,6 +24,9 @@ use crate::core::runtime::is_saas;
/// completions are keyed per profile (`tenant::profile_key`) and web-channel
/// sessions per tenant.
pub const USER_METHODS: &[&str] = &[
+ // Detached-worker controls compare the original worker's captured profile.
+ "openhuman.subagent_cancel",
+ "openhuman.subagent_steer",
// Conversation threads: all state lives under the agent's workspace.
"openhuman.threads_list",
"openhuman.threads_upsert",