Spaces:
Runtime error
Runtime error
Update policy_engine.py
Browse files- policy_engine.py +31 -4
policy_engine.py
CHANGED
|
@@ -8,7 +8,7 @@ import datetime
|
|
| 8 |
from collections import OrderedDict
|
| 9 |
from typing import Dict, List, Optional, Any
|
| 10 |
|
| 11 |
-
from agentic_reliability_framework.core.models.event import HealingPolicy, HealingAction,
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
@@ -63,12 +63,10 @@ DEFAULT_HEALING_POLICIES = [
|
|
| 63 |
class PolicyEngine:
|
| 64 |
"""
|
| 65 |
Thread‑safe policy engine with cooldown and rate limiting.
|
| 66 |
-
|
| 67 |
Policies are evaluated in priority order. Each policy has:
|
| 68 |
- conditions (AND logic)
|
| 69 |
- cooldown per (policy, component)
|
| 70 |
- rate limit per hour
|
| 71 |
-
|
| 72 |
The engine maintains an LRU cache of last execution timestamps
|
| 73 |
(using OrderedDict) to bound memory usage.
|
| 74 |
"""
|
|
@@ -174,4 +172,33 @@ class PolicyEngine:
|
|
| 174 |
self.execution_timestamps[key] = []
|
| 175 |
self.execution_timestamps[key].append(ts)
|
| 176 |
if len(self.execution_timestamps[key]) > self.max_execution_history:
|
| 177 |
-
self.execution_timestamps[key] = self.execution_timestamps[key][-self.max_execution_history:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from collections import OrderedDict
|
| 9 |
from typing import Dict, List, Optional, Any
|
| 10 |
|
| 11 |
+
from agentic_reliability_framework.core.models.event import HealingPolicy, HealingAction, PolicyCondition, ReliabilityEvent
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
| 63 |
class PolicyEngine:
|
| 64 |
"""
|
| 65 |
Thread‑safe policy engine with cooldown and rate limiting.
|
|
|
|
| 66 |
Policies are evaluated in priority order. Each policy has:
|
| 67 |
- conditions (AND logic)
|
| 68 |
- cooldown per (policy, component)
|
| 69 |
- rate limit per hour
|
|
|
|
| 70 |
The engine maintains an LRU cache of last execution timestamps
|
| 71 |
(using OrderedDict) to bound memory usage.
|
| 72 |
"""
|
|
|
|
| 172 |
self.execution_timestamps[key] = []
|
| 173 |
self.execution_timestamps[key].append(ts)
|
| 174 |
if len(self.execution_timestamps[key]) > self.max_execution_history:
|
| 175 |
+
self.execution_timestamps[key] = self.execution_timestamps[key][-self.max_execution_history:]
|
| 176 |
+
|
| 177 |
+
# ========== NEW: Simplified evaluation for demo ==========
|
| 178 |
+
def evaluate(self, event_type: str, severity: str, component: str) -> List[Dict[str, Any]]:
|
| 179 |
+
"""
|
| 180 |
+
Simplified policy evaluation for the governance demo.
|
| 181 |
+
Returns a list of recommended actions based on severity and event type.
|
| 182 |
+
Each action is a dict with keys: policy, action, reason.
|
| 183 |
+
"""
|
| 184 |
+
actions = []
|
| 185 |
+
if severity == "critical":
|
| 186 |
+
actions.append({
|
| 187 |
+
"policy": "POL-002",
|
| 188 |
+
"action": "isolate_affected",
|
| 189 |
+
"reason": "Critical failure detected"
|
| 190 |
+
})
|
| 191 |
+
elif severity == "high":
|
| 192 |
+
actions.append({
|
| 193 |
+
"policy": "POL-004",
|
| 194 |
+
"action": "require_approval",
|
| 195 |
+
"reason": "High risk"
|
| 196 |
+
})
|
| 197 |
+
elif severity == "medium" and event_type == "text_generation":
|
| 198 |
+
actions.append({
|
| 199 |
+
"policy": "POL-001",
|
| 200 |
+
"action": "regenerate",
|
| 201 |
+
"reason": "Low confidence"
|
| 202 |
+
})
|
| 203 |
+
# You can add more rules based on component or event_type as needed
|
| 204 |
+
return actions
|