~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
"""Behavioural regression tests for the cancellation-lag fix ported from PR #4872 (#4871).
Each test cancels at one specific ``check_termination`` call site by
targeting the ``context`` slug that call passes (e.g. ``"pre_synthesis"``),
so the tests stay valid when unrelated progress emits or additional checks
are added earlier in a strategy's flow.
"""
from unittest.mock import MagicMock, patch
from local_deep_research.advanced_search_system.strategies.base_strategy import (
CHECK_CONTEXT_ENTRY,
CHECK_CONTEXT_FALLBACK_SYNTHESIS,
CHECK_CONTEXT_ITERATION_START,
CHECK_CONTEXT_POST_SEARCH,
CHECK_CONTEXT_PRE_ANALYSIS,
CHECK_CONTEXT_PRE_SYNTHESIS,
CHECK_CONTEXT_REFINEMENT_LOOP,
)
import pytest
def _cancel_on_context(target_context):
"""Build a progress callback that cancels at one named check site.
Only a ``termination_check`` callback whose ``context`` matches
``target_context`` raises; every other progress emission is ignored.
``fired["n"]`` counts how often the targeted check actually fired so
tests can assert the site was reached (and not, say, skipped entirely).
"""
from local_deep_research.exceptions import ResearchTerminatedException
fired = {"n": 0}
def callback(message, progress_percent, metadata):
if (
metadata.get("phase") == "termination_check"
and metadata.get("context") == target_context
):
fired["n"] += 1
raise ResearchTerminatedException("cancelled")
return callback, fired
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, fired = _cancel_on_context(CHECK_CONTEXT_ITERATION_START)
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 fired["n"] == 1
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 at the post-search check of iteration 1 — the check
# inserted after run_parallel_searches, before the next iteration.
callback, fired = _cancel_on_context(CHECK_CONTEXT_POST_SEARCH)
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 fired["n"] == 1
assert gen_calls["n"] == 1
def test_cancel_before_synthesis_aborts_llm_call(self):
from local_deep_research.exceptions import ResearchTerminatedException
strategy = _make_source_based_strategy()
callback, fired = _cancel_on_context(CHECK_CONTEXT_PRE_SYNTHESIS)
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 fired["n"] == 1
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, fired = _cancel_on_context(CHECK_CONTEXT_PRE_ANALYSIS)
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 fired["n"] == 1
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, fired = _cancel_on_context(CHECK_CONTEXT_ENTRY)
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 fired["n"] == 1
assert gen_mock.call_count == 0
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, fired = _cancel_on_context(CHECK_CONTEXT_ENTRY)
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 fired["n"] == 1
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()
# The delegate must return at least one source: analyze_topic
# early-returns before the refinement loop when all_links_of_system
# is empty, and the loop is where the targeted check lives.
strategy.source_strategy.analyze_topic = MagicMock(
return_value={
"all_links_of_system": [
{"title": "T", "snippet": "S", "link": "http://x"}
],
"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, fired = _cancel_on_context(CHECK_CONTEXT_REFINEMENT_LOOP)
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")
# Cancelled at the top of the first refinement iteration, before
# any refinement-question LLM call.
assert fired["n"] == 1
assert refinement_gen_mock.call_count == 0
def test_progress_callback_propagates_to_delegate(self):
"""Pins the set_progress_callback override: without propagation the
delegate's check_termination() calls are silent no-ops and a Stop
during source gathering is not detected until the delegate ends."""
strategy = _make_topic_org_strategy()
received = []
def callback(message, progress_percent, metadata):
received.append((message, progress_percent, metadata))
strategy.set_progress_callback(callback)
# The delegate receives a filtered callback, not the raw one.
delegate_cb = strategy.source_strategy.progress_callback
assert delegate_cb is not callback
assert callable(delegate_cb)
# Termination checks propagate through the filter.
delegate_cb("Checking", None, {"phase": "termination_check"})
assert len(received) == 1
assert received[0][2]["phase"] == "termination_check"
# Ordinary progress events are suppressed. (The delegate's step
# events -- search_planning, search, observation -- are the ones
# that are forwarded, with no percentage; see TestDelegateProgress
# in test_topic_organization_strategy.py.)
delegate_cb("Initializing", 5, {"phase": "init"})
delegate_cb("Synthesizing", 90, {"phase": "synthesis"})
assert len(received) == 1 # still only the termination check
def test_cancel_during_delegate_gathering_stops_delegate(self):
"""End-to-end: with the callback propagated, the delegate's own
iteration_start check fires — the delegate stops before generating
any questions. The outer strategy never emits iteration_start, so
this cancels inside the delegate or not at all."""
from local_deep_research.exceptions import ResearchTerminatedException
strategy = _make_topic_org_strategy()
callback, fired = _cancel_on_context(CHECK_CONTEXT_ITERATION_START)
strategy.set_progress_callback(callback)
delegate_gen_mock = MagicMock(return_value=["Q1"])
with (
patch.object(
strategy.source_strategy.question_generator,
"generate_questions",
delegate_gen_mock,
),
patch(
"local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",
return_value=[],
),
):
with pytest.raises(ResearchTerminatedException):
strategy.analyze_topic("topic query")
assert fired["n"] == 1
assert delegate_gen_mock.call_count == 0
def test_cancel_during_refinement_gathering_stops_refinement_delegate(
self,
):
"""Pins the callback propagation to the per-iteration refinement
strategy: the refinement delegate's own iteration_start check
fires, so it stops before its question-generation LLM call. The
main delegate is a mock here, so the refinement delegate is the
only possible source of an iteration_start check."""
from local_deep_research.exceptions import ResearchTerminatedException
strategy = _make_topic_org_strategy()
strategy.enable_refinement = True
strategy.max_refinement_iterations = 2
strategy.refinement_questions = []
strategy.topic_graph = MagicMock()
strategy.source_strategy = MagicMock()
strategy.source_strategy.analyze_topic = MagicMock(
return_value={
"all_links_of_system": [
{"title": "T", "snippet": "S", "link": "http://x"}
],
"iterations": 1,
"questions_by_iteration": {},
}
)
callback, fired = _cancel_on_context(CHECK_CONTEXT_ITERATION_START)
strategy.set_progress_callback(callback)
topic_mock = MagicMock()
topic_mock.title = "Stub topic"
topic_mock.get_all_sources = MagicMock(
return_value=[{"link": "http://x"}]
)
# Finite side_effect ending in None so the loop can never spin
# forever, whatever happens to the checks under test.
refinement_gen_mock = MagicMock(side_effect=["another question", None])
# The refinement strategy is constructed inside the loop, so its
# question generator can only be intercepted at the class level.
refinement_question_gen = MagicMock(return_value=["Q1"])
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"),
patch(
"local_deep_research.advanced_search_system.questions.standard_question.StandardQuestionGenerator.generate_questions",
refinement_question_gen,
),
patch(
"local_deep_research.advanced_search_system.strategies.source_based_strategy.run_parallel_searches",
return_value=[],
),
):
with pytest.raises(ResearchTerminatedException):
strategy.analyze_topic("topic query")
assert fired["n"] == 1
# A refinement question was generated (the loop genuinely started) …
assert refinement_gen_mock.call_count == 1
# … and the refinement delegate stopped at its own loop-entry
# check, before its question-generation LLM call.
assert refinement_question_gen.call_count == 0
# 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, fired = _cancel_on_context(CHECK_CONTEXT_ENTRY)
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 fired["n"] == 1
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",
)
callback, fired = _cancel_on_context(CHECK_CONTEXT_FALLBACK_SYNTHESIS)
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 fired["n"] == 1
assert model_invoke.call_count == 0