Summary
Tunacode uses a strict cache registry (CacheManager) plus typed cache accessors as the only supported public boundary for cache reads/writes. This removes ad-hoc module-level dict caches, enforces fail-fast behavior for unregistered caches, and makes invalidation strategies (manual vs mtime-driven) testable and deterministic.
Context
Implemented as part of epic tun-gqgx (cache unification). The initial migration targets:
- agent reuse + versioning (
agent_config.py) AGENTS.mdproject context loading.gitignore-driven ignore manager caching
Changes
Added
tunacode.infrastructure.cache:CacheManagersingleton with explicitregister_cache()and strictget_cache()(raises if missing)- per-cache key metadata (
set_metadata/get_metadata) - strategies:
ManualStrategy: never auto-invalidatesMtimeStrategy: invalidates whenos.stat(metadata.path).st_mtime_ns != metadata.mtime_ns(missing file treated asmtime_ns=0)
- deterministic
clear_all()/clear_cache()helpers for tests
Added typed accessor modules under
tunacode.infrastructure.cache.caches/:agents.py: version-aware get/set/invalidate/cleartunacode_context.py: mtime-awareget_context(path: Path) -> str
Added typed accessor modules under
tunacode.tools.cache_accessors/:ignore_manager_cache.py: mtime-awareget_ignore_manager(root: Path) -> IgnoreManagerripgrep_cache.py: manual cache for platform identifier + binary pathxml_prompts_cache.py: mtime-aware XML prompt caching (including cached None)
Behavioral Impact
- No module should expose cache dicts as globals or import them in tests.
- Call sites should import accessors (e.g.
tunacode.infrastructure.cache.caches.agents) and avoid touchingCacheManagerdirectly. - Tests should clear caches via
tunacode.infrastructure.cache.clear_all()or per-accessorclear_*()helpers (no singleton reset).
Related Cards
- [[cache]]