Skip to content

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

feat(storage): add storage grant schemas module

Steven Enamakelcbf07b2c130c
diff --git a/crates/openhuman-core/src/modules/mod.rs b/crates/openhuman-core/src/modules/mod.rs
index fe2d145a7c..27cd8ffd72 100644
--- a/crates/openhuman-core/src/modules/mod.rs
+++ b/crates/openhuman-core/src/modules/mod.rs
@@ -167,7 +167,11 @@ pub async fn import_legacy_mcp(
) {
#[cfg(feature = "modules")]
match storage_import_tinymcp::import_legacy(ctx, data_dir).await {
- Ok(outcome) => log::debug!("[mcp] legacy storage import: {outcome:?}"),
+ Ok(outcome) => {
+ log::debug!("[mcp] legacy storage import: {outcome:?}");
+ storage_grant_schemas::migrate_imported(ctx, storage_grant_schemas::tinymcp::MODULE_ID)
+ .await;
+ }
Err(error) => log::warn!("[mcp] legacy storage import failed, will retry: {error:#}"),
}
#[cfg(not(feature = "modules"))]
@@ -248,6 +252,7 @@ where
Ok(outcome) => {
log::debug!("[{kind}] legacy storage import: {outcome:?}");
if outcome.settled() {
+ storage_grant_schemas::migrate_imported(ctx, module_id).await;
done.insert(key);
}
true
diff --git a/crates/openhuman-core/src/modules/storage_grant_schemas.rs b/crates/openhuman-core/src/modules/storage_grant_schemas.rs
index 2cebd1e554..d43a784631 100644
--- a/crates/openhuman-core/src/modules/storage_grant_schemas.rs
+++ b/crates/openhuman-core/src/modules/storage_grant_schemas.rs
@@ -62,6 +62,38 @@ const ROWS: [Row; 6] = [
},
];
+/// The schema owner whose declarations hold `module_id`'s storage.
+pub(super) fn owner_of(module_id: &str) -> Option<&'static str> {
+ ROWS.iter()
+ .find(|row| row.module == module_id)
+ .map(|row| row.owner)
+}
+
+/// Carries what a legacy-file importer just wrote into `module_id`'s scope
+/// (v1 names and shapes) forward to the owner's v2 schema.
+pub(super) async fn migrate_imported(
+ ctx: &crate::core::runtime::DispatchCtx,
+ module_id: &str,
+) {
+ let (Some(owner), Some(backend)) = (owner_of(module_id), crate::storage::installed()) else {
+ return;
+ };
+ let scope = match super::storage_host::current_module_scope(ctx, module_id) {
+ Ok(scope) => scope,
+ Err(error) => {
+ tracing::warn!(module = module_id, kind = ?error.kind(), "[storage_grant] no module scope to migrate");
+ return;
+ }
+ };
+ if let Err(error) = crate::storage::boot::remigrate_owner(&backend, &scope, owner).await {
+ tracing::warn!(
+ module = module_id,
+ %error,
+ "[storage_grant] could not migrate the imported legacy records; retried at the next import"
+ );
+ }
+}
+
/// The grant generated for `module_id`, or `None` for a module with no row.
pub(super) fn grant_for(module_id: &str) -> Option<StorageGrant> {
let row = ROWS.iter().find(|row| row.module == module_id)?;
diff --git a/crates/openhuman-core/src/storage/boot.rs b/crates/openhuman-core/src/storage/boot.rs
index 2162938cdd..970125a1f2 100644
--- a/crates/openhuman-core/src/storage/boot.rs
+++ b/crates/openhuman-core/src/storage/boot.rs
@@ -73,6 +73,55 @@ pub async fn prepare_and_migrate(
Ok(report)
}
+/// Runs `owner`'s steps again over `scope` after a legacy-file importer wrote
+/// v1-shaped records there. The steps finished at boot (or an earlier import),
+/// and a finished step is skipped, so the owner's progress records are cleared
+/// first; the steps are pure and write absent-only, so re-running over the
+/// already migrated records changes nothing. Returns the steps that ran.
+///
+/// # Errors
+///
+/// The first storage error. The importer's records stay in their v1 form and
+/// the next call retries.
+pub async fn remigrate_owner(
+ backend: &Arc<dyn StorageBackend>,
+ scope: &Scope,
+ owner: &str,
+) -> Result<usize, StorageError> {
+ use tinystoragedrivers::{DocumentStoreExt, Query};
+ let registry = super::schema::registry()?;
+ let scoped = backend.for_scope(scope)?;
+ let docs = scoped.documents();
+ docs.ensure_collection(&tinystoragedrivers::CollectionSpec::new(
+ tinystoragedrivers::MIGRATIONS_COLLECTION,
+ ))
+ .await?;
+ for progress in docs
+ .query_all(tinystoragedrivers::MIGRATIONS_COLLECTION, &Query::all())
+ .await?
+ {
+ if progress.doc.get("owner").and_then(|v| v.as_str()) == Some(owner)
+ && progress.doc.get("to").and_then(|v| v.as_u64()).unwrap_or(0) > 0
+ {
+ docs.delete(
+ tinystoragedrivers::MIGRATIONS_COLLECTION,
+ &progress.id,
+ tinystoragedrivers::Precondition::None,
+ )
+ .await?;
+ }
+ }
+ let report = registry.migrate(&scoped, &MigrateOptions::new()).await?;
+ tracing::info!(
+ target: "openhuman::storage",
+ scope = scope.as_str(),
+ owner,
+ steps = report.ran.len(),
+ "[storage][boot] re-ran an owner's steps after a legacy import"
+ );
+ Ok(report.ran.len())
+}
+
/// `local`, plus every v2 scope the backend reports (when it can enumerate).
async fn scopes_to_migrate(backend: &Arc<dyn StorageBackend>) -> Vec<Scope> {
let mut scopes = vec![Scope::local()];