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.

32.7 KB
Raw Blame
"""Behavioural contracts for the benchmark SERVICE layer.

Scope: ``local_deep_research.benchmarks.web_api.benchmark_service``, the
machinery beneath ``web/routers/benchmark.py``. The router surface (status
codes, run-id coercion, rate-limit buckets) is covered elsewhere and is not
retested here.

These tests drive the real service against **real on-disk SQLite databases**,
one file per user — the production shape, where ``BenchmarkRun.id``
autoincrements independently inside each user's own encrypted database and
every user's first run is therefore id ``1``. ``:memory:`` is deliberately
avoided: it is per-connection, so the multi-user isolation tests would pass
vacuously. ``PRAGMA foreign_keys = ON`` mirrors production
(``sqlcipher_utils.apply_performance_pragmas`` and
``EncryptedDatabaseManager._apply_pragmas`` both set it), so a result row
written for a run that no longer exists is rejected rather than silently
orphaned.

The only stubs are the execution boundary — ``load_dataset``,
``quick_summary`` and ``grade_single_result`` — so no LLM, search engine or
dataset download is ever touched. ``_create_task_queue``,
``_process_benchmark_task``, ``_run_benchmark_thread``,
``_persist_unsaved_results``, ``sync_pending_results``,
``_sync_results_to_database``, ``cancel_benchmark``, ``start_benchmark`` and
``get_benchmark_status`` all run for real.
"""

import threading
import time
from contextlib import contextmanager
from unittest.mock import Mock, patch

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.exc import OperationalError
from sqlalchemy.orm import Session
from sqlalchemy.pool import NullPool

from local_deep_research.benchmarks.web_api.benchmark_service import (
    _RESULT_PERSISTENCE_ERROR,
    BenchmarkService,
)
from local_deep_research.database.models.benchmark import (
    BenchmarkProgress,
    BenchmarkResult,
    BenchmarkRun,
    BenchmarkStatus,
)

SVC = "local_deep_research.benchmarks.web_api.benchmark_service"
SESSION_CTX = "local_deep_research.database.session_context"

ALICE = "alice"
BOB = "bob"

_EXAMPLES = [
    {"id": "ex-1", "problem": "First question?", "answer": "one"},
    {"id": "ex-2", "problem": "Second question?", "answer": "two"},
    {"id": "ex-3", "problem": "Third question?", "answer": "three"},
]


# --------------------------------------------------------------------------
# Real per-user on-disk SQLite databases
# --------------------------------------------------------------------------


def _fk_on(dbapi_connection, _record):
    dbapi_connection.execute("PRAGMA foreign_keys = ON")


class UserDatabases:
    """One real on-disk SQLite file per username, as production has."""

    def __init__(self, root):
        self.root = root
        self._engines = {}
        self._commit_failures = {}

    def engine(self, username):
        if username not in self._engines:
            path = self.root / f"{username}.sqlite"
            engine = create_engine(
                f"sqlite:///{path}",
                poolclass=NullPool,
                connect_args={"check_same_thread": False, "timeout": 30},
            )
            event.listen(engine, "connect", _fk_on)
            for table in (
                BenchmarkRun.__table__,
                BenchmarkResult.__table__,
                BenchmarkProgress.__table__,
            ):
                table.create(bind=engine, checkfirst=True)
            self._engines[username] = engine
        return self._engines[username]

    def fail_commits(self, username, times=1):
        """Arm ``times`` commit failures for sessions opened for ``username``."""
        self._commit_failures[username] = times

    def open(self, username):
        """A plain session for test assertions (never commit-poisoned)."""
        return Session(bind=self.engine(username))

    @contextmanager
    def session_cm(self, username=None, password=None, session_id=None):
        """Stand-in for ``get_user_db_session``.

        Same contract the service relies on: a username selects the database,
        and the session is scoped to the ``with`` block.
        """
        assert username, "service must always name the user whose DB it opens"
        session = Session(bind=self.engine(username))
        remaining = self._commit_failures.get(username, 0)
        if remaining:
            self._commit_failures[username] = remaining - 1

            def _boom():
                raise OperationalError(
                    "COMMIT", {}, Exception("disk I/O error")
                )

            session.commit = _boom
        try:
            yield session
        finally:
            session.close()

    def dispose(self):
        for engine in self._engines.values():
            engine.dispose()


