~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
"""Tests for strategy cancellation checks (PRs #4872, #5092).
Verifies:
- BaseSearchStrategy.check_termination() calls progress_callback
- check_termination() is called in each strategy's main loop
- ResearchTerminatedException inherits from BaseException (not Exception)
so that ``except Exception`` blocks naturally let it propagate
"""
import inspect
from unittest.mock import MagicMock
import pytest
from local_deep_research.advanced_search_system.strategies.base_strategy import (
BaseSearchStrategy,
CHECK_CONTEXT_PRE_SYNTHESIS,
)
from local_deep_research.exceptions import ResearchTerminatedException
class TestCheckTerminationMethod:
"""Verify check_termination() on BaseSearchStrategy."""
def test_calls_progress_callback(self):
"""check_termination should call progress_callback with termination_check phase."""
class ConcreteStrategy(BaseSearchStrategy):
def analyze_topic(self, query):
pass
strategy = ConcreteStrategy.__new__(ConcreteStrategy)
strategy.progress_callback = MagicMock()
strategy.check_termination()
strategy.progress_callback.assert_called_once()
args = strategy.progress_callback.call_args
assert args[0][2]["phase"] == "termination_check"
assert "context" not in args[0][2]
def test_context_included_in_metadata(self):
"""A context slug names the call site in the callback metadata.
Call sites pass e.g. ``check_termination("pre_synthesis")`` so
tests can cancel at one specific check instead of counting
callback invocations.
"""
class ConcreteStrategy(BaseSearchStrategy):
def analyze_topic(self, query):
pass
strategy = ConcreteStrategy.__new__(ConcreteStrategy)
strategy.progress_callback = MagicMock()
strategy.check_termination(CHECK_CONTEXT_PRE_SYNTHESIS)
strategy.progress_callback.assert_called_once()
args = strategy.progress_callback.call_args
assert args[0][2]["phase"] == "termination_check"
assert args[0][2]["context"] == CHECK_CONTEXT_PRE_SYNTHESIS
def test_no_callback_no_error(self):
"""check_termination should not error if no callback set."""
class ConcreteStrategy(BaseSearchStrategy):
def analyze_topic(self, query):
pass
strategy = ConcreteStrategy.__new__(ConcreteStrategy)
strategy.progress_callback = None
# Should not raise
strategy.check_termination()
def test_propagates_callback_exception(self):
"""If callback raises ResearchTerminatedException, it should propagate."""
class ConcreteStrategy(BaseSearchStrategy):
def analyze_topic(self, query):
pass
strategy = ConcreteStrategy.__new__(ConcreteStrategy)
strategy.progress_callback = MagicMock(
side_effect=ResearchTerminatedException("Research terminated")
)
with pytest.raises(
ResearchTerminatedException, match="Research terminated"
):
strategy.check_termination()
class TestStrategiesCallCheckTermination:
"""Verify each strategy calls check_termination() in its main loop."""
def test_source_based_calls_check(self):
from local_deep_research.advanced_search_system.strategies.source_based_strategy import (
SourceBasedSearchStrategy,
)
source = inspect.getsource(SourceBasedSearchStrategy.analyze_topic)
assert "self.check_termination(" in source
def test_focused_iteration_calls_check(self):
"""FocusedIterationStrategy.analyze_topic must call check_termination().
Regression test for the bug where cancelling a focused-iteration
research would take 30-50 seconds to actually stop because the
strategy's main loop never re-checked the termination flag — it
only fired progress emits at the START of each iteration, so a
cancel clicked mid-iteration had to wait for the in-flight LLM
question-generation call and parallel search to finish before
the next iteration's progress emit re-checked the flag.
"""
from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
FocusedIterationStrategy,
)
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."""
def test_is_base_exception_subclass(self):
"""ResearchTerminatedException should be a subclass of BaseException, not Exception."""
assert issubclass(ResearchTerminatedException, BaseException)
assert not issubclass(ResearchTerminatedException, Exception)
def test_distinguishable_from_generic(self):
"""isinstance should distinguish ResearchTerminatedException from generic Exception."""
termination_exc = ResearchTerminatedException("cancelled")
generic_exc = Exception("some error")
assert isinstance(termination_exc, ResearchTerminatedException)
assert isinstance(termination_exc, BaseException)
assert not isinstance(termination_exc, Exception)
assert not isinstance(generic_exc, ResearchTerminatedException)
def test_except_exception_does_not_catch(self):
"""The core guarantee: except Exception blocks can't swallow termination."""
with pytest.raises(ResearchTerminatedException):
try:
raise ResearchTerminatedException("cancelled")
except Exception:
pass # Must NOT catch it
class TestTerminationCheckPhaseSkipsLogging:
"""Verify that termination_check phase is handled silently in research_service."""
def test_research_service_handles_termination_check_phase(self):
"""research_service progress_callback should early-return for termination_check phase."""
from local_deep_research.web.services import research_service
source = inspect.getsource(research_service)
# Verify the early return for termination_check phase exists
assert 'metadata.get("phase") == "termination_check"' in source