Skip to content

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

test(user_agents): cover recovery on first open and empty workspace

Steven Enamakelea5919ad7bf3
diff --git a/crates/openhuman-core/src/user_agents/background_tests.rs b/crates/openhuman-core/src/user_agents/background_tests.rs
new file mode 100644
index 0000000000..28c783378e
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/background_tests.rs
@@ -0,0 +1,62 @@
+use super::*;
+use crate::core::runtime::{CoreContext, DomainSet, SaasConfig};
+use crate::memory::lifecycle::jobs::{enqueue, BackgroundJob};
+use crate::user_agents::layout::agent_config;
+use crate::user_agents::UserAgentId;
+use tinymemory_api::{ConsolidateRequest, ItemKind, Namespace, Reach};
+
+fn host(tmp: &tempfile::TempDir, idle_secs: u64) -> AgentHost {
+ let mut saas = SaasConfig::new(tmp.path());
+ saas.idle_evict_secs = idle_secs;
+ AgentHost::new(saas, CoreContext::for_test(DomainSet::full(), None))
+}
+
+fn build_job() -> BackgroundJob {
+ BackgroundJob::BuildBeliefs {
+ request: ConsolidateRequest::new(Reach::exact(Namespace::agent("a")))
+ .kinds([ItemKind::Conversation]),
+ }
+}
+
+#[tokio::test]
+async fn nothing_to_do_does_nothing() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 3600);
+ assert_eq!(tick(&host).await, TickReport::default());
+ let id = UserAgentId::for_user(&format!("idle-{}", uuid::Uuid::new_v4())).unwrap();
+ host.provision(&id).unwrap();
+ assert_eq!(tick(&host).await, TickReport::default());
+ assert!(!host.is_open(&id), "an agent with no queued work is not opened");
+}
+
+#[tokio::test]
+async fn only_agents_with_queued_memory_jobs_are_run() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 3600);
+ let busy = UserAgentId::for_user(&format!("busy-{}", uuid::Uuid::new_v4())).unwrap();
+ let quiet = UserAgentId::for_user(&format!("quiet-{}", uuid::Uuid::new_v4())).unwrap();
+ host.provision(&busy).unwrap();
+ host.provision(&quiet).unwrap();
+
+ let config = agent_config(&host.layout_of(&busy), &busy);
+ crate::memory::test_fixtures::bind_reference(&config);
+ enqueue(&config, &Namespace::ROOT, vec![build_job()]).await;
+ assert!(jobs::has_pending(&config.workspace_dir));
+
+ let report = tick(&host).await;
+ assert_eq!(report.ran, 1, "{report:?}");
+ assert!(host.is_open(&busy), "the busy agent was opened to run its jobs");
+ assert!(!host.is_open(&quiet), "the quiet agent was left closed");
+}
+
+#[tokio::test]
+async fn a_tick_sweeps_idle_agents() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 0);
+ let id = UserAgentId::for_user(&format!("sweep-{}", uuid::Uuid::new_v4())).unwrap();
+ host.provision(&id).unwrap();
+ drop(host.open(&id).unwrap());
+ assert!(host.is_open(&id));
+ tick(&host).await;
+ assert!(!host.is_open(&id));
+}
diff --git a/crates/openhuman-core/src/user_agents/host_tests.rs b/crates/openhuman-core/src/user_agents/host_tests.rs
index 183ac49454..804ae527f1 100644
--- a/crates/openhuman-core/src/user_agents/host_tests.rs
+++ b/crates/openhuman-core/src/user_agents/host_tests.rs
@@ -160,3 +160,27 @@ fn a_reprovisioned_agent_does_not_inherit_the_old_credential() {
"deprovisioning must forget the credential"
);
}
+
+#[test]
+fn the_first_open_settles_a_previous_process_once() {
+ let tmp = tempfile::tempdir().unwrap();
+ let host = host(&tmp, 4, 0);
+ let id = agent(&format!("recover-{}", uuid::Uuid::new_v4()));
+ host.provision(&id).unwrap();
+ drop(host.open(&id).unwrap());
+ assert!(host.recovered.lock().unwrap().contains(&id));
+ host.evict_idle();
+ drop(host.open(&id).unwrap());
+ assert_eq!(
+ host.recovered.lock().unwrap().len(),
+ 1,
+ "a re-open after eviction does not sweep again"
+ );
+}
+
+#[test]
+fn recovery_of_an_empty_workspace_is_a_no_op() {
+ let tmp = tempfile::tempdir().unwrap();
+ let id = agent("empty");
+ recover_workspace(&id, tmp.path());
+}