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.

17.7 KB
Raw Blame
"""Integration tests for the citation-formatter sentinel wiring inside
``run_research_process``.

Ported from the now-deleted Flask suite
(``tests/web/services/test_research_service_coverage.py::
TestRunResearchProcessDetailedMode`` sentinel tests) for main commit
367df4ceb "fix(citation): make format_document_split resilient to
LLM-emitted Sources headers (#5283)".

The source fix survived the FastAPI migration unchanged:
``citation_formatter.py::format_document_split`` returns a 3-tuple
``(answer, sources, on_sentinel)`` and ``research_service.py`` uses
``on_sentinel`` to skip the "appears to have over-stripped" safety-net
warning when the boundary came from ``LDR_APPENDED_SOURCES_SENTINEL``
(detailed mode, which trusts the sentinel by default) while quick mode
always passes ``trust_sentinel=False`` (raw LLM output, where a spurious
sentinel match must not be trusted). Only the Flask/old-globals test
harness was removed.

This follows the CURRENT (FastAPI-branch) integration-test pattern
established in ``test_research_service_progress_integration.py`` —
patching ``local_deep_research.web.research_state`` (not the old Flask
``web.routes.globals``) and calling ``run_research_process`` directly
(it is safe to call un-wrapped; ``@log_for_research`` sets the research id
via a contextvar and needs no Flask/FastAPI request context) — extended
with the additional mocks (``get_llm``, ``get_search``,
``get_citation_formatter``, ``IntegratedReportGenerator``,
``get_report_storage``, ``ResearchSourcesService``) needed to drive the
run all the way through report generation and the save path.
"""

from contextlib import contextmanager, ExitStack
from local_deep_research.web.services import research_service  # noqa: E402
from unittest.mock import create_autospec, MagicMock, Mock, patch

from loguru import logger

RS = "local_deep_research.web.services.research_service"

# The production progress_callback emits at the custom "MILESTONE" log
# level (registered by log_utils.init_loguru). Tests don't run
# init_loguru, so register it here once — idempotent if it already
# exists. Mirrors test_research_service_progress_integration.py.
try:
    logger.level("MILESTONE", no=26)
except (ValueError, TypeError):
    pass


@contextmanager
def _noop_db_session(*_, **__):
    session = MagicMock()
    session.__enter__ = Mock(return_value=session)
    session.__exit__ = Mock(return_value=False)
    yield session


def _base_patches(mock_system, mock_formatter):
    """Patches shared by every test in this module — mirrors the
    ``captured_progress_callback`` harness in
    ``test_research_service_progress_integration.py``, extended to reach
    the citation-formatting / report-save code past the search phase.
    """
    return (
        patch(
            "local_deep_research.web.research_state.is_termination_requested",
            return_value=False,
        ),
        patch(
            "local_deep_research.web.research_state.is_research_active",
            return_value=True,
        ),
        patch(
            "local_deep_research.web.research_state.update_progress_and_check_active",
            return_value=(50, True),
        ),
        patch(f"{RS}.get_llm", return_value=MagicMock()),
        patch(f"{RS}.get_search", return_value=MagicMock()),
        patch(f"{RS}.AdvancedSearchSystem", return_value=mock_system),
        patch(f"{RS}.get_citation_formatter", return_value=mock_formatter),
        patch(f"{RS}.get_user_db_session", side_effect=_noop_db_session),
        patch(f"{RS}.cleanup_research_resources"),
        patch(f"{RS}.set_search_context"),
        # research.created_at is a MagicMock attribute (no real DB row is
        # seeded); calculate_duration otherwise hands it to
        # dateutil.parser.parse(), which spins forever tokenizing a
        # MagicMock instead of raising.
        patch(f"{RS}.calculate_duration", return_value=20.0),
        # autospec so a signature change (e.g. the required ``owner`` kwarg) fails here instead of being silently swallowed -- a bare mock accepts any call and hid exactly that break once already.
        patch(
            f"{RS}._sio_emit",
            create_autospec(research_service._sio_emit, spec_set=True),
        ),
        patch(
            f"{RS}._socket_emitter",
            create_autospec(research_service._socket_emitter, spec_set=True),
        ),
        patch(f"{RS}.extract_links_from_search_results", return_value=[]),
        patch("local_deep_research.chat.service.ChatService", MagicMock()),
        patch("local_deep_research.web.queue.processor_v2.queue_processor"),
        patch("local_deep_research.settings.logger.log_settings"),
        patch(
            "local_deep_research.config.thread_settings.set_settings_context"
        ),
    )


