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(chat): de-flake per-user cap test (cleanup_middleware 1% sample) (#4688)

LearningCircuitd61d81706832
diff --git a/tests/chat/test_chat_send_message_followups.py b/tests/chat/test_chat_send_message_followups.py
index 6ac9b32d22..3b36dbd384 100644
--- a/tests/chat/test_chat_send_message_followups.py
+++ b/tests/chat/test_chat_send_message_followups.py
@@ -190,20 +190,37 @@ class TestGlobalPerUserCap:
# (its UserActiveResearch row is never cleaned up by completion).
patch("local_deep_research.chat.routes.start_research_process"),
# Treat the first research's (stubbed-spawn) thread as alive.
- # The second send's cap path runs reclaim_stale_user_active_research,
- # which flips IN_PROGRESS rows whose thread is dead — gated only by a
- # 30s grace window on started_at. Because the spawn is stubbed, the
- # first row has no real thread, so the row is protected ONLY by that
- # wall-clock window. Under heavy CI load a >30s stall between these two
- # sequential posts pushes the row past the window and it gets reclaimed,
- # dropping the count to 0 → the second send wrongly succeeds (flaky 200
- # instead of 429). Forcing thread-alive removes the timing dependency:
- # reclaim short-circuits before the grace check, so this test now
- # verifies the cap-counts-the-row invariant deterministically.
+ # Two independent cleanup paths would otherwise delete the first
+ # send's UserActiveResearch row before the second send's cap check
+ # sees it (dropping active_count back to 0 → flaky 200 instead of
+ # 429). Because the spawn is stubbed, the row has no real thread /
+ # no _active_research entry, so it looks "done" to both paths:
+ #
+ # 1. reclaim_stale_user_active_research (run inline by
+ # chat.routes.send_message) — flips IN_PROGRESS rows whose
+ # thread is dead, gated only by a 30s grace window on
+ # started_at. Patching is_research_thread_alive -> True
+ # short-circuits before the grace check, so a >30s GC stall
+ # between the two sequential posts can no longer trip it.
+ #
+ # 2. cleanup_completed_research before_request middleware
+ # (web/auth/cleanup_middleware.py) — sampled on ~1% of
+ # requests, deletes UserActiveResearch rows whose
+ # research_id is not in _active_research. Patching
+ # is_research_active -> True makes the middleware skip the
+ # row, so the 1% sample can no longer trip it.
+ #
+ # Both patches model the production invariant (a live research's
+ # row is left alone) without the timing/lottery dependencies that
+ # surfaced under heavy CI load on the 36k-test xdist lane.
patch(
"local_deep_research.web.routes.globals.is_research_thread_alive",
return_value=True,
),
+ patch(
+ "local_deep_research.web.routes.globals.is_research_active",
+ return_value=True,
+ ),
):
first = authenticated_client.post(
f"/api/chat/sessions/{session_a}/messages",