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.

34.6 KB
Raw Blame
"""Contract tests for the search-strategy layer (``advanced_search_system``).

Scope: strategy dispatch, cancellation checkpoints, iteration/budget
bounds, the findings repository, and question decomposition.

Every test here drives the production objects directly with both
boundaries stubbed: ``model.invoke`` (LLM) and ``search.run`` /
``explorer.explore`` (search).  No network, no database, no app boot.

Three tests are ``xfail(strict=True)`` — each pins a *confirmed* defect
found while writing this file and will start failing (and so must be
un-marked) the moment the defect is fixed:

1. ``FindingsRepository.synthesize_findings`` cannot run off the main
   thread — its SIGALRM timeout raises ``ValueError`` which its own
   ``except Exception`` swallows, so the LLM is never called.
2. ``EnhancedContextualFollowUpStrategy._filter_relevant_sources``
   assumes the ``search.max_followup_sources`` setting is dict-wrapped
   and raises ``AttributeError`` on a raw-scalar settings snapshot.
3. A non-string ``research_context["delegate_strategy"]`` crashes
   dispatch with ``AttributeError`` instead of degrading.
"""

import ast
import pathlib
import threading
from unittest.mock import MagicMock

import pytest

from local_deep_research.advanced_search_system.findings.repository import (
    FindingsRepository,
)
from local_deep_research.advanced_search_system.questions.decomposition_question import (
    DecompositionQuestionGenerator,
)
from local_deep_research.advanced_search_system.questions.standard_question import (
    StandardQuestionGenerator,
)
from local_deep_research.advanced_search_system.strategies.base_strategy import (
    CHECK_CONTEXT_ITERATION_START,
    CHECK_CONTEXT_POST_SEARCH,
    CHECK_CONTEXT_PRE_SYNTHESIS,
)
from local_deep_research.advanced_search_system.strategies.focused_iteration_strategy import (
    FocusedIterationStrategy,
)
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.exceptions import ResearchTerminatedException
from local_deep_research.search_system import AdvancedSearchSystem
from local_deep_research.search_system_factory import (
    AVAILABLE_STRATEGIES,
    create_strategy,
)


# ---------------------------------------------------------------------------
# helpers
# ---------------------------------------------------------------------------


class ScriptedLLM:
    """Minimal LLM stub: returns a fixed body for every ``invoke``.

    Records every prompt so tests can assert on what the layer hands to
    the model instead of on a mock's call signature alone.
    """

    def __init__(self, body: str):
        self.body = body
        self.prompts: list[str] = []

    def invoke(self, prompt, *args, **kwargs):
        self.prompts.append(str(prompt))
        message = MagicMock()
        message.content = self.body
        return message

    @property
    def calls(self) -> int:
        return len(self.prompts)


def _counting_search(result_count: int = 1):
    """Search stub that records each query it is asked to run."""
    search = MagicMock()
    queries: list[str] = []

    def run(query, **kwargs):
        queries.append(query)
        return [
            {
                "title": f"t{i}",
                "link": f"https://example.invalid/{i}",
                "snippet": "s",
            }
            for i in range(result_count)
        ]

    search.run = MagicMock(side_effect=run)
    search.queries = queries
    return search


def _cancel_at(target_context):
    """Progress callback that cancels at exactly one check-site slug.

    Returns ``(callback, seen)`` where ``seen`` maps every observed
    termination-check context to its firing count, so a test can prove
    the site it targeted was actually reached.
    """
    seen: dict[str, int] = {}

    def callback(message, progress_percent, metadata):
        metadata = metadata or {}
        if metadata.get("phase") != "termination_check":
            return
        context = metadata.get("context")
        seen[context] = seen.get(context, 0) + 1
        if context == target_context:
            raise ResearchTerminatedException("cancelled")

    return callback, seen


def _recording_callback():
    """Non-cancelling control callback (positive-control counterpart)."""
    seen: dict[str, int] = {}

    def callback(message, progress_percent, metadata):
        metadata = metadata or {}
        if metadata.get("phase") == "termination_check":
            context = metadata.get("context")
            seen[context] = seen.get(context, 0) + 1

    return callback, seen


