From 7000128a8dae378af219d4a41cf7695a6daf7b47 Mon Sep 17 00:00:00 2001 From: "Grapette.L" <90967079+Lesereingrape@users.noreply.github.com> Date: Mon, 12 Oct 2026 23:47:58 +0800 Subject: [PATCH] Python: Reject out-of-range Cosmos memory search parameters --- .../_context_provider.py | 8 ++++++++ .../tests/test_context_provider.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/python/packages/azure-cosmos-memory/agent_framework_azure_cosmos_memory/_context_provider.py b/python/packages/azure-cosmos-memory/agent_framework_azure_cosmos_memory/_context_provider.py index b8452fd4e39..613bb2f1e14 100644 --- a/python/packages/azure-cosmos-memory/agent_framework_azure_cosmos_memory/_context_provider.py +++ b/python/packages/azure-cosmos-memory/agent_framework_azure_cosmos_memory/_context_provider.py @@ -150,12 +150,20 @@ def __init__( SettingNotFoundError: If ``cosmos_endpoint``, ``foundry_endpoint``, ``embedding_model``, or ``chat_model`` cannot be resolved from arguments or the environment (only when ``memory_client`` is not supplied). + ValueError: If ``top_k`` is not positive, or ``min_confidence`` is outside 0.0-1.0. """ super().__init__(source_id) # Track whether we created the client (and thus should close it in __aexit__) # vs. received a pre-created client (which the caller owns and should close) self._should_close_client = False + # Both values reach the toolkit on every retrieval, where bad ones fail silently: a + # non-positive top_k makes the toolkit raise (logged per turn, no memories), and a + # min_confidence outside 0.0-1.0 either matches nothing or drops the filter entirely. + if top_k <= 0: + raise ValueError("top_k must be greater than 0") + if not 0.0 <= min_confidence <= 1.0: + raise ValueError("min_confidence must be between 0.0 and 1.0") self.top_k = top_k self.min_confidence = min_confidence self.memory_types: list[MemoryType] = list(memory_types) if memory_types else ["fact", "procedural"] diff --git a/python/packages/azure-cosmos-memory/tests/test_context_provider.py b/python/packages/azure-cosmos-memory/tests/test_context_provider.py index b12296c3331..5c601cd1b5c 100644 --- a/python/packages/azure-cosmos-memory/tests/test_context_provider.py +++ b/python/packages/azure-cosmos-memory/tests/test_context_provider.py @@ -132,6 +132,25 @@ def test_init_default_values(self, mock_memory_client: AsyncMock) -> None: assert provider.context_prompt == DEFAULT_CONTEXT_PROMPT assert provider.auto_extract is True + def test_init_rejects_non_positive_top_k(self, mock_memory_client: AsyncMock) -> None: + """The toolkit rejects a non-positive top_k on every search, so fail at construction.""" + for top_k in (0, -1): + with pytest.raises(ValueError, match="top_k must be greater than 0"): + CosmosMemoryContextProvider(memory_client=mock_memory_client, top_k=top_k) + + def test_init_rejects_min_confidence_outside_documented_range(self, mock_memory_client: AsyncMock) -> None: + """Scores above 1.0 match no memory and negative scores drop the filter; both are silent.""" + for min_confidence in (-0.1, 1.01, float("nan"), float("inf")): + with pytest.raises(ValueError, match="min_confidence must be between 0.0 and 1.0"): + CosmosMemoryContextProvider(memory_client=mock_memory_client, min_confidence=min_confidence) + + def test_init_accepts_boundary_search_params(self, mock_memory_client: AsyncMock) -> None: + """The documented bounds are inclusive.""" + provider = CosmosMemoryContextProvider(memory_client=mock_memory_client, top_k=1, min_confidence=1.0) + + assert provider.top_k == 1 + assert provider.min_confidence == 1.0 + def test_init_creates_client_when_none(self) -> None: """When no client provided, creates AsyncCosmosMemoryClient with default credential.""" with (