use crate::models::Memory;
#[must_use]
pub fn is_visible_to_caller(mem: &Memory, caller: &str) -> bool {
use crate::models::namespace::MemoryScope;
let scope = mem
.metadata
.get(crate::META_KEY_SCOPE)
.and_then(serde_json::Value::as_str)
.unwrap_or(MemoryScope::Private.as_str());
if scope != MemoryScope::Private.as_str() {
return true;
}
let owner = mem
.metadata
.get(crate::META_KEY_AGENT_ID)
.and_then(serde_json::Value::as_str)
.unwrap_or("");
if owner == caller {
return true;
}
let target = mem
.metadata
.get(crate::META_KEY_TARGET_AGENT_ID)
.and_then(serde_json::Value::as_str)
.unwrap_or("");
target == caller
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::{ConfidenceSource, Memory, MemoryKind, Tier};
use serde_json::json;
fn mem_with_metadata(metadata: serde_json::Value) -> Memory {
Memory {
id: "test-id".to_string(),
tier: Tier::Long,
namespace: "test-ns".to_string(),
title: "test".to_string(),
content: "test".to_string(),
tags: vec![],
priority: 5,
confidence: 1.0,
source: "test".to_string(),
access_count: 0,
created_at: "2026-05-20T00:00:00Z".to_string(),
updated_at: "2026-05-20T00:00:00Z".to_string(),
last_accessed_at: None,
expires_at: None,
metadata,
reflection_depth: 0,
memory_kind: MemoryKind::Observation,
entity_id: None,
persona_version: None,
citations: vec![],
source_uri: None,
source_span: None,
confidence_source: ConfidenceSource::CallerProvided,
confidence_signals: None,
confidence_decayed_at: None,
version: 1,
}
}
#[test]
fn private_default_owner_can_see() {
let m = mem_with_metadata(json!({"agent_id": "alice"}));
assert!(is_visible_to_caller(&m, "alice"));
}
#[test]
fn private_default_non_owner_cannot_see() {
let m = mem_with_metadata(json!({"agent_id": "alice"}));
assert!(!is_visible_to_caller(&m, "bob"));
}
#[test]
fn explicit_private_owner_can_see() {
let m = mem_with_metadata(json!({"agent_id": "alice", "scope": "private"}));
assert!(is_visible_to_caller(&m, "alice"));
}
#[test]
fn explicit_private_non_owner_cannot_see() {
let m = mem_with_metadata(json!({"agent_id": "alice", "scope": "private"}));
assert!(!is_visible_to_caller(&m, "bob"));
}
#[test]
fn shared_scope_anyone_can_see() {
let m = mem_with_metadata(json!({"agent_id": "alice", "scope": "shared"}));
assert!(is_visible_to_caller(&m, "bob"));
assert!(is_visible_to_caller(&m, "carol"));
}
#[test]
fn inbox_target_can_see_private_row() {
let m = mem_with_metadata(json!({
"agent_id": "alice",
"scope": "private",
"target_agent_id": "bob"
}));
assert!(is_visible_to_caller(&m, "bob"));
assert!(!is_visible_to_caller(&m, "carol"));
}
#[test]
fn empty_owner_blocks_named_caller() {
let m = mem_with_metadata(json!({"scope": "private"}));
assert!(!is_visible_to_caller(&m, "alice"));
}
#[test]
fn empty_owner_visible_to_empty_caller_edge_case() {
let m = mem_with_metadata(json!({"scope": "private"}));
assert!(is_visible_to_caller(&m, ""));
}
}