HOSTILE_QUESTION_FLOOD = "\n".join(
    f"Q: flood question number {i}" for i in range(5000)
)


def _make_source_based(iterations: int, questions_per_iteration: int, llm):
    strategy = SourceBasedSearchStrategy(
        model=llm,
        search=_counting_search(),
        use_cross_engine_filter=False,
        all_links_of_system=[],
        settings_snapshot={
            "search.iterations": iterations,
            "search.questions_per_iteration": questions_per_iteration,
        },
    )
    # Synthesis is a separate boundary (citation handler -> LLM); stub it
    # so these tests measure the iteration loop only.
    strategy.citation_handler.analyze_followup = MagicMock(
        return_value={"content": "synthesised", "documents": []}
    )
    return strategy


def _make_focused(llm, max_iterations=3, questions_per_iteration=2):
    """Focused-iteration in its production shape (browsecomp explorer on),
    with the explorer's search boundary stubbed out."""
    strategy = FocusedIterationStrategy(
        model=llm,
        search=_counting_search(),
        all_links_of_system=[],
        max_iterations=max_iterations,
        questions_per_iteration=questions_per_iteration,
        use_browsecomp_optimization=True,
    )
    progress = MagicMock()
    progress.found_candidates = {}
    progress.entity_coverage = {}
    explorer = MagicMock()
    explored: list[list[str]] = []

    def explore(queries, **kwargs):
        explored.append(list(queries))
        return ([], progress)

    explorer.explore = MagicMock(side_effect=explore)
    explorer.progress = progress
    strategy.explorer = explorer
    strategy.explored = explored
    strategy.citation_handler.analyze_followup = MagicMock(
        return_value={"content": "synthesised", "documents": []}
    )
    return strategy


# ---------------------------------------------------------------------------
# 1. Strategy dispatch
# ---------------------------------------------------------------------------


EXPECTED_REGISTRY_CLASSES = {
    "source-based": "SourceBasedSearchStrategy",
    "focused-iteration": "FocusedIterationStrategy",
    "focused-iteration-standard": "FocusedIterationStrategy",
    "topic-organization": "TopicOrganizationStrategy",
    "langgraph-agent": "LangGraphAgentStrategy",
}