class TestRunResearchProcessDetailedModeSentinel:
    """The over-strip safety check is bypassed when detailed-mode content
    contains the appended-sources sentinel emitted by
    IntegratedReportGenerator.

    Regression for triage item #4: a 1380-source detailed-mode run logged
    a misleading ``format_document_split appears to have over-stripped
    (answer=84716 chars, original=196122 chars)`` warning even though the
    splitter had correctly identified the trailing sources section. The
    warning fired because the answer/sources ratio (43%) was below the
    50% safety threshold. With the sentinel, the splitter knows the
    boundary is correct by construction and the check no longer fires.
    """

    def test_sentinel_skips_overstrip_warning(self, loguru_caplog):
        from local_deep_research.text_optimization.citation_formatter import (
            LDR_APPENDED_SOURCES_SENTINEL,
        )

        mock_system = MagicMock()
        mock_system.all_links_of_system = [
            {"url": "http://a.com", "title": "A"}
        ]
        mock_system.analyze_topic.return_value = {
            "findings": [{"content": "data"}],
            "formatted_findings": "# Report",
            "iterations": 5,
            "search_system": mock_system,
        }

        # Long content where the answer is well under 50% of the total —
        # this would trip the over-strip safety check by ratio alone.
        answer = "A" * 900  # > SAFETY_MIN_LEN (800)
        sources_tail = "[1] S\n   URL: https://s.example\n" * 100
        content = (
            answer
            + f"\n\n{LDR_APPENDED_SOURCES_SENTINEL}\n\n"
            + "## Sources\n\n"
            + sources_tail
        )
        assert len(answer) < len(content) * 0.5

        mock_report_gen = MagicMock()
        mock_report_gen.generate_report.return_value = {
            "content": content,
            "metadata": {"sections": 3},
        }

        mock_formatter = MagicMock()
        # format_document_split returns (answer, sources, on_sentinel).
        # on_sentinel=True mimics the detailed-mode path where the report
        # generator emitted the sentinel.
        mock_formatter.format_document_split.return_value = (
            answer,
            content[len(answer) :],
            True,
        )

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 1

        with loguru_caplog.at_level("WARNING"), ExitStack() as stack:
            for cm in _base_patches(mock_system, mock_formatter):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    f"{RS}.IntegratedReportGenerator",
                    return_value=mock_report_gen,
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.web.services.research_sources_service.ResearchSourcesService",
                    return_value=mock_sources_service,
                )
            )
            from local_deep_research.web.services.research_service import (
                run_research_process,
            )

            run_research_process(
                research_id=1,
                query="query",
                mode="detailed",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )

        assert (
            "format_document_split appears to have over-stripped"
            not in loguru_caplog.text
        )

        mock_storage.save_report.assert_called_once()
        persisted_content = mock_storage.save_report.call_args.kwargs["content"]
        assert persisted_content == answer

    def test_legacy_oversplit_without_sentinel_still_fires_warning(
        self, loguru_caplog
    ):
        """Counterpart: the over-strip safety check still fires when no
        sentinel is present and the splitter returned a suspiciously
        short answer.

        The sentinel is an opt-in safety net for the detailed-mode report
        generator; callers that bypass it must still get the warning +
        fallback, so a genuine over-strip bug doesn't go silent.
        """
        mock_system = MagicMock()
        mock_system.all_links_of_system = [
            {"url": "http://a.com", "title": "A"}
        ]
        mock_system.analyze_topic.return_value = {
            "findings": [{"content": "data"}],
            "formatted_findings": "# Report",
            "iterations": 5,
            "search_system": mock_system,
        }

        answer = "A" * 900
        sources_tail = "[1] S\n   URL: https://s.example\n" * 100
        content = answer + "\n\n## Sources\n\n" + sources_tail
        assert len(answer) < len(content) * 0.5

        mock_report_gen = MagicMock()
        mock_report_gen.generate_report.return_value = {
            "content": content,
            "metadata": {"sections": 3},
        }

        mock_formatter = MagicMock()
        # Simulate what the splitter does on an early "## Sources" match:
        # returns the truncated answer, on_sentinel=False (no sentinel in
        # the input — the legacy regex matched an early ## Sources).
        mock_formatter.format_document_split.return_value = (
            answer,
            content[len(answer) :],
            False,
        )
        mock_formatter.apply_inline_hyperlinks.return_value = (
            "hyperlinked full content"
        )

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        mock_sources_service = MagicMock()
        mock_sources_service.save_research_sources.return_value = 1

        with loguru_caplog.at_level("WARNING"), ExitStack() as stack:
            for cm in _base_patches(mock_system, mock_formatter):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    f"{RS}.IntegratedReportGenerator",
                    return_value=mock_report_gen,
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                )
            )
            stack.enter_context(
                patch(
                    "local_deep_research.web.services.research_sources_service.ResearchSourcesService",
                    return_value=mock_sources_service,
                )
            )
            from local_deep_research.web.services.research_service import (
                run_research_process,
            )

            run_research_process(
                research_id=1,
                query="query",
                mode="detailed",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )

        assert (
            "format_document_split appears to have over-stripped"
            in loguru_caplog.text
        )

        mock_formatter.apply_inline_hyperlinks.assert_called()
        mock_storage.save_report.assert_called_once()
        persisted_content = mock_storage.save_report.call_args.kwargs["content"]
        assert persisted_content == "hyperlinked full content"