@pytest.fixture
def dbs(tmp_path):
    store = UserDatabases(tmp_path)
    with patch(
        f"{SESSION_CTX}.get_user_db_session",
        store.session_cm,
    ):
        yield store
    store.dispose()


@pytest.fixture
def service():
    return BenchmarkService(socket_service=Mock())


# --------------------------------------------------------------------------
# Execution-boundary stubs (no LLM, no search, no dataset download)
# --------------------------------------------------------------------------


class Execution:
    """Records what the (stubbed) research/grading boundary was asked to do."""

    def __init__(self):
        self.queries = []
        self.graded = []
        self.concurrent = 0
        self.max_concurrent = 0
        self._lock = threading.Lock()


@contextmanager
def stub_execution(
    on_research=None, research_error=None, grade=None, examples=_EXAMPLES
):
    exec_log = Execution()

    def fake_load_dataset(dataset_type, num_examples, seed=None):
        return [dict(e) for e in examples[:num_examples]]

    def fake_quick_summary(**kwargs):
        with exec_log._lock:
            exec_log.queries.append(kwargs["query"])
            exec_log.concurrent += 1
            exec_log.max_concurrent = max(
                exec_log.max_concurrent, exec_log.concurrent
            )
            call_index = len(exec_log.queries)
        try:
            if on_research is not None:
                on_research(call_index, kwargs)
            if research_error is not None:
                raise RuntimeError(research_error)
            return {"summary": "Answer: stubbed", "sources": []}
        finally:
            with exec_log._lock:
                exec_log.concurrent -= 1

    def fake_grade(result_data, dataset_type, evaluation_config, snapshot):
        exec_log.graded.append(result_data["id"])
        if grade is not None:
            return grade(result_data)
        return {
            "is_correct": True,
            "graded_confidence": "95",
            "grader_response": "matches",
        }

    with (
        patch(f"{SVC}.load_dataset", fake_load_dataset),
        patch(f"{SVC}.quick_summary", fake_quick_summary),
        patch(f"{SVC}.grade_single_result", fake_grade),
    ):
        yield exec_log


# --------------------------------------------------------------------------
# Helpers
# --------------------------------------------------------------------------


def create_run(service, username, run_name, count=3):
    return service.create_benchmark_run(
        run_name=run_name,
        search_config={
            "iterations": 1,
            "questions_per_iteration": 1,
            "search_tool": "searxng",
            "model_name": "stub-model",
            "provider": "ollama",
        },
        evaluation_config={"provider": "ollama", "model_name": "stub-grader"},
        datasets_config={"simpleqa": {"count": count, "seed": 7}},
        username=username,
    )


@contextmanager
def _settings_stubbed():
    with patch("local_deep_research.settings.SettingsManager") as manager:
        manager.return_value.get_all_settings.return_value = {}
        yield manager


def start_only(service, run_id, username):
    """Run the real ``start_benchmark`` prelude; do not run the worker body.

    ``_run_benchmark_thread`` is replaced for the duration of the call, so the
    spawned thread is inert. Everything else — the DB status transition, the
    provenance write, the ``active_runs`` entry shape — is production code.
    """
    with (
        _settings_stubbed(),
        patch.object(service, "_run_benchmark_thread", Mock()),
    ):
        return service.start_benchmark(run_id, username=username)


def start_and_run(service, run_id, username):
    """Start a run, then execute the real worker body synchronously."""
    assert start_only(service, run_id, username) is True
    service._run_benchmark_thread(username, run_id)