class TestStrategyDispatch:
    """Every registry name resolves; unknown names degrade, not crash."""

    def test_registry_matches_expected_class_table(self):
        """The registry itself has not drifted from this test's table.

        Guards the two tests below from silently going vacuous if a
        strategy is added to or removed from ``AVAILABLE_STRATEGIES``.
        """
        names = [entry["name"] for entry in AVAILABLE_STRATEGIES]
        assert sorted(names) == sorted(EXPECTED_REGISTRY_CLASSES)

    @pytest.mark.parametrize("name", sorted(EXPECTED_REGISTRY_CLASSES))
    def test_registry_name_resolves_to_its_own_class(self, name):
        """Each registry name builds the class it advertises.

        Notably stronger than "not None": a name that silently fell
        through to the source-based fallback would pass an
        is-not-None check but fails here.
        """
        strategy = create_strategy(name, MagicMock(), MagicMock())
        assert type(strategy).__name__ == EXPECTED_REGISTRY_CLASSES[name]

    def test_focused_iteration_variants_differ_in_citation_handler(self):
        """``focused-iteration-standard`` is not merely an alias.

        Both variants build ``FocusedIterationStrategy``, so the class
        check above cannot tell them apart; the citation handler is the
        actual difference the factory encodes.
        """
        plain = create_strategy("focused-iteration", MagicMock(), MagicMock())
        standard = create_strategy(
            "focused-iteration-standard", MagicMock(), MagicMock()
        )
        assert (
            type(plain.citation_handler._handler).__name__
            == "ForcedAnswerCitationHandler"
        )
        assert (
            type(standard.citation_handler._handler).__name__
            == "StandardCitationHandler"
        )

    @pytest.mark.parametrize("alias", ["mcp", "agentic", "langgraph_agent"])
    def test_removed_aliases_route_to_langgraph(self, alias):
        """Deprecated names route to their successor, not the fallback."""
        strategy = create_strategy(alias, MagicMock(), MagicMock())
        assert type(strategy).__name__ == "LangGraphAgentStrategy"

    @pytest.mark.parametrize(
        "name",
        ["no-such-strategy", "", "SOURCE BASED", "iterdrag", "évidence"],
    )
    def test_unknown_name_degrades_to_source_based(self, name):
        """An unrecognised name must fall back, never raise."""
        strategy = create_strategy(name, MagicMock(), MagicMock())
        assert type(strategy).__name__ == "SourceBasedSearchStrategy"

    @pytest.mark.parametrize("name", sorted(EXPECTED_REGISTRY_CLASSES))
    def test_delegate_strategy_from_research_context(self, name):
        """``research_context["delegate_strategy"]`` picks the delegate.

        Exercises the real factory through
        ``AdvancedSearchSystem.__init__`` rather than a patched
        ``create_strategy``, so a rename on either side of that seam is
        caught.
        """
        system = AdvancedSearchSystem(
            llm=MagicMock(),
            search=MagicMock(),
            strategy_name="contextual-followup",
            research_context={"delegate_strategy": name},
        )
        assert isinstance(system.strategy, EnhancedContextualFollowUpStrategy)
        assert (
            type(system.strategy.delegate_strategy).__name__
            == EXPECTED_REGISTRY_CLASSES[name]
        )

    @pytest.mark.parametrize(
        "research_context", [None, {}, {"delegate_strategy": "who-knows"}]
    )
    def test_missing_or_unknown_delegate_degrades(self, research_context):
        """Absent/unknown delegate names still produce a usable delegate."""
        system = AdvancedSearchSystem(
            llm=MagicMock(),
            search=MagicMock(),
            strategy_name="enhanced-contextual-followup",
            research_context=research_context,
        )
        assert isinstance(
            system.strategy.delegate_strategy, SourceBasedSearchStrategy
        )

    @pytest.mark.parametrize("name", [123, None, ["source-based"]])
    def test_non_string_strategy_name_degrades_rather_than_crashing(self, name):
        """A settings value or replayed queue row can hold any JSON type."""
        system = AdvancedSearchSystem(
            llm=MagicMock(), search=MagicMock(), strategy_name=name
        )
        assert isinstance(system.strategy, SourceBasedSearchStrategy)

    @pytest.mark.parametrize(
        "research_context", [["focused-iteration"], "text", 7]
    )
    def test_non_dict_research_context_degrades_rather_than_crashing(
        self, research_context
    ):
        system = AdvancedSearchSystem(
            llm=MagicMock(),
            search=MagicMock(),
            strategy_name="contextual-followup",
            research_context=research_context,
        )
        assert system.research_context is None
        assert isinstance(
            system.strategy.delegate_strategy, SourceBasedSearchStrategy
        )

    @pytest.mark.parametrize(
        "research_context",
        [
            {"past_findings": 42},
            {"formatted_findings": 42},
            {"report_content": ["text"]},
            {"resources": 7},
            {"resources": "https://example.com"},
            {"past_links": ["https://example.com"]},
        ],
    )
    def test_malformed_follow_up_fields_do_not_break_construction(
        self, research_context
    ):
        """The real follow-up constructor, with every consumer of the
        findings and sources fields, accepts a JSON value of any type."""
        system = AdvancedSearchSystem(
            llm=MagicMock(),
            search=MagicMock(),
            strategy_name="contextual-followup",
            research_context=research_context,
        )
        context = system.strategy.full_context
        assert context["past_sources"] == []
        assert context["past_findings"] == "No previous findings available"

    def test_well_formed_follow_up_fields_survive_construction(self):
        source = {"url": "https://a.example", "title": "A"}
        system = AdvancedSearchSystem(
            llm=MagicMock(),
            search=MagicMock(),
            strategy_name="contextual-followup",
            research_context={
                "resources": [source, "https://b.example"],
                "past_findings": "Earlier result.",
            },
        )
        context = system.strategy.full_context
        assert context["past_sources"] == [source]
        assert context["past_findings"] == "Earlier result."

    @pytest.mark.parametrize("delegate", [123, None, ["focused-iteration"]])
    def test_non_string_delegate_degrades_rather_than_crashing(self, delegate):
        system = AdvancedSearchSystem(
            llm=MagicMock(),
            search=MagicMock(),
            strategy_name="contextual-followup",
            research_context={"delegate_strategy": delegate},
        )
        assert isinstance(
            system.strategy.delegate_strategy, SourceBasedSearchStrategy
        )


