Skip to content

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

fix(inbound): a pinned source's events in a mixed turn still run its agent

M3gA-Mind5b1f889d0ee4
diff --git a/crates/openhuman-core/src/inbound/deliver.rs b/crates/openhuman-core/src/inbound/deliver.rs
index d43cf5cd5f..9bd3d37c18 100644
--- a/crates/openhuman-core/src/inbound/deliver.rs
+++ b/crates/openhuman-core/src/inbound/deliver.rs
@@ -33,13 +33,16 @@
//! A turn of a source configured under `[inbound.sources.*]` skips triage
//! altogether: it runs on the source's pinned agent ([`super::pinned`]), and
//! `ClassifySalience` answers `actionable` for its events without triaging.
+//! A turn mixing sources (a digest that collected a spilled burst) is split:
+//! each pinned source's events run on its agent, at most `max_batch` a turn,
+//! under ids derived from the turn's, and only the rest is triaged.
//!
//! One decision per turn: the triage timeout stays under the module's
//! callback deadline (`[inbound] triage_timeout_ms`, at most 25 s), and a
//! turn that was already accepted is accepted again without triage, so a
//! redelivery that raced a slow answer never applies a second decision.
-use std::collections::{HashMap, VecDeque};
+use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::{Arc, Mutex};
use std::time::Duration;
@@ -296,6 +299,47 @@ impl Deliverer {
Some((first.source.clone(), source))
}
+ /// `turn`'s events of pinned sources, as turns of at most the source's
+ /// `max_batch` events with ids derived from the turn's, and the turn of
+ /// the other events (`None` when there are none). A redelivery derives
+ /// the same turns: each source's events are ordered by id.
+ async fn split_pinned(
+ &self,
+ turn: &Turn,
+ ) -> (Vec<(Turn, String, InboundSourceConfig)>, Option<Turn>) {
+ let inbound = self.inbound().await;
+ let mut pinned: BTreeMap<String, (InboundSourceConfig, Vec<InboundEvent>)> =
+ BTreeMap::new();
+ let mut rest = Vec::new();
+ for event in &turn.events {
+ match inbound.source(&event.source) {
+ Ok(source) => pinned
+ .entry(event.source.clone())
+ .or_insert_with(|| (source.clone(), Vec::new()))
+ .1
+ .push(event.clone()),
+ Err(_) => rest.push(event.clone()),
+ }
+ }
+ let mut parts = Vec::new();
+ for (name, (source, mut events)) in pinned {
+ events.sort_by(|a, b| a.event_id.cmp(&b.event_id));
+ let max = usize::try_from(source.max_batch.max(1)).unwrap_or(usize::MAX);
+ for (n, chunk) in events.chunks(max).enumerate() {
+ let mut part = turn.clone();
+ part.turn_id = format!("{}:{name}:{n}", turn.turn_id);
+ part.events = chunk.to_vec();
+ parts.push((part, name.clone(), source.clone()));
+ }
+ }
+ let rest = (!rest.is_empty()).then(|| {
+ let mut left = turn.clone();
+ left.events = rest;
+ left
+ });
+ (parts, rest)
+ }
+
/// What runs pinned turns: given, or the production agent over the
/// workspace's results store.
async fn pinned(&self) -> Option<Pinned> {
@@ -419,6 +463,24 @@ impl Deliverer {
let status = self.deliver_pinned(&turn, &name, &source, remaining).await;
return DeliverResponse::new(status);
}
+ let (parts, rest) = self.split_pinned(&turn).await;
+ for (part, name, source) in &parts {
+ let remaining = self.budget.saturating_sub(started.elapsed());
+ let status = self.deliver_pinned(part, name, source, remaining).await;
+ // Retried before the rest is triaged, so the rest is decided once;
+ // the parts already finished are not run again on redelivery.
+ if status != DeliverStatus::Accepted {
+ return DeliverResponse::new(status);
+ }
+ }
+ let turn = match rest {
+ Some(rest) => rest,
+ None if parts.is_empty() => turn,
+ None => {
+ self.accepted().remember(&turn.turn_id);
+ return DeliverResponse::new(DeliverStatus::Accepted);
+ }
+ };
let (envelope, first) = match self.envelope(&turn) {
Ok(found) => found,
Err(detail) => {
diff --git a/crates/openhuman-core/src/inbound/deliver_tests.rs b/crates/openhuman-core/src/inbound/deliver_tests.rs
index c4edf2acdd..9919a12db9 100644
--- a/crates/openhuman-core/src/inbound/deliver_tests.rs
+++ b/crates/openhuman-core/src/inbound/deliver_tests.rs
@@ -640,3 +640,26 @@ fn a_benign_batch_passes_the_whole_turn_guard() {
);
assert_eq!(decision.action, PromptEnforcementAction::Allow, "{prompt}");
}
+
+#[tokio::test]
+async fn a_pinned_source_in_a_mixed_digest_still_runs_its_agent() {
+ let triage = ScriptedTriage::new([decision(TriageAction::Acknowledge)]);
+ let agent = ScriptedAgent::new([Reply::Text("draft")]);
+ let results = memory_results();
+ let response = pinned_deliverer(&triage, &agent, &results)
+ .deliver(turn(
+ vec![mention("t2"), trigger_event("u1"), mention("t1")],
+ Lane::Digest,
+ 1,
+ ))
+ .await;
+ assert_eq!(response.status, DeliverStatus::Accepted);
+ let runs = agent.runs();
+ assert_eq!(runs.len(), 1, "the pinned events skipped their agent");
+ assert!(runs[0].message.contains("hello t1") && runs[0].message.contains("hello t2"));
+ let stored = results.get("turn-1:x:mentions:0").unwrap().unwrap();
+ assert_eq!(stored.result.status, ResultStatus::Completed);
+ assert_eq!(stored.result.event_ids, ["t1", "t2"]);
+ // The rest of the digest is still triaged, once.
+ assert_eq!(triage.runs(), 1);
+}