~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
"""Search, rate and token telemetry join a caller's write transaction only.
In a scope that has written, telemetry commits or rolls back with the
caller. In a scope that has only read, it commits at once on its own, so
no writer lock outlives it and the scope's exit rollback cannot drop it.
"""
from concurrent.futures import ThreadPoolExecutor
from itertools import permutations
import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.database.models import (
Base,
ModelUsage,
RateLimitAttempt,
RateLimitEstimate,
SearchCall,
TokenUsage,
)
from local_deep_research.database.session_context import get_user_db_session
from local_deep_research.database.sqlcipher_utils import (
create_sqlcipher_connection,
)
from local_deep_research.database.thread_local_session import (
cleanup_current_thread,
)
from local_deep_research.metrics.search_tracker import SearchTracker
from local_deep_research.metrics.token_counter import TokenCountingCallback
from local_deep_research.utilities.thread_context import search_context
from local_deep_research.web_search_engines.rate_limiting.tracker import (
AdaptiveRateLimitTracker,
)
CONTEXT = {
"username": "alice",
"user_password": "test-password",
"research_id": "r",
}
MODELS = (SearchCall, RateLimitEstimate, TokenUsage, ModelUsage)
@pytest.fixture(params=["sqlite", "sqlcipher"])
def database(request, tmp_path, monkeypatch):
"""Independent connections to a real WAL database, including encryption."""
path = tmp_path / "combined.db"
if request.param == "sqlcipher":
pytest.importorskip("sqlcipher3", reason="requires real SQLCipher")
monkeypatch.setenv("LDR_DB_CONFIG_BUSY_TIMEOUT_MS", "100")
def connect():
return create_sqlcipher_connection(
path,
hex_key="ab" * 32,
creation_mode=not path.exists(),
connect_kwargs={"timeout": 0.1, "check_same_thread": False},
)
engine = create_engine("sqlite://", creator=connect, poolclass=NullPool)
else:
engine = create_engine(
f"sqlite:///{path}",
connect_args={"timeout": 0.1, "check_same_thread": False},
poolclass=NullPool,
)
@event.listens_for(engine, "connect")
def enforce_foreign_keys(connection, _record):
connection.execute("PRAGMA foreign_keys=ON")
with engine.connect() as connection:
if request.param == "sqlcipher":
assert connection.exec_driver_sql("PRAGMA cipher_version").scalar()
assert (
connection.exec_driver_sql("PRAGMA journal_mode=WAL").scalar()
== "wal"
)
connection.exec_driver_sql("BEGIN")
Base.metadata.create_all(
connection,
tables=[m.__table__ for m in (*MODELS, RateLimitAttempt)],
)
connection.commit()
factory = sessionmaker(bind=engine)
owners = []
def create_session(owner, _password):
owners.append(owner)
return factory()
monkeypatch.setattr(db_manager, "open_user_database", lambda *_: engine)
monkeypatch.setattr(
db_manager, "create_thread_safe_session_for_metrics", create_session
)
try:
yield factory, owners
finally:
engine.dispose()
if request.param == "sqlcipher":
with path.open("rb") as db_file:
assert db_file.read(16) != b"SQLite format 3\x00"
@pytest.fixture(params=["main", "worker"])
def run_in_thread(request):
def run(action):
def isolated():
cleanup_current_thread()
try:
action()
finally:
cleanup_current_thread()
if request.param == "main":
isolated()
else:
with ThreadPoolExecutor(max_workers=1) as executor:
executor.submit(isolated).result(timeout=10)
return run
def record(kind, *, failing_token=False):
with search_context(CONTEXT):
if kind == "search":
SearchTracker.record_search("test", "telemetry", results_count=1)
elif kind == "rate":
tracker = AdaptiveRateLimitTracker(
settings_snapshot={"rate_limiting.enabled": True}
)
for _ in range(3):
tracker.record_outcome("test", 0.2, True, 0)
elif kind == "token":
callback = TokenCountingCallback(
research_id="r", research_context=CONTEXT
)
callback.current_model = "test-model"
callback.current_provider = (
None if failing_token else "test-provider"
)
callback._save_to_db(3, 2)
def counts(session):
return tuple(session.query(model).count() for model in MODELS)
def caller_write():
"""A row of the caller's own, in a table no telemetry here writes."""
return RateLimitAttempt(
engine_type="caller",
timestamp=1.0,
wait_time=0.0,
retry_count=0,
success=True,
)
def write_from_another_connection(factory):
"""Fails with "database is locked" while any writer lock is held."""
with factory() as other:
other.add(caller_write())
other.commit()
@pytest.mark.parametrize(
"order", list(permutations(("search", "rate", "token"))), ids="-".join
)
@pytest.mark.parametrize("finish", ["commit", "rollback", "scope_exit"])
def test_telemetry_in_write_scope_follows_caller_transaction(
database, run_in_thread, order, finish
):
factory, owners = database
def action():
with get_user_db_session("alice", "test-password") as session:
# The caller holds an open write transaction (and its writer lock).
session.add(caller_write())
session.flush()
for kind in order:
record(kind)
with factory() as observer:
assert counts(observer) == (0, 0, 0, 0)
assert owners == ["alice"]
assert counts(session) == (1, 1, 1, 1)
if finish != "scope_exit":
getattr(session, finish)()
run_in_thread(action)
with factory() as observer:
expected = 1 if finish == "commit" else 0
assert counts(observer) == (expected,) * 4
assert observer.query(RateLimitAttempt).count() == expected
@pytest.mark.parametrize(
"order", list(permutations(("search", "rate", "token"))), ids="-".join
)
@pytest.mark.parametrize("finish", ["commit", "rollback", "scope_exit"])
def test_telemetry_in_read_only_scope_commits_on_its_own(
database, run_in_thread, order, finish
):
factory, owners = database
def action():
with get_user_db_session("alice", "test-password") as session:
assert session.query(SearchCall).count() == 0
recorded = set()
for kind in order:
record(kind)
recorded.add(kind)
# Durable at once, and no writer lock outlives the write.
with factory() as observer:
assert counts(observer) == (
int("search" in recorded),
int("rate" in recorded),
int("token" in recorded),
int("token" in recorded),
)
write_from_another_connection(factory)
assert owners == ["alice"]
if finish != "scope_exit":
getattr(session, finish)()
run_in_thread(action)
with factory() as observer:
assert counts(observer) == (1, 1, 1, 1)
assert observer.query(RateLimitAttempt).count() == len(order)
def test_failed_telemetry_in_read_only_scope_releases_writer_lock(
database, run_in_thread
):
factory, owners = database
def action():
with get_user_db_session("alice", "test-password") as session:
assert session.query(SearchCall).count() == 0
# The failing INSERT takes the writer lock; ROLLBACK TO keeps it.
record("token", failing_token=True)
assert session.is_active
write_from_another_connection(factory)
record("search")
with factory() as observer:
assert counts(observer) == (1, 0, 0, 0)
assert owners == ["alice"]
run_in_thread(action)
with factory() as observer:
assert counts(observer) == (1, 0, 0, 0)
assert observer.query(RateLimitAttempt).count() == 1
@pytest.mark.parametrize("pending", ["search", "rate"])
def test_combined_telemetry_retains_pending_caller_work(
database, run_in_thread, pending
):
factory, owners = database
if pending == "rate":
with factory() as session:
session.add(
RateLimitEstimate(
engine_type="test",
base_wait_seconds=1,
min_wait_seconds=0.5,
max_wait_seconds=3,
last_updated=1,
total_attempts=5,
success_rate=1,
)
)
session.commit()
def action():
with get_user_db_session("alice", "test-password") as session:
if pending == "rate":
row = session.query(RateLimitEstimate).one()
row.max_wait_seconds = 7.5
else:
row = SearchCall(
research_id="r", search_engine="caller", query="pending"
)
session.add(row)
for kind in ("token", "rate", "search"):
record(kind)
assert row in session
assert row in (session.dirty if pending == "rate" else session.new)
assert owners == ["alice"]
session.commit()
run_in_thread(action)
with factory() as observer:
assert observer.query(TokenUsage).count() == 0
assert observer.query(ModelUsage).count() == 0
if pending == "rate":
assert (
observer.query(RateLimitEstimate).one().max_wait_seconds == 7.5
)
assert observer.query(SearchCall).count() == 0
else:
assert observer.query(SearchCall).one().query == "pending"
assert observer.query(RateLimitEstimate).count() == 1
def test_failed_middle_telemetry_leaves_caller_and_other_metrics_intact(
database, run_in_thread
):
factory, owners = database
def action():
with get_user_db_session("alice", "test-password") as session:
session.add(
SearchCall(
research_id="r", search_engine="caller", query="caller"
)
)
session.flush()
record("search")
record("token", failing_token=True)
assert session.is_active
record("rate")
record("search")
assert owners == ["alice"]
session.commit()
run_in_thread(action)
with factory() as observer:
assert observer.query(SearchCall).count() == 3
assert observer.query(RateLimitEstimate).count() == 1
assert observer.query(TokenUsage).count() == 0
assert observer.query(ModelUsage).count() == 0