Skip to content

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

feat(agent): deliver background task results to web chat channels

Steven Enamakelc86541285ac8
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 65b1e201b6..aaaa2c9f03 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -94,6 +94,12 @@ pub(crate) fn clear_busy_for_thread(thread_id: &str) -> usize {
cleared
}
+/// Ask for a delivery attempt on `thread_id` soon (after a Stop cleared a stale
+/// busy mark, or anything else that may have left a drain without its trigger).
+pub(crate) fn kick_delivery(thread_id: &str) {
+ schedule_delivery(thread_id.to_string(), Duration::from_millis(300));
+}
+
struct BackgroundDeliveryHandler;
#[async_trait]
diff --git a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
index 11265b598c..875361cd05 100644
--- a/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
+++ b/crates/openhuman-core/src/web_chat/ops/channel_ops.rs
@@ -117,8 +117,14 @@ async fn cancel_chat_inner(
// A turn cancelled cooperatively may never publish a terminal event, which
// would leave its session marked busy and defer this thread's background
// deliveries until restart.
- if removed_request_id.is_some() || request_id.is_none() {
+ //
+ // Only once nothing is left running on the thread: a scoped cancel can leave
+ // the primary or a parallel turn alive, and those still owe a terminal event.
+ if cancelled_any.is_some() && !thread_has_live_turn(thread_id, &map_key).await {
crate::agent::orchestration::background_delivery::clear_busy_for_thread(thread_id);
+ // A drain that fired while the cancelled turn counted as busy returned
+ // without claiming anything; ask for another.
+ crate::agent::orchestration::background_delivery::kick_delivery(thread_id);
}
let subagents_cancelled = if request_id.is_none() {
// Gate completion recording before aborting: Tokio abort is
@@ -177,6 +183,18 @@ async fn cancel_chat_inner(
})
}
+/// Is a primary or parallel turn still running on `thread_id`?
+async fn thread_has_live_turn(thread_id: &str, map_key: &str) -> bool {
+ if IN_FLIGHT.lock().await.contains_key(map_key) {
+ return true;
+ }
+ super::state::PARALLEL_IN_FLIGHT
+ .lock()
+ .await
+ .values()
+ .any(|entry| entry.thread_id == thread_id)
+}
+
pub async fn channel_web_chat(
client_id: &str,
thread_id: &str,