| """Memory system evaluation helpers.""" | |
| from __future__ import annotations | |
| from typing import Any | |
| from plane_mode_scholar.storage.sqlite_store import SQLiteStore | |
| def evaluate_memory_health(store: SQLiteStore | None = None) -> dict[str, Any]: | |
| store = store or SQLiteStore() | |
| active = store.list_memories(status="active") | |
| archived = store.list_memories(status="archived") | |
| expired = store.list_memories(status="expired") | |
| canonical_forms = [m.canonical_form for m in active if m.canonical_form] | |
| duplicate_rate = 1 - (len(set(canonical_forms)) / len(canonical_forms)) if canonical_forms else 0.0 | |
| stale = [m for m in active if m.confidence < 0.4] | |
| stale_rate = len(stale) / len(active) if active else 0.0 | |
| return { | |
| "active_count": len(active), | |
| "archived_count": len(archived), | |
| "expired_count": len(expired), | |
| "duplicate_rate": round(duplicate_rate, 3), | |
| "stale_rate": round(stale_rate, 3), | |
| "type_distribution": _type_distribution(active), | |
| } | |
| def _type_distribution(memories: list) -> dict[str, int]: | |
| dist: dict[str, int] = {} | |
| for m in memories: | |
| dist[m.type.value] = dist.get(m.type.value, 0) + 1 | |
| return dist | |