Skip to content

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

Copy again before finishing after an unconfirmed import

M3gA-Mind3d56da084958
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index 450a9258d9..d659fe060e 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -242,22 +242,26 @@ 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.
+ /// The import ended with its last batch not confirmed listed (the final
+ /// wait gave up); see [`listed_unconfirmed`].
#[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.
+/// Whether the last import ended with its last batch not confirmed listed:
+/// the layout migration then forgets by id, never erasing scopes whole, and
+/// copies again before it finishes. A confirming import, or it, clears it.
pub(crate) fn listed_unconfirmed(workspace_dir: &Path) -> bool {
read_file(workspace_dir).listed_unconfirmed
}
+/// Clears [`listed_unconfirmed`] once the migration has re-checked.
+pub(crate) fn clear_listed_unconfirmed(workspace_dir: &Path) {
+ let mut file = read_file(workspace_dir);
+ file.listed_unconfirmed = false;
+ write_file(workspace_dir, &file);
+}
+
fn file_path(workspace_dir: &Path) -> PathBuf {
workspace_dir.join("memory").join("import_state.json")
}
diff --git a/crates/openhuman-core/src/memory/layout_migration/job.rs b/crates/openhuman-core/src/memory/layout_migration/job.rs
index dc8b84c658..1e61e57008 100644
--- a/crates/openhuman-core/src/memory/layout_migration/job.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/job.rs
@@ -111,6 +111,10 @@ pub trait LayoutHost: Send + Sync {
fn legacy_claim(&self, config: &Config) -> MemoryResult<Option<ClaimKey>>;
}
+/// The most extra catch-up passes after an import that ended with its last
+/// batch not confirmed listed.
+const MAX_RECHECKS: u32 = 3;
+
/// Runs (or resumes) the migration of `config`'s account. `paused` is the
/// scheduler's own pause (background work held), asked before every page.
///
@@ -129,7 +133,9 @@ where
{
let dir = config.workspace_dir.as_path();
let mut state = state::load(dir)?;
- if state.phase == Phase::Cleaned {
+ // Cleaned with the import still unconfirmed (a stop between cleanup and
+ // the re-check below) goes round once more rather than counting as done.
+ if state.phase == Phase::Cleaned && !crate::memory::import::listed_unconfirmed(dir) {
return Ok(Outcome::Done);
}
let engines = host.engines(config)?;
@@ -190,6 +196,27 @@ where
stop,
)
.await?;
+ // An import that ended with its last batch not confirmed listed may hold
+ // items no copy saw yet: copy again on a later tick (at least once, then
+ // while a pass still moves something new, at most MAX_RECHECKS times)
+ // before finishing. A late item is never erased meanwhile (`cleanup`).
+ if state.phase == Phase::Cleaned && crate::memory::import::listed_unconfirmed(dir) {
+ let new = state.copied > state.replayed;
+ if (state.rechecks == 0 || new) && state.rechecks < MAX_RECHECKS {
+ state.rechecks += 1;
+ state.caught_up = false;
+ state.cleaning = false;
+ state.cursor = None;
+ state.phase = Phase::Copied;
+ state::save(dir, &state)?;
+ tracing::info!(
+ rechecks = state.rechecks,
+ "[memory:layout_migration] import not confirmed listed; copying again"
+ );
+ return Ok(Outcome::Paused);
+ }
+ crate::memory::import::clear_listed_unconfirmed(dir);
+ }
Ok(if state.phase == Phase::Cleaned {
Outcome::Done
} else {
diff --git a/crates/openhuman-core/src/memory/layout_migration/job_tests.rs b/crates/openhuman-core/src/memory/layout_migration/job_tests.rs
index c827b5a65d..adfea62095 100644
--- a/crates/openhuman-core/src/memory/layout_migration/job_tests.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/job_tests.rs
@@ -151,3 +151,52 @@ async fn a_tree_another_account_took_is_left_alone() {
.needed
);
}
+
+/// After an import that ended with its last batch not confirmed listed, an
+/// item the listing shows only after the first pass is still moved: the run
+/// copies again on later ticks instead of finishing, and finishes (clearing
+/// the flag) once a pass moves nothing new.
+#[tokio::test]
+async fn after_an_unconfirmed_import_an_item_listed_late_is_still_moved() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = crate::memory::test_fixtures::config_in(&tmp);
+ let memory_dir = config.workspace_dir.join("memory");
+ std::fs::create_dir_all(&memory_dir).unwrap();
+ std::fs::write(
+ memory_dir.join("import_state.json"),
+ r#"{"listed_unconfirmed": true}"#,
+ )
+ .unwrap();
+ assert!(crate::memory::import::listed_unconfirmed(
+ &config.workspace_dir
+ ));
+ let host = FakeHost::with(3).await;
+ let manual = Trigger::Manual { takeover: false };
+
+ assert_eq!(
+ go(&config, &host, manual).await,
+ Outcome::Paused,
+ "an unconfirmed import is copied again, not finished"
+ );
+ assert_eq!(count(host.tree.as_ref()).await, 3);
+ // Accepted by the import, listed only now.
+ host.legacy.store(fact("listed late")).await.unwrap();
+
+ let mut outcome = Outcome::Paused;
+ for _ in 0..5 {
+ outcome = go(&config, &host, manual).await;
+ if outcome == Outcome::Done {
+ break;
+ }
+ }
+ assert_eq!(outcome, Outcome::Done);
+ assert_eq!(
+ count(host.tree.as_ref()).await,
+ 4,
+ "the late item was moved"
+ );
+ assert_eq!(count(host.legacy.as_ref()).await, 0);
+ assert!(!crate::memory::import::listed_unconfirmed(
+ &config.workspace_dir
+ ));
+}
diff --git a/crates/openhuman-core/src/memory/layout_migration/service.rs b/crates/openhuman-core/src/memory/layout_migration/service.rs
index f9394f54f1..0540ebfe87 100644
--- a/crates/openhuman-core/src/memory/layout_migration/service.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/service.rs
@@ -215,7 +215,12 @@ pub fn tick(
paused: Arc<dyn Fn() -> bool + Send + Sync>,
) -> bool {
match state::load(&config.workspace_dir) {
- Ok(state) if state.phase == Phase::Cleaned => false,
+ Ok(state)
+ if state.phase == Phase::Cleaned
+ && !crate::memory::import::listed_unconfirmed(&config.workspace_dir) =>
+ {
+ false
+ }
Ok(_) if paused() || is_running(config) => false,
Ok(_) => start(config.clone(), host, Trigger::Auto, paused),
Err(error) => {
diff --git a/crates/openhuman-core/src/memory/layout_migration/state.rs b/crates/openhuman-core/src/memory/layout_migration/state.rs
index 012707ae7d..0a855039b0 100644
--- a/crates/openhuman-core/src/memory/layout_migration/state.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/state.rs
@@ -73,6 +73,14 @@ pub struct MigrationState {
/// The user agreed to take a legacy tree other accounts may share.
#[serde(default)]
pub takeover: bool,
+ /// Extra catch-up passes made because the import ended with its last
+ /// batch not confirmed listed (see `job::run`).
+ #[serde(default, skip_serializing_if = "is_zero")]
+ pub rechecks: u32,
+}
+
+fn is_zero(n: &u32) -> bool {
+ *n == 0
}
/// The state file of `workspace_dir`.
diff --git a/crates/openhuman-core/src/memory/layout_migration/state_tests.rs b/crates/openhuman-core/src/memory/layout_migration/state_tests.rs
index c8bbe49231..77adba8877 100644
--- a/crates/openhuman-core/src/memory/layout_migration/state_tests.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/state_tests.rs
@@ -24,6 +24,7 @@ fn a_saved_state_loads_back_and_leaves_no_temporary_file() {
switched: true,
caught_up: false,
takeover: true,
+ rechecks: 1,
};
save(tmp.path(), &state).unwrap();
assert_eq!(load(tmp.path()).unwrap(), state);
diff --git a/docs/TEST-COVERAGE-MATRIX.md b/docs/TEST-COVERAGE-MATRIX.md
index e54425421d..e8a4472f92 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, 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.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`), which is then copied again until a pass moves nothing new, so an item listed late is still moved (`after_an_unconfirmed_import_an_item_listed_late_is_still_moved`); 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