-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memory_system.py
More file actions
391 lines (299 loc) · 10.2 KB
/
test_memory_system.py
File metadata and controls
391 lines (299 loc) · 10.2 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from memevolve.components.manage import SimpleManagementStrategy
from memevolve.components.retrieve import KeywordRetrievalStrategy
from memevolve.components.store import JSONFileStore
from memevolve.memory_system import MemorySystem, MemorySystemConfig
import sys
import tempfile
import pytest
# sys.path.insert(0, 'src') # No longer needed with package structure
@pytest.fixture
def temp_json_store():
with tempfile.NamedTemporaryFile(
delete=False,
suffix='.json',
mode='w'
) as f:
filepath = f.name
f.write("{}")
return JSONFileStore(filepath)
@pytest.fixture
def memory_system(temp_json_store):
config = MemorySystemConfig(
storage_backend=temp_json_store,
retrieval_strategy=KeywordRetrievalStrategy(),
management_strategy=SimpleManagementStrategy(),
log_level="WARNING"
)
return MemorySystem(config)
def test_memory_system_initialization(temp_json_store):
config = MemorySystemConfig(
storage_backend=temp_json_store,
retrieval_strategy=KeywordRetrievalStrategy(),
management_strategy=SimpleManagementStrategy(),
log_level="WARNING"
)
system = MemorySystem(config)
assert system.encoder is not None
assert system.storage == temp_json_store
assert system.retrieval_context is not None
assert system.memory_manager is not None
def test_memory_system_default_config():
system = MemorySystem()
import os
assert system.config.memory_base_url == os.getenv("MEMEVOLVE_MEMORY_BASE_URL")
assert system.config.default_retrieval_top_k == 5
assert system.config.enable_auto_management is True
def test_add_experience(memory_system):
experience = {
"id": "exp_001",
"action": "search",
"result": "found documents",
"feedback": "positive"
}
unit_id = memory_system.add_experience(experience)
assert unit_id is not None
assert isinstance(unit_id, str)
retrieved = memory_system.storage.retrieve(unit_id)
assert retrieved is not None
assert "type" in retrieved
assert "content" in retrieved
def test_add_trajectory(memory_system):
trajectory = [
{
"id": "exp_001",
"action": "search",
"result": "found documents"
},
{
"id": "exp_002",
"action": "analyze",
"result": "completed analysis"
}
]
unit_ids = memory_system.add_trajectory(trajectory)
assert len(unit_ids) == 2
assert all(isinstance(uid, str) for uid in unit_ids)
for unit_id in unit_ids:
assert memory_system.storage.exists(unit_id)
def test_query_memory(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Python programming basics",
"type": "lesson",
"tags": ["python", "programming"]
})
memory_system.add_experience({
"id": "exp_002",
"content": "Java development techniques",
"type": "skill",
"tags": ["java", "development"]
})
results = memory_system.query_memory("python", top_k=5)
assert len(results) > 0
assert any(
"python" in r.get("content", "").lower() or
"python" in str(r.get("tags", [])).lower()
for r in results
)
def test_query_memory_with_filters(memory_system):
memory_system.add_experience({
"id": "exp_001",
"type": "lesson",
"content": "Test lesson",
"tags": ["test"]
})
memory_system.add_experience({
"id": "exp_002",
"type": "skill",
"content": "Test skill",
"tags": ["test"]
})
results = memory_system.query_memory(
"test",
filters={"type": "lesson"}
)
assert all(r.get("type") == "lesson" for r in results)
def test_retrieve_by_ids(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Test content 1"
})
memory_system.add_experience({
"id": "exp_002",
"content": "Test content 2"
})
all_units = memory_system.storage.retrieve_all()
unit_ids = [u["id"] for u in all_units]
retrieved = memory_system.retrieve_by_ids(unit_ids)
assert len(retrieved) == 2
assert all(r.get("content") is not None for r in retrieved)
def test_generate_abstraction(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Python is a programming language",
"type": "lesson"
})
memory_system.add_experience({
"id": "exp_002",
"content": "Java is another programming language",
"type": "lesson"
})
all_units = memory_system.storage.retrieve_all()
unit_ids = [u["id"] for u in all_units[:1]]
try:
abstraction = memory_system.generate_abstraction(unit_ids)
assert "abstraction" in abstraction or "content" in abstraction
except RuntimeError:
pass
def test_manage_prune(memory_system):
for i in range(10):
memory_system.add_experience({
"id": f"exp_{i}",
"type": "lesson" if i % 2 == 0 else "skill",
"content": f"Test content {i}"
})
initial_count = memory_system.storage.count()
memory_system.manage_memory("prune", criteria={"max_count": 5})
final_count = memory_system.storage.count()
assert final_count == 5
def test_manage_consolidate(memory_system):
memory_system.add_experience({
"id": "exp_001",
"type": "lesson",
"content": "Test lesson 1"
})
memory_system.add_experience({
"id": "exp_002",
"type": "lesson",
"content": "Test lesson 2"
})
consolidated = memory_system.manage_memory("consolidate")
assert isinstance(consolidated, list)
def test_manage_deduplicate(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Duplicate content",
"type": "test"
})
memory_system.add_experience({
"id": "exp_002",
"content": "Duplicate content",
"type": "test"
})
initial_count = memory_system.storage.count()
memory_system.manage_memory("deduplicate")
final_count = memory_system.storage.count()
assert final_count < initial_count
def test_manage_forget(memory_system):
for i in range(10):
memory_system.add_experience({
"id": f"exp_{i}",
"content": f"Test content {i}"
})
initial_count = memory_system.storage.count()
memory_system.manage_memory("forget", strategy="lru", count=2)
final_count = memory_system.storage.count()
assert final_count == initial_count - 2
def test_get_health_metrics(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Test content",
"type": "lesson"
})
metrics = memory_system.get_health_metrics()
assert metrics is not None
assert metrics.total_units >= 1
assert metrics.total_size_bytes > 0
def test_get_health_metrics_without_manager():
config = MemorySystemConfig(log_level="WARNING")
system = MemorySystem(config)
metrics = system.get_health_metrics()
assert metrics is not None
assert metrics.total_units == 0
assert metrics.total_size_bytes == 0
def test_operation_log(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Test"
})
log = memory_system.get_operation_log()
assert len(log) >= 1
assert "operation" in log[0]
assert "timestamp" in log[0]
def test_clear_operation_log(memory_system):
memory_system.add_experience({
"id": "exp_001",
"content": "Test"
})
memory_system.clear_operation_log()
log = memory_system.get_operation_log()
assert len(log) == 0
def test_auto_management_enabled(memory_system):
config = MemorySystemConfig(
storage_backend=memory_system.storage,
retrieval_strategy=KeywordRetrievalStrategy(),
management_strategy=SimpleManagementStrategy(),
enable_auto_management=True,
auto_prune_threshold=5,
log_level="WARNING"
)
system = MemorySystem(config)
for i in range(10):
system.add_experience({
"id": f"exp_{i}",
"content": f"Test {i}"
})
assert system.storage is not None
count = system.storage.count()
assert count > 0
def test_auto_management_disabled(memory_system):
config = MemorySystemConfig(
storage_backend=memory_system.storage,
retrieval_strategy=KeywordRetrievalStrategy(),
management_strategy=SimpleManagementStrategy(),
enable_auto_management=False,
log_level="WARNING"
)
system = MemorySystem(config)
for i in range(20):
system.add_experience({
"id": f"exp_{i}",
"content": f"Test {i}"
})
assert system.storage is not None
count = system.storage.count()
assert count == 20
def test_callbacks(memory_system):
callbacks = {
"encode_called": False,
"retrieve_called": False,
"manage_called": False
}
def on_encode(unit_id, unit):
callbacks["encode_called"] = True
def on_retrieve(query, results):
callbacks["retrieve_called"] = True
def on_manage(operation, result):
callbacks["manage_called"] = True
memory_system.config.on_encode_complete = on_encode
memory_system.config.on_retrieve_complete = on_retrieve
memory_system.config.on_manage_complete = on_manage
memory_system.add_experience({"id": "exp_001", "content": "Test"})
memory_system.query_memory("test")
memory_system.manage_memory("prune", criteria={})
assert callbacks["encode_called"]
assert callbacks["retrieve_called"]
assert callbacks["manage_called"]
def test_error_handling_add_experience(memory_system):
invalid_experience = None
with pytest.raises(RuntimeError):
memory_system.add_experience(invalid_experience)
def test_error_handling_query_memory(memory_system):
config = MemorySystemConfig(
storage_backend=memory_system.storage,
log_level="WARNING"
)
config.retrieval_strategy = None
system = MemorySystem(config)
with pytest.raises(RuntimeError):
system.query_memory("test")