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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
LITEHAT SELF-HEALING
|
| 3 |
+
Autonomous failure recovery β detect, rollback, analyze, fix, redeploy.
|
| 4 |
+
|
| 5 |
+
The self-healing loop:
|
| 6 |
+
1. Monitor: Detect deployment/application failures
|
| 7 |
+
2. Triage: Classify failure severity and type
|
| 8 |
+
3. Rollback: Auto-revert to last known good state
|
| 9 |
+
4. Analyze: Read logs, identify root cause
|
| 10 |
+
5. Fix: The Brain patches the code
|
| 11 |
+
6. Verify: Run tests on the fix
|
| 12 |
+
7. Redeploy: Push the fixed version
|
| 13 |
+
8. Learn: Record the failure pattern for future prevention
|
| 14 |
+
|
| 15 |
+
All autonomous. No human touches the keyboard.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
import time
|
| 20 |
+
import re
|
| 21 |
+
from typing import Optional, Dict, Any, List, Tuple
|
| 22 |
+
from dataclasses import dataclass, field
|
| 23 |
+
from enum import Enum
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class FailureSeverity(str, Enum):
|
| 27 |
+
CRITICAL = "critical" # App is completely down
|
| 28 |
+
DEGRADED = "degraded" # Partially functional
|
| 29 |
+
WARNING = "warning" # Still working but at risk
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class FailureCategory(str, Enum):
|
| 33 |
+
OOM = "out_of_memory"
|
| 34 |
+
CRASH = "crash_loop"
|
| 35 |
+
NETWORK = "network_error"
|
| 36 |
+
DEPENDENCY = "missing_dependency"
|
| 37 |
+
CONFIG = "config_error"
|
| 38 |
+
BUILD = "build_error"
|
| 39 |
+
DEPLOY = "deploy_error"
|
| 40 |
+
SYNTAX = "syntax_error"
|
| 41 |
+
LOGIC = "logic_error"
|
| 42 |
+
TIMEOUT = "timeout"
|
| 43 |
+
UNKNOWN = "unknown"
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@dataclass
|
| 47 |
+
class FailureEvent:
|
| 48 |
+
"""A single failure event β analyzed and annotated."""
|
| 49 |
+
timestamp: float
|
| 50 |
+
app_name: str
|
| 51 |
+
severity: FailureSeverity
|
| 52 |
+
category: FailureCategory
|
| 53 |
+
error_message: str
|
| 54 |
+
stack_trace: Optional[str] = None
|
| 55 |
+
pod_logs: Optional[str] = None
|
| 56 |
+
root_cause: Optional[str] = None
|
| 57 |
+
fix_applied: Optional[str] = None
|
| 58 |
+
fix_successful: bool = False
|
| 59 |
+
rollback_performed: bool = False
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class SelfHealingEngine:
|
| 63 |
+
"""
|
| 64 |
+
Autonomous self-healing engine.
|
| 65 |
+
|
| 66 |
+
The engine watches the application, detects failures, and autonomously
|
| 67 |
+
heals them. It learns from past failures to prevent recurrence.
|
| 68 |
+
|
| 69 |
+
Pattern: detect β rollback β analyze β fix β verify β redeploy
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
def __init__(self):
|
| 73 |
+
self.failure_history: List[FailureEvent] = []
|
| 74 |
+
self.known_fixes: Dict[str, str] = {} # error_pattern β fix_strategy
|
| 75 |
+
self.healing_in_progress: Dict[str, bool] = {}
|
| 76 |
+
|
| 77 |
+
def detect_failure(
|
| 78 |
+
self,
|
| 79 |
+
app_name: str,
|
| 80 |
+
logs: str,
|
| 81 |
+
health_status: int = 200,
|
| 82 |
+
) -> Optional[FailureEvent]:
|
| 83 |
+
"""
|
| 84 |
+
Detect if a failure has occurred.
|
| 85 |
+
|
| 86 |
+
Returns a FailureEvent if failure detected, None if healthy.
|
| 87 |
+
"""
|
| 88 |
+
if health_status == 200:
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
event = FailureEvent(
|
| 92 |
+
timestamp=time.time(),
|
| 93 |
+
app_name=app_name,
|
| 94 |
+
severity=self._classify_severity(logs, health_status),
|
| 95 |
+
category=self._classify_category(logs),
|
| 96 |
+
error_message=self._extract_error(logs),
|
| 97 |
+
pod_logs=logs,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
self.failure_history.append(event)
|
| 101 |
+
return event
|
| 102 |
+
|
| 103 |
+
def heal(self, event: FailureEvent) -> bool:
|
| 104 |
+
"""
|
| 105 |
+
Heal a failure autonomously.
|
| 106 |
+
|
| 107 |
+
Returns True if the healing was successful.
|
| 108 |
+
"""
|
| 109 |
+
if self.healing_in_progress.get(event.app_name):
|
| 110 |
+
return False # Already healing
|
| 111 |
+
|
| 112 |
+
self.healing_in_progress[event.app_name] = True
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
print(f"\nπ HEALING {event.app_name} β {event.category.value}")
|
| 116 |
+
|
| 117 |
+
# Step 1: Immediate rollback if critical
|
| 118 |
+
if event.severity == FailureSeverity.CRITICAL:
|
| 119 |
+
print(f"π Rolling back {event.app_name}...")
|
| 120 |
+
self._rollback(event.app_name)
|
| 121 |
+
event.rollback_performed = True
|
| 122 |
+
|
| 123 |
+
# Step 2: Analyze root cause
|
| 124 |
+
root_cause = self._analyze_root_cause(event)
|
| 125 |
+
event.root_cause = root_cause
|
| 126 |
+
print(f"π Root cause: {root_cause}")
|
| 127 |
+
|
| 128 |
+
# Step 3: Generate fix
|
| 129 |
+
fix = self._generate_fix(event)
|
| 130 |
+
event.fix_applied = fix
|
| 131 |
+
print(f"π§ Fix: {fix}")
|
| 132 |
+
|
| 133 |
+
# Step 4: Apply fix
|
| 134 |
+
self._apply_fix(event, fix)
|
| 135 |
+
|
| 136 |
+
# Step 5: Verify
|
| 137 |
+
verified = self._verify_fix(event)
|
| 138 |
+
print(f"{'β
' if verified else 'β'} Verification: {'passed' if verified else 'failed'}")
|
| 139 |
+
|
| 140 |
+
# Step 6: Redeploy
|
| 141 |
+
if verified:
|
| 142 |
+
self._redeploy(event.app_name)
|
| 143 |
+
event.fix_successful = True
|
| 144 |
+
print(f"π Redeployed: {event.app_name}")
|
| 145 |
+
|
| 146 |
+
# Step 7: Learn
|
| 147 |
+
self._learn_from_failure(event)
|
| 148 |
+
print(f"π Learned new healing pattern")
|
| 149 |
+
|
| 150 |
+
return verified
|
| 151 |
+
|
| 152 |
+
finally:
|
| 153 |
+
self.healing_in_progress[event.app_name] = False
|
| 154 |
+
|
| 155 |
+
def _classify_severity(self, logs: str, health_status: int) -> FailureSeverity:
|
| 156 |
+
"""Classify failure severity."""
|
| 157 |
+
if health_status >= 500:
|
| 158 |
+
return FailureSeverity.CRITICAL
|
| 159 |
+
if health_status >= 400:
|
| 160 |
+
return FailureSeverity.DEGRADED
|
| 161 |
+
return FailureSeverity.WARNING
|
| 162 |
+
|
| 163 |
+
def _classify_category(self, logs: str) -> FailureCategory:
|
| 164 |
+
"""Classify the type of failure from logs."""
|
| 165 |
+
patterns = {
|
| 166 |
+
FailureCategory.OOM: [r"OOMKilled", r"out of memory", r"memory limit"],
|
| 167 |
+
FailureCategory.CRASH: [r"CrashLoopBackOff", r"segfault", r"SIGSEGV"],
|
| 168 |
+
FailureCategory.NETWORK: [r"connection refused", r"ECONNREFUSED", r"timeout"],
|
| 169 |
+
FailureCategory.DEPENDENCY: [r"module not found", r"cannot find module", r"ModuleNotFoundError"],
|
| 170 |
+
FailureCategory.CONFIG: [r"invalid configuration", r"config error"],
|
| 171 |
+
FailureCategory.BUILD: [r"build failed", r"compilation error"],
|
| 172 |
+
FailureCategory.DEPLOY: [r"ImagePullBackOff", r"ErrImagePull"],
|
| 173 |
+
FailureCategory.SYNTAX: [r"SyntaxError", r"syntax error", r"unexpected token"],
|
| 174 |
+
FailureCategory.LOGIC: [r"TypeError", r"ReferenceError", r"undefined is not"],
|
| 175 |
+
FailureCategory.TIMEOUT: [r"timed out", r"ETIMEDOUT", r"TimeoutError"],
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
for category, regexes in patterns.items():
|
| 179 |
+
for regex in regexes:
|
| 180 |
+
if re.search(regex, logs, re.IGNORECASE):
|
| 181 |
+
return category
|
| 182 |
+
|
| 183 |
+
return FailureCategory.UNKNOWN
|
| 184 |
+
|
| 185 |
+
def _extract_error(self, logs: str) -> str:
|
| 186 |
+
"""Extract the error message from logs."""
|
| 187 |
+
# Look for common error patterns
|
| 188 |
+
error_patterns = [
|
| 189 |
+
r"Error: (.+?)(?:\n|$)",
|
| 190 |
+
r"ERROR: (.+?)(?:\n|$)",
|
| 191 |
+
r"FATAL: (.+?)(?:\n|$)",
|
| 192 |
+
r"panic: (.+?)(?:\n|$)",
|
| 193 |
+
r"Exception: (.+?)(?:\n|$)",
|
| 194 |
+
r"(\w+Error): (.+?)(?:\n|$)",
|
| 195 |
+
]
|
| 196 |
+
|
| 197 |
+
for pattern in error_patterns:
|
| 198 |
+
match = re.search(pattern, logs, re.MULTILINE)
|
| 199 |
+
if match:
|
| 200 |
+
return match.group(0).strip()
|
| 201 |
+
|
| 202 |
+
# Return last non-empty line as fallback
|
| 203 |
+
lines = [l for l in logs.split('\n') if l.strip()]
|
| 204 |
+
return lines[-1] if lines else "Unknown error"
|
| 205 |
+
|
| 206 |
+
def _analyze_root_cause(self, event: FailureEvent) -> str:
|
| 207 |
+
"""Deep analysis of root cause."""
|
| 208 |
+
analysis_map = {
|
| 209 |
+
FailureCategory.OOM: (
|
| 210 |
+
f"Memory exhaustion in {event.app_name}. "
|
| 211 |
+
f"Container hit memory limit. Increase memory request or optimize memory usage."
|
| 212 |
+
),
|
| 213 |
+
FailureCategory.CRASH: (
|
| 214 |
+
f"Application crash in {event.app_name}. "
|
| 215 |
+
f"Check for segfaults in native modules or unhandled exceptions."
|
| 216 |
+
),
|
| 217 |
+
FailureCategory.NETWORK: (
|
| 218 |
+
f"Network error in {event.app_name}. "
|
| 219 |
+
f"Dependency service unreachable or port mismatch."
|
| 220 |
+
),
|
| 221 |
+
FailureCategory.DEPENDENCY: (
|
| 222 |
+
f"Missing dependency in {event.app_name}. "
|
| 223 |
+
f"Check package.json/requirements.txt for missing packages."
|
| 224 |
+
),
|
| 225 |
+
FailureCategory.CONFIG: (
|
| 226 |
+
f"Configuration error in {event.app_name}. "
|
| 227 |
+
f"Environment variables or config files are invalid."
|
| 228 |
+
),
|
| 229 |
+
FailureCategory.SYNTAX: (
|
| 230 |
+
f"Syntax error in {event.app_name}. "
|
| 231 |
+
f"Code has invalid syntax that prevents execution."
|
| 232 |
+
),
|
| 233 |
+
FailureCategory.LOGIC: (
|
| 234 |
+
f"Runtime logic error in {event.app_name}. "
|
| 235 |
+
f"Type error, null reference, or undefined value at runtime."
|
| 236 |
+
),
|
| 237 |
+
FailureCategory.BUILD: (
|
| 238 |
+
f"Build failure in {event.app_name}. "
|
| 239 |
+
f"Compilation or bundling step failed."
|
| 240 |
+
),
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
return analysis_map.get(
|
| 244 |
+
event.category,
|
| 245 |
+
f"Unknown failure in {event.app_name}: {event.error_message}"
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
def _generate_fix(self, event: FailureEvent) -> str:
|
| 249 |
+
"""Generate a fix for the failure."""
|
| 250 |
+
# Check known fixes first
|
| 251 |
+
for pattern, fix in self.known_fixes.items():
|
| 252 |
+
if pattern in event.error_message.lower():
|
| 253 |
+
return fix
|
| 254 |
+
|
| 255 |
+
fix_map = {
|
| 256 |
+
FailureCategory.OOM: "Increase memory limit in deployment config and optimize allocations",
|
| 257 |
+
FailureCategory.DEPENDENCY: "Add missing dependency to package manifest and rebuild",
|
| 258 |
+
FailureCategory.CONFIG: "Fix environment variable configuration and redeploy",
|
| 259 |
+
FailureCategory.SYNTAX: "Fix syntax error in source code",
|
| 260 |
+
FailureCategory.LOGIC: "Add null checks and type guards",
|
| 261 |
+
FailureCategory.NETWORK: "Verify service connectivity and port configuration",
|
| 262 |
+
FailureCategory.CRASH: "Add error boundary and graceful shutdown handler",
|
| 263 |
+
FailureCategory.BUILD: "Fix build script and dependency resolution",
|
| 264 |
+
FailureCategory.DEPLOY: "Verify container registry access and image tags",
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
return fix_map.get(event.category, "Manual investigation required")
|
| 268 |
+
|
| 269 |
+
def _apply_fix(self, event: FailureEvent, fix: str):
|
| 270 |
+
"""Apply the fix to the codebase/deployment."""
|
| 271 |
+
# The Brain modifies the actual source files to implement the fix
|
| 272 |
+
# For deployment-level fixes, it modifies the Kuberns configs
|
| 273 |
+
pass
|
| 274 |
+
|
| 275 |
+
def _verify_fix(self, event: FailureEvent) -> bool:
|
| 276 |
+
"""Verify the fix by running tests."""
|
| 277 |
+
# Run the test suite
|
| 278 |
+
# Run health checks against the fixed deployment
|
| 279 |
+
return True # Simulated for now
|
| 280 |
+
|
| 281 |
+
def _rollback(self, app_name: str):
|
| 282 |
+
"""Rollback to the last known good deployment."""
|
| 283 |
+
# Execute kubectl rollout undo
|
| 284 |
+
print(f" βͺ Rolling back {app_name} to previous version")
|
| 285 |
+
|
| 286 |
+
def _redeploy(self, app_name: str):
|
| 287 |
+
"""Redeploy the fixed application."""
|
| 288 |
+
# Build new image, push, and deploy
|
| 289 |
+
print(f" βͺ Redeploying {app_name}")
|
| 290 |
+
|
| 291 |
+
def _learn_from_failure(self, event: FailureEvent):
|
| 292 |
+
"""Learn from this failure to prevent recurrence."""
|
| 293 |
+
if event.root_cause and event.fix_applied:
|
| 294 |
+
key = event.error_message.lower()[:100] # Use error message as pattern key
|
| 295 |
+
self.known_fixes[key] = event.fix_applied
|
| 296 |
+
|
| 297 |
+
def get_health_report(self) -> Dict[str, Any]:
|
| 298 |
+
"""Generate a health report for all applications."""
|
| 299 |
+
total_failures = len(self.failure_history)
|
| 300 |
+
healed = sum(1 for f in self.failure_history if f.fix_successful)
|
| 301 |
+
|
| 302 |
+
return {
|
| 303 |
+
"total_failures": total_failures,
|
| 304 |
+
"healed": healed,
|
| 305 |
+
"heal_rate": healed / total_failures if total_failures > 0 else 1.0,
|
| 306 |
+
"known_patterns": len(self.known_fixes),
|
| 307 |
+
"recent_failures": [
|
| 308 |
+
{
|
| 309 |
+
"app": f.app_name,
|
| 310 |
+
"category": f.category.value,
|
| 311 |
+
"severity": f.severity.value,
|
| 312 |
+
"healed": f.fix_successful,
|
| 313 |
+
"time_ago_s": time.time() - f.timestamp,
|
| 314 |
+
}
|
| 315 |
+
for f in self.failure_history[-5:]
|
| 316 |
+
],
|
| 317 |
+
}
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 321 |
+
# CONTINUOUS MONITOR
|
| 322 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 323 |
+
|
| 324 |
+
class ContinuousMonitor:
|
| 325 |
+
"""
|
| 326 |
+
Continuous monitoring loop β watches apps and triggers self-healing.
|
| 327 |
+
|
| 328 |
+
Runs as a background daemon:
|
| 329 |
+
- Pings health endpoints every 30s
|
| 330 |
+
- Collects pod metrics
|
| 331 |
+
- Detects anomalies
|
| 332 |
+
- Triggers self-healing on failure
|
| 333 |
+
"""
|
| 334 |
+
|
| 335 |
+
def __init__(self, healer: SelfHealingEngine):
|
| 336 |
+
self.healer = healer
|
| 337 |
+
self.apps: Dict[str, str] = {} # app_name β health_url
|
| 338 |
+
|
| 339 |
+
def register_app(self, app_name: str, health_url: str):
|
| 340 |
+
"""Register an app for monitoring."""
|
| 341 |
+
self.apps[app_name] = health_url
|
| 342 |
+
|
| 343 |
+
async def monitor_loop(self, interval_s: int = 30):
|
| 344 |
+
"""Main monitoring loop."""
|
| 345 |
+
import asyncio
|
| 346 |
+
|
| 347 |
+
while True:
|
| 348 |
+
for app_name, health_url in self.apps.items():
|
| 349 |
+
try:
|
| 350 |
+
# Health check
|
| 351 |
+
import urllib.request
|
| 352 |
+
resp = urllib.request.urlopen(health_url, timeout=5)
|
| 353 |
+
|
| 354 |
+
if resp.status != 200:
|
| 355 |
+
# Failure detected
|
| 356 |
+
event = self.healer.detect_failure(
|
| 357 |
+
app_name,
|
| 358 |
+
logs=f"Health check returned {resp.status}",
|
| 359 |
+
health_status=resp.status,
|
| 360 |
+
)
|
| 361 |
+
if event:
|
| 362 |
+
self.healer.heal(event)
|
| 363 |
+
|
| 364 |
+
except Exception as e:
|
| 365 |
+
# Connection failure
|
| 366 |
+
event = self.healer.detect_failure(
|
| 367 |
+
app_name,
|
| 368 |
+
logs=f"Health check failed: {e}",
|
| 369 |
+
health_status=503,
|
| 370 |
+
)
|
| 371 |
+
if event:
|
| 372 |
+
self.healer.heal(event)
|
| 373 |
+
|
| 374 |
+
await asyncio.sleep(interval_s)
|