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): deduplicate cross-engine filter indices (#3867)

qWait26fd86cd3e29
diff --git a/changelog.d/+cross-engine-filter-deduplicate-indices.bugfix.md b/changelog.d/+cross-engine-filter-deduplicate-indices.bugfix.md
new file mode 100644
index 0000000000..f5d8dc0bba
--- /dev/null
+++ b/changelog.d/+cross-engine-filter-deduplicate-indices.bugfix.md
@@ -0,0 +1 @@
+Cross-engine filtering now deduplicates repeated LLM-ranked result indices so the same source does not appear multiple times with different citation numbers.
diff --git a/src/local_deep_research/advanced_search_system/filters/cross_engine_filter.py b/src/local_deep_research/advanced_search_system/filters/cross_engine_filter.py
index 6b6caa783e..c965242e80 100644
--- a/src/local_deep_research/advanced_search_system/filters/cross_engine_filter.py
+++ b/src/local_deep_research/advanced_search_system/filters/cross_engine_filter.py
@@ -78,6 +78,21 @@ class CrossEngineFilter(BaseFilter):
result["index"] = str(i + start_index + 1)
return results
+ def _valid_unique_indices(self, ranked_indices, upper_bound):
+ """Yield valid indices once, preserving first-seen order."""
+ seen = set()
+ for idx in ranked_indices:
+ if not isinstance(idx, int) or isinstance(idx, bool):
+ logger.warning(
+ f"Skipping non-integer ranked index from cross-engine filter: {idx!r}"
+ )
+ continue
+ if idx in seen:
+ continue
+ if 0 <= idx < upper_bound:
+ seen.add(idx)
+ yield idx
+
def filter_results(
self,
results: List[Dict],
@@ -159,10 +174,11 @@ If no results seem relevant to the query, return an empty array: []"""
# Just keep the results that were deemed relevant
filtered_results = []
for idx in sorted(
- ranked_indices
+ self._valid_unique_indices(
+ ranked_indices, len(context_results)
+ )
): # Sort to maintain original order
- if 0 <= idx < len(context_results):
- filtered_results.append(context_results[idx])
+ filtered_results.append(context_results[idx])
# Limit results if needed
final_results = filtered_results[
@@ -191,9 +207,10 @@ If no results seem relevant to the query, return an empty array: []"""
# Create ranked results list (reordering)
ranked_results = []
- for idx in ranked_indices:
- if 0 <= idx < len(context_results):
- ranked_results.append(context_results[idx])
+ for idx in self._valid_unique_indices(
+ ranked_indices, len(context_results)
+ ):
+ ranked_results.append(context_results[idx])
# If filtering removed everything, return top results
if not ranked_results and results:
diff --git a/tests/advanced_search_system/filters/test_cross_engine_filter.py b/tests/advanced_search_system/filters/test_cross_engine_filter.py
index dc6a8a8633..345ac7284b 100644
--- a/tests/advanced_search_system/filters/test_cross_engine_filter.py
+++ b/tests/advanced_search_system/filters/test_cross_engine_filter.py
@@ -454,6 +454,18 @@ class TestFilterResultsValidation:
assert filtered[1] is results[1]
assert filtered[2] is results[2]
+ def test_deduplicates_repeated_llm_indices_reorder_true(self):
+ """Repeated indices should only contribute one ranked result."""
+ f, results = _make_filter_and_results("[2, 2, 0, 2, 1]")
+ filtered = f.filter_results(results, "query", reorder=True)
+ assert filtered == [results[2], results[0], results[1]]
+
+ def test_deduplicates_repeated_llm_indices_reorder_false(self):
+ """Repeated indices should be deduplicated before original-order sorting."""
+ f, results = _make_filter_and_results("[2, 2, 0, 2, 1]")
+ filtered = f.filter_results(results, "query", reorder=False)
+ assert filtered == [results[0], results[1], results[2]]
+
def test_indices_outside_context_window_are_rejected(self):
"""LLM indices cannot reference results outside the evaluated context."""
f, results = _make_filter_and_results("[0, 4]")
@@ -472,6 +484,18 @@ class TestFilterResultsValidation:
assert filtered == [results[0]]
+ @patch(
+ "local_deep_research.advanced_search_system.filters.cross_engine_filter.logger.warning"
+ )
+ def test_logs_warning_for_non_integer_indices(self, mock_warning):
+ """Non-integer indices are skipped with a warning."""
+ f, results = _make_filter_and_results('[0, "1", true, 2]')
+
+ filtered = f.filter_results(results, "query", reorder=True)
+
+ assert filtered == [results[0], results[2]]
+ assert mock_warning.call_count == 2
+
def test_all_invalid_indices_reorder_false_returns_fallback(self):
"""When all indices are invalid with reorder=False, return top 10."""
f, results = _make_filter_and_results("[999, 998]")