()
| 242 | } |
| 243 | |
| 244 | private drain(): void { |
| 245 | // Grow toward maxSize while queued work outstrips workers that are idle OR |
| 246 | // already on their way up (pending) — so we never spawn the whole pool for a |
| 247 | // single call whose eager worker just hasn't reported ready yet. |
| 248 | while ( |
| 249 | this.queue.length > this.idle.length + this.pendingWorkers.size && |
| 250 | this.workers.size < this.maxSize && |
| 251 | this.pendingWorkers.size < MAX_CONCURRENT_SPAWN && |
| 252 | this.healthy |
| 253 | ) { |
| 254 | this.spawnOne(); |
| 255 | } |
| 256 | while (this.idle.length && this.queue.length) { |
| 257 | // Skip jobs the backstop already answered. |
| 258 | let job: Job | undefined; |
| 259 | while (this.queue.length && (job = this.queue.shift()) && job.settled) job = undefined; |
| 260 | if (!job || job.settled) break; |
| 261 | const w = this.idle.pop()!; |
| 262 | this.inflight.set(w, job); |
| 263 | w.postMessage({ type: 'call', id: job.id, toolName: job.toolName, args: job.args }); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | private settle(job: Job, result: ToolResult): void { |
| 268 | if (job.settled) return; // already answered (by backstop or worker) |
no test coverage detected