The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(mcp): reserve agent directories until host cleanup completes
Steven Enamakel563ded1d200c
diff --git a/crates/openhuman-core/src/mcp/agent_cleanup.rs b/crates/openhuman-core/src/mcp/agent_cleanup.rsindex d8ba90007e..782a48f099 100644--- a/crates/openhuman-core/src/mcp/agent_cleanup.rs+++ b/crates/openhuman-core/src/mcp/agent_cleanup.rs@@ -85,3 +85,63 @@ where.map_err(|_| ModuleCallError::Unavailable)}}++/// A process-local directory reservation with no runtime guard or callback.+/// Dropping the token retains the fence; only confirmed host completion clears it.+#[derive(Clone)]+pub struct AgentDirectoryCleanup {+ directory: PathBuf,+ reservation: Arc<CleanupReservation>,+}++/// Reserve an agent's directory until host-owned cleanup succeeds.+/// This never creates a client or loads a module.+pub fn reserve_agent_directory(+ workspace: &std::path::Path,+ agent_id: &str,+) -> AgentDirectoryCleanup {+ let directory = workspace.join("agents").join(agent_id);+ let mut reservations = CLEANUP_RESERVATIONS+ .get_or_init(|| Mutex::new(HashMap::new()))+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let reservation = Arc::clone(+ reservations+ .entry(directory.clone())+ .or_insert_with(|| Arc::new(CleanupReservation::default())),+ );+ reservation+ .host_completion_pending+ .store(true, Ordering::Release);+ AgentDirectoryCleanup {+ directory,+ reservation,+ }+}++impl AgentDirectoryCleanup {+ /// Release this exact reservation after module close and host purge succeed.+ /// An independently registered canonical callback keeps its own reservation.+ pub fn complete(&self) {+ let after_close = self+ .reservation+ .after_close+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ let mut reservations = CLEANUP_RESERVATIONS+ .get_or_init(|| Mutex::new(HashMap::new()))+ .lock()+ .unwrap_or_else(std::sync::PoisonError::into_inner);+ if reservations+ .get(&self.directory)+ .is_some_and(|current| Arc::ptr_eq(current, &self.reservation))+ {+ self.reservation+ .host_completion_pending+ .store(false, Ordering::Release);+ if after_close.is_none() {+ reservations.remove(&self.directory);+ }+ }+ }+}diff --git a/crates/openhuman-core/src/mcp/cached_cleanup.rs b/crates/openhuman-core/src/mcp/cached_cleanup.rsindex b46984b764..b746e90248 100644--- a/crates/openhuman-core/src/mcp/cached_cleanup.rs+++ b/crates/openhuman-core/src/mcp/cached_cleanup.rs@@ -213,7 +213,7 @@ impl McpConfiguredClient {.unwrap_or_else(std::sync::PoisonError::into_inner).get(data_dir).is_some_and(|stored| Arc::ptr_eq(stored, &reservation));- if !is_current {+ if !is_current || reservation.host_completion_pending.load(Ordering::Acquire) {return false;}let mut clients = if let Some(client) = client.as_ref() {@@ -242,6 +242,9 @@ impl McpConfiguredClient {let mut reservations = reservations.lock().unwrap_or_else(std::sync::PoisonError::into_inner);+ if reservation.host_completion_pending.load(Ordering::Acquire) {+ return false;+ }if reservations.get(data_dir).is_some_and(|stored| Arc::ptr_eq(stored, &reservation))diff --git a/crates/openhuman-core/src/mcp/host.rs b/crates/openhuman-core/src/mcp/host.rsindex 52d3e482d2..1192e716a4 100644--- a/crates/openhuman-core/src/mcp/host.rs+++ b/crates/openhuman-core/src/mcp/host.rs@@ -10,7 +10,8 @@ use std::sync::OnceLock;use crate::config::Config;pub use super::module_client::agent_cleanup::{- spawn_agent_cleanup, take_agent_cleanup, AgentMcpCleanup,+ reserve_agent_directory, spawn_agent_cleanup, take_agent_cleanup, AgentDirectoryCleanup,+ AgentMcpCleanup,};const PROXY_SERVICE_KEY: &str = "tool.mcp_client";diff --git a/crates/openhuman-core/src/mcp/module_client.rs b/crates/openhuman-core/src/mcp/module_client.rsindex e739b0329e..1bc36f1eb1 100644--- a/crates/openhuman-core/src/mcp/module_client.rs+++ b/crates/openhuman-core/src/mcp/module_client.rs@@ -40,6 +40,7 @@ type CleanupAction = Arc<dyn Fn() -> bool + Send + Sync>;#[derive(Default)]struct CleanupReservation {after_close: Mutex<Option<CleanupAction>>,+ host_completion_pending: AtomicBool,}static CLEANUP_RESERVATIONS: OnceLock<Mutex<HashMap<PathBuf, Arc<CleanupReservation>>>> =diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup.rs b/crates/openhuman-embed/src/agent/mcp_cleanup.rsindex f7288703a2..2e09a2996d 100644--- a/crates/openhuman-embed/src/agent/mcp_cleanup.rs+++ b/crates/openhuman-embed/src/agent/mcp_cleanup.rs@@ -25,6 +25,7 @@ pub(crate) fn finish_removal(source,})?;}+ pending.directory.complete();agents.remove(id);agents.pending.remove(id);Ok(())@@ -169,6 +170,10 @@ impl AgentInner {.entry(self.id.clone()).or_insert_with(|| PendingCleanup {cleanup,+ directory: openhuman_core::mcp::host::reserve_agent_directory(+ &self.config.workspace_dir,+ &self.id,+ ),instance,home: self.layout.home.clone(),purge,diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rsindex 20cc5d2546..3c22a19f90 100644--- a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs+++ b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs@@ -3,6 +3,10 @@ use super::*;fn pending(home: std::path::PathBuf) -> PendingCleanup {PendingCleanup {cleanup: None,+ directory: openhuman_core::mcp::host::reserve_agent_directory(+ home.parent().unwrap(),+ home.file_name().unwrap().to_str().unwrap(),+ ),instance: Weak::new(),home,purge: true,diff --git a/crates/openhuman-embed/src/runtime/host_agents.rs b/crates/openhuman-embed/src/runtime/host_agents.rsindex 4938340dbc..0097c398a8 100644--- a/crates/openhuman-embed/src/runtime/host_agents.rs+++ b/crates/openhuman-embed/src/runtime/host_agents.rs@@ -22,6 +22,7 @@ pub(crate) type AgentMap = Mutex<AgentRegistry>;#[derive(Clone)]pub(crate) struct PendingCleanup {pub(crate) cleanup: Option<openhuman_core::mcp::host::AgentMcpCleanup>,+ pub(crate) directory: openhuman_core::mcp::host::AgentDirectoryCleanup,pub(crate) instance: Weak<AgentInner>,pub(crate) home: std::path::PathBuf,pub(crate) purge: bool,