Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
Loading