Skip to content

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

fix(memory): prevent panic on rebind of unbound binding

Steven Enamakelffc0361adbc5
diff --git a/crates/openhuman-core/src/memory/binding.rs b/crates/openhuman-core/src/memory/binding.rs
index 79651c83bb..f6b62633d2 100644
--- a/crates/openhuman-core/src/memory/binding.rs
+++ b/crates/openhuman-core/src/memory/binding.rs
@@ -525,8 +525,8 @@ pub(crate) fn bind_provider_for_test(
/// Keyed by workspace **and memory subtree**: a profile that opted into
/// dedicated memory is a different store, so it must be a different binding.
/// The subtree is `"memory"` for every ordinary caller.
-type BindingCacheKey = (PathBuf, String, MemorySubsystemConfig);
-static BINDINGS: OnceLock<RwLock<HashMap<BindingCacheKey, Arc<MemoryBinding>>>> = OnceLock::new();
+pub(super) type BindingCacheKey = (PathBuf, String, MemorySubsystemConfig);
+pub(super) static BINDINGS: OnceLock<RwLock<HashMap<BindingCacheKey, Arc<MemoryBinding>>>> = OnceLock::new();
/// Every binding this process has built so far, for the exit path.
///
@@ -718,89 +718,7 @@ 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)
-}
+pub use super::binding_rebind::rebind;
/// The bound memory driver for the workspace a whole [`Config`] names.
///
diff --git a/crates/openhuman-core/src/memory/binding_rebind.rs b/crates/openhuman-core/src/memory/binding_rebind.rs
new file mode 100644
index 0000000000..3b05aa9e8d
--- /dev/null
+++ b/crates/openhuman-core/src/memory/binding_rebind.rs
@@ -0,0 +1,96 @@
+//! In-process engine switch: [`rebind`].
+//!
+//! Split from [`crate::memory::binding`] to keep that file within the layout
+//! limit; it owns no state of its own and works on the binding cache there.
+
+use std::path::Path;
+use std::sync::Arc;
+
+use crate::config::schema::MemorySubsystemConfig;
+use crate::core::subsystem::DriverClass;
+
+use super::binding::{for_workspace, MemoryBinding, BINDINGS};
+
+/// 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)
+}
diff --git a/crates/openhuman-core/src/memory/mod.rs b/crates/openhuman-core/src/memory/mod.rs
index 7f5d9434b8..e2715f11b7 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_rebind;
pub mod binding_remote;
pub mod driver;
pub mod exit;