Skip to content

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

fix(storage): migrate graph and harness records the scope move copies in

M3gA-Mind57ee400adc17
diff --git a/crates/openhuman-core/src/storage/boot.rs b/crates/openhuman-core/src/storage/boot.rs
index 30b17c94bd..fcb4366f0f 100644
--- a/crates/openhuman-core/src/storage/boot.rs
+++ b/crates/openhuman-core/src/storage/boot.rs
@@ -92,9 +92,37 @@ pub async fn prepare_scope(storage: &Storage, scope: &Scope) -> Result<usize, St
/// key-value stores move to their v2 names. Idempotent.
async fn migrate_owners(storage: &Storage, scope: &Scope) -> Result<(), StorageError> {
let scoped = tinyagents_storage_kit::ScopedStorage::new(storage.clone(), scope.clone());
- tinyagents_graph::storage::open_storage(scoped.clone()).await?;
- tinyagents_harness::storage::open_storage(scoped).await?;
- Ok(())
+ // Each owner migrates independently: a graph failure must not hold back
+ // the harness records. The first error is returned after both ran.
+ let graph = tinyagents_graph::storage::open_storage(scoped.clone()).await;
+ let harness = tinyagents_harness::storage::open_storage(scoped).await;
+ graph.and(harness).map(|_| ())
+}
+
+/// Runs the graph and harness owner migrations on `scope` again after an
+/// importer copied v1 records into it (the scope move). Their steps already
+/// finished on the then-empty scope when it was prepared, and a finished step
+/// is skipped, so the two owners' progress records are cleared first. The
+/// steps write absent-only, so re-running over migrated records changes
+/// nothing.
+///
+/// # Errors
+///
+/// The first storage error; the cleared steps re-run on the next preparation.
+pub async fn remigrate_owners(storage: &Storage, scope: &Scope) -> Result<(), StorageError> {
+ use tinyagents_storage_kit::{DocumentStoreExt, MIGRATIONS_COLLECTION};
+ use tinystoragedrivers::{CollectionSpec, Precondition, Query};
+ let docs = storage.documents();
+ docs.ensure_collection(&CollectionSpec::new(MIGRATIONS_COLLECTION))
+ .await?;
+ for progress in docs.query_all(MIGRATIONS_COLLECTION, &Query::all()).await? {
+ let owner = progress.doc.get("owner").and_then(|value| value.as_str());
+ if matches!(owner, Some("graph" | "harness")) {
+ docs.delete(MIGRATIONS_COLLECTION, &progress.id, Precondition::None)
+ .await?;
+ }
+ }
+ migrate_owners(storage, scope).await
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/storage/boot_tests.rs b/crates/openhuman-core/src/storage/boot_tests.rs
index 8d04aa0503..f6f6220c36 100644
--- a/crates/openhuman-core/src/storage/boot_tests.rs
+++ b/crates/openhuman-core/src/storage/boot_tests.rs
@@ -125,3 +125,25 @@ async fn prepare_scope_migrates_v1_graph_and_harness_rows() {
assert_v1_rows_migrated(&storage).await;
}
+
+#[tokio::test]
+async fn remigrate_owners_carries_v1_rows_copied_after_preparation() {
+ use tinyagents_graph::checkpoint::{Checkpointer, DriverCheckpointer};
+ let backend = tinystoragedrivers_testkit::MemoryBackend::new();
+ let scope = super::super::scope::scope_for_profile("profile-b");
+ let storage = backend.for_scope(&scope).unwrap();
+ // The scope move prepares the target (the owner steps finish on an empty
+ // scope) and only then copies the v1 rows in.
+ prepare_scope(&storage, &scope).await.unwrap();
+ put_v1_rows(&storage).await;
+
+ // A finished step is skipped, so preparing again does not carry them.
+ prepare_scope(&storage, &scope).await.unwrap();
+ let checkpointer =
+ DriverCheckpointer::<i32>::with_namespace(Arc::clone(storage.documents()), "delegation");
+ assert!(checkpointer.get("t", None).await.unwrap().is_none());
+
+ remigrate_owners(&storage, &scope).await.unwrap();
+
+ assert_v1_rows_migrated(&storage).await;
+}
diff --git a/crates/openhuman-core/src/storage/scope_move_boot.rs b/crates/openhuman-core/src/storage/scope_move_boot.rs
index bf3bc46d61..cb2184c62b 100644
--- a/crates/openhuman-core/src/storage/scope_move_boot.rs
+++ b/crates/openhuman-core/src/storage/scope_move_boot.rs
@@ -202,6 +202,7 @@ fn import_inventory(
found += inventory_scope_counts(inventory, &source_scope);
}
Some(Some(scope_report)) => {
+ remigrate_copied_scope(&scope_backend, &target_scope);
let scope_found = inventory_scope_counts(inventory, &source_scope);
found += scope_found;
written += usize::try_from(
@@ -241,6 +242,28 @@ fn import_inventory(
}
}
+/// The copy wrote v1 graph and harness records into a scope whose owner
+/// migrations already ran when it was prepared; run them again so the v2
+/// readers see the records. Best effort: a failure is logged, and the cleared
+/// steps re-run on the next preparation of the scope.
+#[cfg(feature = "legacy-import")]
+fn remigrate_copied_scope(backend: &Arc<dyn StorageBackend>, scope: &Scope) {
+ let result = backend.for_scope(scope).and_then(|storage| {
+ let target = scope.to_storage_scope();
+ crate::storage::block_on(async move {
+ crate::storage::boot::remigrate_owners(&storage, &target).await
+ })
+ });
+ if let Err(error) = result {
+ tracing::warn!(
+ target: "openhuman::storage",
+ scope = scope.as_str(),
+ %error,
+ "[storage][scope_move] owner migration after the copy failed; retried when the scope is next prepared"
+ );
+ }
+}
+
#[cfg(feature = "legacy-import")]
type BackendForScope = (
Arc<dyn StorageBackend>,