Skip to content

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

refactor(storage): extract profile context check into shared helper

Steven Enamakel84c1d1735a18
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index 2274ecfc4e..adffe3da41 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -55,14 +55,15 @@ fn capture_module_storage(
let profile = host
.get(&id)
.ok_or_else(|| anyhow!("caller profile is no longer open"))?;
- if !Arc::ptr_eq(profile.context(), &caller_context) {
- return Err(anyhow!(
- "caller context is not the currently admitted context for profile {profile_id}"
- ));
- }
+ require_profile_context(profile.context(), &caller_context, &profile_id)?;
let backend = host
.storage_backend_for(&profile)
- .map_err(|error| anyhow!("capture the caller profile's storage fence: {:?}", error.kind()))?
+ .map_err(|error| {
+ anyhow!(
+ "capture the caller profile's storage fence: {:?}",
+ error.kind()
+ )
+ })?
.ok_or_else(|| anyhow!("profile storage is unavailable for this import"))?;
Ok(Some(CapturedModuleStorage {
backend,
@@ -71,6 +72,23 @@ fn capture_module_storage(
}))
}
+/// Rejects work from an old context after a profile has been reopened under a
+/// different lease. Callers must capture the backend from this same profile
+/// before starting a claim, then retain the profile for the whole operation.
+pub(super) fn require_profile_context(
+ admitted: &Arc<crate::core::runtime::CoreContext>,
+ caller: &Arc<crate::core::runtime::CoreContext>,
+ profile_id: &str,
+) -> Result<()> {
+ if Arc::ptr_eq(admitted, caller) {
+ Ok(())
+ } else {
+ Err(anyhow!(
+ "caller context is not the currently admitted context for profile {profile_id}"
+ ))
+ }
+}
+
/// The module's scope of the installed backend, or `None` when the process
/// runs without a storage backend (the module is then not using this storage).
pub(super) fn module_scope(
diff --git a/crates/openhuman-core/src/modules/storage_import_tinymcp.rs b/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
index 1f6f1335e9..c7ed69abe3 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinymcp.rs
@@ -29,9 +29,9 @@
use std::path::{Path, PathBuf};
use std::sync::Arc;
-use crate::storage::{CollectionSpec, DocumentStore, Precondition, Storage};
#[cfg(test)]
use crate::storage::StorageBackend;
+use crate::storage::{CollectionSpec, DocumentStore, Precondition, Storage};
use anyhow::{Context, Result, anyhow};
use serde_json::{Value, json};
use sha2::Digest;
@@ -122,14 +122,10 @@ pub(crate) async fn import_legacy(ctx: &DispatchCtx, data_dir: &Path) -> Result<
})
.await
.context("the legacy reader panicked")??;
- let outcome = import_payload(&scoped, &scope, legacy_servers, legacy_audit, None)
+ let outcome =
+ import_payload(&scoped, &scope, legacy_servers, legacy_audit, None).await?;
+ super::storage_import_support::mark_imported(&scoped, MARKER_COLLECTION, &marker)
.await?;
- super::storage_import_support::mark_imported(
- &scoped,
- MARKER_COLLECTION,
- &marker,
- )
- .await?;
Ok(outcome)
})
},
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 28780328c6..9ca342a464 100644
--- a/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_import_tinymcp_tests.rs
@@ -265,3 +265,26 @@ async fn an_unreadable_file_writes_no_marker_so_the_next_start_retries() {
.unwrap();
assert!(!done);
}
+
+#[test]
+fn a_context_from_before_profile_takeover_cannot_import_under_the_new_lease() {
+ use crate::core::runtime::{CoreContext, DomainSet};
+
+ let old_context = CoreContext::for_test(DomainSet::full(), None);
+ let reopened_context = CoreContext::for_test(DomainSet::full(), None);
+ assert!(
+ super::super::storage_import_support::require_profile_context(
+ &reopened_context,
+ &reopened_context,
+ "profile-1"
+ )
+ .is_ok()
+ );
+ let error = super::super::storage_import_support::require_profile_context(
+ &reopened_context,
+ &old_context,
+ "profile-1",
+ )
+ .unwrap_err();
+ assert!(error.to_string().contains("currently admitted context"));
+}