Skip to content

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

test(storage): cover stale import context against reopened profile fence

Steven Enamakel4b3e6e356e96
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index f392b03e1b..bc93ec9501 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -19,10 +19,10 @@ use tinystoragedrivers_types::WriteOptions;
/// Storage and profile admission captured from the import caller's context.
/// Keeping the profile alive prevents eviction until all writes using the
/// captured lease fence have completed.
-struct CapturedModuleStorage {
- backend: Arc<dyn crate::storage::StorageBackend>,
- scope: crate::storage::Scope,
- _profile: Option<Arc<crate::profiles::Profile>>,
+pub(super) struct CapturedModuleStorage {
+ pub(super) backend: Arc<dyn crate::storage::StorageBackend>,
+ pub(super) scope: crate::storage::Scope,
+ pub(super) _profile: Option<Arc<crate::profiles::Profile>>,
}
fn capture_module_storage(
@@ -61,7 +61,7 @@ fn capture_module_storage(
)?))
}
-fn capture_profile_module_storage(
+pub(super) fn capture_profile_module_storage(
host: &crate::profiles::ProfileHost,
id: &crate::profiles::ProfileId,
profile_id: &str,
diff --git a/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs b/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
index 9ca342a464..eddf785afe 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
@@ -288,3 +288,57 @@ fn a_context_from_before_profile_takeover_cannot_import_under_the_new_lease() {
.unwrap_err();
assert!(error.to_string().contains("currently admitted context"));
}
+
+#[tokio::test]
+async fn an_import_cannot_recapture_a_reopened_profiles_new_fence() {
+ use crate::core::runtime::{CoreContext, DomainSet, SaasConfig};
+ use crate::profiles::{ProfileHost, ProfileId, ProfileIdMode};
+
+ let root = tempfile::tempdir().unwrap();
+ let fixture = crate::storage::test_support::sqlite_backend().unwrap();
+ let backend: Arc<dyn StorageBackend> = Arc::clone(&fixture.backend);
+ let id = ProfileId::for_user("import-takeover", ProfileIdMode::Raw).unwrap();
+ let host = ProfileHost::with_backend(
+ SaasConfig::new(root.path()),
+ CoreContext::for_test(DomainSet::full(), None),
+ Some(Arc::clone(&backend)),
+ )
+ .unwrap()
+ .with_fences(Arc::new(crate::storage::fence::FenceRegistry::new(
+ crate::profiles::lease::now_ms,
+ )));
+ host.provision(&id).await.unwrap();
+
+ let old_profile = host.open(&id).await.unwrap();
+ let old_context = Arc::clone(old_profile.context());
+ drop(old_profile);
+ assert!(host.release(&id).await.unwrap());
+ let current_profile = host.open(&id).await.unwrap();
+ let current_context = Arc::clone(current_profile.context());
+ assert!(!Arc::ptr_eq(&old_context, &current_context));
+
+ let module_scope = crate::storage::scope_for_module(Some(id.as_str()), MODULE_ID).unwrap();
+ let stale = super::super::storage_import_support::capture_profile_module_storage(
+ &host,
+ &id,
+ id.as_str(),
+ &old_context,
+ module_scope.clone(),
+ );
+ assert!(
+ stale.is_err(),
+ "old dispatch context cannot borrow the new fence"
+ );
+
+ let captured = super::super::storage_import_support::capture_profile_module_storage(
+ &host,
+ &id,
+ id.as_str(),
+ &current_context,
+ module_scope,
+ )
+ .unwrap();
+ // The capture retains the profile object as long as its claim/payload
+ // closure owns it. Its backend is the exact context's fence wrapper.
+ let _scoped = captured.backend.for_scope(&captured.scope).unwrap();
+}