~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
"""Dropped-write reporting through real metric writers and the dashboard."""
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from pathlib import Path
import re
from urllib.parse import urlsplit
from types import SimpleNamespace
from unittest.mock import MagicMock
from bs4 import BeautifulSoup
from markdown_it import MarkdownIt
import pytest
from sqlalchemy.exc import OperationalError
from local_deep_research.utilities import metric_write_health as health_module
@pytest.fixture
def health(monkeypatch):
state = health_module.MetricWriteHealth()
monkeypatch.setattr(health_module, "_health", state)
return state
def _assert_snapshot_shape(snapshot):
assert set(snapshot) == {"dropped_writes", "by_source", "by_reason"}
assert set(snapshot["by_source"]) == {"tokens", "search", "rate_limits"}
assert set(snapshot["by_reason"]) == {
"write_error",
"missing_credentials",
"pending_transaction",
"database_unavailable",
}
assert all(
type(count) is int
for count in (
snapshot["dropped_writes"],
*snapshot["by_source"].values(),
*snapshot["by_reason"].values(),
)
)
def test_populated_snapshot_has_exact_public_shape(health):
for source, reason, count in (
("tokens", "write_error", 1),
("tokens", "missing_credentials", 2),
("search", "pending_transaction", 3),
("rate_limits", "database_unavailable", 4),
):
for _ in range(count):
health.record("alice", source, reason)
snapshot = health.snapshot("alice")
_assert_snapshot_shape(snapshot)
assert snapshot == {
"dropped_writes": 10,
"by_source": {"tokens": 3, "search": 3, "rate_limits": 4},
"by_reason": {
"write_error": 1,
"missing_credentials": 2,
"pending_transaction": 3,
"database_unavailable": 4,
},
}
def test_default_capacity_retains_512_recent_writers_and_complete_counts(
health,
):
for index in range(512):
username = f"user-{index}"
health.record(username, "tokens", "write_error")
health.record(username, "search", "missing_credentials")
# A write refreshes user-0; reading user-1 must not save it from eviction.
health.record("user-0", "rate_limits", "database_unavailable")
for username in ("user-1", "unknown", None):
health.snapshot(username)
health.record("user-512", "tokens", "write_error")
health.record("user-512", "search", "missing_credentials")
expected_users = {f"user-{index}" for index in range(513)} - {"user-1"}
assert len(health._users) == 512
assert set(health._users) == expected_users
assert health.snapshot("user-1")["dropped_writes"] == 0
for username in expected_users:
refreshed = username == "user-0"
assert health.snapshot(username) == {
"dropped_writes": 3 if refreshed else 2,
"by_source": {
"tokens": 1,
"search": 1,
"rate_limits": int(refreshed),
},
"by_reason": {
"write_error": 1,
"missing_credentials": 1,
"pending_transaction": 0,
"database_unavailable": int(refreshed),
},
}
@pytest.mark.parametrize("username", [None, 42])
@pytest.mark.parametrize(
"path",
[
"search",
"rate_limits",
"tokens_main",
"tokens_worker",
"tokens_scoped_main",
"tokens_scoped_worker",
],
)
def test_real_writers_do_not_allocate_invalid_user_health(
health, monkeypatch, path, username
):
import threading
from local_deep_research.database import (
session_context,
thread_local_session,
)
from local_deep_research.metrics import search_tracker, token_counter
from local_deep_research.web_search_engines.rate_limiting import (
tracker as rate_module,
)
context = _context(password=False)
context["username"] = username
manager = thread_local_session.thread_session_manager
monkeypatch.setattr(manager, "in_scope", lambda: "scoped" in path)
monkeypatch.setattr(manager, "get_scoped_session", lambda _: None)
monkeypatch.setattr(
threading,
"current_thread",
lambda: SimpleNamespace(
name="worker" if "worker" in path else "MainThread"
),
)
# None must stay anonymous, including the main-thread authentication fallback.
monkeypatch.setattr(token_counter, "get_current_username", lambda: None)
monkeypatch.setattr(search_tracker, "get_search_context", lambda: context)
monkeypatch.setattr(rate_module, "get_search_context", lambda: context)
session = MagicMock()
session.query.return_value.filter_by.return_value.first.return_value = None
session.commit.side_effect = RuntimeError("private write error")
@contextmanager
def failed_session(*args, **kwargs):
yield session
monkeypatch.setattr(session_context, "get_user_db_session", failed_session)
module = (
search_tracker
if path == "search"
else rate_module
if path == "rate_limits"
else token_counter
)
report = MagicMock(wraps=health_module.record_metric_write_drop)
monkeypatch.setattr(module, "record_metric_write_drop", report)
tracker = rate_module.AdaptiveRateLimitTracker(programmatic_mode=True)
tracker.programmatic_mode = False
tracker.recent_attempts["test-engine"] = [
{"wait_time": wait, "success": True} for wait in (1, 2, 3)
]
def write():
if path == "search":
search_tracker.SearchTracker.record_search(
"test-engine", "private query"
)
elif path == "rate_limits":
tracker._update_estimate("test-engine")
else:
token_counter.TokenCountingCallback(
research_context=context
)._save_to_db(10, 5)
write()
assert report.call_count == int(username is not None)
assert not health._users
# Positive control uses the same context, writer body and drop seam.
context["username"] = "alice"
write()
source = path if path in ("search", "rate_limits") else "tokens"
reason = (
"database_unavailable"
if "scoped" in path
else "write_error"
if path == "tokens_main"
else "missing_credentials"
)
report.assert_called_with("alice", source, reason)
assert set(health._users) == {"alice"}
snapshot = health.snapshot("alice")
assert snapshot["dropped_writes"] == 1
assert snapshot["by_source"][source] == 1
assert snapshot["by_reason"][reason] == 1
def test_counts_are_isolated_copied_and_bounded():
health = health_module.MetricWriteHealth(max_users=2)
health.record("alice", "tokens", "write_error")
health.record("bob", "search", "missing_credentials")
health.record("alice", "search", "pending_transaction")
snapshot = health.snapshot("alice")
assert snapshot["dropped_writes"] == 2
assert snapshot["by_source"] == {"tokens": 1, "search": 1, "rate_limits": 0}
assert snapshot["by_reason"]["write_error"] == 1
assert snapshot["by_reason"]["missing_credentials"] == 0
snapshot["by_reason"]["write_error"] = 500
assert health.snapshot("alice")["by_reason"]["write_error"] == 1
# Reads neither evict writers nor make an older writer recent again.
for username in ("bob", "unknown", None):
health.snapshot(username)
health.record("carol", "rate_limits", "database_unavailable")
assert health.snapshot("bob")["dropped_writes"] == 0
assert health.snapshot("alice")["dropped_writes"] == 2
assert health.snapshot("carol")["dropped_writes"] == 1
def test_concurrent_writers_do_not_lose_counts(health):
def record_batch(_):
for _ in range(100):
health_module.record_metric_write_drop(
"alice", "tokens", "write_error"
)
with ThreadPoolExecutor(max_workers=8) as pool:
list(pool.map(record_batch, range(16)))
assert (
health_module.get_metric_write_health("alice")["dropped_writes"] == 1600
)
assert health.snapshot("bob")["dropped_writes"] == 0
def test_only_known_categories_and_identified_users_are_retained(health):
for username in (None, ""):
health.record(username, "tokens", "write_error")
health.record("alice", "private query", "write_error")
health.record("alice", "tokens", "private exception message")
assert health.snapshot("alice")["dropped_writes"] == 0
assert health.snapshot(None)["dropped_writes"] == 0
with pytest.raises(ValueError, match="positive"):
health_module.MetricWriteHealth(max_users=0)
class _AliasedUsername(str):
"""A ``str`` subclass that hashes and compares like ``alice``."""
def __hash__(self):
return hash("alice")
def __eq__(self, other):
return other == "alice"
def test_a_str_subclass_username_cannot_reach_another_users_bucket(health):
health.record("alice", "tokens", "write_error")
impostor = _AliasedUsername("mallory")
health.record(impostor, "search", "write_error")
assert health.snapshot("alice")["dropped_writes"] == 1
assert health.snapshot("alice")["by_source"]["search"] == 0
assert health.snapshot(impostor)["dropped_writes"] == 0
def test_a_non_string_username_is_not_counted_but_is_logged(
health, loguru_caplog
):
with loguru_caplog.at_level("DEBUG"):
health.record(42, "tokens", "write_error")
assert not health._users
assert health.snapshot("42")["dropped_writes"] == 0
assert "username type int is not str" in loguru_caplog.text
def test_none_username_records_and_logs_nothing(health, loguru_caplog):
with loguru_caplog.at_level("DEBUG"):
health.record(None, "tokens", "write_error")
assert not health._users
assert "username type" not in loguru_caplog.text
def _context(password=True):
return {
"username": "alice",
"user_password": "test-password" if password else None,
"research_id": "health-research",
}
@pytest.mark.parametrize("fail", [False, True])
@pytest.mark.parametrize("explicit_owner", [False, True])
def test_main_thread_token_commit_result(
health, monkeypatch, fail, explicit_owner
):
from local_deep_research.database import session_context
from local_deep_research.metrics import token_counter
session = MagicMock()
session.query.return_value.filter_by.return_value.first.return_value = None
if fail:
session.commit.side_effect = RuntimeError("private write error")
opened_sessions = []
@contextmanager
def get_session(username, password=None):
opened_sessions.append((username, password))
yield session
monkeypatch.setattr(session_context, "get_user_db_session", get_session)
monkeypatch.setattr(
token_counter,
"get_current_username",
lambda: "bob" if explicit_owner else "alice",
)
# pytest invokes this on MainThread, so exercise the real synchronous path.
callback = token_counter.TokenCountingCallback(
research_id="health-research",
research_context=_context() if explicit_owner else None,
)
callback._save_to_db(10, 5)
assert opened_sessions == [
("alice", "test-password" if explicit_owner else None)
]
assert session.commit.call_count == 1
assert health.snapshot("alice")["dropped_writes"] == int(fail)
assert health.snapshot("alice")["by_source"]["tokens"] == int(fail)
assert health.snapshot("bob")["dropped_writes"] == 0
@pytest.mark.parametrize("failures, expected", [(0, 0), (2, 0), (3, 1)])
def test_background_tokens_count_only_exhausted_retries(
health, monkeypatch, failures, expected
):
import threading
from local_deep_research.database import sqlcipher_utils, thread_metrics
from local_deep_research.metrics.token_counter import TokenCountingCallback
writer = MagicMock()
locked = OperationalError("INSERT", {}, Exception("database is locked"))
writer.write_token_metrics.side_effect = [locked] * failures + [None]
monkeypatch.setattr(thread_metrics, "metrics_writer", writer)
monkeypatch.setattr(sqlcipher_utils.time, "sleep", lambda _: None)
monkeypatch.setattr(
threading, "current_thread", lambda: SimpleNamespace(name="worker")
)
callback = TokenCountingCallback(research_context=_context())
callback._save_to_db(10, 5)
assert writer.write_token_metrics.call_count == min(failures + 1, 3)
assert health.snapshot("alice")["by_source"]["tokens"] == expected
assert health.snapshot("alice")["by_reason"]["write_error"] == expected
def test_background_tokens_missing_credentials(health, monkeypatch):
import threading
from local_deep_research.database import thread_metrics
from local_deep_research.metrics.token_counter import TokenCountingCallback
writer = MagicMock()
monkeypatch.setattr(thread_metrics, "metrics_writer", writer)
monkeypatch.setattr(
threading, "current_thread", lambda: SimpleNamespace(name="worker")
)
TokenCountingCallback(
research_context=_context(password=False)
)._save_to_db(10, 5)
assert health.snapshot("alice")["by_reason"]["missing_credentials"] == 1
writer.write_token_metrics.assert_not_called()
@pytest.mark.parametrize(
"mode, expected_reason",
[
("success", None),
("owned_failure", "write_error"),
("nested_failure", "write_error"),
("double_failure", "write_error"),
("nested_success", None),
("owner_mismatch", "database_unavailable"),
("pending", "pending_transaction"),
("missing_password", "missing_credentials"),
("no_context", None),
],
)
def test_search_failures_count_once_without_touching_caller_transaction(
health, monkeypatch, mode, expected_reason
):
from local_deep_research.database import (
session_context,
thread_local_session,
thread_metrics,
)
from local_deep_research.metrics import search_tracker
session = MagicMock()
session.new = [object()] if mode == "pending" else []
session.dirty = session.deleted = []
if mode in ("owned_failure", "nested_failure", "double_failure"):
session.add.side_effect = RuntimeError("private query and password")
if mode == "double_failure":
session.begin_nested.return_value.__exit__.side_effect = RuntimeError(
"private cleanup error"
)
@contextmanager
def get_session(*args):
try:
yield session
finally:
if mode == "double_failure":
raise RuntimeError("private cleanup error")
writer = MagicMock()
writer.get_session.side_effect = get_session
monkeypatch.setattr(thread_metrics, "metrics_writer", writer)
monkeypatch.setattr(session_context, "get_user_db_session", get_session)
monkeypatch.setattr(
thread_local_session.thread_session_manager,
"in_scope",
lambda: (
mode
in (
"nested_failure",
"double_failure",
"nested_success",
"owner_mismatch",
"pending",
)
),
)
borrow = MagicMock(
return_value=None if mode == "owner_mismatch" else session
)
monkeypatch.setattr(
thread_local_session.thread_session_manager,
"get_scoped_session",
borrow,
)
monkeypatch.setattr(
search_tracker,
"get_search_context",
lambda: (
None
if mode == "no_context"
else _context(mode != "missing_password")
),
)
search_tracker.SearchTracker.record_search("test-engine", "private query")
snapshot = health.snapshot("alice")
assert snapshot["dropped_writes"] == int(expected_reason is not None)
if expected_reason:
assert snapshot["by_reason"][expected_reason] == 1
if mode in ("pending", "owner_mismatch"):
session.add.assert_not_called()
session.begin_nested.assert_not_called()
elif mode in ("success", "nested_success"):
session.add.assert_called_once()
if mode in (
"nested_failure",
"double_failure",
"nested_success",
"owner_mismatch",
"pending",
):
borrow.assert_called_once_with("alice")
writer.get_session.assert_not_called()
session.commit.assert_not_called()
session.rollback.assert_not_called()
session.flush.assert_not_called()
_assert_snapshot_shape(snapshot)
@pytest.mark.parametrize("worker", [False, True])
@pytest.mark.parametrize(
"mode, reason",
[
("success", None),
("no_password", None),
("write_error", "write_error"),
("pending", "pending_transaction"),
("owner_mismatch", "database_unavailable"),
],
)
def test_scoped_tokens_report_skips_without_changing_the_transaction(
health, monkeypatch, worker, mode, reason
):
import threading
from local_deep_research.database import (
thread_local_session,
thread_metrics,
)
from local_deep_research.metrics import token_counter
session = MagicMock()
session.new = [object()] if mode == "pending" else []
session.dirty = session.deleted = []
writer = MagicMock()
if mode == "write_error":
writer.add_token_metrics_to_session.side_effect = RuntimeError(
"private write error"
)
monkeypatch.setattr(thread_metrics, "metrics_writer", writer)
monkeypatch.setattr(
threading,
"current_thread",
lambda: SimpleNamespace(name="worker" if worker else "MainThread"),
)
manager = thread_local_session.thread_session_manager
monkeypatch.setattr(manager, "in_scope", lambda: True)
borrow = MagicMock(
return_value=None if mode == "owner_mismatch" else session
)
monkeypatch.setattr(manager, "get_scoped_session", borrow)
monkeypatch.setattr(token_counter, "get_current_username", lambda: "bob")
callback = token_counter.TokenCountingCallback(
research_context=_context(password=mode != "no_password")
)
callback._save_to_db(10, 5)
borrow.assert_called_once_with("alice")
snapshot = health.snapshot("alice")
assert snapshot["dropped_writes"] == int(reason is not None)
if reason:
assert snapshot["by_reason"][reason] == 1
assert health.snapshot("bob")["dropped_writes"] == 0
_assert_snapshot_shape(snapshot)
assert writer.add_token_metrics_to_session.call_count == int(
mode not in ("pending", "owner_mismatch")
)
writer.write_token_metrics.assert_not_called()
writer.set_user_password.assert_not_called()
session.commit.assert_not_called()
session.rollback.assert_not_called()
session.flush.assert_not_called()
if mode in ("pending", "owner_mismatch"):
session.begin_nested.assert_not_called()
@pytest.mark.parametrize(
"mode, failures, reason",
[
("normal", 0, None),
("normal", 2, None),
("normal", 3, "write_error"),
("missing_password", 0, "missing_credentials"),
("unavailable", 0, "database_unavailable"),
("programmatic", 0, None),
("no_context", 0, None),
],
)
def test_rate_estimate_final_outcome(
health, monkeypatch, mode, failures, reason
):
from local_deep_research.database import sqlcipher_utils
from local_deep_research.web_search_engines.rate_limiting import (
tracker as rate_module,
)
tracker = rate_module.AdaptiveRateLimitTracker(programmatic_mode=True)
tracker.programmatic_mode = mode == "programmatic"
tracker.recent_attempts["test-engine"] = [
{"wait_time": wait, "success": True} for wait in (1, 2, 3)
]
session = MagicMock()
session.query.return_value.filter_by.return_value.first.return_value = None
calls = 0
@contextmanager
def get_session(*args):
nonlocal calls
calls += 1
if calls <= failures:
raise OperationalError(
"INSERT", {}, Exception("database is locked")
)
yield session
imports = MagicMock(
return_value={}
if mode == "unavailable"
else {"RateLimitEstimate": MagicMock()}
)
monkeypatch.setattr(rate_module, "_get_db_imports", imports)
monkeypatch.setattr(
rate_module,
"get_search_context",
lambda: (
None
if mode == "no_context"
else _context(mode != "missing_password")
),
)
monkeypatch.setattr(tracker, "_resolve_session", get_session)
monkeypatch.setattr(sqlcipher_utils.time, "sleep", lambda _: None)
tracker._update_estimate("test-engine")
snapshot = health.snapshot("alice")
assert snapshot["dropped_writes"] == int(reason is not None)
if reason:
assert snapshot["by_reason"][reason] == 1
if mode == "normal":
assert calls == min(failures + 1, 3)
assert session.add.call_count == int(failures < 3)
else:
assert calls == 0
if mode in ("programmatic", "no_context"):
imports.assert_not_called()
# The adaptive calculation still completes even if persistence fails.
assert tracker.current_estimates["test-engine"]["base"] == 2
@pytest.fixture
def dashboard(monkeypatch):
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates
from fastapi.testclient import TestClient
from jinja2 import ChoiceLoader, DictLoader
from starlette.middleware.sessions import SessionMiddleware
from local_deep_research.web.routers import metrics
# Render the actual page and help macros; omit only the unrelated site shell.
env = metrics.templates.env.overlay(
loader=ChoiceLoader(
[
DictLoader({"base.html": "{% block content %}{% endblock %}"}),
metrics.templates.env.loader,
]
)
)
env.globals = {**env.globals, "url_for": lambda *a, **kw: "/navigation"}
monkeypatch.setattr(metrics, "templates", Jinja2Templates(env=env))
app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="test-session-secret")
app.include_router(metrics.router)
with TestClient(app) as client:
yield app, client, metrics.require_auth
def test_dashboard_uses_authenticated_owner_and_fixed_reason_labels(
health, dashboard
):
app, client, require_auth = dashboard
health.record("alice", "search", "missing_credentials")
health.record("bob", "tokens", "write_error")
health.record("bob", "tokens", "write_error")
app.dependency_overrides[require_auth] = lambda: "alice"
response = client.get("/metrics/?username=bob")
assert response.status_code == 200
assert 'id="metric-write-warning"' in response.text
banner = BeautifulSoup(response.text, "html.parser").select_one(
"#metric-write-warning[role=status]"
)
assert banner is not None
assert "Missing database credentials: 1" in banner.get_text()
assert "Database write failures" not in banner.get_text()
assert (
"Recording problems since the server started: at least 1."
in " ".join(banner.get_text().split())
)
app.dependency_overrides[require_auth] = lambda: "bob"
response = client.get("/metrics/")
banner = BeautifulSoup(response.text, "html.parser").select_one(
"#metric-write-warning[role=status]"
)
assert banner is not None
assert "Database write failures: 2" in banner.get_text()
assert "Missing database credentials" not in banner.get_text()
def test_dashboard_omits_warning_when_no_drops_recorded(health, dashboard):
app, client, require_auth = dashboard
health.record("alice", "tokens", "write_error")
app.dependency_overrides[require_auth] = lambda: "carol"
response = client.get("/metrics/")
assert response.status_code == 200
assert 'id="metric-write-warning"' not in response.text
def test_anonymous_dashboard_does_not_expose_counts(health, dashboard):
_, client, _ = dashboard
health.record("alice", "tokens", "write_error")
response = client.get("/metrics/?username=alice")
assert response.status_code == 401
assert "metric-write-warning" not in response.text
DOC_PATH = "docs/metrics-dashboard.md"
DOC_ANCHOR = "incomplete-metrics-warning"
DOC_URL = (
"https://github.com/LearningCircuit/local-deep-research/blob/main/"
f"{DOC_PATH}#{DOC_ANCHOR}"
)
def _assert_warning_documentation_target(url):
target = urlsplit(url)
assert target.fragment == DOC_ANCHOR
root = Path(__file__).resolve().parents[2]
document = root / DOC_PATH
assert document.is_file()
tokens = MarkdownIt().parse(document.read_text(encoding="utf-8"))
headings = [
tokens[index + 1].content
for index, token in enumerate(tokens)
if token.type == "heading_open"
]
assert "Incomplete Metrics Warning" in headings
assert target.fragment in [
re.sub(r"[^\w -]", "", heading.lower()).replace(" ", "-")
for heading in headings
]
def test_readme_links_to_incomplete_metrics_warning():
root = Path(__file__).resolve().parents[2]
readme = (root / "README.md").read_text(encoding="utf-8")
links = re.findall(r"\[[^\]]+\]\(([^)]+)\)", readme)
target = f"{DOC_PATH}#{DOC_ANCHOR}"
assert target in links
_assert_warning_documentation_target(target)
@pytest.mark.parametrize("username", ["alice", "bob"])
@pytest.mark.parametrize("has_drops", [False, True])
def test_dashboard_help_explains_incomplete_metrics(
health, dashboard, username, has_drops
):
app, client, require_auth = dashboard
# Another user's drops must not control this user's banner or static help.
health.record("other-user", "search", "missing_credentials")
if has_drops:
health.record(username, "tokens", "write_error")
app.dependency_overrides[require_auth] = lambda: username
response = client.get("/metrics/")
assert response.status_code == 200
page = BeautifulSoup(response.text, "html.parser")
banner = page.select_one("#metric-write-warning[role=status]")
assert (banner is not None) == has_drops
panel = page.select_one("#help-panel-metrics-how")
assert panel is not None
assert panel.select_one(".ldr-help-panel-title").get_text(strip=True) == (
"Understanding Metrics"
)
content = panel.select_one("#help-panel-content-metrics-how")
steps = content.select(".ldr-help-step")
warning_steps = [
step
for step in steps
if step.select_one("strong").get_text(strip=True)
== "Incomplete Metrics"
]
assert len(warning_steps) == 1
step = warning_steps[0]
text = step.get_text(" ", strip=True)
assert "Metrics may be incomplete" in text
assert "cannot be recorded" in text
assert "server logs" in text
link = step.select_one("a[href]")
assert link is not None
assert link["href"] == DOC_URL
assert link.get_text(strip=True)
assert link.get("target") == "_blank"
assert set(link.get("rel", [])) == {"noopener", "noreferrer"}
_assert_warning_documentation_target(link["href"])