-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memory_fix.py
More file actions
169 lines (141 loc) · 5.36 KB
/
Copy pathtest_memory_fix.py
File metadata and controls
169 lines (141 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
"""
Test script to verify memory system functionality.
Tests both direct database saving and the agent orchestration flow.
"""
import asyncio
import os
import sys
import uuid
from datetime import datetime
# Add the apps/api directory to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'apps', 'api'))
from memory.direct_memory_core import DirectMemoryCore, DirectMemoryConfig
from services.unified_memory_knowledge_service import UnifiedMemoryKnowledgeService
async def test_direct_memory_save():
"""Test direct memory saving to database."""
print("🧠 Testing direct memory save...")
# Test data
project_id = "5a05f05d-3668-44e8-a733-2b19346c3061"
agent_id = "e1a79bdb-74b1-48c8-9d86-6bd8b39de84c" # Research Specialist Agent
config = DirectMemoryConfig(
database_url=os.getenv("DATABASE_URL"),
project_id=project_id
)
memory_core = DirectMemoryCore(config)
# Test memory save
result = await memory_core.add_memory_with_graph(
agent_id=agent_id,
content="Test memory for debugging memory system issue",
entities=[
("Agent", "Test Agent"),
("Task", "Memory System Debug"),
("System", "Memory Core")
],
relationships=[
("Test Agent", "debugged", "Memory System Debug"),
("Memory System Debug", "affects", "System"),
("Agent", "uses", "Memory Core")
],
metadata={
"test": True,
"created_at": datetime.utcnow().isoformat(),
"debug_session": True
},
importance=0.8,
project_id=project_id
)
print(f"💾 Direct memory save result: {result}")
return result.get("success", False)
async def test_unified_memory_service():
"""Test the unified memory service."""
print("🔄 Testing unified memory service...")
service = UnifiedMemoryKnowledgeService()
# Test data
project_id = "5a05f05d-3668-44e8-a733-2b19346c3061"
agent_id = "e1a79bdb-74b1-48c8-9d86-6bd8b39de84c" # Research Specialist Agent
try:
result = await service.memory_core.add_memory_with_graph(
agent_id=agent_id,
content="Test memory via unified service for memory debugging",
entities=[
("Service", "Unified Memory Service"),
("Debug", "Memory System Test"),
("Agent", "Test Agent")
],
relationships=[
("Test Agent", "uses", "Unified Memory Service"),
("Unified Memory Service", "performs", "Memory System Test"),
("Memory System Test", "validates", "Debug")
],
metadata={
"project_id": project_id,
"service_test": True,
"timestamp": datetime.utcnow().isoformat()
},
importance=0.9
)
print(f"🔄 Unified service result: {result}")
return result.get("success", False)
except Exception as e:
print(f"❌ Unified service failed: {e}")
return False
async def check_database_memories():
"""Check if memories were actually saved to database."""
print("🔍 Checking database for saved memories...")
try:
from models.base import get_db_session
from sqlalchemy import text
async with get_db_session() as session:
result = await session.execute(text("""
SELECT COUNT(*) as total_memories,
COUNT(DISTINCT agent_id) as unique_agents,
MAX(created_at) as latest_memory
FROM memories
WHERE project_id = '5a05f05d-3668-44e8-a733-2b19346c3061'
"""))
row = result.fetchone()
if row:
print(f"📊 Database stats:")
print(f" - Total memories: {row[0]}")
print(f" - Unique agents: {row[1]}")
print(f" - Latest memory: {row[2]}")
return row[0] > 0
else:
print("❌ No memory data found")
return False
except Exception as e:
print(f"❌ Database check failed: {e}")
return False
async def main():
"""Run all memory system tests."""
print("🚀 Starting memory system diagnostic tests...")
print("=" * 50)
# Test 1: Direct memory save
direct_success = await test_direct_memory_save()
print()
# Test 2: Unified service
service_success = await test_unified_memory_service()
print()
# Test 3: Database verification
db_success = await check_database_memories()
print()
# Summary
print("=" * 50)
print("🎯 Test Results Summary:")
print(f" ✅ Direct Memory Save: {'PASS' if direct_success else 'FAIL'}")
print(f" ✅ Unified Service: {'PASS' if service_success else 'FAIL'}")
print(f" ✅ Database Check: {'PASS' if db_success else 'FAIL'}")
if direct_success and service_success and db_success:
print("\n🎉 All tests passed! Memory system is working.")
return True
else:
print("\n❌ Some tests failed. Memory system needs attention.")
return False
if __name__ == "__main__":
# Load environment variables
from dotenv import load_dotenv
load_dotenv()
# Run tests
success = asyncio.run(main())
sys.exit(0 if success else 1)