Skip to content

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

refactor(memory): the migration host's switch is async

M3gA-Mindc5517b84f0be
diff --git a/crates/openhuman-core/src/memory/layout_migration/host.rs b/crates/openhuman-core/src/memory/layout_migration/host.rs
index 9ad13f651e..fab9418eec 100644
--- a/crates/openhuman-core/src/memory/layout_migration/host.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/host.rs
@@ -33,12 +33,15 @@ pub trait LayoutHost: Send + Sync {
/// Whether reads and writes already use the per-user tree.
fn is_switched(&self, config: &Config) -> bool;
- /// Moves reads and writes to the per-user tree.
+ /// Moves reads and writes to the per-user tree: persists the layout
+ /// setting and drops the cached engines. The real host awaits
+ /// `memory::scope::switch_to_v3()`, which loads the config fresh, so a
+ /// setting the user changed during a long migration is never reverted.
///
/// # Errors
///
/// When the setting cannot be saved.
- fn switch(&self, config: &Config) -> MemoryResult<()>;
+ async fn switch(&self, config: &Config) -> MemoryResult<()>;
/// Whether moving memory costs the user nothing right now (always, off
/// the hosted engine).
diff --git a/crates/openhuman-core/src/memory/layout_migration/job.rs b/crates/openhuman-core/src/memory/layout_migration/job.rs
index cf619f6313..c28ab5c4d6 100644
--- a/crates/openhuman-core/src/memory/layout_migration/job.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/job.rs
@@ -77,7 +77,7 @@ where
}
let engines = host.engines(config)?;
if state.phase == Phase::Idle && !state.switched && !legacy_present(&*engines.legacy).await? {
- return nothing_to_move(config, host, &mut state);
+ return nothing_to_move(config, host, &mut state).await;
}
if let Trigger::Manual { takeover: true } = trigger {
state.takeover = true;
@@ -100,7 +100,7 @@ where
return Ok(Outcome::Paused);
}
if !host.is_switched(config) {
- host.switch(config)?;
+ host.switch(config).await?;
}
state.switched = true;
state.cursor = None;
@@ -123,13 +123,13 @@ where
})
}
-fn nothing_to_move(
+async fn nothing_to_move(
config: &Config,
host: &dyn LayoutHost,
state: &mut MigrationState,
) -> MemoryResult<Outcome> {
if !host.is_switched(config) {
- host.switch(config)?;
+ host.switch(config).await?;
}
state.switched = true;
state.caught_up = true;
diff --git a/crates/openhuman-core/src/memory/layout_migration/test_host.rs b/crates/openhuman-core/src/memory/layout_migration/test_host.rs
index c90ca18087..7a84dddbd5 100644
--- a/crates/openhuman-core/src/memory/layout_migration/test_host.rs
+++ b/crates/openhuman-core/src/memory/layout_migration/test_host.rs
@@ -87,10 +87,10 @@ impl LayoutHost for FakeHost {
self.switched.load(Ordering::SeqCst)
}
- fn switch(&self, _: &Config) -> MemoryResult<()> {
- if let Some(item) = self.racing_write.lock().unwrap().take() {
- // Synchronous on purpose: the reference engine stores in memory.
- futures::executor::block_on(self.legacy.store(item)).unwrap();
+ async fn switch(&self, _: &Config) -> MemoryResult<()> {
+ let racing = self.racing_write.lock().unwrap().take();
+ if let Some(item) = racing {
+ self.legacy.store(item).await.unwrap();
}
self.switched.store(true, Ordering::SeqCst);
Ok(())