class TestRunResearchProcessQuickModeSentinel:
    """Quick mode always passes ``trust_sentinel=False`` to
    ``format_document_split`` — the input is raw LLM output the report
    generator never wraps in the appended-sources sentinel, so an LLM
    that quotes the marker verbatim must not silently trip the over-strip
    safety bypass.
    """

    def test_quick_mode_passes_trust_sentinel_false(self):
        mock_system = MagicMock()
        mock_system.all_links_of_system = []
        mock_system.analyze_topic.return_value = {
            "findings": [{"phase": "s", "content": "data"}],
            "formatted_findings": "# Results",
            "iterations": 1,
        }

        mock_formatter = MagicMock()
        mock_formatter.format_document_split.return_value = (
            "answer",
            "sources",
            False,
        )

        mock_storage = MagicMock()
        mock_storage.save_report.return_value = True

        with ExitStack() as stack:
            for cm in _base_patches(mock_system, mock_formatter):
                stack.enter_context(cm)
            stack.enter_context(
                patch(
                    "local_deep_research.storage.get_report_storage",
                    return_value=mock_storage,
                )
            )
            from local_deep_research.web.services.research_service import (
                run_research_process,
            )

            run_research_process(
                research_id=1,
                query="query",
                mode="quick",
                username="alice",
                settings_snapshot={"search.tool": "searxng"},
                model="m",
                search_engine="s",
            )

        mock_formatter.format_document_split.assert_called_once_with(
            "# Results", trust_sentinel=False
        )


def _run_detailed_and_capture_saved_content(formatter, report_content):
    """Run detailed mode with ``formatter`` and return what was saved."""
    mock_system = MagicMock()
    mock_system.all_links_of_system = [
        {"url": "https://s.example", "title": "S", "index": 1}
    ]
    mock_system.analyze_topic.return_value = {
        "findings": [{"content": "data"}],
        "formatted_findings": "# Report",
        "iterations": 5,
        "search_system": mock_system,
    }
    mock_report_gen = MagicMock()
    mock_report_gen.generate_report.return_value = {
        "content": report_content,
        "metadata": {"sections": 3},
    }
    mock_storage = MagicMock()
    mock_storage.save_report.return_value = True
    mock_sources_service = MagicMock()
    mock_sources_service.save_research_sources.return_value = 1

    with ExitStack() as stack:
        for cm in _base_patches(mock_system, formatter):
            stack.enter_context(cm)
        stack.enter_context(
            patch(
                f"{RS}.IntegratedReportGenerator",
                return_value=mock_report_gen,
            )
        )
        stack.enter_context(
            patch(
                "local_deep_research.storage.get_report_storage",
                return_value=mock_storage,
            )
        )
        stack.enter_context(
            patch(
                "local_deep_research.web.services.research_sources_service.ResearchSourcesService",
                return_value=mock_sources_service,
            )
        )
        from local_deep_research.web.services.research_service import (
            run_research_process,
        )

        run_research_process(
            research_id=1,
            query="query",
            mode="detailed",
            username="alice",
            settings_snapshot={"search.tool": "searxng"},
            model="m",
            search_engine="s",
        )

    mock_storage.save_report.assert_called_once()
    return mock_storage.save_report.call_args.kwargs["content"]


class TestRunResearchProcessDetailedModeSavesNoSentinel:
    """The appended-sources sentinel is an HTML comment, and
    renderMarkdown() shows raw HTML in Markdown as literal text. When the
    split keeps the Sources block in the saved answer, the sentinel must not
    be saved with it."""

    ANSWER = "The answer [1]."
    SOURCES = "## Sources\n\n[1] S\n   URL: https://s.example\n"

    def _content(self):
        from local_deep_research.text_optimization.citation_formatter import (
            LDR_APPENDED_SOURCES_SENTINEL,
        )

        return f"{self.ANSWER}\n\n{LDR_APPENDED_SOURCES_SENTINEL}\n\n{self.SOURCES}"

    def test_no_hyperlinks_mode_saves_the_report_without_the_sentinel(self):
        """NO_HYPERLINKS keeps the whole report as the answer, Sources block
        and all, so the sentinel used to be saved and shown on the page."""
        from local_deep_research.text_optimization.citation_formatter import (
            CitationFormatter,
            CitationMode,
        )

        saved = _run_detailed_and_capture_saved_content(
            CitationFormatter(mode=CitationMode.NO_HYPERLINKS), self._content()
        )

        assert "LDR_APPENDED_SOURCES_START" not in saved
        assert saved.startswith(self.ANSWER)
        assert saved.endswith(self.SOURCES)

    def test_formatter_failure_saves_the_raw_report_without_the_sentinel(
        self,
    ):
        formatter = MagicMock()
        formatter.format_document_split.side_effect = RuntimeError("boom")

        saved = _run_detailed_and_capture_saved_content(
            formatter, self._content()
        )

        assert "LDR_APPENDED_SOURCES_START" not in saved
        assert saved.startswith(self.ANSWER)
        assert saved.endswith(self.SOURCES)