Skip to content

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

feat(cron): run scheduled jobs and task polls per agent scope

Steven Enamakelca3d663c92ff
diff --git a/crates/openhuman-core/src/cron/scheduler.rs b/crates/openhuman-core/src/cron/scheduler.rs
index 8e87f3fa2f..0b51b21ac1 100644
--- a/crates/openhuman-core/src/cron/scheduler.rs
+++ b/crates/openhuman-core/src/cron/scheduler.rs
@@ -75,9 +75,25 @@ pub async fn run(config: Config) -> Result<()> {
loop {
interval.tick().await;
tick_once(&config, &security, &mut last_emitted_health).await;
+ tick_agents(&config, &security).await;
}
}
+/// The same poll for every agent that keeps its jobs in its own storage scope
+/// (`crate::storage::agents`): a job an agent scheduled from its own turn
+/// lives there, and runs there — under that agent's context and, when the
+/// agent is live, its configuration. Only the `local` pass reports the
+/// scheduler's health; an agent pass still reports a failing job.
+pub(crate) async fn tick_agents(config: &Config, security: &Arc<SecurityPolicy>) {
+ crate::storage::agents::for_each_agent("cron", || async {
+ let config = crate::core::runtime::CoreContext::current_embedder_config()
+ .unwrap_or_else(|| config.clone());
+ let mut steady = Some(true);
+ tick_once(&config, security, &mut steady).await;
+ })
+ .await;
+}
+
/// Single poll cycle of the scheduler loop, extracted so tests can drive
/// it without owning `tokio::time::interval`.
///
diff --git a/crates/openhuman-core/src/integrations/task_sources/periodic.rs b/crates/openhuman-core/src/integrations/task_sources/periodic.rs
index faac8d2e00..bd34c8656c 100644
--- a/crates/openhuman-core/src/integrations/task_sources/periodic.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/periodic.rs
@@ -70,7 +70,7 @@ pub fn start_periodic_poll() {
tracing::debug!("[task_sources:periodic] scheduler already running, skipping start");
return;
}
- tokio::spawn(async move {
+ crate::core::runtime::spawn_scoped(async move {
tracing::info!(
tick_seconds = TICK_SECONDS,
"[task_sources:periodic] scheduler starting"
@@ -87,8 +87,12 @@ async fn run_loop() {
ticker.tick().await;
loop {
ticker.tick().await;
- if let Err(e) = run_one_tick().await {
- tracing::warn!(error = %e, "[task_sources:periodic] tick failed (continuing)");
+ // `local`, then every agent that keeps its sources in its own storage
+ // scope (`crate::storage::agents`).
+ for (agent, result) in crate::storage::agents::for_each_scope("task_sources", run_one_tick).await {
+ if let Err(e) = result {
+ tracing::warn!(error = %e, agent = agent.as_deref().unwrap_or("local"), "[task_sources:periodic] tick failed (continuing)");
+ }
}
}
}
diff --git a/crates/openhuman-core/src/storage/agents.rs b/crates/openhuman-core/src/storage/agents.rs
index a5a7195250..493b5dc0b0 100644
--- a/crates/openhuman-core/src/storage/agents.rs
+++ b/crates/openhuman-core/src/storage/agents.rs
@@ -150,10 +150,9 @@ fn agent_contexts(fallback: Option<&Arc<CoreContext>>) -> Vec<(String, Arc<CoreC
}
/// Runs `step` for every storage scope background work must cover: once
-/// under the current context (the `local` scope, outside SaaS mode), then —
-/// when a storage backend is installed — once under each known agent's
-/// context. Steps run one after another; each result is returned with the
-/// agent it ran for (`None` for `local`).
+/// under the current context (the `local` scope, outside SaaS mode), then
+/// once per known agent ([`for_each_agent`]). Each result is returned with
+/// the agent it ran for (`None` for `local`).
///
/// `label` names the caller in logs.
pub async fn for_each_scope<T, F, Fut>(label: &str, step: F) -> Vec<(Option<String>, T)>
@@ -162,18 +161,35 @@ where
Fut: Future<Output = T>,
{
let mut results = Vec::new();
- let saas = crate::core::runtime::mode::is_saas();
- if !saas {
+ if !crate::core::runtime::mode::is_saas() {
results.push((None, step().await));
}
+ for (agent, value) in for_each_agent(label, step).await {
+ results.push((Some(agent), value));
+ }
+ results
+}
+
+/// Runs `step` once under each known agent's context, one after another —
+/// only when a storage backend is installed, since without one the stores do
+/// not split by agent and the `local` pass already covers everything.
+///
+/// For a loop that handles the `local` scope itself (the cron scheduler keeps
+/// its process-wide health tracking there) and needs the agents on top.
+pub async fn for_each_agent<T, F, Fut>(label: &str, step: F) -> Vec<(String, T)>
+where
+ F: Fn() -> Fut,
+ Fut: Future<Output = T>,
+{
if installed().is_none() {
- return results;
+ return Vec::new();
}
let fallback = CoreContext::current();
+ let mut results = Vec::new();
for (agent, context) in agent_contexts(fallback.as_ref()) {
tracing::trace!(%agent, label, "[storage::agents] visiting agent scope");
let value = CoreContext::scope(context, step()).await;
- results.push((Some(agent), value));
+ results.push((agent, value));
}
results
}