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.

12.1 KB
Raw Blame
"""The sync/async split of the citation-handler LLM calls (#5854).

Two invariants are pinned here:

* the sync entry points (``_invoke_text`` / ``_invoke_with_streaming``) stay
  on the synchronous LangChain API and never start an event loop — langchain's
  async httpx client is process-cached and loop-bound, so a throwaway per-call
  loop would break or re-send every call after the first (#6293);
* the async cores (``_invoke_text_async`` /
  ``_invoke_with_streaming_async``) use only the async API. They are additive:
  no production caller awaits them yet.
"""

import asyncio
from unittest.mock import AsyncMock, Mock

import pytest

from local_deep_research.citation_handlers.standard_citation_handler import (
    StandardCitationHandler,
)


def _forbid_event_loops(monkeypatch):
    """Make any attempt to start an event loop fail loudly."""

    def _boom(*args, **kwargs):
        raise AssertionError(
            "the sync path must not create an event loop (#6293)"
        )

    monkeypatch.setattr(asyncio, "run", _boom)
    monkeypatch.setattr(asyncio, "new_event_loop", _boom)
    monkeypatch.setattr(asyncio, "Runner", _boom)


def _dual_api_llm(chunks=None, response="full response"):
    """An LLM exposing BOTH the sync and the async API.

    Exposing both is what makes the sync-path assertions discriminating: the
    handler could reach either, and the test proves it picks the sync one.
    """

    async def _astream(_prompt):
        for chunk in chunks or []:
            yield Mock(content=chunk)

    def _stream(_prompt):
        for chunk in chunks or []:
            yield Mock(content=chunk)

    llm = Mock()
    llm.invoke = Mock(return_value=Mock(content=response))
    llm.ainvoke = AsyncMock(return_value=Mock(content=response))
    llm.stream = Mock(side_effect=_stream)
    llm.astream = Mock(side_effect=_astream)
    return llm


class TestSyncEntryPointsStartNoEventLoop:
    """The sync entry points use ``invoke``/``stream``, no loop involved."""

    def test_invoke_text_uses_sync_invoke(self, monkeypatch):
        _forbid_event_loops(monkeypatch)
        llm = _dual_api_llm(response="sync answer")
        handler = StandardCitationHandler(llm=llm)

        assert handler._invoke_text("prompt") == "sync answer"

        llm.invoke.assert_called_once_with("prompt")
        llm.ainvoke.assert_not_called()

    def test_invoke_with_streaming_uses_sync_stream(self, monkeypatch):
        _forbid_event_loops(monkeypatch)
        llm = _dual_api_llm(chunks=["one ", "two"])
        handler = StandardCitationHandler(llm=llm)
        callback = Mock()
        handler.set_stream_callback(callback)

        assert handler._invoke_with_streaming("prompt") == "one two"

        llm.stream.assert_called_once_with("prompt")
        llm.astream.assert_not_called()
        llm.ainvoke.assert_not_called()
        assert callback.call_count == 2

    def test_invoke_with_streaming_without_callback_uses_sync_invoke(
        self, monkeypatch
    ):
        _forbid_event_loops(monkeypatch)
        llm = _dual_api_llm(chunks=["ignored"], response="sync answer")
        handler = StandardCitationHandler(llm=llm)

        assert handler._invoke_with_streaming("prompt") == "sync answer"

        llm.invoke.assert_called_once_with("prompt")
        llm.stream.assert_not_called()
        llm.ainvoke.assert_not_called()
        llm.astream.assert_not_called()


