~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(focused-iteration): reset per-call state and offset citations across subsections (#4850)
S3ef3900c94ae
diff --git a/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.py b/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.pyindex d8905d63d9..4952f5ecd6 100644--- a/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/focused_iteration_strategy.py@@ -141,6 +141,10 @@ class FocusedIterationStrategy(BaseSearchStrategy):"""logger.info(f"Starting focused iteration search: {query}")+ # Clear results from any previous runs+ self.all_search_results = []+ self.results_by_iteration = {}+self._update_progress("Initializing focused iteration search",5,@@ -265,7 +269,6 @@ class FocusedIterationStrategy(BaseSearchStrategy):# Accumulate all results (no filtering!)self.all_search_results.extend(iteration_results)- self.all_links_of_system.extend(iteration_results)# Track result count for this iterationself.results_by_iteration[iteration] = len(iteration_results)@@ -291,6 +294,22 @@ class FocusedIterationStrategy(BaseSearchStrategy):logger.info(f"Early termination at iteration {iteration}")break+ # Capture citation count BEFORE we add this call's results to the+ # shared bibliography. Used as the citation offset so each+ # analyze_topic() call produces continuous citation numbers+ # (e.g. first call: [1]-[15], second: [16]-[28]). This matches+ # what source-based and langgraph-agent already do.+ total_citation_count_before_this_search = len(+ self.all_links_of_system+ )++ # Extend the SHARED all_links_of_system with this call's results.+ # analyze_followup populates "index" on these dicts (see+ # base_citation_handler._create_documents), and since they're the+ # same objects now in all_links_of_system, the indices propagate+ # to the shared list automatically.+ self.all_links_of_system.extend(self.all_search_results)+# Final synthesis (like source-based - trust the LLM!)self._update_progress(f"Synthesizing {len(self.all_search_results)} sources from {len(self.questions_by_iteration)} iterations...",@@ -299,12 +318,12 @@ class FocusedIterationStrategy(BaseSearchStrategy):)# Use citation handler for final synthesis- # Note: nr_of_links=0 ensures citations start at [1]+ # Note: nr_of_links=total_citation_count_before_this_search ensures sequential report citations are offset correctlyfinal_result = self.citation_handler.analyze_followup(query,self.all_search_results,previous_knowledge="",- nr_of_links=0,+ nr_of_links=total_citation_count_before_this_search,)synthesized_content = final_result.get(diff --git a/tests/strategies/test_strategy_behaviors.py b/tests/strategies/test_strategy_behaviors.pyindex 5b894a3f5b..8d60815181 100644--- a/tests/strategies/test_strategy_behaviors.py+++ b/tests/strategies/test_strategy_behaviors.py@@ -159,6 +159,169 @@ class TestFocusedIterationStrategy:# Should have questions trackedassert strategy.questions_by_iteration is not None+ def test_does_not_accumulate_results_across_calls(+ self,+ strategy_mock_llm,+ strategy_mock_search,+ strategy_settings_snapshot,+ ):+ """Test that successive analyze_topic calls do not accumulate search results."""+ from local_deep_research.search_system_factory import (+ create_strategy,+ )++ strategy = create_strategy(+ strategy_name="focused-iteration",+ model=strategy_mock_llm,+ search=strategy_mock_search,+ settings_snapshot=strategy_settings_snapshot,+ )++ strategy.analyze_topic("First query")+ first_len = len(strategy.all_search_results)+ assert first_len > 0++ strategy.analyze_topic("Second query")+ # Should be fresh, not cumulative+ assert len(strategy.all_search_results) == first_len++ def test_citation_offset_uses_existing_all_links_count(+ self,+ strategy_mock_llm,+ strategy_mock_search,+ strategy_settings_snapshot,+ ):+ """analyze_topic must pass nr_of_links=len(all_links_of_system) to the+ citation handler, not a hardcoded 0. Otherwise detailed-report+ subsections all start citations at [1] and collide on the shared+ bibliography."""+ import copy+ from unittest.mock import patch++ # Return fresh copies per call so dict identity does not leak+ # indices across calls.+ template_results = strategy_mock_search.run.return_value+ strategy_mock_search.run.side_effect = lambda *a, **kw: copy.deepcopy(+ template_results+ )++ # Pre-populate all_links_of_system to simulate a prior subsection+ # that already added 7 sources.+ prior_links = [+ {+ "title": f"prior-{i}",+ "link": f"http://prior-{i}.example",+ "snippet": f"prior snippet {i}",+ "full_content": f"prior content {i}",+ }+ for i in range(7)+ ]+ prior_count = len(prior_links)+ # create_strategy with shared all_links_of_system+ from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import ( # noqa: E501+ FocusedIterationStrategy,+ )++ strategy = FocusedIterationStrategy(+ model=strategy_mock_llm,+ search=strategy_mock_search,+ all_links_of_system=prior_links,+ settings_snapshot=strategy_settings_snapshot,+ max_iterations=1,+ )++ with patch.object(+ strategy.citation_handler,+ "analyze_followup",+ return_value={+ "content": "ok",+ "documents": [],+ },+ ) as spy:+ strategy.analyze_topic("Some query")++ # The handler MUST have been called once.+ assert spy.call_count == 1+ # And the nr_of_links kwarg MUST reflect the pre-populated count+ # so citations continue, not restart at [1]. (Note: prior_links+ # is the same list object as strategy.all_links_of_system and+ # gets extended during analyze_topic, so capture the prior+ # length before the call.)+ kwargs = spy.call_args.kwargs+ assert kwargs["nr_of_links"] == prior_count++ def test_report_mode_indices_stay_sequential_across_sections(+ self,+ strategy_mock_llm,+ strategy_mock_search,+ strategy_settings_snapshot,+ ):+ """In detailed report mode, IntegratedReportGenerator calls+ analyze_topic() once per subsection against a SHARED+ all_links_of_system. After subsection N, the next subsection must+ continue citation indices (e.g. [16]) rather than restart at [1].+ This is the regression covered by #4851."""+ import copy+ from unittest.mock import patch++ # Fresh dicts per search.run() call so that previously assigned+ # "index" values do not bleed across subsections.+ template_results = strategy_mock_search.run.return_value+ strategy_mock_search.run.side_effect = lambda *a, **kw: copy.deepcopy(+ template_results+ )++ shared_links: list = []+ from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import ( # noqa: E501+ FocusedIterationStrategy,+ )++ strategy = FocusedIterationStrategy(+ model=strategy_mock_llm,+ search=strategy_mock_search,+ all_links_of_system=shared_links,+ settings_snapshot=strategy_settings_snapshot,+ max_iterations=1,+ )++ # Stub citation handler so it does not need a real LLM. The real+ # handler mutates result["index"] in place; we replicate that here+ # so we can inspect all_links_of_system afterwards.+ def fake_analyze_followup(+ question, search_results, previous_knowledge, nr_of_links+ ):+ for i, r in enumerate(search_results):+ if isinstance(r, dict) and "index" not in r:+ r["index"] = str(i + nr_of_links + 1)+ return {"content": "ok", "documents": []}++ with patch.object(+ strategy.citation_handler,+ "analyze_followup",+ side_effect=fake_analyze_followup,+ ):+ strategy.analyze_topic("Subsection A")+ count_after_a = len(shared_links)+ assert count_after_a > 0+ strategy.analyze_topic("Subsection B")++ # Total sources = A's + B's+ assert len(shared_links) == 2 * count_after_a++ # Index of the first source in subsection B must be one past the+ # last index from subsection A — i.e. citations continued, they+ # did not restart at [1].+ subsection_a_indices = [+ int(shared_links[i]["index"]) for i in range(count_after_a)+ ]+ subsection_b_indices = [+ int(shared_links[i]["index"])+ for i in range(count_after_a, len(shared_links))+ ]+ assert subsection_b_indices[0] == max(subsection_a_indices) + 1+ # And there must be no overlap between the two index ranges.+ assert not (set(subsection_a_indices) & set(subsection_b_indices))+class TestTopicOrganizationStrategy:"""Tests for TopicOrganizationStrategy."""