Skip to content

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

Keep a stopped retry retryable, stop a failing credits resume looping

M3gA-Mind9c62463a7685
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index 39e89cabe3..7bb051617b 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -417,6 +417,8 @@ pub(crate) async fn resume_interrupted_with(
let mut file = read_file(&config.workspace_dir);
file.state.phase = ImportPhase::Error;
file.state.error = Some(format!("the import could not resume: {error}"));
+ // Not resumed again on its own: the failure would recur.
+ file.paused_for_credits = false;
write_file(&config.workspace_dir, &file);
false
}
@@ -533,7 +535,14 @@ pub async fn retry_failed(config: &Config) -> MemoryResult<ImportState> {
}
async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFile) {
- let wanted: HashSet<String> = file.failed.iter().map(|failed| failed.id.clone()).collect();
+ // An item with no legacy id cannot be found again, so it is never
+ // matched (every item the importer yields has one).
+ let wanted: HashSet<String> = file
+ .failed
+ .iter()
+ .map(|failed| failed.id.clone())
+ .filter(|id| !id.is_empty())
+ .collect();
let reader_dir = workspace_dir.to_path_buf();
let items = tokio::task::spawn_blocking(move || {
let workspace = LegacyWorkspace::open(&reader_dir).map_err(|error| error.to_string())?;
@@ -577,12 +586,10 @@ async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFi
}
}
file.state.failed = file.failed.len() as u64;
- // The import itself is finished either way; a stopped retry says why.
- file.state.phase = if failure.is_some() {
- ImportPhase::Error
- } else {
- ImportPhase::Done
- };
+ // The import itself is finished either way, so it stays `Done`: a retry
+ // stopped by the engine or the account keeps the items still to retry
+ // and says why, and the user retries again once that is resolved.
+ file.state.phase = ImportPhase::Done;
file.state.error = failure;
tracing::info!(
still_failed = file.failed.len(),
@@ -645,7 +652,13 @@ async fn run(
}
let outcome = store_batch(bound, std::mem::take(&mut batch)).await;
file.state.imported += outcome.stored;
- file.failed.extend(outcome.failed);
+ // One entry per legacy id; a later refusal replaces the reason.
+ for failed in outcome.failed {
+ match file.failed.iter_mut().find(|known| known.id == failed.id) {
+ Some(known) => *known = failed,
+ None => file.failed.push(failed),
+ }
+ }
file.state.failed = file.failed.len() as u64;
if let Some(checkpoint) = outcome.checkpoint {
file.checkpoint = checkpoint;
diff --git a/crates/openhuman-core/src/memory/import_recovery_tests.rs b/crates/openhuman-core/src/memory/import_recovery_tests.rs
index 63d145d7ad..1399e5a476 100644
--- a/crates/openhuman-core/src/memory/import_recovery_tests.rs
+++ b/crates/openhuman-core/src/memory/import_recovery_tests.rs
@@ -4,7 +4,8 @@
//! retried.
use super::tests::{
- always, billing, bind_failing, legacy_workspace, out_of_credits, wait_until_settled,
+ after_document, always, billing, bind_failing, legacy_workspace, out_of_credits,
+ wait_until_settled,
};
use super::*;
use crate::memory::error::INVALID_REQUEST;
@@ -30,10 +31,7 @@ async fn a_resumed_import_keeps_its_total_instead_of_rescanning() {
error: Some("unavailable".into()),
failed: 0,
},
- checkpoint: Checkpoint {
- documents: Some("d1".into()),
- ..Checkpoint::default()
- },
+ checkpoint: after_document("d1"),
},
);
let started = start(&config, true).await.unwrap();
@@ -59,10 +57,7 @@ fn the_import_state_is_written_whole_and_leaves_no_staging_file() {
error: None,
failed: 0,
},
- checkpoint: Checkpoint {
- documents: Some("d9".into()),
- ..Checkpoint::default()
- },
+ checkpoint: after_document("d9"),
};
write_file(tmp.path(), &file);
write_file(tmp.path(), &file);
@@ -182,22 +177,30 @@ fn only_a_self_hosted_engine_runs_automatically_until_the_free_period_check_land
);
}
-/// Whether a refusing engine still refuses the "Ideas" document.
-static REFUSE_IDEAS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
-
-fn refuse_ideas(item: &tinymemory_api::StoreItem) -> Option<tinymemory_api::Error> {
- let ideas = matches!(item, tinymemory_api::StoreItem::Document { title: Some(title), .. } if title == "Ideas");
- (ideas && REFUSE_IDEAS.load(std::sync::atomic::Ordering::SeqCst))
- .then(|| tinymemory_api::Error::InvalidRequest("item too large".into()))
+/// An engine refusal of the "Ideas" document while `$flag` is set, one flag
+/// per test so tests running in parallel never toggle each other's.
+macro_rules! ideas_refusal {
+ ($flag:ident, $refuse:ident) => {
+ static $flag: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
+ fn $refuse(item: &tinymemory_api::StoreItem) -> Option<tinymemory_api::Error> {
+ let ideas = matches!(item, tinymemory_api::StoreItem::Document { title: Some(title), .. } if title == "Ideas");
+ (ideas && $flag.load(std::sync::atomic::Ordering::SeqCst))
+ .then(|| tinymemory_api::Error::InvalidRequest("item too large".into()))
+ }
+ };
}
+ideas_refusal!(REFUSE_IDEAS_KEPT, refuse_ideas_kept);
+ideas_refusal!(REFUSE_IDEAS_STOPPED, refuse_ideas_stopped);
+ideas_refusal!(REFUSE_IDEAS_QUIT, refuse_ideas_quit);
+
#[tokio::test]
async fn a_refused_item_is_kept_and_a_retry_stores_it() {
let tmp = tempfile::tempdir().unwrap();
let config = config_in(&tmp);
legacy_workspace(&config.workspace_dir);
- REFUSE_IDEAS.store(true, std::sync::atomic::Ordering::SeqCst);
- let engine = bind_failing(&config, refuse_ideas);
+ REFUSE_IDEAS_KEPT.store(true, std::sync::atomic::Ordering::SeqCst);
+ let engine = bind_failing(&config, refuse_ideas_kept);
start(&config, true).await.unwrap();
let done = wait_until_settled(&config).await;
@@ -224,7 +227,7 @@ async fn a_refused_item_is_kept_and_a_retry_stores_it() {
);
// The engine takes it now: the list empties and the item is stored.
- REFUSE_IDEAS.store(false, std::sync::atomic::Ordering::SeqCst);
+ REFUSE_IDEAS_KEPT.store(false, std::sync::atomic::Ordering::SeqCst);
retry_failed(&config).await.unwrap();
let fixed = wait_until_settled(&config).await;
assert_eq!(
@@ -252,3 +255,91 @@ async fn retrying_needs_a_finished_import_with_failed_items() {
INVALID_REQUEST
);
}
+
+#[tokio::test]
+async fn a_retry_stopped_by_the_engine_stays_retryable() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ legacy_workspace(&config.workspace_dir);
+ REFUSE_IDEAS_STOPPED.store(true, std::sync::atomic::Ordering::SeqCst);
+ bind_failing(&config, refuse_ideas_stopped);
+ start(&config, true).await.unwrap();
+ assert_eq!(wait_until_settled(&config).await.failed, 1);
+
+ // The engine is down for the retry: it stops, still finished, still listed.
+ bind_failing(&config, |_| {
+ Some(tinymemory_api::Error::Unauthorized("sign in".into()))
+ });
+ retry_failed(&config).await.unwrap();
+ let stopped = wait_until_settled(&config).await;
+ assert_eq!(
+ (stopped.phase, stopped.failed),
+ (ImportPhase::Done, 1),
+ "{stopped:?}"
+ );
+ assert!(stopped.error.is_some());
+
+ // Back up: the same retry goes through.
+ REFUSE_IDEAS_STOPPED.store(false, std::sync::atomic::Ordering::SeqCst);
+ let engine = bind_failing(&config, refuse_ideas_stopped);
+ retry_failed(&config).await.unwrap();
+ let done = wait_until_settled(&config).await;
+ assert_eq!(
+ (done.phase, done.failed, done.error),
+ (ImportPhase::Done, 0, None)
+ );
+ assert!(stored(&engine, MetaFilter::default())
+ .await
+ .iter()
+ .any(|i| i.text.contains("oolong")));
+}
+
+#[tokio::test]
+async fn a_credits_pause_whose_resume_fails_is_not_retried_every_tick() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ legacy_workspace(&config.workspace_dir);
+ bind_failing(&config, out_of_credits);
+ start(&config, true).await.unwrap();
+ wait_until_settled(&config).await;
+ assert!(read_file(&config.workspace_dir).paused_for_credits);
+
+ // The legacy store is gone: the automatic resume cannot start.
+ std::fs::remove_file(config.workspace_dir.join("memory").join("memory.db")).unwrap();
+ bind_reference(&config);
+ assert!(!resume_interrupted_with(&config, always(false), billing(true)).await);
+ assert!(!read_file(&config.workspace_dir).paused_for_credits);
+ assert!(!resume_interrupted_with(&config, always(false), billing(true)).await);
+}
+
+#[tokio::test]
+async fn a_retry_the_app_quit_during_finishes_with_its_failed_items_kept() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ legacy_workspace(&config.workspace_dir);
+ REFUSE_IDEAS_QUIT.store(true, std::sync::atomic::Ordering::SeqCst);
+ let engine = bind_failing(&config, refuse_ideas_quit);
+ start(&config, true).await.unwrap();
+ assert_eq!(wait_until_settled(&config).await.failed, 1);
+ let stored_before = stored(&engine, MetaFilter::default()).await.len();
+
+ // The app quit mid-retry: the state file says Running, nothing is live.
+ let mut file = read_file(&config.workspace_dir);
+ file.state.phase = ImportPhase::Running;
+ write_file(&config.workspace_dir, &file);
+
+ // The background resume reads on from the import's final checkpoint, so
+ // it stores nothing new and finishes with the failed item still listed.
+ assert!(resume_interrupted_with(&config, always(false), billing(false)).await);
+ let done = wait_until_settled(&config).await;
+ assert_eq!(
+ (done.phase, done.failed),
+ (ImportPhase::Done, 1),
+ "{done:?}"
+ );
+ assert_eq!(
+ stored(&engine, MetaFilter::default()).await.len(),
+ stored_before
+ );
+ assert_eq!(read_file(&config.workspace_dir).failed.len(), 1);
+}
diff --git a/crates/openhuman-core/src/memory/import_tests.rs b/crates/openhuman-core/src/memory/import_tests.rs
index 0263c5d2e7..69c09f91a9 100644
--- a/crates/openhuman-core/src/memory/import_tests.rs
+++ b/crates/openhuman-core/src/memory/import_tests.rs
@@ -56,6 +56,15 @@ pub(super) fn legacy_workspace(workspace_dir: &Path) {
.unwrap();
}
+/// The checkpoint after document `id`. Built from `default()` and an
+/// assignment: tinymemory's `Checkpoint` is non-exhaustive from its next
+/// release, which refuses a struct literal outside the crate.
+pub(super) fn after_document(id: &str) -> Checkpoint {
+ let mut checkpoint = Checkpoint::default();
+ checkpoint.documents = Some(id.to_string());
+ checkpoint
+}
+
pub(super) async fn wait_until_settled(config: &Config) -> ImportState {
for _ in 0..400 {
let state = status(config);
@@ -173,10 +182,7 @@ async fn an_interrupted_import_resumes_from_its_checkpoint() {
error: Some("unauthorized: sign in".into()),
failed: 0,
},
- checkpoint: Checkpoint {
- documents: Some("d1".into()),
- ..Checkpoint::default()
- },
+ checkpoint: after_document("d1"),
},
);
assert_eq!(status(&config).phase, ImportPhase::Error);
@@ -420,10 +426,7 @@ async fn an_import_the_app_quit_during_resumes_on_its_own() {
error: None,
failed: 0,
},
- checkpoint: Checkpoint {
- documents: Some("d1".into()),
- ..Checkpoint::default()
- },
+ checkpoint: after_document("d1"),
},
);
@@ -478,10 +481,7 @@ fn quit_mid_import(config: &Config) {
error: None,
failed: 0,
},
- checkpoint: Checkpoint {
- documents: Some("d1".into()),
- ..Checkpoint::default()
- },
+ checkpoint: after_document("d1"),
},
);
}
diff --git a/tests/memory_v2_e2e.rs b/tests/memory_v2_e2e.rs
index 8e91c31f70..8521f2f22f 100644
--- a/tests/memory_v2_e2e.rs
+++ b/tests/memory_v2_e2e.rs
@@ -1698,6 +1698,14 @@ async fn import_scan_finds_nothing_and_start_needs_consent() {
let status = f.ok("openhuman.memory_import_status", json!({})).await;
assert_eq!(status["state"]["phase"], json!("idle"));
+ assert_eq!(status["state"]["failed"], json!(0));
+
+ // Nothing finished, nothing skipped: there is nothing to retry.
+ assert_eq!(
+ f.code("openhuman.memory_import_retry_failed", json!({}))
+ .await,
+ "INVALID_REQUEST"
+ );
// Importing uploads local data: refused without consent, signed in or not.
assert_eq!(