opencode icon indicating copy to clipboard operation
opencode copied to clipboard

feat(util): Add bounded LRU cache utility

Open sauerdaniel opened this issue 3 days ago • 1 comments

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 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.

sauerdaniel avatar Jan 17 '26 23:01 sauerdaniel

The following comment was made by an LLM, it may be inaccurate:

Based on my search results, I found one potentially related PR:

Related PR:

  • #1493 - fix: implement LRU cache for session memory management
    • https://github.com/anomalyco/opencode/pull/1493
    • This PR appears to address similar concerns about LRU caching for memory management in sessions, which aligns with your PR's goal of preventing unbounded memory growth.

The current PR (9141) itself appears in the search results as expected. The other results (#1163 on TUI performance, #8535 on pagination, #7036 on tool caching) are less directly related to the LRU cache utility feature.

github-actions[bot] avatar Jan 17 '26 23:01 github-actions[bot]