Skip to content

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

refactor(memory): rework memory binding to use ambient context and hosted engine detection

Steven Enamakela029e43d21ed
diff --git a/crates/openhuman-core/src/agent/learning/startup.rs b/crates/openhuman-core/src/agent/learning/startup.rs
index 2f226cd035..0c1643c312 100644
--- a/crates/openhuman-core/src/agent/learning/startup.rs
+++ b/crates/openhuman-core/src/agent/learning/startup.rs
@@ -77,7 +77,19 @@ pub fn register_learning_subscribers(workspace_dir: std::path::PathBuf) {
/// configured off, which is the one state in which a rebuild loop has nothing
/// to rebuild against.
fn memory_is_bindable(workspace_dir: &Path) -> bool {
- match bound_memory(workspace_dir) {
+ use crate::config::schema::MemorySubsystemConfig;
+ // An explicit `driver = "null"` advertises no capabilities at all, which is
+ // how the context reports "memory is off" without exposing the binding.
+ if let Some(ctx) = ambient_context_for(workspace_dir) {
+ if ctx.memory_capabilities().iter().next().is_none() {
+ tracing::warn!(
+ "[learning::startup] memory is disabled for this workspace — learning subscribers will not register"
+ );
+ return false;
+ }
+ return true;
+ }
+ match crate::memory::binding::for_workspace(workspace_dir, &MemorySubsystemConfig::default()) {
Ok(binding) if binding.disables_memory() => {
tracing::warn!(
driver = %binding.driver_id(),
@@ -99,25 +111,17 @@ fn memory_is_bindable(workspace_dir: &Path) -> bool {
}
}
-/// The memory binding for `workspace_dir`, honouring the selected engine.
-///
-/// The ambient context carries the workspace's `[subsystems.memory]` config
-/// (including a memory-engine switch), so learning binds to the engine the user
-/// chose rather than always to the default module. Without a context for this
-/// workspace (a bare unit-test boot) it falls back to the default config.
+/// The ambient context serving `workspace_dir`, if any.
///
-/// Note the facet cache built from this is created once at boot; a later
-/// engine switch does not re-point it until the next start.
-fn bound_memory(
+/// It carries the workspace's `[subsystems.memory]` config (including a
+/// memory-engine switch), so learning binds to the engine the user chose rather
+/// than always to the default module. `None` means a bare boot with no context
+/// for this workspace (unit tests), which falls back to the default config.
+fn ambient_context_for(
workspace_dir: &Path,
-) -> Result<std::sync::Arc<crate::memory::binding::MemoryBinding>, String> {
- use crate::config::schema::MemorySubsystemConfig;
- if let Some(ctx) = crate::core::runtime::context::CoreContext::current() {
- if ctx.workspace_dir().ok().as_deref() == Some(workspace_dir) {
- return ctx.memory_binding();
- }
- }
- crate::memory::binding::for_workspace(workspace_dir, &MemorySubsystemConfig::default())
+) -> Option<std::sync::Arc<crate::core::runtime::context::CoreContext>> {
+ crate::core::runtime::context::CoreContext::current()
+ .filter(|ctx| ctx.workspace_dir().ok().as_deref() == Some(workspace_dir))
}
fn register_email_signature_once<F>(handle_cell: &OnceLock<Option<SubscriptionHandle>>, register: F)
@@ -150,7 +154,17 @@ where
fn facet_cache_for(
workspace_dir: &std::path::Path,
) -> Option<crate::agent::learning::cache::FacetCache> {
- match bound_memory(workspace_dir) {
+ use crate::config::schema::MemorySubsystemConfig;
+ if let Some(ctx) = ambient_context_for(workspace_dir) {
+ return match ctx.memory() {
+ Ok(guard) => Some(crate::agent::learning::cache::FacetCache::new(guard)),
+ Err(error) => {
+ tracing::warn!("[learning::startup] no memory binding for facet cache: {error}");
+ None
+ }
+ };
+ }
+ match crate::memory::binding::for_workspace(workspace_dir, &MemorySubsystemConfig::default()) {
Ok(binding) => Some(crate::agent::learning::cache::FacetCache::new(
binding.guard(),
)),
diff --git a/crates/openhuman-core/src/core/memory_cli.rs b/crates/openhuman-core/src/core/memory_cli.rs
index 2fe96de0e0..584e0c98e0 100644
--- a/crates/openhuman-core/src/core/memory_cli.rs
+++ b/crates/openhuman-core/src/core/memory_cli.rs
@@ -558,7 +558,8 @@ async fn create_memory_binding(
#[cfg(feature = "modules")]
crate::modules::memory::set_modules_policy(std::sync::Arc::new(config.clone()));
- crate::memory::binding::for_config(&config)
+ crate::memory::binding_remote::note_api_url(&config.workspace_dir, &config.api_url);
+ crate::memory::binding::for_workspace(&config.workspace_dir, &config.subsystems.memory)
.map_err(|error| anyhow::anyhow!(error))
}
diff --git a/crates/openhuman-core/src/memory/binding_rebind.rs b/crates/openhuman-core/src/memory/binding_rebind.rs
index 3b05aa9e8d..a474fd1a93 100644
--- a/crates/openhuman-core/src/memory/binding_rebind.rs
+++ b/crates/openhuman-core/src/memory/binding_rebind.rs
@@ -72,7 +72,7 @@ pub fn rebind(
{
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
- if let Err(e) = binding.unguarded_provider().shutdown().await {
+ if let Err(e) = binding.provider().shutdown().await {
log::debug!("[memory:binding] evicted driver shutdown failed: {e}");
}
});
diff --git a/crates/openhuman-core/src/memory/ops/engine.rs b/crates/openhuman-core/src/memory/ops/engine.rs
index a8500b88b0..6f467640b8 100644
--- a/crates/openhuman-core/src/memory/ops/engine.rs
+++ b/crates/openhuman-core/src/memory/ops/engine.rs
@@ -270,8 +270,9 @@ pub(super) fn prepare_target(params: EngineTargetParams) -> Result<Prepared, Str
_ => return Err("endpoint must be an http(s) URL".to_string()),
}
}
+ let hosted = entry.hosted || id == HOSTED_ENGINE_ID;
let deployment = blank_to_none(params.deployment);
- if let Some(deployment) = deployment.as_deref() {
+ if let Some(deployment) = deployment.as_deref().filter(|_| !hosted) {
if !entry.deployments.iter().any(|d| d == deployment) {
return Err(format!(
"deployment must be one of [{}]",
@@ -279,7 +280,6 @@ pub(super) fn prepare_target(params: EngineTargetParams) -> Result<Prepared, Str
));
}
}
- let hosted = entry.hosted || id == HOSTED_ENGINE_ID;
Ok(Prepared {
id: id.to_string(),
// First-party engines own their endpoint; a caller-supplied one is
diff --git a/crates/openhuman-core/src/memory/ops/engine_migrate.rs b/crates/openhuman-core/src/memory/ops/engine_migrate.rs
index c6ba251e7d..41e09eb131 100644
--- a/crates/openhuman-core/src/memory/ops/engine_migrate.rs
+++ b/crates/openhuman-core/src/memory/ops/engine_migrate.rs
@@ -160,7 +160,7 @@ pub async fn memory_engine_migrate(params: MigrateParams) -> Result<RpcOutcome<M
let target = if prepared.id == MODULE_ID {
let mut cfg = config.subsystems.memory.clone();
cfg.driver = MODULE_ID.to_string();
- Arc::clone(binding::for_workspace(&config.workspace_dir, &cfg)?.provider())
+ Arc::clone(binding::for_subtree(&config.workspace_dir, "memory", &cfg)?.provider())
} else {
build_target_provider(&config, &prepared)?
};
diff --git a/crates/openhuman-core/src/memory/ops/provider.rs b/crates/openhuman-core/src/memory/ops/provider.rs
index 4942333ed1..d38751959f 100644
--- a/crates/openhuman-core/src/memory/ops/provider.rs
+++ b/crates/openhuman-core/src/memory/ops/provider.rs
@@ -62,7 +62,8 @@ async fn standalone_status() -> SubsystemStatus {
}
};
- match crate::memory::binding::for_config(&config) {
+ crate::memory::binding_remote::note_api_url(&config.workspace_dir, &config.api_url);
+ match crate::memory::binding::for_workspace(&config.workspace_dir, &config.subsystems.memory) {
Ok(binding) => status_from_binding(&binding).await,
Err(err) => {
log::debug!(
diff --git a/crates/openhuman-core/src/memory/schemas/provider_tests.rs b/crates/openhuman-core/src/memory/schemas/provider_tests.rs
index 296b8ca9c6..a6c0a43f1b 100644
--- a/crates/openhuman-core/src/memory/schemas/provider_tests.rs
+++ b/crates/openhuman-core/src/memory/schemas/provider_tests.rs
@@ -1,9 +1,22 @@
use super::*;
#[test]
-fn provider_schema_only_exposes_provider_status() {
- assert_eq!(FUNCTIONS, &["provider_status"]);
- assert_eq!(controllers().len(), 1);
+fn provider_family_exposes_status_and_the_engine_selector() {
+ assert_eq!(
+ FUNCTIONS,
+ &[
+ "provider_status",
+ "engines_list",
+ "engine_get",
+ "engine_set",
+ "engine_migrate",
+ "engine_migrate_status"
+ ]
+ );
+ assert_eq!(controllers().len(), FUNCTIONS.len());
+ for function in FUNCTIONS {
+ assert!(schema(function).is_some(), "{function} has no schema");
+ }
}
#[test]