Skip to content

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

test(profiles): cover fence replacement across roots and dead anchors

Steven Enamakel56b378d74cf2
diff --git a/crates/openhuman-core/src/profiles/host_tests.rs b/crates/openhuman-core/src/profiles/host_tests.rs
index bea08f574a..d92179544c 100644
--- a/crates/openhuman-core/src/profiles/host_tests.rs
+++ b/crates/openhuman-core/src/profiles/host_tests.rs
@@ -508,7 +508,7 @@ async fn opening_an_existing_profile_adopts_the_enforced_cron_cap() {
}
#[tokio::test]
-async fn opening_replaces_a_dropped_hosts_fence_before_cron_reconciliation() {
+async fn provisioning_with_a_new_root_replaces_a_dead_higher_epoch_fence() {
let first_root = tempfile::tempdir().unwrap();
let second_root = tempfile::tempdir().unwrap();
let id = profile(&format!("cron-fence-{}", uuid::Uuid::new_v4().simple()));
@@ -540,15 +540,51 @@ async fn opening_replaces_a_dropped_hosts_fence_before_cron_reconciliation() {
second_saas.cron.max_active_jobs = 1;
let second = ProfileHost::new(second_saas, CoreContext::for_test(DomainSet::full(), None));
let provisioned = second.provision(&id).await;
- if let Err(error) = &provisioned {
- eprintln!("new-root provision error: {error}");
- }
assert!(
provisioned.unwrap(),
"the new root must be able to register its own profile fence"
);
}
+#[tokio::test]
+async fn opening_replaces_a_dropped_hosts_fence_before_cron_reconciliation() {
+ let tmp = tempfile::tempdir().unwrap();
+ let id = profile(&format!("cron-fence-{}", uuid::Uuid::new_v4().simple()));
+ 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 mut first_saas = SaasConfig::new(tmp.path());
+ first_saas.cron.agent_jobs_only = true;
+ first_saas.cron.max_active_jobs = 1;
+ let first = ProfileHost::new(first_saas, CoreContext::for_test(DomainSet::full(), None));
+ first.provision(&id).await.unwrap();
+ let opened = first.open(&id).await.unwrap();
+ let old_epoch = opened.lease_fence().epoch();
+ let _retire_stale_fence = RetireProfileFence {
+ registry: Arc::clone(&registry),
+ profile_id: id.as_str().to_string(),
+ epoch: old_epoch,
+ };
+ drop(opened);
+ drop(first);
+
+ let mut second_saas = SaasConfig::new(tmp.path());
+ second_saas.cron.agent_jobs_only = true;
+ second_saas.cron.max_active_jobs = 1;
+ let second = ProfileHost::new(second_saas, CoreContext::for_test(DomainSet::full(), None));
+ assert!(!second.provision(&id).await.unwrap());
+ let reopened = second.open(&id).await;
+ assert!(
+ reopened.is_ok(),
+ "the later lease in this root must replace the dropped host's profile fence"
+ );
+}
+
#[tokio::test]
async fn disabling_profile_cron_limits_clears_the_persisted_store_cap() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/storage/fence_tests.rs b/crates/openhuman-core/src/storage/fence_tests.rs
index a154f3c57d..9d2f0e2bff 100644
--- a/crates/openhuman-core/src/storage/fence_tests.rs
+++ b/crates/openhuman-core/src/storage/fence_tests.rs
@@ -217,6 +217,65 @@ fn the_registry_replaces_retires_and_prunes_dead_tombstones() {
assert!(registry.lookup("profile:alice").is_none());
}
+#[test]
+fn registration_prunes_a_dead_anchored_fence_before_comparing_epochs() {
+ let backend = MemoryStorage::new();
+ let store = leases(&backend, "a");
+ let registry = FenceRegistry::new(|| 0);
+ let make = |epoch| {
+ Arc::new(LeaseFence::new(
+ Arc::clone(&store),
+ "alice",
+ "a",
+ epoch,
+ u64::MAX,
+ 0,
+ scopes(),
+ ))
+ };
+ let dead_anchor_fence = make(2);
+ let dead_anchor: Arc<dyn Any + Send + Sync> = Arc::new(());
+ dead_anchor_fence.anchor_to(Arc::downgrade(&dead_anchor));
+ registry.register(dead_anchor_fence);
+ drop(dead_anchor);
+
+ let lower_epoch = make(1);
+ registry.register(Arc::clone(&lower_epoch));
+ assert_eq!(registry.lookup("profile:alice").unwrap().epoch(), 1);
+}
+
+#[test]
+fn registration_preserves_a_live_or_unanchored_higher_epoch_fence() {
+ let backend = MemoryStorage::new();
+ let store = leases(&backend, "a");
+ let registry = FenceRegistry::new(|| 0);
+ let make = |epoch| {
+ Arc::new(LeaseFence::new(
+ Arc::clone(&store),
+ "alice",
+ "a",
+ epoch,
+ u64::MAX,
+ 0,
+ scopes(),
+ ))
+ };
+ let higher = make(2);
+ let live_anchor: Arc<dyn Any + Send + Sync> = Arc::new(());
+ higher.anchor_to(Arc::downgrade(&live_anchor));
+ registry.register(Arc::clone(&higher));
+ registry.register(make(1));
+ assert_eq!(registry.lookup("profile:alice").unwrap().epoch(), 2);
+
+ // No anchor means ownership has not been tied to a disposable host yet;
+ // it must remain protected from a lower-epoch registration.
+ registry.retire("alice", 2);
+ let unanchored = make(3);
+ registry.register(Arc::clone(&unanchored));
+ registry.register(make(1));
+ assert_eq!(registry.lookup("profile:alice").unwrap().epoch(), 3);
+}
+
#[tokio::test]
async fn the_guard_passes_unfenced_scopes_and_refuses_with_a_typed_error() {
let backend = MemoryStorage::new();