The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(cron): make policy reads fall back to defaults
Steven Enamakel96c91d7e3c9c
diff --git a/crates/openhuman-core/src/cron/policy.rs b/crates/openhuman-core/src/cron/policy.rsindex 2ee7b50c34..497da1fe95 100644--- a/crates/openhuman-core/src/cron/policy.rs+++ b/crates/openhuman-core/src/cron/policy.rs@@ -79,49 +79,30 @@ async fn declare(docs.ensure_collection(&CollectionSpec::new(POLICIES)).await}-/// 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,- })+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),+ }}-/// 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.+/// The policy stored for `job_id`; the default when none is.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?;- match docs.get(POLICIES, &id).await? {- Some(stored) => from_doc(&stored.doc),- None => Ok(JobPolicy::default()),- }+ Ok(docs+ .get(POLICIES, &id)+ .await?+ .map(|stored| from_doc(&stored.doc))+ .unwrap_or_default())});}let conn = open(config)?;@@ -216,6 +197,15 @@ 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;