Skip to content

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

feat(embed): add cron scheduling to the embed runtime

Steven Enamakeld7319e5c7bef
diff --git a/crates/openhuman-embed/src/cron.rs b/crates/openhuman-embed/src/cron.rs
index 02d691e729..ca05ad3a0f 100644
--- a/crates/openhuman-embed/src/cron.rs
+++ b/crates/openhuman-embed/src/cron.rs
@@ -66,7 +66,12 @@ use {
openhuman_core::cron::JobType, openhuman_core::cron::Schedule,
};
+pub use openhuman_core::cron::handlers::{JobKindHandler, JobOutcome, JobRunContext};
pub use openhuman_core::cron::system_job_handlers::SystemJobContext;
+/// The kind of a scheduled job (`shell`, `agent`, `flow`), which decides the
+/// handler that runs it: see
+/// [`RuntimeBuilder::cron_job_handler`](crate::RuntimeBuilder::cron_job_handler).
+pub use openhuman_core::cron::JobType as JobKind;
/// When a job fires.
#[derive(Debug, Clone, PartialEq, Eq)]
diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rs
index c8b15b13d6..b68b7bcb29 100644
--- a/crates/openhuman-embed/src/lib.rs
+++ b/crates/openhuman-embed/src/lib.rs
@@ -177,8 +177,8 @@ pub use channels::{ChannelError, ChannelListener, Channels, StreamMode, Telegram
pub use config::{Config, RuntimeFlags};
pub use core_agent::CoreAgent;
pub use cron::{
- Cron, CronError, JobRun, JobRunRecord, JobSchedule, JobSpec, JobTarget, ScheduledJob,
- SystemJobContext,
+ Cron, CronError, JobKind, JobKindHandler, JobOutcome, JobRun, JobRunContext, JobRunRecord,
+ JobSchedule, JobSpec, JobTarget, ScheduledJob, SystemJobContext,
};
pub use error::CoreError;
pub use harness::{
diff --git a/crates/openhuman-embed/src/runtime/build.rs b/crates/openhuman-embed/src/runtime/build.rs
index ada353d59e..bf4fa29000 100644
--- a/crates/openhuman-embed/src/runtime/build.rs
+++ b/crates/openhuman-embed/src/runtime/build.rs
@@ -348,6 +348,9 @@ impl RuntimeBuilder {
storage_info,
storage_backend,
);
+ for (kind, handler) in self.cron_job_handlers {
+ runtime.register_job_handler(kind, handler);
+ }
if starts_services_on_build(services, self.defer_services) {
log::debug!("[embed][runtime] starting background services {services:?}");
runtime.start_services().await;
diff --git a/crates/openhuman-embed/src/runtime/builder.rs b/crates/openhuman-embed/src/runtime/builder.rs
index aaec18d055..7180f760e8 100644
--- a/crates/openhuman-embed/src/runtime/builder.rs
+++ b/crates/openhuman-embed/src/runtime/builder.rs
@@ -100,6 +100,10 @@ pub struct RuntimeBuilder {
pub(super) memory_engine: Option<Arc<dyn tinymemory_api::MemoryEngine>>,
pub(super) session_store: Option<Arc<dyn SessionStoreProvider>>,
pub(super) cron_delivery: Option<Arc<dyn openhuman_core::cron::host_delivery::CronDelivery>>,
+ pub(super) cron_job_handlers: Vec<(
+ crate::cron::JobKind,
+ Arc<dyn crate::cron::JobKindHandler>,
+ )>,
pub(super) seams: HostSeams,
pub(super) max_agents: usize,
pub(super) agent_defaults: super::AgentDefaults,
@@ -146,6 +150,7 @@ impl RuntimeBuilder {
memory_engine: None,
session_store: None,
cron_delivery: None,
+ cron_job_handlers: Vec::new(),
seams: HostSeams::default(),
max_agents: DEFAULT_MAX_AGENTS,
agent_defaults: super::AgentDefaults::default(),
@@ -281,6 +286,22 @@ impl RuntimeBuilder {
self
}
+ /// Run scheduled jobs of `kind` through `handler` instead of the built-in
+ /// one (see [`Runtime::register_job_handler`](crate::Runtime::register_job_handler)).
+ /// Registered when the runtime is built and restored when it drops, so a
+ /// host can swap how `shell`, `agent` or `flow` jobs run without touching
+ /// cron internals. Call it once per kind; a later call replaces an earlier
+ /// one for the same kind.
+ pub fn cron_job_handler(
+ mut self,
+ kind: crate::cron::JobKind,
+ handler: Arc<dyn crate::cron::JobKindHandler>,
+ ) -> Self {
+ self.cron_job_handlers.retain(|(existing, _)| *existing != kind);
+ self.cron_job_handlers.push((kind, handler));
+ self
+ }
+
/// The most agents this runtime hosts at once; [`crate::Runtime::agent`]
/// returns [`AgentError::AgentLimit`](crate::AgentError::AgentLimit)
/// beyond it. Removed and dropped agents do not count. Defaults to
diff --git a/crates/openhuman-embed/src/runtime/mod.rs b/crates/openhuman-embed/src/runtime/mod.rs
index e89a0246c3..907d9b6b9f 100644
--- a/crates/openhuman-embed/src/runtime/mod.rs
+++ b/crates/openhuman-embed/src/runtime/mod.rs
@@ -203,6 +203,9 @@ pub(crate) struct CoreGuard {
/// Handlers registered with [`Runtime::on_system_job`], by job name.
system_jobs:
Mutex<HashMap<String, openhuman_core::cron::system_job_handlers::SystemJobRegistration>>,
+ /// Job-kind handlers registered with [`Runtime::register_job_handler`] or
+ /// [`RuntimeBuilder::cron_job_handler`]; dropping restores the defaults.
+ kind_handlers: Mutex<Vec<openhuman_core::cron::handlers::KindRegistration>>,
/// Process-global seams this runtime installed; dropping restores the
/// restorable ones (see [`seams`]).
seams: Mutex<Option<seams::InstalledSeams>>,
@@ -230,6 +233,12 @@ impl Drop for CoreGuard {
.get_mut()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.clear();
+ // Newest first, so each registration restores what it replaced.
+ let handlers = self
+ .kind_handlers
+ .get_mut()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ while handlers.pop().is_some() {}
// Drop the core while the process slot is still claimed. Releasing it
// first lets another builder initialize process-scoped state while
// this runtime's keyring, bearer, event bus and subscribers are live.
@@ -425,6 +434,27 @@ impl Runtime {
Ok(())
}
+ /// Run every job of `kind` through `handler` instead of the built-in one,
+ /// for the life of this runtime (dropping the runtime restores what it
+ /// replaced). A job's kind decides which handler runs it: `shell` and
+ /// `agent` rows reach it as one attempt of the run (the core still applies
+ /// its operator limits and classifies a failure as retryable or
+ /// permanent); a `flow` row reaches it as a schedule tick. A later
+ /// registration of the same kind replaces this one.
+ pub fn register_job_handler(
+ &self,
+ kind: crate::cron::JobKind,
+ handler: std::sync::Arc<dyn crate::cron::JobKindHandler>,
+ ) {
+ let registration = openhuman_core::cron::handlers::register_kind(kind, handler);
+ log::debug!("[embed][runtime] job kind handler registered kind={kind:?}");
+ self.guard
+ .kind_handlers
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .push(registration);
+ }
+
/// Start the background services this runtime's [`ServiceSet`] selects
/// (the cron scheduler, channel listeners, …). [`RuntimeBuilder::build`]
/// already does this when the set asks for background work; call
@@ -607,6 +637,7 @@ impl Runtime {
previous_session_store,
host_agents: resolver,
system_jobs: Mutex::new(HashMap::new()),
+ kind_handlers: Mutex::new(Vec::new()),
seams: Mutex::new(seams),
}),
base_config,