Skip to content

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

test(cron): cover trusted memory jobs under agent-only limits

Steven Enamakel85ef98d04a21
diff --git a/crates/openhuman-core/src/cron/bus_host.rs b/crates/openhuman-core/src/cron/bus_host.rs
index 9f693f88b6..7847d99810 100644
--- a/crates/openhuman-core/src/cron/bus_host.rs
+++ b/crates/openhuman-core/src/cron/bus_host.rs
@@ -292,3 +292,7 @@ pub(crate) fn kind_refusal(binding: &Binding, job_type: &JobType) -> Option<Stri
#[cfg(test)]
#[path = "bus_host_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "bus_host_trusted_system_tests.rs"]
+mod trusted_system_tests;
diff --git a/crates/openhuman-core/src/cron/bus_host_trusted_system_tests.rs b/crates/openhuman-core/src/cron/bus_host_trusted_system_tests.rs
new file mode 100644
index 0000000000..0054312789
--- /dev/null
+++ b/crates/openhuman-core/src/cron/bus_host_trusted_system_tests.rs
@@ -0,0 +1,81 @@
+use super::*;
+use std::sync::atomic::{AtomicUsize, Ordering};
+use tempfile::TempDir;
+use tinyflows_bus::schedule::{callbacks, Attempt, CALLBACK_INTERFACE};
+
+fn config(tmp: &TempDir) -> Config {
+ let config = Config {
+ workspace_dir: tmp.path().join("workspace"),
+ action_dir: tmp.path().join("workspace"),
+ config_path: tmp.path().join("config.toml"),
+ ..Config::default()
+ };
+ std::fs::create_dir_all(&config.workspace_dir).unwrap();
+ config
+}
+
+async fn agent_only_host(config: &Config) -> (HostRegistration, tinybus::Proxy) {
+ crate::modules::tinyflows::proxy(config)
+ .await
+ .expect("module attaches");
+ let security = Arc::new(crate::security::SecurityPolicy::from_config(
+ &config.autonomy,
+ &config.workspace_dir,
+ &config.action_dir,
+ ));
+ let binding = Binding::new(&DispatchCtx::ambient(), config, security).agent_only();
+ let registration = register(binding).await.expect("host object is served");
+ let address = registration.address();
+ let proxy = crate::modules::runtime::runtime()
+ .await
+ .unwrap()
+ .connection()
+ .proxy(
+ &address.destination,
+ &address.object_path,
+ CALLBACK_INTERFACE,
+ )
+ .expect("the contract callback interface is served");
+ (registration, proxy)
+}
+
+#[tokio::test]
+async fn schedule_trigger_runs_an_internally_seeded_memory_job_in_agent_only_scope() {
+ let tmp = TempDir::new().unwrap();
+ let mut config = config(&tmp);
+ crate::cron::system_jobs::ensure_memory_jobs(&config).unwrap();
+ config.cron.agent_jobs_only = true;
+ let job = crate::cron::list_jobs(&config)
+ .unwrap()
+ .into_iter()
+ .find(|job| {
+ crate::cron::system_jobs::system_job_name(job)
+ == Some(crate::memory::lifecycle::jobs::BACKGROUND_JOB)
+ })
+ .unwrap();
+
+ let calls = Arc::new(AtomicUsize::new(0));
+ let seen = Arc::clone(&calls);
+ let _handler = crate::cron::system_job_handlers::register(
+ crate::memory::lifecycle::jobs::BACKGROUND_JOB,
+ Arc::new(move |context| {
+ assert_eq!(context.name, crate::memory::lifecycle::jobs::BACKGROUND_JOB);
+ let seen = Arc::clone(&seen);
+ Box::pin(async move {
+ seen.fetch_add(1, Ordering::SeqCst);
+ Ok(())
+ })
+ }),
+ );
+ let (_host, proxy) = agent_only_host(&config).await;
+ let attempt: Attempt = proxy
+ .call(
+ callbacks::SCHEDULE_TRIGGER,
+ (&job.command, "run-memory-agent-only"),
+ )
+ .await
+ .unwrap();
+
+ assert!(attempt.success, "{}", attempt.output);
+ assert_eq!(calls.load(Ordering::SeqCst), 1);
+}
diff --git a/crates/openhuman-core/src/cron/limits_tests.rs b/crates/openhuman-core/src/cron/limits_tests.rs
index 6d36bedf30..c7aee09c07 100644
--- a/crates/openhuman-core/src/cron/limits_tests.rs
+++ b/crates/openhuman-core/src/cron/limits_tests.rs
@@ -269,6 +269,30 @@ fn only_reserved_memory_system_jobs_pass_the_agent_only_run_check() {
assert!(limits::check_run(&config, &other_system).is_err());
}
+#[test]
+fn only_reserved_memory_system_jobs_pass_the_agent_only_run_check() {
+ let tmp = TempDir::new().unwrap();
+ let mut config = limited(&tmp);
+ config.cron.agent_jobs_only = false;
+ crate::cron::system_jobs::ensure_memory_jobs(&config).unwrap();
+ let memory = crate::cron::list_jobs(&config)
+ .unwrap()
+ .into_iter()
+ .find(|job| {
+ crate::cron::system_jobs::system_job_name(job)
+ == Some(crate::memory::lifecycle::jobs::BACKGROUND_JOB)
+ })
+ .unwrap();
+
+ config.cron.agent_jobs_only = true;
+ limits::check_run(&config, &memory)
+ .expect("the memory owner may run its internally seeded system job");
+
+ let mut other_system = memory;
+ other_system.command = "system:untrusted_flow".into();
+ assert!(limits::check_run(&config, &other_system).is_err());
+}
+
#[tokio::test]
async fn run_now_refuses_a_stored_job_that_breaks_the_limits() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/openhuman-core/src/cron/schemas_tests.rs b/crates/openhuman-core/src/cron/schemas_tests.rs
index 6f85e16664..31f492d55b 100644
--- a/crates/openhuman-core/src/cron/schemas_tests.rs
+++ b/crates/openhuman-core/src/cron/schemas_tests.rs
@@ -227,6 +227,28 @@ async fn rpc_add_refuses_when_cron_is_disabled_without_creating_job() {
assert!(crate::cron::list_jobs(&config).unwrap().is_empty());
}
+#[tokio::test]
+async fn rpc_add_cannot_create_a_memory_system_flow_from_parameters() {
+ let tmp = TempDir::new().unwrap();
+ let mut config = cron_test_config(&tmp);
+ config.cron.agent_jobs_only = true;
+ let runtime = cron_test_runtime(config.clone()).await;
+ let result = runtime
+ .invoke(
+ "openhuman.cron_add",
+ json!({
+ "job_type": "flow",
+ "schedule": {"kind":"every", "every_ms":300000},
+ "command": "system:memory_background"
+ }),
+ )
+ .await
+ .unwrap_err();
+
+ assert!(result.contains("invalid 'job_type': flow"), "{result}");
+ assert!(crate::cron::list_jobs(&config).unwrap().is_empty());
+}
+
#[test]
fn all_controller_schemas_covers_every_supported_function() {
let names: Vec<_> = all_controller_schemas()
diff --git a/crates/openhuman-core/src/cron/system_jobs_tests.rs b/crates/openhuman-core/src/cron/system_jobs_tests.rs
index 73c5b4e74d..d4e82586a1 100644
--- a/crates/openhuman-core/src/cron/system_jobs_tests.rs
+++ b/crates/openhuman-core/src/cron/system_jobs_tests.rs
@@ -147,3 +147,22 @@ fn memory_jobs_seed_under_agent_only_without_authorizing_generic_flows() {
assert!(add_flow_schedule_job(&config, "system:memory_background", every(30)).is_err());
assert!(ensure_system_job(&config, "untrusted_flow", every(30)).is_err());
}
+
+#[test]
+fn memory_jobs_seed_under_agent_only_without_authorizing_generic_flows() {
+ let tmp = TempDir::new().unwrap();
+ let mut config = test_config(&tmp);
+ config.cron.agent_jobs_only = true;
+
+ ensure_memory_jobs(&config).expect("trusted memory jobs should seed in an enforced scope");
+ let background = system_rows(&config, BACKGROUND_JOB);
+ let sync = system_rows(&config, SOURCES_SYNC_JOB);
+ assert_eq!(background.len(), 1);
+ assert_eq!(sync.len(), 1);
+ for job in background.iter().chain(sync.iter()) {
+ crate::cron::limits::check_run(&config, job).unwrap();
+ }
+
+ assert!(add_flow_schedule_job(&config, "system:memory_background", every(30)).is_err());
+ assert!(ensure_system_job(&config, "untrusted_flow", every(30)).is_err());
+}
diff --git a/crates/openhuman-core/src/profiles/cron_tests.rs b/crates/openhuman-core/src/profiles/cron_tests.rs
index 0b450f2028..7f0326eb9e 100644
--- a/crates/openhuman-core/src/profiles/cron_tests.rs
+++ b/crates/openhuman-core/src/profiles/cron_tests.rs
@@ -1,5 +1,38 @@
use super::*;
use crate::core::runtime::{CoreContext, DomainSet, SaasConfig};
+use crate::cron::host_delivery::{CronDelivery, CronReply};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+struct NoopDelivery;
+
+#[async_trait::async_trait]
+impl CronDelivery for NoopDelivery {
+ async fn deliver(&self, _: CronReply) -> Result<(), String> {
+ Ok(())
+ }
+}
+
+struct RestoreInstalledStorage(Option<Arc<dyn crate::storage::StorageBackend>>);
+
+impl Drop for RestoreInstalledStorage {
+ fn drop(&mut self) {
+ crate::storage::clear();
+ if let Some(previous) = self.0.take() {
+ crate::storage::install(previous);
+ }
+ }
+}
+
+struct RestoreCronDelivery(Option<Arc<dyn CronDelivery>>);
+
+impl Drop for RestoreCronDelivery {
+ fn drop(&mut self) {
+ crate::cron::host_delivery::clear();
+ if let Some(previous) = self.0.take() {
+ crate::cron::host_delivery::install(previous);
+ }
+ }
+}
#[tokio::test]
async fn a_host_without_a_shared_backend_sweeps_nothing() {
@@ -10,3 +43,116 @@ async fn a_host_without_a_shared_backend_sweeps_nothing() {
);
assert_eq!(sweep(&host, Utc::now()).await, SweepReport::default());
}
+
+#[tokio::test]
+async fn hosted_cron_sweep_runs_trusted_memory_jobs_in_agent_only_profiles() {
+ const CHILD: &str = "OPENHUMAN_HOSTED_MEMORY_CRON_SWEEP_CHILD";
+ if std::env::var_os(CHILD).is_none() {
+ let output = std::process::Command::new(std::env::current_exe().unwrap())
+ .args([
+ "--exact",
+ "profiles::cron::tests::hosted_cron_sweep_runs_trusted_memory_jobs_in_agent_only_profiles",
+ "--nocapture",
+ ])
+ .env(CHILD, "1")
+ .output()
+ .unwrap();
+ let stdout = String::from_utf8_lossy(&output.stdout);
+ assert!(
+ output.status.success(),
+ "isolated sweep child failed:\n{stdout}\n{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ assert!(
+ stdout.contains("running 1 test"),
+ "child selected no test: {stdout}"
+ );
+ assert!(
+ stdout.contains("test profiles::cron::tests::hosted_cron_sweep_runs_trusted_memory_jobs_in_agent_only_profiles"),
+ "expected child test did not run: {stdout}"
+ );
+ assert!(
+ stdout.contains("test result: ok. 1 passed"),
+ "child failed: {stdout}"
+ );
+ return;
+ }
+
+ hosted_memory_cron_sweep_child().await;
+}
+
+async fn hosted_memory_cron_sweep_child() {
+ let registry = crate::storage::fence::registry();
+ let backend: Arc<dyn crate::storage::StorageBackend> =
+ Arc::new(crate::storage::fenced_backend::FencedBackend::new(
+ Arc::new(crate::storage::MemoryStorage::new()),
+ Arc::clone(&registry),
+ ));
+ let _storage = RestoreInstalledStorage(crate::storage::install(backend));
+ let _delivery =
+ RestoreCronDelivery(crate::cron::host_delivery::install(Arc::new(NoopDelivery)));
+
+ let tmp = tempfile::tempdir().unwrap();
+ let id = crate::profiles::ProfileId::for_user(
+ "memory-cron-hosted-sweep",
+ crate::profiles::ProfileIdMode::Raw,
+ )
+ .unwrap();
+ let context = CoreContext::for_test(DomainSet::full(), None);
+ let seed_host = ProfileHost::new(SaasConfig::new(tmp.path()), Arc::clone(&context));
+ seed_host.provision(&id).await.unwrap();
+ let opened = seed_host.open(&id).await.unwrap();
+ let config = opened.config.clone();
+ CoreContext::scope(Arc::clone(opened.context()), async {
+ crate::cron::system_jobs::ensure_memory_jobs(&config).unwrap();
+ let due_at = Utc::now() - chrono::Duration::seconds(1);
+ for job in crate::cron::list_jobs(&config).unwrap() {
+ if crate::cron::system_jobs::system_job_name(&job).is_some_and(|name| {
+ name == crate::memory::lifecycle::jobs::BACKGROUND_JOB
+ || name == crate::memory::bus::SOURCES_SYNC_JOB
+ }) {
+ crate::cron::store::tests::force_next_run_for_test(&config, &job.id, due_at)
+ .unwrap();
+ }
+ }
+ })
+ .await;
+ seed_host.release(&id).await.unwrap();
+
+ let mut saas = SaasConfig::new(tmp.path());
+ saas.cron.agent_jobs_only = true;
+ saas.cron.max_active_jobs = 2;
+ let host = ProfileHost::new(saas, context);
+ assert!(!host.provision(&id).await.unwrap());
+
+ let calls = Arc::new(AtomicUsize::new(0));
+ let background_calls = Arc::clone(&calls);
+ let _background = crate::cron::system_job_handlers::register(
+ crate::memory::lifecycle::jobs::BACKGROUND_JOB,
+ Arc::new(move |_| {
+ let calls = Arc::clone(&background_calls);
+ Box::pin(async move {
+ calls.fetch_add(1, Ordering::SeqCst);
+ Ok(())
+ })
+ }),
+ );
+ let sync_calls = Arc::clone(&calls);
+ let _sync = crate::cron::system_job_handlers::register(
+ crate::memory::bus::SOURCES_SYNC_JOB,
+ Arc::new(move |_| {
+ let calls = Arc::clone(&sync_calls);
+ Box::pin(async move {
+ calls.fetch_add(1, Ordering::SeqCst);
+ Ok(())
+ })
+ }),
+ );
+
+ let report = sweep(&host, Utc::now()).await;
+ assert_eq!(
+ report.ran, 2,
+ "both trusted memory jobs should run: {report:?}"
+ );
+ assert_eq!(calls.load(Ordering::SeqCst), 2);
+}