Skip to content

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

fix(storage): capture profile storage fence during module import

Steven Enamakel4a2200919f33
diff --git a/crates/openhuman-core/src/modules/storage_import_support.rs b/crates/openhuman-core/src/modules/storage_import_support.rs
index 157b4657ef..2274ecfc4e 100644
--- a/crates/openhuman-core/src/modules/storage_import_support.rs
+++ b/crates/openhuman-core/src/modules/storage_import_support.rs
@@ -16,19 +16,73 @@ use sha2::Digest;
use tinystoragedrivers::ErrorKind;
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>>,
+}
+
+fn capture_module_storage(
+ ctx: &crate::core::runtime::DispatchCtx,
+ module_id: &str,
+) -> Result<Option<CapturedModuleStorage>> {
+ let Some(installed) = crate::storage::installed() else {
+ return Ok(None);
+ };
+ let scope = super::storage_host::current_module_scope(ctx, module_id)
+ .map_err(|error| anyhow!("resolve the module scope: {:?}", error.kind()))?;
+ let tenant = ctx
+ .tenant()
+ .map_err(|error| anyhow!("resolve the import caller's tenant: {error}"))?;
+ let Some(profile_id) = tenant.profile else {
+ return Ok(Some(CapturedModuleStorage {
+ backend: installed,
+ scope,
+ _profile: None,
+ }));
+ };
+
+ let caller_context = ctx
+ .resolved()
+ .ok_or_else(|| anyhow!("profile import has no admitted caller context"))?;
+ let host = crate::profiles::host()
+ .ok_or_else(|| anyhow!("profile import has no active profile host"))?;
+ let id = crate::profiles::ProfileId::parse(&profile_id)
+ .map_err(|error| anyhow!("invalid caller profile id: {error}"))?;
+ 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}"
+ ));
+ }
+ let backend = host
+ .storage_backend_for(&profile)
+ .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,
+ scope,
+ _profile: Some(profile),
+ }))
+}
+
/// 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(
ctx: &crate::core::runtime::DispatchCtx,
module_id: &str,
) -> Result<Option<Storage>> {
- let Some(backend) = crate::storage::installed() else {
+ let Some(captured) = capture_module_storage(ctx, module_id)? else {
return Ok(None);
};
- let scope = super::storage_host::current_module_scope(ctx, module_id)
- .map_err(|error| anyhow!("resolve the module scope: {:?}", error.kind()))?;
- let scoped = backend
- .for_scope(&scope)
+ let scoped = captured
+ .backend
+ .for_scope(&captured.scope)
.map_err(|error| anyhow!("open the module scope: {:?}", error.kind()))?;
Ok(Some(scoped))
}
@@ -47,11 +101,14 @@ pub(super) fn with_module_claim<T>(
where
T: Send + 'static,
{
- let Some(backend) = crate::storage::installed() else {
+ let Some(captured) = capture_module_storage(ctx, module_id)? else {
return Ok(None);
};
- let scope = super::storage_host::current_module_scope(ctx, module_id)
- .map_err(|error| anyhow!("resolve the module scope: {:?}", error.kind()))?;
+ let CapturedModuleStorage {
+ backend,
+ scope,
+ _profile: profile,
+ } = captured;
let repo = crate::storage::documents::Repo::on(
&backend,
&scope,
@@ -65,6 +122,10 @@ where
crate::storage::import_state::HOST_OWNER,
&crate::storage::import_state::step_id(marker_kind, marker_id),
move |condition| {
+ // Keep the admitted profile context alive across claim, payload,
+ // and completion writes; the backend itself holds its exact
+ // captured fence and never performs a later registry lookup.
+ let _profile = profile;
let scoped_backend = crate::storage::import_state::backend_with_condition(
claim_backend,
condition.clone(),