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(channels): delegate channel startup to module runtime
Steven Enamakelb7b54401e592
diff --git a/crates/openhuman-core/src/channels/runtime/startup/start_channels.rs b/crates/openhuman-core/src/channels/runtime/startup/start_channels.rsindex 0beabbca64..b04d9d6539 100644--- a/crates/openhuman-core/src/channels/runtime/startup/start_channels.rs+++ b/crates/openhuman-core/src/channels/runtime/startup/start_channels.rs@@ -1,25 +1,16 @@//! The channel runtime's entry point: wiring subscribers, providers, the//! system prompt, and the message dispatch loop.-use super::super::dispatch::{run_message_dispatch_loop, RuntimeChannelMessage};-use super::super::supervision::spawn_supervised_listener;-use super::credentials::{hydrate_channel_credentials, RuntimeProxyClients};-use super::relay::start_relay_runtime;+use super::super::dispatch::run_message_dispatch_loop;+use super::module_providers::ModuleChannelRuntime;use super::turn_parts::{build_channel_turn_parts, runtime_context, PromptToolDescs};-use crate::channels::context::{- DEFAULT_CHANNEL_INITIAL_BACKOFF_SECS, DEFAULT_CHANNEL_MAX_BACKOFF_SECS,-};-use crate::channels::traits;use crate::config::Config;use crate::core::bus::BUS;use crate::core::events::DomainEvent;use crate::security::SecurityPolicy;use anyhow::Result;-use std::collections::HashMap;-use std::sync::Arc;-use tinychannels::runtime::compute_max_in_flight_messages;-use tinychannels_runtime::config::RelayRuntimeConfigExt as _;-use tokio_util::task::AbortOnDropHandle;+use std::sync::{Arc, OnceLock, Weak};+use tokio_util::sync::CancellationToken;pub async fn start_channels(config: Config) -> Result<()> {start_channels_with_session(config, super::super::session::channel_session()).await@@ -29,10 +20,13 @@ pub(crate) async fn start_channels_with_session(config: Config,session: tokio_util::sync::CancellationToken,) -> Result<()> {- super::super::session::run_in_session(session, start_channels_inner(config)).await+ start_channels_inner(config, session).await}-async fn start_channels_inner(mut config: Config) -> Result<()> {+async fn start_channels_inner(mut config: Config, session: CancellationToken) -> Result<()> {+ if session.is_cancelled() {+ return Ok(());+ }// Initialize the global event bus singleton and register the tracing// subscriber for debug logging of all domain events.crate::core::bus::init().await.expect("bus init");@@ -149,38 +143,34 @@ async fn start_channels_inner(mut config: Config) -> Result<()> {);}- // Assemble the ChannelHost capability surface (shutdown, STT/TTS, reaction- // gate, approvals, conversation store, event sink). Ported rich providers- // reach host capabilities through this instead of calling core internals.- let channel_host = crate::channels::host::build_channel_host(Arc::new(config.clone()));-- // Provider construction lives in `tinychannels::factory` so that this host- // and the `tinychannels-module` cdylib build the same providers from the- // same config. It used to be ~200 lines inline here; a second copy is how- // the two drift, and only one of them would have been the one under test.- //- // Three things stay on this side because they are host policy, and the- // factory's docs say so explicitly:- // - credential hydration (below) reads OpenHuman's keyring,- // - `RuntimeProxyClients` applies the configured HTTP proxy,- // - `channel_host` is the capability surface assembled just above.- let channels = tinychannels::build_channels(- &hydrate_channel_credentials(&config),- &channel_host,- &RuntimeProxyClients,- );-- let relay_config = config- .channels_config- .relay- .clone()- .filter(tinychannels::config::RelayRuntimeConfig::is_listener_configured);-- if channels.is_empty() && relay_config.is_none() {+ let configured = crate::channels::commands::configured_providers(&config);+ let relay_configured = config.channels_config.relay.as_ref().is_some_and(|relay| {+ !relay.url.is_empty() && !relay.identities.is_empty()+ });+ if configured.is_empty() && !relay_configured {println!("No channels configured. Set up channels in the web UI.");return Ok(());}+ // The module runtime captures one config identity and host callback object.+ // A weak context cell is filled once the turn context is ready, avoiding a+ // callback -> context -> channel -> lease -> callback reference cycle.+ let (dispatch_tx, rx) = tokio::sync::mpsc::channel(100);+ let context_slot: Arc<OnceLock<Weak<crate::channels::context::ChannelRuntimeContext>>> =+ Arc::new(OnceLock::new());+ let mut module_runtime = tokio::select! {+ biased;+ _ = session.cancelled() => return Ok(()),+ prepared = ModuleChannelRuntime::prepare(Arc::new(config.clone()), Arc::clone(&context_slot), dispatch_tx.clone()) => {+ Some(prepared?)+ }+ };+ let channels_by_name = module_runtime+ .as_ref()+ .expect("runtime prepared when providers are configured")+ .channels();+ let channels = channels_by_name.values().cloned().collect::<Vec<_>>();+println!("🦀 OpenHuman Channel Server");println!(" 🤖 Model: {model}");println!(@@ -196,7 +186,7 @@ async fn start_channels_inner(mut config: Config) -> Result<()> {channels.iter().map(|c| c.name())- .chain(relay_config.as_ref().map(|_| "relay"))+ .chain(relay_configured.then_some("relay")).collect::<Vec<_>>().join(", "));@@ -208,62 +198,6 @@ async fn start_channels_inner(mut config: Config) -> Result<()> {component: "channels".into(),});- let initial_backoff_secs = config- .reliability- .channel_initial_backoff_secs- .max(DEFAULT_CHANNEL_INITIAL_BACKOFF_SECS);- let max_backoff_secs = config- .reliability- .channel_max_backoff_secs- .max(DEFAULT_CHANNEL_MAX_BACKOFF_SECS);-- // Providers still publish legacy `ChannelMessage`s through the public- // channel trait. The runtime dispatch queue wraps those messages so relay- // inbound can carry its original TinyChannels envelope through processing.- let (provider_tx, mut provider_rx) = tokio::sync::mpsc::channel::<traits::ChannelMessage>(100);- let (dispatch_tx, rx) = tokio::sync::mpsc::channel::<RuntimeChannelMessage>(100);- let provider_dispatch_tx = dispatch_tx.clone();- let provider_bridge = AbortOnDropHandle::new(tokio::spawn(async move {- while let Some(msg) = provider_rx.recv().await {- if provider_dispatch_tx- .send(RuntimeChannelMessage::from(msg))- .await- .is_err()- {- break;- }- }- }));-- let mut relay_handles = Vec::new();- if let Some(ref relay) = relay_config {- match start_relay_runtime(relay, dispatch_tx.clone()).await {- Ok(handle) => relay_handles.push(handle),- Err(error) => {- tracing::warn!("[channels][relay] failed to start relay runtime: {error}")- }- }- }-- // Spawn a listener for each channel- let mut handles = Vec::new();- for ch in &channels {- handles.push(AbortOnDropHandle::new(spawn_supervised_listener(- ch.clone(),- provider_tx.clone(),- initial_backoff_secs,- max_backoff_secs,- )));- }- drop(provider_tx); // Drop our copy so provider_rx closes when all channels stop.- drop(dispatch_tx); // Drop startup's copy; relay/bridge clones keep dispatch alive.-- let channels_by_name = Arc::new(- channels- .iter()- .map(|ch| (ch.name().to_string(), Arc::clone(ch)))- .collect::<HashMap<_, _>>(),- );// Register the cron delivery subscriber so cron jobs can deliver output// to channels via events instead of directly constructing channel instances.let _cron_delivery_handle = bus.subscribe(Arc::new(@@ -307,7 +241,10 @@ async fn start_channels_inner(mut config: Config) -> Result<()> {println!(" 🚦 In-flight message limit: {max_in_flight_messages}");- let runtime_ctx = Arc::new(runtime_context(&config, parts, channels_by_name));+ let runtime_ctx = Arc::new(runtime_context(&config, parts, Arc::clone(&channels_by_name)));+ context_slot+ .set(Arc::downgrade(&runtime_ctx))+ .map_err(|_| anyhow::anyhow!("channel callback context was already initialized"))?;// Let cron jobs created from a channel chat read that chat's history, send// into it and append their replies to it.crate::cron::channel_bridge::register_channel_bridge(@@ -316,13 +253,24 @@ async fn start_channels_inner(mut config: Config) -> Result<()> {crate::channels::context::MAX_CHANNEL_HISTORY,);- run_message_dispatch_loop(rx, runtime_ctx, max_in_flight_messages).await;+ let startup = module_runtime+ .as_mut()+ .expect("runtime prepared when providers are configured")+ .start(&config);+ tokio::select! {+ biased;+ _ = session.cancelled() => {},+ result = startup => result?,+ }- // Wait for all channel tasks- for h in handles {- let _ = h.await;+ tokio::select! {+ biased;+ _ = session.cancelled() => {},+ _ = run_message_dispatch_loop(rx, runtime_ctx, max_in_flight_messages) => {},+ }+ if let Some(runtime) = module_runtime.as_mut() {+ runtime.close().await?;}- let _ = provider_bridge.await;Ok(())}