Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(search): validate and deduplicate GitHub engine ranking indices (#4338)

LearningCircuitb14412eea783
diff --git a/changelog.d/+github-engine-validate-rank-indices.bugfix.md b/changelog.d/+github-engine-validate-rank-indices.bugfix.md
new file mode 100644
index 0000000000..b6a67041fc
--- /dev/null
+++ b/changelog.d/+github-engine-validate-rank-indices.bugfix.md
@@ -0,0 +1 @@
+GitHub search relevance ranking now rejects negative, out-of-range, and non-integer result indices and deduplicates repeated ones, so a malformed LLM response can no longer select the wrong result, list the same repository twice, or discard all results.
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_github.py b/src/local_deep_research/web_search_engines/engines/search_engine_github.py
index 869eacbc1b..b68445bab3 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_github.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_github.py
@@ -871,6 +871,24 @@ class GitHubSearchEngine(BaseSearchEngine):
self.search_endpoint = f"{self.api_base}/search/{search_type}"
logger.info(f"Set GitHub search type to: {search_type}")
+ @staticmethod
+ def _valid_unique_indices(ranked_indices, upper_bound):
+ """Yield valid indices once, preserving first-seen (ranked) order.
+
+ Rejects non-integers, booleans, negative and out-of-range indices,
+ and deduplicates so a malformed LLM response cannot select the wrong
+ preview (e.g. ``-1`` -> last preview) or list the same preview twice.
+ """
+ seen = set()
+ for idx in ranked_indices:
+ if not isinstance(idx, int) or isinstance(idx, bool):
+ continue
+ if idx in seen:
+ continue
+ if 0 <= idx < upper_bound:
+ seen.add(idx)
+ yield idx
+
def _filter_for_relevance(
self, previews: List[Dict[str, Any]], query: str
) -> List[Dict[str, Any]]:
@@ -912,11 +930,14 @@ Do not include any other text or explanation."""
ranked_indices = extract_json(response_text, expected_type=list)
if ranked_indices is not None:
- # Return the results in ranked order
- ranked_results = []
- for idx in ranked_indices:
- if idx < len(previews):
- ranked_results.append(previews[idx])
+ # Return the results in ranked order, validated and
+ # deduplicated so each preview appears at most once.
+ ranked_results = [
+ previews[idx]
+ for idx in self._valid_unique_indices(
+ ranked_indices, len(previews)
+ )
+ ]
# Limit to max_filtered_results if specified
if (
diff --git a/tests/web_search_engines/engines/test_search_engine_github_coverage.py b/tests/web_search_engines/engines/test_search_engine_github_coverage.py
index d1575560d4..55c17b3bd5 100644
--- a/tests/web_search_engines/engines/test_search_engine_github_coverage.py
+++ b/tests/web_search_engines/engines/test_search_engine_github_coverage.py
@@ -761,6 +761,50 @@ class TestFilterForRelevanceCoverage:
result = engine._filter_for_relevance(previews, "query")
assert [r["id"] for r in result] == ["a", "b"]
+ def test_negative_index_rejected(self):
+ """Negative indices must be rejected, not select the last preview."""
+ llm = Mock()
+ engine = _make_engine(llm=llm)
+ previews = [{"id": "a"}, {"id": "b"}, {"id": "c"}]
+ with patch(f"{MODULE}.extract_json", return_value=[-1, 0, 2]):
+ with patch(
+ f"{MODULE}.get_llm_response_text", return_value="[-1, 0, 2]"
+ ):
+ result = engine._filter_for_relevance(previews, "query")
+ # -1 previously selected previews[-1] (the last preview); it must
+ # now be dropped entirely.
+ assert [r["id"] for r in result] == ["a", "c"]
+
+ def test_duplicate_indices_deduplicated(self):
+ """Repeated indices contribute one preview, preserving rank order."""
+ llm = Mock()
+ engine = _make_engine(llm=llm)
+ previews = [{"id": "a"}, {"id": "b"}, {"id": "c"}]
+ with patch(f"{MODULE}.extract_json", return_value=[2, 2, 0, 2, 1]):
+ with patch(
+ f"{MODULE}.get_llm_response_text",
+ return_value="[2, 2, 0, 2, 1]",
+ ):
+ result = engine._filter_for_relevance(previews, "query")
+ assert [r["id"] for r in result] == ["c", "a", "b"]
+
+ def test_non_integer_indices_skipped(self):
+ """Booleans, strings, and floats are skipped without raising."""
+ llm = Mock()
+ engine = _make_engine(llm=llm)
+ previews = [{"id": "a"}, {"id": "b"}, {"id": "c"}]
+ with patch(
+ f"{MODULE}.extract_json", return_value=[0, "1", True, 1.0, 2]
+ ):
+ with patch(
+ f"{MODULE}.get_llm_response_text",
+ return_value='[0, "1", true, 1.0, 2]',
+ ):
+ result = engine._filter_for_relevance(previews, "query")
+ # Only the genuine ints 0 and 2 survive; "1"/True/1.0 are dropped
+ # rather than raising and nuking the whole result set.
+ assert [r["id"] for r in result] == ["a", "c"]
+
# ---------------------------------------------------------------------------
# close