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.

6.6 KB
Raw Blame
"""reconcile_orphan_active_research must NOT fail research whose thread is alive.

It runs on every login. A user opening a second tab / logging in on another
device while a research is still running would, without a liveness guard, have
that live research wrongly flagged FAILED (and then delete_attempt could
hard-delete it out from under the running worker). Only genuinely dead-thread
rows (e.g. after a server restart) should be reconciled — matching the
is_research_thread_alive() guard chat.py and the research-start reclaim use.

A brand-new research has no live thread either, between the commit of its
claim and its worker's registration. The login step therefore reconciles
under the same per-user admission gate that a start holds across that gap.
"""

import threading

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


@pytest.fixture
def session():
    from local_deep_research.database.models import Base

    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    s = sessionmaker(bind=engine)()
    yield s
    s.close()


def _seed(session, username, research_id):
    from local_deep_research.database.models.active_research import (
        UserActiveResearch,
    )
    from local_deep_research.database.models import ResearchHistory
    from local_deep_research.constants import ResearchStatus

    session.add(
        UserActiveResearch(
            username=username,
            research_id=research_id,
            status=ResearchStatus.IN_PROGRESS,
        )
    )
    session.add(
        ResearchHistory(
            id=research_id,
            query="q",
            mode="quick",
            status=ResearchStatus.IN_PROGRESS.value,
            created_at="2026-01-01T00:00:00+00:00",
        )
    )
    session.commit()


def test_live_thread_research_is_not_failed(session):
    from local_deep_research.web.queue.processor_v2 import queue_processor
    from local_deep_research import web
    from local_deep_research.web import research_state
    from local_deep_research.database.models import ResearchHistory
    from local_deep_research.constants import ResearchStatus

    _ = web  # keep import
    live_id = "11111111-1111-1111-1111-111111111111"
    _seed(session, "alice", live_id)

    # Register a genuinely-alive worker thread for this research.
    stop = threading.Event()
    t = threading.Thread(target=stop.wait, daemon=True)
    t.start()
    research_state.set_active_research(live_id, {"thread": t, "progress": 10})
    try:
        n = queue_processor.reconcile_orphan_active_research("alice", session)
        assert n == 0, "a live-thread research must not be reconciled"
        research = session.query(ResearchHistory).filter_by(id=live_id).first()
        assert research.status == ResearchStatus.IN_PROGRESS.value
    finally:
        stop.set()
        t.join(timeout=2)
        with research_state._lock:
            research_state._active_research.pop(live_id, None)


def test_dead_thread_research_is_failed(session):
    from local_deep_research.web.queue.processor_v2 import queue_processor
    from local_deep_research.web import research_state
    from local_deep_research.database.models import ResearchHistory
    from local_deep_research.constants import ResearchStatus

    dead_id = "22222222-2222-2222-2222-222222222222"
    _seed(session, "bob", dead_id)
    # Not registered in _active_research -> is_research_thread_alive == False.
    with research_state._lock:
        research_state._active_research.pop(dead_id, None)

    n = queue_processor.reconcile_orphan_active_research("bob", session)
    assert n == 1, "a dead/unregistered research must be reconciled"
    research = session.query(ResearchHistory).filter_by(id=dead_id).first()
    assert research.status == ResearchStatus.FAILED


def test_login_reconcile_runs_under_the_admission_gate():
    """Post-login reconciliation must hold the per-user admission gate.

    A start keeps that gate from its claim commit until its worker
    registers. In that gap the claim has no live thread yet, so a reconcile
    that ran without the gate would fail it as a restart orphan. The stub
    reconcile below runs a non-blocking acquire of the same gate from
    another thread, which must fail. Dropping the
    ``with user_research_start_gate(username)`` block in
    ``_perform_post_login_tasks_body`` (web/routers/auth.py) lets it
    succeed.
    """
    import uuid
    from contextlib import contextmanager
    from unittest.mock import MagicMock, patch

    from local_deep_research.web.research_state import (
        get_user_research_start_lock,
    )
    from local_deep_research.web.routers import auth as auth_module

    username = f"login_gate_{uuid.uuid4().hex[:8]}"
    gate = get_user_research_start_lock(username)
    user_db = MagicMock(name="user_db")
    reconciled = []
    competing_acquisitions = []

    @contextmanager
    def user_db_session(*_args, **_kwargs):
        yield user_db

    def compete_for_gate():
        acquired = gate.acquire(blocking=False)
        competing_acquisitions.append(acquired)
        if acquired:
            gate.release()

    def reconcile(name, db_session):
        reconciled.append((name, db_session))
        contender = threading.Thread(target=compete_for_gate)
        contender.start()
        contender.join(timeout=10)
        return 0

    # Steps 1 and 4 see a current settings version and backups disabled, so
    # they do nothing. Step 3's auth DB fails fast inside its own handler.
    settings_manager = MagicMock()
    settings_manager.db_version_matches_package.return_value = True
    settings_manager.get_setting.return_value = False

    with (
        patch(
            "local_deep_research.database.session_context.get_user_db_session",
            user_db_session,
        ),
        patch(
            "local_deep_research.settings.manager.SettingsManager",
            return_value=settings_manager,
        ),
        patch(
            "local_deep_research.database.library_init.initialize_library_for_user",
            return_value={"success": True},
        ),
        patch.object(
            auth_module,
            "auth_db_session",
            side_effect=RuntimeError("auth db unavailable"),
        ),
        patch(
            "local_deep_research.web.queue.processor_v2.queue_processor"
        ) as queue_processor,
    ):
        queue_processor.reconcile_orphan_active_research.side_effect = reconcile
        auth_module._perform_post_login_tasks_body(username, "pw", "sess-1")

    # The step swallows its own errors, so first prove the reconcile ran.
    assert reconciled == [(username, user_db)]
    assert competing_acquisitions == [False]
    assert not gate.locked()