def start_then_run_with_failing_sync(service, dbs, run_id, username):
    """Start for real, then make the worker's terminal DB write fail.

    The failure is armed only after ``start_benchmark`` has committed its own
    IN_PROGRESS transition, so the single commit it hits is the terminal
    result sync.
    """
    assert start_only(service, run_id, username) is True
    dbs.fail_commits(username, times=1)
    service._run_benchmark_thread(username, run_id)


def run_row(dbs, username, run_id=1):
    with dbs.open(username) as session:
        row = session.get(BenchmarkRun, run_id)
        if row is None:
            return None
        return {
            "run_name": row.run_name,
            "status": row.status,
            "end_time": row.end_time,
            "completed_examples": row.completed_examples,
            "failed_examples": row.failed_examples,
            "overall_accuracy": row.overall_accuracy,
            "error_message": row.error_message,
        }


def result_rows(dbs, username):
    with dbs.open(username) as session:
        return [
            {
                "run_id": r.benchmark_run_id,
                "example_id": r.example_id,
                "query_hash": r.query_hash,
                "is_correct": r.is_correct,
                "research_error": r.research_error,
            }
            for r in session.query(BenchmarkResult)
            .order_by(BenchmarkResult.id)
            .all()
        ]


# ==========================================================================
# A. Per-user recording and cross-user isolation
# ==========================================================================


def test_two_users_first_runs_share_id_one_but_not_data(service, dbs):
    """Both users' first run is id 1; each reads only its own row."""
    alice_id = create_run(service, ALICE, "alice-only")
    bob_id = create_run(service, BOB, "bob-only")

    # The collision is the whole point: ids are unique per user DB, not global.
    assert alice_id == bob_id == 1

    alice_status = service.get_benchmark_status(1, username=ALICE)
    bob_status = service.get_benchmark_status(1, username=BOB)
    assert alice_status["run_name"] == "alice-only"
    assert bob_status["run_name"] == "bob-only"

    # And a run id that exists only for alice is invisible to bob.
    second_alice = create_run(service, ALICE, "alice-second")
    assert second_alice == 2
    assert service.get_benchmark_status(2, username=ALICE) is not None
    assert service.get_benchmark_status(2, username=BOB) is None


def test_cancelling_one_users_run_leaves_the_other_users_same_id_alone(
    service, dbs
):
    create_run(service, ALICE, "alice-only")
    create_run(service, BOB, "bob-only")
    service.update_benchmark_status(
        1, BenchmarkStatus.IN_PROGRESS, username=ALICE
    )
    service.update_benchmark_status(
        1, BenchmarkStatus.IN_PROGRESS, username=BOB
    )

    assert service.cancel_benchmark(1, username=BOB) is True

    assert run_row(dbs, BOB)["status"] is BenchmarkStatus.CANCELLED
    # Seeded second user's row is what this would fail against.
    assert run_row(dbs, ALICE)["status"] is BenchmarkStatus.IN_PROGRESS


def test_active_runs_entries_do_not_collide_across_users(service, dbs):
    """In-memory run state is keyed by (username, run_id), not run_id."""
    create_run(service, ALICE, "alice-only")
    create_run(service, BOB, "bob-only")
    assert start_only(service, 1, ALICE) is True
    assert start_only(service, 1, BOB) is True

    assert set(service.active_runs) == {(ALICE, 1), (BOB, 1)}
    assert service.active_runs[(ALICE, 1)]["data"]["username"] == ALICE
    assert service.active_runs[(BOB, 1)]["data"]["username"] == BOB

    service.cancel_benchmark(1, username=BOB)
    assert service.active_runs[(BOB, 1)]["status"] == "cancelled"
    # Alice's identically-numbered run must be untouched in memory too.
    assert service.active_runs[(ALICE, 1)]["status"] == "running"


