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
fix(mcp): make reconnect supervisor restartable and shut down cleanly
Steven Enamakeld2d4d4ac61a2
diff --git a/crates/openhuman-core/src/mcp/mod.rs b/crates/openhuman-core/src/mcp/mod.rsindex 548a5bf3c6..98aebe9e12 100644--- a/crates/openhuman-core/src/mcp/mod.rs+++ b/crates/openhuman-core/src/mcp/mod.rs@@ -158,11 +158,34 @@ pub(crate) async fn refresh_configured_tool_cache(config: &crate::config::Config/// workspace.#[cfg(feature = "mcp")]fn spawn_reconnect_supervisor() {- static SUPERVISOR_SPAWNED: std::sync::Once = std::sync::Once::new();- SUPERVISOR_SPAWNED.call_once(|| {- tokio::spawn(async move {- registry::supervisor::run().await;- });+ static SUPERVISOR_SPAWNED: std::sync::atomic::AtomicBool =+ std::sync::atomic::AtomicBool::new(false);+ static SUPERVISOR_TASK: std::sync::OnceLock<tokio::sync::Mutex<Option<tokio::task::JoinHandle<()>>>> =+ std::sync::OnceLock::new();+ use std::sync::atomic::Ordering;++ if SUPERVISOR_SPAWNED.swap(true, Ordering::AcqRel) {+ return;+ }+ let task = tokio::spawn(async move {+ registry::supervisor::run().await;+ });+ let task_slot = SUPERVISOR_TASK.get_or_init(|| tokio::sync::Mutex::new(None));+ if let Ok(mut slot) = task_slot.try_lock() {+ *slot = Some(task);+ } else {+ task.abort();+ SUPERVISOR_SPAWNED.store(false, Ordering::Release);+ return;+ }+ crate::core::shutdown::register(|| async {+ if let Some(task) = SUPERVISOR_TASK.get() {+ if let Some(task) = task.lock().await.take() {+ task.abort();+ let _ = task.await;+ }+ }+ SUPERVISOR_SPAWNED.store(false, Ordering::Release);});}diff --git a/crates/openhuman-core/src/mcp/module_client_tests.rs b/crates/openhuman-core/src/mcp/module_client_tests.rsindex 4ab0e05b42..8025c9fc40 100644--- a/crates/openhuman-core/src/mcp/module_client_tests.rs+++ b/crates/openhuman-core/src/mcp/module_client_tests.rs@@ -76,6 +76,17 @@ impl McpModulePort for FakePort {args: Value,confidential: bool,) -> Result<Value, ModuleCallError> {+ if member == names::methods::DRAIN_SUPERVISOR_EVENTS {+ self.calls.lock().unwrap().push((+ object_path.to_owned(),+ member.to_owned(),+ args,+ confidential,+ ));+ self.call_started.notify_one();+ return serde_json::to_value(SupervisorBatch::default())+ .map_err(|_| ModuleCallError::ModuleFault);+ }self.calls.lock().unwrap().push((object_path.to_owned(),member.to_owned(),@@ -90,6 +101,32 @@ impl McpModulePort for FakePort {}}+#[tokio::test]+async fn supervisor_drain_only_uses_an_open_object_and_keeps_the_bounded_limit() {+ let port = FakePort::default();+ let calls = Arc::clone(&port.calls);+ let client = client(port);++ assert_eq!(client.drain_supervisor_events(256).await.unwrap(), None);+ assert!(calls.lock().unwrap().is_empty());++ client+ .call::<bool>(&configured("server"), "ConnectedOverview", ())+ .await+ .unwrap();+ let batch = client+ .drain_supervisor_events(256)+ .await+ .unwrap()+ .expect("open child is polled");+ assert!(batch.events.is_empty());+ let calls = calls.lock().unwrap();+ assert_eq!(calls[1].0, "/workspace/1");+ assert_eq!(calls[1].1, names::methods::DRAIN_SUPERVISOR_EVENTS);+ assert_eq!(calls[1].2, serde_json::json!([256]));+ assert!(!calls[1].3);+}+fn client(port: FakePort) -> McpConfiguredClient {McpConfiguredClient::with_port("/private/workspace".to_owned(), Box::new(port))}