Skip to content

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

fix(mcp): keep agent home until module close is acknowledged

Steven Enamakelf94b1c807538
diff --git a/crates/openhuman-core/src/mcp/host.rs b/crates/openhuman-core/src/mcp/host.rs
index ad2e0c1949..5cd5725458 100644
--- a/crates/openhuman-core/src/mcp/host.rs
+++ b/crates/openhuman-core/src/mcp/host.rs
@@ -50,6 +50,14 @@ pub fn schedule_agent_close(workspace_dir: &std::path::Path, agent_id: &str) {
);
}
+/// Whether a prior agent-scoped module close is still pending acknowledgment.
+#[cfg(feature = "mcp")]
+pub fn agent_close_pending(workspace_dir: &std::path::Path, agent_id: &str) -> bool {
+ crate::mcp::module_client::McpConfiguredClient::agent_close_pending(
+ &workspace_dir.join("agents").join(agent_id),
+ )
+}
+
/// There is no MCP client to close when the feature is disabled.
#[cfg(not(feature = "mcp"))]
pub async fn close_agent(_workspace_dir: &std::path::Path, _agent_id: &str) -> bool {
@@ -60,6 +68,12 @@ pub async fn close_agent(_workspace_dir: &std::path::Path, _agent_id: &str) -> b
#[cfg(not(feature = "mcp"))]
pub fn schedule_agent_close(_workspace_dir: &std::path::Path, _agent_id: &str) {}
+/// No agent-scoped module close is pending when MCP is disabled.
+#[cfg(not(feature = "mcp"))]
+pub fn agent_close_pending(_workspace_dir: &std::path::Path, _agent_id: &str) -> bool {
+ false
+}
+
/// Converts the MCP section of product configuration to the module contract.
pub fn client_config(config: &Config) -> tinymcp_bus::McpClientConfig {
let mut client = tinymcp_bus::McpClientConfig {
diff --git a/crates/openhuman-core/src/mcp/module_client_tests.rs b/crates/openhuman-core/src/mcp/module_client_tests.rs
index cd5beb924e..e6953169f2 100644
--- a/crates/openhuman-core/src/mcp/module_client_tests.rs
+++ b/crates/openhuman-core/src/mcp/module_client_tests.rs
@@ -476,6 +476,54 @@ async fn failed_agent_close_stays_retired_and_can_be_retried() {
assert!(McpConfiguredClient::for_config(&config).is_ok());
}
+#[tokio::test]
+async fn delayed_agent_close_cannot_evict_a_replacement_cache_entry() {
+ let workspace = tempfile::tempdir().expect("workspace");
+ let mut config = Config::default();
+ config.workspace_dir = workspace.path().to_path_buf();
+ config.modules.enabled = true;
+
+ let old_port = FakePort::default();
+ let old_closed = Arc::clone(&old_port.closed);
+ let old = McpConfiguredClient::install_test_port(&config, Box::new(old_port))
+ .expect("install old client");
+ old.call::<bool>(&configured("server"), "ConnectedOverview", ())
+ .await
+ .unwrap();
+ let captured = McpConfiguredClient::retire_cached_client(workspace.path())
+ .expect("retirement captures the exact old client");
+
+ let replacement_port = FakePort::default();
+ let replacement_closed = Arc::clone(&replacement_port.closed);
+ let replacement = Arc::new(McpConfiguredClient::with_port(
+ workspace.path().to_string_lossy().into_owned(),
+ Box::new(replacement_port),
+ ));
+ CLIENTS
+ .get()
+ .expect("old client initialized the cache")
+ .lock()
+ .unwrap()
+ .insert(workspace.path().to_path_buf(), Arc::clone(&replacement));
+
+ assert!(McpConfiguredClient::close_retired_client(
+ workspace.path().to_path_buf(),
+ captured,
+ )
+ .await);
+ assert_eq!(old_closed.lock().unwrap().len(), 1);
+ assert!(replacement_closed.lock().unwrap().is_empty());
+ let current = CLIENTS
+ .get()
+ .unwrap()
+ .lock()
+ .unwrap()
+ .get(workspace.path())
+ .cloned()
+ .expect("replacement remains cached");
+ assert!(Arc::ptr_eq(&current, &replacement));
+}
+
#[tokio::test]
async fn configuration_switches_replace_the_object_before_dispatch() {
let port = FakePort::default();
diff --git a/crates/openhuman-embed/src/runtime/lifecycle.rs b/crates/openhuman-embed/src/runtime/lifecycle.rs
index 40af66e66e..d70aa834b4 100644
--- a/crates/openhuman-embed/src/runtime/lifecycle.rs
+++ b/crates/openhuman-embed/src/runtime/lifecycle.rs
@@ -43,10 +43,16 @@ impl Drop for RemovalSlot<'_> {
fn drop(&mut self) {
self.inner.teardown("agent_removed");
if self.purge && !self.mcp_cleanup_confirmed {
- self.purge = false;
- log::warn!(
- "[embed][runtime] retaining agent home until MCP cleanup is acknowledged"
+ self.mcp_cleanup_confirmed = !openhuman_core::mcp::host::agent_close_pending(
+ &self.inner.config.workspace_dir,
+ &self.inner.id,
);
+ if !self.mcp_cleanup_confirmed {
+ self.purge = false;
+ log::warn!(
+ "[embed][runtime] retaining agent home until MCP cleanup is acknowledged"
+ );
+ }
}
if let Err(error) = self.purge_home() {
log::warn!("[embed][runtime] cancelled removal purge failed: {error}");
@@ -146,7 +152,7 @@ impl Runtime {
}
removal.mcp_cleanup_confirmed =
openhuman_core::mcp::host::close_agent(&inner.config.workspace_dir, &inner.id).await;
- if !removal.mcp_cleanup_confirmed && removal.purge {
+ if !removal.mcp_cleanup_confirmed {
removal.purge = false;
return Err(AgentError::CleanupPending);
}