def test_results_are_written_only_to_the_owning_users_database(service, dbs):
    """A run's results land in its own user's DB and nowhere else."""
    create_run(service, ALICE, "alice-only")
    create_run(service, BOB, "bob-only")

    with stub_execution(examples=[_EXAMPLES[0]]):
        start_and_run(service, 1, ALICE)
    with stub_execution(examples=[_EXAMPLES[1]]):
        start_and_run(service, 1, BOB)

    assert [r["example_id"] for r in result_rows(dbs, ALICE)] == ["ex-1"]
    assert [r["example_id"] for r in result_rows(dbs, BOB)] == ["ex-2"]


def test_persistence_failure_flag_is_scoped_to_the_failing_user(service, dbs):
    """Alice's write failure must not be reported to bob, or vice versa."""
    create_run(service, ALICE, "alice-only")
    create_run(service, BOB, "bob-only")

    with stub_execution(examples=[_EXAMPLES[0]]):
        start_then_run_with_failing_sync(service, dbs, 1, ALICE)
    with stub_execution(examples=[_EXAMPLES[1]]):
        start_and_run(service, 1, BOB)

    alice_err = service.get_result_persistence_error(1, ALICE)
    assert alice_err == _RESULT_PERSISTENCE_ERROR
    assert alice_err["code"] == "database_write_failed"
    # Bob's identically-numbered run completed cleanly and must stay clean.
    assert service.get_result_persistence_error(1, BOB) is None
    assert result_rows(dbs, BOB) != []
    assert result_rows(dbs, ALICE) == []


def test_persistence_error_payload_cannot_be_mutated_by_a_caller(service, dbs):
    create_run(service, ALICE, "alice-only")
    with stub_execution(examples=[_EXAMPLES[0]]):
        start_then_run_with_failing_sync(service, dbs, 1, ALICE)

    returned = service.get_result_persistence_error(1, ALICE)
    returned["message"] = "clobbered"
    assert _RESULT_PERSISTENCE_ERROR["message"] != "clobbered"
    assert service.get_result_persistence_error(1, ALICE)["message"] != (
        "clobbered"
    )


# ==========================================================================
# B. sync_pending_results / get_result_persistence_error on interruption
# ==========================================================================


def test_sync_pending_results_ignores_a_run_it_does_not_own(service, dbs):
    create_run(service, ALICE, "alice-only")
    create_run(service, BOB, "bob-only")
    assert start_only(service, 1, ALICE) is True
    service.active_runs[(ALICE, 1)]["results"] = [
        {
            "example_id": "alices-secret-question",
            "query_hash": "a" * 32,
            "dataset_type": "simpleqa",
            "question": "What is alice researching?",
            "correct_answer": "her business",
            "is_correct": True,
        }
    ]

    # Alice has pending in-memory results for run 1; bob has none. Asking as
    # bob must not find alice's entry, and above all must not flush alice's
    # results into bob's database.
    assert service.sync_pending_results(1, BOB) == 0
    assert result_rows(dbs, BOB) == []
    assert result_rows(dbs, ALICE) == []

    # Alice's own sync still works and lands in alice's database only.
    assert service.sync_pending_results(1, ALICE) == 1
    assert [r["example_id"] for r in result_rows(dbs, ALICE)] == [
        "alices-secret-question"
    ]
    assert result_rows(dbs, BOB) == []


def test_interrupted_run_results_are_recoverable_and_sync_is_idempotent(
    service, dbs
):
    """A failed terminal sync leaves the results retryable, not lost."""
    create_run(service, ALICE, "alice-only")

    with stub_execution() as executed:
        start_then_run_with_failing_sync(service, dbs, 1, ALICE)
    assert len(executed.queries) == 3

    # Nothing persisted, the failure is flagged, and the in-memory entry is
    # deliberately retained so the results can still be recovered.
    assert result_rows(dbs, ALICE) == []
    assert service.get_result_persistence_error(1, ALICE) is not None
    assert (ALICE, 1) in service.active_runs
    assert service.active_runs[(ALICE, 1)].get("saved_indices") in (None, set())

    # The /api/results request path retries and recovers all three.
    assert service.sync_pending_results(1, ALICE) == 3
    assert len(result_rows(dbs, ALICE)) == 3
    assert service.get_result_persistence_error(1, ALICE) is None

    # Idempotent: a second sync neither duplicates rows nor trips uix_run_query.
    assert service.sync_pending_results(1, ALICE) == 0
    assert len(result_rows(dbs, ALICE)) == 3


