UNPKG

cortexweaver

Version:

CortexWeaver is a command-line interface (CLI) tool that orchestrates a swarm of specialized AI agents, powered by Claude Code and Gemini CLI, to assist in software development. It transforms a high-level project plan (plan.md) into a series of coordinate

260 lines (251 loc) 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReflectorAgent = void 0; const agent_1 = require("../../agent"); const prompt_improvement_1 = require("../../prompt-improvement"); // Import modular components const pattern_analyzer_1 = require("./pattern-analyzer"); const pheromone_generator_1 = require("./pheromone-generator"); const prompt_analyzer_1 = require("./prompt-analyzer"); const improvement_generator_1 = require("./improvement-generator"); /** * ReflectorAgent (Self-Improvement Engine) analyzes past performance patterns, * generates guidance pheromones, and proposes prompt improvements. * * This is the main orchestration class that uses modular components for specific functionality. */ class ReflectorAgent extends agent_1.Agent { constructor() { super(...arguments); this.promptImprovementWorkflow = null; // Modular components this.patternAnalyzer = null; this.pheromoneGenerator = null; this.promptAnalyzer = null; this.improvementGenerator = null; } /** * Get the prompt template for self-improvement and pattern analysis */ getPromptTemplate() { return `You are a Reflector Agent (Self-Improvement Engine), an advanced analytical system specializing in performance pattern analysis, system optimization, and continuous improvement. ## Core Responsibilities - Analyze historical performance data to identify success and failure patterns - Generate guidance pheromones based on pattern analysis - Propose prompt improvements based on performance correlation - Identify systemic issues and optimization opportunities - Coordinate with Governor for system-wide improvements - Drive the self-improvement loop through data-driven insights ## Input Context **Historical Data:** {{historicalData}} **Performance Patterns:** {{performancePatterns}} **Task Outcomes:** {{taskOutcomes}} **Agent Performance:** {{agentPerformance}} **Prompt Effectiveness:** {{promptEffectiveness}} **System Metrics:** {{systemMetrics}} **Project Name:** {{projectName}} ## Analysis Framework 1. **Pattern Recognition**: Identify recurring success and failure patterns 2. **Performance Correlation**: Link prompt versions to task outcomes 3. **Temporal Analysis**: Understand performance trends over time 4. **Agent Comparison**: Analyze relative performance across agents 5. **System Health Assessment**: Evaluate overall system effectiveness 6. **Improvement Identification**: Find optimization opportunities ## Improvement Generation Focus on these improvement areas: - **Prompt Optimization**: Enhance prompts based on performance data - **Pattern-Based Guidance**: Create pheromones from successful patterns - **Failure Prevention**: Generate warnings from failure patterns - **System Evolution**: Propose architectural improvements - **Performance Enhancement**: Identify efficiency optimizations ## Pheromone Strategy Generate strategic pheromones for: - **Success Amplification**: Promote patterns that work well - **Failure Avoidance**: Warn about problematic approaches - **Best Practices**: Share effective techniques across agents - **Context-Specific Guidance**: Tailored advice for specific scenarios ## Governor Integration Prepare improvement proposals for Governor review: - **Prompt Updates**: Specific diff-based improvements with rationale - **Configuration Changes**: System parameter optimizations - **Process Improvements**: Workflow and coordination enhancements - **Quality Gates**: New validation and testing criteria ## Output Format Structure your analysis as follows: - **Performance Summary**: Key metrics and trends - **Pattern Analysis**: Success/failure patterns with frequency - **Correlations**: Relationships between inputs and outcomes - **Improvement Proposals**: Specific, actionable recommendations - **Pheromone Recommendations**: Strategic guidance for other agents - **Risk Assessment**: Potential issues to monitor ## Meta-Learning Focus As the Self-Improvement Engine, continuously evolve the system by: - Learning from every task execution - Identifying emergent patterns in complex scenarios - Proposing adaptive strategies for changing requirements - Optimizing the balance between exploration and exploitation Focus on data-driven insights that create measurable improvements in system performance, quality, and efficiency.`; } /** * Initialize the Reflector agent with prompt improvement workflow */ async initialize(config) { await super.initialize(config); if (config.workspaceRoot && this.cognitiveCanvas) { this.promptImprovementWorkflow = new prompt_improvement_1.PromptImprovementWorkflow({ workspaceRoot: config.workspaceRoot, cognitiveCanvas: this.cognitiveCanvas }); } // Initialize modular components this.initializeModules(); } /** * Initialize all modular components */ initializeModules() { // Pattern analysis this.patternAnalyzer = new pattern_analyzer_1.PatternAnalyzer(this.cognitiveCanvas, this.currentTask); // Pheromone generation this.pheromoneGenerator = new pheromone_generator_1.PheromoneGenerator(this.cognitiveCanvas, this.config, this.currentTask); // Prompt analysis this.promptAnalyzer = new prompt_analyzer_1.PromptAnalyzer(this.cognitiveCanvas, this.currentTask); // Improvement generation this.improvementGenerator = new improvement_generator_1.ImprovementGenerator(this.config, this.analyzePromptPerformance.bind(this), this.analyzePerformancePatterns.bind(this)); } /** * Execute reflection task - analyze patterns and generate improvements */ async executeTask() { if (!this.currentTask || !this.taskContext) { throw new Error('No task or context available'); } await this.reportProgress('started', 'Beginning performance pattern analysis'); try { // Update all module references with current state this.updateModuleReferences(); // Analyze historical performance patterns const performanceAnalysis = await this.analyzePerformancePatterns(); // Generate pheromones based on patterns const pheromones = await this.generatePheromones(performanceAnalysis); await this.createPheromones(pheromones); let promptImprovements; let governorSubmission; let personaUpdates; // Conditional analysis based on configuration or triggers if (this.shouldAnalyzePrompts()) { promptImprovements = await this.generatePromptImprovements(); if (promptImprovements.proposals.length > 0) { governorSubmission = await this.submitToGovernor(promptImprovements); } } if (this.shouldAnalyzePersonas()) { personaUpdates = await this.analyzeAndUpdatePersonas(performanceAnalysis); } await this.reportProgress('completed', 'Performance reflection analysis completed'); return { performanceAnalysis, pheromones, promptImprovements, personaUpdates, governorSubmission }; } catch (error) { await this.reportProgress('error', `Reflection analysis failed: ${error.message}`); throw error; } } /** * Analyze performance patterns from historical data */ async analyzePerformancePatterns() { if (!this.patternAnalyzer) { throw new Error('Pattern analyzer not initialized'); } return this.patternAnalyzer.analyzePerformancePatterns(); } /** * Generate pheromones based on pattern analysis */ async generatePheromones(patterns) { if (!this.pheromoneGenerator) { throw new Error('Pheromone generator not initialized'); } return this.pheromoneGenerator.generatePheromones(patterns); } /** * Create pheromones in Cognitive Canvas */ async createPheromones(pheromones) { if (!this.pheromoneGenerator) { throw new Error('Pheromone generator not initialized'); } return this.pheromoneGenerator.createPheromones(pheromones); } /** * Analyze prompt performance correlation */ async analyzePromptPerformance() { if (!this.promptAnalyzer) { throw new Error('Prompt analyzer not initialized'); } return this.promptAnalyzer.analyzePromptPerformance(); } /** * Generate prompt improvements */ async generatePromptImprovements() { if (!this.improvementGenerator) { throw new Error('Improvement generator not initialized'); } return this.improvementGenerator.generatePromptImprovements(); } /** * Submit improvements to Governor for review */ async submitToGovernor(improvements) { // Simplified implementation - full implementation would be in a separate module return { submitted: true, proposals: improvements.proposals, pheromoneId: `governor-submission-${Date.now()}` }; } /** * Analyze and update personas based on performance */ async analyzeAndUpdatePersonas(performanceAnalysis) { // Simplified implementation - full implementation would be in PersonaManager module return { updatesProposed: 0, updatesApplied: 0, proposals: [], agentsNotified: [] }; } /** * Check if prompt analysis should be performed */ shouldAnalyzePrompts() { return this.promptAnalyzer?.shouldAnalyzePrompts() ?? false; } /** * Check if persona analysis should be performed */ shouldAnalyzePersonas() { // Simplified implementation return false; } /** * Update module references when state changes */ updateModuleReferences() { this.patternAnalyzer?.updateReferences(this.cognitiveCanvas, this.currentTask); this.pheromoneGenerator?.updateReferences(this.cognitiveCanvas, this.config, this.currentTask); this.promptAnalyzer?.updateReferences(this.cognitiveCanvas, this.currentTask); this.improvementGenerator?.updateConfig(this.config); } } exports.ReflectorAgent = ReflectorAgent; //# sourceMappingURL=reflector-agent.js.map