class TestAsyncCores:
    """The async cores use only ``ainvoke``/``astream``."""

    @pytest.mark.asyncio
    async def test_invoke_text_async_awaits_ainvoke(self):
        llm = _dual_api_llm(response="async answer")
        handler = StandardCitationHandler(llm=llm)

        assert await handler._invoke_text_async("prompt") == "async answer"

        llm.ainvoke.assert_awaited_once_with("prompt")
        llm.invoke.assert_not_called()

    @pytest.mark.asyncio
    async def test_invoke_with_streaming_async_awaits_astream(self):
        llm = _dual_api_llm(chunks=["one ", "two"])
        handler = StandardCitationHandler(llm=llm)
        callback = Mock()
        handler.set_stream_callback(callback)

        result = await handler._invoke_with_streaming_async("prompt")

        assert result == "one two"
        llm.astream.assert_called_once_with("prompt")
        llm.stream.assert_not_called()
        llm.invoke.assert_not_called()
        assert callback.call_count == 2

    @pytest.mark.asyncio
    async def test_invoke_with_streaming_async_without_callback_awaits_ainvoke(
        self,
    ):
        llm = _dual_api_llm(chunks=["ignored"], response="async answer")
        handler = StandardCitationHandler(llm=llm)

        result = await handler._invoke_with_streaming_async("prompt")

        assert result == "async answer"
        llm.ainvoke.assert_awaited_once_with("prompt")
        llm.astream.assert_not_called()
        llm.invoke.assert_not_called()

    @pytest.mark.asyncio
    async def test_invoke_with_streaming_async_falls_back_before_first_chunk(
        self,
    ):
        """A stream that dies before emitting anything falls back to ainvoke."""
        llm = _dual_api_llm(response="async fallback")
        llm.astream = Mock(side_effect=Exception("stream error"))
        handler = StandardCitationHandler(llm=llm)
        handler.set_stream_callback(Mock())

        result = await handler._invoke_with_streaming_async("prompt")

        assert result == "async fallback"
        llm.astream.assert_called_once()
        llm.ainvoke.assert_awaited_once_with("prompt")

    @pytest.mark.asyncio
    async def test_invoke_with_streaming_async_returns_empty_partial_after_think_only_failure(
        self,
    ):
        """A mid-stream failure whose only chunks are ``<think>...</think>``
        content strips to an empty string after normalization. That empty
        partial must still be returned — not treated as "nothing happened"
        (which would restart with a fresh, double-billed ``ainvoke()`` call).
        Pins the ``is not None`` check in ``_partial_after_stream_failure``
        callers: a truthiness check would treat "" as falsy and fall back.
        """

        async def _think_only(_prompt):
            yield Mock(content="<think>")
            yield Mock(content="x</think>")
            raise Exception("connection dropped")

        llm = _dual_api_llm(response="never used")
        llm.astream = Mock(side_effect=_think_only)
        handler = StandardCitationHandler(llm=llm)
        received = []
        handler.set_stream_callback(received.append)

        result = await handler._invoke_with_streaming_async("prompt")

        assert result == ""
        llm.ainvoke.assert_not_called()
        llm.invoke.assert_not_called()

    @pytest.mark.asyncio
    async def test_invoke_with_streaming_async_returns_partial_on_mid_stream_failure(
        self,
    ):
        """Same contract as the sync path: no full-call restart after chunks
        already reached the client (double-bill + UI/DB divergence)."""

        async def _partial(_prompt):
            yield Mock(content="partial ")
            yield Mock(content="content")
            raise Exception("connection dropped")

        llm = _dual_api_llm(response="never used")
        llm.astream = Mock(side_effect=_partial)
        handler = StandardCitationHandler(llm=llm)
        received = []
        handler.set_stream_callback(received.append)

        result = await handler._invoke_with_streaming_async("prompt")

        assert result == "partial content"
        assert received == ["partial ", "content"]
        llm.ainvoke.assert_not_called()
        llm.invoke.assert_not_called()


