Skip to content

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

Never erase legacy scopes whole after an unconfirmed import

M3gA-Mind0d23d1f5762e
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index aeb587f812..450a9258d9 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -242,6 +242,20 @@ struct ImportFile {
retrying: bool,
#[serde(default)]
checkpoint: Checkpoint,
+ /// The import finished without its last batch confirmed listed (the
+ /// final wait gave up). Items the engine accepted may not be listed yet,
+ /// so a migration must not erase legacy scopes whole
+ /// ([`listed_unconfirmed`]). A later import that confirms clears it.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ listed_unconfirmed: bool,
+}
+
+/// Whether the last import in `workspace_dir` finished with its last batch
+/// not yet confirmed listed: the layout migration then forgets moved items
+/// by id instead of erasing whole legacy scopes, which could take an item
+/// the listing does not show yet.
+pub(crate) fn listed_unconfirmed(workspace_dir: &Path) -> bool {
+ read_file(workspace_dir).listed_unconfirmed
}
fn file_path(workspace_dir: &Path) -> PathBuf {
@@ -661,6 +675,7 @@ async fn run(
// resume into the same wait again.
if failure.is_none() && !pausing && !last.is_empty() {
let settled = store_batch(bound, last, WaitFor::Visible).await;
+ file.listed_unconfirmed = settled.fatal.is_some();
if let Some(error) = settled.fatal {
tracing::warn!(
%error,
diff --git a/crates/openhuman-core/src/memory/import_wait_tests.rs b/crates/openhuman-core/src/memory/import_wait_tests.rs
index 5f9be42938..6ec5c3a717 100644
--- a/crates/openhuman-core/src/memory/import_wait_tests.rs
+++ b/crates/openhuman-core/src/memory/import_wait_tests.rs
@@ -94,6 +94,10 @@ async fn batches_are_stored_accepted_and_the_last_waited_for_once() {
],
"one accepted batch, then the visible wait for it"
);
+ assert!(
+ !listed_unconfirmed(&config.workspace_dir),
+ "a confirmed wait leaves whole-scope cleanup allowed"
+ );
}
/// The end-of-import wait is best-effort: a listing that never catches up
@@ -124,4 +128,8 @@ async fn a_last_batch_never_listed_still_finishes_the_import() {
&& waits.len() > 2,
"the final wait was retried, then given up: {waits:?}"
);
+ assert!(
+ listed_unconfirmed(&config.workspace_dir),
+ "an unconfirmed wait is recorded for the migration's cleanup"
+ );
}
diff --git a/crates/openhuman-core/src/memory/layout_migration/cleanup.rs b/crates/openhuman-core/src/memory/layout_migration/cleanup.rs
index 1a0ab234ba..e610a77712 100644
--- a/crates/openhuman-core/src/memory/layout_migration/cleanup.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/cleanup.rs
@@ -121,7 +121,14 @@ where
state.cleaning = true;
state.error = None;
state::save(workspace_dir, state)?;
- match remove_moved(engines, placement, shared, &paused).await {
+ // An import that finished without its last batch confirmed listed may
+ // hold items the survey cannot see yet: never erase a scope whole then,
+ // forget by verified id as for a shared tree.
+ let unconfirmed = crate::memory::import::listed_unconfirmed(workspace_dir);
+ if unconfirmed {
+ tracing::info!("[memory:layout_migration] import not confirmed listed; forgetting by id");
+ }
+ match remove_moved(engines, placement, shared || unconfirmed, &paused).await {
Ok(true) => {}
Ok(false) => return pause(workspace_dir, state, "background work is paused".into()),
Err(error) if error.is_account_wide() => {
diff --git a/crates/openhuman-core/src/memory/layout_migration/cleanup_tests.rs b/crates/openhuman-core/src/memory/layout_migration/cleanup_tests.rs
index 39311f4a41..38f38b2d92 100644
--- a/crates/openhuman-core/src/memory/layout_migration/cleanup_tests.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/cleanup_tests.rs
@@ -286,3 +286,47 @@ async fn a_shared_legacy_tree_is_forgotten_by_id_never_erased() {
assert_eq!(state.phase, Phase::Cleaned);
assert!(texts(legacy.as_ref()).await.is_empty());
}
+
+/// An import that finished without its last batch confirmed listed may hold
+/// items no export shows yet: cleanup must not erase a scope whole then, and
+/// forgets only the moved items by id (an erase would panic here).
+#[tokio::test]
+async fn after_an_unconfirmed_import_moved_items_are_forgotten_by_id_never_erased() {
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::create_dir_all(tmp.path().join("memory")).unwrap();
+ std::fs::write(
+ tmp.path().join("memory").join("import_state.json"),
+ r#"{"listed_unconfirmed": true}"#,
+ )
+ .unwrap();
+ assert!(crate::memory::import::listed_unconfirmed(tmp.path()));
+ let legacy = Arc::new(NeverErase(ReferenceEngine::new()));
+ for i in 0..5 {
+ legacy
+ .store(StoreItem::learning(
+ format!("fact {i}"),
+ LearningKind::Fact,
+ 0.5,
+ MemoryMeta::default(),
+ ))
+ .await
+ .unwrap();
+ }
+ let engines = Engines {
+ legacy: legacy.clone(),
+ tree: Arc::new(ReferenceEngine::new()),
+ };
+ let mut state = copied(tmp.path(), &engines).await;
+ cleanup(
+ tmp.path(),
+ &engines,
+ &placement(),
+ false,
+ &mut state,
+ || async { false },
+ )
+ .await
+ .unwrap();
+ assert_eq!(state.phase, Phase::Cleaned);
+ assert!(texts(legacy.as_ref()).await.is_empty());
+}
diff --git a/docs/TEST-COVERAGE-MATRIX.md b/docs/TEST-COVERAGE-MATRIX.md
index 789d969e3a..e54425421d 100644
--- a/docs/TEST-COVERAGE-MATRIX.md
+++ b/docs/TEST-COVERAGE-MATRIX.md
@@ -371,7 +371,7 @@ Spec: `docs/specs/memory-v2.md` (host) and `vendor/tinymemory/docs/specs/memory-
| 8.2.8 | One-time import of previous (v1) memory | RU+RI+WD | `crates/openhuman-core/src/memory/import_tests.rs`, `tests/memory_v2_e2e.rs`, `app/test/playwright/specs/memory-v2.spec.ts` (`importing previous memory needs explicit consent`, `a stopped import resumes only after consent`), `app/src/components/memory/MemoryImportBanner.test.tsx` | ✅ | Refused without consent; stops (not skips) on exhausted credits (`exhausted_credits_stop_the_import_instead_of_skipping_everything`) and resumes on its own only while memory work is free, per `billing::free_period_active` (`an_import_out_of_credits_resumes_only_once_automatic_runs_are_allowed`); batches are stored accepted and the last waited for once before Done (`batches_are_stored_accepted_and_the_last_waited_for_once`), a wait that never sees the listing still finishes Done (`a_last_batch_never_listed_still_finishes_the_import`); an unavailable engine is retried in-run (`a_transient_failure_is_retried_within_the_run`) and then resumed by the background job (`an_engine_that_stays_unavailable_is_resumed_by_the_background_job`); refused items are kept and retried (`a_refused_item_is_kept_and_a_retry_stores_it`); state written atomically and a resume keeps its total (`the_import_state_is_written_whole_and_leaves_no_staging_file`, `a_resumed_import_keeps_its_total_instead_of_rescanning`); the background job resumes an import the app quit during (`the_background_job_resumes_an_interrupted_import`), never while paused, including a pause mid-run (`nothing_resumes_while_background_work_is_paused`, `a_pause_that_lands_after_the_check_stops_the_run_at_the_next_batch`); a resume that cannot start is stopped, not retried (`an_automatic_resume_that_cannot_start_is_stopped_not_retried`); Resume button after an error, behind consent (`a stopped import resumes only after consent`) |
| 8.2.9 | MCP memory tools | RU | `crates/openhuman-core/src/mcp/server/tools/` | ✅ | `memory.recall`, `memory.fetch`, `memory.list`, `memory.learn`, `memory.forget` |
| 8.2.10 | Memory UI chips and legacy redirects | VU+WD | `app/src/components/memory/*.test.tsx`, `app/test/playwright/specs/memory-v2.spec.ts` | ✅ | Engine, Ask, Explorer, Learnings, Conversations, Brain, Background, Settings; `/brain` and `/settings/memory-engine` redirect |
-| 8.2.11 | Move into the per-user layout (v3) | RU+VU+WD | `crates/openhuman-core/src/memory/layout_migration/*_tests.rs`, `app/src/components/memory/MemoryImportBanner.test.tsx`, `app/test/playwright/specs/memory-v2.spec.ts` (`migrate now moves the legacy memory and the banner goes away`, `a tree other accounts may share is taken only after consent`) | ✅ | Items are stored again and verified, never copied as events; no switch before a full copy, then a catch-up copy; a source is removed only after its twin is verified (`an_item_without_its_twin_is_never_removed`), by kind-scope erasure or by id where the engine cannot erase; resumable after a pause or crash; runs on its own only while moving is free; a shared self-hosted tree moves only with consent and only one account may claim it (`a_tree_another_account_took_is_left_alone`, `the_first_account_takes_the_tree_and_keeps_it`); items are placed in the v3 layout before the setting switches (`places_into_the_v3_layout_before_the_switch`); signed out there is nothing to move; never starts while an import of old local memory is unfinished, counting a reserved import before its first state write (`nothing_starts_while_an_import_is_unfinished`, `an_import_counts_as_in_progress_from_its_reservation`), and starts on its own once the import is done, free period or not (`a_finished_import_starts_organizing_into_the_per_user_tree`); one banner shows import then organize (`does not organize while the import is still running`) |
+| 8.2.11 | Move into the per-user layout (v3) | RU+VU+WD | `crates/openhuman-core/src/memory/layout_migration/*_tests.rs`, `app/src/components/memory/MemoryImportBanner.test.tsx`, `app/test/playwright/specs/memory-v2.spec.ts` (`migrate now moves the legacy memory and the banner goes away`, `a tree other accounts may share is taken only after consent`) | ✅ | Items are stored again and verified, never copied as events; no switch before a full copy, then a catch-up copy; a source is removed only after its twin is verified (`an_item_without_its_twin_is_never_removed`), by kind-scope erasure or by id where the engine cannot erase, and only by id after an import whose last batch was not confirmed listed (`after_an_unconfirmed_import_moved_items_are_forgotten_by_id_never_erased`); resumable after a pause or crash; runs on its own only while moving is free; a shared self-hosted tree moves only with consent and only one account may claim it (`a_tree_another_account_took_is_left_alone`, `the_first_account_takes_the_tree_and_keeps_it`); items are placed in the v3 layout before the setting switches (`places_into_the_v3_layout_before_the_switch`); signed out there is nothing to move; never starts while an import of old local memory is unfinished, counting a reserved import before its first state write (`nothing_starts_while_an_import_is_unfinished`, `an_import_counts_as_in_progress_from_its_reservation`), and starts on its own once the import is done, free period or not (`a_finished_import_starts_organizing_into_the_per_user_tree`); one banner shows import then organize (`does not organize while the import is still running`) |
### 8.3 TinyMemory Engine Contract