Skip to content

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

feat(inbound): add host-side delivery for inbound messages

Steven Enamakel8f55dc4c6bbe
diff --git a/crates/openhuman-core/src/inbound/deliver.rs b/crates/openhuman-core/src/inbound/deliver.rs
index 49556edc2e..453ccce8a7 100644
--- a/crates/openhuman-core/src/inbound/deliver.rs
+++ b/crates/openhuman-core/src/inbound/deliver.rs
@@ -138,7 +138,8 @@ enum Applied {
pub struct Deliverer {
runner: Arc<dyn TriageRunner>,
ctx: DispatchCtx,
- timeout: Duration,
+ /// The triage timeout: given, or read from `[inbound]` on first use.
+ timeout: tokio::sync::OnceCell<Duration>,
now_ms: fn() -> i64,
max_attempts: Box<dyn Fn(Lane) -> Option<u32> + Send + Sync>,
budget: Duration,
@@ -149,7 +150,7 @@ pub struct Deliverer {
impl std::fmt::Debug for Deliverer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Deliverer")
- .field("timeout", &self.timeout)
+ .field("timeout", &self.timeout.get())
.finish_non_exhaustive()
}
}
@@ -163,6 +164,23 @@ impl Deliverer {
/// `timeout`, and reads each lane's attempts from the configured queues.
#[must_use]
pub fn new(runner: Arc<dyn TriageRunner>, ctx: DispatchCtx, timeout: Duration) -> Self {
+ Self::build(runner, ctx, tokio::sync::OnceCell::new_with(Some(timeout)))
+ }
+
+ /// A deliverer whose triage timeout is `[inbound] triage_timeout_ms`, read
+ /// from `ctx`'s configuration on the first callback. Loading it when the
+ /// callbacks are served instead would initialise a config file on a host
+ /// that never configured one.
+ #[must_use]
+ pub fn configured(runner: Arc<dyn TriageRunner>, ctx: DispatchCtx) -> Self {
+ Self::build(runner, ctx, tokio::sync::OnceCell::new())
+ }
+
+ fn build(
+ runner: Arc<dyn TriageRunner>,
+ ctx: DispatchCtx,
+ timeout: tokio::sync::OnceCell<Duration>,
+ ) -> Self {
Self {
runner,
ctx,
@@ -199,6 +217,22 @@ impl Deliverer {
self
}
+ /// The triage timeout, read from `[inbound]` on first use when not given.
+ async fn timeout(&self) -> Duration {
+ *self
+ .timeout
+ .get_or_init(|| async {
+ match self.ctx.load_config().await {
+ Ok(config) => config.inbound.triage_timeout(),
+ Err(error) => {
+ tracing::debug!(error = %error, "[inbound] config unavailable; default triage timeout");
+ crate::config::schema::InboundConfig::default().triage_timeout()
+ }
+ }
+ })
+ .await
+ }
+
fn classified(&self) -> std::sync::MutexGuard<'_, Classified> {
self.classified
.lock()
@@ -223,7 +257,8 @@ impl Deliverer {
) -> Result<ClassifySalienceResponse, String> {
let event = request.event;
let envelope = envelope_of(&event).ok_or("not an inbound trigger")?;
- match tokio::time::timeout(self.timeout, self.runner.run(&self.ctx, &envelope)).await {
+ let timeout = self.timeout().await;
+ match tokio::time::timeout(timeout, self.runner.run(&self.ctx, &envelope)).await {
Ok(Ok(TriageOutcome::Decision(run))) => {
let class = class_of(run.decision.action);
tracing::debug!(
@@ -272,7 +307,10 @@ impl Deliverer {
.and_then(|event| self.classified().take(&event.dedupe_key(), now_ms));
let outcome = match cached {
Some(run) => Ok(Ok(TriageOutcome::Decision(run))),
- None => tokio::time::timeout(self.timeout, self.runner.run(&self.ctx, &envelope)).await,
+ None => {
+ let timeout = self.timeout().await;
+ tokio::time::timeout(timeout, self.runner.run(&self.ctx, &envelope)).await
+ }
};
let status = match outcome {
Ok(Ok(TriageOutcome::Decision(run))) => {
@@ -344,7 +382,7 @@ impl Deliverer {
tracing::warn!(
turn = %turn.turn_id,
label = %envelope.display_label,
- timeout_ms = self.timeout.as_millis() as u64,
+ timeout_ms = self.timeout.get().map_or(0, |t| t.as_millis() as u64),
"[inbound] triage timed out; retrying"
);
self.retry(&turn, None, "triage timed out")
diff --git a/crates/openhuman-core/src/inbound/deliver_tests.rs b/crates/openhuman-core/src/inbound/deliver_tests.rs
index 76fb423f11..8c161d9f4e 100644
--- a/crates/openhuman-core/src/inbound/deliver_tests.rs
+++ b/crates/openhuman-core/src/inbound/deliver_tests.rs
@@ -10,6 +10,7 @@ use tinyeventqueues_bus::{EventKind, Sender};
use super::*;
use crate::inbound::events;
use crate::inbound::fixture::{decision, decision_outcome, deferred, ScriptedTriage, Step};
+use crate::config::schema::inbound::MAX_TRIAGE_TIMEOUT_MS;
use crate::triage::TriggerEnvelope;
const NOW_MS: i64 = 1_000_000;
@@ -319,3 +320,18 @@ async fn a_long_apply_is_acknowledged_at_the_budget_and_finishes_on_its_own() {
let applied = triage.until_applied(1, Duration::from_secs(5)).await;
assert_eq!(applied.len(), 1);
}
+
+#[tokio::test]
+async fn a_configured_deliverer_reads_the_triage_timeout_on_first_use() {
+ let triage = ScriptedTriage::new([decision(TriageAction::React)]);
+ let deliverer = Deliverer::configured(
+ triage.clone() as Arc<dyn TriageRunner>,
+ DispatchCtx::host_default(),
+ );
+ assert!(format!("{deliverer:?}").contains("None"), "read before use");
+ let response = deliverer
+ .deliver(turn(vec![trigger_event("u1")], Lane::Background, 1))
+ .await;
+ assert_eq!(response.status, DeliverStatus::Accepted);
+ assert!(deliverer.timeout().await <= Duration::from_millis(MAX_TRIAGE_TIMEOUT_MS));
+}
diff --git a/crates/openhuman-core/src/inbound/host.rs b/crates/openhuman-core/src/inbound/host.rs
index 27ea51cdf9..311b22dbbb 100644
--- a/crates/openhuman-core/src/inbound/host.rs
+++ b/crates/openhuman-core/src/inbound/host.rs
@@ -56,16 +56,9 @@ impl InboundHost {
/// Serve the callbacks on `connection`, triaging under the host's own context.
pub(crate) async fn install(connection: &tinybus::Connection) -> tinybus::Result<()> {
let ctx = DispatchCtx::host_default();
- // Read once, when the callbacks are served: a change takes effect on the
- // next core start.
- let timeout = match ctx.load_config().await {
- Ok(config) => config.inbound.triage_timeout(),
- Err(error) => {
- tracing::debug!(error = %error, "[inbound] config unavailable; default triage timeout");
- crate::config::schema::InboundConfig::default().triage_timeout()
- }
- };
- let deliverer = Deliverer::new(Arc::new(RemoteTriage), ctx, timeout);
+ // The triage timeout is read from `[inbound]` on the first callback:
+ // loading the config here would initialise one on a host that has none.
+ let deliverer = Deliverer::configured(Arc::new(RemoteTriage), ctx);
connection
.serve_at(
ObjectPath::new(PATH)?,
diff --git a/crates/openhuman-core/src/inbound/host_tests.rs b/crates/openhuman-core/src/inbound/host_tests.rs
index bdc6230647..530bf7bfe2 100644
--- a/crates/openhuman-core/src/inbound/host_tests.rs
+++ b/crates/openhuman-core/src/inbound/host_tests.rs
@@ -56,8 +56,6 @@ async fn deliver_and_classify_go_to_the_deliverer() {
#[tokio::test]
async fn install_serves_the_callbacks_under_the_contract_name() {
- // `install` loads the host config; hold the env lock the config tests use.
- let _env = crate::config::TEST_ENV_LOCK.lock().await;
let bus = tinybus::transport::memory::MemoryBus::new();
tinybus::broker::Broker::new().spawn(bus.clone());
let connection = tinybus::Connection::connect(bus.connect().await.unwrap())