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.1 KB
Raw Blame
"""Semantic ranking must sort on full precision and round only on output.

Ports the regression from #5448, whose Flask test lived in the deleted
``tests/web/routes/test_unified_search_routes.py``. That deletion is invisible
to git: #5448 landed on ``main`` after this branch diverged and modified a file
this branch deletes, so merging resolves delete-vs-modify in favour of the
delete and reverts the fix silently — no conflict, no failing test.

The bug: ``similarity`` was rounded to 3 decimals as each hit was collected,
and the cross-collection merge then sorted on the rounded value. 0.90041 and
0.90049 both become 0.900, the sort sees a tie, and the winner is decided by
fetch order rather than by score. The response must still carry the rounded
value — the rounding moves to the response boundary, after ranking.

The DB and the collection engine are stubbed because the assertion is about
ordering arithmetic, not persistence; ``test_unified_search_router.py`` covers
the live-DB contracts for these endpoints.
"""

import sys
import types

import pytest
from fastapi.testclient import TestClient

# Distinct at full precision, identical once rounded to 3 decimals.
LOWER = 0.90041
HIGHER = 0.90049


class _Chain:
    """Minimal stand-in for a SQLAlchemy query chain."""

    def __init__(self, rows):
        self._rows = rows

    def filter(self, *args, **kwargs):
        return self

    def join(self, *args, **kwargs):
        return self

    def all(self):
        return self._rows


class _FakeSession:
    """Answers the handler's queries in the order it issues them."""

    def __init__(self, script):
        self._script = list(script)

    def query(self, *args, **kwargs):
        return _Chain(self._script.pop(0) if self._script else [])


class _FakeSessionCtx:
    def __init__(self, script):
        self._script = script

    def __enter__(self):
        return _FakeSession(self._script)

    def __exit__(self, *exc):
        return False


class _FakeEngine:
    """Returns the lower score first.

    Fetch order is deliberately the opposite of correct rank order, so a sort
    that cannot distinguish the two scores leaves "lower" in front.
    """

    def __init__(self, **kwargs):
        pass

    def search(self, query, limit=None):
        return [
            {
                "relevance_score": LOWER,
                "title": "lower",
                "snippet": "s",
                "metadata": {"document_id": "lower"},
            },
            {
                "relevance_score": HIGHER,
                "title": "higher",
                "snippet": "s",
                "metadata": {"document_id": "higher"},
            },
        ]


@pytest.fixture
def ranking_client(monkeypatch):
    from local_deep_research.database import session_context
    from local_deep_research.web.dependencies.auth import require_auth
    from local_deep_research.web.fastapi_app import app

    # The handler opens a session twice: once to resolve collections (two
    # queries: collections, then current RAG index names), once to enrich
    # candidates with their Document rows.
    opened = {"n": 0}

    def _fake_session(*args, **kwargs):
        opened["n"] += 1
        if opened["n"] == 1:
            return _FakeSessionCtx([[("c1", "Library", "default_library")], []])
        return _FakeSessionCtx(
            [[("higher", "note", None), ("lower", "note", None)]]
        )

    monkeypatch.setattr(session_context, "get_user_db_session", _fake_session)

    # The handler imports the engine inside the function body, so replacing
    # the source module is what takes effect.
    engine_mod_name = (
        "local_deep_research.web_search_engines.engines."
        "search_engine_collection"
    )
    stub = types.ModuleType(engine_mod_name)
    stub.CollectionSearchEngine = _FakeEngine
    monkeypatch.setitem(sys.modules, engine_mod_name, stub)

    app.dependency_overrides[require_auth] = lambda: "ranking_probe"
    try:
        yield TestClient(app, raise_server_exceptions=False)
    finally:
        app.dependency_overrides.pop(require_auth, None)


def test_ranks_on_full_precision(ranking_client):
    resp = ranking_client.get("/library/search/api/semantic?q=precision")
    assert resp.status_code == 200, resp.text[:300]
    body = resp.json()
    assert body["success"] is True, body

    ids = [row["id"] for row in body["results"]]
    assert ids == ["higher", "lower"], (
        f"semantic results ranked {ids}; expected the higher raw score "
        f"({HIGHER}) first. Rounding before the sort collapses {LOWER} and "
        f"{HIGHER} to a tie, so fetch order decides instead of score (#5448)."
    )


def test_response_still_rounds_to_three_decimals(ranking_client):
    resp = ranking_client.get("/library/search/api/semantic?q=precision")
    assert resp.status_code == 200, resp.text[:300]

    sims = [row["similarity"] for row in resp.json()["results"]]
    assert sims == [0.9, 0.9], (
        f"response carried {sims}; the API contract is 3-decimal rounding at "
        f"the boundary, so both hits must serialise as 0.9 even though they "
        f"rank differently. Dropping the rounding would leak raw float noise "
        f"into the payload."
    )