Skip to content

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

feat(inbound): add route module for inbound message dispatch

Steven Enamakel1812eb41d55a
diff --git a/crates/openhuman-core/src/inbound/route.rs b/crates/openhuman-core/src/inbound/route.rs
index 8221e0b66b..2dfa4410aa 100644
--- a/crates/openhuman-core/src/inbound/route.rs
+++ b/crates/openhuman-core/src/inbound/route.rs
@@ -8,8 +8,21 @@
//! fails, the event is too large to queue), the caller runs its direct path,
//! exactly as it did before the queues existed. Nothing is dropped by the
//! hand-off itself.
+//!
+//! Every attempt has a deadline, and the first is long enough for the module's
+//! first load (a release download). A failure that proves the module never saw
+//! the event (unavailable, another contract) takes the direct path at once. A
+//! failure that does not (a timeout, a transport fault) sends the event again:
+//! `Ingest` is idempotent on `source:event_id`, so an event the module did
+//! persist comes back `Duplicate` rather than queued twice. Only when every
+//! attempt is ambiguous does the caller fall back to the direct path, and
+//! that one case can triage an event the queues also deliver; it is logged.
+//! Callers run the hand-off in their own task, never on a bus subscriber's
+//! loop.
+
+use std::time::Duration;
-use tinybus::module::ModuleClient;
+use tinybus::module::{ModuleCallError, ModuleClient};
use tinyeventqueues_bus::{Disposition, InboundEvent};
use crate::config::Config;
@@ -36,6 +49,37 @@ impl Offered {
}
}
+/// Deadlines of the successive `Ingest` attempts: the first covers the
+/// module's first load.
+const ATTEMPT_DEADLINES: [Duration; 3] = [
+ Duration::from_secs(60),
+ Duration::from_secs(10),
+ Duration::from_secs(10),
+];
+
+/// Pause before re-sending after an ambiguous failure.
+const RESEND_PAUSE: Duration = Duration::from_millis(500);
+
+/// How one `Ingest` attempt ended.
+enum Attempt {
+ Answered(tinyeventqueues_bus::IngestResponse),
+ /// The module never received the event.
+ Unsent(String),
+ /// The module may have persisted the event.
+ Unknown(String),
+}
+
+async fn attempt(client: &ModuleClient, event: &InboundEvent, deadline: Duration) -> Attempt {
+ match tokio::time::timeout(deadline, event_queues::ingest(client, vec![event.clone()])).await {
+ Ok(Ok(response)) => Attempt::Answered(response),
+ Ok(Err(error @ (ModuleCallError::Unavailable | ModuleCallError::IncompatibleContract))) => {
+ Attempt::Unsent(error.to_string())
+ }
+ Ok(Err(error)) => Attempt::Unknown(error.to_string()),
+ Err(_) => Attempt::Unknown(format!("no answer within {deadline:?}")),
+ }
+}
+
/// Offer `event` to the inbound event queues through `client`.
pub async fn offer(config: &Config, client: &ModuleClient, event: InboundEvent) -> Offered {
if !super::active(config) {
@@ -43,17 +87,46 @@ pub async fn offer(config: &Config, client: &ModuleClient, event: InboundEvent)
}
let source = event.source.clone();
let event_id = event.event_id.clone();
- let response = match event_queues::ingest(client, vec![event]).await {
- Ok(response) => response,
- Err(error) => {
- tracing::warn!(
- source = %source,
- event_id = %event_id,
- error = %error,
- "[inbound] ingest failed; taking the direct path"
- );
- return Offered::Direct(format!("ingest failed: {error}"));
+ let mut last = String::new();
+ let mut answered = None;
+ for (n, deadline) in ATTEMPT_DEADLINES.into_iter().enumerate() {
+ if n > 0 {
+ tokio::time::sleep(RESEND_PAUSE).await;
+ }
+ match attempt(client, &event, deadline).await {
+ Attempt::Answered(response) => {
+ answered = Some(response);
+ break;
+ }
+ Attempt::Unsent(error) => {
+ tracing::info!(
+ source = %source,
+ event_id = %event_id,
+ error = %error,
+ "[inbound] queues unavailable; taking the direct path"
+ );
+ return Offered::Direct(format!("ingest failed: {error}"));
+ }
+ Attempt::Unknown(error) => {
+ tracing::warn!(
+ source = %source,
+ event_id = %event_id,
+ attempt = n + 1,
+ error = %error,
+ "[inbound] ingest answer lost; sending again"
+ );
+ last = error;
+ }
}
+ }
+ let Some(response) = answered else {
+ tracing::warn!(
+ source = %source,
+ event_id = %event_id,
+ error = %last,
+ "[inbound] ingest never answered; taking the direct path (the queues may also deliver it)"
+ );
+ return Offered::Direct(format!("ingest failed: {last}"));
};
let Some(result) = response.results.into_iter().next() else {
tracing::warn!(source = %source, "[inbound] ingest answered no result; taking the direct path");