Delete memory_drift_diagnostician.py
Browse files
memory_drift_diagnostician.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import numpy as np
|
| 3 |
-
from typing import Dict, Any, List, Optional
|
| 4 |
-
from agentic_reliability_framework.runtime.agents.base import BaseAgent, AgentSpecialization
|
| 5 |
-
from ai_event import AIEvent
|
| 6 |
-
|
| 7 |
-
logger = logging.getLogger(__name__)
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
class MemoryDriftDiagnosticianAgent(BaseAgent):
|
| 11 |
-
"""
|
| 12 |
-
Detects drift in semantic memory by comparing current retrieval scores
|
| 13 |
-
with their historical distribution using a z‑score test.
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
def __init__(self, history_window: int = 100, zscore_threshold: float = 2.0):
|
| 17 |
-
super().__init__(AgentSpecialization.DIAGNOSTICIAN)
|
| 18 |
-
self.history_window = history_window
|
| 19 |
-
self.zscore_threshold = zscore_threshold
|
| 20 |
-
self._retrieval_scores_history: List[float] = []
|
| 21 |
-
|
| 22 |
-
async def analyze(self, event: AIEvent, context_window: Optional[int] = None) -> Dict[str, Any]:
|
| 23 |
-
"""
|
| 24 |
-
Analyze drift using retrieval scores.
|
| 25 |
-
Args:
|
| 26 |
-
event: AIEvent containing retrieval_scores.
|
| 27 |
-
context_window: Optional override of history window size.
|
| 28 |
-
Returns:
|
| 29 |
-
Dictionary with drift detection findings.
|
| 30 |
-
"""
|
| 31 |
-
try:
|
| 32 |
-
if not event.retrieval_scores:
|
| 33 |
-
return {
|
| 34 |
-
'specialization': 'ai_memory_drift',
|
| 35 |
-
'confidence': 0.0,
|
| 36 |
-
'findings': {},
|
| 37 |
-
'recommendations': []
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
current_avg = float(np.mean(event.retrieval_scores))
|
| 41 |
-
self._retrieval_scores_history.append(current_avg)
|
| 42 |
-
|
| 43 |
-
# Use provided context_window if given, else default
|
| 44 |
-
window = context_window if context_window is not None else self.history_window
|
| 45 |
-
if len(self._retrieval_scores_history) > window:
|
| 46 |
-
self._retrieval_scores_history.pop(0)
|
| 47 |
-
|
| 48 |
-
if len(self._retrieval_scores_history) < 10:
|
| 49 |
-
return {
|
| 50 |
-
'specialization': 'ai_memory_drift',
|
| 51 |
-
'confidence': 0.0,
|
| 52 |
-
'findings': {
|
| 53 |
-
'drift_detected': False,
|
| 54 |
-
'current_avg': current_avg,
|
| 55 |
-
'historical_avg': None,
|
| 56 |
-
'z_score': None
|
| 57 |
-
},
|
| 58 |
-
'recommendations': []
|
| 59 |
-
}
|
| 60 |
-
|
| 61 |
-
historical_avg = float(np.mean(self._retrieval_scores_history[:-1]))
|
| 62 |
-
historical_std = float(np.std(self._retrieval_scores_history[:-1])) + 1e-6
|
| 63 |
-
z_score = (current_avg - historical_avg) / historical_std
|
| 64 |
-
drift_detected = abs(z_score) > self.zscore_threshold
|
| 65 |
-
confidence = min(1.0, abs(z_score) / 5.0)
|
| 66 |
-
|
| 67 |
-
return {
|
| 68 |
-
'specialization': 'ai_memory_drift',
|
| 69 |
-
'confidence': confidence,
|
| 70 |
-
'findings': {
|
| 71 |
-
'drift_detected': drift_detected,
|
| 72 |
-
'current_avg': current_avg,
|
| 73 |
-
'historical_avg': historical_avg,
|
| 74 |
-
'z_score': float(z_score)
|
| 75 |
-
},
|
| 76 |
-
'recommendations': [
|
| 77 |
-
"Reindex knowledge base",
|
| 78 |
-
"Adjust embedding model",
|
| 79 |
-
"Update context window"
|
| 80 |
-
] if drift_detected else []
|
| 81 |
-
}
|
| 82 |
-
except Exception as e:
|
| 83 |
-
logger.error(f"MemoryDriftDiagnostician error: {e}", exc_info=True)
|
| 84 |
-
return {
|
| 85 |
-
'specialization': 'ai_memory_drift',
|
| 86 |
-
'confidence': 0.0,
|
| 87 |
-
'findings': {},
|
| 88 |
-
'recommendations': []
|
| 89 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|