fix(engine): bound ToolResponseStore size and cleanup consumed entries#50
Closed
fix(engine): bound ToolResponseStore size and cleanup consumed entries#50
Conversation
Fixes #5292 and #5293 - ToolResponseStore memory issues. Problem: 1. Store grows without limit 2. Consumed responses not removed Solution: - Added ToolResponseStore with configurable max size (default: 500 entries) - Entries are removed when consumed via take() method (#5293) - Automatic periodic cleanup of expired entries based on TTL - Eviction of oldest entries when capacity is reached (#5292) Features: - MAX_STORE_SIZE constant (500) to prevent unbounded growth - DEFAULT_TTL (5 minutes) for automatic expiration - CLEANUP_INTERVAL (1 minute) for periodic cleanup - get() for peeking without removal - take() for consuming and removing entries - cleanup_expired() and cleanup_read() for manual cleanup - Stats tracking for monitoring store behavior
Greptile OverviewGreptile SummaryAdded bounded Key features implemented:
Minor issues found:
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| src/cortex-engine/src/tools/response_store.rs | New bounded storage for tool responses with size limits, TTL-based cleanup, and statistics tracking - has unused imports and config field |
| src/cortex-engine/src/tools/mod.rs | Added module exports for new response_store with all public types and functions |
Sequence Diagram
sequenceDiagram
participant Client
participant Store as ToolResponseStore
participant HashMap as responses: RwLock<HashMap>
participant Stats as stats: RwLock<StoreStats>
Note over Client,Stats: Store Tool Response
Client->>Store: store(call_id, tool_name, result)
Store->>Store: maybe_cleanup() (periodic)
Store->>HashMap: write().await
alt at capacity (>= MAX_STORE_SIZE)
Store->>HashMap: find_oldest_key()
Store->>HashMap: remove(oldest_key)
Note over Store: evicted = true
end
Store->>HashMap: insert(call_id, StoredResponse)
Store->>Stats: write().await
Store->>Stats: total_stored += 1
alt if evicted
Store->>Stats: evictions += 1
end
Store-->>Client: evicted (bool)
Note over Client,Stats: Peek (Get without removing)
Client->>Store: get(call_id)
Store->>HashMap: write().await
alt entry exists
Store->>HashMap: set read = true
Store->>Stats: write().await
Store->>Stats: reads += 1
Store-->>Client: Some(ToolResult)
else
Store-->>Client: None
end
Note over Client,Stats: Consume (Take and remove)
Client->>Store: take(call_id)
Store->>HashMap: write().await
alt entry exists
Store->>HashMap: remove(call_id)
Store->>Stats: write().await
Store->>Stats: takes += 1
Store-->>Client: Some(ToolResult)
else
Store-->>Client: None
end
Note over Client,Stats: Cleanup Expired
Client->>Store: cleanup_expired()
Store->>HashMap: write().await
Store->>HashMap: retain(|_, v| !v.is_expired(ttl))
alt removed > 0
Store->>Stats: write().await
Store->>Stats: expired_cleanups += removed
end
Store-->>Client: removed (usize)
| use std::time::{Duration, Instant}; | ||
|
|
||
| use tokio::sync::RwLock; | ||
| use tracing::{debug, warn}; |
There was a problem hiding this comment.
warn imported but never used
Suggested change
| use tracing::{debug, warn}; | |
| use tracing::debug; |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-engine/src/tools/response_store.rs
Line: 15:15
Comment:
`warn` imported but never used
```suggestion
use tracing::debug;
```
<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>
How can I resolve this? If you propose a fix, please make it concise.| /// Time-to-live for stored responses. | ||
| pub ttl: Duration, | ||
| /// Whether to remove entries on read (peek vs consume). | ||
| pub remove_on_read: bool, |
There was a problem hiding this comment.
remove_on_read config field is never used - the get() method always keeps entries and take() always removes them regardless of this setting
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cortex-engine/src/tools/response_store.rs
Line: 72:72
Comment:
`remove_on_read` config field is never used - the `get()` method always keeps entries and `take()` always removes them regardless of this setting
How can I resolve this? If you propose a fix, please make it concise.
echobt
added a commit
that referenced
this pull request
Feb 4, 2026
This PR consolidates the following memory and storage fixes: - #44: Add cleanup for stale file locks to prevent memory leak - #45: Add cache size limits to prevent unbounded memory growth - #47: Add fsync after file writes to prevent data loss - #50: Bound ToolResponseStore size and cleanup consumed entries - #51: Eliminate TOCTOU races in MCP server and plugin registry - #52: Improve path validation and tilde expansion Key changes: - Added periodic cleanup of stale file locks - Implemented LRU cache limits for config discovery and tokenizer - Added fsync calls after critical file writes - Created bounded ToolResponseStore with automatic cleanup - Fixed time-of-check-time-of-use races - Improved path validation security
Contributor
Author
|
Consolidated into #80 - fix: consolidated memory and storage improvements |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #5292 and #5293 - ToolResponseStore memory issues.
Problem
Solution
Added ToolResponseStore with:
take()method removes entries when consumedAPI
Configuration
MAX_STORE_SIZE: 500 (default max entries)DEFAULT_TTL: 5 minutes (entry expiration)CLEANUP_INTERVAL: 1 minute (automatic cleanup frequency)