~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(strategies): port cancellation check to 5 strategies (issue #4871) (#5092)
Sfbdeb2609904
diff --git a/changelog.d/+enhanced-followup-cancellation-lag.bugfix.md b/changelog.d/+enhanced-followup-cancellation-lag.bugfix.mdnew file mode 100644index 0000000000..5e184f33ad--- /dev/null+++ b/changelog.d/+enhanced-followup-cancellation-lag.bugfix.md@@ -0,0 +1 @@+Fixed a multi-second lag between clicking **Stop** and an enhanced-contextual follow-up research actually stopping. `EnhancedContextualFollowUpStrategy.analyze_topic` now calls `self.check_termination()` before its own `generate_contextualized_query` LLM work, so a cancellation request aborts the strategy before any contextualized-query or delegate-strategy work. Port of the fix from #4872.diff --git a/changelog.d/+langgraph-fallback-synthesis-cancellation.bugfix.md b/changelog.d/+langgraph-fallback-synthesis-cancellation.bugfix.mdnew file mode 100644index 0000000000..4f1bd677d0--- /dev/null+++ b/changelog.d/+langgraph-fallback-synthesis-cancellation.bugfix.md@@ -0,0 +1 @@+Fixed a multi-second lag between clicking **Stop** and a langgraph-agent research actually stopping when the agent stream errors out. `LangGraphAgentStrategy._synthesize_from_collector` now calls `self.check_termination()` before its fallback synthesis LLM call, so a cancellation request aborts the fallback synthesis rather than waiting for the LLM call to complete. The streaming-loop check granularity was already optimal; the only remaining gap was the fallback synthesis path. Port of the fix from #4872.diff --git a/changelog.d/+news-cancellation-lag.bugfix.md b/changelog.d/+news-cancellation-lag.bugfix.mdnew file mode 100644index 0000000000..b482e63c14--- /dev/null+++ b/changelog.d/+news-cancellation-lag.bugfix.md@@ -0,0 +1 @@+Fixed a multi-second lag between clicking **Stop** and a news-aggregation research actually stopping. `NewsAggregationStrategy.analyze_topic` now calls `self.check_termination()` at the start of the work and again after the parallel search loop (before the `analyze_findings` LLM call), so a cancellation request is detected before any search or analysis LLM call is made. Port of the fix from #4872.diff --git a/changelog.d/+source-based-cancellation-lag.bugfix.md b/changelog.d/+source-based-cancellation-lag.bugfix.mdnew file mode 100644index 0000000000..ab6f980d7f--- /dev/null+++ b/changelog.d/+source-based-cancellation-lag.bugfix.md@@ -0,0 +1 @@+Fixed a multi-second lag between clicking **Stop** and a source-based research actually stopping. `SourceBasedSearchStrategy.analyze_topic` now calls `self.check_termination()` immediately after `run_parallel_searches()` returns and again just before the final synthesis LLM call (`analyze_followup`), so a cancellation request is detected within the current iteration rather than only at the next iteration's progress emit. Port of the fix from #4872.diff --git a/changelog.d/+topic-organization-cancellation-lag.bugfix.md b/changelog.d/+topic-organization-cancellation-lag.bugfix.mdnew file mode 100644index 0000000000..a60e82f193--- /dev/null+++ b/changelog.d/+topic-organization-cancellation-lag.bugfix.md@@ -0,0 +1 @@+Fixed a multi-second lag between clicking **Stop** and a topic-organization research actually stopping. `TopicOrganizationStrategy.analyze_topic` now calls `self.check_termination()` before delegating to the source-gathering strategy and at the top of each refinement-loop iteration, so a cancellation request aborts the strategy before any source gathering or refinement-question LLM call. Port of the fix from #4872.diff --git a/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.py b/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.pyindex 1f36199d0f..35a91303c2 100644--- a/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.py+++ b/src/local_deep_research/advanced_search_system/strategies/followup/enhanced_contextual_followup.py@@ -141,6 +141,12 @@ class EnhancedContextualFollowUpStrategy(BaseSearchStrategy):"""logger.info(f"Starting enhanced follow-up search for: {query}")+ # Check cancellation before any LLM or search work — gives the+ # user a chance to bail before we pay for the+ # ``generate_contextualized_query`` call (10-20 s) and the+ # delegate strategy's own research below.+ self.check_termination()+# Update the context with the actual follow-up queryself.full_context["follow_up_query"] = querydiff --git a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.pyindex 941e207107..2d9c228af3 100644--- a/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/langgraph_agent_strategy.py@@ -1250,6 +1250,13 @@ class LangGraphAgentStrategy(BaseSearchStrategy):def _synthesize_from_collector(self, query: str) -> str:"""Fallback synthesis when the agent was cut short."""+ # Check cancellation before any synthesis LLM work. This is the+ # fallback path for when the agent stream errored out; without an+ # early check, a cancel that arrived during the error path would+ # have to wait for the synthesis LLM call to complete before+ # terminating.+ self.check_termination()+results = self.collector.resultsif not results:return "Research could not be completed within the iteration limit."diff --git a/src/local_deep_research/advanced_search_system/strategies/news_strategy.py b/src/local_deep_research/advanced_search_system/strategies/news_strategy.pyindex 54e3e86319..469de8d139 100644--- a/src/local_deep_research/advanced_search_system/strategies/news_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/news_strategy.py@@ -219,6 +219,11 @@ Ensure variety across different categories when possible."""import asyncio+ # Check cancellation before any work — the news strategy runs a+ # single iteration so without an early check a cancel has to wait+ # for question generation + every search + the analysis LLM call.+ self.check_termination()+# Generate news-specific search queriesquestions = self.generate_questions(query, "")self.questions_by_iteration[0] = questions@@ -242,6 +247,11 @@ Ensure variety across different categories when possible.logger.exception("Search error")continue+ # Check cancellation before the analysis LLM call so a cancel that+ # arrives during the search loop doesn't have to wait for the+ # expensive analysis pass to finish before terminating.+ self.check_termination()+# Analyze findings - handle both sync and async contextstry:# Check if we're already in an async contextdiff --git a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.pyindex 2406b2ee02..2b8e37f8c6 100644--- a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py@@ -382,6 +382,14 @@ class SourceBasedSearchStrategy(BaseSearchStrategy):search_question,context_factory=thread_context,)++ # Check cancellation after parallel search — this is the+ # largest single pause in a typical iteration (10-30 s for+ # multi-question searches). Without this, a cancel that+ # arrived during the search would have to wait for the+ # next iteration's loop-entry check before stopping.+ self.check_termination()+iteration_search_dict = {}iteration_search_results = []for _, payload in completed:@@ -452,6 +460,11 @@ class SourceBasedSearchStrategy(BaseSearchStrategy):# report — format_links_to_markdown reads it at report_generator.py:494.self.all_links_of_system.extend(final_filtered_results)+ # Check cancellation before final synthesis so a stop click that+ # arrives right after the last iteration completes doesn't have to+ # wait for the synthesis LLM call (10-20 s) before terminating.+ self.check_termination()+# SYNTHESIS PHASE — run after all iterations complete.# The citation handler receives final_filtered_results and produces# synthesized content with inline citation numbers like [1], [2], etc.diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.pyindex 98dff9afd7..81f2d3fe55 100644--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py@@ -934,6 +934,12 @@ Otherwise, respond with only the follow-up question, nothing else."""logger.info(f"Starting topic organization for: {query}")+ # Check cancellation before delegating to the source-gathering+ # strategy. The delegate has its own checks, but the LLM topic-+ # extraction call below this point can also take 10-30 s, so we+ # want to give the user a chance to bail before we pay that cost.+ self.check_termination()+strategy_name = ("focused iteration"if self.use_focused_iteration@@ -1122,6 +1128,11 @@ Otherwise, respond with only the follow-up question, nothing else.self.enable_refinementand len(self.refinement_questions) < self.max_refinement_iterations):+ # Check cancellation at the top of each refinement iteration+ # so a cancel that arrives mid-refinement halts cleanly before+ # another LLM refinement-question generation call.+ self.check_termination()+refinement_question = self._generate_refinement_question(relevant_topics, query)diff --git a/tests/advanced_search_system/strategies/test_strategy_cancellation_regression.py b/tests/advanced_search_system/strategies/test_strategy_cancellation_regression.pynew file mode 100644index 0000000000..fbc5e2f728--- /dev/null+++ b/tests/advanced_search_system/strategies/test_strategy_cancellation_regression.py@@ -0,0 +1,341 @@+"""Behavioural regression tests for the cancellation-lag fix ported from PR #4872 (#4871)."""++from unittest.mock import MagicMock, patch+import pytest+++def _cancel_on_nth_callback(cancel_on_call):+ from local_deep_research.exceptions import ResearchTerminatedException++ counter = {"n": 0}++ def callback(message, progress_percent, metadata):+ counter["n"] += 1+ if counter["n"] == cancel_on_call:+ raise ResearchTerminatedException("cancelled")++ return callback, counter+++def _make_source_based_strategy():+ from local_deep_research.advanced_search_system.strategies.source_based_strategy import (+ SourceBasedSearchStrategy,+ )++ return SourceBasedSearchStrategy(+ model=MagicMock(), search=MagicMock(), all_links_of_system=[]+ )+++class TestSourceBasedCancellation:+ def test_cancel_before_first_iteration_runs_no_llm(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_source_based_strategy()+ callback, _c = _cancel_on_nth_callback(cancel_on_call=1)+ strategy.set_progress_callback(callback)+ gen_mock = MagicMock(return_value=["Q1"])+ with patch.object(+ strategy.question_generator, "generate_questions", gen_mock+ ):+ with patch.object(+ strategy.citation_handler,+ "analyze_followup",+ return_value={"content": "Done", "documents": []},+ ):+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("test query")+ assert gen_mock.call_count == 0++ def test_cancel_after_parallel_search_stops_next_iteration(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_source_based_strategy()+ # Cancel on the FIRST iteration loop entry; this is exactly the new+ # check_termination inserted after the parallel search, before the+ # next iteration starts.+ term_counter = {"n": 0}++ def callback(message, progress_percent, metadata):+ if metadata.get("phase") == "termination_check":+ term_counter["n"] += 1+ if term_counter["n"] == 2:+ # 2nd termination_check = post-search check on iteration 1+ raise ResearchTerminatedException("cancelled")++ strategy.set_progress_callback(callback)+ gen_calls = {"n": 0}++ def gen_questions(**kwargs):+ gen_calls["n"] += 1+ return ["Q1"]++ strategy.search.run = MagicMock(return_value=[])+ with (+ patch.object(+ strategy.question_generator,+ "generate_questions",+ side_effect=gen_questions,+ ),+ patch(+ "local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",+ return_value=[],+ ),+ patch.object(+ strategy.citation_handler,+ "analyze_followup",+ return_value={"content": "Done", "documents": []},+ ),+ ):+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("test query")+ # gen_questions WAS called for iteration 1, but cancelled before+ # iteration 2 — so it should have been called exactly once, not twice.+ assert gen_calls["n"] == 1+ assert term_counter["n"] == 2++ def test_cancel_before_synthesis_aborts_llm_call(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_source_based_strategy()+ call_state = {"saw_pre_synthesis_check": False}++ def callback(message, progress_percent, metadata):+ if metadata.get("phase") == "termination_check":+ if message and "Synthesizing" not in str(message):+ call_state["saw_pre_synthesis_check"] = True+ raise ResearchTerminatedException("cancelled")++ strategy.set_progress_callback(callback)+ strategy.search.run = MagicMock(return_value=[])+ synth_mock = MagicMock(+ return_value={"content": "Done", "documents": []}+ )+ with (+ patch.object(+ strategy.question_generator,+ "generate_questions",+ return_value=["Q1"],+ ),+ patch(+ "local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",+ return_value=[],+ ),+ patch.object(+ strategy.citation_handler, "analyze_followup", synth_mock+ ),+ ):+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("test query")+ assert call_state["saw_pre_synthesis_check"] is True+ assert synth_mock.call_count == 0+++# NewsAggregationStrategy+def _make_news_strategy():+ from local_deep_research.advanced_search_system.strategies.news_strategy import (+ NewsAggregationStrategy,+ )++ return NewsAggregationStrategy(model=MagicMock(), search=MagicMock())+++class TestNewsStrategyCancellation:+ def test_cancel_before_analysis_llm_stops_synthesis(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_news_strategy()+ callback, _c = _cancel_on_nth_callback(cancel_on_call=2)+ strategy.set_progress_callback(callback)+ with patch.object(+ strategy.question_generator,+ "generate_questions",+ return_value=["Q1"],+ ):+ strategy.search.run = MagicMock(+ return_value=[+ {"title": "T", "snippet": "S", "link": "http://x"}+ ]+ )+ model_invoke = MagicMock(return_value=MagicMock(content="{}"))+ strategy.model.invoke = model_invoke+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("news query")+ assert model_invoke.call_count == 0++ def test_cancel_before_work_runs_no_search(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_news_strategy()+ callback, _c = _cancel_on_nth_callback(cancel_on_call=1)+ strategy.set_progress_callback(callback)+ gen_mock = MagicMock(return_value=["Q1"])+ with patch.object(+ strategy.question_generator, "generate_questions", gen_mock+ ):+ search_mock = MagicMock(return_value=[])+ strategy.search.run = search_mock+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("news query")+ assert search_mock.call_count == 0+++# TopicOrganizationStrategy+def _make_topic_org_strategy():+ from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (+ TopicOrganizationStrategy,+ )++ return TopicOrganizationStrategy(+ search=MagicMock(), model=MagicMock(), all_links_of_system=[]+ )+++class TestTopicOrganizationCancellation:+ def test_cancel_before_delegation_runs_no_source_strategy(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_topic_org_strategy()+ callback, _c = _cancel_on_nth_callback(cancel_on_call=1)+ strategy.set_progress_callback(callback)+ delegate_mock = MagicMock(+ return_value={+ "all_links_of_system": [],+ "iterations": 0,+ "questions_by_iteration": {},+ }+ )+ strategy.source_strategy = MagicMock()+ strategy.source_strategy.analyze_topic = delegate_mock+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("topic query")+ assert delegate_mock.call_count == 0++ def test_cancel_in_refinement_loop_stops_next_iteration(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_topic_org_strategy()+ strategy.enable_refinement = True+ strategy.max_refinement_iterations = 5+ strategy.refinement_questions = ["first question"]+ strategy.all_links_of_system = []+ strategy.iteration_history = []+ strategy.topic_graph = MagicMock()+ strategy.source_strategy = MagicMock()+ strategy.source_strategy.analyze_topic = MagicMock(+ return_value={+ "all_links_of_system": [],+ "iterations": 0,+ "questions_by_iteration": {},+ }+ )+ topic_mock = MagicMock()+ topic_mock.title = "Stub topic"+ topic_mock.get_all_sources = MagicMock(return_value=[])+ refinement_gen_mock = MagicMock(+ side_effect=["second question", "third question"]+ )+ callback, _c = _cancel_on_nth_callback(cancel_on_call=2)+ strategy.set_progress_callback(callback)+ with (+ patch.object(+ strategy,+ "_extract_topics_from_sources",+ return_value=[topic_mock],+ ),+ patch.object(+ strategy, "_generate_refinement_question", refinement_gen_mock+ ),+ patch.object(strategy, "_find_topic_relationships"),+ ):+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("topic query")+ assert refinement_gen_mock.call_count <= 1+++# EnhancedContextualFollowupStrategy+def _make_followup_strategy():+ from local_deep_research.advanced_search_system.strategies.followup.enhanced_contextual_followup import (+ EnhancedContextualFollowUpStrategy,+ )++ delegate = MagicMock()+ delegate.analyze_topic = MagicMock(+ return_value={+ "findings": [],+ "iterations": 0,+ "questions_by_iteration": {},+ "formatted_findings": "",+ "current_knowledge": "",+ "all_links_of_system": [],+ "error": None,+ }+ )+ return EnhancedContextualFollowUpStrategy(+ model=MagicMock(),+ search=MagicMock(),+ delegate_strategy=delegate,+ all_links_of_system=[],+ )+++class TestEnhancedContextualFollowupCancellation:+ def test_cancel_before_contextualized_query_runs_no_llm(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_followup_strategy()+ callback, _c = _cancel_on_nth_callback(cancel_on_call=1)+ strategy.set_progress_callback(callback)+ contextualized_query_mock = MagicMock(return_value="rewritten query")+ strategy.question_generator.generate_contextualized_query = (+ contextualized_query_mock+ )+ with pytest.raises(ResearchTerminatedException):+ strategy.analyze_topic("follow-up query")+ assert contextualized_query_mock.call_count == 0+ assert strategy.delegate_strategy.analyze_topic.call_count == 0+++# LangGraphAgentStrategy fallback synthesis+def _make_langgraph_strategy():+ from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (+ LangGraphAgentStrategy,+ )++ return LangGraphAgentStrategy(+ model=MagicMock(), search=MagicMock(), all_links_of_system=[]+ )+++class TestLangGraphFallbackSynthesisCancellation:+ def test_cancel_before_fallback_synthesis_aborts_llm_call(self):+ from local_deep_research.exceptions import ResearchTerminatedException++ strategy = _make_langgraph_strategy()+ # collector.add_results (its public API) populates the internal+ # _results list that the read-only .results property exposes.+ strategy.collector.add_results(+ [{"title": "T", "snippet": "S", "link": "http://x"}],+ engine_name="web",+ )+ # Cancel on the very first termination_check (i.e. the one inserted+ # at the top of _synthesize_from_collector).+ cancel_fired = {"n": False}++ def callback(message, progress_percent, metadata):+ if (+ metadata.get("phase") == "termination_check"+ and not cancel_fired["n"]+ ):+ cancel_fired["n"] = True+ raise ResearchTerminatedException("cancelled")++ strategy.set_progress_callback(callback)+ model_invoke = MagicMock(+ return_value=MagicMock(content="synthesized answer")+ )+ strategy.model.invoke = model_invoke+ with pytest.raises(ResearchTerminatedException):+ strategy._synthesize_from_collector("test query")+ assert cancel_fired["n"] is True+ assert model_invoke.call_count == 0diff --git a/tests/strategies/test_cancellation_checks.py b/tests/strategies/test_cancellation_checks.pyindex 8bc39126a9..089385195d 100644--- a/tests/strategies/test_cancellation_checks.py+++ b/tests/strategies/test_cancellation_checks.py@@ -97,6 +97,73 @@ class TestStrategiesCallCheckTermination:source = inspect.getsource(FocusedIterationStrategy.analyze_topic)assert "self.check_termination()" in source+ def test_news_calls_check(self):+ """NewsStrategy.analyze_topic must call check_termination().++ NewsStrategy runs a single iteration with parallel searches and+ then an LLM analysis call. Without an explicit cancellation+ check, a user clicking Stop during the search loop or the+ analysis LLM call had to wait the full length of both before+ the next progress emit re-checked the flag.+ """+ from local_deep_research.advanced_search_system.strategies.news_strategy import (+ NewsAggregationStrategy,+ )++ source = inspect.getsource(NewsAggregationStrategy.analyze_topic)+ assert "self.check_termination()" in source++ def test_topic_organization_calls_check(self):+ """TopicOrganizationStrategy.analyze_topic must call check_termination().++ Topic organization delegates to source/focused for gathering but+ also runs its own LLM topic-extraction call and an optional+ refinement loop. Both are 10-30 s operations that previously+ could not be interrupted mid-flight.+ """+ from local_deep_research.advanced_search_system.strategies.topic_organization_strategy import (+ TopicOrganizationStrategy,+ )++ source = inspect.getsource(TopicOrganizationStrategy.analyze_topic)+ assert "self.check_termination()" in source++ def test_enhanced_contextual_followup_calls_check(self):+ """EnhancedContextualFollowupStrategy.analyze_topic must call check_termination().++ The contextualized-query LLM call (10-20 s) and the delegate+ strategy's research below were previously un-interruptible from+ the strategy's perspective.+ """+ from local_deep_research.advanced_search_system.strategies.followup.enhanced_contextual_followup import (+ EnhancedContextualFollowUpStrategy,+ )++ source = inspect.getsource(+ EnhancedContextualFollowUpStrategy.analyze_topic+ )+ assert "self.check_termination()" in source++ def test_langgraph_calls_check(self):+ """LangGraphAgentStrategy.analyze_topic must call check_termination().++ LangGraph streams agent execution, so the per-chunk check is the+ primary cancellation point. The pre-analysis check ensures that+ if the agent stream is cut short (recursion limit, exception),+ the fallback synthesis LLM call still respects cancellation.+ """+ from local_deep_research.advanced_search_system.strategies.langgraph_agent_strategy import (+ LangGraphAgentStrategy,+ )++ source = inspect.getsource(LangGraphAgentStrategy.analyze_topic)+ assert "self.check_termination()" in source+ # Also verify the fallback synthesis LLM call path is gated.+ synth_source = inspect.getsource(+ LangGraphAgentStrategy._synthesize_from_collector+ )+ assert "self.check_termination()" in synth_source+class TestResearchTerminatedException:"""Verify ResearchTerminatedException class properties."""