Skip to content

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

fix(orchestration): correct subagent cancellation and wait handling

Steven Enamakel502ed06b8055
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 8044e94117..9e1c0997ff 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -36,10 +36,10 @@ pub(crate) struct CancelledSubagent {
///
/// User controls check the captured profile in SaaS. The desktop user retains
/// authority over every sub-agent in their own core.
-pub(crate) fn cancel_by_task(task_id: &str) -> Option<CancelledSubagent> {
+pub(crate) fn cancel_by_task(ctx: &crate::core::runtime::DispatchCtx, task_id: &str) -> Option<CancelledSubagent> {
let id = TaskId::new(task_id);
let snapshot = registry().snapshot_trusted(&id).ok()?;
- if !control_admits(&snapshot.metadata) {
+ if !control_admits(ctx, &snapshot.metadata) {
return None;
}
let cancelled = registry().cancel(&id, &snapshot.owner_id).ok()?;
@@ -69,25 +69,26 @@ pub(crate) fn cancel_by_task(task_id: &str) -> Option<CancelledSubagent> {
}
pub(crate) fn cancel_by_session_in_workspace(
+ ctx: &crate::core::runtime::DispatchCtx,
subagent_session_id: &str,
parent_session: &str,
workspace_dir: &Path,
) -> Option<CancelledSubagent> {
let task_id =
- task_id_for_session_in_workspace(subagent_session_id, parent_session, workspace_dir)
+ task_id_for_session_in_workspace(ctx, subagent_session_id, parent_session, workspace_dir)
.ok()?;
- cancel_by_task(&task_id)
+ cancel_by_task(ctx, &task_id)
}
/// Abort and drop every running sub-agent whose parent chat thread is
/// `thread_id`. Called when that thread is deleted so detached children don't
/// keep running (and later try to deliver) against a thread that no longer
/// exists. Returns the number of sub-agents cancelled.
-pub(crate) fn cancel_for_thread(thread_id: &str) -> usize {
- let own = caller_workspace();
+pub(crate) fn cancel_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> usize {
+ let own = caller_workspace(ctx);
let cancelled = registry()
.cancel_where(|metadata| {
- metadata.parent_thread_id.as_deref() == Some(thread_id) && own.admits(metadata)
+ metadata.parent_thread_id.as_deref() == Some(thread_id) && own.admits(ctx, metadata)
})
.expect("detached task registry lock poisoned");
for entry in &cancelled {
@@ -114,11 +115,11 @@ pub(crate) fn cancel_for_thread(thread_id: &str) -> usize {
/// recorded: delivering one would start a fresh system turn on the thread,
/// which is exactly what Stop is meant to prevent. Returns the cancelled task
/// ids.
-pub(crate) fn stop_for_thread(thread_id: &str) -> Vec<String> {
- let own = caller_workspace();
+pub(crate) fn stop_for_thread(ctx: &crate::core::runtime::DispatchCtx, thread_id: &str) -> Vec<String> {
+ let own = caller_workspace(ctx);
let cancelled = registry()
.cancel_where(|metadata| {
- metadata.parent_thread_id.as_deref() == Some(thread_id) && own.admits(metadata)
+ metadata.parent_thread_id.as_deref() == Some(thread_id) && own.admits(ctx, metadata)
})
.expect("detached task registry lock poisoned");
let mut task_ids = Vec::with_capacity(cancelled.len());
@@ -163,10 +164,10 @@ pub(crate) fn stop_for_thread(thread_id: &str) -> Vec<String> {
/// [`super::super::background_completions`] and drop any straggler completion
/// that wins the cooperative-abort race. Headless sub-agents (no parent thread)
/// are still aborted but contribute no id.
-pub(crate) fn cancel_all() -> Vec<String> {
- let own = caller_workspace();
+pub(crate) fn cancel_all(ctx: &crate::core::runtime::DispatchCtx) -> Vec<String> {
+ let own = caller_workspace(ctx);
let cancelled = registry()
- .cancel_where(|metadata| own.admits(metadata))
+ .cancel_where(|metadata| own.admits(ctx, metadata))
.expect("detached task registry lock poisoned");
let count = cancelled.len();
let mut thread_ids: Vec<String> = Vec::new();
@@ -201,19 +202,23 @@ pub(crate) enum CallerWorkspace {
}
impl CallerWorkspace {
- pub(crate) fn admits(&self, metadata: &super::registry::RunningSubagentMetadata) -> bool {
+ pub(crate) fn admits(
+ &self,
+ ctx: &crate::core::runtime::DispatchCtx,
+ metadata: &super::registry::RunningSubagentMetadata,
+ ) -> bool {
match self {
Self::All => true,
- Self::Only(dir) => metadata.workspace_dir == *dir && control_admits(metadata),
+ Self::Only(dir) => metadata.workspace_dir == *dir && control_admits(ctx, metadata),
Self::Nothing => false,
}
}
}
-pub(crate) fn caller_workspace() -> CallerWorkspace {
+pub(crate) fn caller_workspace(ctx: &crate::core::runtime::DispatchCtx) -> CallerWorkspace {
caller_workspace_in(
crate::core::runtime::is_saas(),
- crate::core::runtime::tenant::context_in(true).as_deref(),
+ ctx.resolved().as_deref(),
)
}
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 6dc2bc5fba..8ab079a656 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/registry.rs
@@ -55,8 +55,9 @@ pub(crate) struct RunningSubagentMetadata {
}
/// Resolve the caller's profile without falling back to the operator in SaaS.
-fn caller_profile() -> Result<Option<String>, WaitError> {
- let profile = crate::core::runtime::tenant::current_tenant()
+fn caller_profile(ctx: &crate::core::runtime::DispatchCtx) -> Result<Option<String>, WaitError> {
+ let profile = ctx
+ .tenant()
.map_err(|_| WaitError::NotOwned)?
.profile;
if crate::core::runtime::is_saas() && profile.is_none() {
@@ -76,15 +77,16 @@ pub(crate) enum RegistrationError {
/// Check the actual caller before any reusable-session or worker-thread write.
pub(crate) fn admit_spawn(
+ ctx: &crate::core::runtime::DispatchCtx,
workspace: &std::path::Path,
) -> Result<Option<String>, RegistrationError> {
- admit_workspace(workspace).map_err(|_| RegistrationError::Scope)?;
- caller_profile().map_err(|_| RegistrationError::Scope)
+ admit_workspace(ctx, workspace).map_err(|_| RegistrationError::Scope)?;
+ caller_profile(ctx).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()?;
+pub(super) fn owner_key(ctx: &crate::core::runtime::DispatchCtx, parent_session: &str) -> Result<String, WaitError> {
+ let profile = caller_profile(ctx)?;
Ok(crate::core::runtime::tenant::tenant_key(
&crate::core::runtime::tenant::Tenant {
profile,
@@ -96,18 +98,21 @@ pub(super) fn owner_key(parent_session: &str) -> Result<String, WaitError> {
/// Public user controls may reach any of their profile's parents; desktop
/// controls retain the process user's existing authority over all workers.
-pub(super) fn control_admits(metadata: &RunningSubagentMetadata) -> bool {
+pub(super) fn control_admits(ctx: &crate::core::runtime::DispatchCtx, metadata: &RunningSubagentMetadata) -> bool {
!crate::core::runtime::is_saas()
- || caller_profile().is_ok_and(|profile| profile == metadata.profile)
+ || caller_profile(ctx).is_ok_and(|profile| profile == metadata.profile)
}
/// Durable fallback paths must stay in the caller's captured workspace.
-pub(super) fn admit_workspace(workspace: &std::path::Path) -> Result<(), WaitError> {
+pub(super) fn admit_workspace(
+ ctx: &crate::core::runtime::DispatchCtx,
+ workspace: &std::path::Path,
+) -> Result<(), WaitError> {
if !crate::core::runtime::is_saas() {
return Ok(());
}
- caller_profile()?;
- let context = crate::core::runtime::CoreContext::scoped().ok_or(WaitError::NotOwned)?;
+ caller_profile(ctx)?;
+ let context = ctx.resolved().ok_or(WaitError::NotOwned)?;
if context.workspace_dir().map_err(|_| WaitError::NotOwned)? != workspace {
return Err(WaitError::NotOwned);
}
@@ -162,6 +167,7 @@ pub(crate) fn status_channel() -> (
/// entries so it stays bounded even if a parent never calls `wait_subagent`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn register(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: String,
agent_id: String,
parent_session: String,
@@ -173,7 +179,7 @@ pub(crate) fn register(
abort: AbortHandle,
status: watch::Receiver<DetachedSubagentStatus>,
) -> Result<(), RegistrationError> {
- let profile = match admit_spawn(&workspace_dir) {
+ let profile = match admit_spawn(ctx, &workspace_dir) {
Ok(profile) => profile,
Err(error) => {
abort.abort();
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs
index 9f518cbbf5..11e7e3b0eb 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/resolve.rs
@@ -17,19 +17,21 @@ use super::task_ledger::{list_task_records, task_record_for_task_in_workspace};
/// Resolve a durable `subagent_session_id` to the currently-running transient
/// `task_id`, enforcing parent-session ownership.
pub(crate) fn task_id_for_session(
+ ctx: &crate::core::runtime::DispatchCtx,
subagent_session_id: &str,
parent_session: &str,
) -> Result<String, WaitError> {
- live_task_id(registry(), subagent_session_id, &owner_key(parent_session)?)
+ live_task_id(registry(), subagent_session_id, &owner_key(ctx, parent_session)?)
}
pub(crate) fn task_id_for_session_in_workspace(
+ ctx: &crate::core::runtime::DispatchCtx,
subagent_session_id: &str,
parent_session: &str,
workspace_dir: &Path,
) -> Result<String, WaitError> {
- admit_workspace(workspace_dir)?;
- match task_id_for_session(subagent_session_id, parent_session) {
+ admit_workspace(ctx, workspace_dir)?;
+ match task_id_for_session(ctx, subagent_session_id, parent_session) {
Ok(task_id) => return Ok(task_id),
Err(WaitError::NotOwned) => return Err(WaitError::NotOwned),
Err(WaitError::RegistryPoisoned) => return Err(WaitError::RegistryPoisoned),
@@ -51,19 +53,21 @@ pub(crate) fn task_id_for_session_in_workspace(
}
pub(crate) fn resume_ref_for_task(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
parent_session: &str,
) -> Result<SubagentResumeRef, WaitError> {
- live_resume_ref(registry(), task_id, &owner_key(parent_session)?)
+ live_resume_ref(registry(), task_id, &owner_key(ctx, parent_session)?)
}
pub(crate) fn resume_ref_for_task_in_workspace(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
parent_session: &str,
workspace_dir: &Path,
) -> Result<SubagentResumeRef, WaitError> {
- admit_workspace(workspace_dir)?;
- match resume_ref_for_task(task_id, parent_session) {
+ admit_workspace(ctx, workspace_dir)?;
+ match resume_ref_for_task(ctx, task_id, parent_session) {
Ok(reference) => return Ok(reference),
Err(WaitError::NotOwned) => return Err(WaitError::NotOwned),
Err(WaitError::RegistryPoisoned) => return Err(WaitError::RegistryPoisoned),
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/roster.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/roster.rs
index 63383904d0..ef23956cec 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/roster.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/roster.rs
@@ -8,8 +8,11 @@ use crate::agent::orchestration::fleet_tools::FleetToolSet;
/// Snapshot the sub-agents registered under `parent_session`, with each status
/// read live from its watch channel (ordered by `agent_id` then `task_id`).
-pub(crate) fn snapshot_for_parent(parent_session: &str) -> Vec<SubagentSnapshot> {
- let Ok(owner) = owner_key(parent_session) else {
+pub(crate) fn snapshot_for_parent(
+ ctx: &crate::core::runtime::DispatchCtx,
+ parent_session: &str,
+) -> Vec<SubagentSnapshot> {
+ let Ok(owner) = owner_key(ctx, parent_session) else {
return Vec::new();
};
snapshot_for_owner(registry(), &owner).unwrap_or_else(|err| {
@@ -79,12 +82,13 @@ const DURABLE_ROSTER_CAP: usize = 12;
/// `wait_subagent` / `steer_subagent` / `close_subagent` since #5701, and
/// telling it otherwise cost an iteration of confused reasoning per turn).
pub(crate) fn active_subagents_context_block(
+ ctx: &crate::core::runtime::DispatchCtx,
parent_session: &str,
workspace_dir: &std::path::Path,
fleet: &FleetToolSet,
) -> Option<String> {
- admit_workspace(workspace_dir).ok()?;
- let workers = snapshot_for_parent(parent_session);
+ admit_workspace(ctx, workspace_dir).ok()?;
+ let workers = snapshot_for_parent(ctx, parent_session);
// Durable sessions not already represented by a live registry entry.
let live_session_ids: std::collections::HashSet<String> = workers
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs
index 2b04ebd5c0..275752776b 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs
@@ -70,6 +70,7 @@ fn send_registered_steering(
/// TinyAgents steering registry when the child run has registered its live
/// handle, and fall back to the OpenHuman `RunQueue` compatibility path.
pub async fn steer(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
parent_session: &str,
text: String,
@@ -84,7 +85,7 @@ pub async fn steer(
return Err(SteerError::UnsupportedLane);
}
let task_id_key = TaskId::new(task_id);
- let owner = owner_key(parent_session).map_err(|_| SteerError::NotOwned)?;
+ let owner = owner_key(ctx, parent_session).map_err(|_| SteerError::NotOwned)?;
let snapshot = registry()
.snapshot(&task_id_key, &owner)
.map_err(steer_error_from_registry)?;
@@ -137,6 +138,7 @@ pub async fn steer(
/// retain authority over the process user's workers. Unknown, foreign or
/// terminal tasks are refused, and steered text is never logged.
pub(crate) async fn steer_control(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
text: String,
lane: QueueLane,
@@ -153,7 +155,7 @@ pub(crate) async fn steer_control(
let snapshot = registry()
.snapshot_trusted(&task_id_key)
.map_err(steer_error_from_registry)?;
- if !control_admits(&snapshot.metadata) {
+ if !control_admits(ctx, &snapshot.metadata) {
return Err(SteerError::NotOwned);
}
if snapshot.status.is_terminal() {
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs
index a8d6936842..0a309ae431 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs
@@ -14,21 +14,23 @@ use super::task_ledger::task_record_for_task_in_workspace;
/// Block until `task_id` reaches a terminal status or `timeout` elapses.
pub(crate) async fn wait(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
parent_session: &str,
timeout: Duration,
) -> Result<WaitOutcome, WaitError> {
- wait_detached(registry(), task_id, &owner_key(parent_session)?, timeout).await
+ wait_detached(registry(), task_id, &owner_key(ctx, parent_session)?, timeout).await
}
pub(crate) async fn wait_in_workspace(
+ ctx: &crate::core::runtime::DispatchCtx,
task_id: &str,
parent_session: &str,
workspace_dir: &Path,
timeout: Duration,
) -> Result<WaitOutcome, WaitError> {
- admit_workspace(workspace_dir)?;
- match wait(task_id, parent_session, timeout).await {
+ admit_workspace(ctx, workspace_dir)?;
+ match wait(ctx, task_id, parent_session, timeout).await {
Ok(outcome) => return Ok(outcome),
Err(WaitError::NotOwned) => return Err(WaitError::NotOwned),
Err(WaitError::RegistryPoisoned) => return Err(WaitError::RegistryPoisoned),
diff --git a/vendor/tinycomputer b/vendor/tinycomputer
index ce95211552..060ee98776 160000
--- a/vendor/tinycomputer
+++ b/vendor/tinycomputer
@@ -1 +1 @@
-Subproject commit ce95211552e5eb1e01804a93c73469fbf56bab7f
+Subproject commit 060ee9877672bf5ecbce3f95d786328489df8171