~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(search): validate and deduplicate follow-up relevance filter indices (#4337)
LearningCircuitebabad7bb026
diff --git a/changelog.d/+followup-relevance-filter-validate-indices.bugfix.md b/changelog.d/+followup-relevance-filter-validate-indices.bugfix.mdnew file mode 100644index 0000000000..d3b019a198--- /dev/null+++ b/changelog.d/+followup-relevance-filter-validate-indices.bugfix.md@@ -0,0 +1 @@+Follow-up relevance filtering now rejects negative and out-of-range source indices and deduplicates repeated ones, so a malformed LLM response can no longer select the wrong source or list the same source twice.diff --git a/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.py b/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.pyindex 31c994fbcf..ade8289e08 100644--- a/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.py+++ b/src/local_deep_research/advanced_search_system/filters/followup_relevance_filter.py@@ -48,8 +48,9 @@ class FollowUpRelevanceFilter(BaseFilter):results, query, past_findings, max_results, original_query)- # Return selected sources- filtered = [results[i] for i in relevant_indices if i < len(results)]+ # Return selected sources. Indices are already validated and+ # deduplicated by _select_relevant_sources, so each is in range.+ filtered = [results[i] for i in relevant_indices]logger.info(f"Filtered {len(results)} sources to {len(filtered)} relevant ones "@@ -58,6 +59,31 @@ class FollowUpRelevanceFilter(BaseFilter):return filtered+ def _valid_unique_indices(self, indices, upper_bound):+ """Yield in-range source indices once each, preserving first-seen order.++ Accepts ints and floats (floats are truncated toward zero, e.g.+ ``1.9`` becomes ``1``; some models emit ``0.0``) and+ rejects booleans, non-numeric values, negative indices, and indices at+ or beyond ``upper_bound``. Deduplication prevents the same source from+ being selected more than once.+ """+ seen = set()+ for raw in indices:+ if isinstance(raw, bool):+ continue+ if isinstance(raw, int):+ idx = raw+ elif isinstance(raw, float):+ idx = int(raw)+ else:+ continue+ if idx in seen:+ continue+ if 0 <= idx < upper_bound:+ seen.add(idx)+ yield idx+def _select_relevant_sources(self,sources: List[Dict],@@ -138,19 +164,21 @@ Return the indices of relevant sources as a JSON array:"""indices = extract_json(content, expected_type=list)if indices is not None:- # Validate it's a list of integers- indices = [- int(i)- for i in indices- if isinstance(i, (int, float)) and int(i) < len(sources)- ]+ # Validate, bound-check, and deduplicate the parsed indices+ indices = list(+ self._valid_unique_indices(indices, len(sources))+ )else:logger.debug("Failed to parse JSON, attempting regex fallback")# Fallback to regex extractionimport renumbers = re.findall(r"\d+", content)- indices = [int(n) for n in numbers if int(n) < len(sources)]+ indices = list(+ self._valid_unique_indices(+ (int(n) for n in numbers), len(sources)+ )+ )return indicesexcept Exception as e:diff --git a/tests/advanced_search_system/filters/test_followup_relevance_filter.py b/tests/advanced_search_system/filters/test_followup_relevance_filter.pyindex 8b713eeade..44e5bbdcc1 100644--- a/tests/advanced_search_system/filters/test_followup_relevance_filter.py+++ b/tests/advanced_search_system/filters/test_followup_relevance_filter.py@@ -99,6 +99,27 @@ class TestFilterResults:assert len(filtered) == 5+ def test_filter_results_deduplicates_sources(self):+ """A repeated index must not return the same source twice."""+ from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (+ FollowUpRelevanceFilter,+ )++ mock_model = Mock()+ mock_model.invoke.return_value = Mock(content="[1, 1, 0]")++ filter_obj = FollowUpRelevanceFilter(model=mock_model)+ results = [+ {"title": "Result 1"},+ {"title": "Result 2"},+ {"title": "Result 3"},+ ]++ filtered = filter_obj.filter_results(results, "test query")++ # Index 1 is repeated but its source appears once; first-seen order.+ assert filtered == [results[1], results[0]]+def test_filter_results_with_past_findings(self):"""Test filtering with past findings context."""from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (@@ -345,6 +366,83 @@ class TestSelectRelevantSources:assert indices == [0, 2, 4]+ def test_rejects_negative_indices(self):+ """Negative indices must be rejected, not select the last source."""+ from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (+ FollowUpRelevanceFilter,+ )++ mock_model = Mock()+ mock_model.invoke.return_value = Mock(content="[-1, 0, 2, -5]")++ filter_obj = FollowUpRelevanceFilter(model=mock_model)+ sources = [{"title": f"Source {i}"} for i in range(5)]++ indices = filter_obj._select_relevant_sources(+ sources, "query", "", max_results=5+ )++ # -1 previously slipped through and selected sources[-1] (the last+ # source); it must now be dropped entirely.+ assert indices == [0, 2]++ def test_deduplicates_repeated_indices(self):+ """Repeated indices contribute one entry, preserving first-seen order."""+ from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (+ FollowUpRelevanceFilter,+ )++ mock_model = Mock()+ mock_model.invoke.return_value = Mock(content="[0, 0, 2, 2, 1]")++ filter_obj = FollowUpRelevanceFilter(model=mock_model)+ sources = [{"title": f"Source {i}"} for i in range(5)]++ indices = filter_obj._select_relevant_sources(+ sources, "query", "", max_results=5+ )++ assert indices == [0, 2, 1]++ def test_rejects_bool_indices(self):+ """Booleans (a subclass of int) must not be treated as indices."""+ from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (+ FollowUpRelevanceFilter,+ )++ mock_model = Mock()+ mock_model.invoke.return_value = Mock(content="[true, 0, 2]")++ filter_obj = FollowUpRelevanceFilter(model=mock_model)+ sources = [{"title": f"Source {i}"} for i in range(5)]++ indices = filter_obj._select_relevant_sources(+ sources, "query", "", max_results=5+ )++ # `true` previously coerced to index 1; it must now be skipped.+ assert indices == [0, 2]++ def test_regex_fallback_deduplicates_indices(self):+ """The regex fallback path also deduplicates repeated indices."""+ from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (+ FollowUpRelevanceFilter,+ )++ mock_model = Mock()+ mock_model.invoke.return_value = Mock(+ content="use sources 0, 0, and 2 (really, 0 and 2)"+ )++ filter_obj = FollowUpRelevanceFilter(model=mock_model)+ sources = [{"title": f"Source {i}"} for i in range(5)]++ indices = filter_obj._select_relevant_sources(+ sources, "query", "", max_results=5+ )++ assert indices == [0, 2]+def test_handles_response_not_a_list(self):"""Test handling when response is not a list."""from local_deep_research.advanced_search_system.filters.followup_relevance_filter import (