Skip to content

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

fix(learning): follow an engine switch with no context serving the workspace

Shanu4b2662b1f056
diff --git a/crates/openhuman-core/src/agent/learning/cache.rs b/crates/openhuman-core/src/agent/learning/cache.rs
index 52a6e51936..004f746dec 100644
--- a/crates/openhuman-core/src/agent/learning/cache.rs
+++ b/crates/openhuman-core/src/agent/learning/cache.rs
@@ -82,8 +82,9 @@ impl FacetCache {
/// guard keeps that driver after an engine switch, since a switch re-points
/// the workspace's context and only a new resolution sees the new engine
/// (`memory::binding_rebind`). Each call goes through the context serving
- /// the workspace when there is one, so it follows a switch, and otherwise
- /// through the binding for `memory`, the workspace's own
+ /// the workspace when there is one, so it follows a switch. Without one it
+ /// takes the binding cached for the workspace, which a switch replaces, and
+ /// only with nothing cached the binding for `memory`, the workspace's own
/// `[subsystems.memory]`. That is the case at boot, before the context
/// exists, where the default config would bind the local engine whatever
/// the user chose.
@@ -182,15 +183,25 @@ impl FacetCache {
}
}
-/// The guarded driver serving `dir` now: through the context that serves the
-/// workspace when there is one, which an engine switch re-points, else through
-/// the binding for `memory`.
+/// The guarded driver serving `dir` now.
+///
+/// Through the context that serves the workspace when there is one, which an
+/// engine switch re-points. Else through the binding cached for the workspace,
+/// which a switch replaces: `memory` was the config when the cache was built,
+/// and binding it again after a switch would rebuild the evicted engine. Only
+/// with nothing cached, as at boot before the first binding, does `memory`
+/// itself decide.
fn workspace_guard(dir: &Path, memory: &MemorySubsystemConfig) -> anyhow::Result<Arc<MemoryGuard>> {
let context = crate::core::runtime::context::CoreContext::current()
.filter(|ctx| ctx.workspace_dir().ok().as_deref() == Some(dir));
let guard = match context {
Some(ctx) => ctx.memory(),
- None => crate::memory::binding::for_workspace(dir, memory).map(|binding| binding.guard()),
+ None => match crate::memory::binding::current_for(dir) {
+ Some(binding) => Ok(binding.guard()),
+ None => {
+ crate::memory::binding::for_workspace(dir, memory).map(|binding| binding.guard())
+ }
+ },
};
guard.map_err(|error| anyhow::anyhow!("no memory binding for the facet cache: {error}"))
}
diff --git a/crates/openhuman-core/src/agent/learning/startup_tests.rs b/crates/openhuman-core/src/agent/learning/startup_tests.rs
index 855ac07350..f9879284b3 100644
--- a/crates/openhuman-core/src/agent/learning/startup_tests.rs
+++ b/crates/openhuman-core/src/agent/learning/startup_tests.rs
@@ -202,3 +202,36 @@ async fn a_long_lived_facet_cache_reads_the_engine_the_workspace_has_now() {
"a cache built before the switch must reach the engine bound after it"
);
}
+
+/// With no context serving the workspace, a switch still reaches the cache:
+/// it takes the binding the switch left, not the config it was built with,
+/// which would bind the engine switched away from again.
+#[tokio::test]
+async fn a_long_lived_facet_cache_follows_a_switch_made_without_a_context() {
+ let tmp = test_workspace();
+ let before = MemorySubsystemConfig {
+ driver: "null".into(),
+ ..MemorySubsystemConfig::default()
+ };
+ let first = Arc::new(RecordingProvider::new());
+ crate::memory::binding::install_for_test(tmp.path(), &before, first.clone());
+ let cache = facet_cache_for(tmp.path(), &before);
+ cache
+ .list_all()
+ .await
+ .expect("facets through the engine bound at first");
+ assert!(reached_profile(&first));
+
+ let after = chosen_engine();
+ let second = Arc::new(RecordingProvider::new());
+ crate::memory::binding::install_for_test(tmp.path(), &after, second.clone());
+ crate::memory::binding::rebind(tmp.path(), &before.driver, &after).expect("switch");
+ cache
+ .list_all()
+ .await
+ .expect("facets through the engine switched to");
+ assert!(
+ reached_profile(&second),
+ "the cache must follow the switch, not rebind the config it was built with"
+ );
+}
diff --git a/crates/openhuman-core/src/memory/binding.rs b/crates/openhuman-core/src/memory/binding.rs
index 12c8a10ac1..e739866948 100644
--- a/crates/openhuman-core/src/memory/binding.rs
+++ b/crates/openhuman-core/src/memory/binding.rs
@@ -596,7 +596,7 @@ pub(crate) fn install_for_test(
.insert(key, binding);
}
-pub use super::binding_rebind::rebind;
+pub use super::binding_rebind::{current_for, 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
index 136c8f21e5..2092f7acdf 100644
--- a/crates/openhuman-core/src/memory/binding_rebind.rs
+++ b/crates/openhuman-core/src/memory/binding_rebind.rs
@@ -11,6 +11,36 @@ use crate::core::subsystem::DriverClass;
use super::binding::{for_workspace, MemoryBinding, BINDINGS};
+/// The binding `workspace_dir`'s shared memory tree resolves to now, when the
+/// cache can tell.
+///
+/// A switch ([`rebind`]) evicts every other binding of the workspace and binds
+/// the new engine, so after one the cache holds a single binding for it: the
+/// current one. Before any switch that is the one boot bound. `None` when
+/// nothing is cached for the workspace, or when bindings under more than one
+/// config are cached and none of them can be told current.
+///
+/// For a caller holding a config from earlier, which may name an engine
+/// switched away from since: binding that config again would rebuild the
+/// evicted driver.
+pub fn current_for(workspace_dir: &Path) -> Option<Arc<MemoryBinding>> {
+ let cache = BINDINGS.get()?;
+ let map = cache.read().ok()?;
+ let mut bound = map
+ .iter()
+ .filter(|((dir, subdir, _), _)| dir == workspace_dir && subdir == "memory")
+ .map(|(_, binding)| binding);
+ let current = bound.next()?;
+ if bound.next().is_some() {
+ log::debug!(
+ "[memory:binding] workspace={} has bindings under more than one config; none is current",
+ workspace_dir.display()
+ );
+ return None;
+ }
+ Some(Arc::clone(current))
+}
+
/// 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.