Skip to content

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

fix(cron): record Run Now delivery failures

Danny Warda99095bf83b1
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index 9a0e5f362f..c18bcf35c8 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -6,7 +6,8 @@ use serde_json::json;
use {
crate::cron, crate::cron::add_shell_job, crate::cron::update_job,
tinyflows_bus::schedule::types::CronJob, tinyflows_bus::schedule::types::CronJobPatch,
- tinyflows_bus::schedule::types::CronRun, tinyflows_bus::schedule::types::Schedule,
+ tinyflows_bus::schedule::types::CronRun, tinyflows_bus::schedule::types::DeliveryStatus,
+ tinyflows_bus::schedule::types::Schedule,
};
/// Claim `job_id` as running. `None` when a Run Now of it already holds it. The
@@ -275,8 +276,9 @@ async fn run_and_record(
) -> (bool, String) {
let run_id = uuid::Uuid::new_v4().to_string();
let started_at = chrono::Utc::now();
- let (success, output) = cron::scheduler::execute_job_now(ctx, config, job, &run_id).await;
- if !success && output.ends_with("is already running") {
+ let (execution_success, output) =
+ cron::scheduler::execute_job_now(ctx, config, job, &run_id).await;
+ if !execution_success && output.ends_with("is already running") {
// A scheduled run holds the job (the module's claim): nothing ran, so
// there is nothing to deliver or record, and the "queued" placeholder
// goes with the refused run.
@@ -286,8 +288,6 @@ async fn run_and_record(
}
let finished_at = chrono::Utc::now();
let duration_ms = (finished_at - started_at).num_milliseconds();
- let status = if success { "ok" } else { "error" };
- tracing::debug!(job_id = %job.id, status, duration_ms, "[cron_run] run finished");
// Remove a "queued" placeholder before inserting the real result so no
// orphaned rows are left in the run history.
let _ = cron::delete_queued_runs(config, &job.id);
@@ -295,6 +295,13 @@ async fn run_and_record(
// origin replies and alerts are sent on "Run Now" too, then record the run
// with how the delivery went.
let delivery_status = cron::scheduler::deliver_job(ctx, config, job, &run_id, &output).await;
+ let success = success_after_delivery(
+ execution_success,
+ &delivery_status,
+ job.delivery.best_effort,
+ );
+ let status = if success { "ok" } else { "error" };
+ tracing::debug!(job_id = %job.id, status, duration_ms, "[cron_run] run finished");
let _ = cron::record_run_with_delivery(
config,
&job.id,
@@ -309,6 +316,16 @@ async fn run_and_record(
(success, output)
}
+/// A required delivery failure makes the run fail; best-effort and unrequested
+/// deliveries leave the execution result unchanged.
+pub(crate) fn success_after_delivery(
+ execution_success: bool,
+ delivery_status: &DeliveryStatus,
+ best_effort: bool,
+) -> bool {
+ execution_success && (!matches!(delivery_status, DeliveryStatus::Failed) || best_effort)
+}
+
pub async fn cron_runs(
config: &Config,
job_id: &str,
diff --git a/crates/openhuman-core/src/cron/ops_tests.rs b/crates/openhuman-core/src/cron/ops_tests.rs
index 4da5385089..13272c60b7 100644
--- a/crates/openhuman-core/src/cron/ops_tests.rs
+++ b/crates/openhuman-core/src/cron/ops_tests.rs
@@ -1,7 +1,7 @@
use super::*;
use crate::cron::get_job;
use tempfile::TempDir;
-use tinyflows_bus::schedule::types::ActiveHours;
+use tinyflows_bus::schedule::types::{ActiveHours, DeliveryConfig};
fn test_config(tmp: &TempDir) -> Config {
let config = Config {
@@ -619,3 +619,72 @@ async fn run_job_now_reports_an_unknown_job() {
.await
.is_err());
}
+
+#[test]
+fn required_delivery_failure_only_fails_non_best_effort_runs() {
+ let cases = [
+ (true, DeliveryStatus::Failed, false, false),
+ (true, DeliveryStatus::Failed, true, true),
+ (true, DeliveryStatus::NotRequested, false, true),
+ (true, DeliveryStatus::Suppressed, false, true),
+ (false, DeliveryStatus::Delivered, true, false),
+ ];
+
+ for (execution_success, delivery_status, best_effort, expected) in cases {
+ assert_eq!(
+ success_after_delivery(execution_success, &delivery_status, best_effort),
+ expected,
+ "unexpected success for execution_success={execution_success}, delivery_status={delivery_status:?}, best_effort={best_effort}"
+ );
+ }
+}
+
+#[tokio::test]
+async fn run_job_now_records_required_delivery_failure_as_error() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let _handler = crate::cron::system_job_handlers::register(
+ "ops-run-now-delivery-failure",
+ std::sync::Arc::new(|_ctx| Box::pin(async { Ok(()) })),
+ );
+ let job = single_flight_system_job(&config, "ops-run-now-delivery-failure");
+ cron::update_job(
+ &config,
+ &job.id,
+ CronJobPatch {
+ delivery: Some(DeliveryConfig {
+ mode: "announce".into(),
+ channel: None,
+ to: Some("target".into()),
+ best_effort: false,
+ }),
+ ..CronJobPatch::default()
+ },
+ )
+ .unwrap();
+
+ let (success, output) = run_job_now(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &job.id,
+ )
+ .await
+ .unwrap();
+
+ assert!(!success, "required delivery failure must fail Run Now");
+ assert!(
+ output.contains("completed"),
+ "execution itself succeeded: {output}"
+ );
+ let runs = cron::list_runs(&config, &job.id, 10).unwrap();
+ assert_eq!(runs.len(), 1);
+ assert_eq!(runs[0].status, "error");
+ assert_eq!(runs[0].delivery_status, Some(DeliveryStatus::Failed));
+ assert_eq!(
+ cron::get_job(&config, &job.id)
+ .unwrap()
+ .last_status
+ .as_deref(),
+ Some("error")
+ );
+}
diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rs
index cce0909c16..7aa8530d81 100644
--- a/crates/openhuman-core/src/cron/tools/run.rs
+++ b/crates/openhuman-core/src/cron/tools/run.rs
@@ -104,10 +104,16 @@ impl Tool for CronRunTool {
let started_at = Utc::now();
let run_id = uuid::Uuid::new_v4().to_string();
- let (success, output) =
+ let (execution_success, output) =
cron::scheduler::execute_job_now(&dispatch, &self.config, &job, &run_id).await;
let finished_at = Utc::now();
let duration_ms = (finished_at - started_at).num_milliseconds();
+ let delivery_status = cron::DeliveryStatus::NotRequested;
+ let success = cron::ops::success_after_delivery(
+ execution_success,
+ &delivery_status,
+ job.delivery.best_effort,
+ );
let status = if success { "ok" } else { "error" };
// The output is returned to the calling agent below, so nothing is
@@ -122,7 +128,7 @@ impl Tool for CronRunTool {
status,
Some(&output),
duration_ms,
- Some(cron::DeliveryStatus::NotRequested),
+ Some(delivery_status),
);
let _ = cron::record_last_run(&self.config, &job.id, finished_at, success, &output);
diff --git a/crates/openhuman-core/src/cron/tools/run_tests.rs b/crates/openhuman-core/src/cron/tools/run_tests.rs
index fbc7302ac2..93b80ab71a 100644
--- a/crates/openhuman-core/src/cron/tools/run_tests.rs
+++ b/crates/openhuman-core/src/cron/tools/run_tests.rs
@@ -52,6 +52,15 @@ async fn force_runs_job_and_records_history() {
let runs = cron::list_runs(&cfg, &job.id, 10).unwrap();
assert_eq!(runs.len(), 1);
}
+
+ let runs = cron::list_runs(&cfg, &job.id, 10).unwrap();
+ if let Some(run) = runs.first() {
+ assert_eq!(run.status, if result.is_error { "error" } else { "ok" });
+ assert_eq!(
+ run.delivery_status,
+ Some(cron::DeliveryStatus::NotRequested)
+ );
+ }
}
#[tokio::test]