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(test): make cleanup sampling gate patchable as a module function (#5225)

Sbc3dbb92f4bd
diff --git a/src/local_deep_research/web/auth/cleanup_middleware.py b/src/local_deep_research/web/auth/cleanup_middleware.py
index ef3652eccc..3d190ba6c6 100644
--- a/src/local_deep_research/web/auth/cleanup_middleware.py
+++ b/src/local_deep_research/web/auth/cleanup_middleware.py
@@ -14,6 +14,22 @@ from ...database.session_context import get_g_db_session
from .middleware_optimizer import should_skip_database_middleware
+def _should_run_cleanup_sample() -> bool:
+ """1% sampling gate: True on ~1% of requests, False the rest.
+
+ Extracted from ``cleanup_completed_research`` so tests can patch the
+ gate as a regular module-level function instead of rebinding the
+ C-implemented ``random.randint`` attribute (which is unreliable
+ under Python 3.14 + pytest-xdist ``-n auto`` workers: the rebind
+ silently fails to take effect and the body-asserting tests in
+ ``tests/web/auth/test_cleanup_middleware.py`` fail with
+ "called 0 times").
+ """
+ return (
+ random.randint(1, 100) <= 1 # noqa: S311 — not cryptographic, just sampling
+ )
+
+
def cleanup_completed_research():
"""
Clean up completed research records for the current user.
@@ -28,7 +44,7 @@ def cleanup_completed_research():
return
# Only run cleanup on ~1% of requests to reduce per-request overhead
- if random.randint(1, 100) > 1: # noqa: S311 — not cryptographic, just sampling
+ if not _should_run_cleanup_sample():
return
username = session.get("username")
diff --git a/tests/web/auth/test_cleanup_middleware.py b/tests/web/auth/test_cleanup_middleware.py
index 7d5fbef6d6..b2723b3819 100644
--- a/tests/web/auth/test_cleanup_middleware.py
+++ b/tests/web/auth/test_cleanup_middleware.py
@@ -17,14 +17,23 @@ class TestCleanupCompletedResearch:
"""Tests for cleanup_completed_research function."""
@pytest.fixture(autouse=True)
- def _bypass_random_gate(self):
- """Ensure the 1% random sampling gate always passes so tests
- exercise the actual cleanup logic."""
- with patch(
- "local_deep_research.web.auth.cleanup_middleware.random.randint",
- return_value=1,
- ):
- yield
+ def _bypass_random_gate(self, monkeypatch):
+ """Force the 1% sampling gate in cleanup_completed_research to pass
+ so tests exercise the actual cleanup logic.
+
+ Patches the module-level ``_should_run_cleanup_sample`` helper
+ (a regular Python function) rather than rebinding
+ ``random.randint`` on the ``random`` module, which is unreliable
+ under Python 3.14 + pytest-xdist ``-n auto`` workers: the rebind
+ silently fails to take effect and every body-asserting test
+ fails with "called 0 times" (observed in CI runs late
+ July 2026).
+ """
+ monkeypatch.setattr(
+ "local_deep_research.web.auth.cleanup_middleware._should_run_cleanup_sample",
+ lambda: True,
+ )
+ yield
def test_skips_when_middleware_should_skip(self):
"""Should skip cleanup when should_skip_database_middleware returns True."""
@@ -333,22 +342,23 @@ class TestCleanupCompletedResearch:
50
)
- def test_skips_cleanup_on_random_sampling(self, _bypass_random_gate):
+ def test_skips_cleanup_on_random_sampling(self, monkeypatch):
"""Should skip cleanup when random sampling doesn't select this request."""
app = Flask(__name__)
app.secret_key = "test"
mock_db_session = MagicMock()
- with (
- patch(
- "local_deep_research.web.auth.cleanup_middleware.should_skip_database_middleware"
- ) as mock_skip,
- patch(
- "local_deep_research.web.auth.cleanup_middleware.random.randint",
- return_value=50,
- ),
- ):
+ # Override the autouse gate-bypass for this test: the gate
+ # should trigger (sampling did NOT select this request).
+ monkeypatch.setattr(
+ "local_deep_research.web.auth.cleanup_middleware._should_run_cleanup_sample",
+ lambda: False,
+ )
+
+ with patch(
+ "local_deep_research.web.auth.cleanup_middleware.should_skip_database_middleware"
+ ) as mock_skip:
mock_skip.return_value = False
from local_deep_research.web.auth.cleanup_middleware import (