~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
test(metrics): pin write-health safety contracts (#7665)
Chanhyo Jungc95daf4fe60d
diff --git a/tests/metrics/test_metric_write_health.py b/tests/metrics/test_metric_write_health.pyindex ced0489278..a350fe9686 100644--- a/tests/metrics/test_metric_write_health.py+++ b/tests/metrics/test_metric_write_health.py@@ -18,6 +18,185 @@ def health(monkeypatch):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),+ },+ }++[email protected]("username", [None, 42])+ "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")@@ -296,7 +475,7 @@ def test_search_failures_count_once_without_touching_caller_transaction(session.commit.assert_not_called()session.rollback.assert_not_called()session.flush.assert_not_called()- assert "private" not in str(snapshot)+ _assert_snapshot_shape(snapshot)@pytest.mark.parametrize("worker", [False, True])@@ -353,7 +532,7 @@ def test_scoped_tokens_report_skips_without_changing_the_transaction(if reason:assert snapshot["by_reason"][reason] == 1assert health.snapshot("bob")["dropped_writes"] == 0- assert "private" not in str(snapshot)+ _assert_snapshot_shape(snapshot)assert writer.add_token_metrics_to_session.call_count == int(mode not in ("pending", "owner_mismatch"))