Skip to content

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

refactor(orchestration): extract background delivery persistence helpers

Steven Enamakeldde76294e8ab
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 92252d5052..6e2e2e30c5 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -35,7 +35,6 @@ use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;
use async_trait::async_trait;
-use serde_json::json;
use tinyagents_tasks::{CompletionRecord, CompletionRouter};
use crate::core::bus::BUS;
@@ -45,6 +44,8 @@ use tinybus::SubscriptionHandle;
mod slot;
use slot::DeliverySlot;
+mod persistence;
+use persistence::{persist_delivery_reply, persist_then_announce};
use super::background_completions;
use super::busy_guard::is_busy;
@@ -691,56 +692,6 @@ async fn run_system_turn_on_thread(
)
}
-/// Persist a terminal reply, then announce it. A persistence failure returns
-/// before `announce` runs, allowing the outer delivery loop to requeue the
-/// completed background batch without publishing a phantom terminal event.
-fn persist_then_announce<P, A>(
- result: Result<String, String>,
- persist: P,
- announce: A,
-) -> Result<String, String>
-where
- P: FnOnce(&str, bool) -> Result<(), String>,
- A: FnOnce(&Result<String, String>),
-{
- let (content, success) = match &result {
- Ok(text) => (text.trim().to_string(), true),
- Err(error) => (format!("Run failed: {error}"), false),
- };
- if !content.is_empty() {
- persist(&content, success)?;
- }
- announce(&result);
- result
-}
-
-/// Durably append a background-delivery reply before publishing its terminal
-/// chat event. Callers must propagate failures: publishing `chat_done` or
-/// `chat_error` without a stored row loses the result across reconnects.
-fn persist_delivery_reply(
- ctx: &DispatchCtx,
- workspace_dir: std::path::PathBuf,
- thread_id: &str,
- run_id: &str,
- content: String,
- success: bool,
-) -> Result<(), String> {
- crate::threads::store::append_message(
- ctx,
- workspace_dir,
- thread_id,
- crate::threads::store::ConversationMessage {
- id: crate::threads::store::run_reply_message_id(run_id),
- content,
- message_type: "text".to_string(),
- extra_metadata: json!({"scope": "background_delivery", "success": success, "requestId": run_id}),
- sender: "agent".to_string(),
- created_at: chrono::Utc::now().to_rfc3339(),
- },
- )
- .map(|_| ())
-}
-
/// Register the delivery subscriber on the global event bus. Keeps the
/// subscription alive for the process lifetime. Idempotent.
pub(crate) fn register_background_delivery() {
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery/persistence.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery/persistence.rs
new file mode 100644
index 0000000000..8c3bf59025
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery/persistence.rs
@@ -0,0 +1,57 @@
+//! Store a background delivery reply before publishing its terminal event.
+
+use crate::core::runtime::DispatchCtx;
+
+/// Persist a terminal reply, then announce it. A persistence failure returns
+/// before `announce` runs, allowing the outer delivery loop to requeue the
+/// completed background batch without publishing a phantom terminal event.
+pub(super) fn persist_then_announce<P, A>(
+ result: Result<String, String>,
+ persist: P,
+ announce: A,
+) -> Result<String, String>
+where
+ P: FnOnce(&str, bool) -> Result<(), String>,
+ A: FnOnce(&Result<String, String>),
+{
+ let (content, success) = match &result {
+ Ok(text) => (text.trim().to_string(), true),
+ Err(error) => (format!("Run failed: {error}"), false),
+ };
+ if !content.is_empty() {
+ persist(&content, success)?;
+ }
+ announce(&result);
+ result
+}
+
+/// Durably append a background-delivery reply before publishing its terminal
+/// chat event. Callers must propagate failures: publishing `chat_done` or
+/// `chat_error` without a stored row loses the result across reconnects.
+pub(super) fn persist_delivery_reply(
+ ctx: &DispatchCtx,
+ workspace_dir: std::path::PathBuf,
+ thread_id: &str,
+ run_id: &str,
+ content: String,
+ success: bool,
+) -> Result<(), String> {
+ crate::threads::store::append_message(
+ ctx,
+ workspace_dir,
+ thread_id,
+ crate::threads::store::ConversationMessage {
+ id: crate::threads::store::run_reply_message_id(run_id),
+ content,
+ message_type: "text".to_string(),
+ extra_metadata: serde_json::json!({
+ "scope": "background_delivery",
+ "success": success,
+ "requestId": run_id
+ }),
+ sender: "agent".to_string(),
+ created_at: chrono::Utc::now().to_rfc3339(),
+ },
+ )
+ .map(|_| ())
+}