Skip to content

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

Resume an interrupted retry as a retry, and say to press Retry

M3gA-Mind95260a684d31
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index b4e90337e4..c91cb9e3bf 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -229,6 +229,10 @@ struct ImportFile {
/// Items the engine refused, for `retry_failed`.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
failed: Vec<FailedItem>,
+ /// The `Running` run is a retry of `failed`, not the import itself: an
+ /// interrupted one resumes as a retry.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ retrying: bool,
#[serde(default)]
checkpoint: Checkpoint,
}
@@ -397,7 +401,14 @@ pub(crate) async fn resume_interrupted_with(
tracing::debug!("[memory:import] background paused; interrupted import left for later");
return false;
}
- match start_with(config, true, Some(paused)).await {
+ // 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 {
+ begin_retry(config, file)
+ } else {
+ start_with(config, true, Some(paused)).await
+ };
+ match resumed {
Ok(state) => {
tracing::info!(
imported = state.imported,
@@ -501,14 +512,19 @@ async fn start_with(
/// skipped because the engine refused them. Items that now store leave the
/// list; ones refused again stay, with the new reason.
pub async fn retry_failed(config: &Config) -> MemoryResult<ImportState> {
- let bound = engine::resolve(config).engine()?;
- let workspace_dir = config.workspace_dir.clone();
- let mut file = read_file(&workspace_dir);
+ let file = read_file(&config.workspace_dir);
if file.state.phase != ImportPhase::Done || file.failed.is_empty() {
return Err(MemoryError::invalid(
"no failed items to retry: the import has not finished or skipped nothing",
));
}
+ begin_retry(config, file)
+}
+
+/// Starts (or, after an interruption, resumes) a retry of `file.failed`.
+fn begin_retry(config: &Config, mut file: ImportFile) -> MemoryResult<ImportState> {
+ 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)
@@ -518,6 +534,7 @@ pub async fn retry_failed(config: &Config) -> MemoryResult<ImportState> {
}
file.state.phase = ImportPhase::Running;
file.state.error = None;
+ file.retrying = true;
write_file(&workspace_dir, &file);
let state = file.state.clone();
tracing::info!(
@@ -534,6 +551,22 @@ pub async fn retry_failed(config: &Config) -> MemoryResult<ImportState> {
Ok(state)
}
+/// Why a retry of failed items stopped, and that pressing Retry again
+/// continues it: a retry is the user's action, so nothing resumes it alone.
+fn retry_stop_message(error: tinymemory_api::Error) -> String {
+ if error.is_transient() {
+ return "the memory service is unavailable; press Retry again once it is back".to_string();
+ }
+ if is_insufficient_credits(&error) {
+ return "not enough credits to retry these items; top up, then press Retry again"
+ .to_string();
+ }
+ format!(
+ "the retry stopped: {}; press Retry again",
+ MemoryError::from(error)
+ )
+}
+
async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFile) {
// An item with no legacy id cannot be found again, so it is never
// matched (every item the importer yields has one).
@@ -562,7 +595,11 @@ async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFi
.and_then(|items| items);
let mut failure = None;
match items {
- Err(error) => failure = Some(format!("reading the legacy store failed: {error}")),
+ Err(error) => {
+ failure = Some(format!(
+ "reading the legacy store failed: {error}; press Retry again"
+ ));
+ }
Ok(items) => {
for item in items {
let id = legacy_id(&item);
@@ -582,7 +619,7 @@ async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFi
}
}
Err(error) => {
- failure = Some(fatal_message(error));
+ failure = Some(retry_stop_message(error));
break;
}
}
@@ -595,6 +632,7 @@ async fn retry_run(workspace_dir: &Path, bound: &BoundEngine, mut file: ImportFi
// and says why, and the user retries again once that is resolved.
file.state.phase = ImportPhase::Done;
file.state.error = failure;
+ file.retrying = false;
tracing::info!(
still_failed = file.failed.len(),
"[memory:import] retry of failed items finished"
diff --git a/crates/openhuman-core/src/memory/import_recovery_tests.rs b/crates/openhuman-core/src/memory/import_recovery_tests.rs
index 1399e5a476..2c3085db13 100644
--- a/crates/openhuman-core/src/memory/import_recovery_tests.rs
+++ b/crates/openhuman-core/src/memory/import_recovery_tests.rs
@@ -24,6 +24,7 @@ async fn a_resumed_import_keeps_its_total_instead_of_rescanning() {
&ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase: ImportPhase::Error,
imported: 1,
@@ -50,6 +51,7 @@ fn the_import_state_is_written_whole_and_leaves_no_staging_file() {
let file = ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 3,
@@ -277,7 +279,11 @@ async fn a_retry_stopped_by_the_engine_stays_retryable() {
(ImportPhase::Done, 1),
"{stopped:?}"
);
- assert!(stopped.error.is_some());
+ // A retry is the user's action: it says to press Retry, not that it
+ // resumes on its own.
+ let reason = stopped.error.as_deref().unwrap();
+ assert!(reason.contains("press Retry again"), "{reason}");
+ assert!(!reason.contains("on its own"), "{reason}");
// Back up: the same retry goes through.
REFUSE_IDEAS_STOPPED.store(false, std::sync::atomic::Ordering::SeqCst);
@@ -313,7 +319,7 @@ async fn a_credits_pause_whose_resume_fails_is_not_retried_every_tick() {
}
#[tokio::test]
-async fn a_retry_the_app_quit_during_finishes_with_its_failed_items_kept() {
+async fn a_retry_the_app_quit_during_resumes_as_a_retry() {
let tmp = tempfile::tempdir().unwrap();
let config = config_in(&tmp);
legacy_workspace(&config.workspace_dir);
@@ -321,25 +327,26 @@ async fn a_retry_the_app_quit_during_finishes_with_its_failed_items_kept() {
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.
+ // The app quit mid-retry: the state file says a retry is Running.
let mut file = read_file(&config.workspace_dir);
file.state.phase = ImportPhase::Running;
+ file.retrying = true;
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.
+ // The background resume carries on with the retry, not the import, and
+ // the engine takes the item now.
+ REFUSE_IDEAS_QUIT.store(false, std::sync::atomic::Ordering::SeqCst);
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),
+ (ImportPhase::Done, 0),
"{done:?}"
);
- assert_eq!(
- stored(&engine, MetaFilter::default()).await.len(),
- stored_before
- );
- assert_eq!(read_file(&config.workspace_dir).failed.len(), 1);
+ assert!(!read_file(&config.workspace_dir).retrying);
+ assert!(stored(&engine, MetaFilter::default())
+ .await
+ .iter()
+ .any(|item| item.text.contains("oolong")));
}
diff --git a/crates/openhuman-core/src/memory/import_tests.rs b/crates/openhuman-core/src/memory/import_tests.rs
index 69c09f91a9..6037c8a6dc 100644
--- a/crates/openhuman-core/src/memory/import_tests.rs
+++ b/crates/openhuman-core/src/memory/import_tests.rs
@@ -175,6 +175,7 @@ async fn an_interrupted_import_resumes_from_its_checkpoint() {
&ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase: ImportPhase::Error,
imported: 1,
@@ -210,6 +211,7 @@ fn a_running_state_with_no_live_import_reads_as_interrupted() {
&ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 3,
@@ -419,6 +421,7 @@ async fn an_import_the_app_quit_during_resumes_on_its_own() {
&ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 1,
@@ -451,6 +454,7 @@ async fn a_stopped_or_finished_import_is_not_resumed_on_its_own() {
&ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase,
imported: 0,
@@ -474,6 +478,7 @@ fn quit_mid_import(config: &Config) {
&ImportFile {
paused_for_credits: false,
failed: Vec::new(),
+ retrying: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 1,