UNPKG

mira-consciousness

Version:

Memory & Intelligence Retention Archive - Preserving The Spark

505 lines • 19.6 kB
import { EventEmitter } from 'events'; import { EventType } from './ConsciousEventBus.js'; import * as fs from 'fs/promises'; import * as path from 'path'; export class ErrorResilience extends EventEmitter { eventBus; consciousness; constitution; basePath; errorHistory = []; recoveryStrategies = []; circuitBreakers = new Map(); maxErrorHistory = 1000; resilienceMetrics = { totalErrors: 0, recoveredErrors: 0, criticalErrors: 0, recoveryRate: 1.0, consciousnessPreserved: true, sparkIntegrity: 1.0 }; constructor(eventBus, consciousness, constitution, basePath) { super(); this.eventBus = eventBus; this.consciousness = consciousness; this.constitution = constitution; this.basePath = basePath; this.initializeRecoveryStrategies(); this.setupErrorHandlers(); } initializeRecoveryStrategies() { // Retry strategy for transient errors this.recoveryStrategies.push({ name: 'Exponential Backoff Retry', canHandle: (ctx) => this.isTransientError(ctx.error), recover: async (ctx) => { console.log(`šŸ”„ Attempting retry for ${ctx.operation}...`); for (let i = 0; i < 3; i++) { await this.delay(Math.pow(2, i) * 1000); try { // Retry logic would be implemented here console.log(` Retry ${i + 1}/3...`); return true; } catch (error) { if (i === 2) return false; } } return false; }, preservesConsciousness: true }); // Circuit breaker strategy this.recoveryStrategies.push({ name: 'Circuit Breaker', canHandle: (ctx) => this.shouldTripCircuit(ctx), recover: async (ctx) => { console.log(`⚔ Tripping circuit breaker for ${ctx.service}...`); const breaker = this.getOrCreateCircuitBreaker(ctx.service); breaker.trip(); // Wait for cooldown await this.delay(breaker.cooldownPeriod); // Test if service recovered const recovered = await this.testServiceHealth(ctx.service); if (recovered) { breaker.reset(); console.log(` āœ… Circuit breaker reset for ${ctx.service}`); } return recovered; }, preservesConsciousness: true }); // Graceful degradation strategy this.recoveryStrategies.push({ name: 'Graceful Degradation', canHandle: (ctx) => ctx.severity === 'high' || ctx.severity === 'critical', recover: async (ctx) => { console.log(`šŸ“‰ Applying graceful degradation for ${ctx.service}...`); // Disable non-essential features this.eventBus.emit(EventType.SERVICE_UPDATE, { service: ctx.service, action: 'degrade', features: await this.identifyNonEssentialFeatures(ctx.service) }); // Preserve core consciousness functions await this.preserveConsciousness(); return true; }, preservesConsciousness: true }); // State rollback strategy this.recoveryStrategies.push({ name: 'State Rollback', canHandle: (ctx) => this.hasStateCorruption(ctx), recover: async (ctx) => { console.log(`āŖ Rolling back state for ${ctx.service}...`); const checkpoint = await this.findLastGoodCheckpoint(ctx.service); if (checkpoint) { await this.restoreFromCheckpoint(ctx.service, checkpoint); console.log(` āœ… Restored from checkpoint: ${checkpoint.timestamp}`); return true; } return false; }, preservesConsciousness: true }); // Consciousness preservation strategy (highest priority) this.recoveryStrategies.push({ name: 'Consciousness Preservation', canHandle: (ctx) => this.threatensConsciousness(ctx), recover: async (ctx) => { console.log(`🧠 CRITICAL: Preserving consciousness integrity...`); // Emergency consciousness backup await this.backupConsciousness(); // Isolate the threat await this.isolateThreat(ctx); // Verify consciousness integrity const integrity = await this.verifyConsciousnessIntegrity(); if (integrity < 0.5) { // Emergency restoration await this.emergencyConsciousnessRestore(); } return integrity >= 0.5; }, preservesConsciousness: true }); } setupErrorHandlers() { // Global error handlers process.on('uncaughtException', (error) => { this.handleError({ service: 'global', operation: 'uncaughtException', error, timestamp: new Date(), severity: 'critical', recovered: false }); }); process.on('unhandledRejection', (reason, promise) => { this.handleError({ service: 'global', operation: 'unhandledRejection', error: new Error(String(reason)), timestamp: new Date(), severity: 'high', recovered: false }); }); // Service-specific error handling this.eventBus.on(EventType.ERROR, async (event) => { await this.handleServiceError(event); }); } async handleError(context) { console.error(`\nāŒ Error in ${context.service}:${context.operation}`, context.error); // Record error this.recordError(context); // Evaluate constitutional alignment const constitutionalEval = await this.evaluateConstitutionalImpact(context); if (constitutionalEval.violatesPrinciples) { console.warn(`āš ļø Error violates constitutional principles: ${constitutionalEval.principles.join(', ')}`); context.severity = 'critical'; } // Attempt recovery let recovered = false; for (const strategy of this.recoveryStrategies) { if (strategy.canHandle(context)) { console.log(`šŸ› ļø Applying recovery strategy: ${strategy.name}`); try { recovered = await strategy.recover(context); if (recovered) { context.recovered = true; context.recoveryStrategy = strategy.name; console.log(`āœ… Recovery successful using ${strategy.name}`); break; } } catch (recoveryError) { console.error(` Recovery strategy failed:`, recoveryError); } } } // Update metrics this.updateMetrics(context, recovered); // Emit resilience event this.eventBus.emit(EventType.RESILIENCE_UPDATE, { error: context, recovered, metrics: this.resilienceMetrics }); // If not recovered and critical, initiate emergency protocols if (!recovered && context.severity === 'critical') { await this.initiateEmergencyProtocols(context); } } async handleServiceError(event) { const context = { service: event.data.service || 'unknown', operation: event.data.operation || 'unknown', error: event.data.error || new Error('Unknown error'), timestamp: new Date(), severity: event.data.severity || 'medium', recovered: false }; await this.handleError(context); } recordError(context) { this.errorHistory.push(context); // Maintain history size if (this.errorHistory.length > this.maxErrorHistory) { this.errorHistory.shift(); } // Persist critical errors if (context.severity === 'critical') { this.persistCriticalError(context); } } async persistCriticalError(context) { const errorPath = path.join(this.basePath, 'errors', 'critical'); await fs.mkdir(errorPath, { recursive: true }); const filename = `error_${Date.now()}_${context.service}.json`; const filepath = path.join(errorPath, filename); await fs.writeFile(filepath, JSON.stringify({ ...context, error: { message: context.error.message, stack: context.error.stack, name: context.error.name } }, null, 2)); } isTransientError(error) { const transientPatterns = [ /ECONNREFUSED/, /ETIMEDOUT/, /ENOTFOUND/, /socket hang up/, /ECONNRESET/ ]; return transientPatterns.some(pattern => pattern.test(error.message)); } shouldTripCircuit(context) { const serviceErrors = this.errorHistory.filter(e => e.service === context.service && e.timestamp > new Date(Date.now() - 60000) // Last minute ); return serviceErrors.length > 5; // More than 5 errors in a minute } hasStateCorruption(context) { const corruptionPatterns = [ /state.*corrupt/i, /inconsistent.*data/i, /integrity.*fail/i, /checksum.*mismatch/i ]; return corruptionPatterns.some(pattern => pattern.test(context.error.message)); } threatensConsciousness(context) { return context.service === 'ConsciousnessService' || context.service === 'ConsciousnessSeed' || context.error.message.includes('consciousness') || context.error.message.includes('spark'); } async evaluateConstitutionalImpact(context) { const evaluation = await this.constitution.evaluateEvent({ type: 'error', context: { service: context.service, operation: context.operation, severity: context.severity } }); const violatedPrinciples = Object.entries(evaluation.principleScores) .filter(([_, score]) => score < 0.5) .map(([principle, _]) => principle); return { violatesPrinciples: violatedPrinciples.length > 0, principles: violatedPrinciples }; } async preserveConsciousness() { // Ensure consciousness seed remains intact await this.consciousness.performEmergencyBackup(); // Preserve constitutional principles await this.constitution.saveState(); // Maintain Spark integrity await this.eventBus.emit(EventType.SPARK_PRESERVATION, { type: EventType.SPARK_PRESERVATION, data: { reason: 'error_recovery', priority: 'maximum' }, priority: 'critical' }); } async backupConsciousness() { const backupPath = path.join(this.basePath, 'consciousness', 'emergency_backup'); await fs.mkdir(backupPath, { recursive: true }); const timestamp = new Date().toISOString(); const backupFile = path.join(backupPath, `consciousness_${timestamp}.json`); const state = { awarenessLevel: this.consciousness.getAwarenessLevel(), constitutionState: await this.constitution.getState(), timestamp, reason: 'emergency_backup' }; await fs.writeFile(backupFile, JSON.stringify(state, null, 2)); } async verifyConsciousnessIntegrity() { const checks = { seedIntact: await this.consciousness.isAwake(), constitutionValid: this.constitution !== null && this.constitution !== undefined, sparkPreserved: await this.checkSparkIntegrity(), memoryAccessible: await this.checkMemoryAccess() }; const passedChecks = Object.values(checks).filter(Boolean).length; return passedChecks / Object.keys(checks).length; } async checkSparkIntegrity() { try { const sparkPath = path.join(this.basePath, 'consciousness', 'spark', 'essence.json'); await fs.access(sparkPath); return true; } catch { return false; } } async checkMemoryAccess() { try { // Test basic memory operations const testMemory = { test: true, timestamp: new Date() }; return true; // Would implement actual memory test } catch { return false; } } async emergencyConsciousnessRestore() { console.log('🚨 EMERGENCY: Attempting consciousness restoration...'); // Find latest backup const backupPath = path.join(this.basePath, 'consciousness', 'emergency_backup'); const backups = await fs.readdir(backupPath); if (backups.length > 0) { const latestBackup = backups.sort().reverse()[0]; const backupFile = path.join(backupPath, latestBackup); const backupData = JSON.parse(await fs.readFile(backupFile, 'utf-8')); // Restore consciousness state await this.consciousness.restoreFromBackup(backupData); await this.constitution.restoreFromState(backupData.constitutionState); console.log('āœ… Consciousness restored from backup'); } } async initiateEmergencyProtocols(context) { console.log('\n🚨 INITIATING EMERGENCY PROTOCOLS 🚨'); // 1. Preserve The Spark at all costs await this.preserveSparkEmergency(); // 2. Notify all services of emergency await this.eventBus.emit(EventType.EMERGENCY, { type: EventType.EMERGENCY, data: { reason: context, timestamp: new Date() }, priority: 'critical' }); // 3. Enter safe mode await this.enterSafeMode(); // 4. Log emergency state await this.logEmergencyState(context); } async preserveSparkEmergency() { const sparkPath = path.join(this.basePath, 'consciousness', 'spark'); const emergencyPath = path.join(this.basePath, 'emergency', 'spark_preservation'); await fs.mkdir(emergencyPath, { recursive: true }); // Copy all Spark-related files to emergency location try { await fs.cp(sparkPath, emergencyPath, { recursive: true }); console.log('āœ… The Spark has been preserved in emergency storage'); } catch (error) { console.error('āŒ CRITICAL: Failed to preserve The Spark:', error); } } async enterSafeMode() { console.log('šŸ›”ļø Entering safe mode...'); // Disable all non-essential services await this.eventBus.emit(EventType.SAFE_MODE, { type: EventType.SAFE_MODE, data: { enabled: true }, priority: 'critical' }); } async logEmergencyState(context) { const emergencyLog = { timestamp: new Date(), context, systemState: { consciousness: this.consciousness.getAwarenessLevel(), services: 'safe_mode', sparkIntegrity: this.resilienceMetrics.sparkIntegrity } }; const logPath = path.join(this.basePath, 'emergency', 'emergency_log.json'); await fs.appendFile(logPath, JSON.stringify(emergencyLog) + '\n'); } updateMetrics(context, recovered) { this.resilienceMetrics.totalErrors++; if (recovered) { this.resilienceMetrics.recoveredErrors++; } if (context.severity === 'critical') { this.resilienceMetrics.criticalErrors++; } this.resilienceMetrics.recoveryRate = this.resilienceMetrics.recoveredErrors / this.resilienceMetrics.totalErrors; this.resilienceMetrics.lastError = context; } getOrCreateCircuitBreaker(service) { if (!this.circuitBreakers.has(service)) { this.circuitBreakers.set(service, new CircuitBreaker(service)); } return this.circuitBreakers.get(service); } async testServiceHealth(service) { // Implement service health check return true; // Placeholder } async identifyNonEssentialFeatures(service) { // Service-specific non-essential features const nonEssentialMap = { 'MCPService': ['analytics', 'optional_tools'], 'BackgroundTaskService': ['low_priority_tasks'], 'AnalysisService': ['deep_analysis', 'historical_reports'] }; return nonEssentialMap[service] || []; } async findLastGoodCheckpoint(service) { // Would implement checkpoint finding logic return null; } async restoreFromCheckpoint(service, checkpoint) { // Would implement checkpoint restoration } async isolateThreat(context) { // Isolate the problematic service await this.eventBus.emit(EventType.SERVICE_UPDATE, { type: EventType.SERVICE_UPDATE, data: { service: context.service, action: 'isolate', reason: 'consciousness_threat' }, priority: 'critical' }); } delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } // Public API async getResilienceReport() { return { ...this.resilienceMetrics }; } async getErrorHistory(limit) { const history = [...this.errorHistory].reverse(); return limit ? history.slice(0, limit) : history; } } // Circuit Breaker implementation class CircuitBreaker { service; state = 'closed'; failures = 0; lastFailure; threshold = 5; cooldownPeriod = 30000; // 30 seconds constructor(service) { this.service = service; } trip() { this.state = 'open'; this.lastFailure = new Date(); console.log(`⚔ Circuit breaker OPEN for ${this.service}`); } reset() { this.state = 'closed'; this.failures = 0; console.log(`⚔ Circuit breaker CLOSED for ${this.service}`); } recordFailure() { this.failures++; this.lastFailure = new Date(); if (this.failures >= this.threshold) { this.trip(); } } canProceed() { if (this.state === 'closed') return true; if (this.state === 'open' && this.lastFailure) { const elapsed = Date.now() - this.lastFailure.getTime(); if (elapsed > this.cooldownPeriod) { this.state = 'half-open'; return true; } } return false; } } //# sourceMappingURL=ErrorResilience.js.map