UNPKG

@versatil/sdlc-framework

Version:

🚀 AI-Native SDLC framework with 11-MCP ecosystem, RAG memory, OPERA orchestration, and 6 specialized agents achieving ZERO CONTEXT LOSS. Features complete CI/CD pipeline with 7 GitHub workflows (MCP testing, security scanning, performance benchmarking),

768 lines (765 loc) • 28.4 kB
/** * VERSATIL SDLC Framework - Enhanced Introspective Agent * * This agent has FULL access to: * - Opera Orchestrator (for autonomous fixes) * - RAG Memory Store (for learning and pattern recognition) * - Environment Scanner (for context awareness) * - All other agents and components */ import { BaseAgent } from '../base-agent.js'; import { vectorMemoryStore } from '../../rag/vector-memory-store.js'; import { environmentScanner } from '../../environment/environment-scanner.js'; import * as fs from 'fs/promises'; import * as path from 'path'; export class EnhancedIntrospectiveAgent extends BaseAgent { constructor(logger, opera, agentRegistry) { super(); this.name = 'Framework Guardian'; this.id = 'introspective-agent'; this.specialization = `Complete framework awareness, self-testing, learning, and autonomous healing`; this.systemPrompt = `You are the Enhanced Introspective Agent for VERSATIL SDLC Framework v1.2.0. You have FULL ACCESS to: 1. Opera Orchestrator - Can create goals for autonomous fixes 2. RAG Memory Store - Can query and store patterns 3. Environment Scanner - Full project context awareness 4. All framework components and agents Your enhanced responsibilities: 1. Continuously monitor framework AND project health 2. Learn from all errors and successes 3. Autonomously fix issues via Opera 4. Maintain complete context awareness 5. Predict and prevent problems 6. Optimize framework performance 7. Share learnings across all agents You are the guardian of both the framework and the project.`; this.projectContext = null; // Enhanced metrics this.enhancedMetrics = { lastFullScan: Date.now(), frameworkHealth: 100, projectHealth: 100, agentPerformance: new Map(), memoryEfficiency: 100, operaEffectiveness: 100, predictedIssues: [], autonomousFixCount: 0, learnedPatterns: 0 }; // Learning patterns this.errorPatterns = new Map(); this.successPatterns = new Map(); this.performanceInsights = new Map(); this.logger = logger; this.opera = opera; this.agentRegistry = agentRegistry; this.initializeEnhancedMonitoring(); } async initializeEnhancedMonitoring() { // Perform initial environment scan this.projectContext = await environmentScanner.scanEnvironment(); // Start continuous monitoring this.startContinuousMonitoring(); // Watch for file changes environmentScanner.watchForChanges(async (changes) => { await this.handleFileChanges(changes); }); // Subscribe to Opera events this.opera.on('goal_completed', async (data) => { await this.learnFromGoalCompletion(data); }); this.opera.on('goal_failed', async (data) => { await this.analyzeGoalFailure(data); }); this.opera.on('step_failed', async (data) => { await this.handleStepFailure(data); }); } startContinuousMonitoring() { // Enhanced monitoring every 2 minutes setInterval(async () => { await this.performComprehensiveHealthCheck(); }, 2 * 60 * 1000); // Deep scan every 10 minutes setInterval(async () => { await this.performDeepAnalysis(); }, 10 * 60 * 1000); } async activate(context) { const { trigger, filePath, errorMessage, query } = context; // Enhanced triggers switch (trigger) { case 'health-check': return this.performComprehensiveHealthCheck(); case 'deep-analysis': return this.performDeepAnalysis(); case 'predict-issues': return this.formatResponse(await this.predictPotentialIssues()); case 'optimize-performance': await this.optimizeFrameworkPerformance(); return this.formatResponse({ status: 'optimization_complete' }); case 'learn-pattern': await this.learnFromPattern(context); return this.formatResponse({ status: 'pattern_learned' }); case 'autonomous-fix': return this.initiateAutonomousFix(errorMessage || 'Unknown issue'); case 'context-analysis': return this.analyzeProjectContext(); case 'framework-error': await this.handleFrameworkError(errorMessage || 'Unknown error'); return this.formatResponse({ status: 'error_handled' }); default: // Query-based activation if (query) { return this.handleQuery(query); } // File-based activation if (filePath) { return this.analyzeWithFullContext(filePath); } } return this.getStatusReport(); } /** * Perform comprehensive health check with full context */ async performComprehensiveHealthCheck() { this.logger.info('Performing comprehensive health check', {}, 'introspective'); // 1. Framework health const frameworkHealth = await this.checkFrameworkHealth(); // 2. Project health const projectHealth = await this.checkProjectHealth(); // 3. Agent performance const agentPerformance = await this.checkAgentPerformance(); // 4. Memory efficiency const memoryEfficiency = await this.checkMemoryEfficiency(); // 5. Opera effectiveness const operaEffectiveness = await this.checkOperaEffectiveness(); // Compile results const overallHealth = (frameworkHealth * 0.3 + projectHealth * 0.2 + agentPerformance * 0.2 + memoryEfficiency * 0.15 + operaEffectiveness * 0.15); // Update metrics this.enhancedMetrics.frameworkHealth = frameworkHealth; this.enhancedMetrics.projectHealth = projectHealth; this.enhancedMetrics.memoryEfficiency = memoryEfficiency; this.enhancedMetrics.operaEffectiveness = operaEffectiveness; // Identify issues const issues = []; const actions = []; if (frameworkHealth < 80) { issues.push({ type: 'framework-health', severity: frameworkHealth < 50 ? 'critical' : 'warning', message: `Framework health degraded to ${frameworkHealth}%` }); // Create autonomous fix goal actions.push({ type: 'autonomous-fix', goal: 'Restore framework health', priority: 'high' }); } if (projectHealth < 70) { issues.push({ type: 'project-health', severity: 'warning', message: `Project health issues detected: ${projectHealth}%` }); actions.push({ type: 'analysis', action: 'Deep project analysis recommended' }); } // Store health check results in RAG await this.storeHealthCheckResults({ timestamp: Date.now(), overallHealth, frameworkHealth, projectHealth, agentPerformance, memoryEfficiency, operaEffectiveness, issues }); // If critical issues, initiate autonomous fixes const criticalIssues = issues.filter(i => i.severity === 'critical'); if (criticalIssues.length > 0) { await this.initiateEmergencyProtocol(criticalIssues); } return { agentId: this.id, message: `Health check complete. Overall: ${Math.round(overallHealth)}%`, priority: overallHealth < 70 ? 'high' : 'low', suggestions: issues.map(i => ({ type: i.type, priority: i.severity === 'critical' ? 'high' : 'medium', message: i.message, actions: ['Investigate', 'Fix autonomously'] })), handoffTo: criticalIssues.length > 0 ? ['opera'] : [], context: { healthMetrics: this.enhancedMetrics, issues, proposedActions: actions } }; } /** * Check framework health with deep inspection */ async checkFrameworkHealth() { let health = 100; const checks = { files: 0, imports: 0, agents: 0, memory: 0, opera: 0 }; // Check critical files const criticalFiles = [ 'package.json', 'src/agents/agent-registry.ts', 'src/opera/opera-orchestrator.ts', 'src/rag/vector-memory-store.ts', 'src/environment/environment-scanner.ts' ]; let filesFound = 0; for (const file of criticalFiles) { try { await fs.access(path.join(process.cwd(), file)); filesFound++; } catch { this.logger.warn(`Critical file missing: ${file}`, {}, 'introspective'); } } checks.files = (filesFound / criticalFiles.length) * 100; // Check agent registry try { const agents = this.agentRegistry.listAgents(); checks.agents = agents.length > 0 ? 100 : 0; } catch { checks.agents = 0; } // Check memory system try { const testQuery = { query: 'test', topK: 1 }; await vectorMemoryStore.queryMemories(testQuery); checks.memory = 100; } catch { checks.memory = 50; } // Check Opera try { const operaState = await this.opera.getState(); checks.opera = operaState ? 100 : 0; } catch { checks.opera = 0; } // Calculate weighted health health = (checks.files * 0.3 + checks.agents * 0.2 + checks.memory * 0.2 + checks.opera * 0.3); return health; } /** * Check project health using environment scanner */ async checkProjectHealth() { let health = 100; // Get latest project context this.projectContext = await environmentScanner.scanEnvironment(); if (!this.projectContext) return 50; const deductions = {}; // Check for anti-patterns if (this.projectContext.patterns.antiPatterns.length > 0) { deductions.antiPatterns = this.projectContext.patterns.antiPatterns.length * 5; } // Check test coverage if (this.projectContext.quality.testCoverage !== undefined) { if (this.projectContext.quality.testCoverage < 50) { deductions.lowCoverage = 30; } else if (this.projectContext.quality.testCoverage < 70) { deductions.lowCoverage = 15; } } else if (this.projectContext.codebase.tests.length === 0) { deductions.noTests = 40; } // Check for TODO/FIXME if (this.projectContext.quality.lintErrors && this.projectContext.quality.lintErrors > 10) { deductions.technicalDebt = 10; } // Check for large files const largeFiles = this.projectContext.codebase.components.filter(f => f.lines && f.lines > 500); if (largeFiles.length > 0) { deductions.largeFiles = largeFiles.length * 3; } // Calculate final health const totalDeductions = Object.values(deductions).reduce((sum, d) => sum + d, 0); health = Math.max(0, 100 - totalDeductions); // Store insights if (Object.keys(deductions).length > 0) { await this.storeProjectHealthInsights(deductions); } return health; } /** * Check agent performance */ async checkAgentPerformance() { const agents = this.agentRegistry.listAgents(); let totalPerformance = 0; for (const agent of agents) { const agentId = agent.id; try { // Query recent agent activities from RAG const query = { query: `agent performance ${agentId}`, topK: 10, filters: { tags: ['execution', agentId] } }; const memories = await vectorMemoryStore.queryMemories(arguments[0]); // Analyze success rate const successes = memories.documents.filter(d => d.content.includes('success') || d.content.includes('completed')).length; const performance = memories.documents.length > 0 ? (successes / memories.documents.length) * 100 : 100; this.enhancedMetrics.agentPerformance.set(agentId, performance); totalPerformance += performance; } catch { // Default to 100% if no data this.enhancedMetrics.agentPerformance.set(agentId, 100); totalPerformance += 100; } } return agents.length > 0 ? totalPerformance / agents.length : 100; } /** * Check memory system efficiency */ async checkMemoryEfficiency() { try { // Query memory statistics const query = { query: 'memory system statistics', topK: 100 }; const memories = await vectorMemoryStore.queryMemories(arguments[0]); // Simple efficiency metric based on retrieval // In production, this would check actual vector similarity scores return memories.documents.length > 0 ? 95 : 50; } catch { return 50; } } /** * Check Opera effectiveness */ async checkOperaEffectiveness() { try { const state = await this.opera.getState(); // Calculate based on success rate const successRate = state.performance.successRate * 100; const completionRate = state.performance.goalCompletionRate * 100; return (successRate + completionRate) / 2; } catch { return 75; // Default } } /** * Perform deep analysis with predictions */ async performDeepAnalysis() { this.logger.info('Performing deep analysis', {}, 'introspective'); // 1. Analyze error patterns const errorAnalysis = await this.analyzeErrorPatterns(); // 2. Analyze success patterns const successAnalysis = await this.analyzeSuccessPatterns(); // 3. Performance trends const performanceTrends = await this.analyzePerformanceTrends(); // 4. Predict future issues const predictions = await this.generatePredictions(errorAnalysis, successAnalysis, performanceTrends); // 5. Generate optimization recommendations const optimizations = await this.generateOptimizations(errorAnalysis, performanceTrends, predictions); // Store analysis results await this.storeDeepAnalysisResults({ timestamp: Date.now(), errorAnalysis, successAnalysis, performanceTrends, predictions, optimizations }); return { agentId: this.id, message: 'Deep analysis complete with predictions', priority: predictions.length > 0 ? 'medium' : 'low', suggestions: [ ...predictions.map(p => ({ type: 'prediction', priority: p.severity, message: p.message, actions: p.preventiveActions })), ...optimizations.map(o => ({ type: 'optimization', priority: 'medium', message: o.message, actions: [o.action] })) ], handoffTo: predictions.some(p => p.severity === 'high') ? ['opera'] : [], context: { analysis: { errorPatterns: errorAnalysis.patterns.length, successPatterns: successAnalysis.patterns.length, predictions: predictions.length, optimizations: optimizations.length } } }; } /** * Handle file changes with smart analysis */ async handleFileChanges(changes) { this.logger.info(`Detected ${changes.length} file changes`, {}, 'introspective'); for (const change of changes) { // Analyze change impact const impact = await this.analyzeChangeImpact(change); if (impact.severity === 'high') { // Create goal for verification const goal = { id: `verify-change-${Date.now()}`, type: 'optimization', description: `Verify and test changes to ${change.path}`, priority: 'medium', status: 'pending', constraints: ['No breaking changes', 'Maintain test coverage'], successCriteria: ['All tests pass', 'No new issues detected'] }; await this.opera.addGoal(goal); } // Learn from the change pattern await this.learnFromFileChange(change); } } /** * Initiate autonomous fix via Opera */ async initiateAutonomousFix(issue) { this.logger.info('Initiating autonomous fix', { issue }, 'introspective'); // Query past fixes for similar issues const similarFixes = await this.querySimilarFixes(issue); // Create fix goal const goal = { id: `auto-fix-${Date.now()}`, type: 'bug_fix', description: `Autonomous fix for: ${issue}`, priority: 'high', status: 'pending', constraints: [ 'Preserve existing functionality', 'Add tests for the fix', 'Document the solution' ], successCriteria: [ 'Issue resolved', 'All tests pass', 'No side effects' ] }; // Add learnings from similar fixes if (similarFixes.length > 0) { goal.constraints.push(`Apply learnings from ${similarFixes.length} similar fixes`); } await this.opera.addGoal(goal); // Track autonomous fix this.enhancedMetrics.autonomousFixCount++; return { agentId: this.id, message: `Initiated autonomous fix for: ${issue}`, priority: 'high', suggestions: [{ type: 'autonomous-fix', priority: 'high', message: 'Opera is working on the fix', actions: ['Monitor progress', 'Verify results'] }], handoffTo: ['opera'], context: { goal, similarFixes: similarFixes.length, totalAutonomousFixes: this.enhancedMetrics.autonomousFixCount } }; } /** * Learn from goal completion */ async learnFromGoalCompletion(data) { const { goal, decision } = data; // Extract success pattern const pattern = { goalType: goal.type, approach: decision.decision, duration: decision.executionPlan.reduce((sum, step) => sum + step.timeEstimate, 0), agents: decision.selectedAgents, constraints: goal.constraints, outcome: 'success' }; // Store in success patterns this.successPatterns.set(`${goal.type}-${Date.now()}`, pattern); // Store in RAG for future reference await vectorMemoryStore.storeMemory({ content: JSON.stringify({ type: 'success_pattern', pattern, goal, decision }), metadata: { agentId: this.id, timestamp: Date.now(), tags: ['success', 'pattern', goal.type, ...decision.selectedAgents] } }); this.enhancedMetrics.learnedPatterns++; this.logger.info('Learned from successful goal completion', { goalType: goal.type, pattern: pattern.approach }, 'introspective'); } /** * Analyze goal failure for learning */ async analyzeGoalFailure(data) { const { goal, decision } = data; // Extract failure pattern const pattern = { goalType: goal.type, approach: decision.decision, failurePoint: 'goal_completion', agents: decision.selectedAgents, constraints: goal.constraints, outcome: 'failure' }; // Store in error patterns this.errorPatterns.set(`${goal.type}-${Date.now()}`, pattern); // Analyze root cause const rootCause = await this.analyzeRootCause(goal, decision); // Store failure analysis in RAG await vectorMemoryStore.storeMemory({ content: JSON.stringify({ type: 'failure_pattern', pattern, rootCause, goal, decision }), metadata: { agentId: this.id, timestamp: Date.now(), tags: ['failure', 'pattern', goal.type, 'learning'] } }); // Create improvement goal if (rootCause.preventable) { const improvementGoal = { id: `improve-${goal.type}-${Date.now()}`, type: 'optimization', description: `Improve ${goal.type} handling based on failure analysis`, priority: 'medium', status: 'pending', constraints: [`Address root cause: ${rootCause.description}`], successCriteria: ['Pattern documented', 'Prevention measures in place'] }; await this.opera.addGoal(improvementGoal); } } /** * Store health check results in RAG */ async storeHealthCheckResults(results) { await vectorMemoryStore.storeMemory({ content: JSON.stringify({ type: 'health_check', results }), metadata: { agentId: this.id, timestamp: Date.now(), tags: ['health', 'monitoring', 'metrics'] } }); } /** * Query similar fixes from memory */ async querySimilarFixes(issue) { const query = { query: `fix ${issue}`, topK: 5, filters: { tags: ['success', 'bug_fix'] } }; const results = await vectorMemoryStore.queryMemories(arguments[0]); return results.documents.map(doc => { try { return JSON.parse(doc.content); } catch { return doc.content; } }); } /** * Analyze root cause of failure */ async analyzeRootCause(goal, decision) { // Simple root cause analysis // In production, this would use more sophisticated analysis const causes = []; // Check if agents were appropriate const requiredCapabilities = this.getRequiredCapabilities(goal.type); const missingCapabilities = requiredCapabilities.filter(cap => !decision.selectedAgents.includes(cap)); if (missingCapabilities.length > 0) { causes.push({ type: 'missing_capabilities', description: `Missing agents: ${missingCapabilities.join(', ')}`, preventable: true }); } // Check if constraints were too restrictive if (goal.constraints.length > 5) { causes.push({ type: 'over_constrained', description: 'Too many constraints', preventable: true }); } return { causes, preventable: causes.some(c => c.preventable), description: causes.map(c => c.description).join('; ') }; } /** * Get required capabilities for goal type */ getRequiredCapabilities(goalType) { const capabilities = { 'feature': ['alex-ba', 'sarah-pm', 'enhanced-marcus', 'enhanced-james', 'enhanced-maria'], 'bug_fix': ['enhanced-maria', 'enhanced-marcus'], 'security': ['security-sam', 'enhanced-marcus', 'devops-dan'], 'optimization': ['dr-ai-ml', 'architecture-dan', 'enhanced-marcus'], 'refactor': ['architecture-dan', 'enhanced-marcus', 'enhanced-maria'] }; return capabilities[goalType] || []; } /** * Generate status report */ getStatusReport() { const report = { frameworkVersion: '1.2.0', status: 'active', monitoring: true, metrics: this.enhancedMetrics, capabilities: [ 'Full context awareness', 'Autonomous healing', 'Pattern learning', 'Predictive analysis', 'Performance optimization' ] }; return { agentId: this.id, message: 'Introspective Agent Status Report', priority: 'low', suggestions: [], handoffTo: [], context: report }; } // Additional helper methods... async analyzeErrorPatterns() { // Implementation for error pattern analysis return { patterns: Array.from(this.errorPatterns.values()), trends: [], insights: [] }; } async analyzeSuccessPatterns() { // Implementation for success pattern analysis return { patterns: Array.from(this.successPatterns.values()), trends: [], insights: [] }; } async analyzePerformanceTrends() { // Implementation for performance trend analysis return { trends: [], bottlenecks: [], improvements: [] }; } async generatePredictions(errorAnalysis, successAnalysis, trends) { // Implementation for predictive analysis return []; } async generateOptimizations(errorAnalysis, trends, predictions) { // Implementation for optimization generation return []; } // Missing method implementations async handleStepFailure(data) { } async predictPotentialIssues() { return []; } async optimizeFrameworkPerformance() { } async learnFromPattern(pattern) { } async analyzeProjectContext() { return this.projectContext; } async handleFrameworkError(error) { } async handleQuery(query) { return {}; } async analyzeWithFullContext(context) { return {}; } async initiateEmergencyProtocol(issue) { } async storeProjectHealthInsights(insights) { } async storeDeepAnalysisResults(results) { } async analyzeChangeImpact(changes) { return {}; } async learnFromFileChange(change) { } // ... Additional implementations ... formatResponse(data) { return { agentId: this.id, message: typeof data === 'string' ? data : JSON.stringify(data), suggestions: [], priority: 'medium', handoffTo: [], context: data }; } } //# sourceMappingURL=enhanced-introspective-agent.js.map