def test_failed_terminal_sync_leaves_the_run_stuck_in_progress(service, dbs):
    """Characterisation: only the RESULTS are retryable, not the run STATUS.

    ``_sync_results_to_database`` is the sole writer of the terminal status and
    runs exactly once, in the worker's ``finally``. If its commit fails the run
    row keeps ``IN_PROGRESS`` and no later call re-attempts it —
    ``sync_pending_results`` writes results only. Recovering the results (see
    the test above) does not recover the status.
    """
    create_run(service, ALICE, "alice-only")
    with stub_execution():
        start_then_run_with_failing_sync(service, dbs, 1, ALICE)

    stuck = run_row(dbs, ALICE)
    assert stuck["status"] is BenchmarkStatus.IN_PROGRESS
    assert stuck["end_time"] is None

    service.sync_pending_results(1, ALICE)
    still_stuck = run_row(dbs, ALICE)
    assert still_stuck["status"] is BenchmarkStatus.IN_PROGRESS
    assert still_stuck["end_time"] is None


def test_malformed_result_does_not_block_its_healthy_neighbours(service, dbs):
    """One bad payload must not roll back the whole pending batch (#4861)."""
    create_run(service, ALICE, "alice-only")
    assert start_only(service, 1, ALICE) is True

    entry = service.active_runs[(ALICE, 1)]
    good = {
        "example_id": "ex-good",
        "query_hash": "h-good",
        "dataset_type": "simpleqa",
        "question": "Q?",
        "correct_answer": "A",
        "is_correct": True,
    }
    entry["results"] = [
        {"example_id": "ex-bad", "dataset_type": "simpleqa"},  # no query_hash
        good,
    ]

    assert service.sync_pending_results(1, ALICE) == 1
    assert [r["example_id"] for r in result_rows(dbs, ALICE)] == ["ex-good"]
    assert service.get_result_persistence_error(1, ALICE) is None


# ==========================================================================
# C. Terminal state on failure
# ==========================================================================


def test_task_level_crash_still_reaches_a_terminal_status(service, dbs):
    """A task blowing up past the research boundary must not wedge the run."""
    create_run(service, ALICE, "alice-only")

    real_process = service._process_benchmark_task
    calls = {"n": 0}

    def flaky(task, search_config, evaluation_config):
        calls["n"] += 1
        if calls["n"] == 2:
            raise RuntimeError("task 2 exploded")
        return real_process(task, search_config, evaluation_config)

    with (
        stub_execution(),
        patch.object(service, "_process_benchmark_task", flaky),
    ):
        start_and_run(service, 1, ALICE)

    row = run_row(dbs, ALICE)
    assert row["status"] is not BenchmarkStatus.IN_PROGRESS
    assert row["status"] is BenchmarkStatus.COMPLETED
    assert row["end_time"] is not None
    assert row["completed_examples"] == 2
    assert row["failed_examples"] == 1
    assert len(result_rows(dbs, ALICE)) == 2


def test_run_level_failure_records_failed_and_a_generic_client_error(
    service, dbs
):
    """Dataset loading blowing up must land FAILED, not IN_PROGRESS."""
    create_run(service, ALICE, "alice-only")

    def exploding_load(dataset_type, num_examples, seed=None):
        raise RuntimeError("dataset host /srv/ldr/data unreachable")

    with (
        patch(f"{SVC}.load_dataset", exploding_load),
        patch(f"{SVC}.quick_summary", Mock(side_effect=AssertionError)),
    ):
        start_and_run(service, 1, ALICE)

    row = run_row(dbs, ALICE)
    assert row["status"] is BenchmarkStatus.FAILED
    assert "dataset host" in row["error_message"]

    # ...but the raw message (which carries a server path) never reaches the
    # client; get_benchmark_status genericises it (CWE-209).
    status = service.get_benchmark_status(1, username=ALICE)
    assert status["status"] == "failed"
    assert "/srv/ldr/data" not in status["error_message"]
    assert "internal error" in status["error_message"]


