| 407 | // retry its in-flight job once; a job that keeps crashing workers fails |
| 408 | // gracefully so it can't loop the pool forever. |
| 409 | private onWorkerGone(w: PoolWorker): void { |
| 410 | if (!this.workers.has(w)) return; // already handled (error+exit both fire) |
| 411 | this.workers.delete(w); |
| 412 | this.pendingWorkers.delete(w); |
| 413 | this.idle = this.idle.filter((x) => x !== w); |
| 414 | this.totalCrashes++; |
| 415 | const job = this.inflight.get(w); |
| 416 | this.inflight.delete(w); |
| 417 | try { void w.terminate(); } catch { /* already gone */ } |
| 418 | if (this.healthy) this.spawnOne(); // keep capacity |
| 419 | if (job) { |
| 420 | if (job.retries < this.maxRetries && this.healthy) { |
| 421 | job.retries++; |
| 422 | this.queue.unshift(job); // head of line — retry promptly |
| 423 | } else { |
| 424 | this.settle(job, { isError: true, content: [{ type: 'text', text: 'codegraph worker crashed; please retry the call.' }] }); |
| 425 | } |
| 426 | } |
| 427 | this.drain(); |
| 428 | } |
| 429 | |
| 430 | private drain(): void { |
| 431 | // Grow toward maxSize while queued work outstrips workers that are idle OR |