Skip to content

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

fix(threads): treat a child that settles before cancel as settled, not failed

M3gA-Mind8f8219826396
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
index f07303591f..94991f4d7b 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents.rs
@@ -6,6 +6,8 @@ mod registry;
mod roster;
mod task_ledger;
+#[cfg(test)]
+pub(crate) use cancel::cancel_for_thread_with_hook;
pub(crate) use cancel::{
cancel_all, cancel_by_session_in_workspace, cancel_by_task_with_workspace_reason,
cancel_for_thread, stop_for_thread, CancelledSubagent,
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 0ae00b0afb..1aae4b5e93 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/cancel.rs
@@ -297,6 +297,20 @@ pub(crate) async fn cancel_for_thread(
ctx: &crate::core::runtime::DispatchCtx,
thread_id: &str,
) -> Result<usize, String> {
+ cancel_for_thread_with_hook(ctx, thread_id, || async {}).await
+}
+
+/// [`cancel_for_thread`] with a hook that runs after a live job is selected and
+/// before it is cancelled, so tests can let the job settle inside that window.
+pub(crate) async fn cancel_for_thread_with_hook<F, Fut>(
+ ctx: &crate::core::runtime::DispatchCtx,
+ thread_id: &str,
+ mut before_cancel: F,
+) -> Result<usize, String>
+where
+ F: FnMut() -> Fut,
+ Fut: std::future::Future<Output = ()>,
+{
let Some(core_context) = ctx.resolved() else {
return Err("cannot resolve core context to cancel thread-owned driver jobs".into());
};
@@ -390,10 +404,23 @@ pub(crate) async fn cancel_for_thread(
crate::agent::orchestration::completion_owners::finish_driver_task(ctx, &task_id);
}
for job in live_jobs {
+ before_cancel().await;
match jobs.cancel_owned(job.id.as_str(), &owner_key) {
Ok(cancelled_job) => {
cancelled += usize::from(!cancelled_job.status.is_terminal());
}
+ // The job finished on its own after it was listed as live. It
+ // is settled, not stuck: fall through to the wait, which
+ // returns at once for a terminal job, and release its owner.
+ Err(tinyagents_orchestration::subagent::SubAgentJobError::Terminal {
+ status,
+ ..
+ }) => {
+ log::debug!(
+ "[running_subagents] cancel_for_thread job_id={} settled before cancel status={status:?}",
+ job.id
+ );
+ }
Err(error) => {
failures.push(format!(
"driver thread deletion cancellation failed: {error}"
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 14a7f1461f..25e34c7f63 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
@@ -630,6 +630,8 @@ mod driver_reuse_tests;
mod driver_tests;
#[path = "tools_e2e_profile_cancel_tests.rs"]
mod profile_cancel_tests;
+#[path = "tools_e2e_thread_cancel_settled_tests.rs"]
+mod thread_cancel_settled_tests;
#[path = "tools_e2e_thread_delete_tests.rs"]
mod thread_delete_tests;
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_cancel_settled_tests.rs b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_cancel_settled_tests.rs
new file mode 100644
index 0000000000..519ffcccd9
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/tools/tools_e2e_thread_cancel_settled_tests.rs
@@ -0,0 +1,184 @@
+use super::*;
+
+/// A driver child listed as live by thread deletion/purge, which then finishes
+/// on its own before `cancel_owned` runs, is settled, not a cancellation
+/// failure. `cancel_owned` answers `Terminal` for it; thread deletion and purge
+/// both settle children through `cancel_for_thread`, which used to report that
+/// as "could not safely settle" and fail the whole operation.
+#[tokio::test]
+async fn child_settling_between_listing_and_cancel_counts_as_settled() {
+ use crate::agent::orchestration::running_subagents::cancel_for_thread_with_hook;
+ use crate::agent::tinyagents::host::OpenHumanRunContext;
+ use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
+ use crate::threads::store::{blocking, CreateConversationThread};
+ use crate::tools::toolpacks::ToolGroups;
+ use tinyagents_harness::context::RunContext;
+ use tinyagents_orchestration::subagent::SubAgentJobRegistry;
+ use tinyagents_orchestration::subagent::SubAgentJobStatus;
+
+ struct GatedModel {
+ started: tokio::sync::mpsc::Sender<()>,
+ release: Arc<tokio::sync::Notify>,
+ }
+
+ #[async_trait]
+ impl ChatModel<()> for GatedModel {
+ fn profile(&self) -> Option<&ModelProfile> {
+ static PROFILE: std::sync::OnceLock<ModelProfile> = std::sync::OnceLock::new();
+ Some(PROFILE.get_or_init(|| {
+ let mut profile = ModelProfile::default();
+ profile.tool_calling = true;
+ profile.parallel_tool_calls = true;
+ profile
+ }))
+ }
+
+ async fn invoke(
+ &self,
+ _state: &(),
+ _request: ModelRequest,
+ ) -> tinyinference_llm::Result<ModelResponse> {
+ let _ = self.started.send(()).await;
+ self.release.notified().await;
+ Ok(ModelResponse::assistant("child finished before cancel"))
+ }
+ }
+
+ let scratch = tempfile::tempdir().expect("isolated cancel-window fixture");
+ let workspace = scratch.path().join("workspace");
+ std::fs::create_dir_all(&workspace).expect("create workspace");
+ let thread_id = "cancel-window-parent-thread";
+ let parent_session = "cancel-window-parent-session";
+ let (started_tx, mut started_rx) = tokio::sync::mpsc::channel(1);
+ let release = Arc::new(tokio::sync::Notify::new());
+ let provider = Arc::new(GatedModel {
+ started: started_tx,
+ release: release.clone(),
+ });
+ let mut parent = parent_context(&workspace, provider, vec![]);
+ let workspace_descriptor = tinytools::WorkspaceDescriptor::new(workspace.clone());
+ parent.workspace_descriptor = Some(workspace_descriptor.clone());
+ parent.workspace_dir = workspace.clone();
+ parent.session_id = parent_session.into();
+
+ let definitions = Arc::new(AgentDefinitionRegistry::builtins_only());
+ let mut config = crate::config::Config::default();
+ config.workspace_dir = workspace.clone();
+ config.components.juice = false;
+ let core = CoreContext::for_test_with_config(DomainSet::full(), config.clone()).derive_with(
+ ContextOverlay::new(config, DomainSet::full(), ToolGroups::none()).definitions(definitions),
+ );
+ let mut host_context = OpenHumanRunContext::new()
+ .with_core(Some(Arc::clone(&core)))
+ .with_parent(parent);
+ host_context.thread_id = Some(thread_id.into());
+ let live = RunContext::<OpenHumanRunContext>::new(
+ tinyagents_harness::context::RunConfig::new("cancel-window-parent").with_thread(thread_id),
+ host_context,
+ )
+ .with_workspace(workspace_descriptor.clone());
+ let dispatch = live.data.dispatch_ctx();
+ blocking::ensure_thread(
+ dispatch.clone(),
+ workspace.clone(),
+ CreateConversationThread {
+ id: thread_id.into(),
+ title: "Cancel window parent".into(),
+ created_at: "2026-01-01T00:00:00Z".into(),
+ parent_thread_id: None,
+ labels: None,
+ personality_id: None,
+ working_dir: None,
+ },
+ )
+ .await
+ .expect("persist parent thread before spawning child");
+
+ let completion_store: Arc<dyn tinyagents_tasks::CompletionStore> =
+ Arc::new(tinyagents_tasks::InMemoryCompletionStore::new());
+ crate::agent::orchestration::background_completions::install_store_for_test(
+ &workspace,
+ completion_store,
+ );
+ let detached_data = live.data.detached_child();
+ let detached = live
+ .child(
+ tinyagents_harness::context::RunConfig::new("cancel-window-child"),
+ detached_data.clone(),
+ )
+ .expect("detached child context")
+ .with_cancellation(detached_data.cancellation.clone());
+ let spawned = crate::agent::orchestration::tools::SpawnSubagentTool::new()
+ .execute_with_live_parent_context(
+ json!({
+ "agent_id": "task_manager_agent",
+ "prompt": "finish while the parent thread is being deleted",
+ "model": "test-model"
+ }),
+ None,
+ live.data.child(),
+ detached,
+ )
+ .await
+ .expect("spawn tool execution");
+ assert!(!spawned.is_error, "{}", spawned.output());
+ tokio::time::timeout(std::time::Duration::from_secs(5), started_rx.recv())
+ .await
+ .expect("child reaches its gated provider call")
+ .expect("provider start notification");
+
+ let jobs = crate::core::runtime::slot_for::<SubAgentJobRegistry>(&dispatch);
+ let job = jobs
+ .list()
+ .into_iter()
+ .find(|job| job.status == SubAgentJobStatus::Running && job.agent == "task_manager_agent")
+ .expect("spawned child is live in the TinyAgents job registry");
+
+ // The hook runs after cancel_for_thread has listed the job as live and
+ // before it calls cancel_owned. Let the child finish and wait until the
+ // registry reports it terminal, so cancel_owned meets a settled job.
+ let settle_before_cancel = {
+ let release = release.clone();
+ let jobs = jobs.clone();
+ let job_id = job.id.clone();
+ move || {
+ let release = release.clone();
+ let jobs = jobs.clone();
+ let job_id = job_id.clone();
+ async move {
+ release.notify_one();
+ tokio::time::timeout(std::time::Duration::from_secs(5), async {
+ while !jobs
+ .get(job_id.as_str())
+ .is_some_and(|job| job.status.is_terminal())
+ {
+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .expect("released child settles on its own");
+ }
+ }
+ };
+
+ let cancelled = cancel_for_thread_with_hook(&dispatch, thread_id, settle_before_cancel)
+ .await
+ .expect("a child that settled before cancel is not a cancellation failure");
+
+ assert_eq!(
+ cancelled, 0,
+ "the child finished on its own, nothing was cancelled"
+ );
+ assert_eq!(
+ jobs.get(job.id.as_str()).map(|job| job.status),
+ Some(SubAgentJobStatus::Completed),
+ "the child's own completion stands"
+ );
+ assert!(
+ crate::agent::orchestration::completion_owners::driver_tasks_for_thread(
+ &dispatch, &workspace, thread_id,
+ )
+ .is_empty(),
+ "the settled child's owner slot is released"
+ );
+}