Skip to content

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

fix(memory): import and organize never start over each other; organize only while free

M3gA-Mind2da132b7fb36
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index 60721f3a77..52db651f52 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -217,6 +217,11 @@ async fn store_batch(bound: &BoundEngine, batch: Vec<ImportedItem>) -> BatchOutc
/// Imports running now, per workspace.
static RUNNING: LazyLock<Mutex<HashSet<PathBuf>>> = LazyLock::new(|| Mutex::new(HashSet::new()));
+/// Held while an import or a layout migration decides to start: each checks
+/// the other is not going, then registers itself, under this one lock, so
+/// neither can slip in between the other's check and its registration.
+pub(crate) static START_GATE: Mutex<()> = Mutex::new(());
+
#[derive(Debug, Default, Serialize, Deserialize)]
struct ImportFile {
#[serde(default)]
@@ -304,8 +309,14 @@ pub async fn scan(config: &Config) -> MemoryResult<ImportScanView> {
/// old local memory lands before the tree is reorganised.
#[must_use]
pub fn in_progress(config: &Config) -> bool {
+ // A live import counts from its reservation, before its first state
+ // write (the scan of the old store comes first).
+ let live = RUNNING
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .contains(&config.workspace_dir);
let file = read_file(&config.workspace_dir);
- file.state.phase == ImportPhase::Running || file.paused_for_credits
+ live || file.state.phase == ImportPhase::Running || file.paused_for_credits
}
/// `memory_import_status`.
@@ -467,10 +478,22 @@ async fn start_with(
}
let bound = engine::resolve(config).engine()?;
let workspace_dir = config.workspace_dir.clone();
- let claimed = RUNNING
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .insert(workspace_dir.clone());
+ let claimed = {
+ let _gate = START_GATE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ // Imported items written to the legacy tree while it is being moved
+ // would be left behind there.
+ if super::layout_migration::service::is_running(config) {
+ return Err(MemoryError::invalid(
+ "memory is being organized; import once that finishes",
+ ));
+ }
+ RUNNING
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .insert(workspace_dir.clone())
+ };
if !claimed {
return Ok(status(config));
}
@@ -523,7 +546,9 @@ async fn start_with(
let started = super::layout_migration::start(
config,
Arc::new(super::layout_migration::AppHost),
- super::layout_migration::Trigger::Manual { takeover: false },
+ // Not the user's start: it runs only while moving is free,
+ // like the background job's.
+ super::layout_migration::Trigger::Auto,
Arc::new(scheduler_paused),
);
tracing::info!(started, "[memory:import] import done; organizing next");
diff --git a/crates/openhuman-core/src/memory/import_tests.rs b/crates/openhuman-core/src/memory/import_tests.rs
index 31f1197238..27241450e4 100644
--- a/crates/openhuman-core/src/memory/import_tests.rs
+++ b/crates/openhuman-core/src/memory/import_tests.rs
@@ -252,6 +252,18 @@ async fn a_full_import_stores_every_item_and_finishes_done() {
assert!(file_path(&config.workspace_dir).exists());
}
+#[test]
+fn an_import_counts_as_in_progress_from_its_reservation() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ assert!(!in_progress(&config));
+ // Reserved, still scanning the old store: no state written yet.
+ RUNNING.lock().unwrap().insert(config.workspace_dir.clone());
+ assert!(in_progress(&config));
+ RUNNING.lock().unwrap().remove(&config.workspace_dir);
+ assert!(!in_progress(&config));
+}
+
#[tokio::test]
async fn a_finished_import_starts_organizing_into_the_per_user_tree() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/memory/layout_migration/service.rs b/crates/openhuman-core/src/memory/layout_migration/service.rs
index a4f0de125b..f9394f54f1 100644
--- a/crates/openhuman-core/src/memory/layout_migration/service.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/service.rs
@@ -103,7 +103,7 @@ pub fn status(config: &Config) -> MemoryResult<MigrationStatus> {
})
}
-fn is_running(config: &Config) -> bool {
+pub(crate) fn is_running(config: &Config) -> bool {
RUNNING
.lock()
.unwrap_or_else(PoisonError::into_inner)
@@ -131,6 +131,9 @@ pub fn start(
trigger: Trigger,
paused: Arc<dyn Fn() -> bool + Send + Sync>,
) -> bool {
+ let _gate = crate::memory::import::START_GATE
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
if crate::memory::import::in_progress(&config) {
tracing::info!("[memory:layout_migration] waiting for the import to finish");
return false;
diff --git a/docs/TEST-COVERAGE-MATRIX.md b/docs/TEST-COVERAGE-MATRIX.md
index 8276f05165..cd2b0ad56f 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`); 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, and starts on its own once the import is done (`nothing_starts_while_an_import_is_unfinished`); 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; 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, under the free-period check (`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