Skip to content

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

feat(core): allow memory subsystem rebind and fix migration cancellation

Steven Enamakel63e1d9042b5a
diff --git a/app/src/components/settings/panels/MemoryEnginePanel.tsx b/app/src/components/settings/panels/MemoryEnginePanel.tsx
index 375bbdb818..70e9a71272 100644
--- a/app/src/components/settings/panels/MemoryEnginePanel.tsx
+++ b/app/src/components/settings/panels/MemoryEnginePanel.tsx
@@ -280,6 +280,7 @@ export default function MemoryEnginePanel() {
setSaving(false);
if (status.state === 'done') {
log('migrate done job=%s copied=%d', jobId, status.copied);
+ invalidateMemoryEngine();
await finishSwitch(targetDriverRef.current);
} else if (status.state === 'cancelled') {
log('migrate cancelled job=%s', jobId);
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index b937ba79c0..a614a87c68 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -447,13 +447,18 @@ impl CoreContext {
.ok()
.map(|handle| Arc::clone(&**handle));
let parent = parent_handle.as_ref().and_then(|h| h.read().ok());
- match parent {
- Some(parent)
+ match (parent_handle.as_ref(), parent) {
+ (Some(handle), Some(parent))
if parent.workspace_dir.as_deref()
== Some(overlay.config.workspace_dir.as_path())
- && parent.memory_subsystem == overlay.config.subsystems.memory =>
+ && (parent.memory_subsystem == overlay.config.subsystems.memory
+ || self.embedder_config.as_ref().is_some_and(|config| {
+ config.workspace_dir == overlay.config.workspace_dir
+ && config.subsystems.memory
+ == overlay.config.subsystems.memory
+ })) =>
{
- Arc::clone(parent_handle.as_ref().expect("parent handle was read"))
+ Arc::clone(handle)
}
_ => Arc::new(RwLock::new(WorkspaceBinding {
workspace_dir: Some(overlay.config.workspace_dir.clone()),
@@ -695,13 +700,24 @@ impl CoreContext {
let binding = binding_handle
.read()
.map_err(|e| format!("workspace rebind failed: binding lock poisoned: {e}"))?;
- if binding.workspace_dir.as_deref() == Some(workspace_dir)
- && binding.memory_subsystem == memory_subsystem
- {
- log::debug!(
- "[core-context] workspace {} already bound with the current subsystem config",
- workspace_dir.display()
- );
+ if binding.workspace_dir.as_deref() == Some(workspace_dir) {
+ if binding.memory_subsystem != memory_subsystem {
+ log::info!(
+ "[core-context] rebound memory subsystem for {} to driver='{}'",
+ workspace_dir.display(),
+ memory_subsystem.driver
+ );
+ drop(binding);
+ binding_handle
+ .write()
+ .map_err(|e| format!("workspace rebind failed: binding lock poisoned: {e}"))?
+ .memory_subsystem = memory_subsystem;
+ } else {
+ log::debug!(
+ "[core-context] workspace {} already bound with the current subsystem config",
+ workspace_dir.display()
+ );
+ }
return Ok(());
}
log::info!(
diff --git a/crates/openhuman-core/src/memory/ops/engine_migrate.rs b/crates/openhuman-core/src/memory/ops/engine_migrate.rs
index c76bb62c6a..c80a10da7e 100644
--- a/crates/openhuman-core/src/memory/ops/engine_migrate.rs
+++ b/crates/openhuman-core/src/memory/ops/engine_migrate.rs
@@ -103,9 +103,14 @@ pub struct MigrateStatus {
}
/// Cancel signals of the jobs in [`JOBS`].
-static CANCELS: OnceLock<Mutex<HashMap<String, Arc<Notify>>>> = OnceLock::new();
+struct CancelSlot {
+ notify: Arc<Notify>,
+ requested: bool,
+}
+
+static CANCELS: OnceLock<Mutex<HashMap<String, CancelSlot>>> = OnceLock::new();
-fn cancels() -> std::sync::MutexGuard<'static, HashMap<String, Arc<Notify>>> {
+fn cancels() -> std::sync::MutexGuard<'static, HashMap<String, CancelSlot>> {
CANCELS
.get_or_init(Default::default)
.lock()
@@ -164,7 +169,10 @@ fn start_job() -> Result<String, String> {
note: None,
},
);
- cancels().insert(job_id.clone(), Arc::new(Notify::new()));
+ cancels().insert(
+ job_id.clone(),
+ CancelSlot { notify: Arc::new(Notify::new()), requested: false },
+ );
Ok(job_id)
}
@@ -223,7 +231,10 @@ async fn run_job<F, Fut>(
F: FnOnce() -> Fut,
Fut: Future<Output = Result<(), String>>,
{
- let cancel = cancels().get(&job_id).cloned().unwrap_or_default();
+ let cancel = cancels()
+ .get(&job_id)
+ .map(|slot| Arc::clone(&slot.notify))
+ .unwrap_or_default();
let progress_id = job_id.clone();
let copy = run_copy(source.as_ref(), target.as_ref(), move |records| {
update_job(&progress_id, |j| j.copied = records);
@@ -263,7 +274,13 @@ async fn run_job<F, Fut>(
CopyEnd::Finished(Ok(report)) => {
// The copy is past its cancellation point. Removing the signal
// makes concurrent cancel requests report `cancelled: false`.
- cancels().remove(&job_id);
+ if cancels()
+ .remove(&job_id)
+ .is_some_and(|slot| slot.requested)
+ {
+ update_job(&job_id, |j| j.state = "cancelled".to_string());
+ return;
+ }
update_job(&job_id, |j| j.copied = report.records);
match commit().await {
Ok(()) => {
@@ -375,8 +392,9 @@ pub async fn memory_engine_migrate_cancel(
.map(|j| j.state == "running")
.ok_or_else(|| "unknown migration job".to_string())?;
let cancelled = running && {
- if let Some(signal) = cancels().get(job_id) {
- signal.notify_one();
+ if let Some(slot) = cancels().get_mut(job_id) {
+ slot.requested = true;
+ slot.notify.notify_one();
true
} else {
false
diff --git a/scripts/mock-api/routes/memory.mjs b/scripts/mock-api/routes/memory.mjs
index 286500e489..e760700e49 100644
--- a/scripts/mock-api/routes/memory.mjs
+++ b/scripts/mock-api/routes/memory.mjs
@@ -141,7 +141,15 @@ export async function handleMemory(ctx) {
const text = String(textOf(body) ?? "");
const seen = store.idempotency.get(key);
if (seen) {
- if (seen.text !== text) {
+ const scope = String(body.scope || "");
+ const modality = String(body.modality || "");
+ const content = body.content ?? {};
+ if (
+ seen.text !== text ||
+ seen.scope !== scope ||
+ seen.modality !== modality ||
+ JSON.stringify(seen.content) !== JSON.stringify(content)
+ ) {
fail(res, 409, "IDEMPOTENCY_CONFLICT", "idempotency key reused");
return true;
}
@@ -154,10 +162,10 @@ export async function handleMemory(ctx) {
store.idempotency.set(key, { text, id });
store.events.push({
id,
- scope: String(body.scope || ""),
- modality: String(body.modality || ""),
+ scope,
+ modality,
wal_offset: store.nextOffset,
- content: body.content ?? {},
+ content,
context: { recorded_at: new Date().toISOString() },
});
ok(res, { event_id: id, status: "captured", replayed_from_idempotency: false });