~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Regression fences for ``web/routers/benchmark.py`` defaults.
Fence 1 — ``llm.local_context_window_size`` default:
``start_benchmark`` (POST /benchmark/api/start) and
``start_benchmark_simple`` (POST /benchmark/api/start-simple) must
resolve ``llm.local_context_window_size`` with a default of **8192**.
During the FastAPI migration review this default briefly drifted to
4096, silently halving the local-model context window for benchmark
runs on databases without an explicit value. These tests assert both
the exact ``get_setting("llm.local_context_window_size", 8192)`` call
and the value that lands in the ``search_config`` handed to
``benchmark_service.create_benchmark_run``.
Fence 2 — ``get_benchmark_results`` persistence_error omission:
When ``benchmark_service.get_result_persistence_error`` returns
``None``, the response payload must NOT contain a
``persistence_error`` key. The present-case (and the omit-case with
an empty result set) live in
``tests/benchmarks/web_api/test_benchmark_results_persistence_error.py``;
the test here is complementary: it fences omission when the run HAS
formatted result rows, and pins the service-call arguments
(``sync_pending_results(run_id, username)`` /
``get_result_persistence_error(run_id)``).
Follows the FastAPI TestClient idiom of
``tests/benchmarks/web_api/test_benchmark_results_persistence_error.py``:
``require_auth`` is dependency-overridden and DB/session/service
boundaries are patched; everything between the HTTP request and the
service singleton is real router code.
"""
from contextlib import contextmanager
from types import SimpleNamespace
from unittest.mock import MagicMock, call, patch
import pytest
from fastapi.testclient import TestClient
TEST_USERNAME = "testuser"
def _make_app():
"""Return the real FastAPI app with ``require_auth`` overridden so
route bodies execute as ``testuser`` without a real login/DB."""
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.dependencies.auth import require_auth
app.dependency_overrides[require_auth] = lambda: TEST_USERNAME
return app
@pytest.fixture(autouse=True)
def _clear_overrides():
"""Don't leak the ``require_auth`` override into other test modules
sharing the module-level FastAPI app."""
yield
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.dependencies.auth import require_auth
app.dependency_overrides.pop(require_auth, None)
def _client(app):
return TestClient(app, raise_server_exceptions=False)
def _fake_settings_manager(overrides=None):
"""SettingsManager stand-in whose ``get_setting`` behaves like a real
one over a DB where only ``overrides`` keys are set: any other key
resolves to whatever default the *caller* passed. This makes the
tests sensitive to the default literal in the router — if the route
drifts back to 4096, the constructed config changes and the fence
fails."""
overrides = overrides or {}
manager = MagicMock(name="settings_manager")
def _get_setting(key, default=None, *args, **kwargs):
return overrides.get(key, default)
manager.get_setting.side_effect = _get_setting
return manager
@contextmanager
def _patch_start_deps(settings_manager):
"""Patch the true boundaries of both start endpoints.
``_start_benchmark_sync`` imports ``get_user_db_session`` and
``SettingsManager`` locally from their source modules, while
``_start_benchmark_simple_sync`` uses the module-level imports in
``routers.benchmark`` — so both import sites are patched. The
``benchmark_service`` singleton is replaced so no real benchmark
threads/LLM calls start. Yields the mock service for config
inspection.
"""
mock_db = MagicMock(name="db_session")
mock_svc = MagicMock(name="benchmark_service")
mock_svc.create_benchmark_run.return_value = 42
mock_svc.start_benchmark.return_value = True
@contextmanager
def _session_ctx(*args, **kwargs):
yield mock_db
settings_manager_cls = MagicMock(return_value=settings_manager)
with (
patch(
"local_deep_research.database.session_context.get_user_db_session",
side_effect=_session_ctx,
),
patch(
"local_deep_research.web.routers.benchmark.get_user_db_session",
side_effect=_session_ctx,
),
patch(
"local_deep_research.settings.SettingsManager",
settings_manager_cls,
),
patch(
"local_deep_research.web.routers.benchmark.SettingsManager",
settings_manager_cls,
),
patch(
"local_deep_research.web.routers.benchmark.benchmark_service",
mock_svc,
),
):
yield mock_svc
_START_ENDPOINTS = [
"/benchmark/api/start",
"/benchmark/api/start-simple",
]
_START_BODY = {
"run_name": "fence-run",
"datasets_config": {"simpleqa": {"count": 1}},
}
def _post_start(app, endpoint, xff):
"""POST a minimal valid benchmark start request.
Fetches a session-bound CSRF token first (same client, so the
session cookie carries over) and sends it as X-CSRFToken — the
start routes sit behind CSRFMiddleware.
A unique X-Forwarded-For per test keeps each test in its own
rate-limit bucket (the limiter trusts XFF from the TestClient peer),
so the tight "3 per minute" cap on these routes can never bleed
across tests or modules.
"""
client = _client(app)
headers = {"X-Forwarded-For": xff}
token_resp = client.get("/auth/csrf-token", headers=headers)
assert token_resp.status_code == 200, token_resp.text
headers["X-CSRFToken"] = token_resp.json()["csrf_token"]
return client.post(endpoint, json=_START_BODY, headers=headers)
class TestLocalContextWindowDefault8192:
"""Fence 1: the 8192 default for llm.local_context_window_size."""
@pytest.mark.parametrize("endpoint", _START_ENDPOINTS)
def test_start_resolves_local_context_window_with_default_8192(
self, endpoint
):
"""With no DB value set, the route must ask the settings manager
for ``llm.local_context_window_size`` with default 8192 and put
8192 into the search_config for ``create_benchmark_run``."""
app = _make_app()
settings_manager = _fake_settings_manager()
with _patch_start_deps(settings_manager) as mock_svc:
resp = _post_start(app, endpoint, xff=f"10.91.{len(endpoint)}.1")
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["success"] is True
assert body["benchmark_run_id"] == 42
# The exact call — this is the literal that drifted to 4096.
assert (
call("llm.local_context_window_size", 8192)
in settings_manager.get_setting.call_args_list
), (
"route no longer resolves llm.local_context_window_size "
"with default 8192: "
f"{settings_manager.get_setting.call_args_list}"
)
# And the resolved default must flow into the run's search config.
mock_svc.create_benchmark_run.assert_called_once()
search_config = mock_svc.create_benchmark_run.call_args.kwargs[
"search_config"
]
assert search_config["local_context_window_size"] == 8192
@pytest.mark.parametrize("endpoint", _START_ENDPOINTS)
def test_start_prefers_db_value_over_default(self, endpoint):
"""An explicit DB setting must win over the 8192 default — proves
the value is read via the settings manager, not hard-coded."""
app = _make_app()
settings_manager = _fake_settings_manager(
overrides={"llm.local_context_window_size": 3123}
)
with _patch_start_deps(settings_manager) as mock_svc:
resp = _post_start(app, endpoint, xff=f"10.92.{len(endpoint)}.1")
assert resp.status_code == 200, resp.text
assert resp.json()["success"] is True
search_config = mock_svc.create_benchmark_run.call_args.kwargs[
"search_config"
]
assert search_config["local_context_window_size"] == 3123
# ---------------------------------------------------------------------------
# Fence 2: persistence_error key omitted when the service reports None
# ---------------------------------------------------------------------------
def _fake_benchmark_result_row():
"""A minimal BenchmarkResult stand-in with only the attributes the
formatting loop reads. ``research_id=None`` keeps the SearchCall
metrics query out of play."""
return SimpleNamespace(
example_id="ex-1",
dataset_type=SimpleNamespace(value="simpleqa"),
question="What is 2+2?",
correct_answer="4",
extracted_answer="4",
response="The answer is 4.",
is_correct=True,
confidence=100,
grader_response="correct",
processing_time=1.5,
sources=None,
completed_at=None,
research_id=None,
)
def _results_query_router(rows):
"""Side-effect for ``mock_db.query`` routing by model class (same
idiom as test_benchmark_results_persistence_error.py)."""
def _route(model, *args):
chain = MagicMock()
chain.filter.return_value = chain
chain.order_by.return_value = chain
chain.limit.return_value = chain
if "SearchCall" in getattr(model, "__name__", ""):
chain.all.return_value = []
else:
chain.all.return_value = rows
return chain
return _route
class TestResultsOmitPersistenceErrorWithRows:
def test_populated_results_omit_persistence_error_key(self):
"""When the service reports no persistence error, the payload for
a run WITH result rows must carry the formatted rows and no
``persistence_error`` key at all (not even ``null``)."""
app = _make_app()
mock_db = MagicMock(name="db_session")
mock_db.query.side_effect = _results_query_router(
[_fake_benchmark_result_row()]
)
mock_svc = MagicMock(name="benchmark_service")
mock_svc.get_result_persistence_error.return_value = None
@contextmanager
def _session_ctx(*args, **kwargs):
yield mock_db
with (
patch(
"local_deep_research.database.session_context.get_user_db_session",
side_effect=_session_ctx,
),
patch(
"local_deep_research.web.routers.benchmark.benchmark_service",
mock_svc,
),
):
resp = _client(app).get("/benchmark/api/results/7")
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["success"] is True
assert len(body["results"]) == 1
assert body["results"][0]["example_id"] == "ex-1"
assert body["results"][0]["is_correct"] is True
assert "persistence_error" not in body
# Pin the service-call contract the route relies on.
mock_svc.sync_pending_results.assert_called_once_with(7, TEST_USERNAME)
# Takes the username too: active_runs is keyed by (username, run_id)
# because a BenchmarkRun.id is only unique within one user's database
# (ADR-0009), so the id alone cannot find the entry.
mock_svc.get_result_persistence_error.assert_called_once_with(
7, TEST_USERNAME
)