Skip to content

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

Keep a retry that cannot resume retryable, and pause it with the scheduler

M3gA-Mindfbd560344735
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index c5c63bddfa..a4b83bf63c 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -404,7 +404,7 @@ pub(crate) async fn resume_interrupted_with(
// An interrupted retry of refused items resumes as a retry, from the
// items still listed (each stored one was taken off as it went).
let resumed = if file.state.phase == ImportPhase::Running && file.retrying {
- retry::begin_retry(config, file)
+ retry::begin_retry(config, file, Some(paused))
} else {
start_with(config, true, Some(paused)).await
};
@@ -426,8 +426,18 @@ pub(crate) async fn resume_interrupted_with(
// the checkpoint stay exactly as persisted, and the user's Resume
// continues from there.
let mut file = read_file(&config.workspace_dir);
- file.state.phase = ImportPhase::Error;
- file.state.error = Some(format!("the import could not resume: {error}"));
+ if file.retrying {
+ // The import itself is finished: keep it `Done` with the
+ // items still to retry, so Retry stays available.
+ file.state.phase = ImportPhase::Done;
+ file.state.error = Some(format!(
+ "the retry could not resume: {error}; press Retry again"
+ ));
+ file.retrying = false;
+ } else {
+ 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);
@@ -564,6 +574,14 @@ async fn run(
file.state.imported += outcome.stored;
// One entry per legacy id; a later refusal replaces the reason.
for failed in outcome.failed {
+ // Every item the importer yields has a legacy id; one without
+ // could never be found again to retry, so it is not listed.
+ if failed.id.is_empty() {
+ tracing::warn!(
+ "[memory:import] a refused item has no legacy id; not kept for retry"
+ );
+ continue;
+ }
match file.failed.iter_mut().find(|known| known.id == failed.id) {
Some(known) => *known = failed,
None => file.failed.push(failed),
diff --git a/crates/openhuman-core/src/memory/import_recovery_tests.rs b/crates/openhuman-core/src/memory/import_recovery_tests.rs
index 2c3085db13..748ad7e610 100644
--- a/crates/openhuman-core/src/memory/import_recovery_tests.rs
+++ b/crates/openhuman-core/src/memory/import_recovery_tests.rs
@@ -350,3 +350,91 @@ async fn a_retry_the_app_quit_during_resumes_as_a_retry() {
.iter()
.any(|item| item.text.contains("oolong")));
}
+
+/// A finished import with `d2` ("Ideas") still to retry, and the state of a
+/// retry the app quit during.
+fn quit_mid_retry(config: &Config) {
+ let mut file = ImportFile::default();
+ file.state = ImportState {
+ phase: ImportPhase::Running,
+ imported: 4,
+ total: 5,
+ error: None,
+ failed: 1,
+ };
+ file.failed = vec![FailedItem {
+ id: "memory_docs:d2".into(),
+ reason: "item too large".into(),
+ }];
+ file.retrying = true;
+ write_file(&config.workspace_dir, &file);
+}
+
+#[tokio::test]
+async fn a_retry_that_cannot_resume_stays_finished_and_retryable() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ legacy_workspace(&config.workspace_dir);
+ quit_mid_retry(&config);
+
+ // No engine is bound: the background resume of the retry cannot start.
+ assert!(!resume_interrupted_with(&config, always(false), billing(false)).await);
+ let file = read_file(&config.workspace_dir);
+ assert_eq!(
+ (file.state.phase, file.failed.len()),
+ (ImportPhase::Done, 1)
+ );
+ assert!(!file.retrying);
+ assert!(file
+ .state
+ .error
+ .as_deref()
+ .unwrap()
+ .contains("press Retry again"));
+
+ // Once an engine is there, Retry goes through.
+ let engine = bind_reference(&config);
+ retry_failed(&config).await.unwrap();
+ let done = wait_until_settled(&config).await;
+ assert_eq!(
+ (done.phase, done.failed),
+ (ImportPhase::Done, 0),
+ "{done:?}"
+ );
+ assert!(stored(&engine, MetaFilter::default())
+ .await
+ .iter()
+ .any(|item| item.text.contains("oolong")));
+}
+
+#[tokio::test]
+async fn a_resumed_retry_stops_when_background_work_is_paused() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ legacy_workspace(&config.workspace_dir);
+ let engine = bind_reference(&config);
+ quit_mid_retry(&config);
+
+ // Paused after the resume check: the retry stores nothing and is left
+ // as a running retry for the next unpaused tick.
+ let checks = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
+ let seen = checks.clone();
+ let paused: PauseCheck =
+ std::sync::Arc::new(move || seen.fetch_add(1, std::sync::atomic::Ordering::SeqCst) > 0);
+ assert!(resume_interrupted_with(&config, paused, billing(false)).await);
+ for _ in 0..200 {
+ if !RUNNING
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .contains(&config.workspace_dir)
+ {
+ break;
+ }
+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;
+ }
+ let file = read_file(&config.workspace_dir);
+ assert_eq!(file.state.phase, ImportPhase::Running);
+ assert!(file.retrying);
+ assert_eq!(file.failed.len(), 1);
+ assert!(stored(&engine, MetaFilter::default()).await.is_empty());
+}
diff --git a/crates/openhuman-core/src/memory/import_retry.rs b/crates/openhuman-core/src/memory/import_retry.rs
index 4c1203f7b9..287c2ff546 100644
--- a/crates/openhuman-core/src/memory/import_retry.rs
+++ b/crates/openhuman-core/src/memory/import_retry.rs
@@ -11,7 +11,7 @@ use tinymemory_integrations::import::LegacyWorkspace;
use super::{
legacy_id, read_file, skips_item, status, with_retries, write_file, FailedItem, ImportFile,
- RUNNING,
+ PauseCheck, RUNNING,
};
use crate::config::Config;
use crate::memory::engine::{self, BoundEngine};
@@ -28,11 +28,17 @@ pub async fn retry_failed(config: &Config) -> MemoryResult<ImportState> {
"no failed items to retry: the import has not finished or skipped nothing",
));
}
- begin_retry(config, file)
+ begin_retry(config, file, None)
}
-/// Starts (or, after an interruption, resumes) a retry of `file.failed`.
-pub(super) fn begin_retry(config: &Config, mut file: ImportFile) -> MemoryResult<ImportState> {
+/// Starts (or, after an interruption, resumes) a retry of `file.failed`. A
+/// resume the background job started passes its `paused` check, asked
+/// before every item, as the import asks it before every batch.
+pub(super) fn begin_retry(
+ config: &Config,
+ mut file: ImportFile,
+ paused: Option<PauseCheck>,
+) -> MemoryResult<ImportState> {
let bound = engine::resolve(config).engine()?;
let workspace_dir = config.workspace_dir.clone();
let claimed = RUNNING
@@ -52,7 +58,7 @@ pub(super) fn begin_retry(config: &Config, mut file: ImportFile) -> MemoryResult
"[memory:import] retrying failed items"
);
tokio::spawn(async move {
- retry_run(&workspace_dir, &bound, file).await;
+ retry_run(&workspace_dir, &bound, file, paused).await;
RUNNING
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
@@ -77,7 +83,12 @@ fn retry_stop_message(error: tinymemory_api::Error) -> String {
)
}
-async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFile) {
+async fn retry_run(
+ workspace_dir: &Path,
+ bound: &BoundEngine,
+ mut file: ImportFile,
+ paused: Option<PauseCheck>,
+) {
// 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
@@ -112,6 +123,13 @@ async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFi
}
Ok(items) => {
for item in items {
+ if paused.as_ref().is_some_and(|paused| paused()) {
+ // Left a running retry with what is still listed: the
+ // next unpaused tick resumes it.
+ tracing::info!("[memory:import] background paused; retry left to resume");
+ write_file(workspace_dir, &file);
+ return;
+ }
let id = legacy_id(&item);
match with_retries(|| bound.engine.store(item.clone())).await {
Ok(_) => {