* 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)
| 2809 | * (#2087). The cache is in last-use order, so idle entries lead it. |
| 2810 | */ |
| 2811 | private trimProjects(maxOpen = MAX_CACHED_PROJECTS): void { |
| 2812 | if (this.activeCalls > 0) return; |
| 2813 | const idleMs = resolveProjectIdleTimeoutMs(); |
| 2814 | const now = Date.now(); |
| 2815 | for (const [root, cg] of this.projectCache) { |
| 2816 | const idle = idleMs > 0 && now - (this.projectUsedAt.get(root) ?? now) >= idleMs; |
| 2817 | if (!this.closing && this.projectCache.size <= maxOpen && !idle) break; |
| 2818 | if (this.projectGates.has(cg)) continue; |
| 2819 | this.projectCache.delete(root); |
| 2820 | this.projectUsedAt.delete(root); |
| 2821 | if (this.projectLifecycle) { |
| 2822 | this.pendingCloses++; |
| 2823 | void Promise.resolve(this.projectLifecycle.release(cg)).finally(() => { |
| 2824 | this.pendingCloses--; |
| 2825 | this.trimProjects(); |
| 2826 | }); |
| 2827 | } else cg.close(); |
| 2828 | } |
| 2829 | if (this.closing && this.projectCache.size === 0 && this.pendingCloses === 0) { |
| 2830 | for (const resolve of this.closeWaiters.splice(0)) resolve(); |
| 2831 | } |
| 2832 | this.scheduleIdleRelease(idleMs); |
| 2833 | } |
| 2834 | |
| 2835 | /** |
| 2836 | * Arm one unref'd timer for the oldest project a trim could release. A |
no test coverage detected