def test_research_failures_are_recorded_as_result_rows_not_failed_examples(
    service, dbs
):
    """Characterisation of where research errors land.

    ``_process_benchmark_task`` swallows every research exception and returns a
    result carrying ``research_error``, so the worker loop's own ``except``
    never sees it: ``failed_examples`` stays 0 and a run in which every single
    question failed still reports COMPLETED. The evidence is per-row.
    """
    create_run(service, ALICE, "alice-only")
    with stub_execution(research_error="searxng refused: 403") as executed:
        start_and_run(service, 1, ALICE)

    row = run_row(dbs, ALICE)
    assert row["status"] is BenchmarkStatus.COMPLETED
    assert row["failed_examples"] == 0
    assert row["completed_examples"] == 3
    # No result was ever graded, so there is no accuracy to report.
    assert executed.graded == []
    assert row["overall_accuracy"] is None

    rows = result_rows(dbs, ALICE)
    assert len(rows) == 3
    assert all(r["research_error"] == "searxng refused: 403" for r in rows)
    assert all(r["is_correct"] is None for r in rows)

    # And the rate-limit signal the loop's except-branch would have set is
    # therefore never raised for a 403 from the search engine.
    assert service.rate_limit_detected == {}


# ==========================================================================
# D. Cancellation
# ==========================================================================


def test_cancellation_stops_the_work_and_records_cancelled(service, dbs):
    create_run(service, ALICE, "alice-only")

    def cancel_during_first_task(call_index, _kwargs):
        if call_index == 1:
            service.cancel_benchmark(1, username=ALICE)

    with stub_execution(on_research=cancel_during_first_task) as executed:
        start_and_run(service, 1, ALICE)

    # Three tasks were queued; cancelling during the first stopped the rest.
    assert len(executed.queries) == 1
    assert len(result_rows(dbs, ALICE)) == 1

    row = run_row(dbs, ALICE)
    assert row["status"] is BenchmarkStatus.CANCELLED
    assert row["completed_examples"] == 1
    assert (ALICE, 1) not in service.active_runs


def test_cancel_persists_even_when_the_run_is_not_active_in_memory(
    service, dbs
):
    create_run(service, ALICE, "alice-only")
    service.update_benchmark_status(
        1, BenchmarkStatus.IN_PROGRESS, username=ALICE
    )
    assert service.active_runs == {}

    assert service.cancel_benchmark(1, username=ALICE) is True
    assert run_row(dbs, ALICE)["status"] is BenchmarkStatus.CANCELLED


# ==========================================================================
# E. Concurrency guard on the execution path
# ==========================================================================


class RecordingSemaphore(threading.Semaphore):
    """Counts permit traffic so a leak is visible, not just a hang."""

    def __init__(self, value):
        super().__init__(value)
        self.acquired = 0
        self.released = 0

    def acquire(self, *args, **kwargs):
        got = super().acquire(*args, **kwargs)
        if got:
            self.acquired += 1
        return got

    def release(self, *args, **kwargs):
        self.released += 1
        return super().release(*args, **kwargs)


def test_every_task_takes_and_returns_a_global_research_permit(service, dbs):
    create_run(service, ALICE, "alice-only")
    semaphore = RecordingSemaphore(4)

    with patch(f"{SVC}._global_research_semaphore", semaphore):
        with stub_execution() as executed:
            start_and_run(service, 1, ALICE)
        assert len(executed.queries) == 3
        # One permit per task, all handed back: none leaked.
        assert semaphore.acquired == 3
        assert semaphore.released == 3
        assert semaphore._value == 4


