Skip to content

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

fix(subagent): make closing an already-closed session idempotent

Steven Enamakelcc5562461826
diff --git a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs
index 09e09b201d..e62fcb2aa7 100644
--- a/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs
+++ b/crates/openhuman-core/src/agent/orchestration/subagent_sessions/tiny_port.rs
@@ -241,6 +241,10 @@ impl TinySessionStore for OpenHumanTinySubagentSessionStore {
let Some(session) = session else {
return Ok(false);
};
+ if session.status == DurableSubagentStatus::Closed {
+ return super::close_if_current(&self.store, &session)
+ .map_err(SubagentError::Persistence);
+ }
if let Some(jobs) = &self.jobs {
let _ = super::cancel_driver_job_for_observed_session(
&self.store,
diff --git a/crates/openhuman-core/src/agent/orchestration/tools/close_subagent.rs b/crates/openhuman-core/src/agent/orchestration/tools/close_subagent.rs
index ec0a53801b..87602d2a6f 100644
--- a/crates/openhuman-core/src/agent/orchestration/tools/close_subagent.rs
+++ b/crates/openhuman-core/src/agent/orchestration/tools/close_subagent.rs
@@ -159,20 +159,21 @@ impl CloseSubagentTool {
tinyagents_orchestration::subagent::SubAgentJobRegistry,
>(&dispatch);
let store = SubagentSessionStore::new(parent.workspace_dir.clone());
- let cancelled = subagent_sessions::cancel_driver_job_for_observed_session(
- &store,
- &parent.workspace_dir.join(".openhuman/subagent_checkpoints"),
- &jobs,
- &observed,
- &parent.session_id,
- thread_id,
- ) || running_subagents::cancel_by_session_in_workspace(
- &dispatch,
- &subagent_session_id,
- &parent.session_id,
- &parent.workspace_dir,
- )
- .is_some();
+ let cancelled = observed.status != subagent_sessions::DurableSubagentStatus::Closed
+ && (subagent_sessions::cancel_driver_job_for_observed_session(
+ &store,
+ &parent.workspace_dir.join(".openhuman/subagent_checkpoints"),
+ &jobs,
+ &observed,
+ &parent.session_id,
+ thread_id,
+ ) || running_subagents::cancel_by_session_in_workspace(
+ &dispatch,
+ &subagent_session_id,
+ &parent.session_id,
+ &parent.workspace_dir,
+ )
+ .is_some());
match subagent_sessions::close_if_current(&store, &observed) {
Ok(false) => Ok(ToolResult::error(
"close_subagent: session changed while it was being closed; retry with its current reference",
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops_tests_model_resolution_tests.rs b/crates/openhuman-core/src/agent/subagent_host/ops_tests_model_resolution_tests.rs
index 2210cf266e..72050e1293 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops_tests_model_resolution_tests.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops_tests_model_resolution_tests.rs
@@ -266,6 +266,10 @@ async fn closing_a_durable_driver_session_cancels_only_its_registered_child() {
.close("close-driver-session")
.await
.expect("Tiny close adapter cancels then closes the durable row"));
+ assert!(port
+ .close("close-driver-session")
+ .await
+ .expect("repeated close is idempotent without re-cancelling"));
assert!(!parent_cancellation.is_cancelled(), "parent stays live");
let tiny_store: std::sync::Arc<dyn TinySessionStore> = std::sync::Arc::new(port.clone());