Skip to content

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

test(memory-engine): add migration cancel, credit error, and config safety tests

Steven Enamakel4492d6972fdd
diff --git a/crates/openhuman-cli/Cargo.toml b/crates/openhuman-cli/Cargo.toml
index 95ecbf4279..5d4c56beb4 100644
--- a/crates/openhuman-cli/Cargo.toml
+++ b/crates/openhuman-cli/Cargo.toml
@@ -277,6 +277,12 @@ path = "../../tests/memory_engine_e2e.rs"
# Needs the memory module (the migration source) and the remote-engine gate.
required-features = ["modules", "memory-remote"]
+[[test]]
+# Engine migration, cancel, credits and config safety.
+name = "memory_engine_migrate_e2e"
+path = "../../tests/memory_engine_migrate_e2e.rs"
+required-features = ["modules", "memory-remote"]
+
[[test]]
name = "memory_roundtrip_e2e"
path = "../../tests/memory_roundtrip_e2e.rs"
diff --git a/tests/memory_engine_e2e.rs b/tests/memory_engine_e2e.rs
index 63f520b2fc..28cd11c171 100644
--- a/tests/memory_engine_e2e.rs
+++ b/tests/memory_engine_e2e.rs
@@ -238,11 +238,10 @@ fn engine_set_binds_the_hosted_engine_without_a_restart_and_switches_back() {
}),
)
.await;
- // Either the RPC is unregistered for this capability set or the handler
- // reports the missing family; both are clean errors.
assert!(
- doc.get("error").is_some(),
- "doc_put on the hosted engine: {doc}"
+ error_message(&doc, "doc_put on the hosted engine")
+ .contains("memory driver does not support the documents family"),
+ "doc_put must name the missing family: {doc}"
);
// And back to the module, live.
@@ -325,3 +324,37 @@ fn engine_set_validates_and_never_stores_or_returns_a_key_in_config() {
});
}
+#[test]
+fn hosted_failures_on_ordinary_memory_rpcs_use_the_engine_error_vocabulary() {
+ run_on_big_stack("engine-classify", || async {
+ let fx = Fixture::new().await;
+ fx.call(
+ "openhuman.memory_engine_set",
+ json!({ "driver": "tinyhumans" }),
+ )
+ .await;
+
+ fx.hosted.force_status.store(402, Ordering::SeqCst);
+ let v = fx.call("openhuman.memory_list_namespaces", json!({})).await;
+ let message = error_message(&v, "list_namespaces on 402");
+ assert!(
+ message.starts_with("INSUFFICIENT_CREDITS:"),
+ "a hosted 402 must read INSUFFICIENT_CREDITS: {message}"
+ );
+
+ fx.hosted.force_status.store(401, Ordering::SeqCst);
+ let v = fx.call("openhuman.memory_list_namespaces", json!({})).await;
+ let message = error_message(&v, "list_namespaces on 401");
+ assert!(
+ message.starts_with("SESSION_EXPIRED:"),
+ "a hosted 401 must read SESSION_EXPIRED: {message}"
+ );
+
+ fx.hosted.force_status.store(0, Ordering::SeqCst);
+ fx.call(
+ "openhuman.memory_engine_set",
+ json!({ "driver": "tinymemory" }),
+ )
+ .await;
+ });
+}
diff --git a/tests/memory_engine_migrate_e2e.rs b/tests/memory_engine_migrate_e2e.rs
index d799c4604e..cd2359e976 100644
--- a/tests/memory_engine_migrate_e2e.rs
+++ b/tests/memory_engine_migrate_e2e.rs
@@ -115,3 +115,108 @@ fn insufficient_credits_fails_the_migration_and_keeps_the_active_engine() {
assert_eq!(state["class"], "module");
});
}
+
+/// Seed enough records that a delayed hosted write keeps a migration running.
+async fn seed_and_start_slow_migration(fx: &Fixture) -> String {
+ for i in 0..3 {
+ fx.put_doc(&format!("slow-{i}"), &format!("slow record {i}"))
+ .await;
+ }
+ fx.hosted.delay_ms.store(700, Ordering::SeqCst);
+ let v = fx
+ .call(
+ "openhuman.memory_engine_migrate",
+ json!({ "to": { "driver": "tinyhumans" } }),
+ )
+ .await;
+ result_of(&v, "engine_migrate")["job_id"]
+ .as_str()
+ .expect("job_id")
+ .to_string()
+}
+
+#[test]
+fn cancel_stops_a_running_migration_and_keeps_the_active_engine() {
+ run_on_big_stack("engine-migrate-cancel", || async {
+ let fx = Fixture::new().await;
+ let job_id = seed_and_start_slow_migration(&fx).await;
+ tokio::time::sleep(Duration::from_millis(150)).await;
+
+ let v = fx
+ .call(
+ "openhuman.memory_engine_migrate_cancel",
+ json!({ "job_id": job_id }),
+ )
+ .await;
+ assert_eq!(result_of(&v, "cancel")["cancelled"], true);
+ let status = fx.wait_job(&job_id).await;
+ assert_eq!(status["state"], "cancelled", "{status}");
+ assert_eq!(
+ fx.state().await["driver"],
+ "tinymemory",
+ "a cancelled job must not switch"
+ );
+ fx.hosted.delay_ms.store(0, Ordering::SeqCst);
+ });
+}
+
+#[test]
+fn engine_set_is_refused_while_a_migration_runs() {
+ run_on_big_stack("engine-set-during-migrate", || async {
+ let fx = Fixture::new().await;
+ let job_id = seed_and_start_slow_migration(&fx).await;
+ tokio::time::sleep(Duration::from_millis(100)).await;
+
+ let v = fx
+ .call(
+ "openhuman.memory_engine_set",
+ json!({ "driver": "tinymemory" }),
+ )
+ .await;
+ assert!(
+ error_message(&v, "engine_set during migration").contains("migration is running"),
+ "{v}"
+ );
+ fx.call(
+ "openhuman.memory_engine_migrate_cancel",
+ json!({ "job_id": job_id.clone() }),
+ )
+ .await;
+ fx.wait_job(&job_id).await;
+ fx.hosted.delay_ms.store(0, Ordering::SeqCst);
+ });
+}
+
+#[test]
+fn a_config_edited_during_the_copy_survives_the_switch() {
+ run_on_big_stack("engine-migrate-stale-config", || async {
+ let fx = Fixture::new().await;
+ let job_id = seed_and_start_slow_migration(&fx).await;
+
+ // Edit an unrelated setting on disk while the copy runs. The commit must
+ // reload the config and patch only `[subsystems.memory]`, not write back
+ // the snapshot taken when the RPC started.
+ let path = shared_config_path();
+ let mut toml = std::fs::read_to_string(&path).unwrap();
+ toml = toml.replace(
+ "default_model = \"e2e-mock-model\"",
+ "default_model = \"edited-during-copy\"",
+ );
+ std::fs::write(&path, &toml).unwrap();
+
+ let status = fx.wait_job(&job_id).await;
+ assert_eq!(status["state"], "done", "{status}");
+ let after = std::fs::read_to_string(&path).unwrap();
+ assert!(
+ after.contains("edited-during-copy"),
+ "the commit clobbered a concurrent edit:\n{after}"
+ );
+ assert!(after.contains("driver = \"tinyhumans\""), "{after}");
+ fx.hosted.delay_ms.store(0, Ordering::SeqCst);
+ fx.call(
+ "openhuman.memory_engine_set",
+ json!({ "driver": "tinymemory" }),
+ )
+ .await;
+ });
+}
diff --git a/tests/support/memory_engine_fixture.rs b/tests/support/memory_engine_fixture.rs
index e871c71707..3f7bed1c7d 100644
--- a/tests/support/memory_engine_fixture.rs
+++ b/tests/support/memory_engine_fixture.rs
@@ -275,12 +275,12 @@ pub struct EnvVarGuard {
}
impl EnvVarGuard {
- pub fnset_to_path(key: &'static str, path: &Path) -> Self {
+ pub fn set_to_path(key: &'static str, path: &Path) -> Self {
let old = std::env::var(key).ok();
std::env::set_var(key, path.as_os_str());
Self { key, old }
}
- pub fnunset(key: &'static str) -> Self {
+ pub fn unset(key: &'static str) -> Self {
let old = std::env::var(key).ok();
std::env::remove_var(key);
Self { key, old }
@@ -288,7 +288,7 @@ impl EnvVarGuard {
}
impl Drop for EnvVarGuard {
- pub fndrop(&mut self) {
+ fn drop(&mut self) {
match &self.old {
Some(v) => std::env::set_var(self.key, v),
None => std::env::remove_var(self.key),
@@ -436,7 +436,7 @@ pub struct Fixture {
}
impl Fixture {
- pub async fnnew() -> Self {
+ pub async fn new() -> Self {
let lock = env_lock();
let tmp = tempdir().expect("home tempdir");
let guards = vec![
@@ -485,17 +485,17 @@ impl Fixture {
}
}
- pub async fncall(&self, method: &str, params: Value) -> Value {
+ pub async fn call(&self, method: &str, params: Value) -> Value {
rpc(&self.base, method, params).await
}
- pub async fnstate(&self) -> Value {
+ pub async fn state(&self) -> Value {
let v = self.call("openhuman.memory_engine_get", json!({})).await;
result_of(&v, "engine_get").clone()
}
/// Poll a migration job to a terminal state.
- pub async fnwait_job(&self, job_id: &str) -> Value {
+ pub async fn wait_job(&self, job_id: &str) -> Value {
for _ in 0..200 {
let v = self
.call(
@@ -512,7 +512,7 @@ impl Fixture {
panic!("migration job {job_id} did not finish");
}
- pub async fnput_doc(&self, key: &str, content: &str) {
+ pub async fn put_doc(&self, key: &str, content: &str) {
let v = self
.call(
"openhuman.memory_doc_put",
@@ -528,8 +528,7 @@ impl Fixture {
}
impl Drop for Fixture {
- pub fndrop(&mut self) {
+ fn drop(&mut self) {
self.join.abort();
}
}
-