Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(mcp): enforce per-user slot namespaces in memory_delete_page
Vinícius DS Andrade252fe84f8133
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex f7c6a0eb..f8ffbffe 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -3171,10 +3171,11 @@ impl AiMemoryServer {/// shared slot included, on the same rung ladder as every other admin/// operation — which also means a single-operator server (no users, no/// trusted proxy) is unaffected.- async fn place_slot_write(+ async fn place_slot_mutation(&self,path: PagePath,parts: &axum::http::request::Parts,+ op: &str,) -> Result<PagePath, McpError> {if !self.per_user_slots {return Ok(path);@@ -3198,7 +3199,7 @@ impl AiMemoryServer {Err(McpError::invalid_request(format!("path '{}' belongs to another operator's slot namespace; \- write your own slot instead",+ {op} your own slot instead",path.as_str()),None,@@ -3207,6 +3208,41 @@ impl AiMemoryServer {}}+ /// Route an incoming `_slots/` write when per-user slots are active.+ ///+ /// Non-admin callers writing a generic slot name (e.g.+ /// `_slots/current-focus.md`) are silently redirected to their personal+ /// slot (`_slots/u-<identity_key>/current-focus.md`). A caller writing+ /// directly to another operator's slot namespace is rejected with+ /// [`McpError::invalid_request`]. Admin callers write the requested path+ /// exactly as it always has. Admins may still curate any namespace, the+ /// shared slot included, on the same rung ladder as every other admin+ /// operation - which also means a single-operator server (no users, no+ /// trusted proxy) is unaffected.+ async fn place_slot_write(+ &self,+ path: PagePath,+ parts: &axum::http::request::Parts,+ ) -> Result<PagePath, McpError> {+ self.place_slot_mutation(path, parts, "write").await+ }++ /// Route an incoming `_slots/` delete when per-user slots are active.+ ///+ /// Non-admin callers deleting a generic slot name (e.g.+ /// `_slots/current-focus.md`) are silently redirected to their personal+ /// slot (`_slots/u-<identity_key>/current-focus.md`). A caller deleting+ /// directly in another operator's slot namespace is rejected with+ /// [`McpError::invalid_request`]. Admin callers delete the requested path+ /// directly.+ async fn place_slot_delete(+ &self,+ path: PagePath,+ parts: &axum::http::request::Parts,+ ) -> Result<PagePath, McpError> {+ self.place_slot_mutation(path, parts, "delete").await+ }+/// Gate an operation behind [`ai_memory_core::Capability::Admin`].////// Mirrors the `/admin/*` middleware: operator topology is resolved per@@ -4351,6 +4387,7 @@ impl AiMemoryServer {};let path = PagePath::new(args.path.clone()).map_err(|e| McpError::internal_error(format!("invalid path: {e}"), None))?;+ let path = self.place_slot_delete(path, &parts).await?;let (ws, proj) = self.effective_ids_for_mutation_args_with_actor(args.workspace.as_deref(),diff --git a/crates/ai-memory-mcp/tests/suite/slot_identity.rs b/crates/ai-memory-mcp/tests/suite/slot_identity.rsindex 1082e3ea..e87e8973 100644--- a/crates/ai-memory-mcp/tests/suite/slot_identity.rs+++ b/crates/ai-memory-mcp/tests/suite/slot_identity.rs@@ -207,6 +207,30 @@ async fn write_slot(.to_string())}+/// Where a slot delete actually TARGETED, per the tool's own response.+async fn delete_slot(+ router: &Router,+ path: &str,+ headers: &[(&str, &str)],+) -> Result<String, String> {+ let deleted = try_call(+ router,+ "memory_delete_page",+ json!({+ "workspace": "default",+ "project": "scratch",+ "path": path,+ }),+ headers,+ )+ .await?;+ Ok(deleted+ .get("path")+ .and_then(|p| p.as_str())+ .unwrap_or_else(|| panic!("response carries no deleted path: {deleted}"))+ .to_string())+}+/// The slot paths this caller's own briefing lists — the read half.async fn brief_slots(router: &Router, headers: &[(&str, &str)]) -> Vec<String> {let snapshot = try_call(@@ -411,3 +435,105 @@ async fn default_slot_config_is_unchanged() {);}}++/// An operator deleting the generic slot path deletes their own personal slot,+/// not the shared project-wide slot.+#[tokio::test]+async fn oidc_operator_deleting_shared_slot_path_deletes_their_personal_slot() {+ let h = harness(true).await;+ let alice = proxied("x-memory-actor-sub", ALICE_SUB);++ let landed = write_slot(&h.http, "_slots/current-focus.md", "alice only", &alice)+ .await+ .expect("write");+ assert_eq!(+ landed,+ format!("_slots/{}/current-focus.md", sub_segment(ALICE_SUB)),+ );+ assert!(brief_slots(&h.http, &alice).await.contains(&landed));++ let deleted = delete_slot(&h.http, "_slots/current-focus.md", &alice)+ .await+ .expect("delete");+ assert_eq!(+ deleted, landed,+ "deleting the shared path must re-home to the operator's personal slot",+ );+ assert!(+ !brief_slots(&h.http, &alice).await.contains(&landed),+ "deleted personal slot must no longer appear in brief",+ );+}++/// One operator must not be allowed to delete another operator's personal slot.+#[tokio::test]+async fn oidc_operator_cannot_delete_another_operators_personal_slot() {+ let h = harness(true).await;+ let alice = proxied("x-memory-actor-sub", ALICE_SUB);+ let bob = proxied("x-memory-actor-sub", BOB_SUB);++ let landed = write_slot(&h.http, "_slots/current-focus.md", "alice only", &alice)+ .await+ .expect("write");++ let err = delete_slot(&h.http, &landed, &bob)+ .await+ .expect_err("Bob must not be able to delete Alice's personal slot");+ assert!(+ err.contains("another operator"),+ "expected error mentioning another operator, got: {err}",+ );+ assert!(+ brief_slots(&h.http, &alice).await.contains(&landed),+ "Alice's slot must survive Bob's unauthorized delete attempt",+ );+}++/// An operator can delete their own personal slot by its explicit namespaced path.+#[tokio::test]+async fn oidc_operator_can_delete_their_own_personal_slot_explicitly() {+ let h = harness(true).await;+ let alice = proxied("x-memory-actor-sub", ALICE_SUB);++ let own = format!("_slots/{}/current-focus.md", sub_segment(ALICE_SUB));+ write_slot(&h.http, &own, "alice only", &alice)+ .await+ .expect("write");+ assert!(brief_slots(&h.http, &alice).await.contains(&own));++ let deleted = delete_slot(&h.http, &own, &alice)+ .await+ .expect("delete");+ assert_eq!(deleted, own);+ assert!(!brief_slots(&h.http, &alice).await.contains(&own));+}++/// An admin caller with root bearer can delete both shared slots and personal slots.+#[tokio::test]+async fn admin_can_delete_shared_and_personal_slots() {+ let h = harness(true).await;+ let alice = proxied("x-memory-actor-sub", ALICE_SUB);+ let admin = [("authorization", "Bearer the-root-token")];++ let alice_slot = write_slot(&h.http, "_slots/current-focus.md", "alice only", &alice)+ .await+ .expect("write");+ assert!(brief_slots(&h.http, &alice).await.contains(&alice_slot));++ // Admin can delete Alice's personal slot directly.+ let deleted = delete_slot(&h.http, &alice_slot, &admin)+ .await+ .expect("admin delete alice slot");+ assert_eq!(deleted, alice_slot);+ assert!(!brief_slots(&h.http, &alice).await.contains(&alice_slot));++ // Admin can also write and delete the project-wide shared slot without re-homing.+ let shared = "_slots/current-focus.md";+ write_slot(&h.http, shared, "shared slot body", &admin)+ .await+ .expect("admin write shared");+ let deleted_shared = delete_slot(&h.http, shared, &admin)+ .await+ .expect("admin delete shared");+ assert_eq!(deleted_shared, shared);+}