Skip to content

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

fix(memory): correct memory binding context after runtime restructure

Steven Enamakel44f8e9e21885
diff --git a/crates/openhuman-core/src/core/runtime/context.rs b/crates/openhuman-core/src/core/runtime/context.rs
index d732b794d3..9646873b0f 100644
--- a/crates/openhuman-core/src/core/runtime/context.rs
+++ b/crates/openhuman-core/src/core/runtime/context.rs
@@ -625,6 +625,33 @@ impl CoreContext {
ctx.rebind_workspace(workspace_dir, memory_subsystem)
}
+ /// Replace only the `[subsystems.memory]` half of this context's workspace
+ /// binding, keeping the workspace dir. The memory-engine switch
+ /// (`memory::binding::rebind`) uses it so [`Self::memory_binding`] resolves
+ /// the new driver without a restart. A no-op when the config is unchanged.
+ pub(crate) fn set_memory_subsystem(
+ &self,
+ workspace_dir: &std::path::Path,
+ memory_subsystem: crate::config::schema::MemorySubsystemConfig,
+ ) -> Result<(), String> {
+ let mut binding = self
+ .workspace_binding
+ .write()
+ .map_err(|e| format!("memory subsystem update failed: binding lock poisoned: {e}"))?;
+ if binding.workspace_dir.as_deref() != Some(workspace_dir)
+ || binding.memory_subsystem == memory_subsystem
+ {
+ return Ok(());
+ }
+ log::info!(
+ "[core-context] memory subsystem driver set to '{}' for {}",
+ memory_subsystem.driver,
+ workspace_dir.display()
+ );
+ binding.memory_subsystem = memory_subsystem;
+ Ok(())
+ }
+
fn rebind_workspace(
&self,
workspace_dir: &std::path::Path,
diff --git a/crates/openhuman-core/src/memory/binding.rs b/crates/openhuman-core/src/memory/binding.rs
index e648e0a85f..afda11d4a0 100644
--- a/crates/openhuman-core/src/memory/binding.rs
+++ b/crates/openhuman-core/src/memory/binding.rs
@@ -718,6 +718,90 @@ pub(crate) fn install_for_test(
.insert(key, binding);
}
+/// Switch a workspace to `new_cfg` in process: evict the bindings the switch
+/// invalidates, re-point every context that serves the workspace, bind the new
+/// driver and announce it.
+///
+/// What is evicted: every cached binding of `workspace_dir` that is not a
+/// built-in (`Module`/`Null`) bound under exactly `new_cfg`. An `External`
+/// binding is evicted even when its config is unchanged, because the thing that
+/// changed may be the keychain entry behind `credential_ref`, which the cache
+/// key cannot see. Evicted `External` drivers are shut down in the background;
+/// the module is never shut down here — it is process-global and a switch back
+/// to it must find it running.
+///
+/// Callers that already hold a resolved `Arc<MemoryBinding>` (a running agent
+/// session, the archivist) keep it until they resolve again; only new
+/// resolutions see the new engine.
+///
+/// # Errors
+///
+/// Lock poisoning, or memory shutting down. A driver that cannot bind is not an
+/// error — it falls back, per kernel.md §3.7, and the returned binding says so.
+pub fn rebind(
+ workspace_dir: &Path,
+ from: &str,
+ new_cfg: &MemorySubsystemConfig,
+) -> Result<Arc<MemoryBinding>, String> {
+ let evicted: Vec<Arc<MemoryBinding>> = {
+ let cache = BINDINGS.get_or_init(Default::default);
+ let mut map = cache
+ .write()
+ .map_err(|e| format!("[memory:binding] cache write lock poisoned: {e}"))?;
+ let mut evicted = Vec::new();
+ map.retain(|(dir, _subdir, cfg), binding| {
+ if dir != workspace_dir {
+ return true;
+ }
+ let builtin = matches!(binding.class(), DriverClass::Module | DriverClass::Null);
+ if builtin && cfg == new_cfg {
+ return true;
+ }
+ evicted.push(Arc::clone(binding));
+ false
+ });
+ evicted
+ };
+
+ for ctx in [
+ crate::core::runtime::context::CoreContext::current(),
+ crate::core::runtime::context::CoreContext::default_context(),
+ ]
+ .into_iter()
+ .flatten()
+ {
+ ctx.set_memory_subsystem(workspace_dir, new_cfg.clone())?;
+ }
+
+ for binding in evicted
+ .into_iter()
+ .filter(|b| b.class() == DriverClass::External)
+ {
+ if let Ok(handle) = tokio::runtime::Handle::try_current() {
+ handle.spawn(async move {
+ if let Err(e) = binding.unguarded_provider().shutdown().await {
+ log::debug!("[memory:binding] evicted driver shutdown failed: {e}");
+ }
+ });
+ }
+ }
+
+ let binding = for_workspace(workspace_dir, new_cfg)?;
+ log::info!(
+ "[memory:binding] rebound workspace={} from='{}' to='{}' bound='{}' class={}",
+ workspace_dir.display(),
+ from,
+ new_cfg.driver,
+ binding.driver_id(),
+ binding.class()
+ );
+ crate::core::bus::BUS.publish(crate::core::events::DomainEvent::MemoryDriverChanged {
+ from: from.to_string(),
+ to: new_cfg.driver.clone(),
+ });
+ Ok(binding)
+}
+
/// The bound memory driver for the workspace a whole [`Config`] names.
///
/// The two pieces [`for_workspace`] needs sit in different halves of `Config`,
diff --git a/crates/openhuman-core/src/memory/mod.rs b/crates/openhuman-core/src/memory/mod.rs
index ce8a5994cd..7f5d9434b8 100644
--- a/crates/openhuman-core/src/memory/mod.rs
+++ b/crates/openhuman-core/src/memory/mod.rs
@@ -44,6 +44,7 @@ pub mod api;
/// how much a block may carry, and the switch are product decisions.
pub mod auto_recall;
pub mod binding;
+pub mod binding_remote;
pub mod driver;
pub mod exit;
pub mod guard;
diff --git a/vendor/tinymemory b/vendor/tinymemory
index e15f6766f9..596eca729a 160000
--- a/vendor/tinymemory
+++ b/vendor/tinymemory
@@ -1 +1 @@
-Subproject commit e15f6766f9e244d7bdff3bcad91f4903879a3750
+Subproject commit 596eca729a60a8b2074b0d6a496f5f938c67ddfc