Skip to content

~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.

21.5 KB
Raw Blame
"""
High-value pure logic tests for FocusedIterationStrategy.

Tests cover constructor defaults, parameter coercion, handler_type selection,
_create_error_response, and _get_current_knowledge_summary -- all without
LLM or network calls.
"""

from unittest.mock import MagicMock, patch

from local_deep_research.advanced_search_system.strategies.base_strategy import (
    CHECK_CONTEXT_ITERATION_START,
    CHECK_CONTEXT_PRE_SYNTHESIS,
)

import pytest

# ---------------------------------------------------------------------------
# Helper: build a FocusedIterationStrategy with the constructor fully mocked
# so we can exercise individual methods / attributes without real components.
# ---------------------------------------------------------------------------

MODULE = "local_deep_research.advanced_search_system.strategies.focused_iteration_strategy"


def _make_strategy(**overrides):
    """Instantiate FocusedIterationStrategy with all heavy deps patched out."""
    from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
        FocusedIterationStrategy,
    )

    defaults = dict(
        model=MagicMock(),
        search=MagicMock(),
        citation_handler=MagicMock(),
        all_links_of_system=[],
    )
    defaults.update(overrides)

    with (
        patch(f"{MODULE}.BrowseCompQuestionGenerator"),
        patch(f"{MODULE}.ProgressiveExplorer"),
        patch(f"{MODULE}.FindingsRepository"),
    ):
        return FocusedIterationStrategy(**defaults)


# =========================================================================
# Constructor defaults and parameter coercion
# =========================================================================


class TestConstructorDefaults:
    """Verify default attribute values set during __init__."""

    def test_max_iterations_default_is_8(self):
        strategy = _make_strategy()
        assert strategy.max_iterations == 8

    def test_questions_per_iteration_default_is_5(self):
        strategy = _make_strategy()
        assert strategy.questions_per_iteration == 5

    def test_use_browsecomp_optimization_default_true(self):
        strategy = _make_strategy()
        assert strategy.use_browsecomp_optimization is True

    def test_enable_adaptive_questions_default_false(self):
        strategy = _make_strategy()
        assert strategy.enable_adaptive_questions is False

    def test_enable_early_termination_default_false(self):
        strategy = _make_strategy()
        assert strategy.enable_early_termination is False

    def test_knowledge_summary_limit_default(self):
        strategy = _make_strategy()
        assert strategy.knowledge_summary_limit == 10

    def test_knowledge_snippet_truncate_default(self):
        strategy = _make_strategy()
        assert strategy.knowledge_snippet_truncate == 200

    def test_prompt_knowledge_truncate_default(self):
        strategy = _make_strategy()
        assert strategy.prompt_knowledge_truncate == 1500

    def test_previous_searches_limit_default(self):
        strategy = _make_strategy()
        assert strategy.previous_searches_limit == 10


class TestParameterCoercion:
    """Verify int coercion and None-fallback logic for iteration params."""

    def test_max_iterations_coerced_from_float(self):
        strategy = _make_strategy(max_iterations=4.9)
        assert strategy.max_iterations == 4
        assert isinstance(strategy.max_iterations, int)

    def test_max_iterations_coerced_from_string(self):
        strategy = _make_strategy(max_iterations="6")
        assert strategy.max_iterations == 6
        assert isinstance(strategy.max_iterations, int)

    def test_max_iterations_none_falls_back_to_3(self):
        strategy = _make_strategy(max_iterations=None)
        assert strategy.max_iterations == 3

    def test_questions_per_iteration_coerced_from_float(self):
        strategy = _make_strategy(questions_per_iteration=3.7)
        assert strategy.questions_per_iteration == 3
        assert isinstance(strategy.questions_per_iteration, int)

    def test_questions_per_iteration_coerced_from_string(self):
        strategy = _make_strategy(questions_per_iteration="10")
        assert strategy.questions_per_iteration == 10
        assert isinstance(strategy.questions_per_iteration, int)

    def test_questions_per_iteration_none_falls_back_to_3(self):
        strategy = _make_strategy(questions_per_iteration=None)
        assert strategy.questions_per_iteration == 3


# =========================================================================
# handler_type selection logic
# =========================================================================


