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
refactor(agent): extract cleanup retry loop into a helper
Steven Enamakelc0244a7b042a
diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup.rs b/crates/openhuman-embed/src/agent/mcp_cleanup.rsindex 8fb0244e1e..24bffa48fb 100644--- a/crates/openhuman-embed/src/agent/mcp_cleanup.rs+++ b/crates/openhuman-embed/src/agent/mcp_cleanup.rs@@ -57,28 +57,20 @@ pub(crate) fn defer_pending(let running = Arc::clone(&pending.retry_running);let executor_failed = Arc::clone(&running);let future = async move {- let mut guard = Some(guard);- for attempt in 0..3 {- let result = match &pending.cleanup {- Some(cleanup) => cleanup.finish().await.map_err(AgentError::McpCleanup),- None => Ok(()),- };- let result = result.and_then(|()| {- complete_with_guard(&mut guard, || finish_removal(®istry, &id, &pending))- });- match result {- Ok(()) => {- running.store(false, Ordering::Release);- return;- }- Err(error) => {- log::warn!("[embed][agent] captured cleanup remains pending: {error}")+ retry_cleanup(+ guard,+ || {+ let cleanup = pending.cleanup.clone();+ async move {+ match cleanup {+ Some(cleanup) => cleanup.finish().await.map_err(AgentError::McpCleanup),+ None => Ok(()),+ }}- }- if attempt < 2 {- tokio::time::sleep(std::time::Duration::from_millis(250 * (1 << attempt))).await;- }- }+ },+ || finish_removal(®istry, &id, &pending),+ )+ .await;// The original receipt, id reservation and purge intent stay in the// registry. A later remove_agent call retries them explicitly.running.store(false, Ordering::Release);@@ -89,6 +81,30 @@ pub(crate) fn defer_pending(}}+async fn retry_cleanup<F, Fut, C>(+ guard: Arc<crate::runtime::CoreGuard>,+ mut close: F,+ mut complete: C,+) where+ F: FnMut() -> Fut,+ Fut: std::future::Future<Output = Result<(), AgentError>>,+ C: FnMut() -> Result<(), AgentError>,+{+ let mut guard = Some(guard);+ for attempt in 0..3 {+ let result = close()+ .await+ .and_then(|()| complete_with_guard(&mut guard, &mut complete));+ match result {+ Ok(()) => return,+ Err(error) => log::warn!("[embed][agent] captured cleanup remains pending: {error}"),+ }+ if attempt < 2 {+ tokio::time::sleep(std::time::Duration::from_millis(250 * (1 << attempt))).await;+ }+ }+}+fn complete_with_guard<T>(guard: &mut Option<Arc<crate::runtime::CoreGuard>>,complete: impl FnOnce() -> T,diff --git a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rsindex 92e7924b34..68ccb92827 100644--- a/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs+++ b/crates/openhuman-embed/src/agent/mcp_cleanup_tests.rs@@ -118,3 +118,58 @@ fn the_last_runtime_guard_keeps_admission_reserved_through_cleanup_completion().unwrap();});}++#[test]+fn exhausted_cleanup_releases_runtime_admission_but_preserves_the_ephemeral_workspace() {+ 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)+ .build()+ .await+ .unwrap();+ let root = runtime.workspace_dir().to_path_buf();+ let home = root.join("agents/unresolved");+ std::fs::create_dir_all(&home).unwrap();+ std::fs::write(home.join("module-state"), "must survive unconfirmed close").unwrap();+ let guard = runtime.guard();+ let weak = Arc::downgrade(&guard);+ drop(runtime);+ let attempts = std::sync::atomic::AtomicUsize::new(0);+ retry_cleanup(+ guard,+ || {+ attempts.fetch_add(1, Ordering::SeqCst);+ async {+ Err(AgentError::McpCleanup(+ openhuman_core::modules::client::ModuleCallError::ModuleFault,+ ))+ }+ },+ || panic!("unconfirmed close must never purge"),+ )+ .await;+ let home_was_preserved = home.join("module-state").exists();+ let _ = std::fs::remove_dir_all(&root);+ assert_eq!(attempts.load(Ordering::SeqCst), 3);+ assert!(+ weak.upgrade().is_none(),+ "exhaustion must release the process guard"+ );+ assert!(!crate::runtime::RUNTIME_LIVE.load(Ordering::Acquire));+ assert!(+ home_was_preserved,+ "failed cleanup must preserve the ephemeral workspace after the last guard drops"+ );+ })+ .await+ .unwrap();+ });+}