Skip to content

Fix type and security issues in Record/Replay functionality #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: devin/1746483771-add-record-replay-functionality
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/crewai/memory/storage/llm_response_cache_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(
) -> None:
self.db_path = db_path
self._printer: Printer = Printer()
self._connection_pool = {}
self._connection_pool: Dict[int, sqlite3.Connection] = {}
self._initialize_db()

def _get_connection(self) -> sqlite3.Connection:
Expand Down Expand Up @@ -214,10 +214,27 @@ def cleanup_expired_cache(self, max_age_days: int = 7) -> None:
"""
Removes cache entries older than the specified number of days.

This method helps maintain the cache size and ensures that only recent
responses are kept, which is important for keeping the cache relevant
and preventing it from growing too large over time.

Args:
max_age_days: Maximum age of cache entries in days. Defaults to 7.
If set to 0, all entries will be deleted.
Must be a non-negative integer.

Raises:
ValueError: If max_age_days is not a non-negative integer.
"""
if not isinstance(max_age_days, int) or max_age_days < 0:
error_msg = "max_age_days must be a non-negative integer"
self._printer.print(
content=f"LLM RESPONSE CACHE ERROR: {error_msg}",
color="red",
)
logger.error(error_msg)
raise ValueError(error_msg)

try:
conn = self._get_connection()
cursor = conn.cursor()
Expand All @@ -228,10 +245,11 @@ def cleanup_expired_cache(self, max_age_days: int = 7) -> None:
logger.info("Deleting all cache entries (max_age_days <= 0)")
else:
cursor.execute(
f"""
DELETE FROM llm_response_cache
WHERE timestamp < datetime('now', '-{max_age_days} days')
"""
DELETE FROM llm_response_cache
WHERE timestamp < datetime('now', ? || ' days')
""",
(f"-{max_age_days}",)
)
deleted_count = cursor.rowcount

Expand Down
Loading