Skip to content

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

fix(inbound): split pinned parts per conversation key, oldest first, within the budget

M3gA-Mindbb4714301676
diff --git a/crates/openhuman-core/src/inbound/deliver.rs b/crates/openhuman-core/src/inbound/deliver.rs
index 9bd3d37c18..8b791585b8 100644
--- a/crates/openhuman-core/src/inbound/deliver.rs
+++ b/crates/openhuman-core/src/inbound/deliver.rs
@@ -299,22 +299,25 @@ 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.
+ /// `turn`'s events of pinned sources, one group per source and
+ /// conversation key, as turns of at most the source's `max_batch` events,
+ /// and the turn of the other events (`None` when there are none). Each
+ /// group is oldest first (event id breaks ties), and a part's id names
+ /// its first and last event, so a redelivery derives the same ids and a
+ /// changed `max_batch` derives new ones instead of reusing a finished id
+ /// for a different set of events.
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>)> =
+ let mut pinned: BTreeMap<(String, 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())
+ .entry((event.source.clone(), event.conversation_key.clone()))
.or_insert_with(|| (source.clone(), Vec::new()))
.1
.push(event.clone()),
@@ -322,12 +325,15 @@ impl Deliverer {
}
}
let mut parts = Vec::new();
- for (name, (source, mut events)) in pinned {
- events.sort_by(|a, b| a.event_id.cmp(&b.event_id));
+ for ((name, key), (source, mut events)) in pinned {
+ events.sort_by(|a, b| {
+ (a.received_at_ms, &a.event_id).cmp(&(b.received_at_ms, &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() {
+ for chunk in events.chunks(max) {
+ let (first, last) = (&chunk[0].event_id, &chunk[chunk.len() - 1].event_id);
let mut part = turn.clone();
- part.turn_id = format!("{}:{name}:{n}", turn.turn_id);
+ part.turn_id = format!("{}:{name}:{key}:{first}..{last}", turn.turn_id);
part.events = chunk.to_vec();
parts.push((part, name.clone(), source.clone()));
}
@@ -473,6 +479,15 @@ impl Deliverer {
return DeliverResponse::new(status);
}
}
+ if !parts.is_empty() && rest.is_some() && started.elapsed() >= self.budget {
+ // The pinned parts used the whole budget: triage the rest on the
+ // redelivery, where the finished parts are skipped.
+ return DeliverResponse::new(self.retry(
+ &turn,
+ None,
+ "delivery budget spent on pinned parts",
+ ));
+ }
let turn = match rest {
Some(rest) => rest,
None if parts.is_empty() => turn,
@@ -496,7 +511,11 @@ impl Deliverer {
let outcome = match cached {
Some(run) => Ok(Ok(TriageOutcome::Decision(run))),
None => {
- let timeout = self.timeout().await;
+ // Within what is left of the delivery's budget.
+ let timeout = self
+ .timeout()
+ .await
+ .min(self.budget.saturating_sub(started.elapsed()));
tokio::time::timeout(timeout, self.runner.run(&self.ctx, &envelope)).await
}
};
diff --git a/crates/openhuman-core/src/inbound/deliver_tests.rs b/crates/openhuman-core/src/inbound/deliver_tests.rs
index 9919a12db9..1179dacb5c 100644
--- a/crates/openhuman-core/src/inbound/deliver_tests.rs
+++ b/crates/openhuman-core/src/inbound/deliver_tests.rs
@@ -657,9 +657,67 @@ async fn a_pinned_source_in_a_mixed_digest_still_runs_its_agent() {
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();
+ let stored = results.get("turn-1:x:mentions:mentions:t1..t2").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);
}
+
+fn mention_at(id: &str, key: &str, received_at_ms: u64) -> InboundEvent {
+ events::external(
+ "x:mentions",
+ key,
+ id,
+ &format!("hello {id}"),
+ json!({ "author": "@a" }),
+ received_at_ms,
+ )
+ .unwrap()
+}
+
+#[tokio::test]
+async fn a_mixed_digest_runs_each_conversation_key_oldest_first() {
+ let triage = ScriptedTriage::new([decision(TriageAction::Acknowledge)]);
+ let agent = ScriptedAgent::new([Reply::Text("a"), Reply::Text("b")]);
+ let results = memory_results();
+ let response = pinned_deliverer(&triage, &agent, &results)
+ .deliver(turn(
+ vec![
+ mention_at("z-old", "mentions", 1),
+ mention_at("a-new", "mentions", 9),
+ mention_at("d1", "dms", 3),
+ trigger_event("u1"),
+ ],
+ Lane::Digest,
+ 1,
+ ))
+ .await;
+ assert_eq!(response.status, DeliverStatus::Accepted);
+ let mut keys: Vec<String> = agent.runs().into_iter().map(|run| run.key).collect();
+ keys.sort();
+ assert_eq!(keys, ["dms", "mentions"]);
+ let mentions = results
+ .get("turn-1:x:mentions:mentions:z-old..a-new")
+ .unwrap()
+ .unwrap();
+ assert_eq!(mentions.result.event_ids, ["z-old", "a-new"]);
+ assert!(results.get("turn-1:x:mentions:dms:d1..d1").unwrap().is_some());
+}
+
+#[tokio::test]
+async fn the_rest_of_a_mixed_digest_waits_when_the_pinned_parts_spent_the_budget() {
+ 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)
+ .with_budget(Duration::ZERO)
+ .deliver(turn(
+ vec![mention("t1"), trigger_event("u1")],
+ Lane::Digest,
+ 1,
+ ))
+ .await;
+ assert!(matches!(response.status, DeliverStatus::Retry { .. }));
+ assert_eq!(triage.runs(), 0, "triage started with no budget left");
+}