fix(memory): add TTL, LRU eviction, and ring buffers to prevent memory leaks#12259
fix(memory): add TTL, LRU eviction, and ring buffers to prevent memory leaks#12259Stranmor wants to merge 1 commit intoanomalyco:devfrom
Conversation
…y leaks - ACPSessionManager: add 1h TTL + 50 session limit with periodic cleanup - Instance cache: add 30min idle TTL + 10 instance LRU limit - LSP clients: add 20 client limit with oldest-first eviction - PTY buffers: replace string concatenation with RingBuffer class Addresses issues anomalyco#10913, anomalyco#9140, anomalyco#5363
|
The following comment was made by an LLM, it may be inaccurate: Based on my search results, I found 3 potentially related PRs that address similar memory leak issues:
These PRs likely overlap with the memory leak fixes in PR #12259. You should review PR #12053 in particular, as it appears to address the same core issue of resolving memory leaks in long-running sessions. |
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
… CPU optimization, MCP OAuth client profile workaround, Session Mind fixes, and memory leak prevention Session Mind improvements: - Publish Session.Event.Finished in prompt.ts for short session capture - Add 60s timeout + 2 retries with exponential backoff for memory extraction LLM - Add async indexing functions for immediate memory searchability - Add exponential backoff retry queue for transient indexing failures - Enable Session Mind features by default (saveMemories, enrich_compaction) - Increase context limits (max_tokens: 2000, max_memories: 5, max_sessions: 5) - Enhanced degraded mode logging (info level) - Updated OpenAPI schema defaults and regenerated SDK types Memory leak prevention (from upstream PRs anomalyco#14650, anomalyco#13186, anomalyco#12259, anomalyco#9149): - ACP session TTL (1h) and LRU eviction (max 50 sessions) with 5min cleanup - Signal handlers (SIGTERM/SIGINT/SIGHUP) for graceful shutdown in serve/worker/CLI - LSP client limit (max 20) with oldest-client eviction - Plugin dispose hook for cleanup on Instance.disposeAll() - O(1) string building: textParts[] in copilot message conversion - O(1) string building: argumentChunks[] in copilot tool call streaming - O(1) buffer management: bufferChunks[] in PTY with 2MB limit - 50ms throttled flush in session processor to batch streaming deltas - LRU cache utility for reusable memory-bounded caching Tests added (44 new tests, 2435 total pass): - ACP session manager tests (TTL expiry, LRU eviction, basic ops) - LSP client limit eviction test - Plugin dispose hook tests - PTY buffer chunk tests - Session processor flush throttling tests - LRU cache utility tests
Summary
Addresses memory leak issues reported in #10913, #9140, #5363.
This PR adds cleanup mechanisms to prevent unbounded memory growth in long-running sessions:
Changes
Technical Details
ACPSessionManager (
acp/session.ts)SessionEntrywrapper withlastAccesstimestampInstance cache (
project/instance.ts)CacheEntrywrapper withlastAccesstrackingevictIdleInstances()called on new instance creationLSP clients (
lsp/index.ts)MAX_LSP_CLIENTS = 20limitPTY buffers (
pty/index.ts)RingBufferclass with chunk-based storageTesting
Related Issues