Skip to content

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

refactor(memory): extract user scope resolution into its own module

Steven Enamakel572496180460
diff --git a/crates/openhuman-core/src/memory/explore.rs b/crates/openhuman-core/src/memory/explore.rs
index 91d3f5e95c..c10fe2cfe1 100644
--- a/crates/openhuman-core/src/memory/explore.rs
+++ b/crates/openhuman-core/src/memory/explore.rs
@@ -94,7 +94,7 @@ pub fn narrowed(filter: Option<MetaFilter>, path: &[PathStep]) -> MemoryResult<M
pub async fn explore(config: &Config, params: ExploreParams) -> MemoryResult<ExplorePage> {
let bound = engine::resolve(config).engine()?;
let mut request = ExploreRequest::new(params.facet, params.limit.unwrap_or(DEFAULT_BUCKETS));
- request.filter = super::ops::confine_filter(config, narrowed(params.filter, &params.path)?);
+ request.filter = super::ops::confine_filter(config, narrowed(params.filter, &params.path)?)?;
if let Some(scan_limit) = params.scan_limit {
request.scan_limit = scan_limit;
}
diff --git a/crates/openhuman-core/src/memory/ops.rs b/crates/openhuman-core/src/memory/ops.rs
index 89153348bf..e1f7e05954 100644
--- a/crates/openhuman-core/src/memory/ops.rs
+++ b/crates/openhuman-core/src/memory/ops.rs
@@ -158,7 +158,7 @@ pub async fn recall(config: &Config, params: RecallParams) -> MemoryResult<Recal
let bound = bound(config)?;
let request = RecallRequest {
question: params.question,
- filter: confine_filter(config, params.filter.unwrap_or_default()),
+ filter: confine_filter(config, params.filter.unwrap_or_default())?,
limit: clamp_limit(params.limit),
instructions: None,
refers_to: time_hint(config, params.refers_to)?,
@@ -195,7 +195,7 @@ pub async fn fetch(config: &Config, params: FetchParams) -> MemoryResult<FetchVi
let request = FetchRequest {
query: params.query,
mode,
- filter: confine_filter(config, params.filter.unwrap_or_default()),
+ filter: confine_filter(config, params.filter.unwrap_or_default())?,
limit: clamp_limit(params.limit),
cursor: params.cursor,
beliefs: 0,
@@ -292,7 +292,12 @@ pub async fn learn_with(
host_meta: Option<MemoryMeta>,
options: WriteOptions,
) -> MemoryResult<LearnView> {
- let item = learning_item(params, host_meta)?;
+ let mut item = learning_item(params, host_meta)?;
+ // The same confinement as `store_item`: a SaaS user's learning lands in
+ // their own tree whatever namespace it names.
+ if let Some(root) = super::user_scope::confinement(config)? {
+ super::user_scope::clamp_item(&mut item, &root);
+ }
let bound = bound(config)?;
let receipt = store_on_with(&bound, item, options).await?;
Ok(LearnView { id: receipt.id.0 })
@@ -300,7 +305,7 @@ pub async fn learn_with(
/// Scrubs `item` and stores it on the bound engine.
pub async fn store_item(config: &Config, mut item: StoreItem) -> MemoryResult<StoreReceipt> {
- if let Some(root) = super::user_scope::confinement(config) {
+ if let Some(root) = super::user_scope::confinement(config)? {
super::user_scope::clamp_item(&mut item, &root);
}
let bound = bound(config)?;
@@ -362,7 +367,7 @@ pub async fn forget(config: &Config, params: ForgetParams) -> MemoryResult<Forge
}
let bound = bound(config)?;
// A SaaS user forgets only what lies in their own tree.
- let reach = match super::user_scope::confinement(config) {
+ let reach = match super::user_scope::confinement(config)? {
Some(root) => Some(super::user_scope::clamp_reach(params.reach, &root)),
None => params.reach,
};
@@ -445,7 +450,7 @@ pub async fn items_list(config: &Config, params: ItemsListParams) -> MemoryResul
filter: confine_filter(
config,
super::explore::narrowed(params.filter, &params.path)?,
- ),
+ )?,
limit: clamp_limit(params.limit),
cursor: params.cursor,
};
@@ -463,11 +468,11 @@ pub async fn items_list(config: &Config, params: ItemsListParams) -> MemoryResul
/// `filter`, confined to the SaaS user's tree when `config` has one
/// ([`super::user_scope`]).
-pub(crate) fn confine_filter(config: &Config, filter: MetaFilter) -> MetaFilter {
- match super::user_scope::confinement(config) {
+pub(crate) fn confine_filter(config: &Config, filter: MetaFilter) -> MemoryResult<MetaFilter> {
+ Ok(match super::user_scope::confinement(config)? {
Some(root) => super::user_scope::clamp_filter(filter, &root),
None => filter,
- }
+ })
}
#[cfg(test)]
diff --git a/crates/openhuman-core/src/memory/user_scope.rs b/crates/openhuman-core/src/memory/user_scope.rs
index 78ec90e39a..f8cc4a7a0b 100644
--- a/crates/openhuman-core/src/memory/user_scope.rs
+++ b/crates/openhuman-core/src/memory/user_scope.rs
@@ -17,31 +17,34 @@
//! - every write lands inside the root: an item addressed elsewhere is moved
//! to the root itself.
//!
-//! Single-user processes and configs without a root are left alone.
+//! A SaaS config without a valid root makes memory refuse rather than reach
+//! everyone. Single-user processes are left alone.
use tinymemory_api::{MetaFilter, Namespace, Reach, StoreItem};
+use super::error::{MemoryError, MemoryResult};
use crate::config::Config;
-/// The root `config`'s memory is confined to, in SaaS mode.
-pub fn confinement(config: &Config) -> Option<Namespace> {
+/// The root `config`'s memory is confined to, in SaaS mode (`None` in single
+/// user mode). A SaaS config without a valid root is refused: neither the
+/// global root (everyone's memory) nor no confinement at all is safe.
+pub fn confinement(config: &Config) -> MemoryResult<Option<Namespace>> {
confinement_in(crate::core::runtime::is_saas(), config)
}
/// [`confinement`] as a pure function of the mode.
-pub fn confinement_in(saas: bool, config: &Config) -> Option<Namespace> {
+pub fn confinement_in(saas: bool, config: &Config) -> MemoryResult<Option<Namespace>> {
if !saas {
- return None;
+ return Ok(None);
}
- let raw = config.memory.root.as_deref()?.trim();
+ let raw = config.memory.root.as_deref().map(str::trim).unwrap_or("");
match raw.parse::<Namespace>() {
- Ok(root) => Some(root),
- Err(error) => {
- // A user agent's root is forced and valid; failing closed here
- // would turn memory off, failing open would read everyone. Log it
- // and confine to a root nothing else uses.
- tracing::error!(root = raw, %error, "[memory:user_scope] invalid confinement root");
- Some(Namespace::ROOT)
+ Ok(root) if root != Namespace::ROOT => Ok(Some(root)),
+ Ok(_) | Err(_) => {
+ tracing::error!(root = raw, "[memory:user_scope] no valid confinement root in SaaS mode");
+ Err(MemoryError::invalid(
+ "memory is unavailable: this agent has no valid memory root",
+ ))
}
}
}
diff --git a/crates/openhuman-core/src/user_agents/surface.rs b/crates/openhuman-core/src/user_agents/surface.rs
index 0f1dec62e0..77ecbb7176 100644
--- a/crates/openhuman-core/src/user_agents/surface.rs
+++ b/crates/openhuman-core/src/user_agents/surface.rs
@@ -59,7 +59,6 @@ pub const USER_METHODS: &[&str] = &[
"openhuman.memory_forget",
"openhuman.memory_items_list",
"openhuman.memory_explore",
- "openhuman.memory_engine_get",
];
/// Whether `method` (of an operator-plane controller or not) may be