From f8f6c75992f0f09944ae3ec1fb04275f31042108 Mon Sep 17 00:00:00 2001 From: AshSgDe29071999 Date: Mon, 10 Aug 2026 09:43:10 +0530 Subject: [PATCH] fix: reject non-positive search limit at the API boundary limit=0 and negative values are not valid top-k bounds; fail fast. Fixes #2 --- src/tenantq/search.py | 2 ++ tests/test_search.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/tenantq/search.py b/src/tenantq/search.py index 41cc5c4..996e9df 100644 --- a/src/tenantq/search.py +++ b/src/tenantq/search.py @@ -91,6 +91,8 @@ def search( prefetch_limit: int = 50, ) -> List[SearchHit]: """Run a tenant-isolated search in the requested retrieval mode.""" + if limit < 1: + raise ValueError(f"limit must be >= 1, got {limit!r}") qfilter = build_filter(tenant_id, category, created_after, created_before) params = models.SearchParams(hnsw_ef=settings.hnsw.hnsw_ef) diff --git a/tests/test_search.py b/tests/test_search.py index 661e0bc..0da52b0 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -38,3 +38,13 @@ def test_created_at_range_filter(ingested, settings, embedder, dataset): by_id = {d.id: d for d in dataset.documents} for h in hits: assert lo <= by_id[h.id].created_at <= hi + + +def test_search_rejects_non_positive_limit(ingested, settings, embedder): + import pytest + from tenantq.search import search + + with pytest.raises(ValueError, match="limit must be >= 1"): + search(ingested, settings, embedder, "query", tenant_id="acme", limit=0) + with pytest.raises(ValueError, match="limit must be >= 1"): + search(ingested, settings, embedder, "query", tenant_id="acme", limit=-5)