Skip to content

Commit e3f8c83

Browse files
phernandezclaude
andcommitted
fix: validate page/page_size in recent_activity before API call
Prevents negative OFFSET errors from reaching the database when invalid pagination values (page=0, page_size=-1, etc.) are passed. Raises clear ValueError messages at the tool boundary. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 14eb35e commit e3f8c83

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/basic_memory/mcp/tools/recent_activity.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ async def recent_activity(
111111
- For focused queries, consider using build_context with a specific URI
112112
- Max timeframe is 1 year in the past
113113
"""
114+
# Validate pagination arguments before they reach the API layer,
115+
# where negative offset would cause a database error.
116+
if page < 1:
117+
raise ValueError(f"page must be >= 1, got {page}")
118+
if page_size < 1:
119+
raise ValueError(f"page_size must be >= 1, got {page_size}")
120+
if page_size > 100:
121+
raise ValueError(f"page_size must be <= 100, got {page_size}")
122+
114123
# Build common parameters for API calls
115124
params: dict = {
116125
"page": page,

tests/mcp/test_tool_recent_activity.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,25 @@ async def test_recent_activity_pagination_params(client, test_project, test_grap
493493
assert "Recent Activity:" in result
494494

495495

496+
@pytest.mark.asyncio
497+
async def test_recent_activity_pagination_validation():
498+
"""Invalid page/page_size values should raise clear ValueError messages."""
499+
with pytest.raises(ValueError, match="page must be >= 1, got 0"):
500+
await recent_activity.fn(page=0)
501+
502+
with pytest.raises(ValueError, match="page must be >= 1, got -1"):
503+
await recent_activity.fn(page=-1)
504+
505+
with pytest.raises(ValueError, match="page_size must be >= 1, got 0"):
506+
await recent_activity.fn(page_size=0)
507+
508+
with pytest.raises(ValueError, match="page_size must be >= 1, got -5"):
509+
await recent_activity.fn(page_size=-5)
510+
511+
with pytest.raises(ValueError, match="page_size must be <= 100, got 999"):
512+
await recent_activity.fn(page_size=999)
513+
514+
496515
def test_format_project_output_has_more_pagination_guidance():
497516
"""When has_more is True, activity summary should show pagination guidance."""
498517
import importlib

0 commit comments

Comments
 (0)