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.

28.5 KB
Raw Blame
"""Cooperative cancellation of in-flight LLM calls on Stop.

Covers ``llm/cancellation.py`` and the termination-aware paths in
``ProcessingLLMWrapper`` / the rate-limiting wrapper: a set termination
flag must abort a running ``invoke`` mid-stream (not just between calls),
block new calls outright, and propagate through ``bind_tools``. All
fakes are synchronous and sleep-free (the mid-stream fake sets the flag
from inside its own generator, simulating a Stop arriving between
tokens).
"""

import asyncio
import uuid

from langchain_core.messages import AIMessage, AIMessageChunk


def _rid():
    return f"test-llm-cancel-{uuid.uuid4().hex[:12]}"


class _FakeStreamingLLM:
    """Minimal ChatModel double with controllable stream/invoke."""

    def __init__(self, chunks=None):
        self.chunks = list(chunks or [])
        self.invoke_calls = 0
        self.stream_calls = 0

    def invoke(self, prompt, *args, **kwargs):
        self.invoke_calls += 1
        return AIMessage(content="direct-invoke-response")

    def stream(self, prompt, *args, **kwargs):
        self.stream_calls += 1

        def _gen():
            for chunk in self.chunks:
                yield chunk

        return _gen()

    def bind_tools(self, tools, **kwargs):
        return self

    def with_structured_output(self, schema, **kwargs):
        return self