def test_permit_is_returned_when_a_task_raises(service, dbs):
    """A leaked permit would permanently shrink server-wide research capacity."""
    create_run(service, ALICE, "alice-only")
    semaphore = RecordingSemaphore(2)

    def always_raises(task, search_config, evaluation_config):
        raise RuntimeError("boom past the research boundary")

    with (
        patch(f"{SVC}._global_research_semaphore", semaphore),
        patch.object(service, "_process_benchmark_task", always_raises),
        stub_execution(),
    ):
        start_and_run(service, 1, ALICE)

    assert semaphore.acquired == 3
    assert semaphore.released == 3
    assert semaphore._value == 2
    assert run_row(dbs, ALICE)["failed_examples"] == 3


def test_concurrent_benchmarks_serialise_on_the_shared_permit_pool(
    service, dbs
):
    """The service-side bound on concurrent LLM work is the shared semaphore.

    ``start_benchmark`` itself imposes no ceiling on how many benchmark
    workers exist, so this is the guard that actually limits in-flight
    research: with one permit available, three simultaneous benchmark workers
    never run two research calls at once.
    """
    users = ["u1", "u2", "u3"]
    for user in users:
        create_run(service, user, f"{user}-run", count=2)

    semaphore = RecordingSemaphore(1)
    barrier = threading.Barrier(len(users))

    def hold_the_permit(_call_index, _kwargs):
        # Widen the window so an unguarded implementation would visibly
        # overlap; see the negative control noted in this file's report.
        time.sleep(0.05)

    with (
        patch(f"{SVC}._global_research_semaphore", semaphore),
        stub_execution(on_research=hold_the_permit) as executed,
    ):
        for user in users:
            assert start_only(service, 1, user) is True

        def worker(user):
            barrier.wait(timeout=30)
            service._run_benchmark_thread(user, 1)

        threads = [
            threading.Thread(target=worker, args=(user,), daemon=True)
            for user in users
        ]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join(timeout=60)
            assert not thread.is_alive()

    assert len(executed.queries) == 6
    assert executed.max_concurrent == 1
    assert semaphore.acquired == 6
    assert semaphore.released == 6
    assert semaphore._value == 1
    for user in users:
        assert run_row(dbs, user)["status"] is BenchmarkStatus.COMPLETED
        assert len(result_rows(dbs, user)) == 2


# ==========================================================================
# F. Progress / result row consistency
# ==========================================================================


def test_service_never_writes_benchmark_progress_rows(service, dbs):
    """Progress is a websocket-only transport; nothing persists it.

    No code in ``src/`` inserts ``BenchmarkProgress``. Pinning that keeps the
    "no orphan progress for a deleted run" property honest: the service cannot
    create one, so the only progress rows a database can hold are legacy.
    """
    create_run(service, ALICE, "alice-only")
    with stub_execution():
        start_and_run(service, 1, ALICE)

    assert len(result_rows(dbs, ALICE)) == 3
    # Progress was emitted, just never written down.
    assert service.socket_service.emit_to_subscribers.call_count >= 3
    with dbs.open(ALICE) as session:
        assert session.query(BenchmarkProgress).count() == 0


def test_sync_pending_results_cannot_orphan_rows_onto_a_deleted_run(
    service, dbs
):
    """Deleting the run while it is still in memory must not leave debris."""
    create_run(service, ALICE, "alice-only")
    assert start_only(service, 1, ALICE) is True
    entry = service.active_runs[(ALICE, 1)]
    entry["results"] = [
        {
            "example_id": "ex-1",
            "query_hash": "h1",
            "dataset_type": "simpleqa",
            "question": "Q?",
            "correct_answer": "A",
            "is_correct": True,
        }
    ]

    with dbs.open(ALICE) as session:
        session.delete(session.get(BenchmarkRun, 1))
        session.commit()

    assert service.sync_pending_results(1, ALICE) == 0
    assert result_rows(dbs, ALICE) == []
    # The write was refused, so it is reported as a persistence failure.
    assert service.get_result_persistence_error(1, ALICE) is not None


