#23744: fix(memory): add max size eviction to session manager cache
agents
size: XS
## Summary
- Problem: Session manager cache (`SESSION_MANAGER_CACHE` Map) grows without bounds as new sessions are tracked, with no maximum size limit
- Why it matters: In long-running gateway instances with many unique sessions, unbounded cache growth causes gradual memory pressure
- What changed: Added a max size limit (500 entries) with LRU-style eviction of oldest entries when the limit is exceeded
- What did NOT change: Existing TTL-based cleanup logic is preserved; cache behavior for entries within the limit is identical
## Change Type
- [x] Bug fix
## Scope
- [x] Gateway / orchestration
- [x] Memory / storage
## Security Impact
- New permissions/capabilities? No
- Secrets/tokens handling changed? No
- New/changed network calls? No
- Command/tool execution surface changed? No
- Data access scope changed? No
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Added a max size limit (500 entries) with LRU-style eviction to prevent unbounded growth of `SESSION_MANAGER_CACHE`. The implementation leverages Map's insertion-order iteration to evict oldest entries when the limit is exceeded.
- Introduced `MAX_CACHE_SIZE` constant set to 500
- Added eviction logic in `trackSessionManagerAccess` after setting new entries
- Off-by-one issue: cache can grow to 501 entries before eviction (checks size after insertion)
<h3>Confidence Score: 4/5</h3>
- Safe to merge after fixing the off-by-one error in the eviction check
- The implementation correctly addresses unbounded cache growth with a sensible max size and LRU eviction. The off-by-one error (checking `>` instead of `>=`) allows the cache to grow to 501 entries instead of 500, but this is a minor issue that doesn't compromise functionality - just allows one extra entry temporarily
- Fix the eviction threshold check in `session-manager-cache.ts:35`
<sub>Last reviewed commit: 2ad97e9</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#10280: fix(infra): add max size limit to Tailscale whois cache to prevent ...
by programming-pupil · 2026-02-06
77.5%
#7592: fix(slack): prevent unbounded memory growth in channel type cache
by namratabhaumik · 2026-02-03
75.4%
#23639: fix(agents): stop re-resizing session history images on every turn ...
by yinghaosang · 2026-02-22
75.1%
#22220: feat(bootstrap): cache session's bootstrap files so we don't invali...
by anisoptera · 2026-02-20
72.4%
#20148: fix(memory): persist session dirty state and fix reindex gate
by togotago · 2026-02-18
71.6%
#16987: fix(config): add skipCache to updateSessionStoreEntry and updateLas...
by AI-Reviewer-QS · 2026-02-15
71.1%
#23706: perf: cache image resize results to avoid redundant processing (#23...
by echoVic · 2026-02-22
70.8%
#19761: fix(security): OC-69 cap ACP session creation to prevent memory exh...
by aether-ai-agent · 2026-02-18
70.4%
#15945: fix(memory-flush): only write memoryFlushCompactionCount when compa...
by aldoeliacim · 2026-02-14
70.1%
#12997: feat(infra): Add query caching layer with TTL and LRU eviction
by trevorgordon981 · 2026-02-10
70.1%