Skip to content

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

fix(cron): deliver scheduled job results to the right session

Steven Enamakel8d7449209714
diff --git a/crates/openhuman-core/src/core/runtime/services.rs b/crates/openhuman-core/src/core/runtime/services.rs
index bf67177af9..48ffdfb894 100644
--- a/crates/openhuman-core/src/core/runtime/services.rs
+++ b/crates/openhuman-core/src/core/runtime/services.rs
@@ -255,7 +255,8 @@ pub fn spawn_flows_boot_reconcile() {
pub fn spawn_cron_service(
ctx: std::sync::Arc<crate::core::runtime::CoreContext>,
) -> tokio::task::JoinHandle<()> {
- tokio::spawn(crate::core::runtime::CoreContext::scope(ctx, async {
+ let dispatch = crate::core::runtime::DispatchCtx::new(ctx.clone());
+ tokio::spawn(crate::core::runtime::CoreContext::scope(ctx, async move {
match crate::config::ops::load_config_with_timeout().await {
Ok(config) => {
if !config.cron.enabled {
@@ -276,7 +277,7 @@ pub fn spawn_cron_service(
"[flows] boot reconciliation of schedule-trigger cron jobs failed: {e}"
);
}
- if let Err(e) = crate::cron::scheduler::run(config).await {
+ if let Err(e) = crate::cron::scheduler::run(&dispatch, config).await {
log::error!("[cron] scheduler loop ended with error: {e}");
}
}
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index bc8a4f63d9..c6b55ddb68 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -267,7 +267,7 @@ async fn run_and_record(
) -> (bool, String) {
let run_id = uuid::Uuid::new_v4().to_string();
let started_at = chrono::Utc::now();
- let (success, output) = cron::scheduler::execute_job_now(config, job, &run_id).await;
+ let (success, output) = cron::scheduler::execute_job_now(ctx, config, job, &run_id).await;
let finished_at = chrono::Utc::now();
let duration_ms = (finished_at - started_at).num_milliseconds();
let status = if success { "ok" } else { "error" };
diff --git a/crates/openhuman-core/src/cron/scheduler.rs b/crates/openhuman-core/src/cron/scheduler.rs
index d4865793b2..757baddb68 100644
--- a/crates/openhuman-core/src/cron/scheduler.rs
+++ b/crates/openhuman-core/src/cron/scheduler.rs
@@ -63,7 +63,7 @@ pub fn is_running() -> bool {
SchedulerSlot::is_running(LOOP_SCOPE)
}
-pub async fn run(config: Config) -> Result<()> {
+pub async fn run(ctx: &crate::core::runtime::DispatchCtx, config: Config) -> Result<()> {
let Some(_slot) = SchedulerSlot::acquire(LOOP_SCOPE) else {
tracing::warn!(
"[cron:scheduler] a scheduler loop is already running; not starting another"
@@ -93,7 +93,7 @@ pub async fn run(config: Config) -> Result<()> {
// interval, forever. `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;
- let mut dispatcher = JobDispatcher::new(&config, &security);
+ let mut dispatcher = JobDispatcher::new(ctx, &config, &security);
let mut agents = LiveAgentSchedulers::default();
loop {
@@ -138,7 +138,7 @@ impl LiveAgentSchedulers {
};
let dispatcher = scheduler
.entry(agent_id.clone())
- .or_insert_with(|| JobDispatcher::new(&config, &security));
+ .or_insert_with(|| JobDispatcher::new(&dispatch, &config, &security));
match dispatcher.tick(&config).await {
Ok(0) => {}
Ok(count) => {
@@ -174,13 +174,18 @@ pub async fn run_live_agent_pass(max_concurrent: usize) {
/// Run `job` now under the caller's `run_id` (also the id its delivery and
/// run record use), with the same executor, retry budget and failure
/// classification as a scheduled run. The caller holds the job's run claim.
-pub async fn execute_job_now(config: &Config, job: &CronJob, run_id: &str) -> (bool, String) {
+pub async fn execute_job_now(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+ job: &CronJob,
+ run_id: &str,
+) -> (bool, String) {
let security = Arc::new(SecurityPolicy::from_config(
&config.autonomy,
&config.workspace_dir,
&config.action_dir,
));
- let binding = host::Binding::current(config, security);
+ let binding = host::Binding::new(ctx, config, security);
let ports = host::ports(&binding);
match execute_with_retry(&ports, &host::scheduler_config(config), job, run_id).await {
Ok(attempt) => (attempt.success, attempt.output),
diff --git a/crates/openhuman-core/src/cron/scheduler/host.rs b/crates/openhuman-core/src/cron/scheduler/host.rs
index bf2c2a6945..4631022601 100644
--- a/crates/openhuman-core/src/cron/scheduler/host.rs
+++ b/crates/openhuman-core/src/cron/scheduler/host.rs
@@ -40,12 +40,16 @@ pub(crate) struct Binding {
}
impl Binding {
- /// A binding for the context the caller is running in.
- pub(crate) fn current(config: &Config, security: Arc<SecurityPolicy>) -> Self {
+ /// A binding for the context `ctx` carries (none when it is unbound).
+ pub(crate) fn new(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+ security: Arc<SecurityPolicy>,
+ ) -> Self {
Self {
config: Arc::new(RwLock::new(config.clone())),
security,
- context: CoreContext::scoped().map(|ctx| Arc::downgrade(&ctx)),
+ context: ctx.core().map(Arc::downgrade),
}
}
@@ -208,9 +212,13 @@ pub(crate) struct JobDispatcher {
}
impl JobDispatcher {
- /// A dispatcher for the context the caller is running in.
- pub(crate) fn new(config: &Config, security: &Arc<SecurityPolicy>) -> Self {
- let binding = Binding::current(config, Arc::clone(security));
+ /// A dispatcher for the context `ctx` carries.
+ pub(crate) fn new(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+ security: &Arc<SecurityPolicy>,
+ ) -> Self {
+ let binding = Binding::new(ctx, config, Arc::clone(security));
Self {
scheduler: Scheduler::new(CLAIM_SCOPE, scheduler_config(config), ports(&binding)),
binding,
diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rs
index c144206e8a..e709b5519f 100644
--- a/crates/openhuman-core/src/cron/tools/run.rs
+++ b/crates/openhuman-core/src/cron/tools/run.rs
@@ -59,14 +59,15 @@ impl Tool for CronRunTool {
}
async fn execute(&self, args: serde_json::Value) -> anyhow::Result<ToolResult> {
- self.execute_with_options(args, ToolCallOptions::default())
+ self.execute_with_context(args, ToolCallOptions::default(), None)
.await
}
- async fn execute_with_options(
+ async fn execute_with_context(
&self,
args: serde_json::Value,
options: ToolCallOptions,
+ context: Option<&dyn tinytools::ToolRunContext>,
) -> anyhow::Result<ToolResult> {
if !self.config.cron.enabled {
return Ok(ToolResult::error(
@@ -74,6 +75,7 @@ impl Tool for CronRunTool {
));
}
+ let dispatch = crate::agent::tinyagents::host::run_context::dispatch_ctx_for_tool(context);
let job_id = match args.get("job_id").and_then(serde_json::Value::as_str) {
Some(v) if !v.trim().is_empty() => v,
_ => {
@@ -102,7 +104,7 @@ impl Tool for CronRunTool {
let started_at = Utc::now();
let run_id = uuid::Uuid::new_v4().to_string();
- let (success, output) = cron::scheduler::execute_job_now(&self.config, &job, &run_id).await;
+ let (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 status = if success { "ok" } else { "error" };
diff --git a/crates/openhuman-core/src/profiles/cron.rs b/crates/openhuman-core/src/profiles/cron.rs
index 3ddfbea4c5..436dc4a63a 100644
--- a/crates/openhuman-core/src/profiles/cron.rs
+++ b/crates/openhuman-core/src/profiles/cron.rs
@@ -123,13 +123,15 @@ pub async fn sweep(host: &ProfileHost, now: DateTime<Utc>) -> SweepReport {
}
let count = claimed.len();
let profile_config = profile.config.clone();
- let outcome = CoreContext::scope(Arc::clone(profile.context()), async move {
+ let profile_ctx = Arc::clone(profile.context());
+ let outcome = CoreContext::scope(Arc::clone(&profile_ctx), async move {
let security = Arc::new(crate::security::SecurityPolicy::from_config(
&profile_config.autonomy,
&profile_config.workspace_dir,
&profile_config.action_dir,
));
- let mut dispatcher = JobDispatcher::new(&profile_config, &security);
+ let dispatch = crate::core::runtime::DispatchCtx::new(Arc::clone(&profile_ctx));
+ let mut dispatcher = JobDispatcher::new(&dispatch, &profile_config, &security);
if let Err(error) = dispatcher.dispatch(claimed).await {
log::warn!("[profiles][cron] dispatch failed: {error}");
}