()
| 459 | } |
| 460 | |
| 461 | private drain(): void { |
| 462 | // Grow toward maxSize while queued work outstrips workers that are idle OR |
| 463 | // already on their way up (pending) — so we never spawn the whole pool for a |
| 464 | // single call whose eager worker just hasn't reported ready yet. |
| 465 | while ( |
| 466 | this.queue.length > this.idle.length + this.pendingWorkers.size && |
| 467 | this.workers.size < this.maxSize && |
| 468 | this.pendingWorkers.size < MAX_CONCURRENT_SPAWN && |
| 469 | this.healthy |
| 470 | ) { |
| 471 | this.spawnOne(); |
| 472 | } |
| 473 | while (this.idle.length && this.queue.length) { |
| 474 | // Skip jobs the backstop already answered. |
| 475 | let job: Job | undefined; |
| 476 | while (this.queue.length && (job = this.queue.shift()) && job.settled) job = undefined; |
| 477 | if (!job || job.settled) break; |
| 478 | const w = this.idle.pop()!; |
| 479 | this.inflight.set(w, job); |
| 480 | job.startedAt = Date.now(); |
| 481 | w.postMessage({ type: 'call', id: job.id, toolName: job.toolName, args: job.args }); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private settle(job: Job, result: ToolResult): void { |
| 486 | if (job.settled) return; // already answered (by backstop or worker) |
no test coverage detected