Skip to content

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

feat(memory): confine memory paths to the workspace root

Steven Enamakelf9f54702a785
diff --git a/crates/openhuman-core/src/memory/confine.rs b/crates/openhuman-core/src/memory/confine.rs
index 39834272f1..454931e547 100644
--- a/crates/openhuman-core/src/memory/confine.rs
+++ b/crates/openhuman-core/src/memory/confine.rs
@@ -16,12 +16,18 @@
//! - places a `learn` with no namespace at the identity's learnings node and
//! refuses one aimed outside the subtree.
-use tinymemory_api::{MemoryMeta, MetaFilter, Namespace, Reach};
+use tinymemory_api::{ExplorePage, MemoryMeta, MetaFilter, Namespace, Reach};
use crate::config::Config;
use super::error::{MemoryError, MemoryResult};
+use super::explore::{self, ExploreParams, ItemsGetParams, ItemsGetView};
+use super::ops;
use super::scope;
+use super::types::{
+ FetchParams, FetchView, ForgetParams, ForgetView, ItemsListParams, ItemsListView, LearnParams,
+ LearnView, RecallParams, RecallView,
+};
/// What the caller under `config` may read and forget: the subtree of the
/// identity in scope's layout root.
@@ -95,6 +101,89 @@ pub fn confine_learn_meta(config: &Config, meta: Option<MemoryMeta>) -> MemoryRe
Ok(meta)
}
+/// `memory_recall`, confined.
+///
+/// # Errors
+///
+/// A reach outside the caller's root, or what [`ops::recall`] returns.
+pub async fn recall(config: &Config, mut params: RecallParams) -> MemoryResult<RecallView> {
+ params.filter = Some(confine_filter(params.filter, &allowed_reach(config))?);
+ ops::recall(config, params).await
+}
+
+/// `memory_fetch`, confined.
+///
+/// # Errors
+///
+/// A reach outside the caller's root, or what [`ops::fetch`] returns.
+pub async fn fetch(config: &Config, mut params: FetchParams) -> MemoryResult<FetchView> {
+ params.filter = Some(confine_filter(params.filter, &allowed_reach(config))?);
+ ops::fetch(config, params).await
+}
+
+/// `memory_learn`, confined: the learning lands inside the caller's layout.
+///
+/// # Errors
+///
+/// A namespace outside the caller's root, or what [`ops::learn`] returns.
+pub async fn learn(config: &Config, mut params: LearnParams) -> MemoryResult<LearnView> {
+ params.meta = Some(confine_learn_meta(config, params.meta)?);
+ ops::learn(config, params, None).await
+}
+
+/// `memory_forget`, confined: an id outside the caller's root is not
+/// forgotten.
+///
+/// # Errors
+///
+/// A reach outside the caller's root, or what [`ops::forget`] returns.
+pub async fn forget(config: &Config, mut params: ForgetParams) -> MemoryResult<ForgetView> {
+ params.reach = Some(confine_reach(params.reach, &allowed_reach(config))?);
+ ops::forget(config, params).await
+}
+
+/// `memory_items_list`, confined. The explorer path narrows first (a
+/// namespace step sets the reach), then the narrowed filter is confined.
+///
+/// # Errors
+///
+/// A reach outside the caller's root, an invalid path, or what
+/// [`ops::items_list`] returns.
+pub async fn items_list(
+ config: &Config,
+ mut params: ItemsListParams,
+) -> MemoryResult<ItemsListView> {
+ let narrowed = explore::narrowed(params.filter.take(), &params.path)?;
+ params.filter = Some(confine_filter(Some(narrowed), &allowed_reach(config))?);
+ params.path = Vec::new();
+ ops::items_list(config, params).await
+}
+
+/// `memory_explore`, confined like [`items_list`].
+///
+/// # Errors
+///
+/// A reach outside the caller's root, an invalid path, or what
+/// [`explore::explore`] returns.
+pub async fn explore(config: &Config, mut params: ExploreParams) -> MemoryResult<ExplorePage> {
+ let narrowed = explore::narrowed(params.filter.take(), &params.path)?;
+ params.filter = Some(confine_filter(Some(narrowed), &allowed_reach(config))?);
+ params.path = Vec::new();
+ explore::explore(config, params).await
+}
+
+/// `memory_items_get`, confined: an id outside the caller's root reads as
+/// unknown.
+///
+/// # Errors
+///
+/// A reach outside the caller's root, or what [`explore::items_get`]
+/// returns.
+pub async fn items_get(config: &Config, mut params: ItemsGetParams) -> MemoryResult<ItemsGetView> {
+ params.reach = Some(confine_reach(params.reach, &allowed_reach(config))?);
+ explore::items_get(config, params).await
+}
+
#[cfg(test)]
#[path = "confine_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index 0436144694..64bbeada46 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -9,14 +9,14 @@ use crate::config::Config;
use crate::core::all::ControllerFuture;
use crate::core::Outcome;
use crate::memory::error::{MemoryError, MemoryResult};
-use crate::memory::explore::{self, ExploreParams, ItemsGetParams};
+use crate::memory::explore::{ExploreParams, ItemsGetParams};
use crate::memory::lifecycle::views::{self, JobsRunParams, PackPreviewParams, PolicySetParams};
use crate::memory::types::{
EmptyParams, EngineSetParams, FetchParams, ForgetParams, ImportStartParams, ImportStateView,
ItemsListParams, LearnParams, RecallParams, SourceAddedView, SourceRemovedView,
SourcesAddParams, SourcesListView, SourcesRemoveParams, SourcesSyncParams, SourcesSyncView,
};
-use crate::memory::{backfill, brain, engine, import, layout_migration, ops, sources};
+use crate::memory::{backfill, brain, confine, engine, import, layout_migration, ops, sources};
fn parse<T: DeserializeOwned>(params: Map<String, Value>) -> Result<T, String> {
serde_json::from_value(Value::Object(params))
@@ -74,49 +74,49 @@ pub(super) fn engine_set(params: Map<String, Value>) -> ControllerFuture {
pub(super) fn recall(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<RecallParams>(params)?;
- finish(ops::recall(&load().await?, params).await)
+ finish(confine::recall(&load().await?, params).await)
})
}
pub(super) fn fetch(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<FetchParams>(params)?;
- finish(ops::fetch(&load().await?, params).await)
+ finish(confine::fetch(&load().await?, params).await)
})
}
pub(super) fn learn(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<LearnParams>(params)?;
- finish(ops::learn(&load().await?, params, None).await)
+ finish(confine::learn(&load().await?, params).await)
})
}
pub(super) fn forget(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<ForgetParams>(params)?;
- finish(ops::forget(&load().await?, params).await)
+ finish(confine::forget(&load().await?, params).await)
})
}
pub(super) fn items_list(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<ItemsListParams>(params)?;
- finish(ops::items_list(&load().await?, params).await)
+ finish(confine::items_list(&load().await?, params).await)
})
}
pub(super) fn explore(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<ExploreParams>(params)?;
- finish(explore::explore(&load().await?, params).await)
+ finish(confine::explore(&load().await?, params).await)
})
}
pub(super) fn items_get(params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<ItemsGetParams>(params)?;
- finish(explore::items_get(&load().await?, params).await)
+ finish(confine::items_get(&load().await?, params).await)
})
}