Skip to content

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

test(memory): cover rpc confinement boundary and default read scoping

Steven Enamakel83365cc6401c
diff --git a/crates/openhuman-core/src/memory/confinement_tests.rs b/crates/openhuman-core/src/memory/confinement_tests.rs
index e3ea373c4e..6deb13f505 100644
--- a/crates/openhuman-core/src/memory/confinement_tests.rs
+++ b/crates/openhuman-core/src/memory/confinement_tests.rs
@@ -8,10 +8,10 @@ use tinymemory_api::{
use tinymemory_tools::confine::{Confinement, Policy};
use tinymemory_tools::layout::identity::Identity;
-use crate::memory::explore::{self, ExploreParams, ItemsGetParams, ItemsListParams, PathStep};
+use crate::memory::explore::{self, ItemsGetParams, PathStep};
use crate::memory::test_fixtures::{bind_reference, config_in};
use crate::memory::types::{
- FetchParams, ForgetParams, LearnParams, RecallParams,
+ FetchParams, ForgetParams, ItemsListParams, LearnParams, RecallParams,
};
fn ns(raw: &str) -> Namespace {
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index 3c16b4d02f..7c12593c0b 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -115,21 +115,21 @@ pub(super) fn engine_set(ctx: DispatchCtx, params: Map<String, Value>) -> Contro
}
pub(super) fn recall(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<RecallParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<RecallParams, _>(ctx, params, |_, config, mut params: RecallParams| async move {
params.filter = Some(rpc_confinement(&config).filter(params.filter)?);
ops::recall(&config, params).await
})
}
pub(super) fn fetch(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<FetchParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<FetchParams, _>(ctx, params, |_, config, mut params: FetchParams| async move {
params.filter = Some(rpc_confinement(&config).filter(params.filter)?);
ops::fetch(&config, params).await
})
}
pub(super) fn learn(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<LearnParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<LearnParams, _>(ctx, params, |_, config, mut params: LearnParams| async move {
let identity = Identity::root().resolve(&scope::identity_pins(&config));
params.meta = Some(
rpc_confinement(&config)
@@ -140,7 +140,7 @@ pub(super) fn learn(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerF
}
pub(super) fn forget(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<ForgetParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<ForgetParams, _>(ctx, params, |_, config, mut params: ForgetParams| async move {
params.reach = Some(rpc_confinement(&config).reach(params.reach)?);
ops::forget(&config, params).await
})
@@ -153,7 +153,7 @@ pub(super) fn erase_all(ctx: DispatchCtx, params: Map<String, Value>) -> Control
}
pub(super) fn items_list(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<ItemsListParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<ItemsListParams, _>(ctx, params, |_, config, mut params: ItemsListParams| async move {
let narrowed = explore::narrowed(params.filter.take(), &params.path)?;
params.filter = Some(rpc_confinement(&config).filter(Some(narrowed))?);
params.path.clear();
@@ -162,7 +162,7 @@ pub(super) fn items_list(ctx: DispatchCtx, params: Map<String, Value>) -> Contro
}
pub(super) fn explore(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<ExploreParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<ExploreParams, _>(ctx, params, |_, config, mut params: ExploreParams| async move {
let narrowed = explore::narrowed(params.filter.take(), &params.path)?;
params.filter = Some(rpc_confinement(&config).filter(Some(narrowed))?);
params.path.clear();
@@ -171,7 +171,7 @@ pub(super) fn explore(ctx: DispatchCtx, params: Map<String, Value>) -> Controlle
}
pub(super) fn items_get(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
- rpc::<ItemsGetParams, _>(ctx, params, |_, config, mut params| async move {
+ rpc::<ItemsGetParams, _>(ctx, params, |_, config, mut params: ItemsGetParams| async move {
params.reach = Some(rpc_confinement(&config).reach(params.reach)?);
explore::items_get(&config, params).await
})
diff --git a/crates/openhuman-core/src/memory/schemas_tests.rs b/crates/openhuman-core/src/memory/schemas_tests.rs
index c4f6c7cec5..d053db093e 100644
--- a/crates/openhuman-core/src/memory/schemas_tests.rs
+++ b/crates/openhuman-core/src/memory/schemas_tests.rs
@@ -328,6 +328,86 @@ async fn read_handlers_answer_over_a_bound_engine() {
assert!(listed["items"].as_array().unwrap().is_empty());
}
+#[tokio::test]
+async fn rpc_memory_handlers_apply_the_refuse_boundary_before_operations() {
+ let tmp = tempfile::tempdir().unwrap();
+ let mut config = config_in(&tmp);
+ config.memory.root = Some("team:acme".into());
+ bind_reference(&config);
+ let outside = serde_json::to_value(tinymemory_api::Reach::subtree(
+ "team:other".parse().unwrap(),
+ ))
+ .unwrap();
+
+ for (function, params) in [
+ (
+ "recall",
+ json!({"question": "x", "filter": {"reach": outside.clone()}}),
+ ),
+ (
+ "fetch",
+ json!({"query": "x", "filter": {"reach": outside.clone()}}),
+ ),
+ (
+ "learn",
+ json!({"text": "outside", "meta": {"namespace": "team:other"}}),
+ ),
+ (
+ "forget",
+ json!({"ids": ["not-present"], "reach": outside.clone()}),
+ ),
+ (
+ "items_list",
+ json!({"filter": {"reach": outside.clone()}}),
+ ),
+ (
+ "explore",
+ json!({
+ "facet": "kind",
+ "path": [{"facet": "namespace", "value": "team:other"}]
+ }),
+ ),
+ (
+ "items_get",
+ json!({"ids": ["not-present"], "reach": outside.clone()}),
+ ),
+ ] {
+ let error = call(&config, function, params).await.unwrap_err();
+ assert!(error.contains("INVALID_REQUEST"), "{function}: {error}");
+ }
+}
+
+#[tokio::test]
+async fn rpc_default_reads_stay_within_the_configured_identity_root() {
+ let tmp = tempfile::tempdir().unwrap();
+ let mut config = config_in(&tmp);
+ config.memory.root = Some("team:acme".into());
+ let engine = bind_reference(&config);
+ for (text, root) in [
+ ("acme memory", "team:acme"),
+ ("other team secret", "team:other"),
+ ] {
+ engine
+ .store(tinymemory_api::StoreItem::learning(
+ text,
+ tinymemory_api::LearningKind::Fact,
+ 0.9,
+ tinymemory_api::MemoryMeta {
+ namespace: root.parse().unwrap(),
+ ..tinymemory_api::MemoryMeta::default()
+ },
+ ))
+ .await
+ .unwrap();
+ }
+
+ let listed = call(&config, "items_list", json!({})).await.unwrap();
+ let listed = listed.get("result").unwrap_or(&listed);
+ let items = listed["items"].as_array().unwrap();
+ assert_eq!(items.len(), 1);
+ assert_eq!(items[0]["meta"]["namespace"], "team:acme");
+}
+
#[tokio::test]
async fn policy_handlers_validate_persist_and_report() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/memory/scope_tests.rs b/crates/openhuman-core/src/memory/scope_tests.rs
index 304ab6a6d1..3c9e6759df 100644
--- a/crates/openhuman-core/src/memory/scope_tests.rs
+++ b/crates/openhuman-core/src/memory/scope_tests.rs
@@ -1,7 +1,7 @@
use super::*;
use crate::config::schema::MemoryAgentConfig;
-use tinymemory_api::{MetaFilter, Reach};
+use tinymemory_api::{MetaFilter, Namespace, Reach};
use tinymemory_tools::confine::{Confinement, Policy};
use tinymemory_tools::layout::identity::{
account_root, actor_of_root, chat_node, validate_root, Identity, ResolvedIdentity,