* 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)
| 2692 | * (#2087). The cache is in last-use order, so idle entries lead it. |
| 2693 | */ |
| 2694 | private trimProjects(maxOpen = MAX_CACHED_PROJECTS): void { |
| 2695 | if (this.activeCalls > 0) return; |
| 2696 | const idleMs = resolveProjectIdleTimeoutMs(); |
| 2697 | const now = Date.now(); |
| 2698 | for (const [root, cg] of this.projectCache) { |
| 2699 | const idle = idleMs > 0 && now - (this.projectUsedAt.get(root) ?? now) >= idleMs; |
| 2700 | if (!this.closing && this.projectCache.size <= maxOpen && !idle) break; |
| 2701 | if (this.projectGates.has(cg)) continue; |
| 2702 | this.projectCache.delete(root); |
| 2703 | this.projectUsedAt.delete(root); |
| 2704 | if (this.projectLifecycle) { |
| 2705 | this.pendingCloses++; |
| 2706 | void Promise.resolve(this.projectLifecycle.release(cg)).finally(() => { |
| 2707 | this.pendingCloses--; |
| 2708 | this.trimProjects(); |
| 2709 | }); |
| 2710 | } else cg.close(); |
| 2711 | } |
| 2712 | if (this.closing && this.projectCache.size === 0 && this.pendingCloses === 0) { |
| 2713 | for (const resolve of this.closeWaiters.splice(0)) resolve(); |
| 2714 | } |
| 2715 | this.scheduleIdleRelease(idleMs); |
| 2716 | } |
| 2717 | |
| 2718 | /** |
| 2719 | * Arm one unref'd timer for the oldest project a trim could release. A |
no test coverage detected