Skip to content

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

test(user_agents): add unit tests for host, layout, ops, schemas, and types

Steven Enamakel71e2c4045c73
diff --git a/crates/openhuman-core/src/user_agents/host_tests.rs b/crates/openhuman-core/src/user_agents/host_tests.rs
new file mode 100644
index 0000000000..1b69800130
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/host_tests.rs
@@ -0,0 +1,135 @@
+use super::*;
+
+fn host(tmp: &tempfile::TempDir, max_open: usize, idle_secs: u64) -> AgentHost {
+ let mut saas = SaasConfig::new(tmp.path());
+ saas.max_agents_open = max_open;
+ saas.idle_evict_secs = idle_secs;
+ AgentHost::new(saas, CoreContext::for_test(DomainSet::full(), None))
+}
+
+fn agent(name: &str) -> UserAgentId {
+ UserAgentId::for_user(name).unwrap()
+}
+
+#[test]
+fn provisioning_creates_the_layout_once() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 60);
+ let id = agent("alice");
+ assert!(host.provision(&id).unwrap());
+ assert!(!host.provision(&id).unwrap(), "second provision is a no-op");
+ let layout = UserAgentLayout::new(tmp.path(), &id);
+ assert!(layout.workspace_dir.is_dir() && layout.sandbox_dir.is_dir());
+ let summary = host.summary(&id).unwrap().unwrap();
+ assert_eq!(summary.agent_id, id);
+ assert!(!summary.open);
+}
+
+#[test]
+fn only_provisioned_agents_open() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 60);
+ let err = host.open(&agent("nobody")).unwrap_err();
+ assert!(err.contains("not provisioned"), "{err}");
+}
+
+#[test]
+fn an_open_agent_runs_under_its_own_scope() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 60);
+ let (a, b) = (agent("alice"), agent("bob"));
+ host.provision(&a).unwrap();
+ host.provision(&b).unwrap();
+ let state_a = host.open(&a).unwrap();
+ let state_b = host.open(&b).unwrap();
+
+ assert_eq!(state_a.context().session_agent(), Some(a.as_str()));
+ assert_eq!(state_b.context().session_agent(), Some(b.as_str()));
+ let config_a = state_a.context().embedder_config().unwrap();
+ let config_b = state_b.context().embedder_config().unwrap();
+ assert_ne!(config_a.workspace_dir, config_b.workspace_dir);
+ assert!(config_a.workspace_dir.starts_with(tmp.path()));
+ assert_eq!(state_a.context().domains(), user_domains());
+ assert!(!state_a.context().user_skill_roots());
+
+ // Re-opening hands back the same state.
+ assert!(Arc::ptr_eq(&state_a, &host.open(&a).unwrap()));
+ assert_eq!(host.open_count(), 2);
+}
+
+#[test]
+fn a_full_host_evicts_the_least_recently_used_idle_agent() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 2, 3600);
+ let ids: Vec<_> = ["a", "b", "c"].iter().map(|n| agent(n)).collect();
+ for id in &ids {
+ host.provision(id).unwrap();
+ }
+ drop(host.open(&ids[0]).unwrap());
+ drop(host.open(&ids[1]).unwrap());
+ drop(host.open(&ids[2]).unwrap());
+ assert_eq!(host.open_count(), 2);
+ assert!(!host.is_open(&ids[0]), "the oldest idle agent made room");
+ assert!(host.is_open(&ids[1]) && host.is_open(&ids[2]));
+}
+
+#[test]
+fn an_agent_in_use_is_never_evicted() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 1, 0);
+ let (a, b) = (agent("a"), agent("b"));
+ host.provision(&a).unwrap();
+ host.provision(&b).unwrap();
+ let held = host.open(&a).unwrap();
+ host.evict_idle();
+ assert!(host.is_open(&a), "held agents survive an idle sweep");
+ let err = host.open(&b).unwrap_err();
+ assert!(err.contains("slots are in use"), "{err}");
+ drop(held);
+ host.open(&b).unwrap();
+}
+
+#[test]
+fn idle_agents_are_swept() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 0);
+ let id = agent("a");
+ host.provision(&id).unwrap();
+ drop(host.open(&id).unwrap());
+ host.evict_idle();
+ assert_eq!(host.open_count(), 0);
+}
+
+#[test]
+fn deprovisioning_archives_and_closes() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 60);
+ let id = agent("a");
+ host.provision(&id).unwrap();
+ drop(host.open(&id).unwrap());
+ assert!(host.deprovision(&id).unwrap());
+ assert!(!host.is_open(&id));
+ assert!(host.summary(&id).unwrap().is_none());
+ let archived: Vec<_> = std::fs::read_dir(layout::archive_dir(tmp.path()))
+ .unwrap()
+ .flatten()
+ .collect();
+ assert_eq!(archived.len(), 1, "state is archived, not deleted");
+ assert!(!host.deprovision(&id).unwrap());
+}
+
+#[test]
+fn list_reports_provisioned_agents_only() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 60);
+ assert!(host.list().unwrap().is_empty());
+ let (a, b) = (agent("a"), agent("b"));
+ host.provision(&a).unwrap();
+ host.provision(&b).unwrap();
+ std::fs::create_dir_all(layout::agents_dir(tmp.path()).join("not-an-agent")).unwrap();
+ let _held = host.open(&b).unwrap();
+ let listed = host.list().unwrap();
+ assert_eq!(listed.len(), 2);
+ let open: Vec<_> = listed.iter().filter(|s| s.open).map(|s| &s.agent_id).collect();
+ assert_eq!(open, vec![&b]);
+}
diff --git a/crates/openhuman-core/src/user_agents/layout_tests.rs b/crates/openhuman-core/src/user_agents/layout_tests.rs
new file mode 100644
index 0000000000..da9ee6121a
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/layout_tests.rs
@@ -0,0 +1,61 @@
+use super::*;
+
+fn id() -> UserAgentId {
+ UserAgentId::for_user("layout-user").unwrap()
+}
+
+#[test]
+fn every_path_sits_under_the_agent_directory() {
+ let layout = UserAgentLayout::new(Path::new("/srv/oh"), &id());
+ assert_eq!(layout.dir, Path::new("/srv/oh/agents").join(id().as_str()));
+ for path in [
+ &layout.meta_path,
+ &layout.config_path,
+ &layout.workspace_dir,
+ &layout.sandbox_dir,
+ ] {
+ assert!(path.starts_with(&layout.dir), "{}", path.display());
+ }
+}
+
+#[test]
+fn the_config_is_rooted_in_the_agent_and_bound_to_its_memory() {
+ let layout = UserAgentLayout::new(Path::new("/srv/oh"), &id());
+ let config = agent_config(&layout, &id());
+ assert_eq!(config.config_path, layout.config_path);
+ assert_eq!(config.workspace_dir, layout.workspace_dir);
+ assert_eq!(config.action_dir, layout.sandbox_dir);
+ assert_eq!(config.memory.agent_id.as_deref(), Some(id().as_str()));
+ assert_eq!(config.memory.root, Some(memory_root(&id())));
+}
+
+#[test]
+fn the_memory_root_is_a_valid_layout_root() {
+ crate::memory::scope::validate_root(&memory_root(&id())).unwrap();
+}
+
+#[test]
+fn the_memory_binding_wins_over_pins_and_teams() {
+ use crate::memory::scope::MemoryIdentity;
+ let layout = UserAgentLayout::new(Path::new("/srv/oh"), &id());
+ let mut config = agent_config(&layout, &id());
+ config.memory.agents.insert(
+ "planner".into(),
+ toml::from_str("agent_id = \"other\"\nroot = \"team:shared\"").unwrap(),
+ );
+ let resolved = MemoryIdentity::team_member("shared", "planner").resolve(&config);
+ assert_eq!(resolved.agent_id, id().as_str());
+ assert_eq!(resolved.root().to_string(), memory_root(&id()));
+}
+
+#[test]
+fn the_policy_is_on_and_closed() {
+ let layout = UserAgentLayout::new(Path::new("/srv/oh"), &id());
+ let autonomy = agent_config(&layout, &id()).autonomy;
+ assert!(autonomy.enabled);
+ assert_eq!(autonomy.level, AutonomyLevel::Supervised);
+ assert!(autonomy.workspace_only);
+ assert!(!autonomy.auto_approve_all);
+ assert!(!autonomy.allow_tool_install);
+ assert!(autonomy.trusted_roots.is_empty());
+}
diff --git a/crates/openhuman-core/src/user_agents/ops_tests.rs b/crates/openhuman-core/src/user_agents/ops_tests.rs
new file mode 100644
index 0000000000..eb3e1fa2a8
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/ops_tests.rs
@@ -0,0 +1,36 @@
+use super::*;
+use crate::core::runtime::{CoreContext, DomainSet, SaasConfig};
+
+fn host(tmp: &tempfile::TempDir) -> AgentHost {
+ AgentHost::new(
+ SaasConfig::new(tmp.path()),
+ CoreContext::for_test(DomainSet::full(), None),
+ )
+}
+
+#[test]
+fn provision_derives_the_agent_and_never_echoes_the_user_id() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp);
+ let out = provision_on(&host, "[email protected]").unwrap();
+ let json = out.into_cli_compatible_json().unwrap().to_string();
+ assert!(!json.contains("alice"), "{json}");
+ assert!(json.contains("\"created\":true"), "{json}");
+ let again = provision_on(&host, "[email protected]").unwrap();
+ assert!(again.into_cli_compatible_json().unwrap().to_string().contains("\"created\":false"));
+}
+
+#[test]
+fn status_and_deprovision_take_agent_ids_only() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp);
+ assert!(status_on(&host, "[email protected]").is_err());
+ assert!(deprovision_on(&host, "../operator").is_err());
+
+ let id = UserAgentId::for_user("alice").unwrap();
+ assert!(status_on(&host, id.as_str()).unwrap_err().contains("not provisioned"));
+ provision_on(&host, "alice").unwrap();
+ status_on(&host, id.as_str()).unwrap();
+ deprovision_on(&host, id.as_str()).unwrap();
+ assert!(status_on(&host, id.as_str()).is_err());
+}
diff --git a/crates/openhuman-core/src/user_agents/schemas_tests.rs b/crates/openhuman-core/src/user_agents/schemas_tests.rs
new file mode 100644
index 0000000000..1b90a9ad4d
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/schemas_tests.rs
@@ -0,0 +1,41 @@
+use super::*;
+
+#[test]
+fn every_controller_has_a_schema_and_a_handler() {
+ let schemas = all_user_agents_controller_schemas();
+ let controllers = all_user_agents_registered_controllers();
+ assert_eq!(schemas.len(), FUNCTIONS.len());
+ assert_eq!(controllers.len(), FUNCTIONS.len());
+ for (schema, controller) in schemas.iter().zip(&controllers) {
+ assert_eq!(schema.namespace, "user_agents");
+ assert_eq!(schema.function, controller.schema.function);
+ assert_ne!(schema.function, "unknown");
+ }
+}
+
+#[test]
+fn the_operator_plane_is_its_own_domain_family() {
+ use crate::core::all::DomainGroup;
+ use crate::core::runtime::DomainSet;
+ assert!(DomainSet::saas().allows(DomainGroup::Operator));
+ for preset in [
+ DomainSet::full(),
+ DomainSet::harness(),
+ DomainSet::embedded(),
+ DomainSet::kernel(),
+ DomainSet::none(),
+ ] {
+ assert!(
+ !preset.allows(DomainGroup::Operator),
+ "a single-user preset never serves the operator plane"
+ );
+ }
+}
+
+#[tokio::test]
+async fn outside_saas_the_controllers_refuse() {
+ let controllers = all_user_agents_registered_controllers();
+ let list = controllers.iter().find(|c| c.schema.function == "list").unwrap();
+ let err = (list.handler)(Default::default()).await.unwrap_err();
+ assert!(err.contains("SaaS"), "{err}");
+}
diff --git a/crates/openhuman-core/src/user_agents/types_tests.rs b/crates/openhuman-core/src/user_agents/types_tests.rs
new file mode 100644
index 0000000000..e466a68eed
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/types_tests.rs
@@ -0,0 +1,51 @@
+use super::*;
+
+#[test]
+fn a_user_maps_to_one_stable_agent() {
+ let a = UserAgentId::for_user("user-123").unwrap();
+ assert_eq!(a, UserAgentId::for_user("user-123").unwrap());
+ assert_ne!(a, UserAgentId::for_user("user-124").unwrap());
+}
+
+#[test]
+fn the_id_fits_the_agent_charset_and_hides_the_user_id() {
+ let id = UserAgentId::for_user("[email protected]").unwrap();
+ let raw = id.as_str();
+ assert!(raw.len() <= 64);
+ assert!(raw
+ .bytes()
+ .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-' || b == b'_'));
+ assert!(!raw.contains("alice"));
+}
+
+#[test]
+fn bad_user_ids_are_refused() {
+ assert!(UserAgentId::for_user("").is_err());
+ assert!(UserAgentId::for_user(&"x".repeat(MAX_USER_ID_LEN + 1)).is_err());
+ assert!(UserAgentId::for_user("a\nb").is_err());
+ assert!(UserAgentId::for_user(&"x".repeat(MAX_USER_ID_LEN)).is_ok());
+}
+
+#[test]
+fn only_derived_ids_parse() {
+ let id = UserAgentId::for_user("u").unwrap();
+ assert_eq!(UserAgentId::parse(id.as_str()).unwrap(), id);
+ for bad in [
+ "orchestrator",
+ "u-",
+ "u-XYZ",
+ "u-../../etc",
+ "u-0123456789abcdef0123456789abcdeg",
+ "u-0123456789abcdef0123456789abcdef0",
+ ] {
+ assert!(UserAgentId::parse(bad).is_err(), "{bad}");
+ }
+}
+
+#[test]
+fn serde_round_trips_and_validates() {
+ let id = UserAgentId::for_user("u").unwrap();
+ let json = serde_json::to_string(&id).unwrap();
+ assert_eq!(serde_json::from_str::<UserAgentId>(&json).unwrap(), id);
+ assert!(serde_json::from_str::<UserAgentId>("\"orchestrator\"").is_err());
+}