Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(runtime): consume tinychannels supervisor

Steven Enamakel0238bb74a244
diff --git a/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs b/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs
index a63810dd40..76478377fa 100644
--- a/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs
+++ b/crates/openhuman-core/src/channels/providers/telegram/remote_control.rs
@@ -6,12 +6,12 @@ use crate::channels::context::{
};
use crate::channels::traits::ChannelMessage;
use crate::memory::conversations::{self as conversations, CreateConversationThread};
+pub use tinychannels::providers::telegram::parse_telegram_remote_command;
pub use tinychannels::providers::telegram::TelegramRemoteCommand;
use tinychannels::providers::telegram::{
build_new_session_response as render_new_session_response, build_remote_help_response,
build_status_response as render_status_response, format_session_line, SESSIONS_LIST_LIMIT,
};
-pub use tinychannels::providers::telegram::parse_telegram_remote_command;
const LOG_PREFIX: &str = "[telegram-remote]";
diff --git a/crates/openhuman-core/src/channels/runtime/supervision.rs b/crates/openhuman-core/src/channels/runtime/supervision.rs
index 4b6f650692..707d40a51b 100644
--- a/crates/openhuman-core/src/channels/runtime/supervision.rs
+++ b/crates/openhuman-core/src/channels/runtime/supervision.rs
@@ -1,125 +1,67 @@
-//! Supervisor helpers for channel listeners.
+//! OpenHuman observer adapter for TinyChannels listener supervision.
use super::super::traits;
use super::super::Channel;
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
use std::sync::Arc;
-use std::time::Duration;
+use tinychannels::runtime::ListenerObserver;
-pub(crate) use tinychannels::runtime::compute_max_in_flight_messages;
+/// Publishes reusable listener lifecycle facts onto OpenHuman's domain bus.
+struct OpenHumanListenerObserver;
-/// Upper bound on reconnect jitter, regardless of how large `backoff` has grown.
-/// One second of spread is plenty to de-correlate lockstep reconnects while
-/// staying inside Discord's 1-5s post-op9 window on the low backoff steps.
-const MAX_JITTER_MS: u64 = 1_000;
+impl ListenerObserver for OpenHumanListenerObserver {
+ fn connected(&self, channel: &str) {
+ BUS.publish(DomainEvent::ChannelConnected {
+ channel: channel.to_string(),
+ });
+ }
+
+ fn disconnected(&self, channel: &str, reason: &str, failed: bool) {
+ if failed {
+ let message = format!("Channel {channel} error: {reason}; restarting");
+ crate::core::observability::report_error_or_expected(
+ &message,
+ "channels",
+ "supervised_listener",
+ &[("channel", channel)],
+ );
+ } else {
+ tracing::warn!("Channel {channel} exited unexpectedly; restarting");
+ }
+ BUS.publish(DomainEvent::ChannelDisconnected {
+ channel: channel.to_string(),
+ reason: reason.to_string(),
+ });
+ }
-/// Randomized delay to add on top of the base backoff before a reconnect, in
-/// millis. Full-jitter over `[0, backoff_secs * 1000)` ms, clamped to
-/// [`MAX_JITTER_MS`] so a large backoff doesn't produce an unboundedly long
-/// extra wait. Returns `0` when there is no window to sample (defensive — the
-/// supervisor's `backoff` is always `>= 1`, so this only guards misuse).
-///
-/// Uses the crate-wide `rand::rng()` CSPRNG idiom (rand 0.10); the window math
-/// is deterministic so the bound is unit-testable independent of the sample.
-fn jitter_millis(backoff_secs: u64) -> u64 {
- use rand::RngExt as _;
- let window_ms = backoff_secs.saturating_mul(1_000).min(MAX_JITTER_MS);
- if window_ms == 0 {
- return 0;
+ fn restarted(&self, channel: &str) {
+ BUS.publish(DomainEvent::HealthRestarted {
+ component: format!("channel:{channel}"),
+ });
}
- rand::rng().random_range(0..window_ms)
}
+/// Start a TinyChannels listener with OpenHuman observability attached.
pub(crate) fn spawn_supervised_listener(
- ch: Arc<dyn Channel>,
+ channel: Arc<dyn Channel>,
tx: tokio::sync::mpsc::Sender<traits::ChannelMessage>,
initial_backoff_secs: u64,
max_backoff_secs: u64,
) -> tokio::task::JoinHandle<()> {
- // Health events need a live bus, but standing one up is async now and this
- // helper is not. Registering the subscriber is still sync and still safe
- // before init; the bus itself is initialised once at startup, and a publish
- // that lands before that is a documented no-op rather than a panic.
crate::platform::health::bus::register_health_subscriber();
-
- tokio::spawn(async move {
- let component = format!("channel:{}", ch.name());
- let mut backoff = initial_backoff_secs.max(1);
- let max_backoff = max_backoff_secs.max(backoff);
-
- tracing::info!(
- channel = ch.name(),
- initial_backoff_secs,
- max_backoff_secs,
- "[channels] supervised listener started"
- );
-
- loop {
- BUS.publish(DomainEvent::ChannelConnected {
- channel: ch.name().to_string(),
- });
- tracing::debug!(
- channel = ch.name(),
- "[channels] listener entering recv loop"
- );
- let result = ch.listen(tx.clone()).await;
-
- if tx.is_closed() {
- break;
- }
-
- match result {
- Ok(()) => {
- // A supervisor asked to stop closes `tx`, which is caught by
- // the `tx.is_closed()` break above — so any `Ok(())` that
- // reaches here is a reconnect-style exit, not an intentional
- // clean shutdown. Discord's gateway returns `Ok(())` on op7
- // (reconnect), op9 (invalid session), and Close frames; a
- // reconnect/invalid-session storm therefore lands here
- // repeatedly. Resetting backoff on this path made it spin at
- // the flat initial delay forever, hammering Discord IDENTIFY
- // and pinning the CPU (#5350). Fall through to the shared
- // escalation below instead — the only difference from `Err`
- // is that this exit is expected and not reported.
- tracing::warn!("Channel {} exited unexpectedly; restarting", ch.name());
- BUS.publish(DomainEvent::ChannelDisconnected {
- channel: ch.name().to_string(),
- reason: "exited unexpectedly".to_string(),
- });
- }
- Err(e) => {
- let message = format!("Channel {} error: {e:#}; restarting", ch.name());
- crate::core::observability::report_error_or_expected(
- message.as_str(),
- "channels",
- "supervised_listener",
- &[("channel", ch.name())],
- );
- BUS.publish(DomainEvent::ChannelDisconnected {
- channel: ch.name().to_string(),
- reason: e.to_string(),
- });
- }
- }
-
- BUS.publish(DomainEvent::HealthRestarted {
- component: component.clone(),
- });
- // Full-jitter on top of the base backoff so many channels/instances
- // don't reconnect in lockstep (Discord asks for a randomized 1-5s
- // wait after op9 before re-IDENTIFY, and lockstep reconnects across
- // channels amplify IDENTIFY rate-limiting). Jitter spans [0, backoff)
- // seconds, expressed in millis, capped so it never dwarfs the base.
- let jitter_ms = jitter_millis(backoff);
- tokio::time::sleep(Duration::from_secs(backoff) + Duration::from_millis(jitter_ms))
- .await;
- // Double backoff AFTER sleeping so the first restart uses initial_backoff
- backoff = backoff.saturating_mul(2).min(max_backoff);
- }
- })
+ tinychannels::runtime::spawn_supervised_listener(
+ channel,
+ tx,
+ initial_backoff_secs,
+ max_backoff_secs,
+ Arc::new(OpenHumanListenerObserver),
+ )
}
+#[cfg(test)]
+pub(crate) use tinychannels::runtime::{jitter_millis, MAX_JITTER_MS};
+
#[cfg(test)]
#[path = "supervision_tests.rs"]
mod tests;
diff --git a/vendor/tinychannels b/vendor/tinychannels
index fef8fe1667..e211100fea 160000
--- a/vendor/tinychannels
+++ b/vendor/tinychannels
@@ -1 +1 @@
-Subproject commit fef8fe16673a6e3cf08dd325fe18c4b4ceb4367a
+Subproject commit e211100feafbe4ca5e7e0538038b919cd8cc87ec