Skip to content

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

fix(profiles): reconcile cron admission limits when profiles are opened

Steven Enamakel6bdf74690caa
diff --git a/crates/openhuman-core/src/cron/limits.rs b/crates/openhuman-core/src/cron/limits.rs
index ece3d00665..0722a861ed 100644
--- a/crates/openhuman-core/src/cron/limits.rs
+++ b/crates/openhuman-core/src/cron/limits.rs
@@ -52,7 +52,7 @@ fn check_delivery(delivery: &DeliveryConfig) -> Result<()> {
bail!("cron output can only return to the conversation that created the job")
}
-fn check_capacity(config: &Config) -> Result<()> {
+fn install_capacity_limit(config: &Config) -> Result<()> {
let max = config.cron.max_active_jobs as usize;
crate::cron::client::store_blocking::<()>(
config,
@@ -61,6 +61,24 @@ fn check_capacity(config: &Config) -> Result<()> {
Ok(())
}
+/// Clear a prior operator cap when enforcement is off. Backends without
+/// transactions could never install one, so they remain compatible without
+/// attempting an unsupported `SetMaxActiveJobs(None)` write.
+fn clear_capacity_limit_if_supported(config: &Config) -> Result<()> {
+ let scope = super::documents_port::DocScope::current(config)?;
+ if !scope.supports_transactions()? {
+ return Ok(());
+ }
+ crate::cron::client::blocking(config, move |config| async move {
+ crate::cron::client::store_in::<()>(
+ &config,
+ &scope,
+ tinyflows_bus::schedule::StoreOperation::SetMaxActiveJobs { limit: None },
+ )
+ .await
+ })
+}
+
/// Refuse a new job that breaks the limits. No-op unless [`enforced`].
pub(crate) fn check_new(
config: &Config,
@@ -69,6 +87,7 @@ pub(crate) fn check_new(
delivery: Option<&DeliveryConfig>,
) -> Result<()> {
if !enforced(config) {
+ clear_capacity_limit_if_supported(config)?;
return Ok(());
}
check_type(job_type)?;
@@ -76,12 +95,13 @@ pub(crate) fn check_new(
if let Some(delivery) = delivery {
check_delivery(delivery)?;
}
- check_capacity(config)
+ install_capacity_limit(config)
}
/// Refuse an update that breaks the limits. No-op unless [`enforced`].
pub(crate) fn check_patch(config: &Config, patch: &CronJobPatch) -> Result<()> {
if !enforced(config) {
+ clear_capacity_limit_if_supported(config)?;
return Ok(());
}
if patch.command.is_some() {
@@ -97,7 +117,7 @@ pub(crate) fn check_patch(config: &Config, patch: &CronJobPatch) -> Result<()> {
check_delivery(delivery)?;
}
if patch.enabled == Some(true) {
- check_capacity(config)?;
+ install_capacity_limit(config)?;
}
Ok(())
}
diff --git a/crates/openhuman-core/src/profiles/host.rs b/crates/openhuman-core/src/profiles/host.rs
index a01a9d2adc..9a1631171e 100644
--- a/crates/openhuman-core/src/profiles/host.rs
+++ b/crates/openhuman-core/src/profiles/host.rs
@@ -250,7 +250,9 @@ impl ProfileHost {
if self.registry.get(id).await?.is_none() {
return Err(format!("profile {id} is not provisioned"));
}
- self.serving_config(&self.layout_of(id), id)
+ let config = self.serving_config(&self.layout_of(id), id)?;
+ super::lifecycle::reconcile_cron_admission(&config, id).await?;
+ Ok(config)
}
/// Profile `id`, opening it — and taking its lease — if it is provisioned
@@ -327,6 +329,10 @@ impl ProfileHost {
let mut config = self
.serving_config(&layout, id)
.map_err(OpenError::Storage)?;
+ if let Err(error) = super::lifecycle::reconcile_cron_admission(&config, id).await {
+ profile_lease::release_all(self, vec![(id.clone(), grant)]).await;
+ return Err(OpenError::Storage(error));
+ }
if let Some(capabilities) = &self.capabilities {
capabilities.configure(&mut config);
}
diff --git a/crates/openhuman-core/src/profiles/lifecycle.rs b/crates/openhuman-core/src/profiles/lifecycle.rs
index dcdc4e6d69..fc0311dec0 100644
--- a/crates/openhuman-core/src/profiles/lifecycle.rs
+++ b/crates/openhuman-core/src/profiles/lifecycle.rs
@@ -37,6 +37,37 @@ use crate::config::Config;
use crate::core::runtime::CoreContext;
use crate::storage::lease::{LeaseError, LeaseGrant};
+/// Adopt or clear the schedule store's operator cap for an explicit profile
+/// scope. Clearing is skipped only when the backend cannot support a policy
+/// at all; the module refuses writes if a policy ledger already exists.
+pub(super) async fn reconcile_cron_admission(
+ config: &Config,
+ id: &ProfileId,
+) -> Result<(), String> {
+ let scope = crate::cron::documents_port::DocScope::for_scope(
+ config,
+ crate::storage::scope_for_profile(id.as_str()),
+ );
+ let limit = if config.cron.agent_jobs_only {
+ Some(config.cron.max_active_jobs as usize)
+ } else {
+ if !scope
+ .supports_transactions()
+ .map_err(|error| format!("checking profile cron store capabilities: {error}"))?
+ {
+ return Ok(());
+ }
+ None
+ };
+ crate::cron::client::store_in::<()>(
+ config,
+ &scope,
+ tinyflows_bus::schedule::StoreOperation::SetMaxActiveJobs { limit },
+ )
+ .await
+ .map_err(|error| format!("reconciling the profile's cron job cap: {error}"))
+}
+
/// One async lock per profile id. See the module docs.
#[derive(Default)]
pub struct ProfileLocks {
@@ -91,6 +122,11 @@ impl ProfileHost {
pub async fn provision(&self, id: &ProfileId) -> Result<bool, String> {
let _profile = self.lifecycle.lock(id).await;
if self.registry.get(id).await?.is_some() {
+ let grant = self.acquire_for_lifecycle(id, "provision").await?;
+ let config = self.forced_config(&self.layout_of(id), id);
+ let reconciled = reconcile_cron_admission(&config, id).await;
+ profile_lease::release_all(self, vec![(id.clone(), grant)]).await;
+ reconciled?;
log::debug!("[profiles] provision profile={id}: already provisioned");
return Ok(false);
}
@@ -110,21 +146,7 @@ impl ProfileHost {
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}"))?;
- }
+ reconcile_cron_admission(&config, id).await?;
let meta = ProfileMeta {
profile_id: id.clone(),
created_at: unix_now(),