def test_terminal_sync_drops_results_when_the_run_row_is_gone(service, dbs):
    """Same property for the worker's own terminal sync."""
    create_run(service, ALICE, "alice-only")
    assert start_only(service, 1, ALICE) is True

    with dbs.open(ALICE) as session:
        session.delete(session.get(BenchmarkRun, 1))
        session.commit()

    with stub_execution():
        service._run_benchmark_thread(ALICE, 1)

    assert result_rows(dbs, ALICE) == []
    with dbs.open(ALICE) as session:
        assert session.query(BenchmarkProgress).count() == 0
    # No run row to update, so the entry is retired without an error flag.
    assert (ALICE, 1) not in service.active_runs
    assert service.get_result_persistence_error(1, ALICE) is None


def test_progress_counts_agree_with_the_rows_that_were_written(service, dbs):
    create_run(service, ALICE, "alice-only")
    with stub_execution():
        start_and_run(service, 1, ALICE)

    row = run_row(dbs, ALICE)
    rows = result_rows(dbs, ALICE)
    assert row["completed_examples"] == len(rows) == 3
    assert row["failed_examples"] == 0
    assert row["overall_accuracy"] == 100.0
    # Every row belongs to this run, and query hashes are unique per run
    # (uix_run_query); a duplicate would have rolled the batch back.
    assert {r["run_id"] for r in rows} == {1}
    assert len({r["query_hash"] for r in rows}) == 3

    status = service.get_benchmark_status(1, username=ALICE)
    assert status["completed_examples"] == 3
    assert status["total_examples"] == 3
    assert status["running_accuracy"] == 100.0


# ==========================================================================
# G. start_benchmark / worker handoff
# ==========================================================================


class _JoiningThread(threading.Thread):
    """A real thread whose ``start()`` returns only once the body is done.

    Models the legal interleaving in which the worker finishes before the
    parent executes its next bytecode — reachable whenever the run is short
    (few examples, fast or cached model).
    """

    def start(self):
        super().start()
        super().join()


def test_fast_finishing_run_is_not_overwritten_with_failed(service, dbs):
    create_run(service, ALICE, "alice-only", count=1)

    with (
        stub_execution(examples=[_EXAMPLES[0]]),
        _settings_stubbed(),
        patch(f"{SVC}.threading.Thread", _JoiningThread),
    ):
        started = service.start_benchmark(1, username=ALICE)

    row = run_row(dbs, ALICE)
    assert len(result_rows(dbs, ALICE)) == 1
    assert row["status"] is BenchmarkStatus.COMPLETED
    assert row["error_message"] is None
    assert started is True


def test_worker_handle_is_registered_before_start_and_released_if_start_fails(
    service, dbs
):
    """The worker may retire its ``active_runs`` entry the moment it starts,
    so the handle must already be registered when ``start()`` is called. If
    the start itself fails, the run must end FAILED and retryable with
    nothing left in ``active_runs``.

    The registry is read from inside ``start()``: that is the only point that
    tells register-then-start from start-then-register, because both orders
    end in the same FAILED row and an empty registry.
    """
    create_run(service, ALICE, "alice-only", count=1)
    handle_registered_at_start = []

    def unstartable_start(thread):
        entry = service.active_runs.get((ALICE, 1), {})
        handle_registered_at_start.append(entry.get("thread") is thread)
        raise RuntimeError("cannot start worker")

    with (
        _settings_stubbed(),
        patch(f"{SVC}.threading.Thread.start", unstartable_start),
    ):
        started = service.start_benchmark(1, username=ALICE)

    assert handle_registered_at_start == [True]
    row = run_row(dbs, ALICE)
    assert started is False
    assert row["status"] is BenchmarkStatus.FAILED
    assert row["error_message"] == "cannot start worker"
    assert result_rows(dbs, ALICE) == []
    assert (ALICE, 1) not in service.active_runs