~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
"""Mid-task abort of benchmark runs on cancel.
The worker loop already stopped *between* tasks on cancel, but the
in-flight task's research + grading LLM calls ran to completion. Each
task publishes its research scope (``current_tracking_id``) so
``cancel_benchmark`` can abort the current LLM call mid-generation via
the termination flag, and the loop breaks on
``ResearchTerminatedException`` instead of crashing or counting a
failure.
"""
import uuid
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
def _task():
return {
"task_index": 0,
"example_id": "ex-1",
"dataset_type": "simpleqa",
"question": "What is 2+2?",
"correct_answer": "4",
"benchmark_run_id": 7,
"username": "u",
"user_password": "pw",
}
class TestCancelBenchmarkFlagsInFlightTask:
def test_cancel_sets_flag_for_published_tracking_id(self):
from local_deep_research.benchmarks.web_api.benchmark_service import (
BenchmarkService,
)
from local_deep_research.web.research_state import (
cleanup_research,
is_termination_requested,
)
service = BenchmarkService(socket_service=Mock())
tracking_id = f"bench-tid-{uuid.uuid4().hex[:12]}"
run_key = BenchmarkService._run_key("u", 7)
service.active_runs[run_key] = {
"status": "running",
"current_tracking_id": tracking_id,
}
service.update_benchmark_status = lambda *a, **k: None
try:
assert service.cancel_benchmark(7, "u") is True
assert service.active_runs[run_key]["status"] == "cancelled"
assert is_termination_requested(tracking_id)
finally:
cleanup_research(tracking_id)
service.active_runs.pop(run_key, None)
def test_cancel_without_inflight_task_still_succeeds(self):
from local_deep_research.benchmarks.web_api.benchmark_service import (
BenchmarkService,
)
service = BenchmarkService(socket_service=Mock())
run_key = BenchmarkService._run_key("u", 7)
service.active_runs[run_key] = {"status": "running"}
service.update_benchmark_status = lambda *a, **k: None
try:
assert service.cancel_benchmark(7, "u") is True
assert service.active_runs[run_key]["status"] == "cancelled"
finally:
service.active_runs.pop(run_key, None)
class TestTaskPublishesScope:
def test_tracking_id_published_and_grading_scoped(self, monkeypatch):
from local_deep_research.benchmarks.web_api import (
benchmark_service as mod,
)
from local_deep_research.utilities.thread_context import (
get_search_context,
)
from local_deep_research.web.research_state import cleanup_research
seen = {}
def _fake_quick_summary(**kwargs):
return {
"summary": "R",
"findings": [],
"iterations": 1,
"questions": {},
"formatted_findings": "",
"sources": [],
}
def _fake_grade(result_data, dataset_type, config, snapshot):
ctx = get_search_context() or {}
seen["grading_scope"] = ctx.get("research_id")
return {"is_correct": True, "graded_confidence": "90"}
monkeypatch.setattr(mod, "quick_summary", _fake_quick_summary)
monkeypatch.setattr(
mod,
"format_query",
lambda question, dataset_type: question,
)
monkeypatch.setattr(
mod,
"extract_answer_from_response",
lambda response, dataset_type: {
"extracted_answer": "R",
"confidence": "100",
},
)
monkeypatch.setattr(mod, "grade_single_result", _fake_grade)
monkeypatch.setattr(
"local_deep_research.config.thread_settings.get_settings_context",
lambda: SimpleNamespace(snapshot={}),
)
service = mod.BenchmarkService(socket_service=Mock())
run_key = mod.BenchmarkService._run_key("u", 7)
service.active_runs[run_key] = {"status": "running", "data": {}}
try:
result = service._process_benchmark_task(
_task(), {"iterations": 1}, {}
)
published = service.active_runs[run_key].get("current_tracking_id")
assert published
assert result.get("research_id") == published
# Grading ran under the same research scope, so a cancel
# flag aborts the grader LLM call too.
assert seen.get("grading_scope") == published
finally:
tid = service.active_runs[run_key].get("current_tracking_id")
service.active_runs.pop(run_key, None)
if tid:
cleanup_research(tid)
def test_terminated_exception_propagates_for_loop_handler(
self, monkeypatch
):
"""The worker loop breaks on this; it must never become a failed task."""
from local_deep_research.benchmarks.web_api import (
benchmark_service as mod,
)
from local_deep_research.exceptions import ResearchTerminatedException
def _raise(**kwargs):
raise ResearchTerminatedException("cancelled")
monkeypatch.setattr(mod, "quick_summary", _raise)
monkeypatch.setattr(
mod,
"format_query",
lambda question, dataset_type: question,
)
monkeypatch.setattr(
"local_deep_research.config.thread_settings.get_settings_context",
lambda: SimpleNamespace(snapshot={}),
)
service = mod.BenchmarkService(socket_service=Mock())
run_key = mod.BenchmarkService._run_key("u", 7)
service.active_runs[run_key] = {"status": "running", "data": {}}
try:
with pytest.raises(ResearchTerminatedException):
service._process_benchmark_task(_task(), {}, {})
finally:
tid = service.active_runs[run_key].get("current_tracking_id")
service.active_runs.pop(run_key, None)
if tid:
from local_deep_research.web.research_state import (
cleanup_research,
)
cleanup_research(tid)
class TestCancelDuringSemaphoreWait:
"""Cancel arriving while the worker is blocked on capacity.
The loop used to check ``status`` only before the semaphore wait and
published ``current_tracking_id`` only afterwards, so a Cancel during
the wait had no ID to flag and the next task ran to completion.
"""
def test_cancel_while_blocked_starts_no_task(self, monkeypatch):
import threading
from local_deep_research.benchmarks.web_api import (
benchmark_service as mod,
)
service = mod.BenchmarkService(socket_service=Mock())
service.update_benchmark_status = lambda *a, **k: None
service._sync_results_to_database = lambda *a, **k: None
run_key = mod.BenchmarkService._run_key("u", 7)
service.active_runs[run_key] = {
"data": {
"username": "u",
"user_password": "pw",
"datasets_config": {},
"search_config": {},
"evaluation_config": {},
"settings_snapshot": {},
},
"status": "running",
"results": [],
}
task = _task()
monkeypatch.setattr(
service,
"_create_task_queue",
lambda *a, **k: [dict(task)],
)
started = []
monkeypatch.setattr(
service,
"_process_benchmark_task",
lambda *a, **k: started.append(True) or {},
)
acquire_entered = threading.Event()
real_sem = threading.Semaphore(0)
class _SignallingSemaphore:
def acquire(self, *a, **k):
acquire_entered.set()
return real_sem.acquire(*a, **k)
def release(self, *a, **k):
return real_sem.release(*a, **k)
monkeypatch.setattr(
mod, "_global_research_semaphore", _SignallingSemaphore()
)
worker = threading.Thread(
target=service._run_benchmark_thread, args=("u", 7), daemon=True
)
worker.start()
try:
assert acquire_entered.wait(timeout=10), "worker never blocked"
# Cancel lands while no tracking ID is published yet.
assert service.cancel_benchmark(7, "u") is True
assert "current_tracking_id" not in service.active_runs[run_key]
# Release capacity: the worker must notice the cancel and skip.
real_sem.release()
worker.join(timeout=30)
assert not worker.is_alive()
assert started == []
from local_deep_research.benchmarks.web_api.benchmark_service import (
BenchmarkStatus,
)
assert (
service.active_runs[run_key]["completion_info"]["status"]
== BenchmarkStatus.CANCELLED
)
finally:
if worker.is_alive():
real_sem.release()
worker.join(timeout=10)
service.active_runs.pop(run_key, None)
def test_publish_after_cancel_aborts_before_llm_calls(self, monkeypatch):
"""Cancel between the status check and the scope publish must not run.
The worker publishes ``current_tracking_id`` inside
``_process_benchmark_task``; a Cancel that landed just before the
publish sees no ID to flag. The publish must cooperate by arming the
new ID and aborting before any LLM call starts.
"""
from local_deep_research.benchmarks.web_api import (
benchmark_service as mod,
)
from local_deep_research.exceptions import ResearchTerminatedException
from local_deep_research.web.research_state import (
cleanup_research,
is_termination_requested,
)
calls = {"research": 0, "grade": 0}
def _fake_quick_summary(**kwargs):
calls["research"] += 1
return {"summary": "R", "sources": []}
def _fake_grade(*a, **k):
calls["grade"] += 1
return {"is_correct": True}
monkeypatch.setattr(mod, "quick_summary", _fake_quick_summary)
monkeypatch.setattr(mod, "grade_single_result", _fake_grade)
monkeypatch.setattr(
mod,
"format_query",
lambda question, dataset_type: question,
)
monkeypatch.setattr(
mod,
"extract_answer_from_response",
lambda response, dataset_type: {
"extracted_answer": "R",
"confidence": "100",
},
)
monkeypatch.setattr(
"local_deep_research.config.thread_settings.get_settings_context",
lambda: SimpleNamespace(snapshot={}),
)
service = mod.BenchmarkService(socket_service=Mock())
run_key = mod.BenchmarkService._run_key("u", 7)
service.active_runs[run_key] = {"status": "cancelled", "data": {}}
try:
with pytest.raises(ResearchTerminatedException):
service._process_benchmark_task(_task(), {}, {})
assert calls == {"research": 0, "grade": 0}
published = service.active_runs[run_key].get("current_tracking_id")
assert published
# The just-published scope is flagged so any racing LLM call
# aborts too.
assert is_termination_requested(published)
finally:
tid = service.active_runs[run_key].get("current_tracking_id")
service.active_runs.pop(run_key, None)
if tid:
cleanup_research(tid)