class TestHandlerTypeSelection:
    """Verify handler_type passed to CitationHandler depends on browsecomp flag."""

    def test_handler_type_forced_answer_when_browsecomp_true(self):
        """When use_browsecomp_optimization=True and no citation_handler provided,
        CitationHandler should be created with handler_type='forced_answer'."""
        from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
            FocusedIterationStrategy,
        )

        with (
            patch(f"{MODULE}.BrowseCompQuestionGenerator"),
            patch(f"{MODULE}.ProgressiveExplorer"),
            patch(f"{MODULE}.FindingsRepository"),
            patch(f"{MODULE}.CitationHandler") as mock_ch_cls,
        ):
            FocusedIterationStrategy(
                model=MagicMock(),
                search=MagicMock(),
                use_browsecomp_optimization=True,
            )
            mock_ch_cls.assert_called_once()
            call_kwargs = mock_ch_cls.call_args
            assert call_kwargs[1]["handler_type"] == "forced_answer" or (
                len(call_kwargs[0]) >= 2
                and call_kwargs[0][1] == "forced_answer"
            )

    def test_handler_type_standard_when_browsecomp_false(self):
        """When use_browsecomp_optimization=False, handler_type should be 'standard'."""
        from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
            FocusedIterationStrategy,
        )

        with (
            patch(f"{MODULE}.FindingsRepository"),
            patch(f"{MODULE}.CitationHandler") as mock_ch_cls,
        ):
            # browsecomp=False triggers the else branch importing StandardQuestionGenerator
            with patch(
                f"{MODULE}.StandardQuestionGenerator",
                create=True,
            ):
                FocusedIterationStrategy(
                    model=MagicMock(),
                    search=MagicMock(),
                    use_browsecomp_optimization=False,
                )
            mock_ch_cls.assert_called_once()
            assert mock_ch_cls.call_args[1]["handler_type"] == "standard"

    def test_provided_citation_handler_skips_creation(self):
        """When citation_handler is explicitly provided, CitationHandler is not created."""
        from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
            FocusedIterationStrategy,
        )

        custom_handler = MagicMock()
        with (
            patch(f"{MODULE}.BrowseCompQuestionGenerator"),
            patch(f"{MODULE}.ProgressiveExplorer"),
            patch(f"{MODULE}.FindingsRepository"),
            patch(f"{MODULE}.CitationHandler") as mock_ch_cls,
        ):
            strategy = FocusedIterationStrategy(
                model=MagicMock(),
                search=MagicMock(),
                citation_handler=custom_handler,
            )
            mock_ch_cls.assert_not_called()
            assert strategy.citation_handler is custom_handler


# =========================================================================
# _create_error_response
# =========================================================================


class TestCreateErrorResponse:
    """Verify the structure returned by _create_error_response."""

    def test_error_response_has_required_keys(self):
        strategy = _make_strategy()
        resp = strategy._create_error_response("something broke")
        # The shared base _create_error_response carries BOTH "questions"
        # and "questions_by_iteration" so a single helper satisfies every
        # consumer (mcp_strategy reads "questions", focused reads
        # "questions_by_iteration"). The focused error response therefore
        # now includes both keys.
        assert set(resp.keys()) == {
            "findings",
            "iterations",
            "questions",
            "questions_by_iteration",
            "formatted_findings",
            "current_knowledge",
            "error",
        }

    def test_error_response_values(self):
        strategy = _make_strategy()
        resp = strategy._create_error_response("timeout")
        assert resp["findings"] == []
        assert resp["iterations"] == 0
        assert resp["questions"] == {}
        assert resp["questions_by_iteration"] == {}
        assert resp["current_knowledge"] == ""
        assert resp["error"] == "timeout"
        assert "timeout" in resp["formatted_findings"]

    def test_error_response_formatted_findings_contains_message(self):
        strategy = _make_strategy()
        resp = strategy._create_error_response("No search engine available")
        assert resp["formatted_findings"] == "Error: No search engine available"


# =========================================================================
# Cancellation regression — see test_cancellation_checks for the static
# source-grep check; this is the behavioural counterpart that actually
# runs analyze_topic and asserts the loop honours a mid-flight cancel.
# =========================================================================


