Skip to content

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

fix(cron): enforce run limits and profile job caps

Steven Enamakel562a5d7670b2
diff --git a/crates/openhuman-core/src/cron/bus_host_tests.rs b/crates/openhuman-core/src/cron/bus_host_tests.rs
index e941fef6a9..d5008b86a2 100644
--- a/crates/openhuman-core/src/cron/bus_host_tests.rs
+++ b/crates/openhuman-core/src/cron/bus_host_tests.rs
@@ -302,8 +302,16 @@ async fn schedule_trigger_checks_agent_only_limits_before_running_a_flow_handler
.unwrap();
assert!(!attempt.success);
- assert!(attempt.output.contains("only agent cron jobs"), "{}", attempt.output);
- assert_eq!(calls.load(Ordering::SeqCst), 0, "the flow handler never ran");
+ assert!(
+ attempt.output.contains("only agent cron jobs"),
+ "{}",
+ attempt.output
+ );
+ assert_eq!(
+ calls.load(Ordering::SeqCst),
+ 0,
+ "the flow handler never ran"
+ );
}
#[cfg(feature = "flows")]
diff --git a/crates/openhuman-core/src/cron/limits.rs b/crates/openhuman-core/src/cron/limits.rs
index 2b37b005e0..ece3d00665 100644
--- a/crates/openhuman-core/src/cron/limits.rs
+++ b/crates/openhuman-core/src/cron/limits.rs
@@ -13,9 +13,7 @@ use anyhow::{bail, Result};
use chrono::{Duration, Utc};
use crate::config::Config;
-use crate::cron::{
- delivery_mode, CronJob, CronJobPatch, DeliveryConfig, JobType, Schedule,
-};
+use crate::cron::{delivery_mode, CronJob, CronJobPatch, DeliveryConfig, JobType, Schedule};
/// Whether `config` restricts cron to agent jobs.
pub fn enforced(config: &Config) -> bool {
diff --git a/crates/openhuman-core/src/cron/limits_tests.rs b/crates/openhuman-core/src/cron/limits_tests.rs
index 1a25b7d6f0..0199da3105 100644
--- a/crates/openhuman-core/src/cron/limits_tests.rs
+++ b/crates/openhuman-core/src/cron/limits_tests.rs
@@ -151,14 +151,10 @@ fn concurrent_preflight_admissions_cannot_oversubscribe_the_store_cap() {
let barrier = Arc::clone(&barrier);
scope.spawn(move || {
barrier.wait();
- limits::check_new(
- &config,
- &JobType::Agent,
- &schedule,
- Some(&delivery),
- )
- .unwrap();
+ let preflight =
+ limits::check_new(&config, &JobType::Agent, &schedule, Some(&delivery));
barrier.wait();
+ preflight?;
crate::cron::client::store_blocking::<crate::cron::CronJob>(
&config,
tinyflows_bus::schedule::StoreOperation::AddAgent {
@@ -179,6 +175,14 @@ fn concurrent_preflight_admissions_cannot_oversubscribe_the_store_cap() {
assert_eq!(results.len(), 2);
assert_eq!(results.iter().filter(|result| result.is_ok()).count(), 1);
+ let rejected = results
+ .iter()
+ .find_map(|result| result.as_ref().err())
+ .unwrap();
+ assert!(
+ rejected.to_string().contains("active job limit"),
+ "unexpected admission error: {rejected}"
+ );
assert_eq!(crate::cron::list_jobs(&config).unwrap().len(), 1);
}
diff --git a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
index 57e2526e8a..afe2427f29 100644
--- a/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
+++ b/crates/openhuman-core/src/cron/scheduler/run_delivery.rs
@@ -137,6 +137,13 @@ pub(crate) async fn trigger(binding: &Binding, flow_id: &str, run_id: &str) -> A
}
};
binding.publish_triggered_once(&job, run_id);
+ if let Err(error) = crate::cron::limits::check_run(&config, &job) {
+ return Attempt {
+ success: false,
+ output: error.to_string(),
+ retryable: false,
+ };
+ }
if let Some(refusal) = kind_refusal(binding, &job.job_type) {
return Attempt {
success: false,
diff --git a/crates/openhuman-core/src/profiles/host_tests.rs b/crates/openhuman-core/src/profiles/host_tests.rs
index ef584ab5eb..4f71bf63df 100644
--- a/crates/openhuman-core/src/profiles/host_tests.rs
+++ b/crates/openhuman-core/src/profiles/host_tests.rs
@@ -430,11 +430,11 @@ async fn provisioning_installs_the_cron_cap_in_the_profile_scope() {
crate::cron::client::store_in::<crate::cron::CronJob>(&config, &scope, operation())
.await
.unwrap();
- let second = crate::cron::client::store_in::<crate::cron::CronJob>(
- &config,
- &scope,
- operation(),
- )
- .await;
- assert!(second.is_err(), "the profile store must enforce its provisioned cap");
+ let second =
+ crate::cron::client::store_in::<crate::cron::CronJob>(&config, &scope, operation()).await;
+ let error = second.expect_err("the profile store must enforce its provisioned cap");
+ assert!(
+ error.to_string().contains("active job limit"),
+ "unexpected admission error: {error}"
+ );
}
diff --git a/crates/openhuman-core/src/profiles/lifecycle.rs b/crates/openhuman-core/src/profiles/lifecycle.rs
index 9213239b0c..dcdc4e6d69 100644
--- a/crates/openhuman-core/src/profiles/lifecycle.rs
+++ b/crates/openhuman-core/src/profiles/lifecycle.rs
@@ -107,7 +107,24 @@ impl ProfileHost {
if self.registry.get(id).await?.is_some() {
return Ok(false);
}
- create_dirs(&self.layout_of(id))?;
+ let layout = self.layout_of(id);
+ create_dirs(&layout)?;
+ let config = self.forced_config(&layout, id);
+ if config.cron.agent_jobs_only {
+ let scope = crate::cron::documents_port::DocScope::for_scope(
+ &config,
+ crate::storage::scope_for_profile(id.as_str()),
+ );
+ crate::cron::client::store_in::<()>(
+ &config,
+ &scope,
+ tinyflows_bus::schedule::StoreOperation::SetMaxActiveJobs {
+ limit: Some(config.cron.max_active_jobs as usize),
+ },
+ )
+ .await
+ .map_err(|error| format!("could not install the profile's cron job cap: {error}"))?;
+ }
let meta = ProfileMeta {
profile_id: id.clone(),
created_at: unix_now(),