* 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)
| 2650 | * (#2087). The cache is in last-use order, so idle entries lead it. |
| 2651 | */ |
| 2652 | private trimProjects(maxOpen = MAX_CACHED_PROJECTS): void { |
| 2653 | if (this.activeCalls > 0) return; |
| 2654 | const idleMs = resolveProjectIdleTimeoutMs(); |
| 2655 | const now = Date.now(); |
| 2656 | for (const [root, cg] of this.projectCache) { |
| 2657 | const idle = idleMs > 0 && now - (this.projectUsedAt.get(root) ?? now) >= idleMs; |
| 2658 | if (!this.closing && this.projectCache.size <= maxOpen && !idle) break; |
| 2659 | if (this.projectGates.has(cg)) continue; |
| 2660 | this.projectCache.delete(root); |
| 2661 | this.projectUsedAt.delete(root); |
| 2662 | if (this.projectLifecycle) { |
| 2663 | this.pendingCloses++; |
| 2664 | void Promise.resolve(this.projectLifecycle.release(cg)).finally(() => { |
| 2665 | this.pendingCloses--; |
| 2666 | this.trimProjects(); |
| 2667 | }); |
| 2668 | } else cg.close(); |
| 2669 | } |
| 2670 | if (this.closing && this.projectCache.size === 0 && this.pendingCloses === 0) { |
| 2671 | for (const resolve of this.closeWaiters.splice(0)) resolve(); |
| 2672 | } |
| 2673 | this.scheduleIdleRelease(idleMs); |
| 2674 | } |
| 2675 | |
| 2676 | /** |
| 2677 | * Arm one unref'd timer for the oldest project a trim could release. A |
no test coverage detected