Skip to content

Commit eba206e

Browse files
committed
Improve parsing of total header
1 parent 94a37d0 commit eba206e

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/tests/test_datasources.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ async def test_get_data_sources_filtered_hint_without_total_header(mock_get_api_
276276
assert "without a query" in payload["message"].lower()
277277

278278

279+
@pytest.mark.asyncio
280+
@patch('tools.datasources.get_api_key_from_context')
281+
async def test_get_data_sources_filtered_hint_with_malformed_total_header(mock_get_api_key):
282+
"""A malformed total header is treated as absent rather than raising."""
283+
mock_get_api_key.return_value = "test-key"
284+
mock_ctx, _ = _ctx_with_response(
285+
[{"id": "repo-1", "name": "Repo", "type": "Repository", "relevanceReason": "checkout flow"}],
286+
headers={"X-CodeAlive-Total-Data-Sources": "not-a-number"},
287+
)
288+
289+
result = await get_data_sources(mock_ctx, alive_only=True, query="checkout")
290+
291+
payload = result
292+
assert "omitted" in payload["message"].lower()
293+
assert "without a query" in payload["message"].lower()
294+
295+
279296
@pytest.mark.asyncio
280297
@patch('tools.datasources.get_api_key_from_context')
281298
async def test_get_data_sources_all_relevant_hint_reports_no_omission(mock_get_api_key):

src/tools/datasources.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ def _relevance_message(data_sources: list, response) -> str:
3737
)
3838

3939
shown = len(data_sources)
40-
total_header = response.headers.get(_TOTAL_HEADER)
41-
total = int(total_header) if total_header and total_header.isdigit() else None
40+
try:
41+
total = int(response.headers.get(_TOTAL_HEADER))
42+
except (TypeError, ValueError):
43+
# Header absent (TypeError on int(None)) or malformed (ValueError).
44+
total = None
4245
if total is not None and total > shown:
4346
return (
4447
f"{shown} of {total} available data sources are relevant to this query; the other "

0 commit comments

Comments
 (0)