Skip to content

~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.

fix(metrics): exclude NULL-engine rows from star-reviews search-engine buckets (#4786)

LearningCircuit8c4d04a5ce61
diff --git a/changelog.d/4786.bugfix.md b/changelog.d/4786.bugfix.md
new file mode 100644
index 0000000000..f18e6d1d73
--- /dev/null
+++ b/changelog.d/4786.bugfix.md
@@ -0,0 +1 @@
+**Star Reviews "Unknown" search-engine bucket no longer double-counts.** The search-engine breakdown attributed any research that made non-search LLM calls (whose token rows have no `search_engine_selected`) to the "Unknown" bucket *in addition to* its real engine, inflating "Unknown" toward every research. Ratings with a recorded engine now count under that engine only; "Unknown" holds only ratings with no recorded search engine.
diff --git a/src/local_deep_research/web/routes/metrics_routes.py b/src/local_deep_research/web/routes/metrics_routes.py
index 525e099823..e2302d2540 100644
--- a/src/local_deep_research/web/routes/metrics_routes.py
+++ b/src/local_deep_research/web/routes/metrics_routes.py
@@ -1484,11 +1484,16 @@ def api_star_reviews():
# Ratings by search engine. Same one-to-many fan-out concern as the LLM
# query — collapse to distinct (research_id, search_engine) pairs first.
+ # search_engine_selected is NULL on non-search LLM-call rows; exclude
+ # those so a research that used a real engine isn't ALSO attributed to
+ # the "Unknown" bucket (a research with no engine still falls through
+ # the outerjoin to "Unknown" exactly once).
research_engines = (
session.query(
TokenUsage.research_id.label("research_id"),
TokenUsage.search_engine_selected.label("search_engine"),
)
+ .filter(TokenUsage.search_engine_selected.isnot(None))
.distinct()
.subquery()
)
diff --git a/tests/web/routes/test_metrics_routes.py b/tests/web/routes/test_metrics_routes.py
index 8d4fe6fc3f..03de9b029c 100644
--- a/tests/web/routes/test_metrics_routes.py
+++ b/tests/web/routes/test_metrics_routes.py
@@ -1634,6 +1634,94 @@ class TestApiStarReviewsFanOut:
assert searxng[0]["rating_count"] == 1, "fan-out inflated count"
assert searxng[0]["positive_ratings"] == 1
+ def test_llm_breakdown_counts_with_multiple_models(
+ self, authenticated_client
+ ):
+ """A rating on a research that used two models counts once per model
+ (the semantic the distinct subquery exists for) — never duplicated
+ within a model, and the recent list shows the rating once."""
+ now = datetime.now(UTC)
+ rows = [
+ ResearchRating(research_id="r1", rating=5, created_at=now),
+ _token_usage("r1", model_name="gpt-4", ts=now),
+ _token_usage("r1", model_name="gpt-4", ts=now),
+ _token_usage("r1", model_name="claude", ts=now),
+ _token_usage("r1", model_name="claude", ts=now),
+ _token_usage("r1", model_name="claude", ts=now),
+ ]
+ with _seeded_metrics_db(*rows):
+ response = authenticated_client.get(
+ f"{METRICS_PREFIX}/api/star-reviews?period=all"
+ )
+ assert response.status_code == 200, response.get_json()
+ data = response.get_json()
+ by_model = {r["model"]: r for r in data["llm_ratings"]}
+ assert by_model["gpt-4"]["rating_count"] == 1
+ assert by_model["claude"]["rating_count"] == 1
+ assert by_model["gpt-4"]["positive_ratings"] == 1
+ assert by_model["claude"]["positive_ratings"] == 1
+ assert len(data["recent_ratings"]) == 1
+
+ def test_search_engine_unknown_bucket_excludes_real_engine_ratings(
+ self, authenticated_client
+ ):
+ """A research that used a real engine must not ALSO appear in the
+ 'Unknown' bucket because of its non-search (NULL-engine) LLM-call
+ rows. 'Unknown' should hold only ratings with no recorded engine."""
+ now = datetime.now(UTC)
+ rows = [
+ # r1 used searxng, plus non-search LLM calls (engine NULL)
+ ResearchRating(research_id="r1", rating=5, created_at=now),
+ _token_usage("r1", ts=now, search_engine="searxng"),
+ _token_usage("r1", ts=now, search_engine=None),
+ _token_usage("r1", ts=now, search_engine=None),
+ # r2 made only non-search LLM calls -> belongs in 'Unknown' once
+ ResearchRating(research_id="r2", rating=3, created_at=now),
+ _token_usage("r2", ts=now, search_engine=None),
+ ]
+ with _seeded_metrics_db(*rows):
+ response = authenticated_client.get(
+ f"{METRICS_PREFIX}/api/star-reviews?period=all"
+ )
+ assert response.status_code == 200, response.get_json()
+ buckets = {
+ r["search_engine"]: r
+ for r in response.get_json()["search_engine_ratings"]
+ }
+ # r1 counts under searxng only
+ assert buckets["searxng"]["rating_count"] == 1
+ # 'Unknown' holds only r2 (the engine-less research), not r1
+ assert buckets["Unknown"]["rating_count"] == 1
+ assert buckets["Unknown"]["avg_rating"] == 3.0
+
+ def test_search_engine_multi_engine_research_counts_once_per_engine(
+ self, authenticated_client
+ ):
+ """A research that used two real engines counts once under EACH engine
+ (the distinct (research_id, engine) semantic) — not duplicated within an
+ engine, and its NULL-engine rows do not spawn an 'Unknown' bucket."""
+ now = datetime.now(UTC)
+ rows = [
+ ResearchRating(research_id="r1", rating=5, created_at=now),
+ _token_usage("r1", ts=now, search_engine="searxng"),
+ _token_usage("r1", ts=now, search_engine="searxng"),
+ _token_usage("r1", ts=now, search_engine="arxiv"),
+ _token_usage("r1", ts=now, search_engine=None),
+ ]
+ with _seeded_metrics_db(*rows):
+ response = authenticated_client.get(
+ f"{METRICS_PREFIX}/api/star-reviews?period=all"
+ )
+ assert response.status_code == 200, response.get_json()
+ buckets = {
+ r["search_engine"]: r
+ for r in response.get_json()["search_engine_ratings"]
+ }
+ assert buckets["searxng"]["rating_count"] == 1
+ assert buckets["arxiv"]["rating_count"] == 1
+ # the NULL-engine row must not create an 'Unknown' bucket here
+ assert "Unknown" not in buckets
+
class TestApiStarReviewsQualityDimensions:
"""Quality radar must not be suppressed when only some dimensions are set."""
@@ -1671,6 +1759,7 @@ class TestApiSaveResearchRatingValidation:
f"{METRICS_PREFIX}/api/ratings/test-id", json={"rating": True}
)
assert response.status_code == 400
+ assert "between 1 and 5" in response.get_json()["message"]
def test_rejects_boolean_sub_dimension(self, authenticated_client):
response = authenticated_client.post(
@@ -1678,6 +1767,7 @@ class TestApiSaveResearchRatingValidation:
json={"rating": 4, "accuracy": True},
)
assert response.status_code == 400
+ assert "accuracy must be an integer" in response.get_json()["message"]
def test_rejects_overlong_feedback(self, authenticated_client):
response = authenticated_client.post(
@@ -1685,3 +1775,4 @@ class TestApiSaveResearchRatingValidation:
json={"rating": 4, "feedback": "x" * 10001},
)
assert response.status_code == 400
+ assert "10000" in response.get_json()["message"]