class TestPreTerminationBlocksNewCalls:
    def test_invoke_raises_without_touching_base(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()
        base = _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")])
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        set_termination_flag(research_id)
        try:
            try:
                llm.invoke("hello")
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
            assert base.stream_calls == 0
            assert base.invoke_calls == 0
        finally:
            cleanup_research(research_id)


class TestMidStreamAbort:
    def test_invoke_aborts_when_flag_set_between_chunks(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()

        def _gen():
            yield AIMessageChunk(content="tok0 ")
            # Simulate the user clicking Stop mid-generation.
            set_termination_flag(research_id)
            yield AIMessageChunk(content="tok1 ")

        class _MidStopLLM(_FakeStreamingLLM):
            def stream(self, prompt, *args, **kwargs):
                self.stream_calls += 1
                return _gen()

        llm = ProcessingLLMWrapper(_MidStopLLM(), research_id=research_id)
        try:
            try:
                llm.invoke("hello")
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
        finally:
            cleanup_research(research_id)

    def test_stream_generator_aborts_between_chunks(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()

        def _gen():
            yield AIMessageChunk(content="a")
            set_termination_flag(research_id)
            yield AIMessageChunk(content="b")

        class _MidStopLLM(_FakeStreamingLLM):
            def stream(self, prompt, *args, **kwargs):
                self.stream_calls += 1
                return _gen()

        llm = ProcessingLLMWrapper(_MidStopLLM(), research_id=research_id)
        try:
            seen = []
            try:
                for chunk in llm.stream("hello"):
                    seen.append(chunk)
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
            assert len(seen) == 1
        finally:
            cleanup_research(research_id)


class TestNoFlagCompletesViaStream:
    def test_invoke_streams_and_combines_when_not_terminated(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(
            chunks=[
                AIMessageChunk(content="tok0 "),
                AIMessageChunk(content="tok1"),
            ]
        )
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        try:
            response = llm.invoke("hello")
            assert getattr(response, "content", "") == "tok0 tok1"
            assert base.stream_calls == 1
            assert base.invoke_calls == 0
        finally:
            cleanup_research(research_id)

    def test_tool_calls_survive_stream_reassembly(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(
            chunks=[
                AIMessageChunk(
                    content="",
                    tool_calls=[
                        {
                            "name": "search",
                            "args": {"q": "x"},
                            "id": "call-1",
                            "type": "tool_call",
                        }
                    ],
                )
            ]
        )
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        try:
            response = llm.invoke("hello")
            assert getattr(response, "tool_calls", None)
        finally:
            cleanup_research(research_id)


class TestThreadContextFallback:
    def test_invoke_without_explicit_id_uses_search_context(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.utilities.thread_context import (
            clear_search_context,
            set_search_context,
        )
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()
        base = _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")])
        llm = ProcessingLLMWrapper(base)
        set_search_context({"research_id": research_id, "username": "u"})
        set_termination_flag(research_id)
        try:
            try:
                llm.invoke("hello")
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
        finally:
            clear_search_context()
            cleanup_research(research_id)


class TestScopePropagation:
    def test_bind_tools_keeps_research_id(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        llm = ProcessingLLMWrapper(_FakeStreamingLLM(), research_id=research_id)
        try:
            bound = llm.bind_tools([])
            assert getattr(bound, "_ldr_research_id", None) == research_id
        finally:
            cleanup_research(research_id)

    def test_bind_llm_to_research_reaches_inner_wrapper(self):
        from local_deep_research.llm.cancellation import bind_llm_to_research
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        outer = _FakeStreamingLLM()
        outer.base_llm = _FakeStreamingLLM()
        try:
            bind_llm_to_research(outer, research_id)
            assert outer._ldr_research_id == research_id
            assert outer.base_llm._ldr_research_id == research_id
        finally:
            cleanup_research(research_id)


class TestRateLimitedWrapper:
    def test_rate_limited_invoke_precheck_raises(self):
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )
        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        research_id = _rid()
        wrapped = create_rate_limited_llm_wrapper(_FakeStreamingLLM(), "openai")
        wrapped._ldr_research_id = research_id
        set_termination_flag(research_id)
        try:
            try:
                wrapped.invoke("hello")
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
        finally:
            cleanup_research(research_id)


class TestAsyncAbort:
    def test_ainvoke_aborts_mid_astream(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()

        class _AsyncMidStopLLM(_FakeStreamingLLM):
            def astream(self, prompt, *args, **kwargs):
                async def _gen():
                    yield AIMessageChunk(content="a")
                    set_termination_flag(research_id)
                    yield AIMessageChunk(content="b")

                return _gen()

        async def _run():
            llm = ProcessingLLMWrapper(
                _AsyncMidStopLLM(), research_id=research_id
            )
            await llm.ainvoke("hello")

        try:
            try:
                asyncio.run(_run())
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
        finally:
            cleanup_research(research_id)


class TestAbortDoesNotDrainStream:
    """Pin that an abort stops consuming the stream (no drain-then-raise)."""

    def test_invoke_aborts_without_draining_stream(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()
        pulled = []

        def _gen():
            for i in range(6):
                pulled.append(i)
                yield AIMessageChunk(content=f"tok{i} ")
                if i == 0:
                    # Simulate Stop arriving between tokens: the flag is set
                    # while the wrapper holds chunk 0, so the very next pull
                    # (chunk 1) must abort without touching chunks 2-5.
                    set_termination_flag(research_id)

        class _CountingLLM(_FakeStreamingLLM):
            def stream(self, prompt, *args, **kwargs):
                self.stream_calls += 1
                return _gen()

        llm = ProcessingLLMWrapper(_CountingLLM(), research_id=research_id)
        try:
            try:
                llm.invoke("hello")
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
            # Genuinely stops at 2/6: chunks 0 and 1 pulled, the rest never
            # consumed (a drain-then-raise implementation would show 6).
            assert pulled == [0, 1], f"drained stream: {pulled}"
        finally:
            cleanup_research(research_id)

    def test_rate_limited_stream_aborts_without_draining(self):
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )
        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        research_id = _rid()
        pulled = []

        def _gen():
            for i in range(6):
                pulled.append(i)
                yield AIMessageChunk(content=f"tok{i} ")
                if i == 0:
                    set_termination_flag(research_id)

        class _CountingLLM(_FakeStreamingLLM):
            def stream(self, prompt, *args, **kwargs):
                self.stream_calls += 1
                return _gen()

        wrapped = create_rate_limited_llm_wrapper(_CountingLLM(), "openai")
        wrapped._ldr_research_id = research_id
        try:
            seen = []
            try:
                for chunk in wrapped.stream("hello"):
                    seen.append(chunk)
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
            assert pulled == [0, 1], f"drained stream: {pulled}"
            assert len(seen) == 1
        finally:
            cleanup_research(research_id)


class TestBatchCancellation:
    def test_batch_returns_results_when_not_terminated(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(
            chunks=[AIMessageChunk(content="hi")],
        )
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        try:
            out = llm.batch(["a", "b"])
            assert len(out) == 2
            assert all(getattr(m, "content", "") == "hi" for m in out)
        finally:
            cleanup_research(research_id)

    def test_batch_propagates_termination_even_with_return_exceptions(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()
        llm = ProcessingLLMWrapper(
            _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")]),
            research_id=research_id,
        )
        set_termination_flag(research_id)
        try:
            try:
                llm.batch(["a", "b"], return_exceptions=True)
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError(
                    "ResearchTerminatedException must not be embedded "
                    "in result list under return_exceptions=True"
                )
        finally:
            cleanup_research(research_id)

    def test_abatch_propagates_termination_even_with_return_exceptions(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )

        research_id = _rid()

        async def _run():
            llm = ProcessingLLMWrapper(
                _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")]),
                research_id=research_id,
            )
            await llm.abatch(["a", "b"], return_exceptions=True)

        set_termination_flag(research_id)
        try:
            try:
                asyncio.run(_run())
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError(
                    "ResearchTerminatedException must not be embedded "
                    "in abatch result list under return_exceptions=True"
                )
        finally:
            cleanup_research(research_id)

    def test_rate_limited_batch_propagates_termination(self):
        from local_deep_research.exceptions import ResearchTerminatedException
        from local_deep_research.web.research_state import (
            cleanup_research,
            set_termination_flag,
        )
        from local_deep_research.web_search_engines.rate_limiting.llm.wrapper import (
            create_rate_limited_llm_wrapper,
        )

        research_id = _rid()
        wrapped = create_rate_limited_llm_wrapper(_FakeStreamingLLM(), "openai")
        wrapped._ldr_research_id = research_id
        set_termination_flag(research_id)
        try:
            try:
                wrapped.batch(["a"], return_exceptions=True)
            except ResearchTerminatedException:
                pass
            else:
                raise AssertionError("expected ResearchTerminatedException")
        finally:
            cleanup_research(research_id)


class TestStructuredOutputPreserved:
    """with_structured_output() must keep the dict/Pydantic contract."""

    def test_invoke_preserves_dict_output(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()

        class _DictRunnable:
            def invoke(self, prompt, *args, **kwargs):
                return {"answer": "x", "score": 1}

            def stream(self, prompt, *args, **kwargs):
                def _gen():
                    yield {"answer": "x"}

                return _gen()

            def with_structured_output(self, schema, **kwargs):
                return self

        base = _FakeStreamingLLM()
        base.with_structured_output = lambda schema, **kw: _DictRunnable()
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        try:
            structured = llm.with_structured_output(dict)
            out = structured.invoke("hello")
            assert out == {"answer": "x", "score": 1}, f"corrupted: {out!r}"
            assert not hasattr(out, "content")
        finally:
            cleanup_research(research_id)

    def test_ainvoke_preserves_dict_output(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()

        class _AsyncDictRunnable:
            async def ainvoke(self, prompt, *args, **kwargs):
                return {"answer": "y"}

            def invoke(self, prompt, *args, **kwargs):
                return {"answer": "y"}

            async def astream(self, prompt, *args, **kwargs):
                async def _gen():
                    yield {"answer": "y"}

                return _gen()

            def stream(self, prompt, *args, **kwargs):
                def _gen():
                    yield {"answer": "y"}

                return _gen()

        class _Base(_FakeStreamingLLM):
            def with_structured_output(self, schema, **kwargs):
                return _AsyncDictRunnable()

            async def ainvoke(self, prompt, *args, **kwargs):
                return {"answer": "y"}

        async def _run():
            llm = ProcessingLLMWrapper(_Base(), research_id=research_id)
            structured = llm.with_structured_output(dict)
            return await structured.ainvoke("hello")

        try:
            out = asyncio.run(_run())
            assert out == {"answer": "y"}, f"corrupted: {out!r}"
        finally:
            cleanup_research(research_id)

    def test_invoke_preserves_pydantic_output(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research
        from pydantic import BaseModel

        class _Answer(BaseModel):
            answer: str

        research_id = _rid()

        class _ModelRunnable:
            def invoke(self, prompt, *args, **kwargs):
                return _Answer(answer="z")

            def stream(self, prompt, *args, **kwargs):
                def _gen():
                    yield _Answer(answer="z")

                return _gen()

        class _Base(_FakeStreamingLLM):
            def with_structured_output(self, schema, **kwargs):
                return _ModelRunnable()

        llm = ProcessingLLMWrapper(_Base(), research_id=research_id)
        try:
            structured = llm.with_structured_output(_Answer)
            out = structured.invoke("hello")
            assert isinstance(out, _Answer), f"corrupted: {out!r}"
            assert out.answer == "z"
        finally:
            cleanup_research(research_id)


class TestStreamUsageGate:
    """Scoped invoke must not silently become streaming when that would
    zero out token/cost metrics (openai_endpoint without stream_usage)."""

    def test_openai_endpoint_without_usage_stays_on_direct_invoke(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")])
        llm = ProcessingLLMWrapper(
            base, research_id=research_id, provider="openai_endpoint"
        )
        try:
            out = llm.invoke("hello")
            assert getattr(out, "content", "") == "direct-invoke-response"
            assert base.invoke_calls == 1
            assert base.stream_calls == 0
        finally:
            cleanup_research(research_id)

    def test_openai_endpoint_with_model_opt_in_streams(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")])
        base.stream_usage = True
        llm = ProcessingLLMWrapper(
            base, research_id=research_id, provider="openai_endpoint"
        )
        try:
            out = llm.invoke("hello")
            assert getattr(out, "content", "") == "hi"
            assert base.stream_calls == 1
            assert base.invoke_calls == 0
        finally:
            cleanup_research(research_id)

    def test_other_providers_keep_streaming(self):
        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(chunks=[AIMessageChunk(content="hi")])
        llm = ProcessingLLMWrapper(
            base, research_id=research_id, provider="ollama"
        )
        try:
            out = llm.invoke("hello")
            assert getattr(out, "content", "") == "hi"
            assert base.stream_calls == 1
        finally:
            cleanup_research(research_id)


class TestMidStreamStructuredFallbackDoubleRequest:
    """Pin the defense-in-depth fallback: a structured chunk mid-stream
    falls back to direct invoke, which is a *second* upstream request for
    one logical call (token callbacks fire twice). No in-app path produces
    such mixed shapes (structured wrappers are excluded up front), so the
    implementation logs a warning instead of silently double-firing.

    NOTE: the package disables its own loguru namespace by default
    (``local_deep_research/__init__.py`` — the app re-enables it at
    startup), so these tests explicitly ``logger.enable`` around the
    invoke and restore the disabled default afterwards.
    """

    def test_sync_structured_after_message_chunk_warns_and_retries_once(
        self,
    ):
        from loguru import logger

        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()

        class _MixedLLM(_FakeStreamingLLM):
            def invoke(self, prompt, *args, **kwargs):
                self.invoke_calls += 1
                return {"answer": "fallback"}

            def stream(self, prompt, *args, **kwargs):
                self.stream_calls += 1

                def _gen():
                    yield AIMessageChunk(content="partial ")
                    yield {"answer": "fallback"}

                return _gen()

        base = _MixedLLM()
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        records = []
        sink = lambda msg: records.append(msg)  # noqa: E731
        handler = logger.add(sink, format="{message}", level="WARNING")
        logger.enable("local_deep_research")
        try:
            out = llm.invoke("hello")
            assert out == {"answer": "fallback"}
            # Exactly one stream attempt + one direct retry: no more, no less.
            assert base.stream_calls == 1
            assert base.invoke_calls == 1
            assert any("second upstream request" in str(r) for r in records), (
                f"missing double-request warning: {records!r}"
            )
        finally:
            logger.disable("local_deep_research")
            logger.remove(handler)
            cleanup_research(research_id)

    def test_async_structured_after_message_chunk_warns_and_retries_once(
        self,
    ):
        import asyncio

        from loguru import logger

        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()

        class _MixedAsyncLLM(_FakeStreamingLLM):
            def __init__(self):
                super().__init__()
                self.ainvoke_calls = 0
                self.astream_calls = 0

            async def ainvoke(self, prompt, *args, **kwargs):
                self.ainvoke_calls += 1
                return {"answer": "fallback"}

            def astream(self, prompt, *args, **kwargs):
                self.astream_calls += 1

                async def _gen():
                    yield AIMessageChunk(content="partial ")
                    yield {"answer": "fallback"}

                return _gen()

        async def _run():
            base = _MixedAsyncLLM()
            llm = ProcessingLLMWrapper(base, research_id=research_id)
            records = []
            sink = lambda msg: records.append(msg)  # noqa: E731
            handler = logger.add(sink, format="{message}", level="WARNING")
            logger.enable("local_deep_research")
            try:
                out = await llm.ainvoke("hello")
                assert out == {"answer": "fallback"}
                assert base.astream_calls == 1
                assert base.ainvoke_calls == 1
                assert any(
                    "second upstream request" in str(r) for r in records
                ), f"missing double-request warning: {records!r}"
            finally:
                logger.disable("local_deep_research")
                logger.remove(handler)
            return True

        try:
            assert asyncio.run(_run())
        finally:
            cleanup_research(research_id)

    def test_empty_stream_falls_back_without_warning(self):
        """Zero chunks (test doubles only — real LangChain models raise
        inside the generator) fall back silently to one direct invoke."""
        from loguru import logger

        from local_deep_research.config.llm_config import ProcessingLLMWrapper
        from local_deep_research.web.research_state import cleanup_research

        research_id = _rid()
        base = _FakeStreamingLLM(chunks=[])
        llm = ProcessingLLMWrapper(base, research_id=research_id)
        records = []
        sink = lambda msg: records.append(msg)  # noqa: E731
        handler = logger.add(sink, format="{message}", level="WARNING")
        logger.enable("local_deep_research")
        try:
            out = llm.invoke("hello")
            assert getattr(out, "content", "") == "direct-invoke-response"
            assert base.stream_calls == 1
            assert base.invoke_calls == 1
            assert not any(
                "second upstream request" in str(r) for r in records
            ), f"unexpected double-request warning: {records!r}"
        finally:
            logger.disable("local_deep_research")
            logger.remove(handler)
            cleanup_research(research_id)