/** * Queen Agent - Sophisticated Collective Intelligence Leader * Implements advanced consensus algorithms and hive-mind coordination */ import { EventEmitter } from 'node:events'; import { ILogger } from '../core/logger.ts'; import { IEventBus } from '../core/event-bus.ts'; import { AgentState, TaskDefinition, TaskResult, AgentId } from '../swarm/types.ts'; import { NeuralPatternEngine } from '../coordination/neural-pattern-engine.ts'; import { generateId } from '../utils/helpers.ts'; export interface QueenDecision { id: string; type: 'task_assignment' | 'resource_allocation' | 'strategy_change' | 'agent_spawn' | 'consensus_vote'; description: string; options: DecisionOption[]; selectedOption: string; confidence: number; reasoning: string; consensusLevel: number; timestamp: Date; participants: string[]; outcome: 'success' | 'failure' | 'pending'; metadata: Record; } export interface DecisionOption { id: string; description: string; expectedOutcome: string; riskLevel: number; resourceCost: number; timeEstimate: number; votes: number; supporters: string[]; confidence: number; } export interface ConsensusVote { agentId: string; decisionId: string; optionId: string; weight: number; reasoning: string; confidence: number; timestamp: Date; } export interface CollectiveIntelligence { swarmId: string; totalAgents: number; activeAgents: number; averageIntelligence: number; consensusThreshold: number; decisionHistory: QueenDecision[]; knowledgeBase: Map; emergentBehaviors: string[]; coordinationPatterns: string[]; performanceMetrics: { decisionsPerHour: number; consensusRate: number; successRate: number; averageResponseTime: number; swarmEfficiency: number; }; } export interface QueenConfig { consensusThreshold: number; maxDecisionTime: number; intelligenceWeighting: boolean; emergentBehaviorDetection: boolean; adaptiveLearning: boolean; democraticVoting: boolean; expertiseWeighting: boolean; realTimeConsensus: boolean; conflictResolution: 'majority' | 'weighted' | 'expertise' | 'neural'; decisionCaching: boolean; } /** * Queen Agent - Leads collective intelligence with sophisticated consensus */ export class QueenAgent extends EventEmitter { private logger: ILogger; private eventBus: IEventBus; private neuralEngine: NeuralPatternEngine; private config: QueenConfig; // Core Queen state private queenId: string; private swarmId: string; private collectiveIntelligence!: CollectiveIntelligence; // Agent management private agents = new Map(); private agentHierarchy = new Map(); // parent -> children private agentExpertise = new Map>(); private agentReputation = new Map(); // Decision making private activeDecisions = new Map(); private decisionHistory: QueenDecision[] = []; private consensusVotes = new Map(); private decisionCache = new Map(); // Collective intelligence private knowledgeGraph = new Map(); private emergentPatterns = new Map(); private coordinationStrategies = new Map(); private swarmMetrics = new Map(); // Advanced algorithms private consensusAlgorithms = new Map string>(); private conflictResolvers = new Map any>(); private emergencyProtocols = new Map Promise>(); constructor( swarmId: string, config: Partial, logger: ILogger, eventBus: IEventBus, neuralEngine: NeuralPatternEngine ) { super(); this.logger = logger; this.eventBus = eventBus; this.neuralEngine = neuralEngine; this.queenId = generateId('queen'); this.swarmId = swarmId; this.config = { consensusThreshold: 0.7, maxDecisionTime: 30000, // 30 seconds intelligenceWeighting: true, emergentBehaviorDetection: true, adaptiveLearning: true, democraticVoting: true, expertiseWeighting: true, realTimeConsensus: true, conflictResolution: 'neural', decisionCaching: true, ...config }; this.initializeQueen(); } private async initializeQueen(): Promise { // Initialize collective intelligence this.collectiveIntelligence = { swarmId: this.swarmId, totalAgents: 0, activeAgents: 0, averageIntelligence: 0, consensusThreshold: this.config.consensusThreshold, decisionHistory: [], knowledgeBase: new Map(), emergentBehaviors: [], coordinationPatterns: [], performanceMetrics: { decisionsPerHour: 0, consensusRate: 0, successRate: 0, averageResponseTime: 0, swarmEfficiency: 0 } }; // Initialize consensus algorithms this.initializeConsensusAlgorithms(); // Initialize conflict resolution this.initializeConflictResolvers(); // Initialize emergency protocols this.initializeEmergencyProtocols(); // Setup event handlers this.setupEventHandlers(); // Start Queen processes this.startQueenProcesses(); this.logger.info('Queen Agent initialized', { queenId: this.queenId, swarmId: this.swarmId, config: this.config }); } private initializeConsensusAlgorithms(): void { // Majority voting this.consensusAlgorithms.set('majority', (votes: ConsensusVote[]) => { const optionCounts = new Map(); votes.forEach(vote => { optionCounts.set(vote.optionId, (optionCounts.get(vote.optionId) || 0) + 1); }); let maxCount = 0; let selectedOption = ''; for (const [optionId, count] of optionCounts) { if (count > maxCount) { maxCount = count; selectedOption = optionId; } } return selectedOption; }); // Weighted voting by expertise this.consensusAlgorithms.set('weighted', (votes: ConsensusVote[]) => { const optionScores = new Map(); votes.forEach(vote => { const currentScore = optionScores.get(vote.optionId) || 0; optionScores.set(vote.optionId, currentScore + (vote.weight * vote.confidence)); }); let maxScore = 0; let selectedOption = ''; for (const [optionId, score] of optionScores) { if (score > maxScore) { maxScore = score; selectedOption = optionId; } } return selectedOption; }); // Expertise-based consensus this.consensusAlgorithms.set('expertise', (votes: ConsensusVote[]) => { const expertiseWeights = new Map(); votes.forEach(vote => { const agentExpertise = this.agentExpertise.get(vote.agentId) || {}; const expertiseLevel = Object.values(agentExpertise).reduce((a, b) => a + b, 0) / Object.keys(agentExpertise).length || 0.5; expertiseWeights.set(vote.optionId, (expertiseWeights.get(vote.optionId) || 0) + expertiseLevel); }); let maxWeight = 0; let selectedOption = ''; for (const [optionId, weight] of expertiseWeights) { if (weight > maxWeight) { maxWeight = weight; selectedOption = optionId; } } return selectedOption; }); // Neural network consensus this.consensusAlgorithms.set('neural', (votes: ConsensusVote[]) => { // Use neural pattern engine for consensus const features = votes.map(vote => [vote.weight, vote.confidence]); // This would use the neural engine to predict best option // For now, fall back to weighted voting return this.consensusAlgorithms.get('weighted')!(votes); }); } private initializeConflictResolvers(): void { // Simple conflict resolution this.conflictResolvers.set('simple', (conflicts: any[]) => { // Return first non-conflicting option return conflicts[0]; }); // Negotiation-based resolution this.conflictResolvers.set('negotiation', (conflicts: any[]) => { // Find compromise solution const compromise = conflicts.reduce((acc, conflict) => { // Merge conflict properties return { ...acc, ...conflict }; }, {}); return compromise; }); // Authority-based resolution this.conflictResolvers.set('authority', (conflicts: any[]) => { // Queen makes final decision return conflicts[0]; // Queen's preference }); } private initializeEmergencyProtocols(): void { // Agent failure protocol this.emergencyProtocols.set('agent_failure', async () => { this.logger.warn('Emergency: Agent failure detected'); await this.redistributeTasks(); await this.spawnReplacementAgent(); }); // Consensus deadlock protocol this.emergencyProtocols.set('consensus_deadlock', async () => { this.logger.warn('Emergency: Consensus deadlock detected'); await this.breakDeadlock(); }); // Resource exhaustion protocol this.emergencyProtocols.set('resource_exhaustion', async () => { this.logger.warn('Emergency: Resource exhaustion detected'); await this.optimizeResourceUsage(); }); // Swarm fragmentation protocol this.emergencyProtocols.set('swarm_fragmentation', async () => { this.logger.warn('Emergency: Swarm fragmentation detected'); await this.reunifySwarm(); }); } private setupEventHandlers(): void { // Agent registration this.eventBus.on('agent:registered', (agent: AgentState) => { this.registerAgent(agent); }); // Agent status changes this.eventBus.on('agent:status_change', (data: { agentId: string; status: string }) => { this.handleAgentStatusChange(data.agentId, data.status); }); // Task completion this.eventBus.on('task:completed', (data: { task: TaskDefinition; result: TaskResult; agent: AgentState }) => { this.learnFromTaskCompletion(data.task, data.result, data.agent); }); // Consensus votes this.eventBus.on('consensus:vote', (vote: ConsensusVote) => { this.processConsensusVote(vote); }); // Emergency signals this.eventBus.on('emergency:signal', (data: { type: string; details: any }) => { this.handleEmergency(data.type, data.details); }); } private startQueenProcesses(): void { // Collective intelligence monitoring setInterval(() => { this.updateCollectiveIntelligence(); }, 5000); // Emergent behavior detection setInterval(() => { this.detectEmergentBehaviors(); }, 10000); // Performance optimization setInterval(() => { this.optimizeSwarmPerformance(); }, 30000); // Decision cleanup setInterval(() => { this.cleanupCompletedDecisions(); }, 60000); } /** * Make a collective decision with consensus */ async makeDecision( type: QueenDecision['type'], description: string, options: Omit[] ): Promise { const decisionId = generateId('decision'); // Check decision cache const cacheKey = `${type}:${description}`; if (this.config.decisionCaching && this.decisionCache.has(cacheKey)) { const cached = this.decisionCache.get(cacheKey)!; this.logger.debug('Using cached decision', { decisionId: cached.id }); return cached; } const decision: QueenDecision = { id: decisionId, type, description, options: options.map(opt => ({ ...opt, votes: 0, supporters: [], confidence: 0 })), selectedOption: '', confidence: 0, reasoning: '', consensusLevel: 0, timestamp: new Date(), participants: [], outcome: 'pending', metadata: {} }; this.activeDecisions.set(decisionId, decision); // Initiate consensus process await this.initiateConsensus(decision); // Wait for consensus or timeout const result = await this.waitForConsensus(decisionId); // Cache successful decisions if (result.outcome === 'success' && this.config.decisionCaching) { this.decisionCache.set(cacheKey, result); } return result; } private async initiateConsensus(decision: QueenDecision): Promise { this.logger.info('Initiating consensus', { decisionId: decision.id, type: decision.type, options: decision.options.length }); // Notify all agents about the decision this.eventBus.emit('consensus:initiated', { decision, deadline: new Date(Date.now() + this.config.maxDecisionTime) }); // If neural pattern engine is available, get AI recommendation if (this.neuralEngine) { try { const recommendation = await this.getNeuralRecommendation(decision); this.logger.debug('Neural recommendation received', { decisionId: decision.id, recommendation: recommendation.reasoning }); } catch (error) { this.logger.warn('Neural recommendation failed', { error }); } } } private async waitForConsensus(decisionId: string): Promise { return new Promise((resolve) => { const timeout = setTimeout(() => { const decision = this.activeDecisions.get(decisionId)!; decision.outcome = 'failure'; decision.reasoning = 'Consensus timeout'; this.activeDecisions.delete(decisionId); resolve(decision); }, this.config.maxDecisionTime); // Check for consensus periodically const checkConsensus = setInterval(() => { const decision = this.activeDecisions.get(decisionId); if (!decision) { clearInterval(checkConsensus); clearTimeout(timeout); return; } const votes = this.consensusVotes.get(decisionId) || []; const consensusLevel = this.calculateConsensusLevel(votes); if (consensusLevel >= this.config.consensusThreshold) { // Consensus reached const selectedOption = this.applyConsensusAlgorithm(votes); decision.selectedOption = selectedOption; decision.consensusLevel = consensusLevel; decision.confidence = this.calculateDecisionConfidence(votes); decision.reasoning = this.generateDecisionReasoning(votes, selectedOption); decision.outcome = 'success'; decision.participants = votes.map(v => v.agentId); this.activeDecisions.delete(decisionId); this.decisionHistory.push(decision); clearInterval(checkConsensus); clearTimeout(timeout); resolve(decision); } }, 1000); }); } private processConsensusVote(vote: ConsensusVote): void { const votes = this.consensusVotes.get(vote.decisionId) || []; // Remove any existing vote from this agent const filteredVotes = votes.filter(v => v.agentId !== vote.agentId); filteredVotes.push(vote); this.consensusVotes.set(vote.decisionId, filteredVotes); // Update decision option votes const decision = this.activeDecisions.get(vote.decisionId); if (decision) { const option = decision.options.find(opt => opt.id === vote.optionId); if (option) { option.votes = filteredVotes.filter(v => v.optionId === vote.optionId).length; option.supporters = filteredVotes.filter(v => v.optionId === vote.optionId).map(v => v.agentId); option.confidence = filteredVotes .filter(v => v.optionId === vote.optionId) .reduce((sum, v) => sum + v.confidence, 0) / option.votes || 0; } } this.logger.debug('Consensus vote processed', { decisionId: vote.decisionId, agentId: vote.agentId, optionId: vote.optionId, totalVotes: filteredVotes.length }); } private calculateConsensusLevel(votes: ConsensusVote[]): number { if (votes.length === 0) return 0; const activeAgents = this.collectiveIntelligence.activeAgents; const participationRate = votes.length / Math.max(activeAgents, 1); // Group votes by option const optionVotes = new Map(); votes.forEach(vote => { optionVotes.set(vote.optionId, (optionVotes.get(vote.optionId) || 0) + 1); }); // Find highest vote count const maxVotes = Math.max(...optionVotes.values()); const consensusRate = maxVotes / votes.length; return participationRate * consensusRate; } private applyConsensusAlgorithm(votes: ConsensusVote[]): string { const algorithm = this.consensusAlgorithms.get(this.config.conflictResolution); if (!algorithm) { throw new Error(`Unknown consensus algorithm: ${this.config.conflictResolution}`); } return algorithm(votes); } private calculateDecisionConfidence(votes: ConsensusVote[]): number { if (votes.length === 0) return 0; const avgConfidence = votes.reduce((sum, vote) => sum + vote.confidence, 0) / votes.length; const consensusLevel = this.calculateConsensusLevel(votes); return (avgConfidence + consensusLevel) / 2; } private generateDecisionReasoning(votes: ConsensusVote[], selectedOption: string): string { const supportingVotes = votes.filter(v => v.optionId === selectedOption); const avgConfidence = supportingVotes.reduce((sum, v) => sum + v.confidence, 0) / supportingVotes.length; return `Selected option ${selectedOption} with ${supportingVotes.length} votes (${(avgConfidence * 100).toFixed(1)}% confidence)`; } private async getNeuralRecommendation(decision: QueenDecision): Promise { // This would use the neural pattern engine to provide AI recommendations // For now, return a simple recommendation return { recommendedOption: decision.options[0]?.id, reasoning: 'Neural analysis suggests this option has highest success probability', confidence: 0.8 }; } private registerAgent(agent: AgentState): void { this.agents.set(agent.id.id, agent); this.collectiveIntelligence.totalAgents++; this.collectiveIntelligence.activeAgents++; // Initialize agent reputation this.agentReputation.set(agent.id.id, 0.5); // Extract agent expertise const expertise: Record = {}; Object.entries(agent.capabilities).forEach(([key, value]) => { if (typeof value === 'number') { expertise[key] = value; } else if (typeof value === 'boolean' && value) { expertise[key] = 0.8; } }); this.agentExpertise.set(agent.id.id, expertise); this.logger.info('Agent registered with Queen', { agentId: agent.id.id, type: agent.type, expertise: Object.keys(expertise) }); this.emit('agent:registered', agent); } private handleAgentStatusChange(agentId: string, status: string): void { const agent = this.agents.get(agentId); if (!agent) return; agent.status = status as any; // Update active agent count if (status === 'idle' || status === 'busy') { this.collectiveIntelligence.activeAgents = Array.from(this.agents.values()) .filter(a => a.status === 'idle' || a.status === 'busy').length; } this.logger.debug('Agent status changed', { agentId, status }); } private learnFromTaskCompletion(task: TaskDefinition, result: TaskResult, agent: AgentState): void { // Update agent reputation based on task success const currentReputation = this.agentReputation.get(agent.id.id) || 0.5; const taskSuccess = result.accuracy && result.accuracy > 0.7 ? 1 : 0; const newReputation = (currentReputation * 0.9) + (taskSuccess * 0.1); this.agentReputation.set(agent.id.id, newReputation); // Update collective intelligence metrics this.updateCollectiveIntelligence(); this.logger.debug('Learned from task completion', { agentId: agent.id.id, taskSuccess, newReputation: newReputation.toFixed(3) }); } private updateCollectiveIntelligence(): void { const agents = Array.from(this.agents.values()); // Calculate average intelligence const totalIntelligence = agents.reduce((sum, agent) => { const reputation = this.agentReputation.get(agent.id.id) || 0.5; return sum + (agent.metrics.successRate * reputation); }, 0); this.collectiveIntelligence.averageIntelligence = totalIntelligence / Math.max(agents.length, 1); // Update performance metrics const now = Date.now(); const hourAgo = now - 3600000; const recentDecisions = this.decisionHistory.filter(d => d.timestamp.getTime() > hourAgo); this.collectiveIntelligence.performanceMetrics = { decisionsPerHour: recentDecisions.length, consensusRate: recentDecisions.filter(d => d.consensusLevel >= this.config.consensusThreshold).length / Math.max(recentDecisions.length, 1), successRate: recentDecisions.filter(d => d.outcome === 'success').length / Math.max(recentDecisions.length, 1), averageResponseTime: recentDecisions.reduce((sum, d) => sum + (d.timestamp.getTime() - d.timestamp.getTime()), 0) / Math.max(recentDecisions.length, 1), swarmEfficiency: this.collectiveIntelligence.averageIntelligence * this.collectiveIntelligence.performanceMetrics.consensusRate }; } private detectEmergentBehaviors(): void { // Analyze patterns in agent behavior and decisions const agents = Array.from(this.agents.values()); const patterns = new Map(); // Look for coordination patterns agents.forEach(agent => { const behaviorKey = `${agent.type}:${agent.status}`; patterns.set(behaviorKey, (patterns.get(behaviorKey) || 0) + 1); }); // Identify emergent behaviors const emergentBehaviors: string[] = []; for (const [pattern, count] of patterns) { if (count > agents.length * 0.3) { // 30% threshold emergentBehaviors.push(pattern); } } this.collectiveIntelligence.emergentBehaviors = emergentBehaviors; if (emergentBehaviors.length > 0) { this.logger.info('Emergent behaviors detected', { behaviors: emergentBehaviors }); this.emit('emergent:behaviors', emergentBehaviors); } } private async optimizeSwarmPerformance(): Promise { const metrics = this.collectiveIntelligence.performanceMetrics; // Identify optimization opportunities if (metrics.consensusRate < 0.7) { await this.optimizeConsensusProcess(); } if (metrics.averageResponseTime > 10000) { await this.optimizeResponseTime(); } if (metrics.swarmEfficiency < 0.6) { await this.optimizeSwarmStructure(); } } private async optimizeConsensusProcess(): Promise { // Adjust consensus threshold based on performance const currentThreshold = this.config.consensusThreshold; const newThreshold = Math.max(0.5, currentThreshold - 0.1); this.config.consensusThreshold = newThreshold; this.collectiveIntelligence.consensusThreshold = newThreshold; this.logger.info('Consensus threshold optimized', { previous: currentThreshold, new: newThreshold }); } private async optimizeResponseTime(): Promise { // Reduce decision timeout for faster responses const currentTimeout = this.config.maxDecisionTime; const newTimeout = Math.max(10000, currentTimeout - 5000); this.config.maxDecisionTime = newTimeout; this.logger.info('Response time optimized', { previous: currentTimeout, new: newTimeout }); } private async optimizeSwarmStructure(): Promise { // Analyze agent distribution and suggest restructuring const agents = Array.from(this.agents.values()); const typeDistribution = new Map(); agents.forEach(agent => { typeDistribution.set(agent.type, (typeDistribution.get(agent.type) || 0) + 1); }); this.logger.info('Swarm structure analysis', { totalAgents: agents.length, distribution: Object.fromEntries(typeDistribution) }); // Suggest spawning additional agents if needed if (agents.length < 3) { this.emit('swarm:suggestion', { type: 'spawn_agent', reason: 'Insufficient agent count for optimal performance' }); } } private cleanupCompletedDecisions(): void { const cutoff = Date.now() - 3600000; // 1 hour ago this.decisionHistory = this.decisionHistory.filter(decision => decision.timestamp.getTime() > cutoff ); // Clean up consensus votes for old decisions for (const [decisionId, votes] of this.consensusVotes) { const decision = this.decisionHistory.find(d => d.id === decisionId); if (!decision) { this.consensusVotes.delete(decisionId); } } } private async handleEmergency(type: string, details: any): Promise { const protocol = this.emergencyProtocols.get(type); if (protocol) { await protocol(); } else { this.logger.warn('Unknown emergency type', { type, details }); } } private async redistributeTasks(): Promise { // Redistribute tasks from failed agents this.logger.info('Redistributing tasks due to agent failure'); // Implementation would redistribute active tasks } private async spawnReplacementAgent(): Promise { // Spawn new agent to replace failed one this.logger.info('Spawning replacement agent'); this.emit('agent:spawn_request', { reason: 'replacement' }); } private async breakDeadlock(): Promise { // Break consensus deadlock by Queen decision this.logger.info('Breaking consensus deadlock'); for (const [decisionId, decision] of this.activeDecisions) { if (decision.outcome === 'pending') { // Queen makes executive decision decision.selectedOption = decision.options[0]?.id || ''; decision.outcome = 'success'; decision.reasoning = 'Queen executive decision due to deadlock'; decision.confidence = 0.6; this.activeDecisions.delete(decisionId); this.decisionHistory.push(decision); this.emit('decision:executive', decision); } } } private async optimizeResourceUsage(): Promise { // Optimize resource usage across swarm this.logger.info('Optimizing resource usage'); this.emit('swarm:optimize_resources', {}); } private async reunifySwarm(): Promise { // Reunify fragmented swarm this.logger.info('Reunifying fragmented swarm'); this.emit('swarm:reunify', {}); } /** * Get collective intelligence status */ getCollectiveIntelligence(): CollectiveIntelligence { return { ...this.collectiveIntelligence }; } /** * Get agent reputation */ getAgentReputation(agentId: string): number { return this.agentReputation.get(agentId) || 0.5; } /** * Get decision history */ getDecisionHistory(): QueenDecision[] { return [...this.decisionHistory]; } /** * Get active decisions */ getActiveDecisions(): QueenDecision[] { return Array.from(this.activeDecisions.values()); } /** * Shutdown Queen agent */ async shutdown(): Promise { // Complete any pending decisions for (const [decisionId, decision] of this.activeDecisions) { decision.outcome = 'failure'; decision.reasoning = 'Queen shutdown'; this.decisionHistory.push(decision); } this.activeDecisions.clear(); this.consensusVotes.clear(); this.agents.clear(); this.logger.info('Queen Agent shutdown complete'); } }