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.

5.2 KB
Raw Blame
"""A finished research must not leave its ``UserActiveResearch`` row behind.

``UserActiveResearch`` counts what a user currently has running, and is the
input to the per-user concurrency cap. Main deleted finished rows from a
``before_request`` hook — ``web/auth/cleanup_middleware`` — which sampled ~1%
of requests, walked the user's rows, and dropped any whose thread was no
longer active. That whole module has no successor under FastAPI.

Nothing replaced its *delete*. The spawn-failure branches drop their own row,
so the gap is invisible on every path that errors; it is only the paths that
*succeed* that leak. The concurrency cap itself still self-heals, because
``reclaim_stale_user_active_research`` re-derives the count at the user's next
start — which is exactly why this went unnoticed: the symptom is not a wrong
cap, it is rows accumulating forever, and the ones that do get reclaimed being
relabelled FAILED despite having succeeded.

These tests pin the delete at both terminal notifications. They deliberately
assert on the DB row rather than on the in-memory ``_active_research`` dict:
``cleanup_research`` already clears the dict, and it was that partial cleanup
that made the path look complete.
"""

from unittest.mock import MagicMock, patch

from local_deep_research.database.models import UserActiveResearch
from local_deep_research.web.queue.processor_v2 import QueueProcessorV2


class _FakeQuery:
    """Minimal stand-in for the SQLAlchemy query chain used by the helper."""

    def __init__(self, recorder):
        self._recorder = recorder

    def filter_by(self, **kwargs):
        self._recorder["filter_by"] = kwargs
        return self

    def delete(self, synchronize_session=False):
        self._recorder["deleted"] = True
        self._recorder["synchronize_session"] = synchronize_session
        return self._recorder.get("rowcount", 1)


class _FakeSession:
    def __init__(self, rowcount=1):
        self.recorder = {"rowcount": rowcount}
        self.committed = False

    def query(self, model):
        self.recorder["model"] = model
        return _FakeQuery(self.recorder)

    def commit(self):
        self.committed = True


class TestDropActiveResearchRow:
    def test_deletes_the_row_scoped_to_user_and_research(self):
        session = _FakeSession()

        QueueProcessorV2._drop_active_research_row(session, "alice", "r-1")

        assert session.recorder["model"] is UserActiveResearch
        # Scoped by BOTH username and research_id: research ids are unique
        # per user's own database, so a bare research_id filter would be a
        # cross-user key (see ADR-0009).
        assert session.recorder["filter_by"] == {
            "username": "alice",
            "research_id": "r-1",
        }
        assert session.recorder["deleted"] is True

    def test_commits_because_the_session_context_does_not(self):
        """``get_user_db_session`` commits nothing on exit — it only rolls
        back on exception. An uncommitted delete would sit pending on the
        thread-local session and quietly never land."""
        session = _FakeSession()

        QueueProcessorV2._drop_active_research_row(session, "alice", "r-1")

        assert session.committed is True

    def test_no_commit_when_no_row_matched(self):
        session = _FakeSession(rowcount=0)

        QueueProcessorV2._drop_active_research_row(session, "alice", "r-1")

        assert session.committed is False

    def test_bookkeeping_failure_never_breaks_the_notification(self):
        """The user is waiting on the completion notification; a failure to
        tidy a counter row must not take it down."""
        session = _FakeSession()
        session.query = MagicMock(side_effect=RuntimeError("db gone"))

        QueueProcessorV2._drop_active_research_row(session, "alice", "r-1")


class TestTerminalNotificationsDropTheRow:
    """Both terminal paths must call the helper. Neither did."""

    def _run(self, method_name, **kwargs):
        processor = QueueProcessorV2()
        session = MagicMock()
        ctx = MagicMock()
        ctx.__enter__.return_value = session
        ctx.__exit__.return_value = False

        target = "local_deep_research.web.queue.processor_v2"
        with (
            patch(f"{target}.get_user_db_session", return_value=ctx),
            patch(f"{target}.UserQueueService"),
            patch.object(QueueProcessorV2, "_drop_active_research_row") as drop,
            patch(
                f"{target}.send_research_completed_notification_from_session"
            ),
            patch(f"{target}.send_research_failed_notification_from_session"),
            patch(
                "local_deep_research.research_library.search.services"
                ".research_history_indexer.auto_convert_research"
            ),
        ):
            getattr(processor, method_name)(
                username="alice", research_id="r-1", **kwargs
            )
        return drop, session

    def test_completion_drops_the_row(self):
        drop, session = self._run("notify_research_completed")
        drop.assert_called_once_with(session, "alice", "r-1")

    def test_failure_drops_the_row(self):
        drop, session = self._run(
            "notify_research_failed", error_message="boom"
        )
        drop.assert_called_once_with(session, "alice", "r-1")