-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_memory_importance_final.py
More file actions
executable file
·474 lines (390 loc) · 13.4 KB
/
test_memory_importance_final.py
File metadata and controls
executable file
·474 lines (390 loc) · 13.4 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#!/usr/bin/env python3
"""
Memory Importance Scoring - FINAL TEST
Standalone test for Phase 3: Memory Importance Scoring component.
"""
import sys
import tempfile
import shutil
from pathlib import Path
from datetime import datetime, timedelta
from dataclasses import dataclass
import math
# ==================== Domain Classes ====================
@dataclass
class MemoryItem:
path: Path
name: str
type: str
size: int
access_count: int
last_accessed: any
created_at: datetime
importance_score: float
tags: list
@dataclass
class ImportanceScore:
item: MemoryItem
score: float
frequency_score: float
recency_score: float
size_score: float
type_score: float
source_score: float
references_score: float
breakdown: dict
# ==================== Implementation ====================
class MemoryImportanceScorer:
def __init__(self):
self.frequency_weight = 0.3
self.recency_weight = 0.2
self.size_weight = 0.1
self.type_weight = 0.2
self.source_weight = 0.1
self.references_weight = 0.1
self.type_importance = {
"code": 1.0,
"config": 0.8,
"data": 0.6,
"log": 0.3,
"cache": 0.2,
"other": 0.5,
}
self.source_importance = {
"user": 1.0,
"agent": 0.7,
"system": 0.5,
"external": 0.3,
"unknown": 0.5,
}
def _calculate_frequency_score(self, item: MemoryItem) -> float:
if item.access_count == 0:
return 0.0
else:
return math.log10(item.access_count + 1) / 4.0
def _calculate_recency_score(self, item: MemoryItem) -> float:
if item.last_accessed is None:
return 0.0
days_since = (datetime.now() - item.last_accessed).total_seconds() / 86400.0
if days_since == 0:
return 1.0
elif days_since < 1:
return 0.9
elif days_since < 7:
return 0.7
elif days_since < 30:
return 0.5
else:
return 0.1
def _calculate_size_score(self, item: MemoryItem) -> float:
if item.size == 0:
return 0.0
max_size = 1_000_000.0
normalized = min(item.size / max_size, 1.0)
return normalized * 0.5
def _calculate_type_score(self, item: MemoryItem) -> float:
return self.type_importance.get(item.type, 0.5)
def _calculate_source_score(self, item: MemoryItem) -> float:
if "source:user" in item.tags:
return self.source_importance["user"]
elif "source:agent" in item.tags:
return self.source_importance["agent"]
elif "source:system" in item.tags:
return self.source_importance["system"]
elif "source:external" in item.tags:
return self.source_importance["external"]
else:
return self.source_importance["unknown"]
def _calculate_references_score(self, item: MemoryItem) -> float:
references = [tag for tag in item.tags if tag.startswith("ref:")]
num_references = len(references)
if num_references == 0:
return 0.0
elif num_references < 5:
return 0.5
elif num_references < 10:
return 0.7
elif num_references < 20:
return 0.9
else:
return 1.0
def score_item(self, item: MemoryItem) -> ImportanceScore:
frequency_score = self._calculate_frequency_score(item)
recency_score = self._calculate_recency_score(item)
size_score = self._calculate_size_score(item)
type_score = self._calculate_type_score(item)
source_score = self._calculate_source_score(item)
references_score = self._calculate_references_score(item)
weighted_score = (
self.frequency_weight * frequency_score +
self.recency_weight * recency_score +
self.size_weight * size_score +
self.type_weight * type_score +
self.source_weight * source_score +
self.references_weight * references_score
)
final_score = max(0.0, min(1.0, weighted_score))
item.importance_score = final_score
return ImportanceScore(
item=item,
score=final_score,
frequency_score=frequency_score,
recency_score=recency_score,
size_score=size_score,
type_score=type_score,
source_score=source_score,
references_score=references_score,
breakdown={}
)
def get_most_important(self, items: list, n: int = 10) -> list:
for item in items:
self.score_item(item)
return sorted(items, key=lambda x: -x.importance_score)[:n]
# ==================== Tests ====================
def test_frequency_scoring():
print("🧪 Testing frequency scoring...")
now = datetime.now()
item = MemoryItem(
path=Path("test.py"),
name="test.py",
type="code",
size=1000,
access_count=0,
last_accessed=None,
created_at=now,
importance_score=0.0,
tags=[]
)
scorer = MemoryImportanceScorer()
score = scorer.score_item(item)
assert score.frequency_score == 0.0
print(" ✅ Never accessed: 0.00")
item.access_count = 100
score = scorer.score_item(item)
assert score.frequency_score > 0.4
print(f" ✅ Frequently accessed (100x): {score.frequency_score:.2f}")
item.access_count = 10
score = scorer.score_item(item)
assert 0.2 < score.frequency_score < 0.4
print(f" ✅ Moderately accessed (10x): {score.frequency_score:.2f}")
print(" ✅ Frequency scoring tests passed")
return True
def test_recency_scoring():
print("\n🧪 Testing recency scoring...")
now = datetime.now()
item = MemoryItem(
path=Path("test.py"),
name="test.py",
type="code",
size=1000,
access_count=0,
last_accessed=None,
created_at=now,
importance_score=0.0,
tags=[]
)
scorer = MemoryImportanceScorer()
score = scorer.score_item(item)
assert score.recency_score == 0.0
print(" ✅ Never accessed: 0.00")
item.last_accessed = now - timedelta(seconds=1)
score = scorer.score_item(item)
assert score.recency_score >= 0.9
print(f" ✅ Accessed very recently (1 second ago): {score.recency_score:.2f}")
item.last_accessed = now - timedelta(days=1)
score = scorer.score_item(item)
assert 0.6 < score.recency_score <= 1.0
print(f" ✅ Accessed yesterday: {score.recency_score:.2f}")
print(" ✅ Recency scoring tests passed")
return True
def test_type_scoring():
print("\n🧪 Testing type scoring...")
now = datetime.now()
item = MemoryItem(
path=Path("code.py"),
name="code.py",
type="code",
size=1000,
access_count=5,
last_accessed=now,
created_at=now,
importance_score=0.0,
tags=[]
)
scorer = MemoryImportanceScorer()
score = scorer.score_item(item)
assert score.type_score == 1.0
print(" ✅ Code type: 1.00")
item.type = "config"
score = scorer.score_item(item)
assert score.type_score == 0.8
print(" ✅ Config type: 0.80")
item.type = "log"
score = scorer.score_item(item)
assert score.type_score == 0.3
print(" ✅ Log type: 0.30")
print(" ✅ Type scoring tests passed")
return True
def test_importance_scoring():
print("\n🧪 Testing importance scoring...")
now = datetime.now()
item = MemoryItem(
path=Path("important.py"),
name="important.py",
type="code",
size=50000,
access_count=100,
last_accessed=now,
created_at=now,
importance_score=0.0,
tags=["ref:code1", "ref:code2", "ref:code3", "ref:code4", "ref:code5"]
)
scorer = MemoryImportanceScorer()
score = scorer.score_item(item)
assert score.score > 0.6
print(f" ✅ High importance: {score.score:.2f}")
print(f" Frequency: {score.frequency_score:.2f}")
print(f" Recency: {score.recency_score:.2f}")
print(f" Size: {score.size_score:.2f}")
print(f" Type: {score.type_score:.2f}")
print(f" References: {score.references_score:.2f}")
print(" ✅ Importance scoring tests passed")
return True
def test_sorting_and_ranking():
print("\n🧪 Testing sorting and ranking...")
now = datetime.now()
items = [
MemoryItem(
path=Path("high.py"),
name="high.py",
type="code",
size=50000,
access_count=100,
last_accessed=now,
created_at=now,
importance_score=0.0,
tags=["ref:code1", "ref:code2", "ref:code3", "ref:code4", "ref:code5"]
),
MemoryItem(
path=Path("medium.py"),
name="medium.py",
type="data",
size=10000,
access_count=10,
last_accessed=now - timedelta(days=7),
created_at=now,
importance_score=0.0,
tags=[]
),
MemoryItem(
path=Path("low.py"),
name="low.py",
type="log",
size=100,
access_count=0,
last_accessed=None,
created_at=now - timedelta(days=30),
importance_score=0.0,
tags=["type:log", "source:system"]
),
]
scorer = MemoryImportanceScorer()
most_important = scorer.get_most_important(items, n=2)
assert len(most_important) == 2
assert most_important[0].name == "high.py"
assert most_important[0].importance_score > 0.6
print(f" ✅ Top 2: {[item.name for item in most_important]}")
print(f" ✅ High importance: {most_important[0].importance_score:.2f}")
print(" ✅ Sorting and ranking tests passed")
return True
def test_complete_workflow():
print("\n🧪 Testing complete workflow...")
test_dir = Path(tempfile.mkdtemp(prefix="mem_imp_test_"))
try:
now = datetime.now()
(test_dir / "file1.txt").write_text("content1")
(test_dir / "file2.txt").write_text("content2")
items = [
MemoryItem(
path=test_dir / "file1.txt",
name="file1.txt",
type="data",
size=8,
access_count=10,
last_accessed=now,
created_at=now,
importance_score=0.0,
tags=["type:data"]
),
MemoryItem(
path=test_dir / "file2.txt",
name="file2.txt",
type="data",
size=8,
access_count=2,
last_accessed=now - timedelta(days=7),
created_at=now,
importance_score=0.0,
tags=["type:data"]
),
]
scorer = MemoryImportanceScorer()
scores = scorer.get_most_important(items, n=2)
assert len(scores) == 2
assert scores[0].importance_score >= scores[1].importance_score
print(" ✅ Workflow tested")
print(f" ✅ Total items: {len(items)}")
print(f" ✅ Sorted: {sorted([item.importance_score for item in scores], reverse=True)}")
return True
except Exception as e:
print(f" ❌ Test failed: {e}")
return False
finally:
if test_dir.exists():
shutil.rmtree(test_dir)
def run_all_tests():
print("=" * 70)
print("🧪 Memory Importance Tests (Phase 3) - FINAL TEST")
print("=" * 70)
print()
tests = {
"Frequency Scoring": test_frequency_scoring,
"Recency Scoring": test_recency_scoring,
"Type Scoring": test_type_scoring,
"Importance Scoring": test_importance_scoring,
"Sorting and Ranking": test_sorting_and_ranking,
"Complete Workflow": test_complete_workflow,
}
results = {}
for name, test_func in tests.items():
try:
results[name] = test_func()
except Exception as e:
print(f"\n❌ Test '{name}' crashed: {e}")
import traceback
traceback.print_exc()
results[name] = False
# Summary
print("\n" + "=" * 70)
print("📊 Test Summary")
print("=" * 70)
passed = sum(1 for v in results.values() if v)
total = len(results)
for name, result in results.items():
status = "✅ PASS" if result else "❌ FAIL"
print(f" {status} - {name}")
print()
print(f"Results: {passed}/{total} tests passed")
if passed == total:
print("\n🎉 All tests passed! Memory Importance component complete.")
print(" Phase 3: Memory Importance Scoring - 100% COMPLETE")
print(" Phase 3: Knowledge Preservation System - 100% COMPLETE 🎉")
print(" Ready to proceed to: Phase 4 - Meta-Learning")
else:
print(f"\n⚠️ {total - passed} test(s) failed. Review and fix.")
return passed == total
if __name__ == "__main__":
success = run_all_tests()
print()
print("=" * 70)
sys.exit(0 if success else 1)