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): split monolithic scheduler module into focused submodules
Steven Enamakel428a983eba7c
diff --git a/crates/openhuman-core/src/agent/prompts/render_helpers.rs b/crates/openhuman-core/src/agent/prompts/render_helpers.rsindex 0b146936c3..95eb13c2aa 100644--- a/crates/openhuman-core/src/agent/prompts/render_helpers.rs+++ b/crates/openhuman-core/src/agent/prompts/render_helpers.rs@@ -23,8 +23,8 @@ pub use section_renderers::{render_user_reflections, render_workspace,};pub use subagent::{render_subagent_system_prompt, render_subagent_system_prompt_with_format};+pub(crate) use workspace_files::write_agents_md_blocks;pub use workspace_files::{default_workspace_file_content, inject_inline_content, inject_snapshot_content,inject_workspace_file, inject_workspace_file_capped, sync_workspace_file,};-pub(crate) use workspace_files::write_agents_md_blocks;diff --git a/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs b/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rsindex 1970252727..5cf4b08537 100644--- a/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs+++ b/crates/openhuman-core/src/agent/prompts/render_helpers/subagent.rs@@ -197,31 +197,30 @@ pub fn render_subagent_system_prompt_with_format(// `extra_tools` — or the model has no way to know they exist.if !matches!(tool_call_format, ToolCallFormat::Native) {out.push_str("## Tools\n\n");- let render_one =- |out: &mut String, tool: &dyn crate::tools::Tool| match tool_call_format {- ToolCallFormat::PFormat => {- let sig = render_pformat_signature_for_box_tool(tool);- let _ = writeln!(- out,- "- **{}**: {}\n Call as: `{}`",- tool.name(),- tool.description(),- sig- );- }- ToolCallFormat::Json => {- let _ = writeln!(- out,- "- **{}**: {}\n Parameters: `{}`",- tool.name(),- tool.description(),- tool.parameters_schema()- );- }- ToolCallFormat::Native => {- // Unreachable — outer guard skips Native entirely.- }- };+ let render_one = |out: &mut String, tool: &dyn crate::tools::Tool| match tool_call_format {+ ToolCallFormat::PFormat => {+ let sig = render_pformat_signature_for_box_tool(tool);+ let _ = writeln!(+ out,+ "- **{}**: {}\n Call as: `{}`",+ tool.name(),+ tool.description(),+ sig+ );+ }+ ToolCallFormat::Json => {+ let _ = writeln!(+ out,+ "- **{}**: {}\n Parameters: `{}`",+ tool.name(),+ tool.description(),+ tool.parameters_schema()+ );+ }+ ToolCallFormat::Native => {+ // Unreachable — outer guard skips Native entirely.+ }+ };for &i in allowed_indices {let Some(tool) = parent_tools.get(i) else {tracing::warn!(diff --git a/crates/openhuman-core/src/agent/prompts/render_helpers/workspace_files.rs b/crates/openhuman-core/src/agent/prompts/render_helpers/workspace_files.rsindex f8e9b36a28..958373ee28 100644--- a/crates/openhuman-core/src/agent/prompts/render_helpers/workspace_files.rs+++ b/crates/openhuman-core/src/agent/prompts/render_helpers/workspace_files.rs@@ -147,7 +147,7 @@ pub fn inject_inline_content(prompt: &mut String, label: &str, content: &str, ma////// Used by both [`crate::agent::prompts::sections::AgentsInstructionsSection`] (the default //// sub-agent builder chains) and the narrow sub-agent renderer-/// ([`render_subagent_system_prompt_with_format`]) so the two paths never+/// ([`super::subagent::render_subagent_system_prompt_with_format`]) so the two paths never/// drift. The heading is emitted only when at least one layer carries content;/// the global layer renders first, then the local/project layer. Each layer is/// injected via [`inject_inline_content`] under its own `###` sub-heading and@@ -228,7 +228,7 @@ pub fn inject_snapshot_content(prompt: &mut String, label: &str, content: &str,/// file bytes at call time, and `max_chars`. Callers must invoke this/// once per session — re-reading mid-session breaks the inference/// backend's automatic prefix cache. See the byte-stability note on-/// [`render_subagent_system_prompt`].+/// [`super::subagent::render_subagent_system_prompt`].pub fn inject_workspace_file_capped(prompt: &mut String,workspace_dir: &Path,diff --git a/crates/openhuman-core/src/cron/scheduler.rs b/crates/openhuman-core/src/cron/scheduler.rsindex e15e875fcd..505615ff69 100644--- a/crates/openhuman-core/src/cron/scheduler.rs+++ b/crates/openhuman-core/src/cron/scheduler.rs@@ -1,6 +1,226 @@+//! Background scheduler loop for cron jobs: polls the store for due jobs,+//! runs them with bounded concurrency, persists results, and emits health+//! signals. Job-type execution, retry, delivery, and persistence live in the+//! submodules below.++mod agent_run;+mod delivery;+mod failure_classification;+mod retry;+mod run_record;+mod shell_job;+#[cfg(test)]#[path = "scheduler_tests.rs"]mod tests;-include!("scheduler_part_01.rs");-include!("scheduler_part_02.rs");-include!("scheduler_part_03.rs");++#[allow(unused_imports)]+use agent_run::*;+#[allow(unused_imports)]+use delivery::*;+#[allow(unused_imports)]+use failure_classification::*;+#[allow(unused_imports)]+use retry::*;+#[allow(unused_imports)]+use run_record::*;+#[allow(unused_imports)]+use shell_job::*;++pub use delivery::deliver_job;+pub use retry::execute_job_now;++use crate::config::Config;+use crate::core::bus::BUS;+use crate::core::events::DomainEvent;+use crate::cron::{due_jobs, CronJob};+use crate::security::SecurityPolicy;+use anyhow::Result;+use chrono::Utc;+use futures_util::{stream, StreamExt};+use std::sync::Arc;+use tokio::time::{self, Duration};++const MIN_POLL_SECONDS: u64 = 5;++pub async fn run(config: Config) -> Result<()> {+ // Ensure the global event bus is initialized so cron delivery events+ // are not silently dropped. This is a no-op if already initialized.+ crate::core::bus::init().await.expect("bus init");+ crate::platform::health::bus::register_health_subscriber();++ let poll_secs = config.reliability.scheduler_poll_secs.max(MIN_POLL_SECONDS);+ let mut interval = time::interval(Duration::from_secs(poll_secs));+ let security = Arc::new(SecurityPolicy::from_config(+ &config.autonomy,+ &config.workspace_dir,+ &config.action_dir,+ ));++ BUS.publish(DomainEvent::SystemStartup {+ component: "scheduler".to_string(),+ });++ // Track the most recently *emitted* scheduler health so we only+ // publish `HealthChanged` on a state transition. Without this the+ // bus would carry a steady `healthy: true` event every poll+ // interval — typically 30 s, forever — churn for any subscriber+ // that logs / persists / reacts to health events. `None` means+ // "nothing emitted yet for this run", so the first successful tick+ // is treated as a transition and emits.+ let mut last_emitted_health: Option<bool> = None;++ loop {+ interval.tick().await;+ tick_once(&config, &security, &mut last_emitted_health).await;+ }+}++/// Single poll cycle of the scheduler loop, extracted so tests can drive+/// it without owning `tokio::time::interval`.+///+/// Emits a `scheduler` health signal in three cases:+/// - Poll itself failed (DB read) → `healthy: false` with the DB error.+/// - Poll succeeded, queue empty or not → `healthy: true` (#3312+/// recovery signal). Without this, a single transient job failure+/// that flipped the component to `error` via [`process_due_jobs`]+/// would stay there indefinitely while the queue was idle — no later+/// event would clear it, the health endpoint would keep returning+/// 503, and Docker would mark the container `unhealthy` for hours+/// until a manual restart. Tick-level "still polling" beats+/// job-level success as the recovery signal because the queue is+/// empty most of the time.+/// - Per-job results (handled inside `process_due_jobs`) continue to+/// flip the component back to `healthy: false` on a failure; the+/// next tick that survives the DB read will re-flip it to+/// `healthy: true`, exactly the auto-recovery behaviour the Docker+/// health check needs.+pub(crate) async fn tick_once(+ config: &Config,+ security: &Arc<SecurityPolicy>,+ last_emitted_health: &mut Option<bool>,+) {+ tracing::debug!("[cron:scheduler] tick poll begin");+ let jobs = match due_jobs(config, Utc::now()) {+ Ok(jobs) => jobs,+ Err(e) => {+ tracing::warn!("[cron:scheduler] tick poll db_error: {e}");+ // Transition-only emission: only publish on the first+ // failure after a previous healthy (or unknown) state.+ // Repeat DB failures stay quiet so subscribers don't see+ // an event-storm during a long outage.+ if *last_emitted_health != Some(false) {+ BUS.publish(DomainEvent::HealthChanged {+ component: "scheduler".to_string(),+ healthy: false,+ message: Some(e.to_string()),+ });+ *last_emitted_health = Some(false);+ }+ return;+ }+ };++ let due_count = jobs.len();+ // Transition-only emission for the recovery / healthy signal: a+ // long idle stretch with no transitions stays silent on the bus,+ // so subscribers don't pay per-poll work for a steady `healthy:+ // true` event every poll interval — the nit oxoxDev caught on+ // #3329. The very first successful tick after boot (or after a+ // failure) is the one that fires; subsequent successful ticks+ // are no-ops on the wire.+ if *last_emitted_health != Some(true) {+ tracing::debug!(+ "[cron:scheduler] tick poll ok due_count={due_count} (recovery signal: healthy=true)"+ );+ BUS.publish(DomainEvent::HealthChanged {+ component: "scheduler".to_string(),+ healthy: true,+ message: None,+ });+ *last_emitted_health = Some(true);+ } else {+ tracing::trace!(+ "[cron:scheduler] tick poll ok due_count={due_count} (steady state, no event)"+ );+ }++ if due_count == 0 {+ tracing::trace!("[cron:scheduler] tick end (no due jobs)");+ return;+ }++ process_due_jobs(config, security, jobs).await;+ tracing::debug!("[cron:scheduler] tick end due_count={due_count} (jobs processed)");++ // `process_due_jobs` itself may have published `healthy: false` on+ // a job failure, but it does so directly on the bus without+ // touching our local tracker. Reset so the next successful tick+ // is again treated as a transition and re-emits `healthy: true` —+ // exactly the auto-recovery behaviour #3312 requires.+ *last_emitted_health = None;+}++async fn process_due_jobs(config: &Config, security: &Arc<SecurityPolicy>, jobs: Vec<CronJob>) {+ let max_concurrent = config.scheduler.max_concurrent.max(1);+ let mut in_flight = stream::iter(jobs.into_iter().map(|job| {+ let config = config.clone();+ let security = Arc::clone(security);+ async move { execute_and_persist_job(&config, security.as_ref(), &job).await }+ }))+ .buffer_unordered(max_concurrent);++ while let Some((job_id, success, failure_message)) = in_flight.next().await {+ if success {+ BUS.publish(DomainEvent::HealthChanged {+ component: "scheduler".to_string(),+ healthy: true,+ message: None,+ });+ } else {+ BUS.publish(DomainEvent::HealthChanged {+ component: "scheduler".to_string(),+ healthy: false,+ message: Some(failure_message.unwrap_or_else(|| format!("job {job_id} failed"))),+ });+ }+ }+}++async fn execute_and_persist_job(+ config: &Config,+ security: &SecurityPolicy,+ job: &CronJob,+) -> (String, bool, Option<String>) {+ warn_if_high_frequency_agent_job(job);++ let started_at = Utc::now();++ BUS.publish(DomainEvent::CronJobTriggered {+ job_id: job.id.clone(),+ job_name: job.name.clone().unwrap_or_default(),+ job_type: format!("{:?}", job.job_type),+ });++ let (execution_success, output) = execute_job_with_retry(config, security, job).await;+ let finished_at = Utc::now();+ let success = persist_job_result(+ config,+ job,+ execution_success,+ &output,+ started_at,+ finished_at,+ )+ .await;++ BUS.publish(DomainEvent::CronJobCompleted {+ job_id: job.id.clone(),+ success,+ output: crate::util::truncate_with_ellipsis(&output, 512),+ });+ let failure_message =+ (!success).then(|| crate::util::truncate_with_ellipsis(&output, 256));++ (job.id.clone(), success, failure_message)+}diff --git a/crates/openhuman-core/src/cron/scheduler_part_02.rs b/crates/openhuman-core/src/cron/scheduler/agent_run.rssimilarity index 52%rename from crates/openhuman-core/src/cron/scheduler_part_02.rsrename to crates/openhuman-core/src/cron/scheduler/agent_run.rsindex d1c1e64edc..a4b0d1b336 100644--- a/crates/openhuman-core/src/cron/scheduler_part_02.rs+++ b/crates/openhuman-core/src/cron/scheduler/agent_run.rs@@ -1,326 +1,20 @@--async fn execute_job_with_retry(- config: &Config,- security: &SecurityPolicy,- job: &CronJob,-) -> (bool, String) {- let mut last_output = String::new();- let mut last_agent_error: Option<String> = None;- let retries = config.reliability.scheduler_retries;- let mut backoff_ms = config.reliability.provider_backoff_ms.max(200);- let mut session_expired = false;- let mut credits_exhausted = false;- let mut budget_exhausted = false;- let mut key_unset = false;- let mut local_unreachable = false;-- for attempt in 0..=retries {- let (success, output, agent_error) = match job.job_type {- JobType::Shell => {- let (success, output) = run_job_command(config, security, job).await;- (success, output, None)- }- JobType::Agent => run_agent_job(config, job).await,- JobType::Flow => {- let (success, output) = run_flow_schedule_job(job);- (success, output, None)- }- };- last_output = output;- if agent_error.is_some() {- last_agent_error = agent_error;- }-- if success {- return (true, last_output);- }-- if last_output.starts_with("blocked by security policy:") {- // Deterministic policy violations are not retryable.- return (false, last_output);- }-- if is_session_expired_failure(- &job.job_type,- last_agent_error.as_deref(),- last_output.as_str(),- ) {- // Halt on the first occurrence — the inference layer already- // published `SessionExpired`, retries cannot recover until the- // user re-auths, and the classifier considers this expected- // user state (TAURI-RUST-N). See `is_session_expired_failure`- // for the full rationale.- session_expired = true;- break;- }-- if is_insufficient_credits_failure(- &job.job_type,- last_agent_error.as_deref(),- last_output.as_str(),- ) {- // Halt on the first occurrence — a BYO provider 402 (out of- // balance) is permanent across the backoff loop, and the- // provider emit site already demoted it from Sentry. Skipping- // the retries-exhausted `report_error` below keeps the residual- // off Sentry at source, independent of the `before_send` chain- // (TAURI-RUST-514). See `is_insufficient_credits_failure`.- // Metadata-only log (no raw provider body — see CLAUDE.md).- log::debug!(- "[cron] action=halt_on_insufficient_credits_402 job_id={} attempt={} retries={}",- job.id.as_str(),- attempt,- retries- );- credits_exhausted = true;- break;- }-- if is_budget_exhausted_failure(- &job.job_type,- last_agent_error.as_deref(),- last_output.as_str(),- ) {- // Halt on the first occurrence — a managed-backend budget 400- // (USER_INSUFFICIENT_CREDITS) is permanent across the backoff- // loop. The tag-gated `is_budget_event` before_send filter never- // matches this cron re-report, so suppressing the report here- // keeps it off Sentry at source (TAURI-RUST-BMW). See- // `is_budget_exhausted_failure`. Metadata-only log (no raw body).- log::debug!(- "[cron] action=halt_on_budget_exhausted_400 job_id={} attempt={} retries={}",- job.id.as_str(),- attempt,- retries- );- budget_exhausted = true;- break;- }-- if is_api_key_unset_failure(- &job.job_type,- last_agent_error.as_deref(),- last_output.as_str(),- ) {- // Halt on the first occurrence — a configured provider with no- // API key fails deterministically at the credential guard before- // any HTTP, so the missing key is permanent across the backoff- // loop. The bare cron `report_error` below bypasses the- // `ApiKeyMissing` `expected_error_kind` demotion, so suppressing- // here keeps the residual off Sentry at source (TAURI-RUST-HCK).- // The failure stays visible to the user via the alerts tab- // (`push_cron_alert`) + run history. See `is_api_key_unset_failure`.- // Metadata-only log (no raw provider body — see CLAUDE.md).- log::debug!(- "[cron] action=halt_on_api_key_unset job_id={} attempt={} retries={}",- job.id.as_str(),- attempt,- retries- );- key_unset = true;- break;- }-- if is_local_provider_unreachable_failure(- &job.job_type,- last_agent_error.as_deref(),- last_output.as_str(),- ) {- // Halt on the first occurrence — a local LLM provider refusing the- // loopback connection (LM Studio / Ollama not running) cannot- // recover across the backoff loop, and the provider/agent emit- // sites already demoted it from Sentry (`LoopbackUnavailable`).- // The bare cron `report_error` below bypasses that demotion, so- // suppressing here keeps the residual off Sentry at source- // (TAURI-RUST-12K). The failure stays visible via the run history- // + cron alert. See `is_local_provider_unreachable_failure`.- // Metadata-only log (no raw provider body — see CLAUDE.md).- log::debug!(- "[cron] action=halt_on_local_provider_unreachable job_id={} attempt={} retries={}",- job.id.as_str(),- attempt,- retries- );- local_unreachable = true;- break;- }-- if attempt < retries {- let jitter_ms = u64::from(Utc::now().timestamp_subsec_millis() % 250);- time::sleep(Duration::from_millis(backoff_ms + jitter_ms)).await;- backoff_ms = (backoff_ms.saturating_mul(2)).min(30_000);- }- }-- // Permanent user-config / billing states are demoted at source: halt the- // loop and skip the retries-exhausted report, independent of the tag-gated- // before_send filters that the cron re-report does not match. Covers BYO- // 402 out-of-credit + managed-backend 400 out-of-budget (TAURI-RUST-514 /- // -BMW) and a configured provider with no API key (TAURI-RUST-HCK). The- // `session_expired` (TAURI-RUST-N) and `local_unreachable` (a local LLM- // server refusing the loopback connection, TAURI-RUST-12K) halts are the- // same shape — suppress the bypassing bare report — but carry no- // user-config remediation surface, so they gate the report directly rather- // than routing through `permanent_config_halt`'s UserErrorCenter swap.- let permanent_config_halt = credits_exhausted || budget_exhausted || key_unset;- if matches!(job.job_type, JobType::Agent)- && !session_expired- && !local_unreachable- && !permanent_config_halt- {- let report_message = last_agent_error.as_deref().unwrap_or(last_output.as_str());- crate::core::observability::report_error(- report_message,- "cron",- "agent_job",- &[- ("job_id", job.id.as_str()),- ("agent_id", job.agent_id.as_deref().unwrap_or("none")),- (- "session_target",- agent_session_target_tag(&job.session_target),- ),- ("failure", "retries_exhausted"),- ],- );- } else if matches!(job.job_type, JobType::Agent) && permanent_config_halt {- // Suppressed the retries-exhausted Sentry report for a permanent- // user-config / billing state. Metadata-only breadcrumb so the- // suppression is diagnosable in production without the raw provider body.- let (reason, user_error_kind) = if credits_exhausted {- ("insufficient_credits_402", "insufficient_credits")- } else if budget_exhausted {- ("budget_exhausted_400", "budget_exceeded")- } else {- ("api_key_unset", "api_key_missing")- };- log::debug!(- "[cron] action=suppress_retries_exhausted_report reason={reason} job_id={} retries={}",- job.id.as_str(),- retries- );- // Replace the generic agent-failure copy with the specific, actionable- // (static, leak-safe) reason so the hoisted /notifications alert + run- // history tell the user the exact next step rather than "Something went- // wrong" (CodeRabbit #4169). The raw `last_agent_error` chain is NEVER- // surfaced here — only the `&'static str` constants from- // `permanent_halt_message`.- last_output = permanent_halt_message(credits_exhausted, budget_exhausted).to_string();- // Also surface the actionable state to the UserErrorCenter so the user- // can fix it (add an API key / top up credits / raise the budget) even- // with no chat thread open. Broadcast-only + metadata-only — see- // `publish_cron_user_error` (#4165 / TAURI-RUST-HCK follow-up).- publish_cron_user_error(user_error_kind);- }-- (false, last_output)-}--/// Static, leak-safe actionable alert copy for a permanent cron halt state.-/// Returns the user-facing `/notifications` body matching the halt reason —-/// `&'static str` only, so it can never carry a raw error field (the no-leak-/// contract that governs [`agent_error_to_user_message`]). Precedence mirrors-/// the halt classifiers' evaluation order: credits → budget → missing key.-fn permanent_halt_message(credits_exhausted: bool, budget_exhausted: bool) -> &'static str {- if credits_exhausted {- CRON_HALT_INSUFFICIENT_CREDITS_MESSAGE- } else if budget_exhausted {- CRON_HALT_BUDGET_EXHAUSTED_MESSAGE- } else {- CRON_HALT_API_KEY_UNSET_MESSAGE- }-}--/// Surface a permanent cron user-config / billing halt to every connected-/// client's UserErrorCenter.-///-/// Broadcasts a metadata-only `user_error` web-channel event to the `"system"`-/// room (which every socket auto-joins). The payload carries only the stable-/// `kind` token in `error_type` — one of `api_key_missing` / `insufficient_credits`-/// / `budget_exceeded`, mirroring the frontend `UserErrorKind` discriminator —-/// plus `error_source = "cron"`. It NEVER carries the raw provider body (see the-/// metadata-only rule in CLAUDE.md), so no secrets / PII leave the core.-///-/// The frontend `socketService` listens for `user_error` and routes it through-/// the same classifier the chat runtime uses, so a background (no-delivery) job-/// failure is no longer silent — it lands in the shell's UserErrorCenter with a-/// deep-link action even though no chat thread is active.-fn publish_cron_user_error(kind: &str) {- log::debug!("[cron] action=surface_user_error kind={kind}");- crate::web_chat::publish_web_channel_event(crate::core::socketio::WebChannelEvent {- event: "user_error".to_string(),- client_id: "system".to_string(),- error_type: Some(kind.to_string()),- error_source: Some("cron".to_string()),- ..Default::default()- });-}--async fn process_due_jobs(config: &Config, security: &Arc<SecurityPolicy>, jobs: Vec<CronJob>) {- let max_concurrent = config.scheduler.max_concurrent.max(1);- let mut in_flight = stream::iter(jobs.into_iter().map(|job| {- let config = config.clone();- let security = Arc::clone(security);- async move { execute_and_persist_job(&config, security.as_ref(), &job).await }- }))- .buffer_unordered(max_concurrent);-- while let Some((job_id, success, failure_message)) = in_flight.next().await {- if success {- BUS.publish(DomainEvent::HealthChanged {- component: "scheduler".to_string(),- healthy: true,- message: None,- });- } else {- BUS.publish(DomainEvent::HealthChanged {- component: "scheduler".to_string(),- healthy: false,- message: Some(failure_message.unwrap_or_else(|| format!("job {job_id} failed"))),- });- }- }-}--async fn execute_and_persist_job(- config: &Config,- security: &SecurityPolicy,- job: &CronJob,-) -> (String, bool, Option<String>) {- warn_if_high_frequency_agent_job(job);-- let started_at = Utc::now();-- BUS.publish(DomainEvent::CronJobTriggered {- job_id: job.id.clone(),- job_name: job.name.clone().unwrap_or_default(),- job_type: format!("{:?}", job.job_type),- });-- let (execution_success, output) = execute_job_with_retry(config, security, job).await;- let finished_at = Utc::now();- let success = persist_job_result(- config,- job,- execution_success,- &output,- started_at,- finished_at,- )- .await;-- BUS.publish(DomainEvent::CronJobCompleted {- job_id: job.id.clone(),- success,- output: crate::util::truncate_with_ellipsis(&output, 512),- });- let failure_message =- (!success).then(|| crate::util::truncate_with_ellipsis(&output, 256));-- (job.id.clone(), success, failure_message)-}--async fn run_agent_job(config: &Config, job: &CronJob) -> (bool, String, Option<String>) {+//! Execution of `JobType::Agent` and `JobType::Flow` jobs: agent construction+//! (definition / profile attribution) and the single scheduled turn.++use super::delivery::is_morning_briefing_job;+use super::failure_classification::classify_agent_anyhow_for_user;+use crate::agent::Agent;+use crate::config::Config;+use crate::core::bus::BUS;+use crate::core::events::DomainEvent;+use crate::cron::{CronJob, SessionTarget};++/// Recency window the morning briefing installs around its turn so Composio+/// task-fetch tools only surface tasks created/changed in the last day. Read+/// by the `composio_execute` handler via `current_task_recency_window`.+pub(super) const MORNING_BRIEFING_TASK_RECENCY_SECS: u64 = 24 * 60 * 60;++pub(super) async fn run_agent_job(config: &Config, job: &CronJob) -> (bool, String, Option<String>) {let name = job.name.clone().unwrap_or_else(|| "cron-job".to_string());let prompt = job.prompt.clone().unwrap_or_default();let prefixed_prompt = format!("[cron:{} {name}] {prompt}", job.id);@@ -517,7 +211,7 @@ async fn run_agent_job(config: &Config, job: &CronJob) -> (bool, String, Option</// actual `flows::ops::flows_run` happens asynchronously in/// `flows::bus::FlowTriggerSubscriber`, which is the sole consumer of this/// event (kept out of the cron domain so cron stays flow-agnostic).-fn run_flow_schedule_job(job: &CronJob) -> (bool, String) {+pub(super) fn run_flow_schedule_job(job: &CronJob) -> (bool, String) {let flow_id = job.command.clone();tracing::info!(target: "flows",@@ -536,7 +230,7 @@ fn run_flow_schedule_job(job: &CronJob) -> (bool, String) {/// Placeholder recorded in run history when an agent job succeeds but returns/// no text. Never delivered to chat — used only for the run-history record.-const EMPTY_AGENT_OUTPUT: &str = "agent job executed";+pub(super) const EMPTY_AGENT_OUTPUT: &str = "agent job executed";/// Resolve the agent profile a cron job is attributed to, if any.///@@ -545,7 +239,7 @@ const EMPTY_AGENT_OUTPUT: &str = "agent job executed";/// runs the job without a profile rather than failing it (2b). Profile-store/// failures are returned: attribution must not fail open when the scheduler/// cannot determine whether the referenced profile still exists.-fn resolve_cron_profile(+pub(super) fn resolve_cron_profile(config: &Config,job: &CronJob,) -> anyhow::Result<Option<crate::agent::profiles::AgentProfile>> {@@ -571,12 +265,12 @@ fn resolve_cron_profile(}}-struct BuiltCronAgent {+pub(super) struct BuiltCronAgent {agent: Agent,profile: Option<crate::agent::profiles::AgentProfile>,}-fn apply_cron_profile_runtime_defaults(+pub(super) fn apply_cron_profile_runtime_defaults(config: &Config,job: &CronJob,profile: &crate::agent::profiles::AgentProfile,@@ -595,7 +289,7 @@ fn apply_cron_profile_runtime_defaults(effective}-fn build_agent_for_cron_job(config: &Config, job: &CronJob) -> anyhow::Result<BuiltCronAgent> {+pub(super) fn build_agent_for_cron_job(config: &Config, job: &CronJob) -> anyhow::Result<BuiltCronAgent> {// 2b — profile attribution. When the job names a profile that still exists,// build the run under it via the SAME profile-aware session path the task// dispatcher uses (`from_config_for_agent_with_profile`), so the run inheritsdiff --git a/crates/openhuman-core/src/cron/scheduler/delivery.rs b/crates/openhuman-core/src/cron/scheduler/delivery.rsnew file mode 100644index 0000000000..1cac048030--- /dev/null+++ b/crates/openhuman-core/src/cron/scheduler/delivery.rs@@ -0,0 +1,256 @@+//! Output delivery for completed cron runs: chat delivery (proactive /+//! announce), the alerts-tab notification, and the morning-briefing copy.++use super::agent_run::EMPTY_AGENT_OUTPUT;+use super::failure_classification::AGENT_JOB_USER_FAILURE_MESSAGE;+use crate::config::Config;+use crate::core::bus::BUS;+use crate::core::events::DomainEvent;+use crate::cron::{CronJob, DeliveryConfig, JobType};+use anyhow::Result;+use chrono::Utc;++pub(super) const MORNING_BRIEFING_AGENT_ID: &str = "morning_briefing";+pub(super) const MORNING_BRIEFING_FAILURE_NOTIFICATION: &str = "Morning briefing could not run. Check your AI provider, API key, and connected apps, then run it again from Settings > Cron Jobs.";++pub(super) fn is_morning_briefing_job(job: &CronJob) -> bool {+ job.name.as_deref() == Some(MORNING_BRIEFING_AGENT_ID)+ || job.agent_id.as_deref() == Some(MORNING_BRIEFING_AGENT_ID)+}++pub(super) fn strip_openhuman_link_markup(input: &str) -> String {+ const OPEN_TAG: &str = "<openhuman-link";+ const CLOSE_TAG: &str = "</openhuman-link>";++ let mut output = String::with_capacity(input.len());+ let mut rest = input;++ while let Some(start) = rest.find(OPEN_TAG) {+ output.push_str(&rest[..start]);+ let tag_and_after = &rest[start..];++ let Some(open_end) = tag_and_after.find('>') else {+ output.push_str(tag_and_after);+ return output;+ };+ let label_and_after = &tag_and_after[open_end + 1..];++ let Some(close_start) = label_and_after.find(CLOSE_TAG) else {+ output.push_str(tag_and_after);+ return output;+ };++ output.push_str(&label_and_after[..close_start]);+ rest = &label_and_after[close_start + CLOSE_TAG.len()..];+ }++ output.push_str(rest);+ output+}++pub(super) fn cron_alert_body(job: &CronJob, output: &str) -> String {+ let trimmed = output.trim();+ if matches!(job.job_type, JobType::Agent)+ && trimmed == AGENT_JOB_USER_FAILURE_MESSAGE+ && is_morning_briefing_job(job)+ {+ return MORNING_BRIEFING_FAILURE_NOTIFICATION.to_string();+ }++ let body = strip_openhuman_link_markup(output);+ crate::util::truncate_with_ellipsis(&body, 512)+}++/// Public entry point for delivering a job's output via the configured+/// delivery mode (proactive / announce). Called by `cron_run` ("Run Now")+/// so manual runs also push notifications and alerts. Manual runs are treated+/// as `success = true` so the user always sees the result they explicitly+/// triggered (empty output is still skipped).+pub async fn deliver_job(config: &Config, job: &CronJob, output: &str) {+ if let Err(e) = deliver_if_configured(config, job, output, true).await {+ if job.delivery.best_effort {+ tracing::warn!("[cron] delivery failed (best_effort, Run Now): {e}");+ } else {+ tracing::warn!("[cron] delivery failed (Run Now): {e}");+ }+ }+}++/// True when an agent job produced no meaningful text — blank output or the+/// [`EMPTY_AGENT_OUTPUT`] placeholder. Such runs are never injected into chat.+pub(super) fn cron_output_is_empty(output: &str) -> bool {+ output.trim().is_empty() || output == EMPTY_AGENT_OUTPUT+}++/// Whether a cron job's output should be injected into the user's chat thread.+/// Skips failed runs and empty/placeholder output; failures still surface in+/// the alerts tab and run history (handled separately by the caller).+pub(super) fn should_deliver_cron_output_to_chat(success: bool, output: &str) -> bool {+ success && !cron_output_is_empty(output)+}++/// Whether a completed cron run should surface in the alerts tab+/// (`/notifications`). Failures stay visible even when they produce no output;+/// only successful-but-empty runs are dropped entirely.+pub(super) fn cron_result_should_alert(success: bool, output: &str) -> bool {+ !success || !cron_output_is_empty(output)+}++pub(super) async fn deliver_if_configured(+ config: &Config,+ job: &CronJob,+ output: &str,+ success: bool,+) -> Result<()> {+ let delivery: &DeliveryConfig = &job.delivery;++ // Don't post failed or empty cron runs into the user's chat: a failed turn+ // (e.g. a transient network error) would otherwise deliver a canned+ // "Something went wrong" message into the conversation with no user+ // message behind it. Failures still reach the alerts tab (`push_cron_alert`)+ // and the run-history / health signals, which are recorded elsewhere.+ let is_empty = cron_output_is_empty(output);+ let deliver_to_chat = should_deliver_cron_output_to_chat(success, output);+ if !deliver_to_chat {+ tracing::debug!(+ job_id = %job.id,+ success,+ is_empty,+ "[cron] skipping chat delivery for failed/empty cron run"+ );+ }++ // A failed run must stay visible in /notifications regardless of delivery+ // mode — a no-delivery agent job that halts on a permanent config/billing+ // state (e.g. a keyless provider, TAURI-RUST-HCK) would otherwise fail+ // silently. A *successful* non-empty run only alerts in the delivering+ // modes (proactive/announce); a `none`-mode success stays silent (its+ // output lives in last_output only — the cron contract), so we don't spam+ // explicitly-silent background jobs with an unread alert every interval+ // (Codex #4166).+ let mode = delivery.mode.trim().to_ascii_lowercase();+ let delivers = matches!(mode.as_str(), "proactive" | "announce");+ let alert_to_notifications =+ cron_result_should_alert(success, output) && (!success || delivers);+ let alert_body = if is_empty {+ "Scheduled job failed without output."+ } else {+ output+ };++ match mode.as_str() {+ // Proactive delivery — the channels module decides where to send.+ // Used by morning briefings, welcome messages, and other+ // user-facing proactive agents.+ "proactive" => {+ if deliver_to_chat {+ let source = format!("cron:{}", job.id);+ tracing::debug!(+ job_id = %job.id,+ source = %source,+ "[cron] publishing ProactiveMessageRequested event"+ );+ BUS.publish(DomainEvent::ProactiveMessageRequested {+ source,+ message: output.to_string(),+ job_name: job.name.clone(),+ });+ }+ }++ // Announce delivery — the cron job specifies the exact channel+ // and target. Used for explicit channel-targeted output.+ "announce" if deliver_to_chat => {+ let channel = delivery+ .channel+ .as_deref()+ .ok_or_else(|| anyhow::anyhow!("delivery.channel is required for announce mode"))?;+ let target = delivery+ .to+ .as_deref()+ .ok_or_else(|| anyhow::anyhow!("delivery.to is required for announce mode"))?;++ tracing::debug!(+ job_id = %job.id,+ channel = %channel,+ target = %target,+ "[cron] publishing CronDeliveryRequested event"+ );+ BUS.publish(DomainEvent::CronDeliveryRequested {+ job_id: job.id.clone(),+ channel: channel.to_string(),+ target: target.to_string(),+ output: output.to_string(),+ });+ }++ // No delivery configured — output is stored in last_output only.+ // The failure still reaches the alerts tab via the hoisted+ // `push_cron_alert` below.+ _ => {}+ }++ // Surface in the alerts tab (/notifications) for any result that isn't a+ // successful-but-empty run — INDEPENDENT of delivery mode. A failed cron+ // job must stay visible to the user even when it has no chat delivery+ // configured (the common case: a keyless agent job failing "API key not+ // set", TAURI-RUST-HCK). Previously this fired only inside the proactive /+ // announce arms, so no-delivery jobs failed silently in /notifications.+ if alert_to_notifications {+ push_cron_alert(config, job, alert_body);+ }++ Ok(())+}++/// Insert a notification into the alerts tab for a completed cron job.+pub(super) fn push_cron_alert(config: &Config, job: &CronJob, output: &str) {+ use crate::desktop::notifications::store as notif_store;+ use crate::desktop::notifications::types::{+ IntegrationNotification, NotificationStatus,+ };++ let name = job.name.as_deref().unwrap_or("Cron job");+ let body = cron_alert_body(job, output);++ let notification = IntegrationNotification {+ id: uuid::Uuid::new_v4().to_string(),+ provider: "cron".to_string(),+ account_id: Some(job.id.clone()),+ title: name.to_string(),+ body,+ raw_payload: serde_json::json!({+ "job_id": job.id,+ "job_name": job.name,+ "delivery_mode": job.delivery.mode,+ }),+ importance_score: Some(0.65),+ triage_action: Some("react".to_string()),+ triage_reason: Some("Scheduled delivery".to_string()),+ status: NotificationStatus::Unread,+ received_at: Utc::now(),+ scored_at: Some(Utc::now()),+ };++ match notif_store::insert_if_not_recent(config, ¬ification) {+ Ok(true) => {+ tracing::debug!(+ job_id = %job.id,+ "[cron] pushed notification alert to alerts tab"+ );+ }+ Ok(false) => {+ tracing::debug!(+ job_id = %job.id,+ "[cron] skipped duplicate notification alert"+ );+ }+ Err(e) => {+ tracing::warn!(+ job_id = %job.id,+ error = %e,+ "[cron] failed to push notification alert"+ );+ }+ }+}diff --git a/crates/openhuman-core/src/cron/scheduler_part_01.rs b/crates/openhuman-core/src/cron/scheduler/failure_classification.rssimilarity index 58%rename from crates/openhuman-core/src/cron/scheduler_part_01.rsrename to crates/openhuman-core/src/cron/scheduler/failure_classification.rsindex 7ae242d70b..127dee1624 100644--- a/crates/openhuman-core/src/cron/scheduler_part_01.rs+++ b/crates/openhuman-core/src/cron/scheduler/failure_classification.rs@@ -1,43 +1,22 @@-use crate::core::bus::BUS;-use crate::core::events::DomainEvent;+//! Classification of agent-job failures into static, leak-safe user copy and+//! the permanent-halt predicates that stop the retry loop early.+use crate::agent::error::AgentError;-use crate::agent::Agent;-use crate::config::Config;-use crate::cron::{- due_jobs, record_last_run, record_run, remove_job, reschedule_after_run, runs_closer_than,- update_job, CronJob, CronJobPatch, DeliveryConfig, JobType, Schedule, SessionTarget,- TooFrequent, MIN_AGENT_JOB_INTERVAL,-};-use crate::security::SecurityPolicy;-use anyhow::Result;-use chrono::{DateTime, Utc};-use futures_util::{stream, StreamExt};-use std::process::Stdio;-use std::sync::Arc;-use tokio::process::Command;-use tokio::time::{self, Duration};+use crate::cron::JobType;-const MIN_POLL_SECONDS: u64 = 5;-const SHELL_JOB_TIMEOUT_SECS: u64 = 120;-const AGENT_JOB_USER_FAILURE_MESSAGE: &str = "Something went wrong. Please try again.\nThis error has been reported. You can also report it on Discord.\n<openhuman-link path=\"community/discord-report\">Report on Discord</openhuman-link>";+pub(super) const AGENT_JOB_USER_FAILURE_MESSAGE: &str = "Something went wrong. Please try again.\nThis error has been reported. You can also report it on Discord.\n<openhuman-link path=\"community/discord-report\">Report on Discord</openhuman-link>";// Actionable, static failure copy for the three permanent cron halt states// (TAURI-RUST-514 / -BMW / -HCK). Surfaced verbatim in the alerts tab + run// history in place of the generic `AGENT_JOB_USER_FAILURE_MESSAGE`, so a user// whose job halts on a permanent config/billing state sees the exact next step// instead of "Something went wrong". Static `&'static str` only — they carry no// `err` fields, honouring the no-leak contract on `agent_error_to_user_message`.-const CRON_HALT_API_KEY_UNSET_MESSAGE: &str =+pub(super) const CRON_HALT_API_KEY_UNSET_MESSAGE: &str ="No API key is set for your AI provider. Add it in Connections \u{2192} API keys \u{2192} LLM, then re-run.";-const CRON_HALT_INSUFFICIENT_CREDITS_MESSAGE: &str =+pub(super) const CRON_HALT_INSUFFICIENT_CREDITS_MESSAGE: &str ="Your AI provider is out of credits. Top it up or update its key in Connections \u{2192} API keys \u{2192} LLM.";-const CRON_HALT_BUDGET_EXHAUSTED_MESSAGE: &str =+pub(super) const CRON_HALT_BUDGET_EXHAUSTED_MESSAGE: &str ="You've reached your managed AI budget. Raise it in Settings \u{2192} Billing.";-const MORNING_BRIEFING_AGENT_ID: &str = "morning_briefing";-const MORNING_BRIEFING_FAILURE_NOTIFICATION: &str = "Morning briefing could not run. Check your AI provider, API key, and connected apps, then run it again from Settings > Cron Jobs.";-/// Recency window the morning briefing installs around its turn so Composio-/// task-fetch tools only surface tasks created/changed in the last day. Read-/// by the `composio_execute` handler via `current_task_recency_window`.-const MORNING_BRIEFING_TASK_RECENCY_SECS: u64 = 24 * 60 * 60;/// Map a typed [`AgentError`] to a canned, user-facing message for cron-job/// failure notifications.@@ -53,7 +32,7 @@ const MORNING_BRIEFING_TASK_RECENCY_SECS: u64 = 24 * 60 * 60;/// Variants for which we have no concrete user action (e.g./// [`AgentError::ToolExecutionError`], [`AgentError::Other`]) fall back to/// [`AGENT_JOB_USER_FAILURE_MESSAGE`], preserving today's behaviour.-fn agent_error_to_user_message(err: &AgentError) -> &'static str {+pub(super) fn agent_error_to_user_message(err: &AgentError) -> &'static str {match err {AgentError::ProviderError { retryable: true, .. } => {"The model provider is temporarily unavailable. The next run will retry automatically."@@ -105,207 +84,13 @@ fn agent_error_to_user_message(err: &AgentError) -> &'static str {/// user-facing message. If the underlying error is a typed [`AgentError`],/// route through [`agent_error_to_user_message`]; otherwise fall back to the/// generic message.-fn classify_agent_anyhow_for_user(err: &anyhow::Error) -> &'static str {+pub(super) fn classify_agent_anyhow_for_user(err: &anyhow::Error) -> &'static str {match err.downcast_ref::<AgentError>() {Some(agent_err) => agent_error_to_user_message(agent_err),None => AGENT_JOB_USER_FAILURE_MESSAGE,}}-fn agent_session_target_tag(target: &SessionTarget) -> &'static str {- match target {- SessionTarget::Main => "main",- SessionTarget::Isolated => "isolated",- }-}--fn is_morning_briefing_job(job: &CronJob) -> bool {- job.name.as_deref() == Some(MORNING_BRIEFING_AGENT_ID)- || job.agent_id.as_deref() == Some(MORNING_BRIEFING_AGENT_ID)-}--fn strip_openhuman_link_markup(input: &str) -> String {- const OPEN_TAG: &str = "<openhuman-link";- const CLOSE_TAG: &str = "</openhuman-link>";-- let mut output = String::with_capacity(input.len());- let mut rest = input;-- while let Some(start) = rest.find(OPEN_TAG) {- output.push_str(&rest[..start]);- let tag_and_after = &rest[start..];-- let Some(open_end) = tag_and_after.find('>') else {- output.push_str(tag_and_after);- return output;- };- let label_and_after = &tag_and_after[open_end + 1..];-- let Some(close_start) = label_and_after.find(CLOSE_TAG) else {- output.push_str(tag_and_after);- return output;- };-- output.push_str(&label_and_after[..close_start]);- rest = &label_and_after[close_start + CLOSE_TAG.len()..];- }-- output.push_str(rest);- output-}--fn cron_alert_body(job: &CronJob, output: &str) -> String {- let trimmed = output.trim();- if matches!(job.job_type, JobType::Agent)- && trimmed == AGENT_JOB_USER_FAILURE_MESSAGE- && is_morning_briefing_job(job)- {- return MORNING_BRIEFING_FAILURE_NOTIFICATION.to_string();- }-- let body = strip_openhuman_link_markup(output);- crate::util::truncate_with_ellipsis(&body, 512)-}--pub async fn run(config: Config) -> Result<()> {- // Ensure the global event bus is initialized so cron delivery events- // are not silently dropped. This is a no-op if already initialized.- crate::core::bus::init().await.expect("bus init");- crate::platform::health::bus::register_health_subscriber();-- let poll_secs = config.reliability.scheduler_poll_secs.max(MIN_POLL_SECONDS);- let mut interval = time::interval(Duration::from_secs(poll_secs));- let security = Arc::new(SecurityPolicy::from_config(- &config.autonomy,- &config.workspace_dir,- &config.action_dir,- ));-- BUS.publish(DomainEvent::SystemStartup {- component: "scheduler".to_string(),- });-- // Track the most recently *emitted* scheduler health so we only- // publish `HealthChanged` on a state transition. Without this the- // bus would carry a steady `healthy: true` event every poll- // interval — typically 30 s, forever — churn for any subscriber- // that logs / persists / reacts to health events. `None` means- // "nothing emitted yet for this run", so the first successful tick- // is treated as a transition and emits.- let mut last_emitted_health: Option<bool> = None;-- loop {- interval.tick().await;- tick_once(&config, &security, &mut last_emitted_health).await;- }-}--/// Single poll cycle of the scheduler loop, extracted so tests can drive-/// it without owning `tokio::time::interval`.-///-/// Emits a `scheduler` health signal in three cases:-/// - Poll itself failed (DB read) → `healthy: false` with the DB error.-/// - Poll succeeded, queue empty or not → `healthy: true` (#3312-/// recovery signal). Without this, a single transient job failure-/// that flipped the component to `error` via [`process_due_jobs`]-/// would stay there indefinitely while the queue was idle — no later-/// event would clear it, the health endpoint would keep returning-/// 503, and Docker would mark the container `unhealthy` for hours-/// until a manual restart. Tick-level "still polling" beats-/// job-level success as the recovery signal because the queue is-/// empty most of the time.-/// - Per-job results (handled inside `process_due_jobs`) continue to-/// flip the component back to `healthy: false` on a failure; the-/// next tick that survives the DB read will re-flip it to-/// `healthy: true`, exactly the auto-recovery behaviour the Docker-/// health check needs.-pub(crate) async fn tick_once(- config: &Config,- security: &Arc<SecurityPolicy>,- last_emitted_health: &mut Option<bool>,-) {- tracing::debug!("[cron:scheduler] tick poll begin");- let jobs = match due_jobs(config, Utc::now()) {- Ok(jobs) => jobs,- Err(e) => {- tracing::warn!("[cron:scheduler] tick poll db_error: {e}");- // Transition-only emission: only publish on the first- // failure after a previous healthy (or unknown) state.- // Repeat DB failures stay quiet so subscribers don't see- // an event-storm during a long outage.- if *last_emitted_health != Some(false) {- BUS.publish(DomainEvent::HealthChanged {- component: "scheduler".to_string(),- healthy: false,- message: Some(e.to_string()),- });- *last_emitted_health = Some(false);- }- return;- }- };-- let due_count = jobs.len();- // Transition-only emission for the recovery / healthy signal: a- // long idle stretch with no transitions stays silent on the bus,- // so subscribers don't pay per-poll work for a steady `healthy:- // true` event every poll interval — the nit oxoxDev caught on- // #3329. The very first successful tick after boot (or after a- // failure) is the one that fires; subsequent successful ticks- // are no-ops on the wire.- if *last_emitted_health != Some(true) {- tracing::debug!(- "[cron:scheduler] tick poll ok due_count={due_count} (recovery signal: healthy=true)"- );- BUS.publish(DomainEvent::HealthChanged {- component: "scheduler".to_string(),- healthy: true,- message: None,- });- *last_emitted_health = Some(true);- } else {- tracing::trace!(- "[cron:scheduler] tick poll ok due_count={due_count} (steady state, no event)"- );- }-- if due_count == 0 {- tracing::trace!("[cron:scheduler] tick end (no due jobs)");- return;- }-- process_due_jobs(config, security, jobs).await;- tracing::debug!("[cron:scheduler] tick end due_count={due_count} (jobs processed)");-- // `process_due_jobs` itself may have published `healthy: false` on- // a job failure, but it does so directly on the bus without- // touching our local tracker. Reset so the next successful tick- // is again treated as a transition and re-emits `healthy: true` —- // exactly the auto-recovery behaviour #3312 requires.- *last_emitted_health = None;-}--/// Public entry point for delivering a job's output via the configured-/// delivery mode (proactive / announce). Called by `cron_run` ("Run Now")-/// so manual runs also push notifications and alerts. Manual runs are treated-/// as `success = true` so the user always sees the result they explicitly-/// triggered (empty output is still skipped).-pub async fn deliver_job(config: &Config, job: &CronJob, output: &str) {- if let Err(e) = deliver_if_configured(config, job, output, true).await {- if job.delivery.best_effort {- tracing::warn!("[cron] delivery failed (best_effort, Run Now): {e}");- } else {- tracing::warn!("[cron] delivery failed (Run Now): {e}");- }- }-}--pub async fn execute_job_now(config: &Config, job: &CronJob) -> (bool, String) {- let security =- SecurityPolicy::from_config(&config.autonomy, &config.workspace_dir, &config.action_dir);- execute_job_with_retry(config, &security, job).await-}-/// Did this failed agent-job attempt hit the backend session-expired state?////// When the OpenHuman backend returns 401 because the user's app JWT has@@ -344,7 +129,7 @@ pub async fn execute_job_now(config: &Config, job: &CronJob) -> (bool, String) {/// 401-shaped string don't go through the inference layer's/// `SessionExpired` publish, so halting them based on stdout would skip/// retries the operator may want.-fn is_session_expired_failure(+pub(super) fn is_session_expired_failure(job_type: &JobType,last_agent_error: Option<&str>,last_output: &str,@@ -373,7 +158,7 @@ fn is_session_expired_failure(/// Routes on `last_agent_error` first (the raw anyhow chain carrying the/// provider's 402 wire body), falling back to `last_output`, identical to/// [`is_session_expired_failure`]. Restricted to `JobType::Agent`.-fn is_insufficient_credits_failure(+pub(super) fn is_insufficient_credits_failure(job_type: &JobType,last_agent_error: Option<&str>,last_output: &str,@@ -399,7 +184,7 @@ fn is_insufficient_credits_failure(/// does not carry — so the residual leaks here. Halt on the first occurrence/// and skip the report, reusing the same body classifier as that filter/// (`provider::is_budget_exhausted_message`). Restricted to `JobType::Agent`.-fn is_budget_exhausted_failure(+pub(super) fn is_budget_exhausted_failure(job_type: &JobType,last_agent_error: Option<&str>,last_output: &str,@@ -422,7 +207,7 @@ fn is_budget_exhausted_failure(/// `expected_error_kind` demotion (that only runs on the `report_error_or_expected`/// path), so we suppress at source here — mirroring -514 / -BMW. Delegates to/// the single-source matcher so the wording cannot drift from the emit site.-fn is_api_key_unset_failure(+pub(super) fn is_api_key_unset_failure(job_type: &JobType,last_agent_error: Option<&str>,last_output: &str,@@ -459,7 +244,7 @@ fn is_api_key_unset_failure(/// anyhow chain carrying the wire message) and `last_output` (the surfaced user/// message), because some provider paths preserve only one of those shapes./// Restricted to `JobType::Agent`.-fn is_local_provider_unreachable_failure(+pub(super) fn is_local_provider_unreachable_failure(job_type: &JobType,last_agent_error: Option<&str>,last_output: &str,@@ -474,8 +259,23 @@ fn is_local_provider_unreachable_failure(|| is_local_provider_no_model_loaded_message(last_output)}-fn is_local_provider_no_model_loaded_message(signal: &str) -> bool {+pub(super) fn is_local_provider_no_model_loaded_message(signal: &str) -> bool {let lower = signal.to_ascii_lowercase();(lower.contains("local inference server") && lower.contains("no model loaded"))|| lower.contains("no models loaded")}++/// Static, leak-safe actionable alert copy for a permanent cron halt state.+/// Returns the user-facing `/notifications` body matching the halt reason —+/// `&'static str` only, so it can never carry a raw error field (the no-leak+/// contract that governs [`agent_error_to_user_message`]). Precedence mirrors+/// the halt classifiers' evaluation order: credits → budget → missing key.+pub(super) fn permanent_halt_message(credits_exhausted: bool, budget_exhausted: bool) -> &'static str {+ if credits_exhausted {+ CRON_HALT_INSUFFICIENT_CREDITS_MESSAGE+ } else if budget_exhausted {+ CRON_HALT_BUDGET_EXHAUSTED_MESSAGE+ } else {+ CRON_HALT_API_KEY_UNSET_MESSAGE+ }+}diff --git a/crates/openhuman-core/src/cron/scheduler/retry.rs b/crates/openhuman-core/src/cron/scheduler/retry.rsnew file mode 100644index 0000000000..3788f4a4e9--- /dev/null+++ b/crates/openhuman-core/src/cron/scheduler/retry.rs@@ -0,0 +1,269 @@+//! The per-job retry loop with exponential backoff, its permanent-halt+//! short-circuits, and the retries-exhausted observability report.++use super::agent_run::{run_agent_job, run_flow_schedule_job};+use super::failure_classification::{+ is_api_key_unset_failure, is_budget_exhausted_failure, is_insufficient_credits_failure,+ is_local_provider_unreachable_failure, is_session_expired_failure, permanent_halt_message,+};+use super::shell_job::run_job_command;+use crate::config::Config;+use crate::cron::{CronJob, JobType, SessionTarget};+use crate::security::SecurityPolicy;+use chrono::Utc;+use tokio::time::{self, Duration};++pub(super) fn agent_session_target_tag(target: &SessionTarget) -> &'static str {+ match target {+ SessionTarget::Main => "main",+ SessionTarget::Isolated => "isolated",+ }+}++pub async fn execute_job_now(config: &Config, job: &CronJob) -> (bool, String) {+ let security =+ SecurityPolicy::from_config(&config.autonomy, &config.workspace_dir, &config.action_dir);+ execute_job_with_retry(config, &security, job).await+}++pub(super) async fn execute_job_with_retry(+ config: &Config,+ security: &SecurityPolicy,+ job: &CronJob,+) -> (bool, String) {+ let mut last_output = String::new();+ let mut last_agent_error: Option<String> = None;+ let retries = config.reliability.scheduler_retries;+ let mut backoff_ms = config.reliability.provider_backoff_ms.max(200);+ let mut session_expired = false;+ let mut credits_exhausted = false;+ let mut budget_exhausted = false;+ let mut key_unset = false;+ let mut local_unreachable = false;++ for attempt in 0..=retries {+ let (success, output, agent_error) = match job.job_type {+ JobType::Shell => {+ let (success, output) = run_job_command(config, security, job).await;+ (success, output, None)+ }+ JobType::Agent => run_agent_job(config, job).await,+ JobType::Flow => {+ let (success, output) = run_flow_schedule_job(job);+ (success, output, None)+ }+ };+ last_output = output;+ if agent_error.is_some() {+ last_agent_error = agent_error;+ }++ if success {+ return (true, last_output);+ }++ if last_output.starts_with("blocked by security policy:") {+ // Deterministic policy violations are not retryable.+ return (false, last_output);+ }++ if is_session_expired_failure(+ &job.job_type,+ last_agent_error.as_deref(),+ last_output.as_str(),+ ) {+ // Halt on the first occurrence — the inference layer already+ // published `SessionExpired`, retries cannot recover until the+ // user re-auths, and the classifier considers this expected+ // user state (TAURI-RUST-N). See `is_session_expired_failure`+ // for the full rationale.+ session_expired = true;+ break;+ }++ if is_insufficient_credits_failure(+ &job.job_type,+ last_agent_error.as_deref(),+ last_output.as_str(),+ ) {+ // Halt on the first occurrence — a BYO provider 402 (out of+ // balance) is permanent across the backoff loop, and the+ // provider emit site already demoted it from Sentry. Skipping+ // the retries-exhausted `report_error` below keeps the residual+ // off Sentry at source, independent of the `before_send` chain+ // (TAURI-RUST-514). See `is_insufficient_credits_failure`.+ // Metadata-only log (no raw provider body — see CLAUDE.md).+ log::debug!(+ "[cron] action=halt_on_insufficient_credits_402 job_id={} attempt={} retries={}",+ job.id.as_str(),+ attempt,+ retries+ );+ credits_exhausted = true;+ break;+ }++ if is_budget_exhausted_failure(+ &job.job_type,+ last_agent_error.as_deref(),+ last_output.as_str(),+ ) {+ // Halt on the first occurrence — a managed-backend budget 400+ // (USER_INSUFFICIENT_CREDITS) is permanent across the backoff+ // loop. The tag-gated `is_budget_event` before_send filter never+ // matches this cron re-report, so suppressing the report here+ // keeps it off Sentry at source (TAURI-RUST-BMW). See+ // `is_budget_exhausted_failure`. Metadata-only log (no raw body).+ log::debug!(+ "[cron] action=halt_on_budget_exhausted_400 job_id={} attempt={} retries={}",+ job.id.as_str(),+ attempt,+ retries+ );+ budget_exhausted = true;+ break;+ }++ if is_api_key_unset_failure(+ &job.job_type,+ last_agent_error.as_deref(),+ last_output.as_str(),+ ) {+ // Halt on the first occurrence — a configured provider with no+ // API key fails deterministically at the credential guard before+ // any HTTP, so the missing key is permanent across the backoff+ // loop. The bare cron `report_error` below bypasses the+ // `ApiKeyMissing` `expected_error_kind` demotion, so suppressing+ // here keeps the residual off Sentry at source (TAURI-RUST-HCK).+ // The failure stays visible to the user via the alerts tab+ // (`push_cron_alert`) + run history. See `is_api_key_unset_failure`.+ // Metadata-only log (no raw provider body — see CLAUDE.md).+ log::debug!(+ "[cron] action=halt_on_api_key_unset job_id={} attempt={} retries={}",+ job.id.as_str(),+ attempt,+ retries+ );+ key_unset = true;+ break;+ }++ if is_local_provider_unreachable_failure(+ &job.job_type,+ last_agent_error.as_deref(),+ last_output.as_str(),+ ) {+ // Halt on the first occurrence — a local LLM provider refusing the+ // loopback connection (LM Studio / Ollama not running) cannot+ // recover across the backoff loop, and the provider/agent emit+ // sites already demoted it from Sentry (`LoopbackUnavailable`).+ // The bare cron `report_error` below bypasses that demotion, so+ // suppressing here keeps the residual off Sentry at source+ // (TAURI-RUST-12K). The failure stays visible via the run history+ // + cron alert. See `is_local_provider_unreachable_failure`.+ // Metadata-only log (no raw provider body — see CLAUDE.md).+ log::debug!(+ "[cron] action=halt_on_local_provider_unreachable job_id={} attempt={} retries={}",+ job.id.as_str(),+ attempt,+ retries+ );+ local_unreachable = true;+ break;+ }++ if attempt < retries {+ let jitter_ms = u64::from(Utc::now().timestamp_subsec_millis() % 250);+ time::sleep(Duration::from_millis(backoff_ms + jitter_ms)).await;+ backoff_ms = (backoff_ms.saturating_mul(2)).min(30_000);+ }+ }++ // Permanent user-config / billing states are demoted at source: halt the+ // loop and skip the retries-exhausted report, independent of the tag-gated+ // before_send filters that the cron re-report does not match. Covers BYO+ // 402 out-of-credit + managed-backend 400 out-of-budget (TAURI-RUST-514 /+ // -BMW) and a configured provider with no API key (TAURI-RUST-HCK). The+ // `session_expired` (TAURI-RUST-N) and `local_unreachable` (a local LLM+ // server refusing the loopback connection, TAURI-RUST-12K) halts are the+ // same shape — suppress the bypassing bare report — but carry no+ // user-config remediation surface, so they gate the report directly rather+ // than routing through `permanent_config_halt`'s UserErrorCenter swap.+ let permanent_config_halt = credits_exhausted || budget_exhausted || key_unset;+ if matches!(job.job_type, JobType::Agent)+ && !session_expired+ && !local_unreachable+ && !permanent_config_halt+ {+ let report_message = last_agent_error.as_deref().unwrap_or(last_output.as_str());+ crate::core::observability::report_error(+ report_message,+ "cron",+ "agent_job",+ &[+ ("job_id", job.id.as_str()),+ ("agent_id", job.agent_id.as_deref().unwrap_or("none")),+ (+ "session_target",+ agent_session_target_tag(&job.session_target),+ ),+ ("failure", "retries_exhausted"),+ ],+ );+ } else if matches!(job.job_type, JobType::Agent) && permanent_config_halt {+ // Suppressed the retries-exhausted Sentry report for a permanent+ // user-config / billing state. Metadata-only breadcrumb so the+ // suppression is diagnosable in production without the raw provider body.+ let (reason, user_error_kind) = if credits_exhausted {+ ("insufficient_credits_402", "insufficient_credits")+ } else if budget_exhausted {+ ("budget_exhausted_400", "budget_exceeded")+ } else {+ ("api_key_unset", "api_key_missing")+ };+ log::debug!(+ "[cron] action=suppress_retries_exhausted_report reason={reason} job_id={} retries={}",+ job.id.as_str(),+ retries+ );+ // Replace the generic agent-failure copy with the specific, actionable+ // (static, leak-safe) reason so the hoisted /notifications alert + run+ // history tell the user the exact next step rather than "Something went+ // wrong" (CodeRabbit #4169). The raw `last_agent_error` chain is NEVER+ // surfaced here — only the `&'static str` constants from+ // `permanent_halt_message`.+ last_output = permanent_halt_message(credits_exhausted, budget_exhausted).to_string();+ // Also surface the actionable state to the UserErrorCenter so the user+ // can fix it (add an API key / top up credits / raise the budget) even+ // with no chat thread open. Broadcast-only + metadata-only — see+ // `publish_cron_user_error` (#4165 / TAURI-RUST-HCK follow-up).+ publish_cron_user_error(user_error_kind);+ }++ (false, last_output)+}++/// Surface a permanent cron user-config / billing halt to every connected+/// client's UserErrorCenter.+///+/// Broadcasts a metadata-only `user_error` web-channel event to the `"system"`+/// room (which every socket auto-joins). The payload carries only the stable+/// `kind` token in `error_type` — one of `api_key_missing` / `insufficient_credits`+/// / `budget_exceeded`, mirroring the frontend `UserErrorKind` discriminator —+/// plus `error_source = "cron"`. It NEVER carries the raw provider body (see the+/// metadata-only rule in CLAUDE.md), so no secrets / PII leave the core.+///+/// The frontend `socketService` listens for `user_error` and routes it through+/// the same classifier the chat runtime uses, so a background (no-delivery) job+/// failure is no longer silent — it lands in the shell's UserErrorCenter with a+/// deep-link action even though no chat thread is active.+pub(super) fn publish_cron_user_error(kind: &str) {+ log::debug!("[cron] action=surface_user_error kind={kind}");+ crate::web_chat::publish_web_channel_event(crate::core::socketio::WebChannelEvent {+ event: "user_error".to_string(),+ client_id: "system".to_string(),+ error_type: Some(kind.to_string()),+ error_source: Some("cron".to_string()),+ ..Default::default()+ });+}diff --git a/crates/openhuman-core/src/cron/scheduler/run_record.rs b/crates/openhuman-core/src/cron/scheduler/run_record.rsnew file mode 100644index 0000000000..44a6d7046a--- /dev/null+++ b/crates/openhuman-core/src/cron/scheduler/run_record.rs@@ -0,0 +1,115 @@+//! Persisting a finished run: delivery, run history, one-shot termination,+//! rescheduling, and the high-frequency agent-job warning.++use super::delivery::deliver_if_configured;+use crate::config::Config;+use crate::cron::{+ record_last_run, record_run, remove_job, reschedule_after_run, runs_closer_than, update_job,+ CronJob, CronJobPatch, JobType, Schedule, TooFrequent, MIN_AGENT_JOB_INTERVAL,+};+use chrono::{DateTime, Utc};++pub(super) async fn persist_job_result(+ config: &Config,+ job: &CronJob,+ mut success: bool,+ output: &str,+ started_at: DateTime<Utc>,+ finished_at: DateTime<Utc>,+) -> bool {+ let duration_ms = (finished_at - started_at).num_milliseconds();++ if let Err(e) = deliver_if_configured(config, job, output, success).await {+ if job.delivery.best_effort {+ tracing::warn!("Cron delivery failed (best_effort): {e}");+ } else {+ success = false;+ tracing::warn!("Cron delivery failed: {e}");+ }+ }++ let _ = record_run(+ config,+ &job.id,+ started_at,+ finished_at,+ if success { "ok" } else { "error" },+ Some(output),+ duration_ms,+ );++ // A fixed-instant (`Schedule::At`) job is inherently one-shot: its `at` is in+ // the past the moment it runs, so `reschedule_after_run` (which writes+ // next_run = at for an `At` schedule) leaves next_run <= now and the job is+ // re-selected by `due_jobs` on every poll, re-executing forever. Terminate+ // every `At` job after a single run, regardless of `delete_after_run`. Only an+ // auto-delete job that succeeded is removed; everything else is kept disabled+ // so its run history stays inspectable. (Inside this `At` branch+ // `is_one_shot_auto_delete` reduces to `job.delete_after_run`.)+ if matches!(job.schedule, Schedule::At { .. }) {+ if is_one_shot_auto_delete(job) && success {+ if let Err(e) = remove_job(config, &job.id) {+ tracing::warn!("Failed to remove one-shot cron job after success: {e}");+ }+ } else {+ let _ = record_last_run(config, &job.id, finished_at, success, output);+ if let Err(e) = update_job(+ config,+ &job.id,+ CronJobPatch {+ enabled: Some(false),+ ..CronJobPatch::default()+ },+ ) {+ tracing::warn!("Failed to disable one-shot cron job: {e}");+ }+ }+ return success;+ }++ if let Err(e) = reschedule_after_run(config, job, success, output) {+ tracing::warn!("Failed to persist scheduler run result: {e}");+ }++ success+}++pub(super) fn is_one_shot_auto_delete(job: &CronJob) -> bool {+ job.delete_after_run && matches!(job.schedule, Schedule::At { .. })+}++/// Why an agent job counts as scheduled more often than every five minutes,+/// if it does. `None` for shell and flow jobs, for one-shot `At` schedules,+/// for anything at or above [`MIN_AGENT_JOB_INTERVAL`], and for an expression+/// that cannot be read (that is not evidence of anything).+///+/// Returns the verdict rather than only logging it, because the verdict is+/// the part worth testing: the `Schedule::Cron` arm used to measure the gap+/// between the next run after now and the next run after now plus one+/// second — the same instant unless a run fell inside that second — so every+/// cron-scheduled agent job warned, and no test could see it because a+/// function that only warns has nothing to assert. The gap is now the+/// shortest one between consecutive runs (`runs_closer_than`), so an+/// irregular expression is judged by its tightest pair and not by whichever+/// pair happens to follow the instant of the check.+pub(super) fn agent_job_too_frequent(job: &CronJob) -> Option<TooFrequent> {+ if !matches!(job.job_type, JobType::Agent) {+ return None;+ }+ runs_closer_than(&job.schedule, Utc::now(), MIN_AGENT_JOB_INTERVAL)+}++/// New agent jobs cannot be created below the floor (`validate_agent_schedule`+/// rejects them), so a job that trips this predates the rule. It keeps+/// running — silently skipping runs would turn its schedule into a lie — and+/// this line is the operator's signal to edit it.+pub(super) fn warn_if_high_frequency_agent_job(job: &CronJob) {+ if let Some(too_frequent) = agent_job_too_frequent(job) {+ tracing::warn!(+ "Cron agent job '{}' is scheduled more frequently than every 5 minutes: it \+ {too_frequent}. It was created before that floor was enforced; edit its \+ schedule to silence this.",+ job.id+ );+ }+}diff --git a/crates/openhuman-core/src/cron/scheduler/shell_job.rs b/crates/openhuman-core/src/cron/scheduler/shell_job.rsnew file mode 100644index 0000000000..7a94f50536--- /dev/null+++ b/crates/openhuman-core/src/cron/scheduler/shell_job.rs@@ -0,0 +1,161 @@+//! Execution of `JobType::Shell` jobs under the security policy, with a+//! forbidden-path argument scan and a hard timeout.++use crate::config::Config;+use crate::cron::CronJob;+use crate::security::SecurityPolicy;+use std::process::Stdio;+use tokio::process::Command;+use tokio::time::{self, Duration};++pub(super) const SHELL_JOB_TIMEOUT_SECS: u64 = 120;++pub(super) fn is_env_assignment(word: &str) -> bool {+ word.contains('=')+ && word+ .chars()+ .next()+ .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')+}++pub(super) fn strip_wrapping_quotes(token: &str) -> &str {+ token.trim_matches(|c| c == '"' || c == '\'')+}++pub(super) fn forbidden_path_argument(security: &SecurityPolicy, command: &str) -> Option<String> {+ let mut normalized = command.to_string();+ for sep in ["&&", "||"] {+ normalized = normalized.replace(sep, "\x00");+ }+ for sep in ['\n', ';', '|'] {+ normalized = normalized.replace(sep, "\x00");+ }++ for segment in normalized.split('\x00') {+ let tokens: Vec<&str> = segment.split_whitespace().collect();+ if tokens.is_empty() {+ continue;+ }++ // Skip leading env assignments and executable token.+ let mut idx = 0;+ while idx < tokens.len() && is_env_assignment(tokens[idx]) {+ idx += 1;+ }+ if idx >= tokens.len() {+ continue;+ }+ idx += 1;++ for token in &tokens[idx..] {+ let candidate = strip_wrapping_quotes(token);+ if candidate.is_empty() || candidate.starts_with('-') || candidate.contains("://") {+ continue;+ }++ let looks_like_path = candidate.starts_with('/')+ || candidate.starts_with("./")+ || candidate.starts_with("../")+ || candidate.starts_with("~/")+ || candidate.contains('/');++ if looks_like_path && !security.is_path_string_allowed(candidate) {+ return Some(candidate.to_string());+ }+ }+ }++ None+}++pub(super) async fn run_job_command(+ config: &Config,+ security: &SecurityPolicy,+ job: &CronJob,+) -> (bool, String) {+ run_job_command_with_timeout(+ config,+ security,+ job,+ Duration::from_secs(SHELL_JOB_TIMEOUT_SECS),+ )+ .await+}++pub(super) async fn run_job_command_with_timeout(+ config: &Config,+ security: &SecurityPolicy,+ job: &CronJob,+ timeout: Duration,+) -> (bool, String) {+ if !security.can_act() {+ return (+ false,+ "blocked by security policy: autonomy is read-only".to_string(),+ );+ }++ if security.is_rate_limited() {+ return (+ false,+ "blocked by security policy: rate limit exceeded".to_string(),+ );+ }++ if !security.is_command_allowed(&job.command) {+ return (+ false,+ format!(+ "blocked by security policy: command not allowed: {}",+ job.command+ ),+ );+ }++ if let Some(path) = forbidden_path_argument(security, &job.command) {+ return (+ false,+ format!("blocked by security policy: forbidden path argument: {path}"),+ );+ }++ if !security.record_action() {+ return (+ false,+ "blocked by security policy: action budget exhausted".to_string(),+ );+ }++ let child = match Command::new("sh")+ .arg("-lc")+ .arg(&job.command)+ .current_dir(&config.action_dir)+ .stdin(Stdio::null())+ .stdout(Stdio::piped())+ .stderr(Stdio::piped())+ .kill_on_drop(true)+ .spawn()+ {+ Ok(child) => child,+ Err(e) => return (false, format!("spawn error: {e}")),+ };++ match time::timeout(timeout, child.wait_with_output()).await {+ Ok(Ok(output)) => {+ let stdout = String::from_utf8_lossy(&output.stdout);+ let stderr = String::from_utf8_lossy(&output.stderr);+ let combined = format!(+ "status={}\nstdout:\n{}\nstderr:\n{}",+ output.status,+ stdout.trim(),+ stderr.trim()+ );+ (output.status.success(), combined)+ }+ Ok(Err(e)) => (false, format!("spawn error: {e}")),+ Err(_) => (+ false,+ format!("job timed out after {}s", timeout.as_secs_f64()),+ ),+ }+}diff --git a/crates/openhuman-core/src/cron/scheduler_part_03.rs b/crates/openhuman-core/src/cron/scheduler_part_03.rsdeleted file mode 100644index d36a57fff8..0000000000--- a/crates/openhuman-core/src/cron/scheduler_part_03.rs+++ /dev/null@@ -1,433 +0,0 @@-async fn persist_job_result(- config: &Config,- job: &CronJob,- mut success: bool,- output: &str,- started_at: DateTime<Utc>,- finished_at: DateTime<Utc>,-) -> bool {- let duration_ms = (finished_at - started_at).num_milliseconds();-- if let Err(e) = deliver_if_configured(config, job, output, success).await {- if job.delivery.best_effort {- tracing::warn!("Cron delivery failed (best_effort): {e}");- } else {- success = false;- tracing::warn!("Cron delivery failed: {e}");- }- }-- let _ = record_run(- config,- &job.id,- started_at,- finished_at,- if success { "ok" } else { "error" },- Some(output),- duration_ms,- );-- // A fixed-instant (`Schedule::At`) job is inherently one-shot: its `at` is in- // the past the moment it runs, so `reschedule_after_run` (which writes- // next_run = at for an `At` schedule) leaves next_run <= now and the job is- // re-selected by `due_jobs` on every poll, re-executing forever. Terminate- // every `At` job after a single run, regardless of `delete_after_run`. Only an- // auto-delete job that succeeded is removed; everything else is kept disabled- // so its run history stays inspectable. (Inside this `At` branch- // `is_one_shot_auto_delete` reduces to `job.delete_after_run`.)- if matches!(job.schedule, Schedule::At { .. }) {- if is_one_shot_auto_delete(job) && success {- if let Err(e) = remove_job(config, &job.id) {- tracing::warn!("Failed to remove one-shot cron job after success: {e}");- }- } else {- let _ = record_last_run(config, &job.id, finished_at, success, output);- if let Err(e) = update_job(- config,- &job.id,- CronJobPatch {- enabled: Some(false),- ..CronJobPatch::default()- },- ) {- tracing::warn!("Failed to disable one-shot cron job: {e}");- }- }- return success;- }-- if let Err(e) = reschedule_after_run(config, job, success, output) {- tracing::warn!("Failed to persist scheduler run result: {e}");- }-- success-}--fn is_one_shot_auto_delete(job: &CronJob) -> bool {- job.delete_after_run && matches!(job.schedule, Schedule::At { .. })-}--/// Why an agent job counts as scheduled more often than every five minutes,-/// if it does. `None` for shell and flow jobs, for one-shot `At` schedules,-/// for anything at or above [`MIN_AGENT_JOB_INTERVAL`], and for an expression-/// that cannot be read (that is not evidence of anything).-///-/// Returns the verdict rather than only logging it, because the verdict is-/// the part worth testing: the `Schedule::Cron` arm used to measure the gap-/// between the next run after now and the next run after now plus one-/// second — the same instant unless a run fell inside that second — so every-/// cron-scheduled agent job warned, and no test could see it because a-/// function that only warns has nothing to assert. The gap is now the-/// shortest one between consecutive runs (`runs_closer_than`), so an-/// irregular expression is judged by its tightest pair and not by whichever-/// pair happens to follow the instant of the check.-fn agent_job_too_frequent(job: &CronJob) -> Option<TooFrequent> {- if !matches!(job.job_type, JobType::Agent) {- return None;- }- runs_closer_than(&job.schedule, Utc::now(), MIN_AGENT_JOB_INTERVAL)-}--/// New agent jobs cannot be created below the floor (`validate_agent_schedule`-/// rejects them), so a job that trips this predates the rule. It keeps-/// running — silently skipping runs would turn its schedule into a lie — and-/// this line is the operator's signal to edit it.-fn warn_if_high_frequency_agent_job(job: &CronJob) {- if let Some(too_frequent) = agent_job_too_frequent(job) {- tracing::warn!(- "Cron agent job '{}' is scheduled more frequently than every 5 minutes: it \- {too_frequent}. It was created before that floor was enforced; edit its \- schedule to silence this.",- job.id- );- }-}--/// True when an agent job produced no meaningful text — blank output or the-/// [`EMPTY_AGENT_OUTPUT`] placeholder. Such runs are never injected into chat.-fn cron_output_is_empty(output: &str) -> bool {- output.trim().is_empty() || output == EMPTY_AGENT_OUTPUT-}--/// Whether a cron job's output should be injected into the user's chat thread.-/// Skips failed runs and empty/placeholder output; failures still surface in-/// the alerts tab and run history (handled separately by the caller).-fn should_deliver_cron_output_to_chat(success: bool, output: &str) -> bool {- success && !cron_output_is_empty(output)-}--/// Whether a completed cron run should surface in the alerts tab-/// (`/notifications`). Failures stay visible even when they produce no output;-/// only successful-but-empty runs are dropped entirely.-fn cron_result_should_alert(success: bool, output: &str) -> bool {- !success || !cron_output_is_empty(output)-}--async fn deliver_if_configured(- config: &Config,- job: &CronJob,- output: &str,- success: bool,-) -> Result<()> {- let delivery: &DeliveryConfig = &job.delivery;-- // Don't post failed or empty cron runs into the user's chat: a failed turn- // (e.g. a transient network error) would otherwise deliver a canned- // "Something went wrong" message into the conversation with no user- // message behind it. Failures still reach the alerts tab (`push_cron_alert`)- // and the run-history / health signals, which are recorded elsewhere.- let is_empty = cron_output_is_empty(output);- let deliver_to_chat = should_deliver_cron_output_to_chat(success, output);- if !deliver_to_chat {- tracing::debug!(- job_id = %job.id,- success,- is_empty,- "[cron] skipping chat delivery for failed/empty cron run"- );- }-- // A failed run must stay visible in /notifications regardless of delivery- // mode — a no-delivery agent job that halts on a permanent config/billing- // state (e.g. a keyless provider, TAURI-RUST-HCK) would otherwise fail- // silently. A *successful* non-empty run only alerts in the delivering- // modes (proactive/announce); a `none`-mode success stays silent (its- // output lives in last_output only — the cron contract), so we don't spam- // explicitly-silent background jobs with an unread alert every interval- // (Codex #4166).- let mode = delivery.mode.trim().to_ascii_lowercase();- let delivers = matches!(mode.as_str(), "proactive" | "announce");- let alert_to_notifications =- cron_result_should_alert(success, output) && (!success || delivers);- let alert_body = if is_empty {- "Scheduled job failed without output."- } else {- output- };-- match mode.as_str() {- // Proactive delivery — the channels module decides where to send.- // Used by morning briefings, welcome messages, and other- // user-facing proactive agents.- "proactive" => {- if deliver_to_chat {- let source = format!("cron:{}", job.id);- tracing::debug!(- job_id = %job.id,- source = %source,- "[cron] publishing ProactiveMessageRequested event"- );- BUS.publish(DomainEvent::ProactiveMessageRequested {- source,- message: output.to_string(),- job_name: job.name.clone(),- });- }- }-- // Announce delivery — the cron job specifies the exact channel- // and target. Used for explicit channel-targeted output.- "announce" if deliver_to_chat => {- let channel = delivery- .channel- .as_deref()- .ok_or_else(|| anyhow::anyhow!("delivery.channel is required for announce mode"))?;- let target = delivery- .to- .as_deref()- .ok_or_else(|| anyhow::anyhow!("delivery.to is required for announce mode"))?;-- tracing::debug!(- job_id = %job.id,- channel = %channel,- target = %target,- "[cron] publishing CronDeliveryRequested event"- );- BUS.publish(DomainEvent::CronDeliveryRequested {- job_id: job.id.clone(),- channel: channel.to_string(),- target: target.to_string(),- output: output.to_string(),- });- }-- // No delivery configured — output is stored in last_output only.- // The failure still reaches the alerts tab via the hoisted- // `push_cron_alert` below.- _ => {}- }-- // Surface in the alerts tab (/notifications) for any result that isn't a- // successful-but-empty run — INDEPENDENT of delivery mode. A failed cron- // job must stay visible to the user even when it has no chat delivery- // configured (the common case: a keyless agent job failing "API key not- // set", TAURI-RUST-HCK). Previously this fired only inside the proactive /- // announce arms, so no-delivery jobs failed silently in /notifications.- if alert_to_notifications {- push_cron_alert(config, job, alert_body);- }-- Ok(())-}--/// Insert a notification into the alerts tab for a completed cron job.-fn push_cron_alert(config: &Config, job: &CronJob, output: &str) {- use crate::desktop::notifications::store as notif_store;- use crate::desktop::notifications::types::{- IntegrationNotification, NotificationStatus,- };-- let name = job.name.as_deref().unwrap_or("Cron job");- let body = cron_alert_body(job, output);-- let notification = IntegrationNotification {- id: uuid::Uuid::new_v4().to_string(),- provider: "cron".to_string(),- account_id: Some(job.id.clone()),- title: name.to_string(),- body,- raw_payload: serde_json::json!({- "job_id": job.id,- "job_name": job.name,- "delivery_mode": job.delivery.mode,- }),- importance_score: Some(0.65),- triage_action: Some("react".to_string()),- triage_reason: Some("Scheduled delivery".to_string()),- status: NotificationStatus::Unread,- received_at: Utc::now(),- scored_at: Some(Utc::now()),- };-- match notif_store::insert_if_not_recent(config, ¬ification) {- Ok(true) => {- tracing::debug!(- job_id = %job.id,- "[cron] pushed notification alert to alerts tab"- );- }- Ok(false) => {- tracing::debug!(- job_id = %job.id,- "[cron] skipped duplicate notification alert"- );- }- Err(e) => {- tracing::warn!(- job_id = %job.id,- error = %e,- "[cron] failed to push notification alert"- );- }- }-}--fn is_env_assignment(word: &str) -> bool {- word.contains('=')- && word- .chars()- .next()- .is_some_and(|c| c.is_ascii_alphabetic() || c == '_')-}--fn strip_wrapping_quotes(token: &str) -> &str {- token.trim_matches(|c| c == '"' || c == '\'')-}--fn forbidden_path_argument(security: &SecurityPolicy, command: &str) -> Option<String> {- let mut normalized = command.to_string();- for sep in ["&&", "||"] {- normalized = normalized.replace(sep, "\x00");- }- for sep in ['\n', ';', '|'] {- normalized = normalized.replace(sep, "\x00");- }-- for segment in normalized.split('\x00') {- let tokens: Vec<&str> = segment.split_whitespace().collect();- if tokens.is_empty() {- continue;- }-- // Skip leading env assignments and executable token.- let mut idx = 0;- while idx < tokens.len() && is_env_assignment(tokens[idx]) {- idx += 1;- }- if idx >= tokens.len() {- continue;- }- idx += 1;-- for token in &tokens[idx..] {- let candidate = strip_wrapping_quotes(token);- if candidate.is_empty() || candidate.starts_with('-') || candidate.contains("://") {- continue;- }-- let looks_like_path = candidate.starts_with('/')- || candidate.starts_with("./")- || candidate.starts_with("../")- || candidate.starts_with("~/")- || candidate.contains('/');-- if looks_like_path && !security.is_path_string_allowed(candidate) {- return Some(candidate.to_string());- }- }- }-- None-}--async fn run_job_command(- config: &Config,- security: &SecurityPolicy,- job: &CronJob,-) -> (bool, String) {- run_job_command_with_timeout(- config,- security,- job,- Duration::from_secs(SHELL_JOB_TIMEOUT_SECS),- )- .await-}--async fn run_job_command_with_timeout(- config: &Config,- security: &SecurityPolicy,- job: &CronJob,- timeout: Duration,-) -> (bool, String) {- if !security.can_act() {- return (- false,- "blocked by security policy: autonomy is read-only".to_string(),- );- }-- if security.is_rate_limited() {- return (- false,- "blocked by security policy: rate limit exceeded".to_string(),- );- }-- if !security.is_command_allowed(&job.command) {- return (- false,- format!(- "blocked by security policy: command not allowed: {}",- job.command- ),- );- }-- if let Some(path) = forbidden_path_argument(security, &job.command) {- return (- false,- format!("blocked by security policy: forbidden path argument: {path}"),- );- }-- if !security.record_action() {- return (- false,- "blocked by security policy: action budget exhausted".to_string(),- );- }-- let child = match Command::new("sh")- .arg("-lc")- .arg(&job.command)- .current_dir(&config.action_dir)- .stdin(Stdio::null())- .stdout(Stdio::piped())- .stderr(Stdio::piped())- .kill_on_drop(true)- .spawn()- {- Ok(child) => child,- Err(e) => return (false, format!("spawn error: {e}")),- };-- match time::timeout(timeout, child.wait_with_output()).await {- Ok(Ok(output)) => {- let stdout = String::from_utf8_lossy(&output.stdout);- let stderr = String::from_utf8_lossy(&output.stderr);- let combined = format!(- "status={}\nstdout:\n{}\nstderr:\n{}",- output.status,- stdout.trim(),- stderr.trim()- );- (output.status.success(), combined)- }- Ok(Err(e)) => (false, format!("spawn error: {e}")),- Err(_) => (- false,- format!("job timed out after {}s", timeout.as_secs_f64()),- ),- }-}