Skip to content

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

feat(cron): add job builder and scheduling schemas

Steven Enamakel6d2c36d69e83
diff --git a/crates/openhuman-core/src/cron/job_builder.rs b/crates/openhuman-core/src/cron/job_builder.rs
index 7a18e21f28..65c554431b 100644
--- a/crates/openhuman-core/src/cron/job_builder.rs
+++ b/crates/openhuman-core/src/cron/job_builder.rs
@@ -12,7 +12,7 @@ use {
crate::cron, tinyflows_schedule::types::delivery_mode, tinyflows_schedule::types::CronJob,
tinyflows_schedule::types::DeliveryConfig, tinyflows_schedule::types::JobOrigin,
tinyflows_schedule::types::Schedule, tinyflows_schedule::types::SessionTarget,
- tinyflows_schedule::AgentJobSpec,
+ tinyflows_schedule::validation, tinyflows_schedule::AgentJobSpec,
};
/// What an agent job creation request carries; everything defaults.
@@ -45,122 +45,19 @@ fn allowed_users_for_channel<'a>(config: &'a Config, channel: &str) -> Option<&'
}
}
-/// The default delivery for an agent job: back to the conversation it was
-/// created in, or the in-app stream when there is none.
-pub fn default_delivery(origin: Option<&JobOrigin>) -> DeliveryConfig {
- DeliveryConfig {
- mode: if origin.is_some() {
- delivery_mode::ORIGIN
- } else {
- delivery_mode::PROACTIVE
- }
- .to_string(),
- channel: None,
- to: None,
- best_effort: true,
- }
-}
-
-/// Reject a session target or delivery that needs a conversation the job does
-/// not have. Shared with the RPC `cron.add` path.
-pub fn check_origin_requirements(
- session_target: &SessionTarget,
- delivery: &DeliveryConfig,
- origin: Option<&JobOrigin>,
-) -> Result<(), String> {
- if origin.is_some() {
- return Ok(());
- }
- if delivery
- .mode
- .trim()
- .eq_ignore_ascii_case(delivery_mode::ORIGIN)
- {
- return Err(
- "delivery mode 'origin' needs the conversation the job is created from; this \
- job has none (use 'proactive' or 'announce')"
- .to_string(),
- );
- }
- if matches!(session_target, SessionTarget::Current) {
- return Err(
- "session_target 'current' needs the conversation the job is created from; this \
- job has none (use 'isolated')"
- .to_string(),
- );
- }
- Ok(())
-}
-
-/// Validate a `DeliveryConfig` at creation time.
-///
-/// `announce` needs `channel` and `to`, and `to` must be in the channel's
-/// `allowed_users` (no cross-tenant `to`, #928) unless it is exactly the
-/// reply target of the conversation the job is created from: the asker is the
-/// recipient. `origin` needs an origin. `proactive` and `none` are not
-/// channel-targeted.
-pub fn validate_delivery(
+/// Validate `delivery` at creation time: the module rules
+/// (`tinyflows_schedule::validation::validate_delivery`) plus the channel's
+/// configured `allowed_users`, which only the host knows.
+fn validate_delivery_for_config(
config: &Config,
delivery: &DeliveryConfig,
origin: Option<&JobOrigin>,
) -> Result<(), String> {
- let mode = delivery.mode.trim().to_ascii_lowercase();
- if mode == delivery_mode::ORIGIN {
- return check_origin_requirements(&SessionTarget::Isolated, delivery, origin);
- }
- if mode != delivery_mode::ANNOUNCE {
- return Ok(());
- }
-
- let channel = delivery
+ let allowed = delivery
.channel
.as_deref()
- .map(str::trim)
- .filter(|s| !s.is_empty())
- .ok_or_else(|| "delivery.channel is required for announce mode".to_string())?;
- let to = delivery
- .to
- .as_deref()
- .map(str::trim)
- .filter(|s| !s.is_empty())
- .ok_or_else(|| "delivery.to is required for announce mode".to_string())?;
-
- if let Some(JobOrigin::Channel {
- channel: origin_channel,
- reply_target,
- ..
- }) = origin
- {
- if channel.eq_ignore_ascii_case(origin_channel) && to == reply_target {
- tracing::debug!(
- channel = %channel,
- "[cron] announce target is the origin reply target; skipping allowed_users check"
- );
- return Ok(());
- }
- }
-
- // "web" announce is a degenerate case (web has no allowed_users gate).
- if channel.eq_ignore_ascii_case("web") {
- return Ok(());
- }
-
- match allowed_users_for_channel(config, channel) {
- Some([]) => Ok(()),
- Some(list) => {
- if list.iter().any(|u| u == to) {
- Ok(())
- } else {
- Err(format!(
- "delivery target '{to}' on channel '{channel}' is not in allowed_users \
- for that channel; refusing to schedule cross-tenant delivery"
- ))
- }
- }
- None => Err(format!(
- "delivery channel '{channel}' is not configured; cannot validate target"
- )),
- }
+ .and_then(|channel| allowed_users_for_channel(config, channel));
+ validation::validate_delivery(delivery, origin, allowed)
}
/// Create an agent job from the current turn.
@@ -184,10 +81,10 @@ pub fn create_agent_job(
});
let delivery = input
.delivery
- .unwrap_or_else(|| default_delivery(origin.as_ref()));
+ .unwrap_or_else(|| validation::default_delivery(origin.as_ref()));
- validate_delivery(config, &delivery, origin.as_ref())?;
- check_origin_requirements(&session_target, &delivery, origin.as_ref())?;
+ validate_delivery_for_config(config, &delivery, origin.as_ref())?;
+ validation::check_origin_requirements(&session_target, &delivery, origin.as_ref())?;
let uses_origin = matches!(session_target, SessionTarget::Current)
|| delivery
diff --git a/crates/openhuman-core/src/cron/mod.rs b/crates/openhuman-core/src/cron/mod.rs
index 0b2e1c02e1..659a2ca5b3 100644
--- a/crates/openhuman-core/src/cron/mod.rs
+++ b/crates/openhuman-core/src/cron/mod.rs
@@ -32,7 +32,7 @@ pub mod scheduler;
pub use ops as rpc;
#[cfg(test)]
pub use ops::update_cron_job;
-pub use ops::{add_once, add_once_at, parse_human_delay, pause_job, resume_job};
+pub use ops::{add_once, add_once_at, pause_job, resume_job};
// Pure scheduling logic (schedule model, next-run computation) lives in
// `tinyflows-schedule`; the host keeps the scheduler runtime, store, config
// and RPC, and re-exports the upstream names so `cron::Schedule` etc. hold.
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index adda107ff9..41bb461b10 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -45,7 +45,7 @@ pub(crate) fn try_acquire_run(job_id: &str) -> Option<ActiveRunGuard> {
}
pub fn add_once(config: &Config, delay: &str, command: &str) -> Result<CronJob> {
- let duration = parse_human_delay(delay)?;
+ let duration = tinyflows_schedule::validation::parse_human_delay(delay)?;
let at = chrono::Utc::now() + duration;
add_once_at(config, at, command)
}
@@ -141,29 +141,6 @@ pub fn update_cron_job(
update_job(config, id, patch)
}
-/// Parse a human-friendly delay string (e.g. "5m", "2h", "30s") into a
-/// `chrono::Duration`. Defaults to minutes when no unit is given.
-pub fn parse_human_delay(input: &str) -> Result<chrono::Duration> {
- let input = input.trim();
- if input.is_empty() {
- anyhow::bail!("delay must not be empty");
- }
- let split = input
- .find(|c: char| !c.is_ascii_digit())
- .unwrap_or(input.len());
- let (num, unit) = input.split_at(split);
- let amount: i64 = num.parse()?;
- let unit = if unit.is_empty() { "m" } else { unit };
- let duration = match unit {
- "s" => chrono::Duration::seconds(amount),
- "m" => chrono::Duration::minutes(amount),
- "h" => chrono::Duration::hours(amount),
- "d" => chrono::Duration::days(amount),
- _ => anyhow::bail!("unsupported delay unit '{unit}', use s/m/h/d"),
- };
- Ok(duration)
-}
-
pub async fn cron_list(config: &Config) -> Result<Outcome<Vec<CronJob>>, String> {
if !config.cron.enabled {
return Err("cron is disabled by config (cron.enabled=false)".to_string());
diff --git a/crates/openhuman-core/src/cron/schemas.rs b/crates/openhuman-core/src/cron/schemas.rs
index c3b030f952..1620961750 100644
--- a/crates/openhuman-core/src/cron/schemas.rs
+++ b/crates/openhuman-core/src/cron/schemas.rs
@@ -373,11 +373,11 @@ fn handle_add(params: Map<String, Value>) -> ControllerFuture {
.is_none()
.then(tinyflows_schedule::types::DeliveryConfig::default)
.or_else(|| {
- Some(crate::cron::job_builder::default_delivery(origin.as_ref()))
+ Some(tinyflows_schedule::validation::default_delivery(origin.as_ref()))
})
});
let delivery_cfg = delivery.clone().unwrap_or_default();
- crate::cron::job_builder::check_origin_requirements(
+ tinyflows_schedule::validation::check_origin_requirements(
&session_target,
&delivery_cfg,
origin.as_ref(),
diff --git a/crates/openhuman-core/src/tools/impl/system/schedule.rs b/crates/openhuman-core/src/tools/impl/system/schedule.rs
index 769550386d..33d4b2c8c9 100644
--- a/crates/openhuman-core/src/tools/impl/system/schedule.rs
+++ b/crates/openhuman-core/src/tools/impl/system/schedule.rs
@@ -305,7 +305,7 @@ impl ScheduleTool {
active_hours: None,
}
} else if let Some(delay_str) = delay {
- let at = Utc::now() + cron::parse_human_delay(delay_str)?;
+ let at = Utc::now() + tinyflows_schedule::validation::parse_human_delay(delay_str)?;
Schedule::At { at }
} else if let Some(at_str) = run_at {
let at: DateTime<Utc> = DateTime::parse_from_rfc3339(at_str)