~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
"""``DELETE /benchmark/api/delete/{id}`` keeps its whole contract inline.
``delete_benchmark_run`` in ``web/routers/benchmark.py`` delegates to nothing.
Three separate guarantees live in its body and nowhere else:
* **the existence check** -- an unknown id answers 404 *before* the status
guard dereferences ``benchmark_run.status``. Drop it and the route stops
404-ing and starts 500-ing on ``AttributeError: 'NoneType' object has no
attribute 'status'`` instead.
* **the running guard** -- ``status.value == "in_progress"`` answers 400
with "Cannot delete a running benchmark. Cancel it first.". Without it a
user can delete the row a live benchmark thread is still writing results
and progress updates into; that thread then keeps INSERTing children for a
parent that no longer exists, and the run vanishes mid-flight from the
history page.
* **the scoping of the child deletes** -- both bulk deletes filter on
``benchmark_run_id``. Lose either filter and deleting *one* run wipes
**every** benchmark result and progress row in that user's database.
WHY THIS FILE EXISTS. This handler has never had a real test on either side
of the FastAPI migration. ``main`` carries a ``TestDeleteBenchmarkRun`` class
in ``tests/benchmarks/web_api/test_benchmark_routes.py`` with four tests whose
docstrings promise the 404, the running guard, cascade deletion and the
success message -- and whose entire bodies are a single
``assert callable(delete_benchmark_run)``. That holds for any function that
has ever been defined, so all four pass against a handler that deletes
running benchmarks, wipes unrelated rows, or 500s on a missing id. They are
vacuous; do not "restore" them. The only surviving FastAPI-era test that
names this route (``tests/security/test_history_and_benchmark_limits_fastapi
.py``, row 36) asserts a 422 for a non-integer path segment -- that is
FastAPI's ``int`` path coercion firing *before* the handler runs, not this
handler's guard at all.
So these tests drive the real handler against a real on-disk SQLite database
holding real ``BenchmarkRun``/``BenchmarkResult``/``BenchmarkProgress`` rows.
Only the session *provider* is patched; the session, the SQL and the rows are
genuine, because a ``MagicMock`` session cannot show that a row is gone, that
a sibling row is still there, or that a status predicate is still in the
WHERE clause.
ON WHAT THE CASCADE ASSERTIONS PIN. Children disappear under three
independent mechanisms: the handler's own bulk deletes, the
``cascade="all, delete-orphan"`` relationships on ``BenchmarkRun``, and
``ondelete="CASCADE"`` on the FKs (live in production -- ``_apply_pragmas``
runs ``PRAGMA foreign_keys = ON`` on every user-database connection, which
``_seeded_db`` mirrors). So the "children are gone" assertions pin the
user-visible invariant -- *no orphaned rows survive a delete* -- rather than
one mechanism; removing the handler's explicit deletes alone would not fail
them. What *is* pinned sharply is the scoping: those bulk deletes are the
only thing that can reach another run's rows, so the sibling-survives test
fails the moment a ``benchmark_run_id`` filter is dropped.
"""
import json
from contextlib import contextmanager
from unittest.mock import patch
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from starlette.responses import JSONResponse
from local_deep_research.database.models import Base
from local_deep_research.database.models.benchmark import (
BenchmarkProgress,
BenchmarkResult,
BenchmarkRun,
BenchmarkStatus,
DatasetType,
)
from local_deep_research.web.routers.benchmark import delete_benchmark_run
USERNAME = "benchuser"
RUNNING_ERROR = "Cannot delete a running benchmark. Cancel it first."
MISSING_ERROR = "Benchmark run not found"
# Every status the running guard must NOT refuse. A guard that rejected, say,
# PAUSED as well would leave those runs undeletable forever.
DELETABLE_STATUSES = [
BenchmarkStatus.PENDING,
BenchmarkStatus.COMPLETED,
BenchmarkStatus.FAILED,
BenchmarkStatus.CANCELLED,
BenchmarkStatus.PAUSED,
]
CHILDREN_PER_RUN = 2
def _add_run(session, status, tag):
"""Persist one BenchmarkRun plus two results and two progress rows."""
run = BenchmarkRun(
run_name=f"run-{tag}",
config_hash=f"cfg{tag}"[:16],
query_hash_list=[],
search_config={},
evaluation_config={},
datasets_config={},
status=status,
total_examples=CHILDREN_PER_RUN,
)
session.add(run)
session.commit()
session.add_all(
BenchmarkResult(
benchmark_run_id=run.id,
example_id=f"ex-{tag}-{i}",
# UNIQUE(benchmark_run_id, query_hash) -- keep these distinct
query_hash=f"qh-{tag}-{i}",
dataset_type=DatasetType.SIMPLEQA,
question="what is the answer?",
correct_answer="42",
)
for i in range(CHILDREN_PER_RUN)
)
session.add_all(
BenchmarkProgress(
benchmark_run_id=run.id,
completed_examples=i,
total_examples=CHILDREN_PER_RUN,
)
for i in range(CHILDREN_PER_RUN)
)
session.commit()
return run.id
def _seeded_db(tmp_path, request, status, name="bench"):
"""A real on-disk SQLite DB holding two seeded runs.
The second ("sibling") run is always COMPLETED and is never the delete
target: it is the control that proves the child deletes stay scoped to
the requested run.
On-disk rather than ``:memory:`` because an in-memory database is
per-connection, and these tests re-open the DB through a fresh session
after the handler has committed and closed its own.
``PRAGMA foreign_keys = ON`` mirrors production: ``DatabaseManager.
_apply_pragmas`` calls ``apply_performance_pragmas`` on every connection,
which sets it. SQLite defaults it OFF, so without this the test DB would
be *less* forgiving than production and the tests would be proving a
guarantee the running system does not rely on.
"""
engine = create_engine(f"sqlite:///{tmp_path}/{name}.db")
request.addfinalizer(engine.dispose)
@event.listens_for(engine, "connect")
def _enable_foreign_keys(dbapi_connection, _record):
dbapi_connection.execute("PRAGMA foreign_keys = ON")
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
with Session() as session:
target_id = _add_run(session, status, "target")
sibling_id = _add_run(session, BenchmarkStatus.COMPLETED, "sibling")
return Session, target_id, sibling_id
@contextmanager
def _user_db(Session):
"""Patch the session provider the handler imports at call time.
``delete_benchmark_run`` does ``from ...database.session_context import
get_user_db_session`` *inside* its body, so the module attribute is the
patch target rather than a router-level name.
"""
@contextmanager
def _session_ctx(username=None, password=None, session_id=None):
with Session() as session:
yield session
with patch(
"local_deep_research.database.session_context.get_user_db_session",
_session_ctx,
):
yield
def _delete(Session, benchmark_run_id):
"""Call the handler directly and normalise its two return shapes.
``request`` is unused by this handler's body; if that ever changes the
call raises, the route's blanket ``except Exception`` turns it into a
500, and every assertion below fails loudly rather than silently.
"""
with _user_db(Session):
response = delete_benchmark_run(
request=None,
benchmark_run_id=benchmark_run_id,
username=USERNAME,
)
if isinstance(response, JSONResponse):
return response.status_code, json.loads(response.body)
return 200, response
def _counts(Session, run_id):
"""(runs, results, progress rows) still stored for ``run_id``."""
with Session() as session:
return (
session.query(BenchmarkRun)
.filter(BenchmarkRun.id == run_id)
.count(),
session.query(BenchmarkResult)
.filter(BenchmarkResult.benchmark_run_id == run_id)
.count(),
session.query(BenchmarkProgress)
.filter(BenchmarkProgress.benchmark_run_id == run_id)
.count(),
)
SEEDED = (1, CHILDREN_PER_RUN, CHILDREN_PER_RUN)
GONE = (0, 0, 0)
# ---------------------------------------------------------------------------
# Positive control: deletion works, and takes the children with it
# ---------------------------------------------------------------------------
def test_a_finished_run_and_all_of_its_children_are_deleted(tmp_path, request):
"""Asserted first and on purpose: without it, "running runs get a 400"
would also be satisfied by a handler that refuses every delete.
"""
Session, target, _ = _seeded_db(
tmp_path, request, BenchmarkStatus.COMPLETED
)
assert _counts(Session, target) == SEEDED, (
"seed control: the run must start out with children, or the "
"post-delete assertion below proves nothing"
)
status_code, payload = _delete(Session, target)
assert (status_code, payload) == (
200,
{
"success": True,
"message": f"Benchmark run {target} deleted successfully",
},
)
assert _counts(Session, target) == GONE, (
"the run and both child tables must be empty; a surviving "
"BenchmarkResult/BenchmarkProgress row is an orphan no code path "
"ever reaps"
)
def test_deleting_one_run_leaves_another_runs_rows_alone(tmp_path, request):
"""The sharp test of the two bulk deletes.
``session.query(BenchmarkResult).delete()`` without the
``benchmark_run_id`` filter is valid SQL and a valid Python expression --
it just empties the whole table. Nothing but this assertion notices.
"""
Session, target, sibling = _seeded_db(
tmp_path, request, BenchmarkStatus.COMPLETED
)
assert _counts(Session, sibling) == SEEDED
_delete(Session, target)
assert _counts(Session, sibling) == SEEDED, (
"deleting one benchmark run wiped another run's results/progress: "
"a benchmark_run_id filter is missing from a bulk delete"
)
@pytest.mark.parametrize("status", DELETABLE_STATUSES, ids=lambda s: s.value)
def test_every_status_except_in_progress_is_deletable(
tmp_path, request, status
):
"""Positive control for the guard's *predicate*, not just its effect.
Pins that the refusal is scoped to exactly ``in_progress``. A guard
widened to, e.g., "anything not COMPLETED" would strand PENDING,
CANCELLED and FAILED runs in the history list permanently.
"""
Session, target, _ = _seeded_db(
tmp_path, request, status, name=status.value
)
status_code, payload = _delete(Session, target)
assert status_code == 200, (status.value, payload)
assert payload["success"] is True
assert _counts(Session, target) == GONE, status.value
# ---------------------------------------------------------------------------
# The running guard
# ---------------------------------------------------------------------------
def test_deleting_a_running_benchmark_is_refused_with_400(tmp_path, request):
"""The message is asserted verbatim: the UI shows it to the user and it
is the only thing telling them to cancel first, so a reword should be a
deliberate edit here rather than a silent drift.
"""
Session, target, _ = _seeded_db(
tmp_path, request, BenchmarkStatus.IN_PROGRESS
)
status_code, payload = _delete(Session, target)
assert (status_code, payload) == (
400,
{"success": False, "error": RUNNING_ERROR},
)
def test_a_refused_delete_leaves_the_running_run_and_its_children_intact(
tmp_path, request
):
"""A 400 that had already run the bulk deletes would be worse than no
guard: the live benchmark thread would keep writing into a run whose
history was silently emptied.
"""
Session, target, sibling = _seeded_db(
tmp_path, request, BenchmarkStatus.IN_PROGRESS
)
assert _counts(Session, target) == SEEDED
_delete(Session, target)
assert _counts(Session, target) == SEEDED
assert _counts(Session, sibling) == SEEDED
# ---------------------------------------------------------------------------
# The existence check
# ---------------------------------------------------------------------------
def test_an_unknown_run_id_is_a_404_not_a_500(tmp_path, request):
"""Ordering matters: the existence check has to precede the status guard,
which dereferences ``benchmark_run.status`` unconditionally. Reversed,
this call raises ``AttributeError`` on ``None`` and the blanket
``except Exception`` reports it as a 500 "internal error".
"""
Session, target, _ = _seeded_db(
tmp_path, request, BenchmarkStatus.COMPLETED
)
status_code, payload = _delete(Session, target + 10_000)
assert (status_code, payload) == (
404,
{"success": False, "error": MISSING_ERROR},
)
def test_a_404_deletes_nothing(tmp_path, request):
"""The bulk deletes key off the *path parameter*, not off the row that
was looked up, so a 404 that fell through to them would still be capable
of destroying data.
"""
Session, target, sibling = _seeded_db(
tmp_path, request, BenchmarkStatus.COMPLETED
)
_delete(Session, target + 10_000)
assert _counts(Session, target) == SEEDED
assert _counts(Session, sibling) == SEEDED