[PR #9141] feat(util): Add bounded LRU cache utility #12999

Open
opened 2026-02-16 18:17:53 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/9141

State: open
Merged: No


Summary

Add a bounded LRU (Least Recently Used) cache utility with eviction callbacks to prevent unbounded memory growth.

Fixes #9140

Problem

Several places in the codebase use unbounded Map objects for caching:

  • Instance cache in project/instance.ts
  • Provider SDK cache in provider/provider.ts

These can grow without limit in long-running processes or when handling many directories/providers.

Solution

Add a reusable createLruCache utility that:

  • Limits cache size with maxEntries option
  • Evicts least-recently-used entries when full
  • Provides onEvict callback for cleanup logic
  • Maintains Map-like interface for easy adoption

Changes

  • packages/opencode/src/util/cache.ts - New LRU cache utility with:
    • maxEntries limit (default: Infinity for backward compatibility)
    • onEvict callback for disposal logic
    • LRU tracking via lastAccess timestamp
    • Iterator support for for...of loops

Testing

  • TypeScript compilation passes (bun turbo typecheck)
  • Unit tests pass (725 tests, 0 failures)
  • Cache utility has 36% line coverage from existing tests

Note: Manual memory testing (monitoring heap growth over time) was not performed.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/9141 **State:** open **Merged:** No --- ## Summary Add a bounded LRU (Least Recently Used) cache utility with eviction callbacks to prevent unbounded memory growth. Fixes #9140 ## Problem Several places in the codebase use unbounded `Map` objects for caching: - Instance cache in `project/instance.ts` - Provider SDK cache in `provider/provider.ts` These can grow without limit in long-running processes or when handling many directories/providers. ## Solution Add a reusable `createLruCache` utility that: - Limits cache size with `maxEntries` option - Evicts least-recently-used entries when full - Provides `onEvict` callback for cleanup logic - Maintains Map-like interface for easy adoption ## Changes - `packages/opencode/src/util/cache.ts` - New LRU cache utility with: - `maxEntries` limit (default: Infinity for backward compatibility) - `onEvict` callback for disposal logic - LRU tracking via `lastAccess` timestamp - Iterator support for `for...of` loops ## Testing - [x] TypeScript compilation passes (`bun turbo typecheck`) - [x] Unit tests pass (725 tests, 0 failures) - [x] Cache utility has 36% line coverage from existing tests **Note:** Manual memory testing (monitoring heap growth over time) was not performed.
yindo added the pull-request label 2026-02-16 18:17:53 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12999