fix(sequentialthinking): prevent unbounded memory growth in thought history#3321
Conversation
…istory The SequentialThinkingServer stores all thoughts in memory arrays that grow without bound across the lifetime of the server process. In long-running sessions (6-8+ hours), this can consume 10GB+ of RAM. Changes: - Add configurable max history limit (SEQUENTIAL_THINKING_MAX_HISTORY env var, default 1000) with automatic trimming of oldest thoughts - Add clearHistory() method to explicitly free memory - Register 'sequentialthinking_clear' tool so clients can reset state - Add tests for memory management and history clearing Fixes modelcontextprotocol#2912
|
Excellent fix for a classic long-running server problem! The approach here is solid: What I like:
One consideration: The Alternatives (only if this becomes a problem in practice):
But honestly, for the stated use case (preventing 10GB+ accumulation in 6-8 hour sessions), the current implementation is perfect. Don't optimize prematurely! Minor suggestion: Consider logging a warning when history is trimmed for the first time in a session: if (!this.hasWarnedAboutTrimming) {
console.warn(`Thought history trimmed (limit: ${maxHistory}). Set SEQUENTIAL_THINKING_MAX_HISTORY to increase.`);
this.hasWarnedAboutTrimming = true;
}This would help operators detect if they need to tune the limit without being noisy on every trim. Great work on documenting the root cause and design decisions in the PR description! |
Summary
Fixes #2912 — The
SequentialThinkingServerstores all thoughts in memory arrays (thoughtHistoryandbranches) that grow without bound across the server process lifetime. In long-running sessions (6-8+ hours of frequent use), this can lead to 10GB+ RAM consumption.Root Cause
thoughtHistory: ThoughtData[]andbranches: Record<string, ThoughtData[]>are append-only — thoughts are pushed but never removed or trimmed. Since the server is a long-lived stdio process, memory accumulates indefinitely.Changes
1. Configurable max history with auto-trimming (
lib.ts)SEQUENTIAL_THINKING_MAX_HISTORYenvironment variable (default:1000)splice()2. Explicit
clearHistory()method (lib.ts)thoughtHistoryandbranchesto empty3. New
sequentialthinking_cleartool (index.ts)4. Documentation (
README.md)DISABLE_THOUGHT_LOGGINGand the newSEQUENTIAL_THINKING_MAX_HISTORY5. Tests (
lib.test.ts)clearHistory— verifies thoughts and branches are fully resetSEQUENTIAL_THINKING_MAX_HISTORYis respectedTesting
All 17 tests pass:
Design Decisions