Skip to content

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

Pause an import that ran out of credits, resume it when runs are free

M3gA-Mindbaa644e33a7d
diff --git a/crates/openhuman-core/src/memory/import.rs b/crates/openhuman-core/src/memory/import.rs
index 2df6004903..5c434d25ca 100644
--- a/crates/openhuman-core/src/memory/import.rs
+++ b/crates/openhuman-core/src/memory/import.rs
@@ -68,6 +68,9 @@ struct BatchOutcome {
/// Whether `fatal` is transient: the engine stayed unavailable through
/// every retry, so the background job resumes the run.
transient: bool,
+ /// Whether `fatal` is the account's credits running out: the run is
+ /// paused until automatic runs are allowed again ([`BillingCheck`]).
+ credits: bool,
}
/// Runs `call` again after each of [`RETRY_DELAYS`] while it fails with a
@@ -151,6 +154,7 @@ async fn store_batch(bound: &BoundEngine, batch: Vec<ImportedItem>) -> BatchOutc
Err(error) if !skips_item(&error) => {
return BatchOutcome {
transient: error.is_transient(),
+ credits: is_insufficient_credits(&error),
fatal: Some(fatal_message(error)),
..BatchOutcome::default()
};
@@ -168,6 +172,7 @@ async fn store_batch(bound: &BoundEngine, batch: Vec<ImportedItem>) -> BatchOutc
Ok(_) => outcome.stored += 1,
Err(error) if !skips_item(&error) => {
outcome.transient = error.is_transient();
+ outcome.credits = is_insufficient_credits(&error);
outcome.fatal = Some(fatal_message(error));
return outcome;
}
@@ -190,6 +195,10 @@ static RUNNING: LazyLock<Mutex<HashSet<PathBuf>>> = LazyLock::new(|| Mutex::new(
struct ImportFile {
#[serde(default)]
state: ImportState,
+ /// The run stopped because the account ran out of credits; the
+ /// background job resumes it once automatic runs are allowed.
+ #[serde(default, skip_serializing_if = "std::ops::Not::not")]
+ paused_for_credits: bool,
#[serde(default)]
checkpoint: Checkpoint,
}
@@ -282,6 +291,22 @@ pub fn status(config: &Config) -> ImportState {
/// before it starts and again before every batch it stores.
pub(crate) type PauseCheck = Arc<dyn Fn() -> bool + Send + Sync>;
+/// Whether memory work that uploads on its own, with no user action (an
+/// automatic import, or resuming one paused for credits), may run now.
+pub(crate) type BillingCheck = Arc<dyn Fn(&Config) -> bool + Send + Sync>;
+
+/// The one gate on automatic migration runs. A self-hosted CortexDB bills
+/// nobody, so it always may. On the TinyHuman memory service an automatic
+/// run spends the user's credits, so it may only while the backend's memory
+/// free period is active.
+///
+/// The shared free-period check (`free_period_active()`) replaces the
+/// TinyHuman half once it lands; until then the free period is unknown, and
+/// unknown is not free.
+pub(crate) fn automatic_run_allowed(config: &Config) -> bool {
+ config.memory.engine.trim() == super::engine::CORTEXDB_ENGINE
+}
+
/// The scheduler's pause, which includes being signed out.
fn scheduler_paused() -> bool {
matches!(
@@ -294,7 +319,12 @@ fn scheduler_paused() -> bool {
/// the scheduler's pause ([`resume_interrupted_with`]). Called from memory's
/// background job.
pub async fn resume_interrupted(config: &Config) -> bool {
- resume_interrupted_with(config, Arc::new(scheduler_paused)).await
+ resume_interrupted_with(
+ config,
+ Arc::new(scheduler_paused),
+ Arc::new(automatic_run_allowed),
+ )
+ .await
}
/// Resumes an interrupted import unless `paused` says background work is
@@ -311,8 +341,19 @@ pub async fn resume_interrupted(config: &Config) -> bool {
/// resume could not start: that failure is persisted as `Error`, so a
/// failure that would recur does not loop. Returns whether a run was
/// started.
-pub(crate) async fn resume_interrupted_with(config: &Config, paused: PauseCheck) -> bool {
- if read_file(&config.workspace_dir).state.phase != ImportPhase::Running {
+pub(crate) async fn resume_interrupted_with(
+ config: &Config,
+ paused: PauseCheck,
+ billing: BillingCheck,
+) -> bool {
+ let file = read_file(&config.workspace_dir);
+ let resumable = match file.state.phase {
+ ImportPhase::Running => true,
+ // Out of credits: only once automatic runs are allowed again.
+ ImportPhase::Error => file.paused_for_credits && billing(config),
+ _ => false,
+ };
+ if !resumable {
return false;
}
let live = RUNNING
@@ -406,6 +447,7 @@ async fn start_with(
return Err(MemoryError::invalid("no v1 memory store to import"));
};
file.state.phase = ImportPhase::Running;
+ file.paused_for_credits = false;
if let Some(total) = total {
file.state.total = total;
}
@@ -451,6 +493,7 @@ async fn run(
let mut since_checkpoint = 0u64;
let mut failure = None;
let mut transient = false;
+ let mut credits = false;
let mut pausing = false;
let mut batch: Vec<ImportedItem> = Vec::with_capacity(STORE_BATCH);
let mut reading = true;
@@ -487,6 +530,7 @@ async fn run(
if let Some(error) = outcome.fatal {
failure = Some(error);
transient = outcome.transient;
+ credits = outcome.credits;
break;
}
}
@@ -512,6 +556,7 @@ async fn run(
file.state.error = Some(error);
}
Some(error) => {
+ file.paused_for_credits = credits;
tracing::warn!(
imported = file.state.imported,
"[memory:import] import stopped"
diff --git a/crates/openhuman-core/src/memory/import_tests.rs b/crates/openhuman-core/src/memory/import_tests.rs
index fd997582f7..f5aafc3e4a 100644
--- a/crates/openhuman-core/src/memory/import_tests.rs
+++ b/crates/openhuman-core/src/memory/import_tests.rs
@@ -164,6 +164,7 @@ async fn an_interrupted_import_resumes_from_its_checkpoint() {
write_file(
&config.workspace_dir,
&ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase: ImportPhase::Error,
imported: 1,
@@ -199,6 +200,7 @@ fn a_running_state_with_no_live_import_reads_as_interrupted() {
write_file(
&config.workspace_dir,
&ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 3,
@@ -405,6 +407,7 @@ async fn an_import_the_app_quit_during_resumes_on_its_own() {
write_file(
&config.workspace_dir,
&ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 1,
@@ -437,6 +440,7 @@ async fn a_stopped_or_finished_import_is_not_resumed_on_its_own() {
write_file(
&config.workspace_dir,
&ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase,
imported: 0,
@@ -457,6 +461,7 @@ fn quit_mid_import(config: &Config) {
write_file(
&config.workspace_dir,
&ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 1,
@@ -494,7 +499,7 @@ async fn nothing_resumes_while_background_work_is_paused() {
let engine = bind_reference(&config);
quit_mid_import(&config);
- assert!(!resume_interrupted_with(&config, always(true)).await);
+ assert!(!resume_interrupted_with(&config, always(true), billing(false)).await);
assert!(stored(&engine, MetaFilter::default()).await.is_empty());
assert_eq!(
read_file(&config.workspace_dir).state.phase,
@@ -502,11 +507,15 @@ async fn nothing_resumes_while_background_work_is_paused() {
"left resumable for a later, unpaused tick"
);
- assert!(resume_interrupted_with(&config, always(false)).await);
+ assert!(resume_interrupted_with(&config, always(false), billing(false)).await);
assert_eq!(wait_until_settled(&config).await.phase, ImportPhase::Done);
}
/// A pause check that always answers `paused`.
+fn billing(allowed: bool) -> BillingCheck {
+ Arc::new(move |_: &Config| allowed)
+}
+
fn always(paused: bool) -> PauseCheck {
Arc::new(move || paused)
}
@@ -537,7 +546,7 @@ async fn a_pause_that_lands_after_the_check_stops_the_run_at_the_next_batch() {
let counter = asked.clone();
let paused: PauseCheck = Arc::new(move || counter.fetch_add(1, Ordering::SeqCst) > 0);
- assert!(resume_interrupted_with(&config, paused).await);
+ assert!(resume_interrupted_with(&config, paused, billing(false)).await);
wait_until_no_live_run(&config).await;
assert!(asked.load(Ordering::SeqCst) >= 2, "the run asked again");
assert!(
@@ -549,7 +558,7 @@ async fn a_pause_that_lands_after_the_check_stops_the_run_at_the_next_batch() {
assert_eq!(file.checkpoint.documents.as_deref(), Some("d1"));
// Unpaused, the next tick finishes it from the checkpoint.
- assert!(resume_interrupted_with(&config, always(false)).await);
+ assert!(resume_interrupted_with(&config, always(false), billing(false)).await);
assert_eq!(wait_until_settled(&config).await.phase, ImportPhase::Done);
assert_eq!(stored(&engine, MetaFilter::default()).await.len(), 4);
}
@@ -562,7 +571,7 @@ async fn an_automatic_resume_that_cannot_start_is_stopped_not_retried() {
// No engine bound: memory is off, so `start` fails before any run.
quit_mid_import(&config);
- assert!(!resume_interrupted_with(&config, always(false)).await);
+ assert!(!resume_interrupted_with(&config, always(false), billing(false)).await);
let state = read_file(&config.workspace_dir).state;
assert_eq!(state.phase, ImportPhase::Error);
assert_eq!(state.imported, 1, "progress is kept");
@@ -571,7 +580,7 @@ async fn an_automatic_resume_that_cannot_start_is_stopped_not_retried() {
"{state:?}"
);
// The next tick does not try again; the user resumes it.
- assert!(!resume_interrupted_with(&config, always(false)).await);
+ assert!(!resume_interrupted_with(&config, always(false), billing(false)).await);
assert_eq!(
read_file(&config.workspace_dir)
.checkpoint
@@ -604,6 +613,7 @@ async fn a_resumed_import_keeps_its_total_instead_of_rescanning() {
write_file(
&config.workspace_dir,
&ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase: ImportPhase::Error,
imported: 1,
@@ -630,6 +640,7 @@ async fn a_resumed_import_keeps_its_total_instead_of_rescanning() {
fn the_import_state_is_written_whole_and_leaves_no_staging_file() {
let tmp = tempfile::tempdir().unwrap();
let file = ImportFile {
+ paused_for_credits: false,
state: ImportState {
phase: ImportPhase::Running,
imported: 3,
@@ -702,8 +713,59 @@ async fn an_engine_that_stays_unavailable_is_resumed_by_the_background_job() {
);
let engine = bind_reference(&config);
- assert!(resume_interrupted_with(&config, always(false)).await);
+ assert!(resume_interrupted_with(&config, always(false), billing(false)).await);
let done = wait_until_settled(&config).await;
assert_eq!(done.phase, ImportPhase::Done, "{done:?}");
assert_eq!(stored(&engine, MetaFilter::default()).await.len(), 5);
}
+
+#[tokio::test]
+async fn an_import_out_of_credits_resumes_only_once_automatic_runs_are_allowed() {
+ 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();
+ let stopped = wait_until_settled(&config).await;
+ assert_eq!(stopped.phase, ImportPhase::Error, "{stopped:?}");
+ assert!(read_file(&config.workspace_dir).paused_for_credits);
+
+ // Credits still out (no free period): left paused.
+ let engine = bind_reference(&config);
+ assert!(!resume_interrupted_with(&config, always(false), billing(false)).await);
+ assert_eq!(status(&config).phase, ImportPhase::Error);
+
+ // Automatic runs allowed again: the background job resumes it.
+ assert!(resume_interrupted_with(&config, always(false), billing(true)).await);
+ let done = wait_until_settled(&config).await;
+ assert_eq!(done.phase, ImportPhase::Done, "{done:?}");
+ assert_eq!(stored(&engine, MetaFilter::default()).await.len(), 5);
+ assert!(!read_file(&config.workspace_dir).paused_for_credits);
+}
+
+#[tokio::test]
+async fn a_stop_that_is_not_about_credits_is_not_resumed_by_billing() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ legacy_workspace(&config.workspace_dir);
+ bind_failing(&config, |_| {
+ Some(tinymemory_api::Error::Unauthorized("sign in".into()))
+ });
+ start(&config, true).await.unwrap();
+ assert_eq!(wait_until_settled(&config).await.phase, ImportPhase::Error);
+ assert!(!read_file(&config.workspace_dir).paused_for_credits);
+ assert!(!resume_interrupted_with(&config, always(false), billing(true)).await);
+}
+
+#[test]
+fn only_a_self_hosted_engine_runs_automatically_until_the_free_period_check_lands() {
+ let tmp = tempfile::tempdir().unwrap();
+ let mut config = config_in(&tmp);
+ config.memory.engine = crate::memory::engine::CORTEXDB_ENGINE.to_string();
+ assert!(automatic_run_allowed(&config));
+ config.memory.engine = crate::memory::engine::TINYHUMANS_ENGINE.to_string();
+ assert!(
+ !automatic_run_allowed(&config),
+ "unknown free period is not free"
+ );
+}