# ---------------------------------------------------------------------------
# 2. Cancellation checkpoints
# ---------------------------------------------------------------------------


def _iter_layer_sources():
    """Yield ``(path, module_source)`` for the strategy layer."""
    import local_deep_research

    root = pathlib.Path(local_deep_research.__file__).parent
    paths = sorted((root / "advanced_search_system").rglob("*.py"))
    paths += [root / "search_system.py", root / "search_system_factory.py"]
    for path in paths:
        yield path, path.read_text(encoding="utf-8")


def _base_exception_catchers(tree):
    """Return line numbers of handlers that would catch BaseException."""
    offending = []
    for node in ast.walk(tree):
        if isinstance(node, ast.ExceptHandler):
            if node.type is None:
                offending.append(node.lineno)
                continue
            caught = (
                node.type.elts
                if isinstance(node.type, ast.Tuple)
                else [node.type]
            )
            for item in caught:
                name = getattr(item, "id", None) or getattr(item, "attr", None)
                if name == "BaseException":
                    offending.append(node.lineno)
        elif isinstance(node, ast.Call):
            func = node.func
            name = getattr(func, "id", None) or getattr(func, "attr", None)
            if name != "suppress":
                continue
            for arg in node.args:
                arg_name = getattr(arg, "id", None) or getattr(
                    arg, "attr", None
                )
                if arg_name == "BaseException":
                    offending.append(node.lineno)
    return offending


