UNPKG

claude-flow-novice

Version:

Claude Flow Novice - Advanced orchestration platform for multi-agent AI workflows with CFN Loop architecture Includes Local RuVector Accelerator and all CFN skills for complete functionality.

189 lines (147 loc) 6.49 kB
# Agent Spawning Module - TypeScript Implementation ## Summary Successfully implemented `spawn-agents.ts` TypeScript module as specified in the DEPENDENCY_DIAGRAM.txt (Phase 2 Spawn & Context). This module wraps the CLI agent spawning functionality for orchestrator integration. ## Implementation Details ### File Locations - **Module**: `.claude/skills/cfn-loop-orchestration/src/helpers/spawn-agents.ts` (349 LOC) - **Tests**: `.claude/skills/cfn-loop-orchestration/tests/spawn-agents.test.ts` (284 LOC) ### Type Definitions ```typescript // Agent spawning result export interface SpawnResult { agentId: string; agentType: string; success: boolean; pid?: number | undefined; error?: string | undefined; } // Spawn summary with metrics export interface SpawnSummary { totalSpawned: number; successCount: number; failureCount: number; results: SpawnResult[]; duration: number; } // Configuration interface export interface SpawnAgentsConfig { taskId: string; iteration: number; agents: string[]; originalContext: string; dryRun?: boolean; logDir?: string; projectRoot?: string; } ``` ### Core Functions #### `spawnAgents(config: SpawnAgentsConfig): Promise<SpawnSummary>` Main agent spawning function with full validation and error handling. **Features:** - Agent type validation (loop3/loop2 only) - Input sanitization (prevents injection attacks) - Unique agent ID generation with iteration and instance tracking - CLI command formatting and validation - Dry-run mode for testing without execution - Comprehensive error handling - Duration tracking and result aggregation **Error Handling:** - Validates task ID is non-empty string - Validates iteration is non-negative integer - Validates agents array is non-empty - Validates original context is non-empty string - Validates all agent types before spawning - Graceful fallback on individual agent spawn failures #### `spawnLoop3Agents(taskId, iteration, context, dryRun?): Promise<SpawnSummary>` Convenience function for spawning single Loop 3 agent. #### `spawnLoop2Agents(taskId, iteration, context, dryRun?): Promise<SpawnSummary>` Convenience function for spawning single Loop 2 agent. ### Helper Functions - **validateAgentType()**: Ensures agent type is 'loop3' or 'loop2' - **sanitizeInput()**: Removes dangerous characters while preserving valid input - **generateAgentId()**: Creates unique agent ID with format: `{type}-{iteration}-{instance}` - **formatSpawnCommand()**: Builds npx claude-flow-novice agent command array - **validateCommandFormat()**: Validates CLI command structure and required parameters - **logMessage()**: Logs to both console and file with timestamps - **spawnSingleAgent()**: Core spawning logic for individual agents ### Key Design Decisions 1. **Agent Type Validation**: Only 'loop3' and 'loop2' are valid types, preventing misconfiguration 2. **Input Sanitization**: Uses regex to allow only safe characters: `[a-zA-Z0-9._:,\-]` 3. **Unique IDs**: Tracks instance counts per agent type for parallel spawning 4. **Dry-Run Mode**: Full test capability without actual command execution 5. **Background Spawning**: Uses `spawn()` with `detached: true` and `unref()` for true background execution 6. **Logging**: Both console output and file logging for debugging 7. **Graceful Degradation**: Individual agent failures don't halt entire spawn operation ## Test Coverage ### Test Suites (19 tests) **Agent Type Validation (4 tests)** - Accepts loop3 agent type - Accepts loop2 agent type - Rejects invalid agent type - Validates all agents in array **CLI Command Formatting (3 tests)** - Formats valid spawn command for loop3 - Formats valid spawn command for loop2 - Sanitizes special characters in input **Dry-Run Mode (2 tests)** - Dry-run mode logs command without executing - Dry-run mode succeeds for multiple agents **Error Handling (4 tests)** - Throws error on missing task ID - Throws error on invalid iteration - Throws error on empty agents array - Throws error on missing context **Spawn Summary (2 tests)** - Returns accurate spawn summary - Counts failures correctly **Convenience Functions (2 tests)** - spawnLoop3Agents works correctly - spawnLoop2Agents works correctly **Iteration Tracking (2 tests)** - Tracks iteration numbers in agent IDs - Tracks instance numbers for duplicate agent types ### Test Results ``` PASS tests/spawn-agents.test.ts ✓ 19 passed ✓ 0 failed Time: 7.648s ``` ## TypeScript Compliance - **Strict Type Checking**: All strict mode flags enabled - **No `any` Types**: Full type coverage - **Null Safety**: Proper handling of optional properties with exactOptionalPropertyTypes - **Type Inference**: Strong inference reduces boilerplate - **JSDoc Comments**: Comprehensive documentation for all functions ## Integration Points This module is designed for integration with: - **orchestrator.ts**: Loop 3 and Loop 2 agent spawning during orchestration - **context-injector.ts**: Context enrichment with broadcast messages - **iteration-manager.ts**: Iteration tracking across spawned agents ## Dependency Analysis **No External Dependencies**: Module uses only Node.js built-ins: - `child_process` (spawn) - `path` - `fs/promises` **Phase Dependency**: Unblocks Phase 3 (Main Orchestrator implementation) ## Exit Criteria Status ✅ Agent type validation (loop3/loop2) ✅ CLI command formatting with proper parameter sequencing ✅ Dry-run mode for testing ✅ Format validation before execution ✅ Error handling for all failure cases ✅ 19 unit tests covering all scenarios ✅ Type-safe implementation with strict TypeScript ✅ Comprehensive logging and metrics ✅ Duration tracking for performance monitoring ## Next Steps This module unblocks: 1. **Phase 3 Orchestrator**: Can now spawn Loop 3 and Loop 2 agents 2. **Gate Checking**: Can validate agent execution results 3. **Consensus Validation**: Can spawn validator agents 4. **Product Owner Decision**: Can integrate decision-making agents ## Notes - Implementation follows existing code patterns in the orchestration framework - Error messages provide clear context for debugging - Logging includes timing information for performance optimization - Instance counter ensures unique IDs when multiple agents of same type are spawned