The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(background_delivery): inject turn runner into try_deliver
Steven Enamakela54c757caea7
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rsindex 7044b3197e..1ea6fc3c5b 100644--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs@@ -28,11 +28,12 @@//! `chat_done`, so a reconnect cannot lose a completed delegated result.use crate::core::runtime::DispatchCtx;+use std::future::Future;use std::path::{Path, PathBuf};use std::sync::{Arc, OnceLock};use std::time::Duration;#[cfg(test)]-use std::{collections::HashSet, future::Future, sync::Mutex};+use std::{collections::HashSet, sync::Mutex};use async_trait::async_trait;use serde_json::json;@@ -160,7 +161,20 @@ fn schedule_delivery_inner(// Scoped: the thread -> workspace table is keyed per profile, so the// delayed task must run under the profile that scheduled it.ctx.clone().spawn(async move {- let outcome = try_deliver(ctx.clone(), thread_id.clone(), fence.clone(), wait_idle).await;+ let outcome = try_deliver(+ ctx.clone(),+ thread_id.clone(),+ fence.clone(),+ wait_idle,+ delay,+ |ctx, workspace, thread_id, notice| async move {+ run_system_turn_on_thread(&ctx, workspace, thread_id, notice)+ .await+ .map(|_| ())+ .map_err(|error| error.to_string())+ },+ )+ .await;if outcome == DeliveryOutcome::ParentBusy {// Still running past the ceiling: leave the record pending and// wait again rather than overlap the user's turn.@@ -263,12 +277,18 @@ fn claim_ready(/// Drain + deliver pending completions for a thread — if idle and not already/// delivering. Batches everything ready at this instant into one system turn.-async fn try_deliver(+async fn try_deliver<D, Fut>(busy_ctx: DispatchCtx,thread_id: String,fence: DrainFence,recovering: bool,-) -> DeliveryOutcome {+ initial_delay: Duration,+ run_turn: D,+) -> DeliveryOutcome+where+ D: Fn(DispatchCtx, PathBuf, String, String) -> Fut,+ Fut: Future<Output = Result<(), String>>,+{if !fence.admits() {log::info!("[background_delivery] profile lease lost; not delivering thread_id={thread_id}"@@ -308,18 +328,13 @@ async fn try_deliver(};let tenant_key = crate::core::runtime::tenant::profile_key_in(&owner, "");let scoped_thread = crate::core::runtime::tenant::profile_key_in(&owner, &thread_id);+ let run_turn = &run_turn;let deliver = move |records: &[CompletionRecord]| {let notice = format_router.formatter().format_batch(records);let ctx = ctx.clone();let workspace_dir = turn_workspace.clone();let thread_id = turn_thread_id.clone();- async move {- let result = run_system_turn_on_thread(&ctx, workspace_dir, thread_id, notice)- .await- .map(|_| ())- .map_err(|error| error.to_string());- result- }+ run_turn(ctx, workspace_dir, thread_id, notice)};let write_undelivered = move |records: &[CompletionRecord], error: &str| {let attempts = recordsdiff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rsindex 23faa10880..b37b3bece9 100644--- a/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery_tests.rs@@ -31,6 +31,46 @@ fn pending_ids(ws: &Path, thread: &str) -> Vec<String> {.collect()}+#[tokio::test]+async fn try_deliver_applies_the_requested_turn_end_delay() {+ let _g = test_guard().await;+ let ws = workspace();+ let workspace_dir = ws.path().to_path_buf();+ let thread_id = "thread-turn-end-delay";+ record(&workspace_dir, "session-turn-end-delay", "sub-delay", "done", thread_id).await;+ let requested_delay = Duration::from_millis(300);+ let started = std::time::Instant::now();+ let delivered_after = Arc::new(std::sync::Mutex::new(None));+ let observed = Arc::clone(&delivered_after);++ try_deliver(+ cx(),+ thread_id.to_owned(),+ DrainFence::current(&cx()),+ false,+ requested_delay,+ move |_ctx, _workspace, _thread, _notice| {+ let observed = Arc::clone(&observed);+ async move {+ *observed.lock().expect("delivery timestamp") = Some(started.elapsed());+ Ok(())+ }+ },+ )+ .await;++ let delivered_after = delivered_after+ .lock()+ .expect("delivery timestamp")+ .expect("try_deliver should invoke the turn callback");+ assert!(delivered_after >= requested_delay);+ assert!(+ delivered_after < Duration::from_secs(2),+ "requested {requested_delay:?} delay was replaced by the 3s debounce: {delivered_after:?}"+ );+ assert!(pending_ids(&workspace_dir, thread_id).is_empty());+}+fn mark_busy(session: &str) {busy().lock()