use crate::agent::BaseAgent;
use crate::config::Config;
use crate::memory::MemoryUnit;
use tempfile::tempdir;
#[tokio::test]
async fn test_memory_isolation() {
let config = Config::default();
let dir1 = tempdir().unwrap();
let dir2 = tempdir().unwrap();
let mut config1 = config.clone();
config1.memory.episodic_path = dir1.path().to_string_lossy().to_string();
let (wm1, em1, sm1) = crate::memory::helpers::create_memory_providers(&config1)
.await
.unwrap();
let llm_provider1 = crate::llm::create_llm_provider(&config1).unwrap();
let agent1 = BaseAgent::new(
config1,
"agent1",
"Test Agent 1",
llm_provider1,
wm1,
em1.clone(),
sm1,
crate::tools::manager::ToolManager::new(),
)
.await
.unwrap();
let mut config2 = config.clone();
config2.memory.episodic_path = dir2.path().to_string_lossy().to_string();
let (wm2, em2, sm2) = crate::memory::helpers::create_memory_providers(&config2)
.await
.unwrap();
let llm_provider2 = crate::llm::create_llm_provider(&config2).unwrap();
let agent2 = BaseAgent::new(
config2,
"agent2",
"Test Agent 2",
llm_provider2,
wm2,
em2.clone(),
sm2,
crate::tools::manager::ToolManager::new(),
)
.await
.unwrap();
println!("Testing Working Memory Isolation...");
let memory1 = MemoryUnit {
id: "mem1".to_string(),
timestamp: 1000,
content: "Secret 1 for Agent 1".to_string(),
embedding: None,
};
agent1
.working_memory
.lock()
.await
.add(memory1)
.await
.unwrap();
let results1 = agent1
.working_memory
.lock()
.await
.retrieve("Secret", 10)
.await
.unwrap();
assert!(!results1.is_empty(), "Agent 1 should find its memory");
assert_eq!(results1[0].content, "Secret 1 for Agent 1");
let results2 = agent2
.working_memory
.lock()
.await
.retrieve("Secret", 10)
.await
.unwrap();
assert!(
results2.is_empty(),
"Agent 2 should NOT find Agent 1's memory"
);
let memory2 = MemoryUnit {
id: "mem2".to_string(),
timestamp: 1001,
content: "Secret 2 for Agent 2".to_string(),
embedding: None,
};
agent2
.working_memory
.lock()
.await
.add(memory2)
.await
.unwrap();
let results2_b = agent2
.working_memory
.lock()
.await
.retrieve("Secret", 10)
.await
.unwrap();
assert!(!results2_b.is_empty());
assert_eq!(results2_b[0].content, "Secret 2 for Agent 2");
let results1_b = agent1
.working_memory
.lock()
.await
.retrieve("Secret", 10)
.await
.unwrap();
assert_eq!(results1_b.len(), 1);
assert_eq!(results1_b[0].content, "Secret 1 for Agent 1");
println!("Working Memory Isolation: PASSED");
println!("Testing Episodic Memory Isolation...");
em1.lock()
.await
.add(MemoryUnit {
id: "ep1".to_string(),
timestamp: 2000,
content: "Episodic 1".to_string(),
embedding: None,
})
.await
.unwrap();
em2.lock()
.await
.add(MemoryUnit {
id: "ep2".to_string(),
timestamp: 2000,
content: "Episodic 2".to_string(),
embedding: None,
})
.await
.unwrap();
let _res1 = em1.lock().await.retrieve("Episodic", 5).await.unwrap();
let _res2 = em2.lock().await.retrieve("Episodic", 5).await.unwrap();
println!(
"Episodic Memory Isolation: Setup successful (Validation skipped due to external dependency)"
);
}