class TestCancellationCannotBeSwallowed:
    """``ResearchTerminatedException`` derives from ``BaseException`` so
    that ``except Exception`` lets it through.  That only holds while no
    handler in the layer widens to ``BaseException``."""

    def test_no_handler_in_the_layer_catches_base_exception(self):
        """Static sweep of every module in the strategy layer.

        A bare ``except:``, an ``except BaseException``, or a
        ``suppress(BaseException)`` anywhere here would silently make a
        Stop click un-honourable.
        """
        scanned = 0
        offenders = []
        for path, source in _iter_layer_sources():
            scanned += 1
            for lineno in _base_exception_catchers(ast.parse(source)):
                offenders.append(f"{path}:{lineno}")
        # Guard against the sweep silently finding nothing to scan.
        assert scanned >= 20, f"only {scanned} modules scanned"
        assert offenders == [], (
            "handlers that would swallow ResearchTerminatedException: "
            + ", ".join(offenders)
        )

    def test_layer_modules_all_parse(self):
        """The sweep above is only meaningful if every file parsed."""
        for path, source in _iter_layer_sources():
            assert ast.parse(source) is not None, path

    @pytest.mark.parametrize(
        "context",
        [
            CHECK_CONTEXT_ITERATION_START,
            CHECK_CONTEXT_POST_SEARCH,
            CHECK_CONTEXT_PRE_SYNTHESIS,
        ],
    )
    def test_focused_iteration_cancels_at_each_checkpoint(self, context):
        """Focused-iteration honours a stop at each of its three checks.

        The strategy wraps its whole loop in ``except Exception`` and
        returns an error dict; if the cancellation were downgraded to
        ``Exception`` the call would return that dict instead of raising.
        """
        llm = ScriptedLLM("Q: alpha\nQ: beta")
        strategy = _make_focused(llm)
        callback, seen = _cancel_at(context)
        strategy.set_progress_callback(callback)

        with pytest.raises(ResearchTerminatedException):
            strategy.analyze_topic("original query")

        assert seen.get(context) == 1
        assert strategy.citation_handler.analyze_followup.call_count == 0

    def test_focused_iteration_positive_control_runs_to_completion(self):
        """Positive control for the three cancellation tests above.

        Identical wiring minus the raise: the run finishes, all three
        checkpoints fire, and synthesis happens — proving the tests above
        observe cancellation and not an unrelated early exit.
        """
        llm = ScriptedLLM("Q: alpha\nQ: beta")
        strategy = _make_focused(llm, max_iterations=3)
        callback, seen = _recording_callback()
        strategy.set_progress_callback(callback)

        result = strategy.analyze_topic("original query")

        assert result.get("error") is None
        assert seen[CHECK_CONTEXT_ITERATION_START] == 3
        assert seen[CHECK_CONTEXT_POST_SEARCH] == 3
        assert seen[CHECK_CONTEXT_PRE_SYNTHESIS] == 1
        assert strategy.citation_handler.analyze_followup.call_count == 1
        assert result["current_knowledge"] == "synthesised"

    def test_cancellation_survives_source_based_error_handler(self):
        """Source-based swallows ``Exception`` into an error finding.

        A stop raised from inside its iteration loop must still surface
        as ``ResearchTerminatedException`` rather than becoming an
        "Error: ..." result the user cannot distinguish from a failure.
        """
        llm = ScriptedLLM("Q: alpha")
        strategy = _make_source_based(2, 1, llm)
        callback, seen = _cancel_at(CHECK_CONTEXT_PRE_SYNTHESIS)
        strategy.set_progress_callback(callback)

        with pytest.raises(ResearchTerminatedException):
            strategy.analyze_topic("original query")
        assert seen.get(CHECK_CONTEXT_PRE_SYNTHESIS) == 1

    def test_source_based_positive_control_returns_findings(self):
        """Positive control: the same strategy completes when not stopped."""
        llm = ScriptedLLM("Q: alpha")
        strategy = _make_source_based(2, 1, llm)
        callback, seen = _recording_callback()
        strategy.set_progress_callback(callback)

        result = strategy.analyze_topic("original query")

        assert seen[CHECK_CONTEXT_PRE_SYNTHESIS] == 1
        assert result["current_knowledge"] == "synthesised"
        assert result["iterations"] == 2

    @pytest.mark.parametrize(
        "call",
        [
            pytest.param(
                lambda llm: DecompositionQuestionGenerator(
                    llm
                ).generate_questions(query="q", context=""),
                id="decomposition_generate_questions",
            ),
            pytest.param(
                lambda llm: StandardQuestionGenerator(
                    llm
                ).generate_sub_questions("q"),
                id="standard_generate_sub_questions",
            ),
            pytest.param(
                lambda llm: FindingsRepository(llm).synthesize_findings(
                    "q", [], [{"content": "x"}]
                ),
                id="repository_synthesize_findings",
            ),
        ],
    )
    def test_broad_handlers_do_not_eat_cancellation(self, call):
        """These three call sites answer LLM failures with a fallback.

        A cancellation raised by the LLM boundary must not be turned
        into that fallback — it has to reach the caller.
        """
        llm = MagicMock()
        llm.invoke.side_effect = ResearchTerminatedException("stop")
        with pytest.raises(ResearchTerminatedException):
            call(llm)

    @pytest.mark.parametrize(
        "call",
        [
            pytest.param(
                lambda llm: DecompositionQuestionGenerator(
                    llm
                ).generate_questions(query="q", context=""),
                id="decomposition_generate_questions",
            ),
            pytest.param(
                lambda llm: StandardQuestionGenerator(
                    llm
                ).generate_sub_questions("q"),
                id="standard_generate_sub_questions",
            ),
        ],
    )
    def test_broad_handlers_still_absorb_ordinary_errors(self, call):
        """Positive control for the cancellation test above.

        The same handlers must keep absorbing a plain ``Exception``,
        otherwise the test above would pass for the wrong reason (a
        handler that catches nothing at all).
        """
        llm = MagicMock()
        llm.invoke.side_effect = RuntimeError("provider exploded")
        assert isinstance(call(llm), list)


