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/ops.rs,crates/openhuman-core/src/cron/policy.rs,

Steven Enamakeldf7893d19631
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index e084a45af4..2d1cdb94b3 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -226,9 +226,6 @@ 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())?;
-
// Guard against duplicate concurrent executions for the same job.
{
let mut active = ACTIVE_RUNS
@@ -289,7 +286,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())?;
let Some(_guard) = try_acquire_run(&job.id) else {
tracing::debug!(job_id = %job.id, "[cron_run] job already running; refused");
return Err(format!("cron job '{}' is already running", job.id));
diff --git a/crates/openhuman-core/src/cron/policy.rs b/crates/openhuman-core/src/cron/policy.rs
index 497da1fe95..2ee7b50c34 100644
--- a/crates/openhuman-core/src/cron/policy.rs
+++ b/crates/openhuman-core/src/cron/policy.rs
@@ -79,30 +79,49 @@ async fn declare(
docs.ensure_collection(&CollectionSpec::new(POLICIES)).await
}
-fn from_doc(doc: &Value) -> JobPolicy {
- JobPolicy {
- retries: doc
- .get("retries")
- .and_then(Value::as_u64)
- .map(|n| u32::try_from(n).unwrap_or(u32::MAX)),
- single_flight: doc
- .get("single_flight")
- .and_then(Value::as_bool)
- .unwrap_or(false),
- }
+/// Decode a stored policy row. A row this code did not write (wrong type,
+/// out-of-range retries, missing `single_flight`) is an error, never a default.
+fn from_doc(doc: &Value) -> Result<JobPolicy> {
+ let retries = match doc.get("retries") {
+ None | Some(Value::Null) => None,
+ Some(value) => Some(
+ value
+ .as_u64()
+ .and_then(|n| u32::try_from(n).ok())
+ .context("retries is not a u32")?,
+ ),
+ };
+ let single_flight = doc
+ .get("single_flight")
+ .and_then(Value::as_bool)
+ .context("single_flight is not a bool")?;
+ Ok(JobPolicy {
+ retries,
+ single_flight,
+ })
}
-/// The policy stored for `job_id`; the default when none is.
+/// The policy stored for `job_id`; the default when there is no row.
+///
+/// Fails closed: a policy backend that errors, or a row that does not decode,
+/// 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> {
+ read_policy(config, job_id).map_err(|_| {
+ tracing::warn!(job_id, "[cron:policy] policy unreadable; refusing to run");
+ anyhow::anyhow!("cron policy for job {job_id} is unavailable; refusing to run it")
+ })
+}
+
+fn read_policy(config: &Config, job_id: &str) -> Result<JobPolicy> {
if let Some(docs) = documents()? {
let id = job_id.to_string();
return block_on_anyhow(async move {
declare(&docs).await?;
- Ok(docs
- .get(POLICIES, &id)
- .await?
- .map(|stored| from_doc(&stored.doc))
- .unwrap_or_default())
+ match docs.get(POLICIES, &id).await? {
+ Some(stored) => from_doc(&stored.doc),
+ None => Ok(JobPolicy::default()),
+ }
});
}
let conn = open(config)?;
@@ -197,15 +216,6 @@ pub fn effective_retries(config: &Config, policy: &JobPolicy) -> u32 {
.unwrap_or(config.reliability.scheduler_retries)
}
-/// The stored policy for `job_id`, or the default when it cannot be read: a
-/// policy-store hiccup must not stop a job from running.
-pub(crate) fn policy_or_default(config: &Config, job_id: &str) -> JobPolicy {
- get_policy(config, job_id).unwrap_or_else(|error| {
- tracing::warn!(job_id, %error, "[cron:policy] policy unreadable; using the default");
- JobPolicy::default()
- })
-}
-
#[cfg(test)]
#[path = "policy_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/cron/scheduler/dispatch.rs b/crates/openhuman-core/src/cron/scheduler/dispatch.rs
index ed2e141dc8..ba46851255 100644
--- a/crates/openhuman-core/src/cron/scheduler/dispatch.rs
+++ b/crates/openhuman-core/src/cron/scheduler/dispatch.rs
@@ -32,7 +32,7 @@ use crate::config::Config;
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
use crate::core::runtime::CoreContext;
-use crate::cron::policy::get_policy;
+use crate::cron::policy::policy_or_default;
use crate::cron::{next_run_for_schedule, record_run, update_job, CronJob, CronJobPatch, Schedule};
use crate::security::SecurityPolicy;
@@ -68,11 +68,6 @@ impl JobDispatcher {
skip_in_flight(config, &job);
continue;
};
- // Fail closed: an unreadable policy refuses the job before its slot
- // is advanced, so the occurrence is retried once the read recovers.
- if get_policy(config, &job.id).is_err() {
- continue;
- }
claim_slot(config, &job);
let config = config.clone();
let security = Arc::clone(security);
@@ -148,8 +143,8 @@ fn claim_slot(config: &Config, job: &CronJob) {
/// A due job whose previous run is still going.
fn skip_in_flight(config: &Config, job: &CronJob) {
- let single_flight = get_policy(config, &job.id).is_ok_and(|policy| policy.single_flight);
- if !single_flight || matches!(job.schedule, Schedule::At { .. }) {
+ let policy = policy_or_default(config, &job.id);
+ if !policy.single_flight || matches!(job.schedule, Schedule::At { .. }) {
tracing::trace!(job_id = %job.id, "[cron:dispatch] still running; not dispatched again");
return;
}
diff --git a/crates/openhuman-core/src/cron/scheduler/retry.rs b/crates/openhuman-core/src/cron/scheduler/retry.rs
index 0f5b5f68a5..d26d53a607 100644
--- a/crates/openhuman-core/src/cron/scheduler/retry.rs
+++ b/crates/openhuman-core/src/cron/scheduler/retry.rs
@@ -48,13 +48,11 @@ pub(super) async fn execute_job_with_retry_for_run(
) -> (bool, String) {
let mut last_output = String::new();
let mut last_agent_error: Option<String> = None;
- // A per-job override (`cron::policy`) wins; `Some(0)` is one attempt. An
- // unreadable policy refuses the run (and any later attempt) outright.
- let policy = match crate::cron::policy::get_policy(config, &job.id) {
- Ok(policy) => policy,
- Err(error) => return (false, error.to_string()),
- };
- let retries = crate::cron::policy::effective_retries(config, &policy);
+ // A per-job override (`cron::policy`) wins; `Some(0)` is one attempt.
+ let retries = crate::cron::policy::effective_retries(
+ config,
+ &crate::cron::policy::policy_or_default(config, &job.id),
+ );
let mut backoff_ms = config.reliability.provider_backoff_ms.max(200);
let mut session_expired = false;
let mut credits_exhausted = false;
@@ -63,11 +61,6 @@ pub(super) async fn execute_job_with_retry_for_run(
let mut local_unreachable = false;
for attempt in 0..=retries {
- if attempt > 0 {
- if let Err(error) = crate::cron::policy::get_policy(config, &job.id) {
- return (false, error.to_string());
- }
- }
let (success, output, agent_error) = match job.job_type {
JobType::Shell => {
let (success, output) = run_job_command(config, security, job).await;
diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rs
index 1b2da23abc..3a1ce85418 100644
--- a/crates/openhuman-core/src/cron/tools/run.rs
+++ b/crates/openhuman-core/src/cron/tools/run.rs
@@ -88,10 +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()));
- }
-
// Claim the job so this run cannot overlap a scheduled tick or a Run Now.
let Some(_run_guard) = cron::ops::try_acquire_run(&job.id) else {
return Ok(ToolResult::error(format!(