Skip to content

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

chore: files changed crates/openhuman-core/src/cron/tools/update_tests.rs

Steven Enamakeldf32f8df55fb
diff --git a/crates/openhuman-core/src/cron/tools/update_tests.rs b/crates/openhuman-core/src/cron/tools/update_tests.rs
index f10592f6e5..18525a168a 100644
--- a/crates/openhuman-core/src/cron/tools/update_tests.rs
+++ b/crates/openhuman-core/src/cron/tools/update_tests.rs
@@ -1,6 +1,66 @@
use super::*;
+use crate::agent::tinyagents::host::OpenHumanRunContext;
+use crate::agent::turn_origin::AgentTurnOrigin;
use crate::config::Config;
use tempfile::TempDir;
+use tinyagents_harness::{context::RunConfig, ids::CallId, tool::ToolExecutionContext};
+use tinyflows_bus::schedule::types::{
+ AgentJobSpec, DeliveryConfig, JobOrigin, Schedule, SessionTarget,
+};
+
+fn channel_origin(reply_target: &str) -> JobOrigin {
+ JobOrigin::Channel {
+ channel: "telegram".into(),
+ reply_target: reply_target.into(),
+ history_key: format!("telegram-user-{reply_target}"),
+ sender: Some("alice".into()),
+ thread_id: None,
+ }
+}
+
+fn trusted_channel_context(reply_target: &str) -> (impl Sized, ToolExecutionContext) {
+ let mut run_context = OpenHumanRunContext::new();
+ run_context.origin = Some(AgentTurnOrigin::ExternalChannel {
+ sender_name: None,
+ channel: "telegram".into(),
+ sender: Some("alice".into()),
+ reply_target: reply_target.into(),
+ message_id: "message-1".into(),
+ history_key: Some(format!("telegram-user-{reply_target}")),
+ });
+ let run = run_context.into_tinyagents(RunConfig::new("cron-update-context"));
+ let tool_context =
+ ToolExecutionContext::from_run_context(&run, CallId::new("cron-update-call"));
+ (run, tool_context)
+}
+
+fn authorized_channel_config(tmp: &TempDir) -> Arc<Config> {
+ let mut config = Config {
+ workspace_dir: tmp.path().join("workspace"),
+ action_dir: tmp.path().join("workspace"),
+ config_path: tmp.path().join("config.toml"),
+ ..Config::default()
+ };
+ config.channels_config.telegram = Some(crate::config::TelegramConfig {
+ bot_token: "test-token".into(),
+ chat_id: None,
+ allowed_users: vec!["alice".into()],
+ stream_mode: Default::default(),
+ draft_update_interval_ms: 1000,
+ silent_streaming: true,
+ mention_only: false,
+ });
+ std::fs::create_dir_all(&config.workspace_dir).unwrap();
+ Arc::new(config)
+}
+
+fn channel_agent_job(config: &Config, origin: JobOrigin) -> tinyflows_bus::schedule::types::CronJob {
+ let mut spec = AgentJobSpec::new(Schedule::Every { every_ms: 300_000 }, "check inbox");
+ spec.session_target = SessionTarget::Isolated;
+ spec.delivery = Some(DeliveryConfig::default());
+ spec.origin = Some(origin);
+ cron::add_agent_job_from_spec(config, spec).unwrap()
+}
async fn test_config(tmp: &TempDir) -> Arc<Config> {
let config = Config {
@@ -157,3 +217,58 @@ async fn rejects_tightening_an_agent_job_below_five_minutes() {
.unwrap();
assert!(!result.is_error, "{:?}", result.output());
}
+
+#[tokio::test]
+async fn trusted_current_channel_can_update_delivery_to_its_own_target() {
+ let tmp = TempDir::new().unwrap();
+ let cfg = authorized_channel_config(&tmp);
+ let job = channel_agent_job(&cfg, channel_origin("42"));
+ let (_run, context) = trusted_channel_context("42");
+ let tool = CronUpdateTool::new(cfg.clone(), test_security(&cfg));
+
+ let result = tool
+ .execute_with_context(
+ json!({
+ "job_id": job.id,
+ "patch": {"delivery":{"mode":"origin"}}
+ }),
+ ToolCallOptions::default(),
+ Some(&context),
+ )
+ .await
+ .unwrap();
+
+ assert!(!result.is_error, "{}", result.output());
+ assert_eq!(cron::get_job(&cfg, &job.id).unwrap().delivery.mode, "origin");
+}
+
+#[tokio::test]
+async fn trusted_current_channel_cannot_authorize_spoofed_origin_update_target() {
+ let tmp = TempDir::new().unwrap();
+ let cfg = authorized_channel_config(&tmp);
+ let original_origin = channel_origin("42");
+ let job = channel_agent_job(&cfg, original_origin.clone());
+ let (_run, context) = trusted_channel_context("42");
+ let tool = CronUpdateTool::new(cfg.clone(), test_security(&cfg));
+
+ let result = tool
+ .execute_with_context(
+ json!({
+ "job_id": job.id,
+ "patch": {
+ "delivery":{"mode":"origin"},
+ "origin": channel_origin("999")
+ }
+ }),
+ ToolCallOptions::default(),
+ Some(&context),
+ )
+ .await
+ .unwrap();
+
+ assert!(result.is_error, "{}", result.output());
+ assert!(result.output().contains("allowed"), "{}", result.output());
+ let saved = cron::get_job(&cfg, &job.id).unwrap();
+ assert_eq!(saved.origin, Some(original_origin));
+ assert_eq!(saved.delivery.mode, "none");
+}