class TestStreamContentBlocks:
    """Sync and async streams accept message content blocks as text chunks."""

    @pytest.mark.asyncio
    @pytest.mark.parametrize("use_async", [False, True])
    @pytest.mark.parametrize(
        "chunks,received,answer",
        [
            (
                [
                    [{"type": "text", "text": "one "}],
                    ["two", {"text": " three"}],
                ],
                ["one ", "two three"],
                "one two three",
            ),
            (
                [
                    [{"type": "tool_use", "id": "tool"}],
                    [{"type": "thinking", "thinking": "hidden"}],
                ],
                [],
                "",
            ),
            (
                [
                    [{"type": "text", "text": "<thi"}],
                    [{"text": "nk>hidden</think>"}],
                    [{"text": " answer"}],
                ],
                ["<thi", "nk>hidden</think>", " answer"],
                "answer",
            ),
        ],
        ids=["text-and-spaces", "nontext-only", "split-think-tags"],
    )
    async def test_content_blocks(self, use_async, chunks, received, answer):
        llm = _dual_api_llm(chunks=chunks)
        handler = StandardCitationHandler(llm=llm)
        actual = []
        handler.set_stream_callback(actual.append)

        if use_async:
            result = await handler._invoke_with_streaming_async("prompt")
            llm.astream.assert_called_once_with("prompt")
            llm.stream.assert_not_called()
        else:
            result = handler._invoke_with_streaming("prompt")
            llm.stream.assert_called_once_with("prompt")
            llm.astream.assert_not_called()

        assert result == answer
        assert actual == received
        assert all(isinstance(chunk, str) for chunk in actual)
        llm.invoke.assert_not_called()
        llm.ainvoke.assert_not_called()

    @pytest.mark.asyncio
    @pytest.mark.parametrize("use_async", [False, True])
    async def test_partial_blocks_do_not_restart(self, use_async):
        def _partial(_prompt):
            yield Mock(content=[{"type": "text", "text": "partial "}])
            yield Mock(content=[{"text": "answer"}])
            raise RuntimeError("connection dropped")

        async def _apartial(prompt):
            for chunk in _partial(prompt):
                yield chunk

        llm = _dual_api_llm(response="never used")
        llm.stream = Mock(side_effect=_partial)
        llm.astream = Mock(side_effect=_apartial)
        handler = StandardCitationHandler(llm=llm)
        received = []
        handler.set_stream_callback(received.append)

        if use_async:
            result = await handler._invoke_with_streaming_async("prompt")
            llm.stream.assert_not_called()
        else:
            result = handler._invoke_with_streaming("prompt")
            llm.astream.assert_not_called()

        assert result == "partial answer"
        assert received == ["partial ", "answer"]
        llm.invoke.assert_not_called()
        llm.ainvoke.assert_not_called()


class TestAsyncStreamRepetitionGuard:
    """The async path carries the same guard as the sync one (#6452).

    Worth its own cell: the two streaming loops are separate code, and a fix
    applied to one of them looks complete from the other's tests.
    """

    _CYCLE = [
        "### High-Speed Intercity Express Routing\n",
        "Prose about routing and station placement.\n",
        "### Electrification Infrastructure\n",
        "Prose about grid interconnect standards.\n",
    ]

    @pytest.mark.asyncio
    async def test_a_repeating_cycle_stops_the_async_stream(self):
        loop = self._CYCLE * 30
        llm = _dual_api_llm(chunks=loop)
        handler = StandardCitationHandler(llm=llm)
        handler.set_stream_callback(Mock())

        text = await handler._invoke_with_streaming_async("prompt")

        assert text.count("High-Speed Intercity Express Routing") <= 4
        assert len(text) < len("".join(loop)) / 2
        # The usable first cycle survives; only the loop is dropped.
        assert "Prose about routing and station placement." in text

    @pytest.mark.asyncio
    async def test_an_ordinary_async_document_streams_whole(self):
        chunks = [
            "# Report\n",
            "## Costs\n",
            "Body.\n",
            "## Summary\n",
            "Body.\n",
        ]
        llm = _dual_api_llm(chunks=chunks)
        handler = StandardCitationHandler(llm=llm)
        handler.set_stream_callback(Mock())

        text = await handler._invoke_with_streaming_async("prompt")

        assert text == "".join(chunks).strip()