Skip to content

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

feat(cron): thread dispatch context through shell execution

Steven Enamakel2556601f73f4
diff --git a/crates/openhuman-core/src/cron/scheduler/executor.rs b/crates/openhuman-core/src/cron/scheduler/executor.rs
index 90ec106f1f..d8d202f242 100644
--- a/crates/openhuman-core/src/cron/scheduler/executor.rs
+++ b/crates/openhuman-core/src/cron/scheduler/executor.rs
@@ -120,8 +120,13 @@ impl HostExecutor {
}
let (success, output, agent_error) = match job.job_type {
JobType::Shell => {
- let (success, output) =
- run_job_command(&config, self.binding.security.as_ref(), job).await;
+ let (success, output) = run_job_command(
+ &self.binding.dispatch_ctx(),
+ &config,
+ self.binding.security.as_ref(),
+ job,
+ )
+ .await;
(success, output, None)
}
JobType::Agent => {
diff --git a/crates/openhuman-core/src/cron/scheduler/shell_job.rs b/crates/openhuman-core/src/cron/scheduler/shell_job.rs
index 592adb6870..bcfbd4cbe9 100644
--- a/crates/openhuman-core/src/cron/scheduler/shell_job.rs
+++ b/crates/openhuman-core/src/cron/scheduler/shell_job.rs
@@ -120,11 +120,13 @@ pub(super) fn forbidden_path_argument(security: &SecurityPolicy, command: &str)
}
pub(super) async fn run_job_command(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
security: &SecurityPolicy,
job: &CronJob,
) -> (bool, String) {
run_job_command_with_timeout(
+ ctx,
config,
security,
job,
@@ -134,6 +136,7 @@ pub(super) async fn run_job_command(
}
pub(super) async fn run_job_command_with_timeout(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
security: &SecurityPolicy,
job: &CronJob,
@@ -167,6 +170,7 @@ pub(super) async fn run_job_command_with_timeout(
// Cron keeps host execution semantics (the host's own network, no jail)
// and a pipeline's default exit status: retries are spent on it.
let output = match crate::sandbox::shell_exec::run_host(
+ ctx,
config,
&job.command,
&config.action_dir,
diff --git a/crates/openhuman-core/src/cron/scheduler_classifier_and_delivery_tests.rs b/crates/openhuman-core/src/cron/scheduler_classifier_and_delivery_tests.rs
index 13b7f986a9..714a36b771 100644
--- a/crates/openhuman-core/src/cron/scheduler_classifier_and_delivery_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_classifier_and_delivery_tests.rs
@@ -488,7 +488,11 @@ async fn scheduler_tick_once_publishes_health_recovery_signal_on_empty_queue() {
tick_once(
&config,
&mut last_emitted_health,
- &mut JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security),
+ &mut JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security,
+ ),
)
.await;
@@ -551,7 +555,11 @@ async fn scheduler_tick_once_does_not_re_emit_recovery_signal_on_steady_state()
tick_once(
&config,
&mut last_emitted_health,
- &mut JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security),
+ &mut JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security,
+ ),
)
.await;
assert_eq!(
@@ -569,7 +577,11 @@ async fn scheduler_tick_once_does_not_re_emit_recovery_signal_on_steady_state()
tick_once(
&config,
&mut last_emitted_health,
- &mut JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security),
+ &mut JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security,
+ ),
)
.await;
assert_eq!(
diff --git a/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs b/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
index 0a6ef1824b..3d8305ac12 100644
--- a/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_dispatch_tests.rs
@@ -115,7 +115,11 @@ async fn a_long_job_does_not_hold_up_the_next_poll() {
let _fast = register("dispatch-fast", counting(Arc::clone(&fast_calls), Ok(())));
let slow = due_system_job(&config, "dispatch-slow");
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security);
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security,
+ );
let mut health = None;
// First poll picks up only the slow job.
tick_once(&config, &mut health, &mut dispatcher).await;
@@ -170,7 +174,11 @@ async fn dispatch_claims_the_slot_so_a_running_job_is_not_due_again() {
}),
);
let job = due_system_job(&config, "dispatch-claim");
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security(&config));
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security(&config),
+ );
dispatcher.dispatch(vec![job.clone()]).await.unwrap();
let stored = cron::get_job(&config, &job.id).unwrap();
assert!(stored.next_run > Utc::now(), "the running slot is claimed");
@@ -190,7 +198,11 @@ async fn a_job_already_in_flight_is_not_dispatched_again() {
let _handler = register("dispatch-dedupe", counting(Arc::clone(&calls), Ok(())));
let job = due_system_job(&config, "dispatch-dedupe");
let running = crate::cron::ops::try_acquire_run(&job.id).expect("the job is free");
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security(&config));
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security(&config),
+ );
dispatcher.dispatch(vec![job.clone()]).await.unwrap();
dispatcher.drain().await;
drop(running);
@@ -219,7 +231,11 @@ async fn a_single_flight_job_records_the_missed_slot_as_skipped() {
.unwrap();
let job = cron::get_job(&config, &job.id).unwrap();
let running = crate::cron::ops::try_acquire_run(&job.id).expect("the job is free");
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security(&config));
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security(&config),
+ );
dispatcher.dispatch(vec![job.clone()]).await.unwrap();
dispatcher.drain().await;
drop(running);
@@ -260,7 +276,13 @@ async fn zero_retries_means_exactly_one_attempt() {
)
.unwrap();
let job = cron::get_job(&config, &job.id).unwrap();
- let (success, output) = execute_job_now(&crate::core::runtime::DispatchCtx::ambient(), &config, &job, "dispatch-run").await;
+ let (success, output) = execute_job_now(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &job,
+ "dispatch-run",
+ )
+ .await;
assert!(!success);
assert!(output.contains("boom"), "{output}");
assert_eq!(calls.load(Ordering::SeqCst), 1);
@@ -278,7 +300,13 @@ async fn without_a_policy_the_configured_retry_budget_applies() {
counting(Arc::clone(&calls), Err("again".to_string())),
);
let job = due_system_job(&config, "dispatch-default-retries");
- let (success, _) = execute_job_now(&crate::core::runtime::DispatchCtx::ambient(), &config, &job, "dispatch-run").await;
+ let (success, _) = execute_job_now(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &job,
+ "dispatch-run",
+ )
+ .await;
assert!(!success);
assert_eq!(calls.load(Ordering::SeqCst), 2);
}
@@ -305,7 +333,11 @@ async fn a_system_job_handler_error_is_recorded_as_the_run_result() {
)
.unwrap();
let job = cron::get_job(&config, &job.id).unwrap();
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security(&config));
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security(&config),
+ );
dispatcher.dispatch(vec![job.clone()]).await.unwrap();
dispatcher.drain().await;
let stored = cron::get_job(&config, &job.id).unwrap();
@@ -321,7 +353,13 @@ async fn a_system_job_without_a_handler_is_still_ok_on_dispatch() {
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp).await;
let job = due_system_job(&config, "dispatch-unclaimed");
- let (success, output) = execute_job_now(&crate::core::runtime::DispatchCtx::ambient(), &config, &job, "dispatch-run").await;
+ let (success, output) = execute_job_now(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &job,
+ "dispatch-run",
+ )
+ .await;
assert!(success);
assert!(output.contains("dispatched"), "{output}");
}
@@ -380,7 +418,11 @@ async fn an_upgraded_job_with_a_side_table_policy_keeps_firing_with_it() {
assert_eq!(job.policy.retries, Some(0));
assert!(job.policy.single_flight);
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security(&config));
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security(&config),
+ );
dispatcher.dispatch(vec![job]).await.unwrap();
dispatcher.drain().await;
@@ -411,7 +453,11 @@ async fn a_flow_schedule_job_fires_through_the_trigger_sink() {
force_due(&config, &job.id);
let job = cron::get_job(&config, &job.id).unwrap();
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security(&config));
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security(&config),
+ );
dispatcher.dispatch(vec![job.clone()]).await.unwrap();
dispatcher.drain().await;
@@ -469,8 +515,10 @@ async fn a_dispatcher_binds_the_context_it_is_handed_not_the_ambient_one() {
let tmp = TempDir::new().unwrap();
let config = test_config(&tmp).await;
let security = security(&config);
- let handed = crate::core::runtime::CoreContext::for_test(crate::core::runtime::DomainSet::full(), None);
- let ambient = crate::core::runtime::CoreContext::for_test(crate::core::runtime::DomainSet::full(), None);
+ let handed =
+ crate::core::runtime::CoreContext::for_test(crate::core::runtime::DomainSet::full(), None);
+ let ambient =
+ crate::core::runtime::CoreContext::for_test(crate::core::runtime::DomainSet::full(), None);
let dispatcher = crate::core::runtime::CoreContext::scope(Arc::clone(&ambient), async {
// Across an await the handed context is still the one bound.
diff --git a/crates/openhuman-core/src/cron/scheduler_halt_and_persist_tests.rs b/crates/openhuman-core/src/cron/scheduler_halt_and_persist_tests.rs
index 608213dd44..1e50025499 100644
--- a/crates/openhuman-core/src/cron/scheduler_halt_and_persist_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_halt_and_persist_tests.rs
@@ -367,7 +367,11 @@ async fn run_through_scheduler(config: &Config, job_id: &str) {
&config.workspace_dir,
&config.action_dir,
));
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), config, &security);
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ config,
+ &security,
+ );
dispatcher.dispatch(vec![job]).await.unwrap();
dispatcher.drain().await;
}
@@ -427,7 +431,11 @@ async fn scheduler_flow_runs_active_hours_job_and_reschedules_inside_window() {
&config.workspace_dir,
&config.workspace_dir,
));
- let mut dispatcher = JobDispatcher::new(&crate::core::runtime::DispatchCtx::ambient(), &config, &security);
+ let mut dispatcher = JobDispatcher::new(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &security,
+ );
dispatcher.dispatch(vec![job.clone()]).await.unwrap();
dispatcher.drain().await;
diff --git a/crates/openhuman-core/src/cron/scheduler_tests.rs b/crates/openhuman-core/src/cron/scheduler_tests.rs
index bf0ce4a92f..717d74d842 100644
--- a/crates/openhuman-core/src/cron/scheduler_tests.rs
+++ b/crates/openhuman-core/src/cron/scheduler_tests.rs
@@ -172,7 +172,13 @@ async fn retryable_shell_failure_consumes_the_whole_retry_budget() {
let counter = tmp.path().join("retryable-attempts.log");
let job = test_job(&counting_failure_command(&counter));
- let (success, output) = execute_job_now(&crate::core::runtime::DispatchCtx::ambient(), &config, &job, "test-run").await;
+ let (success, output) = execute_job_now(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &job,
+ "test-run",
+ )
+ .await;
assert!(
!success,
@@ -230,7 +236,13 @@ async fn security_policy_block_halts_without_spending_the_retry_budget() {
);
let started = std::time::Instant::now();
- let (success, output) = execute_job_now(&crate::core::runtime::DispatchCtx::ambient(), &config, &job, "test-run").await;
+ let (success, output) = execute_job_now(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ &config,
+ &job,
+ "test-run",
+ )
+ .await;
let elapsed = started.elapsed();
assert!(!success, "a blocked job cannot succeed; got {output}");
@@ -275,7 +287,13 @@ async fn a_shell_job_runs_on_this_platform() {
let job = test_job("echo tinyhumans");
- let (success, output) = run_job_command(&config, &security, &job).await;
+ let (success, output) = run_job_command(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ &security,
+ &job,
+ )
+ .await;
assert!(success, "the shell job did not succeed: {output}");
assert!(
@@ -325,7 +343,13 @@ async fn a_pipeline_reports_its_last_stage_rather_than_pipefail() {
let job = test_job("false | true");
- let (success, output) = run_job_command(&config, &security, &job).await;
+ let (success, output) = run_job_command(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ &security,
+ &job,
+ )
+ .await;
assert!(
success,
diff --git a/crates/openhuman-core/src/sandbox/native_jail_tests.rs b/crates/openhuman-core/src/sandbox/native_jail_tests.rs
index 122569c76f..344f8348e7 100644
--- a/crates/openhuman-core/src/sandbox/native_jail_tests.rs
+++ b/crates/openhuman-core/src/sandbox/native_jail_tests.rs
@@ -139,6 +139,7 @@ async fn disabled_tinybox_module_never_falls_back_to_host_execution() -> anyhow:
config.modules.enabled = false;
let result = execute(
+ &crate::core::runtime::DispatchCtx::host_default(),
&config,
&policy,
&format!("touch '{}'", marker.display()),
@@ -251,6 +252,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
docker_overrides: None,
};
let output = execute(
+ &crate::core::runtime::DispatchCtx::host_default(),
&config,
&policy,
"printf tinybox-jailed-ok; printf safe-error >&2",
@@ -266,6 +268,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
assert_eq!(output.stderr, "safe-error");
let output = execute(
+ &crate::core::runtime::DispatchCtx::host_default(),
&config,
&policy,
"i=0; while [ $i -lt 1500 ]; do printf 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; i=$((i + 1)); done",
@@ -284,6 +287,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
);
let output = super::super::shell_exec::run_host(
+ &crate::core::runtime::DispatchCtx::host_default(),
&config,
"printf cron-module-ok; printf cron-stderr >&2",
workspace.path(),
@@ -299,6 +303,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
assert_eq!(output.stderr, "cron-stderr");
let output = super::super::shell_exec::run_host(
+ &crate::core::runtime::DispatchCtx::host_default(),
&config,
"false | true",
workspace.path(),
@@ -312,6 +317,7 @@ async fn released_tinybox_runs_a_native_jail_through_the_pinned_loader() -> anyh
assert!(output.success(), "cron preserves default pipeline status");
let output = super::super::shell_exec::run_host(
+ &crate::core::runtime::DispatchCtx::host_default(),
&config,
"sleep 10",
workspace.path(),
diff --git a/crates/openhuman-core/src/sandbox/ops.rs b/crates/openhuman-core/src/sandbox/ops.rs
index 5bac3875d8..7362a62f7a 100644
--- a/crates/openhuman-core/src/sandbox/ops.rs
+++ b/crates/openhuman-core/src/sandbox/ops.rs
@@ -370,6 +370,7 @@ pub(crate) async fn execute_in_sandbox_metered(
})?,
};
execute_unsandboxed(
+ ctx,
&config,
policy,
command,
@@ -475,6 +476,7 @@ pub(crate) async fn apply_requested_backend(
/// [`SANDBOX_ENV_PASSTHROUGH`] names plus the Windows process-bootstrap
/// variables), applies the caller's overrides and owns the scratch directory.
async fn execute_unsandboxed(
+ ctx: &DispatchCtx,
config: &crate::config::Config,
policy: &SandboxPolicy,
command: &str,
@@ -493,6 +495,7 @@ async fn execute_unsandboxed(
std::fs::create_dir_all(&policy.state_dir)
.map_err(|error| anyhow::anyhow!("Cannot prepare sandbox state directory: {error}"))?;
super::shell_exec::run_host(
+ ctx,
config,
command,
working_dir,
diff --git a/crates/openhuman-core/src/sandbox/ops_tests.rs b/crates/openhuman-core/src/sandbox/ops_tests.rs
index 0824bceeb7..c61b94a6eb 100644
--- a/crates/openhuman-core/src/sandbox/ops_tests.rs
+++ b/crates/openhuman-core/src/sandbox/ops_tests.rs
@@ -127,6 +127,7 @@ async fn run_unsandboxed(
let state = tempfile::tempdir()?;
let policy = unsandboxed_policy(workspace, state.path());
execute_unsandboxed(
+ &crate::core::runtime::DispatchCtx::host_default(),
&crate::config::Config::default(),
&policy,
command,
diff --git a/crates/openhuman-core/src/sandbox/shell_exec.rs b/crates/openhuman-core/src/sandbox/shell_exec.rs
index e9e45446d4..4bb4fa964e 100644
--- a/crates/openhuman-core/src/sandbox/shell_exec.rs
+++ b/crates/openhuman-core/src/sandbox/shell_exec.rs
@@ -321,6 +321,7 @@ pub(crate) fn path_string(path: &std::path::Path, label: &str) -> anyhow::Result
/// `preserve_pipeline_status` keeps a pipeline's default exit status for
/// callers whose retry or settlement behavior depends on it (cron).
pub(crate) async fn run_host(
+ ctx: &crate::core::runtime::DispatchCtx,
config: &Config,
command: &str,
cwd: &std::path::Path,
@@ -334,7 +335,7 @@ pub(crate) async fn run_host(
shell.meter = meter;
shell.env = env;
shell.preserve_pipeline_status = preserve_pipeline_status;
- run(config, shell).await
+ run(ctx, config, shell).await
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/sandbox/shell_exec_tests.rs b/crates/openhuman-core/src/sandbox/shell_exec_tests.rs
index ebfe047f1d..6b9aa43c67 100644
--- a/crates/openhuman-core/src/sandbox/shell_exec_tests.rs
+++ b/crates/openhuman-core/src/sandbox/shell_exec_tests.rs
@@ -117,7 +117,13 @@ async fn a_disabled_module_never_falls_back_to_a_host_shell_for_a_jailed_run() {
enforcement: contract::JailEnforcement::Enforced,
require_process_isolation: true,
});
- assert!(run(&config, shell).await.is_err());
+ assert!(run(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ &config,
+ shell
+ )
+ .await
+ .is_err());
assert!(!marker.exists());
}
@@ -129,6 +135,7 @@ async fn run_host_feeds_the_callers_meter() {
let dir = tempfile::tempdir().unwrap();
let meter = crate::sandbox::shell_progress::ShellOutputMeter::default();
let output = run_host(
+ &crate::core::runtime::DispatchCtx::host_default(),
&Config::default(),
"printf 'line one\\nline two\\n'",
dir.path(),