Skip to content

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

chore: files changed crates/openhuman-core/src/cron/scheduler/run_delivery.rs

Steven Enamakeleac481d292ae
diff --git a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
index 4557b2c87b..928bf4d028 100644
--- a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
@@ -1,12 +1,4 @@
-//! The host's answers to the module's `Deliver` and `ScheduleTrigger`
-//! callbacks.
-//!
-//! Delivery sends a finished run's output to its chat, channel or origin and
-//! raises the alert (`delivery.rs`), then announces the completion on the bus.
-//! The trigger fires a `flow` row through the job-kind registry
-//! ([`crate::cron::handlers`]): the default `flow` handler starts the flow's
-//! schedule tick directly in the flows domain, or runs a host-owned system
-//! job, with no domain event in between.
+//! Host adapters for TinyFlows' send, terminal projection and trigger ports.
use super::delivery::deliver_run;
use crate::core::bus::BUS;
@@ -14,14 +6,11 @@ use crate::core::events::DomainEvent;
use crate::cron::bus_host::{kind_refusal, Binding};
use crate::cron::handlers::{kind_handler, JobRunContext};
use anyhow::Result;
-use chrono::Utc;
-use tinyflows_bus::schedule::types::{
- CronJob, DeliveryConfig, DeliveryStatus, JobPolicy, JobType, Schedule, SessionTarget,
-};
-use tinyflows_bus::schedule::{Attempt, StoreOperation};
+use tinyflows_bus::schedule::types::{CronJob, DeliveryStatus, JobType};
+use tinyflows_bus::schedule::{Attempt, DeliveryProjection};
-/// Deliver `attempt`'s output for `job` and publish the run's completion.
-pub(crate) async fn deliver(
+/// Perform the actual product delivery exactly once.
+pub(crate) async fn send(
binding: &Binding,
job: &CronJob,
run_id: &str,
@@ -31,28 +20,28 @@ pub(crate) async fn deliver(
.within(async {
let config = binding.config();
let ctx = binding.dispatch_ctx();
- let result =
- deliver_run(&ctx, &config, job, run_id, &attempt.output, attempt.success).await;
- // A failed required delivery fails the run, as the module
- // records it.
- let delivered = match &result {
- Ok(status) => *status != DeliveryStatus::Failed || job.delivery.best_effort,
- Err(_) => job.delivery.best_effort,
- };
- let success = attempt.success && delivered;
- // The job ran as its agent (when it had one); a one-shot job
- // is deleted once the run completes, so subscribers learn the
- // owner here, not from the store.
- binding.finish_triggered_run(run_id);
- publish_completion(&ctx, job, success, &attempt.output);
- result
+ deliver_run(&ctx, &config, job, run_id, &attempt.output, attempt.success).await
})
.await
}
-/// Publish the final status shared by scheduled callbacks, Run Now, and the
-/// non-delivering `cron_run` tool. Callers decide delivery and persistence
-/// before projecting this event.
+/// Project the schedule module's authoritative terminal decision and event.
+pub(crate) async fn deliver(binding: &Binding, projection: &DeliveryProjection) {
+ binding
+ .within(async {
+ let ctx = binding.dispatch_ctx();
+ binding.finish_triggered_run(&projection.run_id);
+ publish_completion(
+ &ctx,
+ &projection.job,
+ projection.success,
+ &projection.attempt.output,
+ );
+ })
+ .await;
+}
+
+/// Publish the final status shared by scheduled and manual runs.
pub(crate) fn publish_completion(
ctx: &crate::core::runtime::DispatchCtx,
job: &CronJob,
@@ -78,70 +67,26 @@ pub(crate) fn publish_completion(
});
}
-/// A `flow` row standing in for a trigger whose stored row cannot be read (it
-/// was removed between the claim and the tick). The handler needs only the
-/// bound flow id, which is the row's `command`.
-fn stand_in(flow_id: &str) -> CronJob {
- let now = Utc::now();
- CronJob {
- claim_generation: 0,
- policy: JobPolicy::default(),
- id: flow_id.to_string(),
- expression: String::new(),
- schedule: Schedule::Every { every_ms: 60_000 },
- command: flow_id.to_string(),
- prompt: None,
- name: None,
- job_type: JobType::Flow,
- session_target: SessionTarget::default(),
- model: None,
- agent_id: None,
- enabled: true,
- delivery: DeliveryConfig::default(),
- delete_after_run: false,
- created_at: now,
- next_run: now,
- last_run: None,
- last_status: None,
- last_output: None,
- origin: None,
- }
-}
-
-/// Fire the `flow` row bound to `flow_id` through the registered `flow`
-/// handler.
-pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> Attempt {
+/// Fire the captured flow definition through the registered kind handler.
+pub(crate) async fn trigger(binding: &Binding, job: &CronJob, run_id: &str) -> Attempt {
binding
.within(async {
+ binding.publish_triggered_once(job, run_id);
let config = binding.config();
- let job = match crate::cron::client::store::<Option<CronJob>>(
- &config,
- StoreOperation::FindFlow {
- flow_id: flow_id.to_string(),
- },
- )
- .await
- {
- Ok(Some(job)) => job,
- Ok(None) => stand_in(flow_id),
- Err(error) => {
- tracing::debug!(%flow_id, %error, "[cron:trigger] flow row unreadable; using a stand-in");
- stand_in(flow_id)
- }
- };
- binding.publish_triggered_once(&job, run_id);
- if let Err(error) = crate::cron::limits::check_run(&config, &job) {
+ if let Err(error) = crate::cron::limits::check_run(&config, job) {
return Attempt {
success: false,
output: error.to_string(),
retryable: false,
+ raw_detail: None,
};
}
- if let Some(refusal) = kind_refusal(binding, &job) {
+ if let Some(refusal) = kind_refusal(binding, job) {
return Attempt {
success: false,
output: refusal,
retryable: false,
+ raw_detail: None,
};
}
let Some(handler) = kind_handler(&JobType::Flow) else {
@@ -149,15 +94,17 @@ pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> A
success: false,
output: "no handler is registered for flow jobs".to_string(),
retryable: false,
+ raw_detail: None,
};
};
let outcome = handler
- .run(&JobRunContext::new(binding.clone()), &job, run_id)
+ .run(&JobRunContext::new(binding.clone()), job, run_id)
.await;
Attempt {
success: outcome.success,
output: outcome.output,
retryable: !outcome.permanent,
+ raw_detail: outcome.agent_error,
}
})
.await
@@ -166,6 +113,3 @@ pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> A
#[cfg(test)]
#[path = "run_delivery_tests.rs"]
pub(crate) mod tests;
-
-#[cfg(test)]
-pub(crate) use tests::stand_in_for_test;