# ---------------------------------------------------------------------------
# 3. Iteration / budget bounds
# ---------------------------------------------------------------------------


class TestIterationAndQuestionBudgets:
    """A hostile or empty LLM reply cannot widen the search budget."""

    def test_source_based_question_flood_is_clamped(self):
        """5000 parseable questions still yield ``n`` searches per round.

        The per-iteration question count also bounds the thread pool:
        ``run_parallel_searches`` sizes its executor at ``len(queries)``.
        """
        llm = ScriptedLLM(HOSTILE_QUESTION_FLOOD)
        strategy = _make_source_based(3, 2, llm)

        result = strategy.analyze_topic("original query")

        # Iteration 1 adds the verbatim user query to its 2 questions.
        assert [
            len(v) for _, v in sorted(strategy.questions_by_iteration.items())
        ] == [3, 2, 2]
        assert strategy.search.run.call_count == 7
        assert llm.calls == 3
        assert result["iterations"] == 3

    def test_source_based_flood_is_the_same_as_a_well_formed_reply(self):
        """Positive control: a 3-question reply produces the same budget.

        Proves the clamp above is the budget doing its job and not the
        flood being rejected wholesale.
        """
        llm = ScriptedLLM("Q: alpha\nQ: beta\nQ: gamma")
        strategy = _make_source_based(3, 2, llm)

        strategy.analyze_topic("original query")

        assert strategy.search.run.call_count == 7
        assert strategy.questions_by_iteration[1][0] == "original query"

    def test_source_based_unparseable_reply_terminates(self):
        """An LLM reply with no questions still ends after N iterations.

        Iteration 1 searches the verbatim query; later iterations record
        an empty question list and skip their search phase, rather than
        retrying or looping.
        """
        llm = ScriptedLLM("I refuse. Here is a poem instead.\n\n***")
        strategy = _make_source_based(3, 2, llm)

        result = strategy.analyze_topic("original query")

        assert strategy.search.queries == ["original query"]
        assert strategy.questions_by_iteration == {
            1: ["original query"],
            2: [],
            3: [],
        }
        assert llm.calls == 3
        assert result["iterations"] == 3

    def test_focused_iteration_flood_is_clamped(self):
        """Focused-iteration clamps to ``questions_per_iteration`` too.

        Round 1 searches only the verbatim query (entity-seeded), later
        rounds take at most ``questions_per_iteration`` of the flood.
        """
        llm = ScriptedLLM(HOSTILE_QUESTION_FLOOD)
        strategy = _make_focused(
            llm, max_iterations=4, questions_per_iteration=2
        )

        strategy.analyze_topic("original query")

        assert [len(batch) for batch in strategy.explored] == [1, 2, 2, 2]

    def test_focused_iteration_flood_matches_a_well_formed_reply(self):
        """Positive control: a 2-question reply yields the same budget."""
        llm = ScriptedLLM("Q: alpha\nQ: beta")
        strategy = _make_focused(
            llm, max_iterations=4, questions_per_iteration=2
        )

        strategy.analyze_topic("original query")

        assert [len(batch) for batch in strategy.explored] == [1, 2, 2, 2]
        assert strategy.explored[1] == ["alpha", "beta"]

    def test_focused_iteration_respects_max_iterations(self):
        """The loop bound is ``max_iterations``, whatever the LLM says."""
        llm = ScriptedLLM(HOSTILE_QUESTION_FLOOD)
        strategy = _make_focused(
            llm, max_iterations=2, questions_per_iteration=3
        )

        result = strategy.analyze_topic("original query")

        assert [len(batch) for batch in strategy.explored] == [1, 3]
        assert result["iterations"] == 2

    def test_question_generator_clamps_before_the_strategy_sees_it(self):
        """The clamp lives in the generator, so every caller inherits it."""
        llm = ScriptedLLM(HOSTILE_QUESTION_FLOOD)
        questions = StandardQuestionGenerator(llm).generate_questions(
            current_knowledge="",
            query="q",
            questions_per_iteration=4,
        )
        assert len(questions) == 4


