Skip to content

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

Merge upstream/main into tiny-dep-tinyflows

Steven Enamakel3777e1ab3fae
diff --cc crates/openhuman-core/src/cron/scheduler.rs
index 514e947911,8eb8541455..f9936c8ddf
--- a/crates/openhuman-core/src/cron/scheduler.rs
+++ b/crates/openhuman-core/src/cron/scheduler.rs
@@@ -102,63 -98,42 +102,60 @@@ pub async fn run(config: Config) -> Res
}
}
-/// Runs the due jobs of every live embedded agent, each under that agent's
-/// own context: its config, provider route, policy and job store. An agent
-/// that is not live has no context to run under — no host tools or prompt
-/// for its agent jobs — so its jobs stay dormant until it is instantiated
-/// again, including when a storage backend still records it
-/// (`crate::storage::agents`).
-pub(crate) async fn tick_live_agents(dispatcher: &mut JobDispatcher) {
- for (agent_id, ctx) in crate::core::runtime::AgentContextRegistry::live() {
- let agent = agent_id.clone();
- let dispatcher = &mut *dispatcher;
- crate::core::runtime::CoreContext::scope(ctx, async move {
- let config = match crate::config::ops::load_current_or_init().await {
- Ok(config) => config,
- Err(error) => {
- tracing::debug!(agent = %agent, %error, "[cron:scheduler] agent config unavailable");
- return;
- }
- };
- let jobs = match due_jobs(&config, Utc::now()) {
- Ok(jobs) => jobs,
- Err(error) => {
- tracing::warn!(agent = %agent, "[cron:scheduler] agent poll db_error: {error}");
+/// The schedulers of the live embedded agents, one per agent id.
+#[derive(Default)]
+pub(crate) struct LiveAgentSchedulers {
+ by_agent: HashMap<String, JobDispatcher>,
+}
+
+impl LiveAgentSchedulers {
+ /// Runs the due jobs of every live embedded agent, each under that agent's
+ /// own context: its config, provider route, policy and job store. An agent
+ /// that is not live has no context to run under — no host tools or prompt
+ /// for its agent jobs — so its jobs stay dormant until it is instantiated
+ /// again, including when a storage backend still records it
+ /// (`crate::storage::agents`).
+ pub(crate) async fn tick(&mut self) {
+ let live = crate::core::runtime::AgentContextRegistry::live();
+ // A scheduler whose agent is gone aborts the jobs it still owns.
+ self.by_agent
+ .retain(|id, _| live.iter().any(|(live_id, _)| live_id == id));
+ for (agent_id, ctx) in live {
+ let scheduler = &mut self.by_agent;
+ crate::core::runtime::CoreContext::scope(ctx, async move {
+ let config = match crate::config::ops::load_current_or_init().await {
+ Ok(config) => config,
+ Err(error) => {
+ tracing::debug!(agent = %agent_id, %error, "[cron:scheduler] agent config unavailable");
+ return;
+ }
+ };
- if !agent_jobs_may_exist(&config) {
- return;
- }
+ let Some(security) = crate::security::live_policy::effective() else {
+ tracing::warn!(agent = %agent_id, "[cron:scheduler] agent has no policy; jobs skipped");
return;
+ };
+ let dispatcher = scheduler
+ .entry(agent_id.clone())
+ .or_insert_with(|| JobDispatcher::new(&config, &security));
+ match dispatcher.tick(&config).await {
+ Ok(0) => {}
+ Ok(count) => {
+ tracing::debug!(agent = %agent_id, count, "[cron:scheduler] agent jobs dispatched");
+ }
+ Err(error) => {
+ tracing::warn!(agent = %agent_id, "[cron:scheduler] agent poll failed: {error}");
+ }
}
- };
- if jobs.is_empty() {
- return;
- }
- let Some(security) = crate::security::live_policy::effective() else {
- tracing::warn!(agent = %agent, "[cron:scheduler] agent has no policy; jobs skipped");
- return;
- };
- tracing::debug!(agent = %agent, due_count = jobs.len(), "[cron:scheduler] running agent jobs");
- dispatcher.dispatch(&config, &security, jobs).await;
- })
- .await;
+ })
+ .await;
+ }
+ }
+
+ /// Wait for every dispatched agent job to finish.
+ pub(crate) async fn drain(&mut self) {
+ for dispatcher in self.by_agent.values_mut() {
+ dispatcher.drain().await;
+ }
}
}
@@@ -167,36 -142,9 +164,26 @@@
/// tests that drive the agent pass without owning the loop's interval.
pub async fn run_live_agent_pass(max_concurrent: usize) {
tracing::debug!(max_concurrent, "[cron:scheduler] live agent pass begin");
- let mut dispatcher = JobDispatcher::new(max_concurrent.max(1));
- tick_live_agents(&mut dispatcher).await;
- dispatcher.drain().await;
+ let mut agents = LiveAgentSchedulers::default();
+ agents.tick().await;
+ agents.drain().await;
+}
+
- /// Whether an agent can have jobs to poll: with a storage backend its jobs
- /// live in the backend under its scope (`crate::storage`), so there is always
- /// something to ask; without one, only once its own store exists, or the
- /// `jobs.db` tinyflows used to write there (which the first call imports).
- pub(crate) fn agent_jobs_may_exist(config: &Config) -> bool {
- crate::storage::installed().is_some()
- || crate::cron::store::db_path(config).exists()
- || crate::cron::store::legacy_db_path(config).exists()
- }
-
+/// 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) {
+ let security = Arc::new(SecurityPolicy::from_config(
+ &config.autonomy,
+ &config.workspace_dir,
+ &config.action_dir,
+ ));
+ let binding = host::Binding::current(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),
+ Err(error) => (false, error.to_string()),
+ }
}
/// Single poll cycle of the scheduler loop, extracted so tests can drive
diff --cc crates/openhuman-core/src/cron/store.rs
index 6beb1d5443,81dc092b2a..3950bd1344
--- a/crates/openhuman-core/src/cron/store.rs
+++ b/crates/openhuman-core/src/cron/store.rs
@@@ -41,13 -28,8 +28,8 @@@ pub fn legacy_db_path(config: &Config)
}
/// The cron job store for this call, in the acting agent's scope.
-fn documents(config: &Config) -> Result<CronDocuments> {
+pub(crate) fn documents(config: &Config) -> Result<CronDocuments> {
- let opened = crate::storage::local::open_standing(
- &db_path(config),
- "cron::store",
- config,
- &|backend, scope, config| legacy_import(backend, scope, config),
- )?;
+ let opened = crate::storage::local::open("cron::store", &config.workspace_dir)?;
Ok(
CronDocuments::new(std::sync::Arc::clone(opened.scoped()?.documents()))
.with_limits(config.cron.max_run_history, config.scheduler.max_tasks),