Skip to content

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

feat(cron): add policy module for cron job execution rules

Steven Enamakel67941e87186a
diff --git a/crates/openhuman-core/src/cron/mod.rs b/crates/openhuman-core/src/cron/mod.rs
index 659a2ca5b3..2363ddb81a 100644
--- a/crates/openhuman-core/src/cron/mod.rs
+++ b/crates/openhuman-core/src/cron/mod.rs
@@ -18,8 +18,6 @@ pub mod job_builder;
pub mod limits;
pub mod ops;
pub mod origin;
-/// Per-job run policy (retry budget, single-flight) kept beside the job store.
-pub mod policy;
mod schemas;
pub mod seed;
pub(crate) mod store;
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index 4a724a1340..9643ad5987 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -177,8 +177,7 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
return Err("cron is disabled by config (cron.enabled=false)".to_string());
}
- // Fail closed before queueing anything: an unreadable policy refuses the run.
- cron::policy::get_policy(config, job_id).map_err(|e| e.to_string())?;
+ // Fail closed before queueing anything: an unreadable job refuses the run.
cron::limits::check_run(
config,
&cron::get_job(config, job_id).map_err(|e| e.to_string())?,
@@ -228,7 +227,6 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
/// (scheduled or another Run Now) is already active.
pub async fn run_job_now(config: &Config, job_id: &str) -> Result<(bool, String), String> {
let job = cron::get_job(config, job_id.trim()).map_err(|e| e.to_string())?;
- cron::policy::get_policy(config, &job.id).map_err(|e| e.to_string())?;
cron::limits::check_run(config, &job).map_err(|e| e.to_string())?;
let Some(_guard) = try_acquire_run(&job.id) else {
tracing::debug!(job_id = %job.id, "[cron_run] job already running; refused");
diff --git a/crates/openhuman-core/src/cron/ops_tests.rs b/crates/openhuman-core/src/cron/ops_tests.rs
index 44194d59c8..112e4425db 100644
--- a/crates/openhuman-core/src/cron/ops_tests.rs
+++ b/crates/openhuman-core/src/cron/ops_tests.rs
@@ -503,12 +503,15 @@ fn single_flight_system_job(config: &Config, name: &str) -> CronJob {
},
)
.unwrap();
- crate::cron::policy::set_policy(
+ crate::cron::update_job(
config,
&job.id,
- crate::cron::policy::JobPolicy {
- retries: Some(0),
- single_flight: true,
+ CronJobPatch {
+ policy: Some(tinyflows_schedule::JobPolicy {
+ retries: Some(0),
+ single_flight: true,
+ }),
+ ..CronJobPatch::default()
},
)
.unwrap();
diff --git a/crates/openhuman-core/src/cron/policy.rs b/crates/openhuman-core/src/cron/policy.rs
deleted file mode 100644
index 217c87f562..0000000000
--- a/crates/openhuman-core/src/cron/policy.rs
+++ /dev/null
@@ -1,87 +0,0 @@
-//! Per-job run policy: retry budget and single-flight.
-//!
-//! The policy is a field of the job itself (`CronJob::policy`,
-//! [`JobPolicy`]), owned by `tinyflows-schedule` and stored with the job by
-//! `CronDocuments`, so it is read, written and removed with the job. A job
-//! whose policy is the default has none stored, so every existing job keeps its
-//! behaviour:
-//!
-//! - `retries: None` uses `reliability.scheduler_retries`; `Some(0)` means
-//! exactly one attempt.
-//! - `single_flight: false` is the scheduler's historical behaviour: a job is
-//! never dispatched twice by the scheduler, and a slot it misses while still
-//! running is skipped silently. `true` additionally records each missed slot
-//! as a `skipped` run and refuses a manual "Run now" while a run is in flight.
-//!
-//! Policies the host used to keep apart from the job (the `cron_job_policies`
-//! table of `jobs.db`, then the `cron_job_policies` collection) are folded into
-//! the job by the importer and by `CronDocuments` when it opens.
-
-use anyhow::Result;
-use tinyflows_schedule::CronJobPatch;
-pub use tinyflows_schedule::JobPolicy;
-
-use crate::config::Config;
-
-/// Whether `error` is the store's "no such job" for `job_id`: its exact
-/// outermost message, so a backend error that merely mentions "not found"
-/// (a missing collection, say) stays on the fail-closed path.
-fn is_missing(error: &anyhow::Error, job_id: &str) -> bool {
- error.to_string() == format!("Cron job '{job_id}' not found")
-}
-
-/// The policy stored for `job_id`; the default when there is no such job.
-///
-/// Fails closed: a store that errors is an error, so callers refuse to run the
-/// job. The error names the job only; backend error text (which can carry
-/// credentials or payloads) is dropped.
-pub fn get_policy(config: &Config, job_id: &str) -> Result<JobPolicy> {
- match super::store::get_job(config, job_id) {
- Ok(job) => Ok(job.policy),
- Err(error) if is_missing(&error, job_id) => Ok(JobPolicy::default()),
- Err(_) => {
- tracing::warn!(job_id, "[cron:policy] policy unreadable; refusing to run");
- Err(anyhow::anyhow!(
- "cron policy for job {job_id} is unavailable; refusing to run it"
- ))
- }
- }
-}
-
-/// Store `policy` for `job_id`. The default policy is stored as none.
-///
-/// # Errors
-///
-/// When the job does not exist or the store fails.
-pub fn set_policy(config: &Config, job_id: &str, policy: JobPolicy) -> Result<()> {
- tracing::debug!(job_id, ?policy, "[cron:policy] storing job policy");
- super::store::update_job(
- config,
- job_id,
- CronJobPatch {
- policy: Some(policy),
- ..CronJobPatch::default()
- },
- )?;
- Ok(())
-}
-
-/// Revert `job_id` to the default policy. A job that is gone has nothing to
-/// revert.
-pub fn clear_policy(config: &Config, job_id: &str) -> Result<()> {
- match set_policy(config, job_id, JobPolicy::default()) {
- Err(error) if is_missing(&error, job_id) => Ok(()),
- other => other,
- }
-}
-
-/// The retry budget for a job with `policy`.
-pub fn effective_retries(config: &Config, policy: &JobPolicy) -> u32 {
- policy
- .retries
- .unwrap_or(config.reliability.scheduler_retries)
-}
-
-#[cfg(test)]
-#[path = "policy_tests.rs"]
-mod tests;
diff --git a/crates/openhuman-core/src/cron/policy_tests.rs b/crates/openhuman-core/src/cron/policy_tests.rs
deleted file mode 100644
index de39797ca0..0000000000
--- a/crates/openhuman-core/src/cron/policy_tests.rs
+++ /dev/null
@@ -1,165 +0,0 @@
-use super::*;
-use tempfile::TempDir;
-use tinyflows_schedule::types::Schedule;
-
-fn config(tmp: &TempDir) -> Config {
- let workspace = tmp.path().join("workspace");
- std::fs::create_dir_all(&workspace).unwrap();
- Config {
- workspace_dir: workspace.clone(),
- action_dir: workspace,
- config_path: tmp.path().join("config.toml"),
- ..Config::default()
- }
-}
-
-fn shell_job(config: &Config, name: &str) -> String {
- crate::cron::add_shell_job(
- config,
- Some(name.into()),
- Schedule::Every { every_ms: 60_000 },
- "echo policy",
- )
- .unwrap()
- .id
-}
-
-#[test]
-fn a_job_without_a_policy_row_gets_the_default() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- assert_eq!(
- get_policy(&config, "no-such-job").unwrap(),
- JobPolicy::default()
- );
-}
-
-#[test]
-fn a_policy_round_trips_including_zero_retries() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- let id = shell_job(&config, "round-trip");
- let policy = JobPolicy {
- retries: Some(0),
- single_flight: true,
- };
- set_policy(&config, &id, policy).unwrap();
- assert_eq!(get_policy(&config, &id).unwrap(), policy);
-}
-
-#[test]
-fn setting_the_default_policy_reverts_the_job_to_the_default() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- let id = shell_job(&config, "back-to-default");
- set_policy(
- &config,
- &id,
- JobPolicy {
- retries: Some(5),
- single_flight: false,
- },
- )
- .unwrap();
- set_policy(&config, &id, JobPolicy::default()).unwrap();
- assert_eq!(get_policy(&config, &id).unwrap(), JobPolicy::default());
- assert_eq!(
- crate::cron::get_job(&config, &id).unwrap().policy,
- JobPolicy::default()
- );
-}
-
-#[test]
-fn removing_a_job_takes_its_policy_with_it() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- let id = shell_job(&config, "removed");
- set_policy(
- &config,
- &id,
- JobPolicy {
- retries: Some(1),
- single_flight: true,
- },
- )
- .unwrap();
- crate::cron::remove_job(&config, &id).unwrap();
- assert_eq!(get_policy(&config, &id).unwrap(), JobPolicy::default());
- assert!(crate::cron::get_job(&config, &id).is_err());
-}
-
-#[test]
-fn clearing_every_job_clears_every_policy() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- for name in ["one", "two"] {
- let id = shell_job(&config, name);
- set_policy(
- &config,
- &id,
- JobPolicy {
- retries: None,
- single_flight: true,
- },
- )
- .unwrap();
- }
- crate::cron::clear_all_jobs(&config).unwrap();
- assert!(crate::cron::list_jobs(&config).unwrap().is_empty());
-}
-
-#[test]
-fn effective_retries_prefers_the_job_override() {
- let mut config = Config::default();
- config.reliability.scheduler_retries = 2;
- assert_eq!(effective_retries(&config, &JobPolicy::default()), 2);
- let none = JobPolicy {
- retries: Some(0),
- single_flight: false,
- };
- assert_eq!(effective_retries(&config, &none), 0);
-}
-
-#[test]
-fn a_policy_stays_with_its_job_across_other_updates() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- let id = shell_job(&config, "renamed");
- let policy = JobPolicy {
- retries: Some(3),
- single_flight: true,
- };
- set_policy(&config, &id, policy).unwrap();
- crate::cron::update_job(
- &config,
- &id,
- crate::cron::CronJobPatch {
- name: Some("renamed again".into()),
- ..Default::default()
- },
- )
- .unwrap();
- assert_eq!(get_policy(&config, &id).unwrap(), policy);
-}
-
-#[test]
-fn clearing_the_policy_of_a_missing_job_is_not_an_error() {
- let tmp = TempDir::new().unwrap();
- let config = config(&tmp);
- clear_policy(&config, "no-such-job").unwrap();
-}
-
-#[test]
-fn only_the_stores_own_missing_job_message_counts_as_missing() {
- assert!(is_missing(
- &anyhow::anyhow!("Cron job 'j1' not found"),
- "j1"
- ));
- assert!(!is_missing(
- &anyhow::anyhow!("Cron job 'j2' not found"),
- "j1"
- ));
- // A backend failure that merely mentions "not found" must fail closed.
- let backend = anyhow::anyhow!("collection not found").context("cron store: backend failed");
- assert!(!is_missing(&backend, "j1"));
-}
diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rs
index d1a3bc1c6c..09c23d037e 100644
--- a/crates/openhuman-core/src/cron/tools/run.rs
+++ b/crates/openhuman-core/src/cron/tools/run.rs
@@ -88,9 +88,6 @@ impl Tool for CronRunTool {
}
};
- if let Err(e) = cron::policy::get_policy(&self.config, &job.id) {
- return Ok(ToolResult::error(e.to_string()));
- }
if let Err(e) = cron::limits::check_run(&self.config, &job) {
return Ok(ToolResult::error(e.to_string()));
}