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): rate-limit reset/cleanup operate on RateLimitEstimate (#4735)

LearningCircuitfc3abe8c8956
diff --git a/changelog.d/+rate-limit-reset-cleanup-from-estimate.bugfix.md b/changelog.d/+rate-limit-reset-cleanup-from-estimate.bugfix.md
new file mode 100644
index 0000000000..eb76074728
--- /dev/null
+++ b/changelog.d/+rate-limit-reset-cleanup-from-estimate.bugfix.md
@@ -0,0 +1 @@
+The rate-limiting reset (`POST /api/rate-limiting/engines/<engine>/reset`) and cleanup (`POST /api/rate-limiting/cleanup`) endpoints now operate on the persisted `RateLimitEstimate` table instead of the per-request `get_tracker()` singleton. Like the read endpoints fixed in #4721, the tracker's mutation path is gated on a research-session context that is absent in an analytics HTTP request, so these endpoints were silent no-ops that never cleared the estimates the `/status` and `/current` panels display. Reset now deletes the engine's stored estimate (so it re-learns), and cleanup deletes estimates not updated within the requested window.
diff --git a/src/local_deep_research/web/routes/settings_routes.py b/src/local_deep_research/web/routes/settings_routes.py
index 31502e4bcd..b2d11ff503 100644
--- a/src/local_deep_research/web/routes/settings_routes.py
+++ b/src/local_deep_research/web/routes/settings_routes.py
@@ -2259,12 +2259,22 @@ def api_get_rate_limiting_status():
)
@login_required
def api_reset_engine_rate_limiting(engine_type):
- """Reset rate limiting data for a specific engine"""
+ """Reset (forget) the learned rate-limit estimate for a specific engine.
+
+ Deletes the engine's persisted ``RateLimitEstimate`` row from the user's
+ database so the adaptive tracker re-learns it from scratch. The previous
+ implementation called the per-request ``get_tracker()``, whose mutation
+ path is gated on a research-session context that is absent in an analytics
+ HTTP request — so it was a silent no-op that never cleared the persisted
+ estimate the ``/status`` and ``/current`` endpoints display (#4721).
+ """
try:
- from ...web_search_engines.rate_limiting import get_tracker
-
- tracker = get_tracker()
- tracker.reset_engine(engine_type)
+ username = session["username"]
+ with get_user_db_session(username) as db_session:
+ db_session.query(RateLimitEstimate).filter_by(
+ engine_type=engine_type
+ ).delete(synchronize_session=False)
+ db_session.commit()
return jsonify(
{"message": f"Rate limiting data reset for {engine_type}"}
@@ -2284,8 +2294,6 @@ def api_cleanup_rate_limiting():
here — the endpoint works with or without a payload (defaults to 30 days).
"""
try:
- from ...web_search_engines.rate_limiting import get_tracker
-
data = request.get_json() if request.is_json else None
days = data.get("days", 30) if data is not None else 30
@@ -2296,8 +2304,17 @@ def api_cleanup_rate_limiting():
if days < 1 or days > 365:
return jsonify({"error": "'days' must be between 1 and 365"}), 400
- tracker = get_tracker()
- tracker.cleanup_old_data(days)
+ # Delete persisted estimates not updated within the window. Mirrors the
+ # read endpoints (#4721): operate on RateLimitEstimate rather than the
+ # per-request get_tracker(), whose cleanup path is a no-op outside a
+ # research-session context. last_updated is a unix timestamp (Float).
+ username = session["username"]
+ cutoff = time.time() - days * 86400
+ with get_user_db_session(username) as db_session:
+ db_session.query(RateLimitEstimate).filter(
+ RateLimitEstimate.last_updated < cutoff
+ ).delete(synchronize_session=False)
+ db_session.commit()
return jsonify(
{"message": f"Cleaned up rate limiting data older than {days} days"}
diff --git a/tests/web/routes/test_settings_routes_rate_limiting.py b/tests/web/routes/test_settings_routes_rate_limiting.py
index eb4252b6f4..e48fb5fc08 100644
--- a/tests/web/routes/test_settings_routes_rate_limiting.py
+++ b/tests/web/routes/test_settings_routes_rate_limiting.py
@@ -153,28 +153,26 @@ class TestApiResetEngineRateLimiting:
)
assert response.status_code == 401, response.status_code
- @patch("local_deep_research.web_search_engines.rate_limiting.get_tracker")
- def test_resets_engine(self, mock_get_tracker, authenticated_client):
- mock_tracker = MagicMock()
- mock_get_tracker.return_value = mock_tracker
-
- response = authenticated_client.post(
- f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
- )
+ def test_resets_engine(self, authenticated_client):
+ # Deletes the persisted RateLimitEstimate row for the engine (DB-backed,
+ # no get_tracker) and commits.
+ with _patch_estimates() as session:
+ response = authenticated_client.post(
+ f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
+ )
assert response.status_code == 200
- data = response.get_json()
- assert "google" in data["message"]
- mock_tracker.reset_engine.assert_called_once_with("google")
-
- @patch("local_deep_research.web_search_engines.rate_limiting.get_tracker")
- def test_error_returns_500(self, mock_get_tracker, authenticated_client):
- mock_tracker = MagicMock()
- mock_tracker.reset_engine.side_effect = RuntimeError("fail")
- mock_get_tracker.return_value = mock_tracker
-
- response = authenticated_client.post(
- f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
+ assert "google" in response.get_json()["message"]
+ session.query.return_value.filter_by.assert_called_once_with(
+ engine_type="google"
)
+ session.query.return_value.filter_by.return_value.delete.assert_called_once()
+ session.commit.assert_called_once()
+
+ def test_error_returns_500(self, authenticated_client):
+ with _patch_estimates(query_error=RuntimeError("db fail")):
+ response = authenticated_client.post(
+ f"{SETTINGS_PREFIX}/api/rate-limiting/engines/google/reset"
+ )
assert response.status_code == 500
@@ -185,40 +183,33 @@ class TestApiCleanupRateLimiting:
response = client.post(f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup")
assert response.status_code == 401, response.status_code
- @patch("local_deep_research.web_search_engines.rate_limiting.get_tracker")
- def test_cleanup_default_days(self, mock_get_tracker, authenticated_client):
- mock_tracker = MagicMock()
- mock_get_tracker.return_value = mock_tracker
-
- response = authenticated_client.post(
- f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
- )
+ def test_cleanup_default_days(self, authenticated_client):
+ # DB-backed: deletes old RateLimitEstimate rows and commits (no tracker).
+ with _patch_estimates() as session:
+ response = authenticated_client.post(
+ f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
+ )
assert response.status_code == 200
- data = response.get_json()
- assert "30 days" in data["message"]
- mock_tracker.cleanup_old_data.assert_called_once_with(30)
-
- @patch("local_deep_research.web_search_engines.rate_limiting.get_tracker")
- def test_cleanup_custom_days(self, mock_get_tracker, authenticated_client):
- mock_tracker = MagicMock()
- mock_get_tracker.return_value = mock_tracker
-
- response = authenticated_client.post(
- f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup",
- json={"days": 7},
- )
+ assert "30 days" in response.get_json()["message"]
+ session.query.return_value.filter.return_value.delete.assert_called_once()
+ session.commit.assert_called_once()
+
+ def test_cleanup_custom_days(self, authenticated_client):
+ with _patch_estimates() as session:
+ response = authenticated_client.post(
+ f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup",
+ json={"days": 7},
+ )
assert response.status_code == 200
- data = response.get_json()
- assert "7 days" in data["message"]
- mock_tracker.cleanup_old_data.assert_called_once_with(7)
+ assert "7 days" in response.get_json()["message"]
+ session.query.return_value.filter.return_value.delete.assert_called_once()
+ session.commit.assert_called_once()
- @patch("local_deep_research.web_search_engines.rate_limiting.get_tracker")
- def test_error_returns_500(self, mock_get_tracker, authenticated_client):
- mock_get_tracker.side_effect = RuntimeError("fail")
-
- response = authenticated_client.post(
- f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
- )
+ def test_error_returns_500(self, authenticated_client):
+ with _patch_estimates(query_error=RuntimeError("fail")):
+ response = authenticated_client.post(
+ f"{SETTINGS_PREFIX}/api/rate-limiting/cleanup"
+ )
assert response.status_code == 500
@pytest.mark.parametrize("days_value", [0, -1, 366, 1000])