# ---------------------------------------------------------------------------
# 4. Findings repository
# ---------------------------------------------------------------------------


class TestFindingsRepositoryBounds:
    def test_synthesis_prompt_is_bounded_for_a_large_corpus(self):
        """A 1 MB corpus must not be handed to the LLM verbatim."""
        llm = ScriptedLLM("done")
        repo = FindingsRepository(llm)
        findings = [{"content": "x" * 10_000} for _ in range(100)]

        repo.synthesize_findings("q", ["sub"], findings)

        assert llm.calls == 1
        prompt = llm.prompts[0]
        assert "[...content truncated due to length...]" in prompt
        assert len(prompt) < 60_000

    def test_small_corpus_reaches_the_llm_intact(self):
        """Positive control: below the cap nothing is dropped."""
        llm = ScriptedLLM("done")
        repo = FindingsRepository(llm)

        repo.synthesize_findings("q", ["sub"], [{"content": "needle-42"}])

        assert "needle-42" in llm.prompts[0]
        assert "[...content truncated" not in llm.prompts[0]

    def test_format_findings_to_text_handles_a_large_ragged_corpus(self):
        """Formatting a big corpus of half-populated findings must not
        raise, and must carry every finding through."""
        repo = FindingsRepository(MagicMock())
        repo.set_questions_by_iteration({1: ["q1"], 2: ["q2"]})
        findings = [
            {"phase": f"Iteration {i}", "content": f"body-{i}"}
            if i % 2
            else {"phase": f"Iteration {i}"}
            for i in range(500)
        ]

        text = repo.format_findings_to_text(findings, "synthesis")

        assert not text.startswith("Error during final formatting")
        assert "body-1" in text and "body-499" in text
        # "\n### " does not match the "#### Iteration n:" question
        # headers emitted by the questions-by-iteration section.
        assert text.count("\n### Iteration ") == 500

    @pytest.mark.xfail(
        strict=True,
        reason=(
            "DEFECT: synthesize_findings arms a SIGALRM timeout, which "
            "raises ValueError off the main thread; its own except "
            "Exception swallows that and returns an error string without "
            "ever calling the LLM. Research runs in worker threads."
        ),
    )
    def test_synthesis_works_off_the_main_thread(self):
        llm = ScriptedLLM("worker synthesis")
        repo = FindingsRepository(llm)
        captured = {}

        def target():
            captured["result"] = repo.synthesize_findings(
                "q", [], [{"content": "data"}]
            )

        worker = threading.Thread(target=target)
        worker.start()
        worker.join(timeout=30)
        assert not worker.is_alive()

        assert llm.calls == 1
        assert captured["result"] == "worker synthesis"

    def test_synthesis_positive_control_on_the_main_thread(self):
        """Control for the xfail above: identical call, main thread."""
        llm = ScriptedLLM("main synthesis")
        repo = FindingsRepository(llm)

        result = repo.synthesize_findings("q", [], [{"content": "data"}])

        assert llm.calls == 1
        assert result == "main synthesis"


# ---------------------------------------------------------------------------
# 5. Question decomposition
# ---------------------------------------------------------------------------


