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): delegate run execution to the scheduler module
Steven Enamakel0311733baa15
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rsindex 17e5fddef6..1b4be24888 100644--- a/crates/openhuman-core/src/cron/ops.rs+++ b/crates/openhuman-core/src/cron/ops.rs@@ -136,42 +136,28 @@ pub async fn cron_run(}crate::core::runtime::components::require_config(config, "tinyflows")?;- // Fail closed before queueing anything: an unreadable job refuses the run.- cron::limits::check_run(+ let job = load_job_for_run(config, job_id).map_err(|e| e.to_string())?;+ cron::limits::check_run(config, &job).map_err(|e| e.to_string())?;+ let run_id = uuid::Uuid::new_v4().to_string();+ let reply = cron::scheduler::run_job_now(+ ctx,config,- &load_job_for_run(config, job_id).map_err(|e| e.to_string())?,+ job,+ run_id,+ false,+ tinyflows_bus::schedule::ManualDelivery::Requested,)- .map_err(|e| e.to_string())?;-- // Guard against duplicate concurrent executions for the same job. The- // claim is taken before the job is loaded and moves into the task.- let Some(run_guard) = crate::cron::scheduler::try_acquire_run(job_id) else {- tracing::debug!(job_id, "[cron_run] rejected duplicate concurrent execution");- return Err(format!("cron job '{job_id}' is already running"));- };- let job = load_job_for_run(config, job_id).map_err(|e| e.to_string())?;-- // Insert a "queued" placeholder run record immediately so the frontend- // poller can observe the run as soon as the RPC returns — otherwise the- // run list stays unchanged until execute_job_now finishes.- let queued_at = chrono::Utc::now();- let _ = cron::record_run(config, &job.id, queued_at, queued_at, "queued", None, 0);-- let config_owned = config.clone();- let ctx_owned = ctx.clone();- let job_id_owned = job_id.to_string();-- tracing::debug!(job_id, "[cron_run] enqueuing background execution");-- // Spawn the execution as a background task so the RPC handler returns- // immediately instead of blocking for the full job duration.- tokio::spawn(async move {- // The claim is released even on panic or future cancellation.- let _guard = run_guard;-- tracing::debug!(job_id = %job_id_owned, "[cron_run] background task started");- run_and_record(&ctx_owned, &config_owned, &job).await;- });+ .await+ .map_err(|error| error.to_string())?;+ match reply {+ tinyflows_bus::schedule::RunJobReply::Accepted { .. } => {}+ tinyflows_bus::schedule::RunJobReply::Refused { reason } => {+ return Err(format!("cron job '{job_id}' was not queued: {reason:?}"));+ }+ tinyflows_bus::schedule::RunJobReply::Completed { .. } => {+ return Err("cron module completed a detached run before acknowledgment".to_string());+ }+ }Ok(Outcome::new(json!({@@ -192,71 +178,28 @@ pub async fn run_job_now() -> Result<(bool, String), String> {let job = load_job_for_run(config, job_id.trim()).map_err(|e| e.to_string())?;cron::limits::check_run(config, &job).map_err(|e| e.to_string())?;- let Some(_guard) = crate::cron::scheduler::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));- };- Ok(run_and_record(ctx, config, &job).await)-}--/// Execute `job` once (with its retry budget), deliver its output and persist-/// the run and its last-run state. The caller holds the job's run claim.-async fn run_and_record(- ctx: &crate::core::runtime::DispatchCtx,- config: &Config,- job: &CronJob,-) -> (bool, String) {let run_id = uuid::Uuid::new_v4().to_string();- let started_at = chrono::Utc::now();- let (execution_success, output) =- cron::scheduler::execute_job_now(ctx, config, job, &run_id).await;- if !execution_success && output.ends_with("is already running") {- // A scheduled run holds the job (the module's claim): nothing ran, so- // there is nothing to deliver or record, and the "queued" placeholder- // goes with the refused run.- tracing::debug!(job_id = %job.id, "[cron_run] refused: a scheduled run holds the job");- let _ = cron::delete_queued_runs(config, &job.id);- return (false, output);- }- let finished_at = chrono::Utc::now();- let duration_ms = (finished_at - started_at).num_milliseconds();- // Remove a "queued" placeholder before inserting the real result so no- // orphaned rows are left in the run history.- let _ = cron::delete_queued_runs(config, &job.id);- // Deliver via the same path as the scheduler loop so proactive messages,- // origin replies and alerts are sent on "Run Now" too, then record the run- // with how the delivery went.- let delivery_status = cron::scheduler::deliver_job(ctx, config, job, &run_id, &output).await;- let success = success_after_delivery(- execution_success,- &delivery_status,- job.delivery.best_effort,- );- let status = if success { "ok" } else { "error" };- tracing::debug!(job_id = %job.id, status, duration_ms, "[cron_run] run finished");- let _ = cron::record_run_with_delivery(+ match cron::scheduler::run_job_now(+ ctx,config,- &job.id,- started_at,- finished_at,- status,- Some(&output),- duration_ms,- Some(delivery_status),- );- let _ = cron::record_last_run(config, &job.id, finished_at, success, &output);- crate::cron::scheduler::run_delivery::publish_completion(ctx, job, success, &output);- (success, output)-}--/// A required delivery failure makes the run fail; best-effort and unrequested-/// deliveries leave the execution result unchanged.-pub(crate) fn success_after_delivery(- execution_success: bool,- delivery_status: &DeliveryStatus,- best_effort: bool,-) -> bool {- execution_success && (!matches!(delivery_status, DeliveryStatus::Failed) || best_effort)+ job.clone(),+ run_id,+ true,+ tinyflows_bus::schedule::ManualDelivery::Requested,+ )+ .await+ .map_err(|error| error.to_string())?+ {+ tinyflows_bus::schedule::RunJobReply::Completed {+ success, attempt, ..+ } => Ok((success, attempt.output)),+ tinyflows_bus::schedule::RunJobReply::Refused { reason } => {+ Err(format!("cron job '{}' was not admitted: {reason:?}", job.id))+ }+ tinyflows_bus::schedule::RunJobReply::Accepted { .. } => {+ Err("cron module accepted a synchronous run without completing it".to_string())+ }+ }}pub async fn cron_runs(diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rsindex f232050cfe..faac0c809f 100644--- a/crates/openhuman-core/src/cron/tools/run.rs+++ b/crates/openhuman-core/src/cron/tools/run.rs@@ -93,45 +93,39 @@ impl Tool for CronRunTool {if let Err(e) = cron::limits::check_run(&self.config, &job) {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::scheduler::try_acquire_run(&job.id) else {- return Ok(ToolResult::error(format!(- "cron job '{}' is already running",- job.id- )));- };-let started_at = Utc::now();let run_id = uuid::Uuid::new_v4().to_string();- let (execution_success, output) =- cron::scheduler::execute_job_now(&dispatch, &self.config, &job, &run_id).await;- let finished_at = Utc::now();- let duration_ms = (finished_at - started_at).num_milliseconds();- let delivery_status = cron::DeliveryStatus::NotRequested;- let success = cron::ops::success_after_delivery(- execution_success,- &delivery_status,- job.delivery.best_effort,- );- let status = if success { "ok" } else { "error" };-- // The output is returned to the calling agent below, so nothing is- // delivered here: an origin delivery would append to the transcript- // of the very session this tool call is running inside, and wait on- // that session's own turn lock.- let _ = cron::record_run_with_delivery(+ let reply = match cron::scheduler::run_job_now(+ &dispatch,&self.config,- &job.id,- started_at,- finished_at,- status,- Some(&output),- duration_ms,- Some(delivery_status),- );- let _ = cron::record_last_run(&self.config, &job.id, finished_at, success, &output);- crate::cron::scheduler::run_delivery::publish_completion(&dispatch, &job, success, &output);+ job.clone(),+ run_id,+ true,+ tinyflows_bus::schedule::ManualDelivery::NotRequested,+ )+ .await+ {+ Ok(reply) => reply,+ Err(error) => return Ok(ToolResult::error(error.to_string())),+ };+ let (success, output) = match reply {+ tinyflows_bus::schedule::RunJobReply::Completed {+ success, attempt, ..+ } => (success, attempt.output),+ tinyflows_bus::schedule::RunJobReply::Refused { reason } => {+ return Ok(ToolResult::error(format!(+ "cron job '{}' was not admitted: {reason:?}",+ job.id+ )));+ }+ tinyflows_bus::schedule::RunJobReply::Accepted { .. } => {+ return Ok(ToolResult::error(+ "cron module accepted a synchronous run without completing it".to_string(),+ ));+ }+ };+ let duration_ms = (Utc::now() - started_at).num_milliseconds();+ let status = if success { "ok" } else { "error" };let payload = json!({"job_id": job.id,