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.

8.6 KB
Raw Blame
"""Regression tests for the composed follow-up + report generation path.

Reproduces the failure mode reported on PR #6517: ``EnhancedContextualFollowUpStrategy``
wraps a delegate (``SourceBasedSearchStrategy``) and the report generator's
per-subsection ``max_iterations`` override was only being applied to the
wrapper, so the delegate still ran its full configured iteration loop for
each subsection. The composed test below uses the real wrapper, the real
delegate, and the real report section generator to pin the override end-to-end.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.advanced_search_system.strategies.followup.enhanced_contextual_followup import (
    EnhancedContextualFollowUpStrategy,
)
from local_deep_research.advanced_search_system.strategies.source_based_strategy import (
    SourceBasedSearchStrategy,
)
from local_deep_research.report_generator import IntegratedReportGenerator
from local_deep_research.search_system import AdvancedSearchSystem
from local_deep_research.utilities.type_utils import unwrap_setting


def _build_followup_system(max_iterations=4):
    """Build a real AdvancedSearchSystem wrapping a real
    EnhancedContextualFollowUpStrategy around a real
    SourceBasedSearchStrategy delegate.

    LLM and search engine are mocked to keep the test offline.
    """
    mock_model = MagicMock()
    mock_search = MagicMock()
    mock_search.run.return_value = []
    mock_search.include_full_content = True

    snapshot = {
        "search.iterations": {"value": max_iterations},
        "search.questions_per_iteration": {"value": 2},
    }
    system = AdvancedSearchSystem(
        llm=mock_model,
        search=mock_search,
        strategy_name="enhanced-contextual-followup",
        settings_snapshot=snapshot,
        max_iterations=max_iterations,
        questions_per_iteration=2,
        research_context={"delegate_strategy": "source-based"},
    )
    return system, mock_search


class TestFollowupReportComposedPath:
    def test_delegate_max_iterations_overridden_during_subsection(self):
        """During a report subsection search, the wrapped delegate must run
        with ``max_iterations=1`` even though both the wrapper and the
        delegate were constructed with ``max_iterations=4``.

        This is the exact composition the reviewer reproduced: a real
        AdvancedSearchSystem with enhanced-contextual-followup feeding the
        real IntegratedReportGenerator, with only LLM/search responses mocked.
        """
        system, _ = _build_followup_system(max_iterations=4)
        assert isinstance(system.strategy, EnhancedContextualFollowUpStrategy)
        delegate = system.strategy.delegate_strategy
        assert isinstance(delegate, SourceBasedSearchStrategy)
        assert delegate.max_iterations == 4

        # Wire the report generator around the real system. The LLM only
        # needs to answer the section-research call; report assembly stays
        # in the existing pipeline. Stub ``analyze_topic`` to capture the
        # delegate's iteration count at the moment the call lands.
        generator = IntegratedReportGenerator.__new__(IntegratedReportGenerator)
        generator.search_system = system
        generator.model = MagicMock()
        generator.searches_per_section = 2
        generator.max_context_sections = 3
        generator.max_context_chars = 4000
        generator._owns_llm = False

        captured_max = {}

        def fake_delegate_analyze(query):
            # While the report subsection is being processed, the override
            # must already have taken effect on the DELEGATE (where the
            # actual search loop runs), not just on the wrapper.
            captured_max["delegate"] = delegate.max_iterations
            captured_max["wrapper"] = system.strategy.max_iterations
            captured_max["snapshot_iter"] = (
                system.strategy.settings_snapshot.get("search.iterations")
            )
            return {
                "current_knowledge": "ok",
                "questions_by_iteration": {},
            }

        with patch.object(
            delegate, "analyze_topic", side_effect=fake_delegate_analyze
        ):
            structure = [
                {
                    "name": "S",
                    "subsections": [{"name": "Sub", "purpose": "p"}],
                }
            ]
            generator._research_and_generate_sections(
                {"current_knowledge": "init"}, structure, "q"
            )

        assert captured_max["delegate"] == 1, (
            "delegate.max_iterations must be overridden to 1 during the "
            "subsection search; otherwise the per-subsection cap never "
            "reaches the worker and each subsection runs the full configured "
            "loop"
        )
        assert captured_max["wrapper"] == 1
        assert captured_max["snapshot_iter"] == 1

    def test_delegate_max_iterations_restored_after_subsection(self):
        """After the report subsection loop, the delegate must have its
        original ``max_iterations`` restored, so subsequent calls outside
        the report context see the configured value again.

        Also captures the during-call state to prove the override was
        actually applied — otherwise this assertion would vacuously pass
        with a no-op fix that never overrides the delegate.
        """
        system, _ = _build_followup_system(max_iterations=4)
        delegate = system.strategy.delegate_strategy

        generator = IntegratedReportGenerator.__new__(IntegratedReportGenerator)
        generator.search_system = system
        generator.model = MagicMock()
        generator.searches_per_section = 2
        generator.max_context_sections = 3
        generator.max_context_chars = 4000
        generator._owns_llm = False

        captured = {}

        def fake_delegate_analyze(query):
            captured["during"] = delegate.max_iterations
            return {
                "current_knowledge": "ok",
                "questions_by_iteration": {},
            }

        with patch.object(
            delegate, "analyze_topic", side_effect=fake_delegate_analyze
        ):
            structure = [
                {
                    "name": "S",
                    "subsections": [{"name": "Sub", "purpose": "p"}],
                }
            ]
            generator._research_and_generate_sections(
                {"current_knowledge": "init"}, structure, "q"
            )

        # The override must actually have applied during the call.
        assert captured["during"] == 1
        # …and must be back to the configured value after.
        assert delegate.max_iterations == 4
        assert (
            unwrap_setting(
                system.strategy.settings_snapshot["search.iterations"]
            )
            == 4
        )

    def test_delegate_max_iterations_restored_on_exception(self):
        """If the delegate raises mid-subsection, the delegate
        ``max_iterations`` and the snapshot must still be restored.

        Also captures the during-call state to prove the override was
        applied before the exception was raised.
        """
        system, _ = _build_followup_system(max_iterations=4)
        delegate = system.strategy.delegate_strategy

        generator = IntegratedReportGenerator.__new__(IntegratedReportGenerator)
        generator.search_system = system
        generator.model = MagicMock()
        generator.searches_per_section = 2
        generator.max_context_sections = 3
        generator.max_context_chars = 4000
        generator._owns_llm = False

        captured = {}

        def fake_delegate_analyze(query):
            captured["during"] = delegate.max_iterations
            raise RuntimeError("boom")

        with patch.object(
            delegate, "analyze_topic", side_effect=fake_delegate_analyze
        ):
            structure = [
                {
                    "name": "S",
                    "subsections": [{"name": "Sub", "purpose": "p"}],
                }
            ]
            with pytest.raises(RuntimeError, match="boom"):
                generator._research_and_generate_sections(
                    {"current_knowledge": "init"},
                    structure,
                    "q",
                )

        # The override must actually have applied before the raise.
        assert captured["during"] == 1
        # …and must still be restored to the configured value after.
        assert delegate.max_iterations == 4
        assert (
            unwrap_setting(
                system.strategy.settings_snapshot["search.iterations"]
            )
            == 4
        )