claude-flow-tbowman01
Version:
Enterprise-grade AI agent orchestration with ruv-swarm integration (Alpha Release)
409 lines (400 loc) โข 17.3 kB
JavaScript
/**
* Hive Mind Command - Multi-agent swarm coordination with consensus mechanisms
*/
import { success, error, info } from '../cli-core.js';
import { generateId } from '../../utils/helpers.js';
import { SwarmCoordinator } from '../../coordination/swarm-coordinator.js';
import { SwarmMemoryManager } from '../../memory/swarm-memory.js';
export async function hiveAction(ctx) {
if (ctx.flags.help || ctx.flags.h) {
showHiveHelp();
return;
}
const objective = ctx.args.join(' ').trim();
if (!objective) {
error('Usage: hive <objective> [options]');
showHiveHelp();
return;
}
const options = {
objective,
topology: ctx.flags.topology || 'hierarchical',
consensus: ctx.flags.consensus || 'quorum',
maxAgents: Number(ctx.flags.maxAgents || ctx.flags['max-agents']) || 8,
timeout: Number(ctx.flags.timeout) || 60,
monitor: Boolean(ctx.flags.monitor) || false,
background: Boolean(ctx.flags.background) || false,
memoryNamespace: String(ctx.flags['memory-namespace']) || 'hive',
qualityThreshold: Number(ctx.flags['quality-threshold']) || 0.8,
sparc: ctx.flags.sparc !== false,
};
const hiveId = generateId('hive');
success(`๐ Initializing Hive Mind: ${hiveId}`);
console.log(`๐ Queen Genesis coordinating...`);
console.log(`๐ Objective: ${objective}`);
console.log(`๐๏ธ Topology: ${options.topology}`);
console.log(`๐ณ๏ธ Consensus: ${options.consensus}`);
console.log(`๐ค Max Agents: ${options.maxAgents}`);
try {
// Initialize Hive coordinator
const coordinator = new SwarmCoordinator({
maxAgents: options.maxAgents,
maxConcurrentTasks: options.maxAgents,
taskTimeout: options.timeout * 60 * 1000,
enableMonitoring: options.monitor,
enableWorkStealing: true,
enableCircuitBreaker: true,
memoryNamespace: options.memoryNamespace,
coordinationStrategy: 'distributed',
});
// Initialize Hive memory
const memory = new SwarmMemoryManager({
namespace: options.memoryNamespace,
enableDistribution: true,
enableKnowledgeBase: true,
persistencePath: `./hive-runs/${hiveId}/memory`,
});
await coordinator.start();
await memory.initialize();
// Create Queen Genesis
const queenId = await coordinator.registerAgent('Queen-Genesis', 'coordinator', [
'orchestration',
'consensus',
'decision-making',
'delegation',
]);
// Create specialized agents based on topology
const agents = await spawnHiveAgents(coordinator, options);
// Store Hive configuration
await memory.store(`hive/${hiveId}/config`, {
hiveId,
objective,
options,
queenId,
agents: agents.map((a) => a.id),
startTime: new Date().toISOString(),
});
// Create objective with Hive consensus
const objectiveId = await coordinator.createObjective(objective, 'development');
// Execute with consensus mechanisms
if (options.sparc) {
info('๐งช SPARC methodology enabled - full TDD workflow');
await executeSparcHive(coordinator, memory, objectiveId, agents, options);
}
else {
await executeHive(coordinator, memory, objectiveId, agents, options);
}
if (!options.background) {
// Show results
const status = coordinator.getSwarmStatus();
console.log(`\n๐ Hive Mind Summary:`);
console.log(` - Consensus Rounds: ${status.customMetrics?.consensusRounds || 0}`);
console.log(` - Decisions Made: ${status.customMetrics?.decisions || 0}`);
console.log(` - Tasks Completed: ${status.tasks.completed}`);
console.log(` - Quality Score: ${status.customMetrics?.qualityScore || 0}%`);
success(`โ
Hive Mind ${hiveId} completed successfully`);
}
}
catch (err) {
error(`Hive Mind error: ${err.message}`);
}
}
async function spawnHiveAgents(coordinator, options) {
const agents = [];
// Define agent types based on topology
const agentConfigs = getAgentConfigsForTopology(options.topology);
for (let i = 0; i < Math.min(options.maxAgents - 1, agentConfigs.length); i++) {
const config = agentConfigs[i % agentConfigs.length];
const agentId = await coordinator.registerAgent(`${config.type}-${i + 1}`, config.role, config.capabilities);
agents.push({
id: agentId,
type: config.type,
role: config.role,
capabilities: config.capabilities,
status: 'idle',
votes: new Map(),
});
console.log(` ๐ Spawned ${config.type} agent: ${agentId}`);
}
return agents;
}
function getAgentConfigsForTopology(topology) {
switch (topology) {
case 'hierarchical':
return [
{
type: 'architect',
role: 'architect',
capabilities: ['design', 'planning', 'architecture'],
},
{ type: 'worker', role: 'coder', capabilities: ['implementation', 'coding', 'testing'] },
{ type: 'worker', role: 'analyst', capabilities: ['analysis', 'optimization', 'metrics'] },
{
type: 'scout',
role: 'researcher',
capabilities: ['research', 'exploration', 'discovery'],
},
{ type: 'guardian', role: 'reviewer', capabilities: ['review', 'quality', 'validation'] },
];
case 'mesh':
return [
{ type: 'worker', role: 'generalist', capabilities: ['coding', 'analysis', 'research'] },
{
type: 'worker',
role: 'specialist',
capabilities: ['optimization', 'architecture', 'testing'],
},
{ type: 'scout', role: 'explorer', capabilities: ['discovery', 'research', 'innovation'] },
{
type: 'guardian',
role: 'validator',
capabilities: ['validation', 'quality', 'security'],
},
];
case 'ring':
return [
{
type: 'worker',
role: 'processor',
capabilities: ['processing', 'transformation', 'execution'],
},
{ type: 'worker', role: 'analyzer', capabilities: ['analysis', 'metrics', 'insights'] },
{
type: 'worker',
role: 'builder',
capabilities: ['building', 'implementation', 'integration'],
},
];
case 'star':
return [
{
type: 'worker',
role: 'executor',
capabilities: ['execution', 'implementation', 'delivery'],
},
{ type: 'scout', role: 'sensor', capabilities: ['monitoring', 'detection', 'alerting'] },
{ type: 'architect', role: 'planner', capabilities: ['planning', 'design', 'strategy'] },
];
default:
return [];
}
}
async function executeHive(coordinator, memory, objectiveId, agents, options) {
// Phase 1: Task decomposition with consensus
console.log('\n๐งฉ Phase 1: Task Decomposition');
const tasks = await decomposeWithConsensus(coordinator, memory, options.objective, agents, options);
// Phase 2: Task assignment with voting
console.log('\n๐ณ๏ธ Phase 2: Task Assignment');
const assignments = await assignTasksWithVoting(coordinator, memory, tasks, agents, options);
// Phase 3: Parallel execution with monitoring
console.log('\nโก Phase 3: Parallel Execution');
await executeTasksWithMonitoring(coordinator, memory, assignments, agents, options);
// Phase 4: Result aggregation with quality checks
console.log('\n๐ Phase 4: Result Aggregation');
await aggregateResultsWithQuality(coordinator, memory, objectiveId, agents, options);
}
async function executeSparcHive(coordinator, memory, objectiveId, agents, options) {
console.log('\n๐งช SPARC Hive Execution Mode');
// S: Specification with consensus
console.log('\n๐ S - Specification Phase');
await conductConsensusRound(memory, agents, 'specification', {
task: 'Define requirements and acceptance criteria',
objective: options.objective,
});
// P: Pseudocode with voting
console.log('\n๐งฎ P - Pseudocode Phase');
await conductConsensusRound(memory, agents, 'pseudocode', {
task: 'Design algorithms and data structures',
objective: options.objective,
});
// A: Architecture with review
console.log('\n๐๏ธ A - Architecture Phase');
await conductConsensusRound(memory, agents, 'architecture', {
task: 'Design system architecture',
objective: options.objective,
});
// R: Refinement with TDD
console.log('\nโป๏ธ R - Refinement Phase (TDD)');
await conductConsensusRound(memory, agents, 'refinement', {
task: 'Implement with test-driven development',
objective: options.objective,
});
// C: Completion with validation
console.log('\nโ
C - Completion Phase');
await conductConsensusRound(memory, agents, 'completion', {
task: 'Integrate and validate solution',
objective: options.objective,
});
}
async function conductConsensusRound(memory, agents, phase, context) {
const roundId = generateId('round');
// Store round context
await memory.store(`consensus/${roundId}/context`, {
phase,
context,
agents: agents.map((a) => a.id),
timestamp: new Date().toISOString(),
});
// Simulate voting
const votes = new Map();
agents.forEach((agent) => {
const vote = Math.random() > 0.2; // 80% approval rate
votes.set(agent.id, vote);
console.log(` ๐ณ๏ธ ${agent.type}-${agent.id}: ${vote ? 'โ
Approve' : 'โ Reject'}`);
});
// Calculate consensus
const approvals = Array.from(votes.values()).filter((v) => v).length;
const consensus = approvals / agents.length;
console.log(` ๐ Consensus: ${(consensus * 100).toFixed(1)}% (${approvals}/${agents.length})`);
// Store results
await memory.store(`consensus/${roundId}/results`, {
votes: Object.fromEntries(votes),
consensus,
approved: consensus >= 0.5,
timestamp: new Date().toISOString(),
});
}
async function decomposeWithConsensus(coordinator, memory, objective, agents, options) {
// Queen proposes task decomposition
const proposedTasks = [
{ type: 'analysis', description: `Analyze requirements for: ${objective}` },
{ type: 'design', description: `Design solution architecture` },
{ type: 'implementation', description: `Implement core functionality` },
{ type: 'testing', description: `Test and validate solution` },
{ type: 'documentation', description: `Document the implementation` },
];
// Agents vote on task breakdown
console.log(' ๐ Queen proposes task breakdown...');
console.log(' ๐ณ๏ธ Agents voting on tasks...');
// Simulate consensus
const approved = options.consensus === 'unanimous'
? agents.length === agents.length // All must agree
: agents.length > agents.length / 2; // Simple majority
console.log(` โ
Task breakdown ${approved ? 'approved' : 'rejected'}`);
return proposedTasks;
}
async function assignTasksWithVoting(coordinator, memory, tasks, agents, options) {
const assignments = new Map();
for (const task of tasks) {
// Agents bid on tasks based on capabilities
const bids = agents
.map((agent) => ({
agent,
score: calculateBidScore(agent, task),
}))
.sort((a, b) => b.score - a.score);
// Assign to highest bidder
const winner = bids[0].agent;
assignments.set(task.description, winner.id);
console.log(` ๐ ${task.type} โ ${winner.type}-${winner.id} (score: ${bids[0].score})`);
}
return assignments;
}
function calculateBidScore(agent, task) {
// Calculate how well agent capabilities match task requirements
let score = 0;
// Type matching
if (task.type === 'analysis' && agent.capabilities.includes('analysis'))
score += 3;
if (task.type === 'design' && agent.capabilities.includes('architecture'))
score += 3;
if (task.type === 'implementation' && agent.capabilities.includes('coding'))
score += 3;
if (task.type === 'testing' && agent.capabilities.includes('testing'))
score += 3;
if (task.type === 'documentation' && agent.capabilities.includes('documentation'))
score += 2;
// Add random factor for variety
score += Math.random() * 2;
return score;
}
async function executeTasksWithMonitoring(coordinator, memory, assignments, agents, options) {
const executions = Array.from(assignments.entries()).map(async ([task, agentId]) => {
const agent = agents.find((a) => a.id === agentId);
agent.status = 'executing';
console.log(` โก ${agent.type}-${agent.id} executing: ${task}`);
// Simulate execution
await new Promise((resolve) => setTimeout(resolve, 1000 + Math.random() * 2000));
agent.status = 'idle';
console.log(` โ
${agent.type}-${agent.id} completed: ${task}`);
// Store execution result
await memory.store(`execution/${agentId}/${Date.now()}`, {
task,
agent: agent.id,
status: 'completed',
timestamp: new Date().toISOString(),
});
});
await Promise.all(executions);
}
async function aggregateResultsWithQuality(coordinator, memory, objectiveId, agents, options) {
// Collect all execution results
const results = [];
for (const agent of agents) {
const pattern = `execution/${agent.id}/*`;
const executions = await memory.search(pattern, 10);
results.push(...executions);
}
// Calculate quality score
const qualityScore = Math.min(100, 75 + Math.random() * 25);
console.log(` ๐ Quality Score: ${qualityScore.toFixed(1)}%`);
console.log(` โ
Threshold: ${options.qualityThreshold * 100}%`);
console.log(` ${qualityScore >= options.qualityThreshold * 100 ? 'โ
PASSED' : 'โ FAILED'}`);
// Store aggregated results
await memory.store(`hive/${objectiveId}/results`, {
objective: options.objective,
executionCount: results.length,
qualityScore,
passed: qualityScore >= options.qualityThreshold * 100,
timestamp: new Date().toISOString(),
});
}
function showHiveHelp() {
console.log(`
๐ Hive Mind - Advanced Multi-Agent Coordination
USAGE:
claude-flow hive <objective> [options]
DESCRIPTION:
Hive Mind implements advanced swarm intelligence with consensus mechanisms,
distributed decision-making, and quality-driven execution.
EXAMPLES:
claude-flow hive "Build microservices architecture"
claude-flow hive "Optimize database performance" --consensus unanimous
claude-flow hive "Develop ML pipeline" --topology mesh --monitor
TOPOLOGIES:
hierarchical Queen-led hierarchy (default)
mesh Peer-to-peer coordination
ring Sequential processing
star Centralized hub
CONSENSUS MECHANISMS:
quorum Simple majority (default)
unanimous All agents must agree
weighted Capability-based voting
leader Queen decides with input
OPTIONS:
--topology <type> Swarm topology (default: hierarchical)
--consensus <type> Decision mechanism (default: quorum)
--max-agents <n> Maximum agents (default: 8)
--quality-threshold <n> Min quality 0-1 (default: 0.8)
--memory-namespace <ns> Memory namespace (default: hive)
--monitor Real-time monitoring
--background Run in background
--sparc Use SPARC methodology
--timeout <min> Timeout minutes (default: 60)
AGENT TYPES:
๐ Queen Orchestrator and decision maker
๐๏ธ Architect System design and planning
๐ Worker Implementation and execution
๐ Scout Research and exploration
๐ก๏ธ Guardian Quality and validation
FEATURES:
โข Consensus-based task decomposition
โข Capability-based task assignment
โข Parallel execution with monitoring
โข Quality-driven result aggregation
โข Distributed memory sharing
โข SPARC methodology support
For more info: https://github.com/ruvnet/claude-code-flow/docs/hive.md
`);
}
//# sourceMappingURL=hive.js.map