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): count write-health drops only for exact str usernames (#7751)

Lukas042e1e567405
diff --git a/src/local_deep_research/utilities/metric_write_health.py b/src/local_deep_research/utilities/metric_write_health.py
index f4275a3ada..f2704b6d15 100644
--- a/src/local_deep_research/utilities/metric_write_health.py
+++ b/src/local_deep_research/utilities/metric_write_health.py
@@ -9,6 +9,8 @@ from collections import Counter, OrderedDict
from threading import Lock
from typing import Literal, TypedDict
+from loguru import logger
+
MetricWriteSource = Literal["tokens", "search", "rate_limits"]
MetricWriteReason = Literal[
"write_error",
@@ -55,7 +57,16 @@ class MetricWriteHealth:
source: MetricWriteSource,
reason: MetricWriteReason,
) -> None:
- if not isinstance(username, str) or not username:
+ # ``type(...) is str`` rather than ``isinstance``: a ``str`` subclass
+ # can alias ``__hash__``/``__eq__`` and land in another user's bucket.
+ if type(username) is not str:
+ if username is not None:
+ logger.debug(
+ "Not counting a dropped metric write: username type {} is not str",
+ type(username).__name__,
+ )
+ return
+ if not username:
return
if source not in _SOURCES or reason not in METRIC_WRITE_REASON_LABELS:
return
@@ -69,7 +80,7 @@ class MetricWriteHealth:
def snapshot(self, username: str | None) -> MetricWriteSnapshot:
counts: Counter[tuple[MetricWriteSource, MetricWriteReason]] = Counter()
with self._lock:
- if isinstance(username, str):
+ if type(username) is str:
counts = self._users.get(username, counts).copy()
return {
"dropped_writes": sum(counts.values()),
diff --git a/tests/metrics/test_metric_write_health.py b/tests/metrics/test_metric_write_health.py
index f1c2a1b5cf..ced0489278 100644
--- a/tests/metrics/test_metric_write_health.py
+++ b/tests/metrics/test_metric_write_health.py
@@ -66,6 +66,46 @@ def test_only_known_categories_and_identified_users_are_retained(health):
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",