class TestDecompositionHostileReplies:
    def test_flood_of_sub_queries_is_capped(self):
        """A 5000-line reply is cut to ``max_subqueries``."""
        llm = ScriptedLLM(
            "\n".join(f"What is flood topic number {i}?" for i in range(5000))
        )
        generator = DecompositionQuestionGenerator(llm, max_subqueries=5)

        questions = generator.generate_questions(query="q", context="")

        assert len(questions) == 5
        assert llm.calls == 1

    def test_blank_reply_retries_once_then_falls_back(self):
        """An empty reply costs exactly one retry, never a retry loop."""
        llm = ScriptedLLM("   \n \n   ")
        generator = DecompositionQuestionGenerator(llm, max_subqueries=5)

        questions = generator.generate_questions(query="csrf", context="")

        assert llm.calls == 2
        assert len(questions) == 5
        assert all(isinstance(q, str) and q for q in questions)

    def test_content_block_reply_is_coerced_not_repr_ed(self):
        """A list-form ``content`` (Anthropic blocks) must be read as text.

        Without coercion the list's ``repr()`` would be parsed, producing
        one bogus mega-question.
        """
        llm = MagicMock()
        message = MagicMock()
        message.content = [
            {"type": "text", "text": "What is the first sub topic?"},
            {"type": "tool_use", "id": "t1", "input": {}},
            {"type": "text", "text": "\nWhat is the second sub topic?"},
        ]
        llm.invoke.return_value = message
        generator = DecompositionQuestionGenerator(llm, max_subqueries=5)

        questions = generator.generate_questions(query="q", context="")

        assert questions == [
            "What is the first sub topic?",
            "What is the second sub topic?",
        ]

    def test_reply_of_only_bullet_glyphs_falls_back_to_defaults(self):
        """Formatting-only output yields usable defaults, not an empty list."""
        llm = ScriptedLLM("*\n-\n•\n*\n-")
        generator = DecompositionQuestionGenerator(llm, max_subqueries=3)

        questions = generator.generate_questions(query="rust", context="")

        assert len(questions) == 3
        assert llm.calls == 2

    def test_max_subqueries_zero_yields_nothing(self):
        """A zero budget is honoured rather than silently ignored."""
        llm = ScriptedLLM("What is the first sub topic in this answer?")
        generator = DecompositionQuestionGenerator(llm, max_subqueries=0)

        assert generator.generate_questions(query="q", context="") == []

    def test_context_is_truncated_before_reaching_the_llm(self):
        """A huge context cannot inflate the decomposition prompt."""
        llm = ScriptedLLM("What is the first sub topic in this answer?")
        generator = DecompositionQuestionGenerator(llm, max_subqueries=5)

        generator.generate_questions(query="q", context="C" * 500_000)

        assert llm.prompts[0].count("C") <= 2100
        assert len(llm.prompts[0]) < 5_000


# ---------------------------------------------------------------------------
# 6. Follow-up delegate settings handling
# ---------------------------------------------------------------------------


def _make_followup(settings_snapshot):
    strategy = EnhancedContextualFollowUpStrategy(
        model=MagicMock(),
        search=MagicMock(),
        delegate_strategy=MagicMock(),
        settings_snapshot=settings_snapshot,
        research_context={
            "resources": [{"url": "https://example.invalid/1", "title": "past"}]
        },
    )
    strategy.relevance_filter = MagicMock()
    strategy.relevance_filter.filter_results.return_value = []
    return strategy


class TestFollowUpSourceBudget:
    def test_dict_wrapped_setting_is_honoured(self):
        """Control: the snapshot shape the code was written for works."""
        strategy = _make_followup({"search.max_followup_sources": {"value": 7}})

        strategy._filter_relevant_sources("q")

        call = strategy.relevance_filter.filter_results.call_args
        assert call.kwargs["max_results"] == 7

    def test_missing_setting_uses_the_default_budget(self):
        strategy = _make_followup({})

        strategy._filter_relevant_sources("q")

        call = strategy.relevance_filter.filter_results.call_args
        assert call.kwargs["max_results"] == 15

    @pytest.mark.xfail(
        strict=True,
        reason=(
            "DEFECT: _filter_relevant_sources does .get('value') on the "
            "raw setting instead of using unwrap_setting/get_setting, so "
            "a simplified (raw-scalar) settings snapshot raises "
            "AttributeError out of analyze_topic."
        ),
    )
    def test_raw_scalar_setting_is_honoured(self):
        strategy = _make_followup({"search.max_followup_sources": 7})

        strategy._filter_relevant_sources("q")

        call = strategy.relevance_filter.filter_results.call_args
        assert call.kwargs["max_results"] == 7