Skip to content

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

chore(agent): remove test-only background delivery helpers

Steven Enamakelcbcd6278214e
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index deee00f624..37c8421dda 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -222,26 +222,6 @@ pub(crate) fn recover_on_open(ctx: &DispatchCtx, workspace_dir: &Path, fence: Dr
/// Claim everything ready for a thread **right now** (sync, testable): `None`
/// (queue untouched) when the thread is busy or nothing is pending. A claimed
/// batch stays leased until the caller settles it with `mark_delivered` /
-/// `mark_failed` / `release`.
-#[cfg(test)]
-fn claim_ready(
- ctx: &DispatchCtx,
- router: &CompletionRouter,
- thread_id: &str,
-) -> Option<Vec<CompletionRecord>> {
- if is_busy(ctx, thread_id) {
- return None;
- }
- match router.claim_pending(thread_id, usize::MAX) {
- Ok(batch) if !batch.is_empty() => Some(batch),
- Ok(_) => None,
- Err(error) => {
- log::error!("[background_delivery] claim failed thread_id={thread_id} error={error}");
- None
- }
- }
-}
-
/// 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(
@@ -379,212 +359,6 @@ fn delivery_leases() -> &'static ThreadDeliveryLeases {
LEASES.get_or_init(ThreadDeliveryLeases::default)
}
-/// One pass of the delivery loop whose turn only counts itself, for tests
-/// outside this module.
-#[cfg(test)]
-pub(super) async fn try_deliver_for_test(
- ctx: DispatchCtx,
- thread_id: String,
- router: Arc<CompletionRouter>,
- turns: Arc<std::sync::atomic::AtomicU32>,
-) -> Option<Duration> {
- try_deliver_with(
- &ctx,
- thread_id,
- router,
- move |_, _| {
- turns.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
- async { Ok(String::new()) }
- },
- |_, _| async {},
- )
- .await
-}
-
-/// Backoff before retrying a delivery whose record has now failed `attempts` times.
-#[cfg(test)]
-fn retry_backoff(attempts: u32) -> Duration {
- Duration::from_secs(2u64.saturating_pow(attempts.min(5)))
-}
-
-/// Last resort when the delivery turn has failed [`DEFAULT_MAX_ATTEMPTS`]
-/// times: append the results to the thread directly, with no model turn.
-///
-/// This is the whole point of the ceiling — retries stop, but the user is still
-/// told. If even this append fails there is nowhere left to put the result, so
-/// it is logged at error and the chain ends; that is the one path on which a
-/// result is genuinely lost, and it requires the conversation store to be
-/// failing as well as the agent.
-#[cfg(test)]
-async fn persist_undelivered(ctx: DispatchCtx, dir: PathBuf, thread_id: String, notice: String) {
- let run_id = format!("bgdeliver-undelivered-{}", uuid::Uuid::new_v4());
- match persist_delivery_reply(&ctx, dir, &thread_id, &run_id, notice, false) {
- Ok(()) => log::warn!(
- "[background_delivery] delivery turn gave up; wrote results into the thread \
- verbatim instead thread_id={thread_id} run_id={run_id}"
- ),
- Err(error) => log::error!(
- "[background_delivery] undelivered results LOST — could not append them to the \
- thread thread_id={thread_id} run_id={run_id} error={error}"
- ),
- }
-}
-
-/// Delivery-loop core with an injected router, turn executor and give-up
-/// sink. Keeping the queue and retry boundary independent from host execution
-/// lets tests prove a failed durable append releases the batch before any
-/// terminal announcement is observable, and that a record which exhausts its
-/// attempts is handed to `on_undeliverable` rather than dropped.
-#[cfg(test)]
-async fn try_deliver_with<F, Fut, G, GFut>(
- ctx: &DispatchCtx,
- thread_id: String,
- router: Arc<CompletionRouter>,
- mut deliver: F,
- on_undeliverable: G,
-) -> Option<Duration>
-where
- F: FnMut(String, String) -> Fut,
- Fut: Future<Output = Result<String, String>>,
- G: FnOnce(String, String) -> GFut,
- GFut: Future<Output = ()>,
-{
- let mut retry_after = None;
- if is_busy(ctx, &thread_id) {
- return None;
- }
- // Claim the delivery slot — held for the WHOLE delivery (including the
- // awaited turn) so a concurrent completion can't start a second delivery
- // turn on the same thread. Skip if a delivery is already in flight. The
- // guard frees the slot, and any lease still held, even if this future is
- // dropped mid-turn.
- let mut slot = DeliverySlot::claim(ctx, &thread_id, router.clone())?;
-
- // A busy thread defers *before* the claim: the claim counts a delivery
- // attempt, and a user who keeps typing must not burn a record's budget.
- if let Some(batch) = claim_ready(ctx, &router, &thread_id) {
- let receipts: Vec<_> = batch.iter().filter_map(CompletionRecord::receipt).collect();
- if receipts.len() != batch.len() {
- log::error!(
- "[background_delivery] claimed completion batch contains a record without a receipt; refusing delivery thread_id={thread_id}"
- );
- router.release(&receipts);
- return None;
- }
- slot.hold(&receipts);
- // A user turn can start between the idle check inside `claim_ready` and
- // the awaited turn below; re-check so a system turn is never streamed
- // concurrently with it. Dropping the slot releases the lease (the claim
- // already counted one attempt; this window is narrow).
- if is_busy(ctx, &thread_id) {
- log::debug!(
- "[background_delivery] thread became busy after the claim; deferring \
- thread_id={thread_id}"
- );
- return None;
- }
- let notice = router.formatter().format_batch(&batch);
- log::info!(
- "[background_delivery] delivering {} batched background result(s) thread_id={thread_id}",
- batch.len()
- );
- match deliver(thread_id.clone(), notice).await {
- Ok(_) => {
- if let Err(error) = router.mark_delivered(&receipts) {
- // The reply is already in the thread; the record stays
- // pending, so a later drain or a restart delivers it again
- // (at-least-once).
- log::error!(
- "[background_delivery] delivered but could not settle records \
- thread_id={thread_id} tasks=[{}] error={error}",
- receipts
- .iter()
- .map(|receipt| receipt.task_id.as_str())
- .collect::<Vec<_>>()
- .join(",")
- );
- }
- }
- Err(e) => {
- // A `SESSION_CHECKOUT_FAILURE`-prefixed error (the session could
- // not be checked out, so no turn ran at all) counts the same as a
- // turn that ran and failed. That is deliberate: the budget
- // measures "the result was not delivered", which is equally true
- // either way, and giving up is not lossy — the record is written
- // into the thread rather than discarded. A checkout failure
- // publishes no `AgentError`, so it cannot drive the self-retrigger
- // loop on its own; it only ever spends the budget.
- match router.mark_failed(&receipts) {
- Ok(gave_up) if !gave_up.is_empty() => {
- // Stop retrying, but do NOT drop: the results exist and
- // the user is owed them. Hand them to the give-up sink,
- // which writes them into the thread verbatim along with
- // an explicit statement that delivery failed and why.
- let attempts = gave_up.iter().map(|r| r.attempts).max().unwrap_or(0);
- log::warn!(
- "[background_delivery] giving up on the delivery turn after \
- {attempts} attempts; writing {} result(s) into the thread \
- instead thread_id={thread_id} tasks=[{}] error={e}",
- gave_up.len(),
- gave_up
- .iter()
- .map(|c| c.task_id.as_str())
- .collect::<Vec<_>>()
- .join(","),
- );
- if let Some(undelivered) = build_undelivered_notice(&gave_up, attempts, &e)
- {
- on_undeliverable(thread_id.clone(), undelivered).await;
- }
- }
- Ok(_) => {
- log::warn!(
- "[background_delivery] delivery turn failed thread_id={thread_id} \
- tasks=[{}] max_attempts={} error={e}",
- receipts
- .iter()
- .map(|receipt| receipt.task_id.as_str())
- .collect::<Vec<_>>()
- .join(","),
- router.max_attempts()
- );
- let attempts = router
- .pending_for(&thread_id)
- .iter()
- .map(|r| r.attempts)
- .max()
- .unwrap_or(1);
- retry_after = Some(retry_backoff(attempts));
- }
- Err(error) => {
- log::error!(
- "[background_delivery] could not record the failed delivery \
- thread_id={thread_id} error={error}"
- );
- router.release(&receipts);
- }
- }
- // A batch can mix records at different attempt counts: some gave
- // up above, the rest are still pending. Nothing else would wake a
- // quiet thread for them, so ask for a retry.
- if retry_after.is_none() {
- if let Some(attempts) = router
- .pending_for(&thread_id)
- .iter()
- .map(|r| r.attempts)
- .max()
- {
- retry_after = Some(retry_backoff(attempts));
- }
- }
- }
- }
- }
-
- // The slot is released only AFTER the turn settles (on drop).
- retry_after
-}
-
/// Run one system-authored delivery turn on an existing conversation thread.
/// It only delivers a detached sub-agent result already produced by
/// the completion router.
@@ -707,6 +481,3 @@ pub(crate) fn register_background_delivery() {
#[path = "background_delivery_tests.rs"]
mod tests;
-#[cfg(test)]
-#[path = "background_delivery_slots_tests.rs"]
-mod slots_tests;