Skip to content

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

fix(memory): retry an expired fallback when resolving the current binding

Shanu4e93a4e25a14
diff --git a/crates/openhuman-core/src/agent/learning/cache.rs b/crates/openhuman-core/src/agent/learning/cache.rs
index 004f746dec..8dba322b1e 100644
--- a/crates/openhuman-core/src/agent/learning/cache.rs
+++ b/crates/openhuman-core/src/agent/learning/cache.rs
@@ -83,8 +83,8 @@ impl FacetCache {
/// 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. 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
+ /// resolves the config the workspace is bound under now, which a switch
+ /// replaces, and only with nothing bound `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.
@@ -186,22 +186,17 @@ 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 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.
+/// engine switch re-points. Else through the config the workspace is bound
+/// under now, 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 bound, as at boot before the first binding, does
+/// `memory` itself decide (`memory::binding::current_for`).
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 => match crate::memory::binding::current_for(dir) {
- Some(binding) => Ok(binding.guard()),
- None => {
- crate::memory::binding::for_workspace(dir, memory).map(|binding| binding.guard())
- }
- },
+ None => crate::memory::binding::current_for(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/memory/binding_rebind.rs b/crates/openhuman-core/src/memory/binding_rebind.rs
index 2092f7acdf..1f87de021a 100644
--- a/crates/openhuman-core/src/memory/binding_rebind.rs
+++ b/crates/openhuman-core/src/memory/binding_rebind.rs
@@ -1,4 +1,5 @@
-//! In-process engine switch: [`rebind`].
+//! In-process engine switch, [`rebind`], and the binding a workspace resolves
+//! to after one, [`current_for`].
//!
//! 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.
@@ -11,34 +12,47 @@ 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.
+/// The binding `workspace_dir`'s shared memory tree resolves to now.
///
/// 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>> {
+/// evicted driver. 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, under the config switched to. Before any switch that is the
+/// one boot bound. That config is resolved again through [`for_workspace`]
+/// rather than the cached binding returned, so a transient fallback is retried
+/// once its backoff has passed. `cfg` decides only 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.
+///
+/// # Errors
+///
+/// As [`for_workspace`].
+pub fn current_for(
+ workspace_dir: &Path,
+ cfg: &MemorySubsystemConfig,
+) -> Result<Arc<MemoryBinding>, String> {
+ let bound = bound_config(workspace_dir);
+ for_workspace(workspace_dir, bound.as_ref().unwrap_or(cfg))
+}
+
+/// The config of the one binding cached for `workspace_dir`'s shared tree.
+fn bound_config(workspace_dir: &Path) -> Option<MemorySubsystemConfig> {
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() {
+ let mut configs = map
+ .keys()
+ .filter(|(dir, subdir, _)| dir == workspace_dir && subdir == "memory")
+ .map(|(_, _, cfg)| cfg);
+ let bound = configs.next()?.clone();
+ if configs.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))
+ Some(bound)
}
/// Switch a workspace to `new_cfg` in process: evict the bindings the switch
diff --git a/crates/openhuman-core/src/memory/binding_remote_bind_tests.rs b/crates/openhuman-core/src/memory/binding_remote_bind_tests.rs
index 9e8ef9728c..ace439de59 100644
--- a/crates/openhuman-core/src/memory/binding_remote_bind_tests.rs
+++ b/crates/openhuman-core/src/memory/binding_remote_bind_tests.rs
@@ -167,6 +167,39 @@ mod transient_bind {
);
}
+ /// A caller holding a stale config resolves the engine the workspace is
+ /// bound to, and through `for_workspace`: an expired fallback is retried,
+ /// never served as it was cached.
+ #[tokio::test]
+ async fn the_current_binding_retries_an_expired_fallback() {
+ let dir = tempfile::tempdir().unwrap();
+ let cfg = bad_credential_cfg();
+ let key = (dir.path().to_path_buf(), "memory".to_string(), cfg.clone());
+ let expired = Arc::new(
+ crate::memory::binding_build::build(dir.path(), "memory", &cfg)
+ .retry_after(std::time::Duration::ZERO),
+ );
+ BINDINGS
+ .get_or_init(Default::default)
+ .write()
+ .unwrap()
+ .insert(key, Arc::clone(&expired));
+ let stale = MemorySubsystemConfig {
+ driver: "null".into(),
+ ..Default::default()
+ };
+ let current = current_for(dir.path(), &stale).unwrap();
+ assert_eq!(
+ current.fallback().unwrap().configured_driver,
+ "supermemory",
+ "the engine the workspace is bound to, not the caller's"
+ );
+ assert!(
+ !Arc::ptr_eq(&expired, &current),
+ "an expired fallback is rebuilt"
+ );
+ }
+
#[tokio::test]
async fn an_admission_refusal_is_not_transient() {
let dir = tempfile::tempdir().unwrap();