Skip to content

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

feat(storage): add blob sweep for orphaned storage objects

Steven Enamakela9a734193a77
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index 87ba3fb89c..fe2d145a7c 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -346,25 +346,19 @@ pub(crate) fn expiring_blob_module_scopes(
}
}
-/// [`expiring_blob_module_scopes`] for SaaS profile `profile`, whose acting
-/// scope is `base`, from outside that profile's task scope.
+/// [`expiring_blob_module_scopes`] for SaaS profile `profile`, from outside
+/// that profile's task scope.
#[cfg(feature = "modules")]
-pub(crate) fn expiring_blob_module_scopes_for(
- profile: &str,
- base: &crate::storage::Scope,
-) -> Vec<crate::storage::Scope> {
+pub(crate) fn expiring_blob_module_scopes_for(profile: &str) -> Vec<crate::storage::Scope> {
[storage_grant_schemas::tinyjuice::MODULE_ID]
.into_iter()
- .filter_map(|id| storage_host::module_scope(Some(profile), base, id).ok())
+ .filter_map(|id| storage_host::module_scope(Some(profile), id).ok())
.collect()
}
/// See the `modules` build: with the feature off no module writes blobs.
#[cfg(not(feature = "modules"))]
-pub(crate) fn expiring_blob_module_scopes_for(
- _profile: &str,
- _base: &crate::storage::Scope,
-) -> Vec<crate::storage::Scope> {
+pub(crate) fn expiring_blob_module_scopes_for(_profile: &str) -> Vec<crate::storage::Scope> {
Vec::new()
}
diff --git a/crates/openhuman-core/src/modules/storage_host.rs b/crates/openhuman-core/src/modules/storage_host.rs
index 622d120643..75ea6709b9 100644
--- a/crates/openhuman-core/src/modules/storage_host.rs
+++ b/crates/openhuman-core/src/modules/storage_host.rs
@@ -12,8 +12,8 @@
//! is refused.
//! * **Which scope.** Never read from the request, which has no such field
//! (the wire types reject unknown fields). The scope is derived from the
-//! module id and the host's current scope ([`crate::storage::scope_from`]):
-//! `module:<id>@<base>`, so two modules never share a scope and a module can
+//! module id and the host's current tenant ([`crate::storage::scope_for_module`]):
+//! `<tenant>/module:<id>`, so two modules never share a scope and a module can
//! never name another tenant's. In SaaS mode a call with no profile is
//! refused like any other storage use.
//! * **What it may touch.** The module's [`StorageGrant`], declared by the
@@ -149,46 +149,17 @@ fn failure(kind: StorageKind) -> HostError {
HostError::Storage(StorageFailure { kind })
}
-/// A module's scope under the host's current tenant.
+/// A module's scope under the host's current tenant: `<tenant>/module:<id>`
+/// ([`crate::storage::scope_for_module`]), `local/module:<id>` on the desktop
+/// and `u:<profile>/module:<id>` for a SaaS profile.
///
-/// * For a profile (SaaS): `<profile>~module:<id>`, with the profile escaped
-/// as a session key is. That sits under the profile's session-key prefix,
-/// so the profile's lease fence ([`crate::profiles`]) guards a module's
-/// writes exactly as it guards the profile's transcripts. Too long to be a
-/// scope is an error, never a hash, because a hash would fall outside the
-/// fence.
-/// * Otherwise: `module:<id>@<base>`, or a hash of both when that is not a
-/// valid scope or the id could make the form ambiguous.
-///
-/// Either way two modules never share a scope, and nothing the module sends
-/// takes part.
-pub(crate) fn module_scope(
- profile: Option<&str>,
- base: &Scope,
- module_id: &str,
-) -> Result<Scope, StorageError> {
- let plain = !module_id.is_empty()
- && module_id
- .bytes()
- .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_');
- if let Some(profile) = profile {
- if !plain {
- return Err(StorageError::invalid_input("module id is not a plain name"));
- }
- let prefix = crate::core::runtime::session_key_prefix(profile);
- return Scope::new(format!("{prefix}module:{module_id}"));
- }
- if plain {
- if let Ok(scope) = Scope::new(format!("module:{module_id}@{}", base.as_str())) {
- return Ok(scope);
- }
- }
- use sha2::Digest;
- let mut digest = sha2::Sha256::new();
- digest.update(module_id.as_bytes());
- digest.update([0]);
- digest.update(base.as_str().as_bytes());
- Scope::new(format!("module-sha256:{}", hex::encode(digest.finalize())))
+/// The module scope sits under its tenant, so the tenant's lease fence
+/// ([`crate::profiles`]) guards a module's writes exactly as it guards the
+/// profile's transcripts, and a tenant purge reaches it. Two modules never
+/// share a scope, and nothing the module sends takes part. An id that is not
+/// a valid segment is hashed by the scope grammar itself, inside the tenant.
+pub(crate) fn module_scope(profile: Option<&str>, module_id: &str) -> Result<Scope, StorageError> {
+ crate::storage::scope_for_module(profile, module_id)
}
/// A module bound to a scope: its grant and the server that answers it.
@@ -538,9 +509,10 @@ pub(crate) fn current_module_scope(
// A module call arrives on the module bus, outside any turn, so with no
// tenant the host's default scope applies (refused in SaaS mode).
let tenant = ctx.tenant().unwrap_or_default();
- let base =
- crate::storage::scope_from(tenant.profile.as_deref(), tenant.agent.as_deref(), saas)?;
- module_scope(tenant.profile.as_deref(), &base, module_id)
+ // Resolves the acting scope for its SaaS refusal; the module scope itself
+ // depends only on the profile.
+ crate::storage::scope_from(tenant.profile.as_deref(), tenant.agent.as_deref(), saas)?;
+ module_scope(tenant.profile.as_deref(), module_id)
}
/// Production wiring: the broker's name owners, the installed backend, the
diff --git a/crates/openhuman-core/src/modules/storage_host_tests.rs b/crates/openhuman-core/src/modules/storage_host_tests.rs
index 1ed602aea9..b4bcc5eada 100644
--- a/crates/openhuman-core/src/modules/storage_host_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_host_tests.rs
@@ -34,11 +34,7 @@ impl Environment for Env {
fn module_scope(&self, module_id: &str) -> Result<Scope, StorageError> {
let profile = self.profile.lock().unwrap().clone();
- let base = match &profile {
- Some(profile) => crate::storage::scope_for_profile(profile),
- None => Scope::local(),
- };
- module_scope(profile.as_deref(), &base, module_id)
+ module_scope(profile.as_deref(), module_id)
}
fn grant(&self, module_id: &str) -> Option<StorageGrant> {
@@ -466,7 +462,7 @@ async fn a_collection_listing_shows_only_the_modules_own_names() {
.await
.unwrap();
// The host keeps something else in the module's scope.
- let scope = module_scope(None, &Scope::local(), "alpha").unwrap();
+ let scope = module_scope(None, "alpha").unwrap();
let storage = rig.backend.for_scope(&scope).unwrap();
storage
.documents()
diff --git a/crates/openhuman-core/src/storage/blob_sweep.rs b/crates/openhuman-core/src/storage/blob_sweep.rs
index 8b94109376..fcfe87e762 100644
--- a/crates/openhuman-core/src/storage/blob_sweep.rs
+++ b/crates/openhuman-core/src/storage/blob_sweep.rs
@@ -76,14 +76,14 @@ pub(crate) async fn sweep_scope_within(
}
/// Reclaims the lapsed blobs of each SaaS profile in `profile_ids`: the
-/// profile's own scope (`profile:<id>`) and the scopes the blob-owning modules
+/// profile's own scope (`u:<id>`) and the scopes the blob-owning modules
/// keep for it. Returns the total reclaimed.
pub(crate) async fn sweep_profiles(backend: &dyn StorageBackend, profile_ids: &[String]) -> u64 {
let mut removed = 0;
for id in profile_ids {
let scope = super::scope_for_profile(id);
removed += sweep_scope(backend, &scope).await;
- for module_scope in crate::modules::expiring_blob_module_scopes_for(id, &scope) {
+ for module_scope in crate::modules::expiring_blob_module_scopes_for(id) {
if module_scope != scope {
removed += sweep_scope(backend, &module_scope).await;
}
diff --git a/crates/openhuman-core/src/storage/blob_sweep_tests.rs b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
index 8bf1fbe0c7..b654fdd5b3 100644
--- a/crates/openhuman-core/src/storage/blob_sweep_tests.rs
+++ b/crates/openhuman-core/src/storage/blob_sweep_tests.rs
@@ -137,7 +137,7 @@ async fn sweep_profiles_skips_profiles_not_listed() {
async fn sweep_profiles_reaches_the_modules_scopes_for_that_profile() {
let (now, backend) = backend_with_clock();
let base = crate::storage::scope_for_profile("acme");
- let module_scopes = crate::modules::expiring_blob_module_scopes_for("acme", &base);
+ let module_scopes = crate::modules::expiring_blob_module_scopes_for("acme");
assert!(!module_scopes.is_empty(), "TinyJuice owns expiring blobs");
put_expiring(&backend, &module_scopes[0], "tinyjuice_ccr/t", 10).await;
now.store(2_000, Ordering::SeqCst);