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.

test(cleanup): soften sample-gate explanation and add boundary tests (#5233)

S0e3c298e41d8
diff --git a/src/local_deep_research/web/auth/cleanup_middleware.py b/src/local_deep_research/web/auth/cleanup_middleware.py
index 2d202cbef7..7a8c7c5a84 100644
--- a/src/local_deep_research/web/auth/cleanup_middleware.py
+++ b/src/local_deep_research/web/auth/cleanup_middleware.py
@@ -18,12 +18,13 @@ 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").
+ gate by string path (``monkeypatch.setattr(..., lambda: True)``).
+ The earlier inline-``random.randint`` form could only be reached by
+ rebinding ``random.randint`` on the ``random`` module via
+ ``mock.patch`` / ``monkeypatch.setattr``, which was observed to
+ silently not take effect in some CI configurations — gate-dependent
+ tests then failed with the cleanup body never running. Patching this
+ helper by string path has been reliable in CI so far.
"""
return (
random.randint(1, 100) <= 1 # noqa: S311 — not cryptographic, just sampling
diff --git a/tests/web/auth/test_cleanup_middleware.py b/tests/web/auth/test_cleanup_middleware.py
index c4a01db4e0..fa5c1115ff 100644
--- a/tests/web/auth/test_cleanup_middleware.py
+++ b/tests/web/auth/test_cleanup_middleware.py
@@ -21,13 +21,12 @@ class TestCleanupCompletedResearch:
"""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).
+ Patches the module-level ``_should_run_cleanup_sample`` helper by
+ string path. The earlier implementation rebinding
+ ``random.randint`` on the ``random`` module was observed to be
+ flaky in CI: the rebind would silently not take effect and
+ gate-dependent tests would fail with the cleanup body never
+ running.
"""
monkeypatch.setattr(
"local_deep_research.web.auth.cleanup_middleware._should_run_cleanup_sample",
@@ -409,17 +408,35 @@ class TestCleanupCompletedResearch:
mock_db_session.query.assert_not_called()
- ("sample", "expected"),
- [(1, True), (2, False)],
-)
-def test_should_run_cleanup_sampling(sample: int, expected: bool) -> None:
- """Sampling stays at one selected value without patching global random."""
- from local_deep_research.web.auth import cleanup_middleware
-
- with patch.object(cleanup_middleware, "random") as mock_random:
- mock_random.randint.return_value = sample
-
- assert cleanup_middleware._should_run_cleanup_sample() is expected
+class TestShouldRunCleanupSample:
+ """Direct boundary tests for the ``_should_run_cleanup_sample``
+ sampling gate helper.
+
+ These tests deliberately do *not* inherit the autouse
+ ``_bypass_random_gate`` fixture from ``TestCleanupCompletedResearch``,
+ so the real helper is exercised.
+
+ Note on detection asymmetry: Only roll=1 (low boundary) is a true canary
+ that fails (~99% of runs) if stubbing silently fails to take effect;
+ False-expecting rolls (2, 50, 100) will still pass ~99% of the time if unpatched.
+ """
+
+ @pytest.mark.parametrize(
+ ("roll", "expected"),
+ [
+ (1, True), # Low boundary / canary: only roll satisfying <= 1
+ (2, False), # Just above low boundary
+ (50, False), # Midpoint of roll range
+ (100, False), # High boundary
+ ],
+ )
+ def test_should_run_cleanup_sample_boundaries(
+ self, roll: int, expected: bool
+ ) -> None:
+ """Sampling returns expected decision for boundary rolls."""
+ from local_deep_research.web.auth import cleanup_middleware
- mock_random.randint.assert_called_once_with(1, 100)
+ with patch.object(cleanup_middleware, "random") as mock_random:
+ mock_random.randint.return_value = roll
+ assert cleanup_middleware._should_run_cleanup_sample() is expected
+ mock_random.randint.assert_called_once_with(1, 100)