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: stabilize session, plotting, and notes concurrency checks (#7314)

LearningCircuitaa8f896425ed
diff --git a/tests/benchmarks/optimization/test_optuna_optimizer_coverage.py b/tests/benchmarks/optimization/test_optuna_optimizer_coverage.py
index 415aa81540..67b4254a02 100644
--- a/tests/benchmarks/optimization/test_optuna_optimizer_coverage.py
+++ b/tests/benchmarks/optimization/test_optuna_optimizer_coverage.py
@@ -219,6 +219,8 @@ class TestOptimizationCallbackStoresTrial:
class TestCreateQuickVisualizations:
+ # Keep the lazy loader from replacing the plot mock on a cold import.
+ @patch(f"{MODULE}.plt", Mock())
@patch(f"{MODULE}.CompositeBenchmarkEvaluator")
@patch(f"{MODULE}.PLOTTING_AVAILABLE", True)
@patch(f"{MODULE}.plot_optimization_history")
diff --git a/tests/database/test_session_context_deep_coverage.py b/tests/database/test_session_context_deep_coverage.py
index cab4e524b1..7213dbe4b7 100644
--- a/tests/database/test_session_context_deep_coverage.py
+++ b/tests/database/test_session_context_deep_coverage.py
@@ -131,6 +131,9 @@ class TestGetUserDbSessionErrors:
pass
def test_encrypted_db_no_password_raises(self):
+ from local_deep_research.database.session_passwords import (
+ SessionPasswordStore,
+ )
from local_deep_research.database.session_context import ( # noqa: E402
get_user_db_session,
DatabaseSessionError,
@@ -139,6 +142,12 @@ class TestGetUserDbSessionErrors:
with (
patch(f"{MODULE}.get_search_context", return_value=None),
patch(f"{MODULE}.db_manager") as mock_db,
+ # The missing-password premise also requires no active session.
+ # Other tests may have populated the process-wide store for alice.
+ patch(
+ "local_deep_research.database.session_passwords.session_password_store",
+ SessionPasswordStore(),
+ ),
):
mock_db.has_encryption = True
with pytest.raises(DatabaseSessionError, match="requires password"):
diff --git a/tests/notes/test_note_ai_async_behavior.py b/tests/notes/test_note_ai_async_behavior.py
index 4bf85e020e..e3eec6b814 100644
--- a/tests/notes/test_note_ai_async_behavior.py
+++ b/tests/notes/test_note_ai_async_behavior.py
@@ -11,18 +11,16 @@ code, but by observing runtime behavior:
call included) ran ON a worker thread; the thread identity of the
stub call is the observable difference.
-2. ``test_concurrent_ai_requests_overlap`` — several AI requests with a
- slow stub complete in roughly one stub duration, not the sum. If each
- request still serialized on a worker thread — or worse, blocked the
- event loop — the wall time would be the sum.
+2. ``test_concurrent_ai_requests_overlap`` — every request reaches a
+ held model call before any call is released. Serial execution cannot
+ reach that barrier.
3. ``test_sync_route_stays_responsive_while_ai_requests_in_flight`` —
the issue's own measure of done, in route form: with MORE AI requests
- in flight than the process's ~40 AnyIO worker tokens (45 here, each
- parked on a 1.5s stub), a plain sync-def route (``/api/v1/health`` —
- served from that same worker pool) still answers immediately. Under
- the old model, 45 sync AI handlers would pin every worker token and
- the health route would queue behind them.
+ in flight than the process's ~40 AnyIO worker tokens (45 here, held
+ until explicitly released), a plain sync-def route (``/api/v1/health``
+ — served from that same worker pool) still answers before release.
+ Holding worker tokens across model calls cannot reach that barrier.
Harness notes (mirrors test_notes_router_fastapi.py):
* The real app is driven through ``httpx.AsyncClient`` + ``ASGITransport``
@@ -52,15 +50,21 @@ AI_SLEEP = 0.6
@pytest.fixture()
def fake_ai(monkeypatch):
- """Replace NoteAIService with a recording stub whose methods sleep.
+ """Record model calls, optionally holding them at a shared barrier.
- ``state["sleep"]`` is adjustable per test; every call records the
- thread it executed on so tests can assert where the "model response"
- was actually awaited.
+ The thread-identity test uses a short sleep. Concurrency checks set
+ ``expected`` and release their calls explicitly, so slow request
+ scheduling cannot invalidate the overlap premise.
"""
from local_deep_research.web.routers import notes as notes_router
- state = {"calls": [], "sleep": AI_SLEEP}
+ state = {
+ "calls": [],
+ "sleep": AI_SLEEP,
+ "expected": 0,
+ "started": asyncio.Event(),
+ "release": asyncio.Event(),
+ }
class _SlowAI:
def __init__(self, username, dbpw=None):
@@ -77,7 +81,12 @@ def fake_ai(monkeypatch):
# Bind the record locally so overlapping calls each stamp
# their OWN finish — calls[-1] would race between coroutines.
state["calls"].append(call)
- await asyncio.sleep(state["sleep"])
+ if state["expected"]:
+ if len(state["calls"]) == state["expected"]:
+ state["started"].set()
+ await state["release"].wait()
+ else:
+ await asyncio.sleep(state["sleep"])
call["finished"] = time.monotonic()
return f"summary of {note_id}"
@@ -180,29 +189,22 @@ async def test_ai_call_runs_on_the_event_loop_thread(client, fake_ai):
@pytest.mark.asyncio
async def test_concurrent_ai_requests_overlap(client, fake_ai):
- """Four AI requests complete in ~one stub duration, not the sum.
-
- Serialized execution (one worker thread each, or a blocked loop)
- would need 4 x AI_SLEEP = 2.4s; genuine overlap lands near AI_SLEEP.
- """
- t0 = time.monotonic()
- responses = await asyncio.gather(
- *(_summarize(client, f"note-{i}") for i in range(4))
- )
- elapsed = time.monotonic() - t0
-
- assert all(r.status_code == 200 for r in responses), [
- r.text for r in responses
+ """All four model calls start before any one is allowed to finish."""
+ fake_ai["expected"] = 4
+ tasks = [
+ asyncio.create_task(_summarize(client, f"note-{i}")) for i in range(4)
]
- assert len(fake_ai["calls"]) == 4
- # Every stub really held for the full sleep.
- for call in fake_ai["calls"]:
- assert call["finished"] - call["started"] >= AI_SLEEP * 0.9
- # ...yet the batch finished far below the serialized bound.
- assert elapsed >= AI_SLEEP
- assert elapsed < 4 * AI_SLEEP * 0.6, (
- f"batch took {elapsed:.2f}s — requests appear to be serialized"
- )
+ try:
+ await asyncio.wait_for(fake_ai["started"].wait(), timeout=10)
+ assert len(fake_ai["calls"]) == 4
+ assert all(call["finished"] is None for call in fake_ai["calls"])
+ finally:
+ fake_ai["release"].set()
+ responses = await asyncio.gather(*tasks, return_exceptions=True)
+ assert all(
+ not isinstance(response, BaseException) and response.status_code == 200
+ for response in responses
+ ), responses
# ---------------------------------------------------------------------------
@@ -214,51 +216,29 @@ async def test_concurrent_ai_requests_overlap(client, fake_ai):
async def test_sync_route_stays_responsive_while_ai_requests_in_flight(
client, fake_ai
):
- """/api/v1/health (sync def, served from the same AnyIO pool the old
- sync AI handlers would have pinned) answers while MORE AI requests
- than worker tokens are all awaiting their model stub simultaneously.
+ """The sync health route answers while all 45 model calls remain held.
- 45 in-flight AI requests with a 1.5s stub each: the old model pins
- all ~40 worker tokens for 1.5s and queues the health route behind
- them; the async model frees the pool and health answers at once.
+ Wait for the actual calls, rather than assuming their middleware and
+ database work finished within a fixed fraction of a second. The bounded
+ wait still fails if model calls retain the pool's ~40 worker tokens.
"""
- fake_ai["sleep"] = 1.5
- t0 = time.monotonic()
+ fake_ai["expected"] = 45
tasks = [
asyncio.create_task(_summarize(client, f"note-{i}")) for i in range(45)
]
- # Give every request time to traverse the middleware stack and reach
- # the sleeping stub — well within its 1.5s hold.
- await asyncio.sleep(0.5)
-
- # All 45 model holds are now simultaneously in flight (none can have
- # finished: the stub sleeps 1.5s and only 0.5s have passed).
- assert len(fake_ai["calls"]) == 45, (
- f"only {len(fake_ai['calls'])}/45 AI calls reached the stub — "
- "concurrency premise not established"
- )
- assert all(c["finished"] is None for c in fake_ai["calls"])
-
- t_ping = time.monotonic()
- ping = await client.get("/api/v1/health")
- ping_elapsed = time.monotonic() - t_ping
-
- assert ping.status_code == 200, ping.text
- assert ping_elapsed < 1.0, (
- f"sync health route took {ping_elapsed:.2f}s while AI requests "
- "held no workers — it should answer immediately"
- )
-
- responses = await asyncio.gather(*tasks)
- total = time.monotonic() - t0
- assert all(r.status_code == 200 for r in responses), [
- r.text for r in responses
- ]
- # The health answer landed while the whole batch was still in flight.
- assert t_ping + ping_elapsed < t0 + total - 0.5
- # Sanity: the batch itself overlapped (45 sequential 1.5s holds would
- # be ~67s; 40-token-limited waves would be >= 3s).
- assert total < 3.0, (
- f"AI batch took {total:.2f}s — holds appear to be queuing on the "
- "worker pool rather than awaiting on the loop"
- )
+ try:
+ await asyncio.wait_for(fake_ai["started"].wait(), timeout=10)
+ assert len(fake_ai["calls"]) == 45
+ assert all(call["finished"] is None for call in fake_ai["calls"])
+ ping = await asyncio.wait_for(client.get("/api/v1/health"), timeout=10)
+ assert ping.status_code == 200, ping.text
+ assert all(call["finished"] is None for call in fake_ai["calls"])
+ finally:
+ # Drain every request even if the barrier or health check fails;
+ # pending ASGI work must not race database/session fixture teardown.
+ fake_ai["release"].set()
+ responses = await asyncio.gather(*tasks, return_exceptions=True)
+ assert all(
+ not isinstance(response, BaseException) and response.status_code == 200
+ for response in responses
+ ), responses