Skip to content

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

test(mcp): cover orphaned cleanup retries and ephemeral preservation

Steven Enamakelbcc25f3f8bf6
diff --git a/crates/openhuman-core/src/mcp/module_client_owner_tests.rs b/crates/openhuman-core/src/mcp/module_client_owner_tests.rs
index d6745a948c..4328af03c8 100644
--- a/crates/openhuman-core/src/mcp/module_client_owner_tests.rs
+++ b/crates/openhuman-core/src/mcp/module_client_owner_tests.rs
@@ -408,3 +408,21 @@ async fn a_blocked_close_has_an_overall_cleanup_deadline_and_keeps_the_owner_for
.unwrap()
.contains_key(&directory));
}
+
+#[test]
+fn orphaned_shutdown_cleanup_keeps_a_host_directory_reservation_until_confirmed() {
+ let scratch = tempfile::tempdir().unwrap();
+ let directory = scratch.path().join("agents/purge-pending");
+ let token = agent_cleanup::reserve_agent_directory(scratch.path(), "purge-pending");
+ McpConfiguredClient::retry_orphaned_cleanups();
+ assert!(McpConfiguredClient::agent_close_pending(&directory));
+ let reservation = McpConfiguredClient::reservation_for(&directory);
+ assert!(!McpConfiguredClient::finish_cleanup(
+ &directory,
+ None,
+ reservation
+ ));
+ assert!(McpConfiguredClient::agent_close_pending(&directory));
+ token.complete();
+ assert!(!McpConfiguredClient::agent_close_pending(&directory));
+}
diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs
index 3c22a19f90..78fdf0daa5 100644
--- a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs
+++ b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs
@@ -289,3 +289,102 @@ fn a_failed_purge_fences_the_same_directory_after_its_original_runtime_is_gone()
.unwrap();
});
}
+
+#[test]
+fn repairing_and_retrying_purge_on_the_original_runtime_releases_the_directory() {
+ let executor = tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .worker_threads(1)
+ .thread_stack_size(openhuman_core::core::runtime::AGENT_WORKER_STACK_BYTES)
+ .build()
+ .unwrap();
+ executor.block_on(async {
+ tokio::spawn(async {
+ let scratch = tempfile::tempdir().unwrap();
+ let runtime = crate::Runtime::builder()
+ .config(openhuman_core::config::Config::default())
+ .workspace(crate::Workspace::Dir(scratch.path().into()))
+ .access(crate::Access::full())
+ .build()
+ .await
+ .unwrap();
+ let spec = || {
+ crate::AgentSpec::new("worker")
+ .provider(
+ crate::Provider::openai_compatible("http://127.0.0.1:1", "fixture")
+ .model("fixture"),
+ )
+ .access(crate::Access::full())
+ };
+ let agent = runtime.agent(spec()).unwrap();
+ let home = scratch.path().join("agents/worker");
+ std::fs::remove_dir_all(&home).unwrap();
+ std::fs::write(&home, "force purge failure").unwrap();
+ assert!(matches!(
+ runtime.remove_agent("worker").purge().await,
+ Err(AgentError::Workspace { .. })
+ ));
+ drop(agent);
+ assert!(openhuman_core::mcp::host::agent_close_pending(
+ scratch.path(),
+ "worker"
+ ));
+ std::fs::remove_file(&home).unwrap();
+ std::fs::create_dir_all(&home).unwrap();
+ std::fs::write(home.join("old-state"), "to purge").unwrap();
+ runtime.remove_agent("worker").purge().await.unwrap();
+ assert!(!home.exists());
+ assert!(!openhuman_core::mcp::host::agent_close_pending(
+ scratch.path(),
+ "worker"
+ ));
+ let successor = runtime
+ .agent(spec())
+ .expect("confirmed purge releases the public id");
+ drop(successor);
+ drop(runtime);
+ })
+ .await
+ .unwrap();
+ });
+}
+
+#[test]
+fn dropping_an_agent_without_an_mcp_entry_keeps_normal_ephemeral_cleanup() {
+ let executor = tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .worker_threads(1)
+ .thread_stack_size(openhuman_core::core::runtime::AGENT_WORKER_STACK_BYTES)
+ .build()
+ .unwrap();
+ executor.block_on(async {
+ tokio::spawn(async {
+ let runtime = crate::Runtime::builder()
+ .config(openhuman_core::config::Config::default())
+ .workspace(crate::Workspace::Ephemeral)
+ .access(crate::Access::full())
+ .build()
+ .await
+ .unwrap();
+ let root = runtime.workspace_dir().to_path_buf();
+ let agent = runtime
+ .agent(
+ crate::AgentSpec::new("unused")
+ .provider(
+ crate::Provider::openai_compatible("http://127.0.0.1:1", "fixture")
+ .model("fixture"),
+ )
+ .access(crate::Access::full()),
+ )
+ .unwrap();
+ drop(agent);
+ drop(runtime);
+ assert!(!root.exists());
+ assert!(!openhuman_core::mcp::host::agent_close_pending(
+ &root, "unused"
+ ));
+ })
+ .await
+ .unwrap();
+ });
+}
diff --git a/crates/openhuman-embed/src/runtime/host_agents.rs b/crates/openhuman-embed/src/runtime/host_agents.rs
index 0097c398a8..7f3a4af359 100644
--- a/crates/openhuman-embed/src/runtime/host_agents.rs
+++ b/crates/openhuman-embed/src/runtime/host_agents.rs
@@ -18,7 +18,9 @@ use crate::agent::AgentInner;
pub(crate) type AgentMap = Mutex<AgentRegistry>;
/// Failed cleanup retains its captured owner and purge intent, without keeping
-/// an agent or the runtime guard alive. `remove_agent` can retry this record.
+/// an agent or the runtime guard alive. The original live runtime's
+/// `remove_agent` can retry this record. Its process-local directory fence
+/// survives a lost record, without automatic durable retry or reclamation.
#[derive(Clone)]
pub(crate) struct PendingCleanup {
pub(crate) cleanup: Option<openhuman_core::mcp::host::AgentMcpCleanup>,
diff --git a/crates/openhuman-embed/src/runtime/lifecycle.rs b/crates/openhuman-embed/src/runtime/lifecycle.rs
index 9c0f8e95f2..7452a4c9b0 100644
--- a/crates/openhuman-embed/src/runtime/lifecycle.rs
+++ b/crates/openhuman-embed/src/runtime/lifecycle.rs
@@ -93,6 +93,17 @@ impl Runtime {
/// including after the last agent handle was dropped. Runtime drop does
/// not execute the process signal's shutdown hooks.
///
+ /// Each MCP attempt has a 30-second deadline; three detached attempts add
+ /// at most 750 ms of backoff. Filesystem purge and executor scheduling are
+ /// outside that wait bound. Failed or cancelled cleanup preserves an
+ /// ephemeral workspace when the runtime drops and logs its retained path.
+ /// That preservation is conservative for the rest of this runtime.
+ ///
+ /// The directory fence survives in this process if the original runtime's
+ /// retry record is lost. A new runtime cannot recover that record through
+ /// this method; there is no automatic durable retry or file reclamation.
+ /// Retained state then needs explicit host cleanup.
+ ///
/// Cancellation is cooperative: a tool already executing when the agent
/// is removed, or a sub-agent it detached, may finish after this returns.
pub fn remove_agent(&self, id: &str) -> RemoveAgent<'_> {
diff --git a/crates/openhuman-embed/src/runtime/mod.rs b/crates/openhuman-embed/src/runtime/mod.rs
index 82ceabc55e..65e56691ee 100644
--- a/crates/openhuman-embed/src/runtime/mod.rs
+++ b/crates/openhuman-embed/src/runtime/mod.rs
@@ -179,7 +179,7 @@ pub enum RuntimeError {
/// [`crate::Workspace::Ephemeral`], the workspace it lives in.
///
/// Held as `Arc<CoreGuard>` by both `Runtime` and `AgentInner` so its `Drop`
-/// — releasing [`RUNTIME_LIVE`] and removing an ephemeral workspace — runs
+/// — releasing [`RUNTIME_LIVE`] and normally removing an ephemeral workspace — runs
/// only once the *last* of them goes away. `Agent`s are owned rather than
/// borrowed from `Runtime`, so a caller can drop the `Runtime` handle while
/// an `Agent` (or a `Turn` in flight) is still alive; if `Runtime` tore this
@@ -211,6 +211,9 @@ pub(crate) struct CoreGuard {
}
impl CoreGuard {
+ /// Keep the scratch path after an unconfirmed cleanup, while still
+ /// releasing the process slot. Sticky to avoid clearing another failed
+ /// agent's preservation request when an independent retry succeeds.
pub(crate) fn preserve_ephemeral_workspace(&self) {
self.preserve_ephemeral_workspace
.store(true, std::sync::atomic::Ordering::Release);