opencode
opencode copied to clipboard
feat(util): Add bounded LRU cache utility
Summary
Add a bounded LRU (Least Recently Used) cache utility with eviction callbacks to prevent unbounded memory growth.
Fixes #9143
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
maxEntriesoption - Evicts least-recently-used entries when full
- Provides
onEvictcallback for cleanup logic - Maintains Map-like interface for easy adoption
Changes
-
packages/opencode/src/util/cache.ts- New LRU cache utility with:-
maxEntrieslimit (default: Infinity for backward compatibility) -
onEvictcallback for disposal logic - LRU tracking via
lastAccesstimestamp - Iterator support for
for...ofloops
-
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.