Skip to content

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

test(storage): harden storage e2e drivers and scope assertions

Steven Enamakela688b2f329b8
diff --git a/scripts/__tests__/self-hosted-lanes.test.mjs b/scripts/__tests__/self-hosted-lanes.test.mjs
index 12c6b12dcc..cf4e2a8d62 100644
--- a/scripts/__tests__/self-hosted-lanes.test.mjs
+++ b/scripts/__tests__/self-hosted-lanes.test.mjs
@@ -257,7 +257,7 @@ test("the storage area arms only the storage-drivers lane, with the sqlite and f
const lane = plan.lanes.find((l) => l.name === "storage-drivers");
assert.equal(lane.active, true, profile);
for (const check of lane.checks) {
- assert.match(check.run, /--features [a-z,-]*storage-sqlite,storage-file/);
+ assert.match(check.run, /--features [a-z,-]*storage-sqlite,storage-file(?:\s|$)/);
}
// Every root storage target runs in the lane, so a new one cannot be
// forgotten: the targets on disk are exactly the targets in the command.
diff --git a/tests/storage_agent_scopes_e2e.rs b/tests/storage_agent_scopes_e2e.rs
index 80b1c78f1f..8ebed0f0af 100644
--- a/tests/storage_agent_scopes_e2e.rs
+++ b/tests/storage_agent_scopes_e2e.rs
@@ -20,6 +20,9 @@ use openhuman_core::storage::{self, Scope};
use openhuman_core::HostKind;
use serde_json::json;
+#[path = "support/tinyhumans_boot.rs"]
+mod tinyhumans_boot;
+
#[macro_use]
#[path = "support/storage_drivers.rs"]
mod storage_drivers;
@@ -76,6 +79,22 @@ async fn flow_names(config: &Config) -> Vec<String> {
.collect()
}
+/// Waits (bounded) until the clock has reached the job's next run in the
+/// agent's scope, so the scheduler pass finds it due without a fixed sleep.
+async fn wait_until_due(ctx: &Arc<CoreContext>, config: &Config, id: &str) {
+ for _ in 0..200 {
+ let next_run = CoreContext::scope(Arc::clone(ctx), async {
+ cron::get_job(config, id).unwrap().next_run
+ })
+ .await;
+ if next_run <= chrono::Utc::now() {
+ return;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(25)).await;
+ }
+ panic!("job {id} never became due");
+}
+
async fn two_agents_keep_cron_and_flows_apart(case: Case) {
let workspace = tempfile::tempdir().unwrap();
let config = Config {
@@ -83,6 +102,7 @@ async fn two_agents_keep_cron_and_flows_apart(case: Case) {
config_path: workspace.path().join("config.toml"),
..Config::default()
};
+ tinyhumans_boot::boot();
let _runtime = CoreBuilder::new(HostKind::Library)
.config(config.clone())
.services(ServiceSet::none())
@@ -146,7 +166,9 @@ async fn two_agents_keep_cron_and_flows_apart(case: Case) {
.await;
// The scheduler's agent pass runs each agent's due job under that agent.
- tokio::time::sleep(std::time::Duration::from_millis(20)).await;
+ for (ctx, id) in [(&alpha, &alpha_job.id), (&beta, &beta_job.id)] {
+ wait_until_due(ctx, &config, id).await;
+ }
cron::scheduler::run_live_agent_pass(2).await;
for (agent, ctx, own, other) in [
("alpha", &alpha, &alpha_job.id, &beta_job.id),
@@ -193,6 +215,7 @@ async fn no_acting_agent_falls_back_to_the_local_scope(case: Case) {
config_path: workspace.path().join("config.toml"),
..Config::default()
};
+ tinyhumans_boot::boot();
let _runtime = CoreBuilder::new(HostKind::Library)
.config(config.clone())
.services(ServiceSet::none())
diff --git a/tests/storage_delegation_e2e.rs b/tests/storage_delegation_e2e.rs
index 0f97d320c2..084c0d9f6d 100644
--- a/tests/storage_delegation_e2e.rs
+++ b/tests/storage_delegation_e2e.rs
@@ -18,6 +18,9 @@ use openhuman_core::HostKind;
use tinyagents_graph::checkpoint::{Checkpoint, Checkpointer};
use tinyagents_graph::delegation::DelegationState;
+#[path = "support/tinyhumans_boot.rs"]
+mod tinyhumans_boot;
+
#[macro_use]
#[path = "support/storage_drivers.rs"]
mod storage_drivers;
@@ -55,6 +58,7 @@ async fn the_delegation_checkpointer_resumes_and_keeps_scopes_apart(case: Case)
config_path: workspace.path().join("config.toml"),
..Config::default()
};
+ tinyhumans_boot::boot();
let _runtime = CoreBuilder::new(HostKind::Library)
.config(config.clone())
.services(ServiceSet::none())
@@ -104,9 +108,9 @@ async fn the_delegation_checkpointer_resumes_and_keeps_scopes_apart(case: Case)
// Resume: a fresh handle reads the latest checkpoint of the thread, in
// its own scope.
- for (context, expected, latest) in [
- (&alpha, "alpha plan, step 2", "c2"),
- (&beta, "beta plan", "c1"),
+ for (context, expected, latest, named_c1) in [
+ (&alpha, "alpha plan, step 2", "c2", "alpha plan, step 1"),
+ (&beta, "beta plan", "c1", "beta plan"),
] {
CoreContext::scope(Arc::clone(context), async {
let checkpointer = open_delegation_checkpointer(&config).unwrap();
@@ -115,7 +119,11 @@ async fn the_delegation_checkpointer_resumes_and_keeps_scopes_apart(case: Case)
assert_eq!(resumed.state.plan.as_deref(), Some(expected));
// A named checkpoint resolves in the same scope only.
let first = checkpointer.get("run-1", Some("c1")).await.unwrap();
- assert!(first.is_some());
+ assert_eq!(
+ first.and_then(|checkpoint| checkpoint.state.plan).as_deref(),
+ Some(named_c1),
+ "the named checkpoint is this agent's own"
+ );
assert_eq!(
checkpointer.list_threads().await.unwrap(),
vec!["run-1".to_string()]
diff --git a/tests/storage_scope_e2e.rs b/tests/storage_scope_e2e.rs
index 751889fe48..ab60111740 100644
--- a/tests/storage_scope_e2e.rs
+++ b/tests/storage_scope_e2e.rs
@@ -21,6 +21,9 @@ use openhuman_core::cron::{self, Schedule};
use openhuman_core::storage::agents::{find_owner, for_each_scope, within_agent};
use openhuman_core::HostKind;
+#[path = "support/tinyhumans_boot.rs"]
+mod tinyhumans_boot;
+
#[macro_use]
#[path = "support/storage_drivers.rs"]
mod storage_drivers;
@@ -42,6 +45,7 @@ async fn background_work_visits_every_agent_scope(case: Case) {
config_path: workspace.path().join("config.toml"),
..Config::default()
};
+ tinyhumans_boot::boot();
let _runtime = CoreBuilder::new(HostKind::Library)
.config(config.clone())
.services(ServiceSet::none())
diff --git a/tests/support/storage_drivers.rs b/tests/support/storage_drivers.rs
index 58cd45f864..3ec05f9a4d 100644
--- a/tests/support/storage_drivers.rs
+++ b/tests/support/storage_drivers.rs
@@ -89,6 +89,13 @@ impl Driver {
let base = std::env::var(MONGO_URL_VAR)
.ok()
.filter(|url| !url.trim().is_empty())?;
+ if !is_loopback_mongo_url(base.trim()) {
+ eprintln!(
+ "skipped mongodb: {MONGO_URL_VAR} must name a local throwaway server \
+ (localhost, 127.0.0.1 or ::1); the suite writes databases to it"
+ );
+ return None;
+ }
Some(mongo_url_with_database(
base.trim(),
&format!("oh_e2e_{}", uuid_like()),
@@ -98,6 +105,26 @@ impl Driver {
}
}
+/// Whether every host in a MongoDB URL is the local machine. The matrix
+/// creates and fills databases, so it only ever talks to a throwaway server
+/// on this host or in this job's own container (as `tinystoragedrivers`' CI
+/// does), never to a shared or remote deployment.
+pub fn is_loopback_mongo_url(url: &str) -> bool {
+ let rest = url.split_once("://").map_or(url, |(_, rest)| rest);
+ let authority = rest.split(['/', '?']).next().unwrap_or_default();
+ let hosts = authority.rsplit_once('@').map_or(authority, |(_, h)| h);
+ !hosts.is_empty()
+ && !url.starts_with("mongodb+srv://")
+ && hosts.split(',').all(|host| {
+ let name = if let Some(v6) = host.strip_prefix('[') {
+ v6.split(']').next().unwrap_or_default()
+ } else {
+ host.rsplit_once(':').map_or(host, |(name, _)| name)
+ };
+ matches!(name, "localhost" | "127.0.0.1" | "::1")
+ })
+}
+
/// `base` (a `mongodb://` or `mongodb+srv://` URL, with or without a database
/// and options) pointed at database `database`, keeping its host and query.
pub fn mongo_url_with_database(base: &str, database: &str) -> String {
@@ -204,6 +231,11 @@ pub struct UrlCase {
/// Runs a body that only needs the driver's URL (`driver_cases!(url body)`).
pub fn run_url(driver: Driver, body: fn(UrlCase)) {
+ // Same lock as the in-process cases: the spawned cores and this process
+ // share the environment and, for MongoDB, one server.
+ let _lock = SLOT_LOCK
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
let data_dir = tempfile::tempdir().expect("temp dir");
let Some(url) = driver.url(data_dir.path()) else {
eprintln!("skipped {}: {MONGO_URL_VAR} is not set", driver.name());