Skip to content

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

feat(cron): add origin context to scheduled agent runs

Steven Enamakel7a3368eb9a76
diff --git a/crates/openhuman-core/src/cron/scheduler/agent_run.rs b/crates/openhuman-core/src/cron/scheduler/agent_run.rs
index 24c030eea0..d4343e7aa2 100644
--- a/crates/openhuman-core/src/cron/scheduler/agent_run.rs
+++ b/crates/openhuman-core/src/cron/scheduler/agent_run.rs
@@ -2,6 +2,7 @@
//! (definition selection) and the single scheduled turn.
use super::delivery::is_morning_briefing_job;
+use super::origin_context::build_run_prompt;
use super::failure_classification::classify_agent_anyhow_for_user;
use crate::agent::OpenHumanSessionHost;
use crate::config::Config;
@@ -14,13 +15,23 @@ use crate::cron::{CronJob, SessionTarget};
/// by the `composio_execute` handler via `current_task_recency_window`.
pub(super) const MORNING_BRIEFING_TASK_RECENCY_SECS: u64 = 24 * 60 * 60;
+/// [`run_agent_job_for_run`] under a fresh run id.
+#[cfg(test)]
pub(super) async fn run_agent_job(
config: &Config,
job: &CronJob,
+) -> (bool, String, Option<String>) {
+ let run_id = uuid::Uuid::new_v4().to_string();
+ run_agent_job_for_run(config, job, &run_id).await
+}
+
+pub(super) async fn run_agent_job_for_run(
+ config: &Config,
+ job: &CronJob,
+ run_id: &str,
) -> (bool, String, Option<String>) {
let name = job.name.clone().unwrap_or_else(|| "cron-job".to_string());
- let prompt = job.prompt.clone().unwrap_or_default();
- let prefixed_prompt = format!("[cron:{} {name}] {prompt}", job.id);
+ let prefixed_prompt = build_run_prompt(config, job, &name).await;
// Apply per-job model override onto a cloned Config, so the Agent
// sees it through the normal `default_model` path without mutating
@@ -123,7 +134,7 @@ pub(super) async fn run_agent_job(
}
let run_result = match job.session_target {
- SessionTarget::Main | SessionTarget::Isolated => {
+ SessionTarget::Main | SessionTarget::Isolated | SessionTarget::Current => {
tracing::debug!(
job_id = %job.id,
target = ?job.session_target,
@@ -141,15 +152,14 @@ pub(super) async fn run_agent_job(
// conversation, with its frozen system prompt and stale tool
// names. Every run starts from a fresh prompt instead.
start_cron_turn_clean(&mut agent);
- // Scope a `TrustedAutomation { Cron }` origin around the
- // turn. The approval gate treats this as user-authorized
- // automation and lets external_effect tools run without
- // an in-app prompt — the user explicitly created this
- // cron job and authorized its prompt at the same time.
- let origin = crate::agent::turn_origin::AgentTurnOrigin::TrustedAutomation {
- job_id: job.id.clone(),
- source: crate::agent::turn_origin::TrustedAutomationSource::Cron,
- };
+ // Scope the run's origin around the turn. A job with no
+ // channel origin runs as `TrustedAutomation { Cron }`: the
+ // approval gate treats it as user-authorized automation
+ // (the user created the job and authorized its prompt
+ // together). A job created from a channel chat runs as
+ // that channel's turn instead, so its external-effect
+ // tools stay gated like its creator's were.
+ let origin = crate::cron::origin::turn_origin_for_job_run(job, run_id);
let turn = crate::agent::turn_origin::with_origin(
origin,
agent.run_single(&prefixed_prompt),
diff --git a/crates/openhuman-core/src/cron/scheduler/origin_context.rs b/crates/openhuman-core/src/cron/scheduler/origin_context.rs
index 4ef633e960..1eeea00bd8 100644
--- a/crates/openhuman-core/src/cron/scheduler/origin_context.rs
+++ b/crates/openhuman-core/src/cron/scheduler/origin_context.rs
@@ -2,7 +2,6 @@
//! a bounded tail of the conversation that created the job, plus a fixed
//! preamble saying the run is unattended.
-use super::origin_delivery::NO_REPLY;
use crate::config::Config;
use crate::cron::{channel_bridge, CronJob, JobOrigin};
@@ -14,12 +13,6 @@ pub(crate) const CONTEXT_MAX_CHARS: usize = 1400;
/// Fixed preamble for an unattended scheduled run.
pub(crate) const UNATTENDED_PREAMBLE: &str = "This is a scheduled run, not a live chat. Your final reply is delivered to the user as-is. If there is nothing worth sending, reply exactly NO_REPLY.";
-// Keep the sentinel in the preamble tied to the delivery side's constant.
-const _: () = {
- let (a, b) = (UNATTENDED_PREAMBLE.as_bytes(), NO_REPLY.as_bytes());
- assert!(b.len() < a.len());
-};
-
/// Render the newest messages that fit within `max_messages` / `max_chars` as
/// `role: text` lines, oldest first. A message that does not fit whole is cut
/// to its tail when there is meaningful room left, else dropped.