class TestCancellationDuringIterations:
    """Verify analyze_topic stops promptly when cancellation is requested.

    Bug history: FocusedIterationStrategy.analyze_topic() used to never
    call ``self.check_termination()`` inside its main loop. The only
    progress emits during the strategy were at the *start* of each
    iteration, so a user clicking Stop 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. With default settings (8 iterations × ~5 questions + a final
    LLM synthesis) that produced a visible 30-50 second lag between the
    UI confirming cancellation and the research actually stopping.
    """

    def _make_strategy_with_callback(self, cancel_on_call: int):
        """Build a strategy whose callback raises ``ResearchTerminatedException``
        on the Nth invocation (matching the progress_callback wired up in
        ``research_service.run_research_process``).
        """
        from local_deep_research.exceptions import (
            ResearchTerminatedException,
        )

        call_counter = {"n": 0}

        def callback(message, progress_percent, metadata):
            call_counter["n"] += 1
            if call_counter["n"] == cancel_on_call:
                raise ResearchTerminatedException("cancelled")

        return _make_strategy(), callback, call_counter

    @staticmethod
    def _cancel_on_context(target_context, occurrence=1):
        """Cancel at the Nth firing of one named check_termination site.

        Targets the ``context`` slug the call site passes, so the test
        stays pinned to the intended check even when progress emits or
        other checks are added earlier in the flow.
        """
        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
                if fired["n"] == occurrence:
                    raise ResearchTerminatedException("cancelled")

        return callback, fired

    def test_cancel_during_first_iteration_stops_loop(self):
        """When the cancel fires on iteration 1's loop-entry check, no
        iteration of question generation should run."""
        from local_deep_research.exceptions import (
            ResearchTerminatedException,
        )

        strategy = _make_strategy()
        callback, fired = self._cancel_on_context(CHECK_CONTEXT_ITERATION_START)
        strategy.set_progress_callback(callback)

        # Spy on question generation — if cancellation works, this MUST
        # NOT be called because the loop bails before question generation.
        with patch.object(
            strategy.question_generator,
            "generate_questions",
            return_value=["Q1"],
        ) as 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, (
            "check_termination() should fire BEFORE the first question-"
            "generation call so a cancel during the early 'init' "
            "progress emit stops the loop with zero iterations."
        )

    def test_cancel_after_initialization_runs_no_iterations(self):
        """Even if cancel fires a couple of progress emits in (e.g. after
        the 'initializing' milestone), the loop must stop without
        completing any full iteration's question generation."""
        from local_deep_research.exceptions import (
            ResearchTerminatedException,
        )

        # Cancel fires on the 3rd callback call. The first call is the
        # "Initializing" milestone, the second is the first
        # _emit_question_generation_progress emit — cancel there means
        # the very first iteration must not run any question generation.
        strategy, callback, counter = self._make_strategy_with_callback(
            cancel_on_call=3
        )
        strategy.set_progress_callback(callback)

        with patch.object(
            strategy.question_generator,
            "generate_questions",
            return_value=["Q1"],
        ) as 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_between_iterations_stops_loop(self):
        """A cancel that fires after iteration 1 completes must stop the
        loop before iteration 2 starts its question generation.

        Cancelling at the SECOND ``iteration_start`` check (the loop-entry
        check of iteration 2) is the exact expression of "cancel detected
        between iterations": iteration 1 has fully completed, iteration 2
        has done no work yet. Before the fix, ``analyze_topic`` had no
        ``check_termination()`` call in the loop at all, so the test
        would NOT raise — proving the bug.
        """
        from local_deep_research.exceptions import (
            ResearchTerminatedException,
        )

        strategy = _make_strategy()
        callback, fired = self._cancel_on_context(
            "iteration_start", occurrence=2
        )
        strategy.set_progress_callback(callback)

        gen_call_count = {"n": 0}

        def gen_questions(**kwargs):
            gen_call_count["n"] += 1
            return ["Q1"]

        # The strategy does ``all_search_results.extend(iteration_results)``
        # so iteration_results must be iterable. Returning a plain list
        # is enough — we only care about the cancel being honoured.
        class _FakeSearchProgress:
            found_candidates = []
            entity_coverage = {}

        with (
            patch.object(
                strategy.question_generator,
                "generate_questions",
                side_effect=gen_questions,
            ),
            patch.object(
                strategy.explorer,
                "explore",
                return_value=([], _FakeSearchProgress()),
            ),
            patch.object(
                strategy.explorer,
                "suggest_next_searches",
                return_value=[],
            ),
            patch.object(
                strategy.citation_handler,
                "analyze_followup",
                return_value={"content": "Done", "documents": []},
            ),
        ):
            with pytest.raises(ResearchTerminatedException):
                strategy.analyze_topic("test query")

        # Iteration 1 ran its question generation, but iteration 2 must
        # NOT have. Before the fix there was no check_termination() in
        # the loop at all, so this would have been 8 (default
        # max_iterations).
        assert fired["n"] == 2
        assert gen_call_count["n"] == 1, (
            "Cancel between iteration 1 and iteration 2 must stop the "
            "loop before iteration 2's question-generation call. Got "
            f"{gen_call_count['n']} generation calls; expected exactly 1 "
            "(one for iteration 1, zero for iteration 2)."
        )

    def test_cancel_during_synthesis_aborts_before_llm_call(self):
        """A cancel that arrives while the synthesis LLM call would
        otherwise run must abort the strategy before that LLM call."""
        from local_deep_research.exceptions import (
            ResearchTerminatedException,
        )

        strategy = _make_strategy(max_iterations=1)

        # Cancel at the pre-synthesis check so we assert the synthesis
        # LLM call (analyze_followup) is never reached. explore must be
        # mocked: analyze_topic wraps the loop in ``except Exception``,
        # so an unmocked explore blowing up on the MagicMock search
        # would error-return before ever reaching the pre-synthesis
        # check (the old ordinal test cancelled at the loop-entry check
        # and never noticed).
        callback, fired = self._cancel_on_context(CHECK_CONTEXT_PRE_SYNTHESIS)
        strategy.set_progress_callback(callback)

        class _FakeSearchProgress:
            found_candidates = []
            entity_coverage = {}

        with (
            patch.object(
                strategy.question_generator,
                "generate_questions",
                return_value=["Q1"],
            ),
            patch.object(
                strategy.explorer,
                "explore",
                return_value=([], _FakeSearchProgress()),
            ),
            patch.object(
                strategy.citation_handler,
                "analyze_followup",
                return_value={"content": "Done", "documents": []},
            ) as synth_mock,
        ):
            with pytest.raises(ResearchTerminatedException):
                strategy.analyze_topic("test query")

        assert fired["n"] == 1, (
            "Expected the pre_synthesis termination check to fire"
        )
        assert synth_mock.call_count == 0, (
            "Pre-synthesis check_termination() must abort before the "
            "synthesis LLM call (analyze_followup) is invoked"
        )


