* Never evict a graph while a tool call or its timed-out reconcile uses it. * Evicts over the LRU bound, on close, and once idle past the timeout * (#2087). The cache is in last-use order, so idle entries lead it.
(maxOpen = MAX_CACHED_PROJECTS)
| 2624 | * (#2087). The cache is in last-use order, so idle entries lead it. |
| 2625 | */ |
| 2626 | private trimProjects(maxOpen = MAX_CACHED_PROJECTS): void { |
| 2627 | if (this.activeCalls > 0) return; |
| 2628 | const idleMs = resolveProjectIdleTimeoutMs(); |
| 2629 | const now = Date.now(); |
| 2630 | for (const [root, cg] of this.projectCache) { |
| 2631 | const idle = idleMs > 0 && now - (this.projectUsedAt.get(root) ?? now) >= idleMs; |
| 2632 | if (!this.closing && this.projectCache.size <= maxOpen && !idle) break; |
| 2633 | if (this.projectGates.has(cg)) continue; |
| 2634 | this.projectCache.delete(root); |
| 2635 | this.projectUsedAt.delete(root); |
| 2636 | if (this.projectLifecycle) { |
| 2637 | this.pendingCloses++; |
| 2638 | void Promise.resolve(this.projectLifecycle.release(cg)).finally(() => { |
| 2639 | this.pendingCloses--; |
| 2640 | this.trimProjects(); |
| 2641 | }); |
| 2642 | } else cg.close(); |
| 2643 | } |
| 2644 | if (this.closing && this.projectCache.size === 0 && this.pendingCloses === 0) { |
| 2645 | for (const resolve of this.closeWaiters.splice(0)) resolve(); |
| 2646 | } |
| 2647 | this.scheduleIdleRelease(idleMs); |
| 2648 | } |
| 2649 | |
| 2650 | /** |
| 2651 | * Arm one unref'd timer for the oldest project a trim could release. A |
no test coverage detected