diff --git a/v3/@claude-flow/cli/src/services/worker-queue.ts b/v3/@claude-flow/cli/src/services/worker-queue.ts index 2305ef97c2..32faadf6d0 100644 --- a/v3/@claude-flow/cli/src/services/worker-queue.ts +++ b/v3/@claude-flow/cli/src/services/worker-queue.ts @@ -578,34 +578,60 @@ export class WorkerQueue extends EventEmitter { */ async start( workerTypes: HeadlessWorkerType[], - handler: (task: QueueTask) => Promise + handler: (task: QueueTask) => Promise, + options?: { maxConcurrent?: number } ): Promise { - await this.registerWorker(workerTypes); + await this.registerWorker(workerTypes, { maxConcurrent: options?.maxConcurrent }); const processLoop = async () => { while (!this.isShuttingDown) { - const task = await this.dequeue(workerTypes); - - if (task) { - try { - const result = await handler(task); - await this.complete(task.id, result); - } catch (error) { - await this.fail(task.id, error instanceof Error ? error.message : String(error)); + try { + // Respect concurrency limit + if (this.processingTasks.size >= this.maxConcurrent) { + await new Promise(resolve => setTimeout(resolve, 100)); + continue; } - } else { - // No task available, wait before polling again - await new Promise(resolve => setTimeout(resolve, 1000)); + + const task = await this.dequeue(workerTypes); + + if (task) { + // Process task without blocking the loop (allows concurrency) + this.processTask(task, handler).catch(error => { + this.emit('error', { taskId: task.id, error: String(error) }); + }); + } else { + // No task available, wait before polling again + await new Promise(resolve => setTimeout(resolve, 1000)); + } + } catch (error) { + this.emit('error', { error: error instanceof Error ? error.message : String(error) }); + // Wait before retrying to avoid tight error loop + await new Promise(resolve => setTimeout(resolve, 5000)); } } }; // Start processing processLoop().catch(error => { - this.emit('error', { error: error.message }); + this.emit('error', { error: error instanceof Error ? error.message : String(error) }); }); } + /** + * Process a single task + */ + private async processTask( + task: QueueTask, + handler: (task: QueueTask) => Promise + ): Promise { + try { + const result = await handler(task); + await this.complete(task.id, result); + } catch (error) { + await this.fail(task.id, error instanceof Error ? error.message : String(error)); + } + } + /** * Shutdown the queue gracefully */