# =========================================================================
# _get_current_knowledge_summary
# =========================================================================


class TestGetCurrentKnowledgeSummary:
    """Verify knowledge summary building logic."""

    def test_empty_results_returns_empty_string(self):
        strategy = _make_strategy()
        strategy.all_search_results = []
        assert strategy._get_current_knowledge_summary() == ""

    def test_summary_includes_title_and_truncated_snippet(self):
        strategy = _make_strategy()
        strategy.all_search_results = [
            {"title": "Result A", "snippet": "x" * 300},
        ]
        summary = strategy._get_current_knowledge_summary()
        assert "Result A" in summary
        # Snippet should be truncated to 200 chars + "..."
        assert len(summary.split(": ", 1)[1]) == 200 + 3  # 200 chars + "..."

    def test_summary_respects_knowledge_summary_limit(self):
        strategy = _make_strategy()
        strategy.knowledge_summary_limit = 2
        strategy.all_search_results = [
            {"title": f"T{idx}", "snippet": f"S{idx}"} for idx in range(5)
        ]
        summary = strategy._get_current_knowledge_summary()
        # Only first 2 results should appear
        assert "T0" in summary
        assert "T1" in summary
        assert "T2" not in summary

    def test_summary_no_truncation_when_snippet_truncate_is_none(self):
        strategy = _make_strategy(knowledge_snippet_truncate=None)
        long_snippet = "y" * 500
        strategy.all_search_results = [
            {"title": "Full", "snippet": long_snippet},
        ]
        summary = strategy._get_current_knowledge_summary()
        # Full snippet should be present without trailing "..."
        assert long_snippet in summary
        assert not summary.endswith("......")

    def test_summary_skips_entries_without_title_or_snippet(self):
        strategy = _make_strategy()
        strategy.all_search_results = [
            {"title": "", "snippet": ""},
            {"title": "Has Title", "snippet": "Has Snippet"},
        ]
        summary = strategy._get_current_knowledge_summary()
        lines = [line for line in summary.strip().split("\n") if line]
        assert len(lines) == 1
        assert "Has Title" in lines[0]