| 437 | // retry its in-flight job once; a job that keeps crashing workers fails |
| 438 | // gracefully so it can't loop the pool forever. |
| 439 | private onWorkerGone(w: PoolWorker): void { |
| 440 | if (!this.workers.has(w)) return; // already handled (error+exit both fire) |
| 441 | this.workers.delete(w); |
| 442 | this.pendingWorkers.delete(w); |
| 443 | this.idle = this.idle.filter((x) => x !== w); |
| 444 | this.totalCrashes++; |
| 445 | const job = this.inflight.get(w); |
| 446 | this.inflight.delete(w); |
| 447 | try { void w.terminate(); } catch { /* already gone */ } |
| 448 | if (this.healthy) this.spawnOne(); // keep capacity |
| 449 | if (job) { |
| 450 | if (job.retries < this.maxRetries && this.healthy) { |
| 451 | job.retries++; |
| 452 | job.startedAt = undefined; // waiting again, for the worker that replaces this one |
| 453 | this.queue.unshift(job); // head of line — retry promptly |
| 454 | } else { |
| 455 | this.settle(job, { isError: true, content: [{ type: 'text', text: 'codegraph worker crashed; please retry the call.' }] }); |
| 456 | } |
| 457 | } |
| 458 | this.drain(); |
| 459 | } |
| 460 | |
| 461 | private drain(): void { |
| 462 | // Grow toward maxSize while queued work outstrips workers that are idle OR |