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
chore: files changed crates/openhuman-core/src/modules/runtime.rs,crates/openhuman-core/src/modules/
Steven Enamakel17a6af60e7b6
diff --git a/crates/openhuman-core/src/modules/runtime.rs b/crates/openhuman-core/src/modules/runtime.rsindex 796fcb66b8..036bc10703 100644--- a/crates/openhuman-core/src/modules/runtime.rs+++ b/crates/openhuman-core/src/modules/runtime.rs@@ -33,6 +33,7 @@//! Everything here is created once and lives for the process. There is no//! shutdown path because there is nothing a shutdown could reclaim.+use std::future::Future;use std::sync::OnceLock;use tinybus::module::{ModuleRuntime, RuntimeNames};@@ -40,8 +41,50 @@ use tinybus::module::{ModuleRuntime, RuntimeNames};/// The module bus, built once on first use.static RUNTIME: OnceLock<Result<ModuleRuntime, String>> = OnceLock::new();+/// A process-owned callback installation and its shared result.+#[derive(Default)]+struct CallbackSetup {+ result: OnceLock<tokio::sync::watch::Receiver<Option<Result<(), String>>>>,+}++impl CallbackSetup {+ fn start<F>(+ &self,+ runtime: &ModuleRuntime,+ setup: F,+ ) -> tokio::sync::watch::Receiver<Option<Result<(), String>>>+ where+ F: Future<Output = tinybus::Result<()>> + Send + 'static,+ {+ self.result+ .get_or_init(|| {+ let (result_tx, result_rx) = tokio::sync::watch::channel(None);+ runtime.spawn(async move {+ result_tx.send_replace(Some(setup.await.map_err(|error| error.to_string())));+ });+ result_rx+ })+ .clone()+ }++ async fn wait(+ mut result: tokio::sync::watch::Receiver<Option<Result<(), String>>>,+ ) -> Result<(), String> {+ loop {+ if let Some(result) = result.borrow_and_update().clone() {+ return result;+ }+ if result.changed().await.is_err() {+ return Err("the module callbacks did not finish".to_string());+ }+ }+ }+}+/// Host callbacks installed on the runtime's bus, once.-static CALLBACKS: tokio::sync::OnceCell<Result<(), String>> = tokio::sync::OnceCell::const_new();+static CALLBACKS: CallbackSetup = CallbackSetup {+ result: OnceLock::new(),+};/// The process-wide module runtime, standing it up on first use.///@@ -61,19 +104,12 @@ pub async fn runtime() -> tinybus::Result<&'static ModuleRuntime> {let runtime = started.as_ref().map_err(|error| tinybus::Error::Transport(error.clone()))?;- CALLBACKS- .get_or_init(|| async {- // On the module runtime, not the caller's: the callbacks serve- // objects that must outlive whichever runtime asked first.- match runtime.spawn(install_callbacks(runtime)).await {- Ok(Ok(())) => Ok(()),- Ok(Err(error)) => Err(error.to_string()),- Err(error) => Err(format!("the module callbacks did not finish: {error}")),- }- })+ // Start installation once on the process-lifetime module runtime. Callers+ // only wait on a watch receiver, so cancelling one caller cannot drop the+ // OnceCell initializer and start a second installation while this task runs.+ CallbackSetup::wait(CALLBACKS.start(runtime, install_callbacks(runtime))).await- .as_ref()- .map_err(|error| tinybus::Error::Transport(error.clone()))?;+ .map_err(tinybus::Error::Transport)?;Ok(runtime)}diff --git a/crates/openhuman-core/src/modules/runtime_tests.rs b/crates/openhuman-core/src/modules/runtime_tests.rsindex e7681b075b..2347d0a638 100644--- a/crates/openhuman-core/src/modules/runtime_tests.rs+++ b/crates/openhuman-core/src/modules/runtime_tests.rs@@ -1,4 +1,42 @@use super::{is_started, runtime};+use std::sync::atomic::{AtomicUsize, Ordering};+use std::sync::Arc;++/// Cancelling a runtime caller must not restart process-lifetime callback setup.+#[tokio::test]+async fn cancelling_a_callback_waiter_does_not_restart_callback_setup() {+ let names = tinybus::module::RuntimeNames {+ thread: "openhuman-callback-setup-test".to_string(),+ worker: "openhuman-callback-worker-test".to_string(),+ };+ let module_runtime = tinybus::module::ModuleRuntime::start(&names, None)+ .expect("test module runtime should start");+ let setup = super::CallbackSetup::default();+ let calls = Arc::new(AtomicUsize::new(0));+ let started = Arc::new(tokio::sync::Notify::new());+ let release = Arc::new(tokio::sync::Notify::new());+ let setup_calls = calls.clone();+ let setup_started = started.clone();+ let setup_release = release.clone();+ let result = setup.start(&module_runtime, async move {+ setup_calls.fetch_add(1, Ordering::SeqCst);+ setup_started.notify_one();+ setup_release.notified().await;+ Ok(())+ });++ let abandoned_waiter = {+ let result = result.clone();+ tokio::spawn(async move { super::CallbackSetup::wait(result).await })+ };+ started.notified().await;+ abandoned_waiter.abort();+ let _ = abandoned_waiter.await;+ release.notify_one();++ assert_eq!(super::CallbackSetup::wait(result).await, Ok(()));+ assert_eq!(calls.load(Ordering::SeqCst), 1);+}/// The process-global runtime is stable within one caller runtime.#[tokio::test]