Fix unbounded memory growth (Issue #2) #3003
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2 - Implements bounded state management to prevent memory leaks in long-running server sessions.
The Problem
thoughtHistoryandbranchesarrays grew unboundedWhy PR #2 Was Wrong
PR #2 attempted to fix this by creating per-request instances (stateless architecture). While that prevented memory leaks, it completely broke the intended functionality:
The singleton pattern is actually correct for stdio transport, where each client spawns a dedicated server process. The real issue was unbounded growth, not shared state.
The Solution
Maintain the singleton pattern (necessary for stdio) but add configurable bounds:
Changes
Test Results
Architecture Notes
This server is designed for stdio transport where:
Concurrent requests are theoretically possible but extremely rare in practice (would require LLM explicitly calling tools in parallel).
Memory Impact
Before: Unbounded growth (1000 thoughts = ~1MB, could grow indefinitely)
After: Capped at ~100KB-1MB depending on configuration
Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com