~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
"""Attribution / correctness audit for the metrics + token-counting layer.
Scope of this file (PR #3299, Flask -> FastAPI port):
* every recorded metric must carry the owning username -- including
from background threads and scheduled jobs;
* cost calculation must be correct per provider pricing shape, and a
provider with no pricing entry must degrade rather than crash or
silently report zero;
* aggregates must never mix users;
* counters must not be double-incremented on retry, and a failed call
must not be recorded as a success.
Design notes
------------
No app boot, no Flask/FastAPI test client, no real LLM. The counting
layer is driven directly against real on-disk SQLite engines, one file
per username, standing in for the per-user encrypted databases. The two
seams the production code uses to reach a user's database are patched to
those engines:
* ``database.session_context.get_user_db_session`` -- the MainThread
write path and every read/aggregate path;
* ``database.encrypted_db.db_manager.create_thread_safe_session_for_metrics``
-- the background-thread write path. Patching *this* rather than
``metrics_writer`` keeps the real ``ThreadSafeMetricsWriter`` code
(username resolution, password gate, commit) under test.
Costs are asserted against hand-computed dollar values derived from the
static pricing table in ``pricing_fetcher._load_static_pricing`` -- never
by re-running the production formula.
Tests marked ``xfail(strict=True)`` assert the *correct* behaviour and
document a confirmed defect; they flip to a failure the moment the defect
is fixed, which is the signal to delete the marker.
"""
import asyncio
import datetime
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from unittest.mock import Mock
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from local_deep_research.database.models import (
ModelUsage,
RateLimitAttempt,
RateLimitEstimate,
SearchCall,
TokenUsage,
)
from local_deep_research.database.models.research import ResearchHistory
from local_deep_research.metrics.database import MetricsDatabase
from local_deep_research.metrics.pricing.cost_calculator import CostCalculator
from local_deep_research.metrics.pricing.pricing_fetcher import PricingFetcher
from local_deep_research.metrics.search_tracker import (
SEARCH_STATUS_SKIPPED,
SearchTracker,
)
from local_deep_research.metrics.token_counter import (
TokenCounter,
TokenCountingCallback,
)
from local_deep_research.utilities.request_context import request_user
from local_deep_research.utilities.thread_context import search_context
from local_deep_research.web_search_engines.rate_limiting import RateLimitError
from local_deep_research.web_search_engines.search_engine_base import (
BaseSearchEngine,
)
# Tables the metrics layer touches. ResearchHistory / RateLimit* are
# required because ``_get_metrics_from_encrypted_db`` queries them inside
# the same try/except that swallows every error into empty metrics --
# without them every assertion would trivially see zeros.
_METRICS_TABLES = [
m.__table__
for m in (
TokenUsage,
ModelUsage,
SearchCall,
RateLimitAttempt,
RateLimitEstimate,
ResearchHistory,
)
]
class UserDatabases:
"""One real on-disk SQLite database per username."""
def __init__(self, root):
self._root = root
self._engines = {}
def engine(self, username):
if username not in self._engines:
engine = create_engine(
f"sqlite:///{self._root}/{username}.db",
connect_args={"check_same_thread": False},
)
TokenUsage.metadata.create_all(engine, tables=_METRICS_TABLES)
self._engines[username] = engine
return self._engines[username]
def session(self, username):
return sessionmaker(bind=self.engine(username))()
@contextmanager
def read(self, username):
session = self.session(username)
try:
yield session
finally:
session.close()
def rows(self, username, model):
with self.read(username) as session:
return session.query(model).order_by(model.id).all()
def created_usernames(self):
return set(self._engines)
def dispose(self):
for engine in self._engines.values():
engine.dispose()
@pytest.fixture
def user_dbs(tmp_path, monkeypatch):
"""Route both production database seams at per-user SQLite files."""
import local_deep_research.database.session_context as session_context
import local_deep_research.database.thread_metrics as thread_metrics
import local_deep_research.metrics.database as metrics_database
dbs = UserDatabases(tmp_path)
@contextmanager
def _get_user_db_session(username, password=None, *args, **kwargs):
session = dbs.session(username)
try:
yield session
finally:
session.close()
monkeypatch.setattr(
session_context, "get_user_db_session", _get_user_db_session
)
# metrics/database.py binds get_user_db_session at module import time,
# so the rebind above does not reach SearchTracker's read paths.
monkeypatch.setattr(
metrics_database, "get_user_db_session", _get_user_db_session
)
monkeypatch.setattr(
thread_metrics.db_manager,
"create_thread_safe_session_for_metrics",
lambda username, password: dbs.session(username),
)
try:
yield dbs
finally:
dbs.dispose()
# --------------------------------------------------------------------------
# Stub LLM responses. The model boundary is never crossed.
# --------------------------------------------------------------------------
class StubLLMResult:
"""LangChain ``LLMResult`` shape carrying provider-reported usage."""
def __init__(self, prompt_tokens, completion_tokens, total_tokens=None):
self.llm_output = {
"token_usage": {
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": (
prompt_tokens + completion_tokens
if total_tokens is None
else total_tokens
),
}
}
self.generations = []
class _StubOllamaMessage:
usage_metadata = None
def __init__(self, metadata):
self.response_metadata = metadata
class _StubGeneration:
def __init__(self, metadata):
self.message = _StubOllamaMessage(metadata)
class StubOllamaResult:
"""Ollama shape: usage arrives via ``response_metadata``."""
llm_output = None
def __init__(self, prompt_eval_count, eval_count, total_duration):
self.generations = [
[
_StubGeneration(
{
"prompt_eval_count": prompt_eval_count,
"eval_count": eval_count,
"total_duration": total_duration,
"load_duration": 1_000,
"prompt_eval_duration": 2_000,
"eval_duration": 3_000,
}
)
]
]
def make_callback(research_id, username, password="pw", **extra):
"""Callback whose research context names ``username`` as the owner."""
context = {
"username": username,
"user_password": password,
"research_query": f"query owned by {username}",
"research_mode": "quick",
"research_phase": "search",
"search_iteration": 1,
}
context.update(extra)
callback = TokenCountingCallback(
research_id=research_id, research_context=context
)
callback.preset_model = "gpt-4o"
callback.preset_provider = "openai"
return callback
def seed_other_user(dbs, username="mallory"):
"""Give every isolation assertion something it could fail against.
Without a second user's rows on disk, "no rows leaked to the other
user" is satisfied by an empty world and proves nothing.
"""
with dbs.read(username) as session:
session.add(
TokenUsage(
research_id="mallory-research",
model_name="gpt-4o",
model_provider="openai",
prompt_tokens=99_000,
completion_tokens=11_000,
total_tokens=110_000,
research_query="mallory's private query",
research_mode="quick",
success_status="success",
)
)
session.add(
SearchCall(
research_id="mallory-research",
search_engine="mallory-engine",
query="mallory's private search",
results_count=42,
response_time_ms=10,
success_status="success",
)
)
session.commit()
return username
# ==========================================================================
# 1. Attribution: does every recorded metric carry the owning username?
# ==========================================================================
def test_background_thread_attributes_tokens_to_research_context_owner(
user_dbs,
):
"""Worker-thread path is correct: it uses research_context["username"]."""
other = seed_other_user(user_dbs)
callback = make_callback("research-carol", "carol")
def run_llm_call():
callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r1")
callback.on_llm_end(StubLLMResult(700, 300), run_id="r1")
worker = threading.Thread(target=run_llm_call, name="ldr-worker-1")
worker.start()
worker.join()
carol_rows = user_dbs.rows("carol", TokenUsage)
assert len(carol_rows) == 1
assert carol_rows[0].total_tokens == 1000
assert carol_rows[0].research_query == "query owned by carol"
# The seeded user must be untouched -- one row, unchanged.
other_rows = user_dbs.rows(other, TokenUsage)
assert len(other_rows) == 1
assert other_rows[0].research_id == "mallory-research"
@pytest.mark.parametrize("request_owner", ["bob", "alice", None])
def test_mainthread_tokens_must_not_land_in_another_users_database(
user_dbs, monkeypatch, request_owner
):
import local_deep_research.database.session_context as session_context
seed_other_user(user_dbs)
callback = make_callback(
"research-alice", "alice", password="synthetic-alice-database-key"
)
opened = []
@contextmanager
def owner_session(username, password=None):
opened.append((username, password))
assert (username, password) == (
"alice",
"synthetic-alice-database-key",
)
with user_dbs.read(username) as session:
yield session
monkeypatch.setattr(session_context, "get_user_db_session", owner_session)
# A different, matching, or absent request cannot change the research owner.
with request_user(request_owner):
callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r1")
callback.on_llm_end(StubLLMResult(100, 50), run_id="r1")
alice_rows = user_dbs.rows("alice", TokenUsage)
bob_rows = user_dbs.rows("bob", TokenUsage)
assert [r.total_tokens for r in bob_rows] == [], (
"alice's token usage leaked into bob's database"
)
assert len(alice_rows) == 1
assert alice_rows[0].total_tokens == 150
assert alice_rows[0].research_query == "query owned by alice"
assert opened == [("alice", "synthetic-alice-database-key")]
def test_mainthread_ownerless_callback_retains_request_scoping(
user_dbs, monkeypatch
):
import local_deep_research.database.session_context as session_context
opened = []
existing_session = session_context.get_user_db_session
@contextmanager
def request_session(username, password=None):
opened.append((username, password))
assert username == "bob" and password is None
with existing_session(username, password) as session:
yield session
monkeypatch.setattr(session_context, "get_user_db_session", request_session)
callback = TokenCountingCallback(
research_id="request-owned-research",
research_context={
"research_query": "request-owned query",
"user_password": "unowned-snapshot-password",
},
)
callback.preset_provider = "openai"
callback.preset_model = "gpt-4o"
with request_user("bob"):
callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r2")
callback.on_llm_end(StubLLMResult(25, 50), run_id="r2")
assert user_dbs.rows("alice", TokenUsage) == []
rows = user_dbs.rows("bob", TokenUsage)
assert len(rows) == 1
assert rows[0].total_tokens == 75
assert rows[0].research_query == "request-owned query"
assert opened == [("bob", None)]
def test_mainthread_failed_owner_open_cannot_fall_back_to_request_user(
user_dbs, monkeypatch
):
import local_deep_research.database.session_context as session_context
opened = []
def unavailable_owner(username, password=None):
opened.append((username, password))
raise session_context.DatabaseSessionError("Owner database unavailable")
monkeypatch.setattr(
session_context, "get_user_db_session", unavailable_owner
)
callback = make_callback(
"research-alice", "alice", password="synthetic-unavailable-owner-key"
)
with request_user("bob"):
callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r3")
callback.on_llm_end(StubLLMResult(100, 50), run_id="r3")
assert opened == [("alice", "synthetic-unavailable-owner-key")]
assert user_dbs.rows("alice", TokenUsage) == []
assert user_dbs.rows("bob", TokenUsage) == []
def test_scheduled_job_tokens_are_recorded_for_the_context_owner(user_dbs):
seed_other_user(user_dbs)
callback = make_callback("scheduled-research", "alice")
# No request_user(...) wrapper: this is the scheduler-job shape.
callback.on_llm_start({"_type": "ChatOpenAI"}, ["prompt"], run_id="r1")
callback.on_llm_end(StubLLMResult(400, 100), run_id="r1")
alice_rows = user_dbs.rows("alice", TokenUsage)
assert len(alice_rows) == 1
assert alice_rows[0].total_tokens == 500
def test_search_record_ignores_tracker_instance_db_and_uses_thread_context(
user_dbs,
):
"""What ``record_search`` appears to record vs what it records.
``SearchTracker.__init__`` takes a ``MetricsDatabase`` and stores it on
``self.db``, so a caller reasonably reads ``SearchTracker(db=...)`` as
"record into that database". ``record_search`` is a ``@staticmethod``:
it can never see ``self.db``. The owning username comes solely from
the ambient ``get_search_context()`` contextvar.
"""
other = seed_other_user(user_dbs)
tracker = SearchTracker(
db=MetricsDatabase(username=other, password="mallory-pw")
)
with search_context(
{
"research_id": 7,
"research_query": "cats",
"research_mode": "quick",
"username": "alice",
"user_password": "pw",
"search_iteration": 2,
}
):
tracker.record_search(
"searxng", "cats", results_count=5, response_time_ms=120
)
alice_rows = user_dbs.rows("alice", SearchCall)
assert [(r.search_engine, r.query) for r in alice_rows] == [
("searxng", "cats")
]
# research_id is normalised int -> str for the String(36) column.
assert alice_rows[0].research_id == "7"
assert alice_rows[0].search_iteration == 2
# The instance's MetricsDatabase(username=mallory) had no effect.
assert [r.query for r in user_dbs.rows(other, SearchCall)] == [
"mallory's private search"
]
@pytest.mark.parametrize(
"context,label",
[
(None, "no research context at all"),
(
{"research_id": "8", "user_password": "pw"},
"context without username",
),
({"research_id": "9", "username": "alice"}, "context without password"),
],
)
def test_search_metrics_are_dropped_without_full_credentials(
user_dbs, context, label
):
"""Missing username or password => no row, no exception, warning only."""
other = seed_other_user(user_dbs)
if context is None:
SearchTracker.record_search("brave", f"probe: {label}", results_count=1)
else:
with search_context(context):
SearchTracker.record_search(
"brave", f"probe: {label}", results_count=1
)
assert user_dbs.rows("alice", SearchCall) == []
# The seeded user proves we are not merely looking at an empty world.
assert len(user_dbs.rows(other, SearchCall)) == 1
# ==========================================================================
# 2. Aggregates must never mix users
# ==========================================================================
def seed_token_rows(dbs, username, rows):
with dbs.read(username) as session:
for research_id, model, provider, prompt, completion, mode in rows:
session.add(
TokenUsage(
research_id=research_id,
model_name=model,
model_provider=provider,
prompt_tokens=prompt,
completion_tokens=completion,
total_tokens=prompt + completion,
research_query=f"{username}: {research_id}",
research_mode=mode,
success_status="success",
)
)
session.add(
ModelUsage(
model_name=model,
model_provider=provider,
total_tokens=prompt + completion,
total_calls=1,
)
)
session.commit()
def test_overall_metrics_never_mix_users(user_dbs):
seed_token_rows(
user_dbs,
"alice",
[
("a1", "gpt-4o", "openai", 100, 50, "quick"),
("a2", "gpt-4o", "openai", 200, 100, "quick"),
],
)
seed_token_rows(
user_dbs,
"bob",
[("b1", "gpt-4o", "openai", 7_000, 3_000, "quick")],
)
counter = TokenCounter()
alice = counter.get_overall_metrics(period="30d", username="alice")
bob = counter.get_overall_metrics(period="30d", username="bob")
# 100+50 + 200+100 = 450, hand-computed; bob's 10_000 must not appear.
assert alice["total_tokens"] == 450
assert bob["total_tokens"] == 10_000
assert alice["by_model"][0]["prompt_tokens"] == 300
assert alice["by_model"][0]["completion_tokens"] == 150
assert alice["by_model"][0]["calls"] == 2
def test_research_metrics_are_scoped_to_the_owning_user(user_dbs):
seed_token_rows(
user_dbs, "alice", [("a1", "gpt-4o", "openai", 100, 50, "quick")]
)
seed_token_rows(
user_dbs, "bob", [("b1", "gpt-4o", "openai", 7_000, 3_000, "quick")]
)
counter = TokenCounter()
assert (
counter.get_research_metrics("a1", username="alice")["total_tokens"]
== 150
)
# bob's research id, queried against alice's database: nothing.
bobs_research_as_alice = counter.get_research_metrics(
"b1", username="alice"
)
assert bobs_research_as_alice["total_tokens"] == 0
assert bobs_research_as_alice["model_usage"] == []
# ...and it is genuinely visible to its owner, so the zero above is
# isolation rather than absence.
assert (
counter.get_research_metrics("b1", username="bob")["total_tokens"]
== 10_000
)
def test_search_metrics_aggregate_per_engine_within_one_user(user_dbs):
"""``get_search_metrics`` groups by search ENGINE, not by user.
There is no username column on ``search_calls``; user scoping comes
entirely from which database file is opened.
"""
other = seed_other_user(user_dbs)
with user_dbs.read("alice") as session:
for engine, ok in [
("searxng", True),
("searxng", True),
("searxng", False),
("brave", True),
]:
session.add(
SearchCall(
research_id="a1",
search_engine=engine,
query="q",
results_count=10 if ok else 0,
response_time_ms=100,
research_mode="quick",
success_status="success" if ok else "error",
)
)
session.commit()
metrics = SearchTracker().get_search_metrics(period="30d", username="alice")
by_engine = {s["engine"]: s for s in metrics["search_engine_stats"]}
assert set(by_engine) == {"searxng", "brave"}
assert by_engine["searxng"]["call_count"] == 3
# 2 of 3 succeeded -> 66.666...%, hand-computed.
assert by_engine["searxng"]["success_rate"] == pytest.approx(200 / 3)
assert by_engine["searxng"]["error_count"] == 1
assert by_engine["searxng"]["total_results"] == 20
assert by_engine["brave"]["success_rate"] == 100.0
# mallory's engine must not appear in alice's aggregate.
assert "mallory-engine" not in by_engine
assert "mallory-engine" in {
s["engine"]
for s in SearchTracker().get_search_metrics(
period="30d", username=other
)["search_engine_stats"]
}
def test_overall_metrics_period_all_returns_real_totals(user_dbs):
seed_token_rows(
user_dbs,
"alice",
[
("a1", "gpt-4o", "openai", 100, 50, "quick"),
("a2", "gpt-4o", "openai", 200, 100, "quick"),
],
)
counter = TokenCounter()
# Sanity: the bounded periods do see the data.
assert (
counter.get_overall_metrics(period="30d", username="alice")[
"total_tokens"
]
== 450
)
all_time = counter.get_overall_metrics(period="all", username="alice")
assert all_time["total_tokens"] == 450
assert all_time["by_model"] != []
def test_period_all_counts_rate_limit_attempts_of_every_age(user_dbs):
"""'all' has no cutoff: an attempt older than a year still counts."""
now = time.time()
session = user_dbs.session("alice")
session.add_all(
[
RateLimitAttempt(
engine_type="searxng",
timestamp=now - 60,
wait_time=1.0,
retry_count=0,
success=True,
),
RateLimitAttempt(
engine_type="arxiv",
timestamp=now - 400 * 24 * 3600,
wait_time=3.0,
retry_count=1,
success=False,
error_type="RateLimitError",
),
]
)
session.commit()
session.close()
counter = TokenCounter()
last_year = counter.get_overall_metrics(period="1y", username="alice")
assert last_year["rate_limiting"]["total_attempts"] == 1
assert last_year["rate_limiting"]["tracked_engines"] == 1
all_time = counter.get_overall_metrics(period="all", username="alice")
assert all_time["rate_limiting"]["total_attempts"] == 2
assert all_time["rate_limiting"]["rate_limit_events"] == 1
assert all_time["rate_limiting"]["tracked_engines"] == 2
def test_empty_metrics_shape_matches_populated_shape(user_dbs):
seed_token_rows(
user_dbs, "alice", [("a1", "gpt-4o", "openai", 100, 50, "quick")]
)
counter = TokenCounter()
populated = counter.get_overall_metrics(period="30d", username="alice")
# No username -> _get_empty_metrics() by the documented early return.
empty = counter.get_overall_metrics(period="30d", username=None)
assert set(empty) == set(populated)
assert set(empty["token_breakdown"]) == set(populated["token_breakdown"])
assert set(empty["rate_limiting"]) == set(populated["rate_limiting"])
@pytest.mark.parametrize("path", ["no_user", "database_failure"])
def test_empty_metrics_fallback_matches_an_empty_user_database(
user_dbs, monkeypatch, path
):
counter = TokenCounter()
expected = counter.get_overall_metrics(username="alice")
assert expected["total_tokens"] == 0
assert expected["rate_limiting"]["total_attempts"] == 0
if path == "no_user":
monkeypatch.setattr(
"local_deep_research.metrics.token_counter.get_current_username",
lambda: None,
)
actual = counter.get_overall_metrics()
else:
def unavailable(*args, **kwargs):
raise RuntimeError("Database unavailable")
monkeypatch.setattr(
"local_deep_research.database.session_context.get_user_db_session",
unavailable,
)
actual = counter.get_overall_metrics(username="alice")
assert actual == expected
# ==========================================================================
# 3. Failure accounting and retry counting
# ==========================================================================
def test_success_status_does_not_stick_after_an_earlier_failure(user_dbs):
callback = make_callback("research-alice", "alice")
with request_user("alice"):
callback.on_llm_start({"_type": "ChatOpenAI"}, ["p"], run_id="r1")
callback.on_llm_error(RuntimeError("upstream 500"), run_id="r1")
callback.on_llm_start({"_type": "ChatOpenAI"}, ["p"], run_id="r2")
callback.on_llm_end(StubLLMResult(100, 50), run_id="r2")
rows = user_dbs.rows("alice", TokenUsage)
assert len(rows) == 2
failed, succeeded = rows
assert failed.success_status == "error"
assert failed.error_type == "RuntimeError"
assert failed.total_tokens == 0
assert succeeded.total_tokens == 150
assert succeeded.success_status == "success"
assert succeeded.error_type is None
def test_ollama_raw_metrics_do_not_bleed_into_the_next_providers_row(user_dbs):
callback = make_callback("research-alice", "alice")
with request_user("alice"):
callback.preset_model, callback.preset_provider = "llama3", "ollama"
callback.on_llm_start({"_type": "ChatOllama"}, ["p"], run_id="r1")
callback.on_llm_end(
StubOllamaResult(
prompt_eval_count=900, eval_count=100, total_duration=555
),
run_id="r1",
)
callback.preset_model, callback.preset_provider = "gpt-4o", "openai"
callback.on_llm_start({"_type": "ChatOpenAI"}, ["p"], run_id="r2")
callback.on_llm_end(StubLLMResult(10, 4), run_id="r2")
ollama_row, openai_row = user_dbs.rows("alice", TokenUsage)
assert ollama_row.model_provider == "ollama"
assert ollama_row.ollama_prompt_eval_count == 900
assert ollama_row.ollama_eval_count == 100
assert openai_row.model_provider == "openai"
assert openai_row.prompt_tokens == 10
assert openai_row.ollama_prompt_eval_count is None
assert openai_row.ollama_eval_count is None
assert openai_row.ollama_total_duration is None
def raw_ollama_values(row):
return (
row.ollama_prompt_eval_count,
row.ollama_eval_count,
row.ollama_total_duration,
row.ollama_load_duration,
row.ollama_prompt_eval_duration,
row.ollama_eval_duration,
)
@pytest.mark.parametrize("background", [False, True])
@pytest.mark.parametrize("registered", [False, True])
@pytest.mark.parametrize(
"second_kind", ["token_usage", "usage_metadata", "missing", "error", "raw"]
)
def test_raw_ollama_metrics_belong_only_to_the_response_that_supplied_them(
user_dbs, background, registered, second_kind
):
callback = make_callback("research-alice", "alice")
callback.preset_model, callback.preset_provider = "llama3", "ollama"
def record_calls():
with request_user("alice"):
callback.on_llm_start({"_type": "ChatOllama"}, ["p"], run_id="r1")
callback.on_llm_end(StubOllamaResult(900, 100, 555), run_id="r1")
kwargs = {"run_id": "r2"} if registered else {}
if registered:
callback.on_llm_start({"_type": "ChatOllama"}, ["p"], **kwargs)
if second_kind == "error":
callback.on_llm_error(
TimeoutError("upstream timeout"), **kwargs
)
return
if second_kind == "token_usage":
response = StubLLMResult(10, 4)
elif second_kind == "raw":
response = StubOllamaResult(0, 2, 0)
else:
response = StubOllamaResult(0, 0, 0)
message = response.generations[0][0].message
message.response_metadata = {}
if second_kind == "usage_metadata":
message.usage_metadata = {
"input_tokens": 10,
"output_tokens": 4,
"total_tokens": 14,
}
callback.on_llm_end(response, **kwargs)
if background:
with ThreadPoolExecutor(max_workers=1) as pool:
pool.submit(record_calls).result(timeout=10)
else:
record_calls()
first, second = user_dbs.rows("alice", TokenUsage)
assert raw_ollama_values(first) == (900, 100, 555, 1_000, 2_000, 3_000)
if second_kind == "raw":
assert raw_ollama_values(second) == (0, 2, 0, 1_000, 2_000, 3_000)
assert second.total_tokens == 2
else:
assert raw_ollama_values(second) == (None,) * 6
assert second.total_tokens == (
14 if second_kind in {"token_usage", "usage_metadata"} else 0
)
if second_kind == "error":
assert second.success_status == "error"
assert second.error_type == "TimeoutError"
@pytest.mark.parametrize("second_has_raw_metrics", [False, True])
def test_overlapping_completions_keep_their_own_raw_metrics(
user_dbs, monkeypatch, second_has_raw_metrics
):
"""Finish a second response while the first waits to write its row."""
callback = make_callback("research-alice", "alice")
callback.preset_model, callback.preset_provider = "llama3", "ollama"
callback.on_llm_start({"_type": "ChatOllama"}, ["p"], run_id="r1")
callback.on_llm_start({"_type": "ChatOllama"}, ["p"], run_id="r2")
user_dbs.engine("alice")
first_waiting = threading.Event()
release_first = threading.Event()
save = callback._save_to_db
def pause_first_save(prompt_tokens, completion_tokens, *args, **kwargs):
if prompt_tokens == 900:
first_waiting.set()
assert release_first.wait(timeout=10)
return save(prompt_tokens, completion_tokens, *args, **kwargs)
monkeypatch.setattr(callback, "_save_to_db", pause_first_save)
second_response = (
StubOllamaResult(10, 4, 777)
if second_has_raw_metrics
else StubLLMResult(10, 4)
)
with ThreadPoolExecutor(max_workers=2) as pool:
first = pool.submit(
callback.on_llm_end, StubOllamaResult(900, 100, 555), run_id="r1"
)
try:
assert first_waiting.wait(timeout=10)
pool.submit(
callback.on_llm_end, second_response, run_id="r2"
).result(timeout=10)
finally:
release_first.set()
first.result(timeout=10)
rows = {r.prompt_tokens: r for r in user_dbs.rows("alice", TokenUsage)}
assert set(rows) == {900, 10}
assert raw_ollama_values(rows[900]) == (900, 100, 555, 1_000, 2_000, 3_000)
assert raw_ollama_values(rows[10]) == (
(10, 4, 777, 1_000, 2_000, 3_000)
if second_has_raw_metrics
else (None,) * 6
)
def test_retry_attempts_are_counted_once_each_not_double_incremented(user_dbs):
"""One row and one call-count increment per real attempt.
Two failed attempts followed by a success must yield three rows and
three in-memory calls, with the successful attempt's tokens counted
exactly once.
"""
callback = make_callback("research-alice", "alice")
with request_user("alice"):
for run_id in ("attempt-1", "attempt-2"):
callback.on_llm_start({"_type": "ChatOpenAI"}, ["p"], run_id=run_id)
callback.on_llm_error(TimeoutError("rate limited"), run_id=run_id)
callback.on_llm_start(
{"_type": "ChatOpenAI"}, ["p"], run_id="attempt-3"
)
callback.on_llm_end(StubLLMResult(100, 50), run_id="attempt-3")
rows = user_dbs.rows("alice", TokenUsage)
assert len(rows) == 3
assert [r.total_tokens for r in rows] == [0, 0, 150]
counts = callback.get_counts()
assert counts["by_model"]["gpt-4o"]["calls"] == 3
# The successful attempt's tokens must be counted exactly once.
assert counts["total_tokens"] == 150
assert counts["total_prompt_tokens"] == 100
assert counts["total_completion_tokens"] == 50
def test_failed_search_is_not_stored_as_a_success(user_dbs):
with search_context(
{
"research_id": "1",
"research_query": "q",
"username": "alice",
"user_password": "pw",
}
):
SearchTracker.record_search(
"searxng", "cats", results_count=5, response_time_ms=120
)
SearchTracker.record_search(
"searxng",
"dogs",
results_count=0,
response_time_ms=90,
success=False,
error_message="HTTP 503 from upstream",
)
ok_row, failed_row = user_dbs.rows("alice", SearchCall)
assert ok_row.success_status == "success"
assert ok_row.success == 1
assert failed_row.success_status == "error"
assert failed_row.success == 0
def test_search_error_type_reflects_the_actual_error(user_dbs):
"""Callers supply the class separately from the sanitized message."""
with search_context(
{"research_id": "1", "username": "alice", "user_password": "pw"}
):
SearchTracker.record_search(
"searxng",
"dogs",
success=False,
error_message="HTTP 503 from upstream",
error_type="HTTPError",
)
(row,) = user_dbs.rows("alice", SearchCall)
assert row.error_message == "HTTP 503 from upstream"
assert row.error_type == "HTTPError"
@pytest.mark.parametrize(
"success,status,expected_success,expected_status,expected_type",
[
(True, None, 1, "success", None),
(False, None, 0, "error", None),
(True, "error", 0, "error", None),
(False, "success", 1, "success", None),
(
True,
SEARCH_STATUS_SKIPPED,
0,
SEARCH_STATUS_SKIPPED,
"engine_unavailable",
),
],
)
def test_search_status_override_keeps_persisted_columns_consistent(
user_dbs, success, status, expected_success, expected_status, expected_type
):
with search_context(
{"research_id": "1", "username": "alice", "user_password": "pw"}
):
SearchTracker.record_search(
"searxng", "q", success=success, status=status
)
(row,) = user_dbs.rows("alice", SearchCall)
assert (row.success, row.success_status, row.error_type) == (
expected_success,
expected_status,
expected_type,
)
def test_search_error_text_does_not_invent_an_exception_class(user_dbs):
with search_context(
{"research_id": "1", "username": "alice", "user_password": "pw"}
):
SearchTracker.record_search(
"searxng", "q", success=False, error_message="TimeoutError HTTP 503"
)
(row,) = user_dbs.rows("alice", SearchCall)
assert row.error_type == "unknown_error"
assert row.error_message == "TimeoutError HTTP 503"
class OutcomeSearchEngine(BaseSearchEngine):
"""Run the real search and metrics lifecycle with a controlled provider."""
def __init__(self, outcomes, *, rate_limiting=False, content_error=None):
super().__init__(
programmatic_mode=False,
settings_snapshot={},
search_snippets_only=content_error is None,
)
self.outcomes = iter(outcomes)
self.attempts = 0
self.content_error = content_error
self.rate_tracker = Mock(enabled=rate_limiting)
self.rate_tracker.get_wait_time.return_value = 0.0
def _get_previews(self, query):
self.attempts += 1
outcome = next(self.outcomes)
if isinstance(outcome, Exception):
raise outcome
return outcome
def _get_full_content(self, items):
if self.content_error:
raise self.content_error
return items
@pytest.mark.parametrize(
"error_class,rate_limiting,expected_attempts",
[
(ValueError, False, 1),
(TimeoutError, False, 1),
(RateLimitError, False, 1),
(RateLimitError, True, 3),
],
)
def test_search_engine_persists_exception_type_and_sanitized_message(
user_dbs, monkeypatch, error_class, rate_limiting, expected_attempts
):
engine = OutcomeSearchEngine(
[error_class("provider detail")] * expected_attempts,
rate_limiting=rate_limiting,
)
monkeypatch.setattr(
engine, "_scrub_error", lambda error: "sanitized failure"
)
assert (
engine.run(
"q",
research_context={
"research_id": "1",
"username": "alice",
"user_password": "pw",
},
)
== []
)
assert engine.attempts == expected_attempts
(row,) = user_dbs.rows("alice", SearchCall)
assert row.error_type == error_class.__name__
assert row.error_message == (
"Rate limit hit but rate limiting disabled"
if error_class is RateLimitError and not rate_limiting
else "sanitized failure"
)
assert row.success_status == "error"
assert row.success == 0
@pytest.mark.parametrize(
"results", [[], [{"title": "result", "snippet": "text"}]]
)
def test_search_engine_records_success_after_previous_failure(
user_dbs, results
):
engine = OutcomeSearchEngine([ValueError("provider failure"), results])
context = {"research_id": "1", "username": "alice", "user_password": "pw"}
assert engine.run("first", research_context=context) == []
assert engine.run("second", research_context=context) == results
failed, succeeded = user_dbs.rows("alice", SearchCall)
assert (failed.success, failed.error_type) == (0, "ValueError")
assert succeeded.success == 1
assert succeeded.success_status == "success"
assert succeeded.error_type is None
assert succeeded.error_message is None
assert succeeded.results_count == len(results)
def test_search_engine_recovered_retry_records_one_success(user_dbs):
results = [{"title": "result", "snippet": "text"}]
engine = OutcomeSearchEngine(
[RateLimitError("retry"), results], rate_limiting=True
)
assert (
engine.run(
"q",
research_context={
"research_id": "1",
"username": "alice",
"user_password": "pw",
},
)
== results
)
assert engine.attempts == 2
(row,) = user_dbs.rows("alice", SearchCall)
assert row.success == 1
assert row.success_status == "success"
assert row.error_type is None
assert row.error_message is None
assert row.results_count == 1
def test_search_engine_content_failure_preserves_exception_type(user_dbs):
engine = OutcomeSearchEngine(
[[{"title": "result", "snippet": "text"}]],
content_error=ValueError("content conversion failed"),
)
assert (
engine.run(
"q",
research_context={
"research_id": "1",
"username": "alice",
"user_password": "pw",
},
)
== []
)
(row,) = user_dbs.rows("alice", SearchCall)
assert row.success == 0
assert row.success_status == "error"
assert row.error_type == "ValueError"
assert row.error_message == "content conversion failed"
@pytest.mark.parametrize("worker_thread", [False, True])
def test_model_usage_keeps_providers_separate(user_dbs, worker_thread):
direct = make_callback("research-1", "alice")
proxied = make_callback("research-2", "alice")
proxied.preset_provider = "openrouter"
def record_calls():
with request_user("alice"):
direct.on_llm_start({"_type": "ChatOpenAI"}, ["p"], run_id="r1")
direct.on_llm_end(StubLLMResult(100, 50), run_id="r1")
proxied.on_llm_start({"_type": "X"}, ["p"], run_id="r2")
proxied.on_llm_end(StubLLMResult(10, 5), run_id="r2")
direct.on_llm_start({"_type": "ChatOpenAI"}, ["p"], run_id="r3")
direct.on_llm_end(StubLLMResult(20, 10), run_id="r3")
if worker_thread:
with ThreadPoolExecutor(max_workers=1) as pool:
pool.submit(record_calls).result(timeout=10)
else:
record_calls()
usage = {
(m.model_name, m.model_provider): m
for m in user_dbs.rows("alice", ModelUsage)
}
assert set(usage) == {("gpt-4o", "openai"), ("gpt-4o", "openrouter")}
assert usage[("gpt-4o", "openai")].total_tokens == 180
assert usage[("gpt-4o", "openai")].total_calls == 2
assert usage[("gpt-4o", "openrouter")].total_tokens == 15
assert usage[("gpt-4o", "openrouter")].total_calls == 1
@pytest.mark.parametrize(
"aggregate_state", ["missing", "combined", "wrong_provider"]
)
def test_dashboard_groups_usage_by_provider_without_trusting_aggregate_rows(
user_dbs, aggregate_state
):
seed_token_rows(
user_dbs,
"alice",
[
("a1", "gpt-4o", "openai", 100, 50, "quick"),
("a2", "gpt-4o", "openrouter", 10, 5, "quick"),
("a3", "gpt-4o", "openai", 20, 10, "quick"),
],
)
seed_other_user(user_dbs)
with user_dbs.read("alice") as session:
session.query(ModelUsage).delete()
if aggregate_state != "missing":
session.add(
ModelUsage(
model_name="gpt-4o",
model_provider="openai"
if aggregate_state == "combined"
else "wrong",
total_tokens=195,
total_calls=3,
)
)
session.commit()
result = TokenCounter().get_overall_metrics(username="alice")
assert result["total_tokens"] == 195
assert result["by_model"] == [
{
"model": "gpt-4o",
"provider": "openai",
"tokens": 180,
"calls": 2,
"prompt_tokens": 120,
"completion_tokens": 60,
},
{
"model": "gpt-4o",
"provider": "openrouter",
"tokens": 15,
"calls": 1,
"prompt_tokens": 10,
"completion_tokens": 5,
},
]
@pytest.mark.parametrize(
"period,mode,expected",
[
("30d", "quick", {"openai": 150}),
("30d", "detailed", {"openrouter": 15}),
("30d", "all", {"openai": 150, "openrouter": 15}),
("all", "quick", {"openai": 150, "ollama": 1500}),
],
)
def test_dashboard_provider_totals_preserve_period_and_mode_filters(
user_dbs, period, mode, expected
):
seed_token_rows(
user_dbs,
"alice",
[
("recent", "gpt-4o", "openai", 100, 50, "quick"),
("detailed", "gpt-4o", "openrouter", 10, 5, "detailed"),
("old", "gpt-4o", "ollama", 1000, 500, "quick"),
],
)
with user_dbs.read("alice") as session:
old = session.query(TokenUsage).filter_by(research_id="old").one()
old.timestamp = datetime.datetime.now(
datetime.UTC
) - datetime.timedelta(days=90)
session.commit()
result = TokenCounter().get_overall_metrics(period, mode, username="alice")
assert {
row["provider"]: row["tokens"] for row in result["by_model"]
} == expected
assert result["total_tokens"] == sum(expected.values())
# ==========================================================================
# 4. Cost calculation
#
# Expected dollar figures below are hand-computed from
# pricing_fetcher._load_static_pricing (USD per 1K tokens):
# gpt-4 prompt 0.03 completion 0.06
# gpt-4o-mini prompt 0.00015 completion 0.0006
# claude-3-opus prompt 0.015 completion 0.075
# They are NOT recomputed with the production formula.
# ==========================================================================
def offline_calculator():
"""CostCalculator wired to static pricing only -- no network."""
calculator = CostCalculator()
# PricingFetcher.get_model_pricing consults self.static_pricing only;
# self.session (aiohttp) is never touched on these paths.
calculator.pricing_fetcher = PricingFetcher()
return calculator
@pytest.mark.parametrize(
"model,provider,prompt,completion,expected_prompt,expected_completion,expected_total",
[
# 1.5 * 0.03 = 0.045 ; 0.5 * 0.06 = 0.03 ; total 0.075
("gpt-4", "openai", 1_500, 500, 0.045, 0.03, 0.075),
# 1000 * 0.00015 = 0.15 ; 200 * 0.0006 = 0.12 ; total 0.27
(
"gpt-4o-mini",
"openai",
1_000_000,
200_000,
0.15,
0.12,
0.27,
),
# 3.333 * 0.015 = 0.049995 ; 0.777 * 0.075 = 0.058275 ; total 0.10827
(
"claude-3-opus",
"anthropic",
3_333,
777,
0.049995,
0.058275,
0.10827,
),
],
)
def test_cost_matches_hand_computed_values_for_known_pricing(
model,
provider,
prompt,
completion,
expected_prompt,
expected_completion,
expected_total,
):
calculator = offline_calculator()
result = asyncio.run(
calculator.calculate_cost(model, prompt, completion, provider)
)
assert result["prompt_cost"] == expected_prompt
assert result["completion_cost"] == expected_completion
assert result["total_cost"] == expected_total
assert result["pricing_used"] is not None
def test_local_provider_is_priced_free_without_a_pricing_entry():
"""A local provider degrades to genuine zero, not to a missing entry."""
calculator = offline_calculator()
result = asyncio.run(
calculator.calculate_cost("llama3.3:70b", 1_000, 1_000, "ollama")
)
assert result["total_cost"] == 0.0
assert result["pricing_used"] == {"prompt": 0.0, "completion": 0.0}
assert "error" not in result
def test_unknown_model_degrades_with_an_explicit_error_marker():
"""No pricing entry: zero cost, but flagged -- not silently free."""
calculator = offline_calculator()
result = asyncio.run(
calculator.calculate_cost("mystery-9000", 1_000, 1_000, "openai")
)
assert result["total_cost"] == 0.0
assert result["pricing_used"] is None
assert result["error"] == "No pricing data available for this model"
def test_pricing_falls_back_to_model_name_when_provider_has_no_entry():
"""Pinning the documented fallback: provider is advisory, not binding.
A model routed through a provider with no provider-scoped table entry
is priced by its name alone. ``anthropic/claude-3-opus`` served via
OpenRouter is therefore billed at Anthropic's direct list price --
1.0 * 0.015 + 1.0 * 0.075 = 0.09 -- so any provider markup is invisible
in the reported cost.
"""
calculator = offline_calculator()
result = asyncio.run(
calculator.calculate_cost(
"anthropic/claude-3-opus", 1_000, 1_000, "openrouter"
)
)
assert result["prompt_cost"] == 0.015
assert result["completion_cost"] == 0.075
assert result["total_cost"] == 0.09
def test_sync_cost_path_sees_pricing_the_async_path_just_resolved():
calculator = offline_calculator()
resolved = asyncio.run(
calculator.calculate_cost("llama3.3:70b", 1_000, 1_000, "ollama")
)
assert resolved["pricing_used"] == {"prompt": 0.0, "completion": 0.0}
from_sync = calculator.calculate_cost_sync(
"llama3.3:70b", 1_000, 1_000, provider="ollama"
)
assert from_sync["pricing_used"] == {"prompt": 0.0, "completion": 0.0}
assert "error" not in from_sync
def test_cost_summary_preserves_the_unpriced_signal():
calculator = offline_calculator()
records = [
{
"model_name": "gpt-4",
"provider": "openai",
"prompt_tokens": 1_500,
"completion_tokens": 500,
},
{
"model_name": "mystery-9000",
"provider": "openai",
"prompt_tokens": 900_000,
"completion_tokens": 100_000,
},
]
per_record = asyncio.run(calculator.calculate_batch_costs(records))
# The per-record signal exists...
assert [("error" in c) for c in per_record] == [False, True]
summary = asyncio.run(calculator.get_research_cost_summary(records))
assert summary["total_cost"] == 0.075 # only the priced record
assert summary["pricing_complete"] is False
assert summary["unpriced_calls"] == 1
assert summary["unpriced_tokens"] == 1_000_000
def test_cost_summary_separates_the_same_model_across_providers():
calculator = offline_calculator()
records = [
{
"model_name": "gpt-4",
"provider": "openai",
"prompt_tokens": 1_500,
"completion_tokens": 500,
},
{
"model_name": "gpt-4",
"provider": "ollama",
"prompt_tokens": 1_500,
"completion_tokens": 500,
},
]
summary = asyncio.run(calculator.get_research_cost_summary(records))
# The paid call alone: 1.5*0.03 + 0.5*0.06 = 0.075
assert summary["total_cost"] == 0.075
# Preserve existing model totals; provider attribution is additive.
assert summary["model_breakdown"]["gpt-4"]["calls"] == 2
by_provider = {
row["provider"]: row for row in summary["provider_breakdown"]
}
assert set(by_provider) == {"openai", "ollama"}
assert by_provider["openai"]["total_cost"] == 0.075
assert by_provider["ollama"]["total_cost"] == 0.0
assert all(row["calls"] == 1 for row in by_provider.values())
assert summary["pricing_complete"] is True
# ==========================================================================
# 5. The news search-tracking path records nothing at all
# ==========================================================================
def test_news_search_tracking_is_hardcoded_off():
"""``tracking_enabled`` is a constant ``False``, not a setting."""
from local_deep_research.news.core.search_integration import (
NewsSearchCallback,
)
callback = NewsSearchCallback()
assert callback.tracking_enabled is False
# Re-reading does not consult any setting -- the memoised value stands.
assert callback.tracking_enabled is False
assert callback._tracking_enabled is False
def test_news_search_tracking_target_module_no_longer_exists():
"""Even forced on, the tracking path cannot record anything.
``_track_user_search`` imports
``news.preference_manager.search_tracker``, which is not present in
the package. The resulting ModuleNotFoundError is swallowed by the
bare ``except Exception`` in ``_track_user_search``, so flipping the
flag on would still record nothing and raise nothing.
"""
import importlib
from local_deep_research.news.core.search_integration import (
NewsSearchCallback,
create_search_wrapper,
)
with pytest.raises(ModuleNotFoundError):
importlib.import_module(
"local_deep_research.news.preference_manager.search_tracker"
)
callback = NewsSearchCallback()
callback._tracking_enabled = True # force the "enabled" branch
# Returns normally; the import failure never surfaces.
callback(
"some query",
{"findings": [{"content": "x"}]},
{"is_user_search": True, "user_id": "alice"},
)
# The public wrapper behaves as a pass-through around the real search.
calls = []
def original(self, query, **kwargs):
calls.append(query)
return {"findings": [{"content": "y"}], "strategy": "s"}
wrapped = create_search_wrapper(original)
result = wrapped(object(), "wrapped query", user_id="alice")
assert calls == ["wrapped query"]
assert result["findings"] == [{"content": "y"}]