π The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(daemon): Implement P0/P1 priority fixes from technical review
rUv5ef673153ead
diff --git a/package.json b/package.jsonindex cebfd0f716..2c71556c32 100644--- a/package.json+++ b/package.json@@ -1,12 +1,23 @@{"name": "claude-flow",- "version": "3.0.0-alpha.1",+ "version": "3.0.0-alpha.7","description": "Claude Flow V3 - Domain-Driven Design Architecture with 15-Agent Swarm Coordination","main": "dist/index.js","type": "module","bin": {"claude-flow": "./v3/@claude-flow/cli/bin/cli.js"},+ "files": [+ "v3/@claude-flow/cli/bin/**",+ "v3/@claude-flow/cli/dist/**",+ "v3/@claude-flow/cli/package.json",+ "v3/@claude-flow/shared/dist/**",+ "v3/@claude-flow/shared/package.json",+ ".claude-plugin/**",+ ".claude/**",+ "README.md",+ "LICENSE"+ ],"scripts": {"dev": "tsx watch src/index.ts","build": "tsc",diff --git a/v3/@claude-flow/cli/src/services/worker-daemon.ts b/v3/@claude-flow/cli/src/services/worker-daemon.tsindex 66f3fe8e45..245c138f8d 100644--- a/v3/@claude-flow/cli/src/services/worker-daemon.ts+++ b/v3/@claude-flow/cli/src/services/worker-daemon.ts@@ -70,20 +70,33 @@ interface DaemonConfig {logDir: string;stateFile: string;maxConcurrent: number;+ workerTimeoutMs: number;+ resourceThresholds: {+ maxCpuLoad: number;+ minFreeMemoryPercent: number;+ };workers: WorkerConfig[];}-// Default worker configurations with intervals-const DEFAULT_WORKERS: WorkerConfig[] = [- { type: 'map', intervalMs: 5 * 60 * 1000, priority: 'normal', description: 'Codebase mapping', enabled: true },- { type: 'audit', intervalMs: 10 * 60 * 1000, priority: 'critical', description: 'Security analysis', enabled: true },- { type: 'optimize', intervalMs: 15 * 60 * 1000, priority: 'high', description: 'Performance optimization', enabled: true },- { type: 'consolidate', intervalMs: 30 * 60 * 1000, priority: 'low', description: 'Memory consolidation', enabled: true },- { type: 'testgaps', intervalMs: 20 * 60 * 1000, priority: 'normal', description: 'Test coverage analysis', enabled: true },- { type: 'predict', intervalMs: 2 * 60 * 1000, priority: 'low', description: 'Predictive preloading', enabled: false },- { type: 'document', intervalMs: 60 * 60 * 1000, priority: 'low', description: 'Auto-documentation', enabled: false },+// Worker configuration with staggered offsets to prevent overlap+interface WorkerConfigInternal extends WorkerConfig {+ offsetMs: number; // Stagger start time+}++// Default worker configurations with improved intervals (P0 fix: map 5min -> 15min)+const DEFAULT_WORKERS: WorkerConfigInternal[] = [+ { type: 'map', intervalMs: 15 * 60 * 1000, offsetMs: 0, priority: 'normal', description: 'Codebase mapping', enabled: true },+ { type: 'audit', intervalMs: 10 * 60 * 1000, offsetMs: 2 * 60 * 1000, priority: 'critical', description: 'Security analysis', enabled: true },+ { type: 'optimize', intervalMs: 15 * 60 * 1000, offsetMs: 4 * 60 * 1000, priority: 'high', description: 'Performance optimization', enabled: true },+ { type: 'consolidate', intervalMs: 30 * 60 * 1000, offsetMs: 6 * 60 * 1000, priority: 'low', description: 'Memory consolidation', enabled: true },+ { type: 'testgaps', intervalMs: 20 * 60 * 1000, offsetMs: 8 * 60 * 1000, priority: 'normal', description: 'Test coverage analysis', enabled: true },+ { type: 'predict', intervalMs: 10 * 60 * 1000, offsetMs: 0, priority: 'low', description: 'Predictive preloading', enabled: false },+ { type: 'document', intervalMs: 60 * 60 * 1000, offsetMs: 0, priority: 'low', description: 'Auto-documentation', enabled: false },];+// Worker timeout (5 minutes max per worker)+const DEFAULT_WORKER_TIMEOUT_MS = 5 * 60 * 1000;+/*** Worker Daemon - Manages background workers with Node.js*/@@ -94,6 +107,8 @@ export class WorkerDaemon extends EventEmitter {private running = false;private startedAt?: Date;private projectRoot: string;+ private runningWorkers: Set<WorkerType> = new Set(); // Track concurrent workers+ private pendingWorkers: WorkerType[] = []; // Queue for deferred workersconstructor(projectRoot: string, config?: Partial<DaemonConfig>) {super();@@ -102,13 +117,21 @@ export class WorkerDaemon extends EventEmitter {const claudeFlowDir = join(projectRoot, '.claude-flow');this.config = {- autoStart: config?.autoStart ?? true,+ autoStart: config?.autoStart ?? false, // P1 fix: Default to false for explicit consentlogDir: config?.logDir ?? join(claudeFlowDir, 'logs'),stateFile: config?.stateFile ?? join(claudeFlowDir, 'daemon-state.json'),- maxConcurrent: config?.maxConcurrent ?? 3,+ maxConcurrent: config?.maxConcurrent ?? 2, // P0 fix: Limit concurrent workers+ workerTimeoutMs: config?.workerTimeoutMs ?? DEFAULT_WORKER_TIMEOUT_MS,+ resourceThresholds: config?.resourceThresholds ?? {+ maxCpuLoad: 2.0,+ minFreeMemoryPercent: 20,+ },workers: config?.workers ?? DEFAULT_WORKERS,};+ // Setup graceful shutdown handlers+ this.setupShutdownHandlers();+// Ensure directories existif (!existsSync(claudeFlowDir)) {mkdirSync(claudeFlowDir, { recursive: true });@@ -121,6 +144,53 @@ export class WorkerDaemon extends EventEmitter {this.initializeWorkerStates();}+ /**+ * Setup graceful shutdown handlers+ */+ private setupShutdownHandlers(): void {+ const shutdown = async () => {+ this.log('info', 'Received shutdown signal, stopping daemon...');+ await this.stop();+ process.exit(0);+ };++ process.on('SIGTERM', shutdown);+ process.on('SIGINT', shutdown);+ process.on('SIGHUP', shutdown);+ }++ /**+ * Check if system resources allow worker execution+ */+ private async canRunWorker(): Promise<{ allowed: boolean; reason?: string }> {+ const os = await import('os');+ const cpuLoad = os.loadavg()[0];+ const totalMem = os.totalmem();+ const freeMem = os.freemem();+ const freePercent = (freeMem / totalMem) * 100;++ if (cpuLoad > this.config.resourceThresholds.maxCpuLoad) {+ return { allowed: false, reason: `CPU load too high: ${cpuLoad.toFixed(2)}` };+ }+ if (freePercent < this.config.resourceThresholds.minFreeMemoryPercent) {+ return { allowed: false, reason: `Memory too low: ${freePercent.toFixed(1)}% free` };+ }+ return { allowed: true };+ }++ /**+ * Process pending workers queue+ */+ private async processPendingWorkers(): Promise<void> {+ while (this.pendingWorkers.length > 0 && this.runningWorkers.size < this.config.maxConcurrent) {+ const workerType = this.pendingWorkers.shift()!;+ const workerConfig = this.config.workers.find(w => w.type === workerType);+ if (workerConfig) {+ await this.executeWorkerWithConcurrencyControl(workerConfig);+ }+ }+ }+private initializeWorkerStates(): void {// Try to restore state from fileif (existsSync(this.config.stateFile)) {@@ -222,16 +292,18 @@ export class WorkerDaemon extends EventEmitter {}/**- * Schedule a worker to run at intervals+ * Schedule a worker to run at intervals with staggered start*/private scheduleWorker(workerConfig: WorkerConfig): void {const state = this.workers.get(workerConfig.type)!;+ const internalConfig = workerConfig as WorkerConfigInternal;+ const staggerOffset = internalConfig.offsetMs || 0;- // Calculate initial delay (run immediately if never run, otherwise respect interval)- let initialDelay = 0;+ // Calculate initial delay with stagger offset+ let initialDelay = staggerOffset;if (state.lastRun) {const timeSinceLastRun = Date.now() - state.lastRun.getTime();- initialDelay = Math.max(0, workerConfig.intervalMs - timeSinceLastRun);+ initialDelay = Math.max(staggerOffset, workerConfig.intervalMs - timeSinceLastRun);}state.nextRun = new Date(Date.now() + initialDelay);@@ -239,7 +311,8 @@ export class WorkerDaemon extends EventEmitter {const runAndReschedule = async () => {if (!this.running) return;- await this.executeWorker(workerConfig);+ // Use concurrency-controlled execution (P0 fix)+ await this.executeWorkerWithConcurrencyControl(workerConfig);// Rescheduleif (this.running) {@@ -249,7 +322,7 @@ export class WorkerDaemon extends EventEmitter {}};- // Schedule first run+ // Schedule first run with stagger offsetconst timer = setTimeout(runAndReschedule, initialDelay);this.timers.set(workerConfig.type, timer);@@ -257,20 +330,50 @@ export class WorkerDaemon extends EventEmitter {}/**- * Execute a worker+ * Execute a worker with concurrency control (P0 fix)+ */+ private async executeWorkerWithConcurrencyControl(workerConfig: WorkerConfig): Promise<WorkerResult | null> {+ // Check concurrency limit+ if (this.runningWorkers.size >= this.config.maxConcurrent) {+ this.log('info', `Worker ${workerConfig.type} deferred: max concurrent (${this.config.maxConcurrent}) reached`);+ this.pendingWorkers.push(workerConfig.type);+ this.emit('worker:deferred', { type: workerConfig.type, reason: 'max_concurrent' });+ return null;+ }++ // Check resource availability+ const resourceCheck = await this.canRunWorker();+ if (!resourceCheck.allowed) {+ this.log('info', `Worker ${workerConfig.type} deferred: ${resourceCheck.reason}`);+ this.pendingWorkers.push(workerConfig.type);+ this.emit('worker:deferred', { type: workerConfig.type, reason: resourceCheck.reason });+ return null;+ }++ return this.executeWorker(workerConfig);+ }++ /**+ * Execute a worker with timeout protection*/private async executeWorker(workerConfig: WorkerConfig): Promise<WorkerResult> {const state = this.workers.get(workerConfig.type)!;const workerId = `${workerConfig.type}_${Date.now()}`;const startTime = Date.now();+ // Track running worker+ this.runningWorkers.add(workerConfig.type);state.isRunning = true;this.emit('worker:start', { workerId, type: workerConfig.type });- this.log('info', `Starting worker: ${workerConfig.type}`);+ this.log('info', `Starting worker: ${workerConfig.type} (${this.runningWorkers.size}/${this.config.maxConcurrent} concurrent)`);try {- // Execute worker logic- const output = await this.runWorkerLogic(workerConfig);+ // Execute worker logic with timeout (P1 fix)+ const output = await this.runWithTimeout(+ () => this.runWorkerLogic(workerConfig),+ this.config.workerTimeoutMs,+ `Worker ${workerConfig.type} timed out after ${this.config.workerTimeoutMs / 1000}s`+ );const durationMs = Date.now() - startTime;// Update state@@ -316,9 +419,38 @@ export class WorkerDaemon extends EventEmitter {this.saveState();return result;+ } finally {+ // Remove from running set and process queue+ this.runningWorkers.delete(workerConfig.type);+ this.processPendingWorkers();}}+ /**+ * Run a function with timeout (P1 fix)+ */+ private async runWithTimeout<T>(+ fn: () => Promise<T>,+ timeoutMs: number,+ timeoutMessage: string+ ): Promise<T> {+ return new Promise<T>((resolve, reject) => {+ const timer = setTimeout(() => {+ reject(new Error(timeoutMessage));+ }, timeoutMs);++ fn()+ .then((result) => {+ clearTimeout(timer);+ resolve(result);+ })+ .catch((error) => {+ clearTimeout(timer);+ reject(error);+ });+ });+ }+/*** Run the actual worker logic*/diff --git a/v3/implementation/adrs/ADR-005-implementation-summary.md b/v3/implementation/adrs/ADR-005-implementation-summary.mdindex 221b0ba2b3..fab4715b36 100644--- a/v3/implementation/adrs/ADR-005-implementation-summary.md+++ b/v3/implementation/adrs/ADR-005-implementation-summary.md@@ -430,3 +430,79 @@ The implementation provides a solid foundation for CLI commands, web interfaces,**Code Quality**: Production-ready with stub implementations**Architecture Compliance**: 100% (ADR-005, ADR-006, ADR-002, ADR-007, ADR-014)**Ready for**: CLI integration, testing, service integration++---++## Extension: CLI MCP Tool Integration (2026-01-07)++### CLI Implementation Complete++All MCP tools now exposed via CLI commands in `@claude-flow/[email protected]`:++#### File-Based Persistence Architecture++```+.claude-flow/+βββ agents/store.json # Agent lifecycle state+βββ tasks/store.json # Task execution state+βββ sessions/store.json # Session management+βββ config/config.json # Configuration storage+βββ hive-mind/state.json # Hive collective state+βββ workflows/store.json # Workflow definitions+```++#### CLI MCP Tool Files++| File | Tools | Lines |+|------|-------|-------|+| `agent-tools.ts` | spawn, terminate, status, list, pool, health, update | 467 |+| `hive-mind-tools.ts` | init, status, join, leave, consensus, broadcast, memory | 522 |+| `task-tools.ts` | create, status, list, complete, cancel | 310 |+| `session-tools.ts` | save, restore, list, delete, export | 340 |+| `config-tools.ts` | get, set, list, reset, export, import | 328 |+| `memory-tools.ts` | store, retrieve, list, delete, search | 230 |+| `workflow-tools.ts` | create, execute, list, status, delete | 550 |++**Total**: 7 MCP tool files, ~2,750 lines++#### CLI Command Coverage++| Command | Subcommands | MCP Tools Called |+|---------|-------------|------------------|+| `agent` | spawn, terminate, status, list, pool, health | `agent/*` |+| `hive-mind` | init, spawn, status, task, join, leave, consensus, broadcast, memory, optimize-memory, shutdown | `hive-mind/*` |+| `task` | create, status, list, complete, cancel | `task/*` |+| `session` | save, restore, list, delete, export | `session/*` |+| `config` | get, set, list, reset, export, import | `config/*` |+| `memory` | store, retrieve, list, search, delete | `memory/*` |+| `workflow` | create, execute, list, status, delete | `workflow/*` |+| `daemon` | start, stop, status, trigger, enable | `hooks/daemon-*` |++#### Bug Fixes++1. **Positional Argument Parsing** - Fixed CLI parser to correctly pass positional args to subcommands+ - Issue: `hive-mind join worker-1` wasn't passing `worker-1` to the join handler+ - Fix: Changed `positional.slice(1)` to `positional` when commandPath already includes subcommand++2. **Null Coalescing** - Added null checks for optional response fields+ - `agent pool`, `agent health`, `hive-mind status` now handle undefined values++#### Testing Results++```bash+# All commands working+node bin/cli.js hive-mind join worker-1 # β Works+node bin/cli.js hive-mind leave worker-1 # β Works+node bin/cli.js hive-mind memory --action list # β Works+node bin/cli.js hive-mind consensus --action propose --type feature --value "test" # β Works+node bin/cli.js hive-mind broadcast -m "Hello" # β Works+```++#### Updated Statistics++- **Total MCP Tools**: 45+ (core + hooks + CLI-specific)+- **CLI Commands**: 8 main commands, 50+ subcommands+- **File Persistence**: 6 storage domains+- **Architecture Compliance**: 100%++**Published**: `@claude-flow/[email protected]` with `v3alpha` tagdiff --git a/v3/implementation/adrs/README.md b/v3/implementation/adrs/README.mdindex 48a512fa4f..da7385fe95 100644--- a/v3/implementation/adrs/README.md+++ b/v3/implementation/adrs/README.md@@ -77,10 +77,35 @@ All ADRs consider security:**Last Updated:** 2026-01-07**Project:** Claude-Flow V3-**Version:** 3.0.3+**Version:** 3.0.0-alpha.7### Recent Updates (2026-01-07)-- **ADR-014**: Added Node.js Worker Daemon extension - cross-platform TypeScript daemon replaces shell helpers-- **CLI**: New `daemon` command with start/stop/status/trigger/enable subcommands++#### Release: @claude-flow/[email protected]+- **Hive-Mind CLI**: All MCP tools now exposed via CLI subcommands:+ - `hive-mind join <agent-id>` - Join agent to hive+ - `hive-mind leave <agent-id>` - Remove agent from hive+ - `hive-mind consensus` - Manage consensus proposals and voting+ - `hive-mind broadcast -m <msg>` - Broadcast messages to workers+ - `hive-mind memory` - Access shared memory (get/set/delete/list)+- **Bug Fix**: Fixed positional argument parsing for subcommands in CLI parser+- **File Persistence**: All MCP tools use file-based persistence in `.claude-flow/` directories+- **ADR-014**: Node.js Worker Daemon - cross-platform TypeScript daemon replaces shell helpers+- **CLI**: `daemon` command with start/stop/status/trigger/enable subcommands- **Session Integration**: Auto-start daemon on SessionStart, auto-stop on SessionEnd-- **Package**: `claude-flow@v3alpha` now uses V3 CLI via root `bin` configuration++#### CLI MCP Tool Coverage+| Category | Tools | CLI Status |+|----------|-------|------------|+| Agent | spawn, terminate, status, list, pool, health, update | β Complete |+| Hive-Mind | init, spawn, status, task, join, leave, consensus, broadcast, memory, optimize-memory, shutdown | β Complete |+| Task | create, status, list, complete, cancel | β Complete |+| Session | save, restore, list, delete, export | β Complete |+| Config | get, set, list, reset, export, import | β Complete |+| Memory | store, retrieve, list, delete, search | β Complete |+| Workflow | create, execute, list, status, delete | β Complete |++#### Install+```bash+npx @claude-flow/cli@v3alpha --help+```