()
| 260 | } |
| 261 | |
| 262 | private drain(): void { |
| 263 | // Grow toward maxSize while queued work outstrips workers that are idle OR |
| 264 | // already on their way up (pending) — so we never spawn the whole pool for a |
| 265 | // single call whose eager worker just hasn't reported ready yet. |
| 266 | while ( |
| 267 | this.queue.length > this.idle.length + this.pendingWorkers.size && |
| 268 | this.workers.size < this.maxSize && |
| 269 | this.pendingWorkers.size < MAX_CONCURRENT_SPAWN && |
| 270 | this.healthy |
| 271 | ) { |
| 272 | this.spawnOne(); |
| 273 | } |
| 274 | while (this.idle.length && this.queue.length) { |
| 275 | // Skip jobs the backstop already answered. |
| 276 | let job: Job | undefined; |
| 277 | while (this.queue.length && (job = this.queue.shift()) && job.settled) job = undefined; |
| 278 | if (!job || job.settled) break; |
| 279 | const w = this.idle.pop()!; |
| 280 | this.inflight.set(w, job); |
| 281 | w.postMessage({ type: 'call', id: job.id, toolName: job.toolName, args: job.args }); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | private settle(job: Job, result: ToolResult): void { |
| 286 | if (job.settled) return; // already answered (by backstop or worker) |
no test coverage detected